@mccaskie-labs/workbench-agreements 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/CHANGELOG.md +5 -0
- package/README.md +38 -2
- package/dist/agreements.d.ts +197 -0
- package/dist/agreements.js +614 -0
- package/dist/certificate.d.ts +40 -0
- package/dist/certificate.js +194 -0
- package/dist/cli.d.ts +10 -0
- package/dist/cli.js +119 -0
- package/dist/config.d.ts +42 -0
- package/dist/config.js +81 -0
- package/dist/errors.d.ts +8 -0
- package/dist/errors.js +9 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +13 -0
- package/dist/migration.d.ts +4 -0
- package/dist/migration.js +493 -0
- package/dist/page.d.ts +46 -0
- package/dist/page.js +118 -0
- package/dist/script.d.ts +3 -0
- package/dist/script.js +31 -0
- package/dist/secrets.d.ts +7 -0
- package/dist/secrets.js +26 -0
- package/dist/signature.d.ts +9 -0
- package/dist/signature.js +35 -0
- package/dist/styles.d.ts +33 -0
- package/dist/styles.js +68 -0
- package/dist/words.d.ts +73 -0
- package/dist/words.js +162 -0
- package/package.json +51 -4
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
// The signed copy: the agreement's own pages, unchanged but for a small line
|
|
2
|
+
// at the foot of each ("Signed electronically · Agreement … · Page 2 of 5"),
|
|
3
|
+
// then a signing certificate. The certificate is the audit trail in print: the
|
|
4
|
+
// original's SHA-256, who signed, how each showed it was them, how they signed
|
|
5
|
+
// (their drawn signature, or their typed name), when, from where, and every
|
|
6
|
+
// event with its time in UTC and in the business's time zone. Built with
|
|
7
|
+
// pdf-lib in the app's own Worker, so no browser or Gateway call is needed.
|
|
8
|
+
//
|
|
9
|
+
// The standard PDF fonts only draw Western European letters (WinAnsi), which
|
|
10
|
+
// covers English and Filipino names (ñ, é); anything else is shown by its
|
|
11
|
+
// plain letter, or "?", and the exact text stays in the database.
|
|
12
|
+
import { PDFDocument, rgb, StandardFonts } from "pdf-lib";
|
|
13
|
+
import { AgreementsError } from "./errors.js";
|
|
14
|
+
/** The page count, refusing a file pdf-lib can't open or one that is encrypted (a signed copy couldn't be made from it). */
|
|
15
|
+
export async function pdfPages(bytes) {
|
|
16
|
+
if (bytes.length < 8 || String.fromCharCode(...bytes.slice(0, 5)) !== "%PDF-")
|
|
17
|
+
throw new AgreementsError("invalid_request", "document must be a PDF");
|
|
18
|
+
try {
|
|
19
|
+
const doc = await PDFDocument.load(bytes, { updateMetadata: false });
|
|
20
|
+
const n = doc.getPageCount();
|
|
21
|
+
if (n < 1)
|
|
22
|
+
throw new Error("no pages");
|
|
23
|
+
return n;
|
|
24
|
+
}
|
|
25
|
+
catch (e) {
|
|
26
|
+
throw new AgreementsError("invalid_request", `document can't be used: ${e.message}. Make it with workbench-pdf, unencrypted`, { cause: e });
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const A4 = { width: 595.28, height: 841.89 };
|
|
30
|
+
const MARGIN = 50;
|
|
31
|
+
const INK = rgb(0.11, 0.11, 0.13);
|
|
32
|
+
const MUTED = rgb(0.37, 0.37, 0.42);
|
|
33
|
+
const RULE = rgb(0.84, 0.84, 0.87);
|
|
34
|
+
/** Text a standard font can draw: each character it can't becomes its plain letter, or "?". */
|
|
35
|
+
export function winAnsi(font, s) {
|
|
36
|
+
const set = new Set(font.getCharacterSet());
|
|
37
|
+
let out = "";
|
|
38
|
+
for (const ch of s.replace(/[\r\n\t]+/g, " ")) {
|
|
39
|
+
if (set.has(ch.codePointAt(0)))
|
|
40
|
+
out += ch;
|
|
41
|
+
else {
|
|
42
|
+
const base = ch.normalize("NFKD").replace(/[̀-ͯ]/g, "");
|
|
43
|
+
out += base && [...base].every((c) => set.has(c.codePointAt(0))) ? base : "?";
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
function wrap(font, size, text, width) {
|
|
49
|
+
const words = winAnsi(font, text).split(" ");
|
|
50
|
+
const lines = [];
|
|
51
|
+
let line = "";
|
|
52
|
+
for (const word of words) {
|
|
53
|
+
let wd = word;
|
|
54
|
+
while (font.widthOfTextAtSize(wd, size) > width) {
|
|
55
|
+
// A word longer than the line (a hash, an address): break it.
|
|
56
|
+
let i = wd.length;
|
|
57
|
+
while (i > 1 && font.widthOfTextAtSize(wd.slice(0, i), size) > width)
|
|
58
|
+
i--;
|
|
59
|
+
if (line)
|
|
60
|
+
lines.push(line);
|
|
61
|
+
lines.push(wd.slice(0, i));
|
|
62
|
+
line = "";
|
|
63
|
+
wd = wd.slice(i);
|
|
64
|
+
}
|
|
65
|
+
const next = line ? `${line} ${wd}` : wd;
|
|
66
|
+
if (font.widthOfTextAtSize(next, size) > width && line) {
|
|
67
|
+
lines.push(line);
|
|
68
|
+
line = wd;
|
|
69
|
+
}
|
|
70
|
+
else
|
|
71
|
+
line = next;
|
|
72
|
+
}
|
|
73
|
+
if (line)
|
|
74
|
+
lines.push(line);
|
|
75
|
+
return lines;
|
|
76
|
+
}
|
|
77
|
+
/** "23 Nov 2026, 9:05:12 pm AEDT (10:05:12 UTC)". */
|
|
78
|
+
export function bothTimes(iso, timeZone, locale = "en-AU") {
|
|
79
|
+
const d = new Date(iso);
|
|
80
|
+
const local = new Intl.DateTimeFormat(locale, { timeZone, day: "numeric", month: "short", year: "numeric", hour: "numeric", minute: "2-digit", second: "2-digit", timeZoneName: "short" }).format(d);
|
|
81
|
+
return `${local} (${d.toISOString().replace("T", " ").replace(/\.\d+Z$/, " UTC")})`;
|
|
82
|
+
}
|
|
83
|
+
const METHOD = { typed: "Typed their name", drawn: "Drew their signature" };
|
|
84
|
+
const IDENTITY = { link: "Opened the private signing link sent to them", email_code: "Opened the private signing link and entered a one-time code emailed to them" };
|
|
85
|
+
/** The signed copy's bytes. */
|
|
86
|
+
export async function buildSignedCopy(c) {
|
|
87
|
+
let doc;
|
|
88
|
+
try {
|
|
89
|
+
doc = await PDFDocument.load(c.document, { updateMetadata: false });
|
|
90
|
+
}
|
|
91
|
+
catch (e) {
|
|
92
|
+
throw new AgreementsError("pdf", `the agreement's PDF can't be opened: ${e.message}`, { cause: e });
|
|
93
|
+
}
|
|
94
|
+
const regular = await doc.embedFont(StandardFonts.Helvetica);
|
|
95
|
+
const bold = await doc.embedFont(StandardFonts.HelveticaBold);
|
|
96
|
+
const script = await doc.embedFont(StandardFonts.TimesRomanItalic);
|
|
97
|
+
const short = c.id.slice(0, 8);
|
|
98
|
+
const pages = doc.getPages();
|
|
99
|
+
const total = pages.length;
|
|
100
|
+
pages.forEach((p, i) => {
|
|
101
|
+
const text = winAnsi(regular, `Signed electronically · Agreement ${short} · Page ${i + 1} of ${total} · Certificate at the end`);
|
|
102
|
+
const { width } = p.getSize();
|
|
103
|
+
const size = 7;
|
|
104
|
+
p.drawText(text, { x: Math.max(10, (width - regular.widthOfTextAtSize(text, size)) / 2), y: 12, size, font: regular, color: MUTED });
|
|
105
|
+
});
|
|
106
|
+
let page = doc.addPage([A4.width, A4.height]);
|
|
107
|
+
let y = A4.height - MARGIN;
|
|
108
|
+
const width = A4.width - 2 * MARGIN;
|
|
109
|
+
const newPage = () => {
|
|
110
|
+
page = doc.addPage([A4.width, A4.height]);
|
|
111
|
+
y = A4.height - MARGIN;
|
|
112
|
+
};
|
|
113
|
+
const need = (h) => {
|
|
114
|
+
if (y - h < MARGIN + 20)
|
|
115
|
+
newPage();
|
|
116
|
+
};
|
|
117
|
+
const para = (text, o = {}) => {
|
|
118
|
+
const font = o.font ?? regular;
|
|
119
|
+
const size = o.size ?? 9.5;
|
|
120
|
+
const lines = wrap(font, size, text, width - (o.indent ?? 0));
|
|
121
|
+
for (const line of lines) {
|
|
122
|
+
need(size * 1.4);
|
|
123
|
+
page.drawText(line, { x: MARGIN + (o.indent ?? 0), y: y - size, size, font, color: o.color ?? INK });
|
|
124
|
+
y -= size * 1.4;
|
|
125
|
+
}
|
|
126
|
+
y -= o.gap ?? 4;
|
|
127
|
+
};
|
|
128
|
+
const rule = () => {
|
|
129
|
+
need(12);
|
|
130
|
+
page.drawLine({ start: { x: MARGIN, y: y - 4 }, end: { x: A4.width - MARGIN, y: y - 4 }, thickness: 0.6, color: RULE });
|
|
131
|
+
y -= 14;
|
|
132
|
+
};
|
|
133
|
+
const fact = (label, value) => {
|
|
134
|
+
const lw = 120;
|
|
135
|
+
const lines = wrap(regular, 9.5, value, width - lw);
|
|
136
|
+
need(lines.length * 13.3);
|
|
137
|
+
page.drawText(winAnsi(bold, label), { x: MARGIN, y: y - 9.5, size: 9.5, font: bold, color: INK });
|
|
138
|
+
for (const line of lines) {
|
|
139
|
+
page.drawText(line, { x: MARGIN + lw, y: y - 9.5, size: 9.5, font: regular, color: INK });
|
|
140
|
+
y -= 13.3;
|
|
141
|
+
}
|
|
142
|
+
y -= 2;
|
|
143
|
+
};
|
|
144
|
+
para("Signing certificate", { font: bold, size: 18, gap: 6 });
|
|
145
|
+
para(c.title, { font: bold, size: 12, gap: 2 });
|
|
146
|
+
para(`From ${c.business}`, { color: MUTED, gap: 10 });
|
|
147
|
+
fact("Agreement", c.id);
|
|
148
|
+
fact("Sent", bothTimes(c.createdAt, c.timeZone, c.locale));
|
|
149
|
+
fact("Completed", bothTimes(c.completedAt, c.timeZone, c.locale));
|
|
150
|
+
fact("Original SHA-256", c.documentSha256);
|
|
151
|
+
fact("Pages", `${total} page${total === 1 ? "" : "s"} before this certificate`);
|
|
152
|
+
para("The SHA-256 is a fingerprint of the agreement exactly as each signer saw it. Any change to the original PDF, however small, gives a different fingerprint, so anyone can check it is the same document.", { color: MUTED, size: 8.5, gap: 8 });
|
|
153
|
+
rule();
|
|
154
|
+
for (const [i, s] of c.signers.entries()) {
|
|
155
|
+
need(150);
|
|
156
|
+
para(`Signer ${i + 1}: ${s.name}${s.role ? `, ${s.role}` : ""}`, { font: bold, size: 11, gap: 4 });
|
|
157
|
+
fact("Email", s.email);
|
|
158
|
+
fact("Identity", IDENTITY[s.identity]);
|
|
159
|
+
fact("Signed", bothTimes(s.signedAt, c.timeZone, c.locale));
|
|
160
|
+
fact("How", `${METHOD[s.method]}, after agreeing to sign electronically`);
|
|
161
|
+
fact("Name typed", s.typedName);
|
|
162
|
+
if (s.ip)
|
|
163
|
+
fact("IP address", s.ip);
|
|
164
|
+
if (s.userAgent)
|
|
165
|
+
fact("Browser", s.userAgent.slice(0, 300));
|
|
166
|
+
need(70);
|
|
167
|
+
page.drawText("Signature", { x: MARGIN, y: y - 9.5, size: 9.5, font: bold, color: INK });
|
|
168
|
+
if (s.method === "drawn" && s.signature) {
|
|
169
|
+
const img = await doc.embedPng(s.signature);
|
|
170
|
+
const scale = Math.min(200 / img.width, 60 / img.height);
|
|
171
|
+
page.drawImage(img, { x: MARGIN + 120, y: y - img.height * scale, width: img.width * scale, height: img.height * scale });
|
|
172
|
+
y -= img.height * scale + 6;
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
const text = winAnsi(script, s.typedName);
|
|
176
|
+
page.drawText(text, { x: MARGIN + 120, y: y - 20, size: 20, font: script, color: INK });
|
|
177
|
+
y -= 28;
|
|
178
|
+
}
|
|
179
|
+
page.drawLine({ start: { x: MARGIN + 120, y }, end: { x: MARGIN + 340, y }, thickness: 0.6, color: MUTED });
|
|
180
|
+
y -= 12;
|
|
181
|
+
rule();
|
|
182
|
+
}
|
|
183
|
+
para("Audit trail", { font: bold, size: 12, gap: 4 });
|
|
184
|
+
for (const e of c.events)
|
|
185
|
+
para(`${bothTimes(e.at, c.timeZone, c.locale)} ${e.what}`, { size: 8.5, gap: 1 });
|
|
186
|
+
y -= 8;
|
|
187
|
+
para(`Signed electronically with Workbench agreements. Each signer agreed to sign electronically and to receive the agreement by email. Each time is recorded by the server; local times are in ${c.timeZone}.`, { color: MUTED, size: 8, gap: 0 });
|
|
188
|
+
doc.setTitle(winAnsi(regular, `${c.title} (signed)`));
|
|
189
|
+
doc.setSubject(`Agreement ${c.id}, signed`);
|
|
190
|
+
doc.setProducer("Workbench agreements");
|
|
191
|
+
doc.setCreationDate(new Date(c.completedAt));
|
|
192
|
+
doc.setModificationDate(new Date(c.completedAt));
|
|
193
|
+
return doc.save({ useObjectStreams: false });
|
|
194
|
+
}
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { type AgreementsConfig } from "./config.ts";
|
|
3
|
+
export declare const AGREEMENTS_FILE = "workbench/agreements.json";
|
|
4
|
+
export declare const AGREEMENTS_MODULE = "src/agreements.generated.ts";
|
|
5
|
+
/** The generated module for a checked config. */
|
|
6
|
+
export declare function moduleText(c: AgreementsConfig): string;
|
|
7
|
+
/** Reads and checks the project's agreements.json. Throws naming every problem. */
|
|
8
|
+
export declare function readAgreements(dir: string): AgreementsConfig;
|
|
9
|
+
/** Runs one command; returns the exit code. */
|
|
10
|
+
export declare function run(argv: string[], log?: (s: string) => void, err?: (s: string) => void, at?: () => Date): number;
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Run in a project's folder:
|
|
3
|
+
//
|
|
4
|
+
// workbench-agreements init [dir]
|
|
5
|
+
// writes workbench/agreements.json with the recommended answers, unless
|
|
6
|
+
// it is already there.
|
|
7
|
+
// workbench-agreements generate [--check] [dir]
|
|
8
|
+
// checks workbench/agreements.json and writes src/agreements.generated.ts
|
|
9
|
+
// (AGREEMENTS_CONFIG for createAgreements). --check writes nothing and
|
|
10
|
+
// fails when the file is out of date or the answers are wrong.
|
|
11
|
+
// workbench-agreements migration [dir]
|
|
12
|
+
// writes agreementsMigration into supabase/migrations and its down file
|
|
13
|
+
// into supabase/rollback, unless an agreements migration is already there.
|
|
14
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, realpathSync, writeFileSync } from "node:fs";
|
|
15
|
+
import { dirname, join, resolve } from "node:path";
|
|
16
|
+
import { fileURLToPath } from "node:url";
|
|
17
|
+
import { agreementsProblems, STARTER_AGREEMENTS } from "./config.js";
|
|
18
|
+
import { agreementsMigration } from "./migration.js";
|
|
19
|
+
const USAGE = "usage: workbench-agreements init [dir]\n workbench-agreements generate [--check] [dir]\n workbench-agreements migration [dir]";
|
|
20
|
+
export const AGREEMENTS_FILE = "workbench/agreements.json";
|
|
21
|
+
export const AGREEMENTS_MODULE = "src/agreements.generated.ts";
|
|
22
|
+
const read = (path) => readFileSync(path, "utf8").replace(/\r\n/g, "\n");
|
|
23
|
+
/** The generated module for a checked config. */
|
|
24
|
+
export function moduleText(c) {
|
|
25
|
+
return [
|
|
26
|
+
`// Generated by \`npx workbench-agreements generate\` from ${AGREEMENTS_FILE}. Don't edit: change the`,
|
|
27
|
+
"// JSON and generate again (Checks runs `npx workbench-agreements generate --check`).",
|
|
28
|
+
"",
|
|
29
|
+
'import type { AgreementsConfig } from "@mccaskie-labs/workbench-agreements";',
|
|
30
|
+
"",
|
|
31
|
+
`export const AGREEMENTS_CONFIG = ${JSON.stringify(c, null, 2)} as const satisfies AgreementsConfig;`,
|
|
32
|
+
"",
|
|
33
|
+
].join("\n");
|
|
34
|
+
}
|
|
35
|
+
/** Reads and checks the project's agreements.json. Throws naming every problem. */
|
|
36
|
+
export function readAgreements(dir) {
|
|
37
|
+
const path = join(dir, AGREEMENTS_FILE);
|
|
38
|
+
if (!existsSync(path))
|
|
39
|
+
throw new Error(`${AGREEMENTS_FILE} is missing: run npx workbench-agreements init, then answer the agreements questions (capabilities/agreements.json, offer)`);
|
|
40
|
+
let parsed;
|
|
41
|
+
try {
|
|
42
|
+
parsed = JSON.parse(read(path));
|
|
43
|
+
}
|
|
44
|
+
catch (e) {
|
|
45
|
+
throw new Error(`${AGREEMENTS_FILE}: not valid JSON (${e.message})`);
|
|
46
|
+
}
|
|
47
|
+
const problems = agreementsProblems(parsed, AGREEMENTS_FILE);
|
|
48
|
+
if (problems.length)
|
|
49
|
+
throw new Error(problems.join("\n"));
|
|
50
|
+
const { $schema: _, ...config } = parsed;
|
|
51
|
+
return config;
|
|
52
|
+
}
|
|
53
|
+
/** Runs one command; returns the exit code. */
|
|
54
|
+
export function run(argv, log = console.log, err = console.error, at = () => new Date()) {
|
|
55
|
+
const [command, ...args] = argv;
|
|
56
|
+
const check = args.includes("--check");
|
|
57
|
+
const rest = args.filter((a) => a !== "--check");
|
|
58
|
+
const bad = rest.find((a) => a.startsWith("--"));
|
|
59
|
+
if (!["init", "generate", "migration"].includes(command ?? "") || bad || (check && command !== "generate") || rest.length > 1) {
|
|
60
|
+
err(bad ? `${bad} isn't an option\n${USAGE}` : USAGE);
|
|
61
|
+
return 2;
|
|
62
|
+
}
|
|
63
|
+
const dir = resolve(rest[0] ?? ".");
|
|
64
|
+
try {
|
|
65
|
+
if (command === "init") {
|
|
66
|
+
const path = join(dir, AGREEMENTS_FILE);
|
|
67
|
+
if (existsSync(path)) {
|
|
68
|
+
log(`${AGREEMENTS_FILE} is already there; left as it is`);
|
|
69
|
+
return 0;
|
|
70
|
+
}
|
|
71
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
72
|
+
writeFileSync(path, `${JSON.stringify(STARTER_AGREEMENTS, null, 2)}\n`);
|
|
73
|
+
log(`wrote ${AGREEMENTS_FILE}. Set time_zone from workbench/country.json and the answers to the agreements questions, then run npx workbench-agreements generate.`);
|
|
74
|
+
return 0;
|
|
75
|
+
}
|
|
76
|
+
if (command === "migration") {
|
|
77
|
+
const folder = join(dir, "supabase/migrations");
|
|
78
|
+
if (existsSync(folder) && readdirSync(folder).some((f) => f.endsWith("_workbench_agreements.sql"))) {
|
|
79
|
+
log("supabase/migrations already has the agreements migration; nothing written");
|
|
80
|
+
return 0;
|
|
81
|
+
}
|
|
82
|
+
const name = `${at().toISOString().replace(/[-:T]/g, "").slice(0, 14)}_workbench_agreements.sql`;
|
|
83
|
+
mkdirSync(folder, { recursive: true });
|
|
84
|
+
mkdirSync(join(dir, "supabase/rollback"), { recursive: true });
|
|
85
|
+
writeFileSync(join(folder, name), `-- workbench-agreements: agreements and e-signing (docs/agreements.md)\n${agreementsMigration.up}`);
|
|
86
|
+
writeFileSync(join(dir, "supabase/rollback", name.replace(/\.sql$/, "_down.sql")), `-- Reverses ${name}\n${agreementsMigration.down}`);
|
|
87
|
+
log(`wrote supabase/migrations/${name} and its down file`);
|
|
88
|
+
return 0;
|
|
89
|
+
}
|
|
90
|
+
const text = moduleText(readAgreements(dir));
|
|
91
|
+
const out = join(dir, AGREEMENTS_MODULE);
|
|
92
|
+
if (check) {
|
|
93
|
+
if (!existsSync(out) || read(out) !== text) {
|
|
94
|
+
err(`${AGREEMENTS_MODULE} is out of date: run npx workbench-agreements generate`);
|
|
95
|
+
return 1;
|
|
96
|
+
}
|
|
97
|
+
log(`${AGREEMENTS_MODULE} is up to date`);
|
|
98
|
+
return 0;
|
|
99
|
+
}
|
|
100
|
+
mkdirSync(dirname(out), { recursive: true });
|
|
101
|
+
writeFileSync(out, text);
|
|
102
|
+
log(`wrote ${AGREEMENTS_MODULE}`);
|
|
103
|
+
return 0;
|
|
104
|
+
}
|
|
105
|
+
catch (e) {
|
|
106
|
+
err(e.message);
|
|
107
|
+
return 1;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const isMain = (() => {
|
|
111
|
+
try {
|
|
112
|
+
return process.argv[1] !== undefined && realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url));
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
})();
|
|
118
|
+
if (isMain)
|
|
119
|
+
process.exitCode = run(process.argv.slice(2));
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export declare const AGREEMENT_LANGUAGES: readonly ["en", "tl", "taglish"];
|
|
2
|
+
export type AgreementLanguage = (typeof AGREEMENT_LANGUAGES)[number];
|
|
3
|
+
export declare const SIGNING_METHODS: readonly ["typed", "drawn"];
|
|
4
|
+
export type SigningMethod = (typeof SIGNING_METHODS)[number];
|
|
5
|
+
/** How a signer shows the link is theirs. */
|
|
6
|
+
export declare const IDENTITY_CHECKS: readonly ["link", "email_code"];
|
|
7
|
+
export type IdentityCheck = (typeof IDENTITY_CHECKS)[number];
|
|
8
|
+
export interface AgreementsConfig {
|
|
9
|
+
format: 1;
|
|
10
|
+
/** The first part of a signing link's path: https://<project>/<path>/<token>. */
|
|
11
|
+
path: string;
|
|
12
|
+
/** Days a signer has to sign, 1 to 365. */
|
|
13
|
+
expires_after_days: number;
|
|
14
|
+
/** "link": holding the link is enough. "email_code": the signer also types a code emailed to them. */
|
|
15
|
+
identity: IdentityCheck;
|
|
16
|
+
/** The ways a signer may sign. At least one. */
|
|
17
|
+
methods: readonly SigningMethod[];
|
|
18
|
+
/** Keep each signer's IP address and browser in the audit trail. */
|
|
19
|
+
record_device: boolean;
|
|
20
|
+
/** Email reminders, in days after the invitation and after each reminder before it. At most three. [] for none. */
|
|
21
|
+
reminders: {
|
|
22
|
+
after_days: readonly number[];
|
|
23
|
+
};
|
|
24
|
+
retention: {
|
|
25
|
+
/** Years a completed agreement is kept, 1 to 30. */
|
|
26
|
+
signed_years: number;
|
|
27
|
+
/** Days an agreement that was never completed (declined, voided, expired) is kept after it ended, 1 to 3650. */
|
|
28
|
+
unsigned_days: number;
|
|
29
|
+
};
|
|
30
|
+
/** The business's time zone, for the certificate's local times (an IANA name, from workbench/country.json). */
|
|
31
|
+
time_zone: string;
|
|
32
|
+
/** The default language of the signing page and emails. */
|
|
33
|
+
language: AgreementLanguage;
|
|
34
|
+
}
|
|
35
|
+
export declare const MAX_SIGNERS = 5;
|
|
36
|
+
export declare const MAX_REMINDERS = 3;
|
|
37
|
+
/** A good start: the recommended answer to every question. */
|
|
38
|
+
export declare const STARTER_AGREEMENTS: AgreementsConfig;
|
|
39
|
+
/** Every problem with an answers file, each naming its key. Empty when it is good. */
|
|
40
|
+
export declare function agreementsProblems(v: unknown, where?: string): string[];
|
|
41
|
+
/** The config, checked; throws naming every problem. */
|
|
42
|
+
export declare function checkAgreementsConfig(v: unknown, where?: string): AgreementsConfig;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// The project's answers file, workbench/agreements.json, and its checks. The
|
|
2
|
+
// generator (`npx workbench-agreements generate`) turns a checked file into
|
|
3
|
+
// src/agreements.generated.ts, which the app passes to createAgreements.
|
|
4
|
+
export const AGREEMENT_LANGUAGES = ["en", "tl", "taglish"];
|
|
5
|
+
export const SIGNING_METHODS = ["typed", "drawn"];
|
|
6
|
+
/** How a signer shows the link is theirs. */
|
|
7
|
+
export const IDENTITY_CHECKS = ["link", "email_code"];
|
|
8
|
+
export const MAX_SIGNERS = 5;
|
|
9
|
+
export const MAX_REMINDERS = 3;
|
|
10
|
+
/** A good start: the recommended answer to every question. */
|
|
11
|
+
export const STARTER_AGREEMENTS = {
|
|
12
|
+
format: 1,
|
|
13
|
+
path: "sign",
|
|
14
|
+
expires_after_days: 30,
|
|
15
|
+
identity: "email_code",
|
|
16
|
+
methods: ["typed", "drawn"],
|
|
17
|
+
record_device: true,
|
|
18
|
+
reminders: { after_days: [3, 7] },
|
|
19
|
+
retention: { signed_years: 7, unsigned_days: 90 },
|
|
20
|
+
time_zone: "Australia/Sydney",
|
|
21
|
+
language: "en",
|
|
22
|
+
};
|
|
23
|
+
const KEYS = ["format", "path", "expires_after_days", "identity", "methods", "record_device", "reminders", "retention", "time_zone", "language"];
|
|
24
|
+
const whole = (v, min, max) => typeof v === "number" && Number.isInteger(v) && v >= min && v <= max;
|
|
25
|
+
function validZone(z) {
|
|
26
|
+
try {
|
|
27
|
+
new Intl.DateTimeFormat("en", { timeZone: z });
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/** Every problem with an answers file, each naming its key. Empty when it is good. */
|
|
35
|
+
export function agreementsProblems(v, where = "workbench/agreements.json") {
|
|
36
|
+
const p = [];
|
|
37
|
+
const say = (key, what) => p.push(`${where}: ${key} ${what}`);
|
|
38
|
+
if (!v || typeof v !== "object" || Array.isArray(v))
|
|
39
|
+
return [`${where}: must be an object`];
|
|
40
|
+
const c = v;
|
|
41
|
+
for (const k of Object.keys(c))
|
|
42
|
+
if (!KEYS.includes(k) && k !== "$schema")
|
|
43
|
+
say(k, "isn't a setting");
|
|
44
|
+
if (c.format !== 1)
|
|
45
|
+
say("format", "must be 1");
|
|
46
|
+
if (typeof c.path !== "string" || !/^[a-z][a-z0-9-]{0,29}$/.test(c.path))
|
|
47
|
+
say("path", "must be lower-case letters, digits and dashes, starting with a letter, up to 30 characters");
|
|
48
|
+
if (!whole(c.expires_after_days, 1, 365))
|
|
49
|
+
say("expires_after_days", "must be a whole number of days from 1 to 365");
|
|
50
|
+
if (!IDENTITY_CHECKS.includes(c.identity))
|
|
51
|
+
say("identity", 'must be "link" or "email_code"');
|
|
52
|
+
const m = c.methods;
|
|
53
|
+
if (!Array.isArray(m) || m.length < 1 || m.some((x) => !SIGNING_METHODS.includes(x)) || new Set(m).size !== m.length)
|
|
54
|
+
say("methods", 'must list "typed", "drawn" or both, once each');
|
|
55
|
+
if (typeof c.record_device !== "boolean")
|
|
56
|
+
say("record_device", "must be true or false");
|
|
57
|
+
const r = c.reminders;
|
|
58
|
+
if (!r || typeof r !== "object" || !Array.isArray(r.after_days) || r.after_days.length > MAX_REMINDERS || r.after_days.some((d) => !whole(d, 1, 60)))
|
|
59
|
+
say("reminders.after_days", `must list up to ${MAX_REMINDERS} waits of 1 to 60 days ([] for no reminders)`);
|
|
60
|
+
const ret = c.retention;
|
|
61
|
+
if (!ret || typeof ret !== "object")
|
|
62
|
+
say("retention", "must be { signed_years, unsigned_days }");
|
|
63
|
+
else {
|
|
64
|
+
if (!whole(ret.signed_years, 1, 30))
|
|
65
|
+
say("retention.signed_years", "must be a whole number of years from 1 to 30");
|
|
66
|
+
if (!whole(ret.unsigned_days, 1, 3650))
|
|
67
|
+
say("retention.unsigned_days", "must be a whole number of days from 1 to 3650");
|
|
68
|
+
}
|
|
69
|
+
if (typeof c.time_zone !== "string" || !validZone(c.time_zone))
|
|
70
|
+
say("time_zone", "must be an IANA time zone such as Australia/Sydney or Asia/Manila");
|
|
71
|
+
if (!AGREEMENT_LANGUAGES.includes(c.language))
|
|
72
|
+
say("language", "must be en, tl or taglish");
|
|
73
|
+
return p;
|
|
74
|
+
}
|
|
75
|
+
/** The config, checked; throws naming every problem. */
|
|
76
|
+
export function checkAgreementsConfig(v, where) {
|
|
77
|
+
const p = agreementsProblems(v, where);
|
|
78
|
+
if (p.length)
|
|
79
|
+
throw new Error(p.join("\n"));
|
|
80
|
+
return v;
|
|
81
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type AgreementsErrorCode = "invalid_config" | "invalid_request" | "not_found" | "not_allowed" | "conflict" | "database" | "storage" | "pdf";
|
|
2
|
+
/** Every failure workbench-agreements throws. `code` says whose it is: the project's config, the caller's request, the database, the file store or the PDF. */
|
|
3
|
+
export declare class AgreementsError extends Error {
|
|
4
|
+
readonly code: AgreementsErrorCode;
|
|
5
|
+
constructor(code: AgreementsErrorCode, message: string, options?: {
|
|
6
|
+
cause?: unknown;
|
|
7
|
+
});
|
|
8
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Every failure workbench-agreements throws. `code` says whose it is: the project's config, the caller's request, the database, the file store or the PDF. */
|
|
2
|
+
export class AgreementsError extends Error {
|
|
3
|
+
code;
|
|
4
|
+
constructor(code, message, options) {
|
|
5
|
+
super(`workbench-agreements: ${message}`, options);
|
|
6
|
+
this.name = "AgreementsError";
|
|
7
|
+
this.code = code;
|
|
8
|
+
}
|
|
9
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { createAgreements, type Agreement, type AgreementEvent, type Agreements, type AgreementsClient, type AgreementsOptions, type AgreementStatus, type CreateAgreementRequest, type FileStore, type MailerLike, type ShareLink, type Signer, type SignerLink, type SignerRequest, } from "./agreements.ts";
|
|
2
|
+
export { AGREEMENT_LANGUAGES, agreementsProblems, checkAgreementsConfig, IDENTITY_CHECKS, MAX_REMINDERS, MAX_SIGNERS, SIGNING_METHODS, STARTER_AGREEMENTS, type AgreementLanguage, type AgreementsConfig, type IdentityCheck, type SigningMethod, } from "./config.ts";
|
|
3
|
+
export { bothTimes, buildSignedCopy, pdfPages, type CertificateEvent, type CertificateInput, type CertificateSigner } from "./certificate.ts";
|
|
4
|
+
export { agreementsMigration } from "./migration.ts";
|
|
5
|
+
export { renderCodePage, renderDonePage, renderEndedPage, renderSigningPage, type EndedReason, type SigningView } from "./page.ts";
|
|
6
|
+
export { AGREEMENT_STYLES } from "./styles.ts";
|
|
7
|
+
export { AGREEMENT_WORD_KEYS, AGREEMENT_WORDS, agreementWord, type AgreementStrings, type AgreementWordKey } from "./words.ts";
|
|
8
|
+
export { AgreementsError, type AgreementsErrorCode } from "./errors.ts";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// @mccaskie-labs/workbench-agreements: send a branded PDF agreement, let each
|
|
2
|
+
// person sign it online with a typed or drawn signature, keep a timestamped
|
|
3
|
+
// audit trail, and email the signed copy, with a signing certificate, to
|
|
4
|
+
// everyone (docs/agreements.md). Runs in the app's own Worker, database and
|
|
5
|
+
// R2 bucket; email goes through the Gateway with workbench-email.
|
|
6
|
+
export { createAgreements, } from "./agreements.js";
|
|
7
|
+
export { AGREEMENT_LANGUAGES, agreementsProblems, checkAgreementsConfig, IDENTITY_CHECKS, MAX_REMINDERS, MAX_SIGNERS, SIGNING_METHODS, STARTER_AGREEMENTS, } from "./config.js";
|
|
8
|
+
export { bothTimes, buildSignedCopy, pdfPages } from "./certificate.js";
|
|
9
|
+
export { agreementsMigration } from "./migration.js";
|
|
10
|
+
export { renderCodePage, renderDonePage, renderEndedPage, renderSigningPage } from "./page.js";
|
|
11
|
+
export { AGREEMENT_STYLES } from "./styles.js";
|
|
12
|
+
export { AGREEMENT_WORD_KEYS, AGREEMENT_WORDS, agreementWord } from "./words.js";
|
|
13
|
+
export { AgreementsError } from "./errors.js";
|