Skip to content

Commit

Permalink
Merge pull request #64 from 2fd/fix/add-support-for-cloudflare
Browse files Browse the repository at this point in the history
fix: add support for cloudflare worker
  • Loading branch information
2fd authored Apr 4, 2024
2 parents d72d7f2 + 273a3d2 commit ad5d8a2
Show file tree
Hide file tree
Showing 14 changed files with 2,695 additions and 25 deletions.
33 changes: 31 additions & 2 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ jobs:

strategy:
matrix:
version: ["v1.x"]
version: ["1.x"]

steps:
- uses: actions/checkout@v4

- name: Run denoland/setup-deno@v1 (${{ matrix.version }})
uses: denoland/setup-deno@v1
with:
deno-version: ${{ matrix.version }}
deno-version: "v${{ matrix.version }}"

- uses: actions/download-artifact@v4
with:
Expand All @@ -135,3 +135,32 @@ jobs:
- run: deno run --allow-read=lib/rregex.wasm lib/esm.mjs

- run: deno test --allow-read=lib/rregex.wasm test/deno.test.mjs

test_cf:
runs-on: ubuntu-20.04

needs: build

strategy:
matrix:
version: ["3.x"]

steps:
- uses: actions/checkout@v4

- name: Run actions/setup-node@v4
uses: actions/setup-node@v4
with:
node-version: "20"
registry-url: https://registry.npmjs.org/

- uses: actions/download-artifact@v4
with:
name: lib
path: lib

- run: npm ci
working-directory: ./test/cf

- run: npm test
working-directory: ./test/cf
34 changes: 32 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ jobs:

strategy:
matrix:
version: ["v1.x"]
version: ["1.x"]

steps:
- uses: actions/checkout@v4

- name: Run denoland/setup-deno@v1 (${{ matrix.version }})
uses: denoland/setup-deno@v1
with:
deno-version: ${{ matrix.version }}
deno-version: "v${{ matrix.version }}"

- uses: actions/download-artifact@v4
with:
Expand All @@ -136,6 +136,35 @@ jobs:

- run: deno test --allow-read=lib/rregex.wasm test/deno.test.mjs

test_cf:
runs-on: ubuntu-20.04

needs: build

strategy:
matrix:
version: ["3.x"]

steps:
- uses: actions/checkout@v4

- name: Run actions/setup-node@v4
uses: actions/setup-node@v4
with:
node-version: "20"
registry-url: https://registry.npmjs.org/

- uses: actions/download-artifact@v4
with:
name: lib
path: lib

- run: npm ci
working-directory: ./test/cf

- run: npm test
working-directory: ./test/cf

publish:
runs-on: ubuntu-20.04

Expand All @@ -149,6 +178,7 @@ jobs:
- test_node
- test_bun
- test_deno
- test_cf

steps:
- uses: actions/checkout@v4
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ Rust has a powerful Regex library with a lot of features that don't exists en th

This package includes builds for multiple runtimes

| Runtime | Import |
| ------------------ | ---------------------------------------------------- |
| Node.js (esm) | `import { RRegex, RRegexSet } from 'rregex'` |
| Node.js (commonjs) | `const { RRegex, RRegexSet } = require('rregex')` |
| Deno | `import { RRegex, RRegexSet } from '@rregex/rregex'` |
| Bun | `import { RRegex, RRegexSet } from '@rregex/rregex'` |
| Cloudflare Workers | TODO |
| Browser | TODO |
| Standalone | TODO |
| Runtime | Import | version |
| ------------------ | ------------------------------------------------------- | ---------- |
| Node.js (esm) | `import { RRegex, RRegexSet } from 'rregex'` | `*` |
| Node.js (commonjs) | `const { RRegex, RRegexSet } = require('rregex')` | `*` |
| Deno | `import { RRegex, RRegexSet } from '@rregex/rregex'` | `>=1.10.8` |
| Bun | `import { RRegex, RRegexSet } from '@rregex/rregex'` | `>=1.10.8` |
| Cloudflare Workers | `import { RRegex, RRegexSet } from 'rregex/lib/cf.mjs'` | `>=1.10.8` |
| Browser | TODO | |
| Standalone | TODO | |

## Known Issues

Expand Down
29 changes: 18 additions & 11 deletions bin/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

