@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/assets.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type BrowserModulesOptions } from "./npm.js";
|
|
2
|
+
export declare function rewriteImports(js: string): string;
|
|
3
|
+
export declare function runtimeAssets(): Record<string, string>;
|
|
4
|
+
export declare function islandAssets(modules: Record<string, string>, npm?: BrowserModulesOptions): Record<string, string>;
|
package/dist/assets.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { readdirSync, readFileSync } from "node:fs";
|
|
2
|
+
import { stripTypeScriptTypes } from "node:module";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { browserModules } from "./npm.js";
|
|
6
|
+
const RT = "/_weft/rt/";
|
|
7
|
+
export function rewriteImports(js) {
|
|
8
|
+
let out = "";
|
|
9
|
+
let i = 0;
|
|
10
|
+
while (i < js.length) {
|
|
11
|
+
const ts = js.indexOf(".ts", i);
|
|
12
|
+
if (ts < 0) {
|
|
13
|
+
out += js.slice(i);
|
|
14
|
+
break;
|
|
15
|
+
}
|
|
16
|
+
const q = js[ts + 3];
|
|
17
|
+
if (q !== '"' && q !== "'") {
|
|
18
|
+
out += js.slice(i, ts + 3);
|
|
19
|
+
i = ts + 3;
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
const open = js.lastIndexOf(q, ts - 1);
|
|
23
|
+
const spec = open < 0 ? "" : js.slice(open + 1, ts);
|
|
24
|
+
if (open >= 0 && (spec.startsWith("./") || spec.startsWith("../"))) {
|
|
25
|
+
out += js.slice(i, ts) + ".js";
|
|
26
|
+
i = ts + 3;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
out += js.slice(i, ts + 3);
|
|
30
|
+
i = ts + 3;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
export function runtimeAssets() {
|
|
36
|
+
const src = dirname(fileURLToPath(import.meta.resolve("@weft-framework/runtime")));
|
|
37
|
+
const out = {};
|
|
38
|
+
for (const name of readdirSync(src)) {
|
|
39
|
+
if (name.endsWith(".ts") && !name.endsWith(".d.ts")) {
|
|
40
|
+
const ts = readFileSync(join(src, name), "utf8");
|
|
41
|
+
out[RT + name.replace(/\.ts$/, ".js")] = rewriteImports(stripTypeScriptTypes(ts, { mode: "strip" }));
|
|
42
|
+
}
|
|
43
|
+
else if (name.endsWith(".js")) {
|
|
44
|
+
out[RT + name] ??= rewriteImports(readFileSync(join(src, name), "utf8"));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
out[ISLANDS_ENTRY] = "export const islands = {};\n";
|
|
48
|
+
out["/_weft/client.js"] =
|
|
49
|
+
`import { boot } from "./rt/boot.js";\n` + `import { islands } from "./islands.js";\n` + `boot({ islands });\n`;
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
const ISLANDS_ENTRY = "/_weft/islands.js";
|
|
53
|
+
const ISLANDS_DIR = "/_weft/islands/";
|
|
54
|
+
const ISLANDS_EXPORT = /^export const islands = \{([\w$.,:" ]*)\};$/m;
|
|
55
|
+
export function islandAssets(modules, npm) {
|
|
56
|
+
const out = {};
|
|
57
|
+
const dirs = {};
|
|
58
|
+
const owner = new Map();
|
|
59
|
+
const imports = [];
|
|
60
|
+
const parts = [];
|
|
61
|
+
for (const [i, rel] of Object.keys(modules).sort().entries()) {
|
|
62
|
+
const ts = modules[rel];
|
|
63
|
+
const keys = (ISLANDS_EXPORT.exec(ts)?.[1] ?? "")
|
|
64
|
+
.split(",")
|
|
65
|
+
.map((n) => n.split(":")[0].trim().replace(/^"(.*)"$/, "$1"))
|
|
66
|
+
.filter((n) => n !== "");
|
|
67
|
+
for (const key of keys) {
|
|
68
|
+
const prev = owner.get(key);
|
|
69
|
+
if (prev !== undefined && !/\.[0-9a-f]{8}$/.test(key)) {
|
|
70
|
+
throw new Error(`island ${key} is defined in both ${prev} and ${rel}`);
|
|
71
|
+
}
|
|
72
|
+
owner.set(key, rel);
|
|
73
|
+
}
|
|
74
|
+
const js = stripTypeScriptTypes(ts, { mode: "strip" }).replace(/(\bfrom\s*)(["'])@weft-framework\/runtime\2/g, `$1$2${RT}index.js$2`);
|
|
75
|
+
const url = encodeURI(ISLANDS_DIR + rel.replace(/\.client\.ts$/, ".js"));
|
|
76
|
+
out[url] = js;
|
|
77
|
+
if (npm)
|
|
78
|
+
dirs[url] = join(npm.root, dirname(rel));
|
|
79
|
+
imports.push(`import { islands as m${i} } from ${JSON.stringify(url)};\n`);
|
|
80
|
+
parts.push(`...m${i}`);
|
|
81
|
+
}
|
|
82
|
+
out[ISLANDS_ENTRY] = `${imports.join("")}export const islands = { ${parts.join(", ")} };\n`;
|
|
83
|
+
if (!npm) {
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
const input = {};
|
|
87
|
+
for (const url of Object.keys(dirs))
|
|
88
|
+
input[url] = { js: out[url], dir: dirs[url] };
|
|
89
|
+
const resolved = browserModules(input, npm);
|
|
90
|
+
return { ...out, ...resolved.modules, ...resolved.assets };
|
|
91
|
+
}
|
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import type { SqlDatabase } from "@weft-framework/data";
|
|
2
|
+
import type { PageContext } from "./context.js";
|
|
3
|
+
import type { EmailSignIn } from "./email-signin.js";
|
|
4
|
+
import type { Identity, Provider } from "./oauth.js";
|
|
5
|
+
import type { Saml } from "./saml.js";
|
|
6
|
+
export interface SessionRecord {
|
|
7
|
+
user: string;
|
|
8
|
+
created: number;
|
|
9
|
+
seen: number;
|
|
10
|
+
tenant?: string;
|
|
11
|
+
pending?: true;
|
|
12
|
+
mfa?: true;
|
|
13
|
+
agent?: string;
|
|
14
|
+
viewAs?: {
|
|
15
|
+
user: string;
|
|
16
|
+
tenant?: string;
|
|
17
|
+
until: number;
|
|
18
|
+
dev?: true;
|
|
19
|
+
};
|
|
20
|
+
sso?: {
|
|
21
|
+
c: string;
|
|
22
|
+
n: string;
|
|
23
|
+
f: string | null;
|
|
24
|
+
s: string | null;
|
|
25
|
+
nq?: string;
|
|
26
|
+
spnq?: string;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export interface SessionInfo {
|
|
30
|
+
id: string;
|
|
31
|
+
created: number;
|
|
32
|
+
seen: number;
|
|
33
|
+
agent: string | null;
|
|
34
|
+
current: boolean;
|
|
35
|
+
mfa: boolean;
|
|
36
|
+
pending: boolean;
|
|
37
|
+
}
|
|
38
|
+
export interface SessionBroadcast {
|
|
39
|
+
publish(message: string): unknown;
|
|
40
|
+
subscribe(onMessage: (message: string) => void): (() => void) | void;
|
|
41
|
+
}
|
|
42
|
+
export interface AuthState {
|
|
43
|
+
user: string;
|
|
44
|
+
pending: boolean;
|
|
45
|
+
mfa: boolean;
|
|
46
|
+
tenant?: string;
|
|
47
|
+
}
|
|
48
|
+
export interface SecondFactor {
|
|
49
|
+
readonly page: string;
|
|
50
|
+
needed(user: string, tenant?: string): Maybe<boolean>;
|
|
51
|
+
}
|
|
52
|
+
type Maybe<T> = T | Promise<T>;
|
|
53
|
+
export interface SessionStore {
|
|
54
|
+
get(sid: string): Maybe<SessionRecord | null | undefined>;
|
|
55
|
+
set(sid: string, record: SessionRecord): Maybe<void>;
|
|
56
|
+
delete(sid: string): Maybe<void>;
|
|
57
|
+
forUser?(user: string): Maybe<{
|
|
58
|
+
sid: string;
|
|
59
|
+
record: SessionRecord;
|
|
60
|
+
}[]>;
|
|
61
|
+
}
|
|
62
|
+
export declare class MemorySessionStore implements SessionStore {
|
|
63
|
+
private readonly map;
|
|
64
|
+
private readonly max;
|
|
65
|
+
constructor(maxEntries?: number);
|
|
66
|
+
get(sid: string): SessionRecord | null;
|
|
67
|
+
set(sid: string, record: SessionRecord): void;
|
|
68
|
+
delete(sid: string): void;
|
|
69
|
+
forUser(user: string): {
|
|
70
|
+
sid: string;
|
|
71
|
+
record: SessionRecord;
|
|
72
|
+
}[];
|
|
73
|
+
get size(): number;
|
|
74
|
+
}
|
|
75
|
+
export declare class SqlSessionStore implements SessionStore {
|
|
76
|
+
private readonly db;
|
|
77
|
+
private readonly table;
|
|
78
|
+
constructor(db: SqlDatabase, table?: string);
|
|
79
|
+
get(sid: string): SessionRecord | null;
|
|
80
|
+
forUser(user: string): {
|
|
81
|
+
sid: string;
|
|
82
|
+
record: SessionRecord;
|
|
83
|
+
}[];
|
|
84
|
+
private static record;
|
|
85
|
+
set(sid: string, r: SessionRecord): void;
|
|
86
|
+
delete(sid: string): void;
|
|
87
|
+
sweep(before: number): void;
|
|
88
|
+
}
|
|
89
|
+
export interface AuthOptions {
|
|
90
|
+
store?: SessionStore;
|
|
91
|
+
idleMs?: number;
|
|
92
|
+
maxAgeMs?: number;
|
|
93
|
+
user?(id: string, tenant?: string): unknown;
|
|
94
|
+
providers?: Provider[];
|
|
95
|
+
signIn?(identity: Identity): string | null | undefined | Promise<string | null | undefined>;
|
|
96
|
+
signInError?: string;
|
|
97
|
+
saml?: Saml;
|
|
98
|
+
email?: EmailSignIn;
|
|
99
|
+
twoFactor?: SecondFactor;
|
|
100
|
+
revocations?: SessionBroadcast;
|
|
101
|
+
viewAs?(operator: string, user: string): Maybe<boolean>;
|
|
102
|
+
}
|
|
103
|
+
export interface AuthControl {
|
|
104
|
+
refused: string | null;
|
|
105
|
+
login(user: string, multiFactor?: boolean): Promise<LoginResult>;
|
|
106
|
+
logout(): Promise<void>;
|
|
107
|
+
switchTenant(tenant: string | null): Promise<boolean>;
|
|
108
|
+
signInWith(identity: Identity): Promise<LoginResult | "refused">;
|
|
109
|
+
state(): Promise<AuthState | null>;
|
|
110
|
+
completeTwoFactor(): Promise<boolean>;
|
|
111
|
+
sessions(): Promise<SessionInfo[]>;
|
|
112
|
+
revoke(user: string, o: {
|
|
113
|
+
id?: string;
|
|
114
|
+
keep: boolean;
|
|
115
|
+
}): Promise<number>;
|
|
116
|
+
startViewAs(user: string, tenant: string | undefined, until: number): Promise<boolean>;
|
|
117
|
+
stopViewAs(): Promise<boolean>;
|
|
118
|
+
}
|
|
119
|
+
export type LoginResult = "ok" | "two-factor";
|
|
120
|
+
export declare function attachAuth(ctx: PageContext, control: AuthControl): void;
|
|
121
|
+
export declare function signInWith(ctx: PageContext, identity: Identity): Promise<LoginResult | "refused">;
|
|
122
|
+
export declare function authState(ctx: PageContext): Promise<AuthState | null>;
|
|
123
|
+
export declare function listSessions(ctx: PageContext): Promise<SessionInfo[]>;
|
|
124
|
+
export declare function revokeSession(ctx: PageContext, id: string): Promise<boolean>;
|
|
125
|
+
export declare function signOutEverywhere(ctx: PageContext, o?: {
|
|
126
|
+
keepThis?: boolean;
|
|
127
|
+
}): Promise<number>;
|
|
128
|
+
export declare function signOutUser(ctx: PageContext, user: string): Promise<number>;
|
|
129
|
+
export declare function completeTwoFactor(ctx: PageContext): Promise<boolean>;
|
|
130
|
+
export declare function switchTenant(ctx: PageContext, tenant: string | null): Promise<boolean>;
|
|
131
|
+
export declare function login(ctx: PageContext, userId: string, opts?: {
|
|
132
|
+
multiFactor?: boolean;
|
|
133
|
+
}): Promise<LoginResult>;
|
|
134
|
+
export declare function logout(ctx: PageContext): Promise<void>;
|
|
135
|
+
export interface Viewing {
|
|
136
|
+
user: string;
|
|
137
|
+
tenant?: string;
|
|
138
|
+
operator: string;
|
|
139
|
+
until: number;
|
|
140
|
+
}
|
|
141
|
+
export declare function noteViewing(actor: object, v: Viewing): void;
|
|
142
|
+
export declare function viewingAs(ctx: PageContext): Viewing | null;
|
|
143
|
+
export declare function allowWhileViewing<A extends object>(action: A): A;
|
|
144
|
+
export declare function runsWhileViewing(action: object): boolean;
|
|
145
|
+
export declare function viewAs(ctx: PageContext, user: string, o: {
|
|
146
|
+
tenant?: string;
|
|
147
|
+
until: number;
|
|
148
|
+
}): Promise<boolean>;
|
|
149
|
+
export declare function stopViewingAs(ctx: PageContext): Promise<boolean>;
|
|
150
|
+
export {};
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
export class MemorySessionStore {
|
|
2
|
+
map = new Map();
|
|
3
|
+
max;
|
|
4
|
+
constructor(maxEntries = 100_000) {
|
|
5
|
+
this.max = maxEntries;
|
|
6
|
+
}
|
|
7
|
+
get(sid) {
|
|
8
|
+
const rec = this.map.get(sid);
|
|
9
|
+
if (!rec) {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
this.map.delete(sid);
|
|
13
|
+
this.map.set(sid, rec);
|
|
14
|
+
return rec;
|
|
15
|
+
}
|
|
16
|
+
set(sid, record) {
|
|
17
|
+
this.map.delete(sid);
|
|
18
|
+
this.map.set(sid, record);
|
|
19
|
+
while (this.map.size > this.max) {
|
|
20
|
+
this.map.delete(this.map.keys().next().value);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
delete(sid) {
|
|
24
|
+
this.map.delete(sid);
|
|
25
|
+
}
|
|
26
|
+
forUser(user) {
|
|
27
|
+
const out = [];
|
|
28
|
+
for (const [sid, record] of this.map) {
|
|
29
|
+
if (record.user === user)
|
|
30
|
+
out.push({ sid, record });
|
|
31
|
+
}
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
get size() {
|
|
35
|
+
return this.map.size;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export class SqlSessionStore {
|
|
39
|
+
db;
|
|
40
|
+
table;
|
|
41
|
+
constructor(db, table = "weft_sessions") {
|
|
42
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(table)) {
|
|
43
|
+
throw new Error(`@weft-framework/server: not a valid table name: ${table}`);
|
|
44
|
+
}
|
|
45
|
+
this.db = db;
|
|
46
|
+
this.table = `"${table}"`;
|
|
47
|
+
db.exec(`CREATE TABLE IF NOT EXISTS ${this.table} (sid TEXT PRIMARY KEY NOT NULL, user TEXT NOT NULL, ` +
|
|
48
|
+
`created INTEGER NOT NULL, seen INTEGER NOT NULL, tenant TEXT, pending INTEGER, mfa INTEGER, agent TEXT, view_as TEXT)`);
|
|
49
|
+
const cols = db.prepare(`SELECT name FROM pragma_table_info(?)`).all(table);
|
|
50
|
+
for (const [name, type] of [
|
|
51
|
+
["tenant", "TEXT"],
|
|
52
|
+
["pending", "INTEGER"],
|
|
53
|
+
["mfa", "INTEGER"],
|
|
54
|
+
["agent", "TEXT"],
|
|
55
|
+
["view_as", "TEXT"],
|
|
56
|
+
]) {
|
|
57
|
+
if (!cols.some((c) => c.name === name)) {
|
|
58
|
+
db.exec(`ALTER TABLE ${this.table} ADD COLUMN ${name} ${type}`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
db.exec(`CREATE INDEX IF NOT EXISTS "${table}_user" ON ${this.table} (user)`);
|
|
62
|
+
}
|
|
63
|
+
get(sid) {
|
|
64
|
+
const row = this.db
|
|
65
|
+
.prepare(`SELECT user, created, seen, tenant, pending, mfa, agent, view_as FROM ${this.table} WHERE sid = ?`)
|
|
66
|
+
.all(sid)[0];
|
|
67
|
+
return row ? SqlSessionStore.record(row) : null;
|
|
68
|
+
}
|
|
69
|
+
forUser(user) {
|
|
70
|
+
const rows = this.db
|
|
71
|
+
.prepare(`SELECT sid, user, created, seen, tenant, pending, mfa, agent, view_as FROM ${this.table} WHERE user = ?`)
|
|
72
|
+
.all(user);
|
|
73
|
+
return rows.map((row) => ({ sid: String(row.sid), record: SqlSessionStore.record(row) }));
|
|
74
|
+
}
|
|
75
|
+
static record(row) {
|
|
76
|
+
const rec = { user: String(row.user), created: Number(row.created), seen: Number(row.seen) };
|
|
77
|
+
if (row.tenant !== null) {
|
|
78
|
+
rec.tenant = String(row.tenant);
|
|
79
|
+
}
|
|
80
|
+
if (Number(row.pending ?? 0) === 1) {
|
|
81
|
+
rec.pending = true;
|
|
82
|
+
}
|
|
83
|
+
if (Number(row.mfa ?? 0) === 1) {
|
|
84
|
+
rec.mfa = true;
|
|
85
|
+
}
|
|
86
|
+
if (row.agent !== null && row.agent !== undefined) {
|
|
87
|
+
rec.agent = String(row.agent);
|
|
88
|
+
}
|
|
89
|
+
if (typeof row.view_as === "string") {
|
|
90
|
+
try {
|
|
91
|
+
const v = JSON.parse(row.view_as);
|
|
92
|
+
if (typeof v.user === "string" && typeof v.until === "number") {
|
|
93
|
+
rec.viewAs = { user: v.user, until: v.until, ...(typeof v.tenant === "string" ? { tenant: v.tenant } : {}) };
|
|
94
|
+
if (v.dev === true)
|
|
95
|
+
rec.viewAs.dev = true;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return rec;
|
|
102
|
+
}
|
|
103
|
+
set(sid, r) {
|
|
104
|
+
this.db
|
|
105
|
+
.prepare(`INSERT INTO ${this.table} (sid, user, created, seen, tenant, pending, mfa, agent, view_as) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ` +
|
|
106
|
+
`ON CONFLICT(sid) DO UPDATE SET user = excluded.user, created = excluded.created, seen = excluded.seen, ` +
|
|
107
|
+
`tenant = excluded.tenant, pending = excluded.pending, mfa = excluded.mfa, agent = excluded.agent, view_as = excluded.view_as`)
|
|
108
|
+
.run(sid, r.user, r.created, r.seen, r.tenant ?? null, r.pending ? 1 : null, r.mfa ? 1 : null, r.agent ?? null, r.viewAs ? JSON.stringify(r.viewAs) : null);
|
|
109
|
+
}
|
|
110
|
+
delete(sid) {
|
|
111
|
+
this.db.prepare(`DELETE FROM ${this.table} WHERE sid = ?`).run(sid);
|
|
112
|
+
}
|
|
113
|
+
sweep(before) {
|
|
114
|
+
this.db.prepare(`DELETE FROM ${this.table} WHERE seen < ?`).run(before);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const controls = new WeakMap();
|
|
118
|
+
export function attachAuth(ctx, control) {
|
|
119
|
+
controls.set(ctx, control);
|
|
120
|
+
}
|
|
121
|
+
function control(ctx) {
|
|
122
|
+
const c = controls.get(ctx);
|
|
123
|
+
if (!c) {
|
|
124
|
+
throw new Error("@weft-framework/server: login/logout/switchTenant need the context of a request being handled");
|
|
125
|
+
}
|
|
126
|
+
if (c.refused !== null) {
|
|
127
|
+
throw new Error(`@weft-framework/server: login/logout/switchTenant ${c.refused}`);
|
|
128
|
+
}
|
|
129
|
+
return c;
|
|
130
|
+
}
|
|
131
|
+
export function signInWith(ctx, identity) {
|
|
132
|
+
return control(ctx).signInWith(identity);
|
|
133
|
+
}
|
|
134
|
+
export function authState(ctx) {
|
|
135
|
+
const c = controls.get(ctx);
|
|
136
|
+
if (!c) {
|
|
137
|
+
throw new Error("@weft-framework/server: the login state needs the context of a request being handled");
|
|
138
|
+
}
|
|
139
|
+
return c.state();
|
|
140
|
+
}
|
|
141
|
+
export function listSessions(ctx) {
|
|
142
|
+
const c = controls.get(ctx);
|
|
143
|
+
if (!c) {
|
|
144
|
+
throw new Error("@weft-framework/server: listSessions needs the context of a request being handled");
|
|
145
|
+
}
|
|
146
|
+
return c.sessions();
|
|
147
|
+
}
|
|
148
|
+
export async function revokeSession(ctx, id) {
|
|
149
|
+
if (typeof id !== "string" || id === "")
|
|
150
|
+
return false;
|
|
151
|
+
const c = control(ctx);
|
|
152
|
+
const me = await c.state();
|
|
153
|
+
if (!me || me.pending)
|
|
154
|
+
return false;
|
|
155
|
+
return (await c.revoke(me.user, { id, keep: true })) > 0;
|
|
156
|
+
}
|
|
157
|
+
export async function signOutEverywhere(ctx, o = {}) {
|
|
158
|
+
const c = control(ctx);
|
|
159
|
+
const me = await c.state();
|
|
160
|
+
if (!me || me.pending)
|
|
161
|
+
return 0;
|
|
162
|
+
return c.revoke(me.user, { keep: o.keepThis !== false });
|
|
163
|
+
}
|
|
164
|
+
export function signOutUser(ctx, user) {
|
|
165
|
+
if (typeof user !== "string" || user === "") {
|
|
166
|
+
throw new TypeError("@weft-framework/server: signOutUser needs a user id");
|
|
167
|
+
}
|
|
168
|
+
return control(ctx).revoke(user, { keep: false });
|
|
169
|
+
}
|
|
170
|
+
export function completeTwoFactor(ctx) {
|
|
171
|
+
return control(ctx).completeTwoFactor();
|
|
172
|
+
}
|
|
173
|
+
export function switchTenant(ctx, tenant) {
|
|
174
|
+
if (tenant !== null && (typeof tenant !== "string" || tenant === "")) {
|
|
175
|
+
throw new TypeError("@weft-framework/server: switchTenant needs a non-empty tenant id or null");
|
|
176
|
+
}
|
|
177
|
+
return control(ctx).switchTenant(tenant);
|
|
178
|
+
}
|
|
179
|
+
export function login(ctx, userId, opts = {}) {
|
|
180
|
+
if (typeof userId !== "string" || userId === "") {
|
|
181
|
+
throw new TypeError("@weft-framework/server: login needs a non-empty user id");
|
|
182
|
+
}
|
|
183
|
+
return control(ctx).login(userId, opts.multiFactor === true);
|
|
184
|
+
}
|
|
185
|
+
export function logout(ctx) {
|
|
186
|
+
return control(ctx).logout();
|
|
187
|
+
}
|
|
188
|
+
const viewing = new WeakMap();
|
|
189
|
+
const whileViewing = new WeakSet();
|
|
190
|
+
export function noteViewing(actor, v) {
|
|
191
|
+
viewing.set(actor, Object.freeze({ ...v }));
|
|
192
|
+
}
|
|
193
|
+
export function viewingAs(ctx) {
|
|
194
|
+
const u = ctx.user;
|
|
195
|
+
return typeof u === "object" && u !== null ? (viewing.get(u) ?? null) : null;
|
|
196
|
+
}
|
|
197
|
+
export function allowWhileViewing(action) {
|
|
198
|
+
whileViewing.add(action);
|
|
199
|
+
return action;
|
|
200
|
+
}
|
|
201
|
+
export function runsWhileViewing(action) {
|
|
202
|
+
return whileViewing.has(action);
|
|
203
|
+
}
|
|
204
|
+
export function viewAs(ctx, user, o) {
|
|
205
|
+
if (typeof user !== "string" || user === "")
|
|
206
|
+
throw new TypeError("@weft-framework/server: viewAs needs a user id");
|
|
207
|
+
if (!o || typeof o.until !== "number" || !Number.isFinite(o.until))
|
|
208
|
+
throw new TypeError("@weft-framework/server: viewAs needs `until` (ms)");
|
|
209
|
+
if (o.tenant !== undefined && (typeof o.tenant !== "string" || o.tenant === ""))
|
|
210
|
+
throw new TypeError("@weft-framework/server: viewAs tenant is an id");
|
|
211
|
+
return control(ctx).startViewAs(user, o.tenant, o.until);
|
|
212
|
+
}
|
|
213
|
+
export function stopViewingAs(ctx) {
|
|
214
|
+
return control(ctx).stopViewAs();
|
|
215
|
+
}
|
package/dist/backup.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { type S3Options } from "./uploads.js";
|
|
2
|
+
import type { Secret } from "./secret.js";
|
|
3
|
+
export interface BackupFile {
|
|
4
|
+
name: string;
|
|
5
|
+
size: number;
|
|
6
|
+
time: number;
|
|
7
|
+
}
|
|
8
|
+
export interface BackupTarget {
|
|
9
|
+
put(name: string, data: Uint8Array): Promise<void>;
|
|
10
|
+
get(name: string): Promise<Uint8Array | null>;
|
|
11
|
+
list(): Promise<{
|
|
12
|
+
name: string;
|
|
13
|
+
size: number;
|
|
14
|
+
}[]>;
|
|
15
|
+
remove(name: string): Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
export declare function dirBackups(dir: string): BackupTarget;
|
|
18
|
+
export declare function s3Backups(o: S3Options): BackupTarget;
|
|
19
|
+
export interface BackupOptions {
|
|
20
|
+
database: string;
|
|
21
|
+
target: BackupTarget;
|
|
22
|
+
keep?: number;
|
|
23
|
+
prefix?: string;
|
|
24
|
+
now?(): number;
|
|
25
|
+
}
|
|
26
|
+
export interface Backups {
|
|
27
|
+
run(): Promise<BackupFile>;
|
|
28
|
+
list(): Promise<BackupFile[]>;
|
|
29
|
+
restore(name: string): Promise<{
|
|
30
|
+
previous: string | null;
|
|
31
|
+
}>;
|
|
32
|
+
}
|
|
33
|
+
export declare function defineBackups(o: BackupOptions): Backups;
|
|
34
|
+
export interface PgBackupOptions {
|
|
35
|
+
url: string | Secret;
|
|
36
|
+
target: BackupTarget;
|
|
37
|
+
keep?: number;
|
|
38
|
+
prefix?: string;
|
|
39
|
+
pgDump?: string;
|
|
40
|
+
pgRestore?: string;
|
|
41
|
+
timeoutMs?: number;
|
|
42
|
+
tmpDir?: string;
|
|
43
|
+
now?(): number;
|
|
44
|
+
}
|
|
45
|
+
export declare function pgEnvironment(url: string, base?: Record<string, string | undefined>): Record<string, string>;
|
|
46
|
+
export declare function definePgBackups(o: PgBackupOptions): Backups;
|