@tailwindcss/oxide-wasm32-wasi 0.0.0-insiders.00ef99d

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.
Files changed (115) hide show
  1. package/README.md +3 -0
  2. package/node_modules/@emnapi/core/LICENSE +21 -0
  3. package/node_modules/@emnapi/core/README.md +1 -0
  4. package/node_modules/@emnapi/core/dist/emnapi-core.cjs.js +7746 -0
  5. package/node_modules/@emnapi/core/dist/emnapi-core.cjs.min.d.ts +428 -0
  6. package/node_modules/@emnapi/core/dist/emnapi-core.cjs.min.js +1 -0
  7. package/node_modules/@emnapi/core/dist/emnapi-core.d.mts +428 -0
  8. package/node_modules/@emnapi/core/dist/emnapi-core.d.ts +430 -0
  9. package/node_modules/@emnapi/core/dist/emnapi-core.esm-bundler.js +8741 -0
  10. package/node_modules/@emnapi/core/dist/emnapi-core.js +9777 -0
  11. package/node_modules/@emnapi/core/dist/emnapi-core.min.d.mts +428 -0
  12. package/node_modules/@emnapi/core/dist/emnapi-core.min.js +1 -0
  13. package/node_modules/@emnapi/core/dist/emnapi-core.min.mjs +1 -0
  14. package/node_modules/@emnapi/core/dist/emnapi-core.mjs +7735 -0
  15. package/node_modules/@emnapi/core/index.js +5 -0
  16. package/node_modules/@emnapi/core/package.json +49 -0
  17. package/node_modules/@emnapi/runtime/LICENSE +21 -0
  18. package/node_modules/@emnapi/runtime/README.md +1 -0
  19. package/node_modules/@emnapi/runtime/dist/emnapi.cjs.js +1398 -0
  20. package/node_modules/@emnapi/runtime/dist/emnapi.cjs.min.d.ts +671 -0
  21. package/node_modules/@emnapi/runtime/dist/emnapi.cjs.min.js +1 -0
  22. package/node_modules/@emnapi/runtime/dist/emnapi.d.mts +671 -0
  23. package/node_modules/@emnapi/runtime/dist/emnapi.d.ts +673 -0
  24. package/node_modules/@emnapi/runtime/dist/emnapi.esm-bundler.js +1457 -0
  25. package/node_modules/@emnapi/runtime/dist/emnapi.iife.d.ts +426 -0
  26. package/node_modules/@emnapi/runtime/dist/emnapi.iife.js +1528 -0
  27. package/node_modules/@emnapi/runtime/dist/emnapi.js +1529 -0
  28. package/node_modules/@emnapi/runtime/dist/emnapi.min.d.mts +671 -0
  29. package/node_modules/@emnapi/runtime/dist/emnapi.min.js +1 -0
  30. package/node_modules/@emnapi/runtime/dist/emnapi.min.mjs +1 -0
  31. package/node_modules/@emnapi/runtime/dist/emnapi.mjs +1367 -0
  32. package/node_modules/@emnapi/runtime/index.js +5 -0
  33. package/node_modules/@emnapi/runtime/package.json +48 -0
  34. package/node_modules/@emnapi/wasi-threads/LICENSE +21 -0
  35. package/node_modules/@emnapi/wasi-threads/README.md +203 -0
  36. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.js +915 -0
  37. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.min.d.ts +271 -0
  38. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.min.js +1 -0
  39. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.d.mts +271 -0
  40. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.d.ts +273 -0
  41. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.esm-bundler.js +975 -0
  42. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.js +987 -0
  43. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.d.mts +271 -0
  44. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.js +1 -0
  45. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.mjs +1 -0
  46. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.mjs +910 -0
  47. package/node_modules/@emnapi/wasi-threads/index.js +5 -0
  48. package/node_modules/@emnapi/wasi-threads/package.json +50 -0
  49. package/node_modules/@napi-rs/wasm-runtime/dist/emnapi-plugins.cjs +1826 -0
  50. package/node_modules/@napi-rs/wasm-runtime/dist/emnapi-plugins.js +1823 -0
  51. package/node_modules/@napi-rs/wasm-runtime/dist/fs-proxy.cjs +321 -0
  52. package/node_modules/@napi-rs/wasm-runtime/dist/fs.js +25524 -0
  53. package/node_modules/@napi-rs/wasm-runtime/fs-proxy.js +316 -0
  54. package/node_modules/@napi-rs/wasm-runtime/package.json +74 -0
  55. package/node_modules/@napi-rs/wasm-runtime/runtime.cjs +39 -0
  56. package/node_modules/@napi-rs/wasm-runtime/runtime.js +36 -0
  57. package/node_modules/@tybys/wasm-util/README.md +193 -0
  58. package/node_modules/@tybys/wasm-util/dist/tsdoc-metadata.json +11 -0
  59. package/node_modules/@tybys/wasm-util/dist/wasm-util.d.ts +423 -0
  60. package/node_modules/@tybys/wasm-util/dist/wasm-util.esm-bundler.js +2932 -0
  61. package/node_modules/@tybys/wasm-util/dist/wasm-util.esm.js +2932 -0
  62. package/node_modules/@tybys/wasm-util/dist/wasm-util.esm.min.js +1 -0
  63. package/node_modules/@tybys/wasm-util/dist/wasm-util.js +2954 -0
  64. package/node_modules/@tybys/wasm-util/dist/wasm-util.min.js +1 -0
  65. package/node_modules/@tybys/wasm-util/lib/cjs/asyncify.js +158 -0
  66. package/node_modules/@tybys/wasm-util/lib/cjs/index.js +13 -0
  67. package/node_modules/@tybys/wasm-util/lib/cjs/jspi.js +46 -0
  68. package/node_modules/@tybys/wasm-util/lib/cjs/load.js +97 -0
  69. package/node_modules/@tybys/wasm-util/lib/cjs/memory.js +35 -0
  70. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/error.js +103 -0
  71. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/fd.js +268 -0
  72. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/fs.js +3 -0
  73. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/index.js +192 -0
  74. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/path.js +316 -0
  75. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/preview1.js +1599 -0
  76. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/rights.js +141 -0
  77. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/types.js +220 -0
  78. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/util.js +128 -0
  79. package/node_modules/@tybys/wasm-util/lib/cjs/webassembly.js +13 -0
  80. package/node_modules/@tybys/wasm-util/lib/mjs/asyncify.mjs +153 -0
  81. package/node_modules/@tybys/wasm-util/lib/mjs/index.mjs +9 -0
  82. package/node_modules/@tybys/wasm-util/lib/mjs/jspi.mjs +39 -0
  83. package/node_modules/@tybys/wasm-util/lib/mjs/load.mjs +89 -0
  84. package/node_modules/@tybys/wasm-util/lib/mjs/memory.mjs +28 -0
  85. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/error.mjs +97 -0
  86. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/fd.mjs +256 -0
  87. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/fs.mjs +1 -0
  88. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/index.mjs +186 -0
  89. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/path.mjs +310 -0
  90. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/preview1.mjs +1593 -0
  91. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/rights.mjs +135 -0
  92. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/types.mjs +216 -0
  93. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/util.mjs +113 -0
  94. package/node_modules/@tybys/wasm-util/lib/mjs/webassembly.mjs +9 -0
  95. package/node_modules/@tybys/wasm-util/package.json +58 -0
  96. package/node_modules/tslib/CopyrightNotice.txt +15 -0
  97. package/node_modules/tslib/LICENSE.txt +12 -0
  98. package/node_modules/tslib/README.md +164 -0
  99. package/node_modules/tslib/SECURITY.md +41 -0
  100. package/node_modules/tslib/modules/index.d.ts +38 -0
  101. package/node_modules/tslib/modules/index.js +70 -0
  102. package/node_modules/tslib/modules/package.json +3 -0
  103. package/node_modules/tslib/package.json +47 -0
  104. package/node_modules/tslib/tslib.d.ts +460 -0
  105. package/node_modules/tslib/tslib.es6.html +1 -0
  106. package/node_modules/tslib/tslib.es6.js +402 -0
  107. package/node_modules/tslib/tslib.es6.mjs +401 -0
  108. package/node_modules/tslib/tslib.html +1 -0
  109. package/node_modules/tslib/tslib.js +484 -0
  110. package/package.json +42 -0
  111. package/tailwindcss-oxide.wasi-browser.js +67 -0
  112. package/tailwindcss-oxide.wasi.cjs +123 -0
  113. package/tailwindcss-oxide.wasm32-wasi.wasm +0 -0
  114. package/wasi-worker-browser.mjs +41 -0
  115. package/wasi-worker.mjs +72 -0
