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.
@@ -0,0 +1,22 @@
1
+ import { ServerOptions, Server } from '../types.mjs';
2
+ import * as Bun from 'bun';
3
+ import 'node:http';
4
+ import 'node:net';
5
+ import '@cloudflare/workers-types';
6
+
7
+ type BunHandler = (request: Request, server?: Bun.Server) => Response | Promise<Response>;
8
+ declare function serve(options: ServerOptions): BunServer;
9
+ declare class BunServer implements Server<BunHandler> {
10
+ readonly runtime = "bun";
11
+ readonly options: ServerOptions;
12
+ readonly bun: Server["bun"];
13
+ readonly serveOptions: Bun.ServeOptions;
14
+ readonly fetch: BunHandler;
15
+ constructor(options: ServerOptions);
16
+ serve(): Promise<Awaited<this>>;
17
+ get url(): string | undefined;
18
+ ready(): Promise<Awaited<this>>;
19
+ close(closeAll?: boolean): Promise<void | undefined>;
20
+ }
21
+
22
+ export { serve };
@@ -0,0 +1,51 @@
1
+ import { r as resolvePort } from '../shared/srvx.PbkQy9Ck.mjs';
2
+ import { w as wrapFetch } from '../shared/srvx.DhN4g5wJ.mjs';
3
+
4
+ function serve(options) {
5
+ return new BunServer(options);
6
+ }
7
+ class BunServer {
8
+ constructor(options) {
9
+ this.runtime = "bun";
10
+ this.bun = {};
11
+ this.options = options;
12
+ const fetchHandler = wrapFetch(this, this.options.fetch);
13
+ this.fetch = (request, server) => {
14
+ Object.defineProperties(request, {
15
+ bun: { value: { server }, enumerable: true },
16
+ remoteAddress: {
17
+ get: () => server?.requestIP(request)?.address,
18
+ enumerable: true
19
+ }
20
+ });
21
+ return fetchHandler(request);
22
+ };
23
+ this.serveOptions = {
24
+ hostname: this.options.hostname,
25
+ reusePort: this.options.reusePort,
26
+ port: resolvePort(this.options.port, globalThis.process?.env.PORT),
27
+ ...this.options.bun,
28
+ fetch: this.fetch
29
+ };
30
+ if (!options.manual) {
31
+ this.serve();
32
+ }
33
+ }
34
+ serve() {
35
+ if (!this.bun.server) {
36
+ this.bun.server = Bun.serve(this.serveOptions);
37
+ }
38
+ return Promise.resolve(this);
39
+ }
40
+ get url() {
41
+ return this.bun?.server?.url.href;
42
+ }
43
+ ready() {
44
+ return Promise.resolve(this);
45
+ }
46
+ close(closeAll) {
47
+ return Promise.resolve(this.bun?.server?.stop(closeAll));
48
+ }
49
+ }
50
+
51
+ export { serve };
@@ -0,0 +1,9 @@
1
+ import { ServerOptions, Server } from '../types.mjs';
2
+ import * as CF from '@cloudflare/workers-types';
3
+ import 'node:http';
4
+ import 'node:net';
5
+ import 'bun';
6
+
7
+ declare function serve(options: ServerOptions): Server<CF.ExportedHandlerFetchHandler>;
8
+
9
+ export { serve };
@@ -0,0 +1,44 @@
1
+ import { w as wrapFetch } from '../shared/srvx.DhN4g5wJ.mjs';
2
+
3
+ function serve(options) {
4
+ return new CloudflareServer(options);
5
+ }
6
+ class CloudflareServer {
7
+ constructor(options) {
8
+ this.runtime = "cloudflare";
9
+ this.options = options;
10
+ const fetchHandler = wrapFetch(
11
+ this,
12
+ this.options.fetch
13
+ );
14
+ this.fetch = (request, env, context) => {
15
+ Object.defineProperties(request, {
16
+ cloudflare: { value: { env, context }, enumerable: true },
17
+ remoteAddress: {
18
+ get: () => void 0,
19
+ enumerable: true
20
+ }
21
+ });
22
+ return fetchHandler(request);
23
+ };
24
+ this.serveOptions = {
25
+ fetch: this.fetch
26
+ };
27
+ if (!options.manual) {
28
+ this.serve();
29
+ }
30
+ }
31
+ serve() {
32
+ addEventListener("fetch", (event) => {
33
+ event.respondWith(this.fetch(event.request, {}, event));
34
+ });
35
+ }
36
+ ready() {
37
+ return Promise.resolve().then(() => this);
38
+ }
39
+ close() {
40
+ return Promise.resolve();
41
+ }
42
+ }
43
+
44
+ export { serve };
@@ -0,0 +1,23 @@
1
+ import { ServerOptions, Server } from '../types.mjs';
2
+ import 'node:http';
3
+ import 'node:net';
4
+ import 'bun';
5
+ import '@cloudflare/workers-types';
6
+
7
+ type DenoHandler = (request: Request, info?: Deno.ServeHandlerInfo<Deno.NetAddr>) => Response | Promise<Response>;
8
+ declare function serve(options: ServerOptions): DenoServer;
9
+ declare class DenoServer implements Server<DenoHandler> {
10
+ #private;
11
+ readonly runtime = "deno";
12
+ readonly options: ServerOptions;
13
+ readonly deno: Server["deno"];
14
+ readonly serveOptions: Deno.ServeTcpOptions;
15
+ readonly fetch: DenoHandler;
16
+ constructor(options: ServerOptions);
17
+ serve(): Promise<this>;
18
+ get url(): string | undefined;
19
+ ready(): Promise<Server>;
20
+ close(): Promise<void | undefined>;
21
+ }
22
+
23
+ export { serve };
@@ -0,0 +1,67 @@
1
+ import { r as resolvePort, f as fmtURL } from '../shared/srvx.PbkQy9Ck.mjs';
2
+ import { w as wrapFetch } from '../shared/srvx.DhN4g5wJ.mjs';
3
+
4
+ function serve(options) {
5
+ return new DenoServer(options);
6
+ }
7
+ class DenoServer {
8
+ constructor(options) {
9
+ this.runtime = "deno";
10
+ this.deno = {};
11
+ this.options = options;
12
+ const fetchHandler = wrapFetch(this, this.options.fetch);
13
+ this.fetch = (request, info) => {
14
+ Object.defineProperties(request, {
15
+ deno: { value: { info, server: this.deno?.server }, enumerable: true },
16
+ remoteAddress: {
17
+ get: () => info?.remoteAddr?.hostname,
18
+ enumerable: true
19
+ }
20
+ });
21
+ return fetchHandler(request);
22
+ };
23
+ this.serveOptions = {
24
+ port: resolvePort(this.options.port, globalThis.Deno?.env.get("PORT")),
25
+ hostname: this.options.hostname,
26
+ reusePort: this.options.reusePort,
27
+ ...this.options.deno
28
+ };
29
+ if (!options.manual) {
30
+ this.serve();
31
+ }
32
+ }
33
+ #listeningPromise;
34
+ #listeningInfo;
35
+ serve() {
36
+ if (this.deno?.server) {
37
+ return Promise.resolve(this.#listeningPromise).then(() => this);
38
+ }
39
+ const onListenPromise = Promise.withResolvers();
40
+ this.#listeningPromise = onListenPromise.promise;
41
+ this.deno.server = Deno.serve(
42
+ {
43
+ ...this.serveOptions,
44
+ onListen: (info) => {
45
+ this.#listeningInfo = info;
46
+ if (this.options.deno?.onListen) {
47
+ this.options.deno.onListen(info);
48
+ }
49
+ onListenPromise.resolve();
50
+ }
51
+ },
52
+ this.fetch
53
+ );
54
+ return Promise.resolve(this.#listeningPromise).then(() => this);
55
+ }
56
+ get url() {
57
+ return this.#listeningInfo ? fmtURL(this.#listeningInfo.hostname, this.#listeningInfo.port) : void 0;
58
+ }
59
+ ready() {
60
+ return Promise.resolve(this.#listeningPromise).then(() => this);
61
+ }
62
+ close() {
63
+ return Promise.resolve(this.deno?.server?.shutdown());
64
+ }
65
+ }
66
+
67
+ export { serve };
@@ -0,0 +1,48 @@
1
+ import { ServerOptions, Server } from '../types.mjs';
2
+ import NodeHttp__default from 'node:http';
3
+ import 'node:net';
4
+ import 'bun';
5
+ import '@cloudflare/workers-types';
6
+
7
+ type NodeFastResponse = InstanceType<typeof NodeFastResponse>;
8
+ declare const NodeFastResponse: {
9
+ new (body?: BodyInit | null, init?: ResponseInit): {
10
+ "__#4112@#body"?: BodyInit | null;
11
+ "__#4112@#init"?: ResponseInit;
12
+ /**
13
+ * Prepare Node.js response object
14
+ */
15
+ nodeResponse(): {
16
+ status: number;
17
+ statusText: string;
18
+ headers: NodeHttp__default.OutgoingHttpHeader[];
19
+ body: string | Uint8Array<ArrayBufferLike> | ReadableStream<Uint8Array<ArrayBufferLike>> | Buffer<ArrayBufferLike> | DataView<ArrayBufferLike> | null | undefined;
20
+ };
21
+ /** Lazy initialized response instance */
22
+ "__#4112@#responseObj"?: Response;
23
+ /** Lazy initialized headers instance */
24
+ "__#4112@#headersObj"?: Headers;
25
+ clone(): Response;
26
+ readonly "__#4112@#response": Response;
27
+ readonly headers: Headers;
28
+ readonly ok: boolean;
29
+ readonly redirected: boolean;
30
+ readonly status: number;
31
+ readonly statusText: string;
32
+ readonly type: ResponseType;
33
+ readonly url: string;
34
+ "__#4112@#fastBody"<T extends object>(as: new (...args: any[]) => T): T | null | false;
35
+ readonly body: ReadableStream<Uint8Array> | null;
36
+ readonly bodyUsed: boolean;
37
+ arrayBuffer(): Promise<ArrayBuffer>;
38
+ blob(): Promise<Blob>;
39
+ bytes(): Promise<Uint8Array>;
40
+ formData(): Promise<FormData>;
41
+ text(): Promise<string>;
42
+ json(): Promise<any>;
43
+ };
44
+ };
45
+
46
+ declare function serve(options: ServerOptions): Server;
47
+
48
+ export { NodeFastResponse as Response, serve };