@weft-framework/server 0.0.0-stage → 0.0.1
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/support.js
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { defineModel } from "@weft-framework/data";
|
|
2
|
+
import { defineAction } from "./action.js";
|
|
3
|
+
import { allowWhileViewing, stopViewingAs, viewAs, viewingAs } from "./auth.js";
|
|
4
|
+
const MAX_ID = 200;
|
|
5
|
+
const MAX_QUERY = 200;
|
|
6
|
+
const MAX_REASON = 500;
|
|
7
|
+
const LOG_LIMIT = 1000;
|
|
8
|
+
const KEEPER = Object.freeze({ id: "\u0000weft-support" });
|
|
9
|
+
export function defineSupport(opts) {
|
|
10
|
+
const fail = (msg) => {
|
|
11
|
+
throw new Error(`defineSupport: ${msg}`);
|
|
12
|
+
};
|
|
13
|
+
if (!opts || typeof opts.allow !== "function")
|
|
14
|
+
fail("`allow(operator)` decides who is an operator");
|
|
15
|
+
if (typeof opts.lookup !== "function" || typeof opts.describe !== "function")
|
|
16
|
+
fail("`lookup(query)` and `describe(id)` are required");
|
|
17
|
+
for (const k of ["userId", "now"]) {
|
|
18
|
+
if (opts[k] !== undefined && typeof opts[k] !== "function")
|
|
19
|
+
fail(`${k} is a function`);
|
|
20
|
+
}
|
|
21
|
+
const prefix = opts.name ?? "support";
|
|
22
|
+
if (!/^[a-z][A-Za-z0-9]{0,40}$/.test(prefix))
|
|
23
|
+
fail("name is an identifier");
|
|
24
|
+
const whole = (v, dflt, max, what) => {
|
|
25
|
+
const n = v === undefined ? dflt : v;
|
|
26
|
+
if (typeof n !== "number" || !Number.isInteger(n) || n < 1 || n > max)
|
|
27
|
+
fail(`${what} is a whole number from 1 to ${max}`);
|
|
28
|
+
return n;
|
|
29
|
+
};
|
|
30
|
+
const maxConsent = whole(opts.maxConsentMinutes, 7 * 1440, 90 * 1440, "maxConsentMinutes");
|
|
31
|
+
const maxView = whole(opts.maxViewMinutes, 30, 24 * 60, "maxViewMinutes");
|
|
32
|
+
const maxResults = whole(opts.maxResults, 50, 500, "maxResults");
|
|
33
|
+
const needConsent = opts.consent !== false;
|
|
34
|
+
const now = opts.now ?? Date.now;
|
|
35
|
+
const Name = prefix.charAt(0).toUpperCase() + prefix.slice(1);
|
|
36
|
+
const Consent = defineModel(`${Name}Consent`, {
|
|
37
|
+
fields: { id: "string", until: "number", granted: "number" },
|
|
38
|
+
read: (a) => a === KEEPER,
|
|
39
|
+
write: (a) => a === KEEPER,
|
|
40
|
+
});
|
|
41
|
+
const Log = defineModel(`${Name}Log`, {
|
|
42
|
+
fields: { id: "string", event: "string", user: "string | null", operator: "string | null", at: "number", detail: "string | null" },
|
|
43
|
+
attrs: { user: { index: true }, operator: { index: true } },
|
|
44
|
+
read: (a) => a === KEEPER,
|
|
45
|
+
write: (a) => a === KEEPER,
|
|
46
|
+
});
|
|
47
|
+
let lastAt = 0;
|
|
48
|
+
let seq = 0;
|
|
49
|
+
const record = (event, user, operator, detail = null) => {
|
|
50
|
+
const t = now();
|
|
51
|
+
seq = t === lastAt ? seq + 1 : 0;
|
|
52
|
+
lastAt = t;
|
|
53
|
+
const id = `${t.toString(36).padStart(10, "0")}${seq.toString(36).padStart(4, "0")}${crypto.randomUUID().replaceAll("-", "").slice(0, 12)}`;
|
|
54
|
+
Log.as(KEEPER).create({ id, event, user, operator, at: t, detail });
|
|
55
|
+
};
|
|
56
|
+
const userIdOf = (ctx) => {
|
|
57
|
+
try {
|
|
58
|
+
const id = opts.userId ? opts.userId(ctx) : ctx.user?.id;
|
|
59
|
+
return typeof id === "string" && id !== "" ? id : null;
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
const isOperator = async (id) => {
|
|
66
|
+
if (id === null)
|
|
67
|
+
return false;
|
|
68
|
+
try {
|
|
69
|
+
return (await opts.allow(id)) === true;
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
const operator = async (ctx) => {
|
|
76
|
+
if (viewingAs(ctx))
|
|
77
|
+
return null;
|
|
78
|
+
const id = userIdOf(ctx);
|
|
79
|
+
return (await isOperator(id)) ? id : null;
|
|
80
|
+
};
|
|
81
|
+
const consentUntil = (user) => {
|
|
82
|
+
const row = Consent.as(KEEPER).get(user);
|
|
83
|
+
return row && row.until > now() ? row.until : null;
|
|
84
|
+
};
|
|
85
|
+
const log = (o = {}) => {
|
|
86
|
+
const limit = o.limit === undefined ? 100 : o.limit;
|
|
87
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > LOG_LIMIT)
|
|
88
|
+
throw new RangeError(`support.log: limit is from 1 to ${LOG_LIMIT}`);
|
|
89
|
+
let q = Log.as(KEEPER).query();
|
|
90
|
+
if (o.user !== undefined)
|
|
91
|
+
q = q.where({ user: o.user });
|
|
92
|
+
if (o.operator !== undefined)
|
|
93
|
+
q = q.where({ operator: o.operator });
|
|
94
|
+
return q
|
|
95
|
+
.orderBy("id", "desc")
|
|
96
|
+
.limit(limit)
|
|
97
|
+
.all()
|
|
98
|
+
.map((r) => ({ id: r.id, event: r.event, user: r.user, operator: r.operator, at: r.at, detail: r.detail }));
|
|
99
|
+
};
|
|
100
|
+
const describe = async (id, ctx) => {
|
|
101
|
+
if (typeof id !== "string" || id === "" || id.length > MAX_ID)
|
|
102
|
+
return null;
|
|
103
|
+
const c = await opts.describe(id, ctx);
|
|
104
|
+
return c && typeof c === "object" ? c : null;
|
|
105
|
+
};
|
|
106
|
+
const customerOf = (ctx) => (viewingAs(ctx) ? null : userIdOf(ctx));
|
|
107
|
+
const support = {
|
|
108
|
+
models: [Consent, Log],
|
|
109
|
+
async viewAs(op, user) {
|
|
110
|
+
if (op === user || !(await isOperator(op)))
|
|
111
|
+
return false;
|
|
112
|
+
return !needConsent || consentUntil(user) !== null;
|
|
113
|
+
},
|
|
114
|
+
operator,
|
|
115
|
+
async search(ctx, query) {
|
|
116
|
+
const op = await operator(ctx);
|
|
117
|
+
if (op === null)
|
|
118
|
+
return null;
|
|
119
|
+
const q = typeof query === "string" ? query.trim().slice(0, MAX_QUERY) : "";
|
|
120
|
+
if (q === "")
|
|
121
|
+
return { query: q, results: [], more: false };
|
|
122
|
+
const found = await opts.lookup(q, ctx);
|
|
123
|
+
const results = (Array.isArray(found) ? found : [])
|
|
124
|
+
.filter((m) => m && typeof m.id === "string" && m.id !== "" && m.id.length <= MAX_ID && typeof m.label === "string")
|
|
125
|
+
.slice(0, maxResults + 1);
|
|
126
|
+
record("lookup", null, op, q);
|
|
127
|
+
return { query: q, results: results.slice(0, maxResults), more: results.length > maxResults };
|
|
128
|
+
},
|
|
129
|
+
async customer(ctx, id) {
|
|
130
|
+
const op = await operator(ctx);
|
|
131
|
+
if (op === null)
|
|
132
|
+
return null;
|
|
133
|
+
const c = await describe(id, ctx);
|
|
134
|
+
if (!c)
|
|
135
|
+
return null;
|
|
136
|
+
record("open", id, op);
|
|
137
|
+
const consent = consentUntil(id);
|
|
138
|
+
return { id, customer: c, consent, viewable: !needConsent || consent !== null, log: log({ user: id, limit: 50 }) };
|
|
139
|
+
},
|
|
140
|
+
async startView(ctx, o) {
|
|
141
|
+
const op = await operator(ctx);
|
|
142
|
+
if (op === null)
|
|
143
|
+
return null;
|
|
144
|
+
const c = await describe(o?.user, ctx);
|
|
145
|
+
if (!c)
|
|
146
|
+
return { ok: false, reason: "not found" };
|
|
147
|
+
const why = typeof o.reason === "string" ? o.reason.trim() : "";
|
|
148
|
+
if (why === "" || why.length > MAX_REASON)
|
|
149
|
+
return { ok: false, reason: "bad reason" };
|
|
150
|
+
if (!Number.isInteger(o.minutes) || o.minutes < 1 || o.minutes > maxView)
|
|
151
|
+
return { ok: false, reason: "bad minutes" };
|
|
152
|
+
const tenants = Array.isArray(c.tenants) ? c.tenants : null;
|
|
153
|
+
let tenant;
|
|
154
|
+
if (tenants && tenants.length > 0) {
|
|
155
|
+
tenant = o.tenant === undefined || o.tenant === "" ? tenants[0] : o.tenant;
|
|
156
|
+
if (!tenants.includes(tenant))
|
|
157
|
+
return { ok: false, reason: "bad tenant" };
|
|
158
|
+
}
|
|
159
|
+
else if (o.tenant !== undefined && o.tenant !== "") {
|
|
160
|
+
return { ok: false, reason: "bad tenant" };
|
|
161
|
+
}
|
|
162
|
+
const consent = consentUntil(o.user);
|
|
163
|
+
if (needConsent && consent === null)
|
|
164
|
+
return { ok: false, reason: "no consent" };
|
|
165
|
+
let until = now() + o.minutes * 60_000;
|
|
166
|
+
if (needConsent && consent !== null)
|
|
167
|
+
until = Math.min(until, consent);
|
|
168
|
+
if (!(await viewAs(ctx, o.user, { until, ...(tenant !== undefined ? { tenant } : {}) })))
|
|
169
|
+
return { ok: false, reason: "refused" };
|
|
170
|
+
record("view-start", o.user, op, why);
|
|
171
|
+
return { ok: true, until };
|
|
172
|
+
},
|
|
173
|
+
async stopView(ctx) {
|
|
174
|
+
const v = viewingAs(ctx);
|
|
175
|
+
if (!v)
|
|
176
|
+
return false;
|
|
177
|
+
await stopViewingAs(ctx);
|
|
178
|
+
record("view-stop", v.user, v.operator);
|
|
179
|
+
return true;
|
|
180
|
+
},
|
|
181
|
+
viewing: viewingAs,
|
|
182
|
+
myAccess(ctx) {
|
|
183
|
+
const v = viewingAs(ctx);
|
|
184
|
+
const me = v ? v.user : userIdOf(ctx);
|
|
185
|
+
if (me === null)
|
|
186
|
+
return null;
|
|
187
|
+
return { user: me, consent: consentUntil(me), log: log({ user: me }).filter((e) => e.event !== "lookup"), maxMinutes: maxConsent };
|
|
188
|
+
},
|
|
189
|
+
grant(ctx, minutes) {
|
|
190
|
+
const me = customerOf(ctx);
|
|
191
|
+
if (me === null)
|
|
192
|
+
return null;
|
|
193
|
+
if (!Number.isInteger(minutes) || minutes < 1 || minutes > maxConsent) {
|
|
194
|
+
throw new RangeError(`support.grant: minutes is a whole number from 1 to ${maxConsent}`);
|
|
195
|
+
}
|
|
196
|
+
const t = now();
|
|
197
|
+
const until = t + minutes * 60_000;
|
|
198
|
+
const c = Consent.as(KEEPER);
|
|
199
|
+
if (c.get(me))
|
|
200
|
+
c.update((r) => r.id === me, { until, granted: t });
|
|
201
|
+
else
|
|
202
|
+
c.create({ id: me, until, granted: t });
|
|
203
|
+
record("consent-grant", me, null, String(minutes));
|
|
204
|
+
return until;
|
|
205
|
+
},
|
|
206
|
+
revoke(ctx) {
|
|
207
|
+
const me = customerOf(ctx);
|
|
208
|
+
if (me === null)
|
|
209
|
+
return false;
|
|
210
|
+
const had = consentUntil(me) !== null;
|
|
211
|
+
Consent.as(KEEPER).delete((r) => r.id === me);
|
|
212
|
+
if (had)
|
|
213
|
+
record("consent-revoke", me, null);
|
|
214
|
+
return had;
|
|
215
|
+
},
|
|
216
|
+
consentUntil,
|
|
217
|
+
log,
|
|
218
|
+
actions: {},
|
|
219
|
+
};
|
|
220
|
+
const minutes = (raw) => (/^\d{1,6}$/.test(raw.trim()) ? Number(raw.trim()) : Number.NaN);
|
|
221
|
+
support.actions = {
|
|
222
|
+
[`${prefix}StartView`]: defineAction(`${prefix}StartView`, [
|
|
223
|
+
{ name: "user", type: "string" },
|
|
224
|
+
{ name: "minutes", type: "string" },
|
|
225
|
+
{ name: "reason", type: "string" },
|
|
226
|
+
{ name: "tenant", type: "string", optional: true },
|
|
227
|
+
], (ctx, user, m, reason, tenant) => support.startView(ctx, { user, minutes: minutes(m), reason, ...(tenant !== undefined ? { tenant } : {}) })),
|
|
228
|
+
[`${prefix}StopView`]: allowWhileViewing(defineAction(`${prefix}StopView`, [], (ctx) => support.stopView(ctx))),
|
|
229
|
+
[`${prefix}Grant`]: defineAction(`${prefix}Grant`, [{ name: "minutes", type: "string" }], (ctx, m) => {
|
|
230
|
+
const n = minutes(m);
|
|
231
|
+
return Number.isInteger(n) && n >= 1 && n <= maxConsent ? support.grant(ctx, n) : null;
|
|
232
|
+
}),
|
|
233
|
+
[`${prefix}Revoke`]: defineAction(`${prefix}Revoke`, [], (ctx) => support.revoke(ctx)),
|
|
234
|
+
};
|
|
235
|
+
return support;
|
|
236
|
+
}
|
package/dist/sync.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { UNSCOPED, type Actor, type Change, type Model } from "@weft-framework/data";
|
|
2
|
+
import { type Store } from "@weft-framework/runtime";
|
|
3
|
+
export declare class DepsIndex<O> {
|
|
4
|
+
private readonly models;
|
|
5
|
+
private readonly storeReads;
|
|
6
|
+
deps(owner?: O | null): Deps<O>;
|
|
7
|
+
readersOf(name: string): Set<O>;
|
|
8
|
+
readersOfStore(store: Store<unknown>): Set<O>;
|
|
9
|
+
get size(): number;
|
|
10
|
+
join(deps: Deps<O>, model: string | null, store: Store<unknown> | null): void;
|
|
11
|
+
leave(deps: Deps<O>): void;
|
|
12
|
+
}
|
|
13
|
+
export declare class Deps<O = unknown> {
|
|
14
|
+
readonly reads: Map<string, {
|
|
15
|
+
model: Model<unknown>;
|
|
16
|
+
actors: Set<Actor | typeof UNSCOPED>;
|
|
17
|
+
keys: Set<unknown> | null;
|
|
18
|
+
}>;
|
|
19
|
+
readonly stores: Set<Store<unknown>>;
|
|
20
|
+
owner: O | null;
|
|
21
|
+
private readonly index;
|
|
22
|
+
private released;
|
|
23
|
+
constructor(index?: DepsIndex<O> | null, owner?: O | null);
|
|
24
|
+
renew(): Deps<O>;
|
|
25
|
+
adopt(owner: O): void;
|
|
26
|
+
release(): void;
|
|
27
|
+
addStore(store: Store<unknown>): void;
|
|
28
|
+
add(model: Model<unknown>, actor: Actor | typeof UNSCOPED, key?: unknown): void;
|
|
29
|
+
concerns(change: Change): boolean;
|
|
30
|
+
admits(change: Change): boolean;
|
|
31
|
+
}
|
|
32
|
+
export declare function withSession<R>(sid: string, fn: () => R): R;
|
|
33
|
+
export declare function currentSession(): string | undefined;
|
|
34
|
+
export declare function onStoreWrite(fn: (store: Store<unknown>, sid: string | undefined) => void): () => void;
|
|
35
|
+
export declare function track<R, O>(deps: Deps<O>, fn: () => R): R;
|
package/dist/sync.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import { canSee, onRead, UNSCOPED } from "@weft-framework/data";
|
|
3
|
+
import { configureStores } from "@weft-framework/runtime";
|
|
4
|
+
const MAX_ACTORS = 32;
|
|
5
|
+
const MAX_KEYS = 256;
|
|
6
|
+
export class DepsIndex {
|
|
7
|
+
models = new Map();
|
|
8
|
+
storeReads = new Map();
|
|
9
|
+
deps(owner = null) {
|
|
10
|
+
return new Deps(this, owner);
|
|
11
|
+
}
|
|
12
|
+
readersOf(name) {
|
|
13
|
+
return owners(this.models.get(name));
|
|
14
|
+
}
|
|
15
|
+
readersOfStore(store) {
|
|
16
|
+
return owners(this.storeReads.get(store));
|
|
17
|
+
}
|
|
18
|
+
get size() {
|
|
19
|
+
return this.models.size;
|
|
20
|
+
}
|
|
21
|
+
join(deps, model, store) {
|
|
22
|
+
const [map, k] = model !== null ? [this.models, model] : [this.storeReads, store];
|
|
23
|
+
let set = map.get(k);
|
|
24
|
+
if (!set) {
|
|
25
|
+
set = new Set();
|
|
26
|
+
map.set(k, set);
|
|
27
|
+
}
|
|
28
|
+
set.add(deps);
|
|
29
|
+
}
|
|
30
|
+
leave(deps) {
|
|
31
|
+
for (const name of deps.reads.keys()) {
|
|
32
|
+
const set = this.models.get(name);
|
|
33
|
+
set?.delete(deps);
|
|
34
|
+
if (set?.size === 0)
|
|
35
|
+
this.models.delete(name);
|
|
36
|
+
}
|
|
37
|
+
for (const store of deps.stores) {
|
|
38
|
+
const set = this.storeReads.get(store);
|
|
39
|
+
set?.delete(deps);
|
|
40
|
+
if (set?.size === 0)
|
|
41
|
+
this.storeReads.delete(store);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function owners(set) {
|
|
46
|
+
const out = new Set();
|
|
47
|
+
for (const d of set ?? []) {
|
|
48
|
+
if (d.owner !== null)
|
|
49
|
+
out.add(d.owner);
|
|
50
|
+
}
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
53
|
+
export class Deps {
|
|
54
|
+
reads = new Map();
|
|
55
|
+
stores = new Set();
|
|
56
|
+
owner;
|
|
57
|
+
index;
|
|
58
|
+
released = false;
|
|
59
|
+
constructor(index = null, owner = null) {
|
|
60
|
+
this.index = index;
|
|
61
|
+
this.owner = null;
|
|
62
|
+
if (owner !== null)
|
|
63
|
+
this.adopt(owner);
|
|
64
|
+
}
|
|
65
|
+
renew() {
|
|
66
|
+
this.release();
|
|
67
|
+
return new Deps(this.index, this.owner);
|
|
68
|
+
}
|
|
69
|
+
adopt(owner) {
|
|
70
|
+
this.owner = owner;
|
|
71
|
+
if (this.released || !this.index)
|
|
72
|
+
return;
|
|
73
|
+
for (const name of this.reads.keys())
|
|
74
|
+
this.index.join(this, name, null);
|
|
75
|
+
for (const store of this.stores)
|
|
76
|
+
this.index.join(this, null, store);
|
|
77
|
+
}
|
|
78
|
+
release() {
|
|
79
|
+
if (!this.released) {
|
|
80
|
+
this.released = true;
|
|
81
|
+
this.index?.leave(this);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
addStore(store) {
|
|
85
|
+
if (!this.stores.has(store)) {
|
|
86
|
+
this.stores.add(store);
|
|
87
|
+
if (this.owner !== null && !this.released)
|
|
88
|
+
this.index?.join(this, null, store);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
add(model, actor, key) {
|
|
92
|
+
let r = this.reads.get(model.name);
|
|
93
|
+
if (!r) {
|
|
94
|
+
r = { model, actors: new Set(), keys: key === undefined ? null : new Set() };
|
|
95
|
+
this.reads.set(model.name, r);
|
|
96
|
+
if (this.owner !== null && !this.released)
|
|
97
|
+
this.index?.join(this, model.name, null);
|
|
98
|
+
}
|
|
99
|
+
if (key === undefined || (typeof key !== "string" && typeof key !== "number"))
|
|
100
|
+
r.keys = null;
|
|
101
|
+
else if (r.keys !== null) {
|
|
102
|
+
r.keys.add(key);
|
|
103
|
+
if (r.keys.size > MAX_KEYS)
|
|
104
|
+
r.keys = null;
|
|
105
|
+
}
|
|
106
|
+
if (r.actors.has(UNSCOPED)) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (r.actors.size >= MAX_ACTORS) {
|
|
110
|
+
r.actors.clear();
|
|
111
|
+
r.actors.add(UNSCOPED);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
r.actors.add(actor);
|
|
115
|
+
}
|
|
116
|
+
concerns(change) {
|
|
117
|
+
const r = this.reads.get(change.model);
|
|
118
|
+
if (!r || (r.keys !== null && !r.keys.has(change.key))) {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
for (const actor of r.actors) {
|
|
122
|
+
if (canSee(r.model, actor, change)) {
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
admits(change) {
|
|
129
|
+
const r = this.reads.get(change.model);
|
|
130
|
+
if (!r || (r.keys !== null && !r.keys.has(change.key)))
|
|
131
|
+
return false;
|
|
132
|
+
for (const actor of r.actors) {
|
|
133
|
+
if (actor !== UNSCOPED && canSee(r.model, actor, change))
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const current = new AsyncLocalStorage();
|
|
140
|
+
const session = new AsyncLocalStorage();
|
|
141
|
+
const storeWatchers = new Set();
|
|
142
|
+
configureStores({
|
|
143
|
+
session: () => session.getStore(),
|
|
144
|
+
read: (store) => current.getStore()?.addStore(store),
|
|
145
|
+
write: (store, sid) => {
|
|
146
|
+
for (const w of storeWatchers) {
|
|
147
|
+
w(store, sid);
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
export function withSession(sid, fn) {
|
|
152
|
+
return session.run(sid, fn);
|
|
153
|
+
}
|
|
154
|
+
export function currentSession() {
|
|
155
|
+
return session.getStore();
|
|
156
|
+
}
|
|
157
|
+
export function onStoreWrite(fn) {
|
|
158
|
+
storeWatchers.add(fn);
|
|
159
|
+
return () => {
|
|
160
|
+
storeWatchers.delete(fn);
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
let stopObserving = null;
|
|
164
|
+
export function track(deps, fn) {
|
|
165
|
+
stopObserving ??= onRead((model, actor, key) => current.getStore()?.add(model, actor, key));
|
|
166
|
+
return current.run(deps, fn);
|
|
167
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare class StyleError extends Error {
|
|
2
|
+
}
|
|
3
|
+
export interface ProjectStyles {
|
|
4
|
+
global?: string;
|
|
5
|
+
components: Record<string, string>;
|
|
6
|
+
}
|
|
7
|
+
export interface StyleOptions {
|
|
8
|
+
minify?: boolean;
|
|
9
|
+
theme?: {
|
|
10
|
+
css(): string;
|
|
11
|
+
tailwind(): string;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export declare function projectStyles(root: string, componentCss: readonly string[], opts?: StyleOptions): Promise<ProjectStyles>;
|
package/dist/tailwind.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
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, readFileSync } from "node:fs";
|
|
10
|
+
import { createRequire } from "node:module";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
import { pathToFileURL } from "node:url";
|
|
13
|
+
import { loadPostcss } from "./postcss.js";
|
|
14
|
+
export class StyleError extends Error {
|
|
15
|
+
}
|
|
16
|
+
const GLOBAL_TW = /@import\s+(["'])tailwindcss(\/[^"']*)?\1|@tailwind\b|@theme\b|@apply\b|@utility\b|@plugin\b|@source\b|@custom-variant\b|@config\b/;
|
|
17
|
+
const COMPONENT_TW = /@apply\b|@variant\b|(^|[^\w-])-{0,2}theme\(|@reference\b/;
|
|
18
|
+
async function loadTailwind(root) {
|
|
19
|
+
const req = createRequire(join(root, "package.json"));
|
|
20
|
+
let nodePath;
|
|
21
|
+
let oxidePath;
|
|
22
|
+
try {
|
|
23
|
+
nodePath = req.resolve("@tailwindcss/node");
|
|
24
|
+
oxidePath = createRequire(nodePath).resolve("@tailwindcss/oxide");
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
const node = (await import(__rewriteRelativeImportExtension(pathToFileURL(nodePath).href)));
|
|
30
|
+
const oxide = (await import(__rewriteRelativeImportExtension(pathToFileURL(oxidePath).href)));
|
|
31
|
+
return { node, oxide };
|
|
32
|
+
}
|
|
33
|
+
const MISSING = "uses Tailwind, but it is not installed; add it to the project: `npm install -D tailwindcss @tailwindcss/node`";
|
|
34
|
+
function finish(css) {
|
|
35
|
+
return css
|
|
36
|
+
.replace(/^\/\*! tailwindcss[^*]*\*\/\s*/, "")
|
|
37
|
+
.trim()
|
|
38
|
+
.replace(/<\//g, "<\\/");
|
|
39
|
+
}
|
|
40
|
+
const cache = new Map();
|
|
41
|
+
export async function projectStyles(root, componentCss, opts = {}) {
|
|
42
|
+
const out = await tailwindStyles(root, componentCss, opts);
|
|
43
|
+
const pass = await loadPostcss(root, opts.minify === true);
|
|
44
|
+
if (!pass)
|
|
45
|
+
return out;
|
|
46
|
+
if (out.global !== undefined)
|
|
47
|
+
out.global = await pass(out.global, join(root, "src", "app.css"));
|
|
48
|
+
for (const css of componentCss) {
|
|
49
|
+
const input = (out.components[css] ?? css).replace(/<\\\//g, "</");
|
|
50
|
+
out.components[css] = finish(await pass(input, join(root, "src", "component.css")));
|
|
51
|
+
}
|
|
52
|
+
return out;
|
|
53
|
+
}
|
|
54
|
+
async function tailwindStyles(root, componentCss, opts) {
|
|
55
|
+
const src = join(root, "src");
|
|
56
|
+
const appFile = join(src, "app.css");
|
|
57
|
+
const file = existsSync(appFile) ? readFileSync(appFile, "utf8") : undefined;
|
|
58
|
+
const appTw = file !== undefined && GLOBAL_TW.test(file);
|
|
59
|
+
const themeCss = opts.theme?.css() ?? "";
|
|
60
|
+
const themeTw = opts.theme?.tailwind() ?? "";
|
|
61
|
+
const app = file === undefined ? (themeCss ? themeCss : undefined) : appTw ? `${file}\n${themeTw}${themeCss}` : themeCss + file;
|
|
62
|
+
const needy = componentCss.filter((css) => COMPONENT_TW.test(css));
|
|
63
|
+
const out = { components: {} };
|
|
64
|
+
if (!appTw && needy.length === 0) {
|
|
65
|
+
if (app !== undefined) {
|
|
66
|
+
out.global = app;
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
const tw = await loadTailwind(root);
|
|
71
|
+
if (!tw) {
|
|
72
|
+
throw new StyleError(`${appTw ? "src/app.css" : "a component style"} ${MISSING}`);
|
|
73
|
+
}
|
|
74
|
+
const minify = (css) => (opts.minify ? tw.node.optimize(css, { minify: true }).code : css);
|
|
75
|
+
const fail = (where, err) => {
|
|
76
|
+
throw new StyleError(`${where}: ${err.message}`);
|
|
77
|
+
};
|
|
78
|
+
if (app !== undefined) {
|
|
79
|
+
if (appTw) {
|
|
80
|
+
try {
|
|
81
|
+
const compiler = await tw.node.compile(app, { base: src, onDependency: () => { } });
|
|
82
|
+
const sources = compiler.root === "none"
|
|
83
|
+
? []
|
|
84
|
+
: compiler.root === null
|
|
85
|
+
? [{ base: src, pattern: "**/*", negated: false }]
|
|
86
|
+
: [{ ...compiler.root, negated: false }];
|
|
87
|
+
const candidates = new tw.oxide.Scanner({ sources: [...sources, ...compiler.sources] }).scan();
|
|
88
|
+
out.global = minify(compiler.build(candidates));
|
|
89
|
+
}
|
|
90
|
+
catch (err) {
|
|
91
|
+
fail("src/app.css", err);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
out.global = app;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const reference = appTw ? JSON.stringify(appFile.replace(/\\/g, "/")) : `"tailwindcss"`;
|
|
99
|
+
const refKey = `${appTw ? `${reference}\n${app}` : reference}\n${themeTw}`;
|
|
100
|
+
for (const css of needy) {
|
|
101
|
+
const key = `${refKey}\n${opts.minify ? 1 : 0}\n${css}`;
|
|
102
|
+
let done = cache.get(key);
|
|
103
|
+
if (done === undefined) {
|
|
104
|
+
try {
|
|
105
|
+
const input = `@reference ${reference};\n${themeTw}${css.replace(/<\\\//g, "</")}`;
|
|
106
|
+
const compiler = await tw.node.compile(input, { base: src, onDependency: () => { } });
|
|
107
|
+
done = finish(minify(compiler.build([])));
|
|
108
|
+
}
|
|
109
|
+
catch (err) {
|
|
110
|
+
fail("component style", err);
|
|
111
|
+
}
|
|
112
|
+
cache.set(key, done);
|
|
113
|
+
}
|
|
114
|
+
out.components[css] = done;
|
|
115
|
+
}
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { SqlDatabase } from "@weft-framework/data";
|
|
2
|
+
import { type SecondFactor } from "./auth.js";
|
|
3
|
+
import type { PageContext } from "./context.js";
|
|
4
|
+
import { type Secret } from "./secret.js";
|
|
5
|
+
type Maybe<T> = T | Promise<T>;
|
|
6
|
+
export interface TwoFactorRecord {
|
|
7
|
+
user: string;
|
|
8
|
+
secret: string;
|
|
9
|
+
lastStep: number;
|
|
10
|
+
failures: number;
|
|
11
|
+
lockedUntil: number;
|
|
12
|
+
created: number;
|
|
13
|
+
}
|
|
14
|
+
export interface TwoFactorStore {
|
|
15
|
+
get(user: string): Maybe<TwoFactorRecord | null | undefined>;
|
|
16
|
+
add(r: TwoFactorRecord): Maybe<boolean>;
|
|
17
|
+
remove(user: string): Maybe<boolean>;
|
|
18
|
+
advance(user: string, step: number): Maybe<boolean>;
|
|
19
|
+
fail(user: string, max: number, until: number): Maybe<boolean>;
|
|
20
|
+
succeed(user: string): Maybe<void>;
|
|
21
|
+
setRecovery(user: string, hashes: string[]): Maybe<void>;
|
|
22
|
+
useRecovery(user: string, hash: string): Maybe<boolean>;
|
|
23
|
+
countRecovery(user: string): Maybe<number>;
|
|
24
|
+
}
|
|
25
|
+
export declare class MemoryTwoFactorStore implements TwoFactorStore {
|
|
26
|
+
#private;
|
|
27
|
+
get(user: string): TwoFactorRecord | null;
|
|
28
|
+
add(r: TwoFactorRecord): boolean;
|
|
29
|
+
remove(user: string): boolean;
|
|
30
|
+
advance(user: string, step: number): boolean;
|
|
31
|
+
fail(user: string, max: number, until: number): boolean;
|
|
32
|
+
succeed(user: string): void;
|
|
33
|
+
setRecovery(user: string, hashes: string[]): void;
|
|
34
|
+
useRecovery(user: string, hash: string): boolean;
|
|
35
|
+
countRecovery(user: string): number;
|
|
36
|
+
}
|
|
37
|
+
export declare class SqlTwoFactorStore implements TwoFactorStore {
|
|
38
|
+
#private;
|
|
39
|
+
constructor(db: SqlDatabase, table?: string);
|
|
40
|
+
get(user: string): TwoFactorRecord | null;
|
|
41
|
+
add(r: TwoFactorRecord): boolean;
|
|
42
|
+
remove(user: string): boolean;
|
|
43
|
+
advance(user: string, step: number): boolean;
|
|
44
|
+
fail(user: string, max: number, until: number): boolean;
|
|
45
|
+
succeed(user: string): void;
|
|
46
|
+
setRecovery(user: string, hashes: string[]): void;
|
|
47
|
+
useRecovery(user: string, hash: string): boolean;
|
|
48
|
+
countRecovery(user: string): number;
|
|
49
|
+
}
|
|
50
|
+
export type TwoFactorErrorCode = "signed-out" | "invalid" | "expired" | "conflict" | "locked" | "required" | "not-enrolled";
|
|
51
|
+
export declare class TwoFactorError extends Error {
|
|
52
|
+
readonly code: TwoFactorErrorCode;
|
|
53
|
+
readonly until?: number;
|
|
54
|
+
constructor(code: TwoFactorErrorCode, message: string, until?: number);
|
|
55
|
+
}
|
|
56
|
+
export interface TwoFactorOptions {
|
|
57
|
+
key: Secret | string;
|
|
58
|
+
issuer: string;
|
|
59
|
+
page: string;
|
|
60
|
+
require?(user: string, tenant?: string): Maybe<boolean>;
|
|
61
|
+
store?: TwoFactorStore;
|
|
62
|
+
digits?: 6 | 8;
|
|
63
|
+
window?: number;
|
|
64
|
+
maxAttempts?: number;
|
|
65
|
+
lockMs?: number;
|
|
66
|
+
recoveryCodes?: number;
|
|
67
|
+
now?: () => number;
|
|
68
|
+
}
|
|
69
|
+
export interface TwoFactorSetup {
|
|
70
|
+
secret: string;
|
|
71
|
+
uri: string;
|
|
72
|
+
state: string;
|
|
73
|
+
}
|
|
74
|
+
export interface TwoFactorStatus {
|
|
75
|
+
user: string;
|
|
76
|
+
pending: boolean;
|
|
77
|
+
enabled: boolean;
|
|
78
|
+
required: boolean;
|
|
79
|
+
mfa: boolean;
|
|
80
|
+
recoveryLeft: number;
|
|
81
|
+
}
|
|
82
|
+
export declare function base32(bytes: Uint8Array): string;
|
|
83
|
+
export declare function fromBase32(s: string): Uint8Array | null;
|
|
84
|
+
export declare function totp(secret: Uint8Array, step: number, digits?: 6 | 8): Promise<string>;
|
|
85
|
+
export declare class TwoFactor implements SecondFactor {
|
|
86
|
+
#private;
|
|
87
|
+
readonly page: string;
|
|
88
|
+
constructor(o: TwoFactorOptions);
|
|
89
|
+
needed(user: string, tenant?: string): Promise<boolean>;
|
|
90
|
+
enabled(user: string): Promise<boolean>;
|
|
91
|
+
status(ctx: PageContext): Promise<TwoFactorStatus | null>;
|
|
92
|
+
setup(ctx: PageContext, o?: {
|
|
93
|
+
account?: string;
|
|
94
|
+
}): Promise<TwoFactorSetup>;
|
|
95
|
+
enable(ctx: PageContext, state: string, code: string): Promise<string[] | null>;
|
|
96
|
+
verify(ctx: PageContext, code: string): Promise<boolean>;
|
|
97
|
+
confirm(ctx: PageContext, code: string): Promise<boolean>;
|
|
98
|
+
regenerateRecoveryCodes(ctx: PageContext, code: string): Promise<string[] | null>;
|
|
99
|
+
disable(ctx: PageContext, code: string): Promise<boolean>;
|
|
100
|
+
reset(user: string): Promise<boolean>;
|
|
101
|
+
}
|
|
102
|
+
export declare function defineTwoFactor(o: TwoFactorOptions): TwoFactor;
|
|
103
|
+
export {};
|