@tensorlakeai/tensorlake 0.5.151 → 0.5.152

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 (34) hide show
  1. package/README.md +8 -0
  2. package/dist/applications/index.cjs +7 -2
  3. package/dist/applications/index.cjs.map +1 -1
  4. package/dist/applications/index.js +7 -2
  5. package/dist/applications/index.js.map +1 -1
  6. package/dist/function-agent/main.cjs +1 -1
  7. package/dist/function-agent/main.cjs.map +1 -1
  8. package/dist/function-agent/main.js +1 -1
  9. package/dist/function-agent/main.js.map +1 -1
  10. package/dist/function-executor/main.cjs +2 -2
  11. package/dist/function-executor/main.cjs.map +1 -1
  12. package/dist/function-executor/main.js +2 -2
  13. package/dist/function-executor/main.js.map +1 -1
  14. package/dist/index.cjs +46 -189
  15. package/dist/index.cjs.map +1 -1
  16. package/dist/index.d.cts +2 -6
  17. package/dist/index.d.ts +2 -6
  18. package/dist/index.js +45 -188
  19. package/dist/index.js.map +1 -1
  20. package/dist/sandbox-image.cjs +7 -2
  21. package/dist/sandbox-image.cjs.map +1 -1
  22. package/dist/sandbox-image.js +7 -2
  23. package/dist/sandbox-image.js.map +1 -1
  24. package/package.json +8 -7
  25. package/runtime/function-executor/manifest.json +7 -7
  26. package/runtime/function-executor/package/dist/function-executor/main.js +2 -2
  27. package/runtime/function-executor/package/dist/function-executor/main.js.map +1 -1
  28. package/runtime/function-executor/package/npm-shrinkwrap.json +2 -2
  29. package/runtime/function-executor/package/package.json +1 -1
  30. package/runtime/typescript-function-runner/manifest.json +7 -7
  31. package/runtime/typescript-function-runner/package/dist/function-agent/main.js +1 -1
  32. package/runtime/typescript-function-runner/package/dist/function-agent/main.js.map +1 -1
  33. package/runtime/typescript-function-runner/package/npm-shrinkwrap.json +26 -26
  34. package/runtime/typescript-function-runner/package/package.json +7 -7
package/dist/index.d.cts CHANGED
@@ -1000,15 +1000,12 @@ interface TunnelAddress {
1000
1000
  port: number;
1001
1001
  }
