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

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 (155) hide show
  1. package/dist/api/artifacts.js +28 -7
  2. package/dist/api/artifacts.js.map +1 -1
  3. package/dist/api/build.js +220 -39
  4. package/dist/api/build.js.map +1 -1
  5. package/dist/api/create-npm-dirs.js +45 -8
  6. package/dist/api/create-npm-dirs.js.map +1 -1
  7. package/dist/api/new.js +57 -20
  8. package/dist/api/new.js.map +1 -1
  9. package/dist/api/pre-publish.js +9 -7
  10. package/dist/api/pre-publish.js.map +1 -1
  11. package/dist/api/rename.js +9 -1
  12. package/dist/api/rename.js.map +1 -1
  13. package/dist/api/templates/ci-template.d.ts +3 -1
  14. package/dist/api/templates/ci-template.js +124 -124
  15. package/dist/api/templates/ci-template.js.map +1 -1
  16. package/dist/api/templates/ci.yml.d.ts +3 -1
  17. package/dist/api/templates/ci.yml.js +21 -5
  18. package/dist/api/templates/ci.yml.js.map +1 -1
  19. package/dist/api/templates/js-binding.js +104 -214
  20. package/dist/api/templates/js-binding.js.map +1 -1
  21. package/dist/api/templates/lib.rs.js +1 -4
  22. package/dist/api/templates/lib.rs.js.map +1 -1
  23. package/dist/api/templates/load-wasi-template.d.ts +2 -0
  24. package/dist/api/templates/load-wasi-template.js +166 -0
  25. package/dist/api/templates/load-wasi-template.js.map +1 -0
  26. package/dist/api/templates/package.json.d.ts +9 -2
  27. package/dist/api/templates/package.json.js +26 -4
  28. package/dist/api/templates/package.json.js.map +1 -1
  29. package/dist/api/templates/wasi-worker-template.d.ts +2 -0
  30. package/dist/api/templates/wasi-worker-template.js +122 -0
  31. package/dist/api/templates/wasi-worker-template.js.map +1 -0
  32. package/dist/api/universalize.js +5 -5
  33. package/dist/api/universalize.js.map +1 -1
  34. package/dist/api/version.js +2 -2
  35. package/dist/api/version.js.map +1 -1
  36. package/dist/commands/artifacts.js +2 -2
  37. package/dist/commands/artifacts.js.map +1 -1
  38. package/dist/commands/build.js +1 -1
  39. package/dist/commands/build.js.map +1 -1
  40. package/dist/commands/new.js +30 -8
  41. package/dist/commands/new.js.map +1 -1
  42. package/dist/def/artifacts.d.ts +21 -1
  43. package/dist/def/artifacts.js +10 -2
  44. package/dist/def/artifacts.js.map +1 -1
  45. package/dist/def/build.d.ts +22 -4
  46. package/dist/def/build.js +16 -4
  47. package/dist/def/build.js.map +1 -1
  48. package/dist/def/create-npm-dirs.d.ts +10 -0
  49. package/dist/def/create-npm-dirs.js +4 -0
  50. package/dist/def/create-npm-dirs.js.map +1 -1
  51. package/dist/def/new.d.ts +25 -7
  52. package/dist/def/new.js +11 -1
  53. package/dist/def/new.js.map +1 -1
  54. package/dist/def/pre-publish.d.ts +12 -2
  55. package/dist/def/pre-publish.js +7 -3
  56. package/dist/def/pre-publish.js.map +1 -1
  57. package/dist/def/rename.d.ts +16 -6
  58. package/dist/def/rename.js +5 -1
  59. package/dist/def/rename.js.map +1 -1
  60. package/dist/def/universalize.d.ts +10 -0
  61. package/dist/def/universalize.js +4 -0
  62. package/dist/def/universalize.js.map +1 -1
  63. package/dist/def/version.d.ts +10 -0
  64. package/dist/def/version.js +4 -0
  65. package/dist/def/version.js.map +1 -1
  66. package/dist/index.cjs +8143 -14638
  67. package/dist/index.d.ts +1 -0
  68. package/dist/index.js +1 -0
  69. package/dist/index.js.map +1 -1
  70. package/dist/utils/__tests__/config.spec.d.ts +1 -0
  71. package/dist/utils/__tests__/config.spec.js +63 -0
  72. package/dist/utils/__tests__/config.spec.js.map +1 -0
  73. package/dist/utils/__tests__/typegen.spec.js +8 -4
  74. package/dist/utils/__tests__/typegen.spec.js.map +1 -1
  75. package/dist/utils/cargo.js +1 -1
  76. package/dist/utils/cargo.js.map +1 -1
  77. package/dist/utils/config.d.ts +49 -5
  78. package/dist/utils/config.js +38 -6
  79. package/dist/utils/config.js.map +1 -1
  80. package/dist/utils/index.d.ts +0 -1
  81. package/dist/utils/index.js +0 -1
  82. package/dist/utils/index.js.map +1 -1
  83. package/dist/utils/metadata.js +2 -2
  84. package/dist/utils/metadata.js.map +1 -1
  85. package/dist/utils/misc.d.ts +1 -2
  86. package/dist/utils/misc.js +3 -3
  87. package/dist/utils/misc.js.map +1 -1
  88. package/dist/utils/target.d.ts +7 -5
  89. package/dist/utils/target.js +17 -3
  90. package/dist/utils/target.js.map +1 -1
  91. package/dist/utils/typegen.d.ts +1 -1
  92. package/dist/utils/typegen.js +29 -9
  93. package/dist/utils/typegen.js.map +1 -1
  94. package/dist/utils/version.d.ts +2 -1
  95. package/dist/utils/version.js +10 -8
  96. package/dist/utils/version.js.map +1 -1
  97. package/package.json +25 -24
  98. package/src/api/artifacts.ts +81 -9
  99. package/src/api/build.ts +379 -58
  100. package/src/api/create-npm-dirs.ts +56 -8
  101. package/src/api/new.ts +89 -31
  102. package/src/api/pre-publish.ts +13 -7
  103. package/src/api/rename.ts +10 -1
  104. package/src/api/templates/ci-template.ts +134 -124
  105. package/src/api/templates/ci.yml.ts +27 -6
  106. package/src/api/templates/js-binding.ts +105 -214
  107. package/src/api/templates/lib.rs.ts +1 -4
  108. package/src/api/templates/load-wasi-template.ts +180 -0
  109. package/src/api/templates/package.json.ts +47 -5
  110. package/src/api/templates/wasi-worker-template.ts +122 -0
  111. package/src/api/universalize.ts +13 -8
  112. package/src/api/version.ts +5 -2
  113. package/src/commands/artifacts.ts +2 -2
  114. package/src/commands/build.ts +1 -1
  115. package/src/commands/new.ts +31 -8
  116. package/src/def/artifacts.ts +22 -3
  117. package/src/def/build.ts +36 -8
  118. package/src/def/create-npm-dirs.ts +9 -0
  119. package/src/def/new.ts +28 -3
  120. package/src/def/pre-publish.ts +12 -3
  121. package/src/def/rename.ts +11 -2
  122. package/src/def/universalize.ts +9 -0
  123. package/src/def/version.ts +9 -0
  124. package/src/index.ts +2 -0
  125. package/src/utils/__tests__/__fixtures__/napi_type_def +130 -130
  126. package/src/utils/__tests__/__snapshots__/config.spec.ts.md +97 -0
  127. package/src/utils/__tests__/__snapshots__/config.spec.ts.snap +0 -0
  128. package/src/utils/__tests__/__snapshots__/target.spec.ts.md +28 -0
  129. package/src/utils/__tests__/__snapshots__/target.spec.ts.snap +0 -0
  130. package/src/utils/__tests__/__snapshots__/typegen.spec.ts.md +713 -158
  131. package/src/utils/__tests__/__snapshots__/typegen.spec.ts.snap +0 -0
  132. package/src/utils/__tests__/__snapshots__/version.spec.ts.md +8 -7
  133. package/src/utils/__tests__/__snapshots__/version.spec.ts.snap +0 -0
  134. package/src/utils/__tests__/config.spec.ts +81 -0
  135. package/src/utils/__tests__/typegen.spec.ts +18 -3
  136. package/src/utils/cargo.ts +1 -1
  137. package/src/utils/config.ts +114 -12
  138. package/src/utils/index.ts +0 -1
  139. package/src/utils/metadata.ts +2 -2
  140. package/src/utils/misc.ts +11 -3
  141. package/src/utils/target.ts +22 -4
  142. package/src/utils/typegen.ts +37 -8
  143. package/src/utils/version.ts +10 -8
  144. package/dist/api/load-wasi-template.d.ts +0 -1
  145. package/dist/api/load-wasi-template.js +0 -63
  146. package/dist/api/load-wasi-template.js.map +0 -1
  147. package/dist/api/wasi-worker-template.d.ts +0 -1
  148. package/dist/api/wasi-worker-template.js +0 -53
  149. package/dist/api/wasi-worker-template.js.map +0 -1
  150. package/dist/utils/ci.d.ts +0 -11
  151. package/dist/utils/ci.js +0 -96
  152. package/dist/utils/ci.js.map +0 -1
  153. package/src/api/load-wasi-template.ts +0 -65
  154. package/src/api/wasi-worker-template.ts +0 -52
  155. package/src/utils/ci.ts +0 -111
