@napi-rs/cli 3.0.0-alpha.5 → 3.0.0-alpha.6

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 (122) hide show
  1. package/dist/api/artifacts.js +5 -2
  2. package/dist/api/artifacts.js.map +1 -0
  3. package/dist/api/build.d.ts +1 -1
  4. package/dist/api/build.js +73 -16
  5. package/dist/api/build.js.map +1 -0
  6. package/dist/api/create-npm-dirs.js +1 -0
  7. package/dist/api/create-npm-dirs.js.map +1 -0
  8. package/dist/api/load-wasi-template.d.ts +1 -0
  9. package/dist/api/load-wasi-template.js +63 -0
  10. package/dist/api/load-wasi-template.js.map +1 -0
  11. package/dist/api/new.js +2 -1
  12. package/dist/api/new.js.map +1 -0
  13. package/dist/api/pre-publish.js +1 -0
  14. package/dist/api/pre-publish.js.map +1 -0
  15. package/dist/api/rename.js +1 -0
  16. package/dist/api/rename.js.map +1 -0
  17. package/dist/api/templates/.gitignore.d.ts +1 -1
  18. package/dist/api/templates/.gitignore.js +2 -0
  19. package/dist/api/templates/.gitignore.js.map +1 -0
  20. package/dist/api/templates/.npmignore.js +1 -0
  21. package/dist/api/templates/.npmignore.js.map +1 -0
  22. package/dist/api/templates/build.rs.js +1 -0
  23. package/dist/api/templates/build.rs.js.map +1 -0
  24. package/dist/api/templates/cargo.toml.js +1 -0
  25. package/dist/api/templates/cargo.toml.js.map +1 -0
  26. package/dist/api/templates/ci-template.js +48 -33
  27. package/dist/api/templates/ci-template.js.map +1 -0
  28. package/dist/api/templates/ci.yml.d.ts +1 -1
  29. package/dist/api/templates/ci.yml.js +2 -1
  30. package/dist/api/templates/ci.yml.js.map +1 -0
  31. package/dist/api/templates/index.js +1 -0
  32. package/dist/api/templates/index.js.map +1 -0
  33. package/dist/api/templates/js-binding.js +46 -11
  34. package/dist/api/templates/js-binding.js.map +1 -0
  35. package/dist/api/templates/lib.rs.js +1 -0
  36. package/dist/api/templates/lib.rs.js.map +1 -0
  37. package/dist/api/templates/package.json.js +1 -0
  38. package/dist/api/templates/package.json.js.map +1 -0
  39. package/dist/api/universalize.js +1 -0
  40. package/dist/api/universalize.js.map +1 -0
  41. package/dist/api/version.js +1 -0
  42. package/dist/api/version.js.map +1 -0
  43. package/dist/api/wasi-worker-template.d.ts +1 -0
  44. package/dist/api/wasi-worker-template.js +53 -0
  45. package/dist/api/wasi-worker-template.js.map +1 -0
  46. package/dist/cli.js +1 -0
  47. package/dist/cli.js.map +1 -0
  48. package/dist/commands/artifacts.js +1 -0
  49. package/dist/commands/artifacts.js.map +1 -0
  50. package/dist/commands/build.js +1 -0
  51. package/dist/commands/build.js.map +1 -0
  52. package/dist/commands/create-npm-dirs.js +1 -0
  53. package/dist/commands/create-npm-dirs.js.map +1 -0
  54. package/dist/commands/help.js +1 -0
  55. package/dist/commands/help.js.map +1 -0
  56. package/dist/commands/new.js +1 -0
  57. package/dist/commands/new.js.map +1 -0
  58. package/dist/commands/pre-publish.js +1 -0
  59. package/dist/commands/pre-publish.js.map +1 -0
  60. package/dist/commands/rename.js +1 -0
  61. package/dist/commands/rename.js.map +1 -0
  62. package/dist/commands/universalize.js +1 -0
  63. package/dist/commands/universalize.js.map +1 -0
  64. package/dist/commands/version.js +1 -0
  65. package/dist/commands/version.js.map +1 -0
  66. package/dist/def/artifacts.js +1 -0
  67. package/dist/def/artifacts.js.map +1 -0
  68. package/dist/def/build.js +1 -0
  69. package/dist/def/build.js.map +1 -0
  70. package/dist/def/create-npm-dirs.js +1 -0
  71. package/dist/def/create-npm-dirs.js.map +1 -0
  72. package/dist/def/new.js +1 -0
  73. package/dist/def/new.js.map +1 -0
  74. package/dist/def/pre-publish.js +1 -0
  75. package/dist/def/pre-publish.js.map +1 -0
  76. package/dist/def/rename.js +1 -0
  77. package/dist/def/rename.js.map +1 -0
  78. package/dist/def/universalize.js +1 -0
  79. package/dist/def/universalize.js.map +1 -0
  80. package/dist/def/version.js +1 -0
  81. package/dist/def/version.js.map +1 -0
  82. package/dist/index.cjs +534 -200
  83. package/dist/index.js +1 -0
  84. package/dist/index.js.map +1 -0
  85. package/dist/utils/__tests__/target.spec.js +1 -0
  86. package/dist/utils/__tests__/target.spec.js.map +1 -0
  87. package/dist/utils/__tests__/typegen.spec.js +1 -0
  88. package/dist/utils/__tests__/typegen.spec.js.map +1 -0
  89. package/dist/utils/__tests__/version.spec.js +1 -0
  90. package/dist/utils/__tests__/version.spec.js.map +1 -0
  91. package/dist/utils/cargo.js +1 -0
  92. package/dist/utils/cargo.js.map +1 -0
  93. package/dist/utils/ci.js +1 -0
  94. package/dist/utils/ci.js.map +1 -0
  95. package/dist/utils/config.js +1 -0
  96. package/dist/utils/config.js.map +1 -0
  97. package/dist/utils/index.js +1 -0
  98. package/dist/utils/index.js.map +1 -0
  99. package/dist/utils/log.js +1 -0
  100. package/dist/utils/log.js.map +1 -0
  101. package/dist/utils/metadata.js +1 -0
  102. package/dist/utils/metadata.js.map +1 -0
  103. package/dist/utils/misc.js +1 -0
  104. package/dist/utils/misc.js.map +1 -0
  105. package/dist/utils/target.d.ts +4 -3
  106. package/dist/utils/target.js +10 -0
  107. package/dist/utils/target.js.map +1 -0
  108. package/dist/utils/typegen.js +1 -0
  109. package/dist/utils/typegen.js.map +1 -0
  110. package/dist/utils/version.js +1 -0
  111. package/dist/utils/version.js.map +1 -0
  112. package/package.json +43 -11
  113. package/src/api/artifacts.ts +5 -2
  114. package/src/api/build.ts +100 -15
  115. package/src/api/load-wasi-template.ts +65 -0
  116. package/src/api/new.ts +1 -4
  117. package/src/api/templates/.gitignore.ts +1 -0
  118. package/src/api/templates/ci-template.ts +47 -33
  119. package/src/api/templates/ci.yml.ts +1 -4
  120. package/src/api/templates/js-binding.ts +45 -11
  121. package/src/api/wasi-worker-template.ts +52 -0
  122. package/src/utils/target.ts +16 -7
