@standardserver/node 0.0.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/LICENCE +21 -0
- package/dist/index.d.mts +63 -0
- package/dist/index.d.ts +63 -0
- package/dist/index.mjs +177 -0
- package/package.json +36 -0
package/LICENCE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Standard Server
|
|
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/dist/index.d.mts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { StandardBodyHint, StandardBody, StandardHeaders, StandardMethod, StandardLazyRequest, StandardResponse, StandardUrl } from '@standardserver/core';
|
|
2
|
+
import { ToEventStreamOptions as ToEventStreamOptions$1 } from '@standardserver/fetch';
|
|
3
|
+
import { AsyncIteratorClass } from '@standardserver/shared';
|
|
4
|
+
import Stream, { Readable } from 'node:stream';
|
|
5
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
6
|
+
import { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
|
|
7
|
+
|
|
8
|
+
declare function toEventIterator(stream: Readable): AsyncIteratorClass<unknown>;
|
|
9
|
+
interface ToEventStreamOptions extends ToEventStreamOptions$1 {
|
|
10
|
+
}
|
|
11
|
+
declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): Readable;
|
|
12
|
+
|
|
13
|
+
type NodeHttpRequest = (IncomingMessage | Http2ServerRequest) & {
|
|
14
|
+
/**
|
|
15
|
+
* Prefer `originalUrl` over `url` if it is available.
|
|
16
|
+
*/
|
|
17
|
+
originalUrl?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Body might already parsed by upstream framework like express.js, ...
|
|
20
|
+
*/
|
|
21
|
+
body?: unknown;
|
|
22
|
+
};
|
|
23
|
+
type NodeHttpResponse = ServerResponse | Http2ServerResponse;
|
|
24
|
+
|
|
25
|
+
interface ToStandardBodyOptions {
|
|
26
|
+
/**
|
|
27
|
+
* Hints on how the body should be parsed.
|
|
28
|
+
*/
|
|
29
|
+
hint?: StandardBodyHint | undefined;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Parses the body of a node http request.
|
|
33
|
+
*/
|
|
34
|
+
declare function toStandardBody(req: NodeHttpRequest, options?: ToStandardBodyOptions): Promise<StandardBody>;
|
|
35
|
+
interface ToNodeHttpBodyOptions {
|
|
36
|
+
/**
|
|
37
|
+
* Options for the event iterator, like keep-alive settings, initial comment, etc.
|
|
38
|
+
*/
|
|
39
|
+
eventIterator?: ToEventStreamOptions;
|
|
40
|
+
}
|
|
41
|
+
declare function toNodeHttpBody(body: StandardBody, headers: StandardHeaders, options?: ToNodeHttpBodyOptions): Promise<[
|
|
42
|
+
body: Readable | undefined | string,
|
|
43
|
+
headers: StandardHeaders
|
|
44
|
+
]>;
|
|
45
|
+
|
|
46
|
+
declare function toStandardMethod(method: string | undefined): StandardMethod;
|
|
47
|
+
|
|
48
|
+
declare function toStandardLazyRequest(req: NodeHttpRequest, res: NodeHttpResponse): StandardLazyRequest;
|
|
49
|
+
|
|
50
|
+
interface SendStandardResponseOptions {
|
|
51
|
+
/**
|
|
52
|
+
* Options for converting the response body to a Node.js HTTP body.
|
|
53
|
+
*/
|
|
54
|
+
body?: ToNodeHttpBodyOptions;
|
|
55
|
+
}
|
|
56
|
+
declare function sendStandardResponse(res: NodeHttpResponse, standardResponse: StandardResponse, options?: SendStandardResponseOptions): Promise<void>;
|
|
57
|
+
|
|
58
|
+
declare function toAbortSignal(stream: Stream.Writable): AbortSignal;
|
|
59
|
+
|
|
60
|
+
declare function toStandardUrl(req: NodeHttpRequest): StandardUrl;
|
|
61
|
+
|
|
62
|
+
export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest, toStandardMethod, toStandardUrl };
|
|
63
|
+
export type { NodeHttpRequest, NodeHttpResponse, SendStandardResponseOptions, ToEventStreamOptions, ToNodeHttpBodyOptions, ToStandardBodyOptions };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { StandardBodyHint, StandardBody, StandardHeaders, StandardMethod, StandardLazyRequest, StandardResponse, StandardUrl } from '@standardserver/core';
|
|
2
|
+
import { ToEventStreamOptions as ToEventStreamOptions$1 } from '@standardserver/fetch';
|
|
3
|
+
import { AsyncIteratorClass } from '@standardserver/shared';
|
|
4
|
+
import Stream, { Readable } from 'node:stream';
|
|
5
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
6
|
+
import { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
|
|
7
|
+
|
|
8
|
+
declare function toEventIterator(stream: Readable): AsyncIteratorClass<unknown>;
|
|
9
|
+
interface ToEventStreamOptions extends ToEventStreamOptions$1 {
|
|
10
|
+
}
|
|
11
|
+
declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): Readable;
|
|
12
|
+
|
|
13
|
+
type NodeHttpRequest = (IncomingMessage | Http2ServerRequest) & {
|
|
14
|
+
/**
|
|
15
|
+
* Prefer `originalUrl` over `url` if it is available.
|
|
16
|
+
*/
|
|
17
|
+
originalUrl?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Body might already parsed by upstream framework like express.js, ...
|
|
20
|
+
*/
|
|
21
|
+
body?: unknown;
|
|
22
|
+
};
|
|
23
|
+
type NodeHttpResponse = ServerResponse | Http2ServerResponse;
|
|
24
|
+
|
|
25
|
+
interface ToStandardBodyOptions {
|
|
26
|
+
/**
|
|
27
|
+
* Hints on how the body should be parsed.
|
|
28
|
+
*/
|
|
29
|
+
hint?: StandardBodyHint | undefined;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Parses the body of a node http request.
|
|
33
|
+
*/
|
|
34
|
+
declare function toStandardBody(req: NodeHttpRequest, options?: ToStandardBodyOptions): Promise<StandardBody>;
|
|
35
|
+
interface ToNodeHttpBodyOptions {
|
|
36
|
+
/**
|
|
37
|
+
* Options for the event iterator, like keep-alive settings, initial comment, etc.
|
|
38
|
+
*/
|
|
39
|
+
eventIterator?: ToEventStreamOptions;
|
|
40
|
+
}
|
|
41
|
+
declare function toNodeHttpBody(body: StandardBody, headers: StandardHeaders, options?: ToNodeHttpBodyOptions): Promise<[
|
|
42
|
+
body: Readable | undefined | string,
|
|
43
|
+
headers: StandardHeaders
|
|
44
|
+
]>;
|
|
45
|
+
|
|
46
|
+
declare function toStandardMethod(method: string | undefined): StandardMethod;
|
|
47
|
+
|
|
48
|
+
declare function toStandardLazyRequest(req: NodeHttpRequest, res: NodeHttpResponse): StandardLazyRequest;
|
|
49
|
+
|
|
50
|
+
interface SendStandardResponseOptions {
|
|
51
|
+
/**
|
|
52
|
+
* Options for converting the response body to a Node.js HTTP body.
|
|
53
|
+
*/
|
|
54
|
+
body?: ToNodeHttpBodyOptions;
|
|
55
|
+
}
|
|
56
|
+
declare function sendStandardResponse(res: NodeHttpResponse, standardResponse: StandardResponse, options?: SendStandardResponseOptions): Promise<void>;
|
|
57
|
+
|
|
58
|
+
declare function toAbortSignal(stream: Stream.Writable): AbortSignal;
|
|
59
|
+
|
|
60
|
+
declare function toStandardUrl(req: NodeHttpRequest): StandardUrl;
|
|
61
|
+
|
|
62
|
+
export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest, toStandardMethod, toStandardUrl };
|
|
63
|
+
export type { NodeHttpRequest, NodeHttpResponse, SendStandardResponseOptions, ToEventStreamOptions, ToNodeHttpBodyOptions, ToStandardBodyOptions };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { Readable } from 'node:stream';
|
|
2
|
+
import { flattenStandardHeader, getFilenameFromContentDisposition, generateContentDisposition } from '@standardserver/core';
|
|
3
|
+
import { parseEmptyableJSON, isAsyncIteratorObject, stringifyJSON, AbortError } from '@standardserver/shared';
|
|
4
|
+
import { toEventIterator as toEventIterator$1, toEventStream as toEventStream$1 } from '@standardserver/fetch';
|
|
5
|
+
|
|
6
|
+
function toEventIterator(stream) {
|
|
7
|
+
return toEventIterator$1(Readable.toWeb(stream));
|
|
8
|
+
}
|
|
9
|
+
function toEventStream(iterator, options = {}) {
|
|
10
|
+
return Readable.fromWeb(toEventStream$1(iterator, options));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function toStandardMethod(method) {
|
|
14
|
+
return method ?? "GET";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const EMPTY_BODY_METHOD_SET = /* @__PURE__ */ new Set(["GET", "HEAD"]);
|
|
18
|
+
async function toStandardBody(req, options = {}) {
|
|
19
|
+
const hint = flattenStandardHeader(req.headers["standard-server"]) ?? options?.hint;
|
|
20
|
+
const contentType = req.headers["content-type"];
|
|
21
|
+
const mimeType = contentType?.split(";")[0]?.trim();
|
|
22
|
+
const contentDisposition = req.headers["content-disposition"];
|
|
23
|
+
const contentLength = req.headers["content-length"];
|
|
24
|
+
if (hint === "none") {
|
|
25
|
+
return void 0;
|
|
26
|
+
}
|
|
27
|
+
if (hint === void 0 && EMPTY_BODY_METHOD_SET.has(toStandardMethod(req.method))) {
|
|
28
|
+
return void 0;
|
|
29
|
+
}
|
|
30
|
+
if (req.body !== void 0) {
|
|
31
|
+
return req.body;
|
|
32
|
+
}
|
|
33
|
+
if (!req.readable) {
|
|
34
|
+
throw new TypeError("Failed to read body: body stream already read or destroyed");
|
|
35
|
+
}
|
|
36
|
+
if (hint === "json" || hint === void 0 && contentDisposition === void 0 && mimeType === "application/json") {
|
|
37
|
+
const text = await _streamToString(req);
|
|
38
|
+
return parseEmptyableJSON(text);
|
|
39
|
+
}
|
|
40
|
+
if (hint === "form-data" || hint === void 0 && contentDisposition === void 0 && mimeType === "multipart/form-data") {
|
|
41
|
+
return _streamToFormData(req, contentType);
|
|
42
|
+
}
|
|
43
|
+
if (hint === "url-search-params" || hint === void 0 && contentDisposition === void 0 && mimeType === "application/x-www-form-urlencoded") {
|
|
44
|
+
const text = await _streamToString(req);
|
|
45
|
+
return new URLSearchParams(text);
|
|
46
|
+
}
|
|
47
|
+
if (hint === "event-stream" || hint === void 0 && contentDisposition === void 0 && mimeType === "text/event-stream") {
|
|
48
|
+
return toEventIterator(req);
|
|
49
|
+
}
|
|
50
|
+
if (hint === "file" || hint === void 0 && (contentDisposition !== void 0 || contentLength !== void 0)) {
|
|
51
|
+
const fileName = contentDisposition !== void 0 ? getFilenameFromContentDisposition(contentDisposition) : void 0;
|
|
52
|
+
return _streamToFile(req, fileName ?? "blob", contentType ?? "");
|
|
53
|
+
}
|
|
54
|
+
return Readable.toWeb(req);
|
|
55
|
+
}
|
|
56
|
+
async function toNodeHttpBody(body, headers, options = {}) {
|
|
57
|
+
headers = { ...headers };
|
|
58
|
+
if (body === void 0) {
|
|
59
|
+
headers["standard-server"] = "none";
|
|
60
|
+
return [void 0, headers];
|
|
61
|
+
}
|
|
62
|
+
if (body instanceof ReadableStream) {
|
|
63
|
+
headers["standard-server"] = "octet-stream";
|
|
64
|
+
headers["content-type"] ??= "application/octet-stream";
|
|
65
|
+
return [Readable.fromWeb(body), headers];
|
|
66
|
+
}
|
|
67
|
+
if (body instanceof Blob) {
|
|
68
|
+
headers["standard-server"] = "file";
|
|
69
|
+
headers["content-type"] ??= body.type;
|
|
70
|
+
headers["content-disposition"] ??= generateContentDisposition(body instanceof File ? body.name : "blob");
|
|
71
|
+
if (!Number.isNaN(body.size)) {
|
|
72
|
+
headers["content-length"] ??= body.size.toString();
|
|
73
|
+
}
|
|
74
|
+
return [Readable.fromWeb(body.stream()), headers];
|
|
75
|
+
}
|
|
76
|
+
if (body instanceof FormData) {
|
|
77
|
+
const response = new Response(body);
|
|
78
|
+
const blob = await response.blob();
|
|
79
|
+
headers["standard-server"] = "form-data";
|
|
80
|
+
headers["content-type"] ??= blob.type;
|
|
81
|
+
headers["content-length"] ??= blob.size.toString();
|
|
82
|
+
return [Readable.fromWeb(blob.stream()), headers];
|
|
83
|
+
}
|
|
84
|
+
if (body instanceof URLSearchParams) {
|
|
85
|
+
headers["standard-server"] = "url-search-params";
|
|
86
|
+
headers["content-type"] ??= "application/x-www-form-urlencoded";
|
|
87
|
+
return [body.toString(), headers];
|
|
88
|
+
}
|
|
89
|
+
if (isAsyncIteratorObject(body)) {
|
|
90
|
+
headers["standard-server"] = "event-stream";
|
|
91
|
+
headers["content-type"] ??= "text/event-stream";
|
|
92
|
+
return [toEventStream(body, options.eventIterator), headers];
|
|
93
|
+
}
|
|
94
|
+
headers["standard-server"] = "json";
|
|
95
|
+
headers["content-type"] ??= "application/json";
|
|
96
|
+
return [stringifyJSON(body), headers];
|
|
97
|
+
}
|
|
98
|
+
function _streamToFormData(stream, contentType) {
|
|
99
|
+
const response = new Response(stream, {
|
|
100
|
+
headers: {
|
|
101
|
+
"content-type": contentType
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
return response.formData();
|
|
105
|
+
}
|
|
106
|
+
async function _streamToString(stream) {
|
|
107
|
+
let string = "";
|
|
108
|
+
for await (const chunk of stream) {
|
|
109
|
+
string += chunk.toString();
|
|
110
|
+
}
|
|
111
|
+
return string;
|
|
112
|
+
}
|
|
113
|
+
async function _streamToFile(stream, fileName, contentType) {
|
|
114
|
+
const chunks = [];
|
|
115
|
+
for await (const chunk of stream) {
|
|
116
|
+
chunks.push(chunk);
|
|
117
|
+
}
|
|
118
|
+
return new File(chunks, fileName, { type: contentType });
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function toAbortSignal(stream) {
|
|
122
|
+
const controller = new AbortController();
|
|
123
|
+
stream.once("error", (error) => controller.abort(error));
|
|
124
|
+
stream.once("close", () => {
|
|
125
|
+
if (!stream.writableFinished) {
|
|
126
|
+
controller.abort(new AbortError("Writable stream closed before it finished writing"));
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
return controller.signal;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function toStandardUrl(req) {
|
|
133
|
+
const url = req.originalUrl ?? req.url ?? "/";
|
|
134
|
+
return `${url.startsWith("/") ? "" : "/"}${url}`;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function toStandardLazyRequest(req, res) {
|
|
138
|
+
const signal = toAbortSignal(res);
|
|
139
|
+
return {
|
|
140
|
+
url: toStandardUrl(req),
|
|
141
|
+
method: toStandardMethod(req.method),
|
|
142
|
+
headers: req.headers,
|
|
143
|
+
resolveBody: (hint) => toStandardBody(req, { hint }),
|
|
144
|
+
signal
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async function sendStandardResponse(res, standardResponse, options = {}) {
|
|
149
|
+
const [resBody, resHeaders] = await toNodeHttpBody(standardResponse.body, standardResponse.headers, options.body);
|
|
150
|
+
return new Promise((resolve, reject) => {
|
|
151
|
+
res.once("error", reject);
|
|
152
|
+
res.once("close", resolve);
|
|
153
|
+
res.statusCode = standardResponse.status;
|
|
154
|
+
for (const key in resHeaders) {
|
|
155
|
+
const value = resHeaders[key];
|
|
156
|
+
if (value !== void 0) {
|
|
157
|
+
res.setHeader(key, value);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (resBody === void 0) {
|
|
161
|
+
res.end();
|
|
162
|
+
} else if (typeof resBody === "string") {
|
|
163
|
+
res.end(resBody);
|
|
164
|
+
} else {
|
|
165
|
+
res.writeHead(standardResponse.status);
|
|
166
|
+
res.once("close", () => {
|
|
167
|
+
if (!resBody.closed) {
|
|
168
|
+
resBody.destroy(res.errored ?? void 0);
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
resBody.once("error", (error) => res.destroy(error));
|
|
172
|
+
resBody.pipe(res);
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest, toStandardMethod, toStandardUrl };
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@standardserver/node",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://standardserver.dev",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/standardserver/standardserver.git",
|
|
10
|
+
"directory": "packages/node"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.mts",
|
|
15
|
+
"import": "./dist/index.mjs",
|
|
16
|
+
"default": "./dist/index.mjs"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@standardserver/core": "0.0.0",
|
|
24
|
+
"@standardserver/shared": "0.0.0",
|
|
25
|
+
"@standardserver/fetch": "0.0.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/node": "^25.0.3",
|
|
29
|
+
"@types/supertest": "^6.0.3",
|
|
30
|
+
"supertest": "^7.1.4"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "unbuild",
|
|
34
|
+
"type:check": "tsc -b"
|
|
35
|
+
}
|
|
36
|
+
}
|