srvx 0.1.3 → 0.2.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.
@@ -1,138 +0,0 @@
1
- import NodeHttp__default from 'node:http';
2
- import { xHeaders, xRequest } from '../types.js';
3
- import 'node:net';
4
- import 'bun';
5
-
6
- declare const kNodeReq: unique symbol;
7
- declare const kNodeRes: unique symbol;
8
- declare const kNodeInspect: unique symbol;
9
-
10
- declare const NodeRequestProxy: {
11
- new (nodeReq: NodeHttp__default.IncomingMessage): {
12
- cache: RequestCache;
13
- credentials: RequestCredentials;
14
- destination: RequestDestination;
15
- integrity: string;
16
- keepalive: boolean;
17
- mode: RequestMode;
18
- redirect: RequestRedirect;
19
- referrer: string;
20
- referrerPolicy: ReferrerPolicy;
21
- headers: xHeaders;
22
- bodyUsed: boolean;
23
- _url: URL;
24
- "__#448@#abortSignal"?: AbortController;
25
- "__#448@#hasBody": boolean | undefined;
26
- "__#448@#bodyBytes"?: Promise<Uint8Array>;
27
- "__#448@#blobBody"?: Promise<Blob>;
28
- "__#448@#formDataBody"?: Promise<FormData>;
29
- "__#448@#jsonBody"?: Promise<any>;
30
- "__#448@#textBody"?: Promise<string>;
31
- "__#448@#bodyStream"?: undefined | ReadableStream<Uint8Array>;
32
- readonly xRemoteAddress: string | undefined;
33
- clone(): xRequest;
34
- readonly url: string;
35
- readonly method: string;
36
- readonly signal: AbortSignal;
37
- readonly _hasBody: boolean;
38
- readonly body: ReadableStream<Uint8Array<ArrayBufferLike>> | null;
39
- bytes(): Promise<Uint8Array>;
40
- arrayBuffer(): Promise<ArrayBuffer>;
41
- blob(): Promise<Blob>;
42
- formData(): Promise<FormData>;
43
- text(): Promise<string>;
44
- json(): Promise<any>;
45
- [kNodeReq]: NodeHttp__default.IncomingMessage;
46
- readonly [Symbol.toStringTag]: string;
47
- [kNodeInspect](): {
48
- method: string;
49
- url: string;
50
- headers: xHeaders;
51
- };
52
- };
53
- };
54
-
55
- type NodeFastResponse = InstanceType<typeof NodeFastResponse>;
56
- declare const NodeFastResponse: {
57
- new (body?: BodyInit | null, init?: ResponseInit): {
58
- "__#449@#body"?: BodyInit | null;
59
- "__#449@#init"?: ResponseInit;
60
- /**
61
- * Prepare Node.js response object
62
- */
63
- xNodeResponse(): {
64
- status: number;
65
- statusText: string;
66
- headers: NodeHttp__default.OutgoingHttpHeader[];
67
- body: string | Uint8Array<ArrayBufferLike> | ReadableStream<Uint8Array<ArrayBufferLike>> | Buffer<ArrayBufferLike> | DataView<ArrayBufferLike> | null | undefined;
68
- };
69
- /** Lazy initialized response instance */
70
- "__#449@#responseObj"?: Response;
71
- /** Lazy initialized headers instance */
72
- "__#449@#headersObj"?: Headers;
73
- clone(): Response;
74
- readonly "__#449@#response": Response;
75
- readonly headers: Headers;
76
- readonly ok: boolean;
77
- readonly redirected: boolean;
78
- readonly status: number;
79
- readonly statusText: string;
80
- readonly type: ResponseType;
81
- readonly url: string;
82
- "__#449@#fastBody"<T extends object>(as: new (...args: any[]) => T): T | null | false;
83
- readonly body: ReadableStream<Uint8Array> | null;
84
- readonly bodyUsed: boolean;
85
- arrayBuffer(): Promise<ArrayBuffer>;
86
- blob(): Promise<Blob>;
87
- bytes(): Promise<Uint8Array>;
88
- formData(): Promise<FormData>;
89
- text(): Promise<string>;
90
- json(): Promise<any>;
91
- };
92
- };
93
-
94
- declare const NodeReqHeadersProxy: {
95
- new (req: NodeHttp__default.IncomingMessage): {
96
- append(name: string, value: string): void;
97
- delete(name: string): void;
98
- get(name: string): string | null;
99
- getSetCookie(): string[];
100
- has(name: string): boolean;
101
- set(name: string, value: string): void;
102
- toJSON(): Record<string, string>;
103
- forEach(cb: (value: string, key: string, parent: Headers) => void, thisArg?: any): void;
104
- entries(): HeadersIterator<[string, string]>;
105
- keys(): HeadersIterator<string>;
106
- values(): HeadersIterator<string>;
107
- [kNodeReq]: NodeHttp__default.IncomingMessage;
108
- [Symbol.iterator](): HeadersIterator<[string, string]>;
109
- readonly [Symbol.toStringTag]: string;
110
- [kNodeInspect](): {
111
- [k: string]: string;
112
- };
113
- };
114
- };
115
- declare const NodeResHeadersProxy: {
116
- new (res: NodeHttp__default.ServerResponse): {
117
- append(name: string, value: string): void;
118
- delete(name: string): void;
119
- get(name: string): string | null;
120
- getSetCookie(): string[];
121
- has(name: string): boolean;
122
- set(name: string, value: string): void;
123
- forEach(cb: (value: string, key: string, parent: Headers) => void, thisArg?: any): void;
124
- entries(): HeadersIterator<[string, string]>;
125
- keys(): HeadersIterator<string>;
126
- values(): HeadersIterator<string>;
127
- [kNodeRes]: NodeHttp__default.ServerResponse;
128
- [Symbol.iterator](): HeadersIterator<[string, string]>;
129
- readonly [Symbol.toStringTag]: string;
130
- [kNodeInspect](): {
131
- [k: string]: string;
132
- };
133
- };
134
- };
135
-
136
- declare function sendNodeResponse(nodeRes: NodeHttp__default.ServerResponse, webRes: Response | NodeFastResponse): Promise<void>;
137
-
138
- export { NodeFastResponse, NodeReqHeadersProxy, NodeRequestProxy, NodeResHeadersProxy, sendNodeResponse };
@@ -1,229 +0,0 @@
1
- export { a as NodeReqHeadersProxy, N as NodeRequestProxy, b as NodeResHeadersProxy, s as sendNodeResponse } from '../shared/srvx.1e3bbf3a.mjs';
2
- import { splitSetCookieString } from 'cookie-es';
3
-
4
- const NodeFastResponse = /* @__PURE__ */ (() => (
5
- /**
6
- * Fast Response for Node.js runtime
7
- *
8
- * It is faster because in most cases it doesn't create a full Response instance.
9
- */
10
- class NodeFastResponse {
11
- #body;
12
- #init;
13
- constructor(body, init) {
14
- this.#body = body;
15
- this.#init = init;
16
- }
17
- /**
18
- * Prepare Node.js response object
19
- */
20
- xNodeResponse() {
21
- const status = this.#init?.status ?? 200;
22
- const statusText = this.#init?.statusText ?? "";
23
- const headers = [];
24
- let headersInit = this.#init?.headers;
25
- if (headersInit) {
26
- if (typeof headersInit === "object") {
27
- headersInit = Object.entries(headersInit);
28
- }
29
- for (const [key, value] of headersInit) {
30
- if (key === "set-cookie") {
31
- for (const setCookie of splitSetCookieString(value)) {
32
- headers.push(["set-cookie", setCookie]);
33
- }
34
- } else {
35
- headers.push([key, value]);
36
- }
37
- }
38
- }
39
- const bodyInit = this.#body;
40
- let body;
41
- if (bodyInit) {
42
- if (typeof bodyInit === "string") {
43
- body = bodyInit;
44
- } else if (bodyInit instanceof ReadableStream) {
45
- body = bodyInit;
46
- } else if (bodyInit instanceof ArrayBuffer) {
47
- body = Buffer.from(bodyInit);
48
- } else if (bodyInit instanceof Uint8Array) {
49
- body = Buffer.from(bodyInit);
50
- } else if (bodyInit instanceof DataView) {
51
- body = Buffer.from(bodyInit.buffer);
52
- } else if (bodyInit instanceof Blob) {
53
- body = bodyInit.stream();
54
- if (bodyInit.type) {
55
- headers.push(["content-type", bodyInit.type]);
56
- }
57
- } else {
58
- const res = new Response(bodyInit);
59
- body = res.body;
60
- for (const [key, value] of res.headers) {
61
- headers.push([key, value]);
62
- }
63
- }
64
- }
65
- this.#body = void 0;
66
- this.#init = void 0;
67
- return {
68
- status,
69
- statusText,
70
- headers,
71
- body
72
- };
73
- }
74
- // ... the rest is for interface compatibility only and usually not to be used ...
75
- /** Lazy initialized response instance */
76
- #responseObj;
77
- /** Lazy initialized headers instance */
78
- #headersObj;
79
- clone() {
80
- if (this.#responseObj) {
81
- return this.#responseObj.clone();
82
- }
83
- return new Response(this.#body, this.#init);
84
- }
85
- get #response() {
86
- if (!this.#responseObj) {
87
- this.#responseObj = new Response(this.#body, this.#init);
88
- this.#body = void 0;
89
- this.#init = void 0;
90
- this.#headersObj = void 0;
91
- }
92
- return this.#responseObj;
93
- }
94
- get headers() {
95
- if (this.#responseObj) {
96
- return this.#responseObj.headers;
97
- }
98
- if (!this.#headersObj) {
99
- this.#headersObj = new Headers(this.#init?.headers);
100
- }
101
- return this.#headersObj;
102
- }
103
- get ok() {
104
- if (this.#responseObj) {
105
- return this.#responseObj.ok;
106
- }
107
- const status = this.#init?.status ?? 200;
108
- return status >= 200 && status < 300;
109
- }
110
- get redirected() {
111
- if (this.#responseObj) {
112
- return this.#responseObj.redirected;
113
- }
114
- return false;
115
- }
116
- get status() {
117
- if (this.#responseObj) {
118
- return this.#responseObj.status;
119
- }
120
- return this.#init?.status ?? 200;
121
- }
122
- get statusText() {
123
- if (this.#responseObj) {
124
- return this.#responseObj.statusText;
125
- }
126
- return this.#init?.statusText ?? "";
127
- }
128
- get type() {
129
- if (this.#responseObj) {
130
- return this.#responseObj.type;
131
- }
132
- return "default";
133
- }
134
- get url() {
135
- if (this.#responseObj) {
136
- return this.#responseObj.url;
137
- }
138
- return "";
139
- }
140
- // --- body ---
141
- #fastBody(as) {
142
- const bodyInit = this.#body;
143
- if (bodyInit === null || bodyInit === void 0) {
144
- return null;
145
- }
146
- if (bodyInit instanceof as) {
147
- return bodyInit;
148
- }
149
- return false;
150
- }
151
- get body() {
152
- if (this.#responseObj) {
153
- return this.#responseObj.body;
154
- }
155
- const fastBody = this.#fastBody(ReadableStream);
156
- if (fastBody !== false) {
157
- return fastBody;
158
- }
159
- return this.#response.body;
160
- }
161
- get bodyUsed() {
162
- if (this.#responseObj) {
163
- return this.#responseObj.bodyUsed;
164
- }
165
- return false;
166
- }
167
- arrayBuffer() {
168
- if (this.#responseObj) {
169
- return this.#responseObj.arrayBuffer();
170
- }
171
- const fastBody = this.#fastBody(ArrayBuffer);
172
- if (fastBody !== false) {
173
- return Promise.resolve(fastBody || new ArrayBuffer(0));
174
- }
175
- return this.#response.arrayBuffer();
176
- }
177
- blob() {
178
- if (this.#responseObj) {
179
- return this.#responseObj.blob();
180
- }
181
- const fastBody = this.#fastBody(Blob);
182
- if (fastBody !== false) {
183
- return Promise.resolve(fastBody || new Blob());
184
- }
185
- return this.#response.blob();
186
- }
187
- bytes() {
188
- if (this.#responseObj) {
189
- return this.#responseObj.bytes();
190
- }
191
- const fastBody = this.#fastBody(Uint8Array);
192
- if (fastBody !== false) {
193
- return Promise.resolve(fastBody || new Uint8Array());
194
- }
195
- return this.#response.bytes();
196
- }
197
- formData() {
198
- if (this.#responseObj) {
199
- return this.#responseObj.formData();
200
- }
201
- const fastBody = this.#fastBody(FormData);
202
- if (fastBody !== false) {
203
- return Promise.resolve(fastBody || new FormData());
204
- }
205
- return this.#response.formData();
206
- }
207
- text() {
208
- if (this.#responseObj) {
209
- return this.#responseObj.text();
210
- }
211
- const bodyInit = this.#body;
212
- if (bodyInit === null || bodyInit === void 0) {
213
- return Promise.resolve("");
214
- }
215
- if (typeof bodyInit === "string") {
216
- return Promise.resolve(bodyInit);
217
- }
218
- return this.#response.text();
219
- }
220
- json() {
221
- if (this.#responseObj) {
222
- return this.#responseObj.json();
223
- }
224
- return this.text().then((text) => JSON.parse(text));
225
- }
226
- }
227
- ))();
228
-
229
- export { NodeFastResponse };
package/dist/node.d.mts DELETED
@@ -1,8 +0,0 @@
1
- import { ServerOptions, Server } from './types.mjs';
2
- import 'node:http';
3
- import 'node:net';
4
- import 'bun';
5
-
6
- declare function serve(options: ServerOptions): Server;
7
-
8
- export { serve };
package/dist/node.d.ts DELETED
@@ -1,8 +0,0 @@
1
- import { ServerOptions, Server } from './types.js';
2
- import 'node:http';
3
- import 'node:net';
4
- import 'bun';
5
-
6
- declare function serve(options: ServerOptions): Server;
7
-
8
- export { serve };
package/dist/node.mjs DELETED
@@ -1,70 +0,0 @@
1
- import NodeHttp from 'node:http';
2
- import { S as Server, r as resolvePort } from './shared/srvx.4f681732.mjs';
3
- import { N as NodeRequestProxy, s as sendNodeResponse } from './shared/srvx.1e3bbf3a.mjs';
4
- import 'cookie-es';
5
-
6
- function serve(options) {
7
- return new NodeServer(options);
8
- }
9
- class NodeServer extends Server {
10
- constructor() {
11
- super(...arguments);
12
- this.runtime = "node";
13
- }
14
- _listen() {
15
- const nodeServer = this.nodeServer = NodeHttp.createServer(
16
- {
17
- ...this.options.node
18
- },
19
- (nodeReq, nodeRes) => {
20
- const request = new NodeRequestProxy(nodeReq);
21
- request.xNode = { req: nodeReq, res: nodeRes };
22
- const res = this.fetch(request);
23
- return res instanceof Promise ? res.then((resolvedRes) => sendNodeResponse(nodeRes, resolvedRes)) : sendNodeResponse(nodeRes, res);
24
- }
25
- );
26
- return new Promise((resolve) => {
27
- nodeServer.listen(
28
- {
29
- port: resolvePort(this.options.port, globalThis.process?.env.PORT),
30
- host: this.options.hostname,
31
- exclusive: !this.options.reusePort,
32
- ...this.options.node
33
- },
34
- () => resolve()
35
- );
36
- });
37
- }
38
- get port() {
39
- const addr = this.#addr;
40
- if (!addr) {
41
- return null;
42
- }
43
- return addr.port;
44
- }
45
- get addr() {
46
- const addr = this.#addr;
47
- if (!addr) {
48
- return null;
49
- }
50
- return addr.address;
51
- }
52
- get #addr() {
53
- const addr = this.nodeServer?.address();
54
- if (addr && typeof addr !== "string") {
55
- return addr;
56
- }
57
- }
58
- close(closeAll) {
59
- return new Promise((resolve, reject) => {
60
- if (closeAll) {
61
- this.nodeServer?.closeAllConnections?.();
62
- }
63
- this.nodeServer?.close(
64
- (error) => error ? reject(error) : resolve()
65
- );
66
- });
67
- }
68
- }
69
-
70
- export { serve };
@@ -1,112 +0,0 @@
1
- async function applyPlugins(server) {
2
- const options = server.options;
3
- if (!options.plugins?.length) {
4
- return;
5
- }
6
- const requestHooks = [];
7
- const responseHooks = [];
8
- for (const ctor of options.plugins) {
9
- const plugin = typeof ctor === "function" ? await ctor(server) : ctor;
10
- if (plugin.request) {
11
- requestHooks.push(plugin.request);
12
- }
13
- if (plugin.response) {
14
- responseHooks.push(plugin.response);
15
- }
16
- }
17
- const hasRequestHooks = requestHooks.length > 0;
18
- const hasResponseHooks = responseHooks.length > 0;
19
- if (hasRequestHooks || hasResponseHooks) {
20
- server.fetch = (request) => {
21
- let resValue;
22
- let resPromise;
23
- if (hasRequestHooks) {
24
- for (const reqHook of requestHooks) {
25
- if (resPromise) {
26
- resPromise = resPromise.then((res) => res || reqHook(request));
27
- } else {
28
- const res = reqHook(request);
29
- if (res) {
30
- if (res instanceof Promise) {
31
- resPromise = res;
32
- } else {
33
- return res;
34
- }
35
- }
36
- }
37
- }
38
- }
39
- if (resPromise) {
40
- resPromise = resPromise.then((res) => res || options.fetch(request));
41
- } else {
42
- const res = options.fetch(request);
43
- if (res instanceof Promise) {
44
- resPromise = res;
45
- } else {
46
- resValue = res;
47
- }
48
- }
49
- if (hasResponseHooks) {
50
- for (const resHook of responseHooks) {
51
- if (resPromise) {
52
- resPromise = resPromise.then((res) => {
53
- if (res) {
54
- resValue = res;
55
- }
56
- return resHook(request, resValue);
57
- });
58
- } else {
59
- const res = resHook(request, resValue);
60
- if (res) {
61
- if (res instanceof Promise) {
62
- resPromise = res;
63
- } else {
64
- resValue = res;
65
- }
66
- }
67
- }
68
- }
69
- }
70
- return resPromise ? resPromise.then((res) => res || resValue) : resValue;
71
- };
72
- }
73
- }
74
-
75
- class Server {
76
- #listening;
77
- constructor(options) {
78
- this.options = options;
79
- this.fetch = options.fetch;
80
- this.#listening = applyPlugins(this).then(() => this._listen());
81
- }
82
- /**
83
- * Listening URL.
84
- */
85
- get url() {
86
- let addr = this.addr;
87
- const port = this.port;
88
- if (!addr || !port) {
89
- return null;
90
- }
91
- if (addr.includes(":")) {
92
- addr = `[${addr}]`;
93
- }
94
- return `http://${addr}:${port}/`;
95
- }
96
- /**
97
- * Returns a promise that resolves when the server is ready.
98
- */
99
- ready() {
100
- return Promise.resolve(this.#listening).then(() => this);
101
- }
102
- }
103
-
104
- function resolvePort(portOptions, portEnv) {
105
- const portInput = portOptions ?? portEnv;
106
- if (portInput === void 0) {
107
- return 3e3;
108
- }
109
- return typeof portInput === "number" ? portInput : Number.parseInt(portInput, 10);
110
- }
111
-
112
- export { Server as S, resolvePort as r };