@re2tools/re2-wasm-esm 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,177 @@
1
+ # re2-wasm-esm — Full documentation
2
+
3
+ Standalone ESM build of [Google RE2](https://github.com/google/re2) as WebAssembly with **browser and Node** support. No dependency on the `re2-wasm` npm package; all code and artifacts live in this repo.
4
+
5
+ ---
6
+
7
+ ## 1. Overview
8
+
9
+ - **What it is**: RE2 regex engine compiled to WebAssembly, with an ESM API and async init for browser and Node.
10
+ - **Why it exists**: [re2-wasm](https://github.com/google/re2-wasm) is Node-only (CommonJS, sync). This package is a standalone fork: ESM, browser + Node, with everything vendored so nothing is pulled from re2-wasm at build or runtime.
11
+ - **License**: Apache-2.0 (same as re2-wasm).
12
+
13
+ ---
14
+
15
+ ## 2. Package layout
16
+
17
+ | Path | Purpose |
18
+ |------|--------|
19
+ | **`src/`** | TypeScript source. `loader.ts` loads WASM; `re2-wasm/` contains the vendored RE2 class and types (from google/re2-wasm). |
20
+ | **`wasm/`** | **Build-time only.** Committed artifacts: `re2.wasm` (compiled RE2) and `re2-glue.js` (Emscripten glue). The build script copies these into `dist/`. |
21
+ | **`dist/`** | **Runtime.** Output of `tsc` plus copied `re2.wasm` and `re2-glue.js`. This is what gets published and what Node/browser use at runtime. |
22
+ | **`scripts/`** | `copy-wasm.cjs` copies `wasm/re2.wasm` and `wasm/re2-glue.js` into `dist/`; invoked by the `build` script. |
23
+
24
+ **Exports:** The package exposes only `dist/` (and README). `main` / `module` / `types` point to `dist/index.js` and `dist/index.d.ts`.
25
+
26
+ ---
27
+
28
+ ## 3. Build
29
+
30
+ ```bash
31
+ npm run build
32
+ ```
33
+
34
+ - Runs `tsc` (emits JS and `.d.ts` into `dist/`).
35
+ - Copies `wasm/re2.wasm` and `wasm/re2-glue.js` into `dist/`.
36
+ If `wasm/re2.wasm` is missing, the build throws.
37
+
38
+ **Publish:** `prepublishOnly` runs `npm run build`, so published tarballs always include a built `dist/` with WASM and glue.
39
+
40
+ ---
41
+
42
+ ## 4. The `wasm/` folder
43
+
44
+ ### What it is
45
+
46
+ - **`re2.wasm`** — The compiled RE2 engine. All regex behavior (match, exec, etc.) runs inside this binary.
47
+ - **`re2-glue.js`** — Emscripten-generated JS that loads the WASM, creates the `Module` with `WrappedRE2`, and bridges C++ to JS.
48
+
49
+ ### When it’s used
50
+
51
+ - **Build time only.** The build copies these two files into `dist/`. Runtime (Node and browser) always uses the **`dist/`** copies, never reads from `wasm/` directly.
52
+
53
+ ### Can the package work without `wasm/`?
54
+
55
+ **No.** Without the WASM binary and glue there is no RE2 engine. Removing `wasm/` would mean nothing to copy to `dist/`, so Node would fail reading the files and the browser would fail fetching them. The package fundamentally depends on these artifacts; `wasm/` is simply where this repo stores them.
56
+
57
+ ### Does `wasm/` affect browser vs Node?
58
+
59
+ **No.** Both environments use the same files that end up in `dist/`. Only the loading mechanism differs: Node reads from disk (`fs`), browser fetches by URL.
60
+
61
+ ---
62
+
63
+ ## 5. Runtime behavior
64
+
65
+ ### Node
66
+
67
+ - `init()` uses `__dirname` relative to the **compiled** loader (i.e. `dist/`).
68
+ - It reads `dist/re2.wasm` and `dist/re2-glue.js` from the filesystem, preloads the binary into `Module.wasmBinary`, and runs the glue so the Emscripten runtime uses sync instantiation and `onRuntimeInitialized` runs.
69
+
70
+ ### Browser
71
+
72
+ - `init()` fetches `re2.wasm` and `re2-glue.js` from URLs (by default derived from `import.meta.url`, or you pass `baseUrl` / `wasmUrl` / `glueUrl`).
73
+ - Those URLs must point to the same files (typically the ones from `dist/`, copied to your app’s public or CDN).
74
+
75
+ Same code path for both; only the way the binary and glue are obtained differs.
76
+
77
+ ---
78
+
79
+ ## 6. Install and usage
80
+
81
+ ### Install
82
+
83
+ ```bash
84
+ npm install @re2tools/re2-wasm-esm
85
+ ```
86
+
87
+ ### Node (ESM)
88
+
89
+ ```js
90
+ import { init, getRE2 } from "@re2tools/re2-wasm-esm";
91
+
92
+ await init();
93
+ const RE2 = getRE2();
94
+ const re = new RE2("a(b*)", "u");
95
+ console.log(re.exec("abbc")); // [ "abb", "bb", ... ]
96
+ ```
97
+
98
+ ### Browser
99
+
100
+ 1. Ensure `dist/re2.wasm` and `dist/re2-glue.js` are served (same origin or pass URLs to `init()`).
101
+ 2. Call `await init()` before any RE2 use.
102
+
103
+ ```js
104
+ import { init, getRE2 } from "@re2tools/re2-wasm-esm";
105
+
106
+ await init();
107
+ const RE2 = getRE2();
108
+ const re = new RE2("a(b*)", "u");
109
+ re.test("abbc"); // true
110
+ ```
111
+
112
+ With a bundler (Vite, Webpack, etc.), either copy `node_modules/@re2tools/re2-wasm-esm/dist/re2.wasm` and `re2-glue.js` to your public/output directory, or pass a base URL:
113
+
114
+ ```js
115
+ await init({ baseUrl: "https://your-cdn.com/@re2tools/re2-wasm-esm/" });
116
+ ```
117
+
118
+ ---
119
+
120
+ ## 7. API reference
121
+
122
+ | Export | Description |
123
+ |--------|-------------|
124
+ | **`init(options?)`** | Initializes the WASM runtime. In browser, loads WASM and glue. Resolves when ready. Call once before `getRE2()`. |
125
+ | **`getRE2()`** | Returns the RE2 class. Throws if `init()` has not been called. |
126
+ | **`whenReady()`** | Returns a Promise that resolves to the WASM module when ready (for advanced use). |
127
+ | **`getModule()`** | Returns the low-level WASM module (e.g. `WrappedRE2`). Throws if not ready. |
128
+
129
+ **`init` options (all optional):**
130
+
131
+ - **`baseUrl`** — Base URL for WASM and glue in browser (e.g. `"https://cdn.example.com/@re2tools/re2-wasm-esm/"`).
132
+ - **`wasmUrl`** — Full URL for `re2.wasm` (overrides `baseUrl` for WASM only).
133
+ - **`glueUrl`** — Full URL for `re2-glue.js` (overrides `baseUrl` for glue only).
134
+
135
+ **RE2 API:** Matches a `RegExp`-like interface: `exec()`, `test()`, `match()`, `replace()`, `search()`, `split()`, flags `g`, `i`, `m`, `u`, etc. RE2 requires the **`u`** (unicode) flag.
136
+
137
+ **Types:** `RE2ExecArray`, `RE2MatchArray`, `Re2WasmModule` are exported from the package.
138
+
139
+ ---
140
+
141
+ ## 8. Using as common utils in a monolith
142
+
143
+ You can use this package only inside the repo (e.g. as a workspace dependency) without publishing to npm.
144
+
145
+ 1. **Add a workspace dependency** in the app or package that needs RE2:
146
+ ```json
147
+ "@re2tools/re2-wasm-esm": "workspace:*"
148
+ ```
149
+ 2. **Build the package** so `dist/` exists (e.g. from repo root: `pnpm --filter @re2tools/re2-wasm-esm build`, or as a pre-step of the app build).
150
+ 3. **In code:** `import { init, getRE2 } from "@re2tools/re2-wasm-esm";` then `await init();` and `getRE2()` as in the examples above.
151
+ 4. **Browser:** Ensure `dist/re2.wasm` and `dist/re2-glue.js` from the package are served (copy to public/output or set `init({ baseUrl: "…" })`).
152
+
153
+ Using the TypeScript source “directly” (e.g. importing from `@re2tools/re2-wasm-esm/src`) doesn’t remove the need for the WASM and glue: the loader still expects to read or fetch those files at runtime, so they must still be produced (e.g. by the same copy step) and available from the right path.
154
+
155
+ ---
156
+
157
+ ## 9. Updating the WASM (maintainers)
158
+
159
+ To refresh from upstream:
160
+
161
+ 1. Build [google/re2-wasm](https://github.com/google/re2-wasm).
162
+ 2. Copy its `build/wasm/re2.wasm` and `build/wasm/re2.js` into this repo’s **`wasm/`** as **`re2.wasm`** and **`re2-glue.js`**.
163
+ 3. Run `npm run build` and test.
164
+
165
+ Until you do this, the package does not change when upstream re2-wasm changes.
166
+
167
+ ---
168
+
169
+ ## 10. Summary
170
+
171
+ | Topic | Summary |
172
+ |-------|--------|
173
+ | **Layout** | `src/` = TS; `wasm/` = WASM + glue (build-time source); `dist/` = built output; `scripts/copy-wasm.cjs` = copy wasm → dist. |
174
+ | **Build** | `tsc` + copy `wasm/*` → `dist/`. |
175
+ | **wasm/** | Required. Holds the only source of the binary and glue; without it the package cannot work. |
176
+ | **Node vs browser** | Both use `dist/`; Node reads files from disk, browser fetches by URL. |
177
+ | **Common utils** | Use as a workspace dependency; build the package and serve `dist/re2.wasm` and `dist/re2-glue.js` in the browser when needed. |
package/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # re2-wasm-esm
2
+
3
+ Standalone ESM build of [Google RE2](https://github.com/google/re2) as WebAssembly with **browser and Node** support. **No dependency on the re2-wasm package** — all code and artifacts live in this repo.
4
+
5
+ 📄 **[Full documentation](DOCUMENTATION.md)** — package layout, `wasm/` role, build, runtime (Node vs browser), API, using as common utils, updating upstream.
6
+
7
+ - **Source**: RE2 class and types are vendored under `src/re2-wasm/` (from [google/re2-wasm/src](https://github.com/google/re2-wasm/tree/main/src), Apache-2.0).
8
+ - **WASM**: `wasm/re2.wasm` and `wasm/re2-glue.js` (Emscripten glue) are committed in `wasm/`. Build copies them into `dist/` for publish and runtime.
9
+
10
+ So if upstream re2-wasm changes, this package does not change until you explicitly update the vendored files.
11
+
12
+ **Runtime:**
13
+
14
+ - **Browser**: Loads the committed WASM and glue asynchronously; call `await init()` before using RE2.
15
+ - **Node**: Uses the same committed WASM and glue from `dist/` (same as browser).
16
+
17
+ ## Install
18
+
19
+ ```bash
20
+ npm install @re2tools/re2-wasm-esm
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ ### Node (ESM)
26
+
27
+ ```js
28
+ import { init, getRE2 } from "@re2tools/re2-wasm-esm";
29
+
30
+ await init();
31
+ const RE2 = getRE2();
32
+ const re = new RE2("a(b*)", "u");
33
+ console.log(re.exec("abbc")); // [ "abb", "bb", ... ]
34
+ ```
35
+
36
+ ### Browser
37
+
38
+ 1. Ensure `dist/re2.wasm` and `dist/re2-glue.js` are served from the same origin (or pass URLs to `init()`).
39
+ 2. Call `await init()` before any RE2 use.
40
+
41
+ ```js
42
+ import { init, getRE2 } from "@re2tools/re2-wasm-esm";
43
+
44
+ await init(); // loads WASM; required in browser
45
+ const RE2 = getRE2();
46
+ const re = new RE2("a(b*)", "u");
47
+ re.test("abbc"); // true
48
+ ```
49
+
50
+ With a bundler (Vite, Webpack, etc.), copy `node_modules/@re2tools/re2-wasm-esm/dist/re2.wasm` and `re2-glue.js` to your public/output directory, or pass a base URL:
51
+
52
+ ```js
53
+ await init({ baseUrl: "https://your-cdn.com/@re2tools/re2-wasm-esm/" });
54
+ ```
55
+
56
+ ### API
57
+
58
+ - **`init(options?: { wasmUrl?, glueUrl?, baseUrl? })`**
59
+ Initializes the runtime. In browser, loads WASM and glue script. Resolves when ready. Call once before `getRE2()`.
60
+
61
+ - **`getRE2()`**
62
+ Returns the RE2 class. Throws if `init()` has not been called.
63
+
64
+ - **`whenReady()`**
65
+ Returns a Promise that resolves to the WASM module when ready (for advanced use).
66
+
67
+ - **`getModule()`**
68
+ Returns the low-level WASM module (e.g. `WrappedRE2`). Throws if not ready.
69
+
70
+ RE2 API matches the standard `RegExp`-like interface: `exec()`, `test()`, `match()`, `replace()`, `search()`, `split()`, flags `g`, `i`, `m`, `u`, etc. RE2 requires the **`u`** (unicode) flag.
71
+
72
+ ## Why
73
+
74
+ - [re2-wasm](https://github.com/google/re2-wasm) is Node-only (sync WASM, CommonJS). This package is a **standalone** fork: ESM, browser + Node, with all needed code and WASM committed here so nothing is pulled from re2-wasm at build or runtime.
75
+
76
+ ### Updating the WASM (maintainers)
77
+
78
+ To refresh from upstream: build [google/re2-wasm](https://github.com/google/re2-wasm) and copy its `build/wasm/re2.wasm` and `build/wasm/re2.js` into this repo’s `wasm/` as `re2.wasm` and `re2-glue.js`.
79
+
80
+ ## License
81
+
82
+ Apache-2.0 (same as re2-wasm).
@@ -0,0 +1,18 @@
1
+ /**
2
+ * re2-wasm-esm — Google RE2 as WASM, ESM + browser support.
3
+ * Uses vendored source from https://github.com/google/re2-wasm (Apache-2.0).
4
+ */
5
+ export { init, getModule, whenReady } from "./loader.js";
6
+ export type { Re2WasmModule } from "./loader.js";
7
+ export type { RE2ExecArray, RE2MatchArray } from "./re2-wasm/re2.js";
8
+ /**
9
+ * Returns the RE2 class. Must call await init() first.
10
+ *
11
+ * @example
12
+ * await init();
13
+ * const RE2 = getRE2();
14
+ * const re = new RE2("a(b*)", "u");
15
+ * re.exec("abbc");
16
+ */
17
+ export declare function getRE2(): (new (pattern: string | RegExp, flags?: string) => import("./re2-wasm/re2.js").RE2);
18
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACzD,YAAY,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACjD,YAAY,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAIrE;;;;;;;;GAQG;AACH,wBAAgB,MAAM,IAAI,CAAC,KACzB,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,KAAK,CAAC,EAAE,MAAM,KACX,OAAO,mBAAmB,EAAE,GAAG,CAAC,CAUpC"}
package/dist/index.js ADDED
@@ -0,0 +1,21 @@
1
+ /**
2
+ * re2-wasm-esm — Google RE2 as WASM, ESM + browser support.
3
+ * Uses vendored source from https://github.com/google/re2-wasm (Apache-2.0).
4
+ */
5
+ export { init, getModule, whenReady } from "./loader.js";
6
+ import { resolvedRE2Class } from "./loader.js";
7
+ /**
8
+ * Returns the RE2 class. Must call await init() first.
9
+ *
10
+ * @example
11
+ * await init();
12
+ * const RE2 = getRE2();
13
+ * const re = new RE2("a(b*)", "u");
14
+ * re.exec("abbc");
15
+ */
16
+ export function getRE2() {
17
+ if (!resolvedRE2Class) {
18
+ throw new Error("RE2 not ready. Call await init() before using getRE2().");
19
+ }
20
+ return resolvedRE2Class;
21
+ }
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Async loader for RE2 WASM. Uses vendored re2-wasm build (wasm + Emscripten glue).
3
+ * Same code path for Node and browser; no dependency on re2-wasm at runtime.
4
+ * (C) Port for ESM/browser — original re2-wasm by Google (Apache-2.0).
5
+ */
6
+ declare global {
7
+ interface Window {
8
+ Module?: Record<string, unknown>;
9
+ }
10
+ var Module: Record<string, unknown> | undefined;
11
+ }
12
+ /** Set by init(): the RE2 class (vendored from re2-wasm). */
13
+ export declare let resolvedRE2Class: unknown;
14
+ export interface Re2WasmModule {
15
+ WrappedRE2: new (pattern: string, ignoreCase: boolean, multiline: boolean, dotAll: boolean) => {
16
+ ok(): boolean;
17
+ error(): string;
18
+ pattern(): string;
19
+ match(input: string, start: number, getCapturingGroups: boolean): {
20
+ match: string;
21
+ index: number;
22
+ groups: (string | undefined)[];
23
+ };
24
+ capturingGroupNames(): {
25
+ keys(): {
26
+ get(i: number): number;
27
+ size(): number;
28
+ };
29
+ get(key: number): string;
30
+ };
31
+ };
32
+ }
33
+ /**
34
+ * Initialize the RE2 WASM runtime. Must be called before using RE2.
35
+ *
36
+ * @param options - Optional: wasmUrl, glueUrl (browser), or baseUrl
37
+ */
38
+ export declare function init(options?: {
39
+ wasmUrl?: string;
40
+ glueUrl?: string;
41
+ baseUrl?: string;
42
+ }): Promise<void>;
43
+ export declare function getModule(): Re2WasmModule;
44
+ export declare function whenReady(): Promise<Re2WasmModule>;
45
+ //# sourceMappingURL=loader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;KAClC;IAED,IAAI,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC;CACjD;AAYD,6DAA6D;AAC7D,eAAO,IAAI,gBAAgB,EAAE,OAAc,CAAC;AAE5C,MAAM,WAAW,aAAa;IAC5B,UAAU,EAAE,KACV,OAAO,EAAE,MAAM,EACf,UAAU,EAAE,OAAO,EACnB,SAAS,EAAE,OAAO,EAClB,MAAM,EAAE,OAAO,KACZ;QACH,EAAE,IAAI,OAAO,CAAC;QACd,KAAK,IAAI,MAAM,CAAC;QAChB,OAAO,IAAI,MAAM,CAAC;QAClB,KAAK,CACH,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,kBAAkB,EAAE,OAAO,GAC1B;YAAE,KAAK,EAAE,MAAM,CAAC;YAAC,KAAK,EAAE,MAAM,CAAC;YAAC,MAAM,EAAE,CAAC,MAAM,GAAG,SAAS,CAAC,EAAE,CAAA;SAAE,CAAC;QACpE,mBAAmB,IAAI;YAAE,IAAI,IAAI;gBAAE,GAAG,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;gBAAC,IAAI,IAAI,MAAM,CAAA;aAAE,CAAC;YAAC,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;SAAE,CAAC;KACzG,CAAC;CACH;AAoGD;;;;GAIG;AACH,wBAAsB,IAAI,CAAC,OAAO,CAAC,EAAE;IACnC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,GAAG,OAAO,CAAC,IAAI,CAAC,CAoChB;AAED,wBAAgB,SAAS,IAAI,aAAa,CAOzC;AAED,wBAAgB,SAAS,IAAI,OAAO,CAAC,aAAa,CAAC,CAMlD"}
package/dist/loader.js ADDED
@@ -0,0 +1,146 @@
1
+ /**
2
+ * Async loader for RE2 WASM. Uses vendored re2-wasm build (wasm + Emscripten glue).
3
+ * Same code path for Node and browser; no dependency on re2-wasm at runtime.
4
+ * (C) Port for ESM/browser — original re2-wasm by Google (Apache-2.0).
5
+ */
6
+ const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
7
+ const isNode = typeof process !== "undefined" &&
8
+ typeof process.versions !== "undefined" &&
9
+ typeof process.versions.node === "string";
10
+ let ready = null;
11
+ let resolvedModule = null;
12
+ /** Set by init(): the RE2 class (vendored from re2-wasm). */
13
+ export let resolvedRE2Class = null;
14
+ /**
15
+ * Resolve base URL for WASM and glue script (browser).
16
+ */
17
+ function getBaseUrl() {
18
+ if (typeof import.meta !== "undefined" && import.meta.url) {
19
+ const u = import.meta.url;
20
+ return u.slice(0, u.lastIndexOf("/") + 1);
21
+ }
22
+ return "";
23
+ }
24
+ /**
25
+ * Load and run the RE2 WASM runtime (vendored wasm + glue from re2-wasm).
26
+ * In Node we preload wasm into Module.wasmBinary so the Emscripten glue uses sync
27
+ * instantiation and onRuntimeInitialized runs. In browser we use async instantiateWasm.
28
+ */
29
+ function createLoader(getWasmBuffer, getGlueScript, nodeGlueContext) {
30
+ return new Promise((resolve, rej) => {
31
+ function onReady() {
32
+ resolvedModule = globalThis.Module ?? Module;
33
+ // Defer so Emscripten glue stack unwinds (Node). Use queueMicrotask so
34
+ // the loader's dynamic import runs after the current sync glue stack.
35
+ const doResolve = () => {
36
+ const re2Url = typeof import.meta.url !== "undefined"
37
+ ? new URL("./re2-wasm/re2.js", import.meta.url).href
38
+ : "./re2-wasm/re2.js";
39
+ import(/* @vite-ignore */ re2Url)
40
+ .then((m) => {
41
+ resolvedRE2Class = m.RE2;
42
+ resolve(resolvedModule);
43
+ })
44
+ .catch(rej);
45
+ };
46
+ queueMicrotask(doResolve);
47
+ }
48
+ const Module = {
49
+ locateFile(path) {
50
+ if (nodeGlueContext && path === "re2.wasm")
51
+ return nodeGlueContext.wasmPath;
52
+ return path;
53
+ },
54
+ onRuntimeInitialized: onReady,
55
+ print: (typeof console !== "undefined" && console.log) ? (x) => console.log(x) : undefined,
56
+ printErr: (typeof console !== "undefined" && console.warn) ? (x) => console.warn(x) : undefined,
57
+ };
58
+ if (!nodeGlueContext) {
59
+ Module.instantiateWasm = function (imports, successCallback) {
60
+ getWasmBuffer()
61
+ .then((buffer) => WebAssembly.instantiate(buffer, imports))
62
+ .then(({ instance }) => successCallback(instance))
63
+ .catch(rej);
64
+ };
65
+ }
66
+ globalThis.Module = Module;
67
+ const runGlue = async (scriptText) => {
68
+ try {
69
+ if (isNode && nodeGlueContext) {
70
+ const { createRequire } = await import("module");
71
+ const req = createRequire(import.meta.url);
72
+ globalThis.require = req;
73
+ const fn = new Function("__dirname", "__filename", scriptText);
74
+ fn(nodeGlueContext.__dirname, nodeGlueContext.__filename);
75
+ // Emscripten glue can overwrite Module['onRuntimeInitialized'] before doRun();
76
+ // invoke our callback now that the runtime is ready.
77
+ onReady();
78
+ }
79
+ else {
80
+ const fn = new Function(scriptText);
81
+ fn();
82
+ }
83
+ }
84
+ catch (e) {
85
+ rej(e);
86
+ }
87
+ };
88
+ if (isNode && nodeGlueContext) {
89
+ getWasmBuffer()
90
+ .then((buffer) => {
91
+ Module.wasmBinary = buffer;
92
+ return getGlueScript();
93
+ })
94
+ .then(runGlue)
95
+ .catch(rej);
96
+ }
97
+ else {
98
+ getGlueScript().then(runGlue).catch(rej);
99
+ }
100
+ });
101
+ }
102
+ /**
103
+ * Initialize the RE2 WASM runtime. Must be called before using RE2.
104
+ *
105
+ * @param options - Optional: wasmUrl, glueUrl (browser), or baseUrl
106
+ */
107
+ export async function init(options) {
108
+ if (resolvedModule)
109
+ return;
110
+ if (isNode) {
111
+ const { readFile } = await import("fs/promises");
112
+ const { fileURLToPath } = await import("url");
113
+ const path = await import("path");
114
+ const __filename = fileURLToPath(import.meta.url);
115
+ const __dirname = path.dirname(__filename);
116
+ const wasmPath = path.join(__dirname, "re2.wasm");
117
+ const gluePath = path.join(__dirname, "re2-glue.js");
118
+ ready = createLoader(() => readFile(wasmPath).then((b) => b.buffer), () => readFile(gluePath, "utf8"), { __dirname, __filename: gluePath, wasmPath });
119
+ await ready;
120
+ return;
121
+ }
122
+ if (isBrowser) {
123
+ if (!ready) {
124
+ const base = options?.baseUrl ?? getBaseUrl();
125
+ const wasmUrl = options?.wasmUrl ?? base + "re2.wasm";
126
+ const glueUrl = options?.glueUrl ?? base + "re2-glue.js";
127
+ ready = createLoader(() => fetch(wasmUrl).then((r) => r.arrayBuffer()), () => fetch(glueUrl).then((r) => r.text()));
128
+ }
129
+ await ready;
130
+ return;
131
+ }
132
+ throw new Error("Unsupported environment: not Node and not browser");
133
+ }
134
+ export function getModule() {
135
+ if (resolvedModule && typeof resolvedModule.WrappedRE2 === "function") {
136
+ return resolvedModule;
137
+ }
138
+ throw new Error("RE2 WASM not ready. Call await init() before using RE2 (required in browser).");
139
+ }
140
+ export function whenReady() {
141
+ if (resolvedModule)
142
+ return Promise.resolve(resolvedModule);
143
+ if (ready)
144
+ return ready;
145
+ return Promise.reject(new Error("Call init() first before using whenReady()"));
146
+ }