@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/shared.js
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { rowSource } from "@weft-framework/data";
|
|
2
|
+
import { applyEdit, diffText, moveIndex, transform } from "@weft-framework/runtime";
|
|
3
|
+
const LOG = 200;
|
|
4
|
+
const CURSOR_MS = 50;
|
|
5
|
+
const KNOWN = 10_000;
|
|
6
|
+
const known = new Map();
|
|
7
|
+
const docs = new Map();
|
|
8
|
+
const docName = (model, field, key) => "s." + Buffer.from(JSON.stringify([model, field, key]), "utf8").toString("base64url");
|
|
9
|
+
const sameKey = (a, b) => a === b || JSON.stringify(a) === JSON.stringify(b);
|
|
10
|
+
function isTextField(model, field) {
|
|
11
|
+
const kind = model.config.fields[field];
|
|
12
|
+
return typeof kind === "string" && /^string(\s*\|\s*null)?$/.test(kind.trim()) && field !== model.key;
|
|
13
|
+
}
|
|
14
|
+
function render(row, field) {
|
|
15
|
+
const src = rowSource(row);
|
|
16
|
+
if (!src || typeof field !== "string")
|
|
17
|
+
return null;
|
|
18
|
+
const model = src.by.model;
|
|
19
|
+
if (!model || !isTextField(model, field))
|
|
20
|
+
return null;
|
|
21
|
+
const id = docName(model.name, field, src.key);
|
|
22
|
+
if (id.length > 512)
|
|
23
|
+
return null;
|
|
24
|
+
known.delete(id);
|
|
25
|
+
known.set(id, { model, key: src.key, field });
|
|
26
|
+
if (known.size > KNOWN)
|
|
27
|
+
known.delete(known.keys().next().value);
|
|
28
|
+
const d = docs.get(id);
|
|
29
|
+
if (d)
|
|
30
|
+
return { doc: id, rev: d.rev, text: d.text };
|
|
31
|
+
const v = row[field];
|
|
32
|
+
return { doc: id, rev: 0, text: typeof v === "string" ? v : "" };
|
|
33
|
+
}
|
|
34
|
+
globalThis[Symbol.for("weft.shared")] = { render };
|
|
35
|
+
function scoped(d, actor) {
|
|
36
|
+
return d.model.as(actor);
|
|
37
|
+
}
|
|
38
|
+
function readable(d, actor) {
|
|
39
|
+
const row = d.model.as(actor).get(d.key);
|
|
40
|
+
if (row === undefined)
|
|
41
|
+
return false;
|
|
42
|
+
const rule = d.model.fieldReads.find(([f]) => f === d.field)?.[1];
|
|
43
|
+
if (!rule)
|
|
44
|
+
return true;
|
|
45
|
+
const raw = d.model.syncDriver().select([], d.key).find((r) => sameKey(r[d.model.key], d.key));
|
|
46
|
+
try {
|
|
47
|
+
return raw !== undefined && rule(actor, raw) === true;
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function stored(d) {
|
|
54
|
+
const row = d.model.syncDriver().select([], d.key).find((r) => sameKey(r[d.model.key], d.key));
|
|
55
|
+
if (!row)
|
|
56
|
+
return undefined;
|
|
57
|
+
const v = row[d.field];
|
|
58
|
+
return typeof v === "string" ? v : "";
|
|
59
|
+
}
|
|
60
|
+
let counter = 0;
|
|
61
|
+
const memberId = () => `m${(++counter).toString(36)}${Math.random().toString(36).slice(2, 6)}`;
|
|
62
|
+
function state(d, m, refused) {
|
|
63
|
+
const writable = scoped(d, m.actor).allows("write", d.key);
|
|
64
|
+
const msg = { t: "text", doc: d.id, rev: d.rev, text: d.text, writable };
|
|
65
|
+
if (refused)
|
|
66
|
+
msg.refused = refused;
|
|
67
|
+
m.send(msg);
|
|
68
|
+
}
|
|
69
|
+
function take(d, edit, from) {
|
|
70
|
+
d.text = applyEdit(d.text, edit);
|
|
71
|
+
d.rev += 1;
|
|
72
|
+
d.log.push({ rev: d.rev, edit });
|
|
73
|
+
if (d.log.length > LOG)
|
|
74
|
+
d.log.shift();
|
|
75
|
+
for (const m of d.members.values()) {
|
|
76
|
+
if (m.sel)
|
|
77
|
+
m.sel = [moveIndex(m.sel[0], edit, m === from), moveIndex(m.sel[1], edit, m === from)];
|
|
78
|
+
m.send(m === from ? { t: "text", doc: d.id, rev: d.rev, mine: true } : { t: "text", doc: d.id, rev: d.rev, edit });
|
|
79
|
+
}
|
|
80
|
+
cursorsSoon(d);
|
|
81
|
+
}
|
|
82
|
+
function cursorsSoon(d) {
|
|
83
|
+
if (d.cursorTimer !== null)
|
|
84
|
+
return;
|
|
85
|
+
d.cursorTimer = setTimeout(() => {
|
|
86
|
+
d.cursorTimer = null;
|
|
87
|
+
for (const [entry, m] of d.members) {
|
|
88
|
+
const cursors = [];
|
|
89
|
+
for (const [other, o] of d.members) {
|
|
90
|
+
if (other === entry || !o.sel)
|
|
91
|
+
continue;
|
|
92
|
+
cursors.push(o.label === undefined ? { id: o.id, a: o.sel[0], h: o.sel[1] } : { id: o.id, label: o.label, a: o.sel[0], h: o.sel[1] });
|
|
93
|
+
}
|
|
94
|
+
m.send({ t: "text", doc: d.id, rev: d.rev, cursors });
|
|
95
|
+
}
|
|
96
|
+
}, CURSOR_MS);
|
|
97
|
+
d.cursorTimer.unref?.();
|
|
98
|
+
}
|
|
99
|
+
function saveSoon(d) {
|
|
100
|
+
if (d.saveTimer !== null)
|
|
101
|
+
clearTimeout(d.saveTimer);
|
|
102
|
+
d.saveTimer = setTimeout(() => save(d), d.saveMs);
|
|
103
|
+
d.saveTimer.unref?.();
|
|
104
|
+
}
|
|
105
|
+
function save(d) {
|
|
106
|
+
if (d.saveTimer !== null)
|
|
107
|
+
clearTimeout(d.saveTimer);
|
|
108
|
+
d.saveTimer = null;
|
|
109
|
+
if (d.text === d.saved)
|
|
110
|
+
return;
|
|
111
|
+
const before = d.saved;
|
|
112
|
+
const text = d.text;
|
|
113
|
+
d.saved = text;
|
|
114
|
+
let ok = false;
|
|
115
|
+
try {
|
|
116
|
+
const handle = d.model.as(d.saveAs);
|
|
117
|
+
ok = handle.query().where({ [d.model.key]: d.key }).update({ [d.field]: text }) === 1;
|
|
118
|
+
}
|
|
119
|
+
catch (err) {
|
|
120
|
+
d.onError(err);
|
|
121
|
+
}
|
|
122
|
+
if (ok)
|
|
123
|
+
return;
|
|
124
|
+
d.saved = before;
|
|
125
|
+
const now = stored(d);
|
|
126
|
+
if (now === undefined)
|
|
127
|
+
return gone(d);
|
|
128
|
+
d.saved = now;
|
|
129
|
+
const back = diffText(d.text, now);
|
|
130
|
+
if (back.length)
|
|
131
|
+
take(d, back, null);
|
|
132
|
+
for (const m of d.members.values())
|
|
133
|
+
state(d, m, "The change could not be saved; this is the saved text.");
|
|
134
|
+
}
|
|
135
|
+
function gone(d) {
|
|
136
|
+
for (const m of d.members.values())
|
|
137
|
+
m.send({ t: "text", doc: d.id, rev: d.rev, text: "", writable: false, refused: "This row is gone." });
|
|
138
|
+
drop(d);
|
|
139
|
+
}
|
|
140
|
+
function drop(d) {
|
|
141
|
+
if (d.saveTimer !== null)
|
|
142
|
+
clearTimeout(d.saveTimer);
|
|
143
|
+
if (d.cursorTimer !== null)
|
|
144
|
+
clearTimeout(d.cursorTimer);
|
|
145
|
+
docs.delete(d.id);
|
|
146
|
+
}
|
|
147
|
+
export function sharedMessage(entry, actor, msg, send, opts) {
|
|
148
|
+
let d = docs.get(msg.doc);
|
|
149
|
+
if (msg.join) {
|
|
150
|
+
const where = d ?? known.get(msg.doc);
|
|
151
|
+
if (!where)
|
|
152
|
+
return;
|
|
153
|
+
if (!readable(where, actor)) {
|
|
154
|
+
send({ t: "text", doc: msg.doc, rev: 0, text: "", writable: false, refused: "You cannot read this field." });
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (!d) {
|
|
158
|
+
const w = known.get(msg.doc);
|
|
159
|
+
d = {
|
|
160
|
+
id: msg.doc,
|
|
161
|
+
model: w.model,
|
|
162
|
+
key: w.key,
|
|
163
|
+
field: w.field,
|
|
164
|
+
text: "",
|
|
165
|
+
rev: 0,
|
|
166
|
+
saved: "",
|
|
167
|
+
log: [],
|
|
168
|
+
members: new Map(),
|
|
169
|
+
saveAs: actor,
|
|
170
|
+
saveTimer: null,
|
|
171
|
+
cursorTimer: null,
|
|
172
|
+
saveMs: Math.max(50, opts.saveMs ?? 800),
|
|
173
|
+
maxLength: opts.maxLength ?? 100_000,
|
|
174
|
+
onError: opts.onError,
|
|
175
|
+
};
|
|
176
|
+
const now = stored(d);
|
|
177
|
+
if (now === undefined)
|
|
178
|
+
return send({ t: "text", doc: msg.doc, rev: 0, text: "", writable: false, refused: "This row is gone." });
|
|
179
|
+
d.text = d.saved = now;
|
|
180
|
+
docs.set(d.id, d);
|
|
181
|
+
}
|
|
182
|
+
const label = opts.label?.(actor);
|
|
183
|
+
const m = { id: d.members.get(entry)?.id ?? memberId(), sel: null, send, actor };
|
|
184
|
+
if (typeof label === "string" && label !== "")
|
|
185
|
+
m.label = label.slice(0, 200);
|
|
186
|
+
d.members.set(entry, m);
|
|
187
|
+
state(d, m);
|
|
188
|
+
cursorsSoon(d);
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
const m = d?.members.get(entry);
|
|
192
|
+
if (!d || !m)
|
|
193
|
+
return;
|
|
194
|
+
m.actor = actor;
|
|
195
|
+
if (msg.sel) {
|
|
196
|
+
let [a, h] = msg.sel;
|
|
197
|
+
if (msg.rev < d.rev - d.log.length || msg.rev > d.rev)
|
|
198
|
+
return;
|
|
199
|
+
for (const e of d.log) {
|
|
200
|
+
if (e.rev > msg.rev) {
|
|
201
|
+
a = moveIndex(a, e.edit);
|
|
202
|
+
h = moveIndex(h, e.edit);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
m.sel = [Math.min(a, d.text.length), Math.min(h, d.text.length)];
|
|
206
|
+
cursorsSoon(d);
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
if (!msg.edit)
|
|
210
|
+
return;
|
|
211
|
+
if (!scoped(d, actor).allows("write", d.key))
|
|
212
|
+
return state(d, m, "You may not change this field.");
|
|
213
|
+
if (msg.rev > d.rev || (msg.rev < d.rev && (d.log.length === 0 || d.log[0].rev > msg.rev + 1)))
|
|
214
|
+
return state(d, m);
|
|
215
|
+
let edit = msg.edit;
|
|
216
|
+
for (const e of d.log)
|
|
217
|
+
if (e.rev > msg.rev)
|
|
218
|
+
edit = transform(edit, e.edit, false)[0];
|
|
219
|
+
let next;
|
|
220
|
+
try {
|
|
221
|
+
next = applyEdit(d.text, edit);
|
|
222
|
+
}
|
|
223
|
+
catch {
|
|
224
|
+
return state(d, m);
|
|
225
|
+
}
|
|
226
|
+
if (next.length > d.maxLength)
|
|
227
|
+
return state(d, m, `The text is longer than ${d.maxLength} characters.`);
|
|
228
|
+
take(d, edit, m);
|
|
229
|
+
d.saveAs = actor;
|
|
230
|
+
saveSoon(d);
|
|
231
|
+
}
|
|
232
|
+
export function sharedLeave(entry) {
|
|
233
|
+
for (const d of [...docs.values()]) {
|
|
234
|
+
if (!d.members.delete(entry))
|
|
235
|
+
continue;
|
|
236
|
+
if (d.members.size === 0) {
|
|
237
|
+
save(d);
|
|
238
|
+
drop(d);
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
cursorsSoon(d);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
export function sharedChange(change) {
|
|
246
|
+
for (const d of [...docs.values()]) {
|
|
247
|
+
if (d.model.name !== change.model)
|
|
248
|
+
continue;
|
|
249
|
+
if (!change.redacted && !sameKey(change.key, d.key))
|
|
250
|
+
continue;
|
|
251
|
+
if (change.op === "delete" && !change.redacted) {
|
|
252
|
+
if (stored(d) === undefined)
|
|
253
|
+
gone(d);
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
const after = change.redacted ? stored(d) : (change.after?.[d.field] ?? "");
|
|
257
|
+
if (after === undefined) {
|
|
258
|
+
gone(d);
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
if (typeof after !== "string" || after === d.saved)
|
|
262
|
+
continue;
|
|
263
|
+
const theirs = diffText(d.saved, after);
|
|
264
|
+
const ours = diffText(d.saved, d.text);
|
|
265
|
+
const [merged] = transform(theirs, ours, false);
|
|
266
|
+
d.saved = after;
|
|
267
|
+
if (merged.length)
|
|
268
|
+
take(d, merged, null);
|
|
269
|
+
for (const [entry, m] of d.members) {
|
|
270
|
+
if (!readable(d, m.actor)) {
|
|
271
|
+
m.send({ t: "text", doc: d.id, rev: d.rev, text: "", writable: false, refused: "You cannot read this field." });
|
|
272
|
+
d.members.delete(entry);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (d.text !== d.saved)
|
|
276
|
+
saveSoon(d);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
export function resetShared() {
|
|
280
|
+
for (const d of [...docs.values()])
|
|
281
|
+
drop(d);
|
|
282
|
+
known.clear();
|
|
283
|
+
}
|
package/dist/shell.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare const DEFAULT_SHELL = "<!doctype html>\n<html lang=\"%lang%\">\n <head>\n <meta charset=\"utf-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n <!--weft:head-->\n </head>\n <body>\n <!--weft:body-->\n </body>\n</html>\n";
|
|
2
|
+
export interface Shell {
|
|
3
|
+
render(parts: {
|
|
4
|
+
lang: string;
|
|
5
|
+
head: string;
|
|
6
|
+
body: string;
|
|
7
|
+
nonce: string;
|
|
8
|
+
html?: string;
|
|
9
|
+
}): string;
|
|
10
|
+
}
|
|
11
|
+
export declare function htmlAttributeMarkup(attrs: Record<string, string | boolean | null | undefined> | undefined): string;
|
|
12
|
+
export declare function parseShell(source?: string): Shell;
|
package/dist/shell.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { escapeAttr } from "@weft-framework/runtime";
|
|
2
|
+
export const DEFAULT_SHELL = `<!doctype html>
|
|
3
|
+
<html lang="%lang%">
|
|
4
|
+
<head>
|
|
5
|
+
<meta charset="utf-8">
|
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
7
|
+
<!--weft:head-->
|
|
8
|
+
</head>
|
|
9
|
+
<body>
|
|
10
|
+
<!--weft:body-->
|
|
11
|
+
</body>
|
|
12
|
+
</html>
|
|
13
|
+
`;
|
|
14
|
+
const HEAD = "<!--weft:head-->";
|
|
15
|
+
const BODY = "<!--weft:body-->";
|
|
16
|
+
const NONCE = "\u0000weft-nonce\u0000";
|
|
17
|
+
export function htmlAttributeMarkup(attrs) {
|
|
18
|
+
let out = "";
|
|
19
|
+
for (const [name, value] of Object.entries(attrs ?? {})) {
|
|
20
|
+
if (!/^[a-z][a-z0-9-]*$/.test(name) || name.startsWith("on")) {
|
|
21
|
+
throw new Error(`htmlAttributes: refused attribute name ${JSON.stringify(name)}`);
|
|
22
|
+
}
|
|
23
|
+
if (name === "lang")
|
|
24
|
+
continue;
|
|
25
|
+
if (value === true)
|
|
26
|
+
out += ` ${name}`;
|
|
27
|
+
else if (typeof value === "string")
|
|
28
|
+
out += ` ${name}="${escapeAttr(value)}"`;
|
|
29
|
+
}
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
export function parseShell(source = DEFAULT_SHELL) {
|
|
33
|
+
for (const marker of [HEAD, BODY]) {
|
|
34
|
+
const first = source.indexOf(marker);
|
|
35
|
+
if (first < 0) {
|
|
36
|
+
throw new Error(`index.html: missing ${marker}`);
|
|
37
|
+
}
|
|
38
|
+
if (source.indexOf(marker, first + 1) >= 0) {
|
|
39
|
+
throw new Error(`index.html: ${marker} appears more than once`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (source.indexOf(HEAD) > source.indexOf(BODY)) {
|
|
43
|
+
throw new Error(`index.html: ${HEAD} must come before ${BODY}`);
|
|
44
|
+
}
|
|
45
|
+
if (tagHeads(source).some(hasInlineHandler)) {
|
|
46
|
+
throw new Error("index.html: inline event handler attributes are blocked by the CSP; use a <script>");
|
|
47
|
+
}
|
|
48
|
+
if (tagHeads(source).some(hasNonce)) {
|
|
49
|
+
throw new Error("index.html: nonces are added per response; remove the nonce attribute");
|
|
50
|
+
}
|
|
51
|
+
const withNonce = source.replace(/<(script|style)\b/gi, (m) => `${m} nonce="${NONCE}"`);
|
|
52
|
+
const [beforeHead, rest] = splitOnce(withNonce, HEAD);
|
|
53
|
+
const [between, afterBody] = splitOnce(rest, BODY);
|
|
54
|
+
const htmlTag = /<html\b/i.exec(beforeHead);
|
|
55
|
+
const at = htmlTag ? htmlTag.index + htmlTag[0].length : -1;
|
|
56
|
+
return {
|
|
57
|
+
render({ lang, head, body, nonce, html = "" }) {
|
|
58
|
+
const fill = (s) => s.split(NONCE).join(escapeAttr(nonce)).split("%lang%").join(escapeAttr(lang));
|
|
59
|
+
const before = html && at >= 0 ? beforeHead.slice(0, at) + html + beforeHead.slice(at) : beforeHead;
|
|
60
|
+
return fill(before) + head + fill(between) + body + fill(afterBody);
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function tagHeads(source) {
|
|
65
|
+
const out = [];
|
|
66
|
+
let i = 0;
|
|
67
|
+
while (i < source.length) {
|
|
68
|
+
const lt = source.indexOf("<", i);
|
|
69
|
+
if (lt < 0)
|
|
70
|
+
break;
|
|
71
|
+
const gt = source.indexOf(">", lt + 1);
|
|
72
|
+
if (gt < 0) {
|
|
73
|
+
out.push(source.slice(lt + 1).toLowerCase());
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
out.push(source.slice(lt + 1, gt).toLowerCase());
|
|
77
|
+
i = gt + 1;
|
|
78
|
+
}
|
|
79
|
+
return out;
|
|
80
|
+
}
|
|
81
|
+
function hasInlineHandler(head) {
|
|
82
|
+
let p = 0;
|
|
83
|
+
while (p < head.length) {
|
|
84
|
+
const on = head.indexOf(" on", p);
|
|
85
|
+
if (on < 0)
|
|
86
|
+
return false;
|
|
87
|
+
let n = on + 3;
|
|
88
|
+
if (n >= head.length || head.charCodeAt(n) < 97 || head.charCodeAt(n) > 122) {
|
|
89
|
+
p = n;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
while (n < head.length && head.charCodeAt(n) >= 97 && head.charCodeAt(n) <= 122)
|
|
93
|
+
n++;
|
|
94
|
+
while (n < head.length && head[n] === " ")
|
|
95
|
+
n++;
|
|
96
|
+
if (head[n] === "=")
|
|
97
|
+
return true;
|
|
98
|
+
p = n;
|
|
99
|
+
}
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
function hasNonce(head) {
|
|
103
|
+
const nameEnd = head.search(/[\s/]/);
|
|
104
|
+
const name = (nameEnd < 0 ? head : head.slice(0, nameEnd)).trim();
|
|
105
|
+
if (name !== "script" && name !== "style")
|
|
106
|
+
return false;
|
|
107
|
+
const nonce = head.indexOf(" nonce");
|
|
108
|
+
if (nonce < 0)
|
|
109
|
+
return false;
|
|
110
|
+
let n = nonce + 6;
|
|
111
|
+
while (n < head.length && head[n] === " ")
|
|
112
|
+
n++;
|
|
113
|
+
return head[n] === "=";
|
|
114
|
+
}
|
|
115
|
+
function splitOnce(s, sep) {
|
|
116
|
+
const i = s.indexOf(sep);
|
|
117
|
+
return [s.slice(0, i), s.slice(i + sep.length)];
|
|
118
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Model } from "@weft-framework/data";
|
|
2
|
+
export interface StorageOptions {
|
|
3
|
+
database?: string;
|
|
4
|
+
onWarning?(message: string): void;
|
|
5
|
+
}
|
|
6
|
+
export declare class ModelStorage {
|
|
7
|
+
private db;
|
|
8
|
+
private dbPath;
|
|
9
|
+
private memory;
|
|
10
|
+
attach(models: readonly Model<unknown>[], opts?: StorageOptions): Promise<void>;
|
|
11
|
+
close(): void;
|
|
12
|
+
private closeDb;
|
|
13
|
+
}
|
package/dist/storage.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { mkdirSync } from "node:fs";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import { useDatabase } from "@weft-framework/data";
|
|
4
|
+
function signature(m) {
|
|
5
|
+
return JSON.stringify([m.key, m.fields]);
|
|
6
|
+
}
|
|
7
|
+
export class ModelStorage {
|
|
8
|
+
db = null;
|
|
9
|
+
dbPath = null;
|
|
10
|
+
memory = new Map();
|
|
11
|
+
async attach(models, opts = {}) {
|
|
12
|
+
if (opts.database !== undefined) {
|
|
13
|
+
if (this.dbPath !== opts.database) {
|
|
14
|
+
const { openSqlite } = await import("@weft-framework/data/sqlite");
|
|
15
|
+
if (opts.database !== ":memory:") {
|
|
16
|
+
mkdirSync(dirname(opts.database), { recursive: true });
|
|
17
|
+
}
|
|
18
|
+
const next = openSqlite(opts.database);
|
|
19
|
+
this.closeDb();
|
|
20
|
+
this.db = next;
|
|
21
|
+
this.dbPath = opts.database;
|
|
22
|
+
}
|
|
23
|
+
this.memory.clear();
|
|
24
|
+
useDatabase(this.db, models, { onWarning: opts.onWarning });
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
this.closeDb();
|
|
28
|
+
const seen = new Set();
|
|
29
|
+
for (const m of models) {
|
|
30
|
+
const sig = signature(m);
|
|
31
|
+
const prev = this.memory.get(m.name);
|
|
32
|
+
if (prev && prev.sig === sig) {
|
|
33
|
+
m.use(prev.driver);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
this.memory.set(m.name, { sig, driver: m.attached });
|
|
37
|
+
}
|
|
38
|
+
seen.add(m.name);
|
|
39
|
+
}
|
|
40
|
+
for (const name of [...this.memory.keys()]) {
|
|
41
|
+
if (!seen.has(name)) {
|
|
42
|
+
this.memory.delete(name);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
close() {
|
|
47
|
+
this.closeDb();
|
|
48
|
+
this.memory.clear();
|
|
49
|
+
}
|
|
50
|
+
closeDb() {
|
|
51
|
+
this.db?.close();
|
|
52
|
+
this.db = null;
|
|
53
|
+
this.dbPath = null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { type Model } from "@weft-framework/data";
|
|
2
|
+
import { type Action } from "./action.js";
|
|
3
|
+
import { type Viewing } from "./auth.js";
|
|
4
|
+
import type { PageContext } from "./context.js";
|
|
5
|
+
type Maybe<T> = T | Promise<T>;
|
|
6
|
+
export interface SupportMatch {
|
|
7
|
+
id: string;
|
|
8
|
+
label: string;
|
|
9
|
+
detail?: string;
|
|
10
|
+
}
|
|
11
|
+
export type SupportEvent = "lookup" | "open" | "view-start" | "view-stop" | "consent-grant" | "consent-revoke";
|
|
12
|
+
export interface SupportLogEntry {
|
|
13
|
+
id: string;
|
|
14
|
+
event: SupportEvent;
|
|
15
|
+
user: string | null;
|
|
16
|
+
operator: string | null;
|
|
17
|
+
at: number;
|
|
18
|
+
detail: string | null;
|
|
19
|
+
}
|
|
20
|
+
export interface SupportCustomerView<C> {
|
|
21
|
+
id: string;
|
|
22
|
+
customer: C;
|
|
23
|
+
consent: number | null;
|
|
24
|
+
viewable: boolean;
|
|
25
|
+
log: SupportLogEntry[];
|
|
26
|
+
}
|
|
27
|
+
export type StartViewResult = {
|
|
28
|
+
ok: true;
|
|
29
|
+
until: number;
|
|
30
|
+
} | {
|
|
31
|
+
ok: false;
|
|
32
|
+
reason: "not found" | "no consent" | "bad reason" | "bad minutes" | "bad tenant" | "refused";
|
|
33
|
+
};
|
|
34
|
+
export interface SupportOptions<C extends object> {
|
|
35
|
+
allow(operator: string): Maybe<boolean>;
|
|
36
|
+
lookup(query: string, ctx: PageContext): Maybe<readonly SupportMatch[]>;
|
|
37
|
+
describe(id: string, ctx: PageContext): Maybe<(C & {
|
|
38
|
+
tenants?: readonly string[];
|
|
39
|
+
}) | null>;
|
|
40
|
+
userId?(ctx: PageContext): string | null;
|
|
41
|
+
consent?: boolean;
|
|
42
|
+
maxConsentMinutes?: number;
|
|
43
|
+
maxViewMinutes?: number;
|
|
44
|
+
maxResults?: number;
|
|
45
|
+
name?: string;
|
|
46
|
+
now?(): number;
|
|
47
|
+
}
|
|
48
|
+
export interface Support<C extends object> {
|
|
49
|
+
models: readonly Model<never>[];
|
|
50
|
+
viewAs(operator: string, user: string): Promise<boolean>;
|
|
51
|
+
operator(ctx: PageContext): Promise<string | null>;
|
|
52
|
+
search(ctx: PageContext, query: string): Promise<{
|
|
53
|
+
query: string;
|
|
54
|
+
results: SupportMatch[];
|
|
55
|
+
more: boolean;
|
|
56
|
+
} | null>;
|
|
57
|
+
customer(ctx: PageContext, id: string): Promise<SupportCustomerView<C> | null>;
|
|
58
|
+
startView(ctx: PageContext, o: {
|
|
59
|
+
user: string;
|
|
60
|
+
minutes: number;
|
|
61
|
+
reason: string;
|
|
62
|
+
tenant?: string;
|
|
63
|
+
}): Promise<StartViewResult | null>;
|
|
64
|
+
stopView(ctx: PageContext): Promise<boolean>;
|
|
65
|
+
viewing(ctx: PageContext): Viewing | null;
|
|
66
|
+
myAccess(ctx: PageContext): {
|
|
67
|
+
user: string;
|
|
68
|
+
consent: number | null;
|
|
69
|
+
log: SupportLogEntry[];
|
|
70
|
+
maxMinutes: number;
|
|
71
|
+
} | null;
|
|
72
|
+
grant(ctx: PageContext, minutes: number): number | null;
|
|
73
|
+
revoke(ctx: PageContext): boolean;
|
|
74
|
+
consentUntil(user: string): number | null;
|
|
75
|
+
log(o?: {
|
|
76
|
+
user?: string;
|
|
77
|
+
operator?: string;
|
|
78
|
+
limit?: number;
|
|
79
|
+
}): SupportLogEntry[];
|
|
80
|
+
actions: Record<string, Action<never[]>>;
|
|
81
|
+
}
|
|
82
|
+
export declare function defineSupport<C extends object = Record<string, unknown>>(opts: SupportOptions<C>): Support<C>;
|
|
83
|
+
export {};
|