@daytonaio/sdk 0.24.2 → 0.24.3

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.
@@ -0,0 +1,33 @@
1
+ declare global {
2
+ /**
3
+ * In Deno this global exists and has a `version.deno` string;
4
+ * in all other runtimes it will be `undefined`.
5
+ */
6
+ var Deno: {
7
+ version: {
8
+ deno: string;
9
+ };
10
+ env: {
11
+ get(name: string): string | undefined;
12
+ toObject(): Record<string, string>;
13
+ };
14
+ } | undefined;
15
+ /**
16
+ * In Bun this global exists and has a `version.bun` string;
17
+ * in all other runtimes it will be `undefined`.
18
+ */
19
+ var Bun: {
20
+ version: {
21
+ bun: string;
22
+ };
23
+ } | undefined;
24
+ }
25
+ export declare enum Runtime {
26
+ NODE = "node",
27
+ DENO = "deno",
28
+ BUN = "bun",
29
+ BROWSER = "browser",
30
+ UNKNOWN = "unknown"
31
+ }
32
+ export declare const RUNTIME: Runtime;
33
+ export declare function getEnvVar(name: string): string | undefined;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright 2025 Daytona Platforms Inc.
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.RUNTIME = exports.Runtime = void 0;
8
+ exports.getEnvVar = getEnvVar;
9
+ var Runtime;
10
+ (function (Runtime) {
11
+ Runtime["NODE"] = "node";
12
+ Runtime["DENO"] = "deno";
13
+ Runtime["BUN"] = "bun";
14
+ Runtime["BROWSER"] = "browser";
15
+ Runtime["UNKNOWN"] = "unknown";
16
+ })(Runtime || (exports.Runtime = Runtime = {}));
17
+ exports.RUNTIME = typeof Deno !== 'undefined'
18
+ ? Runtime.DENO
19
+ : typeof Bun !== 'undefined' && !!Bun.version
20
+ ? Runtime.BUN
21
+ : typeof process !== 'undefined' && !!process.versions?.node
22
+ ? Runtime.NODE
23
+ : typeof window !== 'undefined'
24
+ ? Runtime.BROWSER
25
+ : Runtime.UNKNOWN;
26
+ function getEnvVar(name) {
27
+ if (exports.RUNTIME === Runtime.NODE || exports.RUNTIME === Runtime.BUN) {
28
+ return process.env[name];
29
+ }
30
+ if (exports.RUNTIME === Runtime.DENO) {
31
+ return Deno.env.get(name);
32
+ }
33
+ return undefined;
34
+ }
35
+ //# sourceMappingURL=Runtime.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Runtime.js","sourceRoot":"","sources":["../../../../../libs/sdk-typescript/src/utils/Runtime.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAyCH,8BASC;AA5BD,IAAY,OAMX;AAND,WAAY,OAAO;IACjB,wBAAa,CAAA;IACb,wBAAa,CAAA;IACb,sBAAW,CAAA;IACX,8BAAmB,CAAA;IACnB,8BAAmB,CAAA;AACrB,CAAC,EANW,OAAO,uBAAP,OAAO,QAMlB;AAEY,QAAA,OAAO,GAClB,OAAO,IAAI,KAAK,WAAW;IACzB,CAAC,CAAC,OAAO,CAAC,IAAI;IACd,CAAC,CAAC,OAAO,GAAG,KAAK,WAAW,IAAI,CAAC,CAAC,GAAG,CAAC,OAAO;QAC3C,CAAC,CAAC,OAAO,CAAC,GAAG;QACb,CAAC,CAAC,OAAO,OAAO,KAAK,WAAW,IAAI,CAAC,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI;YAC1D,CAAC,CAAC,OAAO,CAAC,IAAI;YACd,CAAC,CAAC,OAAO,MAAM,KAAK,WAAW;gBAC7B,CAAC,CAAC,OAAO,CAAC,OAAO;gBACjB,CAAC,CAAC,OAAO,CAAC,OAAO,CAAA;AAE3B,SAAgB,SAAS,CAAC,IAAY;IACpC,IAAI,eAAO,KAAK,OAAO,CAAC,IAAI,IAAI,eAAO,KAAK,OAAO,CAAC,GAAG,EAAE,CAAC;QACxD,OAAO,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;IAC1B,CAAC;IACD,IAAI,eAAO,KAAK,OAAO,CAAC,IAAI,EAAE,CAAC;QAC7B,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;IAC3B,CAAC;IAED,OAAO,SAAS,CAAA;AAClB,CAAC"}
@@ -1,13 +1,10 @@
1
1
  /**
2
- * Process a streaming response from a URL. Stream will terminate if the server-side stream
3
- * ends or if the shouldTerminate function returns True.
2
+ * Process a streaming response from fetch(), where getStream() returns a Fetch Response.
4
3
  *
5
- * @param getStream - A function that returns a promise of an AxiosResponse with .data being the stream
6
- * @param onChunk - A function to process each chunk of the response
7
- * @param shouldTerminate - A function to check if the response should be terminated
8
- * @param chunkTimeout - The timeout for each chunk
9
- * @param requireConsecutiveTermination - Whether to require two consecutive termination signals
10
- * to terminate the stream.
4
+ * @param getStream – zero-arg function that does `await fetch(...)` and returns the Response
5
+ * @param onChunk called with each decoded UTF-8 chunk
6
+ * @param shouldTerminate pollable; if true for two consecutive timeouts (or once if requireConsecutiveTermination=false), the loop breaks
7
+ * @param chunkTimeout milliseconds to wait for a new chunk before calling shouldTerminate()
8
+ * @param requireConsecutiveTermination whether you need two time-outs in a row to break
11
9
  */
