modal 0.3.0 → 0.3.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.
Files changed (2) hide show
  1. package/dist/index.js +129 -90
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -36665,13 +36665,19 @@ var timeoutMiddleware = async function* timeoutMiddleware2(call, options) {
36665
36665
  }
36666
36666
  }
36667
36667
  };
36668
- var RETRYABLE_GRPC_STATUS_CODES = [
36668
+ var retryableGrpcStatusCodes = /* @__PURE__ */ new Set([
36669
36669
  Status.DEADLINE_EXCEEDED,
36670
36670
  Status.UNAVAILABLE,
36671
36671
  Status.CANCELLED,
36672
36672
  Status.INTERNAL,
36673
36673
  Status.UNKNOWN
36674
- ];
36674
+ ]);
36675
+ function isRetryableGrpc(error) {
36676
+ if (error instanceof ClientError) {
36677
+ return retryableGrpcStatusCodes.has(error.code);
36678
+ }
36679
+ return false;
36680
+ }
36675
36681
  var sleep = (ms, signal) => new Promise((resolve, reject) => {
36676
36682
  if (signal?.aborted) return reject(signal.reason);
36677
36683
  const t = setTimeout(resolve, ms);
@@ -36698,7 +36704,7 @@ var retryMiddleware = async function* retryMiddleware2(call, options) {
36698
36704
  return yield* call.next(call.request, restOptions);
36699
36705
  }
36700
36706
  const retryableCodes = /* @__PURE__ */ new Set([
36701
- ...RETRYABLE_GRPC_STATUS_CODES,
36707
+ ...retryableGrpcStatusCodes,
36702
36708
  ...additionalStatusCodes
36703
36709
  ]);
36704
36710
  const idempotencyKey = uuidv4();
@@ -36802,81 +36808,112 @@ ${result.exception}`
36802
36808
 
36803
36809
  // src/streams.ts
36804
36810
  function toModalReadStream(stream) {
36805
- return Object.assign(stream, {
36806
- async readText() {
36807
- const reader = stream.getReader();
36808
- try {
36809
- const decoder = new TextDecoder("utf-8");
36810
- const chunks = [];
36811
- while (true) {
36812
- const { value, done } = await reader.read();
36813
- if (value) {
36814
- if (typeof value === "string") chunks.push(value);
36815
- else {
36816
- chunks.push(decoder.decode(value.buffer, { stream: true }));
36817
- }
36818
- }
36819
- if (done) {
36820
- chunks.push(decoder.decode(void 0, { stream: false }));
36821
- break;
36811
+ return Object.assign(stream, readMixin);
36812
+ }
36813
+ function toModalWriteStream(stream) {
36814
+ return Object.assign(stream, writeMixin);
36815
+ }
36816
+ var readMixin = {
36817
+ async readText() {
36818
+ const reader = this.getReader();
36819
+ try {
36820
+ const decoder = new TextDecoder("utf-8");
36821
+ const chunks = [];
36822
+ while (true) {
36823
+ const { value, done } = await reader.read();
36824
+ if (value) {
36825
+ if (typeof value === "string") chunks.push(value);
36826
+ else {
36827
+ chunks.push(decoder.decode(value.buffer, { stream: true }));
36822
36828
  }
36823
36829
  }
36824
- return chunks.join("");
36825
- } finally {
36826
- reader.releaseLock();
36830
+ if (done) {
36831
+ chunks.push(decoder.decode(void 0, { stream: false }));
36832
+ break;
36833
+ }
36827
36834
  }
36828
- },
36829
- async readBytes() {
36830
- const chunks = [];
36831
- const reader = stream.getReader();
36832
- try {
36833
- while (true) {
36834
- const { value, done } = await reader.read();
36835
- if (value) {
36836
- if (typeof value === "string") {
36837
- chunks.push(new TextEncoder().encode(value));
36838
- } else {
36839
- chunks.push(value);
36840
- }
36835
+ return chunks.join("");
36836
+ } finally {
36837
+ reader.releaseLock();
36838
+ }
36839
+ },
36840
+ async readBytes() {
36841
+ const chunks = [];
36842
+ const reader = this.getReader();
36843
+ try {
36844
+ while (true) {
36845
+ const { value, done } = await reader.read();
36846
+ if (value) {
36847
+ if (typeof value === "string") {
36848
+ chunks.push(new TextEncoder().encode(value));
36849
+ } else {
36850
+ chunks.push(value);
36841
36851
  }
36842
- if (done) break;
36843
36852
  }
36844
- } finally {
36845
- reader.releaseLock();
36846
- }
36847
- let totalLength = 0;
36848
- for (const chunk of chunks) {
36849
- totalLength += chunk.length;
36853
+ if (done) break;
36850
36854
  }
36851
- const result = new Uint8Array(totalLength);
36852
- let offset = 0;
36853
- for (const chunk of chunks) {
36854
- result.set(chunk, offset);
36855
- offset += chunk.length;
36856
- }
36857
- return result;
36855
+ } finally {
36856
+ reader.releaseLock();
36858
36857
  }
36859
- });
36860
- }
36861
- function toModalWriteStream(stream) {
36862
- return Object.assign(stream, {
36863
- async writeText(text) {
36864
- const writer = stream.getWriter();
36865
- try {
36866
- await writer.write(text);
36867
- } finally {
36868
- writer.releaseLock();
36858
+ let totalLength = 0;
36859
+ for (const chunk of chunks) {
36860
+ totalLength += chunk.length;
36861
+ }
36862
+ const result = new Uint8Array(totalLength);
36863
+ let offset = 0;
36864
+ for (const chunk of chunks) {
36865
+ result.set(chunk, offset);
36866
+ offset += chunk.length;
36867
+ }
36868
+ return result;
36869
+ }
36870
+ };
36871
+ var writeMixin = {
36872
+ async writeText(text) {
36873
+ const writer = this.getWriter();
36874
+ try {
36875
+ await writer.write(text);
36876
+ } finally {
36877
+ writer.releaseLock();
36878
+ }
36879
+ },
36880
+ async writeBytes(bytes) {
36881
+ const writer = this.getWriter();
36882
+ try {
36883
+ await writer.write(bytes);
36884
+ } finally {
36885
+ writer.releaseLock();
36886
+ }
36887
+ }
36888
+ };
36889
+ function streamConsumingIter(iterable) {
36890
+ const iter = iterable[Symbol.asyncIterator]();
36891
+ return new ReadableStream(
36892
+ {
36893
+ async pull(controller) {
36894
+ const { done, value } = await iter.next();
36895
+ if (value) {
36896
+ controller.enqueue(value);
36897
+ }
36898
+ if (done) {
36899
+ controller.close();
36900
+ }
36901
+ },
36902
+ async cancel() {
36903
+ consumeIterator(iter);
36869
36904
  }
36870
36905
  },
36871
- async writeBytes(bytes) {
36872
- const writer = stream.getWriter();
36873
- try {
36874
- await writer.write(bytes);
36875
- } finally {
36876
- writer.releaseLock();
36877
- }
36878
- }
36879
- });
36906
+ new ByteLengthQueuingStrategy({
36907
+ highWaterMark: 64 * 1024
36908
+ // 64 KiB
36909
+ })
36910
+ );
36911
+ }
36912
+ async function consumeIterator(iter) {
36913
+ while (true) {
36914
+ const { done } = await iter.next();
36915
+ if (done) break;
36916
+ }
36880
36917
  }
36881
36918
 
36882
36919
  // src/sandbox.ts
@@ -36890,14 +36927,14 @@ var Sandbox2 = class {
36890
36927
  this.sandboxId = sandboxId;
36891
36928
  this.stdin = toModalWriteStream(inputStreamSb(sandboxId));
36892
36929
  this.stdout = toModalReadStream(
36893
- ReadableStream.from(
36930
+ streamConsumingIter(
36894
36931
  outputStreamSb(sandboxId, 1 /* FILE_DESCRIPTOR_STDOUT */)
36895
- )
36932
+ ).pipeThrough(new TextDecoderStream())
36896
36933
  );
36897
36934
  this.stderr = toModalReadStream(
36898
- ReadableStream.from(
36935
+ streamConsumingIter(
36899
36936
  outputStreamSb(sandboxId, 2 /* FILE_DESCRIPTOR_STDERR */)
36900
- )
36937
+ ).pipeThrough(new TextDecoderStream())
36901
36938
  );
36902
36939
  }
36903
36940
  async exec(command, options) {
@@ -36951,12 +36988,20 @@ var ContainerProcess = class {
36951
36988
  const stderr = options?.stderr ?? "pipe";
36952
36989
  this.#execId = execId;
36953
36990
  this.stdin = toModalWriteStream(inputStreamCp(execId));
36954
- const stdoutStream = ReadableStream.from(
36955
- stdout === "pipe" ? outputStreamCp(execId, 1 /* FILE_DESCRIPTOR_STDOUT */) : []
36991
+ let stdoutStream = streamConsumingIter(
36992
+ outputStreamCp(execId, 1 /* FILE_DESCRIPTOR_STDOUT */)
36956
36993
  );
36957
- const stderrStream = ReadableStream.from(
36958
- stderr === "pipe" ? outputStreamCp(execId, 2 /* FILE_DESCRIPTOR_STDERR */) : []
36994
+ if (stdout === "ignore") {
36995
+ stdoutStream.cancel();
36996
+ stdoutStream = ReadableStream.from([]);
36997
+ }
36998
+ let stderrStream = streamConsumingIter(
36999
+ outputStreamCp(execId, 2 /* FILE_DESCRIPTOR_STDERR */)
36959
37000
  );
37001
+ if (stderr === "ignore") {
37002
+ stderrStream.cancel();
37003
+ stderrStream = ReadableStream.from([]);
37004
+ }
36960
37005
  if (mode === "text") {
36961
37006
  this.stdout = toModalReadStream(
36962
37007
  stdoutStream.pipeThrough(new TextDecoderStream())
@@ -36985,7 +37030,7 @@ var ContainerProcess = class {
36985
37030
  async function* outputStreamSb(sandboxId, fileDescriptor) {
36986
37031
  let lastIndex = "0-0";
36987
37032
  let completed = false;
36988
- let retriesRemaining = 10;
37033
+ let retries = 10;
36989
37034
  while (!completed) {
36990
37035
  try {
36991
37036
  const outputIterator = client.sandboxGetLogs({
@@ -36996,25 +37041,22 @@ async function* outputStreamSb(sandboxId, fileDescriptor) {
36996
37041
  });
36997
37042
  for await (const batch of outputIterator) {
36998
37043
  lastIndex = batch.entryId;
36999
- yield* batch.items.map((item) => item.data);
37044
+ yield* batch.items.map((item) => new TextEncoder().encode(item.data));
37000
37045
  if (batch.eof) {
37001
37046
  completed = true;
37002
37047
  break;
37003
37048
  }
37004
37049
  }
37005
37050
  } catch (error) {
37006
- if (retriesRemaining > 0) {
37007
- retriesRemaining--;
37008
- } else {
37009
- throw error;
37010
- }
37051
+ if (isRetryableGrpc(error) && retries > 0) retries--;
37052
+ else throw error;
37011
37053
  }
37012
37054
  }
37013
37055
  }
37014
37056
  async function* outputStreamCp(execId, fileDescriptor) {
37015
37057
  let lastIndex = 0;
37016
37058
  let completed = false;
37017
- let retriesRemaining = 10;
37059
+ let retries = 10;
37018
37060
  while (!completed) {
37019
37061
  try {
37020
37062
  const outputIterator = client.containerExecGetOutput({
@@ -37033,11 +37075,8 @@ async function* outputStreamCp(execId, fileDescriptor) {
37033
37075
  }
37034
37076
  }
37035
37077
  } catch (error) {
37036
- if (retriesRemaining > 0) {
37037
- retriesRemaining--;
37038
- } else {
37039
- throw error;
37040
- }
37078
+ if (isRetryableGrpc(error) && retries > 0) retries--;
37079
+ else throw error;
37041
37080
  }
37042
37081
  }
37043
37082
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "modal",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Modal client library for JavaScript",
5
5
  "license": "MIT",
6
6
  "homepage": "https://modal.com/docs",