@dogsbay/types 0.2.0-beta.156 → 0.2.0-beta.158

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -11,3 +11,5 @@ export { WELL_KNOWN_FIELDS, INERT_FIELDS, AGGREGATOR_OWNED, EMPTY_POLICY, typeOf
11
11
  export type { FieldType, FieldSpec, PropSpec, TaxonomySpec, FrontmatterPolicy } from "./frontmatter-schema.js";
12
12
  export { checkFrontmatter, frontmatterPolicyFromConfig } from "./frontmatter-check.js";
13
13
  export type { FrontmatterFinding } from "./frontmatter-check.js";
14
+ export type { ImportLimits, LimitName, LimitHost } from "./limits.js";
15
+ export { DEFAULT_LIMITS, LIMIT_DESCRIPTIONS, LIMIT_REACHED, resolveLimits, parseLimit, formatLimit, limitMessage, utf8Bytes } from "./limits.js";
package/dist/index.js CHANGED
@@ -3,3 +3,4 @@ export { treeToJson, chunkByHeadings, extractText } from "./json.js";
3
3
  export { STATUS_VALUES, parseMeta } from "./meta.js";
4
4
  export { WELL_KNOWN_FIELDS, INERT_FIELDS, AGGREGATOR_OWNED, EMPTY_POLICY, typeOf, matchesType, nearMisses, isSafeAutoFix, frontmatterKeyLines, globMatches, contentRelativePath, requiredFieldsFor, } from "./frontmatter-schema.js";
5
5
  export { checkFrontmatter, frontmatterPolicyFromConfig } from "./frontmatter-check.js";
6
+ export { DEFAULT_LIMITS, LIMIT_DESCRIPTIONS, LIMIT_REACHED, resolveLimits, parseLimit, formatLimit, limitMessage, utf8Bytes } from "./limits.js";
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Import limits: the RESOURCE limits an import puts on someone else's repo, in one place
3
+ * (plans/import-limits.md). They exist so a hostile repo cannot make an import read gigabytes or
4
+ * build a table of a million rows; for the operator's own repo they are only a way for content
5
+ * to turn into a note, so `trusted` sets none.
6
+ *
7
+ * Who may set them: the OPERATOR (CLI flags, a host's `migrate()` options), never the repo being
8
+ * imported. Safety rules (containment, no repo code run, escaping) are NOT limits and are not
9
+ * here: they hold whether the repo is trusted or not.
10
+ *
11
+ * Each default is measured against the largest real value it guards, with the headroom noted.
12
+ * A limit that trips is REPORTED (`LIMIT_REACHED`), never silent.
13
+ */
14
+ export interface ImportLimits {
15
+ /** One file read as data (bytes). */
16
+ fileBytes: number;
17
+ /** Files in one data folder. */
18
+ folderFiles: number;
19
+ /** All data read in one import (bytes). */
20
+ importBytes: number;
21
+ /** Data one catalogue (a table built from data files) may read (bytes). */
22
+ catalogueBytes: number;
23
+ /** Rows in one catalogue. */
24
+ catalogueRows: number;
25
+ /** Characters of text in one table cell. */
26
+ cellChars: number;
27
+ }
28
+ export type LimitName = keyof ImportLimits;
29
+ /**
30
+ * The defaults, for a repo that is not trusted. Measured on cloudflare-docs (6e85ac29,
31
+ * 2026-11-03), the largest real use of each today:
32
+ */
33
+ export declare const DEFAULT_LIMITS: Readonly<ImportLimits>;
34
+ /** What each limit guards, for the report. */
35
+ export declare const LIMIT_DESCRIPTIONS: Readonly<Record<LimitName, string>>;
36
+ /** The ledger code a tripped limit is reported under. */
37
+ export declare const LIMIT_REACHED = "limit-reached";
38
+ /**
39
+ * The limits an import runs with: the defaults, or none when `trusted`, then each given value.
40
+ * A given value must be a positive number (or Infinity); anything else throws, naming it — a typo
41
+ * must not run with a limit the operator did not mean.
42
+ */
43
+ export declare function resolveLimits(given?: Partial<Record<string, unknown>>, opts?: {
44
+ trusted?: boolean;
45
+ }): ImportLimits;
46
+ /**
47
+ * One `--limit name=value` flag: `catalogueBytes=500MB`, `catalogueRows=50000`, `cellChars=none`.
48
+ * Sizes take `KB`/`MB`/`GB` (powers of 1,000) or `KiB`/`MiB`/`GiB`; `none` is no limit.
49
+ */
50
+ export declare function parseLimit(flag: string): [LimitName, number];
51
+ /** A limit's value as a person reads it: `256 MB`, `20,000`, `none`. */
52
+ export declare function formatLimit(name: LimitName, value: number): string;
53
+ /**
54
+ * How an operator raises a limit, by where the import runs: the CLI's flags, `site build`'s config,
55
+ * a host's options (the playground). The report names the one that works where it is read.
56
+ */
57
+ export type LimitHost = "cli" | "config" | "host";
58
+ /**
59
+ * The report for a tripped limit: what was not done, the measured value against the limit, and
60
+ * how to raise it where the import runs.
61
+ */
62
+ export declare function limitMessage(what: string, name: LimitName, measured: number | undefined, limit: number, host?: LimitHost): string;
63
+ /** UTF-8 bytes of a string, without encoding it: limits on bytes count bytes, not UTF-16 units. */
64
+ export declare function utf8Bytes(s: string): number;
package/dist/limits.js ADDED
@@ -0,0 +1,147 @@
1
+ /**
2
+ * Import limits: the RESOURCE limits an import puts on someone else's repo, in one place
3
+ * (plans/import-limits.md). They exist so a hostile repo cannot make an import read gigabytes or
4
+ * build a table of a million rows; for the operator's own repo they are only a way for content
5
+ * to turn into a note, so `trusted` sets none.
6
+ *
7
+ * Who may set them: the OPERATOR (CLI flags, a host's `migrate()` options), never the repo being
8
+ * imported. Safety rules (containment, no repo code run, escaping) are NOT limits and are not
9
+ * here: they hold whether the repo is trusted or not.
10
+ *
11
+ * Each default is measured against the largest real value it guards, with the headroom noted.
12
+ * A limit that trips is REPORTED (`LIMIT_REACHED`), never silent.
13
+ */
14
+ /**
15
+ * The defaults, for a repo that is not trusted. Measured on cloudflare-docs (6e85ac29,
16
+ * 2026-11-03), the largest real use of each today:
17
+ */
18
+ export const DEFAULT_LIMITS = Object.freeze({
19
+ // largest model JSON 496 KB → 32×
20
+ fileBytes: 16_000_000,
21
+ // catalog-models holds 199 files → 100×
22
+ folderFiles: 20_000,
23
+ // all the data files in Cloudflare's src/content (JSON/YAML outside the pages): 15.6 MB → 64×;
24
+ // the whole src/content, pages included, is 54.7 MB
25
+ importBytes: 1_000_000_000,
26
+ // the model catalogue reads 14.1 MB of JSON → 18×
27
+ catalogueBytes: 256_000_000,
28
+ // 272 models, 193 fields → 70×
29
+ catalogueRows: 20_000,
30
+ // longest model description 611 characters → 16×
31
+ cellChars: 10_000,
32
+ });
33
+ /** What each limit guards, for the report. */
34
+ export const LIMIT_DESCRIPTIONS = Object.freeze({
35
+ fileBytes: "bytes in one data file",
36
+ folderFiles: "files in one data folder",
37
+ importBytes: "data read in one import",
38
+ catalogueBytes: "data one catalogue reads",
39
+ catalogueRows: "rows in one catalogue",
40
+ cellChars: "characters in one table cell",
41
+ });
42
+ /** The ledger code a tripped limit is reported under. */
43
+ export const LIMIT_REACHED = "limit-reached";
44
+ /** Every limit unset: what `trusted` gives. */
45
+ const NONE = Object.freeze({
46
+ fileBytes: Infinity,
47
+ folderFiles: Infinity,
48
+ importBytes: Infinity,
49
+ catalogueBytes: Infinity,
50
+ catalogueRows: Infinity,
51
+ cellChars: Infinity,
52
+ });
53
+ const isLimitName = (k) => Object.hasOwn(DEFAULT_LIMITS, k);
54
+ /**
55
+ * The limits an import runs with: the defaults, or none when `trusted`, then each given value.
56
+ * A given value must be a positive number (or Infinity); anything else throws, naming it — a typo
57
+ * must not run with a limit the operator did not mean.
58
+ */
59
+ export function resolveLimits(given, opts = {}) {
60
+ const out = { ...(opts.trusted ? NONE : DEFAULT_LIMITS) };
61
+ for (const [k, v] of Object.entries(given ?? {})) {
62
+ if (v === undefined)
63
+ continue;
64
+ if (!isLimitName(k))
65
+ throw new Error(`No such limit: ${k} (there is ${Object.keys(DEFAULT_LIMITS).join(", ")})`);
66
+ // a whole number ≥ 1, or Infinity (none): `cellChars=0.5` cut a cell to one character and an ellipsis
67
+ if (typeof v !== "number" || !(v === Infinity || (Number.isInteger(v) && v >= 1)))
68
+ throw new Error(`Limit ${k} must be a whole number of at least 1 (or none), not ${JSON.stringify(v)}`);
69
+ out[k] = v;
70
+ }
71
+ return out;
72
+ }
73
+ /**
74
+ * One `--limit name=value` flag: `catalogueBytes=500MB`, `catalogueRows=50000`, `cellChars=none`.
75
+ * Sizes take `KB`/`MB`/`GB` (powers of 1,000) or `KiB`/`MiB`/`GiB`; `none` is no limit.
76
+ */
77
+ export function parseLimit(flag) {
78
+ const m = /^([A-Za-z]+)=(.+)$/.exec(flag.trim());
79
+ if (!m)
80
+ throw new Error(`--limit takes name=value, not ${JSON.stringify(flag)}`);
81
+ const [, name, raw] = m;
82
+ if (!isLimitName(name))
83
+ throw new Error(`No such limit: ${name} (there is ${Object.keys(DEFAULT_LIMITS).join(", ")})`);
84
+ if (/^(none|unlimited|infinity)$/i.test(raw))
85
+ return [name, Infinity];
86
+ const v = /^(\d+(?:\.\d+)?)\s*([KMG]i?B)?$/i.exec(raw.replace(/_/g, ""));
87
+ if (!v)
88
+ throw new Error(`--limit ${name}: ${JSON.stringify(raw)} is not a number (or 500MB, or none)`);
89
+ const unit = (v[2] ?? "").toUpperCase();
90
+ if (unit && !name.endsWith("Bytes"))
91
+ throw new Error(`--limit ${name} is a count, not a size: ${JSON.stringify(raw)}`);
92
+ const scale = { "": 1, KB: 1e3, MB: 1e6, GB: 1e9, KIB: 1024, MIB: 1024 ** 2, GIB: 1024 ** 3 };
93
+ // a size rounds to whole bytes; a count must be whole
94
+ const n = unit ? Math.round(Number(v[1]) * scale[unit]) : Number(v[1]);
95
+ // only the word `none` means no limit: 400 digits is a typo, not Infinity
96
+ if (!Number.isFinite(n) || n > Number.MAX_SAFE_INTEGER)
97
+ throw new Error(`--limit ${name}: ${JSON.stringify(raw)} is too large (use none for no limit)`);
98
+ if (!Number.isInteger(n) || n < 1)
99
+ throw new Error(`--limit ${name} must be a whole number of at least 1, not ${JSON.stringify(raw)}`);
100
+ return [name, n];
101
+ }
102
+ /** A limit's value as a person reads it: `256 MB`, `20,000`, `none`. */
103
+ export function formatLimit(name, value) {
104
+ if (!Number.isFinite(value))
105
+ return "none";
106
+ if (name.endsWith("Bytes")) {
107
+ if (value >= 1e9)
108
+ return `${+(value / 1e9).toFixed(1)} GB`;
109
+ if (value >= 1e6)
110
+ return `${+(value / 1e6).toFixed(1)} MB`;
111
+ if (value >= 1e3)
112
+ return `${+(value / 1e3).toFixed(1)} KB`;
113
+ return `${value} bytes`;
114
+ }
115
+ return value.toLocaleString("en-US");
116
+ }
117
+ const HOW = {
118
+ cli: (name) => `--limit ${name}=…, or --trusted for a repo you trust`,
119
+ config: (name) => `\`limits: { ${name}: … }\` in dogsbay.config.yml, or \`limits: { trusted: true }\``,
120
+ host: (name) => `the import's \`limits.${name}\` option, or \`trusted\` for a repo you trust`,
121
+ };
122
+ /**
123
+ * The report for a tripped limit: what was not done, the measured value against the limit, and
124
+ * how to raise it where the import runs.
125
+ */
126
+ export function limitMessage(what, name, measured, limit, host = "cli") {
127
+ const over = measured === undefined ? `over the ${formatLimit(name, limit)} limit` : `${formatLimit(name, measured)}, over the ${formatLimit(name, limit)} limit`;
128
+ return `${what}: ${LIMIT_DESCRIPTIONS[name]} ${over} (${HOW[host](name)})`;
129
+ }
130
+ /** UTF-8 bytes of a string, without encoding it: limits on bytes count bytes, not UTF-16 units. */
131
+ export function utf8Bytes(s) {
132
+ let n = 0;
133
+ for (let i = 0; i < s.length; i++) {
134
+ const c = s.charCodeAt(i);
135
+ if (c < 0x80)
136
+ n += 1;
137
+ else if (c < 0x800)
138
+ n += 2;
139
+ else if (c >= 0xd800 && c <= 0xdbff && i + 1 < s.length && (s.charCodeAt(i + 1) & 0xfc00) === 0xdc00) {
140
+ n += 4;
141
+ i++;
142
+ }
143
+ else
144
+ n += 3;
145
+ }
146
+ return n;
147
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dogsbay/types",
3
- "version": "0.2.0-beta.156",
3
+ "version": "0.2.0-beta.158",
4
4
  "description": "Universal document tree model for Dogsbay format loaders and renderers",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",