@@ -0,0 +1,316 @@
1
+ // @ts-check
2
+
3
+ /**
4
+ * @param {unknown} value
5
+ */
6
+ const getType = (value) => {
7
+ if (value === undefined) return 0
8
+ if (value === null) return 1
9
+ if (isBuffer(value)) return 10
10
+ const t = typeof value
11
+ if (t === 'boolean') return 2
12
+ if (t === 'number') return 3
13
+ if (t === 'string') return 4
14
+ if (t === 'object') return 6
15
+ if (t === 'bigint') return 9
16
+ return -1
17
+ }
18
+
19
+ /**
20
+ * @param {unknown} value
21
+ * @returns {value is Uint8Array}
22
+ */
23
+ const isBuffer = (value) => {
24
+ const BufferCtor = globalThis.Buffer
25
+ if (typeof BufferCtor?.isBuffer === 'function') {
26
+ return BufferCtor.isBuffer(value)
27
+ }
28
+ return value instanceof Uint8Array && value.constructor?.name === 'Buffer'
29
+ }
30
+
31
+ const RESPONSE_HEADER_SIZE = 16
32
+ const RESPONSE_PAYLOAD_SIZE = 10240
33
+ const RESPONSE_BUFFER_SIZE = RESPONSE_HEADER_SIZE + RESPONSE_PAYLOAD_SIZE
34
+
35
+ /**
36
+ * @param {Int32Array} sab
37
+ * @param {Uint8Array} payload
38
+ */
39
+ const writeResponsePayload = (sab, payload) => {
40
+ if (payload.length > RESPONSE_PAYLOAD_SIZE) {
41
+ throw new RangeError('payload overflow')
42
+ }
43
+ new Uint8Array(sab.buffer).set(payload, RESPONSE_HEADER_SIZE)
44
+ }
45
+
46
+ /**
47
+ * @param {import('memfs').IFs} memfs
48
+ * @param {any} value
49
+ * @param {ReturnType<typeof getType>} type
50
+ * @returns {Uint8Array}
51
+ */
52
+ const encodeValue = (memfs, value, type) => {
53
+ switch (type) {
54
+ case 0:
55
+ case 1:
56
+ return new Uint8Array(0)
57
+ case 2: {
58
+ const view = new Int32Array(1)
59
+ view[0] = value ? 1 : 0
60
+ return new Uint8Array(view.buffer)
61
+ }
62
+ case 3: {
63
+ const view = new Float64Array(1)
64
+ view[0] = value
65
+ return new Uint8Array(view.buffer)
66
+ }
67
+ case 4: {
68
+ const view = new TextEncoder().encode(value)
69
+ return view
70
+ }
71
+ case 10:
72
+ return new Uint8Array(value)
73
+ case 6: {
74
+ function storeConstructor(obj, memfs, processed = new WeakSet()) {
75
+ if (!obj || typeof obj !== 'object') {
76
+ return
77
+ }
78
+
79
+ if (processed.has(obj)) {
80
+ return
81
+ }
82
+
83
+ processed.add(obj)
84
+
85
+ const [entry] =
86
+ Object.entries(memfs).filter(([_, v]) => v === obj.constructor)[0] ??
87
+ []
88
+ if (entry) {
89
+ Object.defineProperty(obj, '__constructor__', {
90
+ configurable: true,
91
+ writable: true,
92
+ enumerable: true,
93
+ value: entry,
94
+ })
95
+ }
96
+
97
+ for (const value of Object.values(obj)) {
98
+ storeConstructor(value, memfs, processed)
99
+ }
100
+ }
101
+
102
+ storeConstructor(value, memfs)
103
+
104
+ const json = JSON.stringify(value, (_, value) => {
105
+ if (typeof value === 'bigint') {
106
+ return `BigInt(${String(value)})`
107
+ }
108
+ if (value instanceof Error) {
109
+ return {
110
+ ...value,
111
+ message: value.message,
112
+ stack: value.stack,
113
+ __error__: value.constructor.name,
114
+ }
115
+ }
116
+ return value
117
+ })
118
+ const view = new TextEncoder().encode(json)
119
+ return view
120
+ }
121
+ case 9: {
122
+ return new TextEncoder().encode(String(value))
123
+ }
124
+ case -1:
125
+ default:
126
+ throw new Error('unsupported data')
127
+ }
128
+ }
129
+
130
+ /**
131
+ * @param {typeof import('memfs')} memfs
132
+ * @param {Uint8Array} payload
133
+ * @param {number} type
134
+ * @returns {any}
135
+ */
136
+ const decodeValue = (memfs, payload, type) => {
137
+ if (type === 0) return undefined
138
+ if (type === 1) return null
139
+ if (type === 2)
140
+ return Boolean(new Int32Array(payload.buffer, payload.byteOffset, 1)[0])
141
+ if (type === 3)
142
+ return new Float64Array(payload.buffer, payload.byteOffset, 1)[0]
143
+ if (type === 4) return new TextDecoder().decode(payload.slice())
144
+ if (type === 10) {
145
+ const BufferCtor = globalThis.Buffer
146
+ if (typeof BufferCtor?.from === 'function') {
147
+ return BufferCtor.from(payload)
148
+ }
149
+ return payload.slice()
150
+ }
151
+ if (type === 6) {
152
+ const obj = JSON.parse(
153
+ new TextDecoder().decode(payload.slice()),
154
+ (_key, value) => {
155
+ if (typeof value === 'string') {
156
+ const matched = value.match(/^BigInt\((-?\d+)\)$/)
157
+ if (matched && matched[1]) {
158
+ return BigInt(matched[1])
159
+ }
160
+ }
161
+ return value
162
+ },
163
+ )
164
+
165
+ function loadConstructor(obj, memfs, processed = new WeakSet()) {
166
+ if (!obj || typeof obj !== 'object') {
167
+ return
168
+ }
169
+
170
+ if (processed.has(obj)) {
171
+ return
172
+ }
173
+
174
+ processed.add(obj)
175
+
176
+ if (obj.__constructor__) {
177
+ const ctor = obj.__constructor__
178
+ delete obj.__constructor__
179
+ Object.setPrototypeOf(obj, memfs[ctor].prototype)
180
+ }
181
+
182
+ for (const value of Object.values(obj)) {
183
+ loadConstructor(value, memfs, processed)
184
+ }
185
+ }
186
+
187
+ loadConstructor(obj, memfs)
188
+
189
+ if (obj.__error__) {
190
+ const name = obj.__error__
191
+ const ErrorConstructor = globalThis[name] || Error
192
+ delete obj.__error__
193
+ const err = new ErrorConstructor(obj.message)
194
+ Object.defineProperty(err, 'stack', {
195
+ configurable: true,
196
+ enumerable: false,
197
+ writable: true,
198
+ value: err.stack,
199
+ })
200
+ Object.defineProperty(err, Symbol.toStringTag, {
201
+ configurable: true,
202
+ enumerable: false,
203
+ writable: true,
204
+ value: name,
205
+ })
206
+ for (const [k, v] of Object.entries(obj)) {
207
+ if (k === 'message' || k === 'stack') continue
208
+ err[k] = v
209
+ }
210
+ return err
211
+ }
212
+ return obj
213
+ }
214
+ if (type === 9) {
215
+ return BigInt(new TextDecoder().decode(payload.slice()))
216
+ }
217
+ throw new Error('unsupported data')
218
+ }
219
+
220
+ /**
221
+ * @param {import('memfs').IFs} fs
222
+ * @returns {(e: { data: { __fs__: { sab: Int32Array, type: keyof import('memfs').IFs, payload: any[] } } }) => void}
223
+ */
224
+ // oxlint-disable-next-line no-unused-vars -- fixed in an upcoming release
225
+ export const createOnMessage = (fs) =>
226
+ function onMessage(e) {
227
+ if (e.data.__fs__) {
228
+ /**
229
+ * 0..4 status(int32_t): 21(waiting) 0(success) 1(error)
230
+ * 5..8 type(napi_valuetype): 0(undefined) 1(null) 2(boolean) 3(number) 4(string) 6(jsonstring) 9(bigint) 10(buffer) -1(unsupported)
231
+ * 9..16 payload_size(uint32_t) <= 10240
232
+ * 16..16 + payload_size payload_content
233
+ */
234
+ const { sab, type, payload } = e.data.__fs__
235
+ const fn = fs[type]
236
+ try {
237
+ const ret = fn.apply(fs, payload)
238
+ const t = getType(ret)
239
+ Atomics.store(sab, 1, t)
240
+ const v = encodeValue(fs, ret, t)
241
+ Atomics.store(sab, 2, v.length)
242
+ writeResponsePayload(sab, v)
243
+ Atomics.store(sab, 0, 0) // success
244
+ } catch (/** @type {any} */ err) {
245
+ let t = getType(err)
246
+ let v
247
+ try {
248
+ if (t === -1) {
249
+ throw new Error('unsupported data')
250
+ }
251
+ v = encodeValue(fs, err, t)
252
+ } catch {
253
+ const fallback = (() => {
254
+ try {
255
+ return String(err)
256
+ } catch {
257
+ return 'Unserializable thrown value'
258
+ }
259
+ })()
260
+ t = getType(fallback)
261
+ v = encodeValue(fs, fallback, t)
262
+ }
263
+ if (v.length > RESPONSE_PAYLOAD_SIZE) {
264
+ const overflowErr = new RangeError('payload overflow')
265
+ t = getType(overflowErr)
266
+ v = encodeValue(fs, overflowErr, t)
267
+ }
268
+ Atomics.store(sab, 1, t)
269
+ Atomics.store(sab, 2, v.length)
270
+ writeResponsePayload(sab, v)
271
+ Atomics.store(sab, 0, 1) // error
272
+ } finally {
273
+ Atomics.notify(sab, 0)
274
+ }
275
+ }
276
+ }
277
+
278
+ /**
279
+ * @param {typeof import('memfs')} memfs
280
+ */
281
+ export const createFsProxy = (memfs) =>
282
+ new Proxy(
283
+ {},
284
+ {
285
+ get(_target, p, _receiver) {
286
+ /**
287
+ * @param {any[]} args
288
+ */
289
+ return function (...args) {
290
+ const sab = new SharedArrayBuffer(RESPONSE_BUFFER_SIZE)
291
+ const i32arr = new Int32Array(sab)
292
+ Atomics.store(i32arr, 0, 21)
293
+
294
+ postMessage({
295
+ __fs__: {
296
+ sab: i32arr,
297
+ type: p,
298
+ payload: args,
299
+ },
300
+ })
301
+
302
+ Atomics.wait(i32arr, 0, 21)
303
+
304
+ const status = Atomics.load(i32arr, 0)
305
+ const type = Atomics.load(i32arr, 1)
306
+ const size = Atomics.load(i32arr, 2)
307
+ const content = new Uint8Array(sab, RESPONSE_HEADER_SIZE, size)
308
+ const value = decodeValue(memfs, content, type)
309
+ if (status === 1) {
310
+ throw value
311
+ }
312
+ return value
313
+ }
314
+ },
315
+ },
316
+ )
@@ -0,0 +1,74 @@
1
+ {
2
+ "name": "@napi-rs/wasm-runtime",
3
+ "version": "1.2.2",
4
+ "type": "module",
5
+ "description": "Runtime and polyfill for wasm targets",
6
+ "engines": {
7
+ "node": "^20.19.0 || ^22.13.0 || >=23.5.0"
8
+ },
9
+ "homepage": "https://napi.rs/",
10
+ "author": {
11
+ "name": "LongYinan",
12
+ "url": "https://github.com/Brooooooklyn"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/napi-rs/napi-rs.git",
17
+ "directory": "wasm-runtime"
18
+ },
19
+ "license": "MIT",
20
+ "publishConfig": {
21
+ "registry": "https://registry.npmjs.org/",
22
+ "access": "public"
23
+ },
24
+ "files": [
25
+ "runtime.cjs",
26
+ "runtime.js",
27
+ "fs-proxy.js",
28
+ "dist/*.cjs",
29
+ "dist/*.js"
30
+ ],
31
+ "devDependencies": {
32
+ "@emnapi/core": "2.0.0-alpha.3",
33
+ "@rollup/plugin-alias": "^6.0.0",
34
+ "@rollup/plugin-commonjs": "^29.0.3",
35
+ "@rollup/plugin-inject": "^5.0.5",
36
+ "@rollup/plugin-json": "^6.1.0",
37
+ "@rollup/plugin-node-resolve": "^16.0.3",
38
+ "@rollup/plugin-replace": "^6.0.3",
39
+ "buffer": "^6.0.3",
40
+ "events": "^3.3.0",
41
+ "memfs": "^4.64.0",
42
+ "node-inspect-extracted": "^3.3.2",
43
+ "path-browserify": "^1.0.1",
44
+ "process": "^0.11.10",
45
+ "readable-stream": "^4.7.0",
46
+ "rollup": "^4.61.1",
47
+ "rollup-plugin-polyfill-node": "^0.13.0",
48
+ "tslib": "^2.8.1"
49
+ },
50
+ "dependencies": {
51
+ "@tybys/wasm-util": "^0.10.3"
52
+ },
53
+ "peerDependencies": {
54
+ "@emnapi/core": "^1.7.1 || ^2.0.0-alpha.3",
55
+ "@emnapi/runtime": "^1.7.1 || ^2.0.0-alpha.3"
56
+ },
57
+ "scripts": {
58
+ "build": "rollup -c rollup.config.js",
59
+ "test": "yarn run build && node --test"
60
+ },
61
+ "funding": {
62
+ "type": "github",
63
+ "url": "https://github.com/sponsors/Brooooooklyn"
64
+ },
65
+ "exports": {
66
+ ".": {
67
+ "import": "./runtime.js",
68
+ "require": "./runtime.cjs"
69
+ },
70
+ "./fs": {
71
+ "import": "./dist/fs.js"
72
+ }
73
+ }
74
+ }
@@ -0,0 +1,39 @@
1
+ const {
2
+ MessageHandler,
3
+ instantiateNapiModuleSync,
4
+ instantiateNapiModule,
5
+ } = require('@emnapi/core')
6
+ // Single-threaded (non-shared-memory) WASI builds link an emnapi archive
7
+ // without the C async-work and threadsafe-function implementations (they are
8
+ // unconditional `napi_generic_failure` stubs without threads), so the
9
+ // generated loaders provide the JavaScript implementations through these
10
+ // plugins instead. Raw `instantiateNapiModule(Sync)` callers instantiating a
11
+ // single-threaded napi-rs wasm must pass them too:
12
+ // `plugins: [emnapiAsyncWorkPlugin, emnapiTSFNPlugin]` — without them
13
+ // instantiation fails with a LinkError naming the missing import. Threaded
14
+ // (shared-memory) builds link the C implementations and need no plugins;
15
+ // this mirrors the upstream @emnapi/core v2 plugin split (v1 bundled these
16
+ // implementations in the core runtime). The plugins are bundled into this
17
+ // package so loading the runtime with @emnapi/core v1 does not resolve the
18
+ // v2-only `@emnapi/core/plugins` package export.
19
+ const {
20
+ emnapiAsyncWorkPlugin,
21
+ emnapiTSFNPlugin,
22
+ } = require('./dist/emnapi-plugins.cjs')
23
+ const { createContext, getDefaultContext } = require('@emnapi/runtime')
24
+ const { WASI } = require('@tybys/wasm-util')
25
+
26
+ const { createFsProxy, createOnMessage } = require('./dist/fs-proxy.cjs')
27
+
28
+ module.exports = {
29
+ MessageHandler,
30
+ createContext,
31
+ emnapiAsyncWorkPlugin,
32
+ emnapiTSFNPlugin,
33
+ instantiateNapiModule,
34
+ instantiateNapiModuleSync,
35
+ getDefaultContext,
36
+ WASI,
37
+ createFsProxy,
38
+ createOnMessage,
39
+ }
@@ -0,0 +1,36 @@
1
+ import * as emnapiCore from '@emnapi/core'
2
+ import * as emnapiRuntime from '@emnapi/runtime'
3
+
4
+ import {
5
+ emnapiAsyncWorkPlugin,
6
+ emnapiTSFNPlugin,
7
+ } from './dist/emnapi-plugins.js'
8
+
9
+ const { instantiateNapiModuleSync, instantiateNapiModule, MessageHandler } =
10
+ emnapiCore
11
+ const { createContext, getDefaultContext } = emnapiRuntime
12
+
13
+ // Single-threaded (non-shared-memory) WASI builds link an emnapi archive
14
+ // without the C async-work and threadsafe-function implementations (they are
15
+ // unconditional `napi_generic_failure` stubs without threads), so the
16
+ // generated loaders provide the JavaScript implementations through these
17
+ // plugins instead. Raw `instantiateNapiModule(Sync)` callers instantiating a
18
+ // single-threaded napi-rs wasm must pass them too:
19
+ // `plugins: [emnapiAsyncWorkPlugin, emnapiTSFNPlugin]` — without them
20
+ // instantiation fails with a LinkError naming the missing import. Threaded
21
+ // (shared-memory) builds link the C implementations and need no plugins;
22
+ // this mirrors the upstream @emnapi/core v2 plugin split (v1 bundled these
23
+ // implementations in the core runtime). The plugins are bundled into this
24
+ // package so loading the runtime with @emnapi/core v1 does not resolve the
25
+ // v2-only `@emnapi/core/plugins` package export.
26
+ export {
27
+ MessageHandler,
28
+ createContext,
29
+ emnapiAsyncWorkPlugin,
30
+ emnapiTSFNPlugin,
31
+ getDefaultContext,
32
+ instantiateNapiModule,
33
+ instantiateNapiModuleSync,
34
+ }
35
+ export * from '@tybys/wasm-util'
36
+ export { createOnMessage, createFsProxy } from './fs-proxy.js'
@@ -0,0 +1,193 @@
1
+ # @tybys/wasm-util
2
+
3
+ WebAssembly related utils for browser environment
4
+
5
+ **The output code is ES2019**
6
+
7
+ ## Features
8
+
9
+ All example code below need to be bundled by ES module bundlers like `webpack` / `rollup`, or specify import map in browser native ES module runtime.
10
+
11
+ ### WASI polyfill for browser
12
+
13
+ The API is similar to the `require('wasi').WASI` in Node.js.
14
+
15
+ You can use `memfs-browser` to provide filesystem capability.
16
+
17
+ - Example: [https://github.com/toyobayashi/wasi-wabt](https://github.com/toyobayashi/wasi-wabt)
18
+ - Demo: [https://toyobayashi.github.io/wasi-wabt/](https://toyobayashi.github.io/wasi-wabt/)
19
+
20
+ ```js
21
+ import { load, WASI } from '@tybys/wasm-util'
22
+ import { Volume, createFsFromVolume } from 'memfs-browser'
23
+
24
+ const fs = createFsFromVolume(Volume.fromJSON({
25
+ '/home/wasi': null
26
+ }))
27
+
28
+ const wasi = new WASI({
29
+ args: ['chrome', 'file.wasm'],
30
+ env: {
31
+ NODE_ENV: 'development',
32
+ WASI_SDK_PATH: '/opt/wasi-sdk'
33
+ },
34
+ preopens: {
35
+ '/': '/'
36
+ },
37
+ fs,
38
+
39
+ // redirect stdout / stderr
40
+
41
+ // print (text) { console.log(text) },
42
+ // printErr (text) { console.error(text) }
43
+ })
44
+
45
+ const imports = {
46
+ wasi_snapshot_preview1: wasi.wasiImport
47
+ }
48
+
49
+ const { module, instance } = await load('/path/to/file.wasm', imports)
50
+ wasi.start(instance)
51
+ // wasi.initialize(instance)
52
+ ```
53
+
54
+ Implemented syscalls: [wasi_snapshot_preview1](#wasi_snapshot_preview1)
55
+
56
+ ### `load` / `loadSync`
57
+
58
+ `loadSync` has 4KB wasm size limit in browser.
59
+
60
+ ```js
61
+ // bundler
62
+ import { load, loadSync } from '@tybys/wasm-util'
63
+
64
+ const imports = { /* ... */ }
65
+
66
+ // using path
67
+ const { module, instance } = await load('/path/to/file.wasm', imports)
68
+ const { module, instance } = loadSync('/path/to/file.wasm', imports)
69
+
70
+ // using URL
71
+ const { module, instance } = await load(new URL('./file.wasm', import.meta.url), imports)
72
+ const { module, instance } = loadSync(new URL('./file.wasm', import.meta.url), imports)
73
+
74
+ // using Uint8Array
75
+ const buffer = new Uint8Array([
76
+ 0x00, 0x61, 0x73, 0x6d,
77
+ 0x01, 0x00, 0x00, 0x00
78
+ ])
79
+ const { module, instance } = await load(buffer, imports)
80
+ const { module, instance } = loadSync(buffer, imports)
81
+
82
+ // auto asyncify
83
+ const {
84
+ module,
85
+ instance: asyncifiedInstance
86
+ } = await load(buffer, imports, { /* asyncify options */})
87
+ asyncifiedInstance.exports.fn() // => return Promise
88
+ ```
89
+
90
+ ### Extend Memory instance
91
+
92
+ ```js
93
+ import { Memory, extendMemory } from '@tybys/wasm-util'
94
+
95
+ const memory = new WebAssembly.Memory({ initial: 256 })
96
+ // const memory = instance.exports.memory
97
+
98
+ extendMemory(memory)
99
+ console.log(memory instanceof Memory)
100
+ console.log(memory instanceof WebAssembly.Memory)
101
+ // expose memory view getters like Emscripten
102
+ const { HEAPU8, HEAPU32, view } = memory
103
+ ```
104
+
105
+ ### Asyncify wrap
106
+
107
+ Build the C code using `clang`, `wasm-ld` and `wasm-opt`
108
+
109
+ ```c
110
+ void async_sleep(int ms);
111
+
112
+ int main() {
113
+ async_sleep(200);
114
+ return 0;
115
+ }
116
+ ```
117
+
118
+ ```js
119
+ import { Asyncify } from '@tybys/wasm-util'
120
+
121
+ const asyncify = new Asyncify()
122
+
123
+ const imports = {
124
+ env: {
125
+ async_sleep: asyncify.wrapImportFunction(function (ms) {
126
+ return new Promise((resolve) => {
127
+ setTimeout(resolve, ms)
128
+ })
129
+ })
130
+ }
131
+ }
132
+
133
+ // async_sleep(200)
134
+ const bytes = await (await fetch('/asyncfied_by_wasm-opt.wasm')).arrayBuffer()
135
+ const { instance } = await WebAssembly.instantiate(bytes, imports)
136
+ const asyncifiedInstance = asyncify.init(instance.exports.memory, instance, {
137
+ wrapExports: ['_start']
138
+ })
139
+
140
+ const p = asyncifedInstance._start()
141
+ console.log(typeof p.then === 'function')
142
+ const now = Date.now()
143
+ await p
144
+ console.log(Date.now() - now >= 200)
145
+ ```
146
+
147
+ ### wasi_snapshot_preview1
148
+
149
+ - [x] args_get
150
+ - [x] args_sizes_get
151
+ - [x] environ_get
152
+ - [x] environ_sizes_get
153
+ - [x] clock_res_get
154
+ - [x] clock_time_get
155
+ - [ ] ~~fd_advise~~
156
+ - [x] fd_allocate
157
+ - [x] fd_close
158
+ - [x] fd_datasync
159
+ - [x] fd_fdstat_get
160
+ - [ ] ~~fd_fdstat_set_flags~~
161
+ - [x] fd_fdstat_set_rights
162
+ - [x] fd_filestat_get
163
+ - [x] fd_filestat_set_size
164
+ - [x] fd_filestat_set_times
165
+ - [x] fd_pread
166
+ - [x] fd_prestat_get
167
+ - [x] fd_prestat_dir_name
168
+ - [x] fd_pwrite
169
+ - [x] fd_read
170
+ - [x] fd_readdir
171
+ - [x] fd_renumber
172
+ - [x] fd_seek
173
+ - [x] fd_sync
174
+ - [x] fd_tell
175
+ - [x] fd_write
176
+ - [x] path_create_directory
177
+ - [x] path_filestat_get
178
+ - [x] path_filestat_set_times
179
+ - [x] path_link
180
+ - [x] path_open
181
+ - [x] path_readlink
182
+ - [x] path_remove_directory
183
+ - [x] path_rename
184
+ - [x] path_symlink
185
+ - [x] path_unlink_file
186
+ - [x] poll_oneoff (timer only)
187
+ - [x] proc_exit
188
+ - [ ] ~~proc_raise~~
189
+ - [x] sched_yield
190
+ - [x] random_get
191
+ - [ ] ~~sock_recv~~
192
+ - [ ] ~~sock_send~~
193
+ - [ ] ~~sock_shutdown~~
@@ -0,0 +1,11 @@
1
+ // This file is read by tools that parse documentation comments conforming to the TSDoc standard.
2
+ // It should be published with your NPM package. It should not be tracked by Git.
3
+ {
4
+ "tsdocVersion": "0.12",
5
+ "toolPackages": [
6
+ {
7
+ "packageName": "@microsoft/api-extractor",
8
+ "packageVersion": "7.34.4"
9
+ }
10
+ ]
11
+ }