@@ -5,6 +5,10 @@ env:
5
5
  DEBUG: 'napi:*'
6
6
  MACOSX_DEPLOYMENT_TARGET: '10.13'
7
7
 
8
+ permissions:
9
+ contents: write
10
+ id-token: write
11
+
8
12
  on:
9
13
  push:
10
14
  branches:
@@ -107,10 +111,10 @@ jobs:
107
111
  runs-on: \${{ matrix.settings.host }}
108
112
 
109
113
  steps:
110
- - uses: actions/checkout@v3
114
+ - uses: actions/checkout@v4
111
115
 
112
116
  - name: Setup node
113
- uses: actions/setup-node@v3
117
+ uses: actions/setup-node@v4
114
118
  if: \${{ !matrix.settings.docker }}
115
119
  with:
116
120
  node-version: 18
@@ -137,7 +141,7 @@ jobs:
137
141
  - uses: goto-bus-stop/setup-zig@v2
138
142
  if: \${{ matrix.settings.target == 'armv7-unknown-linux-gnueabihf' }}
139
143
  with:
140
- version: 0.10.1
144
+ version: 0.11.0
141
145
 
142
146
  - name: Setup toolchain
143
147
  run: \${{ matrix.settings.setup }}
@@ -153,7 +157,7 @@ jobs:
153
157
  run: yarn install