echo " 🚀 Building..."
concurrently \
-c "cyan,blue,green,yellow,red" \
-n "std,web,cjs,esm,mod" \
-c "cyan,blue,green,yellow" \
-n "std,web,cjs,esm" \
"wasm-pack build -d lib_no_modules --release --target no-modules" \
"wasm-pack build -d lib_web --release --target web" \
"wasm-pack build -d lib_nodejs --release --target nodejs" \
"wasm-pack build -d lib_bundler --release --target bundler" \
"wasm-pack build -d lib_deno --release --target deno"

function replace() {
Expand All @@ -34,26 +33,37 @@ for t in lib_*/*.d.ts; do
replace 's/matches(text: string): any\[\];/matches(text: string): number[];/g' $t
done

echo " 📦 Bundling Cloudflare bundle..."
mkdir -p lib_cf

echo '' > lib_cf/cf.mjs
echo 'import wasm from "./rregex.wasm";' >> lib_cf/cf.mjs
echo 'import { initSync } from "./web.js";' >> lib_cf/cf.mjs
echo 'export * from "./web.js";' >> lib_cf/cf.mjs
echo '' >> lib_cf/cf.mjs
echo 'initSync(wasm);' >> lib_cf/cf.mjs
echo '' >> lib_cf/cf.mjs

echo " 📝 Adding metadata..."
node bin/medatata.mjs

echo " 🔨 Creating lib..."
mkdir -p lib
cp lib_web/rregex.js lib/web.js
cp lib_web/rregex.d.ts lib/web.d.ts
cp lib_web/rregex.d.ts lib/cf.d.ts
cp lib_web/rregex.d.ts lib/types.d.ts
cp lib_web/rregex_bg.wasm lib/rregex.wasm
cp lib_web/rregex_bg.wasm.d.ts lib/rregex.wasm.d.ts

cp lib_cf/cf.mjs lib/cf.mjs

cp lib_nodejs/rregex.js lib/commonjs.cjs
cp lib_nodejs/rregex.d.ts lib/commonjs.d.ts

cp lib_no_modules/rregex.js lib/standalone.js
cp lib_no_modules/rregex.d.ts lib/standalone.d.ts

cp lib_bundler/rregex.js lib/bundler.mjs
cp lib_bundler/rregex_bg.js lib/bundler_bg.mjs
cp lib_bundler/rregex.d.ts lib/bundler.d.ts

cp lib_deno/rregex.d.ts lib/esm.d.ts
echo -e "import { readFile } from \"node:fs/promises\";\n$(cat lib_deno/rregex.js)" > lib/esm.mjs

Expand All @@ -66,17 +76,14 @@ replace 's/__wbindgen_placeholder__/wbg/g' lib/commonjs.cjs
replace 's/\\\.js\$/standalone\\.js$/g' lib/standalone.js
replace 's/_bg\.wasm/rregex.wasm/g' lib/standalone.js

replace 's/rregex_bg\.wasm/rregex.wasm/g' lib/bundler.mjs
replace 's/rregex_bg\.js/bundler_bg.mjs/g' lib/bundler.mjs

replace 's/rregex_bg\.wasm/rregex.wasm/g' lib/esm.mjs
replace 's/__wbindgen_placeholder__/wbg/g' lib/esm.mjs
replace 's/Deno\.readFile/readFile/g' lib/esm.mjs

echo " 🧹 Removing build files..."
rm -rf lib_web
rm -rf lib_nodejs
rm -rf lib_bundler
rm -rf lib_cf
rm -rf lib_deno
rm -rf lib_no_modules
if [[ "$OSTYPE" == "darwin"* ]]; then
Expand Down
2 changes: 1 addition & 1 deletion bin/medatata.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ function append(path, data) {
append(`./lib_web/rregex.d.ts`, `export const metadata: ${metadata}`)
append(`./lib_web/rregex.js`, `export const metadata = ${metadata}`)
append(`./lib_deno/rregex.js`, `export const metadata = ${metadata}`)
append(`./lib_bundler/rregex.js`, `export const metadata = ${metadata}`)
append(`./lib_cf/cf.mjs`, `export const metadata = ${metadata}`)
append(`./lib_nodejs/commonjs.cjs`, `module.exports.metadata = ${metadata}`)
12 changes: 12 additions & 0 deletions test/cf/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# http://editorconfig.org
root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.yml]
indent_style = space
Loading

0 comments on commit ad5d8a2

Please sign in to comment.