@oxc-minify/binding-wasm32-wasi 0.141.0 → 0.143.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.
@@ -1,9 +1,11 @@
1
1
  import {
2
+ emnapiAsyncWorkPlugin as __emnapiAsyncWorkPlugin,
3
+ emnapiTSFNPlugin as __emnapiTSFNPlugin,
2
4
  createOnMessage as __wasmCreateOnMessageForFsProxy,
3
- getDefaultContext as __emnapiGetDefaultContext,
4
5
  instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync,
5
6
  WASI as __WASI,
6
7
  } from '@napi-rs/wasm-runtime'
8
+ import { createContext as __emnapiCreateContext } from '@emnapi/runtime'
7
9
 
8
10
 
9
11
 
@@ -12,8 +14,18 @@ const __wasi = new __WASI({
12
14
  })
13
15
 
14
16
  const __wasmUrl = new URL('./minify.wasm32-wasi.wasm', import.meta.url).href
15
- const __emnapiContext = __emnapiGetDefaultContext()
16
-
17
+ const __wasmResponse = await globalThis.fetch(__wasmUrl)
18
+ if (!__wasmResponse.ok) {
19
+ throw new Error(
20
+ 'Failed to fetch WASI module ' +
21
+ __wasmUrl +
22
+ ': ' +
23
+ __wasmResponse.status +
24
+ ' ' +
25
+ (__wasmResponse.statusText || 'Unknown Status'),
26
+ )
27
+ }
28
+ const __wasmFile = await __wasmResponse.arrayBuffer()
17
29
 