154
158
 
155
159
  - name: Setup node x86
156
- uses: actions/setup-node@v3
160
+ uses: actions/setup-node@v4
157
161
  if: matrix.settings.target == 'i686-pc-windows-msvc'
158
162
  with:
159
163
  node-version: 18
@@ -184,32 +188,32 @@ jobs:
184
188
  runs-on: macos-12
185
189
  name: Build FreeBSD
186
190
  steps:
187
- - uses: actions/checkout@v3
191
+ - uses: actions/checkout@v4
188
192
  - name: Build
189
193
  id: build
190
- uses: vmactions/freebsd-vm@v0
194
+ uses: cross-platform-actions/action@v0.21.0
191
195
  env:
192
196
  DEBUG: 'napi:*'
193
- RUSTUP_HOME: /usr/local/rustup
194
- CARGO_HOME: /usr/local/cargo
195
197
  RUSTUP_IO_THREADS: 1
196
198
  with:
197
- envs: 'DEBUG RUSTUP_HOME CARGO_HOME RUSTUP_IO_THREADS'
198
- usesh: true
199
- mem: 3000
199
+ operating_system: freebsd
200
+ version: '13.2'
201
+ memory: 13G
202
+ cpu_count: 3
203
+ environment_variables: 'DEBUG RUSTUP_IO_THREADS'
204
+ shell: bash
200
205
  prepare: |
201
- pkg install -y -f curl node libnghttp2 npm yarn
206
+ sudo pkg install -y -f curl node libnghttp2 npm
207
+ sudo npm install -g yarn --ignore-scripts
202
208
  curl https://sh.rustup.rs -sSf --output rustup.sh
203
- sh rustup.sh -y --profile minimal --default-toolchain beta
204
- export PATH="/usr/local/cargo/bin:$PATH"
209
+ sh rustup.sh -y --profile minimal --default-toolchain stable
210
+ source "$HOME/.cargo/env"
205
211
  echo "~~~~ rustc --version ~~~~"
206
212
  rustc --version
207
213
  echo "~~~~ node -v ~~~~"
208
214
  node -v
209
215
  echo "~~~~ yarn --version ~~~~"
210
216
  yarn --version
211
- run: |
212
- export PATH="/usr/local/cargo/bin:$PATH"
213
217
  pwd
214
218
  ls -lah
215
219
  whoami
@@ -242,13 +246,14 @@ jobs:
242
246
  - host: windows-latest
243
247
  target: 'x86_64-pc-windows-msvc'
244
248
  node: ['16', '18']
249
+ node: ['18', '20']
245
250
  runs-on: \${{ matrix.settings.host }}
246
251
 
247
252
  steps:
248
- - uses: actions/checkout@v3
253
+ - uses: actions/checkout@v4
249
254
 
250
255
  - name: Setup node
251
- uses: actions/setup-node@v3
256
+ uses: actions/setup-node@v4
252
257
  with:
253
258
  node-version: \${{ matrix.node }}
254
259
  cache: 'yarn'
@@ -277,13 +282,14 @@ jobs:
277
282
  fail-fast: false
278
283
  matrix:
279
284
  node: ['16', '18']
285
+ node: ['18', '20']
280
286
  runs-on: ubuntu-latest
281
287
 
282
288
  steps:
283
- - uses: actions/checkout@v3
289
+ - uses: actions/checkout@v4
284
290
 
285
291
  - name: Setup node
286
- uses: actions/setup-node@v3
292
+ uses: actions/setup-node@v4
287
293
  with:
288
294
  node-version: \${{ matrix.node }}
289
295
  cache: 'yarn'
@@ -312,13 +318,14 @@ jobs:
312
318
  fail-fast: false