12
- export declare function processStreamingResponse(getStream: () => Promise<any>, // can return AxiosResponse with .data being the stream
13
- onChunk: (chunk: string) => void, shouldTerminate: () => Promise<boolean>, chunkTimeout?: number, requireConsecutiveTermination?: boolean): Promise<void>;
10
+ export declare function processStreamingResponse(getStream: () => Promise<Response>, onChunk: (chunk: string) => void, shouldTerminate: () => Promise<boolean>, chunkTimeout?: number, requireConsecutiveTermination?: boolean): Promise<void>;
@@ -6,77 +6,61 @@
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
7
  exports.processStreamingResponse = processStreamingResponse;
8
8
  /**
9
- * Process a streaming response from a URL. Stream will terminate if the server-side stream
10
- * ends or if the shouldTerminate function returns True.
9
+ * Process a streaming response from fetch(), where getStream() returns a Fetch Response.
11
10
  *
12
- * @param getStream - A function that returns a promise of an AxiosResponse with .data being the stream
13
- * @param onChunk - A function to process each chunk of the response
14
- * @param shouldTerminate - A function to check if the response should be terminated
15
- * @param chunkTimeout - The timeout for each chunk
16
- * @param requireConsecutiveTermination - Whether to require two consecutive termination signals
17
- * to terminate the stream.
11
+ * @param getStream – zero-arg function that does `await fetch(...)` and returns the Response
12
+ * @param onChunk called with each decoded UTF-8 chunk
13
+ * @param shouldTerminate pollable; if true for two consecutive timeouts (or once if requireConsecutiveTermination=false), the loop breaks
14
+ * @param chunkTimeout milliseconds to wait for a new chunk before calling shouldTerminate()
15
+ * @param requireConsecutiveTermination whether you need two time-outs in a row to break
18
16
  */
