priority-axi 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/LICENSE +21 -0
- package/NOTICE +13 -0
- package/README.md +177 -2
- package/dist/bin/priority-axi.js +17 -0
- package/dist/cli/parse.js +69 -0
- package/dist/cli/run.js +323 -0
- package/dist/cli/table.js +37 -0
- package/dist/commands/catalog.js +180 -0
- package/dist/commands/common.js +173 -0
- package/dist/commands/create.js +207 -0
- package/dist/commands/find.js +153 -0
- package/dist/commands/home.js +56 -0
- package/dist/commands/read.js +264 -0
- package/dist/commands/refresh.js +26 -0
- package/dist/config.js +76 -0
- package/dist/errors.js +149 -0
- package/dist/http.js +118 -0
- package/dist/index/build.js +169 -0
- package/dist/index/cache.js +129 -0
- package/dist/index/catalog.js +216 -0
- package/dist/odata.js +108 -0
- package/dist/output/html.js +58 -0
- package/dist/output/shape.js +57 -0
- package/dist/output/text.js +48 -0
- package/dist/output/toon.js +78 -0
- package/dist/search.js +118 -0
- package/dist/version.js +2 -0
- package/package.json +55 -4
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// A local copy of one screen's key, description, unit, family and status
|
|
2
|
+
// columns, for `find`. Priority cannot search text (contains and startswith
|
|
3
|
+
// answer 501), so the catalog is pulled once, page by page, and searched
|
|
4
|
+
// locally. It is rebuilt after a day, or with --refresh. Like the index, it is
|
|
5
|
+
// derived data under the connection's cache directory.
|
|
6
|
+
import { join } from 'node:path';
|
|
7
|
+
import { usage } from '../errors.js';
|
|
8
|
+
import { queryString } from '../odata.js';
|
|
9
|
+
import { readJson, writeJsonAtomic } from './cache.js';
|
|
10
|
+
export const CATALOG_FORMAT = 1;
|
|
11
|
+
export const MAX_CATALOG_AGE_MS = 24 * 60 * 60 * 1000;
|
|
12
|
+
export const CATALOG_PAGE = 1000;
|
|
13
|
+
/** Pages read at once after the first, as the live sweep does. */
|
|
14
|
+
export const CATALOG_PARALLEL = 4;
|
|
15
|
+
/** Top-level records read at most; beyond this the catalog is marked partial. */
|
|
16
|
+
export const MAX_CATALOG_RECORDS = 200_000;
|
|
17
|
+
const unique = (names) => [
|
|
18
|
+
...new Set(names.filter((n) => !!n)),
|
|
19
|
+
];
|
|
20
|
+
const has = (type, name) => type.fields.some((f) => f.name === name);
|
|
21
|
+
/**
|
|
22
|
+
* A screen level's searchable columns: its key; its main description (the
|
|
23
|
+
* first editable text field named ...DES, such as PARTDES, CUSTDES or PROJDES,
|
|
24
|
+
* so a customer name copied onto a project does not win); the foreign
|
|
25
|
+
* description beside it; the sales unit; the family; the status; and the
|
|
26
|
+
* customer it belongs to (CUSTNAME and CUSTDES on a project).
|
|
27
|
+
*/
|
|
28
|
+
function level(type, label, hint) {
|
|
29
|
+
const key = type.key.filter((k) => has(type, k));
|
|
30
|
+
const named = type.fields.filter((f) => f.type === 'String' &&
|
|
31
|
+
!f.internalId &&
|
|
32
|
+
!key.includes(f.name) &&
|
|
33
|
+
f.name !== 'STATDES' &&
|
|
34
|
+
/DES$/.test(f.name));
|
|
35
|
+
const description = (named.find((f) => !f.readOnly) ?? named[0])?.name;
|
|
36
|
+
if (!description) {
|
|
37
|
+
throw usage(`${label} has no description field for find to search. ${hint}`);
|
|
38
|
+
}
|
|
39
|
+
const foreign = has(type, `E${description}`) ? `E${description}` : undefined;
|
|
40
|
+
const unit = ['PUNITNAME', 'UNITNAME'].find((n) => has(type, n));
|
|
41
|
+
const customer = description !== 'CUSTDES' &&
|
|
42
|
+
!key.includes('CUSTNAME') &&
|
|
43
|
+
has(type, 'CUSTDES')
|
|
44
|
+
? unique([has(type, 'CUSTNAME') ? 'CUSTNAME' : undefined, 'CUSTDES'])
|
|
45
|
+
: [];
|
|
46
|
+
return {
|
|
47
|
+
key,
|
|
48
|
+
description,
|
|
49
|
+
foreign,
|
|
50
|
+
unit,
|
|
51
|
+
family: has(type, 'FAMILYDES') ? 'FAMILYDES' : undefined,
|
|
52
|
+
status: has(type, 'STATDES') ? 'STATDES' : undefined,
|
|
53
|
+
customer,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
export function layout(screen, sub) {
|
|
57
|
+
const top = level(screen.type, screen.name, `Use \`priority-axi get ${screen.name} --where F=V\` instead.`);
|
|
58
|
+
if (!sub) {
|
|
59
|
+
const columns = unique([
|
|
60
|
+
...top.key,
|
|
61
|
+
top.description,
|
|
62
|
+
top.foreign,
|
|
63
|
+
top.unit,
|
|
64
|
+
top.family,
|
|
65
|
+
top.status,
|
|
66
|
+
...top.customer,
|
|
67
|
+
]);
|
|
68
|
+
const own = unique([...top.key, top.description, top.foreign, top.family]);
|
|
69
|
+
return {
|
|
70
|
+
columns,
|
|
71
|
+
select: columns,
|
|
72
|
+
search: unique([
|
|
73
|
+
...own,
|
|
74
|
+
top.customer.includes('CUSTDES') ? 'CUSTDES' : undefined,
|
|
75
|
+
]),
|
|
76
|
+
show: unique([
|
|
77
|
+
...top.key,
|
|
78
|
+
top.description,
|
|
79
|
+
top.unit,
|
|
80
|
+
top.family,
|
|
81
|
+
...top.customer,
|
|
82
|
+
]),
|
|
83
|
+
status: unique([top.status]),
|
|
84
|
+
key: top.key,
|
|
85
|
+
parentKey: top.key,
|
|
86
|
+
own,
|
|
87
|
+
context: top.customer.length ? 'customer' : undefined,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
const low = level(sub.def, `${screen.name} > ${sub.name}`, `Use \`priority-axi show ${screen.name} <key> --with ${sub.name}\` instead.`);
|
|
91
|
+
const parent = unique([...top.key, top.description, top.status]);
|
|
92
|
+
const subFields = unique([
|
|
93
|
+
...low.key,
|
|
94
|
+
low.description,
|
|
95
|
+
low.foreign,
|
|
96
|
+
low.unit,
|
|
97
|
+
low.family,
|
|
98
|
+
low.status,
|
|
99
|
+
]);
|
|
100
|
+
// A sub-screen column named like a parent column is stored as SUB.FIELD.
|
|
101
|
+
const stored = (f) => f === undefined ? undefined : parent.includes(f) ? `${sub.name}.${f}` : f;
|
|
102
|
+
const own = unique([
|
|
103
|
+
...low.key.map(stored),
|
|
104
|
+
stored(low.description),
|
|
105
|
+
stored(low.foreign),
|
|
106
|
+
stored(low.family),
|
|
107
|
+
]);
|
|
108
|
+
return {
|
|
109
|
+
columns: [...parent, ...subFields.map((f) => stored(f))],
|
|
110
|
+
select: parent,
|
|
111
|
+
subSelect: subFields,
|
|
112
|
+
search: unique([...own, top.description]),
|
|
113
|
+
own,
|
|
114
|
+
context: 'parent',
|
|
115
|
+
show: unique([
|
|
116
|
+
...low.key.map(stored),
|
|
117
|
+
stored(low.description),
|
|
118
|
+
stored(low.unit),
|
|
119
|
+
...top.key,
|
|
120
|
+
top.description,
|
|
121
|
+
]),
|
|
122
|
+
status: unique([top.status, stored(low.status)]),
|
|
123
|
+
key: low.key.map((k) => stored(k)),
|
|
124
|
+
parentKey: top.key,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function catalogPath(dir, screen, sub) {
|
|
128
|
+
return join(dir, 'find', `${screen}${sub ? `.${sub}` : ''}.json`);
|
|
129
|
+
}
|
|
130
|
+
export function readCatalog(dir, screen, sub, columns, now) {
|
|
131
|
+
const value = readJson(catalogPath(dir, screen, sub));
|
|
132
|
+
if (!value ||
|
|
133
|
+
value.format !== CATALOG_FORMAT ||
|
|
134
|
+
value.screen !== screen ||
|
|
135
|
+
value.sub !== sub ||
|
|
136
|
+
!Array.isArray(value.rows) ||
|
|
137
|
+
!Array.isArray(value.columns) ||
|
|
138
|
+
value.columns.join(',') !== columns.join(',') ||
|
|
139
|
+
typeof value.builtAt !== 'string') {
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
const age = now.getTime() - Date.parse(value.builtAt);
|
|
143
|
+
if (!(age >= 0 && age < MAX_CATALOG_AGE_MS))
|
|
144
|
+
return undefined;
|
|
145
|
+
return value;
|
|
146
|
+
}
|
|
147
|
+
export function writeCatalog(dir, catalog) {
|
|
148
|
+
writeJsonAtomic(catalogPath(dir, catalog.screen, catalog.sub), catalog);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Read every record, ordered by the key, CATALOG_PAGE at a time: the first page
|
|
152
|
+
* alone, then CATALOG_PARALLEL pages at once until a short page.
|
|
153
|
+
*/
|
|
154
|
+
export async function pullCatalog(screen, sub, lay, read, now) {
|
|
155
|
+
const order = screen.type.key.filter((k) => has(screen.type, k)).join(',');
|
|
156
|
+
const query = (skip) => queryString({
|
|
157
|
+
$select: lay.select.join(','),
|
|
158
|
+
$orderby: order || undefined,
|
|
159
|
+
$top: CATALOG_PAGE,
|
|
160
|
+
$skip: skip || undefined,
|
|
161
|
+
$expand: sub && lay.subSelect
|
|
162
|
+
? `${sub.nav}($select=${lay.subSelect.join(',')})`
|
|
163
|
+
: undefined,
|
|
164
|
+
});
|
|
165
|
+
const rows = [];
|
|
166
|
+
let records = 0;
|
|
167
|
+
let partial = false;
|
|
168
|
+
const take = (page) => {
|
|
169
|
+
records += page.length;
|
|
170
|
+
for (const record of page) {
|
|
171
|
+
const head = lay.select.map((c) => record[c] ?? null);
|
|
172
|
+
if (!sub || !lay.subSelect) {
|
|
173
|
+
rows.push(head);
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
const value = record[sub.nav];
|
|
177
|
+
const lines = Array.isArray(value)
|
|
178
|
+
? value
|
|
179
|
+
: value && typeof value === 'object'
|
|
180
|
+
? [value]
|
|
181
|
+
: [];
|
|
182
|
+
for (const line of lines) {
|
|
183
|
+
rows.push([...head, ...lay.subSelect.map((c) => line[c] ?? null)]);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
const first = await read(query(0));
|
|
188
|
+
take(first);
|
|
189
|
+
let done = first.length < CATALOG_PAGE;
|
|
190
|
+
let skip = CATALOG_PAGE;
|
|
191
|
+
while (!done) {
|
|
192
|
+
if (records >= MAX_CATALOG_RECORDS) {
|
|
193
|
+
partial = true;
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
const pages = await Promise.all(Array.from({ length: CATALOG_PARALLEL }, (_, i) => read(query(skip + i * CATALOG_PAGE))));
|
|
197
|
+
skip += CATALOG_PARALLEL * CATALOG_PAGE;
|
|
198
|
+
for (const page of pages) {
|
|
199
|
+
take(page);
|
|
200
|
+
if (page.length < CATALOG_PAGE) {
|
|
201
|
+
done = true;
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return {
|
|
207
|
+
format: CATALOG_FORMAT,
|
|
208
|
+
builtAt: now.toISOString(),
|
|
209
|
+
screen: screen.name,
|
|
210
|
+
...(sub ? { sub: sub.name } : {}),
|
|
211
|
+
columns: lay.columns,
|
|
212
|
+
rows,
|
|
213
|
+
records,
|
|
214
|
+
...(partial ? { partial: true } : {}),
|
|
215
|
+
};
|
|
216
|
+
}
|
package/dist/odata.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// OData request building: --where to $filter, typed literals, query strings
|
|
2
|
+
// and paths. Paths are built only from names checked against the index, so a
|
|
3
|
+
// write path never needs percent-encoding (the Gate refuses those).
|
|
4
|
+
import { AxiError, usage } from './errors.js';
|
|
5
|
+
import { resolveField } from './index/build.js';
|
|
6
|
+
const OPERATORS = {
|
|
7
|
+
'=': 'eq',
|
|
8
|
+
'!=': 'ne',
|
|
9
|
+
'>': 'gt',
|
|
10
|
+
'>=': 'ge',
|
|
11
|
+
'<': 'lt',
|
|
12
|
+
'<=': 'le',
|
|
13
|
+
};
|
|
14
|
+
const WHERE = /^([A-Za-z][A-Za-z0-9_]*)\s*(!=|>=|<=|=|>|<|~)\s?([\s\S]*)$/;
|
|
15
|
+
const WHERE_HELP = 'use FIELD=VALUE, FIELD!=VALUE, FIELD>VALUE, FIELD>=VALUE, FIELD<VALUE or FIELD<=VALUE';
|
|
16
|
+
export function parseWhere(expr) {
|
|
17
|
+
const m = WHERE.exec(expr);
|
|
18
|
+
if (!m?.[1] || !m[2] || m[3] === undefined)
|
|
19
|
+
throw usage(`Invalid --where; ${WHERE_HELP}.`);
|
|
20
|
+
return { field: m[1], op: m[2], value: m[3] };
|
|
21
|
+
}
|
|
22
|
+
const INTEGER = new Set(['Int16', 'Int32', 'Int64', 'Byte', 'SByte']);
|
|
23
|
+
const DECIMAL = new Set(['Decimal', 'Double', 'Single']);
|
|
24
|
+
const DAY = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
25
|
+
const TIME = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}(\.\d+)?)?(Z|[+-]\d{2}:\d{2})$/;
|
|
26
|
+
/** A UTC timestamp with seconds and Z, as Priority recommends for filters. */
|
|
27
|
+
export function utc(raw, flag) {
|
|
28
|
+
const day = DAY.exec(raw);
|
|
29
|
+
let ms;
|
|
30
|
+
if (day) {
|
|
31
|
+
ms = Date.UTC(Number(day[1]), Number(day[2]) - 1, Number(day[3]));
|
|
32
|
+
if (new Date(ms).toISOString().slice(0, 10) !== raw)
|
|
33
|
+
ms = Number.NaN;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
ms = TIME.test(raw) ? Date.parse(raw) : Number.NaN;
|
|
37
|
+
}
|
|
38
|
+
if (!Number.isFinite(ms)) {
|
|
39
|
+
throw usage(`Invalid date for ${flag}; use YYYY-MM-DD or a full time such as 2026-10-01T08:00:00Z.`);
|
|
40
|
+
}
|
|
41
|
+
return new Date(ms).toISOString().replace(/\.\d{3}Z$/, 'Z');
|
|
42
|
+
}
|
|
43
|
+
/** An OData literal for a value of this field's type. */
|
|
44
|
+
export function literal(field, raw) {
|
|
45
|
+
if (field.type === 'String')
|
|
46
|
+
return `'${raw.replace(/'/g, "''")}'`;
|
|
47
|
+
if (INTEGER.has(field.type)) {
|
|
48
|
+
if (!/^-?\d+$/.test(raw))
|
|
49
|
+
throw usage(`${field.name} is a whole number; the --where value is not.`);
|
|
50
|
+
return raw;
|
|
51
|
+
}
|
|
52
|
+
if (DECIMAL.has(field.type)) {
|
|
53
|
+
if (!/^-?\d+(\.\d+)?$/.test(raw)) {
|
|
54
|
+
throw usage(`${field.name} is a number; write it with a point, such as 12.50.`);
|
|
55
|
+
}
|
|
56
|
+
return raw;
|
|
57
|
+
}
|
|
58
|
+
if (field.type === 'Boolean') {
|
|
59
|
+
if (raw !== 'true' && raw !== 'false')
|
|
60
|
+
throw usage(`${field.name} is true or false.`);
|
|
61
|
+
return raw;
|
|
62
|
+
}
|
|
63
|
+
if (field.type === 'DateTimeOffset' || field.type === 'Date')
|
|
64
|
+
return utc(raw, `--where ${field.name}`);
|
|
65
|
+
throw usage(`--where cannot filter ${field.name} (${field.type}); use --filter with raw OData.`);
|
|
66
|
+
}
|
|
67
|
+
export function whereToFilter(wheres, type, screenName) {
|
|
68
|
+
const parts = wheres.map((expr) => {
|
|
69
|
+
const term = parseWhere(expr);
|
|
70
|
+
const field = resolveField(type, screenName, term.field);
|
|
71
|
+
if (term.op === '~') {
|
|
72
|
+
// QA (Priority 23.1) answers 501 to both contains() and startswith(),
|
|
73
|
+
// so ~ is refused before a request is spent on it.
|
|
74
|
+
throw usage(`Priority does not support contains, so --where has no ~. For text that starts with ABC, use a range: --where '${field.name}>=ABC' --where '${field.name}<ABD'.`);
|
|
75
|
+
}
|
|
76
|
+
return `${field.name} ${OPERATORS[term.op]} ${literal(field, term.value)}`;
|
|
77
|
+
});
|
|
78
|
+
return parts.length ? parts.join(' and ') : undefined;
|
|
79
|
+
}
|
|
80
|
+
export function combineFilters(where, raw) {
|
|
81
|
+
if (raw !== undefined && !raw.trim())
|
|
82
|
+
throw usage('--filter is empty.');
|
|
83
|
+
if (where && raw)
|
|
84
|
+
return `${where} and (${raw})`;
|
|
85
|
+
return where ?? raw;
|
|
86
|
+
}
|
|
87
|
+
/** --sort F or F:desc. */
|
|
88
|
+
export function orderBy(sort, type, screenName) {
|
|
89
|
+
const m = /^([A-Za-z][A-Za-z0-9_]*)(?::(asc|desc))?$/i.exec(sort);
|
|
90
|
+
if (!m?.[1])
|
|
91
|
+
throw usage('Invalid --sort; use FIELD or FIELD:desc.');
|
|
92
|
+
const field = resolveField(type, screenName, m[1]);
|
|
93
|
+
return m[2]?.toLowerCase() === 'desc' ? `${field.name} desc` : field.name;
|
|
94
|
+
}
|
|
95
|
+
/** "?$top=5&$filter=..." with each value encoded; option names stay literal. */
|
|
96
|
+
export function queryString(params) {
|
|
97
|
+
const parts = Object.entries(params)
|
|
98
|
+
.filter((entry) => entry[1] !== undefined)
|
|
99
|
+
.map(([k, v]) => `${k}=${encodeURIComponent(String(v))}`);
|
|
100
|
+
return parts.length ? `?${parts.join('&')}` : '';
|
|
101
|
+
}
|
|
102
|
+
/** A path segment for a screen name already resolved from the index. */
|
|
103
|
+
export function screenSegment(name) {
|
|
104
|
+
if (!/^[A-Za-z][A-Za-z0-9_]*$/.test(name)) {
|
|
105
|
+
throw new AxiError('internal_error', 'A screen name reached the request builder without being checked.');
|
|
106
|
+
}
|
|
107
|
+
return name;
|
|
108
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Priority stores the text of its remarks sub-screens (ORDERSTEXT and the
|
|
2
|
+
// like) as HTML: a <style> block, then <p dir=rtl> paragraphs. People and
|
|
3
|
+
// agents read it as plain text, so paragraphs and line breaks become lines,
|
|
4
|
+
// tags are dropped and character references are decoded. Pure: no I/O.
|
|
5
|
+
// visible() still runs on the result, so a decoded control character is
|
|
6
|
+
// shown escaped like any other.
|
|
7
|
+
/** Blocks whose content is never text. */
|
|
8
|
+
const DROPPED = /<(style|script|head|title)\b[^>]*>[\s\S]*?<\/\1\s*>/gi;
|
|
9
|
+
/** Tags that end a line. */
|
|
10
|
+
const LINE_END = /<br\b[^>]*>|<\/(?:p|div|li|tr|h[1-6]|table|ul|ol)\s*>/gi;
|
|
11
|
+
const TAG = /<!--[\s\S]*?-->|<\/?[a-z][^>]*>/gi;
|
|
12
|
+
const HAS_TAG = /<!--[\s\S]*?-->|<\/?[a-z][^>]*>/i;
|
|
13
|
+
const REFERENCE = /&(?:#(\d{1,7})|#x([0-9a-f]{1,6})|([a-z]+));/gi;
|
|
14
|
+
const NAMED = {
|
|
15
|
+
amp: '&',
|
|
16
|
+
lt: '<',
|
|
17
|
+
gt: '>',
|
|
18
|
+
quot: '"',
|
|
19
|
+
apos: "'",
|
|
20
|
+
nbsp: ' ',
|
|
21
|
+
};
|
|
22
|
+
function character(code, written) {
|
|
23
|
+
const valid = code > 0 && code <= 0x10ffff && (code < 0xd800 || code > 0xdfff);
|
|
24
|
+
return valid ? String.fromCodePoint(code) : written;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* A sub-screen of Priority's remarks kind: one record with TEXT, APPEND and
|
|
28
|
+
* SIGNATURE (all 450 such sub-screens on QA). Line lists that have a TEXT
|
|
29
|
+
* field hold short plain text instead.
|
|
30
|
+
*/
|
|
31
|
+
export function isTextForm(type) {
|
|
32
|
+
return ['TEXT', 'APPEND', 'SIGNATURE'].every((name) => type.fields.some((f) => f.name === name));
|
|
33
|
+
}
|
|
34
|
+
/** The text of Priority's HTML: one line per paragraph, at most one blank line in a row. */
|
|
35
|
+
export function plainText(html) {
|
|
36
|
+
if (!HAS_TAG.test(html))
|
|
37
|
+
return html;
|
|
38
|
+
const text = html
|
|
39
|
+
.replace(DROPPED, '')
|
|
40
|
+
.replace(LINE_END, '\n')
|
|
41
|
+
.replace(TAG, '')
|
|
42
|
+
.replace(REFERENCE, (written, dec, hex, name) => {
|
|
43
|
+
if (dec)
|
|
44
|
+
return character(Number(dec), written);
|
|
45
|
+
if (hex)
|
|
46
|
+
return character(Number.parseInt(hex, 16), written);
|
|
47
|
+
return NAMED[String(name).toLowerCase()] ?? written;
|
|
48
|
+
});
|
|
49
|
+
const lines = [];
|
|
50
|
+
for (const raw of text.split('\n')) {
|
|
51
|
+
const line = raw.replace(/\s+/g, ' ').trim();
|
|
52
|
+
if (line || (lines.length && lines[lines.length - 1] !== ''))
|
|
53
|
+
lines.push(line);
|
|
54
|
+
}
|
|
55
|
+
while (lines.length && lines[lines.length - 1] === '')
|
|
56
|
+
lines.pop();
|
|
57
|
+
return lines.join('\n');
|
|
58
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// How records from Priority become rows: which columns, how values look.
|
|
2
|
+
import { isTextForm, plainText } from './html.js';
|
|
3
|
+
import { cut } from './toon.js';
|
|
4
|
+
/** Default columns for lists: the key, then non-empty fields in form order, up to `max` in all. */
|
|
5
|
+
export const DEFAULT_COLUMNS = 4;
|
|
6
|
+
const isEmpty = (v) => v === null || v === undefined || v === '';
|
|
7
|
+
export function defaultColumns(type, rows, max = DEFAULT_COLUMNS) {
|
|
8
|
+
// Every key field is shown, even past `max`: without it a row cannot be named.
|
|
9
|
+
const columns = type.key.filter((k) => type.fields.some((f) => f.name === k));
|
|
10
|
+
for (const field of type.fields) {
|
|
11
|
+
if (columns.length >= max)
|
|
12
|
+
break;
|
|
13
|
+
if (columns.includes(field.name) || field.internalId)
|
|
14
|
+
continue;
|
|
15
|
+
if (rows.some((row) => !isEmpty(row[field.name])))
|
|
16
|
+
columns.push(field.name);
|
|
17
|
+
}
|
|
18
|
+
return columns;
|
|
19
|
+
}
|
|
20
|
+
/** Format one value for display. Dates of DateType "Date" show as YYYY-MM-DD. */
|
|
21
|
+
export function formatValue(field, value, fieldLimit) {
|
|
22
|
+
if (value === '' || value === undefined)
|
|
23
|
+
return null;
|
|
24
|
+
if (typeof value !== 'string')
|
|
25
|
+
return value;
|
|
26
|
+
if (field?.dateType === 'Date' && /^\d{4}-\d{2}-\d{2}T/.test(value))
|
|
27
|
+
return value.slice(0, 10);
|
|
28
|
+
return Number.isFinite(fieldLimit) ? cut(value, fieldLimit) : value;
|
|
29
|
+
}
|
|
30
|
+
/** A table row: exactly `columns`, empty values as null, so TOON keeps the tabular form. */
|
|
31
|
+
export function tableRow(type, row, columns, fieldLimit) {
|
|
32
|
+
const out = {};
|
|
33
|
+
for (const name of columns) {
|
|
34
|
+
out[name] = formatValue(type.fields.find((f) => f.name === name), row[name], fieldLimit);
|
|
35
|
+
}
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* A single record: every non-empty field in form order; internal IDs only with
|
|
40
|
+
* `withIds`. The HTML of a remarks sub-screen's TEXT is shown as plain text.
|
|
41
|
+
*/
|
|
42
|
+
export function recordView(type, row, fieldLimit, withIds = false) {
|
|
43
|
+
const out = {};
|
|
44
|
+
const textForm = isTextForm(type);
|
|
45
|
+
for (const field of type.fields) {
|
|
46
|
+
if (field.internalId && !withIds && !type.key.includes(field.name))
|
|
47
|
+
continue;
|
|
48
|
+
const raw = row[field.name];
|
|
49
|
+
const value = textForm && field.name === 'TEXT' && typeof raw === 'string'
|
|
50
|
+
? plainText(raw)
|
|
51
|
+
: raw;
|
|
52
|
+
if (isEmpty(value))
|
|
53
|
+
continue;
|
|
54
|
+
out[field.name] = formatValue(field, value, fieldLimit);
|
|
55
|
+
}
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// Text from Priority is data written by people and imports. Before printing:
|
|
2
|
+
// - invisible and control characters are shown as visible \uXXXX escapes
|
|
3
|
+
// (Comis D380), so bidirectional overrides cannot hide or reorder text;
|
|
4
|
+
// - unpaired surrogates become U+FFFD;
|
|
5
|
+
// - a value that looks like a credential is withheld.
|
|
6
|
+
// Hebrew and other ordinary text is kept as it is.
|
|
7
|
+
// Kept as escape text, never as literal characters: they are invisible in source.
|
|
8
|
+
const INVISIBLE = '\\p{Cf}\\p{Default_Ignorable_Code_Point}\\u2800';
|
|
9
|
+
const ESCAPE = new RegExp(String.raw `[${INVISIBLE}]|(?![\n\t])[\p{Cc}\p{Zl}\p{Zp}]`, 'gu');
|
|
10
|
+
const STRIP = new RegExp(`[${INVISIBLE}]`, 'gu');
|
|
11
|
+
const LONE_SURROGATE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g;
|
|
12
|
+
// Checked on the text with invisible characters removed, so a token split by
|
|
13
|
+
// zero-width characters is still found.
|
|
14
|
+
const CREDENTIAL = new RegExp([
|
|
15
|
+
String.raw `\bBearer\s+[A-Za-z0-9._~+/=-]{8,}`,
|
|
16
|
+
String.raw `\bsk-[A-Za-z0-9_-]{16,}`,
|
|
17
|
+
String.raw `\bAKIA[A-Z0-9]{16}\b`,
|
|
18
|
+
String.raw `\bghp_[A-Za-z0-9]{36}\b`,
|
|
19
|
+
String.raw `\bgithub_pat_[A-Za-z0-9_]{82}\b`,
|
|
20
|
+
String.raw `\bxox[baprs]-[A-Za-z0-9-]{10,}`,
|
|
21
|
+
'(?<![A-Za-z0-9_-])AIza[A-Za-z0-9_-]{35}',
|
|
22
|
+
'-----BEGIN [A-Z ]*PRIVATE KEY-----',
|
|
23
|
+
].join('|'));
|
|
24
|
+
export const WITHHELD = '[withheld: looks like a credential]';
|
|
25
|
+
const REPLACEMENT = String.fromCodePoint(0xfffd);
|
|
26
|
+
const hex = (unit) => `\\u${unit.toString(16).padStart(4, '0')}`;
|
|
27
|
+
export function visibleString(value) {
|
|
28
|
+
if (CREDENTIAL.test(value.replace(STRIP, '')))
|
|
29
|
+
return WITHHELD;
|
|
30
|
+
return value.replace(LONE_SURROGATE, REPLACEMENT).replace(ESCAPE, (ch) => ch
|
|
31
|
+
.split('')
|
|
32
|
+
.map((u) => hex(u.charCodeAt(0)))
|
|
33
|
+
.join(''));
|
|
34
|
+
}
|
|
35
|
+
/** Deep copy with every string (keys included) made visible. */
|
|
36
|
+
export function visible(value) {
|
|
37
|
+
if (typeof value === 'string')
|
|
38
|
+
return visibleString(value);
|
|
39
|
+
if (Array.isArray(value))
|
|
40
|
+
return value.map((v) => visible(v));
|
|
41
|
+
if (value && typeof value === 'object') {
|
|
42
|
+
return Object.fromEntries(Object.entries(value).map(([k, v]) => [
|
|
43
|
+
visibleString(k),
|
|
44
|
+
visible(v),
|
|
45
|
+
]));
|
|
46
|
+
}
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// TOON output on stdout, from the official encoder pinned exactly, with the
|
|
2
|
+
// AXI help footer and the size bounds Comis uses (D240, D380).
|
|
3
|
+
import { encode } from '@toon-format/toon';
|
|
4
|
+
import { AxiError } from '../errors.js';
|
|
5
|
+
import { visible } from './text.js';
|
|
6
|
+
/** A long field is cut to this many code points unless --full. */
|
|
7
|
+
export const FIELD_LIMIT = 500;
|
|
8
|
+
/** Default output fits this many UTF-16 units. */
|
|
9
|
+
export const OUTPUT_LIMIT = 4000;
|
|
10
|
+
/** --full output fits this many encoded bytes. */
|
|
11
|
+
export const FULL_LIMIT = 262144;
|
|
12
|
+
/** The indented help footer that follows the encoded body as AXI text. */
|
|
13
|
+
export function helpBlock(lines) {
|
|
14
|
+
if (!lines.length)
|
|
15
|
+
return '';
|
|
16
|
+
return `help[${lines.length}]:\n${lines.map((l) => ` ${l}\n`).join('')}`;
|
|
17
|
+
}
|
|
18
|
+
/** Encode a document (made visible first) and append help lines. */
|
|
19
|
+
export function render(doc, help = []) {
|
|
20
|
+
return `${encode(visible(doc))}\n${helpBlock(help)}`;
|
|
21
|
+
}
|
|
22
|
+
/** Cut text to `limit` code points with the standard marker naming its size. */
|
|
23
|
+
export function cut(text, limit = FIELD_LIMIT) {
|
|
24
|
+
const points = [...text];
|
|
25
|
+
if (points.length <= limit)
|
|
26
|
+
return text;
|
|
27
|
+
return `${points.slice(0, limit).join('')} … (truncated, ${points.length} code points total; use --full to see all)`;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Fit a view into the default bound by first shortening long fields, then
|
|
31
|
+
* dropping trailing rows (the builder reports what it dropped). With --full,
|
|
32
|
+
* allow up to FULL_LIMIT encoded bytes and refuse beyond that.
|
|
33
|
+
*/
|
|
34
|
+
export function bounded(view, full) {
|
|
35
|
+
if (full) {
|
|
36
|
+
const text = view.build(view.total, Number.POSITIVE_INFINITY);
|
|
37
|
+
const bytes = Buffer.byteLength(text);
|
|
38
|
+
if (bytes > FULL_LIMIT) {
|
|
39
|
+
throw new AxiError('output_too_large', `The full output is ${bytes} encoded bytes; the limit is ${FULL_LIMIT}. Read less with ${view.narrower}.`, { bytes, limit: FULL_LIMIT });
|
|
40
|
+
}
|
|
41
|
+
return text;
|
|
42
|
+
}
|
|
43
|
+
let fieldLimit = FIELD_LIMIT;
|
|
44
|
+
let text = view.build(view.total, fieldLimit);
|
|
45
|
+
if (text.length <= OUTPUT_LIMIT)
|
|
46
|
+
return text;
|
|
47
|
+
while (fieldLimit > 16 &&
|
|
48
|
+
view.build(Math.min(1, view.total), fieldLimit).length > OUTPUT_LIMIT) {
|
|
49
|
+
fieldLimit = Math.floor(fieldLimit / 2);
|
|
50
|
+
}
|
|
51
|
+
text = view.build(view.total, fieldLimit);
|
|
52
|
+
if (text.length <= OUTPUT_LIMIT)
|
|
53
|
+
return text;
|
|
54
|
+
let low = 0;
|
|
55
|
+
let high = view.total;
|
|
56
|
+
while (low < high) {
|
|
57
|
+
const mid = Math.ceil((low + high) / 2);
|
|
58
|
+
if (view.build(mid, fieldLimit).length <= OUTPUT_LIMIT)
|
|
59
|
+
low = mid;
|
|
60
|
+
else
|
|
61
|
+
high = mid - 1;
|
|
62
|
+
}
|
|
63
|
+
return view.build(low, fieldLimit);
|
|
64
|
+
}
|
|
65
|
+
/** The error record on stdout: message, code, usage, then extra fields. */
|
|
66
|
+
export function errorText(error, usageLine) {
|
|
67
|
+
const extra = {};
|
|
68
|
+
for (const [k, v] of Object.entries(error.fields)) {
|
|
69
|
+
if (k !== 'message' && k !== 'code' && k !== 'usage')
|
|
70
|
+
extra[k] = typeof v === 'string' ? cut(v) : v;
|
|
71
|
+
}
|
|
72
|
+
return render({
|
|
73
|
+
message: cut(error.message),
|
|
74
|
+
code: error.code,
|
|
75
|
+
usage: usageLine,
|
|
76
|
+
...extra,
|
|
77
|
+
});
|
|
78
|
+
}
|
package/dist/search.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// Text search over a local copy of a screen's catalog. Priority answers 501
|
|
2
|
+
// to contains() and startswith(), so `find` matches here instead. Pure: no I/O.
|
|
3
|
+
//
|
|
4
|
+
// Matching is forgiving about how people and imports write Hebrew: points
|
|
5
|
+
// (niqqud) are ignored, final letters match their plain forms, and the Hebrew
|
|
6
|
+
// geresh and gershayim match ' and ". Every query word must appear: numbers
|
|
7
|
+
// as whole numbers standing on their own (4 matches neither 4000, SN4 nor
|
|
8
|
+
// 3/4), other words at the start of a word, after any Hebrew prefix letters.
|
|
9
|
+
/** Hebrew points and cantillation marks. */
|
|
10
|
+
const POINTS = /[\u0591-\u05BD\u05BF\u05C1\u05C2\u05C4\u05C5\u05C7]/g;
|
|
11
|
+
const FINALS = {
|
|
12
|
+
'\u05DA': '\u05DB',
|
|
13
|
+
'\u05DD': '\u05DE',
|
|
14
|
+
'\u05DF': '\u05E0',
|
|
15
|
+
'\u05E3': '\u05E4',
|
|
16
|
+
'\u05E5': '\u05E6',
|
|
17
|
+
};
|
|
18
|
+
const FINAL = /[\u05DA\u05DD\u05DF\u05E3\u05E5]/g;
|
|
19
|
+
const DOUBLE_QUOTES = /[\u05F4\u201C\u201D\u201E]|''/g;
|
|
20
|
+
const SINGLE_QUOTES = /[\u05F3\u2018\u2019`]/g;
|
|
21
|
+
/**
|
|
22
|
+
* A number standing on its own: 4, 110, 1.5, 1/2, 1.1/2, or 110 in 110*200.
|
|
23
|
+
* Digits joined to letters, such as the 4 in SN4, are part of a word instead.
|
|
24
|
+
*/
|
|
25
|
+
const NUMBER = /(?<![\p{L}\d.\/])\d+(?:[./]\d+)*(?![\p{L}\d])/gu;
|
|
26
|
+
const IS_NUMBER = /^\d+(?:[./]\d+)*$/;
|
|
27
|
+
/** Words: runs of letters and digits, with the number punctuation inside them. */
|
|
28
|
+
const TERM = /[\p{L}\p{N}]+(?:[./][\p{N}]+)*/gu;
|
|
29
|
+
export function normalize(text) {
|
|
30
|
+
return text
|
|
31
|
+
.normalize('NFKC')
|
|
32
|
+
.replace(POINTS, '')
|
|
33
|
+
.replace(FINAL, (c) => FINALS[c] ?? c)
|
|
34
|
+
.replace(DOUBLE_QUOTES, '"')
|
|
35
|
+
.replace(SINGLE_QUOTES, "'")
|
|
36
|
+
.toLowerCase();
|
|
37
|
+
}
|
|
38
|
+
/** The terms of a query. One-letter words are dropped; numbers are kept. */
|
|
39
|
+
export function queryTerms(words) {
|
|
40
|
+
const out = [];
|
|
41
|
+
for (const word of words) {
|
|
42
|
+
// Normalizing keeps letters, digits and their order, so the terms of the
|
|
43
|
+
// normalized word line up one to one with the terms as written.
|
|
44
|
+
const written = [
|
|
45
|
+
...word
|
|
46
|
+
.normalize('NFKC')
|
|
47
|
+
.replace(POINTS, '')
|
|
48
|
+
.toLowerCase()
|
|
49
|
+
.matchAll(TERM),
|
|
50
|
+
].map((m) => m[0]);
|
|
51
|
+
[...normalize(word).matchAll(TERM)].forEach((m, i) => {
|
|
52
|
+
const text = m[0];
|
|
53
|
+
const number = IS_NUMBER.test(text);
|
|
54
|
+
if (!number && [...text].length < 2)
|
|
55
|
+
return;
|
|
56
|
+
if (!out.some((t) => t.text === text))
|
|
57
|
+
out.push({ text, shown: written[i] ?? text, number });
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
62
|
+
export function haystack(parts) {
|
|
63
|
+
const text = normalize(parts.filter((p) => p !== null && p !== undefined).join(' '));
|
|
64
|
+
return {
|
|
65
|
+
words: [...text.matchAll(TERM)].map((m) => m[0]),
|
|
66
|
+
numbers: new Set([...text.matchAll(NUMBER)].map((m) => m[0])),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/** Letters that join the front of a Hebrew word: and, the, in, as, to, from, that. */
|
|
70
|
+
const PREFIX = /[\u05D5\u05D4\u05D1\u05DB\u05DC\u05DE\u05E9]/;
|
|
71
|
+
/**
|
|
72
|
+
* A query word matches the start of a word, after up to three Hebrew prefix
|
|
73
|
+
* letters: ביוב matches לביוב and צינור matches צינורות, but לבן does not
|
|
74
|
+
* match מגולבן.
|
|
75
|
+
*/
|
|
76
|
+
function startsWord(word, term) {
|
|
77
|
+
for (let i = 0; i <= 3; i++) {
|
|
78
|
+
if (word.startsWith(term, i))
|
|
79
|
+
return true;
|
|
80
|
+
if (!PREFIX.test(word[i] ?? ''))
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
/** The terms a haystack lacks. */
|
|
86
|
+
export function missing(terms, hay) {
|
|
87
|
+
return terms.filter((t) => t.number
|
|
88
|
+
? !hay.numbers.has(t.text)
|
|
89
|
+
: !hay.words.some((w) => startsWord(w, t.text)));
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Rows matching every term. If none does, the rows matching most terms, as
|
|
93
|
+
* long as they match at least half of them (and at least one).
|
|
94
|
+
*/
|
|
95
|
+
export function rank(rows, text, terms) {
|
|
96
|
+
const all = [];
|
|
97
|
+
const near = [];
|
|
98
|
+
rows.forEach((row, i) => {
|
|
99
|
+
const lacking = missing(terms, text(row));
|
|
100
|
+
if (!lacking.length)
|
|
101
|
+
all.push(row);
|
|
102
|
+
else if (lacking.length < terms.length)
|
|
103
|
+
near.push({ row, lacking, i });
|
|
104
|
+
});
|
|
105
|
+
if (all.length || !near.length)
|
|
106
|
+
return { all, closest: [] };
|
|
107
|
+
const fewest = Math.min(...near.map((n) => n.lacking.length));
|
|
108
|
+
const enough = terms.length - fewest >= Math.ceil(terms.length / 2);
|
|
109
|
+
return {
|
|
110
|
+
all,
|
|
111
|
+
closest: enough
|
|
112
|
+
? near
|
|
113
|
+
.filter((n) => n.lacking.length === fewest)
|
|
114
|
+
.sort((a, b) => a.i - b.i)
|
|
115
|
+
.map((n) => ({ row: n.row, lacking: n.lacking.map((t) => t.shown) }))
|
|
116
|
+
: [],
|
|
117
|
+
};
|
|
118
|
+
}
|