@onkernel/sdk 0.1.0-alpha.12 → 0.1.0-alpha.14
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 +17 -0
- package/core/app-framework.d.mts +3 -3
- package/core/app-framework.d.mts.map +1 -1
- package/core/app-framework.d.ts +3 -3
- package/core/app-framework.d.ts.map +1 -1
- package/core/app-framework.js +14 -31
- package/core/app-framework.js.map +1 -1
- package/core/app-framework.mjs +14 -31
- package/core/app-framework.mjs.map +1 -1
- package/core/streaming.d.mts +31 -0
- package/core/streaming.d.mts.map +1 -0
- package/core/streaming.d.ts +31 -0
- package/core/streaming.d.ts.map +1 -0
- package/core/streaming.js +257 -0
- package/core/streaming.js.map +1 -0
- package/core/streaming.mjs +252 -0
- package/core/streaming.mjs.map +1 -0
- package/internal/decoders/line.d.mts +17 -0
- package/internal/decoders/line.d.mts.map +1 -0
- package/internal/decoders/line.d.ts +17 -0
- package/internal/decoders/line.d.ts.map +1 -0
- package/internal/decoders/line.js +113 -0
- package/internal/decoders/line.js.map +1 -0
- package/internal/decoders/line.mjs +108 -0
- package/internal/decoders/line.mjs.map +1 -0
- package/internal/parse.d.mts.map +1 -1
- package/internal/parse.d.ts.map +1 -1
- package/internal/parse.js +10 -0
- package/internal/parse.js.map +1 -1
- package/internal/parse.mjs +10 -0
- package/internal/parse.mjs.map +1 -1
- package/internal/request-options.d.mts +2 -0
- package/internal/request-options.d.mts.map +1 -1
- package/internal/request-options.d.ts +2 -0
- package/internal/request-options.d.ts.map +1 -1
- package/internal/request-options.js.map +1 -1
- package/internal/request-options.mjs.map +1 -1
- package/internal/tslib.js +17 -17
- package/package.json +11 -1
- package/resources/apps/apps.d.mts +2 -2
- package/resources/apps/apps.d.mts.map +1 -1
- package/resources/apps/apps.d.ts +2 -2
- package/resources/apps/apps.d.ts.map +1 -1
- package/resources/apps/apps.js.map +1 -1
- package/resources/apps/apps.mjs +1 -1
- package/resources/apps/apps.mjs.map +1 -1
- package/resources/apps/deployments.d.mts +70 -1
- package/resources/apps/deployments.d.mts.map +1 -1
- package/resources/apps/deployments.d.ts +70 -1
- package/resources/apps/deployments.d.ts.map +1 -1
- package/resources/apps/deployments.js +19 -0
- package/resources/apps/deployments.js.map +1 -1
- package/resources/apps/deployments.mjs +19 -0
- package/resources/apps/deployments.mjs.map +1 -1
- package/resources/apps/index.d.mts +1 -1
- package/resources/apps/index.d.mts.map +1 -1
- package/resources/apps/index.d.ts +1 -1
- package/resources/apps/index.d.ts.map +1 -1
- package/resources/apps/index.js.map +1 -1
- package/resources/apps/index.mjs +1 -1
- package/resources/apps/index.mjs.map +1 -1
- package/src/core/app-framework.ts +19 -36
- package/src/core/streaming.ts +301 -0
- package/src/internal/decoders/line.ts +135 -0
- package/src/internal/parse.ts +14 -0
- package/src/internal/request-options.ts +2 -0
- package/src/resources/apps/apps.ts +7 -1
- package/src/resources/apps/deployments.ts +93 -0
- package/src/resources/apps/index.ts +6 -1
- package/src/streaming.ts +2 -0
- package/src/version.ts +1 -1
- package/streaming.d.mts +2 -0
- package/streaming.d.mts.map +1 -0
- package/streaming.d.ts +2 -0
- package/streaming.d.ts.map +1 -0
- package/streaming.js +6 -0
- package/streaming.js.map +1 -0
- package/streaming.mjs +2 -0
- package/streaming.mjs.map +1 -0
- package/version.d.mts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { KernelError } from './error';
|
|
2
|
+
import { type ReadableStream } from '../internal/shim-types';
|
|
3
|
+
import { makeReadableStream } from '../internal/shims';
|
|
4
|
+
import { findDoubleNewlineIndex, LineDecoder } from '../internal/decoders/line';
|
|
5
|
+
import { ReadableStreamToAsyncIterable } from '../internal/shims';
|
|
6
|
+
import { isAbortError } from '../internal/errors';
|
|
7
|
+
import { encodeUTF8 } from '../internal/utils/bytes';
|
|
8
|
+
|
|
9
|
+
type Bytes = string | ArrayBuffer | Uint8Array | null | undefined;
|
|
10
|
+
|
|
11
|
+
export type ServerSentEvent = {
|
|
12
|
+
event: string | null;
|
|
13
|
+
data: string;
|
|
14
|
+
raw: string[];
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export class Stream<Item> implements AsyncIterable<Item> {
|
|
18
|
+
controller: AbortController;
|
|
19
|
+
|
|
20
|
+
constructor(
|
|
21
|
+
private iterator: () => AsyncIterator<Item>,
|
|
22
|
+
controller: AbortController,
|
|
23
|
+
) {
|
|
24
|
+
this.controller = controller;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static fromSSEResponse<Item>(response: Response, controller: AbortController): Stream<Item> {
|
|
28
|
+
let consumed = false;
|
|
29
|
+
|
|
30
|
+
async function* iterator(): AsyncIterator<Item, any, undefined> {
|
|
31
|
+
if (consumed) {
|
|
32
|
+
throw new KernelError('Cannot iterate over a consumed stream, use `.tee()` to split the stream.');
|
|
33
|
+
}
|
|
34
|
+
consumed = true;
|
|
35
|
+
let done = false;
|
|
36
|
+
try {
|
|
37
|
+
for await (const sse of _iterSSEMessages(response, controller)) {
|
|
38
|
+
try {
|
|
39
|
+
yield JSON.parse(sse.data);
|
|
40
|
+
} catch (e) {
|
|
41
|
+
console.error(`Could not parse message into JSON:`, sse.data);
|
|
42
|
+
console.error(`From chunk:`, sse.raw);
|
|
43
|
+
throw e;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
done = true;
|
|
47
|
+
} catch (e) {
|
|
48
|
+
// If the user calls `stream.controller.abort()`, we should exit without throwing.
|
|
49
|
+
if (isAbortError(e)) return;
|
|
50
|
+
throw e;
|
|
51
|
+
} finally {
|
|
52
|
+
// If the user `break`s, abort the ongoing request.
|
|
53
|
+
if (!done) controller.abort();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return new Stream(iterator, controller);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Generates a Stream from a newline-separated ReadableStream
|
|
62
|
+
* where each item is a JSON value.
|
|
63
|
+
*/
|
|
64
|
+
static fromReadableStream<Item>(readableStream: ReadableStream, controller: AbortController): Stream<Item> {
|
|
65
|
+
let consumed = false;
|
|
66
|
+
|
|
67
|
+
async function* iterLines(): AsyncGenerator<string, void, unknown> {
|
|
68
|
+
const lineDecoder = new LineDecoder();
|
|
69
|
+
|
|
70
|
+
const iter = ReadableStreamToAsyncIterable<Bytes>(readableStream);
|
|
71
|
+
for await (const chunk of iter) {
|
|
72
|
+
for (const line of lineDecoder.decode(chunk)) {
|
|
73
|
+
yield line;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
for (const line of lineDecoder.flush()) {
|
|
78
|
+
yield line;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async function* iterator(): AsyncIterator<Item, any, undefined> {
|
|
83
|
+
if (consumed) {
|
|
84
|
+
throw new KernelError('Cannot iterate over a consumed stream, use `.tee()` to split the stream.');
|
|
85
|
+
}
|
|
86
|
+
consumed = true;
|
|
87
|
+
let done = false;
|
|
88
|
+
try {
|
|
89
|
+
for await (const line of iterLines()) {
|
|
90
|
+
if (done) continue;
|
|
91
|
+
if (line) yield JSON.parse(line);
|
|
92
|
+
}
|
|
93
|
+
done = true;
|
|
94
|
+
} catch (e) {
|
|
95
|
+
// If the user calls `stream.controller.abort()`, we should exit without throwing.
|
|
96
|
+
if (isAbortError(e)) return;
|
|
97
|
+
throw e;
|
|
98
|
+
} finally {
|
|
99
|
+
// If the user `break`s, abort the ongoing request.
|
|
100
|
+
if (!done) controller.abort();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return new Stream(iterator, controller);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
[Symbol.asyncIterator](): AsyncIterator<Item> {
|
|
108
|
+
return this.iterator();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Splits the stream into two streams which can be
|
|
113
|
+
* independently read from at different speeds.
|
|
114
|
+
*/
|
|
115
|
+
tee(): [Stream<Item>, Stream<Item>] {
|
|
116
|
+
const left: Array<Promise<IteratorResult<Item>>> = [];
|
|
117
|
+
const right: Array<Promise<IteratorResult<Item>>> = [];
|
|
118
|
+
const iterator = this.iterator();
|
|
119
|
+
|
|
120
|
+
const teeIterator = (queue: Array<Promise<IteratorResult<Item>>>): AsyncIterator<Item> => {
|
|
121
|
+
return {
|
|
122
|
+
next: () => {
|
|
123
|
+
if (queue.length === 0) {
|
|
124
|
+
const result = iterator.next();
|
|
125
|
+
left.push(result);
|
|
126
|
+
right.push(result);
|
|
127
|
+
}
|
|
128
|
+
return queue.shift()!;
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
return [
|
|
134
|
+
new Stream(() => teeIterator(left), this.controller),
|
|
135
|
+
new Stream(() => teeIterator(right), this.controller),
|
|
136
|
+
];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Converts this stream to a newline-separated ReadableStream of
|
|
141
|
+
* JSON stringified values in the stream
|
|
142
|
+
* which can be turned back into a Stream with `Stream.fromReadableStream()`.
|
|
143
|
+
*/
|
|
144
|
+
toReadableStream(): ReadableStream {
|
|
145
|
+
const self = this;
|
|
146
|
+
let iter: AsyncIterator<Item>;
|
|
147
|
+
|
|
148
|
+
return makeReadableStream({
|
|
149
|
+
async start() {
|
|
150
|
+
iter = self[Symbol.asyncIterator]();
|
|
151
|
+
},
|
|
152
|
+
async pull(ctrl: any) {
|
|
153
|
+
try {
|
|
154
|
+
const { value, done } = await iter.next();
|
|
155
|
+
if (done) return ctrl.close();
|
|
156
|
+
|
|
157
|
+
const bytes = encodeUTF8(JSON.stringify(value) + '\n');
|
|
158
|
+
|
|
159
|
+
ctrl.enqueue(bytes);
|
|
160
|
+
} catch (err) {
|
|
161
|
+
ctrl.error(err);
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
async cancel() {
|
|
165
|
+
await iter.return?.();
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export async function* _iterSSEMessages(
|
|
172
|
+
response: Response,
|
|
173
|
+
controller: AbortController,
|
|
174
|
+
): AsyncGenerator<ServerSentEvent, void, unknown> {
|
|
175
|
+
if (!response.body) {
|
|
176
|
+
controller.abort();
|
|
177
|
+
if (
|
|
178
|
+
typeof (globalThis as any).navigator !== 'undefined' &&
|
|
179
|
+
(globalThis as any).navigator.product === 'ReactNative'
|
|
180
|
+
) {
|
|
181
|
+
throw new KernelError(
|
|
182
|
+
`The default react-native fetch implementation does not support streaming. Please use expo/fetch: https://docs.expo.dev/versions/latest/sdk/expo/#expofetch-api`,
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
throw new KernelError(`Attempted to iterate over a response with no body`);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const sseDecoder = new SSEDecoder();
|
|
189
|
+
const lineDecoder = new LineDecoder();
|
|
190
|
+
|
|
191
|
+
const iter = ReadableStreamToAsyncIterable<Bytes>(response.body);
|
|
192
|
+
for await (const sseChunk of iterSSEChunks(iter)) {
|
|
193
|
+
for (const line of lineDecoder.decode(sseChunk)) {
|
|
194
|
+
const sse = sseDecoder.decode(line);
|
|
195
|
+
if (sse) yield sse;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
for (const line of lineDecoder.flush()) {
|
|
200
|
+
const sse = sseDecoder.decode(line);
|
|
201
|
+
if (sse) yield sse;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Given an async iterable iterator, iterates over it and yields full
|
|
207
|
+
* SSE chunks, i.e. yields when a double new-line is encountered.
|
|
208
|
+
*/
|
|
209
|
+
async function* iterSSEChunks(iterator: AsyncIterableIterator<Bytes>): AsyncGenerator<Uint8Array> {
|
|
210
|
+
let data = new Uint8Array();
|
|
211
|
+
|
|
212
|
+
for await (const chunk of iterator) {
|
|
213
|
+
if (chunk == null) {
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const binaryChunk =
|
|
218
|
+
chunk instanceof ArrayBuffer ? new Uint8Array(chunk)
|
|
219
|
+
: typeof chunk === 'string' ? encodeUTF8(chunk)
|
|
220
|
+
: chunk;
|
|
221
|
+
|
|
222
|
+
let newData = new Uint8Array(data.length + binaryChunk.length);
|
|
223
|
+
newData.set(data);
|
|
224
|
+
newData.set(binaryChunk, data.length);
|
|
225
|
+
data = newData;
|
|
226
|
+
|
|
227
|
+
let patternIndex;
|
|
228
|
+
while ((patternIndex = findDoubleNewlineIndex(data)) !== -1) {
|
|
229
|
+
yield data.slice(0, patternIndex);
|
|
230
|
+
data = data.slice(patternIndex);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (data.length > 0) {
|
|
235
|
+
yield data;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
class SSEDecoder {
|
|
240
|
+
private data: string[];
|
|
241
|
+
private event: string | null;
|
|
242
|
+
private chunks: string[];
|
|
243
|
+
|
|
244
|
+
constructor() {
|
|
245
|
+
this.event = null;
|
|
246
|
+
this.data = [];
|
|
247
|
+
this.chunks = [];
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
decode(line: string) {
|
|
251
|
+
if (line.endsWith('\r')) {
|
|
252
|
+
line = line.substring(0, line.length - 1);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (!line) {
|
|
256
|
+
// empty line and we didn't previously encounter any messages
|
|
257
|
+
if (!this.event && !this.data.length) return null;
|
|
258
|
+
|
|
259
|
+
const sse: ServerSentEvent = {
|
|
260
|
+
event: this.event,
|
|
261
|
+
data: this.data.join('\n'),
|
|
262
|
+
raw: this.chunks,
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
this.event = null;
|
|
266
|
+
this.data = [];
|
|
267
|
+
this.chunks = [];
|
|
268
|
+
|
|
269
|
+
return sse;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
this.chunks.push(line);
|
|
273
|
+
|
|
274
|
+
if (line.startsWith(':')) {
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
let [fieldname, _, value] = partition(line, ':');
|
|
279
|
+
|
|
280
|
+
if (value.startsWith(' ')) {
|
|
281
|
+
value = value.substring(1);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (fieldname === 'event') {
|
|
285
|
+
this.event = value;
|
|
286
|
+
} else if (fieldname === 'data') {
|
|
287
|
+
this.data.push(value);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
return null;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function partition(str: string, delimiter: string): [string, string, string] {
|
|
295
|
+
const index = str.indexOf(delimiter);
|
|
296
|
+
if (index !== -1) {
|
|
297
|
+
return [str.substring(0, index), delimiter, str.substring(index + delimiter.length)];
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
return [str, '', ''];
|
|
301
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { concatBytes, decodeUTF8, encodeUTF8 } from '../utils/bytes';
|
|
2
|
+
|
|
3
|
+
export type Bytes = string | ArrayBuffer | Uint8Array | null | undefined;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A re-implementation of httpx's `LineDecoder` in Python that handles incrementally
|
|
7
|
+
* reading lines from text.
|
|
8
|
+
*
|
|
9
|
+
* https://github.com/encode/httpx/blob/920333ea98118e9cf617f246905d7b202510941c/httpx/_decoders.py#L258
|
|
10
|
+
*/
|
|
11
|
+
export class LineDecoder {
|
|
12
|
+
// prettier-ignore
|
|
13
|
+
static NEWLINE_CHARS = new Set(['\n', '\r']);
|
|
14
|
+
static NEWLINE_REGEXP = /\r\n|[\n\r]/g;
|
|
15
|
+
|
|
16
|
+
#buffer: Uint8Array;
|
|
17
|
+
#carriageReturnIndex: number | null;
|
|
18
|
+
|
|
19
|
+
constructor() {
|
|
20
|
+
this.#buffer = new Uint8Array();
|
|
21
|
+
this.#carriageReturnIndex = null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
decode(chunk: Bytes): string[] {
|
|
25
|
+
if (chunk == null) {
|
|
26
|
+
return [];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const binaryChunk =
|
|
30
|
+
chunk instanceof ArrayBuffer ? new Uint8Array(chunk)
|
|
31
|
+
: typeof chunk === 'string' ? encodeUTF8(chunk)
|
|
32
|
+
: chunk;
|
|
33
|
+
|
|
34
|
+
this.#buffer = concatBytes([this.#buffer, binaryChunk]);
|
|
35
|
+
|
|
36
|
+
const lines: string[] = [];
|
|
37
|
+
let patternIndex;
|
|
38
|
+
while ((patternIndex = findNewlineIndex(this.#buffer, this.#carriageReturnIndex)) != null) {
|
|
39
|
+
if (patternIndex.carriage && this.#carriageReturnIndex == null) {
|
|
40
|
+
// skip until we either get a corresponding `\n`, a new `\r` or nothing
|
|
41
|
+
this.#carriageReturnIndex = patternIndex.index;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// we got double \r or \rtext\n
|
|
46
|
+
if (
|
|
47
|
+
this.#carriageReturnIndex != null &&
|
|
48
|
+
(patternIndex.index !== this.#carriageReturnIndex + 1 || patternIndex.carriage)
|
|
49
|
+
) {
|
|
50
|
+
lines.push(decodeUTF8(this.#buffer.subarray(0, this.#carriageReturnIndex - 1)));
|
|
51
|
+
this.#buffer = this.#buffer.subarray(this.#carriageReturnIndex);
|
|
52
|
+
this.#carriageReturnIndex = null;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const endIndex =
|
|
57
|
+
this.#carriageReturnIndex !== null ? patternIndex.preceding - 1 : patternIndex.preceding;
|
|
58
|
+
|
|
59
|
+
const line = decodeUTF8(this.#buffer.subarray(0, endIndex));
|
|
60
|
+
lines.push(line);
|
|
61
|
+
|
|
62
|
+
this.#buffer = this.#buffer.subarray(patternIndex.index);
|
|
63
|
+
this.#carriageReturnIndex = null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return lines;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
flush(): string[] {
|
|
70
|
+
if (!this.#buffer.length) {
|
|
71
|
+
return [];
|
|
72
|
+
}
|
|
73
|
+
return this.decode('\n');
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* This function searches the buffer for the end patterns, (\r or \n)
|
|
79
|
+
* and returns an object with the index preceding the matched newline and the
|
|
80
|
+
* index after the newline char. `null` is returned if no new line is found.
|
|
81
|
+
*
|
|
82
|
+
* ```ts
|
|
83
|
+
* findNewLineIndex('abc\ndef') -> { preceding: 2, index: 3 }
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
function findNewlineIndex(
|
|
87
|
+
buffer: Uint8Array,
|
|
88
|
+
startIndex: number | null,
|
|
89
|
+
): { preceding: number; index: number; carriage: boolean } | null {
|
|
90
|
+
const newline = 0x0a; // \n
|
|
91
|
+
const carriage = 0x0d; // \r
|
|
92
|
+
|
|
93
|
+
for (let i = startIndex ?? 0; i < buffer.length; i++) {
|
|
94
|
+
if (buffer[i] === newline) {
|
|
95
|
+
return { preceding: i, index: i + 1, carriage: false };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (buffer[i] === carriage) {
|
|
99
|
+
return { preceding: i, index: i + 1, carriage: true };
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function findDoubleNewlineIndex(buffer: Uint8Array): number {
|
|
107
|
+
// This function searches the buffer for the end patterns (\r\r, \n\n, \r\n\r\n)
|
|
108
|
+
// and returns the index right after the first occurrence of any pattern,
|
|
109
|
+
// or -1 if none of the patterns are found.
|
|
110
|
+
const newline = 0x0a; // \n
|
|
111
|
+
const carriage = 0x0d; // \r
|
|
112
|
+
|
|
113
|
+
for (let i = 0; i < buffer.length - 1; i++) {
|
|
114
|
+
if (buffer[i] === newline && buffer[i + 1] === newline) {
|
|
115
|
+
// \n\n
|
|
116
|
+
return i + 2;
|
|
117
|
+
}
|
|
118
|
+
if (buffer[i] === carriage && buffer[i + 1] === carriage) {
|
|
119
|
+
// \r\r
|
|
120
|
+
return i + 2;
|
|
121
|
+
}
|
|
122
|
+
if (
|
|
123
|
+
buffer[i] === carriage &&
|
|
124
|
+
buffer[i + 1] === newline &&
|
|
125
|
+
i + 3 < buffer.length &&
|
|
126
|
+
buffer[i + 2] === carriage &&
|
|
127
|
+
buffer[i + 3] === newline
|
|
128
|
+
) {
|
|
129
|
+
// \r\n\r\n
|
|
130
|
+
return i + 4;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return -1;
|
|
135
|
+
}
|
package/src/internal/parse.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
2
|
|
|
3
3
|
import type { FinalRequestOptions } from './request-options';
|
|
4
|
+
import { Stream } from '../core/streaming';
|
|
4
5
|
import { type Kernel } from '../client';
|
|
5
6
|
import { formatRequestDetails, loggerFor } from './utils/log';
|
|
6
7
|
|
|
@@ -16,6 +17,19 @@ export type APIResponseProps = {
|
|
|
16
17
|
export async function defaultParseResponse<T>(client: Kernel, props: APIResponseProps): Promise<T> {
|
|
17
18
|
const { response, requestLogID, retryOfRequestLogID, startTime } = props;
|
|
18
19
|
const body = await (async () => {
|
|
20
|
+
if (props.options.stream) {
|
|
21
|
+
loggerFor(client).debug('response', response.status, response.url, response.headers, response.body);
|
|
22
|
+
|
|
23
|
+
// Note: there is an invariant here that isn't represented in the type system
|
|
24
|
+
// that if you set `stream: true` the response type must also be `Stream<T>`
|
|
25
|
+
|
|
26
|
+
if (props.options.__streamClass) {
|
|
27
|
+
return props.options.__streamClass.fromSSEResponse(response, props.controller) as any;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return Stream.fromSSEResponse(response, props.controller) as any;
|
|
31
|
+
}
|
|
32
|
+
|
|
19
33
|
// fetch refuses to read the body when the status code is 204.
|
|
20
34
|
if (response.status === 204) {
|
|
21
35
|
return null as T;
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { NullableHeaders } from './headers';
|
|
4
4
|
|
|
5
5
|
import type { BodyInit } from './builtin-types';
|
|
6
|
+
import { Stream } from '../core/streaming';
|
|
6
7
|
import type { HTTPMethod, MergedRequestInit } from './types';
|
|
7
8
|
import { type HeadersLike } from './headers';
|
|
8
9
|
|
|
@@ -22,6 +23,7 @@ export type RequestOptions = {
|
|
|
22
23
|
idempotencyKey?: string;
|
|
23
24
|
|
|
24
25
|
__binaryResponse?: boolean | undefined;
|
|
26
|
+
__streamClass?: typeof Stream;
|
|
25
27
|
};
|
|
26
28
|
|
|
27
29
|
export type EncodedContent = { bodyHeaders: HeadersLike; body: BodyInit };
|
|
@@ -2,7 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
import { APIResource } from '../../core/resource';
|
|
4
4
|
import * as DeploymentsAPI from './deployments';
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
DeploymentCreateParams,
|
|
7
|
+
DeploymentCreateResponse,
|
|
8
|
+
DeploymentFollowResponse,
|
|
9
|
+
Deployments,
|
|
10
|
+
} from './deployments';
|
|
6
11
|
import * as InvocationsAPI from './invocations';
|
|
7
12
|
import {
|
|
8
13
|
InvocationCreateParams,
|
|
@@ -23,6 +28,7 @@ export declare namespace Apps {
|
|
|
23
28
|
export {
|
|
24
29
|
Deployments as Deployments,
|
|
25
30
|
type DeploymentCreateResponse as DeploymentCreateResponse,
|
|
31
|
+
type DeploymentFollowResponse as DeploymentFollowResponse,
|
|
26
32
|
type DeploymentCreateParams as DeploymentCreateParams,
|
|
27
33
|
};
|
|
28
34
|
|
|
@@ -2,9 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
import { APIResource } from '../../core/resource';
|
|
4
4
|
import { APIPromise } from '../../core/api-promise';
|
|
5
|
+
import { Stream } from '../../core/streaming';
|
|
5
6
|
import { type Uploadable } from '../../core/uploads';
|
|
7
|
+
import { buildHeaders } from '../../internal/headers';
|
|
6
8
|
import { RequestOptions } from '../../internal/request-options';
|
|
7
9
|
import { multipartFormRequestOptions } from '../../internal/uploads';
|
|
10
|
+
import { path } from '../../internal/utils/path';
|
|
8
11
|
|
|
9
12
|
export class Deployments extends APIResource {
|
|
10
13
|
/**
|
|
@@ -21,6 +24,24 @@ export class Deployments extends APIResource {
|
|
|
21
24
|
create(body: DeploymentCreateParams, options?: RequestOptions): APIPromise<DeploymentCreateResponse> {
|
|
22
25
|
return this._client.post('/deploy', multipartFormRequestOptions({ body, ...options }, this._client));
|
|
23
26
|
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Establishes a Server-Sent Events (SSE) stream that delivers real-time logs and
|
|
30
|
+
* status updates for a deployed application. The stream terminates automatically
|
|
31
|
+
* once the application reaches a terminal state.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const response = await client.apps.deployments.follow('id');
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
follow(id: string, options?: RequestOptions): APIPromise<Stream<DeploymentFollowResponse>> {
|
|
39
|
+
return this._client.get(path`/apps/${id}/events`, {
|
|
40
|
+
...options,
|
|
41
|
+
headers: buildHeaders([{ Accept: 'text/event-stream' }, options?.headers]),
|
|
42
|
+
stream: true,
|
|
43
|
+
}) as APIPromise<Stream<DeploymentFollowResponse>>;
|
|
44
|
+
}
|
|
24
45
|
}
|
|
25
46
|
|
|
26
47
|
export interface DeploymentCreateResponse {
|
|
@@ -68,6 +89,77 @@ export namespace DeploymentCreateResponse {
|
|
|
68
89
|
}
|
|
69
90
|
}
|
|
70
91
|
|
|
92
|
+
/**
|
|
93
|
+
* A stream of application events (state updates and logs) in SSE format.
|
|
94
|
+
*/
|
|
95
|
+
export type DeploymentFollowResponse = Array<
|
|
96
|
+
| DeploymentFollowResponse.StateEvent
|
|
97
|
+
| DeploymentFollowResponse.StateUpdateEvent
|
|
98
|
+
| DeploymentFollowResponse.LogEvent
|
|
99
|
+
>;
|
|
100
|
+
|
|
101
|
+
export namespace DeploymentFollowResponse {
|
|
102
|
+
/**
|
|
103
|
+
* Initial state of the application, emitted once when subscribing.
|
|
104
|
+
*/
|
|
105
|
+
export interface StateEvent {
|
|
106
|
+
/**
|
|
107
|
+
* Event type identifier (always "state").
|
|
108
|
+
*/
|
|
109
|
+
event: 'state';
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Current application state (e.g., "deploying", "running", "succeeded", "failed").
|
|
113
|
+
*/
|
|
114
|
+
state: string;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Time the state was reported.
|
|
118
|
+
*/
|
|
119
|
+
timestamp?: string;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* An update emitted when the application's state changes.
|
|
124
|
+
*/
|
|
125
|
+
export interface StateUpdateEvent {
|
|
126
|
+
/**
|
|
127
|
+
* Event type identifier (always "state_update").
|
|
128
|
+
*/
|
|
129
|
+
event: 'state_update';
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* New application state (e.g., "running", "succeeded", "failed").
|
|
133
|
+
*/
|
|
134
|
+
state: string;
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Time the state change occurred.
|
|
138
|
+
*/
|
|
139
|
+
timestamp?: string;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* A log entry from the application.
|
|
144
|
+
*/
|
|
145
|
+
export interface LogEvent {
|
|
146
|
+
/**
|
|
147
|
+
* Event type identifier (always "log").
|
|
148
|
+
*/
|
|
149
|
+
event: 'log';
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Log message text.
|
|
153
|
+
*/
|
|
154
|
+
message: string;
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Time the log entry was produced.
|
|
158
|
+
*/
|
|
159
|
+
timestamp?: string;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
71
163
|
export interface DeploymentCreateParams {
|
|
72
164
|
/**
|
|
73
165
|
* Relative path to the entrypoint of the application
|
|
@@ -104,6 +196,7 @@ export interface DeploymentCreateParams {
|
|
|
104
196
|
export declare namespace Deployments {
|
|
105
197
|
export {
|
|
106
198
|
type DeploymentCreateResponse as DeploymentCreateResponse,
|
|
199
|
+
type DeploymentFollowResponse as DeploymentFollowResponse,
|
|
107
200
|
type DeploymentCreateParams as DeploymentCreateParams,
|
|
108
201
|
};
|
|
109
202
|
}
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
2
|
|
|
3
3
|
export { Apps } from './apps';
|
|
4
|
-
export {
|
|
4
|
+
export {
|
|
5
|
+
Deployments,
|
|
6
|
+
type DeploymentCreateResponse,
|
|
7
|
+
type DeploymentFollowResponse,
|
|
8
|
+
type DeploymentCreateParams,
|
|
9
|
+
} from './deployments';
|
|
5
10
|
export {
|
|
6
11
|
Invocations,
|
|
7
12
|
type InvocationCreateResponse,
|
package/src/streaming.ts
ADDED
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '0.1.0-alpha.
|
|
1
|
+
export const VERSION = '0.1.0-alpha.14'; // x-release-please-version
|
package/streaming.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"streaming.d.mts","sourceRoot":"","sources":["src/streaming.ts"],"names":[],"mappings":""}
|
package/streaming.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"streaming.d.ts","sourceRoot":"","sources":["src/streaming.ts"],"names":[],"mappings":""}
|
package/streaming.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("./internal/tslib.js");
|
|
4
|
+
/** @deprecated Import from ./core/streaming instead */
|
|
5
|
+
tslib_1.__exportStar(require("./core/streaming.js"), exports);
|
|
6
|
+
//# sourceMappingURL=streaming.js.map
|
package/streaming.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"streaming.js","sourceRoot":"","sources":["src/streaming.ts"],"names":[],"mappings":";;;AAAA,uDAAuD;AACvD,8DAAiC"}
|
package/streaming.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"streaming.mjs","sourceRoot":"","sources":["src/streaming.ts"],"names":[],"mappings":""}
|
package/version.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const VERSION = "0.1.0-alpha.
|
|
1
|
+
export declare const VERSION = "0.1.0-alpha.14";
|
|
2
2
|
//# sourceMappingURL=version.d.mts.map
|
package/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const VERSION = "0.1.0-alpha.
|
|
1
|
+
export declare const VERSION = "0.1.0-alpha.14";
|
|
2
2
|
//# sourceMappingURL=version.d.ts.map
|