@dokebi/web 0.4.5 → 0.4.6
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/AI.md +2 -2
- package/dist/index.js +1 -1
- package/dist/points.d.ts +26 -4
- package/dist/points.js +15 -5
- package/package.json +1 -1
package/AI.md
CHANGED
|
@@ -150,7 +150,7 @@ http.route({ method: 'POST', url: '/import', handler: async (request, reply) =>
|
|
|
150
150
|
|
|
151
151
|
- `dependency` of `@dokebi/web/dependency` (also exported from the root) is the provider descriptor for plugins and app code that always install web (D-0063 §6; a small module that does not load Fastify).
|
|
152
152
|
- `@dokebi/web/points` exports the contribution points `viewEngines` (`dokebi-web:view-engine`) and `realtime` (`dokebi-web:realtime`) and their value types (`WebViewEngine`, `WebRealtimeTransport`, `WebRealtimeScope`): template-ejs and template-inertia contribute view engines, websocket and sse contribute realtime transports, and @dokebi/app collects them.
|
|
153
|
-
- `viewData` (`dokebi-web:view-data`, D-0076 §5) carries values a plugin adds to every rendered page: contribute `{ key, value(request) }` (list the point under `contributes`). @dokebi/app puts it in the EJS data of every app page and error view (`data.<key>`: after `sealed` and the flash, before the app's `shared` and the page data, so those win), template-inertia in the shared props (before the entry's `shared` and the page props). `value` runs only when a page renders (JSON routes never call it) and may return a Promise. `collectViewData(contributions, { plugin, reserved })` is the collectors' check: a key twice, a key the collector sets itself (app: `sealed`, `seal`, `errors`, `old`, `flash`; template-inertia: `errors`, `flash`) or an invalid entry refuses the setup with `VIEW_DATA_CONFLICT` (`plugin` is the collector).
|
|
153
|
+
- `viewData` (`dokebi-web:view-data`, D-0076 §5) carries values a plugin adds to every rendered page: contribute `{ key, value(request) }` (list the point under `contributes`). @dokebi/app puts it in the EJS data of every app page and error view (`data.<key>`: after `sealed` and the flash, before the app's `shared` and the page data, so those win), template-inertia in the shared props (before the entry's `shared` and the page props). `value` runs only when a page renders (JSON routes never call it) and may return a Promise. **Public values (0.4.6, D-0195)**: a contribution with `public: true` says its value is the same for every visitor of a URL and language (@dokebi/i18n `locale` and `i18n`), never the visitor's own (`auth`, `csrf`). Only those go into the first HTML of a template-inertia public page (`render(…, { public: true })`); `value(request, { public: true })` is then called, and must not depend on what the browser sent besides the URL and its language. `collectViewData` returns `publicKeys`, and `values(request, { public: true })` calls only the public contributors. `collectViewData(contributions, { plugin, reserved })` is the collectors' check: a key twice, a key the collector sets itself (app: `sealed`, `seal`, `errors`, `old`, `flash`; template-inertia: `errors`, `flash`) or an invalid entry refuses the setup with `VIEW_DATA_CONFLICT` (`plugin` is the collector).
|
|
154
154
|
- `errorTranslators` (`dokebi-web:error-translator`, D-0093) is collected by web itself: contribute `{ translate(error, request) }` (type `WebErrorTranslator`; list the point under `contributes`, and core sets the contributor up before web). An error that would answer 500 is offered to every contribution in setup order: an uncoded error (`INTERNAL_SERVER_ERROR`, no Fastify 4xx), and a Dokebi error answering 500, thrown in a handler or any hook: a plugin's coded failure around a driver error (dokebi-auth's HOOK_FAILED of a `findById` on a full pool, dokebi-session's STORE_FAILED over a full Redis connection), or a plugin's own error its translator knows (a bare @dokebi/redis `QUEUE_FULL`, D-0102); for these only a translation answering 5xx counts. The first `DokebiError` returned answers instead, through the normal path of §6 (its status and code, JSON or HTML, `errorBody`, the error responders, sealed JSON). `translate` is synchronous, returns undefined to pass, puts the thrown value in the result's `cause` and no driver text, values or names in its message, hint or data; a numeric `data.retryAfterMs` also sets `Retry-After` (seconds, rounded up). @dokebi/db contributes one from its adapters' `classifyError` (`DB_CONFLICT` 409, `DB_INVALID_DATA` 422, `DB_TIMEOUT`/`DB_BUSY`/`DB_UNAVAILABLE` 503, db AI.md §4.1); @dokebi/redis one for its transient errors (`REDIS_BUSY`/`REDIS_TIMEOUT`/`REDIS_UNAVAILABLE` 503, redis AI.md §4). A contribution without a `translate` function fails the setup with `INVALID_ERROR_TRANSLATOR`; a translator that throws or returns anything else is logged and skipped (§6).
|
|
155
155
|
- `@dokebi/web/points` also has the shared pieces of app binding (D-0132, §2.1): `appOption(description)` (the `app` option of a plugin that adds routes, for `defineOptions`), `appUrlMatch(routing, url, apps)` (whether a URL a plugin publishes is a domain of the apps that serve it), `APP_NAME`, `APP_NAMES_MAX` and the types `WebAppSetting`, `WebAppRouting`, `WebAppUrlMatch` (also exported from the root, with `appNames`).
|
|
156
156
|
- **Before-hijack steps** (D-0089, `@dokebi/web/points`, also from the root): a hijacked reply (an SSE stream, a WebSocket upgrade) runs no `onSend`, so plugins that finish work there (@dokebi/session saves the session and writes its cookie, @dokebi/auth its cookies) also push a step onto `beforeHijackSteps(scope)` (one list per server, under `BEFORE_HIJACK` = `Symbol.for('dokebi.web.beforeHijack')`, shared by name), and a transport calls `runBeforeHijack(request, reply, timeoutMs, timeoutError)` before it writes its own head: `undefined` when nothing is to be waited for (no Promise, no timer), else a Promise of the number of steps waited for, rejecting with the first failure or `timeoutError()` (`WebBeforeHijackStep`: `(request, reply) => PromiseLike<void> | undefined`).
|
|
@@ -386,7 +386,7 @@ dokebi-web knows no session: the caller flashes `errors` and `old` and answers `
|
|
|
386
386
|
| `ROUTE_APP_UNAVAILABLE` | start: a route is bound to apps (`app`, a plugin's `<plugin>.app`) but no app routing is registered (@dokebi/app missing; `data.route`, `data.apps`) (D-0132) |
|
|
387
387
|
| `ROUTE_APP_UNKNOWN` | start: a route is bound to a name that is not an app (`data.app`, `data.apps`) (D-0132) |
|
|
388
388
|
| `ROUTE_APP_CONFLICT` | start: two routes with the same method and path answer on one app (`data.route`, `data.app`) (D-0132) |
|
|
389
|
-
| `VIEW_DATA_CONFLICT` | setup of a collector (@dokebi/app, template-inertia): two plugins contribute one `dokebi-web:view-data` key, a key is the collector's own, or a contribution is not `{ key, value }` (thrown by `collectViewData`) |
|
|
389
|
+
| `VIEW_DATA_CONFLICT` | setup of a collector (@dokebi/app, template-inertia): two plugins contribute one `dokebi-web:view-data` key, a key is the collector's own, or a contribution is not `{ key, value, public? }` (thrown by `collectViewData`) |
|
|
390
390
|
|
|
391
391
|
Option errors are core `INVALID_PLUGIN_OPTIONS` (cause `INVALID_OPTIONS` with paths such as `trustProxy[0]` in `details.issues`). A 500 leaves the `error` log `HTTP request failed` (`code: WEB_REQUEST_FAILED`, `requestId`, `method`, `route` (the route pattern, no query), `status`, `errorCode`, `error` (name, full message, stack and `Caused by:` causes; only URL credentials are removed as `//***@`, cut above 16 KB)). The same route and error is logged once per `logRepeatWindowMs` (10 seconds) and the count in between appears as `repeated` in the next record (D-0047). A 4xx is debug `HTTP request rejected` (`status`, `route`, the full `error` text, `errorCode`). A translated error (D-0093), whatever its status, is the `error` log `HTTP request error translated` (`code: WEB_ERROR_TRANSLATED`, `requestId`, `method`, `route`, `status`, `errorCode`, `translator` (the plugin), `error`: the translated error with the thrown value as its `Caused by:`), once per route, code and 10 seconds with `repeated` like the 500 log, which it replaces. **Retryable 503s under a burst ([D-0113](https://git.nyx-zone.com/dokebi/framework/-/blob/main/docs/decisions/D-0113-overload-log-and-storage-wait.md)).** A translated error that answers 503, and a coded error that answers 503 with `data.retryAfterMs` (`Retry-After`), is an expected refusal: a burst of them must not write a full stack per request (AI-M25: 193,466 records, about 200 MB in 65 s, rotated every other log out). The first one of each status, error code, method and route in a window of `transientLogWindowMs` is logged in full as above (`WEB_ERROR_TRANSLATED` or `WEB_REQUEST_FAILED`, at `transientLogLevel`; not the seal refusals whose own layer writes their one record: `SEAL_NONCES_LOST`, `SEAL_BUSY`, and `SEAL_NONCE_STORE_UNAVAILABLE` of dokebi-seal's shared stores (see the error code table)); the rest are counted, and when the window ends, or when the server closes, one line per pair gives the exact count: `HTTP transient errors repeated` (`code: WEB_TRANSIENT_REPEATED`, `method`, `route`, `status`, `errorCode`, `translator` when translated, `repeated` (the ones after the logged first), `windowMs`). The next window logs its first one in full again. Past `transientLogMaxKeys` pairs in one window, new pairs are only counted, in one `WEB_TRANSIENT_REPEATED` line with `overflow: true`, `repeated`, `maxKeys` and a `fix`. Every other 5xx (a 500, a 503 without `Retry-After`) and a translated 4xx keep the rule above. A translator that throws or returns something else is the `error` log `Web error translator failed` (`code: WEB_ERROR_TRANSLATOR_FAILED`, `route`, `translator`, `fix`, `error`), once per translator, route and interval; the next translator, finally the 500 answer, takes the error. If you register your own error handler, its security is your responsibility.
|
|
392
392
|
|
package/dist/index.js
CHANGED
package/dist/points.d.ts
CHANGED
|
@@ -58,8 +58,25 @@ export interface WebRealtimeTransport<TRoute = unknown, THandle = unknown> {
|
|
|
58
58
|
export interface WebViewData {
|
|
59
59
|
/** The name the value appears under. Two contributors with the same key refuse the setup. */
|
|
60
60
|
readonly key: string;
|
|
61
|
-
/**
|
|
62
|
-
|
|
61
|
+
/**
|
|
62
|
+
* D-0195: the value is the same for every visitor of a URL (a language, the site's settings),
|
|
63
|
+
* never the visitor's own (a user, a token). Only such values go into the first HTML of a
|
|
64
|
+
* public page (template-inertia `render(…, { public: true })`), which a shared cache may keep;
|
|
65
|
+
* the others reach the page afterwards. Default false.
|
|
66
|
+
*/
|
|
67
|
+
readonly public?: boolean;
|
|
68
|
+
/**
|
|
69
|
+
* The value for one request; may be a Promise. Must not throw for an ordinary request. `view`
|
|
70
|
+
* says what it is for: `public: true` for the first HTML of a public page (D-0195), where the
|
|
71
|
+
* value must not depend on what this browser sent besides the URL and its language (cookies
|
|
72
|
+
* that only save bytes, for example).
|
|
73
|
+
*/
|
|
74
|
+
value(request: FastifyRequest, view?: WebViewDataRequest): unknown;
|
|
75
|
+
}
|
|
76
|
+
/** What one view data value is computed for (D-0195). */
|
|
77
|
+
export interface WebViewDataRequest {
|
|
78
|
+
/** The first HTML of a public page: the same for every visitor of the URL and language. */
|
|
79
|
+
readonly public: boolean;
|
|
63
80
|
}
|
|
64
81
|
/**
|
|
65
82
|
* Turns an unhandled error into a coded one (`dokebi-web:error-translator`, D-0093): dokebi-db
|
|
@@ -93,8 +110,13 @@ export declare const errorTranslators: import("@dokebi/core").ContributionPoint<
|
|
|
93
110
|
export interface WebViewDataCollection {
|
|
94
111
|
/** Contributed keys in setup order; empty when nothing was contributed (then `values` is never needed). */
|
|
95
112
|
readonly keys: readonly string[];
|
|
96
|
-
/**
|
|
97
|
-
|
|
113
|
+
/** The keys whose contributors said `public: true` (D-0195), in setup order. */
|
|
114
|
+
readonly publicKeys: readonly string[];
|
|
115
|
+
/**
|
|
116
|
+
* The contributors' values for this request, computed together. `{ public: true }` (D-0195):
|
|
117
|
+
* only the public contributors, each told so; the others are not called.
|
|
118
|
+
*/
|
|
119
|
+
values(request: FastifyRequest, view?: WebViewDataRequest): Promise<Record<string, unknown>>;
|
|
98
120
|
}
|
|
99
121
|
/**
|
|
100
122
|
* Checks the contributions of `dokebi-web:view-data` for one collector (dokebi-app for EJS,
|
package/dist/points.js
CHANGED
|
@@ -17,11 +17,14 @@ const VIEW_DATA_KEY = /^[A-Za-z_$][A-Za-z0-9_$]{0,63}$/;
|
|
|
17
17
|
export function collectViewData(contributions, collector) {
|
|
18
18
|
const entries = new Map();
|
|
19
19
|
for (const { plugin, value: contribution } of contributions) {
|
|
20
|
-
const { key, value } = (contribution ?? {});
|
|
20
|
+
const { key, value, public: shared } = (contribution ?? {});
|
|
21
21
|
const refuse = (message, fix) => {
|
|
22
22
|
throw new DokebiError('VIEW_DATA_CONFLICT', message, { plugin: collector.plugin, fix });
|
|
23
23
|
};
|
|
24
|
-
if (typeof key !== 'string' ||
|
|
24
|
+
if (typeof key !== 'string' ||
|
|
25
|
+
!VIEW_DATA_KEY.test(key) ||
|
|
26
|
+
typeof value !== 'function' ||
|
|
27
|
+
(shared !== undefined && typeof shared !== 'boolean'))
|
|
25
28
|
refuse(`${plugin} contributed invalid view data`, `Contribute { key: 'name', value: (request) => value } to dokebi-web:view-data (${plugin}).`);
|
|
26
29
|
const name = key;
|
|
27
30
|
if (collector.reserved.includes(name))
|
|
@@ -32,12 +35,19 @@ export function collectViewData(contributions, collector) {
|
|
|
32
35
|
entries.set(name, { plugin, source: contribution });
|
|
33
36
|
}
|
|
34
37
|
const list = [...entries];
|
|
38
|
+
const publicList = list.filter(([, entry]) => entry.source.public === true);
|
|
39
|
+
const ALL = Object.freeze({ public: false });
|
|
40
|
+
const PUBLIC = Object.freeze({ public: true });
|
|
35
41
|
return Object.freeze({
|
|
36
42
|
keys: Object.freeze(list.map(([key]) => key)),
|
|
37
|
-
|
|
38
|
-
|
|
43
|
+
publicKeys: Object.freeze(publicList.map(([key]) => key)),
|
|
44
|
+
values: async (request, view) => {
|
|
45
|
+
const only = view?.public === true;
|
|
46
|
+
const chosen = only ? publicList : list;
|
|
47
|
+
const told = only ? PUBLIC : ALL;
|
|
48
|
+
const values = await Promise.all(chosen.map(([, entry]) => entry.source.value(request, told)));
|
|
39
49
|
const result = {};
|
|
40
|
-
|
|
50
|
+
chosen.forEach(([key], index) => {
|
|
41
51
|
result[key] = values[index];
|
|
42
52
|
});
|
|
43
53
|
return result;
|