@sigmx/hono 0.1.1 → 0.2.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/README.md +2 -2
- package/dist/index.js +7 -5
- package/dist/node.d.ts +9 -1
- package/dist/node.js +6 -4
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ import { serveClient } from '@sigmx/hono/node';
|
|
|
13
13
|
|
|
14
14
|
const app = new Hono();
|
|
15
15
|
app.use(sigmx());
|
|
16
|
-
app.get('/sigmx.js', serveClient()); // the script-tag build from node_modules;
|
|
16
|
+
app.get('/sigmx.js', serveClient()); // the script-tag build from node_modules; or serveClient({ path }) for your own bundle
|
|
17
17
|
|
|
18
18
|
app.get('/api/towns', async (c) => {
|
|
19
19
|
const { q = '' } = await c.var.sigmx.signals();
|
|
@@ -56,7 +56,7 @@ If you define your own `app.onError`, return `error.getResponse()` for `HTTPExce
|
|
|
56
56
|
|
|
57
57
|
## Serving the client
|
|
58
58
|
|
|
59
|
-
`serveClient()` from `@sigmx/hono/node` reads `sigmx.standalone.js` from the installed `sigmx` package and serves it with an ETag and `cache-control`. It needs a filesystem, so it is a separate entry for Node, Bun and Deno; on Workers, bundle the client with your app instead. Pass `{ file, maxAge }` to serve another file from sigmx's `dist` or change the cache lifetime.
|
|
59
|
+
`serveClient()` from `@sigmx/hono/node` reads `sigmx.standalone.js` from the installed `sigmx` package and serves it with an ETag and `cache-control`. It needs a filesystem, so it is a separate entry for Node, Bun and Deno; on Workers, bundle the client with your app instead. Pass `{ file, maxAge }` to serve another file from sigmx's `dist` or change the cache lifetime. Pass `path` to serve a bundle you built yourself (for example with `sigmxAuto()` from `sigmx/vite`) with the same headers: `serveClient({ path: 'public/sigmx.js' })`.
|
|
60
60
|
|
|
61
61
|
## JSX
|
|
62
62
|
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createMiddleware } from 'hono/factory';
|
|
2
2
|
import { HTTPException } from 'hono/http-exception';
|
|
3
3
|
import { streamSSE } from 'hono/streaming';
|
|
4
|
-
import { executeScript, formatEvent, patchElements, patchSignals, removeElements, removeSignals, SIGNALS_KEY, SSE_HEADERS, validateSignals, } from 'sigmx/server';
|
|
4
|
+
import { executeScript, fieldsToObject, formatEvent, patchElements, patchSignals, removeElements, removeSignals, SIGNALS_KEY, SSE_HEADERS, validateSignals, } from 'sigmx/server';
|
|
5
5
|
export { EVENT_PATCH_ELEMENTS, EVENT_PATCH_SIGNALS, executeScript, formatEvent, patchElements, patchSignals, removeElements, removeSignals, SIGNALS_KEY, SSE_HEADERS, } from 'sigmx/server';
|
|
6
6
|
/**
|
|
7
7
|
* Thrown by `signals()` when the payload cannot be parsed (400) or fails validation (422). It is an
|
|
@@ -68,14 +68,16 @@ const readSignals = async (c, schema) => {
|
|
|
68
68
|
try {
|
|
69
69
|
if (method === 'GET' || method === 'DELETE') {
|
|
70
70
|
const q = c.req.query(SIGNALS_KEY);
|
|
71
|
-
raw = q
|
|
71
|
+
raw = q
|
|
72
|
+
? JSON.parse(q)
|
|
73
|
+
: fieldsToObject(Object.entries(c.req.queries()).map(([k, v]) => [k, v.length > 1 ? v : v[0]]));
|
|
72
74
|
}
|
|
73
75
|
else if (type.includes('application/json')) {
|
|
74
76
|
const text = await c.req.text();
|
|
75
77
|
raw = text ? JSON.parse(text) : {};
|
|
76
78
|
}
|
|
77
79
|
else if (type.includes('form')) {
|
|
78
|
-
raw = await c.req.parseBody();
|
|
80
|
+
raw = await c.req.parseBody({ all: true });
|
|
79
81
|
}
|
|
80
82
|
else {
|
|
81
83
|
raw = {};
|
|
@@ -84,6 +86,8 @@ const readSignals = async (c, schema) => {
|
|
|
84
86
|
catch (e) {
|
|
85
87
|
throw new SignalsError(`could not parse signals: ${e.message}`, [], 400);
|
|
86
88
|
}
|
|
89
|
+
if (typeof raw !== 'object' || raw === null || Array.isArray(raw))
|
|
90
|
+
throw new SignalsError('signals must be an object', [], 400);
|
|
87
91
|
try {
|
|
88
92
|
return await validateSignals(raw, schema);
|
|
89
93
|
}
|
|
@@ -100,8 +104,6 @@ const contextFor = (c) => ({
|
|
|
100
104
|
c.header('sigmx-selector', o.selector);
|
|
101
105
|
if (o.mode)
|
|
102
106
|
c.header('sigmx-mode', o.mode);
|
|
103
|
-
if (o.useViewTransition)
|
|
104
|
-
c.header('sigmx-use-view-transition', 'true');
|
|
105
107
|
return c.html(body, o.status ?? 200);
|
|
106
108
|
},
|
|
107
109
|
json: (signals, o = {}) => {
|
package/dist/node.d.ts
CHANGED
|
@@ -2,12 +2,20 @@ import type { Handler } from 'hono';
|
|
|
2
2
|
export interface ServeClientOptions {
|
|
3
3
|
/** File inside sigmx's `dist` to serve (default `sigmx.standalone.js`, every plugin, minified). */
|
|
4
4
|
file?: string;
|
|
5
|
+
/**
|
|
6
|
+
* Serve this file instead of one from sigmx's `dist`: a bundle you built yourself, for example with
|
|
7
|
+
* `sigmxAuto()` from `sigmx/vite` so that only the plugins your markup uses ship. Absolute, or
|
|
8
|
+
* relative to the working directory.
|
|
9
|
+
*/
|
|
10
|
+
path?: string;
|
|
5
11
|
/** `max-age` in seconds for the `cache-control` header (default one hour); an ETag handles the rest. */
|
|
6
12
|
maxAge?: number;
|
|
7
13
|
}
|
|
8
14
|
/**
|
|
9
|
-
* A route handler that serves the client
|
|
15
|
+
* A route handler that serves the client with an ETag: the script-tag build from `node_modules/sigmx`
|
|
16
|
+
* when no bundler is involved, or your own bundle via `path`:
|
|
10
17
|
*
|
|
11
18
|
* app.get('/sigmx.js', serveClient())
|
|
19
|
+
* app.get('/sigmx.js', serveClient({ path: 'public/sigmx.js' }))
|
|
12
20
|
*/
|
|
13
21
|
export declare const serveClient: (o?: ServeClientOptions) => Handler;
|
package/dist/node.js
CHANGED
|
@@ -3,17 +3,19 @@ import { readFile } from 'node:fs/promises';
|
|
|
3
3
|
import { createRequire } from 'node:module';
|
|
4
4
|
import { dirname, join } from 'node:path';
|
|
5
5
|
/**
|
|
6
|
-
* A route handler that serves the client
|
|
6
|
+
* A route handler that serves the client with an ETag: the script-tag build from `node_modules/sigmx`
|
|
7
|
+
* when no bundler is involved, or your own bundle via `path`:
|
|
7
8
|
*
|
|
8
9
|
* app.get('/sigmx.js', serveClient())
|
|
10
|
+
* app.get('/sigmx.js', serveClient({ path: 'public/sigmx.js' }))
|
|
9
11
|
*/
|
|
10
12
|
export const serveClient = (o = {}) => {
|
|
11
13
|
let cached;
|
|
12
14
|
const load = () => {
|
|
13
15
|
cached ??= (async () => {
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
const body = await readFile(
|
|
16
|
+
const file = o.path ??
|
|
17
|
+
join(dirname(createRequire(import.meta.url).resolve('sigmx/package.json')), 'dist', o.file ?? 'sigmx.standalone.js');
|
|
18
|
+
const body = await readFile(file, 'utf8');
|
|
17
19
|
const etag = `"${createHash('sha1').update(body).digest('base64url').slice(0, 16)}"`;
|
|
18
20
|
return { body, etag };
|
|
19
21
|
})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sigmx/hono",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Hono middleware for sigmx: read signals from the request, answer with HTML, JSON or a stream of patches, serve the client.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"hono",
|
|
@@ -57,6 +57,6 @@
|
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|
|
59
59
|
"hono": ">=4.6.0",
|
|
60
|
-
"sigmx": "^0.
|
|
60
|
+
"sigmx": "^0.2.0"
|
|
61
61
|
}
|
|
62
62
|
}
|