@frdt/frdt 0.2.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/LICENSE +21 -0
- package/README.md +119 -0
- package/THIRD-PARTY-NOTICES.md +4952 -0
- package/frdt.wasi.cjs +2144 -0
- package/frdt.wasm32-wasi.wasm +0 -0
- package/index.d.ts +129 -0
- package/index.js +785 -0
- package/package.json +39 -0
- package/wasi-worker.mjs +186 -0
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@frdt/frdt",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Read and edit Resonite .brson (FrDT) files without expanding them into JSON",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"resonite",
|
|
8
|
+
"brson",
|
|
9
|
+
"bson",
|
|
10
|
+
"frdt",
|
|
11
|
+
"napi-rs"
|
|
12
|
+
],
|
|
13
|
+
"main": "index.js",
|
|
14
|
+
"types": "index.d.ts",
|
|
15
|
+
"files": [
|
|
16
|
+
"index.js",
|
|
17
|
+
"index.d.ts",
|
|
18
|
+
"frdt.wasi.cjs",
|
|
19
|
+
"frdt.wasm32-wasi.wasm",
|
|
20
|
+
"wasi-worker.mjs",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE",
|
|
23
|
+
"THIRD-PARTY-NOTICES.md"
|
|
24
|
+
],
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": "^20.19.0 || ^22.13.0 || >=23.5.0"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@emnapi/core": "2.0.0-alpha.5",
|
|
30
|
+
"@emnapi/runtime": "2.0.0-alpha.5",
|
|
31
|
+
"@napi-rs/wasm-runtime": "^1.2.4"
|
|
32
|
+
},
|
|
33
|
+
"optionalDependencies": {
|
|
34
|
+
"@frdt/binding-win32-x64-msvc": "0.2.0",
|
|
35
|
+
"@frdt/binding-linux-x64-gnu": "0.2.0",
|
|
36
|
+
"@frdt/binding-linux-x64-musl": "0.2.0",
|
|
37
|
+
"@frdt/binding-darwin-arm64": "0.2.0"
|
|
38
|
+
}
|
|
39
|
+
}
|
package/wasi-worker.mjs
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
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, threadId, Worker, workerData } from 'node:worker_threads'
|
|
6
|
+
|
|
7
|
+
const require = createRequire(import.meta.url)
|
|
8
|
+
|
|
9
|
+
// The thread spawn that created this worker has already returned, so the loader
|
|
10
|
+
// thread may be waiting on this thread. A failure before the crash hook below is
|
|
11
|
+
// in place (a missing '@napi-rs/wasm-runtime', say) has to raise the crash flags
|
|
12
|
+
// too, then fail the worker as before.
|
|
13
|
+
let handler
|
|
14
|
+
try {
|
|
15
|
+
const {
|
|
16
|
+
instantiateNapiModuleSync,
|
|
17
|
+
MessageHandler,
|
|
18
|
+
getDefaultContext,
|
|
19
|
+
emnapiAsyncWorkPlugin,
|
|
20
|
+
emnapiTSFNPlugin,
|
|
21
|
+
} = require('@napi-rs/wasm-runtime')
|
|
22
|
+
|
|
23
|
+
if (parentPort) {
|
|
24
|
+
parentPort.on('message', (data) => {
|
|
25
|
+
globalThis.onmessage({ data })
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
Object.assign(globalThis, {
|
|
30
|
+
self: globalThis,
|
|
31
|
+
require,
|
|
32
|
+
Worker,
|
|
33
|
+
importScripts: function (f) {
|
|
34
|
+
// oxlint-disable-next-line no-eval -- WASI importScripts polyfill
|
|
35
|
+
;(0, eval)(fs.readFileSync(f, 'utf8') + '//# sourceURL=' + f)
|
|
36
|
+
},
|
|
37
|
+
postMessage: function (msg) {
|
|
38
|
+
if (parentPort) {
|
|
39
|
+
parentPort.postMessage(msg)
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
const emnapiContext = getDefaultContext()
|
|
45
|
+
|
|
46
|
+
const __cwd = process.cwd()
|
|
47
|
+
const __rootDir =
|
|
48
|
+
(workerData && typeof workerData.rootDir === 'string' && workerData.rootDir) ||
|
|
49
|
+
parse(__cwd).root
|
|
50
|
+
const __hostRoot =
|
|
51
|
+
(workerData && typeof workerData.hostRoot === 'string' && workerData.hostRoot) ||
|
|
52
|
+
(process.platform === 'android' ? __cwd : __rootDir)
|
|
53
|
+
|
|
54
|
+
handler = new MessageHandler({
|
|
55
|
+
onLoad({ wasmModule, wasmMemory }) {
|
|
56
|
+
const wasi = new WASI({
|
|
57
|
+
version: 'preview1',
|
|
58
|
+
env: process.env,
|
|
59
|
+
preopens: {
|
|
60
|
+
[__rootDir]: __hostRoot,
|
|
61
|
+
[__hostRoot]: __hostRoot,
|
|
62
|
+
},
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
return instantiateNapiModuleSync(wasmModule, {
|
|
66
|
+
childThread: true,
|
|
67
|
+
wasi,
|
|
68
|
+
context: emnapiContext,
|
|
69
|
+
// The wasm links a "basic" emnapi archive (no C async-work /
|
|
70
|
+
// threadsafe-function implementations), so every thread that
|
|
71
|
+
// instantiates it must provide the JavaScript implementations
|
|
72
|
+
// through the emnapi plugins.
|
|
73
|
+
plugins: [emnapiAsyncWorkPlugin, emnapiTSFNPlugin],
|
|
74
|
+
overwriteImports(importObject) {
|
|
75
|
+
importObject.env = {
|
|
76
|
+
...importObject.env,
|
|
77
|
+
...importObject.napi,
|
|
78
|
+
...importObject.emnapi,
|
|
79
|
+
memory: wasmMemory,
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
})
|
|
83
|
+
},
|
|
84
|
+
})
|
|
85
|
+
} catch (error) {
|
|
86
|
+
if (workerData && workerData.crashFlag instanceof Int32Array) {
|
|
87
|
+
__raiseWasiThreadCrashFlags(error)
|
|
88
|
+
}
|
|
89
|
+
throw error
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Tell the loader thread synchronously that this wasm thread died, so its 'exit'
|
|
93
|
+
// teardown does not re-enter wasm and wait on this thread forever. emnapi calls
|
|
94
|
+
// beforeReportError (which runs emnapi_thread_crashed, so the main thread may
|
|
95
|
+
// throw 'unwind' and start exiting) before the 'error' event is even posted.
|
|
96
|
+
// A loader that predates the flag passes none.
|
|
97
|
+
if (workerData && workerData.crashFlag instanceof Int32Array) {
|
|
98
|
+
const __beforeReportError = handler.beforeReportError
|
|
99
|
+
handler.beforeReportError = function (...args) {
|
|
100
|
+
if (!__raiseWasiThreadCrashFlags(args[0])) {
|
|
101
|
+
// No view (the thread was spawned before the loader read the address,
|
|
102
|
+
// say): the export stores into the same word, but only a worker that
|
|
103
|
+
// loaded has an instance to call it on.
|
|
104
|
+
try {
|
|
105
|
+
const __napiThreadCrashed = this.instance?.exports?.napi_wasm_thread_crashed
|
|
106
|
+
if (typeof __napiThreadCrashed === 'function') {
|
|
107
|
+
__napiThreadCrashed()
|
|
108
|
+
}
|
|
109
|
+
} catch {}
|
|
110
|
+
}
|
|
111
|
+
return __beforeReportError.apply(this, args)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// The error goes into the shared crash report first. The 'error' event that
|
|
116
|
+
// would carry it is dropped when the loader terminates this worker before the
|
|
117
|
+
// event is sent, so the report is the only copy the loader can count on.
|
|
118
|
+
// Layout: three Int32 words — state (0 empty, 1 writing, 2 written), byte
|
|
119
|
+
// length, threadId — then the UTF-8 JSON of { name, message, stack }. Only the
|
|
120
|
+
// first worker to crash writes it, and it is complete before the flag is raised.
|
|
121
|
+
//
|
|
122
|
+
// Then the loader's flag, then the addon's. The loader thread may already be
|
|
123
|
+
// inside wasm, in a cleanup call that waits on this thread; the addon's flag is
|
|
124
|
+
// what those waits check between short slices. It is a word in the shared wasm
|
|
125
|
+
// memory (the loader passes a view of it, see napi_wasm_thread_crash_flag_address),
|
|
126
|
+
// so no instance is needed: a worker that failed while loading raises it too.
|
|
127
|
+
// Once it is up the waits trap, and the loader, whose flag is already up, turns
|
|
128
|
+
// the throw into its crash rejection. Returns whether the addon's flag was raised.
|
|
129
|
+
function __raiseWasiThreadCrashFlags(error) {
|
|
130
|
+
try {
|
|
131
|
+
__writeCrashReport(workerData.crashReport, error)
|
|
132
|
+
} catch {}
|
|
133
|
+
try {
|
|
134
|
+
Atomics.store(workerData.crashFlag, 0, 1)
|
|
135
|
+
} catch {}
|
|
136
|
+
const addonCrashFlag = workerData.addonCrashFlag
|
|
137
|
+
if (!(addonCrashFlag instanceof Int32Array)) {
|
|
138
|
+
return false
|
|
139
|
+
}
|
|
140
|
+
try {
|
|
141
|
+
Atomics.store(addonCrashFlag, 0, 1)
|
|
142
|
+
return true
|
|
143
|
+
} catch {
|
|
144
|
+
return false
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function __writeCrashReport(report, error) {
|
|
149
|
+
if (!(report instanceof SharedArrayBuffer) || report.byteLength <= 12) {
|
|
150
|
+
return
|
|
151
|
+
}
|
|
152
|
+
const header = new Int32Array(report, 0, 3)
|
|
153
|
+
if (Atomics.compareExchange(header, 0, 0, 1) !== 0) {
|
|
154
|
+
return
|
|
155
|
+
}
|
|
156
|
+
let length = 0
|
|
157
|
+
try {
|
|
158
|
+
const body = new Uint8Array(report, 12)
|
|
159
|
+
const isObject =
|
|
160
|
+
error !== null && (typeof error === 'object' || typeof error === 'function')
|
|
161
|
+
const name = isObject && typeof error.name === 'string' ? error.name : 'Error'
|
|
162
|
+
const message = isObject && typeof error.message === 'string'
|
|
163
|
+
? error.message
|
|
164
|
+
: String(error)
|
|
165
|
+
const stack = isObject && typeof error.stack === 'string' ? error.stack : undefined
|
|
166
|
+
const encoder = new TextEncoder()
|
|
167
|
+
let bytes = encoder.encode(JSON.stringify({ name, message, stack }))
|
|
168
|
+
if (bytes.length > body.length) {
|
|
169
|
+
bytes = encoder.encode(
|
|
170
|
+
JSON.stringify({ name, message: message.slice(0, body.length >> 3) }),
|
|
171
|
+
)
|
|
172
|
+
}
|
|
173
|
+
if (bytes.length <= body.length) {
|
|
174
|
+
body.set(bytes)
|
|
175
|
+
length = bytes.length
|
|
176
|
+
}
|
|
177
|
+
Atomics.store(header, 2, threadId)
|
|
178
|
+
} finally {
|
|
179
|
+
Atomics.store(header, 1, length)
|
|
180
|
+
Atomics.store(header, 0, 2)
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
globalThis.onmessage = function (e) {
|
|
185
|
+
handler.handle(e)
|
|
186
|
+
}
|