@mark1russell7/layout-contract 0.2.21 → 0.3.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 +8 -1
- package/build/index.d.ts +21 -2
- package/build/index.d.ts.map +1 -1
- package/build/index.js +146 -14
- package/build/index.js.map +1 -1
- package/package.json +1 -1
- package/src/hash.ts +7 -6
- package/src/record.ts +82 -3
- package/src/utf8.ts +48 -9
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@mark1russell7/layout-contract",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.3.0",
|
|
5
5
|
"description": "The shared contract of the packing and highways layout engines: the input and record formats, the turn rule, margins, tolerances and hard rules.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"layout",
|
package/src/hash.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { canonicalInput, type LayoutInput } from "./input.ts";
|
|
2
2
|
import { RECORD_EPS } from "./tolerance.ts";
|
|
3
|
-
import {
|
|
3
|
+
import { utf8Write } from "./utf8.ts";
|
|
4
4
|
|
|
5
5
|
/** The round constants of SHA-256. */
|
|
6
6
|
const K: readonly number[] = [
|
|
@@ -16,12 +16,13 @@ const K: readonly number[] = [
|
|
|
16
16
|
* It uses no API of Node or of the browser.
|
|
17
17
|
*/
|
|
18
18
|
export function sha256Hex(text: string): string {
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
const
|
|
19
|
+
// The bytes go straight into the padded buffer, so the digest makes one array, not a list of the bytes and a copy.
|
|
20
|
+
const length = utf8Write(text);
|
|
21
|
+
const bits = length * 8;
|
|
22
|
+
const total = Math.ceil((length + 9) / 64) * 64;
|
|
22
23
|
const data = new Uint8Array(total);
|
|
23
|
-
data
|
|
24
|
-
data[
|
|
24
|
+
utf8Write(text, data);
|
|
25
|
+
data[length] = 0x80;
|
|
25
26
|
const view = new DataView(data.buffer);
|
|
26
27
|
view.setUint32(total - 8, Math.floor(bits / 0x100000000));
|
|
27
28
|
view.setUint32(total - 4, bits >>> 0);
|
package/src/record.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Tolerance } from "./tolerance.ts";
|
|
2
2
|
import type { LayoutInput } from "./input.ts";
|
|
3
|
-
import { shapeKindsOf, type ShapeKind } from "./shapes.ts";
|
|
3
|
+
import { isShapeKind, shapeKindsOf, type ShapeKind } from "./shapes.ts";
|
|
4
4
|
|
|
5
5
|
/** The stage of a record: the packer, the router, or both together. */
|
|
6
6
|
export type Stage = "pack" | "route" | "joint";
|
|
@@ -30,8 +30,11 @@ export interface LayoutRecord {
|
|
|
30
30
|
readonly hard: Readonly<Record<string, number>>;
|
|
31
31
|
/** The tolerance of each hard rule of the run. */
|
|
32
32
|
readonly tolerance?: Readonly<Partial<Tolerance>>;
|
|
33
|
-
/**
|
|
34
|
-
|
|
33
|
+
/**
|
|
34
|
+
* The value is `true` when two runs with the same seed give the same positions.
|
|
35
|
+
* A skipped record has `null`, because no run made a claim. Each other record has a boolean.
|
|
36
|
+
*/
|
|
37
|
+
readonly deterministic: boolean | null;
|
|
35
38
|
/** The id of the prediction that was written before the run. */
|
|
36
39
|
readonly prediction?: string;
|
|
37
40
|
/** The median of `msRuns`, or the time of the one run. */
|
|
@@ -67,6 +70,10 @@ export interface RunEnv {
|
|
|
67
70
|
readonly cpu: string;
|
|
68
71
|
/** The count of the logical processors. */
|
|
69
72
|
readonly cpus: number;
|
|
73
|
+
/** The mode of the timing, for example `process` or `fresh` of `paired-time/1`. Each timing record names its mode. */
|
|
74
|
+
readonly mode?: string;
|
|
75
|
+
/** The context of the run, for example `ci` or `local`, with the Node flags. */
|
|
76
|
+
readonly context?: string;
|
|
70
77
|
}
|
|
71
78
|
|
|
72
79
|
/** The facts of a run that the input does not give. */
|
|
@@ -90,3 +97,75 @@ export function toLayoutRecord(input: LayoutInput, run: RunFacts): LayoutRecord
|
|
|
90
97
|
...(onlyRect ? {} : { shapes }),
|
|
91
98
|
};
|
|
92
99
|
}
|
|
100
|
+
|
|
101
|
+
type Fields = Readonly<Record<string, unknown>>;
|
|
102
|
+
|
|
103
|
+
const isFields = (v: unknown): v is Fields => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
104
|
+
|
|
105
|
+
const STAGES: readonly string[] = ["pack", "route", "joint"];
|
|
106
|
+
const RECORD_STRINGS = ["step", "strategy", "corpus", "input", "inputHash", "set"] as const;
|
|
107
|
+
const RECORD_NUMBERS = ["seed", "nodes", "edges", "ms"] as const;
|
|
108
|
+
const RECORD_OPTIONAL_STRINGS = ["prediction", "outputHash", "skipped", "commit"] as const;
|
|
109
|
+
const ENV_STRINGS = ["node", "os", "arch", "runner", "cpu"] as const;
|
|
110
|
+
const ENV_OPTIONAL_STRINGS = ["mode", "context"] as const;
|
|
111
|
+
|
|
112
|
+
/** This function adds a problem for each value of a map that is not a number. */
|
|
113
|
+
function numberMap(out: string[], v: unknown, name: string): void {
|
|
114
|
+
if (!isFields(v)) {
|
|
115
|
+
out.push(`${name} is not an object`);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
for (const [key, n] of Object.entries(v)) if (typeof n !== "number") out.push(`${name}.${key} is not a number`);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** This function gives the problems of the field `env` of a record. */
|
|
122
|
+
function envProblems(env: unknown): string[] {
|
|
123
|
+
if (!isFields(env)) return ["env is not an object"];
|
|
124
|
+
const out: string[] = [];
|
|
125
|
+
for (const key of ENV_STRINGS) if (typeof env[key] !== "string") out.push(`env.${key} is not a string`);
|
|
126
|
+
if (typeof env["cpus"] !== "number") out.push("env.cpus is not a number");
|
|
127
|
+
for (const key of ENV_OPTIONAL_STRINGS) if (env[key] !== undefined && typeof env[key] !== "string") out.push(`env.${key} is not a string`);
|
|
128
|
+
return out;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* This function gives the problems of one `layout-record/1` value. An empty list means a good record.
|
|
133
|
+
* A record that has `skipped` has `deterministic: null`. An old skipped record with a boolean is also correct.
|
|
134
|
+
* A record without `skipped` has a boolean. The check accepts fields that the format does not know.
|
|
135
|
+
*/
|
|
136
|
+
export function checkLayoutRecord(value: unknown): string[] {
|
|
137
|
+
if (!isFields(value)) return ["the record is not an object"];
|
|
138
|
+
const out: string[] = [];
|
|
139
|
+
const v = value;
|
|
140
|
+
if (v["schema"] !== "layout-record/1") out.push("schema is not layout-record/1");
|
|
141
|
+
if (typeof v["stage"] !== "string" || !STAGES.includes(v["stage"])) out.push("stage is not pack, route or joint");
|
|
142
|
+
for (const key of RECORD_STRINGS) if (typeof v[key] !== "string") out.push(`${key} is not a string`);
|
|
143
|
+
for (const key of RECORD_NUMBERS) if (typeof v[key] !== "number") out.push(`${key} is not a number`);
|
|
144
|
+
for (const key of RECORD_OPTIONAL_STRINGS) if (v[key] !== undefined && typeof v[key] !== "string") out.push(`${key} is not a string`);
|
|
145
|
+
if (!isFields(v["options"])) out.push("options is not an object");
|
|
146
|
+
numberMap(out, v["measures"], "measures");
|
|
147
|
+
numberMap(out, v["hard"], "hard");
|
|
148
|
+
if (v["tolerance"] !== undefined) numberMap(out, v["tolerance"], "tolerance");
|
|
149
|
+
const shapes = v["shapes"];
|
|
150
|
+
if (shapes !== undefined && (!Array.isArray(shapes) || !shapes.every(isShapeKind))) out.push("shapes is not a list of shape kinds");
|
|
151
|
+
const msRuns = v["msRuns"];
|
|
152
|
+
if (msRuns !== undefined && (!Array.isArray(msRuns) || !msRuns.every((n) => typeof n === "number"))) out.push("msRuns is not a list of numbers");
|
|
153
|
+
if (v["env"] !== undefined) out.push(...envProblems(v["env"]));
|
|
154
|
+
const deterministic = v["deterministic"];
|
|
155
|
+
if (v["skipped"] === undefined) {
|
|
156
|
+
if (typeof deterministic !== "boolean") out.push("deterministic is not a boolean, and the record has no skipped");
|
|
157
|
+
} else if (deterministic !== null && typeof deterministic !== "boolean") out.push("deterministic of a skipped record is not null or a boolean");
|
|
158
|
+
return out;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* This function reads one `layout-record/1` value, for example one parsed line of a JSON Lines file.
|
|
163
|
+
* It throws a `TypeError` with the problems of `checkLayoutRecord`.
|
|
164
|
+
* An old skipped record has a boolean `deterministic`. The result has `null` in its place, because no run made that claim.
|
|
165
|
+
*/
|
|
166
|
+
export function parseLayoutRecord(value: unknown): LayoutRecord {
|
|
167
|
+
const problems = checkLayoutRecord(value);
|
|
168
|
+
if (problems.length > 0) throw new TypeError(`The record is not a layout-record/1: ${problems.join(", ")}.`);
|
|
169
|
+
const record = value as LayoutRecord;
|
|
170
|
+
return record.skipped !== undefined && record.deterministic !== null ? { ...record, deterministic: null } : record;
|
|
171
|
+
}
|
package/src/utf8.ts
CHANGED
|
@@ -1,12 +1,51 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
/**
|
|
2
|
+
* This function writes the UTF-8 bytes of a string into `out`, from the index `at`, and gives the index after the last byte.
|
|
3
|
+
* With no `out`, it writes nothing and gives the count of the bytes. A lone surrogate gives the 3 bytes of its code.
|
|
4
|
+
* It reads the UTF-16 code units, so it makes no string for each character (L81: `sha256Hex` allocated 2 GB in one pair).
|
|
5
|
+
*/
|
|
6
|
+
export function utf8Write(text: string, out?: Uint8Array, at = 0): number {
|
|
7
|
+
let n = at;
|
|
8
|
+
for (let i = 0; i < text.length; i++) {
|
|
9
|
+
let c = text.charCodeAt(i);
|
|
10
|
+
if (c >= 0xd800 && c < 0xdc00 && i + 1 < text.length) {
|
|
11
|
+
const low = text.charCodeAt(i + 1);
|
|
12
|
+
if (low >= 0xdc00 && low < 0xe000) {
|
|
13
|
+
c = 0x10000 + ((c - 0xd800) << 10) + (low - 0xdc00);
|
|
14
|
+
i++;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
if (c < 0x80) {
|
|
18
|
+
if (out !== undefined) out[n] = c;
|
|
19
|
+
n += 1;
|
|
20
|
+
} else if (c < 0x800) {
|
|
21
|
+
if (out !== undefined) {
|
|
22
|
+
out[n] = 0xc0 | (c >> 6);
|
|
23
|
+
out[n + 1] = 0x80 | (c & 63);
|
|
24
|
+
}
|
|
25
|
+
n += 2;
|
|
26
|
+
} else if (c < 0x10000) {
|
|
27
|
+
if (out !== undefined) {
|
|
28
|
+
out[n] = 0xe0 | (c >> 12);
|
|
29
|
+
out[n + 1] = 0x80 | ((c >> 6) & 63);
|
|
30
|
+
out[n + 2] = 0x80 | (c & 63);
|
|
31
|
+
}
|
|
32
|
+
n += 3;
|
|
33
|
+
} else {
|
|
34
|
+
if (out !== undefined) {
|
|
35
|
+
out[n] = 0xf0 | (c >> 18);
|
|
36
|
+
out[n + 1] = 0x80 | ((c >> 12) & 63);
|
|
37
|
+
out[n + 2] = 0x80 | ((c >> 6) & 63);
|
|
38
|
+
out[n + 3] = 0x80 | (c & 63);
|
|
39
|
+
}
|
|
40
|
+
n += 4;
|
|
41
|
+
}
|
|
10
42
|
}
|
|
43
|
+
return n;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** This function gives the UTF-8 bytes of a string. The package has no runtime dependencies, thus it does not use `TextEncoder`. */
|
|
47
|
+
export function utf8(text: string): Uint8Array {
|
|
48
|
+
const bytes = new Uint8Array(utf8Write(text));
|
|
49
|
+
utf8Write(text, bytes);
|
|
11
50
|
return bytes;
|
|
12
51
|
}
|