@backtickjs/hono 0.0.0-stage → 0.2.5
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 +21 -0
- package/README.md +29 -2
- package/dist/index.d.ts +19 -0
- package/dist/index.js +39 -0
- package/dist/index.js.map +1 -0
- package/package.json +56 -3
- package/src/index.ts +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jean-François Bisson
|
|
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,30 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @backtickjs/hono
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[Hono](https://hono.dev) routes as [Backtick](https://backtickjs.com) screens: under `backtick()`, `context.render(<Home />)` answers a request with your server components, bundled for the modules the client that asked provides, as Hono's own `jsxRenderer()` answers with HTML. Each route is a screen, and a path on the client is the same path on the server. Hono is a peer dependency: this package is built against Hono 4.13.13, and works with any later 4.x.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install hono @backtickjs/hono @backtickjs/core @backtickjs/bundler
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
The [repository's README](https://github.com/backtickjs/backtick#packages) says what each package is.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import { backtick } from "@backtickjs/hono";
|
|
17
|
+
import { Hono } from "hono";
|
|
18
|
+
import { Home } from "./Home.js";
|
|
19
|
+
|
|
20
|
+
const app = new Hono();
|
|
21
|
+
|
|
22
|
+
app.use(backtick());
|
|
23
|
+
app.get("/", (context) => context.render(<Home />));
|
|
24
|
+
|
|
25
|
+
export default app;
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The client sends its modules, each at its package's version, in `x-backtick-modules` (in an Expo app, `modulesOf` from `@backtickjs/core` writes them). The answer is the screen's bundle as CommonJS, `text/javascript`, for the client to run with `execRoot`.
|
|
29
|
+
|
|
30
|
+
A client without a module the screen needs gets a `426`, and any other refusal of the bundler's a `400`, each with `{ name, message }` as JSON: the client can show why. An error your server components throw passes through to Hono.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Spliceable, SplicesAs } from "@backtickjs/core";
|
|
2
|
+
import type { MiddlewareHandler } from "hono";
|
|
3
|
+
declare module "hono" {
|
|
4
|
+
interface ContextRenderer {
|
|
5
|
+
<T extends [T] extends [SplicesAs<T>] ? unknown : Spliceable>(input: T): Promise<Response>;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Hono's renderer for Backtick screens: under it, `context.render(input)`
|
|
10
|
+
* answers with `input` bundled for the modules the client sent in
|
|
11
|
+
* `x-backtick-modules`, so each route is a screen.
|
|
12
|
+
*
|
|
13
|
+
* app.use(backtick());
|
|
14
|
+
* app.get("/", (context) => context.render(<Home />));
|
|
15
|
+
*
|
|
16
|
+
* A client without a module the screen needs gets a 426, and any other
|
|
17
|
+
* refusal of the bundler's a 400, each with `{ name, message }`.
|
|
18
|
+
*/
|
|
19
|
+
export declare function backtick(): MiddlewareHandler;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { bundler, BundlerError } from "@backtickjs/bundler";
|
|
2
|
+
/**
|
|
3
|
+
* Hono's renderer for Backtick screens: under it, `context.render(input)`
|
|
4
|
+
* answers with `input` bundled for the modules the client sent in
|
|
5
|
+
* `x-backtick-modules`, so each route is a screen.
|
|
6
|
+
*
|
|
7
|
+
* app.use(backtick());
|
|
8
|
+
* app.get("/", (context) => context.render(<Home />));
|
|
9
|
+
*
|
|
10
|
+
* A client without a module the screen needs gets a 426, and any other
|
|
11
|
+
* refusal of the bundler's a 400, each with `{ name, message }`.
|
|
12
|
+
*/
|
|
13
|
+
export function backtick() {
|
|
14
|
+
return async (context, next) => {
|
|
15
|
+
context.setRenderer(async (input) => {
|
|
16
|
+
let modules;
|
|
17
|
+
try {
|
|
18
|
+
modules = JSON.parse(context.req.header("x-backtick-modules") ?? "{}");
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
const { name, message } = error;
|
|
22
|
+
return context.json({ name, message: `x-backtick-modules isn't JSON: ${message}` }, 400);
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
const build = await bundler.build({ input, modules });
|
|
26
|
+
const { code } = build.generate({ format: "cjs" });
|
|
27
|
+
return context.body(code, 200, { "content-type": "text/javascript" });
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
if (error instanceof BundlerError) {
|
|
31
|
+
return context.json({ name: error.name, message: error.message }, error.name === "ClientVersionMismatch" ? 426 : 400);
|
|
32
|
+
}
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
await next();
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,YAAY,EAAgB,MAAM,qBAAqB,CAAC;AAc1E;;;;;;;;;;GAUG;AACH,MAAM,UAAU,QAAQ;IACtB,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE;QAC7B,OAAO,CAAC,WAAW,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE;YAClC,IAAI,OAAgB,CAAC;YACrB,IAAI,CAAC;gBACH,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,oBAAoB,CAAC,IAAI,IAAI,CAAC,CAAC;YACzE,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,KAAoB,CAAC;gBAC/C,OAAO,OAAO,CAAC,IAAI,CACjB,EAAE,IAAI,EAAE,OAAO,EAAE,kCAAkC,OAAO,EAAE,EAAE,EAC9D,GAAG,CACJ,CAAC;YACJ,CAAC;YACD,IAAI,CAAC;gBACH,MAAM,KAAK,GAAG,MAAM,OAAO,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC;gBACtD,MAAM,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;gBACnD,OAAO,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,EAAE,cAAc,EAAE,iBAAiB,EAAE,CAAC,CAAC;YACxE,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,IAAI,KAAK,YAAY,YAAY,EAAE,CAAC;oBAClC,OAAO,OAAO,CAAC,IAAI,CACjB,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,EAC5C,KAAK,CAAC,IAAI,KAAK,uBAAuB,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CACnD,CAAC;gBACJ,CAAC;gBACD,MAAM,KAAK,CAAC;YACd,CAAC;QACH,CAAC,CAAC,CAAC;QACH,MAAM,IAAI,EAAE,CAAC;IACf,CAAC,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,59 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backtickjs/hono",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.5",
|
|
4
|
+
"description": "Hono routes as Backtick screens: a route's answer is its server components, bundled for the client that asked",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"backtick",
|
|
7
|
+
"hono",
|
|
8
|
+
"react-native",
|
|
9
|
+
"server-driven-ui",
|
|
10
|
+
"server-components"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://backtickjs.com",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/backtickjs/backtick/issues"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/backtickjs/backtick.git",
|
|
19
|
+
"directory": "packages/hono"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"author": "Jean-François Bisson <jf@backtickjs.com>",
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "./dist/index.js",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist",
|
|
33
|
+
"!dist/._cache",
|
|
34
|
+
"src"
|
|
35
|
+
],
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@backtickjs/bundler": "^0.2.5",
|
|
41
|
+
"@backtickjs/core": "^0.2.5"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"hono": "^4.13.13"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"hono": "^4.13.13",
|
|
48
|
+
"prettier": "^3.6.2",
|
|
49
|
+
"typescript": "^6.0.3"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "tsc -b",
|
|
53
|
+
"dev": "tsc -b --watch",
|
|
54
|
+
"format": "prettier --write --ignore-unknown .",
|
|
55
|
+
"format:check": "prettier --check --ignore-unknown .",
|
|
56
|
+
"test": "node --test \"test/**/*.test.ts\"",
|
|
57
|
+
"clean": "rm -rf node_modules dist .turbo"
|
|
58
|
+
}
|
|
6
59
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { bundler, BundlerError, type Modules } from "@backtickjs/bundler";
|
|
2
|
+
import type { Spliceable, SplicesAs } from "@backtickjs/core";
|
|
3
|
+
import type { MiddlewareHandler } from "hono";
|
|
4
|
+
|
|
5
|
+
declare module "hono" {
|
|
6
|
+
// What `context.render` takes under `backtick()`, as
|
|
7
|
+
// `jsxRenderer()` types its own.
|
|
8
|
+
interface ContextRenderer {
|
|
9
|
+
<T extends [T] extends [SplicesAs<T>] ? unknown : Spliceable>(
|
|
10
|
+
input: T,
|
|
11
|
+
): Promise<Response>;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Hono's renderer for Backtick screens: under it, `context.render(input)`
|
|
17
|
+
* answers with `input` bundled for the modules the client sent in
|
|
18
|
+
* `x-backtick-modules`, so each route is a screen.
|
|
19
|
+
*
|
|
20
|
+
* app.use(backtick());
|
|
21
|
+
* app.get("/", (context) => context.render(<Home />));
|
|
22
|
+
*
|
|
23
|
+
* A client without a module the screen needs gets a 426, and any other
|
|
24
|
+
* refusal of the bundler's a 400, each with `{ name, message }`.
|
|
25
|
+
*/
|
|
26
|
+
export function backtick(): MiddlewareHandler {
|
|
27
|
+
return async (context, next) => {
|
|
28
|
+
context.setRenderer(async (input) => {
|
|
29
|
+
let modules: Modules;
|
|
30
|
+
try {
|
|
31
|
+
modules = JSON.parse(context.req.header("x-backtick-modules") ?? "{}");
|
|
32
|
+
} catch (error) {
|
|
33
|
+
const { name, message } = error as SyntaxError;
|
|
34
|
+
return context.json(
|
|
35
|
+
{ name, message: `x-backtick-modules isn't JSON: ${message}` },
|
|
36
|
+
400,
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
const build = await bundler.build({ input, modules });
|
|
41
|
+
const { code } = build.generate({ format: "cjs" });
|
|
42
|
+
return context.body(code, 200, { "content-type": "text/javascript" });
|
|
43
|
+
} catch (error) {
|
|
44
|
+
if (error instanceof BundlerError) {
|
|
45
|
+
return context.json(
|
|
46
|
+
{ name: error.name, message: error.message },
|
|
47
|
+
error.name === "ClientVersionMismatch" ? 426 : 400,
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
await next();
|
|
54
|
+
};
|
|
55
|
+
}
|