@desert-ant-labs/gist 0.1.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.md +1 -0
- package/channel.js +37 -0
- package/dist/GistWeb.wasm +0 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +25 -0
- package/dist/instantiate.d.ts +107 -0
- package/dist/instantiate.js +147 -0
- package/dist/platforms/browser.d.ts +10 -0
- package/dist/platforms/browser.js +31 -0
- package/dist/platforms/browser.worker.js +18 -0
- package/dist/platforms/node.d.ts +11 -0
- package/dist/platforms/node.js +81 -0
- package/dist/runtime.d.ts +218 -0
- package/dist/runtime.js +981 -0
- package/index.d.ts +77 -0
- package/index.js +179 -0
- package/model/gist.tflite +0 -0
- package/model/gist_config.json +36 -0
- package/model/gist_embedding.i8 +0 -0
- package/model/gist_embedding.json +1 -0
- package/model/gist_tokenizer.bin +0 -0
- package/model/taxonomy.json +187 -0
- package/package.json +42 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Desert Ant Labs Source-Available License 1.0 - https://license.desertant.ai/1.0
|
package/channel.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Channel roll-up: aggregate a channel's per-post topic scores into a ranked list
|
|
2
|
+
// of channel-level topics. Pure and deterministic — no model. Mirrors the Swift
|
|
3
|
+
// `channelTopics` and the reference in the training repo.
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @param {{ topics: Record<string, number>, timestamp?: number | Date }[]} posts
|
|
7
|
+
* @param {{ topN?: number, floor?: number, minPosts?: number, halfLifeDays?: number, touch?: number, now?: number }} [options]
|
|
8
|
+
* @returns {{ slug: string, share: number, postCount: number }[]}
|
|
9
|
+
*/
|
|
10
|
+
export function channelTopics(posts, options = {}) {
|
|
11
|
+
const { topN = 5, floor = 0.05, minPosts = 3, halfLifeDays = 0, touch = 0.15, now = Date.now() } = options;
|
|
12
|
+
if (posts.length < minPosts) return [];
|
|
13
|
+
|
|
14
|
+
const weight = new Map();
|
|
15
|
+
const count = new Map();
|
|
16
|
+
const decay = halfLifeDays > 0 ? Math.LN2 / (halfLifeDays * 86_400_000) : 0;
|
|
17
|
+
|
|
18
|
+
for (const post of posts) {
|
|
19
|
+
let w = 1;
|
|
20
|
+
const t = post.timestamp instanceof Date ? post.timestamp.getTime() : post.timestamp;
|
|
21
|
+
if (decay > 0 && t !== undefined) w = Math.exp(-decay * Math.max(0, now - t));
|
|
22
|
+
for (const [slug, prob] of Object.entries(post.topics)) {
|
|
23
|
+
if (!(prob > 0)) continue;
|
|
24
|
+
weight.set(slug, (weight.get(slug) ?? 0) + prob * w);
|
|
25
|
+
if (prob >= touch) count.set(slug, (count.get(slug) ?? 0) + 1);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const total = [...weight.values()].reduce((a, b) => a + b, 0);
|
|
30
|
+
if (total <= 0) return [];
|
|
31
|
+
|
|
32
|
+
return [...weight.entries()]
|
|
33
|
+
.map(([slug, mass]) => ({ slug, share: mass / total, postCount: count.get(slug) ?? 0 }))
|
|
34
|
+
.filter((t) => t.share >= floor)
|
|
35
|
+
.sort((a, b) => (b.share !== a.share ? b.share - a.share : a.slug.localeCompare(b.slug)))
|
|
36
|
+
.slice(0, topN);
|
|
37
|
+
}
|
|
Binary file
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Exports, Imports, ModuleSource } from './instantiate.js'
|
|
2
|
+
|
|
3
|
+
export type Options = {
|
|
4
|
+
/**
|
|
5
|
+
* The WebAssembly module to instantiate
|
|
6
|
+
*
|
|
7
|
+
* If not provided, the module will be fetched from the default path.
|
|
8
|
+
*/
|
|
9
|
+
module?: ModuleSource
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Instantiate and initialize the module
|
|
14
|
+
*
|
|
15
|
+
* This is a convenience function for browser environments.
|
|
16
|
+
* If you need a more flexible API, see `instantiate`.
|
|
17
|
+
*/
|
|
18
|
+
export declare function init(options?: Options): Promise<{
|
|
19
|
+
instance: WebAssembly.Instance,
|
|
20
|
+
exports: Exports
|
|
21
|
+
}>
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { instantiate } from './instantiate.js';
|
|
3
|
+
import { defaultBrowserSetup } from './platforms/browser.js';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
/** @type {import('./index.d').init} */
|
|
7
|
+
async function initBrowser(_options) {
|
|
8
|
+
/** @type {import('./index.d').Options} */
|
|
9
|
+
const options = _options || {
|
|
10
|
+
};
|
|
11
|
+
let module = options.module;
|
|
12
|
+
if (!module) {
|
|
13
|
+
module = fetch(new URL("GistWeb.wasm", import.meta.url))
|
|
14
|
+
}
|
|
15
|
+
const instantiateOptions = await defaultBrowserSetup({
|
|
16
|
+
module,
|
|
17
|
+
})
|
|
18
|
+
return await instantiate(instantiateOptions);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
/** @type {import('./index.d').init} */
|
|
23
|
+
export async function init(options) {
|
|
24
|
+
return initBrowser(options);
|
|
25
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
|
|
2
|
+
export type Imports = {}
|
|
3
|
+
export type Exports = {}
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The path to the WebAssembly module relative to the root of the package
|
|
7
|
+
*/
|
|
8
|
+
export declare const MODULE_PATH: string;
|
|
9
|
+
|
|
10
|
+
export interface WASI {
|
|
11
|
+
/**
|
|
12
|
+
* The WASI Preview 1 import object
|
|
13
|
+
*/
|
|
14
|
+
wasiImport: WebAssembly.ModuleImports
|
|
15
|
+
/**
|
|
16
|
+
* Initialize the WASI reactor instance
|
|
17
|
+
*
|
|
18
|
+
* @param instance - The instance of the WebAssembly module
|
|
19
|
+
*/
|
|
20
|
+
initialize(instance: WebAssembly.Instance): void
|
|
21
|
+
/**
|
|
22
|
+
* Set a new instance of the WebAssembly module to the WASI context
|
|
23
|
+
* Typically used when instantiating a WebAssembly module for a thread
|
|
24
|
+
*
|
|
25
|
+
* @param instance - The instance of the WebAssembly module
|
|
26
|
+
*/
|
|
27
|
+
setInstance(instance: WebAssembly.Instance): void
|
|
28
|
+
/**
|
|
29
|
+
* Extract a file from the WASI filesystem
|
|
30
|
+
*
|
|
31
|
+
* @param path - The path to the file to extract
|
|
32
|
+
* @returns The data of the file if it was extracted, undefined otherwise
|
|
33
|
+
*/
|
|
34
|
+
extractFile?(path: string): Uint8Array | undefined
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type SwiftRuntime = {
|
|
38
|
+
UnsafeEventLoopYield: { [Symbol.hasInstance]: (value: unknown) => boolean }
|
|
39
|
+
main(): void;
|
|
40
|
+
startThread(tid: number, startArg: number): void;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type ModuleSource = WebAssembly.Module | ArrayBufferView | ArrayBuffer | Response | PromiseLike<Response>
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The options for instantiating a WebAssembly module
|
|
47
|
+
*/
|
|
48
|
+
export type InstantiateOptions = {
|
|
49
|
+
/**
|
|
50
|
+
* The WebAssembly namespace to use for instantiation.
|
|
51
|
+
* Defaults to the globalThis.WebAssembly object.
|
|
52
|
+
*/
|
|
53
|
+
WebAssembly?: typeof globalThis.WebAssembly,
|
|
54
|
+
/**
|
|
55
|
+
* The WebAssembly module to instantiate
|
|
56
|
+
*/
|
|
57
|
+
module: ModuleSource,
|
|
58
|
+
/**
|
|
59
|
+
* The WASI implementation to use
|
|
60
|
+
*/
|
|
61
|
+
wasi: WASI,
|
|
62
|
+
/**
|
|
63
|
+
* Add imports to the WebAssembly import object
|
|
64
|
+
* @param imports - The imports to add
|
|
65
|
+
* @param context - The context object
|
|
66
|
+
*/
|
|
67
|
+
addToCoreImports?: (
|
|
68
|
+
imports: WebAssembly.Imports,
|
|
69
|
+
context: {
|
|
70
|
+
getInstance: () => WebAssembly.Instance | null,
|
|
71
|
+
getExports: () => Exports | null,
|
|
72
|
+
_swift: SwiftRuntime,
|
|
73
|
+
}
|
|
74
|
+
) => void
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Instrument the WebAssembly instance
|
|
78
|
+
*
|
|
79
|
+
* @param instance - The instance of the WebAssembly module
|
|
80
|
+
* @param context - The context object
|
|
81
|
+
* @returns The instrumented instance
|
|
82
|
+
*/
|
|
83
|
+
instrumentInstance?: (
|
|
84
|
+
instance: WebAssembly.Instance,
|
|
85
|
+
context: {
|
|
86
|
+
_swift: SwiftRuntime
|
|
87
|
+
}
|
|
88
|
+
) => WebAssembly.Instance
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Instantiate the given WebAssembly module
|
|
93
|
+
*/
|
|
94
|
+
export declare function instantiate(options: InstantiateOptions): Promise<{
|
|
95
|
+
instance: WebAssembly.Instance,
|
|
96
|
+
swift: SwiftRuntime,
|
|
97
|
+
exports: Exports
|
|
98
|
+
}>
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Instantiate the given WebAssembly module for a thread
|
|
102
|
+
*/
|
|
103
|
+
export declare function instantiateForThread(tid: number, startArg: number, options: InstantiateOptions): Promise<{
|
|
104
|
+
instance: WebAssembly.Instance,
|
|
105
|
+
swift: SwiftRuntime,
|
|
106
|
+
exports: Exports
|
|
107
|
+
}>
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { SwiftRuntime } from "./runtime.js";
|
|
3
|
+
|
|
4
|
+
export const MODULE_PATH = "GistWeb.wasm";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @param {import('./instantiate.d').InstantiateOptions} options
|
|
8
|
+
* @param {any} swift
|
|
9
|
+
*/
|
|
10
|
+
async function createInstantiator(options, swift) {
|
|
11
|
+
return {
|
|
12
|
+
/**
|
|
13
|
+
* @param {WebAssembly.Imports} importObject
|
|
14
|
+
* @param {unknown} importsContext
|
|
15
|
+
*/
|
|
16
|
+
addImports: (importObject, importsContext) => {
|
|
17
|
+
// Provide a default implementation for BridgeJS functions that are not
|
|
18
|
+
// used at runtime without BridgeJS but required to instantiate the module.
|
|
19
|
+
const unexpectedBjsCall = () => {
|
|
20
|
+
throw new Error("Unexpected call to BridgeJS function");
|
|
21
|
+
};
|
|
22
|
+
importObject["bjs"] = {
|
|
23
|
+
swift_js_return_string: unexpectedBjsCall,
|
|
24
|
+
swift_js_init_memory: unexpectedBjsCall,
|
|
25
|
+
swift_js_make_js_string: unexpectedBjsCall,
|
|
26
|
+
swift_js_init_memory_with_result: unexpectedBjsCall,
|
|
27
|
+
swift_js_throw: unexpectedBjsCall,
|
|
28
|
+
swift_js_retain: unexpectedBjsCall,
|
|
29
|
+
swift_js_release: unexpectedBjsCall,
|
|
30
|
+
swift_js_push_i32: unexpectedBjsCall,
|
|
31
|
+
swift_js_push_f32: unexpectedBjsCall,
|
|
32
|
+
swift_js_push_f64: unexpectedBjsCall,
|
|
33
|
+
swift_js_push_string: unexpectedBjsCall,
|
|
34
|
+
swift_js_pop_i32: unexpectedBjsCall,
|
|
35
|
+
swift_js_pop_f32: unexpectedBjsCall,
|
|
36
|
+
swift_js_pop_f64: unexpectedBjsCall,
|
|
37
|
+
swift_js_return_optional_bool: unexpectedBjsCall,
|
|
38
|
+
swift_js_return_optional_int: unexpectedBjsCall,
|
|
39
|
+
swift_js_return_optional_string: unexpectedBjsCall,
|
|
40
|
+
swift_js_return_optional_double: unexpectedBjsCall,
|
|
41
|
+
swift_js_return_optional_float: unexpectedBjsCall,
|
|
42
|
+
swift_js_return_optional_heap_object: unexpectedBjsCall,
|
|
43
|
+
swift_js_return_optional_object: unexpectedBjsCall,
|
|
44
|
+
swift_js_get_optional_int_presence: unexpectedBjsCall,
|
|
45
|
+
swift_js_get_optional_int_value: unexpectedBjsCall,
|
|
46
|
+
swift_js_get_optional_string: unexpectedBjsCall,
|
|
47
|
+
swift_js_get_optional_float_presence: unexpectedBjsCall,
|
|
48
|
+
swift_js_get_optional_float_value: unexpectedBjsCall,
|
|
49
|
+
swift_js_get_optional_double_presence: unexpectedBjsCall,
|
|
50
|
+
swift_js_get_optional_double_value: unexpectedBjsCall,
|
|
51
|
+
swift_js_get_optional_heap_object_pointer: unexpectedBjsCall,
|
|
52
|
+
swift_js_push_pointer: unexpectedBjsCall,
|
|
53
|
+
swift_js_pop_pointer: unexpectedBjsCall,
|
|
54
|
+
swift_js_push_i64: unexpectedBjsCall,
|
|
55
|
+
swift_js_pop_i64: unexpectedBjsCall,
|
|
56
|
+
swift_js_closure_unregister: unexpectedBjsCall,
|
|
57
|
+
swift_js_push_typed_array: unexpectedBjsCall,
|
|
58
|
+
swift_js_make_promise: unexpectedBjsCall,
|
|
59
|
+
};
|
|
60
|
+
},
|
|
61
|
+
/** @param {WebAssembly.Instance} instance */
|
|
62
|
+
setInstance: (instance) => {},
|
|
63
|
+
/** @param {WebAssembly.Instance} instance */
|
|
64
|
+
createExports: (instance) => {
|
|
65
|
+
return {};
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** @type {import('./instantiate.d').instantiate} */
|
|
71
|
+
export async function instantiate(options) {
|
|
72
|
+
const result = await _instantiate(options);
|
|
73
|
+
options.wasi.initialize(result.instance);
|
|
74
|
+
result.swift.main();
|
|
75
|
+
return result;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** @type {import('./instantiate.d').instantiateForThread} */
|
|
79
|
+
export async function instantiateForThread(tid, startArg, options) {
|
|
80
|
+
const result = await _instantiate(options);
|
|
81
|
+
options.wasi.setInstance(result.instance);
|
|
82
|
+
result.swift.startThread(tid, startArg);
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** @type {import('./instantiate.d').instantiate} */
|
|
87
|
+
async function _instantiate(options) {
|
|
88
|
+
const _WebAssembly = options.WebAssembly || WebAssembly;
|
|
89
|
+
const moduleSource = options.module;
|
|
90
|
+
const { wasi } = options;
|
|
91
|
+
const swift = new SwiftRuntime({
|
|
92
|
+
});
|
|
93
|
+
const instantiator = await createInstantiator(options, swift);
|
|
94
|
+
|
|
95
|
+
/** @type {WebAssembly.Imports} */
|
|
96
|
+
const importObject = {
|
|
97
|
+
javascript_kit: swift.wasmImports,
|
|
98
|
+
wasi_snapshot_preview1: wasi.wasiImport,
|
|
99
|
+
};
|
|
100
|
+
const importsContext = {
|
|
101
|
+
getInstance: () => instance,
|
|
102
|
+
getExports: () => exports,
|
|
103
|
+
_swift: swift,
|
|
104
|
+
};
|
|
105
|
+
instantiator.addImports(importObject, importsContext);
|
|
106
|
+
options.addToCoreImports?.(importObject, importsContext);
|
|
107
|
+
|
|
108
|
+
let module;
|
|
109
|
+
let instance;
|
|
110
|
+
let exports;
|
|
111
|
+
if (moduleSource instanceof _WebAssembly.Module) {
|
|
112
|
+
module = moduleSource;
|
|
113
|
+
instance = await _WebAssembly.instantiate(module, importObject);
|
|
114
|
+
} else if (
|
|
115
|
+
typeof Response === "function" &&
|
|
116
|
+
(moduleSource instanceof Response || moduleSource instanceof Promise)
|
|
117
|
+
) {
|
|
118
|
+
if (typeof _WebAssembly.instantiateStreaming === "function") {
|
|
119
|
+
const result = await _WebAssembly.instantiateStreaming(
|
|
120
|
+
moduleSource,
|
|
121
|
+
importObject,
|
|
122
|
+
);
|
|
123
|
+
module = result.module;
|
|
124
|
+
instance = result.instance;
|
|
125
|
+
} else {
|
|
126
|
+
const moduleBytes = await (await moduleSource).arrayBuffer();
|
|
127
|
+
module = await _WebAssembly.compile(moduleBytes);
|
|
128
|
+
instance = await _WebAssembly.instantiate(module, importObject);
|
|
129
|
+
}
|
|
130
|
+
} else {
|
|
131
|
+
// @ts-expect-error: Type 'Response' is not assignable to type 'BufferSource'
|
|
132
|
+
module = await _WebAssembly.compile(moduleSource);
|
|
133
|
+
instance = await _WebAssembly.instantiate(module, importObject);
|
|
134
|
+
}
|
|
135
|
+
instance =
|
|
136
|
+
options.instrumentInstance?.(instance, { _swift: swift }) ?? instance;
|
|
137
|
+
|
|
138
|
+
swift.setInstance(instance);
|
|
139
|
+
instantiator.setInstance(instance);
|
|
140
|
+
exports = instantiator.createExports(instance);
|
|
141
|
+
|
|
142
|
+
return {
|
|
143
|
+
instance,
|
|
144
|
+
swift,
|
|
145
|
+
exports,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { InstantiateOptions, ModuleSource } from "../instantiate.js"
|
|
2
|
+
|
|
3
|
+
export function defaultBrowserSetup(options: {
|
|
4
|
+
module: ModuleSource,
|
|
5
|
+
args?: string[],
|
|
6
|
+
onStdoutLine?: (line: string) => void,
|
|
7
|
+
onStderrLine?: (line: string) => void,
|
|
8
|
+
}): Promise<InstantiateOptions>
|
|
9
|
+
|
|
10
|
+
export function createDefaultWorkerFactory(preludeScript?: string): (module: WebAssembly.Module, memory: WebAssembly.Memory, startArg: any) => Worker
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { MODULE_PATH } from "../instantiate.js"
|
|
3
|
+
// @ts-ignore
|
|
4
|
+
import { WASI, File, OpenFile, ConsoleStdout, PreopenDirectory } from '@bjorn3/browser_wasi_shim';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
/** @type {import('./browser.d.ts').defaultBrowserSetup} */
|
|
8
|
+
export async function defaultBrowserSetup(options) {
|
|
9
|
+
const args = options.args ?? []
|
|
10
|
+
const onStdoutLine = options.onStdoutLine ?? ((line) => console.log(line))
|
|
11
|
+
const onStderrLine = options.onStderrLine ?? ((line) => console.error(line))
|
|
12
|
+
const wasi = new WASI(/* args */[MODULE_PATH, ...args], /* env */[], /* fd */[
|
|
13
|
+
new OpenFile(new File([])), // stdin
|
|
14
|
+
ConsoleStdout.lineBuffered((stdout) => {
|
|
15
|
+
onStdoutLine(stdout);
|
|
16
|
+
}),
|
|
17
|
+
ConsoleStdout.lineBuffered((stderr) => {
|
|
18
|
+
onStderrLine(stderr);
|
|
19
|
+
}),
|
|
20
|
+
new PreopenDirectory("/", new Map()),
|
|
21
|
+
], { debug: false })
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
module: options.module,
|
|
25
|
+
wasi: Object.assign(wasi, {
|
|
26
|
+
setInstance(instance) {
|
|
27
|
+
wasi.inst = instance;
|
|
28
|
+
}
|
|
29
|
+
}),
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { instantiateForThread } from "../instantiate.js"
|
|
2
|
+
import { defaultBrowserThreadSetup } from "./browser.js"
|
|
3
|
+
|
|
4
|
+
self.onmessage = async (event) => {
|
|
5
|
+
const { module, memory, tid, startArg, preludeScript } = event.data;
|
|
6
|
+
let options = await defaultBrowserThreadSetup();
|
|
7
|
+
if (preludeScript) {
|
|
8
|
+
const prelude = await import(preludeScript);
|
|
9
|
+
if (prelude.setupOptions) {
|
|
10
|
+
options = prelude.setupOptions(options, { isMainThread: false })
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
await instantiateForThread(tid, startArg, {
|
|
14
|
+
...options,
|
|
15
|
+
module, memory,
|
|
16
|
+
getImports() { return {} },
|
|
17
|
+
})
|
|
18
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { InstantiateOptions } from "../instantiate.js"
|
|
2
|
+
import type { Worker } from "node:worker_threads"
|
|
3
|
+
|
|
4
|
+
export type DefaultNodeSetupOptions = {
|
|
5
|
+
args?: string[],
|
|
6
|
+
onExit?: (code: number) => void,
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function defaultNodeSetup(options?: DefaultNodeSetupOptions): Promise<InstantiateOptions>
|
|
10
|
+
|
|
11
|
+
export function createDefaultWorkerFactory(preludeScript?: string): (module: WebAssembly.Module, memory: WebAssembly.Memory, startArg: any) => Worker
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { Worker, parentPort } from "node:worker_threads";
|
|
4
|
+
import { MODULE_PATH } from "../instantiate.js"
|
|
5
|
+
import { WASI, File, OpenFile, ConsoleStdout, PreopenDirectory, Directory, Inode } from '@bjorn3/browser_wasi_shim';
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
/** @type {import('./node.d.ts').defaultNodeSetup} */
|
|
9
|
+
export async function defaultNodeSetup(options = {}) {
|
|
10
|
+
const path = await import("node:path");
|
|
11
|
+
const { fileURLToPath } = await import("node:url");
|
|
12
|
+
const { readFile } = await import("node:fs/promises")
|
|
13
|
+
|
|
14
|
+
const args = options.args ?? process.argv.slice(2)
|
|
15
|
+
const rootFs = new Map();
|
|
16
|
+
const wasi = new WASI(/* args */[MODULE_PATH, ...args], /* env */[], /* fd */[
|
|
17
|
+
new OpenFile(new File([])), // stdin
|
|
18
|
+
ConsoleStdout.lineBuffered((stdout) => {
|
|
19
|
+
console.log(stdout);
|
|
20
|
+
}),
|
|
21
|
+
ConsoleStdout.lineBuffered((stderr) => {
|
|
22
|
+
console.error(stderr);
|
|
23
|
+
}),
|
|
24
|
+
new PreopenDirectory("/", rootFs),
|
|
25
|
+
], { debug: false })
|
|
26
|
+
const pkgDir = path.dirname(path.dirname(fileURLToPath(import.meta.url)))
|
|
27
|
+
const module = await WebAssembly.compile(new Uint8Array(await readFile(path.join(pkgDir, MODULE_PATH))))
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
module,
|
|
31
|
+
wasi: Object.assign(wasi, {
|
|
32
|
+
setInstance(instance) {
|
|
33
|
+
wasi.inst = instance;
|
|
34
|
+
},
|
|
35
|
+
/**
|
|
36
|
+
* @param {string} path
|
|
37
|
+
* @returns {Uint8Array | undefined}
|
|
38
|
+
*/
|
|
39
|
+
extractFile(path) {
|
|
40
|
+
/**
|
|
41
|
+
* @param {Map<string, Inode>} parent
|
|
42
|
+
* @param {string[]} components
|
|
43
|
+
* @param {number} index
|
|
44
|
+
* @returns {Inode | undefined}
|
|
45
|
+
*/
|
|
46
|
+
const getFile = (parent, components, index) => {
|
|
47
|
+
const name = components[index];
|
|
48
|
+
const entry = parent.get(name);
|
|
49
|
+
if (entry === undefined) {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
if (index === components.length - 1) {
|
|
53
|
+
return entry;
|
|
54
|
+
}
|
|
55
|
+
if (entry instanceof Directory) {
|
|
56
|
+
return getFile(entry.contents, components, index + 1);
|
|
57
|
+
}
|
|
58
|
+
throw new Error(`Expected directory at ${components.slice(0, index).join("/")}`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const components = path.split("/");
|
|
62
|
+
const file = getFile(rootFs, components, 0);
|
|
63
|
+
if (file === undefined) {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
if (file instanceof File) {
|
|
67
|
+
return file.data;
|
|
68
|
+
}
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
}),
|
|
72
|
+
addToCoreImports(importObject) {
|
|
73
|
+
importObject["wasi_snapshot_preview1"]["proc_exit"] = (code) => {
|
|
74
|
+
if (options.onExit) {
|
|
75
|
+
options.onExit(code);
|
|
76
|
+
}
|
|
77
|
+
process.exit(code);
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
}
|
|
81
|
+
}
|