Skip to content

Commit

Permalink
fix: add support for cloudflare worker
Browse files Browse the repository at this point in the history
  • Loading branch information
2fd committed Apr 4, 2024
1 parent d72d7f2 commit df5b940
Show file tree
Hide file tree
Showing 13 changed files with 2,659 additions and 27 deletions.
33 changes: 27 additions & 6 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,13 @@ jobs:

needs: build

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

steps:
- uses: actions/checkout@v4

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

- uses: actions/download-artifact@v4
with:
Expand All @@ -135,3 +131,28 @@ 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

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
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.7` |
| Bun | `import { RRegex, RRegexSet } from '@rregex/rregex'` | `>=1.10.7` |
| 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
175 changes: 175 additions & 0 deletions test/cf/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
\*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
\*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

\*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

\*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.cache
.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

.cache/

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp
.cache

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.\*

# wrangler project

.dev.vars
.wrangler/
cf.mjs
web.js
rregex.wasm
6 changes: 6 additions & 0 deletions test/cf/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"printWidth": 140,
"singleQuote": true,
"semi": true,
"useTabs": true
}
Loading

0 comments on commit df5b940

Please sign in to comment.