@onkernel/sdk 0.1.0-alpha.9 → 0.1.0
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 +72 -0
- package/README.md +42 -29
- package/client.d.mts +5 -5
- package/client.d.mts.map +1 -1
- package/client.d.ts +5 -5
- package/client.d.ts.map +1 -1
- package/client.js +4 -4
- package/client.js.map +1 -1
- package/client.mjs +4 -4
- package/client.mjs.map +1 -1
- 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/package.json +11 -4
- package/resources/apps/apps.d.mts +65 -0
- package/resources/apps/apps.d.mts.map +1 -0
- package/resources/apps/apps.d.ts +65 -0
- package/resources/apps/apps.d.ts.map +1 -0
- package/resources/apps/apps.js +33 -0
- package/resources/apps/apps.js.map +1 -0
- package/resources/apps/apps.mjs +28 -0
- package/resources/apps/apps.mjs.map +1 -0
- package/resources/apps/deployments.d.mts +157 -0
- package/resources/apps/deployments.d.mts.map +1 -0
- package/resources/apps/deployments.d.ts +157 -0
- package/resources/apps/deployments.d.ts.map +1 -0
- package/resources/apps/deployments.js +43 -0
- package/resources/apps/deployments.js.map +1 -0
- package/resources/apps/deployments.mjs +39 -0
- package/resources/apps/deployments.mjs.map +1 -0
- package/resources/apps/index.d.mts +4 -0
- package/resources/apps/index.d.mts.map +1 -0
- package/resources/apps/index.d.ts +4 -0
- package/resources/apps/index.d.ts.map +1 -0
- package/resources/apps/index.js +11 -0
- package/resources/apps/index.js.map +1 -0
- package/resources/apps/index.mjs +5 -0
- package/resources/apps/index.mjs.map +1 -0
- package/resources/apps/invocations.d.mts +110 -0
- package/resources/apps/invocations.d.mts.map +1 -0
- package/resources/apps/invocations.d.ts +110 -0
- package/resources/apps/invocations.d.ts.map +1 -0
- package/resources/apps/invocations.js +38 -0
- package/resources/apps/invocations.js.map +1 -0
- package/resources/apps/invocations.mjs +34 -0
- package/resources/apps/invocations.mjs.map +1 -0
- package/resources/apps.d.mts +1 -140
- package/resources/apps.d.mts.map +1 -1
- package/resources/apps.d.ts +1 -140
- package/resources/apps.d.ts.map +1 -1
- package/resources/apps.js +2 -49
- package/resources/apps.js.map +1 -1
- package/resources/apps.mjs +1 -47
- package/resources/apps.mjs.map +1 -1
- package/resources/browsers.d.mts +65 -0
- package/resources/browsers.d.mts.map +1 -0
- package/resources/browsers.d.ts +65 -0
- package/resources/browsers.d.ts.map +1 -0
- package/resources/browsers.js +36 -0
- package/resources/browsers.js.map +1 -0
- package/resources/browsers.mjs +32 -0
- package/resources/browsers.mjs.map +1 -0
- package/resources/index.d.mts +2 -2
- package/resources/index.d.mts.map +1 -1
- package/resources/index.d.ts +2 -2
- package/resources/index.d.ts.map +1 -1
- package/resources/index.js +4 -4
- package/resources/index.js.map +1 -1
- package/resources/index.mjs +2 -2
- package/resources/index.mjs.map +1 -1
- package/src/client.ts +13 -21
- 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 +104 -0
- package/src/resources/apps/deployments.ts +202 -0
- package/src/resources/apps/index.ts +15 -0
- package/src/resources/apps/invocations.ts +140 -0
- package/src/resources/apps.ts +1 -184
- package/src/resources/browsers.ts +85 -0
- package/src/resources/index.ts +6 -8
- 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.mts.map +1 -1
- package/version.d.ts +1 -1
- package/version.d.ts.map +1 -1
- package/version.js +1 -1
- package/version.js.map +1 -1
- package/version.mjs +1 -1
- package/version.mjs.map +1 -1
- package/resources/browser.d.mts +0 -33
- package/resources/browser.d.mts.map +0 -1
- package/resources/browser.d.ts +0 -33
- package/resources/browser.d.ts.map +0 -1
- package/resources/browser.js +0 -15
- package/resources/browser.js.map +0 -1
- package/resources/browser.mjs +0 -11
- package/resources/browser.mjs.map +0 -1
- package/src/resources/browser.ts +0 -48
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
export interface KernelContext {
|
|
2
|
-
|
|
2
|
+
invocation_id: string;
|
|
3
3
|
}
|
|
4
4
|
|
|
5
5
|
export interface KernelAction {
|
|
6
6
|
name: string;
|
|
7
|
-
handler: (context: KernelContext,
|
|
7
|
+
handler: (context: KernelContext, payload?: any) => Promise<any>;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
export interface KernelJson {
|
|
@@ -34,45 +34,28 @@ export class KernelApp {
|
|
|
34
34
|
* Define an action
|
|
35
35
|
*/
|
|
36
36
|
action<T, R>(
|
|
37
|
-
|
|
38
|
-
handler
|
|
37
|
+
name: string,
|
|
38
|
+
handler: ((context: KernelContext) => Promise<R>) | ((context: KernelContext, payload?: T) => Promise<R>),
|
|
39
39
|
) {
|
|
40
|
-
let
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
if (
|
|
44
|
-
//
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
//
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const singleArgHandler = handler as (input: T) => Promise<R>;
|
|
51
|
-
actionHandler = async (context: KernelContext, input: T) => singleArgHandler(input);
|
|
52
|
-
} else {
|
|
53
|
-
// Handler takes both context and input
|
|
54
|
-
actionHandler = handler as (context: KernelContext, input: T) => Promise<R>;
|
|
55
|
-
}
|
|
56
|
-
} else if (typeof nameOrHandler === 'function') {
|
|
57
|
-
// Case: app.action(handler)
|
|
58
|
-
actionName = nameOrHandler.name || 'default';
|
|
59
|
-
|
|
60
|
-
// Create a handler that accepts context and input, adapting if needed
|
|
61
|
-
if (nameOrHandler.length === 1) {
|
|
62
|
-
// Original handler only takes input, so we adapt it to the new signature
|
|
63
|
-
const singleArgHandler = nameOrHandler as (input: T) => Promise<R>;
|
|
64
|
-
actionHandler = async (context: KernelContext, input: T) => singleArgHandler(input);
|
|
65
|
-
} else {
|
|
66
|
-
// Handler takes both context and input
|
|
67
|
-
actionHandler = nameOrHandler as (context: KernelContext, input: T) => Promise<R>;
|
|
68
|
-
}
|
|
40
|
+
let actionHandler: (context: KernelContext, payload?: T) => Promise<R>;
|
|
41
|
+
|
|
42
|
+
// Create a handler that accepts context and payload, adapting if needed
|
|
43
|
+
if (handler.length === 0) {
|
|
44
|
+
// Handlers with no arguments are not supported
|
|
45
|
+
throw new Error('Action handlers must accept at least the context parameter');
|
|
46
|
+
} else if (handler.length === 1) {
|
|
47
|
+
// Handler takes context only
|
|
48
|
+
const contextOnlyHandler = handler as (context: KernelContext) => Promise<R>;
|
|
49
|
+
actionHandler = async (context: KernelContext, _payload?: T) => contextOnlyHandler(context);
|
|
69
50
|
} else {
|
|
70
|
-
|
|
51
|
+
// Handler takes both context and payload
|
|
52
|
+
const twoArgHandler = handler as (context: KernelContext, payload?: T) => Promise<R>;
|
|
53
|
+
actionHandler = twoArgHandler;
|
|
71
54
|
}
|
|
72
55
|
|
|
73
56
|
// Register the action
|
|
74
|
-
this.actions.set(
|
|
75
|
-
name
|
|
57
|
+
this.actions.set(name, {
|
|
58
|
+
name,
|
|
76
59
|
handler: actionHandler,
|
|
77
60
|
});
|
|
78
61
|
|
|
@@ -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 };
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import { APIResource } from '../../core/resource';
|
|
4
|
+
import * as DeploymentsAPI from './deployments';
|
|
5
|
+
import {
|
|
6
|
+
DeploymentCreateParams,
|
|
7
|
+
DeploymentCreateResponse,
|
|
8
|
+
DeploymentFollowResponse,
|
|
9
|
+
Deployments,
|
|
10
|
+
} from './deployments';
|
|
11
|
+
import * as InvocationsAPI from './invocations';
|
|
12
|
+
import {
|
|
13
|
+
InvocationCreateParams,
|
|
14
|
+
InvocationCreateResponse,
|
|
15
|
+
InvocationRetrieveResponse,
|
|
16
|
+
Invocations,
|
|
17
|
+
} from './invocations';
|
|
18
|
+
import { APIPromise } from '../../core/api-promise';
|
|
19
|
+
import { RequestOptions } from '../../internal/request-options';
|
|
20
|
+
|
|
21
|
+
export class Apps extends APIResource {
|
|
22
|
+
deployments: DeploymentsAPI.Deployments = new DeploymentsAPI.Deployments(this._client);
|
|
23
|
+
invocations: InvocationsAPI.Invocations = new InvocationsAPI.Invocations(this._client);
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* List application versions for the authenticated user. Optionally filter by app
|
|
27
|
+
* name and/or version label.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* const apps = await client.apps.list();
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
list(query: AppListParams | null | undefined = {}, options?: RequestOptions): APIPromise<AppListResponse> {
|
|
35
|
+
return this._client.get('/apps', { query, ...options });
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type AppListResponse = Array<AppListResponse.AppListResponseItem>;
|
|
40
|
+
|
|
41
|
+
export namespace AppListResponse {
|
|
42
|
+
/**
|
|
43
|
+
* Summary of an application version.
|
|
44
|
+
*/
|
|
45
|
+
export interface AppListResponseItem {
|
|
46
|
+
/**
|
|
47
|
+
* Unique identifier for the app version
|
|
48
|
+
*/
|
|
49
|
+
id: string;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Name of the application
|
|
53
|
+
*/
|
|
54
|
+
app_name: string;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Deployment region code
|
|
58
|
+
*/
|
|
59
|
+
region: string;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Version label for the application
|
|
63
|
+
*/
|
|
64
|
+
version: string;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Environment variables configured for this app version
|
|
68
|
+
*/
|
|
69
|
+
env_vars?: Record<string, string>;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface AppListParams {
|
|
74
|
+
/**
|
|
75
|
+
* Filter results by application name.
|
|
76
|
+
*/
|
|
77
|
+
app_name?: string;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Filter results by version label.
|
|
81
|
+
*/
|
|
82
|
+
version?: string;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
Apps.Deployments = Deployments;
|
|
86
|
+
Apps.Invocations = Invocations;
|
|
87
|
+
|
|
88
|
+
export declare namespace Apps {
|
|
89
|
+
export { type AppListResponse as AppListResponse, type AppListParams as AppListParams };
|
|
90
|
+
|
|
91
|
+
export {
|
|
92
|
+
Deployments as Deployments,
|
|
93
|
+
type DeploymentCreateResponse as DeploymentCreateResponse,
|
|
94
|
+
type DeploymentFollowResponse as DeploymentFollowResponse,
|
|
95
|
+
type DeploymentCreateParams as DeploymentCreateParams,
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
export {
|
|
99
|
+
Invocations as Invocations,
|
|
100
|
+
type InvocationCreateResponse as InvocationCreateResponse,
|
|
101
|
+
type InvocationRetrieveResponse as InvocationRetrieveResponse,
|
|
102
|
+
type InvocationCreateParams as InvocationCreateParams,
|
|
103
|
+
};
|
|
104
|
+
}
|