@tupik/bun 0.0.0-stage → 1.0.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) 2026 Carlos Alberto Barbosa Stenzel / Tupik Contributors
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 CHANGED
@@ -1,3 +1,39 @@
1
- # Temporary Holding Version
1
+ # @tupik/bun
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Bun adapter for **Tupik**: serves an app with `Bun.serve`.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ bun add @tupik/bun @tupik/core
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ Run with `bun run server.ts`:
14
+
15
+ ```ts
16
+ import { createApp, createModule } from '@tupik/core';
17
+ import { serveBun } from '@tupik/bun';
18
+
19
+ const app = createApp().register(
20
+ createModule().get('/', { access: 'public', handler: () => ({ runtime: 'bun' }) }),
21
+ );
22
+
23
+ const server = serveBun(app, { port: 3000 });
24
+ const response = await fetch('http://localhost:3000/');
25
+ console.log(await response.json()); // { runtime: 'bun' }
26
+ server.stop();
27
+ ```
28
+
29
+ ## Key API
30
+
31
+ | Export | Purpose |
32
+ | --- | --- |
33
+ | `serveBun(app, { port?, hostname? })` | Starts `Bun.serve` with the app's paths as native `routes` and `fetch` as fallback; returns the server (`stop()`) |
34
+ | `createBunRoutes(app, handler)` / `toBunPath(path)` | The `routes` object `serveBun` builds |
35
+ | `createBunHandler(app)` | Bare fetch handler for `Bun.serve({ fetch })` |
36
+
37
+ ## License
38
+
39
+ MIT
@@ -0,0 +1,49 @@
1
+ /** Structural subset of `@tupik/core` apps accepted by the Bun adapter. */
2
+ interface BunApp {
3
+ fetch(request: Request): Promise<Response>;
4
+ /** `@tupik/core` apps: like `fetch`, but returns the `Response` itself when nothing had to wait. */
5
+ handle?(request: Request): Response | Promise<Response>;
6
+ /** Registered routes (`@tupik/core` apps); their paths become `Bun.serve` `routes`. */
7
+ readonly routes?: readonly {
8
+ readonly path: string;
9
+ }[];
10
+ }
11
+ type BunHandler = (request: Request) => Promise<Response>;
12
+ /** Handler given to `Bun.serve` (`fetch` and every entry of `routes`). */
13
+ type BunRouteHandler = (request: Request) => Response | Promise<Response>;
14
+ interface BunServeOptions {
15
+ readonly port?: number;
16
+ readonly hostname?: string;
17
+ }
18
+ /** Minimal structural shape of `Bun.serve`. */
19
+ interface BunRuntime {
20
+ serve(options: {
21
+ fetch: BunRouteHandler;
22
+ routes?: Record<string, BunRouteHandler>;
23
+ } & BunServeOptions): BunServer;
24
+ }
25
+ interface BunServer {
26
+ stop(): void;
27
+ }
28
+
29
+ /** `Bun.serve`-compatible `fetch` handler: no conversion, straight to the app. */
30
+ declare function createBunHandler(app: BunApp): BunHandler;
31
+ declare function toBunPath(path: string): string | undefined;
32
+ /**
33
+ * `Bun.serve` `routes` for the app's registered paths, all pointing at the
34
+ * same handler. Bun's native router picks the entry and the app's pipeline
35
+ * (hooks, access, middleware, params decoding, 405/OPTIONS) still answers, so
36
+ * behavior is identical to `fetch`; paths Bun cannot express go to `fetch`.
37
+ */
38
+ declare function createBunRoutes(app: BunApp, handler: BunRouteHandler): Record<string, BunRouteHandler>;
39
+ /**
40
+ * Serve a Tupik app with `Bun.serve`: registered routes go through Bun's
41
+ * native router (`routes`), anything else through `fetch`. Uses `app.handle`
42
+ * (synchronous when it can) when the app has it. Runtime injectable for tests.
43
+ */
44
+ declare function serveBun(app: BunApp, options?: BunServeOptions & {
45
+ readonly bun?: BunRuntime;
46
+ }): BunServer;
47
+
48
+ export { createBunHandler, createBunRoutes, serveBun, toBunPath };
49
+ export type { BunApp, BunHandler, BunRouteHandler, BunRuntime, BunServeOptions, BunServer };
@@ -0,0 +1,49 @@
1
+ /** Structural subset of `@tupik/core` apps accepted by the Bun adapter. */
2
+ interface BunApp {
3
+ fetch(request: Request): Promise<Response>;
4
+ /** `@tupik/core` apps: like `fetch`, but returns the `Response` itself when nothing had to wait. */
5
+ handle?(request: Request): Response | Promise<Response>;
6
+ /** Registered routes (`@tupik/core` apps); their paths become `Bun.serve` `routes`. */
7
+ readonly routes?: readonly {
8
+ readonly path: string;
9
+ }[];
10
+ }
11
+ type BunHandler = (request: Request) => Promise<Response>;
12
+ /** Handler given to `Bun.serve` (`fetch` and every entry of `routes`). */
13
+ type BunRouteHandler = (request: Request) => Response | Promise<Response>;
14
+ interface BunServeOptions {
15
+ readonly port?: number;
16
+ readonly hostname?: string;
17
+ }
18
+ /** Minimal structural shape of `Bun.serve`. */
19
+ interface BunRuntime {
20
+ serve(options: {
21
+ fetch: BunRouteHandler;
22
+ routes?: Record<string, BunRouteHandler>;
23
+ } & BunServeOptions): BunServer;
24
+ }
25
+ interface BunServer {
26
+ stop(): void;
27
+ }
28
+
29
+ /** `Bun.serve`-compatible `fetch` handler: no conversion, straight to the app. */
30
+ declare function createBunHandler(app: BunApp): BunHandler;
31
+ declare function toBunPath(path: string): string | undefined;
32
+ /**
33
+ * `Bun.serve` `routes` for the app's registered paths, all pointing at the
34
+ * same handler. Bun's native router picks the entry and the app's pipeline
35
+ * (hooks, access, middleware, params decoding, 405/OPTIONS) still answers, so
36
+ * behavior is identical to `fetch`; paths Bun cannot express go to `fetch`.
37
+ */
38
+ declare function createBunRoutes(app: BunApp, handler: BunRouteHandler): Record<string, BunRouteHandler>;
39
+ /**
40
+ * Serve a Tupik app with `Bun.serve`: registered routes go through Bun's
41
+ * native router (`routes`), anything else through `fetch`. Uses `app.handle`
42
+ * (synchronous when it can) when the app has it. Runtime injectable for tests.
43
+ */
44
+ declare function serveBun(app: BunApp, options?: BunServeOptions & {
45
+ readonly bun?: BunRuntime;
46
+ }): BunServer;
47
+
48
+ export { createBunHandler, createBunRoutes, serveBun, toBunPath };
49
+ export type { BunApp, BunHandler, BunRouteHandler, BunRuntime, BunServeOptions, BunServer };
package/dist/index.mjs ADDED
@@ -0,0 +1,32 @@
1
+ function createBunHandler(app) {
2
+ return (request) => app.fetch(request);
3
+ }
4
+ const BUN_PATH = /^\/[A-Za-z0-9\-._~/:*]*$/;
5
+ function toBunPath(path) {
6
+ if (!BUN_PATH.test(path)) return void 0;
7
+ return path.replace(/\/\*[A-Za-z0-9_]*$/, "/*");
8
+ }
9
+ function createBunRoutes(app, handler) {
10
+ const routes = {};
11
+ for (const route of app.routes ?? []) {
12
+ const path = toBunPath(route.path);
13
+ if (path !== void 0) routes[path] = handler;
14
+ }
15
+ return routes;
16
+ }
17
+ function serveBun(app, options = {}) {
18
+ const bun = options.bun ?? globalThis.Bun;
19
+ if (bun === void 0) {
20
+ throw new Error("Bun runtime not found");
21
+ }
22
+ const handler = app.handle === void 0 ? createBunHandler(app) : app.handle.bind(app);
23
+ const routes = createBunRoutes(app, handler);
24
+ return bun.serve({
25
+ fetch: handler,
26
+ ...Object.keys(routes).length > 0 ? { routes } : {},
27
+ ...options.port !== void 0 ? { port: options.port } : {},
28
+ ...options.hostname !== void 0 ? { hostname: options.hostname } : {}
29
+ });
30
+ }
31
+
32
+ export { createBunHandler, createBunRoutes, serveBun, toBunPath };
package/package.json CHANGED
@@ -1,6 +1,52 @@
1
1
  {
2
2
  "name": "@tupik/bun",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "1.0.0",
4
+ "description": "Bun adapter for Tupik: serve a Tupik app with Bun.serve.",
5
+ "license": "MIT",
6
+ "keywords": [
7
+ "tupik",
8
+ "typescript",
9
+ "web-framework",
10
+ "bun",
11
+ "http",
12
+ "server"
13
+ ],
14
+ "homepage": "https://github.com/CodeHamper/tupikjs/tree/main/packages/bun#readme",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/CodeHamper/tupikjs.git",
18
+ "directory": "packages/bun"
19
+ },
20
+ "bugs": {
21
+ "url": "https://github.com/CodeHamper/tupikjs/issues"
22
+ },
23
+ "type": "module",
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "files": [
28
+ "dist"
29
+ ],
30
+ "exports": {
31
+ ".": {
32
+ "types": "./dist/index.d.ts",
33
+ "import": "./dist/index.mjs"
34
+ }
35
+ },
36
+ "engines": {
37
+ "node": ">=22.12"
38
+ },
39
+ "dependencies": {
40
+ "@tupik/core": "^1.0.0"
41
+ },
42
+ "devDependencies": {
43
+ "typescript": "^5.6.0",
44
+ "unbuild": "^3.0.0",
45
+ "vitest": "^2.1.0"
46
+ },
47
+ "scripts": {
48
+ "build": "unbuild",
49
+ "typecheck": "tsc --noEmit",
50
+ "test": "vitest run"
51
+ }
6
52
  }