@declaw/sdk 1.1.0 → 1.1.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/CHANGELOG.md CHANGED
@@ -5,6 +5,26 @@ All notable changes to the Declaw TypeScript / JavaScript SDK are documented in
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [1.1.1]
9
+
10
+ ### Changed
11
+
12
+ - **Process-wide HTTP client cache.** `Sandbox.create` / `.connect` /
13
+ `.list`, `Volumes.*`, and `Template.*` now share a single `ApiClient`
14
+ per `(apiKey, apiUrl, requestTimeout)` via the new `getSharedClient`
15
+ helper instead of constructing a fresh instance per call. Matches the
16
+ symmetry added on the Python side and removes unnecessary
17
+ `AbortController` churn. Node's global fetch / undici dispatcher was
18
+ already pooling TCP + TLS under the hood, so end-to-end latency is
19
+ unchanged for most callers — the user-visible win is that
20
+ `close()` on a returned `Sandbox` is now a no-op rather than
21
+ aborting an in-flight request on the shared client.
22
+ - New exports from the package root: `getSharedClient`,
23
+ `resetSharedClients`. Use `resetSharedClients()` from tests or
24
+ long-running services that want to force a full teardown.
25
+ - `Sandbox.close()` is now a no-op. `Sandbox.kill()` still kills the
26
+ VM and is unchanged.
27
+
8
28
  ## [1.1.0]
9
29
 
10
30
  ### Added