313
319
  matrix:
314
320
  node: ['16', '18']
321
+ node: ['18', '20']
315
322
  runs-on: ubuntu-latest
316
323
 
317
324
  steps:
318
- - uses: actions/checkout@v3
325
+ - uses: actions/checkout@v4
319
326
 
320
327
  - name: Setup node
321
- uses: actions/setup-node@v3
328
+ uses: actions/setup-node@v4
322
329
  with:
323
330
  node-version: \${{ matrix.node }}
324
331
  cache: 'yarn'
@@ -349,10 +356,11 @@ jobs:
349
356
  fail-fast: false
350
357
  matrix:
351
358
  node: ['16', '18']
359
+ node: ['18', '20']
352
360
  runs-on: ubuntu-latest
353
361
 
354
362
  steps:
355
- - uses: actions/checkout@v3
363
+ - uses: actions/checkout@v4
356
364
 
357
365
  - name: Download artifacts
358
366
  uses: actions/download-artifact@v3
@@ -371,7 +379,7 @@ jobs:
371
379
  yarn install
372
380
 
373
381
  - name: Set up QEMU
374
- uses: docker/setup-qemu-action@v2
382
+ uses: docker/setup-qemu-action@v3
375
383
  with:
376
384
  platforms: arm64
377
385
  - run: docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
@@ -390,11 +398,15 @@ jobs:
390
398
  name: Test bindings on aarch64-unknown-linux-musl - node@\${{ matrix.node }}
391
399
  needs:
392
400
  - build
401
+ strategy:
402
+ fail-fast: false
403
+ matrix:
404
+ node: ['16', '18']
393
405
 
394
406
  runs-on: ubuntu-latest
395
407
 
396
408
  steps:
397
- - uses: actions/checkout@v3
409
+ - uses: actions/checkout@v4
398
410
 
399
411
  - name: Download artifacts
400
412
  uses: actions/download-artifact@v3
@@ -413,7 +425,7 @@ jobs:
413
425
  yarn install
414
426
 
415
427
  - name: Set up QEMU
416
- uses: docker/setup-qemu-action@v2
428
+ uses: docker/setup-qemu-action@v3
417
429
  with:
418
430
  platforms: arm64
419
431
  - run: docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
@@ -421,7 +433,7 @@ jobs:
421
433
  - name: Setup and run tests
422
434
  uses: addnab/docker-run-action@v3
423
435
  with:
424
- image: node:lts-alpine
436
+ image: node:\${{ matrix.node }}-alpine
425
437
  options: --platform linux/arm64 -v \${{ github.workspace }}:/build -w /build
426
438
  run: |
427
439
  set -e
@@ -435,10 +447,11 @@ jobs:
435
447
  fail-fast: false
436
448
  matrix:
437
449
  node: ['16', '18']
450
+ node: ['18', '20']
438
451
  runs-on: ubuntu-latest
439
452
 
440
453
  steps:
441
- - uses: actions/checkout@v3
454
+ - uses: actions/checkout@v4
442
455
 
443
456
  - name: Download artifacts
444
457
  uses: actions/download-artifact@v3
@@ -456,7 +469,7 @@ jobs:
456
469
  yarn install
457
470
 
458
471
  - name: Set up QEMU
459
- uses: docker/setup-qemu-action@v2
472
+ uses: docker/setup-qemu-action@v3
460
473
  with:
461
474
  platforms: arm
462
475
  - run: docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
@@ -478,10 +491,10 @@ jobs:
478
491
  runs-on: macos-latest
479
492
 
480
493
  steps:
481
- - uses: actions/checkout@v3
494
+ - uses: actions/checkout@v4
482
495
 
483
496
  - name: Setup node
484
- uses: actions/setup-node@v3
497
+ uses: actions/setup-node@v4
485
498
  with:
486
499
  node-version: 18
487
500
  cache: yarn
@@ -523,10 +536,10 @@ jobs:
523
536
  - build-freebsd
524
537
 
525
538
  steps:
526
- - uses: actions/checkout@v3
539
+ - uses: actions/checkout@v4
527
540
 
528
541
  - name: Setup node
