@stll/fuzzy-search-wasm32-wasi 0.2.1

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,66 @@
1
+ import {
2
+ createOnMessage as __wasmCreateOnMessageForFsProxy,
3
+ getDefaultContext as __emnapiGetDefaultContext,
4
+ instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync,
5
+ WASI as __WASI,
6
+ } from "@napi-rs/wasm-runtime";
7
+
8
+ const __wasi = new __WASI({
9
+ version: "preview1",
10
+ });
11
+
12
+ const __wasmUrl = new URL(
13
+ "./fuzzy-search.wasm",
14
+ import.meta.url,
15
+ ).href;
16
+ const __emnapiContext = __emnapiGetDefaultContext();
17
+
18
+ const __sharedMemory = new WebAssembly.Memory({
19
+ initial: 4000,
20
+ maximum: 65536,
21
+ shared: true,
22
+ });
23
+
24
+ const __wasmFile = await fetch(__wasmUrl).then((res) =>
25
+ res.arrayBuffer(),
26
+ );
27
+
28
+ const {
29
+ instance: __napiInstance,
30
+ module: __wasiModule,
31
+ napiModule: __napiModule,
32
+ } = __emnapiInstantiateNapiModuleSync(__wasmFile, {
33
+ context: __emnapiContext,
34
+ asyncWorkPoolSize: 4,
35
+ wasi: __wasi,
36
+ onCreateWorker() {
37
+ const worker = new Worker(
38
+ new URL("./wasi-worker-browser.mjs", import.meta.url),
39
+ {
40
+ type: "module",
41
+ },
42
+ );
43
+
44
+ return worker;
45
+ },
46
+ overwriteImports(importObject) {
47
+ importObject.env = {
48
+ ...importObject.env,
49
+ ...importObject.napi,
50
+ ...importObject.emnapi,
51
+ memory: __sharedMemory,
52
+ };
53
+ return importObject;
54
+ },
55
+ beforeInit({ instance }) {
56
+ for (const name of Object.keys(instance.exports)) {
57
+ if (name.startsWith("__napi_register__")) {
58
+ instance.exports[name]();
59
+ }
60
+ }
61
+ },
62
+ });
63
+ export default __napiModule.exports;
64
+ export const FuzzySearch = __napiModule.exports.FuzzySearch;
65
+ export const Metric = __napiModule.exports.Metric;
66
+ export const distance = __napiModule.exports.distance;
@@ -0,0 +1,132 @@
1
+ /* eslint-disable */
2
+ /* prettier-ignore */
3
+
4
+ /* auto-generated by NAPI-RS */
5
+
6
+ const __nodeFs = require('node:fs')
7
+ const __nodePath = require("node:path");
8
+ const { WASI: __nodeWASI } = require("node:wasi");
9
+ const { Worker } = require("node:worker_threads");
10
+
11
+ const {
12
+ createOnMessage: __wasmCreateOnMessageForFsProxy,
13
+ getDefaultContext: __emnapiGetDefaultContext,
14
+ instantiateNapiModuleSync:
15
+ __emnapiInstantiateNapiModuleSync,
16
+ } = require("@napi-rs/wasm-runtime");
17
+
18
+ const __rootDir = __nodePath.parse(process.cwd()).root;
19
+
20
+ const __wasi = new __nodeWASI({
21
+ version: "preview1",
22
+ env: process.env,
23
+ preopens: {
24
+ [__rootDir]: __rootDir,
25
+ },
26
+ });
27
+
28
+ const __emnapiContext = __emnapiGetDefaultContext();
29
+
30
+ const __sharedMemory = new WebAssembly.Memory({
31
+ initial: 4000,
32
+ maximum: 65536,
33
+ shared: true,
34
+ });
35
+
36
+ let __wasmFilePath = __nodePath.join(
37
+ __dirname,
38
+ "fuzzy-search.wasm",
39
+ );
40
+ const __wasmDebugFilePath = __nodePath.join(
41
+ __dirname,
42
+ "fuzzy-search.debug.wasm",
43
+ );
44
+
45
+ if (__nodeFs.existsSync(__wasmDebugFilePath)) {
46
+ __wasmFilePath = __wasmDebugFilePath;
47
+ } else if (!__nodeFs.existsSync(__wasmFilePath)) {
48
+ try {
49
+ __wasmFilePath =
50
+ require.resolve("@stll/fuzzy-search-wasm32-wasi/fuzzy-search.wasm");
51
+ } catch {
52
+ throw new Error(
53
+ "Cannot find fuzzy-search.wasm file, and @stll/fuzzy-search-wasm32-wasi package is not installed.",
54
+ );
55
+ }
56
+ }
57
+
58
+ const {
59
+ instance: __napiInstance,
60
+ module: __wasiModule,
61
+ napiModule: __napiModule,
62
+ } = __emnapiInstantiateNapiModuleSync(
63
+ __nodeFs.readFileSync(__wasmFilePath),
64
+ {
65
+ context: __emnapiContext,
66
+ asyncWorkPoolSize: (function () {
67
+ const threadsSizeFromEnv = Number(
68
+ process.env.NAPI_RS_ASYNC_WORK_POOL_SIZE ??
69
+ process.env.UV_THREADPOOL_SIZE,
70
+ );
71
+ // NaN > 0 is false
72
+ if (threadsSizeFromEnv > 0) {
73
+ return threadsSizeFromEnv;
74
+ } else {
75
+ return 4;
76
+ }
77
+ })(),
78
+ reuseWorker: true,
79
+ wasi: __wasi,
80
+ onCreateWorker() {
81
+ const worker = new Worker(
82
+ __nodePath.join(__dirname, "wasi-worker.mjs"),
83
+ {
84
+ env: process.env,
85
+ },
86
+ );
87
+ worker.onmessage = ({ data }) => {
88
+ __wasmCreateOnMessageForFsProxy(__nodeFs)(data);
89
+ };
90
+
91
+ {
92
+ const kPublicPort = Object.getOwnPropertySymbols(
93
+ worker,
94
+ ).find((s) => s.toString().includes("kPublicPort"));
95
+ if (kPublicPort) {
96
+ worker[kPublicPort].ref = () => {};
97
+ }
98
+
99
+ const kHandle = Object.getOwnPropertySymbols(
100
+ worker,
101
+ ).find((s) => s.toString().includes("kHandle"));
102
+ if (kHandle) {
103
+ worker[kHandle].ref = () => {};
104
+ }
105
+
106
+ worker.unref();
107
+ }
108
+ return worker;
109
+ },
110
+ overwriteImports(importObject) {
111
+ importObject.env = {
112
+ ...importObject.env,
113
+ ...importObject.napi,
114
+ ...importObject.emnapi,
115
+ memory: __sharedMemory,
116
+ };
117
+ return importObject;
118
+ },
119
+ beforeInit({ instance }) {
120
+ for (const name of Object.keys(instance.exports)) {
121
+ if (name.startsWith("__napi_register__")) {
122
+ instance.exports[name]();
123
+ }
124
+ }
125
+ },
126
+ },
127
+ );
128
+ module.exports = __napiModule.exports;
129
+ module.exports.FuzzySearch =
130
+ __napiModule.exports.FuzzySearch;
131
+ module.exports.Metric = __napiModule.exports.Metric;
132
+ module.exports.distance = __napiModule.exports.distance;
Binary file
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@stll/fuzzy-search-wasm32-wasi",
3
+ "version": "0.2.1",
4
+ "os": ["any"],
5
+ "cpu": ["wasm32"],
6
+ "main": "fuzzy-search.wasi.cjs",
7
+ "browser": "fuzzy-search.wasi-browser.js",
8
+ "files": [
9
+ "fuzzy-search.wasm32-wasi.wasm",
10
+ "fuzzy-search.wasi.cjs",
11
+ "fuzzy-search.wasi-browser.js",
12
+ "wasi-worker.mjs",
13
+ "wasi-worker-browser.mjs"
14
+ ],
15
+ "dependencies": {
16
+ "@napi-rs/wasm-runtime": "^1.1.1"
17
+ },
18
+ "license": "MIT"
19
+ }
@@ -0,0 +1,36 @@
1
+ import {
2
+ instantiateNapiModuleSync,
3
+ MessageHandler,
4
+ WASI,
5
+ } from "@napi-rs/wasm-runtime";
6
+
7
+ const handler = new MessageHandler({
8
+ onLoad({ wasmModule, wasmMemory }) {
9
+ const wasi = new WASI({
10
+ print: function () {
11
+ // eslint-disable-next-line no-console
12
+ console.log.apply(console, arguments);
13
+ },
14
+ printErr: function () {
15
+ // eslint-disable-next-line no-console
16
+ console.error.apply(console, arguments);
17
+ },
18
+ });
19
+ return instantiateNapiModuleSync(wasmModule, {
20
+ childThread: true,
21
+ wasi,
22
+ overwriteImports(importObject) {
23
+ importObject.env = {
24
+ ...importObject.env,
25
+ ...importObject.napi,
26
+ ...importObject.emnapi,
27
+ memory: wasmMemory,
28
+ };
29
+ },
30
+ });
31
+ },
32
+ });
33
+
34
+ globalThis.onmessage = function (e) {
35
+ handler.handle(e);
36
+ };
@@ -0,0 +1,69 @@
1
+ import fs from "node:fs";
2
+ import { createRequire } from "node:module";
3
+ import { parse } from "node:path";
4
+ import { WASI } from "node:wasi";
5
+ import { parentPort, Worker } from "node:worker_threads";
6
+
7
+ const require = createRequire(import.meta.url);
8
+
9
+ const {
10
+ instantiateNapiModuleSync,
11
+ MessageHandler,
12
+ getDefaultContext,
13
+ } = require("@napi-rs/wasm-runtime");
14
+
15
+ if (parentPort) {
16
+ parentPort.on("message", (data) => {
17
+ globalThis.onmessage({ data });
18
+ });
19
+ }
20
+
21
+ Object.assign(globalThis, {
22
+ self: globalThis,
23
+ require,
24
+ Worker,
25
+ importScripts: function (f) {
26
+ (0, eval)(
27
+ fs.readFileSync(f, "utf8") + "//# sourceURL=" + f,
28
+ );
29
+ },
30
+ postMessage: function (msg) {
31
+ if (parentPort) {
32
+ parentPort.postMessage(msg);
33
+ }
34
+ },
35
+ });
36
+
37
+ const emnapiContext = getDefaultContext();
38
+
39
+ const __rootDir = parse(process.cwd()).root;
40
+
41
+ const handler = new MessageHandler({
42
+ onLoad({ wasmModule, wasmMemory }) {
43
+ const wasi = new WASI({
44
+ version: "preview1",
45
+ env: process.env,
46
+ preopens: {
47
+ [__rootDir]: __rootDir,
48
+ },
49
+ });
50
+
51
+ return instantiateNapiModuleSync(wasmModule, {
52
+ childThread: true,
53
+ wasi,
54
+ context: emnapiContext,
55
+ overwriteImports(importObject) {
56
+ importObject.env = {
57
+ ...importObject.env,
58
+ ...importObject.napi,
59
+ ...importObject.emnapi,
60
+ memory: wasmMemory,
61
+ };
62
+ },
63
+ });
64
+ },
65
+ });
66
+
67
+ globalThis.onmessage = function (e) {
68
+ handler.handle(e);
69
+ };