@@ -9,12 +9,13 @@ Generated by [AVA](https://avajs.dev).
9
9
  > Snapshot 1
10
10
 
11
11
  [
12
- '>= 8.6.0',
13
- '>= 8.10.0 < 9 || >= 9.3.0',
14
- '>= 6.14.2 < 7 || >= 8.11.2 < 9 || >= 9.11.0',
15
- '>= 10.16.0 < 11 || >= 11.8.0',
16
- '>= 10.17.0 < 11 || >= 12.11.0',
12
+ '>= 8.6.0 < 9 || >= 9.0.0 < 10 || >= 10.0.0',
13
+ '>= 8.10.0 < 9 || >= 9.3.0 < 10 || >= 10.0.0',
14
+ '>= 6.14.2 < 7 || >= 8.11.2 < 9 || >= 9.11.0 < 10 || >= 10.0.0',
15
+ '>= 10.16.0 < 11 || >= 11.8.0 < 12 || >= 12.0.0',
16
+ '>= 10.17.0 < 11 || >= 12.11.0 < 13 || >= 13.0.0',
17
17
  '>= 10.20.0 < 11 || >= 12.17.0 < 13 || >= 14.0.0',
18
- '>= 10.23.0 < 11 || >= 12.19.0 < 13 || >= 14.12.0',
19
- '>= 12.22.0 < 13 || >= 14.17.0 < 15 || >= 15.12.0',
18
+ '>= 10.23.0 < 11 || >= 12.19.0 < 13 || >= 14.12.0 < 15 || >= 15.0.0',
19
+ '>= 12.22.0 < 13 || >= 14.17.0 < 15 || >= 15.12.0 < 16 || >= 16.0.0',
20
+ '>= 18.17.0 < 19 || >= 20.3.0 < 21 || >= 21.1.0',
20
21
  ]
@@ -0,0 +1,81 @@
1
+ import { unlink, writeFile } from 'node:fs/promises'
2
+ import { tmpdir } from 'node:os'
3
+ import { join } from 'node:path'
4
+
5
+ import ava, { TestFn } from 'ava'
6
+
7
+ import {
8
+ CommonPackageJsonFields,
9
+ UserNapiConfig,
10
+ readNapiConfig,
11
+ } from '../config.js'
12
+
13
+ const NON_EXISTENT_FILE = 'non-existent-file'
14
+
15
+ const test = ava as TestFn<{
16
+ configPath: string
17
+ packageJson: string
18
+ pkgJson: CommonPackageJsonFields
19
+ config: UserNapiConfig
20
+ }>
21
+
22
+ test.before(async (t) => {
23
+ const tmp = tmpdir()
24
+ const configPath = join(tmp, 'napi.json')
25
+ const packageJson = join(tmp, 'package.json')
26
+ const pkgJson = {
27
+ name: '@napi-rs/testing',
28
+ version: '0.0.0',
29
+ napi: {
30
+ binaryName: 'testing',
31
+ packageName: '@napi-rs/testing',
32
+ targets: [
33
+ 'x86_64-unknown-linux-gnu',
34
+ 'x86_64-pc-windows-msvc',
35
+ 'x86_64-apple-darwin',
36
+ ],
37
+ },
38
+ }
39
+ await writeFile(packageJson, JSON.stringify(pkgJson, null, 2))
40
+ const config = {
41
+ binaryName: 'testing',
42
+ packageName: '@node-rs/testing',
43
+ targets: [
44
+ 'x86_64-unknown-linux-gnu',
45
+ 'x86_64-apple-darwin',
46
+ 'aarch64-apple-darwin',
47
+ ],
48
+ }
49
+ await writeFile(configPath, JSON.stringify(config, null, 2))
50
+ t.context = { configPath, config, packageJson, pkgJson }
51
+ })
52
+
53
+ test.after(async (t) => {
54
+ await unlink(t.context.configPath)
55
+ await unlink(t.context.packageJson)
56
+ })
57
+
58
+ test('should throw if package.json not found', async (t) => {
59
+ await t.throwsAsync(() => readNapiConfig(NON_EXISTENT_FILE), {
60
+ message: `package.json not found at ${NON_EXISTENT_FILE}`,
61
+ })
62
+ })
63
+
64
+ test('should throw if napi.json not found', async (t) => {
65
+ const { packageJson } = t.context
66
+ await t.throwsAsync(() => readNapiConfig(packageJson, NON_EXISTENT_FILE), {
67
+ message: `NAPI-RS config not found at ${NON_EXISTENT_FILE}`,
68
+ })
69
+ })
70
+
71
+ test('should be able to read config from package.json', async (t) => {
72
+ const { packageJson } = t.context
73
+ const config = await readNapiConfig(packageJson)
74
+ t.snapshot(config)
75
+ })
76
+
77
+ test('should be able to read config from napi.json', async (t) => {
78
+ const { packageJson, configPath } = t.context
79
+ const config = await readNapiConfig(packageJson, configPath)
80
+ t.snapshot(config)
81
+ })
@@ -18,13 +18,13 @@ test('should ident string correctly', (t) => {
18
18
  foo() {
19
19
  a = b
20
20
  }
21
-
21
+
22
22
  bar = () => {
23
-
23
+
24
24
  }
25
25
  boz = 1
26
26
  }
27
-
27
+
28
28
  namespace B {
29
29
  namespace C {
30
30
  type D = A
@@ -43,6 +43,21 @@ test('should process type def correctly', async (t) => {
43
43
  '__fixtures__',
44
44
  'napi_type_def',
45
45
  ),
46
+ true,
47
+ )
48
+
49
+ t.snapshot(dts)
50
+ })
51
+
52
+ test('should process type def with noConstEnum correctly', async (t) => {
53
+ const { dts } = await processTypeDef(
54
+ join(
55
+ fileURLToPath(import.meta.url),
56
+ '../',
57
+ '__fixtures__',
58
+ 'napi_type_def',
59
+ ),
60
+ false,
46
61
  )
47
62
 
48
63
  t.snapshot(dts)
@@ -1,4 +1,4 @@
1
- import { execSync } from 'child_process'
1
+ import { execSync } from 'node:child_process'
2
2
 
3
3
  import { debug } from './log.js'
4
4
 
@@ -1,9 +1,22 @@
1
+ import { underline, yellow } from 'colorette'
1
2
  import { merge, omit } from 'lodash-es'
2
3
 
3
4
  import { fileExists, readFileAsync } from './misc.js'
4
5
  import { DEFAULT_TARGETS, parseTriple, Target } from './target.js'
5
6
 
6
- interface UserNapiConfig {
7
+ export type ValueOfConstArray<T> = T[Exclude<keyof T, keyof Array<any>>]
8
+
9
+ export const SupportedPackageManagers = ['yarn'] as const
10
+ export const SupportedTestFrameworks = ['ava'] as const
11
+
12
+ export type SupportedPackageManager = ValueOfConstArray<
13
+ typeof SupportedPackageManagers
14
+ >
15
+ export type SupportedTestFramework = ValueOfConstArray<
16
+ typeof SupportedTestFrameworks
17
+ >
18
+
19
+ export interface UserNapiConfig {
7
20
  /**
8
21
  * Name of the binary to be generated, default to `index`
9
22
  */
@@ -24,6 +37,47 @@ interface UserNapiConfig {
24
37
  */
25
38
  npmClient?: string
26
39
 
40
+ /**
41
+ * Whether generate const enum for typescript bindings
42
+ */
43
+ constEnum?: boolean
44
+
45
+ /**
46
+ * dts header prepend to the generated dts file
47
+ */
48
+ dtsHeader?: string
49
+
50
+ /**
51
+ * dts header file path to be prepended to the generated dts file
52
+ * if both dtsHeader and dtsHeaderFile are provided, dtsHeaderFile will be used
53
+ */
54
+ dtsHeaderFile?: string
55
+
56
+ /**
57
+ * wasm compilation options
58
+ */
59
+ wasm?: {
60
+ /**
61
+ * https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/Memory
62
+ * @default 4000 pages (256MiB)
63
+ */
64
+ initialMemory?: number
65
+ /**
66
+ * @default 65536 pages (4GiB)
67
+ */
68
+ maximumMemory?: number
69
+
70
+ /**
71
+ * Browser wasm binding configuration
72
+ */
73
+ browser: {
74
+ /**
75
+ * Whether to use fs module in browser
76
+ */
77
+ fs?: boolean
78
+ }
79
+ }
80
+
27
81
  /**
28
82
  * @deprecated binaryName instead
29
83
  */
@@ -41,7 +95,7 @@ interface UserNapiConfig {
41
95
  /**
42
96
  * Whether enable default targets
43
97
  */
44
- default: boolean
98
+ defaults: boolean
45
99
  /**
46
100
  * Additional targets to be compiled for
47
101
  */
@@ -71,22 +125,34 @@ export interface CommonPackageJsonFields {
71
125
  main?: string
72
126
  module?: string
73
127
  types?: string
128
+ browser?: string
74
129
  exports?: any
75
130
 
76
131
  dependencies?: Record<string, string>
77
132
  devDependencies?: Record<string, string>
133
+
134
+ ava?: {
135
+ timeout?: string
136
+ }
78
137
  }
79
138
 
80
139
  export type NapiConfig = Required<
81
140
  Pick<UserNapiConfig, 'binaryName' | 'packageName' | 'npmClient'>
82
- > & {
83
- targets: Target[]
84
- packageJson: CommonPackageJsonFields
85
- }
141
+ > &
142
+ Pick<UserNapiConfig, 'wasm' | 'dtsHeader' | 'dtsHeaderFile'> & {
143
+ targets: Target[]
144
+ packageJson: CommonPackageJsonFields
145
+ }
86
146
 
87
- export async function readNapiConfig(path: string): Promise<NapiConfig> {
147
+ export async function readNapiConfig(
148
+ path: string,
149
+ configPath?: string,
150
+ ): Promise<NapiConfig> {
151
+ if (configPath && !(await fileExists(configPath))) {
152
+ throw new Error(`NAPI-RS config not found at ${configPath}`)
153
+ }
88
154
  if (!(await fileExists(path))) {
89
- throw new Error(`napi-rs config not found at ${path}`)
155
+ throw new Error(`package.json not found at ${path}`)
90
156
  }
91
157
  // May support multiple config sources later on.
92
158
  const content = await readFileAsync(path, 'utf8')
@@ -94,12 +160,34 @@ export async function readNapiConfig(path: string): Promise<NapiConfig> {
94
160
  try {
95
161
  pkgJson = JSON.parse(content) as CommonPackageJsonFields
96
162
  } catch (e) {
97
- throw new Error('Failed to parse napi-rs config', {
163
+ throw new Error(`Failed to parse package.json at ${path}`, {
98
164
  cause: e,
99
165
  })
100
166
  }
101
167
 
168
+ let separatedConfig: UserNapiConfig | undefined
169
+ if (configPath) {
170
+ const configContent = await readFileAsync(configPath, 'utf8')
171
+ try {
172
+ separatedConfig = JSON.parse(configContent) as UserNapiConfig
173
+ } catch (e) {
174
+ throw new Error(`Failed to parse NAPI-RS config at ${configPath}`, {
175
+ cause: e,
176
+ })
177
+ }
178
+ }
179
+
102
180
  const userNapiConfig = pkgJson.napi ?? {}
181
+ if (pkgJson.napi && separatedConfig) {
182
+ const pkgJsonPath = underline(path)
183
+ const configPathUnderline = underline(configPath!)
184
+ console.warn(
185
+ yellow(
186
+ `Both napi field in ${pkgJsonPath} and [NAPI-RS config](${configPathUnderline}) file are found, the NAPI-RS config file will be used.`,
187
+ ),
188
+ )
189
+ Object.assign(userNapiConfig, separatedConfig)
190
+ }
103
191
  const napiConfig: NapiConfig = merge(
104
192
  {
105
193
  binaryName: 'index',
@@ -114,17 +202,31 @@ export async function readNapiConfig(path: string): Promise<NapiConfig> {
114
202
  let targets: string[] = userNapiConfig.targets ?? []
115
203
 
116
204
  // compatible with old config
117
- if (userNapiConfig.package?.name) {
118
- napiConfig.packageName = userNapiConfig.package.name
205
+ if (userNapiConfig?.name) {
206
+ console.warn(
207
+ yellow(
208
+ `[DEPRECATED] napi.name is deprecated, use napi.binaryName instead.`,
209
+ ),
210
+ )
211
+ napiConfig.binaryName = userNapiConfig.name
119
212
  }
120
213
 
121
214
  if (!targets.length) {
122
- if (userNapiConfig.triples?.default) {
215
+ let deprecatedWarned = false
216
+ const warning = yellow(
217
+ `[DEPRECATED] napi.triples is deprecated, use napi.targets instead.`,
218
+ )
219
+ if (userNapiConfig.triples?.defaults) {
220
+ deprecatedWarned = true
221
+ console.warn(warning)
123
222
  targets = targets.concat(DEFAULT_TARGETS)
124
223
  }
125
224
 
126
225
  if (userNapiConfig.triples?.additional?.length) {
127
226
  targets = targets.concat(userNapiConfig.triples.additional)
227
+ if (!deprecatedWarned) {
228
+ console.warn(warning)
229
+ }
128
230
  }
129
231
  }
130
232
 
@@ -1,4 +1,3 @@
1
- export * from './ci.js'
2
1
  export * from './log.js'
3
2
  export * from './misc.js'
4
3
  export * from './target.js'
@@ -1,5 +1,5 @@
1
- import { execSync } from 'child_process'
2
- import fs from 'fs'
1
+ import { execSync } from 'node:child_process'
2
+ import fs from 'node:fs'
3
3
 
4
4
  export type CrateTargetKind =
5
5
  | 'bin'
package/src/utils/misc.ts CHANGED
@@ -1,6 +1,14 @@
1
- import { readFile, writeFile, copyFile, mkdir, unlink, stat, readdir } from 'fs'
2
- import { createRequire } from 'module'
3
- import { promisify } from 'util'
1
+ import {
2
+ readFile,
3
+ writeFile,
4
+ copyFile,
5
+ mkdir,
6
+ unlink,
7
+ stat,
8
+ readdir,
9
+ } from 'node:fs'
10
+ import { createRequire } from 'node:module'
11
+ import { promisify } from 'node:util'
4
12
 
5
13
  import { debug } from './log.js'
6
14
 
@@ -1,7 +1,11 @@
1
- import { execSync } from 'child_process'
1
+ import { execSync } from 'node:child_process'
2
2
 
3
3
  export type Platform = NodeJS.Platform | 'wasm' | 'wasi'
4
4
 
5
+ export const UNIVERSAL_TARGETS = {
6
+ 'universal-apple-darwin': ['aarch64-apple-darwin', 'x86_64-apple-darwin'],
7
+ } as const
8
+
5
9
  export const AVAILABLE_TARGETS = [
6
10
  'aarch64-apple-darwin',
7
11
  'aarch64-linux-android',
@@ -15,15 +19,20 @@ export const AVAILABLE_TARGETS = [
15
19
  'x86_64-unknown-freebsd',
16
20
  'i686-pc-windows-msvc',
17
21
  'armv7-unknown-linux-gnueabihf',
22
+ 'armv7-unknown-linux-musleabihf',
18
23
  'armv7-linux-androideabi',
19
24
  'universal-apple-darwin',
20
25
  'riscv64gc-unknown-linux-gnu',
26
+ 'powerpc64le-unknown-linux-gnu',
27
+ 's390x-unknown-linux-gnu',
28
+ 'wasm32-wasi-preview1-threads',
21
29
  ] as const
22
30
 
23
31
  export type TargetTriple = (typeof AVAILABLE_TARGETS)[number]
24
32
 
25
33
  export const DEFAULT_TARGETS = [
26
34
  'x86_64-apple-darwin',
35
+ 'aarch64-apple-darwin',
27
36
  'x86_64-pc-windows-msvc',
28
37
  'x86_64-unknown-linux-gnu',
29
38
  ] as const
@@ -31,6 +40,8 @@ export const DEFAULT_TARGETS = [
31
40
  export const TARGET_LINKER: Record<string, string> = {
32
41
  'aarch64-unknown-linux-musl': 'aarch64-linux-musl-gcc',
33
42
  'riscv64gc-unknown-linux-gnu': 'riscv64-linux-gnu-gcc',
43
+ 'powerpc64le-unknown-linux-gnu': 'powerpc64le-linux-gnu-gcc',
44
+ 's390x-unknown-linux-gnu': 's390x-linux-gnu-gcc',
34
45
  }
35
46
 
36
47
  // https://nodejs.org/api/process.html#process_process_arch
@@ -48,6 +59,7 @@ type NodeJSArch =
48
59
  | 'x32'
49
60
  | 'x64'
50
61
  | 'universal'
62
+ | 'wasm32'
51
63
 
52
64
  const CpuToNodeArch: Record<string, NodeJSArch> = {
53
65
  x86_64: 'x64',
@@ -55,6 +67,7 @@ const CpuToNodeArch: Record<string, NodeJSArch> = {
55
67
  i686: 'ia32',
56
68
  armv7: 'arm',
57
69
  riscv64gc: 'riscv64',
70
+ powerpc64le: 'ppc64',
58
71
  }
59
72
 
60
73
  export const NodeArchToCpu: Record<string, string> = {
@@ -63,6 +76,7 @@ export const NodeArchToCpu: Record<string, string> = {
63
76
  ia32: 'i686',
64
77
  arm: 'armv7',
65
78
  riscv64: 'riscv64gc',
79
+ ppc64: 'powerpc64le',
66
80
  }
67
81
 
68
82
  const SysToNodePlatform: Record<string, Platform> = {
@@ -80,7 +94,7 @@ export interface Target {
80
94
  triple: string
81
95
  platformArchABI: string
82
96
  platform: Platform
83
- arch: NodeJSArch | 'wasm32'
97
+ arch: NodeJSArch
84
98
  abi: string | null
85
99
  }
86
100
 
@@ -95,10 +109,14 @@ export interface Target {
95
109
  * - `abi` = The ABI, for example `gnu`, `android`, `eabi`, etc.
96
110
  */
97
111
  export function parseTriple(rawTriple: string): Target {
98
- if (rawTriple === 'wasm32-wasi-preview1-threads') {
112
+ if (
113
+ rawTriple === 'wasm32-wasi' ||
114
+ rawTriple === 'wasm32-wasi-preview1-threads' ||
115
+ rawTriple.startsWith('wasm32-wasip')
116
+ ) {
99
117
  return {
100
118
  triple: rawTriple,
101
- platformArchABI: rawTriple,
119
+ platformArchABI: 'wasm32-wasi',
102
120
  platform: 'wasi',
103
121
  arch: 'wasm32',
104
122
  abi: 'wasi',
@@ -10,6 +10,7 @@ export const DEFAULT_TYPE_DEF_HEADER = `/* auto-generated by NAPI-RS */
10
10
  enum TypeDefKind {
11
11
  Const = 'const',
12
12
  Enum = 'enum',
13
+ StringEnum = 'string_enum',
13
14
  Interface = 'interface',
14
15
  Fn = 'fn',
15
16
  Struct = 'struct',
@@ -25,7 +26,12 @@ interface TypeDefLine {
25
26
  js_mod?: string
26
27
  }
27
28
 
28
- function prettyPrint(line: TypeDefLine, ident: number): string {
29
+ function prettyPrint(
30
+ line: TypeDefLine,
31
+ constEnum: boolean,
32
+ ident: number,
33
+ ambient = false,
34
+ ): string {
29
35
  let s = line.js_doc ?? ''
30
36
  switch (line.kind) {
31
37
  case TypeDefKind.Interface:
@@ -33,16 +39,29 @@ function prettyPrint(line: TypeDefLine, ident: number): string {
33
39
  break
34
40
 
35
41
  case TypeDefKind.Enum:
36
- s += `export const enum ${line.name} {\n${line.def}\n}`
42
+ const enumName = constEnum ? 'const enum' : 'enum'
43
+ s += `${exportDeclare(ambient)} ${enumName} ${line.name} {\n${line.def}\n}`
44
+ break
45
+
46
+ case TypeDefKind.StringEnum:
47
+ if (constEnum) {
48
+ s += `${exportDeclare(ambient)} const enum ${line.name} {\n${line.def}\n}`
49
+ } else {
50
+ s += `export type ${line.name} = ${line.def.replaceAll(/.*=/g, '').replaceAll(',', '|')};`
51
+ }
37
52
  break
38
53
 
39
54
  case TypeDefKind.Struct:
40
- s += `export class ${line.name} {\n${line.def}\n}`
55
+ s += `${exportDeclare(ambient)} class ${line.name} {\n${line.def}\n}`
41
56
  if (line.original_name && line.original_name !== line.name) {
42
57
  s += `\nexport type ${line.original_name} = ${line.name}`
43
58
  }
44
59
  break
45
60
 
61
+ case TypeDefKind.Fn:
62
+ s += `${exportDeclare(ambient)} ${line.def}`
63
+ break
64
+
46
65
  default:
47
66
  s += line.def
48
67
  }
@@ -50,25 +69,35 @@ function prettyPrint(line: TypeDefLine, ident: number): string {
50
69
  return correctStringIdent(s, ident)
51
70
  }
52
71
 
72
+ function exportDeclare(ambient: boolean): string {
73
+ if (ambient) {
74
+ return 'export'
75
+ }
76
+
77
+ return 'export declare'
78
+ }
79
+
53
80
  export async function processTypeDef(
54
81
  intermediateTypeFile: string,
82
+ constEnum: boolean,
55
83
  header?: string,
56
84
  ) {
57
85
  const exports: string[] = []
58
86
  const defs = await readIntermediateTypeFile(intermediateTypeFile)
59
87
  const groupedDefs = preprocessTypeDef(defs)
60
88
 
61
- header = header ? header + '\n' : ''
89
+ header = header ?? ''
62
90
  let dts = ''
63
91
 
64
92
  sortBy(Array.from(groupedDefs), ([namespace]) => namespace).forEach(
65
93
  ([namespace, defs]) => {
66
94
  if (namespace === TOP_LEVEL_NAMESPACE) {
67
95
  for (const def of defs) {
68
- dts += prettyPrint(def, 0) + '\n\n'
96
+ dts += prettyPrint(def, constEnum, 0) + '\n\n'
69
97
  switch (def.kind) {
70
98
  case TypeDefKind.Const:
71
99
  case TypeDefKind.Enum:
100
+ case TypeDefKind.StringEnum:
72
101
  case TypeDefKind.Fn:
73
102
  case TypeDefKind.Struct: {
74
103
  exports.push(def.name)
@@ -83,9 +112,9 @@ export async function processTypeDef(
83
112
  }
84
113
  } else {
85
114
  exports.push(namespace)
86
- dts += `export namespace ${namespace} {\n`
115
+ dts += `export declare namespace ${namespace} {\n`
87
116
  for (const def of defs) {
88
- dts += prettyPrint(def, 2) + '\n'
117
+ dts += prettyPrint(def, constEnum, 2, true) + '\n'
89
118
  }
90
119
  dts += '}\n\n'
91
120
  }
@@ -94,7 +123,7 @@ export async function processTypeDef(
94
123
 
95
124
  if (dts.indexOf('ExternalObject<') > -1) {
96
125
  header += `
97
- export class ExternalObject<T> {
126
+ export declare class ExternalObject<T> {
98
127
  readonly '': {
99
128
  readonly '': unique symbol
100
129
  [K: symbol]: T
@@ -7,20 +7,22 @@ export enum NapiVersion {
7
7
  Napi6,
8
8
  Napi7,
9
9
  Napi8,
10
+ Napi9,
10
11
  }
11
12
 
12
13
  /// because node support new napi version in some minor version updates, so we might meet such situation:
13
14
  /// `node v10.20.0` supports `napi5` and `napi6`, but `node v12.0.0` only support `napi4`,
14
- /// by which, we can not tell directly napi version supportness from node version directly.
15
+ /// by which, we can not tell directly napi version supportless from node version directly.
15
16
  const NAPI_VERSION_MATRIX = new Map<NapiVersion, string>([
16
- [NapiVersion.Napi1, '8.6.0'],
17
- [NapiVersion.Napi2, '8.10.0 | 9.3.0'],
18
- [NapiVersion.Napi3, '6.14.2 | 8.11.2 | 9.11.0'],
19
- [NapiVersion.Napi4, '10.16.0 | 11.8.0'],
20
- [NapiVersion.Napi5, '10.17.0 | 12.11.0'],
17
+ [NapiVersion.Napi1, '8.6.0 | 9.0.0 | 10.0.0'],
18
+ [NapiVersion.Napi2, '8.10.0 | 9.3.0 | 10.0.0'],
19
+ [NapiVersion.Napi3, '6.14.2 | 8.11.2 | 9.11.0 | 10.0.0'],
20
+ [NapiVersion.Napi4, '10.16.0 | 11.8.0 | 12.0.0'],
21
+ [NapiVersion.Napi5, '10.17.0 | 12.11.0 | 13.0.0'],
21
22
  [NapiVersion.Napi6, '10.20.0 | 12.17.0 | 14.0.0'],
22
- [NapiVersion.Napi7, '10.23.0 | 12.19.0 | 14.12.0'],
23
- [NapiVersion.Napi8, '12.22.0 | 14.17.0 | 15.12.0'],
23
+ [NapiVersion.Napi7, '10.23.0 | 12.19.0 | 14.12.0 | 15.0.0'],
24
+ [NapiVersion.Napi8, '12.22.0 | 14.17.0 | 15.12.0 | 16.0.0'],
25
+ [NapiVersion.Napi9, '18.17.0 | 20.3.0 | 21.1.0'],
24
26
  ])
25
27
 
26
28
  interface NodeVersion {
@@ -1 +0,0 @@
1
- export declare const createWasiBinding: (localName: string, wasiRegisterFunctions: string[]) => string;
@@ -1,63 +0,0 @@
1
- export const createWasiBinding = (localName, wasiRegisterFunctions) => `/* eslint-disable */
2
- /* prettier-ignore */
3
-
4
- /* auto-generated by NAPI-RS */
5
-
6
- import * as __nodeFsPromises from 'node:fs/promises'
7
- import * as __nodePath from 'node:path'
8
- import { WASI as __nodeWASI } from 'node:wasi'
9
- import { Worker } from 'node:worker_threads'
10
- import * as __nodeURL from 'node:url'
11
-
12
- import { instantiateNapiModule as __emnapiInstantiateNapiModule } from '@emnapi/core'
13
- import { getDefaultContext as __emnapiGetDefaultContext } from '@emnapi/runtime'
14
-
15
- const __wasi = new __nodeWASI({
16
- env: process.env,
17
- preopens: {
18
- '/': __nodePath.join(__nodeURL.fileURLToPath(import.meta.url), '..'),
19
- }
20
- })
21
-
22
- const __dirname = __nodePath.join(__nodeURL.fileURLToPath(import.meta.url), '..')
23
-
24
- const __emnapiContext = __emnapiGetDefaultContext()
25
-
26
- const __sharedMemory = new WebAssembly.Memory({
27
- initial: 1024,
28
- maximum: 10240,
29
- shared: true,
30
- })
31
-
32
- const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = await __emnapiInstantiateNapiModule(__nodeFsPromises.readFile(__nodePath.join(__dirname, '${localName}.wasi-wasm32.wasm')), {
33
- context: __emnapiContext,
34
- asyncWorkPoolSize: 4,
35
- wasi: __wasi,
36
- onCreateWorker() {
37
- return new Worker(__nodePath.join(__dirname, 'wasi-worker.mjs'), {
38
- env: process.env,
39
- execArgv: ['--experimental-wasi-unstable-preview1'],
40
- })
41
- },
42
- overwriteImports(importObject) {
43
- importObject.env = {
44
- ...importObject.env,
45
- ...importObject.napi,
46
- ...importObject.emnapi,
47
- memory: __sharedMemory,
48
- }
49
- },
50
- beforeInit({ instance }) {
51
- __napi_rs_initialize_modules(instance)
52
- }
53
- })
54
-
55
- function __napi_rs_initialize_modules(__napiInstance) {
56
- ${wasiRegisterFunctions
57
- .map((name) => ` __napiInstance.exports['${name}']()`)
58
- .join('\n')}
59
- }
60
-
61
- const binding = __napiModule.exports
62
- `;
63
- //# sourceMappingURL=load-wasi-template.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"load-wasi-template.js","sourceRoot":"","sources":["../../src/api/load-wasi-template.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,SAAiB,EACjB,qBAA+B,EAC/B,EAAE,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iLA+B4K,SAAS;;;;;;;;;;;;;;;;;;;;;;;;EAwBxL,qBAAqB;KACpB,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,6BAA6B,IAAI,MAAM,CAAC;KACtD,IAAI,CAAC,IAAI,CAAC;;;;CAIZ,CAAA"}