@temporary-name/standard-server-node 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/index.d.mts +52 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.mjs +152 -0
- package/package.json +40 -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
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { StandardBody, StandardHeaders, StandardLazyRequest, StandardResponse } from '@temporary-name/standard-server';
|
|
2
|
+
import Stream, { Readable } from 'node:stream';
|
|
3
|
+
import { ToEventIteratorOptions as ToEventIteratorOptions$1, ToEventStreamOptions as ToEventStreamOptions$1 } from '@temporary-name/standard-server-fetch';
|
|
4
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
5
|
+
import { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
|
|
6
|
+
|
|
7
|
+
interface ToEventIteratorOptions extends ToEventIteratorOptions$1 {
|
|
8
|
+
}
|
|
9
|
+
declare function toEventIterator(stream: Readable, options?: ToEventIteratorOptions): AsyncIteratorObject<unknown | void, unknown | void, void> & AsyncGenerator<unknown | void, unknown | void, void>;
|
|
10
|
+
interface ToEventStreamOptions extends ToEventStreamOptions$1 {
|
|
11
|
+
}
|
|
12
|
+
declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): Readable;
|
|
13
|
+
|
|
14
|
+
type NodeHttpRequest = (IncomingMessage | Http2ServerRequest) & {
|
|
15
|
+
/**
|
|
16
|
+
* Replace `req.url` with `req.originalUrl` when `req.originalUrl` is available.
|
|
17
|
+
* This is useful for `express.js` middleware.
|
|
18
|
+
*/
|
|
19
|
+
originalUrl?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Prefer parsed body if it is available.
|
|
22
|
+
*/
|
|
23
|
+
body?: unknown;
|
|
24
|
+
};
|
|
25
|
+
type NodeHttpResponse = ServerResponse | Http2ServerResponse;
|
|
26
|
+
|
|
27
|
+
interface ToStandardBodyOptions extends ToEventIteratorOptions {
|
|
28
|
+
}
|
|
29
|
+
declare function toStandardBody(req: NodeHttpRequest, options?: ToStandardBodyOptions): Promise<StandardBody>;
|
|
30
|
+
interface ToNodeHttpBodyOptions extends ToEventStreamOptions {
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* @param body
|
|
34
|
+
* @param headers - WARNING: The headers can be changed by the function and effects on the original headers.
|
|
35
|
+
* @param options
|
|
36
|
+
*/
|
|
37
|
+
declare function toNodeHttpBody(body: StandardBody, headers: StandardHeaders, options?: ToNodeHttpBodyOptions): Readable | undefined | string;
|
|
38
|
+
|
|
39
|
+
declare function toStandardMethod(method: string | undefined): string;
|
|
40
|
+
|
|
41
|
+
declare function toStandardLazyRequest(req: NodeHttpRequest, res: NodeHttpResponse): StandardLazyRequest;
|
|
42
|
+
|
|
43
|
+
interface SendStandardResponseOptions extends ToNodeHttpBodyOptions {
|
|
44
|
+
}
|
|
45
|
+
declare function sendStandardResponse(res: NodeHttpResponse, standardResponse: StandardResponse, options?: SendStandardResponseOptions): Promise<void>;
|
|
46
|
+
|
|
47
|
+
declare function toAbortSignal(stream: Stream.Writable): AbortSignal;
|
|
48
|
+
|
|
49
|
+
declare function toStandardUrl(req: NodeHttpRequest): URL;
|
|
50
|
+
|
|
51
|
+
export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest, toStandardMethod, toStandardUrl };
|
|
52
|
+
export type { NodeHttpRequest, NodeHttpResponse, SendStandardResponseOptions, ToEventIteratorOptions, ToEventStreamOptions, ToNodeHttpBodyOptions, ToStandardBodyOptions };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { StandardBody, StandardHeaders, StandardLazyRequest, StandardResponse } from '@temporary-name/standard-server';
|
|
2
|
+
import Stream, { Readable } from 'node:stream';
|
|
3
|
+
import { ToEventIteratorOptions as ToEventIteratorOptions$1, ToEventStreamOptions as ToEventStreamOptions$1 } from '@temporary-name/standard-server-fetch';
|
|
4
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
5
|
+
import { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
|
|
6
|
+
|
|
7
|
+
interface ToEventIteratorOptions extends ToEventIteratorOptions$1 {
|
|
8
|
+
}
|
|
9
|
+
declare function toEventIterator(stream: Readable, options?: ToEventIteratorOptions): AsyncIteratorObject<unknown | void, unknown | void, void> & AsyncGenerator<unknown | void, unknown | void, void>;
|
|
10
|
+
interface ToEventStreamOptions extends ToEventStreamOptions$1 {
|
|
11
|
+
}
|
|
12
|
+
declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): Readable;
|
|
13
|
+
|
|
14
|
+
type NodeHttpRequest = (IncomingMessage | Http2ServerRequest) & {
|
|
15
|
+
/**
|
|
16
|
+
* Replace `req.url` with `req.originalUrl` when `req.originalUrl` is available.
|
|
17
|
+
* This is useful for `express.js` middleware.
|
|
18
|
+
*/
|
|
19
|
+
originalUrl?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Prefer parsed body if it is available.
|
|
22
|
+
*/
|
|
23
|
+
body?: unknown;
|
|
24
|
+
};
|
|
25
|
+
type NodeHttpResponse = ServerResponse | Http2ServerResponse;
|
|
26
|
+
|
|
27
|
+
interface ToStandardBodyOptions extends ToEventIteratorOptions {
|
|
28
|
+
}
|
|
29
|
+
declare function toStandardBody(req: NodeHttpRequest, options?: ToStandardBodyOptions): Promise<StandardBody>;
|
|
30
|
+
interface ToNodeHttpBodyOptions extends ToEventStreamOptions {
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* @param body
|
|
34
|
+
* @param headers - WARNING: The headers can be changed by the function and effects on the original headers.
|
|
35
|
+
* @param options
|
|
36
|
+
*/
|
|
37
|
+
declare function toNodeHttpBody(body: StandardBody, headers: StandardHeaders, options?: ToNodeHttpBodyOptions): Readable | undefined | string;
|
|
38
|
+
|
|
39
|
+
declare function toStandardMethod(method: string | undefined): string;
|
|
40
|
+
|
|
41
|
+
declare function toStandardLazyRequest(req: NodeHttpRequest, res: NodeHttpResponse): StandardLazyRequest;
|
|
42
|
+
|
|
43
|
+
interface SendStandardResponseOptions extends ToNodeHttpBodyOptions {
|
|
44
|
+
}
|
|
45
|
+
declare function sendStandardResponse(res: NodeHttpResponse, standardResponse: StandardResponse, options?: SendStandardResponseOptions): Promise<void>;
|
|
46
|
+
|
|
47
|
+
declare function toAbortSignal(stream: Stream.Writable): AbortSignal;
|
|
48
|
+
|
|
49
|
+
declare function toStandardUrl(req: NodeHttpRequest): URL;
|
|
50
|
+
|
|
51
|
+
export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest, toStandardMethod, toStandardUrl };
|
|
52
|
+
export type { NodeHttpRequest, NodeHttpResponse, SendStandardResponseOptions, ToEventIteratorOptions, ToEventStreamOptions, ToNodeHttpBodyOptions, ToStandardBodyOptions };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { runWithSpan, parseEmptyableJSON, isAsyncIteratorObject, stringifyJSON, AbortError, guard, once } from '@temporary-name/shared';
|
|
2
|
+
import { getFilenameFromContentDisposition, flattenHeader, generateContentDisposition } from '@temporary-name/standard-server';
|
|
3
|
+
import { Readable } from 'node:stream';
|
|
4
|
+
import { toEventIterator as toEventIterator$1, toEventStream as toEventStream$1 } from '@temporary-name/standard-server-fetch';
|
|
5
|
+
|
|
6
|
+
function toEventIterator(stream, options = {}) {
|
|
7
|
+
return toEventIterator$1(Readable.toWeb(stream), options);
|
|
8
|
+
}
|
|
9
|
+
function toEventStream(iterator, options = {}) {
|
|
10
|
+
return Readable.fromWeb(toEventStream$1(iterator, options));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function toStandardBody(req, options = {}) {
|
|
14
|
+
if (req.body !== void 0) {
|
|
15
|
+
return Promise.resolve(req.body);
|
|
16
|
+
}
|
|
17
|
+
return runWithSpan({ name: "parse_standard_body", signal: options.signal }, async () => {
|
|
18
|
+
const contentDisposition = req.headers["content-disposition"];
|
|
19
|
+
const contentType = req.headers["content-type"];
|
|
20
|
+
if (typeof contentDisposition === "string") {
|
|
21
|
+
const fileName = getFilenameFromContentDisposition(contentDisposition) ?? "blob";
|
|
22
|
+
return _streamToFile(req, fileName, contentType ?? "");
|
|
23
|
+
}
|
|
24
|
+
if (!contentType || contentType.startsWith("application/json")) {
|
|
25
|
+
const text = await _streamToString(req);
|
|
26
|
+
return parseEmptyableJSON(text);
|
|
27
|
+
}
|
|
28
|
+
if (contentType.startsWith("multipart/form-data")) {
|
|
29
|
+
return _streamToFormData(req, contentType);
|
|
30
|
+
}
|
|
31
|
+
if (contentType.startsWith("application/x-www-form-urlencoded")) {
|
|
32
|
+
const text = await _streamToString(req);
|
|
33
|
+
return new URLSearchParams(text);
|
|
34
|
+
}
|
|
35
|
+
if (contentType.startsWith("text/event-stream")) {
|
|
36
|
+
return toEventIterator(req, options);
|
|
37
|
+
}
|
|
38
|
+
if (contentType.startsWith("text/plain")) {
|
|
39
|
+
return _streamToString(req);
|
|
40
|
+
}
|
|
41
|
+
return _streamToFile(req, "blob", contentType);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
function toNodeHttpBody(body, headers, options = {}) {
|
|
45
|
+
const currentContentDisposition = flattenHeader(headers["content-disposition"]);
|
|
46
|
+
delete headers["content-type"];
|
|
47
|
+
delete headers["content-disposition"];
|
|
48
|
+
if (body === void 0) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (body instanceof Blob) {
|
|
52
|
+
headers["content-type"] = body.type;
|
|
53
|
+
headers["content-length"] = body.size.toString();
|
|
54
|
+
headers["content-disposition"] = currentContentDisposition ?? generateContentDisposition(body instanceof File ? body.name : "blob");
|
|
55
|
+
return Readable.fromWeb(body.stream());
|
|
56
|
+
}
|
|
57
|
+
if (body instanceof FormData) {
|
|
58
|
+
const response = new Response(body);
|
|
59
|
+
headers["content-type"] = response.headers.get("content-type");
|
|
60
|
+
return Readable.fromWeb(response.body);
|
|
61
|
+
}
|
|
62
|
+
if (body instanceof URLSearchParams) {
|
|
63
|
+
headers["content-type"] = "application/x-www-form-urlencoded";
|
|
64
|
+
return body.toString();
|
|
65
|
+
}
|
|
66
|
+
if (isAsyncIteratorObject(body)) {
|
|
67
|
+
headers["content-type"] = "text/event-stream";
|
|
68
|
+
return toEventStream(body, options);
|
|
69
|
+
}
|
|
70
|
+
headers["content-type"] = "application/json";
|
|
71
|
+
return stringifyJSON(body);
|
|
72
|
+
}
|
|
73
|
+
function _streamToFormData(stream, contentType) {
|
|
74
|
+
const response = new Response(stream, {
|
|
75
|
+
headers: {
|
|
76
|
+
"content-type": contentType
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
return response.formData();
|
|
80
|
+
}
|
|
81
|
+
async function _streamToString(stream) {
|
|
82
|
+
let string = "";
|
|
83
|
+
for await (const chunk of stream) {
|
|
84
|
+
string += chunk.toString();
|
|
85
|
+
}
|
|
86
|
+
return string;
|
|
87
|
+
}
|
|
88
|
+
async function _streamToFile(stream, fileName, contentType) {
|
|
89
|
+
const chunks = [];
|
|
90
|
+
for await (const chunk of stream) {
|
|
91
|
+
chunks.push(chunk);
|
|
92
|
+
}
|
|
93
|
+
return new File(chunks, fileName, { type: contentType });
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function toStandardMethod(method) {
|
|
97
|
+
return method ?? "GET";
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function toAbortSignal(stream) {
|
|
101
|
+
const controller = new AbortController();
|
|
102
|
+
stream.once("error", (error) => controller.abort(error));
|
|
103
|
+
stream.once("close", () => {
|
|
104
|
+
if (!stream.writableFinished) {
|
|
105
|
+
controller.abort(new AbortError("Writable stream closed before it finished writing"));
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
return controller.signal;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function toStandardUrl(req) {
|
|
112
|
+
const protocol = "encrypted" in req.socket && req.socket.encrypted ? "https:" : "http:";
|
|
113
|
+
const origin = guard(() => new URL(`${protocol}//${req.headers.host ?? "localhost"}`).origin) ?? `${protocol}//localhost`;
|
|
114
|
+
const path = req.originalUrl ?? req.url ?? "/";
|
|
115
|
+
return new URL(`${origin}${path.startsWith("/") ? "" : "/"}${path}`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function toStandardLazyRequest(req, res) {
|
|
119
|
+
const signal = toAbortSignal(res);
|
|
120
|
+
return {
|
|
121
|
+
method: toStandardMethod(req.method),
|
|
122
|
+
url: toStandardUrl(req),
|
|
123
|
+
headers: req.headers,
|
|
124
|
+
body: once(() => toStandardBody(req, { signal })),
|
|
125
|
+
signal
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function sendStandardResponse(res, standardResponse, options = {}) {
|
|
130
|
+
return new Promise((resolve, reject) => {
|
|
131
|
+
res.once("error", reject);
|
|
132
|
+
res.once("close", resolve);
|
|
133
|
+
const resHeaders = { ...standardResponse.headers };
|
|
134
|
+
const resBody = toNodeHttpBody(standardResponse.body, resHeaders, options);
|
|
135
|
+
res.writeHead(standardResponse.status, resHeaders);
|
|
136
|
+
if (resBody === void 0) {
|
|
137
|
+
res.end();
|
|
138
|
+
} else if (typeof resBody === "string") {
|
|
139
|
+
res.end(resBody);
|
|
140
|
+
} else {
|
|
141
|
+
res.once("close", () => {
|
|
142
|
+
if (!resBody.closed) {
|
|
143
|
+
resBody.destroy(res.errored ?? void 0);
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
resBody.once("error", (error) => res.destroy(error));
|
|
147
|
+
resBody.pipe(res);
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest, toStandardMethod, toStandardUrl };
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@temporary-name/standard-server-node",
|
|
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-node"
|
|
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
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@temporary-name/shared": "1.9.3-alpha.03e17aa3340f4b261a549bf9e3fe948b084e2309",
|
|
27
|
+
"@temporary-name/standard-server": "1.9.3-alpha.03e17aa3340f4b261a549bf9e3fe948b084e2309",
|
|
28
|
+
"@temporary-name/standard-server-fetch": "1.9.3-alpha.03e17aa3340f4b261a549bf9e3fe948b084e2309"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/node": "^22.15.30",
|
|
32
|
+
"@types/supertest": "^6.0.3",
|
|
33
|
+
"supertest": "^7.1.4"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "unbuild",
|
|
37
|
+
"build:watch": "pnpm run build --watch",
|
|
38
|
+
"type:check": "tsc -b"
|
|
39
|
+
}
|
|
40
|
+
}
|