package/dist/index.cjs CHANGED
@@ -69,6 +69,7 @@ __export(index_exports, {
69
69
  createToxicityConfig: () => createToxicityConfig,
70
70
  createTransformationRule: () => createTransformationRule,
71
71
  domainMatches: () => domainMatches,
72
+ getSharedClient: () => getSharedClient,
72
73
  invisibleTextConfigToJSON: () => invisibleTextConfigToJSON,
73
74
  isSensitive: () => isSensitive,
74
75
  networkPolicyToOpts: () => networkPolicyToOpts,
@@ -96,6 +97,7 @@ __export(index_exports, {
96
97
  parseVolumeInfo: () => parseVolumeInfo,
97
98
  parseWriteInfo: () => parseWriteInfo,
98
99
  requiresTlsInterception: () => requiresTlsInterception,
100
+ resetSharedClients: () => resetSharedClients,
99
101
  securityPolicyToJSON: () => securityPolicyToJSON,
100
102
  toxicityConfigToJSON: () => toxicityConfigToJSON,
101
103
  validateNetworkEntry: () => validateNetworkEntry,
@@ -276,6 +278,12 @@ var ApiClient = class {
276
278
  }
277
279
  /**
278
280
  * Abort all in-flight requests and release resources.
281
+ *
282
+ * Since 1.1.1 the SDK maintains a process-wide shared ApiClient cache
283
+ * for hot class-method paths (Sandbox.create, Volumes.*, Template.*).
284
+ * Calling `close()` on a shared instance aborts its AbortController,
285
+ * which would cancel any concurrent in-flight call. Prefer
286
+ * `resetSharedClients()` if you want to tear down every cached client.
279
287
  */
280
288
  close() {
281
289
  this.abortController.abort();
@@ -395,6 +403,28 @@ var ApiClient = class {
395
403
  await new Promise((resolve) => setTimeout(resolve, ms));
396
404
  }
397
405
  };
406
+ var _sharedClients = /* @__PURE__ */ new Map();
407
+ function sharedKey(config) {
408
+ return [config.apiKey ?? "", config.apiUrl, config.requestTimeout ?? ""].join("|");
409
+ }
410
+ function getSharedClient(config) {
411
+ const key = sharedKey(config);
412
+ let client = _sharedClients.get(key);
413
+ if (!client) {
414
+ client = new ApiClient(config);
415
+ _sharedClients.set(key, client);
416
+ }
417
+ return client;
418
+ }
419
+ function resetSharedClients() {
420
+ for (const client of _sharedClients.values()) {
421
+ try {
422
+ client.close();
423
+ } catch {
424
+ }
425
+ }
426
+ _sharedClients.clear();
427
+ }
398
428
 
399
429
  // src/sandbox/network.ts
400
430
  var ALL_TRAFFIC = "0.0.0.0/0";
@@ -1944,54 +1974,49 @@ var Sandbox = class _Sandbox {
1944
1974
  apiUrl: opts?.apiUrl,
1945
1975
  requestTimeout: opts?.requestTimeout
1946
1976
  });
1947
- const client = new ApiClient(config);
1948
- try {
1949
- const body = {
1950
- template: opts?.template ?? DEFAULT_TEMPLATE,
1951
- timeout: opts?.timeout ?? DEFAULT_TIMEOUT,
1952
- secure: opts?.secure ?? true
1953
- };
1954
- if (opts?.metadata) {
1955
- body.metadata = opts.metadata;
1956
- }
1957
- if (opts?.envs) {
1958
- body.envs = opts.envs;
1959
- }
1960
- if (opts?.network) {
1961
- body.network = networkOptsToJSON(opts.network);
1962
- } else if (opts?.allowInternetAccess === false) {
1963
- body.network = { deny_out: [ALL_TRAFFIC] };
1964
- }
1965
- if (opts?.security) {
1966
- body.security = { policy_json: JSON.stringify(securityPolicyToJSON(opts.security)) };
1967
- if (opts.security.network && !body.network) {
1968
- body.network = typeof opts.security.network === "object" && "allowOut" in opts.security.network ? networkOptsToJSON(opts.security.network) : opts.security.network;
1969
- }
1970
- }
1971
- if (opts?.lifecycle) {
1972
- body.lifecycle = lifecycleToJSON(opts.lifecycle);
1973
- }
1974
- if (opts?.volumes && opts.volumes.length > 0) {
1975
- body.volumes = opts.volumes.map(volumeAttachmentToJSON);
1977
+ const client = getSharedClient(config);
1978
+ const body = {
1979
+ template: opts?.template ?? DEFAULT_TEMPLATE,
1980
+ timeout: opts?.timeout ?? DEFAULT_TIMEOUT,
1981
+ secure: opts?.secure ?? true
1982
+ };
1983
+ if (opts?.metadata) {
1984
+ body.metadata = opts.metadata;
1985
+ }
1986
+ if (opts?.envs) {
1987
+ body.envs = opts.envs;
1988
+ }
1989
+ if (opts?.network) {
1990
+ body.network = networkOptsToJSON(opts.network);
1991
+ } else if (opts?.allowInternetAccess === false) {
1992
+ body.network = { deny_out: [ALL_TRAFFIC] };
1993
+ }
1994
+ if (opts?.security) {
1995
+ body.security = { policy_json: JSON.stringify(securityPolicyToJSON(opts.security)) };
1996
+ if (opts.security.network && !body.network) {
1997
+ body.network = typeof opts.security.network === "object" && "allowOut" in opts.security.network ? networkOptsToJSON(opts.security.network) : opts.security.network;
1976
1998
  }
1977
- const data = await client.post("/sandboxes", {
1978
- json: body,
1979
- timeout: opts?.requestTimeout
1980
- });
1981
- const sandboxId = data.sandbox_id;
1982
- assertValidId(sandboxId, "sandbox ID (from server)");
1983
- return new _Sandbox(
1984
- sandboxId,
1985
- config,
1986
- client,
1987
- data.envd_access_token,
1988
- data.sandbox_domain,
1989
- data.traffic_access_token
1990
- );
1991
- } catch (error) {
1992
- client.close();
1993
- throw error;
1994
1999
  }
2000
+ if (opts?.lifecycle) {
2001
+ body.lifecycle = lifecycleToJSON(opts.lifecycle);
2002
+ }
2003
+ if (opts?.volumes && opts.volumes.length > 0) {
2004
+ body.volumes = opts.volumes.map(volumeAttachmentToJSON);
2005
+ }
2006
+ const data = await client.post("/sandboxes", {
2007
+ json: body,
2008
+ timeout: opts?.requestTimeout
2009
+ });
2010
+ const sandboxId = data.sandbox_id;
2011
+ assertValidId(sandboxId, "sandbox ID (from server)");
2012
+ return new _Sandbox(
2013
+ sandboxId,
2014
+ config,
2015
+ client,
2016
+ data.envd_access_token,
2017
+ data.sandbox_domain,
2018
+ data.traffic_access_token
2019
+ );
1995
2020
  }
1996
2021
  /**
1997
2022
  * Connect to an existing sandbox.
@@ -2008,23 +2033,18 @@ var Sandbox = class _Sandbox {
2008
2033
  apiUrl: opts?.apiUrl,
2009
2034
  requestTimeout: opts?.requestTimeout
2010
2035
  });
2011
- const client = new ApiClient(config);
2012
- try {
2013
- const data = await client.get(`/sandboxes/${sandboxId}`, {
2014
- timeout: opts?.requestTimeout
2015
- });
2016
- return new _Sandbox(
2017
- data.sandbox_id,
2018
- config,
2019
- client,
2020
- data.envd_access_token,
2021
- data.sandbox_domain,
2022
- data.traffic_access_token
2023
- );
2024
- } catch (error) {
2025
- client.close();
2026
- throw error;
2027
- }
2036
+ const client = getSharedClient(config);
2037
+ const data = await client.get(`/sandboxes/${sandboxId}`, {
2038
+ timeout: opts?.requestTimeout
2039
+ });
2040
+ return new _Sandbox(
2041
+ data.sandbox_id,
2042
+ config,
2043
+ client,
2044
+ data.envd_access_token,
2045
+ data.sandbox_domain,
2046
+ data.traffic_access_token
2047
+ );
2028
2048
  }
2029
2049
  /**
2030
2050
  * List sandboxes.
@@ -2041,35 +2061,31 @@ var Sandbox = class _Sandbox {
2041
2061
  apiUrl: opts?.apiUrl,
2042
2062
  requestTimeout: opts?.requestTimeout
2043
2063
  });
2044
- const client = new ApiClient(config);
2045
- try {
2046
- const params = {};
2047
- if (opts?.query) {
2048
- for (const [key, value] of Object.entries(opts.query)) {
2049
- params[key] = value;
2050
- }
2051
- }
2052
- if (opts?.limit !== void 0) {
2053
- params.limit = String(opts.limit);
2054
- }
2055
- if (opts?.nextToken) {
2056
- params.next_token = opts.nextToken;
2064
+ const client = getSharedClient(config);
2065
+ const params = {};
2066
+ if (opts?.query) {
2067
+ for (const [key, value] of Object.entries(opts.query)) {
2068
+ params[key] = value;
2057
2069
  }
2058
- const data = await client.get("/sandboxes", {
2059
- params,
2060
- timeout: opts?.requestTimeout
2061
- });
2062
- const rawSandboxes = data.sandboxes ?? [];
2063
- const sandboxes = rawSandboxes.map(
2064
- (s) => parseSandboxInfo(s)
2065
- );
2066
- return {
2067
- sandboxes,
2068
- nextToken: data.next_token ?? void 0
2069
- };
2070
- } finally {
2071
- client.close();
2072
2070
  }
2071
+ if (opts?.limit !== void 0) {
2072
+ params.limit = String(opts.limit);
2073
+ }
2074
+ if (opts?.nextToken) {
2075
+ params.next_token = opts.nextToken;
2076
+ }
2077
+ const data = await client.get("/sandboxes", {
2078
+ params,
2079
+ timeout: opts?.requestTimeout
2080
+ });
2081
+ const rawSandboxes = data.sandboxes ?? [];
2082
+ const sandboxes = rawSandboxes.map(
2083
+ (s) => parseSandboxInfo(s)
2084
+ );
2085
+ return {
2086
+ sandboxes,
2087
+ nextToken: data.next_token ?? void 0
2088
+ };
2073
2089
  }
2074
2090
  /**
2075
2091
  * Kill this sandbox.
@@ -2221,33 +2237,31 @@ var Sandbox = class _Sandbox {
2221
2237
  apiUrl: options.apiUrl,
2222
2238
  requestTimeout: options.requestTimeout
2223
2239
  });
2224
- const client = new ApiClient(config);
2225
- try {
2226
- const query = options.snapshotId ? `?snapshot_id=${encodeURIComponent(options.snapshotId)}` : "";
2227
- await client.post(`/sandboxes/${sandboxId}/restore${query}`, {
2228
- timeout: options.requestTimeout
2229
- });
2230
- const data = await client.get(`/sandboxes/${sandboxId}`, {
2231
- timeout: options.requestTimeout
2232
- });
2233
- return new _Sandbox(
2234
- data.sandbox_id,
2235
- config,
2236
- client,
2237
- data.envd_access_token,
2238
- data.sandbox_domain,
2239
- data.traffic_access_token
2240
- );
2241
- } catch (error) {
2242
- client.close();
2243
- throw error;
2244
- }
2240
+ const client = getSharedClient(config);
2241
+ const query = options.snapshotId ? `?snapshot_id=${encodeURIComponent(options.snapshotId)}` : "";
2242
+ await client.post(`/sandboxes/${sandboxId}/restore${query}`, {
2243
+ timeout: options.requestTimeout
2244
+ });
2245
+ const data = await client.get(`/sandboxes/${sandboxId}`, {
2246
+ timeout: options.requestTimeout
2247
+ });
2248
+ return new _Sandbox(
2249
+ data.sandbox_id,
2250
+ config,
2251
+ client,
2252
+ data.envd_access_token,
2253
+ data.sandbox_domain,
2254
+ data.traffic_access_token
2255
+ );
2245
2256
  }
2246
2257
  /**
2247
- * Close the underlying HTTP client and release resources.
2258
+ * Historically closed the sandbox's HTTP client; since 1.1.1 the SDK
2259
+ * maintains a process-wide shared ApiClient (see `getSharedClient` in
2260
+ * api/client.ts) so per-sandbox close no longer tears the connection
2261
+ * pool down. Kept as a no-op for backwards compatibility — call
2262
+ * `resetSharedClients()` if you want to force a full teardown.
2248
2263
  */
2249
2264
  close() {
2250
- this._client.close();
2251
2265
  }
2252
2266
  /**
2253
2267
  * Support `await using sandbox = await Sandbox.create(...)` for automatic cleanup.
@@ -2477,36 +2491,32 @@ var Template = class {
2477
2491
  domain: opts?.domain,
2478
2492
  requestTimeout: opts?.requestTimeout
2479
2493
  });
2480
- const client = new ApiClient(config);
2481
- try {
2482
- const body = {
2483
- template: template.toJSON(),
2484
- alias
2485
- };
2486
- if (opts?.cpuCount !== void 0) {
2487
- body.cpu_count = opts.cpuCount;
2488
- }
2489
- if (opts?.memoryMb !== void 0) {
2490
- body.memory_mb = opts.memoryMb;
2491
- }
2492
- if (opts?.diskMb !== void 0) {
2493
- body.disk_mb = opts.diskMb;
2494
- }
2495
- const data = await client.post("/templates/build", {
2496
- json: body,
2497
- timeout: opts?.requestTimeout
2498
- });
2499
- const response = data;
2500
- const result = parseBuildInfo(response);
2501
- if (opts?.onBuildLogs && Array.isArray(response.logs)) {
2502
- for (const log of response.logs) {
2503
- opts.onBuildLogs(log);
2504
- }
2494
+ const client = getSharedClient(config);
2495
+ const body = {
2496
+ template: template.toJSON(),
2497
+ alias
2498
+ };
2499
+ if (opts?.cpuCount !== void 0) {
2500
+ body.cpu_count = opts.cpuCount;
2501
+ }
2502
+ if (opts?.memoryMb !== void 0) {
2503
+ body.memory_mb = opts.memoryMb;
2504
+ }
2505
+ if (opts?.diskMb !== void 0) {
2506
+ body.disk_mb = opts.diskMb;
2507
+ }
2508
+ const data = await client.post("/templates/build", {
2509
+ json: body,
2510
+ timeout: opts?.requestTimeout
2511
+ });
2512
+ const response = data;
2513
+ const result = parseBuildInfo(response);
2514
+ if (opts?.onBuildLogs && Array.isArray(response.logs)) {
2515
+ for (const log of response.logs) {
2516
+ opts.onBuildLogs(log);
2505
2517
  }
2506
- return result;
2507
- } finally {
2508
- client.close();
2509
2518
  }
2519
+ return result;
2510
2520
  }
2511
2521
  /**
2512
2522
  * Start a template build in the background.
@@ -2519,30 +2529,26 @@ var Template = class {
2519
2529
  domain: opts?.domain,
2520
2530
  requestTimeout: opts?.requestTimeout
2521
2531
  });
2522
- const client = new ApiClient(config);
2523
- try {
2524
- const body = {
2525
- template: template.toJSON(),
2526
- alias,
2527
- background: true
2528
- };
2529
- if (opts?.cpuCount !== void 0) {
2530
- body.cpu_count = opts.cpuCount;
2531
- }
2532
- if (opts?.memoryMb !== void 0) {
2533
- body.memory_mb = opts.memoryMb;
2534
- }
2535
- if (opts?.diskMb !== void 0) {
2536
- body.disk_mb = opts.diskMb;
2537
- }
2538
- const data = await client.post("/templates/build", {
2539
- json: body,
2540
- timeout: opts?.requestTimeout
2541
- });
2542
- return parseBuildInfo(data);
2543
- } finally {
2544
- client.close();
2532
+ const client = getSharedClient(config);
2533
+ const body = {
2534
+ template: template.toJSON(),
2535
+ alias,
2536
+ background: true
2537
+ };
2538
+ if (opts?.cpuCount !== void 0) {
2539
+ body.cpu_count = opts.cpuCount;
2545
2540
  }
2541
+ if (opts?.memoryMb !== void 0) {
2542
+ body.memory_mb = opts.memoryMb;
2543
+ }
2544
+ if (opts?.diskMb !== void 0) {
2545
+ body.disk_mb = opts.diskMb;
2546
+ }
2547
+ const data = await client.post("/templates/build", {
2548
+ json: body,
2549
+ timeout: opts?.requestTimeout
2550
+ });
2551
+ return parseBuildInfo(data);
2546
2552
  }
2547
2553
  /**
2548
2554
  * Get the status of a template build.
@@ -2556,15 +2562,11 @@ var Template = class {
2556
2562
  domain: opts?.domain,
2557
2563
  requestTimeout: opts?.requestTimeout
2558
2564
  });
2559
- const client = new ApiClient(config);
2560
- try {
2561
- const data = await client.get(`/templates/builds/${buildId}`, {
2562
- timeout: opts?.requestTimeout
2563
- });
2564
- return parseTemplateBuildStatus(data);
2565
- } finally {
2566
- client.close();
2567
- }
2565
+ const client = getSharedClient(config);
2566
+ const data = await client.get(`/templates/builds/${buildId}`, {
2567
+ timeout: opts?.requestTimeout
2568
+ });
2569
+ return parseTemplateBuildStatus(data);
2568
2570
  }
2569
2571
  };
2570
2572
 
@@ -2589,19 +2591,15 @@ var Volumes = class {
2589
2591
  apiUrl: opts?.apiUrl,
2590
2592
  requestTimeout: opts?.requestTimeout
2591
2593
  });
2592
- const client = new ApiClient(config);
2593
- try {
2594
- const body = data instanceof Uint8Array ? data : new Uint8Array(data);
2595
- const resp = await client.post("/volumes", {
2596
- params: { name },
2597
- body,
2598
- headers: { "Content-Type": opts?.contentType ?? "application/gzip" },
2599
- timeout: opts?.requestTimeout
2600
- });
2601
- return parseVolumeInfo(resp);
2602
- } finally {
2603
- client.close();
2604
- }
2594
+ const client = getSharedClient(config);
2595
+ const body = data instanceof Uint8Array ? data : new Uint8Array(data);
2596
+ const resp = await client.post("/volumes", {
2597
+ params: { name },
2598
+ body,
2599
+ headers: { "Content-Type": opts?.contentType ?? "application/gzip" },
2600
+ timeout: opts?.requestTimeout
2601
+ });
2602
+ return parseVolumeInfo(resp);
2605
2603
  }
2606
2604
  /** Fetch metadata for a single volume. */
2607
2605
  static async get(volumeId, opts) {
@@ -2612,15 +2610,11 @@ var Volumes = class {
2612
2610
  apiUrl: opts?.apiUrl,
2613
2611
  requestTimeout: opts?.requestTimeout
2614
2612
  });
2615
- const client = new ApiClient(config);
2616
- try {
2617
- const resp = await client.get(`/volumes/${volumeId}`, {
2618
- timeout: opts?.requestTimeout
2619
- });
2620
- return parseVolumeInfo(resp);
2621
- } finally {
2622
- client.close();
2623
- }
2613
+ const client = getSharedClient(config);
2614
+ const resp = await client.get(`/volumes/${volumeId}`, {
2615
+ timeout: opts?.requestTimeout
2616
+ });
2617
+ return parseVolumeInfo(resp);
2624
2618
  }
2625
2619
  /** List all volumes owned by the caller, newest first. */
2626
2620
  static async list(opts) {
@@ -2630,16 +2624,12 @@ var Volumes = class {
2630
2624
  apiUrl: opts?.apiUrl,
2631
2625
  requestTimeout: opts?.requestTimeout
2632
2626
  });
2633
- const client = new ApiClient(config);
2634
- try {
2635
- const resp = await client.get("/volumes", {
2636
- timeout: opts?.requestTimeout
2637
- });
2638
- const rows = resp.volumes ?? [];
2639
- return rows.map(parseVolumeInfo);
2640
- } finally {
2641
- client.close();
2642
- }
2627
+ const client = getSharedClient(config);
2628
+ const resp = await client.get("/volumes", {
2629
+ timeout: opts?.requestTimeout
2630
+ });
2631
+ const rows = resp.volumes ?? [];
2632
+ return rows.map(parseVolumeInfo);
2643
2633
  }
2644
2634
  /** Delete a volume and its blob. Idempotent on the wire. */
2645
2635
  static async delete(volumeId, opts) {
@@ -2650,12 +2640,8 @@ var Volumes = class {
2650
2640
  apiUrl: opts?.apiUrl,
2651
2641
  requestTimeout: opts?.requestTimeout
2652
2642
  });
2653
- const client = new ApiClient(config);
2654
- try {
2655
- await client.delete(`/volumes/${volumeId}`, { timeout: opts?.requestTimeout });
2656
- } finally {
2657
- client.close();
2658
- }
2643
+ const client = getSharedClient(config);
2644
+ await client.delete(`/volumes/${volumeId}`, { timeout: opts?.requestTimeout });
2659
2645
  }
2660
2646
  };
2661
2647
  // Annotate the CommonJS export names for ESM import in node:
@@ -2709,6 +2695,7 @@ var Volumes = class {
2709
2695
  createToxicityConfig,
2710
2696
  createTransformationRule,
2711
2697
  domainMatches,
2698
+ getSharedClient,
2712
2699
  invisibleTextConfigToJSON,
2713
2700
  isSensitive,
2714
2701
  networkPolicyToOpts,
@@ -2736,6 +2723,7 @@ var Volumes = class {
2736
2723
  parseVolumeInfo,
2737
2724
  parseWriteInfo,
2738
2725
  requiresTlsInterception,
2726
+ resetSharedClients,
2739
2727
  securityPolicyToJSON,
2740
2728
  toxicityConfigToJSON,
2741
2729
  validateNetworkEntry,