@hunghg255/napi-zig-cli 1.5.0 → 1.6.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/README.md +12 -0
- package/bin/napi-zig.mjs +7 -4
- package/lib/artifacts.mjs +24 -3
- package/lib/build.mjs +8 -0
- package/lib/project.mjs +3 -0
- package/lib/publish.mjs +8 -3
- package/lib/targets.mjs +7 -5
- package/lib/wasm.mjs +165 -0
- package/package.json +1 -1
- package/template/_dot_github/workflows/ci.yml.tmpl +4 -0
- package/template/_dot_gitignore.tmpl +2 -0
- package/template/index.js.tmpl +23 -7
- package/template/package.json.tmpl +1 -1
package/README.md
CHANGED
|
@@ -27,4 +27,16 @@ Project configuration lives in `package.json`:
|
|
|
27
27
|
}
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
+
`artifacts` writes one package per target under `npm/<target>/`, listed as
|
|
31
|
+
`optionalDependencies` of the main package, and the generated `index.js`
|
|
32
|
+
loads the one that fits the machine. The WebAssembly targets are packaged
|
|
33
|
+
too: `npm/wasm32-wasi/` holds `<name>.wasm32-wasi.wasm` and the loader
|
|
34
|
+
`<name>.wasm32-wasi.cjs` (its `main`), depends on `@emnapi/core` and
|
|
35
|
+
`@emnapi/runtime`, and installs on every platform; `wasm32-wasi-threads`
|
|
36
|
+
adds a worker script and `@emnapi/wasi-threads`. `index.js` tries the native
|
|
37
|
+
binary for the platform first, then `wasm32-wasi-threads`, then
|
|
38
|
+
`wasm32-wasi`. If `build.zig` changes the threaded module's memory bounds
|
|
39
|
+
(`wasm_initial_memory` / `wasm_max_memory`), give the loader the same ones:
|
|
40
|
+
`"napi": { "wasm": { "initialMemory": ..., "maxMemory": ... } }` (bytes).
|
|
41
|
+
|
|
30
42
|
The `zig` executable is taken from `PATH` (override with `NAPI_ZIG_ZIG`).
|
package/bin/napi-zig.mjs
CHANGED
|
@@ -25,11 +25,14 @@ Usage:
|
|
|
25
25
|
--glibc <ver> minimum glibc for *-gnu targets (default 2.28)
|
|
26
26
|
--dynamic-host resolve Node-API at load time (Electron, renamed hosts)
|
|
27
27
|
The target darwin-universal merges darwin-x64 and darwin-arm64 into one
|
|
28
|
-
file
|
|
28
|
+
file. wasm32-wasi and wasm32-wasi-threads write <name>.<target>.wasm
|
|
29
|
+
plus the <name>.<target>.cjs that loads it through emnapi.
|
|
29
30
|
|
|
30
31
|
napi-zig artifacts [--target <id>]...
|
|
31
32
|
Create npm/<target>/ packages for the built binaries and list them as
|
|
32
|
-
optionalDependencies in package.json.
|
|
33
|
+
optionalDependencies in package.json. A wasm target's package carries
|
|
34
|
+
the .wasm and its loader and depends on emnapi; the generated index.js
|
|
35
|
+
falls back to it where no native binary fits.
|
|
33
36
|
|
|
34
37
|
napi-zig publish [--dry-run] [--tag <tag>] [--otp <code>] [--provenance] [--access <a>]
|
|
35
38
|
Verify the npm/<target>/ packages, then "npm publish" each of them and
|
|
@@ -112,8 +115,8 @@ function main() {
|
|
|
112
115
|
case 'targets':
|
|
113
116
|
for (const target of TARGETS) {
|
|
114
117
|
const how = target.merge ? `(${target.merge.join(' + ')})` : target.zig
|
|
115
|
-
const notes = [target.id === hostTargetId() && 'host', target.
|
|
116
|
-
console.log(`${target.id.padEnd(
|
|
118
|
+
const notes = [target.id === hostTargetId() && 'host', target.wasm && 'any platform, through emnapi'].filter(Boolean)
|
|
119
|
+
console.log(`${target.id.padEnd(20)} ${how}${notes.length ? ` [${notes.join(', ')}]` : ''}`)
|
|
117
120
|
}
|
|
118
121
|
break
|
|
119
122
|
default:
|
package/lib/artifacts.mjs
CHANGED
|
@@ -2,12 +2,19 @@ import { copyFileSync, existsSync, mkdirSync, writeFileSync } from 'node:fs'
|
|
|
2
2
|
import { join, resolve } from 'node:path'
|
|
3
3
|
import { loadProject } from './project.mjs'
|
|
4
4
|
import { binaryName, findTarget } from './targets.mjs'
|
|
5
|
+
import { EMNAPI_DEPENDENCIES, EMNAPI_THREADS_DEPENDENCIES, loaderName, wasmLoaderFiles } from './wasm.mjs'
|
|
5
6
|
|
|
6
7
|
/** The npm package that carries the binary of one target. */
|
|
7
8
|
export function platformPackageName(pkg, id) {
|
|
8
9
|
return `${pkg.name}-${id}`
|
|
9
10
|
}
|
|
10
11
|
|
|
12
|
+
/** The files of a target's package: the binary, then a wasm loader. */
|
|
13
|
+
export function packageFiles(project, target) {
|
|
14
|
+
const binary = binaryName(project.addonName, target)
|
|
15
|
+
return target.wasm ? [binary, ...Object.keys(wasmLoaderFiles(project, target))] : [binary]
|
|
16
|
+
}
|
|
17
|
+
|
|
11
18
|
/**
|
|
12
19
|
* Lays out one npm package per platform under `npm/<target>/`, the way
|
|
13
20
|
* prebuilt native addons are usually published:
|
|
@@ -17,6 +24,15 @@ export function platformPackageName(pkg, id) {
|
|
|
17
24
|
*
|
|
18
25
|
* and lists them as optionalDependencies of the main package, so that
|
|
19
26
|
* `npm install` downloads only the binary matching the user's machine.
|
|
27
|
+
*
|
|
28
|
+
* A WebAssembly target's package fits every machine (no `os` or `cpu`):
|
|
29
|
+
* it holds the `.wasm`, the loader that is its `main` (and, threaded, the
|
|
30
|
+
* worker script), and depends on emnapi. The generated index.js falls
|
|
31
|
+
* back to it where no native package fits.
|
|
32
|
+
*
|
|
33
|
+
* npm/wasm32-wasi/package.json { main: "<name>.wasm32-wasi.cjs", dependencies: { "@emnapi/core", ... } }
|
|
34
|
+
* npm/wasm32-wasi/<name>.wasm32-wasi.wasm
|
|
35
|
+
* npm/wasm32-wasi/<name>.wasm32-wasi.cjs
|
|
20
36
|
*/
|
|
21
37
|
export function artifacts(options) {
|
|
22
38
|
const project = loadProject(resolve(options.cwd ?? '.'))
|
|
@@ -35,12 +51,13 @@ export function artifacts(options) {
|
|
|
35
51
|
const platformPackage = {
|
|
36
52
|
name: platformPackageName(pkg, id),
|
|
37
53
|
version: pkg.version,
|
|
38
|
-
description: `${pkg.name} prebuilt binary for ${id}`,
|
|
54
|
+
description: `${pkg.name} prebuilt ${target.wasm ? 'WebAssembly build' : 'binary'} for ${id}`,
|
|
39
55
|
...(target.os ? { os: [target.os] } : {}),
|
|
40
56
|
...(target.cpu ? { cpu: target.cpu } : {}),
|
|
41
57
|
...(target.libc ? { libc: [target.libc] } : {}),
|
|
42
|
-
main: binary,
|
|
43
|
-
files:
|
|
58
|
+
main: target.wasm ? loaderName(addonName, target) : binary,
|
|
59
|
+
files: packageFiles(project, target),
|
|
60
|
+
...(target.wasm ? { dependencies: target.threads ? EMNAPI_THREADS_DEPENDENCIES : EMNAPI_DEPENDENCIES } : {}),
|
|
44
61
|
license: pkg.license,
|
|
45
62
|
repository: pkg.repository,
|
|
46
63
|
...(pkg.publishConfig ? { publishConfig: pkg.publishConfig } : {}),
|
|
@@ -50,6 +67,10 @@ export function artifacts(options) {
|
|
|
50
67
|
const source = join(project.dir, binary)
|
|
51
68
|
if (existsSync(source)) copyFileSync(source, join(dir, binary))
|
|
52
69
|
else missing.push(binary)
|
|
70
|
+
// (Generated afresh: they only depend on the project.)
|
|
71
|
+
if (target.wasm) {
|
|
72
|
+
for (const [name, text] of Object.entries(wasmLoaderFiles(project, target))) writeFileSync(join(dir, name), text)
|
|
73
|
+
}
|
|
53
74
|
created.push(dir)
|
|
54
75
|
}
|
|
55
76
|
|
package/lib/build.mjs
CHANGED
|
@@ -4,6 +4,7 @@ import { loadProject } from './project.mjs'
|
|
|
4
4
|
import { DEFAULT_GLIBC, binaryName, findTarget, hostTargetId, zigTriple } from './targets.mjs'
|
|
5
5
|
import { createUniversal } from './macho.mjs'
|
|
6
6
|
import { run, zigExe } from './exec.mjs'
|
|
7
|
+
import { wasmLoaderFiles } from './wasm.mjs'
|
|
7
8
|
|
|
8
9
|
const OPTIMIZE_MODES = { debug: 'debug', safe: 'safe', fast: 'fast', small: 'small' }
|
|
9
10
|
// Spellings used by Zig before 0.17.
|
|
@@ -77,6 +78,13 @@ export function build(options) {
|
|
|
77
78
|
}
|
|
78
79
|
copyFileSync(source, dest)
|
|
79
80
|
built.push(dest)
|
|
81
|
+
// A wasm build comes with the JavaScript that loads it.
|
|
82
|
+
if (target.wasm) {
|
|
83
|
+
for (const [name, text] of Object.entries(wasmLoaderFiles(project, target))) {
|
|
84
|
+
writeFileSync(join(outDir, name), text)
|
|
85
|
+
built.push(join(outDir, name))
|
|
86
|
+
}
|
|
87
|
+
}
|
|
80
88
|
|
|
81
89
|
const dts = join(prefix, project.dts)
|
|
82
90
|
if (existsSync(dts)) copyFileSync(dts, join(outDir, project.dts))
|
package/lib/project.mjs
CHANGED
|
@@ -23,5 +23,8 @@ export function loadProject(dir) {
|
|
|
23
23
|
addonName,
|
|
24
24
|
targets: config.targets ?? [],
|
|
25
25
|
dts: config.dts ?? 'index.d.ts',
|
|
26
|
+
// Memory bounds of the threaded wasm build, if build.zig changes them:
|
|
27
|
+
// { initialMemory, maxMemory } in bytes.
|
|
28
|
+
wasm: config.wasm ?? {},
|
|
26
29
|
}
|
|
27
30
|
}
|
package/lib/publish.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import { existsSync, readFileSync, readdirSync } from 'node:fs'
|
|
|
2
2
|
import { dirname, join, resolve } from 'node:path'
|
|
3
3
|
import { loadProject } from './project.mjs'
|
|
4
4
|
import { binaryName, findTarget } from './targets.mjs'
|
|
5
|
-
import { platformPackageName } from './artifacts.mjs'
|
|
5
|
+
import { packageFiles, platformPackageName } from './artifacts.mjs'
|
|
6
6
|
import { run } from './exec.mjs'
|
|
7
7
|
|
|
8
8
|
/**
|
|
@@ -46,13 +46,18 @@ export function publishPlan(project) {
|
|
|
46
46
|
const manifestPath = join(dir, 'package.json')
|
|
47
47
|
if (!existsSync(manifestPath)) continue
|
|
48
48
|
const manifest = JSON.parse(readFileSync(manifestPath, 'utf8'))
|
|
49
|
-
const
|
|
49
|
+
const target = findTarget(id)
|
|
50
|
+
const binary = binaryName(addonName, target)
|
|
50
51
|
const expectedName = platformPackageName(pkg, id)
|
|
51
52
|
if (manifest.name !== expectedName) problems.push(`npm/${id}: name is ${manifest.name}, expected ${expectedName}`)
|
|
52
53
|
if (manifest.version !== pkg.version) {
|
|
53
54
|
problems.push(`npm/${id}: version ${manifest.version} does not match ${pkg.version} (re-run "napi-zig artifacts")`)
|
|
54
55
|
}
|
|
55
|
-
|
|
56
|
+
for (const file of packageFiles(project, target)) {
|
|
57
|
+
if (!existsSync(join(dir, file))) {
|
|
58
|
+
problems.push(`npm/${id}: ${file} is missing (${file === binary ? 'build it, then ' : ''}re-run "napi-zig artifacts")`)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
56
61
|
if (pkg.optionalDependencies?.[expectedName] !== pkg.version) {
|
|
57
62
|
problems.push(`package.json: optionalDependencies["${expectedName}"] should be "${pkg.version}"`)
|
|
58
63
|
}
|
package/lib/targets.mjs
CHANGED
|
@@ -16,16 +16,18 @@ export const TARGETS = [
|
|
|
16
16
|
{ id: 'darwin-universal', os: 'darwin', cpu: ['x64', 'arm64'], merge: ['darwin-x64', 'darwin-arm64'] },
|
|
17
17
|
{ id: 'win32-x64', os: 'win32', cpu: ['x64'], zig: 'x86_64-windows-gnu' },
|
|
18
18
|
{ id: 'win32-arm64', os: 'win32', cpu: ['arm64'], zig: 'aarch64-windows-gnu' },
|
|
19
|
-
//
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
//
|
|
19
|
+
// WebAssembly, loaded through emnapi on any platform: the fallback of the
|
|
20
|
+
// generated index.js where no native binary fits. Single-threaded...
|
|
21
|
+
{ id: 'wasm32-wasi', zig: 'wasm32-wasi', ext: 'wasm', wasm: true },
|
|
22
|
+
// ...and with threads (shared memory + wasi-threads): Async,
|
|
23
|
+
// ThreadsafeFunction and streams work.
|
|
23
24
|
{
|
|
24
25
|
id: 'wasm32-wasi-threads',
|
|
25
26
|
zig: 'wasm32-wasi',
|
|
26
27
|
zigArgs: ['-Dcpu=baseline+atomics+bulk_memory'],
|
|
27
28
|
ext: 'wasm',
|
|
28
|
-
|
|
29
|
+
wasm: true,
|
|
30
|
+
threads: true,
|
|
29
31
|
},
|
|
30
32
|
]
|
|
31
33
|
|
package/lib/wasm.mjs
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// The JavaScript that loads a WebAssembly build of an addon through emnapi
|
|
2
|
+
// (the Node-API implementation for WebAssembly): written next to the .wasm
|
|
3
|
+
// by `napi-zig build` and shipped in its npm package by `napi-zig
|
|
4
|
+
// artifacts`. Generated from the reference loaders of napi-zig's wasm
|
|
5
|
+
// examples (examples/wasm/load.cjs, examples/wasm-threads/load.cjs).
|
|
6
|
+
|
|
7
|
+
import { binaryName } from './targets.mjs'
|
|
8
|
+
|
|
9
|
+
/** The emnapi packages a loader needs, as npm dependency ranges. */
|
|
10
|
+
export const EMNAPI_DEPENDENCIES = {
|
|
11
|
+
'@emnapi/core': '^1.4.0',
|
|
12
|
+
'@emnapi/runtime': '^1.4.0',
|
|
13
|
+
}
|
|
14
|
+
export const EMNAPI_THREADS_DEPENDENCIES = {
|
|
15
|
+
...EMNAPI_DEPENDENCIES,
|
|
16
|
+
'@emnapi/wasi-threads': '^1.0.0',
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// The linker's defaults in addAddon (wasm_initial_memory / wasm_max_memory).
|
|
20
|
+
export const DEFAULT_INITIAL_MEMORY = 32 << 20
|
|
21
|
+
export const DEFAULT_MAX_MEMORY = 2 ** 30
|
|
22
|
+
|
|
23
|
+
/** `<name>.<target>.cjs`: the module that loads `<name>.<target>.wasm`. */
|
|
24
|
+
export function loaderName(addonName, target) {
|
|
25
|
+
return `${addonName}.${target.id}.cjs`
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** `<name>.<target>.worker.cjs`: what each wasm thread runs. */
|
|
29
|
+
export function workerName(addonName, target) {
|
|
30
|
+
return `${addonName}.${target.id}.worker.cjs`
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The files that load a wasm target's binary: `{ name: contents }`. */
|
|
34
|
+
export function wasmLoaderFiles(project, target) {
|
|
35
|
+
const threaded = Boolean(target.threads)
|
|
36
|
+
const binary = binaryName(project.addonName, target)
|
|
37
|
+
const files = {
|
|
38
|
+
[loaderName(project.addonName, target)]: threaded
|
|
39
|
+
? threadedLoader(binary, workerName(project.addonName, target), project.wasm ?? {})
|
|
40
|
+
: loader(binary),
|
|
41
|
+
}
|
|
42
|
+
if (threaded) files[workerName(project.addonName, target)] = worker()
|
|
43
|
+
return files
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const header = (what) => `// Generated by napi-zig: ${what}
|
|
47
|
+
// through emnapi (@emnapi/core, @emnapi/runtime).
|
|
48
|
+
'use strict'
|
|
49
|
+
`
|
|
50
|
+
|
|
51
|
+
const shared = `
|
|
52
|
+
// Bun's node:wasi has no \`initialize\` (the entry point of reactor modules
|
|
53
|
+
// such as this one); do what it does there.
|
|
54
|
+
function newWasi() {
|
|
55
|
+
const wasi = new WASI({ version: 'preview1' })
|
|
56
|
+
if (typeof wasi.initialize !== 'function') {
|
|
57
|
+
wasi.initialize = (instance) => {
|
|
58
|
+
wasi.setMemory(instance.exports.memory)
|
|
59
|
+
instance.exports._initialize?.()
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return wasi
|
|
63
|
+
}
|
|
64
|
+
`
|
|
65
|
+
|
|
66
|
+
const context = `
|
|
67
|
+
// emnapi keeps the event loop alive while work is pending by holding a
|
|
68
|
+
// referenced MessagePort, which Deno does not count: there a referenced
|
|
69
|
+
// timer does the job instead.
|
|
70
|
+
function context() {
|
|
71
|
+
const ctx = getDefaultContext()
|
|
72
|
+
if (globalThis.Deno && ctx.refCounter && !ctx.refCounter.napiZigTimer) {
|
|
73
|
+
let count = 0
|
|
74
|
+
let timer = null
|
|
75
|
+
ctx.refCounter = {
|
|
76
|
+
napiZigTimer: true,
|
|
77
|
+
increase() {
|
|
78
|
+
if (count++ === 0) timer = setInterval(() => {}, 1 << 30)
|
|
79
|
+
},
|
|
80
|
+
decrease() {
|
|
81
|
+
if (count === 0) return
|
|
82
|
+
if (--count === 0) clearInterval(timer)
|
|
83
|
+
},
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return ctx
|
|
87
|
+
}
|
|
88
|
+
`
|
|
89
|
+
|
|
90
|
+
function loader(binary) {
|
|
91
|
+
return `${header('loads the wasm32-wasi build of this addon')}
|
|
92
|
+
const { readFileSync } = require('node:fs')
|
|
93
|
+
const { join } = require('node:path')
|
|
94
|
+
const { WASI } = require('node:wasi')
|
|
95
|
+
const { instantiateNapiModuleSync } = require('@emnapi/core')
|
|
96
|
+
const { getDefaultContext } = require('@emnapi/runtime')
|
|
97
|
+
${shared}${context}
|
|
98
|
+
const { napiModule } = instantiateNapiModuleSync(readFileSync(join(__dirname, ${JSON.stringify(binary)})), {
|
|
99
|
+
context: context(),
|
|
100
|
+
wasi: newWasi(),
|
|
101
|
+
})
|
|
102
|
+
module.exports = napiModule.exports
|
|
103
|
+
`
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function threadedLoader(binary, workerFile, options) {
|
|
107
|
+
const initial = options.initialMemory ?? DEFAULT_INITIAL_MEMORY
|
|
108
|
+
const maximum = options.maxMemory ?? DEFAULT_MAX_MEMORY
|
|
109
|
+
return `${header('loads the threaded wasm32-wasi build of this addon')}
|
|
110
|
+
const { readFileSync } = require('node:fs')
|
|
111
|
+
const { join } = require('node:path')
|
|
112
|
+
const { WASI } = require('node:wasi')
|
|
113
|
+
const { Worker } = require('node:worker_threads')
|
|
114
|
+
const { instantiateNapiModuleSync } = require('@emnapi/core')
|
|
115
|
+
const { getDefaultContext } = require('@emnapi/runtime')
|
|
116
|
+
${shared}${context}
|
|
117
|
+
// One memory shared by every thread, with the bounds the module was linked
|
|
118
|
+
// with ("napi.wasm" in package.json: initialMemory, maxMemory).
|
|
119
|
+
const memory = new WebAssembly.Memory({
|
|
120
|
+
initial: ${initial} / 65536,
|
|
121
|
+
maximum: ${maximum} / 65536,
|
|
122
|
+
shared: true,
|
|
123
|
+
})
|
|
124
|
+
const { napiModule } = instantiateNapiModuleSync(readFileSync(join(__dirname, ${JSON.stringify(binary)})), {
|
|
125
|
+
context: context(),
|
|
126
|
+
wasi: newWasi(),
|
|
127
|
+
// Each thread the addon spawns is a worker running ${workerFile}.
|
|
128
|
+
onCreateWorker: () => new Worker(join(__dirname, ${JSON.stringify(workerFile)}), { env: process.env }),
|
|
129
|
+
waitThreadStart: 5000,
|
|
130
|
+
overwriteImports(imports) {
|
|
131
|
+
imports.env.memory = memory
|
|
132
|
+
},
|
|
133
|
+
})
|
|
134
|
+
module.exports = napiModule.exports
|
|
135
|
+
`
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function worker() {
|
|
139
|
+
return `${header('runs one thread of the threaded wasm32-wasi build of this addon')}
|
|
140
|
+
const { parentPort } = require('node:worker_threads')
|
|
141
|
+
const { WASI } = require('node:wasi')
|
|
142
|
+
const { instantiateNapiModuleSync, MessageHandler } = require('@emnapi/core')
|
|
143
|
+
${shared}
|
|
144
|
+
const postMessage = (message) => parentPort.postMessage(message)
|
|
145
|
+
// emnapi's thread-side code talks to the main thread through the web
|
|
146
|
+
// worker globals; give it those names on Node.js.
|
|
147
|
+
Object.assign(globalThis, { self: globalThis, postMessage })
|
|
148
|
+
|
|
149
|
+
const handler = new MessageHandler({
|
|
150
|
+
postMessage,
|
|
151
|
+
onLoad({ wasmModule, wasmMemory }) {
|
|
152
|
+
return instantiateNapiModuleSync(wasmModule, {
|
|
153
|
+
childThread: true,
|
|
154
|
+
wasi: newWasi(),
|
|
155
|
+
postMessage,
|
|
156
|
+
overwriteImports(imports) {
|
|
157
|
+
imports.env.memory = wasmMemory
|
|
158
|
+
},
|
|
159
|
+
})
|
|
160
|
+
},
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
parentPort.on('message', (data) => handler.handle({ data }))
|
|
164
|
+
`
|
|
165
|
+
}
|
package/package.json
CHANGED
|
@@ -29,8 +29,12 @@ jobs:
|
|
|
29
29
|
- uses: actions/upload-artifact@v4
|
|
30
30
|
with:
|
|
31
31
|
name: binaries
|
|
32
|
+
# (*.wasm and their loaders: the wasm32-wasi targets, if listed
|
|
33
|
+
# in "napi.targets".)
|
|
32
34
|
path: |
|
|
33
35
|
*.node
|
|
36
|
+
*.wasm
|
|
37
|
+
*.wasm32-wasi*.cjs
|
|
34
38
|
index.d.ts
|
|
35
39
|
if-no-files-found: error
|
|
36
40
|
|
package/template/index.js.tmpl
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// Loads the binary for the current platform: a local build if there is one
|
|
2
|
-
// (`napi-zig build`), otherwise the prebuilt optional dependency
|
|
2
|
+
// (`napi-zig build`), otherwise the prebuilt optional dependency - and where
|
|
3
|
+
// no native binary fits, the WebAssembly build (through emnapi), local or
|
|
4
|
+
// prebuilt.
|
|
3
5
|
'use strict'
|
|
4
6
|
|
|
5
7
|
const { existsSync } = require('node:fs')
|
|
@@ -8,6 +10,9 @@ const { join } = require('node:path')
|
|
|
8
10
|
const ADDON = '{{ADDON_NAME}}'
|
|
9
11
|
const PACKAGE = '{{PACKAGE_NAME}}'
|
|
10
12
|
|
|
13
|
+
// Threaded first: it can do everything the single-threaded build can.
|
|
14
|
+
const WASM_IDS = ['wasm32-wasi-threads', 'wasm32-wasi']
|
|
15
|
+
|
|
11
16
|
function targetIds() {
|
|
12
17
|
const { platform, arch } = process
|
|
13
18
|
if (platform === 'darwin') return [`darwin-${arch}`, 'darwin-universal']
|
|
@@ -20,18 +25,29 @@ function targetIds() {
|
|
|
20
25
|
function load() {
|
|
21
26
|
const ids = targetIds()
|
|
22
27
|
const errors = []
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
if (existsSync(local)) return require(local)
|
|
28
|
+
const attempt = (local, id) => {
|
|
29
|
+
if (existsSync(local)) return { found: true, exports: require(local) }
|
|
26
30
|
try {
|
|
27
|
-
return require(`${PACKAGE}-${id}`)
|
|
31
|
+
return { found: true, exports: require(`${PACKAGE}-${id}`) }
|
|
28
32
|
} catch (err) {
|
|
29
33
|
errors.push(err)
|
|
34
|
+
return { found: false }
|
|
30
35
|
}
|
|
31
36
|
}
|
|
37
|
+
for (const id of ids) {
|
|
38
|
+
const result = attempt(join(__dirname, `${ADDON}.${id}.node`), id)
|
|
39
|
+
if (result.found) return result.exports
|
|
40
|
+
}
|
|
41
|
+
// A wasm build's loader sits next to its .wasm (it is the main module of
|
|
42
|
+
// the wasm package).
|
|
43
|
+
for (const id of WASM_IDS) {
|
|
44
|
+
const result = attempt(join(__dirname, `${ADDON}.${id}.cjs`), id)
|
|
45
|
+
if (result.found) return result.exports
|
|
46
|
+
}
|
|
32
47
|
throw new Error(
|
|
33
|
-
`${PACKAGE}: no
|
|
34
|
-
`Run "napi-zig build", or check that the optional dependency ${PACKAGE}-${ids[0]}
|
|
48
|
+
`${PACKAGE}: no binary found for ${ids.join(' / ')}, nor a WebAssembly build. ` +
|
|
49
|
+
`Run "napi-zig build", or check that the optional dependency ${PACKAGE}-${ids[0]} ` +
|
|
50
|
+
`(or ${PACKAGE}-${WASM_IDS[1]}) is installed.`,
|
|
35
51
|
{ cause: errors[0] },
|
|
36
52
|
)
|
|
37
53
|
}
|