srvx 0.0.0 → 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Pooya Parsa <pooya@pi0.io>
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,68 @@
1
+ # 💥 srvx
2
+
3
+ <!-- automd:badges color=yellow -->
4
+
5
+ [![npm version](https://img.shields.io/npm/v/srvx?color=yellow)](https://npmjs.com/package/srvx)
6
+ [![npm downloads](https://img.shields.io/npm/dm/srvx?color=yellow)](https://npm.chart.dev/srvx)
7
+
8
+ <!-- /automd -->
9
+
10
+ Universal Server API based on web platform standards. Works with [Deno](https://deno.com/), [Bun](https://bun.sh/) and [Node.js](https://nodejs.org/en).
11
+
12
+ - ✅ Seamless runtime integration with identical usage ([handler](https://srvx.unjs.io/guide/handler) and
13
+ - ✅ Zero overhead [Deno](https://deno.com/) and [Bun](https://bun.sh/) support
14
+ - ✅ [Node.js compatibility](https://srvx.unjs.io/guide/node) with ~native perf and [fast response]https://srvx.unjs.io/guide/node#fast-response) support
15
+
16
+ ## Quick start
17
+
18
+ ```js
19
+ import { serve } from "srvx";
20
+
21
+ const server = serve({
22
+ port: 3000,
23
+ fetch(request) {
24
+ return new Response("👋 Hello there!");
25
+ },
26
+ });
27
+
28
+ await server.ready();
29
+
30
+ console.log(`🚀 Server ready at ${server.url}`);
31
+ ```
32
+
33
+ 👉 **Visit the 📖 [Documentation](https://srvx.unjs.io/) to learn more.**
34
+
35
+ ## Development
36
+
37
+ <details>
38
+
39
+ <summary>local development</summary>
40
+
41
+ - Clone this repository
42
+ - Install the latest LTS version of [Node.js](https://nodejs.org/en/)
43
+ - Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable`
44
+ - Install dependencies using `pnpm install`
45
+ - Run interactive tests using `pnpm dev`
46
+
47
+ </details>
48
+
49
+ ## License
50
+
51
+ <!-- automd:contributors author=pi0 license=MIT -->
52
+
53
+ Published under the [MIT](https://github.com/unjs/srvx/blob/main/LICENSE) license.
54
+ Made by [@pi0](https://github.com/pi0) and [community](https://github.com/unjs/srvx/graphs/contributors) 💛
55
+ <br><br>
56
+ <a href="https://github.com/unjs/srvx/graphs/contributors">
57
+ <img src="https://contrib.rocks/image?repo=unjs/srvx" />
58
+ </a>
59
+
60
+ <!-- /automd -->
61
+
62
+ <!-- automd:with-automd -->
63
+
64
+ ---
65
+
66
+ _🤖 auto updated with [automd](https://automd.unjs.io)_
67
+
68
+ <!-- /automd -->
package/dist/bun.d.mts ADDED
@@ -0,0 +1,8 @@
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/bun.d.ts ADDED
@@ -0,0 +1,8 @@
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/bun.mjs ADDED
@@ -0,0 +1,42 @@
1
+ import { S as Server, r as resolvePort } from './shared/srvx.4f681732.mjs';
2
+
3
+ function serve(options) {
4
+ return new BunServer(options);
5
+ }
6
+ class BunServer extends Server {
7
+ constructor() {
8
+ super(...arguments);
9
+ this.runtime = "bun";
10
+ }
11
+ _listen() {
12
+ let serverFetch = this.fetch;
13
+ if (this.options.xRemoteAddress) {
14
+ const userFetch = this.fetch;
15
+ serverFetch = (request) => {
16
+ Object.defineProperty(request, "xRemoteAddress", {
17
+ get: () => this.bunServer?.requestIP(request)?.address,
18
+ enumerable: true
19
+ });
20
+ return userFetch(request);
21
+ };
22
+ }
23
+ this.bunServer = Bun.serve({
24
+ port: resolvePort(this.options.port, globalThis.process?.env.PORT),
25
+ hostname: this.options.hostname,
26
+ reusePort: this.options.reusePort,
27
+ ...this.options.bun,
28
+ fetch: serverFetch
29
+ });
30
+ }
31
+ get port() {
32
+ return this.bunServer?.port ?? null;
33
+ }
34
+ get addr() {
35
+ return this.bunServer?.hostname ?? null;
36
+ }
37
+ close(closeAll) {
38
+ this.bunServer?.stop(closeAll);
39
+ }
40
+ }
41
+
42
+ export { serve };
@@ -0,0 +1,8 @@
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/deno.d.ts ADDED
@@ -0,0 +1,8 @@
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/deno.mjs ADDED
@@ -0,0 +1,56 @@
1
+ import { S as Server, r as resolvePort } from './shared/srvx.4f681732.mjs';
2
+
3
+ function serve(options) {
4
+ return new DenoServer(options);
5
+ }
6
+ class DenoServer extends Server {
7
+ constructor() {
8
+ super(...arguments);
9
+ this.runtime = "deno";
10
+ }
11
+ #listeningInfo;
12
+ _listen() {
13
+ const onListenPromise = Promise.withResolvers();
14
+ let serverFetch = this.fetch;
15
+ if (this.options.xRemoteAddress) {
16
+ const userFetch = serverFetch;
17
+ serverFetch = (request, info) => {
18
+ Object.defineProperty(request, "xRemoteAddress", {
19
+ get: () => info?.remoteAddr?.hostname,
20
+ enumerable: true
21
+ });
22
+ return userFetch(request);
23
+ };
24
+ }
25
+ this.denoServer = Deno.serve(
26
+ {
27
+ port: resolvePort(
28
+ this.options.port,
29
+ globalThis.Deno?.env.get("PORT")
30
+ ),
31
+ hostname: this.options.hostname,
32
+ reusePort: this.options.reusePort,
33
+ ...this.options.deno,
34
+ onListen: (info) => {
35
+ if (this.options.deno?.onListen) {
36
+ this.options.deno.onListen(info);
37
+ }
38
+ this.#listeningInfo = info;
39
+ onListenPromise.resolve();
40
+ }
41
+ },
42
+ serverFetch
43
+ );
44
+ }
45
+ get port() {
46
+ return this.#listeningInfo?.port ?? null;
47
+ }
48
+ get addr() {
49
+ return this.#listeningInfo?.hostname ?? null;
50
+ }
51
+ close(_closeAll) {
52
+ this.denoServer?.shutdown();
53
+ }
54
+ }
55
+
56
+ export { serve };
@@ -0,0 +1,140 @@
1
+ import NodeHttp__default from 'node:http';
2
+ import { xHeaders, xRequest } from '../types.mjs';
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
+ xNode: {
24
+ req: NodeHttp__default.IncomingMessage;
25
+ res: NodeHttp__default.ServerResponse;
26
+ } | undefined;
27
+ "__#444@#url": URL;
28
+ "__#444@#abortSignal"?: AbortController;
29
+ "__#444@#hasBody": boolean | undefined;
30
+ "__#444@#rawBody"?: Promise<Uint8Array>;
31
+ "__#444@#blobBody"?: Promise<Blob>;
32
+ "__#444@#formDataBody"?: Promise<FormData>;
33
+ "__#444@#jsonBody"?: Promise<any>;
34
+ "__#444@#textBody"?: Promise<string>;
35
+ "__#444@#bodyStream"?: undefined | ReadableStream<Uint8Array>;
36
+ readonly xRemoteAddress: string | undefined;
37
+ clone(): xRequest;
38
+ readonly url: string;
39
+ readonly method: string;
40
+ readonly signal: AbortSignal;
41
+ readonly _hasBody: boolean;
42
+ readonly body: ReadableStream<Uint8Array> | null;
43
+ arrayBuffer(): Promise<ArrayBuffer>;
44
+ blob(): Promise<Blob>;
45
+ formData(): Promise<FormData>;
46
+ json(): Promise<any>;
47
+ text(): Promise<string>;
48
+ [kNodeReq]: NodeHttp__default.IncomingMessage;
49
+ readonly [Symbol.toStringTag]: string;
50
+ [kNodeInspect](): {
51
+ method: string;
52
+ url: string;
53
+ headers: xHeaders;
54
+ };
55
+ };
56
+ };
57
+
58
+ type NodeFastResponse = InstanceType<typeof NodeFastResponse>;
59
+ declare const NodeFastResponse: {
60
+ new (body?: BodyInit | null, init?: ResponseInit): {
61
+ "__#445@#body"?: BodyInit | null;
62
+ "__#445@#init"?: ResponseInit;
63
+ /**
64
+ * Prepare Node.js response object
65
+ */
66
+ xNodeResponse(): {
67
+ status: number;
68
+ statusText: string;
69
+ headers: NodeHttp__default.OutgoingHttpHeader[];
70
+ body: string | Uint8Array | ReadableStream<Uint8Array> | Buffer | DataView | null | undefined;
71
+ };
72
+ /** Lazy initialized response instance */
73
+ "__#445@#responseObj"?: Response;
74
+ /** Lazy initialized headers instance */
75
+ "__#445@#headersObj"?: Headers;
76
+ clone(): Response;
77
+ readonly "__#445@#response": Response;
78
+ readonly headers: Headers;
79
+ readonly ok: boolean;
80
+ readonly redirected: boolean;
81
+ readonly status: number;
82
+ readonly statusText: string;
83
+ readonly type: ResponseType;
84
+ readonly url: string;
85
+ "__#445@#fastBody"<T extends object>(as: new (...args: any[]) => T): T | null | false;
86
+ readonly body: ReadableStream<Uint8Array> | null;
87
+ readonly bodyUsed: boolean;
88
+ arrayBuffer(): Promise<ArrayBuffer>;
89
+ blob(): Promise<Blob>;
90
+ formData(): Promise<FormData>;
91
+ text(): Promise<string>;
92
+ json(): Promise<any>;
93
+ };
94
+ };
95
+
96
+ declare const NodeReqHeadersProxy: {
97
+ new (req: NodeHttp__default.IncomingMessage): {
98
+ append(name: string, value: string): void;
99
+ delete(name: string): void;
100
+ get(name: string): string | null;
101
+ getSetCookie(): string[];
102
+ has(name: string): boolean;
103
+ set(name: string, value: string): void;
104
+ toJSON(): Record<string, string>;
105
+ forEach(cb: (value: string, key: string, parent: Headers) => void, thisArg?: any): void;
106
+ entries(): HeadersIterator<[string, string]>;
107
+ keys(): HeadersIterator<string>;
108
+ values(): HeadersIterator<string>;
109
+ [kNodeReq]: NodeHttp__default.IncomingMessage;
110
+ [Symbol.iterator](): HeadersIterator<[string, string]>;
111
+ readonly [Symbol.toStringTag]: string;
112
+ [kNodeInspect](): {
113
+ [k: string]: string;
114
+ };
115
+ };
116
+ };
117
+ declare const NodeResHeadersProxy: {
118
+ new (res: NodeHttp__default.ServerResponse): {
119
+ append(name: string, value: string): void;
120
+ delete(name: string): void;
121
+ get(name: string): string | null;
122
+ getSetCookie(): string[];
123
+ has(name: string): boolean;
124
+ set(name: string, value: string): void;
125
+ forEach(cb: (value: string, key: string, parent: Headers) => void, thisArg?: any): void;
126
+ entries(): HeadersIterator<[string, string]>;
127
+ keys(): HeadersIterator<string>;
128
+ values(): HeadersIterator<string>;
129
+ [kNodeRes]: NodeHttp__default.ServerResponse;
130
+ [Symbol.iterator](): HeadersIterator<[string, string]>;
131
+ readonly [Symbol.toStringTag]: string;
132
+ [kNodeInspect](): {
133
+ [k: string]: string;
134
+ };
135
+ };
136
+ };
137
+
138
+ declare function sendNodeResponse(nodeRes: NodeHttp__default.ServerResponse, webRes: Response | NodeFastResponse): Promise<void>;
139
+
140
+ export { NodeFastResponse, NodeReqHeadersProxy, NodeRequestProxy, NodeResHeadersProxy, sendNodeResponse };
@@ -0,0 +1,140 @@
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
+ xNode: {
24
+ req: NodeHttp__default.IncomingMessage;
25
+ res: NodeHttp__default.ServerResponse;
26
+ } | undefined;
27
+ "__#444@#url": URL;
28
+ "__#444@#abortSignal"?: AbortController;
29
+ "__#444@#hasBody": boolean | undefined;
30
+ "__#444@#rawBody"?: Promise<Uint8Array>;
31
+ "__#444@#blobBody"?: Promise<Blob>;
32
+ "__#444@#formDataBody"?: Promise<FormData>;
33
+ "__#444@#jsonBody"?: Promise<any>;
34
+ "__#444@#textBody"?: Promise<string>;
35
+ "__#444@#bodyStream"?: undefined | ReadableStream<Uint8Array>;
36
+ readonly xRemoteAddress: string | undefined;
37
+ clone(): xRequest;
38
+ readonly url: string;
39
+ readonly method: string;
40
+ readonly signal: AbortSignal;
41
+ readonly _hasBody: boolean;
42
+ readonly body: ReadableStream<Uint8Array> | null;
43
+ arrayBuffer(): Promise<ArrayBuffer>;
44
+ blob(): Promise<Blob>;
45
+ formData(): Promise<FormData>;
46
+ json(): Promise<any>;
47
+ text(): Promise<string>;
48
+ [kNodeReq]: NodeHttp__default.IncomingMessage;
49
+ readonly [Symbol.toStringTag]: string;
50
+ [kNodeInspect](): {
51
+ method: string;
52
+ url: string;
53
+ headers: xHeaders;
54
+ };
55
+ };
56
+ };
57
+
58
+ type NodeFastResponse = InstanceType<typeof NodeFastResponse>;
59
+ declare const NodeFastResponse: {
60
+ new (body?: BodyInit | null, init?: ResponseInit): {
61
+ "__#445@#body"?: BodyInit | null;
62
+ "__#445@#init"?: ResponseInit;
63
+ /**
64
+ * Prepare Node.js response object
65
+ */
66
+ xNodeResponse(): {
67
+ status: number;
68
+ statusText: string;
69
+ headers: NodeHttp__default.OutgoingHttpHeader[];
70
+ body: string | Uint8Array | ReadableStream<Uint8Array> | Buffer | DataView | null | undefined;
71
+ };
72
+ /** Lazy initialized response instance */
73
+ "__#445@#responseObj"?: Response;
74
+ /** Lazy initialized headers instance */
75
+ "__#445@#headersObj"?: Headers;
76
+ clone(): Response;
77
+ readonly "__#445@#response": Response;
78
+ readonly headers: Headers;
79
+ readonly ok: boolean;
80
+ readonly redirected: boolean;
81
+ readonly status: number;
82
+ readonly statusText: string;
83
+ readonly type: ResponseType;
84
+ readonly url: string;
85
+ "__#445@#fastBody"<T extends object>(as: new (...args: any[]) => T): T | null | false;
86
+ readonly body: ReadableStream<Uint8Array> | null;
87
+ readonly bodyUsed: boolean;
88
+ arrayBuffer(): Promise<ArrayBuffer>;
89
+ blob(): Promise<Blob>;
90
+ formData(): Promise<FormData>;
91
+ text(): Promise<string>;
92
+ json(): Promise<any>;
93
+ };
94
+ };
95
+
96
+ declare const NodeReqHeadersProxy: {
97
+ new (req: NodeHttp__default.IncomingMessage): {
98
+ append(name: string, value: string): void;
99
+ delete(name: string): void;
100
+ get(name: string): string | null;
101
+ getSetCookie(): string[];
102
+ has(name: string): boolean;
103
+ set(name: string, value: string): void;
104
+ toJSON(): Record<string, string>;
105
+ forEach(cb: (value: string, key: string, parent: Headers) => void, thisArg?: any): void;
106
+ entries(): HeadersIterator<[string, string]>;
107
+ keys(): HeadersIterator<string>;
108
+ values(): HeadersIterator<string>;
109
+ [kNodeReq]: NodeHttp__default.IncomingMessage;
110
+ [Symbol.iterator](): HeadersIterator<[string, string]>;
111
+ readonly [Symbol.toStringTag]: string;
112
+ [kNodeInspect](): {
113
+ [k: string]: string;
114
+ };
115
+ };
116
+ };
117
+ declare const NodeResHeadersProxy: {
118
+ new (res: NodeHttp__default.ServerResponse): {
119
+ append(name: string, value: string): void;
120
+ delete(name: string): void;
121
+ get(name: string): string | null;
122
+ getSetCookie(): string[];
123
+ has(name: string): boolean;
124
+ set(name: string, value: string): void;
125
+ forEach(cb: (value: string, key: string, parent: Headers) => void, thisArg?: any): void;
126
+ entries(): HeadersIterator<[string, string]>;
127
+ keys(): HeadersIterator<string>;
128
+ values(): HeadersIterator<string>;
129
+ [kNodeRes]: NodeHttp__default.ServerResponse;
130
+ [Symbol.iterator](): HeadersIterator<[string, string]>;
131
+ readonly [Symbol.toStringTag]: string;
132
+ [kNodeInspect](): {
133
+ [k: string]: string;
134
+ };
135
+ };
136
+ };
137
+
138
+ declare function sendNodeResponse(nodeRes: NodeHttp__default.ServerResponse, webRes: Response | NodeFastResponse): Promise<void>;
139
+
140
+ export { NodeFastResponse, NodeReqHeadersProxy, NodeRequestProxy, NodeResHeadersProxy, sendNodeResponse };