modal 0.6.1 → 0.6.2

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/dist/index.cjs CHANGED
@@ -46484,6 +46484,7 @@ var Queue = class _Queue {
46484
46484
 
46485
46485
  // src/sandbox.ts
46486
46486
  var import_nice_grpc8 = require("nice-grpc");
46487
+ var import_promises = require("timers/promises");
46487
46488
 
46488
46489
  // src/sandbox_filesystem.ts
46489
46490
  var SandboxFile = class {
@@ -46667,7 +46668,7 @@ var writeMixin = {
46667
46668
  }
46668
46669
  }
46669
46670
  };
46670
- function streamConsumingIter(iterable) {
46671
+ function streamConsumingIter(iterable, onCancel) {
46671
46672
  const iter = iterable[Symbol.asyncIterator]();
46672
46673
  return new ReadableStream(
46673
46674
  {
@@ -46681,7 +46682,13 @@ function streamConsumingIter(iterable) {
46681
46682
  }
46682
46683
  },
46683
46684
  async cancel() {
46684
- consumeIterator(iter);
46685
+ try {
46686
+ onCancel?.();
46687
+ } finally {
46688
+ if (typeof iter.return === "function") {
46689
+ await iter.return();
46690
+ }
46691
+ }
46685
46692
  }
46686
46693
  },
46687
46694
  new ByteLengthQueuingStrategy({
@@ -46690,14 +46697,11 @@ function streamConsumingIter(iterable) {
46690
46697
  })
46691
46698
  );
46692
46699
  }
46693
- async function consumeIterator(iter) {
46694
- while (true) {
46695
- const { done } = await iter.next();
46696
- if (done) break;
46697
- }
46698
- }
46699
46700
 
46700
46701
  // src/sandbox.ts
46702
+ var SB_LOGS_INITIAL_DELAY_MS = 10;
46703
+ var SB_LOGS_DELAY_FACTOR = 2;
46704
+ var SB_LOGS_MAX_RETRIES = 10;
46701
46705
  async function buildSandboxCreateRequestProto(appId, imageId, params = {}) {
46702
46706
  checkForRenamedParams(params, {
46703
46707
  memory: "memoryMiB",
@@ -47073,34 +47077,61 @@ async function buildContainerExecRequestProto(taskId, command, params) {
47073
47077
  var Sandbox2 = class _Sandbox {
47074
47078
  #client;
47075
47079
  sandboxId;
47076
- stdin;
47077
- stdout;
47078
- stderr;
47080
+ #stdin;
47081
+ #stdout;
47082
+ #stderr;
47083
+ #stdoutAbort;
47084
+ #stderrAbort;
47079
47085
  #taskId;
47080
47086
  #tunnels;
47081
47087
  /** @ignore */
47082
47088
  constructor(client2, sandboxId) {
47083
47089
  this.#client = client2;
47084
47090
  this.sandboxId = sandboxId;
47085
- this.stdin = toModalWriteStream(inputStreamSb(client2.cpClient, sandboxId));
47086
- this.stdout = toModalReadStream(
47087
- streamConsumingIter(
47091
+ }
47092
+ get stdin() {
47093
+ if (!this.#stdin) {
47094
+ this.#stdin = toModalWriteStream(
47095
+ inputStreamSb(this.#client.cpClient, this.sandboxId)
47096
+ );
47097
+ }
47098
+ return this.#stdin;
47099
+ }
47100
+ get stdout() {
47101
+ if (!this.#stdout) {
47102
+ this.#stdoutAbort = new AbortController();
47103
+ const bytesStream = streamConsumingIter(
47088
47104
  outputStreamSb(
47089
- client2.cpClient,
47090
- sandboxId,
47091
- 1 /* FILE_DESCRIPTOR_STDOUT */
47092
- )
47093
- ).pipeThrough(new TextDecoderStream())
47094
- );
47095
- this.stderr = toModalReadStream(
47096
- streamConsumingIter(
47105
+ this.#client.cpClient,
47106
+ this.sandboxId,
47107
+ 1 /* FILE_DESCRIPTOR_STDOUT */,
47108
+ this.#stdoutAbort.signal
47109
+ ),
47110
+ () => this.#stdoutAbort?.abort()
47111
+ );
47112
+ this.#stdout = toModalReadStream(
47113
+ bytesStream.pipeThrough(new TextDecoderStream())
47114
+ );
47115
+ }
47116
+ return this.#stdout;
47117
+ }
47118
+ get stderr() {
47119
+ if (!this.#stderr) {
47120
+ this.#stderrAbort = new AbortController();
47121
+ const bytesStream = streamConsumingIter(
47097
47122
  outputStreamSb(
47098
- client2.cpClient,
47099
- sandboxId,
47100
- 2 /* FILE_DESCRIPTOR_STDERR */
47101
- )
47102
- ).pipeThrough(new TextDecoderStream())
47103
- );
47123
+ this.#client.cpClient,
47124
+ this.sandboxId,
47125
+ 2 /* FILE_DESCRIPTOR_STDERR */,
47126
+ this.#stderrAbort.signal
47127
+ ),
47128
+ () => this.#stderrAbort?.abort()
47129
+ );
47130
+ this.#stderr = toModalReadStream(
47131
+ bytesStream.pipeThrough(new TextDecoderStream())
47132
+ );
47133
+ }
47134
+ return this.#stderr;
47104
47135
  }
47105
47136
  /** Set tags (key-value pairs) on the Sandbox. Tags can be used to filter results in {@link SandboxService#list Sandbox.list}. */
47106
47137
  async setTags(tags) {
@@ -47399,19 +47430,25 @@ var ContainerProcess = class {
47399
47430
  }
47400
47431
  }
47401
47432
  };
47402
- async function* outputStreamSb(cpClient, sandboxId, fileDescriptor) {
47433
+ async function* outputStreamSb(cpClient, sandboxId, fileDescriptor, signal) {
47403
47434
  let lastIndex = "0-0";
47404
47435
  let completed = false;
47405
- let retries = 10;
47436
+ let retriesRemaining = SB_LOGS_MAX_RETRIES;
47437
+ let delayMs = SB_LOGS_INITIAL_DELAY_MS;
47406
47438
  while (!completed) {
47407
47439
  try {
47408
- const outputIterator = cpClient.sandboxGetLogs({
47409
- sandboxId,
47410
- fileDescriptor,
47411
- timeout: 55,
47412
- lastEntryId: lastIndex
47413
- });
47440
+ const outputIterator = cpClient.sandboxGetLogs(
47441
+ {
47442
+ sandboxId,
47443
+ fileDescriptor,
47444
+ timeout: 55,
47445
+ lastEntryId: lastIndex
47446
+ },
47447
+ { signal }
47448
+ );
47414
47449
  for await (const batch of outputIterator) {
47450
+ delayMs = SB_LOGS_INITIAL_DELAY_MS;
47451
+ retriesRemaining = SB_LOGS_MAX_RETRIES;
47415
47452
  lastIndex = batch.entryId;
47416
47453
  yield* batch.items.map((item) => new TextEncoder().encode(item.data));
47417
47454
  if (batch.eof) {
@@ -47420,8 +47457,21 @@ async function* outputStreamSb(cpClient, sandboxId, fileDescriptor) {
47420
47457
  }
47421
47458
  }
47422
47459
  } catch (err) {
47423
- if (isRetryableGrpc(err) && retries > 0) retries--;
47424
- else throw err;
47460
+ if (signal?.aborted) {
47461
+ return;
47462
+ }
47463
+ if (isRetryableGrpc(err) && retriesRemaining > 0) {
47464
+ try {
47465
+ await (0, import_promises.setTimeout)(delayMs, void 0, { signal });
47466
+ } catch {
47467
+ return;
47468
+ }
47469
+ delayMs *= SB_LOGS_DELAY_FACTOR;
47470
+ retriesRemaining--;
47471
+ continue;
47472
+ } else {
47473
+ throw err;
47474
+ }
47425
47475
  }
47426
47476
  }
47427
47477
  }
@@ -47826,7 +47876,7 @@ var AuthTokenManager = class {
47826
47876
 
47827
47877
  // src/version.ts
47828
47878
  function getSDKVersion() {
47829
- return true ? "0.6.1" : "0.0.0";
47879
+ return true ? "0.6.2" : "0.0.0";
47830
47880
  }
47831
47881
 
47832
47882
  // src/logger.ts
package/dist/index.d.cts CHANGED
@@ -5937,11 +5937,11 @@ declare class Tunnel {
5937
5937
  declare class Sandbox {
5938
5938
  #private;
5939
5939
  readonly sandboxId: string;
5940
- stdin: ModalWriteStream<string>;
5941
- stdout: ModalReadStream<string>;
5942
- stderr: ModalReadStream<string>;
5943
5940
  /** @ignore */
5944
5941
  constructor(client: ModalClient, sandboxId: string);
5942
+ get stdin(): ModalWriteStream<string>;
5943
+ get stdout(): ModalReadStream<string>;
5944
+ get stderr(): ModalReadStream<string>;
5945
5945
  /** Set tags (key-value pairs) on the Sandbox. Tags can be used to filter results in {@link SandboxService#list Sandbox.list}. */
5946
5946
  setTags(tags: Record<string, string>): Promise<void>;
5947
5947
  /** Get tags (key-value pairs) currently attached to this Sandbox from the server. */
package/dist/index.d.ts CHANGED
@@ -5937,11 +5937,11 @@ declare class Tunnel {
5937
5937
  declare class Sandbox {
5938
5938
  #private;
5939
5939
  readonly sandboxId: string;
5940
- stdin: ModalWriteStream<string>;
5941
- stdout: ModalReadStream<string>;
5942
- stderr: ModalReadStream<string>;
5943
5940
  /** @ignore */
5944
5941
  constructor(client: ModalClient, sandboxId: string);
5942
+ get stdin(): ModalWriteStream<string>;
5943
+ get stdout(): ModalReadStream<string>;
5944
+ get stderr(): ModalReadStream<string>;
5945
5945
  /** Set tags (key-value pairs) on the Sandbox. Tags can be used to filter results in {@link SandboxService#list Sandbox.list}. */
5946
5946
  setTags(tags: Record<string, string>): Promise<void>;
5947
5947
  /** Get tags (key-value pairs) currently attached to this Sandbox from the server. */
package/dist/index.js CHANGED
@@ -46416,6 +46416,7 @@ var Queue = class _Queue {
46416
46416
 
46417
46417
  // src/sandbox.ts
46418
46418
  import { ClientError as ClientError7, Status as Status7 } from "nice-grpc";
46419
+ import { setTimeout as setTimeout2 } from "timers/promises";
46419
46420
 
46420
46421
  // src/sandbox_filesystem.ts
46421
46422
  var SandboxFile = class {
@@ -46599,7 +46600,7 @@ var writeMixin = {
46599
46600
  }
46600
46601
  }
46601
46602
  };
46602
- function streamConsumingIter(iterable) {
46603
+ function streamConsumingIter(iterable, onCancel) {
46603
46604
  const iter = iterable[Symbol.asyncIterator]();
46604
46605
  return new ReadableStream(
46605
46606
  {
@@ -46613,7 +46614,13 @@ function streamConsumingIter(iterable) {
46613
46614
  }
46614
46615
  },
46615
46616
  async cancel() {
46616
- consumeIterator(iter);
46617
+ try {
46618
+ onCancel?.();
46619
+ } finally {
46620
+ if (typeof iter.return === "function") {
46621
+ await iter.return();
46622
+ }
46623
+ }
46617
46624
  }
46618
46625
  },
46619
46626
  new ByteLengthQueuingStrategy({
@@ -46622,14 +46629,11 @@ function streamConsumingIter(iterable) {
46622
46629
  })
46623
46630
  );
46624
46631
  }
46625
- async function consumeIterator(iter) {
46626
- while (true) {
46627
- const { done } = await iter.next();
46628
- if (done) break;
46629
- }
46630
- }
46631
46632
 
46632
46633
  // src/sandbox.ts
46634
+ var SB_LOGS_INITIAL_DELAY_MS = 10;
46635
+ var SB_LOGS_DELAY_FACTOR = 2;
46636
+ var SB_LOGS_MAX_RETRIES = 10;
46633
46637
  async function buildSandboxCreateRequestProto(appId, imageId, params = {}) {
46634
46638
  checkForRenamedParams(params, {
46635
46639
  memory: "memoryMiB",
@@ -47005,34 +47009,61 @@ async function buildContainerExecRequestProto(taskId, command, params) {
47005
47009
  var Sandbox2 = class _Sandbox {
47006
47010
  #client;
47007
47011
  sandboxId;
47008
- stdin;
47009
- stdout;
47010
- stderr;
47012
+ #stdin;
47013
+ #stdout;
47014
+ #stderr;
47015
+ #stdoutAbort;
47016
+ #stderrAbort;
47011
47017
  #taskId;
47012
47018
  #tunnels;
47013
47019
  /** @ignore */
47014
47020
  constructor(client2, sandboxId) {
47015
47021
  this.#client = client2;
47016
47022
  this.sandboxId = sandboxId;
47017
- this.stdin = toModalWriteStream(inputStreamSb(client2.cpClient, sandboxId));
47018
- this.stdout = toModalReadStream(
47019
- streamConsumingIter(
47023
+ }
47024
+ get stdin() {
47025
+ if (!this.#stdin) {
47026
+ this.#stdin = toModalWriteStream(
47027
+ inputStreamSb(this.#client.cpClient, this.sandboxId)
47028
+ );
47029
+ }
47030
+ return this.#stdin;
47031
+ }
47032
+ get stdout() {
47033
+ if (!this.#stdout) {
47034
+ this.#stdoutAbort = new AbortController();
47035
+ const bytesStream = streamConsumingIter(
47020
47036
  outputStreamSb(
47021
- client2.cpClient,
47022
- sandboxId,
47023
- 1 /* FILE_DESCRIPTOR_STDOUT */
47024
- )
47025
- ).pipeThrough(new TextDecoderStream())
47026
- );
47027
- this.stderr = toModalReadStream(
47028
- streamConsumingIter(
47037
+ this.#client.cpClient,
47038
+ this.sandboxId,
47039
+ 1 /* FILE_DESCRIPTOR_STDOUT */,
47040
+ this.#stdoutAbort.signal
47041
+ ),
47042
+ () => this.#stdoutAbort?.abort()
47043
+ );
47044
+ this.#stdout = toModalReadStream(
47045
+ bytesStream.pipeThrough(new TextDecoderStream())
47046
+ );
47047
+ }
47048
+ return this.#stdout;
47049
+ }
47050
+ get stderr() {
47051
+ if (!this.#stderr) {
47052
+ this.#stderrAbort = new AbortController();
47053
+ const bytesStream = streamConsumingIter(
47029
47054
  outputStreamSb(
47030
- client2.cpClient,
47031
- sandboxId,
47032
- 2 /* FILE_DESCRIPTOR_STDERR */
47033
- )
47034
- ).pipeThrough(new TextDecoderStream())
47035
- );
47055
+ this.#client.cpClient,
47056
+ this.sandboxId,
47057
+ 2 /* FILE_DESCRIPTOR_STDERR */,
47058
+ this.#stderrAbort.signal
47059
+ ),
47060
+ () => this.#stderrAbort?.abort()
47061
+ );
47062
+ this.#stderr = toModalReadStream(
47063
+ bytesStream.pipeThrough(new TextDecoderStream())
47064
+ );
47065
+ }
47066
+ return this.#stderr;
47036
47067
  }
47037
47068
  /** Set tags (key-value pairs) on the Sandbox. Tags can be used to filter results in {@link SandboxService#list Sandbox.list}. */
47038
47069
  async setTags(tags) {
@@ -47331,19 +47362,25 @@ var ContainerProcess = class {
47331
47362
  }
47332
47363
  }
47333
47364
  };
47334
- async function* outputStreamSb(cpClient, sandboxId, fileDescriptor) {
47365
+ async function* outputStreamSb(cpClient, sandboxId, fileDescriptor, signal) {
47335
47366
  let lastIndex = "0-0";
47336
47367
  let completed = false;
47337
- let retries = 10;
47368
+ let retriesRemaining = SB_LOGS_MAX_RETRIES;
47369
+ let delayMs = SB_LOGS_INITIAL_DELAY_MS;
47338
47370
  while (!completed) {
47339
47371
  try {
47340
- const outputIterator = cpClient.sandboxGetLogs({
47341
- sandboxId,
47342
- fileDescriptor,
47343
- timeout: 55,
47344
- lastEntryId: lastIndex
47345
- });
47372
+ const outputIterator = cpClient.sandboxGetLogs(
47373
+ {
47374
+ sandboxId,
47375
+ fileDescriptor,
47376
+ timeout: 55,
47377
+ lastEntryId: lastIndex
47378
+ },
47379
+ { signal }
47380
+ );
47346
47381
  for await (const batch of outputIterator) {
47382
+ delayMs = SB_LOGS_INITIAL_DELAY_MS;
47383
+ retriesRemaining = SB_LOGS_MAX_RETRIES;
47347
47384
  lastIndex = batch.entryId;
47348
47385
  yield* batch.items.map((item) => new TextEncoder().encode(item.data));
47349
47386
  if (batch.eof) {
@@ -47352,8 +47389,21 @@ async function* outputStreamSb(cpClient, sandboxId, fileDescriptor) {
47352
47389
  }
47353
47390
  }
47354
47391
  } catch (err) {
47355
- if (isRetryableGrpc(err) && retries > 0) retries--;
47356
- else throw err;
47392
+ if (signal?.aborted) {
47393
+ return;
47394
+ }
47395
+ if (isRetryableGrpc(err) && retriesRemaining > 0) {
47396
+ try {
47397
+ await setTimeout2(delayMs, void 0, { signal });
47398
+ } catch {
47399
+ return;
47400
+ }
47401
+ delayMs *= SB_LOGS_DELAY_FACTOR;
47402
+ retriesRemaining--;
47403
+ continue;
47404
+ } else {
47405
+ throw err;
47406
+ }
47357
47407
  }
47358
47408
  }
47359
47409
  }
@@ -47758,7 +47808,7 @@ var AuthTokenManager = class {
47758
47808
 
47759
47809
  // src/version.ts
47760
47810
  function getSDKVersion() {
47761
- return true ? "0.6.1" : "0.0.0";
47811
+ return true ? "0.6.2" : "0.0.0";
47762
47812
  }
47763
47813
 
47764
47814
  // src/logger.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "modal",
3
- "version": "0.6.1",
3
+ "version": "0.6.2",
4
4
  "description": "Modal SDK for JavaScript/TypeScript",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://modal.com/docs/guide/sdk-javascript-go",