@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/otel.js
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import { redact } from "./log.js";
|
|
2
|
+
const TRACEPARENT = /^00-([0-9a-f]{32})-([0-9a-f]{16})-[0-9a-f]{2}$/;
|
|
3
|
+
function hex(bytes) {
|
|
4
|
+
return Array.from(crypto.getRandomValues(new Uint8Array(bytes)), (b) => b.toString(16).padStart(2, "0")).join("");
|
|
5
|
+
}
|
|
6
|
+
function value(v) {
|
|
7
|
+
if (typeof v === "boolean")
|
|
8
|
+
return { boolValue: v };
|
|
9
|
+
if (typeof v === "number")
|
|
10
|
+
return Number.isInteger(v) ? { intValue: String(v) } : { doubleValue: v };
|
|
11
|
+
return { stringValue: typeof v === "string" ? v : JSON.stringify(redact(v)) };
|
|
12
|
+
}
|
|
13
|
+
function attributes(o) {
|
|
14
|
+
return Object.entries(o)
|
|
15
|
+
.filter(([, v]) => v !== undefined && v !== null)
|
|
16
|
+
.map(([key, v]) => ({ key, value: value(v) }));
|
|
17
|
+
}
|
|
18
|
+
const nanos = (ms) => `${BigInt(Math.round(ms * 1000)) * 1000n}`;
|
|
19
|
+
const BOUNDS = [0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10];
|
|
20
|
+
const DESCRIPTIONS = {
|
|
21
|
+
"http.server.request.duration": "Duration of HTTP server requests.",
|
|
22
|
+
"weft.live.event.duration": "Duration of live page events.",
|
|
23
|
+
"weft.job.duration": "Duration of job runs.",
|
|
24
|
+
};
|
|
25
|
+
const errorType = (err) => (err instanceof Error ? err.name : typeof err);
|
|
26
|
+
export function otlpExporter(o) {
|
|
27
|
+
let base;
|
|
28
|
+
try {
|
|
29
|
+
base = new URL(o.endpoint);
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
throw new Error("otlpExporter: endpoint is a URL");
|
|
33
|
+
}
|
|
34
|
+
if (base.protocol !== "https:" && !["localhost", "127.0.0.1", "[::1]"].includes(base.hostname)) {
|
|
35
|
+
throw new Error("otlpExporter: endpoint must be https (http only for a local collector)");
|
|
36
|
+
}
|
|
37
|
+
if (typeof o.service !== "string" || o.service === "")
|
|
38
|
+
throw new Error("otlpExporter: service names the service");
|
|
39
|
+
const batchMs = o.batchMs ?? 2000;
|
|
40
|
+
const maxBatch = o.maxBatch ?? 512;
|
|
41
|
+
const maxQueue = o.maxQueue ?? 2048;
|
|
42
|
+
const metricsMs = o.metricsMs ?? 60_000;
|
|
43
|
+
const maxSeries = o.maxSeries ?? 1000;
|
|
44
|
+
for (const [k, v, lo] of [
|
|
45
|
+
["batchMs", batchMs, 10],
|
|
46
|
+
["maxBatch", maxBatch, 1],
|
|
47
|
+
["maxQueue", maxQueue, 1],
|
|
48
|
+
["metricsMs", metricsMs, 1000],
|
|
49
|
+
["maxSeries", maxSeries, 1],
|
|
50
|
+
]) {
|
|
51
|
+
if (!Number.isInteger(v) || v < lo)
|
|
52
|
+
throw new Error(`otlpExporter: ${k} is a whole number of at least ${lo}`);
|
|
53
|
+
}
|
|
54
|
+
if (metricsMs > 3600_000)
|
|
55
|
+
throw new Error("otlpExporter: metricsMs is at most 1 hour");
|
|
56
|
+
const doFetch = o.fetch ?? ((url, init) => fetch(url, init));
|
|
57
|
+
const onError = o.onError ?? ((err) => console.error("[weft] otlp:", err));
|
|
58
|
+
const now = o.now ?? Date.now;
|
|
59
|
+
const root = base.href.endsWith("/") ? base.href : `${base.href}/`;
|
|
60
|
+
const resource = { attributes: attributes({ "service.name": o.service, "telemetry.sdk.name": "weft", ...o.resource }) };
|
|
61
|
+
const scope = { name: "@weft-framework/server" };
|
|
62
|
+
const spans = [];
|
|
63
|
+
const logs = [];
|
|
64
|
+
let dropped = 0;
|
|
65
|
+
let timer = null;
|
|
66
|
+
let sending = Promise.resolve();
|
|
67
|
+
const headers = () => {
|
|
68
|
+
const h = { "content-type": "application/json" };
|
|
69
|
+
for (const [k, v] of Object.entries(o.headers ?? {}))
|
|
70
|
+
h[k] = typeof v === "string" ? v : v.reveal();
|
|
71
|
+
return h;
|
|
72
|
+
};
|
|
73
|
+
const post = async (path, body) => {
|
|
74
|
+
try {
|
|
75
|
+
const res = await doFetch(root + path, { method: "POST", headers: headers(), body: JSON.stringify(body) });
|
|
76
|
+
if (!res.ok)
|
|
77
|
+
throw new Error(`OTLP ${path} answered HTTP ${res.status}`);
|
|
78
|
+
await res.body?.cancel().catch(() => undefined);
|
|
79
|
+
}
|
|
80
|
+
catch (err) {
|
|
81
|
+
onError(err);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
const send = () => {
|
|
85
|
+
if (timer) {
|
|
86
|
+
clearTimeout(timer);
|
|
87
|
+
timer = null;
|
|
88
|
+
}
|
|
89
|
+
const s = spans.splice(0, maxBatch);
|
|
90
|
+
const l = logs.splice(0, maxBatch);
|
|
91
|
+
const jobs = [];
|
|
92
|
+
if (s.length)
|
|
93
|
+
jobs.push(post("v1/traces", { resourceSpans: [{ resource, scopeSpans: [{ scope, spans: s }] }] }));
|
|
94
|
+
if (l.length)
|
|
95
|
+
jobs.push(post("v1/logs", { resourceLogs: [{ resource, scopeLogs: [{ scope, logRecords: l }] }] }));
|
|
96
|
+
sending = sending.then(() => Promise.all(jobs)).then(() => undefined);
|
|
97
|
+
if (spans.length || logs.length)
|
|
98
|
+
schedule();
|
|
99
|
+
return sending;
|
|
100
|
+
};
|
|
101
|
+
const schedule = () => {
|
|
102
|
+
if (timer)
|
|
103
|
+
return;
|
|
104
|
+
timer = setTimeout(() => void send(), batchMs);
|
|
105
|
+
timer.unref?.();
|
|
106
|
+
};
|
|
107
|
+
const started = now();
|
|
108
|
+
const metrics = new Map();
|
|
109
|
+
let metricTimer = null;
|
|
110
|
+
const OVERFLOW = JSON.stringify([["otel.metric.overflow", true]]);
|
|
111
|
+
const record = (name, attrs, seconds) => {
|
|
112
|
+
let byKey = metrics.get(name);
|
|
113
|
+
if (!byKey)
|
|
114
|
+
metrics.set(name, (byKey = new Map()));
|
|
115
|
+
const clean = Object.entries(attrs).filter(([, v]) => v !== undefined).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
116
|
+
let key = JSON.stringify(clean);
|
|
117
|
+
let s = byKey.get(key);
|
|
118
|
+
if (!s && byKey.size >= maxSeries) {
|
|
119
|
+
key = OVERFLOW;
|
|
120
|
+
s = byKey.get(key);
|
|
121
|
+
clean.splice(0, clean.length, ["otel.metric.overflow", true]);
|
|
122
|
+
}
|
|
123
|
+
if (!s) {
|
|
124
|
+
s = { attributes: Object.fromEntries(clean), count: 0, sum: 0, min: Infinity, max: -Infinity, buckets: new Array(BOUNDS.length + 1).fill(0) };
|
|
125
|
+
byKey.set(key, s);
|
|
126
|
+
}
|
|
127
|
+
const v = Number.isFinite(seconds) && seconds > 0 ? seconds : 0;
|
|
128
|
+
s.count++;
|
|
129
|
+
s.sum += v;
|
|
130
|
+
s.min = Math.min(s.min, v);
|
|
131
|
+
s.max = Math.max(s.max, v);
|
|
132
|
+
let i = 0;
|
|
133
|
+
while (i < BOUNDS.length && v > BOUNDS[i])
|
|
134
|
+
i++;
|
|
135
|
+
s.buckets[i]++;
|
|
136
|
+
if (!metricTimer) {
|
|
137
|
+
metricTimer = setTimeout(() => void sendMetrics(), metricsMs);
|
|
138
|
+
metricTimer.unref?.();
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
const sendMetrics = () => {
|
|
142
|
+
if (metricTimer) {
|
|
143
|
+
clearTimeout(metricTimer);
|
|
144
|
+
metricTimer = null;
|
|
145
|
+
}
|
|
146
|
+
if (metrics.size === 0)
|
|
147
|
+
return sending;
|
|
148
|
+
const at = nanos(now());
|
|
149
|
+
const from = nanos(started);
|
|
150
|
+
const body = {
|
|
151
|
+
resourceMetrics: [{
|
|
152
|
+
resource,
|
|
153
|
+
scopeMetrics: [{
|
|
154
|
+
scope,
|
|
155
|
+
metrics: [...metrics].map(([name, byKey]) => ({
|
|
156
|
+
name,
|
|
157
|
+
unit: "s",
|
|
158
|
+
description: DESCRIPTIONS[name] ?? "",
|
|
159
|
+
histogram: {
|
|
160
|
+
aggregationTemporality: 2,
|
|
161
|
+
dataPoints: [...byKey.values()].map((s) => ({
|
|
162
|
+
attributes: attributes(s.attributes),
|
|
163
|
+
startTimeUnixNano: from,
|
|
164
|
+
timeUnixNano: at,
|
|
165
|
+
count: String(s.count),
|
|
166
|
+
sum: s.sum,
|
|
167
|
+
min: s.min,
|
|
168
|
+
max: s.max,
|
|
169
|
+
bucketCounts: s.buckets.map(String),
|
|
170
|
+
explicitBounds: BOUNDS,
|
|
171
|
+
})),
|
|
172
|
+
},
|
|
173
|
+
})),
|
|
174
|
+
}],
|
|
175
|
+
}],
|
|
176
|
+
};
|
|
177
|
+
sending = sending.then(() => post("v1/metrics", body));
|
|
178
|
+
metricTimer = setTimeout(() => void sendMetrics(), metricsMs);
|
|
179
|
+
metricTimer.unref?.();
|
|
180
|
+
return sending;
|
|
181
|
+
};
|
|
182
|
+
const queue = (list, item) => {
|
|
183
|
+
list.push(item);
|
|
184
|
+
if (spans.length + logs.length > maxQueue) {
|
|
185
|
+
(spans.length >= logs.length ? spans : logs).shift();
|
|
186
|
+
dropped++;
|
|
187
|
+
}
|
|
188
|
+
if (list.length >= maxBatch)
|
|
189
|
+
void send();
|
|
190
|
+
else
|
|
191
|
+
schedule();
|
|
192
|
+
};
|
|
193
|
+
return {
|
|
194
|
+
get dropped() {
|
|
195
|
+
return dropped;
|
|
196
|
+
},
|
|
197
|
+
startRequest(req) {
|
|
198
|
+
const parent = TRACEPARENT.exec(req.headers.get("traceparent") ?? "");
|
|
199
|
+
const valid = parent && !/^0+$/.test(parent[1]) && !/^0+$/.test(parent[2]);
|
|
200
|
+
return {
|
|
201
|
+
traceId: valid ? parent[1] : hex(16),
|
|
202
|
+
spanId: hex(8),
|
|
203
|
+
...(valid ? { parentSpanId: parent[2] } : {}),
|
|
204
|
+
start: now(),
|
|
205
|
+
};
|
|
206
|
+
},
|
|
207
|
+
endRequest(t, info) {
|
|
208
|
+
queue(spans, {
|
|
209
|
+
traceId: t.traceId,
|
|
210
|
+
spanId: t.spanId,
|
|
211
|
+
...(t.parentSpanId ? { parentSpanId: t.parentSpanId } : {}),
|
|
212
|
+
name: info.route ? `${info.method} ${info.route}` : info.method,
|
|
213
|
+
kind: 2,
|
|
214
|
+
startTimeUnixNano: nanos(t.start),
|
|
215
|
+
endTimeUnixNano: nanos(now()),
|
|
216
|
+
attributes: attributes({
|
|
217
|
+
"http.request.method": info.method,
|
|
218
|
+
"http.route": info.route,
|
|
219
|
+
"url.path": info.path,
|
|
220
|
+
"http.response.status_code": info.status,
|
|
221
|
+
}),
|
|
222
|
+
status: info.status >= 500 ? { code: 2 } : { code: 0 },
|
|
223
|
+
});
|
|
224
|
+
record("http.server.request.duration", {
|
|
225
|
+
"http.request.method": info.method,
|
|
226
|
+
"http.route": info.route,
|
|
227
|
+
"http.response.status_code": info.status,
|
|
228
|
+
"error.type": info.status >= 500 ? String(info.status) : undefined,
|
|
229
|
+
}, (now() - t.start) / 1000);
|
|
230
|
+
},
|
|
231
|
+
span(name, so = {}) {
|
|
232
|
+
const parent = so.parent ?? null;
|
|
233
|
+
const context = {
|
|
234
|
+
traceId: parent ? parent.traceId : hex(16),
|
|
235
|
+
spanId: hex(8),
|
|
236
|
+
...(parent ? { parentSpanId: parent.spanId } : {}),
|
|
237
|
+
start: Math.max(parent?.start ?? -Infinity, now() - (Number.isFinite(so.elapsedMs) && so.elapsedMs > 0 ? so.elapsedMs : 0)),
|
|
238
|
+
};
|
|
239
|
+
let ended = false;
|
|
240
|
+
return {
|
|
241
|
+
context,
|
|
242
|
+
end(eo = {}) {
|
|
243
|
+
if (ended)
|
|
244
|
+
return;
|
|
245
|
+
ended = true;
|
|
246
|
+
const failed = eo.error !== undefined;
|
|
247
|
+
queue(spans, {
|
|
248
|
+
traceId: context.traceId,
|
|
249
|
+
spanId: context.spanId,
|
|
250
|
+
...(context.parentSpanId ? { parentSpanId: context.parentSpanId } : {}),
|
|
251
|
+
name,
|
|
252
|
+
kind: so.kind === "consumer" ? 5 : 1,
|
|
253
|
+
startTimeUnixNano: nanos(context.start),
|
|
254
|
+
endTimeUnixNano: nanos(now()),
|
|
255
|
+
attributes: attributes({ ...so.attributes, ...eo.attributes, "error.type": failed ? errorType(eo.error) : undefined }),
|
|
256
|
+
status: failed ? { code: 2 } : { code: 0 },
|
|
257
|
+
});
|
|
258
|
+
if (so.metric) {
|
|
259
|
+
const extra = failed ? { "error.type": errorType(eo.error) } : {};
|
|
260
|
+
record(so.metric.name, { ...so.metric.attributes, ...extra }, (now() - context.start) / 1000);
|
|
261
|
+
}
|
|
262
|
+
},
|
|
263
|
+
};
|
|
264
|
+
},
|
|
265
|
+
error(err, report, t) {
|
|
266
|
+
const e = err instanceof Error ? err : null;
|
|
267
|
+
queue(logs, {
|
|
268
|
+
timeUnixNano: nanos(now()),
|
|
269
|
+
severityNumber: 17,
|
|
270
|
+
severityText: "ERROR",
|
|
271
|
+
body: { stringValue: e ? e.message : String(redact(err)) },
|
|
272
|
+
...(t ? { traceId: t.traceId, spanId: t.spanId } : {}),
|
|
273
|
+
attributes: attributes({
|
|
274
|
+
"exception.type": e ? e.name : typeof err,
|
|
275
|
+
"exception.message": e ? e.message : undefined,
|
|
276
|
+
"exception.stacktrace": e?.stack,
|
|
277
|
+
"weft.kind": report.kind,
|
|
278
|
+
"http.route": report.route,
|
|
279
|
+
"weft.component": report.component,
|
|
280
|
+
"weft.handler": report.handler,
|
|
281
|
+
"enduser.id": report.actor,
|
|
282
|
+
"url.path": report.path,
|
|
283
|
+
"http.request.method": report.method,
|
|
284
|
+
"service.instance.id": report.instance,
|
|
285
|
+
}),
|
|
286
|
+
});
|
|
287
|
+
},
|
|
288
|
+
flush: () => {
|
|
289
|
+
void send();
|
|
290
|
+
return sendMetrics();
|
|
291
|
+
},
|
|
292
|
+
};
|
|
293
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { SqlDatabase } from "@weft-framework/data";
|
|
2
|
+
import type { PageContext } from "./context.js";
|
|
3
|
+
type Maybe<T> = T | Promise<T>;
|
|
4
|
+
export interface PasskeyCredential {
|
|
5
|
+
id: string;
|
|
6
|
+
user: string;
|
|
7
|
+
handle: string;
|
|
8
|
+
publicKey: JsonWebKey;
|
|
9
|
+
alg: number;
|
|
10
|
+
signCount: number;
|
|
11
|
+
transports: string[];
|
|
12
|
+
backedUp: boolean;
|
|
13
|
+
name: string;
|
|
14
|
+
created: number;
|
|
15
|
+
lastUsed: number | null;
|
|
16
|
+
}
|
|
17
|
+
export interface PasskeyStore {
|
|
18
|
+
add(c: PasskeyCredential): Maybe<void>;
|
|
19
|
+
get(id: string): Maybe<PasskeyCredential | null | undefined>;
|
|
20
|
+
list(user: string): Maybe<PasskeyCredential[]>;
|
|
21
|
+
update(id: string, patch: Partial<Pick<PasskeyCredential, "signCount" | "lastUsed" | "backedUp" | "name">>): Maybe<void>;
|
|
22
|
+
remove(id: string): Maybe<boolean>;
|
|
23
|
+
}
|
|
24
|
+
export declare class MemoryPasskeyStore implements PasskeyStore {
|
|
25
|
+
#private;
|
|
26
|
+
add(c: PasskeyCredential): void;
|
|
27
|
+
get(id: string): PasskeyCredential | null;
|
|
28
|
+
list(user: string): PasskeyCredential[];
|
|
29
|
+
update(id: string, patch: Partial<PasskeyCredential>): void;
|
|
30
|
+
remove(id: string): boolean;
|
|
31
|
+
}
|
|
32
|
+
export declare class SqlPasskeyStore implements PasskeyStore {
|
|
33
|
+
private readonly db;
|
|
34
|
+
private readonly table;
|
|
35
|
+
constructor(db: SqlDatabase, table?: string);
|
|
36
|
+
private row;
|
|
37
|
+
add(c: PasskeyCredential): void;
|
|
38
|
+
get(id: string): PasskeyCredential | null;
|
|
39
|
+
list(user: string): PasskeyCredential[];
|
|
40
|
+
update(id: string, patch: Partial<PasskeyCredential>): void;
|
|
41
|
+
remove(id: string): boolean;
|
|
42
|
+
}
|
|
43
|
+
export declare class PasskeyError extends Error {
|
|
44
|
+
readonly code: "signed-out" | "invalid" | "expired" | "conflict" | "limit" | "not-found";
|
|
45
|
+
constructor(code: PasskeyError["code"], message: string, opts?: ErrorOptions);
|
|
46
|
+
}
|
|
47
|
+
type Cbor = number | bigint | string | boolean | null | undefined | Uint8Array | Cbor[] | Map<Cbor, Cbor>;
|
|
48
|
+
export declare function decodeCbor(buf: Uint8Array, pos?: number, depth?: number): {
|
|
49
|
+
value: Cbor;
|
|
50
|
+
end: number;
|
|
51
|
+
};
|
|
52
|
+
interface AuthData {
|
|
53
|
+
rpIdHash: Uint8Array;
|
|
54
|
+
flags: number;
|
|
55
|
+
signCount: number;
|
|
56
|
+
credential?: {
|
|
57
|
+
id: Uint8Array;
|
|
58
|
+
publicKey: JsonWebKey;
|
|
59
|
+
alg: number;
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
export declare function coseToJwk(key: Cbor): {
|
|
63
|
+
jwk: JsonWebKey;
|
|
64
|
+
alg: number;
|
|
65
|
+
};
|
|
66
|
+
export declare function parseAuthData(buf: Uint8Array): AuthData;
|
|
67
|
+
export declare function derToRaw(der: Uint8Array, size: number): Uint8Array;
|
|
68
|
+
export interface PasskeyOptions {
|
|
69
|
+
url: string;
|
|
70
|
+
name: string;
|
|
71
|
+
rpId?: string;
|
|
72
|
+
store?: PasskeyStore;
|
|
73
|
+
userVerification?: boolean;
|
|
74
|
+
maxPerUser?: number;
|
|
75
|
+
now?(): number;
|
|
76
|
+
}
|
|
77
|
+
export interface PasskeyChallenge {
|
|
78
|
+
publicKey: Record<string, unknown>;
|
|
79
|
+
state: string;
|
|
80
|
+
}
|
|
81
|
+
export declare class Passkeys {
|
|
82
|
+
#private;
|
|
83
|
+
constructor(o: PasskeyOptions);
|
|
84
|
+
registrationOptions(ctx: PageContext, opts?: {
|
|
85
|
+
userName?: string;
|
|
86
|
+
displayName?: string;
|
|
87
|
+
}): Promise<PasskeyChallenge>;
|
|
88
|
+
register(ctx: PageContext, state: string, credential: string, opts?: {
|
|
89
|
+
name?: string;
|
|
90
|
+
}): Promise<PasskeyCredential>;
|
|
91
|
+
signInOptions(ctx: PageContext): Promise<PasskeyChallenge>;
|
|
92
|
+
signIn(ctx: PageContext, state: string, credential: string, onReject?: (reason: Error) => void): Promise<boolean>;
|
|
93
|
+
verify(ctx: PageContext, state: string, credential: string, onReject?: (reason: Error) => void): Promise<string | null>;
|
|
94
|
+
list(user: string): Promise<Omit<PasskeyCredential, "publicKey" | "handle">[]>;
|
|
95
|
+
rename(user: string, id: string, name: string): Promise<void>;
|
|
96
|
+
remove(user: string, id: string): Promise<void>;
|
|
97
|
+
}
|
|
98
|
+
export declare function definePasskeys(o: PasskeyOptions): Passkeys;
|
|
99
|
+
export {};
|