@napi-rs/cli 3.9.0 → 3.9.1
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/dist/cli.js +9087 -8769
- package/dist/index.cjs +9078 -8760
- package/dist/index.d.cts +16 -16
- package/dist/index.d.ts +16 -16
- package/dist/index.js +9087 -8769
- package/package.json +4 -5
- package/src/api/__tests__/__snapshots__/templates.spec.ts.md +9 -13
- package/src/api/__tests__/__snapshots__/templates.spec.ts.snap +0 -0
- package/src/api/__tests__/build.spec.ts +194 -0
- package/src/api/__tests__/create-npm-dirs.spec.ts +4 -3
- package/src/api/__tests__/templates.spec.ts +62 -1
- package/src/api/build.ts +124 -4
- package/src/api/create-npm-dirs.ts +2 -1
- package/src/api/new.ts +13 -18
- package/src/api/pre-publish.ts +5 -10
- package/src/api/rename.ts +10 -21
- package/src/api/templates/js-binding.ts +7 -8
- package/src/api/templates/load-wasi-template.ts +20 -17
- package/src/api/templates/wasi-worker-template.ts +36 -31
- package/src/commands/build.ts +1 -1
- package/src/utils/__tests__/__snapshots__/typegen.spec.ts.md +18 -28
- package/src/utils/__tests__/__snapshots__/typegen.spec.ts.snap +0 -0
- package/src/utils/__tests__/misc.spec.ts +4 -0
- package/src/utils/__tests__/serialize.spec.ts +55 -0
- package/src/utils/__tests__/target.spec.ts +221 -0
- package/src/utils/__tests__/typegen.spec.ts +115 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/misc.ts +4 -5
- package/src/utils/serialize.ts +47 -0
- package/src/utils/target.ts +150 -1
- package/src/utils/typegen.ts +241 -30
package/src/api/new.ts
CHANGED
|
@@ -4,8 +4,8 @@ import { homedir } from 'node:os'
|
|
|
4
4
|
import path from 'node:path'
|
|
5
5
|
import { promises as fs } from 'node:fs'
|
|
6
6
|
|
|
7
|
-
import { parse as parseToml
|
|
8
|
-
import { load as yamlLoad
|
|
7
|
+
import { parse as parseToml } from '@std/toml'
|
|
8
|
+
import { load as yamlLoad } from 'js-yaml'
|
|
9
9
|
|
|
10
10
|
import {
|
|
11
11
|
applyDefaultNewOptions,
|
|
@@ -15,6 +15,9 @@ import {
|
|
|
15
15
|
AVAILABLE_TARGETS,
|
|
16
16
|
CLI_VERSION,
|
|
17
17
|
debugFactory,
|
|
18
|
+
serializeJson,
|
|
19
|
+
serializeToml,
|
|
20
|
+
serializeYaml,
|
|
18
21
|
DEFAULT_TARGETS,
|
|
19
22
|
getWasiTarget,
|
|
20
23
|
mkdirAsync,
|
|
@@ -347,7 +350,7 @@ async function filterTargetsInPackageJson(
|
|
|
347
350
|
}
|
|
348
351
|
}
|
|
349
352
|
|
|
350
|
-
await fs.writeFile(filePath,
|
|
353
|
+
await fs.writeFile(filePath, serializeJson(packageJson))
|
|
351
354
|
await updateGeneratedWasiAttributes(
|
|
352
355
|
path.join(path.dirname(filePath), '.gitattributes'),
|
|
353
356
|
packageJson.napi.binaryName,
|
|
@@ -381,10 +384,10 @@ async function updateGeneratedWasiAttributes(
|
|
|
381
384
|
generatedFiles.add(`${binaryName}.${suffix}-deferred.d.ts`)
|
|
382
385
|
}
|
|
383
386
|
}
|
|
387
|
+
while (lines[lines.length - 1] === '') {
|
|
388
|
+
lines.pop()
|
|
389
|
+
}
|
|
384
390
|
if (generatedFiles.size > 0) {
|
|
385
|
-
while (lines[lines.length - 1] === '') {
|
|
386
|
-
lines.pop()
|
|
387
|
-
}
|
|
388
391
|
lines.push(
|
|
389
392
|
'',
|
|
390
393
|
...[...generatedFiles].map((file) => `${file} linguist-detectable=false`),
|
|
@@ -429,7 +432,7 @@ async function updateCargoTomlTypeDef(
|
|
|
429
432
|
|
|
430
433
|
dependencies['napi-derive'] = dependencyConfig
|
|
431
434
|
|
|
432
|
-
await fs.writeFile(filePath,
|
|
435
|
+
await fs.writeFile(filePath, serializeToml(cargoToml))
|
|
433
436
|
}
|
|
434
437
|
|
|
435
438
|
export async function updateCargoTomlNodeApiVersion(
|
|
@@ -475,7 +478,7 @@ export async function updateCargoTomlNodeApiVersion(
|
|
|
475
478
|
|
|
476
479
|
dependencies.napi = dependencyConfig
|
|
477
480
|
|
|
478
|
-
await fs.writeFile(filePath,
|
|
481
|
+
await fs.writeFile(filePath, serializeToml(cargoToml))
|
|
479
482
|
}
|
|
480
483
|
|
|
481
484
|
async function filterTargetsInGithubActions(
|
|
@@ -683,12 +686,7 @@ async function filterTargetsInGithubActions(
|
|
|
683
686
|
}
|
|
684
687
|
|
|
685
688
|
// Write back the filtered YAML
|
|
686
|
-
|
|
687
|
-
lineWidth: -1,
|
|
688
|
-
noRefs: true,
|
|
689
|
-
sortKeys: false,
|
|
690
|
-
})
|
|
691
|
-
await fs.writeFile(filePath, updatedYaml)
|
|
689
|
+
await fs.writeFile(filePath, serializeYaml(yaml))
|
|
692
690
|
}
|
|
693
691
|
|
|
694
692
|
function processOptions(options: RawNewOptions) {
|
|
@@ -853,10 +851,7 @@ export async function newProject(userOptions: RawNewOptions) {
|
|
|
853
851
|
)
|
|
854
852
|
}
|
|
855
853
|
|
|
856
|
-
await fs.writeFile(
|
|
857
|
-
packageJsonPath,
|
|
858
|
-
JSON.stringify(pkgJson, null, 2) + '\n',
|
|
859
|
-
)
|
|
854
|
+
await fs.writeFile(packageJsonPath, serializeJson(pkgJson))
|
|
860
855
|
} catch (error) {
|
|
861
856
|
throw new Error(`Failed to create project: ${error}`)
|
|
862
857
|
}
|
package/src/api/pre-publish.ts
CHANGED
|
@@ -58,6 +58,7 @@ import {
|
|
|
58
58
|
withFileSystemReconciliation,
|
|
59
59
|
AVAILABLE_TARGETS,
|
|
60
60
|
parseTriple,
|
|
61
|
+
serializeJson,
|
|
61
62
|
type CommonPackageJsonFields,
|
|
62
63
|
type FileSystemTransactionWrite,
|
|
63
64
|
type RootPublisher,
|
|
@@ -2267,7 +2268,7 @@ async function registerPublicationWorkspaces(
|
|
|
2267
2268
|
} else {
|
|
2268
2269
|
manifest.workspaces = releaseWorkspaces
|
|
2269
2270
|
}
|
|
2270
|
-
await writeFileAtomic(manifestPath,
|
|
2271
|
+
await writeFileAtomic(manifestPath, serializeJson(manifest))
|
|
2271
2272
|
}
|
|
2272
2273
|
|
|
2273
2274
|
async function createPublicationExecutionPackage(
|
|
@@ -2391,10 +2392,7 @@ async function materializeRootReleasePlan(
|
|
|
2391
2392
|
}
|
|
2392
2393
|
}
|
|
2393
2394
|
syncThreadlessWasiRootFacadeManifest(updatedPackageJson, plan.packageJson)
|
|
2394
|
-
await writeFileAtomic(
|
|
2395
|
-
packageJsonPath,
|
|
2396
|
-
JSON.stringify(updatedPackageJson, null, 2),
|
|
2397
|
-
)
|
|
2395
|
+
await writeFileAtomic(packageJsonPath, serializeJson(updatedPackageJson))
|
|
2398
2396
|
|
|
2399
2397
|
const generatedFiles = new Set(plan.facade?.generatedFiles ?? [])
|
|
2400
2398
|
await Promise.all(
|
|
@@ -2492,7 +2490,7 @@ async function stageReleasePackage(
|
|
|
2492
2490
|
const packageJsonPath = join(stagedPkgDir, 'package.json')
|
|
2493
2491
|
const packageJson = JSON.parse(await readFileAsync(packageJsonPath, 'utf8'))
|
|
2494
2492
|
packageJson.version = packageVersion
|
|
2495
|
-
await writeFileAtomic(packageJsonPath,
|
|
2493
|
+
await writeFileAtomic(packageJsonPath, serializeJson(packageJson))
|
|
2496
2494
|
return {
|
|
2497
2495
|
pkgDir: options.pkgDir,
|
|
2498
2496
|
rootDir: options.rootDir,
|
|
@@ -2705,10 +2703,7 @@ async function validateReleasePackageContents({
|
|
|
2705
2703
|
if (updateManifest) {
|
|
2706
2704
|
packageFiles.splice(0, packageFiles.length, ...declarationClosure.files)
|
|
2707
2705
|
packageJson.files = packageFiles
|
|
2708
|
-
await writeFileAtomic(
|
|
2709
|
-
packageJsonPath,
|
|
2710
|
-
`${JSON.stringify(packageJson, null, 2)}\n`,
|
|
2711
|
-
)
|
|
2706
|
+
await writeFileAtomic(packageJsonPath, serializeJson(packageJson))
|
|
2712
2707
|
}
|
|
2713
2708
|
|
|
2714
2709
|
const publicFiles = new Set<string>(packageFiles)
|
package/src/api/rename.ts
CHANGED
|
@@ -12,8 +12,8 @@ import {
|
|
|
12
12
|
sep,
|
|
13
13
|
} from 'node:path'
|
|
14
14
|
|
|
15
|
-
import { parse as parseToml
|
|
16
|
-
import { load as yamlParse
|
|
15
|
+
import { parse as parseToml } from '@std/toml'
|
|
16
|
+
import { load as yamlParse } from 'js-yaml'
|
|
17
17
|
import { isNil, omitBy, pick } from 'es-toolkit'
|
|
18
18
|
|
|
19
19
|
import { applyDefaultRenameOptions, type RenameOptions } from '../def/rename.js'
|
|
@@ -23,6 +23,9 @@ import {
|
|
|
23
23
|
readFileAsync,
|
|
24
24
|
resolvePackageReconciliationPaths,
|
|
25
25
|
type Target,
|
|
26
|
+
serializeJson,
|
|
27
|
+
serializeToml,
|
|
28
|
+
serializeYaml,
|
|
26
29
|
wasiLoaderSuffix,
|
|
27
30
|
wasiTargetHasThreads,
|
|
28
31
|
withPackageFileSystemReconciliation,
|
|
@@ -1265,10 +1268,6 @@ function updateNapiConfigRecord(record: JsonRecord, options: RenameOptions) {
|
|
|
1265
1268
|
}
|
|
1266
1269
|
}
|
|
1267
1270
|
|
|
1268
|
-
function serializeJsonLike(content: string, value: unknown) {
|
|
1269
|
-
return `${JSON.stringify(value, null, 2)}${content.endsWith('\n') ? '\n' : ''}`
|
|
1270
|
-
}
|
|
1271
|
-
|
|
1272
1271
|
function sanitizeCargoPackageName(binaryName: string) {
|
|
1273
1272
|
return binaryName.replace(/[^A-Za-z0-9_]/g, '_').toLowerCase()
|
|
1274
1273
|
}
|
|
@@ -1458,8 +1457,7 @@ async function renameProjectUnlocked(
|
|
|
1458
1457
|
const plan = new RenameTransactionPlan()
|
|
1459
1458
|
plan.addWrite(
|
|
1460
1459
|
packageJsonPath,
|
|
1461
|
-
|
|
1462
|
-
packageJsonContent,
|
|
1460
|
+
serializeJson(
|
|
1463
1461
|
rewritePackageManifest(
|
|
1464
1462
|
packageJsonData,
|
|
1465
1463
|
managedWasiRenames,
|
|
@@ -1477,11 +1475,7 @@ async function renameProjectUnlocked(
|
|
|
1477
1475
|
throw new Error(`NAPI config must contain a JSON object: ${configPath}`)
|
|
1478
1476
|
}
|
|
1479
1477
|
updateNapiConfigRecord(configData, options)
|
|
1480
|
-
plan.addWrite(
|
|
1481
|
-
configPath,
|
|
1482
|
-
serializeJsonLike(configContent, configData),
|
|
1483
|
-
configMode,
|
|
1484
|
-
)
|
|
1478
|
+
plan.addWrite(configPath, serializeJson(configData), configMode)
|
|
1485
1479
|
}
|
|
1486
1480
|
|
|
1487
1481
|
if (binaryNameChanged) {
|
|
@@ -1495,7 +1489,7 @@ async function renameProjectUnlocked(
|
|
|
1495
1489
|
const cargoToml = parseToml(tomlContent) as any
|
|
1496
1490
|
if (cargoToml.package) {
|
|
1497
1491
|
cargoToml.package.name = sanitizeCargoPackageName(newName)
|
|
1498
|
-
plan.addWrite(cargoTomlPath,
|
|
1492
|
+
plan.addWrite(cargoTomlPath, serializeToml(cargoToml), cargoTomlMode)
|
|
1499
1493
|
}
|
|
1500
1494
|
|
|
1501
1495
|
const workflowPath = join(projectRoot, '.github', 'workflows', 'CI.yml')
|
|
@@ -1515,11 +1509,7 @@ async function renameProjectUnlocked(
|
|
|
1515
1509
|
workflowData.env.APP_NAME = newName
|
|
1516
1510
|
plan.addWrite(
|
|
1517
1511
|
canonicalWorkflowPath,
|
|
1518
|
-
|
|
1519
|
-
lineWidth: -1,
|
|
1520
|
-
noRefs: true,
|
|
1521
|
-
sortKeys: false,
|
|
1522
|
-
}),
|
|
1512
|
+
serializeYaml(workflowData),
|
|
1523
1513
|
workflowMode,
|
|
1524
1514
|
)
|
|
1525
1515
|
}
|
|
@@ -1567,8 +1557,7 @@ async function renameProjectUnlocked(
|
|
|
1567
1557
|
`Managed package manifest must contain a JSON object: ${targetPackageJsonPath}`,
|
|
1568
1558
|
)
|
|
1569
1559
|
}
|
|
1570
|
-
const updatedContent =
|
|
1571
|
-
content,
|
|
1560
|
+
const updatedContent = serializeJson(
|
|
1572
1561
|
rewritePackageManifest(
|
|
1573
1562
|
manifest,
|
|
1574
1563
|
managedWasiRenames,
|
|
@@ -43,11 +43,11 @@ function createWasiFallbackChain(
|
|
|
43
43
|
specifier,
|
|
44
44
|
isPackage,
|
|
45
45
|
localArtifacts,
|
|
46
|
-
}) => ` if (!wasiBindingLoaded && (!__napiWasiFlavorRequested || __napiWasiFlavor === ${
|
|
46
|
+
}) => ` if (!wasiBindingLoaded && (!__napiWasiFlavorRequested || __napiWasiFlavor === '${flavor}')) {
|
|
47
47
|
let candidateError = null
|
|
48
48
|
let candidateFailed = false
|
|
49
49
|
try {
|
|
50
|
-
candidateError = __napiWasiResolveCandidate('${specifier}', ${isPackage}, ${
|
|
50
|
+
candidateError = __napiWasiResolveCandidate('${specifier}', ${isPackage}, ${localArtifacts ? `[${localArtifacts.map((artifact) => `'${artifact}'`).join(', ')}]` : 'undefined'})
|
|
51
51
|
candidateFailed = candidateError !== null
|
|
52
52
|
if (!candidateFailed) {${
|
|
53
53
|
isPackage && packageVersion
|
|
@@ -169,8 +169,7 @@ ${exportsCode}
|
|
|
169
169
|
`
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
-
const bindingHeader =
|
|
173
|
-
/* eslint-disable */
|
|
172
|
+
const bindingHeader = `/* eslint-disable */
|
|
174
173
|
// @ts-nocheck
|
|
175
174
|
/* auto-generated by NAPI-RS */
|
|
176
175
|
`
|
|
@@ -272,7 +271,7 @@ const isMuslFromChildProcess = () => {
|
|
|
272
271
|
function requireNative() {
|
|
273
272
|
if (process.env.NAPI_RS_NATIVE_LIBRARY_PATH) {
|
|
274
273
|
try {
|
|
275
|
-
return require(process.env.NAPI_RS_NATIVE_LIBRARY_PATH)
|
|
274
|
+
return require(process.env.NAPI_RS_NATIVE_LIBRARY_PATH)
|
|
276
275
|
} catch (err) {
|
|
277
276
|
loadErrors.push(err)
|
|
278
277
|
}
|
|
@@ -287,9 +286,9 @@ function requireNative() {
|
|
|
287
286
|
} else if (process.platform === 'win32') {
|
|
288
287
|
if (process.arch === 'x64') {
|
|
289
288
|
if ((process.config && process.config.variables && process.config.variables.shlib_suffix === 'dll.a') || (process.config && process.config.variables && process.config.variables.node_target_type === 'shared_library')) {
|
|
290
|
-
${requireTuple('win32-x64-gnu')}
|
|
289
|
+
${requireTuple('win32-x64-gnu', 10)}
|
|
291
290
|
} else {
|
|
292
|
-
${requireTuple('win32-x64-msvc')}
|
|
291
|
+
${requireTuple('win32-x64-msvc', 10)}
|
|
293
292
|
}
|
|
294
293
|
} else if (process.arch === 'ia32') {
|
|
295
294
|
${requireTuple('win32-ia32-msvc')}
|
|
@@ -395,7 +394,7 @@ function createLoadErrorChain(errors) {
|
|
|
395
394
|
//
|
|
396
395
|
// NAPI_RS_WASI_FLAVOR selects one exact generated flavor and implies strict
|
|
397
396
|
// WASI loading. It never crosses into another flavor or falls back to native.
|
|
398
|
-
const __napiWasiFlavors = ${
|
|
397
|
+
const __napiWasiFlavors = [${flavors.map((flavor) => `'${flavor}'`).join(', ')}]
|
|
399
398
|
const __napiWasiFlavor = process.env.NAPI_RS_WASI_FLAVOR
|
|
400
399
|
const __napiWasiFlavorRequested =
|
|
401
400
|
typeof __napiWasiFlavor === 'string' && __napiWasiFlavor.length > 0
|
|
@@ -13,11 +13,11 @@ let __emnapiWasmEnvCleanupDrained = false
|
|
|
13
13
|
let __emnapiWasmEnvCleanupDrainPromise
|
|
14
14
|
let __wasiDisposed = false
|
|
15
15
|
let __wasiDisposePromise
|
|
16
|
-
let __completeWasiDisposal = function() {}
|
|
16
|
+
let __completeWasiDisposal = function () {}
|
|
17
17
|
// Overridden by loader flavors that have a last-resort reclaim for a rollback
|
|
18
18
|
// that stopped short of destroying the context. See
|
|
19
19
|
// \`__rollbackWasiInitialization\`.
|
|
20
|
-
let __retainWasiRollbackForRetry = function() {}
|
|
20
|
+
let __retainWasiRollbackForRetry = function () {}
|
|
21
21
|
|
|
22
22
|
function __isThenable(value) {
|
|
23
23
|
return (
|
|
@@ -1923,6 +1923,7 @@ function __createWasiWorker(filename) {
|
|
|
1923
1923
|
return new Worker(filename, {
|
|
1924
1924
|
env: process.env,
|
|
1925
1925
|
execArgv: __workerExecArgv,
|
|
1926
|
+
workerData: { hostRoot: __hostRoot, rootDir: __rootDir },
|
|
1926
1927
|
})
|
|
1927
1928
|
} catch (error) {
|
|
1928
1929
|
if (!error || error.code !== 'ERR_WORKER_INVALID_EXEC_ARGV') {
|
|
@@ -1944,7 +1945,7 @@ function __createWasiWorker(filename) {
|
|
|
1944
1945
|
: ''
|
|
1945
1946
|
const memoryName = threads ? '__sharedMemory' : '__wasmMemory'
|
|
1946
1947
|
const asyncWorkOptions = threads
|
|
1947
|
-
? ` asyncWorkPoolSize: (function() {
|
|
1948
|
+
? ` asyncWorkPoolSize: (function () {
|
|
1948
1949
|
const threadsSizeFromEnv = Number(process.env.NAPI_RS_ASYNC_WORK_POOL_SIZE ?? process.env.UV_THREADPOOL_SIZE)
|
|
1949
1950
|
// NaN > 0 is false
|
|
1950
1951
|
if (threadsSizeFromEnv > 0) {
|
|
@@ -1980,21 +1981,21 @@ function __createWasiWorker(filename) {
|
|
|
1980
1981
|
// According to https://github.com/nodejs/node/blob/19e0d472728c79d418b74bddff588bea70a403d0/lib/internal/worker.js#L415,
|
|
1981
1982
|
// a worker is consist of two handles: kPublicPort and kHandle.
|
|
1982
1983
|
{
|
|
1983
|
-
const kPublicPort = Object.getOwnPropertySymbols(worker).find(s =>
|
|
1984
|
-
s.toString().includes(
|
|
1985
|
-
)
|
|
1984
|
+
const kPublicPort = Object.getOwnPropertySymbols(worker).find((s) =>
|
|
1985
|
+
s.toString().includes('kPublicPort'),
|
|
1986
|
+
)
|
|
1986
1987
|
if (kPublicPort) {
|
|
1987
|
-
worker[kPublicPort].ref = () => {}
|
|
1988
|
+
worker[kPublicPort].ref = () => {}
|
|
1988
1989
|
}
|
|
1989
1990
|
|
|
1990
|
-
const kHandle = Object.getOwnPropertySymbols(worker).find(s =>
|
|
1991
|
-
s.toString().includes(
|
|
1992
|
-
)
|
|
1991
|
+
const kHandle = Object.getOwnPropertySymbols(worker).find((s) =>
|
|
1992
|
+
s.toString().includes('kHandle'),
|
|
1993
|
+
)
|
|
1993
1994
|
if (kHandle) {
|
|
1994
|
-
worker[kHandle].ref = () => {}
|
|
1995
|
+
worker[kHandle].ref = () => {}
|
|
1995
1996
|
}
|
|
1996
1997
|
|
|
1997
|
-
worker.unref()
|
|
1998
|
+
worker.unref()
|
|
1998
1999
|
}
|
|
1999
2000
|
return worker
|
|
2000
2001
|
},
|
|
@@ -2002,8 +2003,6 @@ function __createWasiWorker(filename) {
|
|
|
2002
2003
|
: ''
|
|
2003
2004
|
|
|
2004
2005
|
return `/* eslint-disable */
|
|
2005
|
-
/* prettier-ignore */
|
|
2006
|
-
|
|
2007
2006
|
/* auto-generated by NAPI-RS */
|
|
2008
2007
|
|
|
2009
2008
|
const __nodeFs = require('node:fs')
|
|
@@ -2019,14 +2018,18 @@ ${workerRuntimeImport}\
|
|
|
2019
2018
|
const { createContext: __emnapiCreateContext } = require('@emnapi/runtime')
|
|
2020
2019
|
${workerExecArgv}\
|
|
2021
2020
|
|
|
2022
|
-
const
|
|
2021
|
+
const __cwd = process.cwd()
|
|
2022
|
+
const __rootDir = __nodePath.parse(__cwd).root
|
|
2023
|
+
const __hostRoot =
|
|
2024
|
+
process.platform === 'android' ? __cwd : __rootDir
|
|
2023
2025
|
|
|
2024
2026
|
const __wasi = new __nodeWASI({
|
|
2025
2027
|
version: 'preview1',
|
|
2026
2028
|
env: process.env,
|
|
2027
2029
|
preopens: {
|
|
2028
|
-
[__rootDir]:
|
|
2029
|
-
|
|
2030
|
+
[__rootDir]: __hostRoot,
|
|
2031
|
+
[__hostRoot]: __hostRoot,
|
|
2032
|
+
},
|
|
2030
2033
|
})
|
|
2031
2034
|
|
|
2032
2035
|
const ${memoryName} = new WebAssembly.Memory({
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export const WASI_WORKER_TEMPLATE = `import fs from
|
|
2
|
-
import { createRequire } from
|
|
3
|
-
import { parse } from
|
|
4
|
-
import { WASI } from
|
|
5
|
-
import { parentPort, Worker } from
|
|
1
|
+
export const WASI_WORKER_TEMPLATE = `import fs from 'node:fs'
|
|
2
|
+
import { createRequire } from 'node:module'
|
|
3
|
+
import { parse } from 'node:path'
|
|
4
|
+
import { WASI } from 'node:wasi'
|
|
5
|
+
import { parentPort, Worker, workerData } from 'node:worker_threads'
|
|
6
6
|
|
|
7
|
-
const require = createRequire(import.meta.url)
|
|
7
|
+
const require = createRequire(import.meta.url)
|
|
8
8
|
|
|
9
9
|
const {
|
|
10
10
|
instantiateNapiModuleSync,
|
|
@@ -12,12 +12,12 @@ const {
|
|
|
12
12
|
getDefaultContext,
|
|
13
13
|
emnapiAsyncWorkPlugin,
|
|
14
14
|
emnapiTSFNPlugin,
|
|
15
|
-
} = require(
|
|
15
|
+
} = require('@napi-rs/wasm-runtime')
|
|
16
16
|
|
|
17
17
|
if (parentPort) {
|
|
18
|
-
parentPort.on(
|
|
19
|
-
globalThis.onmessage({ data })
|
|
20
|
-
})
|
|
18
|
+
parentPort.on('message', (data) => {
|
|
19
|
+
globalThis.onmessage({ data })
|
|
20
|
+
})
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
Object.assign(globalThis, {
|
|
@@ -25,18 +25,25 @@ Object.assign(globalThis, {
|
|
|
25
25
|
require,
|
|
26
26
|
Worker,
|
|
27
27
|
importScripts: function (f) {
|
|
28
|
-
|
|
28
|
+
// oxlint-disable-next-line no-eval -- WASI importScripts polyfill
|
|
29
|
+
;(0, eval)(fs.readFileSync(f, 'utf8') + '//# sourceURL=' + f)
|
|
29
30
|
},
|
|
30
31
|
postMessage: function (msg) {
|
|
31
32
|
if (parentPort) {
|
|
32
|
-
parentPort.postMessage(msg)
|
|
33
|
+
parentPort.postMessage(msg)
|
|
33
34
|
}
|
|
34
35
|
},
|
|
35
|
-
})
|
|
36
|
+
})
|
|
36
37
|
|
|
37
|
-
const emnapiContext = getDefaultContext()
|
|
38
|
+
const emnapiContext = getDefaultContext()
|
|
38
39
|
|
|
39
|
-
const
|
|
40
|
+
const __cwd = process.cwd()
|
|
41
|
+
const __rootDir =
|
|
42
|
+
(workerData && typeof workerData.rootDir === 'string' && workerData.rootDir) ||
|
|
43
|
+
parse(__cwd).root
|
|
44
|
+
const __hostRoot =
|
|
45
|
+
(workerData && typeof workerData.hostRoot === 'string' && workerData.hostRoot) ||
|
|
46
|
+
(process.platform === 'android' ? __cwd : __rootDir)
|
|
40
47
|
|
|
41
48
|
const handler = new MessageHandler({
|
|
42
49
|
onLoad({ wasmModule, wasmMemory }) {
|
|
@@ -44,9 +51,10 @@ const handler = new MessageHandler({
|
|
|
44
51
|
version: 'preview1',
|
|
45
52
|
env: process.env,
|
|
46
53
|
preopens: {
|
|
47
|
-
[__rootDir]:
|
|
54
|
+
[__rootDir]: __hostRoot,
|
|
55
|
+
[__hostRoot]: __hostRoot,
|
|
48
56
|
},
|
|
49
|
-
})
|
|
57
|
+
})
|
|
50
58
|
|
|
51
59
|
return instantiateNapiModuleSync(wasmModule, {
|
|
52
60
|
childThread: true,
|
|
@@ -62,16 +70,16 @@ const handler = new MessageHandler({
|
|
|
62
70
|
...importObject.env,
|
|
63
71
|
...importObject.napi,
|
|
64
72
|
...importObject.emnapi,
|
|
65
|
-
memory: wasmMemory
|
|
66
|
-
}
|
|
73
|
+
memory: wasmMemory,
|
|
74
|
+
}
|
|
67
75
|
},
|
|
68
|
-
})
|
|
76
|
+
})
|
|
69
77
|
},
|
|
70
|
-
})
|
|
78
|
+
})
|
|
71
79
|
|
|
72
80
|
globalThis.onmessage = function (e) {
|
|
73
|
-
handler.handle(e)
|
|
74
|
-
}
|
|
81
|
+
handler.handle(e)
|
|
82
|
+
}
|
|
75
83
|
`
|
|
76
84
|
|
|
77
85
|
export const createWasiBrowserWorkerBinding = (
|
|
@@ -110,10 +118,9 @@ const fs = createFsProxy(__memfsExported)`
|
|
|
110
118
|
// eslint-disable-next-line no-console
|
|
111
119
|
console.log.apply(console, arguments)
|
|
112
120
|
},
|
|
113
|
-
printErr: function() {
|
|
121
|
+
printErr: function () {
|
|
114
122
|
// eslint-disable-next-line no-console
|
|
115
|
-
console.error.apply(console, arguments)
|
|
116
|
-
${errorOutputsAppend}
|
|
123
|
+
console.error.apply(console, arguments)${errorOutputsAppend}
|
|
117
124
|
},
|
|
118
125
|
})`
|
|
119
126
|
: `const wasi = new WASI({
|
|
@@ -121,10 +128,9 @@ const fs = createFsProxy(__memfsExported)`
|
|
|
121
128
|
// eslint-disable-next-line no-console
|
|
122
129
|
console.log.apply(console, arguments)
|
|
123
130
|
},
|
|
124
|
-
printErr: function() {
|
|
131
|
+
printErr: function () {
|
|
125
132
|
// eslint-disable-next-line no-console
|
|
126
|
-
console.error.apply(console, arguments)
|
|
127
|
-
${errorOutputsAppend}
|
|
133
|
+
console.error.apply(console, arguments)${errorOutputsAppend}
|
|
128
134
|
},
|
|
129
135
|
})`
|
|
130
136
|
const errorHandler = errorEvent
|
|
@@ -156,8 +162,7 @@ const handler = new MessageHandler({
|
|
|
156
162
|
}
|
|
157
163
|
},
|
|
158
164
|
})
|
|
159
|
-
}
|
|
160
|
-
${errorHandler}
|
|
165
|
+
},${errorHandler ? `\n ${errorHandler}` : ''}
|
|
161
166
|
})
|
|
162
167
|
|
|
163
168
|
globalThis.onmessage = function (e) {
|
package/src/commands/build.ts
CHANGED
|
@@ -11,7 +11,7 @@ const debug = debugFactory('build')
|
|
|
11
11
|
export class BuildCommand extends BaseBuildCommand {
|
|
12
12
|
pipe = Option.String('--pipe', {
|
|
13
13
|
description:
|
|
14
|
-
'Pipe all outputs file to given command. e.g. `napi build --pipe "npx
|
|
14
|
+
'Pipe all outputs file to given command. e.g. `napi build --pipe "npx oxfmt"`',
|
|
15
15
|
})
|
|
16
16
|
|
|
17
17
|
cargoOptions = Option.Rest()
|
|
@@ -142,9 +142,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
142
142
|
}␊
|
|
143
143
|
export type Blake2bHasher = Blake2BHasher␊
|
|
144
144
|
␊
|
|
145
|
-
export declare class Blake2BKey {␊
|
|
146
|
-
␊
|
|
147
|
-
}␊
|
|
145
|
+
export declare class Blake2BKey {}␊
|
|
148
146
|
export type Blake2bKey = Blake2BKey␊
|
|
149
147
|
␊
|
|
150
148
|
export declare class ClassWithFactory {␊
|
|
@@ -257,7 +255,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
257
255
|
␊
|
|
258
256
|
export declare const enum ALIAS {␊
|
|
259
257
|
A = 0,␊
|
|
260
|
-
B = 1
|
|
258
|
+
B = 1,␊
|
|
261
259
|
}␊
|
|
262
260
|
␊
|
|
263
261
|
export interface AliasedStruct {␊
|
|
@@ -345,7 +343,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
345
343
|
Six = 6,␊
|
|
346
344
|
Eight = 8,␊
|
|
347
345
|
Nine = 9,␊
|
|
348
|
-
Ten = 10
|
|
346
|
+
Ten = 10,␊
|
|
349
347
|
}␊
|
|
350
348
|
␊
|
|
351
349
|
export declare function customStatusCode(): void␊
|
|
@@ -358,7 +356,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
358
356
|
export declare function dateToNumber(input: Date): number␊
|
|
359
357
|
␊
|
|
360
358
|
/** This is a const */␊
|
|
361
|
-
export const DEFAULT_COST: number␊
|
|
359
|
+
export declare const DEFAULT_COST: number␊
|
|
362
360
|
␊
|
|
363
361
|
export declare function derefUint8Array(a: Uint8Array, b: Uint8ClampedArray): number␊
|
|
364
362
|
␊
|
|
@@ -374,9 +372,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
374
372
|
␊
|
|
375
373
|
export declare function eitherStringOrNumber(input: string | number): number␊
|
|
376
374
|
␊
|
|
377
|
-
export declare const enum Empty {␊
|
|
378
|
-
␊
|
|
379
|
-
}␊
|
|
375
|
+
export declare const enum Empty {}␊
|
|
380
376
|
␊
|
|
381
377
|
export declare function enumToI32(e: CustomNumEnum): number␊
|
|
382
378
|
␊
|
|
@@ -422,7 +418,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
422
418
|
/** Kills birds */␊
|
|
423
419
|
Cat = 1,␊
|
|
424
420
|
/** Tasty */␊
|
|
425
|
-
Duck = 2
|
|
421
|
+
Duck = 2,␊
|
|
426
422
|
}␊
|
|
427
423
|
␊
|
|
428
424
|
export declare function listObjKeys(obj: object): Array<string>␊
|
|
@@ -519,7 +515,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
519
515
|
export declare const enum Status {␊
|
|
520
516
|
Pristine = 'Pristine',␊
|
|
521
517
|
Loading = 'Loading',␊
|
|
522
|
-
Ready = 'Ready'
|
|
518
|
+
Ready = 'Ready',␊
|
|
523
519
|
}␊
|
|
524
520
|
␊
|
|
525
521
|
export interface StrictObject {␊
|
|
@@ -695,9 +691,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
695
691
|
}␊
|
|
696
692
|
export type Blake2bHasher = Blake2BHasher␊
|
|
697
693
|
␊
|
|
698
|
-
export declare class Blake2BKey {␊
|
|
699
|
-
␊
|
|
700
|
-
}␊
|
|
694
|
+
export declare class Blake2BKey {}␊
|
|
701
695
|
export type Blake2bKey = Blake2BKey␊
|
|
702
696
|
␊
|
|
703
697
|
export declare class ClassWithFactory {␊
|
|
@@ -810,7 +804,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
810
804
|
␊
|
|
811
805
|
export declare enum ALIAS {␊
|
|
812
806
|
A = 0,␊
|
|
813
|
-
B = 1
|
|
807
|
+
B = 1,␊
|
|
814
808
|
}␊
|
|
815
809
|
␊
|
|
816
810
|
export interface AliasedStruct {␊
|
|
@@ -898,7 +892,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
898
892
|
Six = 6,␊
|
|
899
893
|
Eight = 8,␊
|
|
900
894
|
Nine = 9,␊
|
|
901
|
-
Ten = 10
|
|
895
|
+
Ten = 10,␊
|
|
902
896
|
}␊
|
|
903
897
|
␊
|
|
904
898
|
export declare function customStatusCode(): void␊
|
|
@@ -911,7 +905,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
911
905
|
export declare function dateToNumber(input: Date): number␊
|
|
912
906
|
␊
|
|
913
907
|
/** This is a const */␊
|
|
914
|
-
export const DEFAULT_COST: number␊
|
|
908
|
+
export declare const DEFAULT_COST: number␊
|
|
915
909
|
␊
|
|
916
910
|
export declare function derefUint8Array(a: Uint8Array, b: Uint8ClampedArray): number␊
|
|
917
911
|
␊
|
|
@@ -927,9 +921,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
927
921
|
␊
|
|
928
922
|
export declare function eitherStringOrNumber(input: string | number): number␊
|
|
929
923
|
␊
|
|
930
|
-
export declare enum Empty {␊
|
|
931
|
-
␊
|
|
932
|
-
}␊
|
|
924
|
+
export declare enum Empty {}␊
|
|
933
925
|
␊
|
|
934
926
|
export declare function enumToI32(e: CustomNumEnum): number␊
|
|
935
927
|
␊
|
|
@@ -975,7 +967,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
975
967
|
/** Kills birds */␊
|
|
976
968
|
Cat = 1,␊
|
|
977
969
|
/** Tasty */␊
|
|
978
|
-
Duck = 2
|
|
970
|
+
Duck = 2,␊
|
|
979
971
|
}␊
|
|
980
972
|
␊
|
|
981
973
|
export declare function listObjKeys(obj: object): Array<string>␊
|
|
@@ -1069,9 +1061,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1069
1061
|
␊
|
|
1070
1062
|
export declare function setSymbolInObj(symbol: symbol): object␊
|
|
1071
1063
|
␊
|
|
1072
|
-
export type Status =
|
|
1073
|
-
'Loading'|␊
|
|
1074
|
-
'Ready';␊
|
|
1064
|
+
export type Status = 'Pristine' | 'Loading' | 'Ready'␊
|
|
1075
1065
|
␊
|
|
1076
1066
|
export interface StrictObject {␊
|
|
1077
1067
|
name: string␊
|
|
@@ -1172,12 +1162,12 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1172
1162
|
|
|
1173
1163
|
`export declare enum Priority {␊
|
|
1174
1164
|
Low = 0,␊
|
|
1175
|
-
High = 1
|
|
1165
|
+
High = 1,␊
|
|
1176
1166
|
}␊
|
|
1177
1167
|
␊
|
|
1178
1168
|
export declare enum Status {␊
|
|
1179
1169
|
Active = 'Active',␊
|
|
1180
|
-
Inactive = 'Inactive'
|
|
1170
|
+
Inactive = 'Inactive',␊
|
|
1181
1171
|
}␊
|
|
1182
1172
|
`
|
|
1183
1173
|
|
|
@@ -1187,11 +1177,11 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1187
1177
|
|
|
1188
1178
|
`export declare const enum Priority {␊
|
|
1189
1179
|
Low = 0,␊
|
|
1190
|
-
High = 1
|
|
1180
|
+
High = 1,␊
|
|
1191
1181
|
}␊
|
|
1192
1182
|
␊
|
|
1193
1183
|
export declare const enum Status {␊
|
|
1194
1184
|
Active = 'Active',␊
|
|
1195
|
-
Inactive = 'Inactive'
|
|
1185
|
+
Inactive = 'Inactive',␊
|
|
1196
1186
|
}␊
|
|
1197
1187
|
`
|
|
Binary file
|
|
@@ -71,6 +71,10 @@ test('updatePackageJson merges nested objects instead of overwriting them', asyn
|
|
|
71
71
|
fsevents: '^2.3.3',
|
|
72
72
|
'@napi-rs/fixture-darwin-arm64': '1.0.1',
|
|
73
73
|
})
|
|
74
|
+
|
|
75
|
+
const written = await readFile(packageJsonPath, 'utf8')
|
|
76
|
+
t.true(written.endsWith('\n'))
|
|
77
|
+
t.false(written.endsWith('\n\n'))
|
|
74
78
|
})
|
|
75
79
|
|
|
76
80
|
test('statIdentitiesMatch distinguishes inodes that collide as lossy Numbers', (t) => {
|