@temporary-name/standard-server 1.9.3-alpha.03e17aa3340f4b261a549bf9e3fe948b084e2309
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/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/batch/index.d.mts +30 -0
- package/dist/batch/index.d.ts +30 -0
- package/dist/batch/index.mjs +156 -0
- package/dist/index.d.mts +80 -0
- package/dist/index.d.ts +80 -0
- package/dist/index.mjs +313 -0
- package/dist/shared/standard-server.BD8Sg1A5.d.mts +38 -0
- package/dist/shared/standard-server.BD8Sg1A5.d.ts +38 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Stainless
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { S as StandardHeaders, c as StandardRequest, e as StandardResponse } from '../shared/standard-server.BD8Sg1A5.mjs';
|
|
2
|
+
import { Promisable } from '@temporary-name/shared';
|
|
3
|
+
|
|
4
|
+
interface ToBatchRequestOptions {
|
|
5
|
+
url: URL;
|
|
6
|
+
method: 'GET' | 'POST';
|
|
7
|
+
headers: StandardHeaders;
|
|
8
|
+
requests: readonly StandardRequest[];
|
|
9
|
+
}
|
|
10
|
+
declare function toBatchRequest(options: ToBatchRequestOptions): StandardRequest;
|
|
11
|
+
declare function parseBatchRequest(request: StandardRequest): StandardRequest[];
|
|
12
|
+
|
|
13
|
+
type BatchResponseMode = 'streaming' | 'buffered';
|
|
14
|
+
interface BatchResponseBodyItem extends StandardResponse {
|
|
15
|
+
index: number;
|
|
16
|
+
}
|
|
17
|
+
interface ToBatchResponseOptions extends StandardResponse {
|
|
18
|
+
body: AsyncIteratorObject<BatchResponseBodyItem>;
|
|
19
|
+
/**
|
|
20
|
+
* @default 'streaming'
|
|
21
|
+
*/
|
|
22
|
+
mode?: BatchResponseMode;
|
|
23
|
+
}
|
|
24
|
+
declare function toBatchResponse(options: ToBatchResponseOptions): Promisable<StandardResponse>;
|
|
25
|
+
declare function parseBatchResponse(response: StandardResponse): AsyncGenerator<BatchResponseBodyItem>;
|
|
26
|
+
|
|
27
|
+
declare function toBatchAbortSignal(signals: readonly (AbortSignal | undefined)[]): AbortSignal | undefined;
|
|
28
|
+
|
|
29
|
+
export { parseBatchRequest, parseBatchResponse, toBatchAbortSignal, toBatchRequest, toBatchResponse };
|
|
30
|
+
export type { BatchResponseBodyItem, BatchResponseMode, ToBatchRequestOptions, ToBatchResponseOptions };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { S as StandardHeaders, c as StandardRequest, e as StandardResponse } from '../shared/standard-server.BD8Sg1A5.js';
|
|
2
|
+
import { Promisable } from '@temporary-name/shared';
|
|
3
|
+
|
|
4
|
+
interface ToBatchRequestOptions {
|
|
5
|
+
url: URL;
|
|
6
|
+
method: 'GET' | 'POST';
|
|
7
|
+
headers: StandardHeaders;
|
|
8
|
+
requests: readonly StandardRequest[];
|
|
9
|
+
}
|
|
10
|
+
declare function toBatchRequest(options: ToBatchRequestOptions): StandardRequest;
|
|
11
|
+
declare function parseBatchRequest(request: StandardRequest): StandardRequest[];
|
|
12
|
+
|
|
13
|
+
type BatchResponseMode = 'streaming' | 'buffered';
|
|
14
|
+
interface BatchResponseBodyItem extends StandardResponse {
|
|
15
|
+
index: number;
|
|
16
|
+
}
|
|
17
|
+
interface ToBatchResponseOptions extends StandardResponse {
|
|
18
|
+
body: AsyncIteratorObject<BatchResponseBodyItem>;
|
|
19
|
+
/**
|
|
20
|
+
* @default 'streaming'
|
|
21
|
+
*/
|
|
22
|
+
mode?: BatchResponseMode;
|
|
23
|
+
}
|
|
24
|
+
declare function toBatchResponse(options: ToBatchResponseOptions): Promisable<StandardResponse>;
|
|
25
|
+
declare function parseBatchResponse(response: StandardResponse): AsyncGenerator<BatchResponseBodyItem>;
|
|
26
|
+
|
|
27
|
+
declare function toBatchAbortSignal(signals: readonly (AbortSignal | undefined)[]): AbortSignal | undefined;
|
|
28
|
+
|
|
29
|
+
export { parseBatchRequest, parseBatchResponse, toBatchAbortSignal, toBatchRequest, toBatchResponse };
|
|
30
|
+
export type { BatchResponseBodyItem, BatchResponseMode, ToBatchRequestOptions, ToBatchResponseOptions };
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { stringifyJSON, parseEmptyableJSON, AsyncIteratorClass, isAsyncIteratorObject, isObject } from '@temporary-name/shared';
|
|
2
|
+
|
|
3
|
+
function toBatchAbortSignal(signals) {
|
|
4
|
+
const realSignals = signals.filter((signal) => signal !== void 0);
|
|
5
|
+
if (realSignals.length === 0 || realSignals.length !== signals.length) {
|
|
6
|
+
return void 0;
|
|
7
|
+
}
|
|
8
|
+
const controller = new AbortController();
|
|
9
|
+
const abortIfAllInputsAborted = () => {
|
|
10
|
+
if (realSignals.every((signal) => signal.aborted)) {
|
|
11
|
+
controller.abort();
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
abortIfAllInputsAborted();
|
|
15
|
+
for (const signal of realSignals) {
|
|
16
|
+
signal.addEventListener(
|
|
17
|
+
"abort",
|
|
18
|
+
() => {
|
|
19
|
+
abortIfAllInputsAborted();
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
once: true,
|
|
23
|
+
signal: controller.signal
|
|
24
|
+
}
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
return controller.signal;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function toBatchRequest(options) {
|
|
31
|
+
const url = new URL(options.url);
|
|
32
|
+
let body;
|
|
33
|
+
const batchRequestItems = options.requests.map(
|
|
34
|
+
(request) => ({
|
|
35
|
+
body: request.body,
|
|
36
|
+
headers: Object.keys(request.headers).length ? request.headers : void 0,
|
|
37
|
+
method: request.method === options.method ? void 0 : request.method,
|
|
38
|
+
url: request.url
|
|
39
|
+
})
|
|
40
|
+
);
|
|
41
|
+
if (options.method === "GET") {
|
|
42
|
+
url.searchParams.append("batch", stringifyJSON(batchRequestItems));
|
|
43
|
+
} else if (options.method === "POST") {
|
|
44
|
+
body = batchRequestItems;
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
method: options.method,
|
|
48
|
+
url,
|
|
49
|
+
headers: options.headers,
|
|
50
|
+
body,
|
|
51
|
+
signal: toBatchAbortSignal(options.requests.map((request) => request.signal))
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function parseBatchRequest(request) {
|
|
55
|
+
const items = request.method === "GET" ? parseEmptyableJSON(request.url.searchParams.getAll("batch").at(-1)) : request.body;
|
|
56
|
+
if (!Array.isArray(items)) {
|
|
57
|
+
throw new TypeError("Invalid batch request");
|
|
58
|
+
}
|
|
59
|
+
return items.map((item) => {
|
|
60
|
+
return {
|
|
61
|
+
method: item.method ?? request.method,
|
|
62
|
+
url: new URL(item.url),
|
|
63
|
+
headers: item.headers ?? {},
|
|
64
|
+
body: item.body,
|
|
65
|
+
signal: request.signal
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function toBatchResponse(options) {
|
|
71
|
+
const mode = options.mode ?? "streaming";
|
|
72
|
+
const minifyResponseItem = (item) => {
|
|
73
|
+
return {
|
|
74
|
+
index: item.index,
|
|
75
|
+
status: item.status === options.status ? void 0 : item.status,
|
|
76
|
+
headers: Object.keys(item.headers).length ? item.headers : void 0,
|
|
77
|
+
body: item.body
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
if (mode === "buffered") {
|
|
81
|
+
return (async () => {
|
|
82
|
+
try {
|
|
83
|
+
const body = [];
|
|
84
|
+
for await (const item of options.body) {
|
|
85
|
+
body.push(minifyResponseItem(item));
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
headers: options.headers,
|
|
89
|
+
status: options.status,
|
|
90
|
+
body
|
|
91
|
+
};
|
|
92
|
+
} finally {
|
|
93
|
+
await options.body.return?.();
|
|
94
|
+
}
|
|
95
|
+
})();
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
headers: options.headers,
|
|
99
|
+
status: options.status,
|
|
100
|
+
body: new AsyncIteratorClass(
|
|
101
|
+
async () => {
|
|
102
|
+
const { done, value } = await options.body.next();
|
|
103
|
+
if (done) {
|
|
104
|
+
return { done, value };
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
done,
|
|
108
|
+
value: {
|
|
109
|
+
index: value.index,
|
|
110
|
+
status: value.status === options.status ? void 0 : value.status,
|
|
111
|
+
headers: Object.keys(value.headers).length ? value.headers : void 0,
|
|
112
|
+
body: value.body
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
},
|
|
116
|
+
async (reason) => {
|
|
117
|
+
if (reason !== "next") {
|
|
118
|
+
await options.body.return?.();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
)
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
function parseBatchResponse(response) {
|
|
125
|
+
const body = response.body;
|
|
126
|
+
if (isAsyncIteratorObject(body) || Array.isArray(body)) {
|
|
127
|
+
const iterator = (async function* () {
|
|
128
|
+
for await (const item of body) {
|
|
129
|
+
if (!isObject(item) || !("index" in item) || typeof item.index !== "number") {
|
|
130
|
+
throw new TypeError("Invalid batch response", {
|
|
131
|
+
cause: item
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
yield {
|
|
135
|
+
index: item.index,
|
|
136
|
+
status: item.status ?? response.status,
|
|
137
|
+
headers: item.headers ?? {},
|
|
138
|
+
body: item.body
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
})();
|
|
142
|
+
return new AsyncIteratorClass(
|
|
143
|
+
() => iterator.next(),
|
|
144
|
+
async (reason) => {
|
|
145
|
+
if (reason !== "next" && isAsyncIteratorObject(body)) {
|
|
146
|
+
await body.return?.();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
throw new TypeError("Invalid batch response", {
|
|
152
|
+
cause: response
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export { parseBatchRequest, parseBatchResponse, toBatchAbortSignal, toBatchRequest, toBatchResponse };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { AsyncIteratorClass } from '@temporary-name/shared';
|
|
2
|
+
import { S as StandardHeaders, a as StandardLazyResponse } from './shared/standard-server.BD8Sg1A5.mjs';
|
|
3
|
+
export { b as StandardBody, d as StandardLazyRequest, c as StandardRequest, e as StandardResponse } from './shared/standard-server.BD8Sg1A5.mjs';
|
|
4
|
+
|
|
5
|
+
interface EventMessage {
|
|
6
|
+
event: string | undefined;
|
|
7
|
+
id: string | undefined;
|
|
8
|
+
data: string | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* The number of milliseconds to wait before retrying the event iterator if error occurs.
|
|
11
|
+
*/
|
|
12
|
+
retry: number | undefined;
|
|
13
|
+
comments: string[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare function decodeEventMessage(encoded: string): EventMessage;
|
|
17
|
+
interface EventDecoderOptions {
|
|
18
|
+
onEvent?: (event: EventMessage) => void;
|
|
19
|
+
}
|
|
20
|
+
declare class EventDecoder {
|
|
21
|
+
private options;
|
|
22
|
+
private incomplete;
|
|
23
|
+
constructor(options?: EventDecoderOptions);
|
|
24
|
+
feed(chunk: string): void;
|
|
25
|
+
end(): void;
|
|
26
|
+
}
|
|
27
|
+
declare class EventDecoderStream extends TransformStream<string, EventMessage> {
|
|
28
|
+
constructor();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
declare function assertEventId(id: string): void;
|
|
32
|
+
declare function assertEventName(event: string): void;
|
|
33
|
+
declare function assertEventRetry(retry: number): void;
|
|
34
|
+
declare function assertEventComment(comment: string): void;
|
|
35
|
+
declare function encodeEventData(data: string | undefined): string;
|
|
36
|
+
declare function encodeEventComments(comments: string[] | undefined): string;
|
|
37
|
+
declare function encodeEventMessage(message: Partial<EventMessage>): string;
|
|
38
|
+
|
|
39
|
+
declare class EventEncoderError extends TypeError {
|
|
40
|
+
}
|
|
41
|
+
declare class EventDecoderError extends TypeError {
|
|
42
|
+
}
|
|
43
|
+
interface ErrorEventOptions extends ErrorOptions {
|
|
44
|
+
message?: string;
|
|
45
|
+
data?: unknown;
|
|
46
|
+
}
|
|
47
|
+
declare class ErrorEvent extends Error {
|
|
48
|
+
data: unknown;
|
|
49
|
+
constructor(options?: ErrorEventOptions);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
type EventMeta = Partial<Pick<EventMessage, 'retry' | 'id' | 'comments'>>;
|
|
53
|
+
declare function withEventMeta<T extends object>(container: T, meta: EventMeta): T;
|
|
54
|
+
declare function getEventMeta(container: unknown): EventMeta | undefined;
|
|
55
|
+
|
|
56
|
+
declare function mapEventIterator<TYield, TReturn, TNext, TMap = TYield | TReturn>(iterator: AsyncIterator<TYield, TReturn, TNext>, maps: {
|
|
57
|
+
value: (value: NoInfer<TYield | TReturn>, done: boolean | undefined) => Promise<TMap>;
|
|
58
|
+
error: (error: unknown) => Promise<unknown>;
|
|
59
|
+
}): AsyncIteratorClass<TMap, TMap, TNext>;
|
|
60
|
+
|
|
61
|
+
interface HibernationEventIteratorCallback {
|
|
62
|
+
(id: string): void;
|
|
63
|
+
}
|
|
64
|
+
declare class HibernationEventIterator<T, TReturn = unknown, TNext = unknown> extends AsyncIteratorClass<T, TReturn, TNext> {
|
|
65
|
+
/**
|
|
66
|
+
* this property is not transferred to the client, so it should be optional for type safety
|
|
67
|
+
*/
|
|
68
|
+
readonly hibernationCallback?: HibernationEventIteratorCallback;
|
|
69
|
+
constructor(hibernationCallback: HibernationEventIteratorCallback);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
declare function generateContentDisposition(filename: string): string;
|
|
73
|
+
declare function getFilenameFromContentDisposition(contentDisposition: string): string | undefined;
|
|
74
|
+
declare function mergeStandardHeaders(a: StandardHeaders, b: StandardHeaders): StandardHeaders;
|
|
75
|
+
declare function flattenHeader(header: string | readonly string[] | undefined): string | undefined;
|
|
76
|
+
declare function replicateStandardLazyResponse(response: StandardLazyResponse, count: number): StandardLazyResponse[];
|
|
77
|
+
declare function isEventIteratorHeaders(headers: StandardHeaders): boolean;
|
|
78
|
+
|
|
79
|
+
export { ErrorEvent, EventDecoder, EventDecoderError, EventDecoderStream, EventEncoderError, HibernationEventIterator, StandardHeaders, StandardLazyResponse, assertEventComment, assertEventId, assertEventName, assertEventRetry, decodeEventMessage, encodeEventComments, encodeEventData, encodeEventMessage, flattenHeader, generateContentDisposition, getEventMeta, getFilenameFromContentDisposition, isEventIteratorHeaders, mapEventIterator, mergeStandardHeaders, replicateStandardLazyResponse, withEventMeta };
|
|
80
|
+
export type { ErrorEventOptions, EventDecoderOptions, EventMessage, EventMeta, HibernationEventIteratorCallback };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { AsyncIteratorClass } from '@temporary-name/shared';
|
|
2
|
+
import { S as StandardHeaders, a as StandardLazyResponse } from './shared/standard-server.BD8Sg1A5.js';
|
|
3
|
+
export { b as StandardBody, d as StandardLazyRequest, c as StandardRequest, e as StandardResponse } from './shared/standard-server.BD8Sg1A5.js';
|
|
4
|
+
|
|
5
|
+
interface EventMessage {
|
|
6
|
+
event: string | undefined;
|
|
7
|
+
id: string | undefined;
|
|
8
|
+
data: string | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* The number of milliseconds to wait before retrying the event iterator if error occurs.
|
|
11
|
+
*/
|
|
12
|
+
retry: number | undefined;
|
|
13
|
+
comments: string[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare function decodeEventMessage(encoded: string): EventMessage;
|
|
17
|
+
interface EventDecoderOptions {
|
|
18
|
+
onEvent?: (event: EventMessage) => void;
|
|
19
|
+
}
|
|
20
|
+
declare class EventDecoder {
|
|
21
|
+
private options;
|
|
22
|
+
private incomplete;
|
|
23
|
+
constructor(options?: EventDecoderOptions);
|
|
24
|
+
feed(chunk: string): void;
|
|
25
|
+
end(): void;
|
|
26
|
+
}
|
|
27
|
+
declare class EventDecoderStream extends TransformStream<string, EventMessage> {
|
|
28
|
+
constructor();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
declare function assertEventId(id: string): void;
|
|
32
|
+
declare function assertEventName(event: string): void;
|
|
33
|
+
declare function assertEventRetry(retry: number): void;
|
|
34
|
+
declare function assertEventComment(comment: string): void;
|
|
35
|
+
declare function encodeEventData(data: string | undefined): string;
|
|
36
|
+
declare function encodeEventComments(comments: string[] | undefined): string;
|
|
37
|
+
declare function encodeEventMessage(message: Partial<EventMessage>): string;
|
|
38
|
+
|
|
39
|
+
declare class EventEncoderError extends TypeError {
|
|
40
|
+
}
|
|
41
|
+
declare class EventDecoderError extends TypeError {
|
|
42
|
+
}
|
|
43
|
+
interface ErrorEventOptions extends ErrorOptions {
|
|
44
|
+
message?: string;
|
|
45
|
+
data?: unknown;
|
|
46
|
+
}
|
|
47
|
+
declare class ErrorEvent extends Error {
|
|
48
|
+
data: unknown;
|
|
49
|
+
constructor(options?: ErrorEventOptions);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
type EventMeta = Partial<Pick<EventMessage, 'retry' | 'id' | 'comments'>>;
|
|
53
|
+
declare function withEventMeta<T extends object>(container: T, meta: EventMeta): T;
|
|
54
|
+
declare function getEventMeta(container: unknown): EventMeta | undefined;
|
|
55
|
+
|
|
56
|
+
declare function mapEventIterator<TYield, TReturn, TNext, TMap = TYield | TReturn>(iterator: AsyncIterator<TYield, TReturn, TNext>, maps: {
|
|
57
|
+
value: (value: NoInfer<TYield | TReturn>, done: boolean | undefined) => Promise<TMap>;
|
|
58
|
+
error: (error: unknown) => Promise<unknown>;
|
|
59
|
+
}): AsyncIteratorClass<TMap, TMap, TNext>;
|
|
60
|
+
|
|
61
|
+
interface HibernationEventIteratorCallback {
|
|
62
|
+
(id: string): void;
|
|
63
|
+
}
|
|
64
|
+
declare class HibernationEventIterator<T, TReturn = unknown, TNext = unknown> extends AsyncIteratorClass<T, TReturn, TNext> {
|
|
65
|
+
/**
|
|
66
|
+
* this property is not transferred to the client, so it should be optional for type safety
|
|
67
|
+
*/
|
|
68
|
+
readonly hibernationCallback?: HibernationEventIteratorCallback;
|
|
69
|
+
constructor(hibernationCallback: HibernationEventIteratorCallback);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
declare function generateContentDisposition(filename: string): string;
|
|
73
|
+
declare function getFilenameFromContentDisposition(contentDisposition: string): string | undefined;
|
|
74
|
+
declare function mergeStandardHeaders(a: StandardHeaders, b: StandardHeaders): StandardHeaders;
|
|
75
|
+
declare function flattenHeader(header: string | readonly string[] | undefined): string | undefined;
|
|
76
|
+
declare function replicateStandardLazyResponse(response: StandardLazyResponse, count: number): StandardLazyResponse[];
|
|
77
|
+
declare function isEventIteratorHeaders(headers: StandardHeaders): boolean;
|
|
78
|
+
|
|
79
|
+
export { ErrorEvent, EventDecoder, EventDecoderError, EventDecoderStream, EventEncoderError, HibernationEventIterator, StandardHeaders, StandardLazyResponse, assertEventComment, assertEventId, assertEventName, assertEventRetry, decodeEventMessage, encodeEventComments, encodeEventData, encodeEventMessage, flattenHeader, generateContentDisposition, getEventMeta, getFilenameFromContentDisposition, isEventIteratorHeaders, mapEventIterator, mergeStandardHeaders, replicateStandardLazyResponse, withEventMeta };
|
|
80
|
+
export type { ErrorEventOptions, EventDecoderOptions, EventMessage, EventMeta, HibernationEventIteratorCallback };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { isTypescriptObject, AsyncIteratorClass, tryDecodeURIComponent, toArray, once, isAsyncIteratorObject, replicateAsyncIterator } from '@temporary-name/shared';
|
|
2
|
+
|
|
3
|
+
class EventEncoderError extends TypeError {
|
|
4
|
+
}
|
|
5
|
+
class EventDecoderError extends TypeError {
|
|
6
|
+
}
|
|
7
|
+
class ErrorEvent extends Error {
|
|
8
|
+
data;
|
|
9
|
+
constructor(options) {
|
|
10
|
+
super(options?.message ?? "An error event was received", options);
|
|
11
|
+
this.data = options?.data;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function decodeEventMessage(encoded) {
|
|
16
|
+
const lines = encoded.replace(/\n+$/, "").split(/\n/);
|
|
17
|
+
const message = {
|
|
18
|
+
data: void 0,
|
|
19
|
+
event: void 0,
|
|
20
|
+
id: void 0,
|
|
21
|
+
retry: void 0,
|
|
22
|
+
comments: []
|
|
23
|
+
};
|
|
24
|
+
for (const line of lines) {
|
|
25
|
+
const index = line.indexOf(":");
|
|
26
|
+
const key = index === -1 ? line : line.slice(0, index);
|
|
27
|
+
const value = index === -1 ? "" : line.slice(index + 1).replace(/^\s/, "");
|
|
28
|
+
if (index === 0) {
|
|
29
|
+
message.comments.push(value);
|
|
30
|
+
} else if (key === "data") {
|
|
31
|
+
message.data ??= "";
|
|
32
|
+
message.data += `${value}
|
|
33
|
+
`;
|
|
34
|
+
} else if (key === "event") {
|
|
35
|
+
message.event = value;
|
|
36
|
+
} else if (key === "id") {
|
|
37
|
+
message.id = value;
|
|
38
|
+
} else if (key === "retry") {
|
|
39
|
+
const maybeInteger = Number.parseInt(value);
|
|
40
|
+
if (Number.isInteger(maybeInteger) && maybeInteger >= 0 && maybeInteger.toString() === value) {
|
|
41
|
+
message.retry = maybeInteger;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
message.data = message.data?.replace(/\n$/, "");
|
|
46
|
+
return message;
|
|
47
|
+
}
|
|
48
|
+
class EventDecoder {
|
|
49
|
+
constructor(options = {}) {
|
|
50
|
+
this.options = options;
|
|
51
|
+
}
|
|
52
|
+
incomplete = "";
|
|
53
|
+
feed(chunk) {
|
|
54
|
+
this.incomplete += chunk;
|
|
55
|
+
const lastCompleteIndex = this.incomplete.lastIndexOf("\n\n");
|
|
56
|
+
if (lastCompleteIndex === -1) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const completes = this.incomplete.slice(0, lastCompleteIndex).split(/\n\n/);
|
|
60
|
+
this.incomplete = this.incomplete.slice(lastCompleteIndex + 2);
|
|
61
|
+
for (const encoded of completes) {
|
|
62
|
+
const message = decodeEventMessage(`${encoded}
|
|
63
|
+
|
|
64
|
+
`);
|
|
65
|
+
if (this.options.onEvent) {
|
|
66
|
+
this.options.onEvent(message);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
this.incomplete = "";
|
|
70
|
+
}
|
|
71
|
+
end() {
|
|
72
|
+
if (this.incomplete) {
|
|
73
|
+
throw new EventDecoderError("Event Iterator ended before complete");
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
class EventDecoderStream extends TransformStream {
|
|
78
|
+
constructor() {
|
|
79
|
+
let decoder;
|
|
80
|
+
super({
|
|
81
|
+
start(controller) {
|
|
82
|
+
decoder = new EventDecoder({
|
|
83
|
+
onEvent: (event) => {
|
|
84
|
+
controller.enqueue(event);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
},
|
|
88
|
+
transform(chunk) {
|
|
89
|
+
decoder.feed(chunk);
|
|
90
|
+
},
|
|
91
|
+
flush() {
|
|
92
|
+
decoder.end();
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function assertEventId(id) {
|
|
99
|
+
if (id.includes("\n")) {
|
|
100
|
+
throw new EventEncoderError("Event's id must not contain a newline character");
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function assertEventName(event) {
|
|
104
|
+
if (event.includes("\n")) {
|
|
105
|
+
throw new EventEncoderError("Event's event must not contain a newline character");
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function assertEventRetry(retry) {
|
|
109
|
+
if (!Number.isInteger(retry) || retry < 0) {
|
|
110
|
+
throw new EventEncoderError("Event's retry must be a integer and >= 0");
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function assertEventComment(comment) {
|
|
114
|
+
if (comment.includes("\n")) {
|
|
115
|
+
throw new EventEncoderError("Event's comment must not contain a newline character");
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function encodeEventData(data) {
|
|
119
|
+
const lines = data?.split(/\n/) ?? [];
|
|
120
|
+
let output = "";
|
|
121
|
+
for (const line of lines) {
|
|
122
|
+
output += `data: ${line}
|
|
123
|
+
`;
|
|
124
|
+
}
|
|
125
|
+
return output;
|
|
126
|
+
}
|
|
127
|
+
function encodeEventComments(comments) {
|
|
128
|
+
let output = "";
|
|
129
|
+
for (const comment of comments ?? []) {
|
|
130
|
+
assertEventComment(comment);
|
|
131
|
+
output += `: ${comment}
|
|
132
|
+
`;
|
|
133
|
+
}
|
|
134
|
+
return output;
|
|
135
|
+
}
|
|
136
|
+
function encodeEventMessage(message) {
|
|
137
|
+
let output = "";
|
|
138
|
+
output += encodeEventComments(message.comments);
|
|
139
|
+
if (message.event !== void 0) {
|
|
140
|
+
assertEventName(message.event);
|
|
141
|
+
output += `event: ${message.event}
|
|
142
|
+
`;
|
|
143
|
+
}
|
|
144
|
+
if (message.retry !== void 0) {
|
|
145
|
+
assertEventRetry(message.retry);
|
|
146
|
+
output += `retry: ${message.retry}
|
|
147
|
+
`;
|
|
148
|
+
}
|
|
149
|
+
if (message.id !== void 0) {
|
|
150
|
+
assertEventId(message.id);
|
|
151
|
+
output += `id: ${message.id}
|
|
152
|
+
`;
|
|
153
|
+
}
|
|
154
|
+
output += encodeEventData(message.data);
|
|
155
|
+
output += "\n";
|
|
156
|
+
return output;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const EVENT_SOURCE_META_SYMBOL = Symbol("ORPC_EVENT_SOURCE_META");
|
|
160
|
+
function withEventMeta(container, meta) {
|
|
161
|
+
if (meta.id === void 0 && meta.retry === void 0 && !meta.comments?.length) {
|
|
162
|
+
return container;
|
|
163
|
+
}
|
|
164
|
+
if (meta.id !== void 0) {
|
|
165
|
+
assertEventId(meta.id);
|
|
166
|
+
}
|
|
167
|
+
if (meta.retry !== void 0) {
|
|
168
|
+
assertEventRetry(meta.retry);
|
|
169
|
+
}
|
|
170
|
+
if (meta.comments !== void 0) {
|
|
171
|
+
for (const comment of meta.comments) {
|
|
172
|
+
assertEventComment(comment);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return new Proxy(container, {
|
|
176
|
+
get(target, prop, receiver) {
|
|
177
|
+
if (prop === EVENT_SOURCE_META_SYMBOL) {
|
|
178
|
+
return meta;
|
|
179
|
+
}
|
|
180
|
+
return Reflect.get(target, prop, receiver);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
function getEventMeta(container) {
|
|
185
|
+
return isTypescriptObject(container) ? Reflect.get(container, EVENT_SOURCE_META_SYMBOL) : void 0;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function mapEventIterator(iterator, maps) {
|
|
189
|
+
const mapError = async (error) => {
|
|
190
|
+
let mappedError = await maps.error(error);
|
|
191
|
+
if (mappedError !== error) {
|
|
192
|
+
const meta = getEventMeta(error);
|
|
193
|
+
if (meta && isTypescriptObject(mappedError)) {
|
|
194
|
+
mappedError = withEventMeta(mappedError, meta);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return mappedError;
|
|
198
|
+
};
|
|
199
|
+
return new AsyncIteratorClass(
|
|
200
|
+
async () => {
|
|
201
|
+
const { done, value } = await (async () => {
|
|
202
|
+
try {
|
|
203
|
+
return await iterator.next();
|
|
204
|
+
} catch (error) {
|
|
205
|
+
throw await mapError(error);
|
|
206
|
+
}
|
|
207
|
+
})();
|
|
208
|
+
let mappedValue = await maps.value(value, done);
|
|
209
|
+
if (mappedValue !== value) {
|
|
210
|
+
const meta = getEventMeta(value);
|
|
211
|
+
if (meta && isTypescriptObject(mappedValue)) {
|
|
212
|
+
mappedValue = withEventMeta(mappedValue, meta);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return { done, value: mappedValue };
|
|
216
|
+
},
|
|
217
|
+
async () => {
|
|
218
|
+
try {
|
|
219
|
+
await iterator.return?.();
|
|
220
|
+
} catch (error) {
|
|
221
|
+
throw await mapError(error);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
class HibernationEventIterator extends AsyncIteratorClass {
|
|
228
|
+
/**
|
|
229
|
+
* this property is not transferred to the client, so it should be optional for type safety
|
|
230
|
+
*/
|
|
231
|
+
hibernationCallback;
|
|
232
|
+
constructor(hibernationCallback) {
|
|
233
|
+
super(
|
|
234
|
+
async () => {
|
|
235
|
+
throw new Error("Cannot iterate over hibernating iterator directly");
|
|
236
|
+
},
|
|
237
|
+
async (reason) => {
|
|
238
|
+
if (reason !== "next") {
|
|
239
|
+
throw new Error("Cannot cleanup hibernating iterator directly");
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
);
|
|
243
|
+
this.hibernationCallback = hibernationCallback;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function generateContentDisposition(filename) {
|
|
248
|
+
const escapedFileName = filename.replace(/"/g, '\\"');
|
|
249
|
+
const encodedFilenameStar = encodeURIComponent(filename).replace(/['()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`).replace(/%(7C|60|5E)/g, (str, hex) => String.fromCharCode(Number.parseInt(hex, 16)));
|
|
250
|
+
return `inline; filename="${escapedFileName}"; filename*=utf-8''${encodedFilenameStar}`;
|
|
251
|
+
}
|
|
252
|
+
function getFilenameFromContentDisposition(contentDisposition) {
|
|
253
|
+
const encodedFilenameStarMatch = contentDisposition.match(/filename\*=(UTF-8'')?([^;]*)/i);
|
|
254
|
+
if (encodedFilenameStarMatch && typeof encodedFilenameStarMatch[2] === "string") {
|
|
255
|
+
return tryDecodeURIComponent(encodedFilenameStarMatch[2]);
|
|
256
|
+
}
|
|
257
|
+
const encodedFilenameMatch = contentDisposition.match(/filename="((?:\\"|[^"])*)"/i);
|
|
258
|
+
if (encodedFilenameMatch && typeof encodedFilenameMatch[1] === "string") {
|
|
259
|
+
return encodedFilenameMatch[1].replace(/\\"/g, '"');
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
function mergeStandardHeaders(a, b) {
|
|
263
|
+
const merged = { ...a };
|
|
264
|
+
for (const key in b) {
|
|
265
|
+
if (Array.isArray(b[key])) {
|
|
266
|
+
merged[key] = [...toArray(merged[key]), ...b[key]];
|
|
267
|
+
} else if (b[key] !== void 0) {
|
|
268
|
+
if (Array.isArray(merged[key])) {
|
|
269
|
+
merged[key] = [...merged[key], b[key]];
|
|
270
|
+
} else if (merged[key] !== void 0) {
|
|
271
|
+
merged[key] = [merged[key], b[key]];
|
|
272
|
+
} else {
|
|
273
|
+
merged[key] = b[key];
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return merged;
|
|
278
|
+
}
|
|
279
|
+
function flattenHeader(header) {
|
|
280
|
+
if (typeof header === "string" || header === void 0) {
|
|
281
|
+
return header;
|
|
282
|
+
}
|
|
283
|
+
if (header.length === 0) {
|
|
284
|
+
return void 0;
|
|
285
|
+
}
|
|
286
|
+
return header.join(", ");
|
|
287
|
+
}
|
|
288
|
+
function replicateStandardLazyResponse(response, count) {
|
|
289
|
+
const replicated = [];
|
|
290
|
+
let bodyPromise;
|
|
291
|
+
let replicatedAsyncIteratorObjects;
|
|
292
|
+
for (let i = 0; i < count; i++) {
|
|
293
|
+
replicated.push({
|
|
294
|
+
...response,
|
|
295
|
+
body: once(async () => {
|
|
296
|
+
const body = await (bodyPromise ??= response.body());
|
|
297
|
+
if (!isAsyncIteratorObject(body)) {
|
|
298
|
+
return body;
|
|
299
|
+
}
|
|
300
|
+
replicatedAsyncIteratorObjects ??= replicateAsyncIterator(body, count);
|
|
301
|
+
return replicatedAsyncIteratorObjects.shift();
|
|
302
|
+
})
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
return replicated;
|
|
306
|
+
}
|
|
307
|
+
function isEventIteratorHeaders(headers) {
|
|
308
|
+
return Boolean(
|
|
309
|
+
flattenHeader(headers["content-type"])?.startsWith("text/event-stream") && flattenHeader(headers["content-disposition"]) === void 0
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
export { ErrorEvent, EventDecoder, EventDecoderError, EventDecoderStream, EventEncoderError, HibernationEventIterator, assertEventComment, assertEventId, assertEventName, assertEventRetry, decodeEventMessage, encodeEventComments, encodeEventData, encodeEventMessage, flattenHeader, generateContentDisposition, getEventMeta, getFilenameFromContentDisposition, isEventIteratorHeaders, mapEventIterator, mergeStandardHeaders, replicateStandardLazyResponse, withEventMeta };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
interface StandardHeaders {
|
|
2
|
+
[key: string]: string | string[] | undefined;
|
|
3
|
+
}
|
|
4
|
+
type StandardBody = undefined | unknown | Blob | URLSearchParams | FormData | AsyncIterator<unknown | void, unknown | void, undefined>;
|
|
5
|
+
interface StandardRequest {
|
|
6
|
+
method: string;
|
|
7
|
+
url: URL;
|
|
8
|
+
headers: StandardHeaders;
|
|
9
|
+
/**
|
|
10
|
+
* The body has been parsed based on the content-type header.
|
|
11
|
+
*/
|
|
12
|
+
body: StandardBody;
|
|
13
|
+
signal: AbortSignal | undefined;
|
|
14
|
+
}
|
|
15
|
+
interface StandardLazyRequest extends Omit<StandardRequest, 'body'> {
|
|
16
|
+
/**
|
|
17
|
+
* The body has been parsed based on the content-type header.
|
|
18
|
+
* This method can safely call multiple times (cached).
|
|
19
|
+
*/
|
|
20
|
+
body: () => Promise<StandardBody>;
|
|
21
|
+
}
|
|
22
|
+
interface StandardResponse {
|
|
23
|
+
status: number;
|
|
24
|
+
headers: StandardHeaders;
|
|
25
|
+
/**
|
|
26
|
+
* The body has been parsed based on the content-type header.
|
|
27
|
+
*/
|
|
28
|
+
body: StandardBody;
|
|
29
|
+
}
|
|
30
|
+
interface StandardLazyResponse extends Omit<StandardResponse, 'body'> {
|
|
31
|
+
/**
|
|
32
|
+
* The body has been parsed based on the content-type header.
|
|
33
|
+
* This method can safely call multiple times (cached).
|
|
34
|
+
*/
|
|
35
|
+
body: () => Promise<StandardBody>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type { StandardHeaders as S, StandardLazyResponse as a, StandardBody as b, StandardRequest as c, StandardLazyRequest as d, StandardResponse as e };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
interface StandardHeaders {
|
|
2
|
+
[key: string]: string | string[] | undefined;
|
|
3
|
+
}
|
|
4
|
+
type StandardBody = undefined | unknown | Blob | URLSearchParams | FormData | AsyncIterator<unknown | void, unknown | void, undefined>;
|
|
5
|
+
interface StandardRequest {
|
|
6
|
+
method: string;
|
|
7
|
+
url: URL;
|
|
8
|
+
headers: StandardHeaders;
|
|
9
|
+
/**
|
|
10
|
+
* The body has been parsed based on the content-type header.
|
|
11
|
+
*/
|
|
12
|
+
body: StandardBody;
|
|
13
|
+
signal: AbortSignal | undefined;
|
|
14
|
+
}
|
|
15
|
+
interface StandardLazyRequest extends Omit<StandardRequest, 'body'> {
|
|
16
|
+
/**
|
|
17
|
+
* The body has been parsed based on the content-type header.
|
|
18
|
+
* This method can safely call multiple times (cached).
|
|
19
|
+
*/
|
|
20
|
+
body: () => Promise<StandardBody>;
|
|
21
|
+
}
|
|
22
|
+
interface StandardResponse {
|
|
23
|
+
status: number;
|
|
24
|
+
headers: StandardHeaders;
|
|
25
|
+
/**
|
|
26
|
+
* The body has been parsed based on the content-type header.
|
|
27
|
+
*/
|
|
28
|
+
body: StandardBody;
|
|
29
|
+
}
|
|
30
|
+
interface StandardLazyResponse extends Omit<StandardResponse, 'body'> {
|
|
31
|
+
/**
|
|
32
|
+
* The body has been parsed based on the content-type header.
|
|
33
|
+
* This method can safely call multiple times (cached).
|
|
34
|
+
*/
|
|
35
|
+
body: () => Promise<StandardBody>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type { StandardHeaders as S, StandardLazyResponse as a, StandardBody as b, StandardRequest as c, StandardLazyRequest as d, StandardResponse as e };
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@temporary-name/standard-server",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.9.3-alpha.03e17aa3340f4b261a549bf9e3fe948b084e2309",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://www.stainless.com/",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/stainless-api/krusty.git",
|
|
10
|
+
"directory": "packages/standard-server"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"krusty"
|
|
14
|
+
],
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.mts",
|
|
18
|
+
"import": "./dist/index.mjs",
|
|
19
|
+
"default": "./dist/index.mjs"
|
|
20
|
+
},
|
|
21
|
+
"./batch": {
|
|
22
|
+
"types": "./dist/batch/index.d.mts",
|
|
23
|
+
"import": "./dist/batch/index.mjs",
|
|
24
|
+
"default": "./dist/batch/index.mjs"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist"
|
|
29
|
+
],
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@temporary-name/shared": "1.9.3-alpha.03e17aa3340f4b261a549bf9e3fe948b084e2309"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "unbuild",
|
|
35
|
+
"build:watch": "pnpm run build --watch",
|
|
36
|
+
"type:check": "tsc -b"
|
|
37
|
+
}
|
|
38
|
+
}
|