@vizejs/fresco-native 0.717.0 → 0.718.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.
package/index.js CHANGED
@@ -1,368 +1,1075 @@
1
- /* tslint:disable */
2
1
  /* eslint-disable */
3
- /* prettier-ignore */
4
-
2
+ // @ts-nocheck
5
3
  /* auto-generated by NAPI-RS */
6
4
 
7
- const { existsSync } = require('fs')
8
- const { spawnSync } = require("child_process");
9
- const { join } = require("path");
5
+ const { readFileSync } = require("fs");
6
+ let nativeBinding = null;
7
+ // Which artifact actually loaded. The WASI fallback chain overwrites it with
8
+ // the flavor it resolved; the late native retry below leaves it alone because
9
+ // it only runs while no WASI candidate has been loaded. The take-over check
10
+ // after the load-failure block can also set it.
11
+ let __napiLoadedBindingTarget = "native";
12
+ // What this loader's own `requireNative()` calls returned. The check after
13
+ // the load-failure block compares it with `nativeBinding` to tell a binding
14
+ // this loader loaded from one that code outside it put in its place.
15
+ let __napiRequiredNativeBinding = null;
16
+ const loadErrors = [];
10
17
 
11
- const { platform, arch } = process;
18
+ const isMusl = () => {
19
+ let musl = false;
20
+ if (process.platform === "linux") {
21
+ musl = isMuslFromFilesystem();
22
+ if (musl === null) {
23
+ musl = isMuslFromReport();
24
+ }
25
+ if (musl === null) {
26
+ musl = isMuslFromChildProcess();
27
+ }
28
+ }
29
+ return musl;
30
+ };
12
31
 
13
- let nativeBinding = null;
14
- let localFileExisted = false;
15
- let loadError = null;
32
+ const isFileMusl = (f) => f.includes("libc.musl-") || f.includes("ld-musl-");
16
33
 
