@weft-framework/server 0.0.0-stage → 0.0.3
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 +4 -2
- package/dist/account.d.ts +124 -0
- package/dist/account.js +344 -0
- package/dist/action.d.ts +43 -0
- package/dist/action.js +135 -0
- package/dist/admin.d.ts +99 -0
- package/dist/admin.js +431 -0
- package/dist/api.d.ts +21 -0
- package/dist/api.js +72 -0
- package/dist/app.d.ts +202 -0
- package/dist/app.js +3692 -0
- package/dist/assets.d.ts +4 -0
- package/dist/assets.js +91 -0
- package/dist/auth.d.ts +150 -0
- package/dist/auth.js +215 -0
- package/dist/backup.d.ts +46 -0
- package/dist/backup.js +345 -0
- package/dist/build.d.ts +33 -0
- package/dist/build.js +274 -0
- package/dist/bun.d.ts +30 -0
- package/dist/bun.js +53 -0
- package/dist/calendar.d.ts +161 -0
- package/dist/calendar.js +690 -0
- package/dist/compiler.d.ts +13 -0
- package/dist/compiler.js +58 -0
- package/dist/compress.d.ts +6 -0
- package/dist/compress.js +78 -0
- package/dist/config.d.ts +26 -0
- package/dist/config.js +317 -0
- package/dist/context.d.ts +9 -0
- package/dist/context.js +1 -0
- package/dist/contract.d.ts +18 -0
- package/dist/contract.js +171 -0
- package/dist/datamap.d.ts +24 -0
- package/dist/datamap.js +365 -0
- package/dist/db-backup.d.ts +27 -0
- package/dist/db-backup.js +115 -0
- package/dist/dev-events.d.ts +14 -0
- package/dist/dev-events.js +135 -0
- package/dist/dev.d.ts +1 -0
- package/dist/dev.js +240 -0
- package/dist/devtools-client.d.ts +1 -0
- package/dist/devtools-client.js +362 -0
- package/dist/devtools.d.ts +87 -0
- package/dist/devtools.js +68 -0
- package/dist/edge.d.ts +3 -0
- package/dist/edge.js +7 -0
- package/dist/email-signin.d.ts +92 -0
- package/dist/email-signin.js +259 -0
- package/dist/env.d.ts +1 -0
- package/dist/env.js +15 -0
- package/dist/explain.d.ts +12 -0
- package/dist/explain.js +217 -0
- package/dist/files.d.ts +6 -0
- package/dist/files.js +170 -0
- package/dist/grants.d.ts +69 -0
- package/dist/grants.js +206 -0
- package/dist/image/bitmap.d.ts +11 -0
- package/dist/image/bitmap.js +172 -0
- package/dist/image/jpeg.d.ts +10 -0
- package/dist/image/jpeg.js +697 -0
- package/dist/image/png.d.ts +8 -0
- package/dist/image/png.js +282 -0
- package/dist/image/transform.d.ts +20 -0
- package/dist/image/transform.js +52 -0
- package/dist/image/worker.d.ts +1 -0
- package/dist/image/worker.js +11 -0
- package/dist/images.d.ts +39 -0
- package/dist/images.js +283 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.js +31 -0
- package/dist/inspect-hub.d.ts +42 -0
- package/dist/inspect-hub.js +14 -0
- package/dist/inspector.d.ts +51 -0
- package/dist/inspector.js +193 -0
- package/dist/job-admin.d.ts +55 -0
- package/dist/job-admin.js +89 -0
- package/dist/jobs.d.ts +222 -0
- package/dist/jobs.js +789 -0
- package/dist/live.d.ts +72 -0
- package/dist/live.js +481 -0
- package/dist/load-config.d.ts +2 -0
- package/dist/load-config.js +41 -0
- package/dist/log.d.ts +20 -0
- package/dist/log.js +143 -0
- package/dist/mail.d.ts +189 -0
- package/dist/mail.js +999 -0
- package/dist/media.d.ts +129 -0
- package/dist/media.js +470 -0
- package/dist/meta.d.ts +54 -0
- package/dist/meta.js +146 -0
- package/dist/middleware.d.ts +20 -0
- package/dist/middleware.js +101 -0
- package/dist/migrate.d.ts +12 -0
- package/dist/migrate.js +88 -0
- package/dist/node.d.ts +26 -0
- package/dist/node.js +96 -0
- package/dist/notice.d.ts +21 -0
- package/dist/notice.js +48 -0
- package/dist/npm.d.ts +16 -0
- package/dist/npm.js +347 -0
- package/dist/oauth.d.ts +76 -0
- package/dist/oauth.js +408 -0
- package/dist/otel.d.ts +57 -0
- package/dist/otel.js +293 -0
- package/dist/passkey.d.ts +99 -0
- package/dist/passkey.js +572 -0
- package/dist/password.d.ts +8 -0
- package/dist/password.js +65 -0
- package/dist/pdf.d.ts +46 -0
- package/dist/pdf.js +332 -0
- package/dist/personal-data.d.ts +22 -0
- package/dist/personal-data.js +148 -0
- package/dist/pointers.d.ts +9 -0
- package/dist/pointers.js +148 -0
- package/dist/postcss.d.ts +3 -0
- package/dist/postcss.js +89 -0
- package/dist/presence.d.ts +27 -0
- package/dist/presence.js +161 -0
- package/dist/preview.d.ts +25 -0
- package/dist/preview.js +353 -0
- package/dist/production.d.ts +16 -0
- package/dist/production.js +119 -0
- package/dist/project.d.ts +23 -0
- package/dist/project.js +536 -0
- package/dist/regions.d.ts +47 -0
- package/dist/regions.js +511 -0
- package/dist/replay.d.ts +44 -0
- package/dist/replay.js +259 -0
- package/dist/router.d.ts +29 -0
- package/dist/router.js +146 -0
- package/dist/rules-diff.d.ts +18 -0
- package/dist/rules-diff.js +165 -0
- package/dist/rules-test.d.ts +17 -0
- package/dist/rules-test.js +108 -0
- package/dist/saml.d.ts +160 -0
- package/dist/saml.js +959 -0
- package/dist/seal.d.ts +35 -0
- package/dist/seal.js +107 -0
- package/dist/secret.d.ts +14 -0
- package/dist/secret.js +110 -0
- package/dist/security.d.ts +53 -0
- package/dist/security.js +250 -0
- package/dist/seed.d.ts +16 -0
- package/dist/seed.js +94 -0
- package/dist/seo.d.ts +42 -0
- package/dist/seo.js +175 -0
- package/dist/settings.d.ts +79 -0
- package/dist/settings.js +212 -0
- package/dist/shared.d.ts +15 -0
- package/dist/shared.js +283 -0
- package/dist/shell.d.ts +12 -0
- package/dist/shell.js +118 -0
- package/dist/storage.d.ts +13 -0
- package/dist/storage.js +55 -0
- package/dist/support.d.ts +83 -0
- package/dist/support.js +236 -0
- package/dist/sync.d.ts +35 -0
- package/dist/sync.js +167 -0
- package/dist/tailwind.d.ts +14 -0
- package/dist/tailwind.js +117 -0
- package/dist/two-factor.d.ts +103 -0
- package/dist/two-factor.js +435 -0
- package/dist/uploads.d.ts +185 -0
- package/dist/uploads.js +991 -0
- package/dist/webhooks.d.ts +169 -0
- package/dist/webhooks.js +758 -0
- package/dist/webpush.d.ts +97 -0
- package/dist/webpush.js +372 -0
- package/dist/wizard.d.ts +141 -0
- package/dist/wizard.js +410 -0
- package/dist/workflows.d.ts +134 -0
- package/dist/workflows.js +511 -0
- package/package.json +186 -4
package/dist/live.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { type Component, type EventMessage, type ServerMessage } from "@weft-framework/runtime";
|
|
2
|
+
export declare class Redirect {
|
|
3
|
+
readonly to: string;
|
|
4
|
+
constructor(to: string);
|
|
5
|
+
}
|
|
6
|
+
export declare function redirect(to: string): Redirect;
|
|
7
|
+
export type PageState = [path: string, name: string, count: number, values: [number, unknown][]][];
|
|
8
|
+
export interface InspectNode {
|
|
9
|
+
id: string;
|
|
10
|
+
name: string;
|
|
11
|
+
island: boolean;
|
|
12
|
+
renders: number;
|
|
13
|
+
ms: number;
|
|
14
|
+
children: InspectNode[];
|
|
15
|
+
}
|
|
16
|
+
export type Reload = {
|
|
17
|
+
props: object;
|
|
18
|
+
} | {
|
|
19
|
+
message: ServerMessage;
|
|
20
|
+
} | null;
|
|
21
|
+
export interface LivePageOptions {
|
|
22
|
+
onError?(err: unknown, msg?: EventMessage): void;
|
|
23
|
+
maxInstances?: number;
|
|
24
|
+
aroundRender?(render: () => void): void;
|
|
25
|
+
restore?: PageState;
|
|
26
|
+
sealArgs?(action: string, args: readonly unknown[]): string;
|
|
27
|
+
onInvalidate?(): void;
|
|
28
|
+
}
|
|
29
|
+
export declare class LivePage {
|
|
30
|
+
readonly id: string;
|
|
31
|
+
private readonly root;
|
|
32
|
+
private readonly byId;
|
|
33
|
+
private readonly opts;
|
|
34
|
+
private queue;
|
|
35
|
+
private nextId;
|
|
36
|
+
private readonly marker;
|
|
37
|
+
private readonly invalidate;
|
|
38
|
+
private working;
|
|
39
|
+
private closed;
|
|
40
|
+
private fresh;
|
|
41
|
+
private pendingAwaits;
|
|
42
|
+
constructor(component: Component<object>, props: object, opts?: LivePageOptions);
|
|
43
|
+
private readonly restoring;
|
|
44
|
+
snapshot(): PageState;
|
|
45
|
+
inspect(): InspectNode;
|
|
46
|
+
styles(): string[];
|
|
47
|
+
render(): string;
|
|
48
|
+
get rootId(): string;
|
|
49
|
+
html(): string;
|
|
50
|
+
current(): ServerMessage;
|
|
51
|
+
close(): void;
|
|
52
|
+
get awaiting(): boolean;
|
|
53
|
+
inert(): boolean;
|
|
54
|
+
quiet(): boolean;
|
|
55
|
+
componentOf(id: string): string | undefined;
|
|
56
|
+
get size(): number;
|
|
57
|
+
dispatch(msg: EventMessage): Promise<ServerMessage[]>;
|
|
58
|
+
refresh(reload?: () => Promise<Reload>): Promise<ServerMessage[]>;
|
|
59
|
+
private renderAll;
|
|
60
|
+
private mountFresh;
|
|
61
|
+
private changed;
|
|
62
|
+
private apply;
|
|
63
|
+
private create;
|
|
64
|
+
private dispose;
|
|
65
|
+
private renderTree;
|
|
66
|
+
private placeholder;
|
|
67
|
+
private inner;
|
|
68
|
+
private unchanged;
|
|
69
|
+
private wrap;
|
|
70
|
+
private markSent;
|
|
71
|
+
private collectPatches;
|
|
72
|
+
}
|
package/dist/live.js
ADDED
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
import { assignProps, collectSignals, effect, escapeAttr, runHooks, styleOf, untrack, withRenderContext, } from "@weft-framework/runtime";
|
|
2
|
+
import { randomId } from "./security.js";
|
|
3
|
+
export class Redirect {
|
|
4
|
+
to;
|
|
5
|
+
constructor(to) {
|
|
6
|
+
this.to = to;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export function redirect(to) {
|
|
10
|
+
return new Redirect(to);
|
|
11
|
+
}
|
|
12
|
+
const ARG_RENDERS = 8;
|
|
13
|
+
function argTable() {
|
|
14
|
+
return { byKey: new Map(), byToken: new Map(), next: 0, render: 0 };
|
|
15
|
+
}
|
|
16
|
+
const objectIds = new WeakMap();
|
|
17
|
+
let nextObjectId = 0;
|
|
18
|
+
let nextUnkeyed = 0;
|
|
19
|
+
function argsKey(args) {
|
|
20
|
+
return args
|
|
21
|
+
.map((v) => {
|
|
22
|
+
switch (typeof v) {
|
|
23
|
+
case "string":
|
|
24
|
+
return JSON.stringify(v);
|
|
25
|
+
case "number":
|
|
26
|
+
return Object.is(v, -0) ? "n-0" : `n${v}`;
|
|
27
|
+
case "bigint":
|
|
28
|
+
return `b${v}`;
|
|
29
|
+
case "boolean":
|
|
30
|
+
return v ? "t" : "f";
|
|
31
|
+
case "undefined":
|
|
32
|
+
return "u";
|
|
33
|
+
case "symbol":
|
|
34
|
+
return `s${nextUnkeyed++}`;
|
|
35
|
+
default: {
|
|
36
|
+
if (v === null) {
|
|
37
|
+
return "z";
|
|
38
|
+
}
|
|
39
|
+
const obj = v;
|
|
40
|
+
let id = objectIds.get(obj);
|
|
41
|
+
if (id === undefined) {
|
|
42
|
+
id = nextObjectId++;
|
|
43
|
+
objectIds.set(obj, id);
|
|
44
|
+
}
|
|
45
|
+
return `o${id}`;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
.join(",");
|
|
50
|
+
}
|
|
51
|
+
function plainData(v, depth = 0) {
|
|
52
|
+
if (v === null || typeof v === "string" || typeof v === "boolean")
|
|
53
|
+
return true;
|
|
54
|
+
if (typeof v === "number")
|
|
55
|
+
return Number.isFinite(v);
|
|
56
|
+
if (typeof v !== "object" || depth > 16)
|
|
57
|
+
return false;
|
|
58
|
+
if (Array.isArray(v))
|
|
59
|
+
return v.every((x) => plainData(x, depth + 1));
|
|
60
|
+
const proto = Object.getPrototypeOf(v);
|
|
61
|
+
if (proto !== Object.prototype && proto !== null)
|
|
62
|
+
return false;
|
|
63
|
+
return Object.values(v).every((x) => plainData(x, depth + 1));
|
|
64
|
+
}
|
|
65
|
+
const kindOf = (v) => (v === null ? "null" : Array.isArray(v) ? "array" : typeof v);
|
|
66
|
+
export class LivePage {
|
|
67
|
+
id;
|
|
68
|
+
root;
|
|
69
|
+
byId = new Map();
|
|
70
|
+
opts;
|
|
71
|
+
queue = Promise.resolve();
|
|
72
|
+
nextId = 0;
|
|
73
|
+
marker = randomId(12);
|
|
74
|
+
invalidate = () => {
|
|
75
|
+
if (!this.closed)
|
|
76
|
+
this.opts.onInvalidate?.();
|
|
77
|
+
};
|
|
78
|
+
working = false;
|
|
79
|
+
closed = false;
|
|
80
|
+
fresh = [];
|
|
81
|
+
pendingAwaits = 0;
|
|
82
|
+
constructor(component, props, opts = {}) {
|
|
83
|
+
this.id = randomId();
|
|
84
|
+
this.opts = opts;
|
|
85
|
+
if (opts.restore) {
|
|
86
|
+
for (const [path, name, count, values] of opts.restore)
|
|
87
|
+
this.restoring.set(path, { name, count, values });
|
|
88
|
+
}
|
|
89
|
+
this.root = this.create("r", component.def, props, "r");
|
|
90
|
+
}
|
|
91
|
+
restoring = new Map();
|
|
92
|
+
snapshot() {
|
|
93
|
+
const out = [];
|
|
94
|
+
const walk = (rec) => {
|
|
95
|
+
if (rec.signals.length > 0) {
|
|
96
|
+
const values = [];
|
|
97
|
+
rec.signals.forEach((s, i) => {
|
|
98
|
+
const v = untrack(() => s.value);
|
|
99
|
+
if (plainData(v))
|
|
100
|
+
values.push([i, v]);
|
|
101
|
+
});
|
|
102
|
+
if (values.length > 0)
|
|
103
|
+
out.push([rec.path, rec.def.name, rec.signals.length, values]);
|
|
104
|
+
}
|
|
105
|
+
for (const c of rec.children.values())
|
|
106
|
+
walk(c);
|
|
107
|
+
};
|
|
108
|
+
walk(this.root);
|
|
109
|
+
return out;
|
|
110
|
+
}
|
|
111
|
+
inspect() {
|
|
112
|
+
const node = (rec) => ({
|
|
113
|
+
id: rec.id,
|
|
114
|
+
name: rec.def.name,
|
|
115
|
+
island: Boolean(rec.def.island),
|
|
116
|
+
renders: rec.renders,
|
|
117
|
+
ms: Math.round(rec.ms * 100) / 100,
|
|
118
|
+
children: [...rec.children.values()].map(node),
|
|
119
|
+
});
|
|
120
|
+
return node(this.root);
|
|
121
|
+
}
|
|
122
|
+
styles() {
|
|
123
|
+
const out = new Set();
|
|
124
|
+
for (const rec of this.byId.values()) {
|
|
125
|
+
if (rec.def.css)
|
|
126
|
+
out.add(styleOf(rec.def.css));
|
|
127
|
+
for (const css of rec.inlined ?? [])
|
|
128
|
+
out.add(styleOf(css));
|
|
129
|
+
}
|
|
130
|
+
return [...out];
|
|
131
|
+
}
|
|
132
|
+
render() {
|
|
133
|
+
this.renderAll();
|
|
134
|
+
this.markSent(this.root);
|
|
135
|
+
return this.wrap(this.root);
|
|
136
|
+
}
|
|
137
|
+
get rootId() {
|
|
138
|
+
return this.root.id;
|
|
139
|
+
}
|
|
140
|
+
html() {
|
|
141
|
+
return this.wrap(this.root);
|
|
142
|
+
}
|
|
143
|
+
current() {
|
|
144
|
+
return { t: "patch", id: this.root.id, html: this.inner(this.root) };
|
|
145
|
+
}
|
|
146
|
+
close() {
|
|
147
|
+
if (this.closed) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
this.closed = true;
|
|
151
|
+
this.dispose(this.root);
|
|
152
|
+
}
|
|
153
|
+
get awaiting() {
|
|
154
|
+
return this.pendingAwaits > 0;
|
|
155
|
+
}
|
|
156
|
+
inert() {
|
|
157
|
+
for (const rec of this.byId.values()) {
|
|
158
|
+
if (!rec.def.inert)
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
return !this.awaiting;
|
|
162
|
+
}
|
|
163
|
+
quiet() {
|
|
164
|
+
for (const rec of this.byId.values()) {
|
|
165
|
+
if (Object.keys(rec.inst.handlers ?? {}).length > 0 || (rec.inst.mount?.length ?? 0) > 0)
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
return !this.awaiting;
|
|
169
|
+
}
|
|
170
|
+
componentOf(id) {
|
|
171
|
+
return this.byId.get(id)?.def.name;
|
|
172
|
+
}
|
|
173
|
+
get size() {
|
|
174
|
+
return this.byId.size;
|
|
175
|
+
}
|
|
176
|
+
dispatch(msg) {
|
|
177
|
+
const run = this.queue.then(() => this.apply(msg));
|
|
178
|
+
this.queue = run.catch(() => undefined);
|
|
179
|
+
return run;
|
|
180
|
+
}
|
|
181
|
+
refresh(reload) {
|
|
182
|
+
const run = this.queue.then(async () => {
|
|
183
|
+
if (reload) {
|
|
184
|
+
const next = await reload();
|
|
185
|
+
if (next === null) {
|
|
186
|
+
return [];
|
|
187
|
+
}
|
|
188
|
+
if ("message" in next) {
|
|
189
|
+
return [next.message];
|
|
190
|
+
}
|
|
191
|
+
assignProps(this.root.props, next.props);
|
|
192
|
+
}
|
|
193
|
+
this.renderAll();
|
|
194
|
+
const out = [];
|
|
195
|
+
this.collectPatches(this.root, undefined, out);
|
|
196
|
+
return out;
|
|
197
|
+
});
|
|
198
|
+
this.queue = run.catch(() => undefined);
|
|
199
|
+
return run;
|
|
200
|
+
}
|
|
201
|
+
renderAll() {
|
|
202
|
+
const around = this.opts.aroundRender;
|
|
203
|
+
const was = this.working;
|
|
204
|
+
this.working = true;
|
|
205
|
+
this.pendingAwaits = 0;
|
|
206
|
+
try {
|
|
207
|
+
if (around) {
|
|
208
|
+
around(() => this.renderTree(this.root));
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
this.renderTree(this.root);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
finally {
|
|
215
|
+
this.working = was;
|
|
216
|
+
}
|
|
217
|
+
this.mountFresh();
|
|
218
|
+
}
|
|
219
|
+
mountFresh() {
|
|
220
|
+
const fresh = this.fresh;
|
|
221
|
+
this.fresh = [];
|
|
222
|
+
for (let i = fresh.length - 1; i >= 0; i -= 1) {
|
|
223
|
+
const rec = fresh[i];
|
|
224
|
+
if (this.closed || !this.byId.has(rec.id)) {
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
rec.cleanups = runHooks(rec.inst.mount, (err) => this.opts.onError?.(err));
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
changed() {
|
|
231
|
+
if (!this.working) {
|
|
232
|
+
this.invalidate();
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
async apply(msg) {
|
|
236
|
+
const ack = msg.seq;
|
|
237
|
+
const rec = this.byId.get(msg.id);
|
|
238
|
+
const handler = rec && Object.hasOwn(rec.inst.handlers, msg.handler) ? rec.inst.handlers[msg.handler] : undefined;
|
|
239
|
+
if (!rec || typeof handler !== "function") {
|
|
240
|
+
return [{ t: "error", message: "Unknown handler", ack }];
|
|
241
|
+
}
|
|
242
|
+
let args = [];
|
|
243
|
+
if (msg.args !== undefined) {
|
|
244
|
+
const bound = rec.args.byToken.get(msg.args);
|
|
245
|
+
if (!bound) {
|
|
246
|
+
return [{ t: "error", message: "Stale event", ack }];
|
|
247
|
+
}
|
|
248
|
+
args = bound.args;
|
|
249
|
+
}
|
|
250
|
+
let result;
|
|
251
|
+
this.working = true;
|
|
252
|
+
try {
|
|
253
|
+
result = await handler(...args, msg.event);
|
|
254
|
+
}
|
|
255
|
+
catch (err) {
|
|
256
|
+
this.opts.onError?.(err, msg);
|
|
257
|
+
return [{ t: "error", message: "Handler failed", ack }];
|
|
258
|
+
}
|
|
259
|
+
finally {
|
|
260
|
+
this.working = false;
|
|
261
|
+
}
|
|
262
|
+
if (result instanceof Redirect) {
|
|
263
|
+
return [{ t: "redirect", to: result.to }];
|
|
264
|
+
}
|
|
265
|
+
this.renderAll();
|
|
266
|
+
const out = [];
|
|
267
|
+
this.collectPatches(this.root, ack, out);
|
|
268
|
+
if (out.length === 0) {
|
|
269
|
+
out.push({ t: "ack", ack });
|
|
270
|
+
}
|
|
271
|
+
return out;
|
|
272
|
+
}
|
|
273
|
+
create(id, def, props, path) {
|
|
274
|
+
const max = this.opts.maxInstances ?? 10_000;
|
|
275
|
+
if (this.byId.size >= max) {
|
|
276
|
+
throw new Error(`@weft-framework/server: live page exceeded ${max} component instances`);
|
|
277
|
+
}
|
|
278
|
+
const live = { ...props };
|
|
279
|
+
const made = collectSignals(() => def.setup(live));
|
|
280
|
+
const saved = this.restoring.get(path);
|
|
281
|
+
if (saved) {
|
|
282
|
+
this.restoring.delete(path);
|
|
283
|
+
if (saved.name === def.name && saved.count === made.signals.length) {
|
|
284
|
+
for (const [i, v] of saved.values) {
|
|
285
|
+
const s = made.signals[i];
|
|
286
|
+
if (!s)
|
|
287
|
+
continue;
|
|
288
|
+
const was = untrack(() => s.value);
|
|
289
|
+
if (was !== null && was !== undefined && kindOf(was) !== kindOf(v))
|
|
290
|
+
continue;
|
|
291
|
+
try {
|
|
292
|
+
s.value = v;
|
|
293
|
+
}
|
|
294
|
+
catch (err) {
|
|
295
|
+
this.opts.onError?.(err);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
const rec = {
|
|
301
|
+
id,
|
|
302
|
+
def,
|
|
303
|
+
props: live,
|
|
304
|
+
inst: made.value,
|
|
305
|
+
path,
|
|
306
|
+
signals: made.signals,
|
|
307
|
+
args: argTable(),
|
|
308
|
+
children: new Map(),
|
|
309
|
+
template: "",
|
|
310
|
+
sent: "",
|
|
311
|
+
cleanups: [],
|
|
312
|
+
renders: 0,
|
|
313
|
+
ms: 0,
|
|
314
|
+
};
|
|
315
|
+
this.byId.set(id, rec);
|
|
316
|
+
this.fresh.push(rec);
|
|
317
|
+
return rec;
|
|
318
|
+
}
|
|
319
|
+
dispose(rec) {
|
|
320
|
+
for (const c of rec.children.values()) {
|
|
321
|
+
this.dispose(c);
|
|
322
|
+
}
|
|
323
|
+
this.byId.delete(rec.id);
|
|
324
|
+
rec.untrack?.();
|
|
325
|
+
rec.untrack = undefined;
|
|
326
|
+
const report = (err) => this.opts.onError?.(err);
|
|
327
|
+
runHooks(rec.cleanups, report);
|
|
328
|
+
rec.cleanups = [];
|
|
329
|
+
runHooks(rec.inst.destroy, report);
|
|
330
|
+
}
|
|
331
|
+
renderTree(rec) {
|
|
332
|
+
const table = rec.args;
|
|
333
|
+
table.render += 1;
|
|
334
|
+
const counts = new Map();
|
|
335
|
+
const seen = new Set();
|
|
336
|
+
let pendingKey = null;
|
|
337
|
+
rec.inlined = undefined;
|
|
338
|
+
const ctx = {
|
|
339
|
+
child: (def, props) => {
|
|
340
|
+
const itemKey = pendingKey;
|
|
341
|
+
pendingKey = null;
|
|
342
|
+
if (def.inert && !def.island) {
|
|
343
|
+
if (def.css)
|
|
344
|
+
(rec.inlined ??= new Set()).add(def.css);
|
|
345
|
+
return def.setup(props).render();
|
|
346
|
+
}
|
|
347
|
+
let key = itemKey === null ? "" : `${def.name}#${itemKey}`;
|
|
348
|
+
if (key === "" || seen.has(key)) {
|
|
349
|
+
const n = counts.get(def.name) ?? 0;
|
|
350
|
+
counts.set(def.name, n + 1);
|
|
351
|
+
key = `${def.name}:${n}`;
|
|
352
|
+
}
|
|
353
|
+
seen.add(key);
|
|
354
|
+
let child = rec.children.get(key);
|
|
355
|
+
if (child && child.def === def) {
|
|
356
|
+
if (def.static) {
|
|
357
|
+
return this.placeholder(child.id);
|
|
358
|
+
}
|
|
359
|
+
assignProps(child.props, props);
|
|
360
|
+
}
|
|
361
|
+
else {
|
|
362
|
+
if (child) {
|
|
363
|
+
this.dispose(child);
|
|
364
|
+
}
|
|
365
|
+
child = this.create(`${rec.id}.${this.nextId++}`, def, props, `${rec.path}/${key}`);
|
|
366
|
+
if (key.includes("#")) {
|
|
367
|
+
child.key = itemKey ?? undefined;
|
|
368
|
+
}
|
|
369
|
+
rec.children.set(key, child);
|
|
370
|
+
}
|
|
371
|
+
this.renderTree(child);
|
|
372
|
+
return this.placeholder(child.id);
|
|
373
|
+
},
|
|
374
|
+
owner: () => rec.id,
|
|
375
|
+
...(this.opts.sealArgs ? { sealArgs: this.opts.sealArgs } : {}),
|
|
376
|
+
bindArgs: (args) => {
|
|
377
|
+
const key = argsKey(args);
|
|
378
|
+
let bound = table.byKey.get(key);
|
|
379
|
+
if (!bound) {
|
|
380
|
+
const entry = { token: String(table.next++), args, render: table.render, key };
|
|
381
|
+
table.byKey.set(key, entry);
|
|
382
|
+
table.byToken.set(entry.token, entry);
|
|
383
|
+
bound = entry;
|
|
384
|
+
}
|
|
385
|
+
bound.args = args;
|
|
386
|
+
bound.render = table.render;
|
|
387
|
+
return bound.token;
|
|
388
|
+
},
|
|
389
|
+
invalidate: this.invalidate,
|
|
390
|
+
awaiting: () => {
|
|
391
|
+
this.pendingAwaits += 1;
|
|
392
|
+
},
|
|
393
|
+
keyed: (key, render) => {
|
|
394
|
+
pendingKey = key;
|
|
395
|
+
try {
|
|
396
|
+
return render();
|
|
397
|
+
}
|
|
398
|
+
finally {
|
|
399
|
+
pendingKey = null;
|
|
400
|
+
}
|
|
401
|
+
},
|
|
402
|
+
};
|
|
403
|
+
rec.untrack?.();
|
|
404
|
+
let first = true;
|
|
405
|
+
rec.untrack = effect(() => {
|
|
406
|
+
if (first) {
|
|
407
|
+
first = false;
|
|
408
|
+
const started = performance.now();
|
|
409
|
+
rec.template = withRenderContext(ctx, rec.inst.render);
|
|
410
|
+
rec.ms = performance.now() - started;
|
|
411
|
+
rec.renders += 1;
|
|
412
|
+
}
|
|
413
|
+
else {
|
|
414
|
+
this.changed();
|
|
415
|
+
}
|
|
416
|
+
});
|
|
417
|
+
for (const [token, bound] of table.byToken) {
|
|
418
|
+
if (table.render - bound.render >= ARG_RENDERS) {
|
|
419
|
+
table.byToken.delete(token);
|
|
420
|
+
table.byKey.delete(bound.key);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
for (const [key, child] of rec.children) {
|
|
424
|
+
if (!seen.has(key)) {
|
|
425
|
+
this.dispose(child);
|
|
426
|
+
rec.children.delete(key);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
placeholder(id) {
|
|
431
|
+
return `\u0000${this.marker}:${id}\u0000`;
|
|
432
|
+
}
|
|
433
|
+
inner(rec, keep = false) {
|
|
434
|
+
if (!rec.template.includes("\u0000")) {
|
|
435
|
+
return rec.template;
|
|
436
|
+
}
|
|
437
|
+
const re = new RegExp(`\u0000${this.marker}:([^\u0000]+)\u0000`, "g");
|
|
438
|
+
return rec.template.replace(re, (_, id) => {
|
|
439
|
+
const child = this.byId.get(id);
|
|
440
|
+
if (!child)
|
|
441
|
+
return "";
|
|
442
|
+
if (keep && this.unchanged(child)) {
|
|
443
|
+
return `<weft-c data-weft-c="${child.id}" data-weft-keep></weft-c>`;
|
|
444
|
+
}
|
|
445
|
+
return this.wrap(child, keep);
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
unchanged(rec) {
|
|
449
|
+
if (rec.template !== rec.sent)
|
|
450
|
+
return false;
|
|
451
|
+
for (const c of rec.children.values()) {
|
|
452
|
+
if (!this.unchanged(c))
|
|
453
|
+
return false;
|
|
454
|
+
}
|
|
455
|
+
return true;
|
|
456
|
+
}
|
|
457
|
+
wrap(rec, keep = false) {
|
|
458
|
+
const key = rec.key === undefined ? "" : ` data-weft-key="${escapeAttr(rec.key)}"`;
|
|
459
|
+
const isl = rec.def.island;
|
|
460
|
+
const island = isl ? ` data-weft-island="${escapeAttr(typeof isl === "string" ? isl : rec.def.name)}"` : "";
|
|
461
|
+
return `<weft-c data-weft-c="${rec.id}"${key}${island}>${this.inner(rec, keep)}</weft-c>`;
|
|
462
|
+
}
|
|
463
|
+
markSent(rec) {
|
|
464
|
+
rec.sent = rec.template;
|
|
465
|
+
for (const c of rec.children.values()) {
|
|
466
|
+
this.markSent(c);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
collectPatches(rec, ack, out) {
|
|
470
|
+
if (rec.template !== rec.sent) {
|
|
471
|
+
out.push(ack === undefined
|
|
472
|
+
? { t: "patch", id: rec.id, html: this.inner(rec, true) }
|
|
473
|
+
: { t: "patch", id: rec.id, html: this.inner(rec, true), ack });
|
|
474
|
+
this.markSent(rec);
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
for (const c of rec.children.values()) {
|
|
478
|
+
this.collectPatches(c, ack, out);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
import { existsSync } from "node:fs";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { pathToFileURL } from "node:url";
|
|
12
|
+
import { ConfigError, parseConfig } from "./config.js";
|
|
13
|
+
export async function loadConfig(root, cacheKey = "") {
|
|
14
|
+
const ts = join(root, "weft.config.ts");
|
|
15
|
+
const js = join(root, "weft.config.js");
|
|
16
|
+
const tsExists = existsSync(ts);
|
|
17
|
+
const jsExists = existsSync(js);
|
|
18
|
+
if (tsExists && jsExists) {
|
|
19
|
+
throw new ConfigError("weft.config.ts: both weft.config.ts and weft.config.js exist");
|
|
20
|
+
}
|
|
21
|
+
const file = tsExists ? ts : jsExists ? js : null;
|
|
22
|
+
if (!file) {
|
|
23
|
+
return {};
|
|
24
|
+
}
|
|
25
|
+
const href = pathToFileURL(file).href + (cacheKey ? `?v=${encodeURIComponent(cacheKey)}` : "");
|
|
26
|
+
let mod;
|
|
27
|
+
try {
|
|
28
|
+
mod = (await import(__rewriteRelativeImportExtension(href)));
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
if (err instanceof ConfigError) {
|
|
32
|
+
throw err;
|
|
33
|
+
}
|
|
34
|
+
const message = err instanceof Error ? err.message : "error";
|
|
35
|
+
throw new ConfigError(`weft.config.ts: cannot load (${message})`);
|
|
36
|
+
}
|
|
37
|
+
if (!("default" in mod)) {
|
|
38
|
+
throw new ConfigError("weft.config.ts: export a default object");
|
|
39
|
+
}
|
|
40
|
+
return parseConfig(mod.default);
|
|
41
|
+
}
|
package/dist/log.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export type LogLevel = "debug" | "info" | "warn" | "error";
|
|
2
|
+
export interface LoggerOptions {
|
|
3
|
+
level?: LogLevel;
|
|
4
|
+
write?(line: string, level: LogLevel): void;
|
|
5
|
+
redactKeys?: RegExp;
|
|
6
|
+
base?: Record<string, unknown>;
|
|
7
|
+
now?(): number;
|
|
8
|
+
}
|
|
9
|
+
export interface Logger {
|
|
10
|
+
readonly level: LogLevel;
|
|
11
|
+
debug(msg: string, fields?: Record<string, unknown>): void;
|
|
12
|
+
info(msg: string, fields?: Record<string, unknown>): void;
|
|
13
|
+
warn(msg: string, fields?: Record<string, unknown>): void;
|
|
14
|
+
error(msg: string, fields?: Record<string, unknown>): void;
|
|
15
|
+
child(fields: Record<string, unknown>): Logger;
|
|
16
|
+
}
|
|
17
|
+
export declare function redact(value: unknown, keys?: RegExp): unknown;
|
|
18
|
+
export declare function createLogger(o?: LoggerOptions): Logger;
|
|
19
|
+
export declare function assignRequestId(req: Request, trustIncoming: boolean): string;
|
|
20
|
+
export declare function requestIdOf(req: Request): string | null;
|