@oveo/optimizer 0.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Boris Kaul <localvoid@gmail.com>.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # `@napi-rs/package-template`
2
+
3
+ ![https://github.com/napi-rs/package-template/actions](https://github.com/napi-rs/package-template/workflows/CI/badge.svg)
4
+
5
+ > Template project for writing node packages with napi-rs.
6
+
7
+ # Usage
8
+
9
+ 1. Click **Use this template**.
10
+ 2. **Clone** your project.
11
+ 3. Run `yarn install` to install dependencies.
12
+ 4. Run `npx napi rename -n [name]` command under the project folder to rename your package.
13
+
14
+ ## Install this test package
15
+
16
+ ```
17
+ yarn add @napi-rs/package-template
18
+ ```
19
+
20
+ ## Support matrix
21
+
22
+
23
+ ## Ability
24
+
25
+ ### Build
26
+
27
+ After `yarn build/npm run build` command, you can see `package-template.[darwin|win32|linux].node` file in project root. This is the native addon built from [lib.rs](./src/lib.rs).
28
+
29
+ ### Test
30
+
31
+ With [ava](https://github.com/avajs/ava), run `yarn test/npm run test` to testing native addon. You can also switch to another testing framework if you want.
32
+
33
+ ### CI
34
+
35
+ With GitHub Actions, each commit and pull request will be built and tested automatically in [`node@14`, `node@16`, `@node18`] x [`macOS`, `Linux`, `Windows`] matrix. You will never be afraid of the native addon broken in these platforms.
36
+
37
+ ### Release
38
+
39
+ Release native package is very difficult in old days. Native packages may ask developers who use it to install `build toolchain` like `gcc/llvm`, `node-gyp` or something more.
40
+
41
+ With `GitHub actions`, we can easily prebuild a `binary` for major platforms. And with `N-API`, we should never be afraid of **ABI Compatible**.
42
+
43
+ The other problem is how to deliver prebuild `binary` to users. Downloading it in `postinstall` script is a common way that most packages do it right now. The problem with this solution is it introduced many other packages to download binary that has not been used by `runtime codes`. The other problem is some users may not easily download the binary from `GitHub/CDN` if they are behind a private network (But in most cases, they have a private NPM mirror).
44
+
45
+ In this package, we choose a better way to solve this problem. We release different `npm packages` for different platforms. And add it to `optionalDependencies` before releasing the `Major` package to npm.
46
+
47
+ `NPM` will choose which native package should download from `registry` automatically. You can see [npm](./npm) dir for details. And you can also run `yarn add @napi-rs/package-template` to see how it works.
48
+
49
+ ## Develop requirements
50
+
51
+ - Install the latest `Rust`
52
+ - Install `Node.js@10+` which fully supported `Node-API`
53
+ - Install `yarn@1.x`
54
+
55
+ ## Test in local
56
+
57
+ - yarn
58
+ - yarn build
59
+ - yarn test
60
+
61
+ And you will see:
62
+
63
+ ```bash
64
+ $ ava --verbose
65
+
66
+ ✔ sync function from native code
67
+ ✔ sleep function from native code (201ms)
68
+
69
+
70
+ 2 tests passed
71
+ ✨ Done in 1.12s.
72
+ ```
73
+
74
+ ## Release package
75
+
76
+ Ensure you have set your **NPM_TOKEN** in the `GitHub` project setting.
77
+
78
+ In `Settings -> Secrets`, add **NPM_TOKEN** into it.
79
+
80
+ When you want to release the package:
81
+
82
+ ```
83
+ npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease [--preid=<prerelease-id>] | from-git]
84
+
85
+ git push
86
+ ```
87
+
88
+ GitHub actions will do the rest job for you.
package/index.d.ts ADDED
@@ -0,0 +1,23 @@
1
+ /* auto-generated by NAPI-RS */
2
+ /* eslint-disable */
3
+ export declare class Optimizer {
4
+ constructor(options?: OptimizerOptions | undefined | null)
5
+ importExterns(data: Uint8Array): void
6
+ importPropertyMap(data: Uint8Array): void
7
+ optimizeModule(sourceText: string): Promise<OptimizerOutput>
8
+ optimizeChunk(sourceText: string): Promise<OptimizerOutput>
9
+ }
10
+
11
+ export declare class OptimizerOutput {
12
+ code: string
13
+ map: string
14
+ }
15
+
16
+ export interface OptimizerOptions {
17
+ hoist?: boolean
18
+ dedupe?: boolean
19
+ hoistGlobals?: boolean
20
+ inlineExternValues?: boolean
21
+ singletons?: boolean
22
+ renameProperties?: boolean
23
+ }
package/index.js ADDED
@@ -0,0 +1,398 @@
1
+ // prettier-ignore
2
+ /* eslint-disable */
3
+ // @ts-nocheck
4
+ /* auto-generated by NAPI-RS */
5
+
6
+ import { createRequire } from 'node:module'
7
+ const require = createRequire(import.meta.url)
8
+ const __dirname = new URL('.', import.meta.url).pathname
9
+
10
+ const { readFileSync } = require('node:fs')
11
+ let nativeBinding = null
12
+ const loadErrors = []
13
+
14
+ const isMusl = () => {
15
+ let musl = false
16
+ if (process.platform === 'linux') {
17
+ musl = isMuslFromFilesystem()
18
+ if (musl === null) {
19
+ musl = isMuslFromReport()
20
+ }
21
+ if (musl === null) {
22
+ musl = isMuslFromChildProcess()
23
+ }
24
+ }
25
+ return musl
26
+ }
27
+
28
+ const isFileMusl = (f) => f.includes('libc.musl-') || f.includes('ld-musl-')
29
+
30
+ const isMuslFromFilesystem = () => {
31
+ try {
32
+ return readFileSync('/usr/bin/ldd', 'utf-8').includes('musl')
33
+ } catch {
34
+ return null
35
+ }
36
+ }
37
+
38
+ const isMuslFromReport = () => {
39
+ let report = null
40
+ if (typeof process.report?.getReport === 'function') {
41
+ process.report.excludeNetwork = true
42
+ report = process.report.getReport()
43
+ }
44
+ if (!report) {
45
+ return null
46
+ }
47
+ if (report.header && report.header.glibcVersionRuntime) {
48
+ return false
49
+ }
50
+ if (Array.isArray(report.sharedObjects)) {
51
+ if (report.sharedObjects.some(isFileMusl)) {
52
+ return true
53
+ }
54
+ }
55
+ return false
56
+ }
57
+
58
+ const isMuslFromChildProcess = () => {
59
+ try {
60
+ return require('child_process').execSync('ldd --version', { encoding: 'utf8' }).includes('musl')
61
+ } catch (e) {
62
+ // If we reach this case, we don't know if the system is musl or not, so is better to just fallback to false
63
+ return false
64
+ }
65
+ }
66
+
67
+ function requireNative() {
68
+ if (process.env.NAPI_RS_NATIVE_LIBRARY_PATH) {
69
+ try {
70
+ nativeBinding = require(process.env.NAPI_RS_NATIVE_LIBRARY_PATH);
71
+ } catch (err) {
72
+ loadErrors.push(err)
73
+ }
74
+ } else if (process.platform === 'android') {
75
+ if (process.arch === 'arm64') {
76
+ try {
77
+ return require('./oveo-optimizer.android-arm64.node')
78
+ } catch (e) {
79
+ loadErrors.push(e)
80
+ }
81
+ try {
82
+ return require('@oveo/optimizer-android-arm64')
83
+ } catch (e) {
84
+ loadErrors.push(e)
85
+ }
86
+ } else if (process.arch === 'arm') {
87
+ try {
88
+ return require('./oveo-optimizer.android-arm-eabi.node')
89
+ } catch (e) {
90
+ loadErrors.push(e)
91
+ }
92
+ try {
93
+ return require('@oveo/optimizer-android-arm-eabi')
94
+ } catch (e) {
95
+ loadErrors.push(e)
96
+ }
97
+ } else {
98
+ loadErrors.push(new Error(`Unsupported architecture on Android ${process.arch}`))
99
+ }
100
+ } else if (process.platform === 'win32') {
101
+ if (process.arch === 'x64') {
102
+ try {
103
+ return require('./oveo-optimizer.win32-x64-msvc.node')
104
+ } catch (e) {
105
+ loadErrors.push(e)
106
+ }
107
+ try {
108
+ return require('@oveo/optimizer-win32-x64-msvc')
109
+ } catch (e) {
110
+ loadErrors.push(e)
111
+ }
112
+ } else if (process.arch === 'ia32') {
113
+ try {
114
+ return require('./oveo-optimizer.win32-ia32-msvc.node')
115
+ } catch (e) {
116
+ loadErrors.push(e)
117
+ }
118
+ try {
119
+ return require('@oveo/optimizer-win32-ia32-msvc')
120
+ } catch (e) {
121
+ loadErrors.push(e)
122
+ }
123
+ } else if (process.arch === 'arm64') {
124
+ try {
125
+ return require('./oveo-optimizer.win32-arm64-msvc.node')
126
+ } catch (e) {
127
+ loadErrors.push(e)
128
+ }
129
+ try {
130
+ return require('@oveo/optimizer-win32-arm64-msvc')
131
+ } catch (e) {
132
+ loadErrors.push(e)
133
+ }
134
+ } else {
135
+ loadErrors.push(new Error(`Unsupported architecture on Windows: ${process.arch}`))
136
+ }
137
+ } else if (process.platform === 'darwin') {
138
+ try {
139
+ return require('./oveo-optimizer.darwin-universal.node')
140
+ } catch (e) {
141
+ loadErrors.push(e)
142
+ }
143
+ try {
144
+ return require('@oveo/optimizer-darwin-universal')
145
+ } catch (e) {
146
+ loadErrors.push(e)
147
+ }
148
+ if (process.arch === 'x64') {
149
+ try {
150
+ return require('./oveo-optimizer.darwin-x64.node')
151
+ } catch (e) {
152
+ loadErrors.push(e)
153
+ }
154
+ try {
155
+ return require('@oveo/optimizer-darwin-x64')
156
+ } catch (e) {
157
+ loadErrors.push(e)
158
+ }
159
+ } else if (process.arch === 'arm64') {
160
+ try {
161
+ return require('./oveo-optimizer.darwin-arm64.node')
162
+ } catch (e) {
163
+ loadErrors.push(e)
164
+ }
165
+ try {
166
+ return require('@oveo/optimizer-darwin-arm64')
167
+ } catch (e) {
168
+ loadErrors.push(e)
169
+ }
170
+ } else {
171
+ loadErrors.push(new Error(`Unsupported architecture on macOS: ${process.arch}`))
172
+ }
173
+ } else if (process.platform === 'freebsd') {
174
+ if (process.arch === 'x64') {
175
+ try {
176
+ return require('./oveo-optimizer.freebsd-x64.node')
177
+ } catch (e) {
178
+ loadErrors.push(e)
179
+ }
180
+ try {
181
+ return require('@oveo/optimizer-freebsd-x64')
182
+ } catch (e) {
183
+ loadErrors.push(e)
184
+ }
185
+ } else if (process.arch === 'arm64') {
186
+ try {
187
+ return require('./oveo-optimizer.freebsd-arm64.node')
188
+ } catch (e) {
189
+ loadErrors.push(e)
190
+ }
191
+ try {
192
+ return require('@oveo/optimizer-freebsd-arm64')
193
+ } catch (e) {
194
+ loadErrors.push(e)
195
+ }
196
+ } else {
197
+ loadErrors.push(new Error(`Unsupported architecture on FreeBSD: ${process.arch}`))
198
+ }
199
+ } else if (process.platform === 'linux') {
200
+ if (process.arch === 'x64') {
201
+ if (isMusl()) {
202
+ try {
203
+ return require('./oveo-optimizer.linux-x64-musl.node')
204
+ } catch (e) {
205
+ loadErrors.push(e)
206
+ }
207
+ try {
208
+ return require('@oveo/optimizer-linux-x64-musl')
209
+ } catch (e) {
210
+ loadErrors.push(e)
211
+ }
212
+ } else {
213
+ try {
214
+ return require('./oveo-optimizer.linux-x64-gnu.node')
215
+ } catch (e) {
216
+ loadErrors.push(e)
217
+ }
218
+ try {
219
+ return require('@oveo/optimizer-linux-x64-gnu')
220
+ } catch (e) {
221
+ loadErrors.push(e)
222
+ }
223
+ }
224
+ } else if (process.arch === 'arm64') {
225
+ if (isMusl()) {
226
+ try {
227
+ return require('./oveo-optimizer.linux-arm64-musl.node')
228
+ } catch (e) {
229
+ loadErrors.push(e)
230
+ }
231
+ try {
232
+ return require('@oveo/optimizer-linux-arm64-musl')
233
+ } catch (e) {
234
+ loadErrors.push(e)
235
+ }
236
+ } else {
237
+ try {
238
+ return require('./oveo-optimizer.linux-arm64-gnu.node')
239
+ } catch (e) {
240
+ loadErrors.push(e)
241
+ }
242
+ try {
243
+ return require('@oveo/optimizer-linux-arm64-gnu')
244
+ } catch (e) {
245
+ loadErrors.push(e)
246
+ }
247
+ }
248
+ } else if (process.arch === 'arm') {
249
+ if (isMusl()) {
250
+ try {
251
+ return require('./oveo-optimizer.linux-arm-musleabihf.node')
252
+ } catch (e) {
253
+ loadErrors.push(e)
254
+ }
255
+ try {
256
+ return require('@oveo/optimizer-linux-arm-musleabihf')
257
+ } catch (e) {
258
+ loadErrors.push(e)
259
+ }
260
+ } else {
261
+ try {
262
+ return require('./oveo-optimizer.linux-arm-gnueabihf.node')
263
+ } catch (e) {
264
+ loadErrors.push(e)
265
+ }
266
+ try {
267
+ return require('@oveo/optimizer-linux-arm-gnueabihf')
268
+ } catch (e) {
269
+ loadErrors.push(e)
270
+ }
271
+ }
272
+ } else if (process.arch === 'riscv64') {
273
+ if (isMusl()) {
274
+ try {
275
+ return require('./oveo-optimizer.linux-riscv64-musl.node')
276
+ } catch (e) {
277
+ loadErrors.push(e)
278
+ }
279
+ try {
280
+ return require('@oveo/optimizer-linux-riscv64-musl')
281
+ } catch (e) {
282
+ loadErrors.push(e)
283
+ }
284
+ } else {
285
+ try {
286
+ return require('./oveo-optimizer.linux-riscv64-gnu.node')
287
+ } catch (e) {
288
+ loadErrors.push(e)
289
+ }
290
+ try {
291
+ return require('@oveo/optimizer-linux-riscv64-gnu')
292
+ } catch (e) {
293
+ loadErrors.push(e)
294
+ }
295
+ }
296
+ } else if (process.arch === 'ppc64') {
297
+ try {
298
+ return require('./oveo-optimizer.linux-ppc64-gnu.node')
299
+ } catch (e) {
300
+ loadErrors.push(e)
301
+ }
302
+ try {
303
+ return require('@oveo/optimizer-linux-ppc64-gnu')
304
+ } catch (e) {
305
+ loadErrors.push(e)
306
+ }
307
+ } else if (process.arch === 's390x') {
308
+ try {
309
+ return require('./oveo-optimizer.linux-s390x-gnu.node')
310
+ } catch (e) {
311
+ loadErrors.push(e)
312
+ }
313
+ try {
314
+ return require('@oveo/optimizer-linux-s390x-gnu')
315
+ } catch (e) {
316
+ loadErrors.push(e)
317
+ }
318
+ } else {
319
+ loadErrors.push(new Error(`Unsupported architecture on Linux: ${process.arch}`))
320
+ }
321
+ } else if (process.platform === 'openharmony') {
322
+ if (process.arch === 'arm64') {
323
+ try {
324
+ return require('./oveo-optimizer.linux-arm64-ohos.node')
325
+ } catch (e) {
326
+ loadErrors.push(e)
327
+ }
328
+ try {
329
+ return require('@oveo/optimizer-linux-arm64-ohos')
330
+ } catch (e) {
331
+ loadErrors.push(e)
332
+ }
333
+ } else if (process.arch === 'x64') {
334
+ try {
335
+ return require('./oveo-optimizer.linux-x64-ohos.node')
336
+ } catch (e) {
337
+ loadErrors.push(e)
338
+ }
339
+ try {
340
+ return require('@oveo/optimizer-linux-x64-ohos')
341
+ } catch (e) {
342
+ loadErrors.push(e)
343
+ }
344
+ } else if (process.arch === 'arm') {
345
+ try {
346
+ return require('./oveo-optimizer.linux-arm-ohos.node')
347
+ } catch (e) {
348
+ loadErrors.push(e)
349
+ }
350
+ try {
351
+ return require('@oveo/optimizer-linux-arm-ohos')
352
+ } catch (e) {
353
+ loadErrors.push(e)
354
+ }
355
+ } else {
356
+ loadErrors.push(new Error(`Unsupported architecture on OpenHarmony: ${process.arch}`))
357
+ }
358
+ } else {
359
+ loadErrors.push(new Error(`Unsupported OS: ${process.platform}, architecture: ${process.arch}`))
360
+ }
361
+ }
362
+
363
+ nativeBinding = requireNative()
364
+
365
+ if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) {
366
+ try {
367
+ nativeBinding = require('./oveo-optimizer.wasi.cjs')
368
+ } catch (err) {
369
+ if (process.env.NAPI_RS_FORCE_WASI) {
370
+ loadErrors.push(err)
371
+ }
372
+ }
373
+ if (!nativeBinding) {
374
+ try {
375
+ nativeBinding = require('@oveo/optimizer-wasm32-wasi')
376
+ } catch (err) {
377
+ if (process.env.NAPI_RS_FORCE_WASI) {
378
+ loadErrors.push(err)
379
+ }
380
+ }
381
+ }
382
+ }
383
+
384
+ if (!nativeBinding) {
385
+ if (loadErrors.length > 0) {
386
+ throw new Error(
387
+ `Cannot find native binding. ` +
388
+ `npm has a bug related to optional dependencies (https://github.com/npm/cli/issues/4828). ` +
389
+ 'Please try `npm i` again after removing both package-lock.json and node_modules directory.',
390
+ { cause: loadErrors }
391
+ )
392
+ }
393
+ throw new Error(`Failed to load native binding`)
394
+ }
395
+
396
+ const { Optimizer, OptimizerOutput } = nativeBinding
397
+ export { Optimizer }
398
+ export { OptimizerOutput }
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@oveo/optimizer",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "sideEffects": false,
6
+ "exports": {
7
+ ".": "./index.js"
8
+ },
9
+ "files": [
10
+ "index.d.ts",
11
+ "index.js"
12
+ ],
13
+ "optionalDependencies": {
14
+ "@oveo/optimizer-darwin-arm64": "0.0.1",
15
+ "@oveo/optimizer-darwin-x64": "0.0.1",
16
+ "@oveo/optimizer-linux-arm64-gnu": "0.0.1",
17
+ "@oveo/optimizer-linux-x64-gnu": "0.0.1",
18
+ "@oveo/optimizer-win32-arm64-msvc": "0.0.1",
19
+ "@oveo/optimizer-win32-x64-msvc": "0.0.1"
20
+ },
21
+ "napi": {
22
+ "binaryName": "oveo-optimizer",
23
+ "targets": [
24
+ "x86_64-pc-windows-msvc",
25
+ "x86_64-apple-darwin",
26
+ "x86_64-unknown-linux-gnu",
27
+ "aarch64-unknown-linux-gnu",
28
+ "aarch64-apple-darwin",
29
+ "aarch64-pc-windows-msvc"
30
+ ]
31
+ },
32
+ "engines": {
33
+ "node": ">= 20.0.0"
34
+ },
35
+ "scripts": {
36
+ "clean": "rm -rf oveo-optimizer-*.node"
37
+ },
38
+ "description": "oveo optimizer NAPI",
39
+ "license": "MIT",
40
+ "keywords": [
41
+ "oveo"
42
+ ],
43
+ "author": "Boris Kaul <localvoid@gmail.com> (https://github.com/localvoid)",
44
+ "homepage": "https://github.com/localvoid/oveo",
45
+ "bugs": "https://github.com/localvoid/oveo/issues",
46
+ "repository": {
47
+ "type": "git",
48
+ "url": "git+https://github.com/localvoid/oveo.git"
49
+ }
50
+ }