open-nest 0.0.0-stage → 0.0.2

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/dist/bun.d.mts ADDED
@@ -0,0 +1,48 @@
1
+ import { i as Fetcher, r as FetchAdapter } from "./index-DCLeOkkF.mjs";
2
+ import { n as WsGateway, t as GatewayClient } from "./gateway-CrNeg78D.mjs";
3
+ import { INestApplication, NestApplicationOptions, Type } from "@nestjs/common";
4
+ //#region src/adapters/bun/index.d.ts
5
+ /**
6
+ * Records Nest gateways during `init`. `Bun.serve` performs the upgrade.
7
+ */
8
+ export declare class BunWsAdapter extends WsGateway<GatewayClient> {
9
+ constructor();
10
+ dispose(): Promise<void>;
11
+ }
12
+ /**
13
+ * Fetch adapter plus static files. Workers stay on {@link FetchAdapter}.
14
+ */
15
+ export declare class BunAdapter extends FetchAdapter {
16
+ useStaticAssets(root?: string, options?: {
17
+ prefix?: string;
18
+ index?: string | false;
19
+ }): this;
20
+ }
21
+ export interface CreateBunServerOptions {
22
+ /**
23
+ * Root Nest module.
24
+ */
25
+ module: Type<unknown>;
26
+ /**
27
+ * Port passed to `Bun.serve`. `0` asks the OS for a free port.
28
+ */
29
+ port?: number;
30
+ /**
31
+ * Runs after the app is created and before init.
32
+ */
33
+ configure?: (app: INestApplication) => void | Promise<void>;
34
+ /**
35
+ * Forwarded to `NestFactory.create`.
36
+ */
37
+ opts?: NestApplicationOptions;
38
+ }
39
+ export interface BunServer {
40
+ port: number;
41
+ fetch: Fetcher;
42
+ close(): Promise<void>;
43
+ }
44
+ /**
45
+ * Boots a Nest app and serves it with `Bun.serve`, including WebSocket upgrades.
46
+ */
47
+ export declare function createBunServer({ module, port, configure, opts }: CreateBunServerOptions): Promise<BunServer>;
48
+ //#endregion
package/dist/bun.mjs ADDED
@@ -0,0 +1,100 @@
1
+ import { n as createFetcher, t as FetchAdapter } from "./fetch-Bmr9TEe3.mjs";
2
+ import { n as gatewayPath, t as WsGateway } from "./gateway-D7PGV2DW.mjs";
3
+ import { resolve, sep } from "node:path";
4
+ //#region src/adapters/bun/index.ts
5
+ const adapters = [];
6
+ /**
7
+ * Records Nest gateways during `init`. `Bun.serve` performs the upgrade.
8
+ */
9
+ var BunWsAdapter = class extends WsGateway {
10
+ constructor() {
11
+ super();
12
+ adapters.push(this);
13
+ }
14
+ async dispose() {
15
+ const index = adapters.indexOf(this);
16
+ if (index !== -1) adapters.splice(index, 1);
17
+ }
18
+ };
19
+ /**
20
+ * Fetch adapter plus static files. Workers stay on {@link FetchAdapter}.
21
+ */
22
+ var BunAdapter = class extends FetchAdapter {
23
+ useStaticAssets(root, options) {
24
+ if (!root) return this;
25
+ const prefix = options?.prefix ?? "/";
26
+ const index = options?.index === void 0 ? "index.html" : options.index;
27
+ const rootResolved = resolve(root);
28
+ this.router.use(prefix, async (req, res, next) => {
29
+ if (req.method !== "GET" && req.method !== "HEAD") return next();
30
+ let relative;
31
+ try {
32
+ relative = decodeURIComponent(req.path);
33
+ } catch {
34
+ return next();
35
+ }
36
+ if (relative === "/") {
37
+ if (index === false) return next();
38
+ relative = index;
39
+ } else relative = relative.replace(/^\//, "");
40
+ const target = resolve(rootResolved, relative);
41
+ if (target !== rootResolved && !target.startsWith(rootResolved + sep)) return next();
42
+ const file = Bun.file(target);
43
+ if (!await file.exists()) return next();
44
+ res.setHeader("content-type", file.type || "application/octet-stream");
45
+ res.setHeader("content-length", String(file.size));
46
+ if (req.method === "HEAD") {
47
+ res.end();
48
+ return;
49
+ }
50
+ res.end(new Uint8Array(await file.arrayBuffer()));
51
+ });
52
+ return this;
53
+ }
54
+ };
55
+ /**
56
+ * Boots a Nest app and serves it with `Bun.serve`, including WebSocket upgrades.
57
+ */
58
+ async function createBunServer({ module, port, configure, opts }) {
59
+ const fallback = new BunWsAdapter();
60
+ const seen = new Set(adapters);
61
+ const fetcher = createFetcher({
62
+ module,
63
+ opts,
64
+ configure: async (app) => {
65
+ app.useWebSocketAdapter(fallback);
66
+ await configure?.(app);
67
+ }
68
+ }, new BunAdapter());
69
+ await fetcher.app();
70
+ const active = [...adapters].reverse().find((adapter) => !seen.has(adapter) && adapter.hasGateways()) ?? fallback;
71
+ const server = Bun.serve({
72
+ port: port ?? 3e3,
73
+ fetch: (request, bun) => {
74
+ const pathname = new URL(request.url).pathname;
75
+ if (active.matches(pathname) && bun.upgrade(request, { data: { path: gatewayPath(pathname) } })) return;
76
+ return fetcher(request);
77
+ },
78
+ websocket: {
79
+ open: (client) => {
80
+ active.open(client);
81
+ },
82
+ message: (client, message) => {
83
+ active.receive(client, message);
84
+ },
85
+ close: (client) => {
86
+ active.closed(client);
87
+ }
88
+ }
89
+ });
90
+ return {
91
+ port: server.port ?? port ?? 3e3,
92
+ fetch: fetcher,
93
+ close: async () => {
94
+ await server.stop(true);
95
+ await fetcher.close();
96
+ }
97
+ };
98
+ }
99
+ //#endregion
100
+ export { BunAdapter, BunWsAdapter, createBunServer };