@tupik/hono 0.0.0-stage → 1.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) 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,82 @@
1
- # Temporary Holding Version
1
+ # @tupik/hono
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
+ Mount a **Tupik** app inside a [Hono](https://hono.dev) app and move an API to Tupik one route at a time.
4
+ The Hono request (`c.req.raw`), bindings (`c.env`) and execution context (`c.executionCtx`, for `waitUntil`)
5
+ reach the Tupik app as `ctx.req` and `ctx.runtime`; its `Response` (headers and streams included) goes back to Hono as is.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install @tupik/hono @tupik/core hono
11
+ ```
12
+
13
+ `hono` (`^4.10.0`) is a peer dependency: the adapter uses the one your app already has.
14
+
15
+ ## Usage
16
+
17
+ ```ts
18
+ import { Hono } from 'hono';
19
+ import { createApp, createModule } from '@tupik/core';
20
+ import { toHono, tupikHandler } from '@tupik/hono';
21
+
22
+ const v2 = createApp().register(
23
+ createModule().access('public').get('/users/:id', { handler: (ctx) => ({ id: ctx.params.id }) }),
24
+ );
25
+
26
+ const hono = new Hono();
27
+ hono.route('/v2', toHono(v2)); // GET /v2/users/7: Tupik sees /users/7
28
+
29
+ // Same thing with a pattern: pass the prefix to remove.
30
+ hono.all('/v3/*', tupikHandler(v2, { basePath: '/v3' }));
31
+
32
+ export default hono; // Cloudflare Workers, Bun or @hono/node-server
33
+ ```
34
+
35
+ ### Falling back to Hono
36
+
37
+ With `fallthrough: true`, paths the Tupik app has no route for go to the next Hono handler, so the old Hono route
38
+ keeps answering until its Tupik version exists:
39
+
40
+ ```ts
41
+ hono.route('/api', toHono(tupikApp, { fallthrough: true })); // Tupik first
42
+ hono.get('/api/reports', legacyReports); // still Hono
43
+ ```
44
+
45
+ Only unmatched paths fall through. A 404 from a Tupik handler or from `access.owner` is returned as is.
46
+ `fallthrough` registers the app's `notFound` handler, so do not set your own. A path that exists in Tupik with another
47
+ method answers 405: declare that Hono route **before** the mount, or move both methods together.
48
+
49
+ ### Reusing the Hono user
50
+
51
+ `honoAuthenticate` turns what a Hono middleware already resolved into the Tupik principal, so `access` works without a
52
+ second login:
53
+
54
+ ```ts
55
+ import type { Context } from 'hono';
56
+ import { honoAuthenticate } from '@tupik/hono';
57
+
58
+ type AppEnv = { Variables: { user: User | undefined } };
59
+
60
+ const v2 = createApp({ authenticate: honoAuthenticate((c: Context<AppEnv>) => c.get('user')) });
61
+ ```
62
+
63
+ A request that did not come through `toHono`/`tupikHandler` is anonymous.
64
+
65
+ ## Key API
66
+
67
+ | Export | Purpose |
68
+ | --- | --- |
69
+ | `toHono(app, { fallthrough? })` | Hono sub-app for `hono.route(prefix, ...)`; the prefix is removed |
70
+ | `tupikHandler(app, { basePath?, fallthrough? })` | Hono handler for `hono.all(pattern, ...)` or `hono.use(...)` |
71
+ | `honoAuthenticate(read)` | `authenticate` for `createApp` that reads the principal from the Hono context |
72
+
73
+ ## Notes
74
+
75
+ - A Hono middleware that already read the body (`c.req.json()`, a validator) leaves `c.req.raw` consumed. Validate in
76
+ Tupik instead; that is the point of migrating the route.
77
+ - Hono middleware declared before the mount (CORS, logger, auth) runs for Tupik routes too; headers it sets after
78
+ `await next()` land on the Tupik response.
79
+
80
+ ## License
81
+
82
+ MIT
@@ -0,0 +1,58 @@
1
+ import { Env, Context, Hono, MiddlewareHandler } from 'hono';
2
+ import { ExecutionContextLike } from '@tupik/core';
3
+
4
+ /**
5
+ * Structural subset of a `@tupik/core` app accepted by the Hono adapter.
6
+ * `notFound` is used only with `fallthrough`.
7
+ */
8
+ interface MountableApp {
9
+ fetch(request: Request, env?: unknown, executionCtx?: ExecutionContextLike): Promise<Response>;
10
+ notFound(handler: (ctx: {
11
+ readonly req: object;
12
+ }) => Response): unknown;
13
+ }
14
+ interface TupikHandlerOptions {
15
+ /**
16
+ * Prefix removed from the path before the Tupik app sees it. Default: the
17
+ * prefix of `hono.route('/prefix', ...)` (with its params filled in), or
18
+ * none. Pass it when mounting with a pattern: `hono.all('/v2/*', tupikHandler(app, { basePath: '/v2' }))`.
19
+ */
20
+ basePath?: string;
21
+ /**
22
+ * Paths the Tupik app has no route for go to the next Hono handler instead
23
+ * of a Tupik 404. Registers the app's `notFound` handler, so do not set your
24
+ * own. Only unmatched paths fall through: a 404 from a handler or from
25
+ * `access.owner` is returned as is. Default `false`.
26
+ */
27
+ fallthrough?: boolean;
28
+ }
29
+ /** Options of `toHono`: the prefix comes from `hono.route`. */
30
+ type ToHonoOptions = Omit<TupikHandlerOptions, 'basePath'>;
31
+
32
+ /**
33
+ * Hono handler that serves requests with a Tupik app: `c.req.raw`, `c.env`
34
+ * and `c.executionCtx` (`waitUntil`) reach the app, whose `Response` (headers
35
+ * and streams included) goes back to Hono untouched.
36
+ *
37
+ * ```ts
38
+ * hono.all('/v2/*', tupikHandler(app, { basePath: '/v2' }));
39
+ * ```
40
+ */
41
+ declare function tupikHandler<E extends Env = Env>(app: MountableApp, options?: TupikHandlerOptions): MiddlewareHandler<E>;
42
+ /**
43
+ * Hono sub-app that serves every path with a Tupik app, for
44
+ * `hono.route('/v2', toHono(app))`. The `/v2` prefix is removed, so the Tupik
45
+ * routes are declared without it.
46
+ */
47
+ declare function toHono<E extends Env = Env>(app: MountableApp, options?: ToHonoOptions): Hono<E>;
48
+ /**
49
+ * `authenticate` for `createApp` that reads the principal Hono already
50
+ * resolved: `createApp({ authenticate: honoAuthenticate((c) => c.get('user')) })`.
51
+ * Requests that did not come through `tupikHandler`/`toHono` are anonymous.
52
+ */
53
+ declare function honoAuthenticate<TPrincipal, E extends Env = Env>(read: (c: Context<E>) => TPrincipal | null | undefined | Promise<TPrincipal | null | undefined>): (ctx: {
54
+ readonly req: object;
55
+ }) => TPrincipal | null | undefined | Promise<TPrincipal | null | undefined>;
56
+
57
+ export { honoAuthenticate, toHono, tupikHandler };
58
+ export type { MountableApp, ToHonoOptions, TupikHandlerOptions };
@@ -0,0 +1,58 @@
1
+ import { Env, Context, Hono, MiddlewareHandler } from 'hono';
2
+ import { ExecutionContextLike } from '@tupik/core';
3
+
4
+ /**
5
+ * Structural subset of a `@tupik/core` app accepted by the Hono adapter.
6
+ * `notFound` is used only with `fallthrough`.
7
+ */
8
+ interface MountableApp {
9
+ fetch(request: Request, env?: unknown, executionCtx?: ExecutionContextLike): Promise<Response>;
10
+ notFound(handler: (ctx: {
11
+ readonly req: object;
12
+ }) => Response): unknown;
13
+ }
14
+ interface TupikHandlerOptions {
15
+ /**
16
+ * Prefix removed from the path before the Tupik app sees it. Default: the
17
+ * prefix of `hono.route('/prefix', ...)` (with its params filled in), or
18
+ * none. Pass it when mounting with a pattern: `hono.all('/v2/*', tupikHandler(app, { basePath: '/v2' }))`.
19
+ */
20
+ basePath?: string;
21
+ /**
22
+ * Paths the Tupik app has no route for go to the next Hono handler instead
23
+ * of a Tupik 404. Registers the app's `notFound` handler, so do not set your
24
+ * own. Only unmatched paths fall through: a 404 from a handler or from
25
+ * `access.owner` is returned as is. Default `false`.
26
+ */
27
+ fallthrough?: boolean;
28
+ }
29
+ /** Options of `toHono`: the prefix comes from `hono.route`. */
30
+ type ToHonoOptions = Omit<TupikHandlerOptions, 'basePath'>;
31
+
32
+ /**
33
+ * Hono handler that serves requests with a Tupik app: `c.req.raw`, `c.env`
34
+ * and `c.executionCtx` (`waitUntil`) reach the app, whose `Response` (headers
35
+ * and streams included) goes back to Hono untouched.
36
+ *
37
+ * ```ts
38
+ * hono.all('/v2/*', tupikHandler(app, { basePath: '/v2' }));
39
+ * ```
40
+ */
41
+ declare function tupikHandler<E extends Env = Env>(app: MountableApp, options?: TupikHandlerOptions): MiddlewareHandler<E>;
42
+ /**
43
+ * Hono sub-app that serves every path with a Tupik app, for
44
+ * `hono.route('/v2', toHono(app))`. The `/v2` prefix is removed, so the Tupik
45
+ * routes are declared without it.
46
+ */
47
+ declare function toHono<E extends Env = Env>(app: MountableApp, options?: ToHonoOptions): Hono<E>;
48
+ /**
49
+ * `authenticate` for `createApp` that reads the principal Hono already
50
+ * resolved: `createApp({ authenticate: honoAuthenticate((c) => c.get('user')) })`.
51
+ * Requests that did not come through `tupikHandler`/`toHono` are anonymous.
52
+ */
53
+ declare function honoAuthenticate<TPrincipal, E extends Env = Env>(read: (c: Context<E>) => TPrincipal | null | undefined | Promise<TPrincipal | null | undefined>): (ctx: {
54
+ readonly req: object;
55
+ }) => TPrincipal | null | undefined | Promise<TPrincipal | null | undefined>;
56
+
57
+ export { honoAuthenticate, toHono, tupikHandler };
58
+ export type { MountableApp, ToHonoOptions, TupikHandlerOptions };
package/dist/index.mjs ADDED
@@ -0,0 +1,52 @@
1
+ import { Hono } from 'hono';
2
+ import { basePath } from 'hono/route';
3
+
4
+ const honoContexts = /* @__PURE__ */ new WeakMap();
5
+ const unmatched = /* @__PURE__ */ new WeakSet();
6
+ function markUnmatched(ctx) {
7
+ unmatched.add(ctx.req);
8
+ return Response.json({ code: "NOT_FOUND", message: "Route not found" }, { status: 404 });
9
+ }
10
+ function executionContextOf(c) {
11
+ try {
12
+ return c.executionCtx;
13
+ } catch {
14
+ return void 0;
15
+ }
16
+ }
17
+ function stripPrefix(raw, prefix) {
18
+ const base = prefix.endsWith("/") ? prefix.slice(0, -1) : prefix;
19
+ if (base === "") return raw;
20
+ const url = new URL(raw.url);
21
+ const { pathname } = url;
22
+ if (!pathname.startsWith(base)) return raw;
23
+ const rest = pathname.slice(base.length);
24
+ if (rest !== "" && !rest.startsWith("/")) return raw;
25
+ url.pathname = rest === "" ? "/" : rest;
26
+ return new Request(url, raw);
27
+ }
28
+ function tupikHandler(app, options = {}) {
29
+ const { basePath: basePath$1, fallthrough = false } = options;
30
+ if (fallthrough) app.notFound(markUnmatched);
31
+ return async (c, next) => {
32
+ const request = stripPrefix(c.req.raw, basePath$1 ?? basePath(c));
33
+ honoContexts.set(request, c);
34
+ const response = await app.fetch(request, c.env, executionContextOf(c));
35
+ if (unmatched.has(request)) {
36
+ await next();
37
+ return;
38
+ }
39
+ return response;
40
+ };
41
+ }
42
+ function toHono(app, options = {}) {
43
+ return new Hono().all("/*", tupikHandler(app, options));
44
+ }
45
+ function honoAuthenticate(read) {
46
+ return (ctx) => {
47
+ const c = honoContexts.get(ctx.req);
48
+ return c === void 0 ? null : read(c);
49
+ };
50
+ }
51
+
52
+ export { honoAuthenticate, toHono, tupikHandler };
package/package.json CHANGED
@@ -1,6 +1,57 @@
1
1
  {
2
2
  "name": "@tupik/hono",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "1.1.0",
4
+ "description": "Mount a Tupik app inside a Hono app and migrate one route at a time.",
5
+ "license": "MIT",
6
+ "keywords": [
7
+ "tupik",
8
+ "typescript",
9
+ "web-framework",
10
+ "hono",
11
+ "migration",
12
+ "cloudflare-workers"
13
+ ],
14
+ "homepage": "https://github.com/CodeHamper/tupikjs/tree/main/packages/hono#readme",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/CodeHamper/tupikjs.git",
18
+ "directory": "packages/hono"
19
+ },
20
+ "bugs": {
21
+ "url": "https://github.com/CodeHamper/tupikjs/issues"
22
+ },
23
+ "type": "module",
24
+ "sideEffects": false,
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "files": [
29
+ "dist"
30
+ ],
31
+ "exports": {
32
+ ".": {
33
+ "types": "./dist/index.d.ts",
34
+ "import": "./dist/index.mjs"
35
+ }
36
+ },
37
+ "engines": {
38
+ "node": ">=22.12"
39
+ },
40
+ "dependencies": {
41
+ "@tupik/core": "^1.1.0"
42
+ },
43
+ "peerDependencies": {
44
+ "hono": "^4.10.0"
45
+ },
46
+ "devDependencies": {
47
+ "hono": "^4.13.23",
48
+ "typescript": "^5.6.0",
49
+ "unbuild": "^3.0.0",
50
+ "vitest": "^2.1.0"
51
+ },
52
+ "scripts": {
53
+ "build": "unbuild",
54
+ "typecheck": "tsc --noEmit",
55
+ "test": "vitest run"
56
+ }
6
57
  }