-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathvectorizer.wasi.cjs
More file actions
135 lines (122 loc) · 4.91 KB
/
Copy pathvectorizer.wasi.cjs
File metadata and controls
135 lines (122 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* eslint-disable */
/* prettier-ignore */
/* auto-generated by NAPI-RS */
const __nodeFs = require('node:fs')
const __nodePath = require('node:path');
const { WASI: __nodeWASI } = require('node:wasi');
const { Worker } = require('node:worker_threads');
const {
createOnMessage: __wasmCreateOnMessageForFsProxy,
getDefaultContext: __emnapiGetDefaultContext,
instantiateNapiModuleSync: __emnapiInstantiateNapiModuleSync,
} = require('@napi-rs/wasm-runtime');
const __rootDir = __nodePath.parse(process.cwd()).root;
const __wasi = new __nodeWASI({
version: 'preview1',
env: process.env,
preopens: {
[__rootDir]: __rootDir,
},
});
const __emnapiContext = __emnapiGetDefaultContext();
const __sharedMemory = new WebAssembly.Memory({
initial: 4000,
maximum: 65536,
shared: true,
});
let __wasmFilePath = __nodePath.join(__dirname, 'vectorizer.wasm32-wasi.wasm');
const __wasmDebugFilePath = __nodePath.join(__dirname, 'vectorizer.wasm32-wasi.debug.wasm');
if (__nodeFs.existsSync(__wasmDebugFilePath)) {
__wasmFilePath = __wasmDebugFilePath;
} else if (!__nodeFs.existsSync(__wasmFilePath)) {
try {
__wasmFilePath = __nodePath.resolve('@neplex/vectorizer-wasm32-wasi');
} catch {
throw new Error(
'Cannot find vectorizer.wasm32-wasi.wasm file, and @neplex/vectorizer-wasm32-wasi package is not installed.',
);
}
}
const {
instance: __napiInstance,
module: __wasiModule,
napiModule: __napiModule,
} = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__wasmFilePath), {
context: __emnapiContext,
asyncWorkPoolSize: (function () {
const threadsSizeFromEnv = Number(process.env.NAPI_RS_ASYNC_WORK_POOL_SIZE ?? process.env.UV_THREADPOOL_SIZE);
// NaN > 0 is false
if (threadsSizeFromEnv > 0) {
return threadsSizeFromEnv;
} else {
return 4;
}
})(),
reuseWorker: true,
wasi: __wasi,
onCreateWorker() {
const worker = new Worker(__nodePath.join(__dirname, 'wasi-worker.mjs'), {
env: process.env,
});
worker.onmessage = ({ data }) => {
__wasmCreateOnMessageForFsProxy(__nodeFs)(data);
};
// The main thread of Node.js waits for all the active handles before exiting.
// But Rust threads are never waited without `thread::join`.
// So here we hack the code of Node.js to prevent the workers from being referenced (active).
// According to https://github.com/nodejs/node/blob/19e0d472728c79d418b74bddff588bea70a403d0/lib/internal/worker.js#L415,
// a worker is consist of two handles: kPublicPort and kHandle.
{
const kPublicPort = Object.getOwnPropertySymbols(worker).find((s) => s.toString().includes('kPublicPort'));
if (kPublicPort) {
worker[kPublicPort].ref = () => {};
}
const kHandle = Object.getOwnPropertySymbols(worker).find((s) => s.toString().includes('kHandle'));
if (kHandle) {
worker[kHandle].ref = () => {};
}
worker.unref();
}
return worker;
},
overwriteImports(importObject) {
importObject.env = {
...importObject.env,
...importObject.napi,
...importObject.emnapi,
memory: __sharedMemory,
};
return importObject;
},
beforeInit({ instance }) {
for (const name of Object.keys(instance.exports)) {
if (name.startsWith('__napi_register__')) {
instance.exports[name]();
}
}
},
});
module.exports = __napiModule.exports;
module.exports.SvgFile = __napiModule.exports.SvgFile;
module.exports.JsSvgFile = __napiModule.exports.JsSvgFile;
module.exports.colorExistsInImage = __napiModule.exports.colorExistsInImage;
module.exports.colorExistsInImageSync = __napiModule.exports.colorExistsInImageSync;
module.exports.ColorMode = __napiModule.exports.ColorMode;
module.exports.findUnusedColorInImage = __napiModule.exports.findUnusedColorInImage;
module.exports.findUnusedColorInImageSync = __napiModule.exports.findUnusedColorInImageSync;
module.exports.Hierarchical = __napiModule.exports.Hierarchical;
module.exports.optimize = __napiModule.exports.optimize;
module.exports.OptimizePreset = __napiModule.exports.OptimizePreset;
module.exports.JsOptimizePreset = __napiModule.exports.JsOptimizePreset;
module.exports.optimizeSync = __napiModule.exports.optimizeSync;
module.exports.PathSimplifyMode = __napiModule.exports.PathSimplifyMode;
module.exports.JsPathSimplifyMode = __napiModule.exports.JsPathSimplifyMode;
module.exports.Preset = __napiModule.exports.Preset;
module.exports.readImage = __napiModule.exports.readImage;
module.exports.readImageSync = __napiModule.exports.readImageSync;
module.exports.vectorize = __napiModule.exports.vectorize;
module.exports.vectorizeRaw = __napiModule.exports.vectorizeRaw;
module.exports.vectorizeRawSync = __napiModule.exports.vectorizeRawSync;
module.exports.vectorizeRawToCallback = __napiModule.exports.vectorizeRawToCallback;
module.exports.vectorizeSync = __napiModule.exports.vectorizeSync;
module.exports.vectorizeToCallback = __napiModule.exports.vectorizeToCallback;