529
- uses: actions/setup-node@v3
542
+ uses: actions/setup-node@v4
530
543
  with:
531
544
  node-version: 18
532
545
  cache: 'yarn'
@@ -548,6 +561,7 @@ jobs:
548
561
 
549
562
  - name: Publish
550
563
  run: |
564
+ npm config set provenance true
551
565
  if git log -1 --pretty=%B | grep "^[0-9]\\+\\.[0-9]\\+\\.[0-9]\\+$";
552
566
  then
553
567
  echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
@@ -17,10 +17,7 @@ const TEST_LINUX_AARCH64_MUSL = 'test-linux-aarch64-musl-binding'
17
17
  const TEST_LINUX_ARM_GNUEABIHF = 'test-linux-arm-gnueabihf-binding'
18
18
  const UNIVERSAL_MACOS = 'universal-macOS'
19
19
 
20
- export const createGithubActionsCIYml = (
21
- binaryName: string,
22
- targets: string[],
23
- ) => {
20
+ export const createGithubActionsCIYml = (targets: string[]) => {
24
21
  const allTargets = new Set(
25
22
  targets.flatMap((t) => {
26
23
  const platform = parseTriple(t)
@@ -16,18 +16,52 @@ let nativeBinding = null
16
16
  let localFileExisted = false
17
17
  let loadError = null
18
18
 
19
- function isMusl() {
20
- // For Node 10
21
- if (!process.report || typeof process.report.getReport !== 'function') {
22
- try {
23
- const lddPath = require('child_process').execSync('which ldd').toString().trim()
24
- return readFileSync(lddPath, 'utf8').includes('musl')
25
- } catch (e) {
19
+ const isMusl = () => {
20
+ let musl = false
21
+ if (process.platform === 'linux') {
22
+ musl = isMuslFromFilesystem()
23
+ if (musl === null) {
24
+ musl = isMuslFromReport()
25
+ }
26
+ if (musl === null) {
27
+ musl = isMuslFromChildProcess()
28
+ }
29
+ }
30
+ return musl
31
+ }
32
+
33
+ const isFileMusl = (f) => f.includes('libc.musl-') || f.includes('ld-musl-')
34
+
35
+ const isMuslFromFilesystem = () => {
36
+ try {
37
+ return readFileSync('/usr/bin/ldd', 'utf-8').includes('musl')
38
+ } catch {
39
+ return null
40
+ }
41
+ }
42
+
43
+ const isMuslFromReport = () => {
44
+ const report = typeof process.report.getReport === 'function' ? process.report.getReport() : null
45
+ if (!report) {
46
+ return null
47
+ }
48
+ if (report.header && report.header.glibcVersionRuntime) {
49
+ return false
50
+ }
51
+ if (Array.isArray(report.sharedObjects)) {
52
+ if (report.sharedObjects.some(isFileMusl)) {
26
53
  return true
27
54
  }
28
- } else {
29
- const { glibcVersionRuntime } = process.report.getReport().header
30
- return !glibcVersionRuntime
55
+ }
56
+ return false
57
+ }
58
+
59
+ const isMuslFromChildProcess = () => {
60
+ try {
61
+ return require('child_process').execSync('ldd --version', { encoding: 'utf8' }).includes('musl')
62
+ } catch (e) {
63
+ // If we reach this case, we don't know if the system is musl or not, so is better to just fallback to false
64
+ return false
31
65
  }
32
66
  }
33
67
 
@@ -256,7 +290,7 @@ if (!nativeBinding) {
256
290
  }
257
291
 
258
292
  ${idents
259
- .map((ident) => `module.exports.${ident} = binding.${ident}`)
293
+ .map((ident) => `module.exports.${ident} = nativeBinding.${ident}`)
260
294
  .join('\n')}
261
295
  `
262
296
  }
@@ -0,0 +1,52 @@
1
+ export const WASI_WORKER_TEMPLATE = `import fs from "node:fs";
2
+ import { createRequire } from "node:module";
3
+ import { parentPort, Worker } from "node:worker_threads";
4
+
5
+ import { instantiateNapiModuleSync, MessageHandler } from "@emnapi/core";
6
+ import { WASI } from "@tybys/wasm-util";
7
+
8
+ const require = createRequire(import.meta.url);
9
+
10
+ if (parentPort) {
11
+ parentPort.on("message", (data) => {
12
+ globalThis.onmessage({ data });
13
+ });
14
+ }
15
+
16
+ Object.assign(globalThis, {
17
+ self: globalThis,
18
+ require,
19
+ Worker,
20
+ importScripts: function (f) {
21
+ ;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f);
22
+ },
23
+ postMessage: function (msg) {
24
+ if (parentPort) {
25
+ parentPort.postMessage(msg);
26
+ }
27
+ },
28
+ });
29
+
30
+ const handler = new MessageHandler({
31
+ onLoad({ wasmModule, wasmMemory }) {
32
+ const wasi = new WASI({ fs });
33
+
34
+ return instantiateNapiModuleSync(wasmModule, {
35
+ childThread: true,
36
+ wasi,
37
+ overwriteImports(importObject) {
38
+ importObject.env = {
39
+ ...importObject.env,
40
+ ...importObject.napi,
41
+ ...importObject.emnapi,
42
+ memory: wasmMemory
43
+ };
44
+ },
45
+ });
46
+ },
47
+ });
48
+
49
+ globalThis.onmessage = function (e) {
50
+ handler.handle(e);
51
+ };
52
+ `
@@ -1,5 +1,7 @@
1
1
  import { execSync } from 'child_process'
2
2
 
3
+ export type Platform = NodeJS.Platform | 'wasm' | 'wasi'
4
+
3
5
  export const AVAILABLE_TARGETS = [
4
6
  'aarch64-apple-darwin',
5
7
  'aarch64-linux-android',
@@ -63,24 +65,22 @@ export const NodeArchToCpu: Record<string, string> = {
63
65
  riscv64: 'riscv64gc',
64
66
  }
65
67
 
66
- const SysToNodePlatform: Record<string, NodeJS.Platform> = {
68
+ const SysToNodePlatform: Record<string, Platform> = {
67
69
  linux: 'linux',
68
70
  freebsd: 'freebsd',
69
71
  darwin: 'darwin',
70
72
  windows: 'win32',
71
73
  }
72
74
 
73
- export const UniArchsByPlatform: Partial<
74
- Record<NodeJS.Platform, NodeJSArch[]>
75
- > = {
75
+ export const UniArchsByPlatform: Partial<Record<Platform, NodeJSArch[]>> = {
76
76
  darwin: ['x64', 'arm64'],
77
77
  }
78
78
 
79
79
  export interface Target {
80
80
  triple: string
81
81
  platformArchABI: string
82
- platform: NodeJS.Platform
83
- arch: NodeJSArch
82
+ platform: Platform
83
+ arch: NodeJSArch | 'wasm32'
84
84
  abi: string | null
85
85
  }
86
86
 
@@ -95,6 +95,15 @@ export interface Target {
95
95
  * - `abi` = The ABI, for example `gnu`, `android`, `eabi`, etc.
96
96
  */
97
97
  export function parseTriple(rawTriple: string): Target {
98
+ if (rawTriple === 'wasm32-wasi-preview1-threads') {
99
+ return {
100
+ triple: rawTriple,
101
+ platformArchABI: rawTriple,
102
+ platform: 'wasi',
103
+ arch: 'wasm32',
104
+ abi: 'wasi',
105
+ }
106
+ }
98
107
  const triple = rawTriple.endsWith('eabi')
99
108
  ? `${rawTriple.slice(0, -4)}-eabi`
100
109
  : rawTriple
@@ -114,7 +123,7 @@ export function parseTriple(rawTriple: string): Target {
114
123
  ;[cpu, , sys, abi = null] = triples
115
124
  }
116
125
 
117
- const platform = SysToNodePlatform[sys] ?? (sys as NodeJS.Platform)
126
+ const platform = SysToNodePlatform[sys] ?? (sys as Platform)
118
127
  const arch = CpuToNodeArch[cpu] ?? (cpu as NodeJSArch)
119
128
  return {
120
129
  triple: rawTriple,