@soron-ai/next 0.0.0-stage → 0.1.0
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/dist/cli.js +3679 -0
- package/dist/doors.d.ts +35 -0
- package/dist/protocol.d.ts +53 -0
- package/dist/routes.d.ts +6 -0
- package/dist/routes.js +283 -0
- package/dist/styles.css +2210 -0
- package/dist/version.d.ts +1 -0
- package/dist/widget/answer.d.ts +19 -0
- package/dist/widget/badge.d.ts +2 -0
- package/dist/widget/composer-keys.d.ts +11 -0
- package/dist/widget/composer.d.ts +14 -0
- package/dist/widget/core/cards.d.ts +17 -0
- package/dist/widget/core/contract.d.ts +209 -0
- package/dist/widget/core/ending.d.ts +26 -0
- package/dist/widget/core/markdown.d.ts +40 -0
- package/dist/widget/core/run-view.d.ts +33 -0
- package/dist/widget/core/service-link.d.ts +58 -0
- package/dist/widget/core/session.d.ts +55 -0
- package/dist/widget/core/sse.d.ts +8 -0
- package/dist/widget/discussion-list.d.ts +11 -0
- package/dist/widget/discussions.d.ts +26 -0
- package/dist/widget/exchange.d.ts +27 -0
- package/dist/widget/eye.d.ts +5 -0
- package/dist/widget/floor.d.ts +5 -0
- package/dist/widget/panel.d.ts +2 -0
- package/dist/widget/provider.d.ts +73 -0
- package/dist/widget/self-test.d.ts +31 -0
- package/dist/widget/ui.d.ts +54 -0
- package/dist/widget/use-discussions.d.ts +39 -0
- package/dist/widget/viewport.d.ts +12 -0
- package/dist/widget.d.ts +6 -0
- package/dist/widget.js +117 -0
- package/package.json +31 -4
- package/README.md +0 -3
package/dist/doors.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { KeyObject } from 'node:crypto';
|
|
2
|
+
export type Staff = {
|
|
3
|
+
sessionId: string;
|
|
4
|
+
userId: string;
|
|
5
|
+
};
|
|
6
|
+
export type SignedIn = Staff & {
|
|
7
|
+
cookies?: readonly string[];
|
|
8
|
+
};
|
|
9
|
+
export type Scalar = string | number | boolean | null;
|
|
10
|
+
export type RowsQuery = {
|
|
11
|
+
table: string;
|
|
12
|
+
columns: readonly string[];
|
|
13
|
+
where: Readonly<Record<string, Scalar>>;
|
|
14
|
+
limit: number;
|
|
15
|
+
};
|
|
16
|
+
export type SoronAdapter = {
|
|
17
|
+
config: unknown;
|
|
18
|
+
session(request: Request): Promise<SignedIn | null>;
|
|
19
|
+
isStaff(userId: string): Promise<boolean>;
|
|
20
|
+
sessionAlive(staff: Staff): Promise<boolean>;
|
|
21
|
+
rows(query: RowsQuery): Promise<readonly Record<string, unknown>[]>;
|
|
22
|
+
};
|
|
23
|
+
export type RoutesDeps = {
|
|
24
|
+
adapter: SoronAdapter;
|
|
25
|
+
keys: () => Readonly<Record<string, KeyObject>>;
|
|
26
|
+
secret: () => string | undefined;
|
|
27
|
+
version: string;
|
|
28
|
+
now?: () => number;
|
|
29
|
+
};
|
|
30
|
+
export declare class AdapterIncomplete extends Error {
|
|
31
|
+
constructor();
|
|
32
|
+
}
|
|
33
|
+
export declare function createRoutes(d: RoutesDeps): {
|
|
34
|
+
POST(request: Request): Promise<Response>;
|
|
35
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { type KeyObject } from 'node:crypto';
|
|
2
|
+
export { olderThan } from './version.ts';
|
|
3
|
+
export declare const BADGE_TTL_S = 300;
|
|
4
|
+
export type BadgeClaims = {
|
|
5
|
+
sessionId: string;
|
|
6
|
+
userId: string;
|
|
7
|
+
};
|
|
8
|
+
export declare function signBadge(secret: string, c: BadgeClaims, nowS: number): {
|
|
9
|
+
token: string;
|
|
10
|
+
expiresAt: number;
|
|
11
|
+
};
|
|
12
|
+
export declare function verifyBadge(secret: string, token: string, nowS: number): BadgeClaims | undefined;
|
|
13
|
+
export declare const SIGNATURE_HEADER = "x-soron-signature";
|
|
14
|
+
export declare const TIME_HEADER = "x-soron-time";
|
|
15
|
+
export declare const ENVIRONMENT_HEADER = "x-soron-environment";
|
|
16
|
+
export declare function signRead(signer: {
|
|
17
|
+
id: string;
|
|
18
|
+
key: KeyObject;
|
|
19
|
+
}, r: {
|
|
20
|
+
environment: string;
|
|
21
|
+
url: string;
|
|
22
|
+
body: string;
|
|
23
|
+
nowS: number;
|
|
24
|
+
}): Record<string, string>;
|
|
25
|
+
export declare function verifyRead(r: {
|
|
26
|
+
keys: Readonly<Record<string, KeyObject>>;
|
|
27
|
+
url: string;
|
|
28
|
+
body: string;
|
|
29
|
+
header: (name: string) => string | null | undefined;
|
|
30
|
+
nowS: number;
|
|
31
|
+
}): boolean;
|
|
32
|
+
export declare const CONFIG_FILE = "soron.config.json";
|
|
33
|
+
export declare const CONFIG_HEADER = "x-soron-config";
|
|
34
|
+
export declare const VERSION_HEADER = "x-soron-version";
|
|
35
|
+
export type SoronConfig = {
|
|
36
|
+
language: 'fr' | 'en';
|
|
37
|
+
reads: {
|
|
38
|
+
table: string;
|
|
39
|
+
columns: string[];
|
|
40
|
+
}[];
|
|
41
|
+
names: Record<string, string[]>;
|
|
42
|
+
};
|
|
43
|
+
export declare const wordsOf: (name: string) => string;
|
|
44
|
+
export declare const isSecretColumn: (name: string) => boolean;
|
|
45
|
+
export declare const isSecretTable: (table: string) => boolean;
|
|
46
|
+
export declare function secretIn(reads: readonly {
|
|
47
|
+
table: string;
|
|
48
|
+
columns: readonly string[];
|
|
49
|
+
}[]): string | undefined;
|
|
50
|
+
export declare function readConfig(value: unknown): SoronConfig | undefined;
|
|
51
|
+
export declare function configProblem(value: unknown): string | undefined;
|
|
52
|
+
export declare const nameKey: (table: string, column: string) => string;
|
|
53
|
+
export declare const configFingerprint: (value: unknown) => string;
|
package/dist/routes.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type SoronAdapter } from './doors.ts';
|
|
2
|
+
export { AdapterIncomplete } from './doors.ts';
|
|
3
|
+
export type { RowsQuery, Scalar, SignedIn, SoronAdapter, Staff } from './doors.ts';
|
|
4
|
+
export declare function soronRoutes(adapter: SoronAdapter): {
|
|
5
|
+
POST(request: Request): Promise<Response>;
|
|
6
|
+
};
|
package/dist/routes.js
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
// <define:SORON_READ_KEYS>
|
|
2
|
+
var define_SORON_READ_KEYS_default = { current: "MCowBQYDK2VwAyEAf3XDA8Gy+RkYwpiDBtvqkpCmb2ZPocnonWBIqhlzXP0=" };
|
|
3
|
+
|
|
4
|
+
// src/routes.ts
|
|
5
|
+
import { createPublicKey } from "node:crypto";
|
|
6
|
+
|
|
7
|
+
// src/protocol.ts
|
|
8
|
+
import { createHash, createHmac, sign, timingSafeEqual, verify } from "node:crypto";
|
|
9
|
+
var BADGE_TTL_S = 300;
|
|
10
|
+
var BADGE_CONTEXT = "soron-badge.v1";
|
|
11
|
+
var ID = /^[A-Za-z0-9_.:@-]{1,200}$/;
|
|
12
|
+
var mac = (secret, segment) => createHmac("sha256", secret).update(`${BADGE_CONTEXT}.${segment}`).digest("base64url");
|
|
13
|
+
var isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
14
|
+
function signBadge(secret, c, nowS) {
|
|
15
|
+
const e = nowS + BADGE_TTL_S;
|
|
16
|
+
const segment = Buffer.from(JSON.stringify({ v: 1, aud: "soron-read", s: c.sessionId, u: c.userId, e }), "utf8").toString("base64url");
|
|
17
|
+
return { token: `${segment}.${mac(secret, segment)}`, expiresAt: e };
|
|
18
|
+
}
|
|
19
|
+
function verifyBadge(secret, token, nowS) {
|
|
20
|
+
const [segment, sig, extra] = token.split(".");
|
|
21
|
+
if (!segment || !sig || extra !== void 0) return void 0;
|
|
22
|
+
const expected = Buffer.from(mac(secret, segment));
|
|
23
|
+
const given = Buffer.from(sig);
|
|
24
|
+
if (given.length !== expected.length || !timingSafeEqual(given, expected)) return void 0;
|
|
25
|
+
let claims;
|
|
26
|
+
try {
|
|
27
|
+
claims = JSON.parse(Buffer.from(segment, "base64url").toString("utf8"));
|
|
28
|
+
} catch {
|
|
29
|
+
return void 0;
|
|
30
|
+
}
|
|
31
|
+
if (!isObject(claims) || claims["v"] !== 1 || claims["aud"] !== "soron-read") return void 0;
|
|
32
|
+
const { s, u, e } = claims;
|
|
33
|
+
if (typeof s !== "string" || typeof u !== "string" || !ID.test(s) || !ID.test(u) || typeof e !== "number" || !Number.isInteger(e)) return void 0;
|
|
34
|
+
return e > nowS && e <= nowS + BADGE_TTL_S ? { sessionId: s, userId: u } : void 0;
|
|
35
|
+
}
|
|
36
|
+
var SIGNATURE_HEADER = "x-soron-signature";
|
|
37
|
+
var TIME_HEADER = "x-soron-time";
|
|
38
|
+
var ENVIRONMENT_HEADER = "x-soron-environment";
|
|
39
|
+
var MAX_SKEW_S = 60;
|
|
40
|
+
var signed = (environment, timeS, url, body) => Buffer.from(`soron-read-v1
|
|
41
|
+
${environment}
|
|
42
|
+
${timeS}
|
|
43
|
+
${url}
|
|
44
|
+
${createHash("sha256").update(body).digest("hex")}`);
|
|
45
|
+
function verifyRead(r) {
|
|
46
|
+
const [id, sig, extra] = (r.header(SIGNATURE_HEADER) ?? "").split(".");
|
|
47
|
+
const time = Number(r.header(TIME_HEADER));
|
|
48
|
+
const environment = r.header(ENVIRONMENT_HEADER);
|
|
49
|
+
const key = id && Object.hasOwn(r.keys, id) ? r.keys[id] : void 0;
|
|
50
|
+
if (!key || !sig || extra !== void 0 || !environment || !Number.isInteger(time) || Math.abs(r.nowS - time) > MAX_SKEW_S) return false;
|
|
51
|
+
try {
|
|
52
|
+
return verify(null, signed(environment, time, r.url, r.body), key, Buffer.from(sig, "base64url"));
|
|
53
|
+
} catch {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
var CONFIG_HEADER = "x-soron-config";
|
|
58
|
+
var VERSION_HEADER = "x-soron-version";
|
|
59
|
+
var IDENTIFIER = /^[A-Za-z][A-Za-z0-9_]{0,62}$/;
|
|
60
|
+
var wordsOf = (name) => name.replace(/([A-Z]+)([A-Z][a-z])/g, "$1_$2").replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
|
|
61
|
+
var identifiers = (v, min, max) => {
|
|
62
|
+
if (!Array.isArray(v) || v.length < min || v.length > max) return void 0;
|
|
63
|
+
const out = [];
|
|
64
|
+
for (const x of v) {
|
|
65
|
+
if (typeof x !== "string" || !IDENTIFIER.test(x)) return void 0;
|
|
66
|
+
out.push(x);
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
};
|
|
70
|
+
var only = (o, keys2) => Object.keys(o).every((k) => keys2.includes(k));
|
|
71
|
+
var SECRET_TABLES = /* @__PURE__ */ new Set([
|
|
72
|
+
...["session", "sessions", "user_session", "user_sessions", "account", "accounts", "verification", "verifications"],
|
|
73
|
+
...["verificationtoken", "verification_token", "verification_tokens", "two_factor", "twofactor", "passkey", "passkeys"],
|
|
74
|
+
...["apikey", "apikeys", "api_key", "api_keys", "jwks", "authenticator", "authenticators", "two_factors", "twofactors"],
|
|
75
|
+
...["invitation", "invitations", "magic_link", "magic_links", "magiclink", "magiclinks"]
|
|
76
|
+
]);
|
|
77
|
+
var SECRET_WORDS = /* @__PURE__ */ new Set([
|
|
78
|
+
...["password", "passwd", "pwd", "passphrase", "token", "tokens", "secret", "secrets", "hash", "hashed", "salt"],
|
|
79
|
+
...["otp", "totp", "mfa", "pin", "jwt", "key", "keys", "credential", "credentials", "passcode", "pw", "cvv", "cvc", "iban"],
|
|
80
|
+
...["seed", "nonce", "hmac", "bearer", "ciphertext", "encrypted"]
|
|
81
|
+
]);
|
|
82
|
+
var SECRET_INSIDE = /password|passwd|pwd|passcode|token|secret|apikey|privatekey|jwt|ciphertext|encrypted|card_?number|magic_?link|(recovery|backup|verification|reset|confirmation|auth|login)_codes?/;
|
|
83
|
+
var isSecretColumn = (name) => {
|
|
84
|
+
const words = wordsOf(name);
|
|
85
|
+
return words.split("_").some((w) => SECRET_WORDS.has(w.replace(/\d+$/, ""))) || SECRET_INSIDE.test(words) || SECRET_INSIDE.test(name.toLowerCase());
|
|
86
|
+
};
|
|
87
|
+
var isSecretTable = (table) => {
|
|
88
|
+
const t = wordsOf(table);
|
|
89
|
+
return SECRET_TABLES.has(t) || SECRET_TABLES.has(table.toLowerCase()) || [...SECRET_TABLES].some((s) => t.endsWith(`_${s}`) || t.startsWith(`${s}_`)) || isSecretColumn(table);
|
|
90
|
+
};
|
|
91
|
+
function secretIn(reads) {
|
|
92
|
+
for (const { table, columns } of reads) {
|
|
93
|
+
if (isSecretTable(table)) return table;
|
|
94
|
+
const column = columns.find(isSecretColumn);
|
|
95
|
+
if (column !== void 0) return `${table}.${column}`;
|
|
96
|
+
}
|
|
97
|
+
return void 0;
|
|
98
|
+
}
|
|
99
|
+
function nameOutside(config) {
|
|
100
|
+
for (const [table, columns] of Object.entries(config.names)) {
|
|
101
|
+
const read = config.reads.find((r) => r.table === table);
|
|
102
|
+
const column = columns.find((c) => !read?.columns.includes(c));
|
|
103
|
+
if (column !== void 0) return `${table}.${column}`;
|
|
104
|
+
}
|
|
105
|
+
return void 0;
|
|
106
|
+
}
|
|
107
|
+
function readConfig(value) {
|
|
108
|
+
const config = configShape(value);
|
|
109
|
+
return config && secretIn(config.reads) === void 0 && nameOutside(config) === void 0 ? config : void 0;
|
|
110
|
+
}
|
|
111
|
+
function configProblem(value) {
|
|
112
|
+
const config = configShape(value);
|
|
113
|
+
if (!config) return "not valid: an unknown key, a name outside letters, digits and _, or a list past its bound";
|
|
114
|
+
const secret = secretIn(config.reads);
|
|
115
|
+
if (secret !== void 0) return `lists ${secret}, which holds a secret: Soron never reads one`;
|
|
116
|
+
const outside = nameOutside(config);
|
|
117
|
+
return outside === void 0 ? void 0 : `masks ${outside}, which it does not read: a masked name is a column of a read, spelled as the read spells it`;
|
|
118
|
+
}
|
|
119
|
+
function configShape(value) {
|
|
120
|
+
if (!isObject(value) || !only(value, ["$schema", "language", "reads", "names"])) return void 0;
|
|
121
|
+
const { $schema, language, reads = [], names = {} } = value;
|
|
122
|
+
if ($schema !== void 0 && (typeof $schema !== "string" || $schema.length > 500)) return void 0;
|
|
123
|
+
if (language !== "fr" && language !== "en") return void 0;
|
|
124
|
+
if (!Array.isArray(reads) || reads.length > 50 || !isObject(names)) return void 0;
|
|
125
|
+
const config = { language, reads: [], names: {} };
|
|
126
|
+
for (const r of reads) {
|
|
127
|
+
if (!isObject(r) || !only(r, ["table", "columns"]) || typeof r["table"] !== "string" || !IDENTIFIER.test(r["table"])) return void 0;
|
|
128
|
+
const columns = identifiers(r["columns"], 1, 100);
|
|
129
|
+
if (!columns) return void 0;
|
|
130
|
+
config.reads.push({ table: r["table"], columns });
|
|
131
|
+
}
|
|
132
|
+
for (const [table, list] of Object.entries(names)) {
|
|
133
|
+
const columns = identifiers(list, 0, 100);
|
|
134
|
+
if (!IDENTIFIER.test(table) || !columns) return void 0;
|
|
135
|
+
config.names[table] = columns;
|
|
136
|
+
}
|
|
137
|
+
return config;
|
|
138
|
+
}
|
|
139
|
+
var nameKey = (table, column) => `${table}__${column}`;
|
|
140
|
+
var canonical = (v) => Array.isArray(v) ? `[${v.map(canonical).join(",")}]` : isObject(v) ? `{${Object.entries(v).sort(([a], [b]) => a < b ? -1 : 1).map(([k, x]) => `${JSON.stringify(k)}:${canonical(x)}`).join(",")}}` : JSON.stringify(v);
|
|
141
|
+
var configFingerprint = (value) => createHash("sha256").update(canonical(value)).digest("hex").slice(0, 32);
|
|
142
|
+
|
|
143
|
+
// src/doors.ts
|
|
144
|
+
var MAX_ROWS = 50;
|
|
145
|
+
var MAX_READ_BODY = 8e3;
|
|
146
|
+
var MAX_REPLY_CHARS = 2e6;
|
|
147
|
+
var MIN_SECRET = 32;
|
|
148
|
+
var AdapterIncomplete = class extends Error {
|
|
149
|
+
constructor() {
|
|
150
|
+
super("adapter_incomplete");
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
var Refusal = class extends Error {
|
|
154
|
+
status;
|
|
155
|
+
constructor(status, code) {
|
|
156
|
+
super(code);
|
|
157
|
+
this.status = status;
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
var serialize = (body) => JSON.stringify(body, (_, v) => typeof v === "bigint" ? v.toString() : v);
|
|
161
|
+
var reply = (status, body, headers = {}) => new Response(typeof body === "string" ? body : serialize(body), {
|
|
162
|
+
status,
|
|
163
|
+
headers: { "content-type": "application/json", "cache-control": "no-store", ...headers }
|
|
164
|
+
});
|
|
165
|
+
var isObject2 = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
166
|
+
var isScalar = (v) => v === null || ["string", "number", "boolean"].includes(typeof v);
|
|
167
|
+
var jsonOnly = (request) => {
|
|
168
|
+
if (!(request.headers.get("content-type") ?? "").startsWith("application/json")) throw new Refusal(415, "json_only");
|
|
169
|
+
};
|
|
170
|
+
var notFromBrowser = (request) => {
|
|
171
|
+
if (request.headers.get("sec-fetch-site") !== null) throw new Refusal(403, "forbidden");
|
|
172
|
+
};
|
|
173
|
+
var bearer = (request) => /^Bearer ([A-Za-z0-9_.-]{1,2048})$/.exec(request.headers.get("authorization") ?? "")?.[1];
|
|
174
|
+
async function boundedText(request, max) {
|
|
175
|
+
if (Number(request.headers.get("content-length") ?? 0) > max) throw new Refusal(413, "too_large");
|
|
176
|
+
const text = await request.text();
|
|
177
|
+
if (text.length > max) throw new Refusal(413, "too_large");
|
|
178
|
+
return text;
|
|
179
|
+
}
|
|
180
|
+
function readOf(text, config) {
|
|
181
|
+
let body;
|
|
182
|
+
try {
|
|
183
|
+
body = JSON.parse(text);
|
|
184
|
+
} catch {
|
|
185
|
+
throw new Refusal(400, "bad_request");
|
|
186
|
+
}
|
|
187
|
+
if (!isObject2(body) || typeof body["procedure"] !== "string" || !isObject2(body["input"])) throw new Refusal(400, "bad_request");
|
|
188
|
+
const procedure = body["procedure"];
|
|
189
|
+
const read = config.reads.find((r) => r.table === procedure);
|
|
190
|
+
if (!read) throw new Refusal(400, "not_listed");
|
|
191
|
+
const where = {};
|
|
192
|
+
for (const [column, value] of Object.entries(body["input"])) {
|
|
193
|
+
if (!read.columns.includes(column) || !isScalar(value)) throw new Refusal(400, "not_listed");
|
|
194
|
+
where[column] = value;
|
|
195
|
+
}
|
|
196
|
+
return { table: read.table, columns: read.columns, where, limit: MAX_ROWS };
|
|
197
|
+
}
|
|
198
|
+
function createRoutes(d) {
|
|
199
|
+
const nowS = () => Math.floor((d.now ?? Date.now)() / 1e3);
|
|
200
|
+
const secret = () => {
|
|
201
|
+
const s = d.secret();
|
|
202
|
+
if (s === void 0 || s.length < MIN_SECRET) throw new Refusal(503, "unavailable");
|
|
203
|
+
return s;
|
|
204
|
+
};
|
|
205
|
+
const named = async (request) => {
|
|
206
|
+
const token2 = bearer(request);
|
|
207
|
+
const staff = token2 === void 0 ? void 0 : verifyBadge(secret(), token2, nowS());
|
|
208
|
+
if (!staff || !await d.adapter.sessionAlive(staff)) throw new Refusal(401, "unauthorized");
|
|
209
|
+
if (!await d.adapter.isStaff(staff.userId)) throw new Refusal(403, "forbidden");
|
|
210
|
+
return staff;
|
|
211
|
+
};
|
|
212
|
+
const token = async (request) => {
|
|
213
|
+
const site = request.headers.get("sec-fetch-site");
|
|
214
|
+
if (site !== null && site !== "same-origin" || request.headers.get("origin") !== new URL(request.url).origin) throw new Refusal(403, "forbidden");
|
|
215
|
+
jsonOnly(request);
|
|
216
|
+
const key = secret();
|
|
217
|
+
const staff = await d.adapter.session(request);
|
|
218
|
+
if (!staff) throw new Refusal(401, "unauthorized");
|
|
219
|
+
if (!await d.adapter.isStaff(staff.userId)) throw new Refusal(403, "forbidden");
|
|
220
|
+
const badge = signBadge(key, { sessionId: staff.sessionId, userId: staff.userId }, nowS());
|
|
221
|
+
const res = reply(200, { token: badge.token, expiresAt: new Date(badge.expiresAt * 1e3).toISOString() });
|
|
222
|
+
for (const c of staff.cookies ?? []) res.headers.append("set-cookie", c);
|
|
223
|
+
return res;
|
|
224
|
+
};
|
|
225
|
+
const whoami = async (request) => {
|
|
226
|
+
notFromBrowser(request);
|
|
227
|
+
jsonOnly(request);
|
|
228
|
+
const staff = await named(request);
|
|
229
|
+
return reply(200, { userId: staff.userId });
|
|
230
|
+
};
|
|
231
|
+
const read = async (request) => {
|
|
232
|
+
notFromBrowser(request);
|
|
233
|
+
jsonOnly(request);
|
|
234
|
+
const text = await boundedText(request, MAX_READ_BODY);
|
|
235
|
+
if (!verifyRead({ keys: d.keys(), url: request.url, body: text, header: (n) => request.headers.get(n), nowS: nowS() })) throw new Refusal(401, "unauthorized");
|
|
236
|
+
const headers = { [CONFIG_HEADER]: configFingerprint(d.adapter.config) };
|
|
237
|
+
try {
|
|
238
|
+
const config = readConfig(d.adapter.config);
|
|
239
|
+
if (!config) return reply(503, { error: "config_invalid", problem: configProblem(d.adapter.config) }, headers);
|
|
240
|
+
await named(request);
|
|
241
|
+
const query = readOf(text, config);
|
|
242
|
+
const names = config.names[query.table] ?? [];
|
|
243
|
+
const rows = (await d.adapter.rows(query)).slice(0, MAX_ROWS);
|
|
244
|
+
const result = rows.map((row) => Object.fromEntries(query.columns.map((c) => [names.includes(c) ? nameKey(query.table, c) : c, row[c] ?? null])));
|
|
245
|
+
const body = serialize({ result });
|
|
246
|
+
if (body.length > MAX_REPLY_CHARS) throw new Refusal(500, "too_large");
|
|
247
|
+
return reply(200, body, headers);
|
|
248
|
+
} catch (e) {
|
|
249
|
+
return e instanceof Refusal ? reply(e.status, { error: e.message }, headers) : reply(500, { error: "error" }, headers);
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
return {
|
|
253
|
+
// Every reply says this package's version: under our floor, our service reads and admits no more.
|
|
254
|
+
async POST(request) {
|
|
255
|
+
const answered = await (async () => {
|
|
256
|
+
try {
|
|
257
|
+
const door = new URL(request.url).pathname.split("/").at(-1);
|
|
258
|
+
if (door === "token") return await token(request);
|
|
259
|
+
if (door === "whoami") return await whoami(request);
|
|
260
|
+
if (door === "read") return await read(request);
|
|
261
|
+
return reply(404, { error: "not_found" });
|
|
262
|
+
} catch (e) {
|
|
263
|
+
if (e instanceof AdapterIncomplete) return reply(503, { error: "adapter_incomplete" });
|
|
264
|
+
return e instanceof Refusal ? reply(e.status, { error: e.message }) : reply(500, { error: "error" });
|
|
265
|
+
}
|
|
266
|
+
})();
|
|
267
|
+
answered.headers.set(VERSION_HEADER, d.version);
|
|
268
|
+
return answered;
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// src/routes.ts
|
|
274
|
+
var PINNED = typeof define_SORON_READ_KEYS_default === "object" ? define_SORON_READ_KEYS_default : {};
|
|
275
|
+
var pinned;
|
|
276
|
+
var keys = () => pinned ??= Object.fromEntries(Object.entries(PINNED).map(([id, spki]) => [id, createPublicKey({ key: Buffer.from(spki, "base64"), format: "der", type: "spki" })]));
|
|
277
|
+
function soronRoutes(adapter) {
|
|
278
|
+
return createRoutes({ adapter, keys, secret: () => process.env["SORON_BADGE_SECRET"], version: "0.1.0" });
|
|
279
|
+
}
|
|
280
|
+
export {
|
|
281
|
+
AdapterIncomplete,
|
|
282
|
+
soronRoutes
|
|
283
|
+
};
|