1002
1002
  declare class TcpTunnel {
1003
+ private readonly native;
1003
1004
  readonly remotePort: number;
1004
1005
  readonly localHost: string;
1005
1006
  readonly localPort: number;
1006
- private readonly baseUrl;
1007
- private readonly wsHeaders;
1008
- private readonly server;
1009
- private readonly connectTimeoutMs;
1010
- private readonly activeRelays;
1011
1007
  private closePromise;
1008
+ private readonly lifetime;
1012
1009
  private constructor();
1013
1010
  static listen(options: {
1014
1011
  baseUrl: string;
@@ -1020,7 +1017,6 @@ declare class TcpTunnel {
1020
1017
  }): Promise<TcpTunnel>;
1021
1018
  address(): TunnelAddress;
1022
1019
  close(): Promise<void>;
1023
- private handleConnection;
1024
1020
  }
1025
1021
 
1026
1022
  type PtyDataHandler = (data: Uint8Array) => void;
package/dist/index.d.ts CHANGED
@@ -1000,15 +1000,12 @@ interface TunnelAddress {
1000
1000
  port: number;
1001
1001
  }
1002
1002
  declare class TcpTunnel {
1003
+ private readonly native;
1003
1004
  readonly remotePort: number;
1004
1005
  readonly localHost: string;
1005
1006
  readonly localPort: number;
1006
- private readonly baseUrl;
1007
- private readonly wsHeaders;
1008
- private readonly server;
1009
- private readonly connectTimeoutMs;
1010
- private readonly activeRelays;
1011
1007
  private closePromise;
1008
+ private readonly lifetime;
1012
1009
  private constructor();
1013
1010
  static listen(options: {
1014
1011
  baseUrl: string;
@@ -1020,7 +1017,6 @@ declare class TcpTunnel {
1020
1017
  }): Promise<TcpTunnel>;
1021
1018
  address(): TunnelAddress;
1022
1019
  close(): Promise<void>;
1023
- private handleConnection;
1024
1020
  }
1025
1021
 
1026
1022
  type PtyDataHandler = (data: Uint8Array) => void;
package/dist/index.js CHANGED
@@ -46,7 +46,7 @@ var init_defaults = __esm({
46
46
  "src/defaults.ts"() {
47
47
  "use strict";
48
48
  init_esm_shims();
49
- SDK_VERSION = "0.5.151";
49
+ SDK_VERSION = "0.5.152";
50
50
  API_URL = process.env.TENSORLAKE_API_URL ?? "https://api.tensorlake.ai";
51
51
  API_KEY = process.env.TENSORLAKE_API_KEY ?? void 0;
52
52
  NAMESPACE = process.env.INDEXIFY_NAMESPACE ?? "default";
@@ -313,6 +313,11 @@ function createWorkerBinding(runtime) {
313
313
  return runtime.handle("NativeRepositoryClient", args);
314
314
  }
315
315
  },
316
+ NativeTcpTunnel: class {
317
+ constructor() {
318
+ return runtime.handle("NativeTcpTunnel", []);
319
+ }
320
+ },
316
321
  validateManagedName: (name) => runtime.call(void 0, "validateManagedName", [name])
317
322
  };
318
323
  }
@@ -490,7 +495,7 @@ var init_native_worker_client = __esm({
490
495
  }
491
496
  };
492
497
  runtimeKey = /* @__PURE__ */ Symbol.for(
493
- `tensorlake.native.worker.${false ? "source" : "0.5.151"}.v1`
498
+ `tensorlake.native.worker.${false ? "source" : "0.5.152"}.v1`
494
499
  );
495
500
  runtimes = globalThis;
496
501
  }
@@ -862,7 +867,6 @@ var init_pending_sandbox = __esm({
862
867
  });
863
868
 
864
869
  // src/tunnel.ts
865
- import * as net from "net";
866
870
  import WebSocket from "ws";
867
871
  async function openTunnelWebSocket(options) {
868
872
  const wsUrl = buildTunnelWsUrl(options.baseUrl, options.remotePort);
@@ -940,116 +944,6 @@ function normalizeWebSocketData(message) {
940
944
  }
941
945
  return Buffer.from(message);
942
946
  }
943
- async function relaySocket(localSocket, websocket) {
944
- return new Promise((resolve2) => {
945
- let settled = false;
946
- const keepAliveInterval = setInterval(() => {
947
- if (websocket.readyState === WebSocket.OPEN) {
948
- websocket.ping();
949
- }
950
- }, WEBSOCKET_KEEPALIVE_INTERVAL_MS);
951
- keepAliveInterval.unref?.();
952
- const finish = () => {
953
- if (settled) return;
954
- settled = true;
955
- clearInterval(keepAliveInterval);
956
- cleanup();
957
- resolve2();
958
- };
959
- const cleanup = () => {
960
- localSocket.removeListener("data", onLocalData);
961
- localSocket.removeListener("end", onLocalEnd);
962
- localSocket.removeListener("close", onLocalClose);
963
- localSocket.removeListener("error", onLocalError);
964
- websocket.removeListener("message", onWsMessage);
965
- websocket.removeListener("close", onWsClose);
966
- websocket.removeListener("error", onWsError);
967
- websocket.removeListener("ping", onWsPing);
968
- };
969
- const onLocalData = (chunk) => {
970
- if (websocket.readyState !== WebSocket.OPEN) {
971
- localSocket.destroy();
972
- return;
973
- }
974
- websocket.send(chunk, { binary: true }, (error) => {
975
- if (error) {
976
- localSocket.destroy(error);
977
- }
978
- });
979
- };
980
- const onLocalEnd = () => {
981
- if (websocket.readyState === WebSocket.OPEN) {
982
- websocket.close();
983
- }
984
- };
985
- const onLocalClose = () => {
986
- if (websocket.readyState === WebSocket.OPEN || websocket.readyState === WebSocket.CONNECTING) {
987
- websocket.close();
988
- }
989
- finish();
990
- };
991
- const onLocalError = () => {
992
- websocket.close();
993
- finish();
994
- };
995
- const onWsMessage = (message, isBinary) => {
996
- if (!isBinary) {
997
- localSocket.destroy(
998
- new SandboxError("received unexpected text frame from tunnel")
999
- );
1000
- websocket.close();
1001
- return;
1002
- }
1003
- const payload = normalizeWebSocketData(message);
1004
- if (!localSocket.destroyed) {
1005
- localSocket.write(payload);
1006
- }
1007
- };
1008
- const onWsClose = () => {
1009
- if (!localSocket.destroyed) {
1010
- localSocket.end();
1011
- }
1012
- finish();
1013
- };
1014
- const onWsError = (error) => {
1015
- localSocket.destroy(error);
1016
- finish();
1017
- };
1018
- const onWsPing = (data) => {
1019
- if (websocket.readyState === WebSocket.OPEN) {
1020
- websocket.pong(data, false, () => {
1021
- });
1022
- }
1023
- };
1024
- localSocket.on("data", onLocalData);
1025
- localSocket.on("end", onLocalEnd);
1026
- localSocket.on("close", onLocalClose);
1027
- localSocket.on("error", onLocalError);
1028
- websocket.on("message", onWsMessage);
1029
- websocket.on("close", onWsClose);
1030
- websocket.on("error", onWsError);
1031
- websocket.on("ping", onWsPing);
1032
- });
1033
- }
1034
- async function listenServer(server, localPort, localHost) {
1035
- await new Promise((resolve2, reject) => {
1036
- const onError = (error) => {
1037
- server.removeListener("listening", onListening);
1038
- reject(
1039
- new SandboxError(
1040
- `failed to bind local tunnel listener on ${localHost}:${localPort}: ${error.message}`
1041
- )
1042
- );
1043
- };
1044
- const onListening = () => {
1045
- server.removeListener("error", onError);
1046
- resolve2();
1047
- };
1048
- server.once("error", onError);
1049
- server.once("listening", onListening);
1050
- server.listen(localPort, localHost);
1051
- });
1052
- }
1053
947
  function validatePort(port, label, allowZero = false) {
1054
948
  if (!Number.isInteger(port)) {
1055
949
  throw new SandboxError(`${label} must be an integer, got ${port}`);
@@ -1088,6 +982,8 @@ var init_tunnel = __esm({
1088
982
  "use strict";
1089
983
  init_esm_shims();
1090
984
  init_errors();
985
+ init_native_sandbox();
986
+ init_native_worker_client();
1091
987
  DEFAULT_TUNNEL_CONNECT_TIMEOUT_MS = 1e4;
1092
988
  WEBSOCKET_KEEPALIVE_INTERVAL_MS = 15e3;
1093
989
  TunnelByteStream = class _TunnelByteStream {
@@ -1217,24 +1113,17 @@ var init_tunnel = __esm({
1217
1113
  }
1218
1114
  };
1219
1115
  TcpTunnel = class _TcpTunnel {
1220
- remotePort;
1221
- localHost;
1222
- localPort;
1223
- baseUrl;
1224
- wsHeaders;
1225
- server;
1226
- connectTimeoutMs;
1227
- activeRelays = /* @__PURE__ */ new Set();
1228
- closePromise = null;
1229
- constructor(options) {
1230
- this.baseUrl = options.baseUrl;
1231
- this.wsHeaders = options.wsHeaders;
1232
- this.remotePort = options.remotePort;
1233
- this.localHost = options.localHost;
1234
- this.localPort = options.localPort;
1235
- this.server = options.server;
1236
- this.connectTimeoutMs = options.connectTimeoutMs;
1116
+ constructor(native, remotePort, localHost, localPort) {
1117
+ this.native = native;
1118
+ this.remotePort = remotePort;
1119
+ this.localHost = localHost;
1120
+ this.localPort = localPort;
1121
+ this.lifetime = native.waitClosed();
1122
+ void this.lifetime.catch(() => {
1123
+ });
1237
1124
  }
1125
+ closePromise = null;
1126
+ lifetime;
1238
1127
  static async listen(options) {
1239
1128
  const remotePort = validatePort(options.remotePort, "remote port");
1240
1129
  const localHost = options.localHost ?? "127.0.0.1";
@@ -1243,69 +1132,37 @@ var init_tunnel = __esm({
1243
1132
  "local port",
1244
1133
  true
1245
1134
  );
1246
- const connectTimeoutMs = secondsToMillis(options.connectTimeout ?? 10);
1247
- const server = net.createServer();
1248
- await listenServer(server, localPort, localHost);
1249
- const address = server.address();
1250
- if (!address || typeof address === "string") {
1251
- server.close();
1252
- throw new SandboxError("failed to determine bound tunnel address");
1253
- }
1254
- const tunnel = new _TcpTunnel({
1255
- baseUrl: options.baseUrl,
1256
- wsHeaders: options.wsHeaders,
1257
- remotePort,
1258
- localHost,
1259
- localPort: address.port,
1260
- server,
1261
- connectTimeoutMs
1262
- });
1263
- server.on("connection", (localSocket) => {
1264
- void tunnel.handleConnection(localSocket);
1265
- });
1266
- return tunnel;
1135
+ const timeout = secondsToMillis(options.connectTimeout ?? 10) / 1e3;
1136
+ const native = new (loadNativeSandboxBinding()).NativeTcpTunnel();
1137
+ try {
1138
+ const port = await callNative(
1139
+ () => native.listen(
1140
+ options.baseUrl,
1141
+ options.wsHeaders,
1142
+ remotePort,
1143
+ localHost,
1144
+ localPort,
1145
+ timeout
1146
+ )
1147
+ );
1148
+ return new _TcpTunnel(native, remotePort, localHost, port);
1149
+ } catch (error) {
1150
+ releaseNativeHandle(native);
1151
+ throw error;
1152
+ }
1267
1153
  }
1268
1154
  address() {
1269
1155
  return { host: this.localHost, port: this.localPort };
1270
1156
  }
1271
1157
  async close() {
1272
- if (this.closePromise) {
1273
- return this.closePromise;
1274
- }
1275
- for (const relay of this.activeRelays) {
1276
- relay.localSocket.destroy();
1277
- relay.websocket?.close();
1278
- }
1279
- this.closePromise = new Promise((resolve2, reject) => {
1280
- this.server.close((error) => {
1281
- if (error) {
1282
- reject(new SandboxError(`failed to close tunnel listener: ${error.message}`));
1283
- return;
1284
- }
1285
- resolve2();
1286
- });
1287
- });
1288
- return this.closePromise;
1289
- }
1290
- async handleConnection(localSocket) {
1291
- localSocket.setNoDelay(true);
1292
- const relay = { localSocket, websocket: null };
1293
- this.activeRelays.add(relay);
1294
- try {
1295
- relay.websocket = await openTunnelWebSocket({
1296
- baseUrl: this.baseUrl,
1297
- wsHeaders: this.wsHeaders,
1298
- remotePort: this.remotePort,
1299
- connectTimeoutMs: this.connectTimeoutMs
1300
- });
1301
- await relaySocket(localSocket, relay.websocket);
1302
- } catch (error) {
1303
- localSocket.destroy(
1304
- error instanceof Error ? error : new Error(String(error))
1305
- );
1306
- } finally {
1307
- this.activeRelays.delete(relay);
1308
- }
1158
+ return this.closePromise ??= (async () => {
1159
+ try {
1160
+ await callNative(() => this.native.close());
1161
+ await this.lifetime;
1162
+ } finally {
1163
+ releaseNativeHandle(this.native);
1164
+ }
1165
+ })();
1309
1166
  }
1310
1167
  };
1311
1168
  }