18
30
  const __sharedMemory = new WebAssembly.Memory({
19
31
  initial: 4000,
@@ -21,41 +33,314 @@ const __sharedMemory = new WebAssembly.Memory({
21
33
  shared: true,
22
34
  })
23
35
 
24
- const __wasmFile = await fetch(__wasmUrl).then((res) => res.arrayBuffer())
25
-
26
- const {
27
- instance: __napiInstance,
28
- module: __wasiModule,
29
- napiModule: __napiModule,
30
- } = __emnapiInstantiateNapiModuleSync(__wasmFile, {
31
- context: __emnapiContext,
32
- asyncWorkPoolSize: 4,
33
- wasi: __wasi,
34
- onCreateWorker() {
35
- const worker = new Worker(new URL('@oxc-minify/binding-wasm32-wasi/wasi-worker-browser.mjs', import.meta.url), {
36
- type: 'module',
37
- })
38
-
39
-
40
- return worker
41
- },
42
- overwriteImports(importObject) {
43
- importObject.env = {
44
- ...importObject.env,
45
- ...importObject.napi,
46
- ...importObject.emnapi,
47
- memory: __sharedMemory,
48
- }
49
- return importObject
50
- },
51
- beforeInit({ instance }) {
52
- for (const name of Object.keys(instance.exports)) {
53
- if (name.startsWith('__napi_register__')) {
54
- instance.exports[name]()
36
+ let __emnapiContext
37
+
38
+ const __wasiDisposeSymbol = Symbol.for('napi.rs.wasi.dispose')
39
+ const __wasiWorkers = new Set()
40
+ let __napiInstance
41
+ let __emnapiContextDestroyed = false
42
+ let __emnapiContextDestroyPromise
43
+ let __emnapiWasmEnvCleanupPrepared = false
44
+ let __wasiDisposed = false
45
+ let __wasiDisposePromise
46
+ let __completeWasiDisposal = function() {}
47
+
48
+ function __isThenable(value) {
49
+ return (
50
+ value !== null &&
51
+ (typeof value === 'object' || typeof value === 'function') &&
52
+ typeof value.then === 'function'
53
+ )
54
+ }
55
+
56
+ function __createCleanupError(errors, message) {
57
+ if (errors.length === 1) {
58
+ return errors[0]
59
+ }
60
+ const __AggregateError = globalThis.AggregateError
61
+ if (typeof __AggregateError === 'function') {
62
+ return new __AggregateError(errors, message)
63
+ }
64
+ const error = new Error(message)
65
+ error.errors = errors
66
+ return error
67
+ }
68
+
69
+ function __attachCleanupErrors(error, cleanupErrors) {
70
+ if (cleanupErrors.length === 0) {
71
+ return error
72
+ }
73
+ const cleanupError = __createCleanupError(
74
+ cleanupErrors,
75
+ 'WASI binding cleanup failed',
76
+ )
77
+ try {
78
+ if (
79
+ error &&
80
+ (typeof error === 'object' || typeof error === 'function')
81
+ ) {
82
+ if (error.cause === undefined) {
83
+ error.cause = cleanupError
84
+ if (error.cause === cleanupError) {
85
+ return error
86
+ }
87
+ }
88
+ if (Array.isArray(error.cleanupErrors)) {
89
+ error.cleanupErrors.push(cleanupError)
90
+ return error
91
+ } else {
92
+ const attachedCleanupErrors = [cleanupError]
93
+ error.cleanupErrors = attachedCleanupErrors
94
+ if (error.cleanupErrors === attachedCleanupErrors) {
95
+ return error
96
+ }
55
97
  }
56
98
  }
57
- },
58
- })
99
+ } catch {}
100
+ const aggregate = __createCleanupError(
101
+ [error, cleanupError],
102
+ 'WASI binding initialization and cleanup failed',
103
+ )
104
+ try {
105
+ aggregate.cause = error
106
+ } catch {}
107
+ return aggregate
108
+ }
109
+
110
+ function __prepareWasmEnvCleanup() {
111
+ if (__emnapiWasmEnvCleanupPrepared) {
112
+ return
113
+ }
114
+ const prepare = __napiInstance?.exports?.napi_prepare_wasm_env_cleanup
115
+ if (typeof prepare === 'function') {
116
+ prepare()
117
+ }
118
+ __emnapiWasmEnvCleanupPrepared = true
119
+ }
120
+
121
+ function __destroyEmnapiContext() {
122
+ if (__emnapiContextDestroyed || __emnapiContext === undefined) {
123
+ __emnapiContextDestroyed = true
124
+ return
125
+ }
126
+ if (__emnapiContextDestroyPromise) {
127
+ return __emnapiContextDestroyPromise
128
+ }
129
+
130
+ __prepareWasmEnvCleanup()
131
+ const result = __emnapiContext.destroy()
132
+ if (!__isThenable(result)) {
133
+ __emnapiContextDestroyed = true
134
+ return
135
+ }
136
+
137
+ const destroyPromise = Promise.resolve(result).then(
138
+ (value) => {
139
+ __emnapiContextDestroyed = true
140
+ return value
141
+ },
142
+ (error) => {
143
+ __emnapiContextDestroyPromise = undefined
144
+ throw error
145
+ },
146
+ )
147
+ __emnapiContextDestroyPromise = destroyPromise
148
+ return destroyPromise
149
+ }
150
+
151
+ function __terminateWasiWorkers() {
152
+ const cleanupErrors = []
153
+ const pending = []
154
+
155
+ for (const worker of __wasiWorkers) {
156
+ let result
157
+ try {
158
+ result = worker.terminate()
159
+ } catch (error) {
160
+ cleanupErrors.push(error)
161
+ continue
162
+ }
163
+ if (__isThenable(result)) {
164
+ pending.push(
165
+ Promise.resolve(result).then(
166
+ () => {
167
+ __wasiWorkers.delete(worker)
168
+ },
169
+ (error) => {
170
+ cleanupErrors.push(error)
171
+ },
172
+ ),
173
+ )
174
+ } else {
175
+ __wasiWorkers.delete(worker)
176
+ }
177
+ }
178
+
179
+ const finish = () => {
180
+ if (cleanupErrors.length > 0) {
181
+ throw __createCleanupError(
182
+ cleanupErrors,
183
+ 'Failed to terminate WASI workers',
184
+ )
185
+ }
186
+ }
187
+ return pending.length > 0 ? Promise.all(pending).then(finish) : finish()
188
+ }
189
+
190
+ function __finishWasiDisposal() {
191
+ const workerResult = __terminateWasiWorkers()
192
+ if (__isThenable(workerResult)) {
193
+ return Promise.resolve(workerResult).then(__completeWasiDisposal)
194
+ }
195
+ return __completeWasiDisposal()
196
+ }
197
+
198
+ function __startWasiDisposal() {
199
+ const destroyResult = __destroyEmnapiContext()
200
+ if (__isThenable(destroyResult)) {
201
+ return Promise.resolve(destroyResult).then(__finishWasiDisposal)
202
+ }
203
+ return __finishWasiDisposal()
204
+ }
205
+
206
+ /**
207
+ * Disposes this generated WASI binding.
208
+ *
209
+ * Access this function with:
210
+ * binding[Symbol.for('napi.rs.wasi.dispose')]()
211
+ */
212
+ function __disposeWasiBinding() {
213
+ if (__wasiDisposePromise) {
214
+ return __wasiDisposePromise
215
+ }
216
+ if (__wasiDisposed) {
217
+ return Promise.resolve()
218
+ }
219
+
220
+ let resolveDispose
221
+ let rejectDispose
222
+ const disposePromise = new Promise((resolve, reject) => {
223
+ resolveDispose = resolve
224
+ rejectDispose = reject
225
+ })
226
+ __wasiDisposePromise = disposePromise
227
+
228
+ let result
229
+ try {
230
+ result = __startWasiDisposal()
231
+ } catch (error) {
232
+ __wasiDisposePromise = undefined
233
+ rejectDispose(error)
234
+ return disposePromise
235
+ }
236
+
237
+ Promise.resolve(result).then(
238
+ (value) => {
239
+ __wasiDisposed = true
240
+ resolveDispose(value)
241
+ },
242
+ (error) => {
243
+ __wasiDisposePromise = undefined
244
+ rejectDispose(error)
245
+ },
246
+ )
247
+ return disposePromise
248
+ }
249
+
250
+ function __publishWasiDispose(exports) {
251
+ Object.defineProperty(exports, __wasiDisposeSymbol, {
252
+ configurable: false,
253
+ enumerable: false,
254
+ value: __disposeWasiBinding,
255
+ writable: false,
256
+ })
257
+ }
258
+
259
+ function __finishWasiInitializationRollback(cleanupErrors) {
260
+ let workerResult
261
+ try {
262
+ workerResult = __terminateWasiWorkers()
263
+ } catch (cleanupError) {
264
+ cleanupErrors.push(cleanupError)
265
+ return cleanupErrors
266
+ }
267
+ if (__isThenable(workerResult)) {
268
+ return Promise.resolve(workerResult)
269
+ .catch((cleanupError) => {
270
+ cleanupErrors.push(cleanupError)
271
+ })
272
+ .then(() => cleanupErrors)
273
+ }
274
+ return cleanupErrors
275
+ }
276
+
277
+ function __rollbackWasiInitialization() {
278
+ const cleanupErrors = []
279
+ let destroyResult
280
+ try {
281
+ destroyResult = __destroyEmnapiContext()
282
+ } catch (cleanupError) {
283
+ cleanupErrors.push(cleanupError)
284
+ return __finishWasiInitializationRollback(cleanupErrors)
285
+ }
286
+ if (__isThenable(destroyResult)) {
287
+ return Promise.resolve(destroyResult)
288
+ .catch((cleanupError) => {
289
+ cleanupErrors.push(cleanupError)
290
+ })
291
+ .then(() => __finishWasiInitializationRollback(cleanupErrors))
292
+ }
293
+ return __finishWasiInitializationRollback(cleanupErrors)
294
+ }
295
+
296
+ let __wasiModule
297
+ let __napiModule
298
+
299
+ try {
300
+ __emnapiContext = __emnapiCreateContext({ autoDestroy: false })
301
+ __emnapiContext.suppressDestroy()
302
+
303
+ ;({
304
+ instance: __napiInstance,
305
+ module: __wasiModule,
306
+ napiModule: __napiModule,
307
+ } = __emnapiInstantiateNapiModuleSync(__wasmFile, {
308
+ context: __emnapiContext,
309
+ asyncWorkPoolSize: 4,
310
+ plugins: [__emnapiAsyncWorkPlugin, __emnapiTSFNPlugin],
311
+ wasi: __wasi,
312
+ onCreateWorker() {
313
+ const worker = new Worker(new URL('@oxc-minify/binding-wasm32-wasi/wasi-worker-browser.mjs', import.meta.url), {
314
+ type: 'module',
315
+ })
316
+ __wasiWorkers.add(worker)
317
+
318
+
319
+ return worker
320
+ },
321
+ overwriteImports(importObject) {
322
+ importObject.env = {
323
+ ...importObject.env,
324
+ ...importObject.napi,
325
+ ...importObject.emnapi,
326
+ memory: __sharedMemory,
327
+ }
328
+ return importObject
329
+ },
330
+ beforeInit({ instance }) {
331
+ __napiInstance = instance
332
+ for (const name of Object.keys(instance.exports)) {
333
+ if (name.startsWith('__napi_register__')) {
334
+ instance.exports[name]()
335
+ }
336
+ }
337
+ },
338
+ }))
339
+ __publishWasiDispose(__napiModule.exports)
340
+ } catch (error) {
341
+ const cleanupErrors = await __rollbackWasiInitialization()
342
+ throw __attachCleanupErrors(error, cleanupErrors)
343
+ }
59
344
  export default __napiModule.exports
60
345
  export const LegalCommentsMode = __napiModule.exports.LegalCommentsMode
61
346
  export const minify = __napiModule.exports.minify