@bright-logic-digital/site-editor 0.0.0-stage → 1.0.0-rc.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/CHANGELOG.md +30 -0
- package/LICENSE +4 -0
- package/README.md +318 -2
- package/bin/cli.js +92 -0
- package/bin/copy-editor.js +211 -0
- package/bin/node-version.js +20 -0
- package/bin/site-config.js +46 -0
- package/bin/site-editor.js +16 -0
- package/editor/admin.css +438 -0
- package/editor/api.js +138 -0
- package/editor/app.js +1178 -0
- package/editor/asset-map.js +38 -0
- package/editor/boot.js +72 -0
- package/editor/copy.js +239 -0
- package/editor/drafts.js +67 -0
- package/editor/format.js +80 -0
- package/editor/forms.js +918 -0
- package/editor/images.js +212 -0
- package/editor/index.html +27 -0
- package/editor/model.js +152 -0
- package/editor/pages.js +297 -0
- package/editor/photos.js +232 -0
- package/editor/publish.js +45 -0
- package/editor/shared/rules.js +118 -0
- package/editor/shared/site.js +128 -0
- package/editor/shared/validate.js +303 -0
- package/editor/signin.js +154 -0
- package/editor/status.js +90 -0
- package/editor/uploads.js +91 -0
- package/guide/check.js +233 -0
- package/hosts/next.js +19 -0
- package/hosts/pages.js +20 -0
- package/hosts/shared.js +42 -0
- package/package.json +91 -4
- package/server/blob-sha.js +10 -0
- package/server/changes.js +24 -0
- package/server/check-content.js +197 -0
- package/server/config.js +97 -0
- package/server/dispatch.js +207 -0
- package/server/github.js +149 -0
- package/server/handlers.js +364 -0
- package/server/index.js +153 -0
- package/server/local-repo.js +171 -0
- package/server/mapped-repo.js +48 -0
- package/server/memory-repo.js +104 -0
- package/server/path-map.js +26 -0
- package/server/signin/access.js +329 -0
- package/server/signin/code-store.sql +60 -0
- package/server/signin/code.js +192 -0
- package/server/signin/codes.js +171 -0
- package/server/signin/refusal.js +76 -0
- package/server/signin/session.js +90 -0
- package/tests/fixtures/image-files.json +14 -0
- package/tests/fixtures/slugs.json +202 -0
- package/tests/fixtures/validation.json +12734 -0
- package/types/bin/cli.d.ts +1 -0
- package/types/bin/copy-editor.d.ts +32 -0
- package/types/bin/node-version.d.ts +8 -0
- package/types/bin/site-config.d.ts +17 -0
- package/types/bin/site-editor.d.ts +2 -0
- package/types/editor/api.d.ts +37 -0
- package/types/editor/copy.d.ts +153 -0
- package/types/editor/shared/rules.d.ts +33 -0
- package/types/editor/shared/site.d.ts +35 -0
- package/types/editor/shared/validate.d.ts +19 -0
- package/types/guide/check.d.ts +31 -0
- package/types/hosts/next.d.ts +12 -0
- package/types/hosts/pages.d.ts +9 -0
- package/types/hosts/shared.d.ts +21 -0
- package/types/server/blob-sha.d.ts +1 -0
- package/types/server/changes.d.ts +2 -0
- package/types/server/check-content.d.ts +16 -0
- package/types/server/config.d.ts +46 -0
- package/types/server/dispatch.d.ts +51 -0
- package/types/server/github.d.ts +27 -0
- package/types/server/handlers.d.ts +116 -0
- package/types/server/index.d.ts +75 -0
- package/types/server/local-repo.d.ts +16 -0
- package/types/server/mapped-repo.d.ts +10 -0
- package/types/server/memory-repo.d.ts +28 -0
- package/types/server/path-map.d.ts +3 -0
- package/types/server/signin/access.d.ts +68 -0
- package/types/server/signin/code.d.ts +44 -0
- package/types/server/signin/codes.d.ts +98 -0
- package/types/server/signin/refusal.d.ts +122 -0
- package/types/server/signin/session.d.ts +29 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/** The one way an API response is built, so none lacks the headers. */
|
|
2
|
+
export function respond({ status, body }: {
|
|
3
|
+
status: any;
|
|
4
|
+
body: any;
|
|
5
|
+
}): Response;
|
|
6
|
+
export function failure(status: any, error: any, message: any, extra?: {}): {
|
|
7
|
+
status: any;
|
|
8
|
+
body: {
|
|
9
|
+
error: any;
|
|
10
|
+
message: any;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
/** The cross-site request check: a request that can change something must
|
|
14
|
+
* name this site as its Origin. Returns null to go ahead, or the failure. */
|
|
15
|
+
export function crossSiteFailure(request: any): {
|
|
16
|
+
status: any;
|
|
17
|
+
body: {
|
|
18
|
+
error: any;
|
|
19
|
+
message: any;
|
|
20
|
+
};
|
|
21
|
+
} | null;
|
|
22
|
+
export function getState(repo: any, user: any): Promise<{
|
|
23
|
+
status: number;
|
|
24
|
+
body: {
|
|
25
|
+
head: any;
|
|
26
|
+
docs: {};
|
|
27
|
+
user: any;
|
|
28
|
+
};
|
|
29
|
+
}>;
|
|
30
|
+
export function getDoc(repo: any, path: any): Promise<{
|
|
31
|
+
status: any;
|
|
32
|
+
body: {
|
|
33
|
+
error: any;
|
|
34
|
+
message: any;
|
|
35
|
+
};
|
|
36
|
+
} | {
|
|
37
|
+
status: number;
|
|
38
|
+
body: {
|
|
39
|
+
path: any;
|
|
40
|
+
sha: any;
|
|
41
|
+
doc: any;
|
|
42
|
+
};
|
|
43
|
+
}>;
|
|
44
|
+
export function getStatus(repo: any, mine: any, deployed: any): Promise<{
|
|
45
|
+
status: any;
|
|
46
|
+
body: {
|
|
47
|
+
error: any;
|
|
48
|
+
message: any;
|
|
49
|
+
};
|
|
50
|
+
} | {
|
|
51
|
+
status: number;
|
|
52
|
+
body: {
|
|
53
|
+
contains: any;
|
|
54
|
+
};
|
|
55
|
+
}>;
|
|
56
|
+
/** Stores one photo file sent as base64 text. Touches the whole text only
|
|
57
|
+
* through BASE64_RE; size comes from the length and the "=" padding. One
|
|
58
|
+
* repo call, createBlob. */
|
|
59
|
+
export function postUpload(repo: any, name: any, base64: any): Promise<{
|
|
60
|
+
status: any;
|
|
61
|
+
body: {
|
|
62
|
+
error: any;
|
|
63
|
+
message: any;
|
|
64
|
+
};
|
|
65
|
+
} | {
|
|
66
|
+
status: number;
|
|
67
|
+
body: {
|
|
68
|
+
path: string;
|
|
69
|
+
sha: any;
|
|
70
|
+
};
|
|
71
|
+
}>;
|
|
72
|
+
/** "Edit site content: <names> (by <email>)": one line of printable ASCII,
|
|
73
|
+
* at most 200 characters. */
|
|
74
|
+
export function commitMessage(user: any, paths: any): string;
|
|
75
|
+
export function postPublish(repo: any, user: any, body: any, { maxPublishFiles }?: {
|
|
76
|
+
maxPublishFiles?: number | undefined;
|
|
77
|
+
}): Promise<{
|
|
78
|
+
status: any;
|
|
79
|
+
body: {
|
|
80
|
+
error: any;
|
|
81
|
+
message: any;
|
|
82
|
+
};
|
|
83
|
+
} | {
|
|
84
|
+
status: number;
|
|
85
|
+
body: {
|
|
86
|
+
commit: any;
|
|
87
|
+
unchanged: boolean;
|
|
88
|
+
docs: any;
|
|
89
|
+
};
|
|
90
|
+
} | {
|
|
91
|
+
status: number;
|
|
92
|
+
body: {
|
|
93
|
+
commit: any;
|
|
94
|
+
docs: any;
|
|
95
|
+
unchanged?: undefined;
|
|
96
|
+
};
|
|
97
|
+
}>;
|
|
98
|
+
export const SCHEMA_PATH: "content/schema.json";
|
|
99
|
+
export const MAX_IMAGES: 250;
|
|
100
|
+
export const MAX_UPLOAD_BYTES: 2000000;
|
|
101
|
+
/** The longest base64 text of an upload: every 3 bytes, or part of 3, take 4 characters. */
|
|
102
|
+
export const MAX_UPLOAD_CHARS: number;
|
|
103
|
+
export function badRequest(message?: string): {
|
|
104
|
+
status: any;
|
|
105
|
+
body: {
|
|
106
|
+
error: any;
|
|
107
|
+
message: any;
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
export function photoTooLarge(): {
|
|
111
|
+
status: any;
|
|
112
|
+
body: {
|
|
113
|
+
error: any;
|
|
114
|
+
message: any;
|
|
115
|
+
};
|
|
116
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The names of what is wrong with a site's whole config file: keys that
|
|
3
|
+
* belong to neither the browser nor the server, server keys missing or
|
|
4
|
+
* malformed, and the browser keys as siteProblems judges them. Empty when
|
|
5
|
+
* the config is usable.
|
|
6
|
+
* @param {unknown} config
|
|
7
|
+
* @returns {string[]}
|
|
8
|
+
*/
|
|
9
|
+
export function serverConfigProblems(config: unknown): string[];
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {object} EditorApiOptions
|
|
12
|
+
* @property {SiteConfig} config the site's config file (site-editor.config.js)
|
|
13
|
+
* @property {SignInProvider} signIn of the kind config.signIn names
|
|
14
|
+
* @property {(input: string, init?: RequestInit) => Promise<Response>} [fetch]
|
|
15
|
+
* for GitHub; the global fetch when not given
|
|
16
|
+
* @property {() => number} [now] milliseconds; Date.now when not given
|
|
17
|
+
* @property {{ user: string, repo: Repo }} [local] local editing: the
|
|
18
|
+
* address to edit as and the working tree's repository, under its own
|
|
19
|
+
* paths (config.paths.local maps them). Used only when the settings ask
|
|
20
|
+
* for local mode (isLocalMode).
|
|
21
|
+
* @property {(line: string) => void} [log] console.error when not given
|
|
22
|
+
*/
|
|
23
|
+
/**
|
|
24
|
+
* The API. Throws `site config: <names>` when the config is wrong.
|
|
25
|
+
* @param {EditorApiOptions} options
|
|
26
|
+
* @returns {{ handle(request: Request, env: Record<string, string | undefined>): Promise<Response> }}
|
|
27
|
+
*/
|
|
28
|
+
export function createEditorApi({ config, signIn, fetch, now, local, log }: EditorApiOptions): {
|
|
29
|
+
handle(request: Request, env: Record<string, string | undefined>): Promise<Response>;
|
|
30
|
+
};
|
|
31
|
+
/** @typedef {import("./signin/refusal.js").SignInProvider} SignInProvider */
|
|
32
|
+
/** @typedef {import("./signin/refusal.js").SiteConfig} SiteConfig */
|
|
33
|
+
/** @typedef {import("./dispatch.js").Repo} Repo */
|
|
34
|
+
/** The keys only the server reads, beside the browser's (BROWSER_KEYS). */
|
|
35
|
+
export const SERVER_KEYS: readonly string[];
|
|
36
|
+
export type EditorApiOptions = {
|
|
37
|
+
/**
|
|
38
|
+
* the site's config file (site-editor.config.js)
|
|
39
|
+
*/
|
|
40
|
+
config: SiteConfig;
|
|
41
|
+
/**
|
|
42
|
+
* of the kind config.signIn names
|
|
43
|
+
*/
|
|
44
|
+
signIn: SignInProvider;
|
|
45
|
+
/**
|
|
46
|
+
* for GitHub; the global fetch when not given
|
|
47
|
+
*/
|
|
48
|
+
fetch?: ((input: string, init?: RequestInit) => Promise<Response>) | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* milliseconds; Date.now when not given
|
|
51
|
+
*/
|
|
52
|
+
now?: (() => number) | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* local editing: the
|
|
55
|
+
* address to edit as and the working tree's repository, under its own
|
|
56
|
+
* paths (config.paths.local maps them). Used only when the settings ask
|
|
57
|
+
* for local mode (isLocalMode).
|
|
58
|
+
*/
|
|
59
|
+
local?: {
|
|
60
|
+
user: string;
|
|
61
|
+
repo: Repo;
|
|
62
|
+
} | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* console.error when not given
|
|
65
|
+
*/
|
|
66
|
+
log?: ((line: string) => void) | undefined;
|
|
67
|
+
};
|
|
68
|
+
export type SignInProvider = import("./signin/refusal.js").SignInProvider;
|
|
69
|
+
export type SiteConfig = import("./signin/refusal.js").SiteConfig;
|
|
70
|
+
export type Repo = import("./dispatch.js").Repo;
|
|
71
|
+
import { isLocalMode } from "./config.js";
|
|
72
|
+
import { MAX_PUBLISH_CHARS } from "./dispatch.js";
|
|
73
|
+
import { MAX_UPLOAD_BYTES } from "./handlers.js";
|
|
74
|
+
import { MAX_UPLOAD_CHARS } from "./handlers.js";
|
|
75
|
+
export { isLocalMode, MAX_PUBLISH_CHARS, MAX_UPLOAD_BYTES, MAX_UPLOAD_CHARS };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** @typedef {import("./dispatch.js").Repo} Repo */
|
|
2
|
+
/**
|
|
3
|
+
* paths: the site config's paths.local, [[logical, folder], ...] (its
|
|
4
|
+
* folders for content/ and assets/, under rootDir). Required: a layout
|
|
5
|
+
* guessed wrong would lose photos without a word. Throws when it is missing
|
|
6
|
+
* or not of the form the config check takes.
|
|
7
|
+
* rebuild: called with each new commit's id after its files are written.
|
|
8
|
+
* @param {string} rootDir
|
|
9
|
+
* @param {{ rebuild: (commit: string) => unknown, paths: ReadonlyArray<readonly string[]> }} options
|
|
10
|
+
* @returns {Repo}
|
|
11
|
+
*/
|
|
12
|
+
export function createLocalRepo(rootDir: string, { rebuild, paths }: {
|
|
13
|
+
rebuild: (commit: string) => unknown;
|
|
14
|
+
paths: ReadonlyArray<readonly string[]>;
|
|
15
|
+
}): Repo;
|
|
16
|
+
export type Repo = import("./dispatch.js").Repo;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function mapRepo(repo: any, prefixes: any): {
|
|
2
|
+
head(): Promise<{
|
|
3
|
+
commit: any;
|
|
4
|
+
files: Map<any, any>;
|
|
5
|
+
}>;
|
|
6
|
+
readText: (sha: any) => any;
|
|
7
|
+
createBlob: (base64: any) => any;
|
|
8
|
+
commit(input: any): Promise<any>;
|
|
9
|
+
contains: (ancestor: any, descendant: any) => any;
|
|
10
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** @typedef {import("./dispatch.js").Repo} Repo */
|
|
2
|
+
/**
|
|
3
|
+
* A Repo, plus the two helpers tests use: files() is the head's files as
|
|
4
|
+
* {path: text}; moveHead(changes) adds a commit behind the caller's back.
|
|
5
|
+
* @typedef {Repo & {
|
|
6
|
+
* files(): Record<string, string>,
|
|
7
|
+
* moveHead(changes?: Array<{ path: string, text?: string, sha?: string, remove?: boolean }>): Promise<string>,
|
|
8
|
+
* }} MemoryRepo
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* @param {Record<string, string>} [initial] the head's files, {path: text}
|
|
12
|
+
* @returns {MemoryRepo}
|
|
13
|
+
*/
|
|
14
|
+
export function createMemoryRepo(initial?: Record<string, string>): MemoryRepo;
|
|
15
|
+
export type Repo = import("./dispatch.js").Repo;
|
|
16
|
+
/**
|
|
17
|
+
* A Repo, plus the two helpers tests use: files() is the head's files as
|
|
18
|
+
* {path: text}; moveHead(changes) adds a commit behind the caller's back.
|
|
19
|
+
*/
|
|
20
|
+
export type MemoryRepo = Repo & {
|
|
21
|
+
files(): Record<string, string>;
|
|
22
|
+
moveHead(changes?: Array<{
|
|
23
|
+
path: string;
|
|
24
|
+
text?: string;
|
|
25
|
+
sha?: string;
|
|
26
|
+
remove?: boolean;
|
|
27
|
+
}>): Promise<string>;
|
|
28
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {object} AccessConfig
|
|
3
|
+
* @property {string} token
|
|
4
|
+
* @property {string} repo
|
|
5
|
+
* @property {string} branch
|
|
6
|
+
* @property {string} teamDomain
|
|
7
|
+
* @property {string} aud
|
|
8
|
+
* @property {string[]} emails
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* The settings, trimmed, or null unless every one is there and well formed.
|
|
12
|
+
* @param {any} env
|
|
13
|
+
* @returns {AccessConfig | null}
|
|
14
|
+
*/
|
|
15
|
+
export function readConfig(env: any): AccessConfig | null;
|
|
16
|
+
export function resetKeyCache(): void;
|
|
17
|
+
/**
|
|
18
|
+
* The signed-in address, lower-cased, once the token is verified and the
|
|
19
|
+
* address is on the list. Throws AuthError (401, or 403 for an address not
|
|
20
|
+
* on the list); its message names the check, never the token or the address.
|
|
21
|
+
* @param {Request} request
|
|
22
|
+
* @param {AccessConfig} config
|
|
23
|
+
* @param {Partial<VerifyDeps>} [deps]
|
|
24
|
+
* @returns {Promise<{ email: string }>}
|
|
25
|
+
*/
|
|
26
|
+
export function verifyAccess(request: Request, config: AccessConfig, deps?: Partial<VerifyDeps>): Promise<{
|
|
27
|
+
email: string;
|
|
28
|
+
}>;
|
|
29
|
+
/**
|
|
30
|
+
* The NAMES of the settings readConfig would refuse, never a value: empty
|
|
31
|
+
* exactly when readConfig reads a configuration.
|
|
32
|
+
* @param {any} env
|
|
33
|
+
* @returns {string[]}
|
|
34
|
+
*/
|
|
35
|
+
export function accessProblems(env: any): string[];
|
|
36
|
+
/**
|
|
37
|
+
* @param {{ fetch?: FetchKeys }} [options] fetch for the signing keys;
|
|
38
|
+
* the global fetch when not given
|
|
39
|
+
* @returns {SignInProvider}
|
|
40
|
+
*/
|
|
41
|
+
export function accessSignIn({ fetch }?: {
|
|
42
|
+
fetch?: FetchKeys;
|
|
43
|
+
}): SignInProvider;
|
|
44
|
+
export class AuthError extends Error {
|
|
45
|
+
/**
|
|
46
|
+
* @param {number} status
|
|
47
|
+
* @param {string} code
|
|
48
|
+
* @param {string} message
|
|
49
|
+
*/
|
|
50
|
+
constructor(status: number, code: string, message: string);
|
|
51
|
+
status: number;
|
|
52
|
+
code: string;
|
|
53
|
+
}
|
|
54
|
+
export type AccessConfig = {
|
|
55
|
+
token: string;
|
|
56
|
+
repo: string;
|
|
57
|
+
branch: string;
|
|
58
|
+
teamDomain: string;
|
|
59
|
+
aud: string;
|
|
60
|
+
emails: string[];
|
|
61
|
+
};
|
|
62
|
+
export type FetchKeys = (url: string) => Promise<Response>;
|
|
63
|
+
export type VerifyDeps = {
|
|
64
|
+
now: () => number;
|
|
65
|
+
fetch: FetchKeys;
|
|
66
|
+
};
|
|
67
|
+
export type SignInProvider = import("./refusal.js").SignInProvider;
|
|
68
|
+
export type Env = import("./refusal.js").Env;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {object} CodeSignInOptions
|
|
3
|
+
* @property {CodeStore | null} codes null when the code store is not set up
|
|
4
|
+
* @property {(to: string, code: string) => Promise<void>} sendCode
|
|
5
|
+
* @property {() => string} [randomCode] eight digits; a secure default
|
|
6
|
+
* @property {(request: Request) => Promise<boolean>} allowRequest
|
|
7
|
+
* false when this caller has asked too often; the site reads the address
|
|
8
|
+
* itself. Asked on every request for a code (session/request) and every
|
|
9
|
+
* check of one (session/verify); false answers 429.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* @param {CodeSignInOptions} options
|
|
13
|
+
* @returns {SignInProvider}
|
|
14
|
+
*/
|
|
15
|
+
export function codeSignIn({ codes, sendCode, randomCode, allowRequest }: CodeSignInOptions): SignInProvider;
|
|
16
|
+
/** The sentences the sign-in screen shows as they are; a site's guide quotes
|
|
17
|
+
* them (guide/check.js holds the two together). */
|
|
18
|
+
export const SIGN_IN_SENTENCES: Readonly<{
|
|
19
|
+
tooMany: "Too many tries. Wait a few minutes, then try again.";
|
|
20
|
+
down: "Sign-in is not available right now. Try again in a few minutes.";
|
|
21
|
+
badCode: "That code is not right or has expired. Ask for a new one.";
|
|
22
|
+
}>;
|
|
23
|
+
export type CodeSignInOptions = {
|
|
24
|
+
/**
|
|
25
|
+
* null when the code store is not set up
|
|
26
|
+
*/
|
|
27
|
+
codes: CodeStore | null;
|
|
28
|
+
sendCode: (to: string, code: string) => Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* eight digits; a secure default
|
|
31
|
+
*/
|
|
32
|
+
randomCode?: (() => string) | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* false when this caller has asked too often; the site reads the address
|
|
35
|
+
* itself. Asked on every request for a code (session/request) and every
|
|
36
|
+
* check of one (session/verify); false answers 429.
|
|
37
|
+
*/
|
|
38
|
+
allowRequest: (request: Request) => Promise<boolean>;
|
|
39
|
+
};
|
|
40
|
+
export type CodeStore = import("./codes.js").CodeStore;
|
|
41
|
+
export type SignInProvider = import("./refusal.js").SignInProvider;
|
|
42
|
+
export type SignInContext = import("./refusal.js").SignInContext;
|
|
43
|
+
export type AdminConfig = import("../config.js").AdminConfig;
|
|
44
|
+
export { createMemoryCodeStore, createSupabaseCodeStore } from "./codes.js";
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/** @returns {string} */
|
|
2
|
+
export function randomCode(): string;
|
|
3
|
+
/**
|
|
4
|
+
* The code with spaces and hyphens removed, or null unless it is 8 digits.
|
|
5
|
+
* @param {unknown} input
|
|
6
|
+
* @returns {string | null}
|
|
7
|
+
*/
|
|
8
|
+
export function normalizeCode(input: unknown): string | null;
|
|
9
|
+
/**
|
|
10
|
+
* @param {string} email
|
|
11
|
+
* @param {string} code
|
|
12
|
+
* @param {string} secret
|
|
13
|
+
* @returns {string}
|
|
14
|
+
*/
|
|
15
|
+
export function hashCode(email: string, code: string, secret: string): string;
|
|
16
|
+
/**
|
|
17
|
+
* Constant-time comparison; unequal lengths are simply false.
|
|
18
|
+
* @param {string} a
|
|
19
|
+
* @param {string} b
|
|
20
|
+
* @returns {boolean}
|
|
21
|
+
*/
|
|
22
|
+
export function sameHash(a: string, b: string): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* @typedef {object} CodeStore
|
|
25
|
+
* @property {(email: string, codeHash: string) => Promise<boolean>} issue
|
|
26
|
+
* False when an hourly or daily cap is reached.
|
|
27
|
+
* @property {(email: string) => Promise<string | null>} attempt
|
|
28
|
+
* Counts the attempt; the hash to compare, or null.
|
|
29
|
+
* @property {(email: string, codeHash: string) => Promise<boolean>} consume
|
|
30
|
+
* True only when this call used that live code.
|
|
31
|
+
*/
|
|
32
|
+
/**
|
|
33
|
+
* @typedef {object} Row
|
|
34
|
+
* @property {string} email
|
|
35
|
+
* @property {string} hash
|
|
36
|
+
* @property {number} createdAt
|
|
37
|
+
* @property {number} expiresAt
|
|
38
|
+
* @property {number} attempts
|
|
39
|
+
* @property {boolean} used
|
|
40
|
+
*/
|
|
41
|
+
/**
|
|
42
|
+
* Any object with an `rpc(name, args)` method returning `{ data, error }`,
|
|
43
|
+
* such as a Supabase client.
|
|
44
|
+
* @typedef {object} RpcClient
|
|
45
|
+
* @property {(name: string, args: Record<string, string>) =>
|
|
46
|
+
* PromiseLike<{ data: unknown, error: { message: string } | null }>} rpc
|
|
47
|
+
*/
|
|
48
|
+
/**
|
|
49
|
+
* In-memory store with the same rules as the SQL functions.
|
|
50
|
+
* @param {() => number} now
|
|
51
|
+
* @returns {CodeStore}
|
|
52
|
+
*/
|
|
53
|
+
export function createMemoryCodeStore(now: () => number): CodeStore;
|
|
54
|
+
/**
|
|
55
|
+
* Store backed by the three SQL functions; errors never include the arguments.
|
|
56
|
+
* @param {RpcClient} client
|
|
57
|
+
* @returns {CodeStore}
|
|
58
|
+
*/
|
|
59
|
+
export function createSupabaseCodeStore(client: RpcClient): CodeStore;
|
|
60
|
+
export const CODE_DIGITS: 8;
|
|
61
|
+
export const CODE_TTL_MS: 600000;
|
|
62
|
+
export const MAX_ATTEMPTS: 5;
|
|
63
|
+
export const MAX_CODES_PER_HOUR: 5;
|
|
64
|
+
export const MAX_CODES_PER_DAY: 10;
|
|
65
|
+
export type CodeStore = {
|
|
66
|
+
/**
|
|
67
|
+
* False when an hourly or daily cap is reached.
|
|
68
|
+
*/
|
|
69
|
+
issue: (email: string, codeHash: string) => Promise<boolean>;
|
|
70
|
+
/**
|
|
71
|
+
* Counts the attempt; the hash to compare, or null.
|
|
72
|
+
*/
|
|
73
|
+
attempt: (email: string) => Promise<string | null>;
|
|
74
|
+
/**
|
|
75
|
+
* True only when this call used that live code.
|
|
76
|
+
*/
|
|
77
|
+
consume: (email: string, codeHash: string) => Promise<boolean>;
|
|
78
|
+
};
|
|
79
|
+
export type Row = {
|
|
80
|
+
email: string;
|
|
81
|
+
hash: string;
|
|
82
|
+
createdAt: number;
|
|
83
|
+
expiresAt: number;
|
|
84
|
+
attempts: number;
|
|
85
|
+
used: boolean;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Any object with an `rpc(name, args)` method returning `{ data, error }`,
|
|
89
|
+
* such as a Supabase client.
|
|
90
|
+
*/
|
|
91
|
+
export type RpcClient = {
|
|
92
|
+
rpc: (name: string, args: Record<string, string>) => PromiseLike<{
|
|
93
|
+
data: unknown;
|
|
94
|
+
error: {
|
|
95
|
+
message: string;
|
|
96
|
+
} | null;
|
|
97
|
+
}>;
|
|
98
|
+
};
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One log line: the step and the error's message, nothing else.
|
|
3
|
+
* @param {string} step
|
|
4
|
+
* @param {unknown} err
|
|
5
|
+
* @returns {string}
|
|
6
|
+
*/
|
|
7
|
+
export function failureLine(step: string, err: unknown): string;
|
|
8
|
+
/**
|
|
9
|
+
* A sign-in provider.
|
|
10
|
+
*
|
|
11
|
+
* @typedef {object} SignInProvider
|
|
12
|
+
* @property {"code" | "access"} kind matches the site config's `signIn`
|
|
13
|
+
* @property {(env: Env, config: SiteConfig) => string[]} problems
|
|
14
|
+
* The NAMES of the settings that are missing or malformed (plus a guard's
|
|
15
|
+
* own words when it refuses), never a value. Empty when the settings are
|
|
16
|
+
* usable. The dispatcher answers not_configured and logs these otherwise.
|
|
17
|
+
* @property {(env: Env) => GithubSettings} github
|
|
18
|
+
* GITHUB_TOKEN, GITHUB_REPO and GITHUB_BRANCH as this kind reads them.
|
|
19
|
+
* Called only when problems is empty.
|
|
20
|
+
* @property {(request: Request, name: string, ctx: SignInContext) => Promise<Response | null>} route
|
|
21
|
+
* The kind's own routes, which need no sign-in (for example asking for a
|
|
22
|
+
* code). A Response answers the request; null passes it on.
|
|
23
|
+
* @property {(request: Request, ctx: SignInContext) => Promise<string>} user
|
|
24
|
+
* The signed-in address, checked against the kind's own list of addresses.
|
|
25
|
+
* Throws SignInRefused for a caller who is not signed in or not listed;
|
|
26
|
+
* anything else it throws is an error like any other (500, or 502 for
|
|
27
|
+
* code "upstream").
|
|
28
|
+
*/
|
|
29
|
+
/** @typedef {Record<string, string | undefined>} Env */
|
|
30
|
+
/** @typedef {Record<string, any>} SiteConfig the site's whole config file */
|
|
31
|
+
/** @typedef {{ token: string, repo: string, branch: string }} GithubSettings */
|
|
32
|
+
/**
|
|
33
|
+
* What the dispatcher hands a provider with each request.
|
|
34
|
+
* @typedef {object} SignInContext
|
|
35
|
+
* @property {Env} env the settings
|
|
36
|
+
* @property {SiteConfig} config the site's config
|
|
37
|
+
* @property {() => number} now milliseconds
|
|
38
|
+
* @property {(line: string) => void} log one line; never a secret
|
|
39
|
+
*/
|
|
40
|
+
/** A caller refused by the sign-in: the status, code and sentence the owner sees. */
|
|
41
|
+
export class SignInRefused extends Error {
|
|
42
|
+
/**
|
|
43
|
+
* @param {number} status
|
|
44
|
+
* @param {string} code
|
|
45
|
+
* @param {string} message
|
|
46
|
+
*/
|
|
47
|
+
constructor(status: number, code: string, message: string);
|
|
48
|
+
status: number;
|
|
49
|
+
code: string;
|
|
50
|
+
}
|
|
51
|
+
/** The same sentence for both kinds. */
|
|
52
|
+
export const NOT_ALLOWED_MESSAGE: "This account is not allowed to edit the site.";
|
|
53
|
+
export function notConfigured(): {
|
|
54
|
+
status: any;
|
|
55
|
+
body: {
|
|
56
|
+
error: any;
|
|
57
|
+
message: any;
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* A sign-in provider.
|
|
62
|
+
*/
|
|
63
|
+
export type SignInProvider = {
|
|
64
|
+
/**
|
|
65
|
+
* matches the site config's `signIn`
|
|
66
|
+
*/
|
|
67
|
+
kind: "code" | "access";
|
|
68
|
+
/**
|
|
69
|
+
* The NAMES of the settings that are missing or malformed (plus a guard's
|
|
70
|
+
* own words when it refuses), never a value. Empty when the settings are
|
|
71
|
+
* usable. The dispatcher answers not_configured and logs these otherwise.
|
|
72
|
+
*/
|
|
73
|
+
problems: (env: Env, config: SiteConfig) => string[];
|
|
74
|
+
/**
|
|
75
|
+
* GITHUB_TOKEN, GITHUB_REPO and GITHUB_BRANCH as this kind reads them.
|
|
76
|
+
* Called only when problems is empty.
|
|
77
|
+
*/
|
|
78
|
+
github: (env: Env) => GithubSettings;
|
|
79
|
+
/**
|
|
80
|
+
* The kind's own routes, which need no sign-in (for example asking for a
|
|
81
|
+
* code). A Response answers the request; null passes it on.
|
|
82
|
+
*/
|
|
83
|
+
route: (request: Request, name: string, ctx: SignInContext) => Promise<Response | null>;
|
|
84
|
+
/**
|
|
85
|
+
* The signed-in address, checked against the kind's own list of addresses.
|
|
86
|
+
* Throws SignInRefused for a caller who is not signed in or not listed;
|
|
87
|
+
* anything else it throws is an error like any other (500, or 502 for
|
|
88
|
+
* code "upstream").
|
|
89
|
+
*/
|
|
90
|
+
user: (request: Request, ctx: SignInContext) => Promise<string>;
|
|
91
|
+
};
|
|
92
|
+
export type Env = Record<string, string | undefined>;
|
|
93
|
+
/**
|
|
94
|
+
* the site's whole config file
|
|
95
|
+
*/
|
|
96
|
+
export type SiteConfig = Record<string, any>;
|
|
97
|
+
export type GithubSettings = {
|
|
98
|
+
token: string;
|
|
99
|
+
repo: string;
|
|
100
|
+
branch: string;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* What the dispatcher hands a provider with each request.
|
|
104
|
+
*/
|
|
105
|
+
export type SignInContext = {
|
|
106
|
+
/**
|
|
107
|
+
* the settings
|
|
108
|
+
*/
|
|
109
|
+
env: Env;
|
|
110
|
+
/**
|
|
111
|
+
* the site's config
|
|
112
|
+
*/
|
|
113
|
+
config: SiteConfig;
|
|
114
|
+
/**
|
|
115
|
+
* milliseconds
|
|
116
|
+
*/
|
|
117
|
+
now: () => number;
|
|
118
|
+
/**
|
|
119
|
+
* one line; never a secret
|
|
120
|
+
*/
|
|
121
|
+
log: (line: string) => void;
|
|
122
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `v1.<payload>.<signature>`; the payload carries the address and expiry.
|
|
3
|
+
* @param {string} email
|
|
4
|
+
* @param {string} secret
|
|
5
|
+
* @param {number} nowMs
|
|
6
|
+
* @returns {string}
|
|
7
|
+
*/
|
|
8
|
+
export function signSession(email: string, secret: string, nowMs: number): string;
|
|
9
|
+
/**
|
|
10
|
+
* The signed-in address, or null for anything missing, forged, expired or malformed.
|
|
11
|
+
* @param {string | null} cookieHeader
|
|
12
|
+
* @param {string} secret
|
|
13
|
+
* @param {number} nowMs
|
|
14
|
+
* @param {string} name the session cookie's name
|
|
15
|
+
* @returns {string | null}
|
|
16
|
+
*/
|
|
17
|
+
export function readSession(cookieHeader: string | null, secret: string, nowMs: number, name: string): string | null;
|
|
18
|
+
/**
|
|
19
|
+
* @param {string} name
|
|
20
|
+
* @param {string} value
|
|
21
|
+
* @returns {string}
|
|
22
|
+
*/
|
|
23
|
+
export function sessionCookie(name: string, value: string): string;
|
|
24
|
+
/**
|
|
25
|
+
* @param {string} name
|
|
26
|
+
* @returns {string}
|
|
27
|
+
*/
|
|
28
|
+
export function clearedCookie(name: string): string;
|
|
29
|
+
export const SESSION_MS: number;
|