srvx 0.1.4 → 0.2.1

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