@tensorlakeai/tensorlake 0.0.0-bootstrap.0 → 0.5.145
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/README.md +166 -2
- package/bin/function-executor.js +8 -0
- package/bin/tensorlake-create-sandbox-image.js +9 -0
- package/bin/tensorlake-deploy.cjs +8 -0
- package/bin/tensorlake-import-sandbox-image.js +9 -0
- package/bin/tensorlake-typescript-function-runner.js +8 -0
- package/dist/applications/index.cjs +4821 -0
- package/dist/applications/index.cjs.map +1 -0
- package/dist/applications/index.d.cts +130 -0
- package/dist/applications/index.d.ts +130 -0
- package/dist/applications/index.js +4782 -0
- package/dist/applications/index.js.map +1 -0
- package/dist/function-agent/main.cjs +1838 -0
- package/dist/function-agent/main.cjs.map +1 -0
- package/dist/function-agent/main.d.cts +3 -0
- package/dist/function-agent/main.d.ts +3 -0
- package/dist/function-agent/main.js +1825 -0
- package/dist/function-agent/main.js.map +1 -0
- package/dist/function-executor/main.cjs +4944 -0
- package/dist/function-executor/main.cjs.map +1 -0
- package/dist/function-executor/main.d.cts +4 -0
- package/dist/function-executor/main.d.ts +4 -0
- package/dist/function-executor/main.js +4902 -0
- package/dist/function-executor/main.js.map +1 -0
- package/dist/index.cjs +10184 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +2431 -0
- package/dist/index.d.ts +2431 -0
- package/dist/index.js +10108 -0
- package/dist/index.js.map +1 -0
- package/dist/native-worker.cjs +278 -0
- package/dist/native-worker.cjs.map +1 -0
- package/dist/native-worker.d.cts +2 -0
- package/dist/native-worker.d.ts +2 -0
- package/dist/native-worker.js +276 -0
- package/dist/native-worker.js.map +1 -0
- package/dist/remote-CNJ9TmcQ.d.cts +283 -0
- package/dist/remote-dblFxKug.d.ts +283 -0
- package/dist/sandbox-image-ChzWDeZy.d.cts +357 -0
- package/dist/sandbox-image-ChzWDeZy.d.ts +357 -0
- package/dist/sandbox-image.cjs +1992 -0
- package/dist/sandbox-image.cjs.map +1 -0
- package/dist/sandbox-image.d.cts +1 -0
- package/dist/sandbox-image.d.ts +1 -0
- package/dist/sandbox-image.js +1987 -0
- package/dist/sandbox-image.js.map +1 -0
- package/lib/libc.cjs +61 -0
- package/lib/libc.d.cts +1 -0
- package/lib/runtime.cjs +155 -0
- package/package.json +104 -4
- package/runtime/function-executor/manifest.json +59 -0
- package/runtime/function-executor/package/bin/function-executor.js +8 -0
- package/runtime/function-executor/package/dist/function-executor/main.js +4902 -0
- package/runtime/function-executor/package/dist/function-executor/main.js.map +1 -0
- package/runtime/function-executor/package/dist/native-worker.cjs +278 -0
- package/runtime/function-executor/package/dist/native-worker.cjs.map +1 -0
- package/runtime/function-executor/package/npm-shrinkwrap.json +475 -0
- package/runtime/function-executor/package/package.json +22 -0
- package/runtime/function-executor/package/proto/README.md +18 -0
- package/runtime/function-executor/package/proto/tensorlake/function_executor/proto/function_executor.proto +613 -0
- package/runtime/function-executor/package/proto/tensorlake/function_executor/proto/status.proto +42 -0
- package/runtime/typescript-function-runner/manifest.json +44 -0
- package/runtime/typescript-function-runner/package/bin/tensorlake-typescript-function-runner.js +8 -0
- package/runtime/typescript-function-runner/package/dist/function-agent/main.js +1825 -0
- package/runtime/typescript-function-runner/package/dist/function-agent/main.js.map +1 -0
- package/runtime/typescript-function-runner/package/dist/native-worker.cjs +278 -0
- package/runtime/typescript-function-runner/package/dist/native-worker.cjs.map +1 -0
- package/runtime/typescript-function-runner/package/npm-shrinkwrap.json +586 -0
- package/runtime/typescript-function-runner/package/package.json +25 -0
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __esm = (fn, res) => function __init() {
|
|
9
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
10
|
+
};
|
|
11
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
12
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
13
|
+
};
|
|
14
|
+
var __copyProps = (to, from, except, desc) => {
|
|
15
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
16
|
+
for (let key of __getOwnPropNames(from))
|
|
17
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
18
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
23
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
24
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
25
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
26
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
27
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
28
|
+
mod
|
|
29
|
+
));
|
|
30
|
+
|
|
31
|
+
// node_modules/tsup/assets/cjs_shims.js
|
|
32
|
+
var getImportMetaUrl, importMetaUrl;
|
|
33
|
+
var init_cjs_shims = __esm({
|
|
34
|
+
"node_modules/tsup/assets/cjs_shims.js"() {
|
|
35
|
+
"use strict";
|
|
36
|
+
getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.tagName.toUpperCase() === "SCRIPT" ? document.currentScript.src : new URL("main.js", document.baseURI).href;
|
|
37
|
+
importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// lib/libc.cjs
|
|
42
|
+
var require_libc = __commonJS({
|
|
43
|
+
"lib/libc.cjs"(exports2, module2) {
|
|
44
|
+
"use strict";
|
|
45
|
+
init_cjs_shims();
|
|
46
|
+
function detectLinuxLibc2(fs2, executable = process.execPath) {
|
|
47
|
+
const override = process.env.TENSORLAKE_NODE_LIBC;
|
|
48
|
+
if (override != null) {
|
|
49
|
+
if (override === "gnu" || override === "musl") return override;
|
|
50
|
+
throw new Error("TENSORLAKE_NODE_LIBC must be 'gnu' or 'musl'.");
|
|
51
|
+
}
|
|
52
|
+
let fd;
|
|
53
|
+
try {
|
|
54
|
+
let read2 = function(size, offset) {
|
|
55
|
+
if (!Number.isSafeInteger(offset) || offset < 0) {
|
|
56
|
+
throw new Error("Invalid ELF offset");
|
|
57
|
+
}
|
|
58
|
+
const buffer = Buffer.alloc(size);
|
|
59
|
+
let count2 = 0;
|
|
60
|
+
while (count2 < size) {
|
|
61
|
+
const bytes = fs2.readSync(fd, buffer, count2, size - count2, offset + count2);
|
|
62
|
+
if (bytes === 0) throw new Error("Truncated ELF executable");
|
|
63
|
+
count2 += bytes;
|
|
64
|
+
}
|
|
65
|
+
return buffer;
|
|
66
|
+
};
|
|
67
|
+
var read = read2;
|
|
68
|
+
fd = fs2.openSync(executable, "r");
|
|
69
|
+
const header = read2(64, 0);
|
|
70
|
+
if (header.readUInt32BE(0) !== 2135247942 || header[4] !== 2 || header[5] !== 1) {
|
|
71
|
+
throw new Error("Expected a little-endian ELF64 executable");
|
|
72
|
+
}
|
|
73
|
+
const entrySize = header.readUInt16LE(54);
|
|
74
|
+
const count = header.readUInt16LE(56);
|
|
75
|
+
if (entrySize !== 56 || count === 0 || count > 128) {
|
|
76
|
+
throw new Error("Unsupported ELF program headers");
|
|
77
|
+
}
|
|
78
|
+
const entries = read2(entrySize * count, Number(header.readBigUInt64LE(32)));
|
|
79
|
+
for (let i = 0; i < count; i++) {
|
|
80
|
+
const offset = i * entrySize;
|
|
81
|
+
if (entries.readUInt32LE(offset) !== 3) continue;
|
|
82
|
+
const size = Number(entries.readBigUInt64LE(offset + 32));
|
|
83
|
+
if (size < 2 || size > 4096) throw new Error("Invalid ELF interpreter size");
|
|
84
|
+
const interpreter = read2(size, Number(entries.readBigUInt64LE(offset + 8)));
|
|
85
|
+
if (interpreter[size - 1] !== 0) throw new Error("Unterminated ELF interpreter");
|
|
86
|
+
const name = interpreter.subarray(0, -1).toString("utf8").split("/").at(-1);
|
|
87
|
+
if (name.startsWith("ld-musl-")) return "musl";
|
|
88
|
+
if (name.startsWith("ld-linux-")) return "gnu";
|
|
89
|
+
throw new Error("Unrecognized ELF interpreter");
|
|
90
|
+
}
|
|
91
|
+
throw new Error("No ELF interpreter (statically linked executable)");
|
|
92
|
+
} catch (cause) {
|
|
93
|
+
throw new Error(
|
|
94
|
+
"Unable to detect Linux libc from the Node executable. Set TENSORLAKE_NODE_LIBC to 'gnu' or 'musl' for a nonstandard runtime.",
|
|
95
|
+
{ cause }
|
|
96
|
+
);
|
|
97
|
+
} finally {
|
|
98
|
+
if (fd != null) fs2.closeSync(fd);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
module2.exports = { detectLinuxLibc: detectLinuxLibc2 };
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// src/native-worker.ts
|
|
106
|
+
init_cjs_shims();
|
|
107
|
+
var import_node_worker_threads = require("worker_threads");
|
|
108
|
+
|
|
109
|
+
// src/native-binding.ts
|
|
110
|
+
init_cjs_shims();
|
|
111
|
+
var import_libc = __toESM(require_libc(), 1);
|
|
112
|
+
var fs = __toESM(require("fs"), 1);
|
|
113
|
+
var import_node_module = require("module");
|
|
114
|
+
var import_node_path = __toESM(require("path"), 1);
|
|
115
|
+
var import_node_url = require("url");
|
|
116
|
+
var NATIVE_PACKAGES = {
|
|
117
|
+
"darwin-arm64": "@tensorlakeai/native-darwin-arm64",
|
|
118
|
+
"linux-arm64": "@tensorlakeai/native-linux-arm64-gnu",
|
|
119
|
+
"linux-arm64-musl": "@tensorlakeai/native-linux-arm64-musl",
|
|
120
|
+
"linux-x64": "@tensorlakeai/native-linux-x64-gnu",
|
|
121
|
+
"linux-x64-musl": "@tensorlakeai/native-linux-x64-musl",
|
|
122
|
+
"win32-x64": "@tensorlakeai/native-win32-x64"
|
|
123
|
+
};
|
|
124
|
+
function packageRoot() {
|
|
125
|
+
const parent = (0, import_node_url.fileURLToPath)(new URL("../", importMetaUrl));
|
|
126
|
+
if (fs.existsSync(import_node_path.default.join(parent, "package.json"))) return parent;
|
|
127
|
+
const grandparent = (0, import_node_url.fileURLToPath)(new URL("../../", importMetaUrl));
|
|
128
|
+
if (fs.existsSync(import_node_path.default.join(grandparent, "package.json"))) return grandparent;
|
|
129
|
+
return parent;
|
|
130
|
+
}
|
|
131
|
+
var cachedLinuxLibc;
|
|
132
|
+
function linuxLibcFamily(options) {
|
|
133
|
+
if (options.libc != null) return options.libc;
|
|
134
|
+
if (options.report != null) {
|
|
135
|
+
return options.report.header?.glibcVersionRuntime ? "gnu" : "musl";
|
|
136
|
+
}
|
|
137
|
+
if (cachedLinuxLibc != null) return cachedLinuxLibc;
|
|
138
|
+
cachedLinuxLibc = (0, import_libc.detectLinuxLibc)(fs);
|
|
139
|
+
return cachedLinuxLibc;
|
|
140
|
+
}
|
|
141
|
+
function nativeTargetId(platform = process.platform, arch = process.arch, options = {}) {
|
|
142
|
+
const target = `${platform}-${arch}`;
|
|
143
|
+
if (platform !== "linux") return target;
|
|
144
|
+
if (options.libc == null && options.report == null && platform !== process.platform) {
|
|
145
|
+
return target;
|
|
146
|
+
}
|
|
147
|
+
return linuxLibcFamily(options) === "musl" ? `${target}-musl` : target;
|
|
148
|
+
}
|
|
149
|
+
function nativeBindingPath(options = {}) {
|
|
150
|
+
const platform = options.platform ?? process.platform;
|
|
151
|
+
const arch = options.arch ?? process.arch;
|
|
152
|
+
return import_node_path.default.join(
|
|
153
|
+
options.packageRoot ?? packageRoot(),
|
|
154
|
+
"dist",
|
|
155
|
+
"native",
|
|
156
|
+
nativeTargetId(platform, arch, options),
|
|
157
|
+
"tensorlake-node.node"
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
function nativePackageName(platform = process.platform, arch = process.arch, options = {}) {
|
|
161
|
+
return NATIVE_PACKAGES[nativeTargetId(platform, arch, options)];
|
|
162
|
+
}
|
|
163
|
+
function loadNative() {
|
|
164
|
+
const target = nativeTargetId();
|
|
165
|
+
const bindingPath = nativeBindingPath();
|
|
166
|
+
const require2 = (0, import_node_module.createRequire)(importMetaUrl);
|
|
167
|
+
if (fs.existsSync(bindingPath)) return require2(bindingPath);
|
|
168
|
+
const packageName = nativePackageName();
|
|
169
|
+
if (!packageName) {
|
|
170
|
+
throw new Error(
|
|
171
|
+
`Tensorlake does not provide a native binding for ${target}.`
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
let packageEntry;
|
|
175
|
+
try {
|
|
176
|
+
packageEntry = require2.resolve(packageName);
|
|
177
|
+
} catch (cause) {
|
|
178
|
+
throw new Error(
|
|
179
|
+
`Missing native binding package ${packageName} for ${target}. Reinstall @tensorlakeai/tensorlake without omitting optional dependencies, and install dependencies on the machine where they will run.`,
|
|
180
|
+
{ cause }
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
return require2(packageEntry);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// src/native-worker.ts
|
|
187
|
+
var port = import_node_worker_threads.parentPort;
|
|
188
|
+
var binding = loadNative();
|
|
189
|
+
var handles = /* @__PURE__ */ new Map();
|
|
190
|
+
var streams = /* @__PURE__ */ new Map();
|
|
191
|
+
function getHandle(id) {
|
|
192
|
+
const entry = handles.get(id);
|
|
193
|
+
if (!entry) throw new Error("Tensorlake native handle has been released");
|
|
194
|
+
if (entry.error) throw entry.error;
|
|
195
|
+
return entry.value;
|
|
196
|
+
}
|
|
197
|
+
function nativeArgs(value) {
|
|
198
|
+
if (value instanceof Uint8Array)
|
|
199
|
+
return Buffer.from(value.buffer, value.byteOffset, value.byteLength);
|
|
200
|
+
if (Array.isArray(value)) return value.map(nativeArgs);
|
|
201
|
+
if (value && typeof value === "object")
|
|
202
|
+
return Object.fromEntries(
|
|
203
|
+
Object.entries(value).map(([k, v]) => [k, nativeArgs(v)])
|
|
204
|
+
);
|
|
205
|
+
return value;
|
|
206
|
+
}
|
|
207
|
+
function send(message) {
|
|
208
|
+
port.postMessage(message);
|
|
209
|
+
}
|
|
210
|
+
port.on("message", (message) => {
|
|
211
|
+
if (message.type === "create") {
|
|
212
|
+
try {
|
|
213
|
+
const { kind, args, id } = message.handle;
|
|
214
|
+
const ctor = binding[kind];
|
|
215
|
+
if (typeof ctor !== "function")
|
|
216
|
+
throw new Error(
|
|
217
|
+
`native binding does not export ${kind}; rebuild with 'npm run build:native'`
|
|
218
|
+
);
|
|
219
|
+
const value = message.parentId === void 0 ? new ctor(...args) : getHandle(message.parentId).connectProxy(...args);
|
|
220
|
+
handles.set(id, { value });
|
|
221
|
+
} catch (error) {
|
|
222
|
+
handles.set(message.handle.id, { error });
|
|
223
|
+
}
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (message.type === "release") {
|
|
227
|
+
handles.get(message.handleId)?.value?.close?.();
|
|
228
|
+
handles.delete(message.handleId);
|
|
229
|
+
for (const stream of streams.values()) {
|
|
230
|
+
if (stream.handleId === message.handleId) {
|
|
231
|
+
stream.cancel?.();
|
|
232
|
+
stream.ack?.();
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
if (message.type === "cancel" || message.type === "ack") {
|
|
238
|
+
const stream = streams.get(message.id);
|
|
239
|
+
if (message.type === "cancel") stream?.cancel?.();
|
|
240
|
+
stream?.ack?.();
|
|
241
|
+
if (stream) stream.ack = void 0;
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
if (message.type === "call") void invoke(message);
|
|
245
|
+
});
|
|
246
|
+
async function invoke(message) {
|
|
247
|
+
try {
|
|
248
|
+
const target = message.handleId === void 0 ? binding : getHandle(message.handleId);
|
|
249
|
+
const args = nativeArgs(message.args);
|
|
250
|
+
let stream;
|
|
251
|
+
if (message.stream) {
|
|
252
|
+
stream = { handleId: message.handleId };
|
|
253
|
+
const state = stream;
|
|
254
|
+
streams.set(message.id, stream);
|
|
255
|
+
args.push(
|
|
256
|
+
(value2) => new Promise((resolve) => {
|
|
257
|
+
state.ack = resolve;
|
|
258
|
+
send({ type: "event", id: message.id, value: value2 });
|
|
259
|
+
})
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
const call = target[message.method](...args);
|
|
263
|
+
if (stream) stream.cancel = call.cancel;
|
|
264
|
+
const value = await (stream ? call.result : call);
|
|
265
|
+
send({ type: "result", id: message.id, value });
|
|
266
|
+
} catch (cause) {
|
|
267
|
+
const error = cause instanceof Error ? cause : new Error(String(cause));
|
|
268
|
+
send({
|
|
269
|
+
type: "error",
|
|
270
|
+
id: message.id,
|
|
271
|
+
error: { name: error.name, message: error.message, stack: error.stack }
|
|
272
|
+
});
|
|
273
|
+
} finally {
|
|
274
|
+
streams.get(message.id)?.ack?.();
|
|
275
|
+
streams.delete(message.id);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
//# sourceMappingURL=native-worker.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../node_modules/tsup/assets/cjs_shims.js","../lib/libc.cjs","../src/native-worker.ts","../src/native-binding.ts"],"sourcesContent":["// Shim globals in cjs bundle\n// There's a weird bug that esbuild will always inject importMetaUrl\n// if we export it as `const importMetaUrl = ... __filename ...`\n// But using a function will not cause this issue\n\nconst getImportMetaUrl = () => \n typeof document === \"undefined\" \n ? new URL(`file:${__filename}`).href \n : (document.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT') \n ? document.currentScript.src \n : new URL(\"main.js\", document.baseURI).href;\n\nexport const importMetaUrl = /* @__PURE__ */ getImportMetaUrl()\n","// Read the ELF interpreter, rather than generating a process-wide diagnostic\n// report. Both supported Linux architectures use little-endian ELF64. Reads\n// are bounded to the header, program headers, and interpreter pathname.\nfunction detectLinuxLibc(fs, executable = process.execPath) {\n const override = process.env.TENSORLAKE_NODE_LIBC;\n if (override != null) {\n if (override === \"gnu\" || override === \"musl\") return override;\n throw new Error(\"TENSORLAKE_NODE_LIBC must be 'gnu' or 'musl'.\");\n }\n\n let fd;\n try {\n fd = fs.openSync(executable, \"r\");\n function read(size, offset) {\n if (!Number.isSafeInteger(offset) || offset < 0) {\n throw new Error(\"Invalid ELF offset\");\n }\n const buffer = Buffer.alloc(size);\n let count = 0;\n while (count < size) {\n const bytes = fs.readSync(fd, buffer, count, size - count, offset + count);\n if (bytes === 0) throw new Error(\"Truncated ELF executable\");\n count += bytes;\n }\n return buffer;\n }\n\n const header = read(64, 0);\n if (header.readUInt32BE(0) !== 0x7f454c46 || header[4] !== 2 || header[5] !== 1) {\n throw new Error(\"Expected a little-endian ELF64 executable\");\n }\n const entrySize = header.readUInt16LE(54);\n const count = header.readUInt16LE(56);\n if (entrySize !== 56 || count === 0 || count > 128) {\n throw new Error(\"Unsupported ELF program headers\");\n }\n const entries = read(entrySize * count, Number(header.readBigUInt64LE(32)));\n for (let i = 0; i < count; i++) {\n const offset = i * entrySize;\n if (entries.readUInt32LE(offset) !== 3) continue; // PT_INTERP\n const size = Number(entries.readBigUInt64LE(offset + 32));\n if (size < 2 || size > 4096) throw new Error(\"Invalid ELF interpreter size\");\n const interpreter = read(size, Number(entries.readBigUInt64LE(offset + 8)));\n if (interpreter[size - 1] !== 0) throw new Error(\"Unterminated ELF interpreter\");\n const name = interpreter.subarray(0, -1).toString(\"utf8\").split(\"/\").at(-1);\n if (name.startsWith(\"ld-musl-\")) return \"musl\";\n if (name.startsWith(\"ld-linux-\")) return \"gnu\";\n throw new Error(\"Unrecognized ELF interpreter\");\n }\n throw new Error(\"No ELF interpreter (statically linked executable)\");\n } catch (cause) {\n throw new Error(\n \"Unable to detect Linux libc from the Node executable. Set TENSORLAKE_NODE_LIBC to 'gnu' or 'musl' for a nonstandard runtime.\",\n { cause },\n );\n } finally {\n if (fd != null) fs.closeSync(fd);\n }\n}\n\nmodule.exports = { detectLinuxLibc };\n","import { parentPort } from \"node:worker_threads\";\nimport { loadNative } from \"./native-binding.js\";\nimport type {\n WorkerRequest,\n WorkerResponse,\n} from \"./native-worker-protocol.js\";\n\nconst port = parentPort!;\n// This module is only a worker entrypoint. All platform detection, dlopen and\n// native object construction happen in this isolate.\nconst binding = loadNative<Record<string, any>>();\nconst handles = new Map<number, { value?: any; error?: unknown }>();\ninterface StreamState {\n cancel?: () => void;\n handleId?: number;\n ack?: () => void;\n}\nconst streams = new Map<number, StreamState>();\n\nfunction getHandle(id: number): any {\n const entry = handles.get(id);\n if (!entry) throw new Error(\"Tensorlake native handle has been released\");\n if (entry.error) throw entry.error;\n return entry.value;\n}\n\n/** Structured clone turns Buffers into Uint8Arrays; napi expects Buffers. */\nfunction nativeArgs(value: any): any {\n if (value instanceof Uint8Array)\n return Buffer.from(value.buffer, value.byteOffset, value.byteLength);\n if (Array.isArray(value)) return value.map(nativeArgs);\n if (value && typeof value === \"object\")\n return Object.fromEntries(\n Object.entries(value).map(([k, v]) => [k, nativeArgs(v)]),\n );\n return value;\n}\n\nfunction send(message: WorkerResponse): void {\n port.postMessage(message);\n}\n\nport.on(\"message\", (message: WorkerRequest) => {\n if (message.type === \"create\") {\n try {\n const { kind, args, id } = message.handle;\n const ctor = binding[kind];\n if (typeof ctor !== \"function\")\n throw new Error(\n `native binding does not export ${kind}; rebuild with 'npm run build:native'`,\n );\n const value =\n message.parentId === undefined\n ? new ctor(...args)\n : getHandle(message.parentId).connectProxy(...args);\n handles.set(id, { value });\n } catch (error) {\n handles.set(message.handle.id, { error });\n }\n return;\n }\n if (message.type === \"release\") {\n // Cloud clients own cancellation for buffered requests as well as streams.\n handles.get(message.handleId)?.value?.close?.();\n handles.delete(message.handleId);\n for (const stream of streams.values()) {\n if (stream.handleId === message.handleId) {\n stream.cancel?.();\n stream.ack?.();\n }\n }\n return;\n }\n if (message.type === \"cancel\" || message.type === \"ack\") {\n const stream = streams.get(message.id);\n if (message.type === \"cancel\") stream?.cancel?.();\n stream?.ack?.();\n if (stream) stream.ack = undefined;\n return;\n }\n if (message.type === \"call\") void invoke(message);\n});\n\nasync function invoke(\n message: Extract<WorkerRequest, { type: \"call\" }>,\n): Promise<void> {\n try {\n const target =\n message.handleId === undefined ? binding : getHandle(message.handleId);\n const args = nativeArgs(message.args);\n let stream: StreamState | undefined;\n if (message.stream) {\n stream = { handleId: message.handleId };\n const state = stream;\n streams.set(message.id, stream);\n args.push(\n (value: string) =>\n new Promise<void>((resolve) => {\n state.ack = resolve;\n send({ type: \"event\", id: message.id, value });\n }),\n );\n }\n const call = target[message.method](...args);\n if (stream) stream.cancel = call.cancel;\n const value = await (stream ? call.result : call);\n send({ type: \"result\", id: message.id, value });\n } catch (cause) {\n const error = cause instanceof Error ? cause : new Error(String(cause));\n send({\n type: \"error\",\n id: message.id,\n error: { name: error.name, message: error.message, stack: error.stack },\n });\n } finally {\n streams.get(message.id)?.ack?.();\n streams.delete(message.id);\n }\n}\n","import { detectLinuxLibc } from \"../lib/libc.cjs\";\nimport * as fs from \"node:fs\";\nimport { createRequire } from \"node:module\";\nimport path from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\n\ninterface RuntimeReport {\n header?: {\n glibcVersionRuntime?: string;\n };\n}\n\ninterface TargetOptions {\n libc?: \"gnu\" | \"musl\";\n report?: RuntimeReport;\n}\n\ninterface NativeBindingPathOptions extends TargetOptions {\n packageRoot?: string;\n platform?: NodeJS.Platform;\n arch?: string;\n}\n\nconst NATIVE_PACKAGES: Readonly<Record<string, string>> = {\n \"darwin-arm64\": \"@tensorlakeai/native-darwin-arm64\",\n \"linux-arm64\": \"@tensorlakeai/native-linux-arm64-gnu\",\n \"linux-arm64-musl\": \"@tensorlakeai/native-linux-arm64-musl\",\n \"linux-x64\": \"@tensorlakeai/native-linux-x64-gnu\",\n \"linux-x64-musl\": \"@tensorlakeai/native-linux-x64-musl\",\n \"win32-x64\": \"@tensorlakeai/native-win32-x64\",\n};\n\nfunction packageRoot(): string {\n const parent = fileURLToPath(new URL(\"../\", import.meta.url));\n if (fs.existsSync(path.join(parent, \"package.json\"))) return parent;\n const grandparent = fileURLToPath(new URL(\"../../\", import.meta.url));\n if (fs.existsSync(path.join(grandparent, \"package.json\"))) return grandparent;\n return parent;\n}\n\nlet cachedLinuxLibc: \"gnu\" | \"musl\" | undefined;\n\nfunction linuxLibcFamily(options: TargetOptions): \"gnu\" | \"musl\" {\n if (options.libc != null) return options.libc;\n if (options.report != null) {\n return options.report.header?.glibcVersionRuntime ? \"gnu\" : \"musl\";\n }\n if (cachedLinuxLibc != null) return cachedLinuxLibc;\n\n cachedLinuxLibc = detectLinuxLibc(fs);\n return cachedLinuxLibc;\n}\n\nexport function nativeTargetId(\n platform: NodeJS.Platform = process.platform,\n arch: string = process.arch,\n options: TargetOptions = {},\n): string {\n const target = `${platform}-${arch}`;\n if (platform !== \"linux\") return target;\n if (options.libc == null && options.report == null && platform !== process.platform) {\n return target;\n }\n return linuxLibcFamily(options) === \"musl\" ? `${target}-musl` : target;\n}\n\nexport function nativeBindingPath(options: NativeBindingPathOptions = {}): string {\n const platform = options.platform ?? process.platform;\n const arch = options.arch ?? process.arch;\n return path.join(\n options.packageRoot ?? packageRoot(),\n \"dist\",\n \"native\",\n nativeTargetId(platform, arch, options),\n \"tensorlake-node.node\",\n );\n}\n\nexport function nativePackageName(\n platform: NodeJS.Platform = process.platform,\n arch: string = process.arch,\n options: TargetOptions = {},\n): string | undefined {\n return NATIVE_PACKAGES[nativeTargetId(platform, arch, options)];\n}\n\nexport function loadNative<T>(): T {\n const target = nativeTargetId();\n const bindingPath = nativeBindingPath();\n const require = createRequire(import.meta.url);\n\n // Local source builds stage the addon here. Published packages intentionally\n // omit this directory and resolve the platform-specific optional dependency.\n if (fs.existsSync(bindingPath)) return require(bindingPath) as T;\n\n const packageName = nativePackageName();\n if (!packageName) {\n throw new Error(\n `Tensorlake does not provide a native binding for ${target}.`,\n );\n }\n\n let packageEntry: string;\n try {\n packageEntry = require.resolve(packageName);\n } catch (cause) {\n throw new Error(\n `Missing native binding package ${packageName} for ${target}. Reinstall @tensorlakeai/tensorlake without omitting optional dependencies, and install dependencies on the machine where they will run.`,\n { cause },\n );\n }\n return require(packageEntry) as T;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAKM,kBAOO;AAZb;AAAA;AAAA;AAKA,IAAM,mBAAmB,MACvB,OAAO,aAAa,cAChB,IAAI,IAAI,QAAQ,UAAU,EAAE,EAAE,OAC7B,SAAS,iBAAiB,SAAS,cAAc,QAAQ,YAAY,MAAM,WAC1E,SAAS,cAAc,MACvB,IAAI,IAAI,WAAW,SAAS,OAAO,EAAE;AAEtC,IAAM,gBAAgC,iCAAiB;AAAA;AAAA;;;ACZ9D;AAAA,iBAAAA,UAAAC,SAAA;AAAA;AAAA;AAGA,aAASC,iBAAgBC,KAAI,aAAa,QAAQ,UAAU;AAC1D,YAAM,WAAW,QAAQ,IAAI;AAC7B,UAAI,YAAY,MAAM;AACpB,YAAI,aAAa,SAAS,aAAa,OAAQ,QAAO;AACtD,cAAM,IAAI,MAAM,+CAA+C;AAAA,MACjE;AAEA,UAAI;AACJ,UAAI;AAEF,YAASC,QAAT,SAAc,MAAM,QAAQ;AAC1B,cAAI,CAAC,OAAO,cAAc,MAAM,KAAK,SAAS,GAAG;AAC/C,kBAAM,IAAI,MAAM,oBAAoB;AAAA,UACtC;AACA,gBAAM,SAAS,OAAO,MAAM,IAAI;AAChC,cAAIC,SAAQ;AACZ,iBAAOA,SAAQ,MAAM;AACnB,kBAAM,QAAQF,IAAG,SAAS,IAAI,QAAQE,QAAO,OAAOA,QAAO,SAASA,MAAK;AACzE,gBAAI,UAAU,EAA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