19
- async function processStreamingResponse(getStream, // can return AxiosResponse with .data being the stream
20
- onChunk, shouldTerminate, chunkTimeout = 2000, requireConsecutiveTermination = true) {
21
- const response = await getStream();
22
- const stream = response.data;
23
- let nextChunkPromise = null;
17
+ async function processStreamingResponse(getStream, onChunk, shouldTerminate, chunkTimeout = 2000, requireConsecutiveTermination = true) {
18
+ const res = await getStream();
19
+ if (!res.body)
20
+ throw new Error('No streaming support');
21
+ const reader = res.body.getReader();
22
+ const decoder = new TextDecoder();
23
+ const TIMEOUT = Symbol();
24
24
  let exitCheckStreak = 0;
25
- let terminated = false;
26
- const readNext = () => {
27
- return new Promise((resolve) => {
28
- const onData = (data) => {
29
- cleanup();
30
- resolve(data);
31
- };
32
- const cleanup = () => {
33
- stream.off('data', onData);
34
- };
35
- stream.once('data', onData);
36
- });
37
- };
38
- const terminationPromise = new Promise((resolve, reject) => {
39
- stream.on('end', () => {
40
- terminated = true;
41
- resolve();
42
- });
43
- stream.on('close', () => {
44
- terminated = true;
45
- resolve();
46
- });
47
- stream.on('error', (err) => {
48
- terminated = true;
49
- reject(err);
50
- });
51
- });
52
- const processLoop = async () => {
53
- while (!terminated) {
54
- if (!nextChunkPromise) {
55
- nextChunkPromise = readNext();
25
+ // Only one pending read promise at a time:
26
+ let readPromise = null;
27
+ try {
28
+ while (true) {
29
+ // Start a read if none in flight
30
+ if (!readPromise) {
31
+ readPromise = reader.read().then((r) => (r.done ? null : r.value ?? new Uint8Array(0)));
56
32
  }
57
- const timeoutPromise = new Promise((resolve) => setTimeout(() => resolve(null), chunkTimeout));
58
- const result = await Promise.race([nextChunkPromise, timeoutPromise]);
59
- if (result instanceof Buffer) {
60
- onChunk(result.toString('utf8'));
61
- nextChunkPromise = null;
62
- exitCheckStreak = 0;
63
- }
64
- else {
65
- const shouldEnd = await shouldTerminate();
66
- if (shouldEnd) {
67
- exitCheckStreak += 1;
68
- if (!requireConsecutiveTermination || exitCheckStreak > 1) {
33
+ // Race that single read against your timeout
34
+ const timeoutPromise = new Promise((r) => setTimeout(() => r(TIMEOUT), chunkTimeout));
35
+ const result = await Promise.race([readPromise, timeoutPromise]);
36
+ if (result === TIMEOUT) {
37
+ // no data yet, but the readPromise is still pending
38
+ const stop = await shouldTerminate();
39
+ if (stop) {
40
+ exitCheckStreak++;
41
+ if (!requireConsecutiveTermination || exitCheckStreak > 1)
69
42
  break;
70
- }
71
43
  }
72
44
  else {
73
45
  exitCheckStreak = 0;
74
46
  }
47
+ // loop again—but do NOT overwrite readPromise!
48
+ }
49
+ else {
50
+ // readPromise has resolved
51
+ readPromise = null;
52
+ if (result === null) {
53
+ // stream closed
54
+ break;
55
+ }
56
+ // valid chunk
57
+ onChunk(decoder.decode(result));
58
+ exitCheckStreak = 0;
75
59
  }
76
60
  }
77
- stream.destroy();
78
- stream.removeAllListeners();
79
- };
80
- await Promise.race([processLoop(), terminationPromise]);
61
+ }
62
+ finally {
63
+ await reader.cancel();
64
+ }
81
65
  }
82
66
  //# sourceMappingURL=Stream.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Stream.js","sourceRoot":"","sources":["../../../../../libs/sdk-typescript/src/utils/Stream.ts"],"names":[],"mappings":";AAAA;;;GAGG;;AAaH,4DAwEC;AAnFD;;;;;;;;;;GAUG;AACI,KAAK,UAAU,wBAAwB,CAC5C,SAA6B,EAAE,uDAAuD;AACtF,OAAgC,EAChC,eAAuC,EACvC,YAAY,GAAG,IAAI,EACnB,6BAA6B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