17
- function isMusl() {
18
- // For Node 10
19
- if (!process.report || typeof process.report.getReport !== "function") {
20
- const result = spawnSync("ldd", ["--version"], {
21
- encoding: "utf8",
22
- stdio: ["ignore", "pipe", "pipe"],
23
- });
24
- if (result.error) {
34
+ const isMuslFromFilesystem = () => {
35
+ try {
36
+ return readFileSync("/usr/bin/ldd", "utf-8").includes("musl");
37
+ } catch {
38
+ return null;
39
+ }
40
+ };
41
+
42
+ const isMuslFromReport = () => {
43
+ let report = null;
44
+ if (process.report && typeof process.report.getReport === "function") {
45
+ process.report.excludeNetwork = true;
46
+ report = process.report.getReport();
47
+ }
48
+ if (!report) {
49
+ return null;
50
+ }
51
+ if (report.header && report.header.glibcVersionRuntime) {
52
+ return false;
53
+ }
54
+ if (Array.isArray(report.sharedObjects)) {
55
+ if (report.sharedObjects.some(isFileMusl)) {
25
56
  return true;
26
57
  }
27
- return `${result.stdout || ""}\n${result.stderr || ""}`.includes("musl");
28
- } else {
29
- const { glibcVersionRuntime } = process.report.getReport().header;
30
- return !glibcVersionRuntime;
31
58
  }
32
- }
59
+ return false;
60
+ };
61
+
62
+ const isMuslFromChildProcess = () => {
63
+ try {
64
+ return require("child_process")
65
+ .execSync("ldd --version", { encoding: "utf8" })
66
+ .includes("musl");
67
+ } catch (e) {
68
+ // If we reach this case, we don't know if the system is musl or not, so is better to just fallback to false
69
+ return false;
70
+ }
71
+ };
33
72
 
34
- switch (platform) {
35
- case "android":
36
- switch (arch) {
37
- case "arm64":
38
- localFileExisted = existsSync(join(__dirname, "vize-fresco.android-arm64.node"));
73
+ function requireNative() {
74
+ if (process.env.NAPI_RS_NATIVE_LIBRARY_PATH) {
75
+ try {
76
+ const overrideBinding = require(process.env.NAPI_RS_NATIVE_LIBRARY_PATH);
77
+ // The override may be a generated WASI loader, which already reports its
78
+ // own flavor. Adopt it: `module.exports` aliases this object, so claiming
79
+ // 'native' would both misreport the artifact and overwrite the loader's
80
+ // marker through the alias.
81
+ __napiLoadedBindingTarget =
82
+ overrideBinding && typeof overrideBinding.__napiBindingTarget === "string"
83
+ ? overrideBinding.__napiBindingTarget
84
+ : "native";
85
+ return overrideBinding;
86
+ } catch (err) {
87
+ loadErrors.push(err);
88
+ }
89
+ } else if (process.platform === "android") {
90
+ if (process.arch === "arm64") {
91
+ try {
92
+ return require("./vize-fresco.android-arm64.node");
93
+ } catch (e) {
94
+ loadErrors.push(e);
95
+ }
96
+ try {
97
+ const binding = require("@vizejs/fresco-native-android-arm64");
98
+ const bindingPackageVersion =
99
+ require("@vizejs/fresco-native-android-arm64/package.json").version;
100
+ if (
101
+ bindingPackageVersion !== "0.474.0" &&
102
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
103
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
104
+ ) {
105
+ throw new Error(
106
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
107
+ );
108
+ }
109
+ return binding;
110
+ } catch (e) {
111
+ loadErrors.push(e);
112
+ }
113
+ } else if (process.arch === "arm") {
114
+ try {
115
+ return require("./vize-fresco.android-arm-eabi.node");
116
+ } catch (e) {
117
+ loadErrors.push(e);
118
+ }
119
+ try {
120
+ const binding = require("@vizejs/fresco-native-android-arm-eabi");
121
+ const bindingPackageVersion =
122
+ require("@vizejs/fresco-native-android-arm-eabi/package.json").version;
123
+ if (
124
+ bindingPackageVersion !== "0.474.0" &&
125
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
126
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
127
+ ) {
128
+ throw new Error(
129
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
130
+ );
131
+ }
132
+ return binding;
133
+ } catch (e) {
134
+ loadErrors.push(e);
135
+ }
136
+ } else {
137
+ loadErrors.push(new Error(`Unsupported architecture on Android ${process.arch}`));
138
+ }
139
+ } else if (process.platform === "win32") {
140
+ if (process.arch === "x64") {
141
+ if (
142
+ (process.config &&
143
+ process.config.variables &&
144
+ process.config.variables.shlib_suffix === "dll.a") ||
145
+ (process.config &&
146
+ process.config.variables &&
147
+ process.config.variables.node_target_type === "shared_library")
148
+ ) {
149
+ try {
150
+ return require("./vize-fresco.win32-x64-gnu.node");
151
+ } catch (e) {
152
+ loadErrors.push(e);
153
+ }
39
154
  try {
40
- if (localFileExisted) {
41
- nativeBinding = require("./vize-fresco.android-arm64.node");
42
- } else {
43
- nativeBinding = require("@vizejs/fresco-native-android-arm64");
155
+ const binding = require("@vizejs/fresco-native-win32-x64-gnu");
156
+ const bindingPackageVersion =
157
+ require("@vizejs/fresco-native-win32-x64-gnu/package.json").version;
158
+ if (
159
+ bindingPackageVersion !== "0.474.0" &&
160
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
161
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
162
+ ) {
163
+ throw new Error(
164
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
165
+ );
44
166
  }
167
+ return binding;
168
+ } catch (e) {
169
+ loadErrors.push(e);
170
+ }
171
+ } else {
172
+ try {
173
+ return require("./vize-fresco.win32-x64-msvc.node");
45
174
  } catch (e) {
46
- loadError = e;
175
+ loadErrors.push(e);
47
176
  }
48
- break;
49
- case "arm":
50
- localFileExisted = existsSync(join(__dirname, "vize-fresco.android-arm-eabi.node"));
51
177
  try {
52
- if (localFileExisted) {
53
- nativeBinding = require("./vize-fresco.android-arm-eabi.node");
54
- } else {
55
- nativeBinding = require("@vizejs/fresco-native-android-arm-eabi");
178
+ const binding = require("@vizejs/fresco-native-win32-x64-msvc");
179
+ const bindingPackageVersion =
180
+ require("@vizejs/fresco-native-win32-x64-msvc/package.json").version;
181
+ if (
182
+ bindingPackageVersion !== "0.474.0" &&
183
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
184
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
185
+ ) {
186
+ throw new Error(
187
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
188
+ );
56
189
  }
190
+ return binding;
57
191
  } catch (e) {
58
- loadError = e;
192
+ loadErrors.push(e);
193
+ }
194
+ }
195
+ } else if (process.arch === "ia32") {
196
+ try {
197
+ return require("./vize-fresco.win32-ia32-msvc.node");
198
+ } catch (e) {
199
+ loadErrors.push(e);
200
+ }
201
+ try {
202
+ const binding = require("@vizejs/fresco-native-win32-ia32-msvc");
203
+ const bindingPackageVersion =
204
+ require("@vizejs/fresco-native-win32-ia32-msvc/package.json").version;
205
+ if (
206
+ bindingPackageVersion !== "0.474.0" &&
207
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
208
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
209
+ ) {
210
+ throw new Error(
211
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
212
+ );
213
+ }
214
+ return binding;
215
+ } catch (e) {
216
+ loadErrors.push(e);
217
+ }
218
+ } else if (process.arch === "arm64") {
219
+ try {
220
+ return require("./vize-fresco.win32-arm64-msvc.node");
221
+ } catch (e) {
222
+ loadErrors.push(e);
223
+ }
224
+ try {
225
+ const binding = require("@vizejs/fresco-native-win32-arm64-msvc");
226
+ const bindingPackageVersion =
227
+ require("@vizejs/fresco-native-win32-arm64-msvc/package.json").version;
228
+ if (
229
+ bindingPackageVersion !== "0.474.0" &&
230
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
231
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
232
+ ) {
233
+ throw new Error(
234
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
235
+ );
236
+ }
237
+ return binding;
238
+ } catch (e) {
239
+ loadErrors.push(e);
240
+ }
241
+ } else {
242
+ loadErrors.push(new Error(`Unsupported architecture on Windows: ${process.arch}`));
243
+ }
244
+ } else if (process.platform === "darwin") {
245
+ try {
246
+ return require("./vize-fresco.darwin-universal.node");
247
+ } catch (e) {
248
+ loadErrors.push(e);
249
+ }
250
+ try {
251
+ const binding = require("@vizejs/fresco-native-darwin-universal");
252
+ const bindingPackageVersion =
253
+ require("@vizejs/fresco-native-darwin-universal/package.json").version;
254
+ if (
255
+ bindingPackageVersion !== "0.474.0" &&
256
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
257
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
258
+ ) {
259
+ throw new Error(
260
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
261
+ );
262
+ }
263
+ return binding;
264
+ } catch (e) {
265
+ loadErrors.push(e);
266
+ }
267
+ if (process.arch === "x64") {
268
+ try {
269
+ return require("./vize-fresco.darwin-x64.node");
270
+ } catch (e) {
271
+ loadErrors.push(e);
272
+ }
273
+ try {
274
+ const binding = require("@vizejs/fresco-native-darwin-x64");
275
+ const bindingPackageVersion =
276
+ require("@vizejs/fresco-native-darwin-x64/package.json").version;
277
+ if (
278
+ bindingPackageVersion !== "0.474.0" &&
279
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
280
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
281
+ ) {
282
+ throw new Error(
283
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
284
+ );
285
+ }
286
+ return binding;
287
+ } catch (e) {
288
+ loadErrors.push(e);
289
+ }
290
+ } else if (process.arch === "arm64") {
291
+ try {
292
+ return require("./vize-fresco.darwin-arm64.node");
293
+ } catch (e) {
294
+ loadErrors.push(e);
295
+ }
296
+ try {
297
+ const binding = require("@vizejs/fresco-native-darwin-arm64");
298
+ const bindingPackageVersion =
299
+ require("@vizejs/fresco-native-darwin-arm64/package.json").version;
300
+ if (
301
+ bindingPackageVersion !== "0.474.0" &&
302
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
303
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
304
+ ) {
305
+ throw new Error(
306
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
307
+ );
59
308
  }
60
- break;
61
- default:
62
- throw new Error(`Unsupported architecture on Android ${arch}`);
309
+ return binding;
310
+ } catch (e) {
311
+ loadErrors.push(e);
312
+ }
313
+ } else {
314
+ loadErrors.push(new Error(`Unsupported architecture on macOS: ${process.arch}`));
63
315
  }
64
- break;
65
- case "win32":
66
- switch (arch) {
67
- case "x64":
68
- localFileExisted = existsSync(join(__dirname, "vize-fresco.win32-x64-msvc.node"));
316
+ } else if (process.platform === "freebsd") {
317
+ if (process.arch === "x64") {
318
+ try {
319
+ return require("./vize-fresco.freebsd-x64.node");
320
+ } catch (e) {
321
+ loadErrors.push(e);
322
+ }
323
+ try {
324
+ const binding = require("@vizejs/fresco-native-freebsd-x64");
325
+ const bindingPackageVersion =
326
+ require("@vizejs/fresco-native-freebsd-x64/package.json").version;
327
+ if (
328
+ bindingPackageVersion !== "0.474.0" &&
329
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
330
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
331
+ ) {
332
+ throw new Error(
333
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
334
+ );
335
+ }
336
+ return binding;
337
+ } catch (e) {
338
+ loadErrors.push(e);
339
+ }
340
+ } else if (process.arch === "arm64") {
341
+ try {
342
+ return require("./vize-fresco.freebsd-arm64.node");
343
+ } catch (e) {
344
+ loadErrors.push(e);
345
+ }
346
+ try {
347
+ const binding = require("@vizejs/fresco-native-freebsd-arm64");
348
+ const bindingPackageVersion =
349
+ require("@vizejs/fresco-native-freebsd-arm64/package.json").version;
350
+ if (
351
+ bindingPackageVersion !== "0.474.0" &&
352
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
353
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
354
+ ) {
355
+ throw new Error(
356
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
357
+ );
358
+ }
359
+ return binding;
360
+ } catch (e) {
361
+ loadErrors.push(e);
362
+ }
363
+ } else if (process.arch === "ppc64") {
364
+ try {
365
+ return require("./vize-fresco.freebsd-ppc64.node");
366
+ } catch (e) {
367
+ loadErrors.push(e);
368
+ }
369
+ try {
370
+ const binding = require("@vizejs/fresco-native-freebsd-ppc64");
371
+ const bindingPackageVersion =
372
+ require("@vizejs/fresco-native-freebsd-ppc64/package.json").version;
373
+ if (
374
+ bindingPackageVersion !== "0.474.0" &&
375
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
376
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
377
+ ) {
378
+ throw new Error(
379
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
380
+ );
381
+ }
382
+ return binding;
383
+ } catch (e) {
384
+ loadErrors.push(e);
385
+ }
386
+ } else {
387
+ loadErrors.push(new Error(`Unsupported architecture on FreeBSD: ${process.arch}`));
388
+ }
389
+ } else if (process.platform === "linux") {
390
+ if (process.arch === "x64") {
391
+ if (isMusl()) {
69
392
  try {
70
- if (localFileExisted) {
71
- nativeBinding = require("./vize-fresco.win32-x64-msvc.node");
72
- } else {
73
- nativeBinding = require("@vizejs/fresco-native-win32-x64-msvc");
74
- }
393
+ return require("./vize-fresco.linux-x64-musl.node");
75
394
  } catch (e) {
76
- loadError = e;
395
+ loadErrors.push(e);
77
396
  }
78
- break;
79
- case "ia32":
80
- localFileExisted = existsSync(join(__dirname, "vize-fresco.win32-ia32-msvc.node"));
81
397
  try {
82
- if (localFileExisted) {
83
- nativeBinding = require("./vize-fresco.win32-ia32-msvc.node");
84
- } else {
85
- nativeBinding = require("@vizejs/fresco-native-win32-ia32-msvc");
398
+ const binding = require("@vizejs/fresco-native-linux-x64-musl");
399
+ const bindingPackageVersion =
400
+ require("@vizejs/fresco-native-linux-x64-musl/package.json").version;
401
+ if (
402
+ bindingPackageVersion !== "0.474.0" &&
403
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
404
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
405
+ ) {
406
+ throw new Error(
407
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
408
+ );
86
409
  }
410
+ return binding;
87
411
  } catch (e) {
88
- loadError = e;
412
+ loadErrors.push(e);
89
413
  }
90
- break;
91
- case "arm64":
92
- localFileExisted = existsSync(join(__dirname, "vize-fresco.win32-arm64-msvc.node"));
414
+ } else {
93
415
  try {
94
- if (localFileExisted) {
95
- nativeBinding = require("./vize-fresco.win32-arm64-msvc.node");
96
- } else {
97
- nativeBinding = require("@vizejs/fresco-native-win32-arm64-msvc");
416
+ return require("./vize-fresco.linux-x64-gnu.node");
417
+ } catch (e) {
418
+ loadErrors.push(e);
419
+ }
420
+ try {
421
+ const binding = require("@vizejs/fresco-native-linux-x64-gnu");
422
+ const bindingPackageVersion =
423
+ require("@vizejs/fresco-native-linux-x64-gnu/package.json").version;
424
+ if (
425
+ bindingPackageVersion !== "0.474.0" &&
426
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
427
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
428
+ ) {
429
+ throw new Error(
430
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
431
+ );
98
432
  }
433
+ return binding;
99
434
  } catch (e) {
100
- loadError = e;
435
+ loadErrors.push(e);
101
436
  }
102
- break;
103
- default:
104
- throw new Error(`Unsupported architecture on Windows: ${arch}`);
105
- }
106
- break;
107
- case "darwin":
108
- localFileExisted = existsSync(join(__dirname, "vize-fresco.darwin-universal.node"));
109
- try {
110
- if (localFileExisted) {
111
- nativeBinding = require("./vize-fresco.darwin-universal.node");
112
- } else {
113
- nativeBinding = require("@vizejs/fresco-native-darwin-universal");
114
437
  }
115
- break;
116
- } catch {}
117
- switch (arch) {
118
- case "x64":
119
- localFileExisted = existsSync(join(__dirname, "vize-fresco.darwin-x64.node"));
438
+ } else if (process.arch === "arm64") {
439
+ if (isMusl()) {
120
440
  try {
121
- if (localFileExisted) {
122
- nativeBinding = require("./vize-fresco.darwin-x64.node");
123
- } else {
124
- nativeBinding = require("@vizejs/fresco-native-darwin-x64");
125
- }
441
+ return require("./vize-fresco.linux-arm64-musl.node");
126
442
  } catch (e) {
127
- loadError = e;
443
+ loadErrors.push(e);
128
444
  }
129
- break;
130
- case "arm64":
131
- localFileExisted = existsSync(join(__dirname, "vize-fresco.darwin-arm64.node"));
132
445
  try {
133
- if (localFileExisted) {
134
- nativeBinding = require("./vize-fresco.darwin-arm64.node");
135
- } else {
136
- nativeBinding = require("@vizejs/fresco-native-darwin-arm64");
446
+ const binding = require("@vizejs/fresco-native-linux-arm64-musl");
447
+ const bindingPackageVersion =
448
+ require("@vizejs/fresco-native-linux-arm64-musl/package.json").version;
449
+ if (
450
+ bindingPackageVersion !== "0.474.0" &&
451
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
452
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
453
+ ) {
454
+ throw new Error(
455
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
456
+ );
137
457
  }
458
+ return binding;
138
459
  } catch (e) {
139
- loadError = e;
460
+ loadErrors.push(e);
140
461
  }
141
- break;
142
- default:
143
- throw new Error(`Unsupported architecture on macOS: ${arch}`);
144
- }
145
- break;
146
- case "freebsd":
147
- if (arch !== "x64") {
148
- throw new Error(`Unsupported architecture on FreeBSD: ${arch}`);
149
- }
150
- localFileExisted = existsSync(join(__dirname, "vize-fresco.freebsd-x64.node"));
151
- try {
152
- if (localFileExisted) {
153
- nativeBinding = require("./vize-fresco.freebsd-x64.node");
154
462
  } else {
155
- nativeBinding = require("@vizejs/fresco-native-freebsd-x64");
156
- }
157
- } catch (e) {
158
- loadError = e;
159
- }
160
- break;
161
- case "linux":
162
- switch (arch) {
163
- case "x64":
164
- if (isMusl()) {
165
- localFileExisted = existsSync(join(__dirname, "vize-fresco.linux-x64-musl.node"));
166
- try {
167
- if (localFileExisted) {
168
- nativeBinding = require("./vize-fresco.linux-x64-musl.node");
169
- } else {
170
- nativeBinding = require("@vizejs/fresco-native-linux-x64-musl");
171
- }
172
- } catch (e) {
173
- loadError = e;
463
+ try {
464
+ return require("./vize-fresco.linux-arm64-gnu.node");
465
+ } catch (e) {
466
+ loadErrors.push(e);
467
+ }
468
+ try {
469
+ const binding = require("@vizejs/fresco-native-linux-arm64-gnu");
470
+ const bindingPackageVersion =
471
+ require("@vizejs/fresco-native-linux-arm64-gnu/package.json").version;
472
+ if (
473
+ bindingPackageVersion !== "0.474.0" &&
474
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
475
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
476
+ ) {
477
+ throw new Error(
478
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
479
+ );
174
480
  }
175
- } else {
176
- localFileExisted = existsSync(join(__dirname, "vize-fresco.linux-x64-gnu.node"));
177
- try {
178
- if (localFileExisted) {
179
- nativeBinding = require("./vize-fresco.linux-x64-gnu.node");
180
- } else {
181
- nativeBinding = require("@vizejs/fresco-native-linux-x64-gnu");
182
- }
183
- } catch (e) {
184
- loadError = e;
481
+ return binding;
482
+ } catch (e) {
483
+ loadErrors.push(e);
484
+ }
485
+ }
486
+ } else if (process.arch === "arm") {
487
+ if (isMusl()) {
488
+ try {
489
+ return require("./vize-fresco.linux-arm-musleabihf.node");
490
+ } catch (e) {
491
+ loadErrors.push(e);
492
+ }
493
+ try {
494
+ const binding = require("@vizejs/fresco-native-linux-arm-musleabihf");
495
+ const bindingPackageVersion =
496
+ require("@vizejs/fresco-native-linux-arm-musleabihf/package.json").version;
497
+ if (
498
+ bindingPackageVersion !== "0.474.0" &&
499
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
500
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
501
+ ) {
502
+ throw new Error(
503
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
504
+ );
185
505
  }
506
+ return binding;
507
+ } catch (e) {
508
+ loadErrors.push(e);
186
509
  }
187
- break;
188
- case "arm64":
189
- if (isMusl()) {
190
- localFileExisted = existsSync(join(__dirname, "vize-fresco.linux-arm64-musl.node"));
191
- try {
192
- if (localFileExisted) {
193
- nativeBinding = require("./vize-fresco.linux-arm64-musl.node");
194
- } else {
195
- nativeBinding = require("@vizejs/fresco-native-linux-arm64-musl");
196
- }
197
- } catch (e) {
198
- loadError = e;
510
+ } else {
511
+ try {
512
+ return require("./vize-fresco.linux-arm-gnueabihf.node");
513
+ } catch (e) {
514
+ loadErrors.push(e);
515
+ }
516
+ try {
517
+ const binding = require("@vizejs/fresco-native-linux-arm-gnueabihf");
518
+ const bindingPackageVersion =
519
+ require("@vizejs/fresco-native-linux-arm-gnueabihf/package.json").version;
520
+ if (
521
+ bindingPackageVersion !== "0.474.0" &&
522
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
523
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
524
+ ) {
525
+ throw new Error(
526
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
527
+ );
199
528
  }
200
- } else {
201
- localFileExisted = existsSync(join(__dirname, "vize-fresco.linux-arm64-gnu.node"));
202
- try {
203
- if (localFileExisted) {
204
- nativeBinding = require("./vize-fresco.linux-arm64-gnu.node");
205
- } else {
206
- nativeBinding = require("@vizejs/fresco-native-linux-arm64-gnu");
207
- }
208
- } catch (e) {
209
- loadError = e;
529
+ return binding;
530
+ } catch (e) {
531
+ loadErrors.push(e);
532
+ }
533
+ }
534
+ } else if (process.arch === "loong64") {
535
+ if (isMusl()) {
536
+ try {
537
+ return require("./vize-fresco.linux-loong64-musl.node");
538
+ } catch (e) {
539
+ loadErrors.push(e);
540
+ }
541
+ try {
542
+ const binding = require("@vizejs/fresco-native-linux-loong64-musl");
543
+ const bindingPackageVersion =
544
+ require("@vizejs/fresco-native-linux-loong64-musl/package.json").version;
545
+ if (
546
+ bindingPackageVersion !== "0.474.0" &&
547
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
548
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
549
+ ) {
550
+ throw new Error(
551
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
552
+ );
210
553
  }
554
+ return binding;
555
+ } catch (e) {
556
+ loadErrors.push(e);
211
557
  }
212
- break;
213
- case "arm":
214
- if (isMusl()) {
215
- localFileExisted = existsSync(join(__dirname, "vize-fresco.linux-arm-musleabihf.node"));
216
- try {
217
- if (localFileExisted) {
218
- nativeBinding = require("./vize-fresco.linux-arm-musleabihf.node");
219
- } else {
220
- nativeBinding = require("@vizejs/fresco-native-linux-arm-musleabihf");
221
- }
222
- } catch (e) {
223
- loadError = e;
558
+ } else {
559
+ try {
560
+ return require("./vize-fresco.linux-loong64-gnu.node");
561
+ } catch (e) {
562
+ loadErrors.push(e);
563
+ }
564
+ try {
565
+ const binding = require("@vizejs/fresco-native-linux-loong64-gnu");
566
+ const bindingPackageVersion =
567
+ require("@vizejs/fresco-native-linux-loong64-gnu/package.json").version;
568
+ if (
569
+ bindingPackageVersion !== "0.474.0" &&
570
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
571
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
572
+ ) {
573
+ throw new Error(
574
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
575
+ );
224
576
  }
225
- } else {
226
- localFileExisted = existsSync(join(__dirname, "vize-fresco.linux-arm-gnueabihf.node"));
227
- try {
228
- if (localFileExisted) {
229
- nativeBinding = require("./vize-fresco.linux-arm-gnueabihf.node");
230
- } else {
231
- nativeBinding = require("@vizejs/fresco-native-linux-arm-gnueabihf");
232
- }
233
- } catch (e) {
234
- loadError = e;
577
+ return binding;
578
+ } catch (e) {
579
+ loadErrors.push(e);
580
+ }
581
+ }
582
+ } else if (process.arch === "riscv64") {
583
+ if (isMusl()) {
584
+ try {
585
+ return require("./vize-fresco.linux-riscv64-musl.node");
586
+ } catch (e) {
587
+ loadErrors.push(e);
588
+ }
589
+ try {
590
+ const binding = require("@vizejs/fresco-native-linux-riscv64-musl");
591
+ const bindingPackageVersion =
592
+ require("@vizejs/fresco-native-linux-riscv64-musl/package.json").version;
593
+ if (
594
+ bindingPackageVersion !== "0.474.0" &&
595
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
596
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
597
+ ) {
598
+ throw new Error(
599
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
600
+ );
235
601
  }
602
+ return binding;
603
+ } catch (e) {
604
+ loadErrors.push(e);
605
+ }
606
+ } else {
607
+ try {
608
+ return require("./vize-fresco.linux-riscv64-gnu.node");
609
+ } catch (e) {
610
+ loadErrors.push(e);
236
611
  }
237
- break;
238
- case "riscv64":
239
- if (isMusl()) {
240
- localFileExisted = existsSync(join(__dirname, "vize-fresco.linux-riscv64-musl.node"));
241
- try {
242
- if (localFileExisted) {
243
- nativeBinding = require("./vize-fresco.linux-riscv64-musl.node");
244
- } else {
245
- nativeBinding = require("@vizejs/fresco-native-linux-riscv64-musl");
246
- }
247
- } catch (e) {
248
- loadError = e;
612
+ try {
613
+ const binding = require("@vizejs/fresco-native-linux-riscv64-gnu");
614
+ const bindingPackageVersion =
615
+ require("@vizejs/fresco-native-linux-riscv64-gnu/package.json").version;
616
+ if (
617
+ bindingPackageVersion !== "0.474.0" &&
618
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
619
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
620
+ ) {
621
+ throw new Error(
622
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
623
+ );
249
624
  }
250
- } else {
251
- localFileExisted = existsSync(join(__dirname, "vize-fresco.linux-riscv64-gnu.node"));
252
- try {
253
- if (localFileExisted) {
254
- nativeBinding = require("./vize-fresco.linux-riscv64-gnu.node");
255
- } else {
256
- nativeBinding = require("@vizejs/fresco-native-linux-riscv64-gnu");
257
- }
258
- } catch (e) {
259
- loadError = e;
625
+ return binding;
626
+ } catch (e) {
627
+ loadErrors.push(e);
628
+ }
629
+ }
630
+ } else if (process.arch === "ppc64") {
631
+ try {
632
+ return require("./vize-fresco.linux-ppc64-gnu.node");
633
+ } catch (e) {
634
+ loadErrors.push(e);
635
+ }
636
+ try {
637
+ const binding = require("@vizejs/fresco-native-linux-ppc64-gnu");
638
+ const bindingPackageVersion =
639
+ require("@vizejs/fresco-native-linux-ppc64-gnu/package.json").version;
640
+ if (
641
+ bindingPackageVersion !== "0.474.0" &&
642
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
643
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
644
+ ) {
645
+ throw new Error(
646
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
647
+ );
648
+ }
649
+ return binding;
650
+ } catch (e) {
651
+ loadErrors.push(e);
652
+ }
653
+ } else if (process.arch === "s390x") {
654
+ try {
655
+ return require("./vize-fresco.linux-s390x-gnu.node");
656
+ } catch (e) {
657
+ loadErrors.push(e);
658
+ }
659
+ try {
660
+ const binding = require("@vizejs/fresco-native-linux-s390x-gnu");
661
+ const bindingPackageVersion =
662
+ require("@vizejs/fresco-native-linux-s390x-gnu/package.json").version;
663
+ if (
664
+ bindingPackageVersion !== "0.474.0" &&
665
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
666
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
667
+ ) {
668
+ throw new Error(
669
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
670
+ );
671
+ }
672
+ return binding;
673
+ } catch (e) {
674
+ loadErrors.push(e);
675
+ }
676
+ } else {
677
+ loadErrors.push(new Error(`Unsupported architecture on Linux: ${process.arch}`));
678
+ }
679
+ } else if (process.platform === "openharmony") {
680
+ if (process.arch === "arm64") {
681
+ try {
682
+ return require("./vize-fresco.openharmony-arm64.node");
683
+ } catch (e) {
684
+ loadErrors.push(e);
685
+ }
686
+ try {
687
+ const binding = require("@vizejs/fresco-native-openharmony-arm64");
688
+ const bindingPackageVersion =
689
+ require("@vizejs/fresco-native-openharmony-arm64/package.json").version;
690
+ if (
691
+ bindingPackageVersion !== "0.474.0" &&
692
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
693
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
694
+ ) {
695
+ throw new Error(
696
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
697
+ );
698
+ }
699
+ return binding;
700
+ } catch (e) {
701
+ loadErrors.push(e);
702
+ }
703
+ } else if (process.arch === "x64") {
704
+ try {
705
+ return require("./vize-fresco.openharmony-x64.node");
706
+ } catch (e) {
707
+ loadErrors.push(e);
708
+ }
709
+ try {
710
+ const binding = require("@vizejs/fresco-native-openharmony-x64");
711
+ const bindingPackageVersion =
712
+ require("@vizejs/fresco-native-openharmony-x64/package.json").version;
713
+ if (
714
+ bindingPackageVersion !== "0.474.0" &&
715
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
716
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
717
+ ) {
718
+ throw new Error(
719
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
720
+ );
721
+ }
722
+ return binding;
723
+ } catch (e) {
724
+ loadErrors.push(e);
725
+ }
726
+ } else if (process.arch === "arm") {
727
+ try {
728
+ return require("./vize-fresco.openharmony-arm.node");
729
+ } catch (e) {
730
+ loadErrors.push(e);
731
+ }
732
+ try {
733
+ const binding = require("@vizejs/fresco-native-openharmony-arm");
734
+ const bindingPackageVersion =
735
+ require("@vizejs/fresco-native-openharmony-arm/package.json").version;
736
+ if (
737
+ bindingPackageVersion !== "0.474.0" &&
738
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
739
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
740
+ ) {
741
+ throw new Error(
742
+ `Native binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
743
+ );
744
+ }
745
+ return binding;
746
+ } catch (e) {
747
+ loadErrors.push(e);
748
+ }
749
+ } else {
750
+ loadErrors.push(new Error(`Unsupported architecture on OpenHarmony: ${process.arch}`));
751
+ }
752
+ } else {
753
+ loadErrors.push(
754
+ new Error(`Unsupported OS: ${process.platform}, architecture: ${process.arch}`),
755
+ );
756
+ }
757
+ }
758
+
759
+ function createLoadErrorChain(errors) {
760
+ return errors.reduce((previous, current) => {
761
+ let message;
762
+ try {
763
+ message = current && typeof current.message === "string" ? current.message : String(current);
764
+ } catch {
765
+ message = "Unknown error";
766
+ }
767
+ const error = new Error(message);
768
+ error.cause = previous;
769
+ return error;
770
+ }, null);
771
+ }
772
+
773
+ // NAPI_RS_FORCE_WASI is a tri-state flag:
774
+ // unset / any other value → native binding preferred, WASI is only a fallback
775
+ // 'true' → prefer WASI, but retain native as a lazy fallback
776
+ // 'error' → require WASI without initializing a native fallback
777
+ // Treating any non-empty string as truthy (the historical behavior) meant
778
+ // NAPI_RS_FORCE_WASI=false, NAPI_RS_FORCE_WASI=0, etc. inadvertently triggered
779
+ // the WASI path, causing ENOENT for packages shipped without a .wasi.cjs file.
780
+ //
781
+ // NAPI_RS_WASI_FLAVOR selects one exact generated flavor and implies strict
782
+ // WASI loading. It never crosses into another flavor or falls back to native.
783
+ const __napiWasiFlavors = ["wasm32-wasi"];
784
+ const __napiWasiFlavor = process.env.NAPI_RS_WASI_FLAVOR;
785
+ const __napiWasiFlavorRequested =
786
+ typeof __napiWasiFlavor === "string" && __napiWasiFlavor.length > 0;
787
+ if (__napiWasiFlavorRequested && __napiWasiFlavors.indexOf(__napiWasiFlavor) === -1) {
788
+ throw new Error(
789
+ 'Unsupported WASI flavor "' +
790
+ __napiWasiFlavor +
791
+ '". Available flavors: ' +
792
+ __napiWasiFlavors.join(", "),
793
+ );
794
+ }
795
+ const forceWasiError = process.env.NAPI_RS_FORCE_WASI === "error";
796
+ const forceWasi =
797
+ process.env.NAPI_RS_FORCE_WASI === "true" || forceWasiError || __napiWasiFlavorRequested;
798
+
799
+ if (!forceWasi) {
800
+ nativeBinding = requireNative();
801
+ __napiRequiredNativeBinding = nativeBinding;
802
+ }
803
+
804
+ if (!nativeBinding || forceWasi) {
805
+ let wasiBinding = null;
806
+ let wasiBindingLoaded = false;
807
+ const wasiBindingErrors = [];
808
+ const __napiWasiResolveCandidate = (specifier, isPackage, localArtifacts) => {
809
+ try {
810
+ require.resolve(specifier);
811
+ } catch (resolveError) {
812
+ if (!resolveError || resolveError.code !== "MODULE_NOT_FOUND") {
813
+ throw resolveError;
814
+ }
815
+ if (isPackage) {
816
+ try {
817
+ require.resolve(specifier + "/package.json");
818
+ } catch (packageError) {
819
+ if (packageError && packageError.code === "MODULE_NOT_FOUND") {
820
+ return resolveError;
260
821
  }
822
+ // An exports restriction proves the package exists even when its
823
+ // package.json is not public. Preserve the root resolution failure.
824
+ throw resolveError;
261
825
  }
262
- break;
263
- case "s390x":
264
- localFileExisted = existsSync(join(__dirname, "vize-fresco.linux-s390x-gnu.node"));
826
+ // The package exists but its main/export target is broken.
827
+ throw resolveError;
828
+ }
829
+ return resolveError;
830
+ }
831
+ if (localArtifacts) {
832
+ let artifactError = null;
833
+ for (let i = 0; i < localArtifacts.length; i++) {
265
834
  try {
266
- if (localFileExisted) {
267
- nativeBinding = require("./vize-fresco.linux-s390x-gnu.node");
268
- } else {
269
- nativeBinding = require("@vizejs/fresco-native-linux-s390x-gnu");
835
+ require.resolve(localArtifacts[i]);
836
+ return null;
837
+ } catch (resolveError) {
838
+ if (!resolveError || resolveError.code !== "MODULE_NOT_FOUND") {
839
+ throw resolveError;
270
840
  }
271
- } catch (e) {
272
- loadError = e;
841
+ artifactError = resolveError;
273
842
  }
274
- break;
275
- default:
276
- throw new Error(`Unsupported architecture on Linux: ${arch}`);
843
+ }
844
+ return artifactError;
277
845
  }
278
- break;
279
- default:
280
- throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`);
846
+ return null;
847
+ };
848
+ {
849
+ // The local artifact is always the first WASI candidate. Keep this scope
850
+ // separate because the package fallback below reuses the candidate names.
851
+ let candidateError = null;
852
+ let candidateFailed = false;
853
+ try {
854
+ candidateError = __napiWasiResolveCandidate("./vize-fresco.wasi.cjs", false, [
855
+ "./vize-fresco.wasm32-wasi.debug.wasm",
856
+ "./vize-fresco.wasm32-wasi.wasm",
857
+ ]);
858
+ candidateFailed = candidateError !== null;
859
+ if (!candidateFailed) {
860
+ wasiBinding = require("./vize-fresco.wasi.cjs");
861
+ nativeBinding = wasiBinding;
862
+ __napiLoadedBindingTarget = "wasm32-wasi";
863
+ wasiBindingLoaded = true;
864
+ }
865
+ } catch (err) {
866
+ candidateError = err;
867
+ candidateFailed = true;
868
+ }
869
+ if (candidateFailed) {
870
+ wasiBindingErrors.push(candidateError);
871
+ loadErrors.push(candidateError);
872
+ }
873
+ }
874
+ if (!wasiBindingLoaded) {
875
+ let candidateError = null;
876
+ let candidateFailed = false;
877
+ try {
878
+ candidateError = __napiWasiResolveCandidate(
879
+ "@vizejs/fresco-native-wasm32-wasi",
880
+ true,
881
+ undefined,
882
+ );
883
+ candidateFailed = candidateError !== null;
884
+ if (!candidateFailed) {
885
+ if (
886
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
887
+ process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== "0"
888
+ ) {
889
+ const bindingPackageVersion =
890
+ require("@vizejs/fresco-native-wasm32-wasi/package.json").version;
891
+ if (bindingPackageVersion !== "0.474.0") {
892
+ throw new Error(
893
+ `WASI binding package version mismatch, expected 0.474.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`,
894
+ );
895
+ }
896
+ }
897
+ wasiBinding = require("@vizejs/fresco-native-wasm32-wasi");
898
+ nativeBinding = wasiBinding;
899
+ __napiLoadedBindingTarget = "wasm32-wasi";
900
+ wasiBindingLoaded = true;
901
+ }
902
+ } catch (err) {
903
+ candidateError = err;
904
+ candidateFailed = true;
905
+ }
906
+ if (candidateFailed) {
907
+ wasiBindingErrors.push(candidateError);
908
+ loadErrors.push(candidateError);
909
+ }
910
+ }
911
+ if (!wasiBindingLoaded && forceWasi && !forceWasiError && !__napiWasiFlavorRequested) {
912
+ nativeBinding = requireNative();
913
+ __napiRequiredNativeBinding = nativeBinding;
914
+ }
915
+ if ((forceWasiError || __napiWasiFlavorRequested) && !wasiBindingLoaded) {
916
+ const error = new Error(
917
+ __napiWasiFlavorRequested
918
+ ? 'WASI binding for flavor "' + __napiWasiFlavor + '" not found'
919
+ : "WASI binding not found and NAPI_RS_FORCE_WASI is set to error",
920
+ );
921
+ error.cause = createLoadErrorChain(wasiBindingErrors);
922
+ throw error;
923
+ }
281
924
  }
282
925
 
283
926
  if (!nativeBinding) {
284
- if (loadError) {
285
- throw loadError;
927
+ if (loadErrors.length > 0) {
928
+ const error = new Error(
929
+ `Cannot find native binding. ` +
930
+ `npm has a bug related to optional dependencies (https://github.com/npm/cli/issues/4828). ` +
931
+ "Please try `npm i` again after removing both package-lock.json and node_modules directory.",
932
+ );
933
+ // assign instead of the `new Error(message, { cause })` options form,
934
+ // which Node < 16.9 silently ignores
935
+ error.cause = createLoadErrorChain(loadErrors);
936
+ throw error;
286
937
  }
287
938
  throw new Error(`Failed to load native binding`);
288
939
  }
289
940
 
290
- const {
291
- pollEvent,
292
- pollEventNonBlocking,
293
- readEvent,
294
- getImeState,
295
- enableIme,
296
- disableIme,
297
- setImeMode,
298
- initLayout,
299
- createLayoutNode,
300
- createLayoutLeaf,
301
- setLayoutRoot,
302
- addLayoutChild,
303
- removeLayoutChild,
304
- setLayoutStyle,
305
- removeLayoutNode,
306
- computeLayout,
307
- getLayout,
308
- getAllLayouts,
309
- clearLayout,
310
- renderText,
311
- renderBox,
312
- fillRect,
313
- clearRect,
314
- setCursor,
315
- showCursor,
316
- hideCursor,
317
- setCursorShape,
318
- renderTree,
319
- getLastRenderLayouts,
320
- initTerminal,
321
- initTerminalWithMouse,
322
- initTerminalWithOptions,
323
- restoreTerminal,
324
- getTerminalInfo,
325
- clearScreen,
326
- flushTerminal,
327
- flushTerminalMeasured,
328
- syncTerminalSize,
329
- } = nativeBinding;
941
+ // A package can patch its own fallback in front of the load-failure check
942
+ // above, e.g. a WebContainer download through `napi-postinstall/fallback`.
943
+ // That fallback swaps `nativeBinding` for a generated WASI loader, which has
944
+ // already stamped its own flavor, while the target above still says 'native'.
945
+ // Take that flavor over, or the CommonJS stamp throws a conflict and the ESM
946
+ // export reports the wrong artifact. Three limits keep the conflict check:
947
+ // - only a binding this loader did not load itself, so a native addon whose
948
+ // `#[napi(module_exports)]` hook claims the name is still rejected;
949
+ // - only an own data property, read through its descriptor, so no addon
950
+ // getter runs here, and it must read back the same, as the stamp reads it;
951
+ // - only a flavor in `__napiWasiFlavors`, the ones this loader's own WASI
952
+ // chain can report ('wasm32-wasi' for a package without a WASI target).
953
+ // This is metadata and never fails a load: a throwing Proxy trap keeps 'native'.
954
+ if (__napiLoadedBindingTarget === "native" && nativeBinding !== __napiRequiredNativeBinding) {
955
+ try {
956
+ const __napiBindingTargetDescriptor = Object.getOwnPropertyDescriptor(
957
+ nativeBinding,
958
+ "__napiBindingTarget",
959
+ );
960
+ if (
961
+ __napiBindingTargetDescriptor &&
962
+ __napiWasiFlavors.indexOf(__napiBindingTargetDescriptor.value) !== -1 &&
963
+ nativeBinding.__napiBindingTarget === __napiBindingTargetDescriptor.value
964
+ ) {
965
+ __napiLoadedBindingTarget = __napiBindingTargetDescriptor.value;
966
+ }
967
+ } catch {
968
+ // keep 'native'
969
+ }
970
+ }
330
971
 
331
- module.exports.pollEvent = pollEvent;
332
- module.exports.pollEventNonBlocking = pollEventNonBlocking;
333
- module.exports.readEvent = readEvent;
334
- module.exports.getImeState = getImeState;
335
- module.exports.enableIme = enableIme;
336
- module.exports.disableIme = disableIme;
337
- module.exports.setImeMode = setImeMode;
338
- module.exports.initLayout = initLayout;
339
- module.exports.createLayoutNode = createLayoutNode;
340
- module.exports.createLayoutLeaf = createLayoutLeaf;
341
- module.exports.setLayoutRoot = setLayoutRoot;
342
- module.exports.addLayoutChild = addLayoutChild;
343
- module.exports.removeLayoutChild = removeLayoutChild;
344
- module.exports.setLayoutStyle = setLayoutStyle;
345
- module.exports.removeLayoutNode = removeLayoutNode;
346
- module.exports.computeLayout = computeLayout;
347
- module.exports.getLayout = getLayout;
348
- module.exports.getAllLayouts = getAllLayouts;
349
- module.exports.clearLayout = clearLayout;
350
- module.exports.renderText = renderText;
351
- module.exports.renderBox = renderBox;
352
- module.exports.fillRect = fillRect;
353
- module.exports.clearRect = clearRect;
354
- module.exports.setCursor = setCursor;
355
- module.exports.showCursor = showCursor;
356
- module.exports.hideCursor = hideCursor;
357
- module.exports.setCursorShape = setCursorShape;
358
- module.exports.renderTree = renderTree;
359
- module.exports.getLastRenderLayouts = getLastRenderLayouts;
360
- module.exports.initTerminal = initTerminal;
361
- module.exports.initTerminalWithMouse = initTerminalWithMouse;
362
- module.exports.initTerminalWithOptions = initTerminalWithOptions;
363
- module.exports.restoreTerminal = restoreTerminal;
364
- module.exports.getTerminalInfo = getTerminalInfo;
365
- module.exports.clearScreen = clearScreen;
366
- module.exports.flushTerminal = flushTerminal;
367
- module.exports.flushTerminalMeasured = flushTerminalMeasured;
368
- module.exports.syncTerminalSize = syncTerminalSize;
972
+ function __napiStampBindingTarget(exportsObject, target) {
973
+ if (Object.prototype.hasOwnProperty.call(exportsObject, "__napiBindingTarget")) {
974
+ if (exportsObject.__napiBindingTarget === target) {
975
+ // Already ours: the root entry aliases the object it loaded, so a WASI
976
+ // fallback candidate — or a `NAPI_RS_NATIVE_LIBRARY_PATH` override that
977
+ // is a generated loader — arrives already stamped with this same value.
978
+ return target;
979
+ }
980
+ const error = new Error(
981
+ '`__napiBindingTarget` is reserved by the generated binding loader, but the loaded binding already exports it. Rename the export, e.g. #[napi(js_name = "...")].',
982
+ );
983
+ error.code = "ERR_NAPI_BINDING_TARGET_CONFLICT";
984
+ throw error;
985
+ }
986
+ if (!Object.isExtensible(exportsObject)) {
987
+ // A `#[napi(module_exports)]` hook may seal or freeze this object
988
+ // (`Object::seal` / `Object::freeze`). Reporting the artifact is metadata,
989
+ // never a reason to fail an otherwise successful load, so the stamp is
990
+ // skipped. What a consumer still sees then follows the entry point: the
991
+ // browser and deferred loaders declare `__napiBindingTarget` at module
992
+ // level and go on reporting it, while the CommonJS entries hand back this
993
+ // very object as `module.exports`, so there the value is absent.
994
+ return target;
995
+ }
996
+ try {
997
+ // [[Define]], not [[Set]]: an ordinary assignment walks the prototype
998
+ // chain, so an inherited accessor could swallow the value or throw and
999
+ // fail an otherwise successful load. The descriptor is what a successful
1000
+ // assignment would have produced.
1001
+ Object.defineProperty(exportsObject, "__napiBindingTarget", {
1002
+ configurable: true,
1003
+ enumerable: true,
1004
+ value: target,
1005
+ writable: true,
1006
+ });
1007
+ } catch {
1008
+ // Same rule as the non-extensible skip above: reporting the artifact is
1009
+ // metadata, never a reason to fail an otherwise successful load. An exotic
1010
+ // object (a Proxy whose defineProperty trap refuses) is skipped, not
1011
+ // thrown over.
1012
+ }
1013
+ // The CommonJS loaders assign this return value so `cjs-module-lexer` — and
1014
+ // therefore Node's CJS -> ESM named export detection — can see
1015
+ // `__napiBindingTarget` statically.
1016
+ return target;
1017
+ }
1018
+ // Stamp before the alias, not after. The guard only reads `nativeBinding`
1019
+ // (`hasOwnProperty` plus a comparison), which is safe against any addon
1020
+ // accessor; an assignment is not, because a `#[napi(module_exports)]` hook can
1021
+ // expose a getter reporting this very value and a setter that throws. So the
1022
+ // assignment lands on the loader's own `module.exports`, still the original
1023
+ // object here, and the alias below replaces it.
1024
+ //
1025
+ // The assignment is what keeps the marker a statically visible CommonJS export:
1026
+ // `cjs-module-lexer` is Node's CJS -> ESM named export detection, it cannot see
1027
+ // a bare call, and the later `module.exports = nativeBinding` does not undo the
1028
+ // detection. The assignment itself always succeeds — its target is this
1029
+ // loader's own, still extensible `module.exports` — and the alias below then
1030
+ // discards the value it wrote. What a consumer reads is whatever the guard put
1031
+ // on `nativeBinding`, so on a frozen binding, where the guard skips, the
1032
+ // linked import resolves to `undefined`.
1033
+ module.exports.__napiBindingTarget = __napiStampBindingTarget(
1034
+ nativeBinding,
1035
+ __napiLoadedBindingTarget,
1036
+ );
1037
+ module.exports = nativeBinding;
1038
+ module.exports.addLayoutChild = nativeBinding.addLayoutChild;
1039
+ module.exports.clearLayout = nativeBinding.clearLayout;
1040
+ module.exports.clearRect = nativeBinding.clearRect;
1041
+ module.exports.clearScreen = nativeBinding.clearScreen;
1042
+ module.exports.computeLayout = nativeBinding.computeLayout;
1043
+ module.exports.createLayoutLeaf = nativeBinding.createLayoutLeaf;
1044
+ module.exports.createLayoutNode = nativeBinding.createLayoutNode;
1045
+ module.exports.disableIme = nativeBinding.disableIme;
1046
+ module.exports.enableIme = nativeBinding.enableIme;
1047
+ module.exports.fillRect = nativeBinding.fillRect;
1048
+ module.exports.flushTerminal = nativeBinding.flushTerminal;
1049
+ module.exports.flushTerminalMeasured = nativeBinding.flushTerminalMeasured;
1050
+ module.exports.getAllLayouts = nativeBinding.getAllLayouts;
1051
+ module.exports.getImeState = nativeBinding.getImeState;
1052
+ module.exports.getLastRenderLayouts = nativeBinding.getLastRenderLayouts;
1053
+ module.exports.getLayout = nativeBinding.getLayout;
1054
+ module.exports.getTerminalInfo = nativeBinding.getTerminalInfo;
1055
+ module.exports.hideCursor = nativeBinding.hideCursor;
1056
+ module.exports.initLayout = nativeBinding.initLayout;
1057
+ module.exports.initTerminal = nativeBinding.initTerminal;
1058
+ module.exports.initTerminalWithMouse = nativeBinding.initTerminalWithMouse;
1059
+ module.exports.initTerminalWithOptions = nativeBinding.initTerminalWithOptions;
1060
+ module.exports.pollEvent = nativeBinding.pollEvent;
1061
+ module.exports.pollEventNonBlocking = nativeBinding.pollEventNonBlocking;
1062
+ module.exports.readEvent = nativeBinding.readEvent;
1063
+ module.exports.removeLayoutChild = nativeBinding.removeLayoutChild;
1064
+ module.exports.removeLayoutNode = nativeBinding.removeLayoutNode;
1065
+ module.exports.renderBox = nativeBinding.renderBox;
1066
+ module.exports.renderText = nativeBinding.renderText;
1067
+ module.exports.renderTree = nativeBinding.renderTree;
1068
+ module.exports.restoreTerminal = nativeBinding.restoreTerminal;
1069
+ module.exports.setCursor = nativeBinding.setCursor;
1070
+ module.exports.setCursorShape = nativeBinding.setCursorShape;
1071
+ module.exports.setImeMode = nativeBinding.setImeMode;
1072
+ module.exports.setLayoutRoot = nativeBinding.setLayoutRoot;
1073
+ module.exports.setLayoutStyle = nativeBinding.setLayoutStyle;
1074
+ module.exports.showCursor = nativeBinding.showCursor;
1075
+ module.exports.syncTerminalSize = nativeBinding.syncTerminalSize;