@sendproof/hono 0.1.0-alpha.1 → 0.1.0-alpha.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/index.d.ts +4 -0
- package/dist/index.js +22 -8
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -4,10 +4,14 @@ import type { BenchOptions, UnavailableOptions } from '@sendproof/core';
|
|
|
4
4
|
export type SendproofOptions<E extends Env = Env> = BenchOptions<Context<E>> & {
|
|
5
5
|
/** Path on the parent app where this router is mounted, without a trailing slash. */
|
|
6
6
|
basePath?: string;
|
|
7
|
+
/** Parent application, if session GETs must reach routes registered outside this router. */
|
|
8
|
+
application?: Hono<E>;
|
|
7
9
|
};
|
|
8
10
|
/** A Hono route handler or middleware, as accepted by `app.get(path, …)`. */
|
|
9
11
|
export type OperationHandler<E extends Env = Env> = Handler<E> | MiddlewareHandler<E>;
|
|
10
12
|
export type SendproofRouter<E extends Env = Env> = Hono<E> & {
|
|
13
|
+
/** Roll back all sessions as the HTTP host shuts down. */
|
|
14
|
+
close(): Promise<void>;
|
|
11
15
|
/** Register the same Hono handlers for Send and rollback Preview. */
|
|
12
16
|
operation(method: string, template: string, ...handlers: OperationHandler<E>[]): SendproofRouter<E>;
|
|
13
17
|
/**
|
package/dist/index.js
CHANGED
|
@@ -22,16 +22,23 @@ function describe(c, basePath) {
|
|
|
22
22
|
* `app.route(basePath || '/', router)`.
|
|
23
23
|
*/
|
|
24
24
|
export function sendproof(options = {}) {
|
|
25
|
-
const { basePath = '', ...benchOptions } = options;
|
|
25
|
+
const { basePath = '', application, ...benchOptions } = options;
|
|
26
26
|
assertBasePath(basePath);
|
|
27
27
|
const app = new Hono({ strict: true });
|
|
28
28
|
// Preview dispatches into this second router so a handler runs without the workbench gate in front of it.
|
|
29
29
|
const internal = new Hono({ strict: true });
|
|
30
30
|
const operations = [];
|
|
31
31
|
const bench = createBench(benchOptions, () => operations, { language: 'node', framework: 'Hono' });
|
|
32
|
+
const sessionReads = new WeakSet();
|
|
33
|
+
app.close = bench.close;
|
|
32
34
|
app.use('*', async (c, next) => {
|
|
35
|
+
if (sessionReads.has(c.req.raw))
|
|
36
|
+
return next();
|
|
33
37
|
const request = describe(c, basePath);
|
|
34
|
-
const answer = await bench.lookup(request)
|
|
38
|
+
const answer = await bench.lookup(request, async () => {
|
|
39
|
+
const input = await captureResponse(new Response(c.req.raw.body), 1024);
|
|
40
|
+
return input.exceeded ? null : JSON.parse(input.body.toString());
|
|
41
|
+
});
|
|
35
42
|
// Attribute the request's store writes to it on runtimes without Node's HTTP server, whose
|
|
36
43
|
// requests the core attributes itself.
|
|
37
44
|
if (!answer)
|
|
@@ -39,12 +46,16 @@ export function sendproof(options = {}) {
|
|
|
39
46
|
if (!('target' in answer))
|
|
40
47
|
return toResponse(answer, c.req.method);
|
|
41
48
|
const rendered = await bench.preview(request, async () => {
|
|
42
|
-
const
|
|
43
|
-
const
|
|
49
|
+
const sessionRead = c.req.method === 'GET' && Boolean(answer.session);
|
|
50
|
+
const target = new URL((sessionRead && application ? basePath : '') + answer.target, request.origin);
|
|
51
|
+
const input = new Request(target, c.req.raw);
|
|
52
|
+
if (sessionRead)
|
|
53
|
+
sessionReads.add(input);
|
|
54
|
+
const response = await (sessionRead ? application ?? app : internal).fetch(input, c.env);
|
|
44
55
|
if (response.status === 404 && response.headers.get(unmatchedHeader) === '1')
|
|
45
56
|
return { unmatched: true };
|
|
46
57
|
return captureResponse(response, bench.maxResponseBytes);
|
|
47
|
-
});
|
|
58
|
+
}, answer.session);
|
|
48
59
|
return toResponse(rendered, c.req.method);
|
|
49
60
|
});
|
|
50
61
|
internal.notFound(() => new Response(null, { status: 404, headers: { [unmatchedHeader]: '1' } }));
|
|
@@ -63,9 +74,12 @@ export function sendproof(options = {}) {
|
|
|
63
74
|
app.unavailable = (method, template, options, ...handlers) => {
|
|
64
75
|
const upper = register(method, template, handlers);
|
|
65
76
|
const entry = unavailableOperation(upper, template, options);
|
|
66
|
-
// Only the public router gets the handlers, so a Preview dispatch finds no operation.
|
|
67
77
|
const [first, ...rest] = handlers;
|
|
68
|
-
app.on(upper, routePath(template),
|
|
78
|
+
app.on(upper, routePath(template), async (c, next) => {
|
|
79
|
+
if (sessionReads.has(c.req.raw))
|
|
80
|
+
return new Response(null, { status: 404, headers: { [unmatchedHeader]: '1' } });
|
|
81
|
+
await next();
|
|
82
|
+
}, first, ...rest);
|
|
69
83
|
operations.push({ ...entry, preview: false });
|
|
70
84
|
return app;
|
|
71
85
|
};
|
|
@@ -74,7 +88,7 @@ export function sendproof(options = {}) {
|
|
|
74
88
|
const path = routePath(template);
|
|
75
89
|
// The observation wrapper times and captures a real Send when the client asked for a trace.
|
|
76
90
|
const observe = async (c, next) => {
|
|
77
|
-
if (c.req.method !== upper)
|
|
91
|
+
if (c.req.method !== upper || sessionReads.has(c.req.raw))
|
|
78
92
|
return next();
|
|
79
93
|
const observed = await bench.observation(describe(c, basePath));
|
|
80
94
|
if (!observed)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sendproof/hono",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.2",
|
|
4
4
|
"description": "Sendproof integration for Hono",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"node": ">=22.14"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@sendproof/core": "0.1.0-alpha.
|
|
28
|
+
"@sendproof/core": "0.1.0-alpha.2"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
31
|
"hono": "^4.0.0"
|