@mark1russell7/guidebook 0.2.0 → 0.4.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/README.md CHANGED
@@ -12,7 +12,7 @@ The core names no topic. Each topic is an add-on package with its own records. E
12
12
 
13
13
  | Record | What it holds |
14
14
  | --- | --- |
15
- | `Source` | A page or a document: its `id`, `title`, `url`, `license`, the date `checked` of the last read, and the optional name `by` |
15
+ | `Source` | A page or a document: its `id`, `title`, `url`, `license`, the date `checked` of the last read, and the optional name `by`. The optional `terms` is the page of the reuse terms, `updated` is the date of the last change that the source shows, and `sha256` is the hash of the file at the check. |
16
16
  | `CitedUnit` | A unit of text: its `id`, its `use`, its `text`, the `id` of its `source`, and an optional `locator` in the source |
17
17
 
18
18
  A unit has one of three uses:
@@ -57,14 +57,26 @@ The gate gives each finding as data, with a level, a code, the record and a mess
57
57
  | --- | --- | --- |
58
58
  | `duplicate-id` | `error` | Two sources or two units have the same `id`. |
59
59
  | `empty-title`, `empty-text` | `error` | A source has no title, or a unit has no text. |
60
- | `bad-url` | `error` | The address of a source does not start with `https:` or `http:`. |
60
+ | `bad-url` | `error` | The address or the terms address of a source does not start with `https:` or `http:`. With the option `https`, it does not start with `https:`. |
61
61
  | `bad-date` | `error` | The check date is not a real date `YYYY-MM-DD`. |
62
62
  | `unknown-license` | `error` | The license is not in `LICENSES`. |
63
63
  | `checked-in-future` | `error` | The check date is after the date `at` of the gate. |
64
+ | `updated-after-check` | `error` | The change date `updated` is after the check date. |
65
+ | `bad-sha256` | `error` | The hash `sha256` is not 64 lowercase hexadecimal digits. |
66
+ | `missing-field` | `error` | A source has no text in a field of the option `require`. |
64
67
  | `stale` | `warning` | The check is older than `maxAgeDays` (365 by default) before `at`. |
65
68
  | `missing-source` | `error` | A unit cites a source that is not in the list. |
66
69
  | `use-refused` | `error` | The license of the source refuses the use of the unit. |
67
70
 
71
+ A dataset adds its own rules with two options of `checkSources` and `gate`:
72
+
73
+ - `require` is a list of optional fields that each source must have, from `SOURCE_FIELDS`: `by`, `note`, `terms`, `updated` and `sha256`.
74
+ - `https: true` accepts only addresses with `https:`, for the address and the terms address.
75
+
76
+ ```ts
77
+ checkSources(sources, { require: ["by", "terms"], https: true });
78
+ ```
79
+
68
80
  `layerOf(unit, source)` gives the layer of a printed unit. A share-alike quote prints in its own layer, so the rest of the guidebook keeps its own license.
69
81
 
70
82
  ## Limits
package/build/index.d.ts CHANGED
@@ -73,6 +73,12 @@ interface Source {
73
73
  readonly by?: string;
74
74
  /** A term of the publisher in addition to the license, with its date, for example "No AI ingestion (publisher, 2026-11-23)". */
75
75
  readonly note?: string;
76
+ /** The page of the publisher that tells the terms of the reuse, with `https:` or `http:`. */
77
+ readonly terms?: string | undefined;
78
+ /** The date that the source shows as its last change. It is not after the date `checked`. */
79
+ readonly updated?: IsoDate | undefined;
80
+ /** The SHA-256 hash of the file of the source at the date `checked`, as 64 lowercase hexadecimal digits. */
81
+ readonly sha256?: string | undefined;
76
82
  }
77
83
  /** The use of a cited unit. A bundle is a copy of a full source, so a unit cannot have the use `bundle`. */
78
84
  type UnitUse = Exclude<Use, "bundle">;
@@ -93,7 +99,7 @@ interface CitedUnit {
93
99
  */
94
100
  export declare function creditOf(source: Source): string;
95
101
  /** The codes of the findings of the gate. */
96
- export declare const FINDING_CODES: readonly ["duplicate-id", "empty-title", "empty-text", "bad-url", "bad-date", "unknown-license", "checked-in-future", "stale", "missing-source", "use-refused"];
102
+ export declare const FINDING_CODES: readonly ["duplicate-id", "empty-title", "empty-text", "bad-url", "bad-date", "unknown-license", "checked-in-future", "updated-after-check", "bad-sha256", "missing-field", "stale", "missing-source", "use-refused"];
97
103
  /** A code of a finding. */
98
104
  type FindingCode = (typeof FINDING_CODES)[number];
99
105
  /**
@@ -109,12 +115,23 @@ interface Finding<C extends string = FindingCode, R extends string = "source" |
109
115
  readonly id: string;
110
116
  readonly message: string;
111
117
  }
118
+ /** The optional fields of a source, in the order of the findings of the option `require`. */
119
+ export declare const SOURCE_FIELDS: readonly ["by", "note", "terms", "updated", "sha256"];
120
+ /** An optional field of a source that the option `require` of the gate can make necessary. */
121
+ type SourceField = (typeof SOURCE_FIELDS)[number];
112
122
  /** The options of the gate. */
113
123
  interface GateOptions extends UseContext {
114
124
  /** The date of the gate. Without it, the gate does not examine the age of the checks. */
115
125
  readonly at?: IsoDate;
116
126
  /** A source whose check is older than this number of days gets the warning `stale`. The default is 365. */
117
127
  readonly maxAgeDays?: number;
128
+ /**
129
+ * The optional fields that each source must have. A field with no text gets the error `missing-field`.
130
+ * A dataset uses it for its own rules, for example `["by", "terms"]` for the credit and the evidence of a license.
131
+ */
132
+ readonly require?: readonly SourceField[];
133
+ /** When this option is true, the address and the terms address of each source must start with `https:`. The default is false. */
134
+ readonly https?: boolean;
118
135
  }
119
136
  /** The result of a gate. */
120
137
  interface GateResult<F extends Finding<string, string> = Finding> {
@@ -132,7 +149,14 @@ export declare const DAY_MS = 864e5;
132
149
  export declare function timeOf(date: string): number | undefined;
133
150
  /** This function tells if a text is a web address that the gate accepts. */
134
151
  export declare function isWebAddress(url: string): boolean;
135
- /** This function gives the findings of the sources: duplicate ids, empty titles, bad addresses, bad dates, unknown licenses and old checks. */
152
+ /** This function tells if a text is a SHA-256 hash in the form that the gate accepts: 64 lowercase hexadecimal digits. */
153
+ export declare function isSha256(text: string): boolean;
154
+ /**
155
+ * This function gives the findings of the sources: duplicate ids, empty titles, bad addresses, bad dates, unknown licenses and old checks.
156
+ * It also examines the optional fields: the address `terms`, the date `updated` and the form of the hash `sha256`.
157
+ * The options `require` and `https` add the rules of a dataset: the fields that each source must have, and addresses with `https:` only.
158
+ * It throws a `RangeError` for a bad date of the gate or an unknown field in `require`.
159
+ */
136
160
  export declare function checkSources(sources: readonly Source[], options?: GateOptions): readonly Finding[];
137
161
  /** This function gives the findings of the units: duplicate ids, empty text, a missing source, and a use that the license refuses. */
138
162
  export declare function checkUnits(units: readonly CitedUnit[], sources: readonly Source[], options?: UseContext): readonly Finding[];
@@ -142,5 +166,5 @@ export declare function gate(units: readonly CitedUnit[], sources: readonly Sour
142
166
  export declare function assertGate(units: readonly CitedUnit[], sources: readonly Source[], options?: GateOptions): void;
143
167
  /** This function gives the layer of a printed unit. Only a quote of a share-alike source goes to the share-alike layer. */
144
168
  export declare function layerOf(unit: CitedUnit, source: Source): Layer;
145
- export type { CitedUnit, Finding, FindingCode, GateOptions, GateResult, IsoDate, Layer, License, LicenseRule, Source, SourceId, UnitUse, Use, UseContext };
169
+ export type { CitedUnit, Finding, FindingCode, GateOptions, GateResult, IsoDate, Layer, License, LicenseRule, Source, SourceField, SourceId, UnitUse, Use, UseContext };
146
170
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/license.ts","../src/source.ts","../src/gate.ts"],"mappings":";qBAIa;;KAeD,kBAAkB;;qBAGjB;;;;;;KAOD,cAAc;;KAGd;;UAGK;;WAEN;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA,OAAO;;WAEP;;WAEA;;;;;;;;;;;qBAYE,eAAe,SAAS,OAAO,SAAS;;UAqFpC;;WAEN;;WAEA;;;;;;;wBAQK,QAAQ,SAAS,SAAS,KAAK,KAAK,UAAS;;KC5JjD;;KAGA;;UAGK;WACN,IAAI;WACJ;;WAEA;WACA,SAAS;;WAET,SAAS;;WAET;;WAEA;;;KAIC,UAAU,QAAQ;;;;UAKb;WACN;WACA,KAAK;WACL;WACA,QAAQ;;WAER;;;;;;wBAOK,SAAS,QAAQ;;qBCrCpB;;KAcD,sBAAsB;;;;;UAMjB,QAAQ,mBAAmB,aAAa;WAC9C;WACA,MAAM;;WAEN,QAAQ;;WAER;WACA;;;UAIM,oBAAoB;;WAE1B,KAAK;;WAEL;;;UAIM,WAAW,UAAU,0BAA0B;;WAErD;WACA,mBAAmB;;;wBAId,SAAS,UAAU,yBAAyB,mBAAmB,MAAM,WAAW;;wBAKhF,cAAc,cAAc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UT,aAAa,kBAAkB,UAAU,UAAS,uBAA4B;;wBA8C9E,WAAW,gBAAgB,aAAa,kBAAkB,UAAU,UAAS,sBAA2B;;wBAwBxG,KAAK,gBAAgB,aAAa,kBAAkB,UAAU,UAAS,cAAmB;;wBAK1F,WAAW,gBAAgB,aAAa,kBAAkB,UAAU,UAAS;;wBAQ7E,QAAQ,MAAM,WAAW,QAAQ,SAAS"}
package/build/index.js CHANGED
@@ -185,10 +185,21 @@ const FINDING_CODES = [
185
185
  "bad-date",
186
186
  "unknown-license",
187
187
  "checked-in-future",
188
+ "updated-after-check",
189
+ "bad-sha256",
190
+ "missing-field",
188
191
  "stale",
189
192
  "missing-source",
190
193
  "use-refused"
191
194
  ];
195
+ /** The optional fields of a source, in the order of the findings of the option `require`. */
196
+ const SOURCE_FIELDS = [
197
+ "by",
198
+ "note",
199
+ "terms",
200
+ "updated",
201
+ "sha256"
202
+ ];
192
203
  /** This function gives the result of a list of findings. */
193
204
  function resultOf(findings) {
194
205
  return {
@@ -220,7 +231,22 @@ const WEB_ADDRESS = /^https?:\/\/[a-z0-9.-]+(?::\d+)?(?:[/?#]\S*)?$/iu;
220
231
  function isWebAddress(url) {
221
232
  return WEB_ADDRESS.test(url);
222
233
  }
223
- /** This function gives the findings of the sources: duplicate ids, empty titles, bad addresses, bad dates, unknown licenses and old checks. */
234
+ /** The form of a SHA-256 hash: 64 lowercase hexadecimal digits. */
235
+ const SHA256 = /^[0-9a-f]{64}$/u;
236
+ /** This function tells if a web address starts with `https:`, in any case of the letters. */
237
+ function isSecure(url) {
238
+ return url.slice(0, 8).toLowerCase() === "https://";
239
+ }
240
+ /** This function tells if a text is a SHA-256 hash in the form that the gate accepts: 64 lowercase hexadecimal digits. */
241
+ function isSha256(text) {
242
+ return SHA256.test(text);
243
+ }
244
+ /**
245
+ * This function gives the findings of the sources: duplicate ids, empty titles, bad addresses, bad dates, unknown licenses and old checks.
246
+ * It also examines the optional fields: the address `terms`, the date `updated` and the form of the hash `sha256`.
247
+ * The options `require` and `https` add the rules of a dataset: the fields that each source must have, and addresses with `https:` only.
248
+ * It throws a `RangeError` for a bad date of the gate or an unknown field in `require`.
249
+ */
224
250
  function checkSources(sources, options = {}) {
225
251
  const out = [];
226
252
  const error = (code, id, message) => {
@@ -235,14 +261,28 @@ function checkSources(sources, options = {}) {
235
261
  const at = options.at === void 0 ? void 0 : timeOf(options.at);
236
262
  if (options.at !== void 0 && at === void 0) throw new RangeError(`checkSources: the date "${options.at}" of the gate is not a date YYYY-MM-DD.`);
237
263
  const maxAge = options.maxAgeDays ?? 365;
264
+ const unknown = (options.require ?? []).filter((field) => !SOURCE_FIELDS.includes(field));
265
+ if (unknown.length > 0) throw new RangeError(`checkSources: the option require has the unknown fields ${unknown.join(", ")}.`);
266
+ const necessary = SOURCE_FIELDS.filter((field) => options.require?.includes(field) === true);
238
267
  const seen = /* @__PURE__ */ new Set();
239
268
  for (const source of sources) {
240
269
  if (seen.has(source.id)) error("duplicate-id", source.id, `Two sources have the id "${source.id}".`);
241
270
  seen.add(source.id);
242
271
  if (source.title.trim() === "") error("empty-title", source.id, `The source "${source.id}" has no title.`);
243
272
  if (!isWebAddress(source.url)) error("bad-url", source.id, `The source "${source.id}" has the address "${source.url}". It needs an address with https: or http:.`);
273
+ else if (options.https === true && !isSecure(source.url)) error("bad-url", source.id, `The source "${source.id}" has the address "${source.url}". The gate accepts only an address with https:.`);
244
274
  if (!isLicense(source.license)) error("unknown-license", source.id, `The source "${source.id}" has the unknown license "${String(source.license)}".`);
275
+ for (const field of necessary) {
276
+ const value = source[field];
277
+ if (typeof value !== "string" || value.trim() === "") error("missing-field", source.id, `The source "${source.id}" has no field "${field}". The gate makes it necessary.`);
278
+ }
279
+ if (source.terms !== void 0 && !isWebAddress(source.terms)) error("bad-url", source.id, `The source "${source.id}" has the terms address "${source.terms}". It needs an address with https: or http:.`);
280
+ else if (source.terms !== void 0 && options.https === true && !isSecure(source.terms)) error("bad-url", source.id, `The source "${source.id}" has the terms address "${source.terms}". The gate accepts only an address with https:.`);
281
+ if (source.sha256 !== void 0 && !isSha256(source.sha256)) error("bad-sha256", source.id, `The hash of the source "${source.id}" is not 64 lowercase hexadecimal digits.`);
245
282
  const checked = timeOf(source.checked);
283
+ const updated = source.updated === void 0 ? void 0 : timeOf(source.updated);
284
+ if (source.updated !== void 0 && updated === void 0) error("bad-date", source.id, `The source "${source.id}" has the change date "${source.updated}". It needs a date YYYY-MM-DD.`);
285
+ if (updated !== void 0 && checked !== void 0 && updated > checked) error("updated-after-check", source.id, `The source "${source.id}" has a change date after its check date.`);
246
286
  if (checked === void 0) error("bad-date", source.id, `The source "${source.id}" has the check date "${source.checked}". It needs a date YYYY-MM-DD.`);
247
287
  else if (at !== void 0) {
248
288
  if (checked > at) error("checked-in-future", source.id, `The source "${source.id}" has a check date after the date of the gate.`);
@@ -300,6 +340,6 @@ function assertGate(units, sources, options = {}) {
300
340
  function layerOf(unit, source) {
301
341
  return unit.use === "quote" ? LICENSE_RULES[source.license].layer : "main";
302
342
  }
303
- export { DAY_MS, FINDING_CODES, LICENSES, LICENSE_RULES, USES, assertGate, checkSources, checkUnits, creditOf, gate, isWebAddress, layerOf, permits, resultOf, throwOnErrors, timeOf };
343
+ export { DAY_MS, FINDING_CODES, LICENSES, LICENSE_RULES, SOURCE_FIELDS, USES, assertGate, checkSources, checkUnits, creditOf, gate, isSha256, isWebAddress, layerOf, permits, resultOf, throwOnErrors, timeOf };
304
344
 
305
345
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/license.ts","../src/source.ts","../src/gate.ts"],"sourcesContent":["// The licenses of the sources, and the rule of each license.\n// The rules come from the text of each license. A rule tells what a guidebook can do with the text of a source.\n\n/** The licenses that the gate knows. The license `invented` is for test fixtures only. */\nexport const LICENSES = [\n \"us-public-domain\",\n \"public-domain\",\n \"cc0\",\n \"cc-by-4.0\",\n \"cc-by-sa-4.0\",\n \"cc-by-sa-3.0\",\n \"cc-by-nc-sa-3.0\",\n \"ogl-uk-3.0\",\n \"all-rights-reserved\",\n \"link-only\",\n \"invented\",\n] as const;\n\n/** A license of a source. The source gate refuses a use that the license does not permit. */\nexport type License = (typeof LICENSES)[number];\n\n/** The ways to use a source. A unit of text has one use, and an offline copy of the full source is a bundle. */\nexport const USES = [\"quote\", \"summary\", \"fact\", \"bundle\"] as const;\n\n/**\n * A use of a source.\n * `quote` is the text word for word. `summary` is the text in the words of the guidebook. `fact` is a value with a citation.\n * `bundle` is an offline copy of the full source.\n */\nexport type Use = (typeof USES)[number];\n\n/** The layer of a printed quote. A share-alike quote prints in its own layer, with its credit and its license. */\nexport type Layer = \"main\" | \"share-alike\";\n\n/** The rule of one license. */\nexport interface LicenseRule {\n /** The name of the license that a credit line prints. */\n readonly title: string;\n /** The address of the text of the license, or an empty string when the license has no text. */\n readonly url: string;\n /** This field is true when a guidebook can print the text of the source word for word. */\n readonly quote: boolean;\n /** This field is true when a guidebook can keep an offline copy of the full source. */\n readonly bundle: boolean;\n /** This field is true when a guidebook can write a summary or a fact of the source. It is false when a guidebook can only link to the source. */\n readonly units: boolean;\n /** This field is true when the license permits only a use that is not commercial. */\n readonly nonCommercial: boolean;\n /** The layer of a quote. A share-alike license needs its own layer, so the rest of the guidebook keeps its own license. */\n readonly layer: Layer;\n /** This field is true when a quote or a bundle must print the credit line of the source. */\n readonly credit: boolean;\n /** This field is false for the license `invented`. A real guidebook cites only real sources. */\n readonly real: boolean;\n}\n\n/**\n * The rule of each license.\n *\n * - A US federal work (17 USC 105), a work in the public domain and a CC0 work permit each use.\n * - A CC BY work permits each use with a credit line. A CC BY-SA quote also needs its own layer.\n * - A CC BY-NC-SA work permits a bundle only while the guidebook is not commercial. The gate refuses a quote of it.\n * - A work with all rights reserved permits only a summary and a fact, with a citation.\n * - A link-only work permits no use of its text. A guidebook can only link to it, for example when its publisher refuses each AI use.\n */\nexport const LICENSE_RULES: Readonly<Record<License, LicenseRule>> = {\n \"us-public-domain\": {\n title: \"US public domain (17 USC 105)\",\n url: \"https://www.law.cornell.edu/uscode/text/17/105\",\n quote: true,\n bundle: true,\n units: true,\n nonCommercial: false,\n layer: \"main\",\n credit: false,\n real: true,\n },\n \"public-domain\": { title: \"Public domain\", url: \"\", quote: true, bundle: true, units: true, nonCommercial: false, layer: \"main\", credit: false, real: true },\n cc0: {\n title: \"CC0 1.0\",\n url: \"https://creativecommons.org/publicdomain/zero/1.0/\",\n quote: true,\n bundle: true,\n units: true,\n nonCommercial: false,\n layer: \"main\",\n credit: false,\n real: true,\n },\n \"cc-by-4.0\": {\n title: \"CC BY 4.0\",\n url: \"https://creativecommons.org/licenses/by/4.0/\",\n quote: true,\n bundle: true,\n units: true,\n nonCommercial: false,\n layer: \"main\",\n credit: true,\n real: true,\n },\n \"cc-by-sa-4.0\": {\n title: \"CC BY-SA 4.0\",\n url: \"https://creativecommons.org/licenses/by-sa/4.0/\",\n quote: true,\n bundle: true,\n units: true,\n nonCommercial: false,\n layer: \"share-alike\",\n credit: true,\n real: true,\n },\n \"cc-by-sa-3.0\": {\n title: \"CC BY-SA 3.0\",\n url: \"https://creativecommons.org/licenses/by-sa/3.0/\",\n quote: true,\n bundle: true,\n units: true,\n nonCommercial: false,\n layer: \"share-alike\",\n credit: true,\n real: true,\n },\n \"cc-by-nc-sa-3.0\": {\n title: \"CC BY-NC-SA 3.0\",\n url: \"https://creativecommons.org/licenses/by-nc-sa/3.0/\",\n quote: false,\n bundle: true,\n units: true,\n nonCommercial: true,\n layer: \"share-alike\",\n credit: true,\n real: true,\n },\n \"ogl-uk-3.0\": {\n title: \"Open Government Licence v3.0\",\n url: \"https://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/\",\n quote: true,\n bundle: true,\n units: true,\n nonCommercial: false,\n layer: \"main\",\n credit: true,\n real: true,\n },\n \"all-rights-reserved\": { title: \"All rights reserved\", url: \"\", quote: false, bundle: false, units: true, nonCommercial: false, layer: \"main\", credit: false, real: true },\n \"link-only\": { title: \"Link only\", url: \"\", quote: false, bundle: false, units: false, nonCommercial: false, layer: \"main\", credit: false, real: true },\n invented: { title: \"Invented for a test\", url: \"\", quote: false, bundle: false, units: true, nonCommercial: false, layer: \"main\", credit: false, real: false },\n};\n\n/** The context of a use. A guidebook that is sold, or that shows paid advertisements, is commercial. */\nexport interface UseContext {\n /** This field is true when the guidebook is commercial. The default is false. */\n readonly commercial?: boolean;\n /** This field is true when the guidebook is a test fixture, so the license `invented` passes. The default is false. */\n readonly fixture?: boolean;\n}\n\n/**\n * This function tells if a license permits a use.\n * A summary and a fact are the words of the guidebook, so each real license permits them, but a link-only license refuses them.\n * A non-commercial license refuses each use of its text in a commercial guidebook. A summary and a fact are not its text, so they stay.\n */\nexport function permits(license: License, use: Use, context: UseContext = {}): boolean {\n const rule = LICENSE_RULES[license];\n if (!rule.real) return context.fixture === true;\n if (use === \"summary\" || use === \"fact\") return rule.units;\n if (rule.nonCommercial && context.commercial === true) return false;\n return use === \"quote\" ? rule.quote : rule.bundle;\n}\n","// The records of the core: a source and a cited unit of text.\n\nimport { LICENSE_RULES } from \"./license.ts\";\nimport type { License, Use } from \"./license.ts\";\n\n/** A date as `YYYY-MM-DD`. */\nexport type IsoDate = string;\n\n/** The `id` of a source. */\nexport type SourceId = string;\n\n/** A page or a document, with its license and the date of the last check. The license `invented` is for test fixtures only. */\nexport interface Source {\n readonly id: SourceId;\n readonly title: string;\n /** The address of the source, with `https:` or `http:`. */\n readonly url: string;\n readonly license: License;\n /** The date when a person or a job last read the source and compared it with the units that cite it. */\n readonly checked: IsoDate;\n /** The name that a credit line gives, for example the agency or the authors. The default is the title. */\n readonly by?: string;\n /** A term of the publisher in addition to the license, with its date, for example \"No AI ingestion (publisher, 2026-11-23)\". */\n readonly note?: string;\n}\n\n/** The use of a cited unit. A bundle is a copy of a full source, so a unit cannot have the use `bundle`. */\nexport type UnitUse = Exclude<Use, \"bundle\">;\n\n/**\n * A cited unit: a quote (the text word for word), a summary (the words of the guidebook) or a fact (a value with a citation).\n */\nexport interface CitedUnit {\n readonly id: string;\n readonly use: UnitUse;\n readonly text: string;\n readonly source: SourceId;\n /** The place of the text in the source, for example a section or a page. */\n readonly locator?: string;\n}\n\n/**\n * This function gives the credit line of a source: its name, its title, its address, its license and the date of the check.\n * A guidebook prints it under each quote and each bundle whose license needs a credit.\n */\nexport function creditOf(source: Source): string {\n const rule = LICENSE_RULES[source.license];\n const name = source.by === undefined || source.by === source.title ? source.title : `${source.by}: ${source.title}`;\n const license = rule.url === \"\" ? rule.title : `${rule.title} (${rule.url})`;\n return `${name}. ${source.url}. ${license}. Checked ${source.checked}.`;\n}\n","// The source gate: the checks of the sources and the cited units of a guidebook.\n// The gate gives each finding as data. A caller prints the findings, or stops with `assertGate`.\n\nimport { LICENSE_RULES, LICENSES, permits } from \"./license.ts\";\nimport type { Layer, License, UseContext } from \"./license.ts\";\nimport type { CitedUnit, IsoDate, Source, SourceId } from \"./source.ts\";\n\n/** The codes of the findings of the gate. */\nexport const FINDING_CODES = [\n \"duplicate-id\",\n \"empty-title\",\n \"empty-text\",\n \"bad-url\",\n \"bad-date\",\n \"unknown-license\",\n \"checked-in-future\",\n \"stale\",\n \"missing-source\",\n \"use-refused\",\n] as const;\n\n/** A code of a finding. */\nexport type FindingCode = (typeof FINDING_CODES)[number];\n\n/**\n * A finding of a gate. A finding with the level `error` stops the gate. A `warning` tells of a review to do.\n * An add-on gives its own codes `C` and its own kinds of record `R`.\n */\nexport interface Finding<C extends string = FindingCode, R extends string = \"source\" | \"unit\"> {\n readonly level: \"error\" | \"warning\";\n readonly code: C;\n /** The kind of the record with the finding. */\n readonly record: R;\n /** The `id` of the record with the finding. */\n readonly id: string;\n readonly message: string;\n}\n\n/** The options of the gate. */\nexport interface GateOptions extends UseContext {\n /** The date of the gate. Without it, the gate does not examine the age of the checks. */\n readonly at?: IsoDate;\n /** A source whose check is older than this number of days gets the warning `stale`. The default is 365. */\n readonly maxAgeDays?: number;\n}\n\n/** The result of a gate. */\nexport interface GateResult<F extends Finding<string, string> = Finding> {\n /** This field is true when the result has no finding with the level `error`. */\n readonly ok: boolean;\n readonly findings: readonly F[];\n}\n\n/** This function gives the result of a list of findings. */\nexport function resultOf<F extends Finding<string, string>>(findings: readonly F[]): GateResult<F> {\n return { ok: findings.every((finding) => finding.level !== \"error\"), findings };\n}\n\n/** This function throws an error with each finding of the level `error`. The name of the gate starts the message. */\nexport function throwOnErrors(name: string, findings: readonly Finding<string, string>[]): void {\n const lines = findings.filter((finding) => finding.level === \"error\").map((finding) => `- ${finding.code}: ${finding.message}`);\n if (lines.length > 0) throw new Error(`The ${name} found ${lines.length} errors:\\n${lines.join(\"\\n\")}`);\n}\n\n/** The number of milliseconds in one day. */\nexport const DAY_MS = 86_400_000;\nconst ISO_DATE = /^\\d{4}-\\d{2}-\\d{2}$/;\n\n/** This function gives the time of a date `YYYY-MM-DD` in milliseconds, or `undefined` when the date is not a real date. */\nexport function timeOf(date: string): number | undefined {\n if (!ISO_DATE.test(date)) return undefined;\n const time = Date.parse(`${date}T00:00:00Z`);\n if (Number.isNaN(time)) return undefined;\n return new Date(time).toISOString().slice(0, 10) === date ? time : undefined;\n}\n\nfunction isLicense(value: unknown): value is License {\n return typeof value === \"string\" && (LICENSES as readonly string[]).includes(value);\n}\n\n/** An address with `https:` or `http:`, a host, and no space. The core uses no platform type, so it does not use `URL`. */\nconst WEB_ADDRESS = /^https?:\\/\\/[a-z0-9.-]+(?::\\d+)?(?:[/?#]\\S*)?$/iu;\n\n/** This function tells if a text is a web address that the gate accepts. */\nexport function isWebAddress(url: string): boolean {\n return WEB_ADDRESS.test(url);\n}\n\n/** This function gives the findings of the sources: duplicate ids, empty titles, bad addresses, bad dates, unknown licenses and old checks. */\nexport function checkSources(sources: readonly Source[], options: GateOptions = {}): readonly Finding[] {\n const out: Finding[] = [];\n const error = (code: FindingCode, id: string, message: string): void => {\n out.push({ level: \"error\", code, record: \"source\", id, message });\n };\n const at = options.at === undefined ? undefined : timeOf(options.at);\n if (options.at !== undefined && at === undefined) throw new RangeError(`checkSources: the date \"${options.at}\" of the gate is not a date YYYY-MM-DD.`);\n const maxAge = options.maxAgeDays ?? 365;\n const seen = new Set<SourceId>();\n for (const source of sources) {\n if (seen.has(source.id)) error(\"duplicate-id\", source.id, `Two sources have the id \"${source.id}\".`);\n seen.add(source.id);\n if (source.title.trim() === \"\") error(\"empty-title\", source.id, `The source \"${source.id}\" has no title.`);\n if (!isWebAddress(source.url)) error(\"bad-url\", source.id, `The source \"${source.id}\" has the address \"${source.url}\". It needs an address with https: or http:.`);\n if (!isLicense(source.license)) error(\"unknown-license\", source.id, `The source \"${source.id}\" has the unknown license \"${String(source.license)}\".`);\n const checked = timeOf(source.checked);\n if (checked === undefined) {\n error(\"bad-date\", source.id, `The source \"${source.id}\" has the check date \"${source.checked}\". It needs a date YYYY-MM-DD.`);\n } else if (at !== undefined) {\n if (checked > at) error(\"checked-in-future\", source.id, `The source \"${source.id}\" has a check date after the date of the gate.`);\n const days = (at - checked) / DAY_MS;\n if (days > maxAge) {\n out.push({ level: \"warning\", code: \"stale\", record: \"source\", id: source.id, message: `The last check of the source \"${source.id}\" is ${days} days old. Read the source again.` });\n }\n }\n }\n return out;\n}\n\n/** This function gives the findings of the units: duplicate ids, empty text, a missing source, and a use that the license refuses. */\nexport function checkUnits(units: readonly CitedUnit[], sources: readonly Source[], options: UseContext = {}): readonly Finding[] {\n const out: Finding[] = [];\n const error = (code: FindingCode, id: string, message: string): void => {\n out.push({ level: \"error\", code, record: \"unit\", id, message });\n };\n const byId = new Map(sources.map((source) => [source.id, source] as const));\n const seen = new Set<string>();\n for (const unit of units) {\n if (seen.has(unit.id)) error(\"duplicate-id\", unit.id, `Two units have the id \"${unit.id}\".`);\n seen.add(unit.id);\n if (unit.text.trim() === \"\") error(\"empty-text\", unit.id, `The unit \"${unit.id}\" has no text.`);\n const source = byId.get(unit.source);\n if (source === undefined) {\n error(\"missing-source\", unit.id, `The unit \"${unit.id}\" cites the source \"${unit.source}\", but the list does not have it.`);\n continue;\n }\n if (isLicense(source.license) && !permits(source.license, unit.use, options)) {\n error(\"use-refused\", unit.id, `The license \"${source.license}\" of the source \"${source.id}\" refuses the use \"${unit.use}\" of the unit \"${unit.id}\".`);\n }\n }\n return out;\n}\n\n/** This function applies the gate to the sources and the units of a guidebook. */\nexport function gate(units: readonly CitedUnit[], sources: readonly Source[], options: GateOptions = {}): GateResult {\n return resultOf([...checkSources(sources, options), ...checkUnits(units, sources, options)]);\n}\n\n/** This function applies the gate, and throws an error with each finding of the level `error`. */\nexport function assertGate(units: readonly CitedUnit[], sources: readonly Source[], options: GateOptions = {}): void {\n const result = gate(units, sources, options);\n if (result.ok) return;\n const lines = result.findings.filter((finding) => finding.level === \"error\").map((finding) => `- ${finding.code}: ${finding.message}`);\n throw new Error(`The source gate found ${lines.length} errors:\\n${lines.join(\"\\n\")}`);\n}\n\n/** This function gives the layer of a printed unit. Only a quote of a share-alike source goes to the share-alike layer. */\nexport function layerOf(unit: CitedUnit, source: Source): Layer {\n return unit.use === \"quote\" ? LICENSE_RULES[source.license].layer : \"main\";\n}\n"],"mappings":";;AAIA,MAAa,WAAW;CACtB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;;AAMA,MAAa,OAAO;CAAC;CAAS;CAAW;CAAQ;AAAQ;;;;;;;;;;AA2CzD,MAAa,gBAAwD;CACnE,oBAAoB;EAClB,OAAO;EACP,KAAK;EACL,OAAO;EACP,QAAQ;EACR,OAAO;EACP,eAAe;EACf,OAAO;EACP,QAAQ;EACR,MAAM;CACR;CACA,iBAAiB;EAAE,OAAO;EAAiB,KAAK;EAAI,OAAO;EAAM,QAAQ;EAAM,OAAO;EAAM,eAAe;EAAO,OAAO;EAAQ,QAAQ;EAAO,MAAM;CAAK;CAC3J,KAAK;EACH,OAAO;EACP,KAAK;EACL,OAAO;EACP,QAAQ;EACR,OAAO;EACP,eAAe;EACf,OAAO;EACP,QAAQ;EACR,MAAM;CACR;CACA,aAAa;E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UAAuB,CAAC,GAAe;CACnH,OAAO,SAAS,CAAC,GAAG,aAAa,SAAS,OAAO,GAAG,GAAG,WAAW,OAAO,SAAS,OAAO,CAAC,CAAC;AAC7F;;AAGA,SAAgB,WAAW,OAA6B,SAA4B,UAAuB,CAAC,GAAS;CACnH,MAAM,SAAS,KAAK,OAAO,SAAS,OAAO;CAC3C,IAAI,OAAO,IAAI;CACf,MAAM,QAAQ,OAAO,SAAS,QAAQ,YAAY,QAAQ,UAAU,OAAO,CAAC,CAAC,KAAK,YAAY,KAAK,QAAQ,KAAK,IAAI,QAAQ,SAAS;CACrI,MAAM,IAAI,MAAM,yBAAyB,MAAM,OAAO,YAAY,MAAM,KAAK,IAAI,GAAG;AACtF;;AAGA,SAAgB,QAAQ,MAAiB,QAAuB;CAC9D,OAAO,KAAK,QAAQ,UAAU,cAAc,OAAO,QAAQ,CAAC,QAAQ;AACtE"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/license.ts","../src/source.ts","../src/gate.ts"],"sourcesContent":["// The licenses of the sources, and the rule of each license.\n// The rules come from the text of each license. A rule tells what a guidebook can do with the text of a source.\n\n/** The licenses that the gate knows. The license `invented` is for test fixtures only. */\nexport const LICENSES = [\n \"us-public-domain\",\n \"public-domain\",\n \"cc0\",\n \"cc-by-4.0\",\n \"cc-by-sa-4.0\",\n \"cc-by-sa-3.0\",\n \"cc-by-nc-sa-3.0\",\n \"ogl-uk-3.0\",\n \"all-rights-reserved\",\n \"link-only\",\n \"invented\",\n] as const;\n\n/** A license of a source. The source gate refuses a use that the license does not permit. */\nexport type License = (typeof LICENSES)[number];\n\n/** The ways to use a source. A unit of text has one use, and an offline copy of the full source is a bundle. */\nexport const USES = [\"quote\", \"summary\", \"fact\", \"bundle\"] as const;\n\n/**\n * A use of a source.\n * `quote` is the text word for word. `summary` is the text in the words of the guidebook. `fact` is a value with a citation.\n * `bundle` is an offline copy of the full source.\n */\nexport type Use = (typeof USES)[number];\n\n/** The layer of a printed quote. A share-alike quote prints in its own layer, with its credit and its license. */\nexport type Layer = \"main\" | \"share-alike\";\n\n/** The rule of one license. */\nexport interface LicenseRule {\n /** The name of the license that a credit line prints. */\n readonly title: string;\n /** The address of the text of the license, or an empty string when the license has no text. */\n readonly url: string;\n /** This field is true when a guidebook can print the text of the source word for word. */\n readonly quote: boolean;\n /** This field is true when a guidebook can keep an offline copy of the full source. */\n readonly bundle: boolean;\n /** This field is true when a guidebook can write a summary or a fact of the source. It is false when a guidebook can only link to the source. */\n readonly units: boolean;\n /** This field is true when the license permits only a use that is not commercial. */\n readonly nonCommercial: boolean;\n /** The layer of a quote. A share-alike license needs its own layer, so the rest of the guidebook keeps its own license. */\n readonly layer: Layer;\n /** This field is true when a quote or a bundle must print the credit line of the source. */\n readonly credit: boolean;\n /** This field is false for the license `invented`. A real guidebook cites only real sources. */\n readonly real: boolean;\n}\n\n/**\n * The rule of each license.\n *\n * - A US federal work (17 USC 105), a work in the public domain and a CC0 work permit each use.\n * - A CC BY work permits each use with a credit line. A CC BY-SA quote also needs its own layer.\n * - A CC BY-NC-SA work permits a bundle only while the guidebook is not commercial. The gate refuses a quote of it.\n * - A work with all rights reserved permits only a summary and a fact, with a citation.\n * - A link-only work permits no use of its text. A guidebook can only link to it, for example when its publisher refuses each AI use.\n */\nexport const LICENSE_RULES: Readonly<Record<License, LicenseRule>> = {\n \"us-public-domain\": {\n title: \"US public domain (17 USC 105)\",\n url: \"https://www.law.cornell.edu/uscode/text/17/105\",\n quote: true,\n bundle: true,\n units: true,\n nonCommercial: false,\n layer: \"main\",\n credit: false,\n real: true,\n },\n \"public-domain\": { title: \"Public domain\", url: \"\", quote: true, bundle: true, units: true, nonCommercial: false, layer: \"main\", credit: false, real: true },\n cc0: {\n title: \"CC0 1.0\",\n url: \"https://creativecommons.org/publicdomain/zero/1.0/\",\n quote: true,\n bundle: true,\n units: true,\n nonCommercial: false,\n layer: \"main\",\n credit: false,\n real: true,\n },\n \"cc-by-4.0\": {\n title: \"CC BY 4.0\",\n url: \"https://creativecommons.org/licenses/by/4.0/\",\n quote: true,\n bundle: true,\n units: true,\n nonCommercial: false,\n layer: \"main\",\n credit: true,\n real: true,\n },\n \"cc-by-sa-4.0\": {\n title: \"CC BY-SA 4.0\",\n url: \"https://creativecommons.org/licenses/by-sa/4.0/\",\n quote: true,\n bundle: true,\n units: true,\n nonCommercial: false,\n layer: \"share-alike\",\n credit: true,\n real: true,\n },\n \"cc-by-sa-3.0\": {\n title: \"CC BY-SA 3.0\",\n url: \"https://creativecommons.org/licenses/by-sa/3.0/\",\n quote: true,\n bundle: true,\n units: true,\n nonCommercial: false,\n layer: \"share-alike\",\n credit: true,\n real: true,\n },\n \"cc-by-nc-sa-3.0\": {\n title: \"CC BY-NC-SA 3.0\",\n url: \"https://creativecommons.org/licenses/by-nc-sa/3.0/\",\n quote: false,\n bundle: true,\n units: true,\n nonCommercial: true,\n layer: \"share-alike\",\n credit: true,\n real: true,\n },\n \"ogl-uk-3.0\": {\n title: \"Open Government Licence v3.0\",\n url: \"https://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/\",\n quote: true,\n bundle: true,\n units: true,\n nonCommercial: false,\n layer: \"main\",\n credit: true,\n real: true,\n },\n \"all-rights-reserved\": { title: \"All rights reserved\", url: \"\", quote: false, bundle: false, units: true, nonCommercial: false, layer: \"main\", credit: false, real: true },\n \"link-only\": { title: \"Link only\", url: \"\", quote: false, bundle: false, units: false, nonCommercial: false, layer: \"main\", credit: false, real: true },\n invented: { title: \"Invented for a test\", url: \"\", quote: false, bundle: false, units: true, nonCommercial: false, layer: \"main\", credit: false, real: false },\n};\n\n/** The context of a use. A guidebook that is sold, or that shows paid advertisements, is commercial. */\nexport interface UseContext {\n /** This field is true when the guidebook is commercial. The default is false. */\n readonly commercial?: boolean;\n /** This field is true when the guidebook is a test fixture, so the license `invented` passes. The default is false. */\n readonly fixture?: boolean;\n}\n\n/**\n * This function tells if a license permits a use.\n * A summary and a fact are the words of the guidebook, so each real license permits them, but a link-only license refuses them.\n * A non-commercial license refuses each use of its text in a commercial guidebook. A summary and a fact are not its text, so they stay.\n */\nexport function permits(license: License, use: Use, context: UseContext = {}): boolean {\n const rule = LICENSE_RULES[license];\n if (!rule.real) return context.fixture === true;\n if (use === \"summary\" || use === \"fact\") return rule.units;\n if (rule.nonCommercial && context.commercial === true) return false;\n return use === \"quote\" ? rule.quote : rule.bundle;\n}\n","// The records of the core: a source and a cited unit of text.\n\nimport { LICENSE_RULES } from \"./license.ts\";\nimport type { License, Use } from \"./license.ts\";\n\n/** A date as `YYYY-MM-DD`. */\nexport type IsoDate = string;\n\n/** The `id` of a source. */\nexport type SourceId = string;\n\n/** A page or a document, with its license and the date of the last check. The license `invented` is for test fixtures only. */\nexport interface Source {\n readonly id: SourceId;\n readonly title: string;\n /** The address of the source, with `https:` or `http:`. */\n readonly url: string;\n readonly license: License;\n /** The date when a person or a job last read the source and compared it with the units that cite it. */\n readonly checked: IsoDate;\n /** The name that a credit line gives, for example the agency or the authors. The default is the title. */\n readonly by?: string;\n /** A term of the publisher in addition to the license, with its date, for example \"No AI ingestion (publisher, 2026-11-23)\". */\n readonly note?: string;\n /** The page of the publisher that tells the terms of the reuse, with `https:` or `http:`. */\n readonly terms?: string | undefined;\n /** The date that the source shows as its last change. It is not after the date `checked`. */\n readonly updated?: IsoDate | undefined;\n /** The SHA-256 hash of the file of the source at the date `checked`, as 64 lowercase hexadecimal digits. */\n readonly sha256?: string | undefined;\n}\n\n/** The use of a cited unit. A bundle is a copy of a full source, so a unit cannot have the use `bundle`. */\nexport type UnitUse = Exclude<Use, \"bundle\">;\n\n/**\n * A cited unit: a quote (the text word for word), a summary (the words of the guidebook) or a fact (a value with a citation).\n */\nexport interface CitedUnit {\n readonly id: string;\n readonly use: UnitUse;\n readonly text: string;\n readonly source: SourceId;\n /** The place of the text in the source, for example a section or a page. */\n readonly locator?: string;\n}\n\n/**\n * This function gives the credit line of a source: its name, its title, its address, its license and the date of the check.\n * A guidebook prints it under each quote and each bundle whose license needs a credit.\n */\nexport function creditOf(source: Source): string {\n const rule = LICENSE_RULES[source.license];\n const name = source.by === undefined || source.by === source.title ? source.title : `${source.by}: ${source.title}`;\n const license = rule.url === \"\" ? rule.title : `${rule.title} (${rule.url})`;\n return `${name}. ${source.url}. ${license}. Checked ${source.checked}.`;\n}\n","// The source gate: the checks of the sources and the cited units of a guidebook.\n// The gate gives each finding as data. A caller prints the findings, or stops with `assertGate`.\n\nimport { LICENSE_RULES, LICENSES, permits } from \"./license.ts\";\nimport type { Layer, License, UseContext } from \"./license.ts\";\nimport type { CitedUnit, IsoDate, Source, SourceId } from \"./source.ts\";\n\n/** The codes of the findings of the gate. */\nexport const FINDING_CODES = [\n \"duplicate-id\",\n \"empty-title\",\n \"empty-text\",\n \"bad-url\",\n \"bad-date\",\n \"unknown-license\",\n \"checked-in-future\",\n \"updated-after-check\",\n \"bad-sha256\",\n \"missing-field\",\n \"stale\",\n \"missing-source\",\n \"use-refused\",\n] as const;\n\n/** A code of a finding. */\nexport type FindingCode = (typeof FINDING_CODES)[number];\n\n/**\n * A finding of a gate. A finding with the level `error` stops the gate. A `warning` tells of a review to do.\n * An add-on gives its own codes `C` and its own kinds of record `R`.\n */\nexport interface Finding<C extends string = FindingCode, R extends string = \"source\" | \"unit\"> {\n readonly level: \"error\" | \"warning\";\n readonly code: C;\n /** The kind of the record with the finding. */\n readonly record: R;\n /** The `id` of the record with the finding. */\n readonly id: string;\n readonly message: string;\n}\n\n/** The optional fields of a source, in the order of the findings of the option `require`. */\nexport const SOURCE_FIELDS: readonly [\"by\", \"note\", \"terms\", \"updated\", \"sha256\"] = [\"by\", \"note\", \"terms\", \"updated\", \"sha256\"];\n\n/** An optional field of a source that the option `require` of the gate can make necessary. */\nexport type SourceField = (typeof SOURCE_FIELDS)[number];\n\n/** The options of the gate. */\nexport interface GateOptions extends UseContext {\n /** The date of the gate. Without it, the gate does not examine the age of the checks. */\n readonly at?: IsoDate;\n /** A source whose check is older than this number of days gets the warning `stale`. The default is 365. */\n readonly maxAgeDays?: number;\n /**\n * The optional fields that each source must have. A field with no text gets the error `missing-field`.\n * A dataset uses it for its own rules, for example `[\"by\", \"terms\"]` for the credit and the evidence of a license.\n */\n readonly require?: readonly SourceField[];\n /** When this option is true, the address and the terms address of each source must start with `https:`. The default is false. */\n readonly https?: boolean;\n}\n\n/** The result of a gate. */\nexport interface GateResult<F extends Finding<string, string> = Finding> {\n /** This field is true when the result has no finding with the level `error`. */\n readonly ok: boolean;\n readonly findings: readonly F[];\n}\n\n/** This function gives the result of a list of findings. */\nexport function resultOf<F extends Finding<string, string>>(findings: readonly F[]): GateResult<F> {\n return { ok: findings.every((finding) => finding.level !== \"error\"), findings };\n}\n\n/** This function throws an error with each finding of the level `error`. The name of the gate starts the message. */\nexport function throwOnErrors(name: string, findings: readonly Finding<string, string>[]): void {\n const lines = findings.filter((finding) => finding.level === \"error\").map((finding) => `- ${finding.code}: ${finding.message}`);\n if (lines.length > 0) throw new Error(`The ${name} found ${lines.length} errors:\\n${lines.join(\"\\n\")}`);\n}\n\n/** The number of milliseconds in one day. */\nexport const DAY_MS = 86_400_000;\nconst ISO_DATE = /^\\d{4}-\\d{2}-\\d{2}$/;\n\n/** This function gives the time of a date `YYYY-MM-DD` in milliseconds, or `undefined` when the date is not a real date. */\nexport function timeOf(date: string): number | undefined {\n if (!ISO_DATE.test(date)) return undefined;\n const time = Date.parse(`${date}T00:00:00Z`);\n if (Number.isNaN(time)) return undefined;\n return new Date(time).toISOString().slice(0, 10) === date ? time : undefined;\n}\n\nfunction isLicense(value: unknown): value is License {\n return typeof value === \"string\" && (LICENSES as readonly string[]).includes(value);\n}\n\n/** An address with `https:` or `http:`, a host, and no space. The core uses no platform type, so it does not use `URL`. */\nconst WEB_ADDRESS = /^https?:\\/\\/[a-z0-9.-]+(?::\\d+)?(?:[/?#]\\S*)?$/iu;\n\n/** This function tells if a text is a web address that the gate accepts. */\nexport function isWebAddress(url: string): boolean {\n return WEB_ADDRESS.test(url);\n}\n\n/** The form of a SHA-256 hash: 64 lowercase hexadecimal digits. */\nconst SHA256 = /^[0-9a-f]{64}$/u;\n\n/** This function tells if a web address starts with `https:`, in any case of the letters. */\nfunction isSecure(url: string): boolean {\n return url.slice(0, 8).toLowerCase() === \"https://\";\n}\n\n/** This function tells if a text is a SHA-256 hash in the form that the gate accepts: 64 lowercase hexadecimal digits. */\nexport function isSha256(text: string): boolean {\n return SHA256.test(text);\n}\n\n/**\n * This function gives the findings of the sources: duplicate ids, empty titles, bad addresses, bad dates, unknown licenses and old checks.\n * It also examines the optional fields: the address `terms`, the date `updated` and the form of the hash `sha256`.\n * The options `require` and `https` add the rules of a dataset: the fields that each source must have, and addresses with `https:` only.\n * It throws a `RangeError` for a bad date of the gate or an unknown field in `require`.\n */\nexport function checkSources(sources: readonly Source[], options: GateOptions = {}): readonly Finding[] {\n const out: Finding[] = [];\n const error = (code: FindingCode, id: string, message: string): void => {\n out.push({ level: \"error\", code, record: \"source\", id, message });\n };\n const at = options.at === undefined ? undefined : timeOf(options.at);\n if (options.at !== undefined && at === undefined) throw new RangeError(`checkSources: the date \"${options.at}\" of the gate is not a date YYYY-MM-DD.`);\n const maxAge = options.maxAgeDays ?? 365;\n const unknown = (options.require ?? []).filter((field) => !(SOURCE_FIELDS as readonly string[]).includes(field));\n if (unknown.length > 0) throw new RangeError(`checkSources: the option require has the unknown fields ${unknown.join(\", \")}.`);\n const necessary = SOURCE_FIELDS.filter((field) => options.require?.includes(field) === true);\n const seen = new Set<SourceId>();\n for (const source of sources) {\n if (seen.has(source.id)) error(\"duplicate-id\", source.id, `Two sources have the id \"${source.id}\".`);\n seen.add(source.id);\n if (source.title.trim() === \"\") error(\"empty-title\", source.id, `The source \"${source.id}\" has no title.`);\n if (!isWebAddress(source.url)) error(\"bad-url\", source.id, `The source \"${source.id}\" has the address \"${source.url}\". It needs an address with https: or http:.`);\n else if (options.https === true && !isSecure(source.url)) error(\"bad-url\", source.id, `The source \"${source.id}\" has the address \"${source.url}\". The gate accepts only an address with https:.`);\n if (!isLicense(source.license)) error(\"unknown-license\", source.id, `The source \"${source.id}\" has the unknown license \"${String(source.license)}\".`);\n for (const field of necessary) {\n const value: unknown = source[field];\n if (typeof value !== \"string\" || value.trim() === \"\") error(\"missing-field\", source.id, `The source \"${source.id}\" has no field \"${field}\". The gate makes it necessary.`);\n }\n if (source.terms !== undefined && !isWebAddress(source.terms)) error(\"bad-url\", source.id, `The source \"${source.id}\" has the terms address \"${source.terms}\". It needs an address with https: or http:.`);\n else if (source.terms !== undefined && options.https === true && !isSecure(source.terms)) {\n error(\"bad-url\", source.id, `The source \"${source.id}\" has the terms address \"${source.terms}\". The gate accepts only an address with https:.`);\n }\n if (source.sha256 !== undefined && !isSha256(source.sha256)) error(\"bad-sha256\", source.id, `The hash of the source \"${source.id}\" is not 64 lowercase hexadecimal digits.`);\n const checked = timeOf(source.checked);\n const updated = source.updated === undefined ? undefined : timeOf(source.updated);\n if (source.updated !== undefined && updated === undefined) error(\"bad-date\", source.id, `The source \"${source.id}\" has the change date \"${source.updated}\". It needs a date YYYY-MM-DD.`);\n if (updated !== undefined && checked !== undefined && updated > checked) error(\"updated-after-check\", source.id, `The source \"${source.id}\" has a change date after its check date.`);\n if (checked === undefined) {\n error(\"bad-date\", source.id, `The source \"${source.id}\" has the check date \"${source.checked}\". It needs a date YYYY-MM-DD.`);\n } else if (at !== undefined) {\n if (checked > at) error(\"checked-in-future\", source.id, `The source \"${source.id}\" has a check date after the date of the gate.`);\n const days = (at - checked) / DAY_MS;\n if (days > maxAge) {\n out.push({ level: \"warning\", code: \"stale\", record: \"source\", id: source.id, message: `The last check of the source \"${source.id}\" is ${days} days old. Read the source again.` });\n }\n }\n }\n return out;\n}\n\n/** This function gives the findings of the units: duplicate ids, empty text, a missing source, and a use that the license refuses. */\nexport function checkUnits(units: readonly CitedUnit[], sources: readonly Source[], options: UseContext = {}): readonly Finding[] {\n const out: Finding[] = [];\n const error = (code: FindingCode, id: string, message: string): void => {\n out.push({ level: \"error\", code, record: \"unit\", id, message });\n };\n const byId = new Map(sources.map((source) => [source.id, source] as const));\n const seen = new Set<string>();\n for (const unit of units) {\n if (seen.has(unit.id)) error(\"duplicate-id\", unit.id, `Two units have the id \"${unit.id}\".`);\n seen.add(unit.id);\n if (unit.text.trim() === \"\") error(\"empty-text\", unit.id, `The unit \"${unit.id}\" has no text.`);\n const source = byId.get(unit.source);\n if (source === undefined) {\n error(\"missing-source\", unit.id, `The unit \"${unit.id}\" cites the source \"${unit.source}\", but the list does not have it.`);\n continue;\n }\n if (isLicense(source.license) && !permits(source.license, unit.use, options)) {\n error(\"use-refused\", unit.id, `The license \"${source.license}\" of the source \"${source.id}\" refuses the use \"${unit.use}\" of the unit \"${unit.id}\".`);\n }\n }\n return out;\n}\n\n/** This function applies the gate to the sources and the units of a guidebook. */\nexport function gate(units: readonly CitedUnit[], sources: readonly Source[], options: GateOptions = {}): GateResult {\n return resultOf([...checkSources(sources, options), ...checkUnits(units, sources, options)]);\n}\n\n/** This function applies the gate, and throws an error with each finding of the level `error`. */\nexport function assertGate(units: readonly CitedUnit[], sources: readonly Source[], options: GateOptions = {}): void {\n const result = gate(units, sources, options);\n if (result.ok) return;\n const lines = result.findings.filter((finding) => finding.level === \"error\").map((finding) => `- ${finding.code}: ${finding.message}`);\n throw new Error(`The source gate found ${lines.length} errors:\\n${lines.join(\"\\n\")}`);\n}\n\n/** This function gives the layer of a printed unit. Only a quote of a share-alike source goes to the share-alike layer. */\nexport function layerOf(unit: CitedUnit, source: Source): Layer {\n return unit.use === \"quote\" ? LICENSE_RULES[source.license].layer : \"main\";\n}\n"],"mappings":";;AAIA,MAAa,WAAW;CACtB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;;AAMA,MAAa,OAAO;CAAC;CAAS;CAAW;CAAQ;AAAQ;;;;;;;;;;AA2CzD,MAAa,gBAAwD;CACnE,oBAAoB;EAClB,OAAO;EACP,KAAK;EACL,OAAO;EACP,QAAQ;EACR,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-1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@mark1russell7/guidebook",
4
- "version": "0.2.0",
4
+ "version": "0.4.0",
5
5
  "description": "The core of a guidebook of cited knowledge: sources with their licenses, cited units (a quote, a summary or a fact) and the source gate. The core names no topic.",
6
6
  "keywords": [
7
7
  "guidebook",
package/src/gate.ts CHANGED
@@ -14,6 +14,9 @@ export const FINDING_CODES = [
14
14
  "bad-date",
15
15
  "unknown-license",
16
16
  "checked-in-future",
17
+ "updated-after-check",
18
+ "bad-sha256",
19
+ "missing-field",
17
20
  "stale",
18
21
  "missing-source",
19
22
  "use-refused",
@@ -36,12 +39,25 @@ export interface Finding<C extends string = FindingCode, R extends string = "sou
36
39
  readonly message: string;
37
40
  }
38
41
 
42
+ /** The optional fields of a source, in the order of the findings of the option `require`. */
43
+ export const SOURCE_FIELDS: readonly ["by", "note", "terms", "updated", "sha256"] = ["by", "note", "terms", "updated", "sha256"];
44
+
45
+ /** An optional field of a source that the option `require` of the gate can make necessary. */
46
+ export type SourceField = (typeof SOURCE_FIELDS)[number];
47
+
39
48
  /** The options of the gate. */
40
49
  export interface GateOptions extends UseContext {
41
50
  /** The date of the gate. Without it, the gate does not examine the age of the checks. */
42
51
  readonly at?: IsoDate;
43
52
  /** A source whose check is older than this number of days gets the warning `stale`. The default is 365. */
44
53
  readonly maxAgeDays?: number;
54
+ /**
55
+ * The optional fields that each source must have. A field with no text gets the error `missing-field`.
56
+ * A dataset uses it for its own rules, for example `["by", "terms"]` for the credit and the evidence of a license.
57
+ */
58
+ readonly require?: readonly SourceField[];
59
+ /** When this option is true, the address and the terms address of each source must start with `https:`. The default is false. */
60
+ readonly https?: boolean;
45
61
  }
46
62
 
47
63
  /** The result of a gate. */
@@ -86,7 +102,25 @@ export function isWebAddress(url: string): boolean {
86
102
  return WEB_ADDRESS.test(url);
87
103
  }
88
104
 
89
- /** This function gives the findings of the sources: duplicate ids, empty titles, bad addresses, bad dates, unknown licenses and old checks. */
105
+ /** The form of a SHA-256 hash: 64 lowercase hexadecimal digits. */
106
+ const SHA256 = /^[0-9a-f]{64}$/u;
107
+
108
+ /** This function tells if a web address starts with `https:`, in any case of the letters. */
109
+ function isSecure(url: string): boolean {
110
+ return url.slice(0, 8).toLowerCase() === "https://";
111
+ }
112
+
113
+ /** This function tells if a text is a SHA-256 hash in the form that the gate accepts: 64 lowercase hexadecimal digits. */
114
+ export function isSha256(text: string): boolean {
115
+ return SHA256.test(text);
116
+ }
117
+
118
+ /**
119
+ * This function gives the findings of the sources: duplicate ids, empty titles, bad addresses, bad dates, unknown licenses and old checks.
120
+ * It also examines the optional fields: the address `terms`, the date `updated` and the form of the hash `sha256`.
121
+ * The options `require` and `https` add the rules of a dataset: the fields that each source must have, and addresses with `https:` only.
122
+ * It throws a `RangeError` for a bad date of the gate or an unknown field in `require`.
123
+ */
90
124
  export function checkSources(sources: readonly Source[], options: GateOptions = {}): readonly Finding[] {
91
125
  const out: Finding[] = [];
92
126
  const error = (code: FindingCode, id: string, message: string): void => {
@@ -95,14 +129,30 @@ export function checkSources(sources: readonly Source[], options: GateOptions =
95
129
  const at = options.at === undefined ? undefined : timeOf(options.at);
96
130
  if (options.at !== undefined && at === undefined) throw new RangeError(`checkSources: the date "${options.at}" of the gate is not a date YYYY-MM-DD.`);
97
131
  const maxAge = options.maxAgeDays ?? 365;
132
+ const unknown = (options.require ?? []).filter((field) => !(SOURCE_FIELDS as readonly string[]).includes(field));
133
+ if (unknown.length > 0) throw new RangeError(`checkSources: the option require has the unknown fields ${unknown.join(", ")}.`);
134
+ const necessary = SOURCE_FIELDS.filter((field) => options.require?.includes(field) === true);
98
135
  const seen = new Set<SourceId>();
99
136
  for (const source of sources) {
100
137
  if (seen.has(source.id)) error("duplicate-id", source.id, `Two sources have the id "${source.id}".`);
101
138
  seen.add(source.id);
102
139
  if (source.title.trim() === "") error("empty-title", source.id, `The source "${source.id}" has no title.`);
103
140
  if (!isWebAddress(source.url)) error("bad-url", source.id, `The source "${source.id}" has the address "${source.url}". It needs an address with https: or http:.`);
141
+ else if (options.https === true && !isSecure(source.url)) error("bad-url", source.id, `The source "${source.id}" has the address "${source.url}". The gate accepts only an address with https:.`);
104
142
  if (!isLicense(source.license)) error("unknown-license", source.id, `The source "${source.id}" has the unknown license "${String(source.license)}".`);
143
+ for (const field of necessary) {
144
+ const value: unknown = source[field];
145
+ if (typeof value !== "string" || value.trim() === "") error("missing-field", source.id, `The source "${source.id}" has no field "${field}". The gate makes it necessary.`);
146
+ }
147
+ if (source.terms !== undefined && !isWebAddress(source.terms)) error("bad-url", source.id, `The source "${source.id}" has the terms address "${source.terms}". It needs an address with https: or http:.`);
148
+ else if (source.terms !== undefined && options.https === true && !isSecure(source.terms)) {
149
+ error("bad-url", source.id, `The source "${source.id}" has the terms address "${source.terms}". The gate accepts only an address with https:.`);
150
+ }
151
+ if (source.sha256 !== undefined && !isSha256(source.sha256)) error("bad-sha256", source.id, `The hash of the source "${source.id}" is not 64 lowercase hexadecimal digits.`);
105
152
  const checked = timeOf(source.checked);
153
+ const updated = source.updated === undefined ? undefined : timeOf(source.updated);
154
+ if (source.updated !== undefined && updated === undefined) error("bad-date", source.id, `The source "${source.id}" has the change date "${source.updated}". It needs a date YYYY-MM-DD.`);
155
+ if (updated !== undefined && checked !== undefined && updated > checked) error("updated-after-check", source.id, `The source "${source.id}" has a change date after its check date.`);
106
156
  if (checked === undefined) {
107
157
  error("bad-date", source.id, `The source "${source.id}" has the check date "${source.checked}". It needs a date YYYY-MM-DD.`);
108
158
  } else if (at !== undefined) {
package/src/index.ts CHANGED
@@ -5,5 +5,5 @@ export { LICENSES, LICENSE_RULES, USES, permits } from "./license.ts";
5
5
  export type { Layer, License, LicenseRule, Use, UseContext } from "./license.ts";
6
6
  export { creditOf } from "./source.ts";
7
7
  export type { CitedUnit, IsoDate, Source, SourceId, UnitUse } from "./source.ts";
8
- export { DAY_MS, FINDING_CODES, assertGate, checkSources, checkUnits, gate, isWebAddress, layerOf, resultOf, throwOnErrors, timeOf } from "./gate.ts";
9
- export type { Finding, FindingCode, GateOptions, GateResult } from "./gate.ts";
8
+ export { DAY_MS, FINDING_CODES, SOURCE_FIELDS, assertGate, checkSources, checkUnits, gate, isSha256, isWebAddress, layerOf, resultOf, throwOnErrors, timeOf } from "./gate.ts";
9
+ export type { Finding, FindingCode, GateOptions, GateResult, SourceField } from "./gate.ts";
package/src/source.ts CHANGED
@@ -22,6 +22,12 @@ export interface Source {
22
22
  readonly by?: string;
23
23
  /** A term of the publisher in addition to the license, with its date, for example "No AI ingestion (publisher, 2026-11-23)". */
24
24
  readonly note?: string;
25
+ /** The page of the publisher that tells the terms of the reuse, with `https:` or `http:`. */
26
+ readonly terms?: string | undefined;
27
+ /** The date that the source shows as its last change. It is not after the date `checked`. */
28
+ readonly updated?: IsoDate | undefined;
29
+ /** The SHA-256 hash of the file of the source at the date `checked`, as 64 lowercase hexadecimal digits. */
30
+ readonly sha256?: string | undefined;
25
31
  }
26
32
 
27
33
  /** The use of a cited unit. A bundle is a copy of a full source, so a unit cannot have the use `bundle`. */