rida-format 0.0.0-stage → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/LICENSE +51 -0
- package/README.md +1120 -2
- package/package.json +29 -4
- package/src/builder.js +520 -0
- package/src/cli.js +100 -0
- package/src/index.js +360 -0
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rida-format",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Node.js reader and tools for RIDA 0.3.3 datasets",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=20"
|
|
8
|
+
},
|
|
9
|
+
"bin": {
|
|
10
|
+
"rida": "src/cli.js",
|
|
11
|
+
"rida-format": "src/cli.js"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": "./src/index.js",
|
|
15
|
+
"./builder": "./src/builder.js"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"src/",
|
|
19
|
+
"README.md",
|
|
20
|
+
"LICENSE",
|
|
21
|
+
"CHANGELOG.md"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"start": "node ./src/cli.js",
|
|
25
|
+
"check": "node --check src/index.js && node --check src/builder.js && node --check src/cli.js"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"lzma-native": "^8.0.6"
|
|
29
|
+
},
|
|
30
|
+
"license": "SEE LICENSE IN LICENSE"
|
|
31
|
+
}
|
package/src/builder.js
ADDED
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
import { createHash, randomBytes, createCipheriv } from "node:crypto";
|
|
2
|
+
import { readdir, readFile, mkdir, open, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
3
|
+
import { createReadStream } from "node:fs";
|
|
4
|
+
import { basename, dirname, extname, join, relative, resolve } from "node:path";
|
|
5
|
+
import { deflateRawSync } from "node:zlib";
|
|
6
|
+
import { createRequire } from "node:module";
|
|
7
|
+
import { RidaError } from "./index.js";
|
|
8
|
+
|
|
9
|
+
const require = createRequire(import.meta.url);
|
|
10
|
+
const lzma = require("lzma-native");
|
|
11
|
+
const INDEX_MAGIC = Buffer.from("RIDAIDX1");
|
|
12
|
+
const CATALOG_MAGIC = Buffer.from("RDACAT01");
|
|
13
|
+
const HEADER_SIZE = 72;
|
|
14
|
+
|
|
15
|
+
export function compactJSON(value) {
|
|
16
|
+
const text = JSON.stringify(value, (_, item) => {
|
|
17
|
+
if (typeof item === "number" && !Number.isFinite(item)) throw new RidaError("RIDA stores standard JSON only; NaN and Infinity are not supported");
|
|
18
|
+
return item;
|
|
19
|
+
});
|
|
20
|
+
if (text === undefined) throw new RidaError("Value cannot be represented as JSON");
|
|
21
|
+
return Buffer.from(text, "utf8");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function encodeVarint(input) {
|
|
25
|
+
let value = BigInt(input);
|
|
26
|
+
const bytes = [];
|
|
27
|
+
while (value >= 128n) { bytes.push(Number(value & 127n) | 128); value >>= 7n; }
|
|
28
|
+
bytes.push(Number(value));
|
|
29
|
+
return Buffer.from(bytes);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function orderKey(name) {
|
|
33
|
+
const stem = name.slice(0, name.lastIndexOf("."));
|
|
34
|
+
return /^\d+$/.test(stem) ? [0, BigInt(stem), name.toLowerCase()] : [1, stem.toLowerCase(), name.toLowerCase()];
|
|
35
|
+
}
|
|
36
|
+
const lexical = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
37
|
+
function compareNames(a, b) {
|
|
38
|
+
const x = orderKey(a), y = orderKey(b);
|
|
39
|
+
if (x[0] !== y[0]) return x[0] - y[0];
|
|
40
|
+
if (x[1] !== y[1]) return x[1] < y[1] ? -1 : 1;
|
|
41
|
+
return lexical(x[2], y[2]);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function walkJSON(root, { recursive = true } = {}) {
|
|
45
|
+
const files = [];
|
|
46
|
+
async function walk(dir) {
|
|
47
|
+
const entries = (await readdir(dir, { withFileTypes: true })).filter((entry) => !entry.name.startsWith(".")).sort((a, b) => lexical(a.name, b.name));
|
|
48
|
+
for (const entry of entries) {
|
|
49
|
+
const path = join(dir, entry.name);
|
|
50
|
+
if (entry.isDirectory()) { if (recursive) await walk(path); continue; }
|
|
51
|
+
if (entry.isFile() && /\.json$/i.test(entry.name)) files.push(path);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
await walk(root);
|
|
55
|
+
files.sort((a, b) => lexical(relative(root, a).split(/[\\/]/).join("/"), relative(root, b).split(/[\\/]/).join("/")));
|
|
56
|
+
if (!files.length) throw new RidaError(`No JSON files found in ${root}`);
|
|
57
|
+
return files;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function numberedIds(paths, root) {
|
|
61
|
+
const folderChildren = new Map();
|
|
62
|
+
const filesByFolder = new Map();
|
|
63
|
+
const rootText = resolve(root);
|
|
64
|
+
for (const path of paths) {
|
|
65
|
+
const folder = dirname(path);
|
|
66
|
+
if (!filesByFolder.has(folder)) filesByFolder.set(folder, []);
|
|
67
|
+
filesByFolder.get(folder).push(path);
|
|
68
|
+
let current = folder;
|
|
69
|
+
while (current !== rootText && current.startsWith(rootText + "/")) {
|
|
70
|
+
const parent = dirname(current);
|
|
71
|
+
if (!folderChildren.has(parent)) folderChildren.set(parent, new Set());
|
|
72
|
+
folderChildren.get(parent).add(current);
|
|
73
|
+
current = parent;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const folderIds = new Map([[rootText, ""]]);
|
|
77
|
+
const queue = [rootText];
|
|
78
|
+
const mapPaths = {};
|
|
79
|
+
while (queue.length) {
|
|
80
|
+
const parent = queue.shift();
|
|
81
|
+
const children = [...(folderChildren.get(parent) ?? [])].sort((a, b) => lexical(basename(a), basename(b)));
|
|
82
|
+
for (let i = 0; i < children.length; i++) {
|
|
83
|
+
const child = children[i];
|
|
84
|
+
const prefix = [folderIds.get(parent), String(i + 1)].filter(Boolean).join("/");
|
|
85
|
+
folderIds.set(child, prefix);
|
|
86
|
+
mapPaths[prefix] = basename(child);
|
|
87
|
+
queue.push(child);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const ids = new Map();
|
|
91
|
+
for (const [folder, folderFiles] of filesByFolder) {
|
|
92
|
+
folderFiles.sort((a, b) => compareNames(basename(a), basename(b)));
|
|
93
|
+
const prefix = folderIds.get(folder) ?? "";
|
|
94
|
+
folderFiles.forEach((path, i) => ids.set(path, [prefix, String(i + 1)].filter(Boolean).join("/")));
|
|
95
|
+
}
|
|
96
|
+
return { ids, paths: mapPaths };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function uniqueId(id, used) {
|
|
100
|
+
if (!used.has(id)) { used.add(id); return id; }
|
|
101
|
+
let n = 2;
|
|
102
|
+
while (used.has(`${id}-${n}`)) n++;
|
|
103
|
+
const unique = `${id}-${n}`;
|
|
104
|
+
used.add(unique);
|
|
105
|
+
return unique;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async function readRecordFile(path) {
|
|
109
|
+
let value;
|
|
110
|
+
try { value = JSON.parse(await readFile(path, "utf8")); }
|
|
111
|
+
catch (cause) { throw new RidaError(`Invalid JSON in ${path}: ${cause.message}`, { cause }); }
|
|
112
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) throw new RidaError(`Record file must contain one JSON object: ${path}`);
|
|
113
|
+
return value;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
class JsonCursor {
|
|
117
|
+
constructor(path) {
|
|
118
|
+
this.chunks = createReadStream(path, { encoding: "utf8" })[Symbol.asyncIterator]();
|
|
119
|
+
this.chunk = "";
|
|
120
|
+
this.position = 0;
|
|
121
|
+
this.ended = false;
|
|
122
|
+
this.started = false;
|
|
123
|
+
this.path = path;
|
|
124
|
+
}
|
|
125
|
+
async _fill() {
|
|
126
|
+
while (this.position >= this.chunk.length && !this.ended) {
|
|
127
|
+
const next = await this.chunks.next();
|
|
128
|
+
if (next.done) { this.ended = true; this.chunk = ""; this.position = 0; }
|
|
129
|
+
else { this.chunk = next.value; this.position = 0; }
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
async peek() {
|
|
133
|
+
await this._fill();
|
|
134
|
+
if (!this.started && !this.ended) {
|
|
135
|
+
this.started = true;
|
|
136
|
+
if (this.chunk[this.position] === "\uFEFF") this.position++;
|
|
137
|
+
await this._fill();
|
|
138
|
+
}
|
|
139
|
+
return this.ended ? null : this.chunk[this.position];
|
|
140
|
+
}
|
|
141
|
+
async take() { const value = await this.peek(); if (value !== null) this.position++; return value; }
|
|
142
|
+
async skipWhitespace() { while (/\s/.test(await this.peek() ?? "")) await this.take(); }
|
|
143
|
+
async expect(expected) {
|
|
144
|
+
await this.skipWhitespace();
|
|
145
|
+
const actual = await this.take();
|
|
146
|
+
if (actual !== expected) throw new RidaError(`Invalid JSON in ${this.path}: expected ${JSON.stringify(expected)}, found ${JSON.stringify(actual)}`);
|
|
147
|
+
}
|
|
148
|
+
async readValueText() {
|
|
149
|
+
await this.skipWhitespace();
|
|
150
|
+
const first = await this.peek();
|
|
151
|
+
if (first === null) throw new RidaError(`Invalid or incomplete JSON in ${this.path}`);
|
|
152
|
+
const out = [];
|
|
153
|
+
if (first === '"') {
|
|
154
|
+
let escaped = false;
|
|
155
|
+
while (true) {
|
|
156
|
+
const char = await this.take();
|
|
157
|
+
if (char === null) throw new RidaError(`Invalid or incomplete JSON string in ${this.path}`);
|
|
158
|
+
out.push(char);
|
|
159
|
+
if (escaped) escaped = false;
|
|
160
|
+
else if (char === "\\") escaped = true;
|
|
161
|
+
else if (char === '"') break;
|
|
162
|
+
}
|
|
163
|
+
return out.join("");
|
|
164
|
+
}
|
|
165
|
+
if (first === "{" || first === "[") {
|
|
166
|
+
const stack = [];
|
|
167
|
+
let inString = false;
|
|
168
|
+
let escaped = false;
|
|
169
|
+
while (true) {
|
|
170
|
+
const char = await this.take();
|
|
171
|
+
if (char === null) throw new RidaError(`Invalid or incomplete JSON value in ${this.path}`);
|
|
172
|
+
out.push(char);
|
|
173
|
+
if (inString) {
|
|
174
|
+
if (escaped) escaped = false;
|
|
175
|
+
else if (char === "\\") escaped = true;
|
|
176
|
+
else if (char === '"') inString = false;
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
if (char === '"') inString = true;
|
|
180
|
+
else if (char === "{" || char === "[") stack.push(char);
|
|
181
|
+
else if (char === "}" || char === "]") {
|
|
182
|
+
const opening = stack.pop();
|
|
183
|
+
if ((opening === "{" && char !== "}") || (opening === "[" && char !== "]")) throw new RidaError(`Mismatched JSON delimiters in ${this.path}`);
|
|
184
|
+
if (!stack.length) break;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return out.join("");
|
|
188
|
+
}
|
|
189
|
+
while (true) {
|
|
190
|
+
const char = await this.peek();
|
|
191
|
+
if (char === null || /[\s,\]}]/.test(char)) break;
|
|
192
|
+
out.push(await this.take());
|
|
193
|
+
}
|
|
194
|
+
return out.join("");
|
|
195
|
+
}
|
|
196
|
+
async finish() {
|
|
197
|
+
await this.skipWhitespace();
|
|
198
|
+
if (await this.peek() !== null) throw new RidaError(`Unexpected content after JSON value in ${this.path}`);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async function* jsonRecords(path, idKey) {
|
|
203
|
+
const cursor = new JsonCursor(path);
|
|
204
|
+
await cursor.skipWhitespace();
|
|
205
|
+
const top = await cursor.peek();
|
|
206
|
+
if (top === "{") {
|
|
207
|
+
await cursor.expect("{");
|
|
208
|
+
if (await cursor.peek() === "}") { await cursor.take(); await cursor.finish(); return; }
|
|
209
|
+
while (true) {
|
|
210
|
+
const idText = await cursor.readValueText();
|
|
211
|
+
let id;
|
|
212
|
+
try { id = JSON.parse(idText); } catch { throw new RidaError(`Invalid record ID in ${path}`); }
|
|
213
|
+
if (typeof id !== "string") throw new RidaError(`JSON object record IDs must be strings in ${path}`);
|
|
214
|
+
await cursor.expect(":");
|
|
215
|
+
const recordText = await cursor.readValueText();
|
|
216
|
+
let record;
|
|
217
|
+
try { record = JSON.parse(recordText); } catch (cause) { throw new RidaError(`Invalid JSON record ${id} in ${path}: ${cause.message}`, { cause }); }
|
|
218
|
+
if (!record || typeof record !== "object" || Array.isArray(record)) throw new RidaError(`Record ${id} is not a JSON object`);
|
|
219
|
+
yield [id, record];
|
|
220
|
+
await cursor.skipWhitespace();
|
|
221
|
+
const delimiter = await cursor.take();
|
|
222
|
+
if (delimiter === "}") break;
|
|
223
|
+
if (delimiter !== ",") throw new RidaError(`Invalid JSON object in ${path}: expected ',' or '}'`);
|
|
224
|
+
}
|
|
225
|
+
await cursor.finish();
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
if (top === "[") {
|
|
229
|
+
if (!idKey) throw new RidaError("A JSON array needs --id-key so RIDA can identify every record");
|
|
230
|
+
await cursor.expect("[");
|
|
231
|
+
if (await cursor.peek() === "]") { await cursor.take(); await cursor.finish(); return; }
|
|
232
|
+
let count = 0;
|
|
233
|
+
while (true) {
|
|
234
|
+
const recordText = await cursor.readValueText();
|
|
235
|
+
let record;
|
|
236
|
+
try { record = JSON.parse(recordText); } catch (cause) { throw new RidaError(`Invalid JSON array item in ${path}: ${cause.message}`, { cause }); }
|
|
237
|
+
count++;
|
|
238
|
+
if (!record || typeof record !== "object" || Array.isArray(record)) throw new RidaError(`Array item ${count} is not a JSON object`);
|
|
239
|
+
if (!Object.hasOwn(record, idKey) || record[idKey] == null) throw new RidaError(`Array item ${count} has no usable ${idKey} ID`);
|
|
240
|
+
yield [String(record[idKey]), record];
|
|
241
|
+
await cursor.skipWhitespace();
|
|
242
|
+
const delimiter = await cursor.take();
|
|
243
|
+
if (delimiter === "]") break;
|
|
244
|
+
if (delimiter !== ",") throw new RidaError(`Invalid JSON array in ${path}: expected ',' or ']'`);
|
|
245
|
+
}
|
|
246
|
+
await cursor.finish();
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
throw new RidaError(`JSON input must be an object mapping IDs to records or an array: ${path}`);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function encodeValue(value, codec, key) {
|
|
253
|
+
let bytes = compactJSON(value);
|
|
254
|
+
if (codec === "deflate-per-field") bytes = deflateRawSync(bytes, { level: 9 });
|
|
255
|
+
if (key) {
|
|
256
|
+
const nonce = randomBytes(12);
|
|
257
|
+
const cipher = createCipheriv("aes-256-gcm", key, nonce);
|
|
258
|
+
bytes = Buffer.concat([nonce, cipher.update(bytes), cipher.final(), cipher.getAuthTag()]);
|
|
259
|
+
}
|
|
260
|
+
return bytes;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function keyBytes(value) {
|
|
264
|
+
if (!value) return null;
|
|
265
|
+
if (Buffer.isBuffer(value)) return value;
|
|
266
|
+
const text = String(value).trim();
|
|
267
|
+
const decoded = Buffer.from(text, "base64url");
|
|
268
|
+
if (decoded.length === 32) return decoded;
|
|
269
|
+
const hex = Buffer.from(text, "hex");
|
|
270
|
+
if (hex.length === 32) return hex;
|
|
271
|
+
throw new RidaError("Encryption key must be 32 bytes, base64, or hex");
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function companion(output, suffix) {
|
|
275
|
+
let stem = basename(output, extname(output));
|
|
276
|
+
if (stem.endsWith("Data")) stem = stem.slice(0, -4);
|
|
277
|
+
return join(dirname(output), `${stem}${suffix}`);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function progress(label, done, total) {
|
|
281
|
+
const width = 24;
|
|
282
|
+
const ratio = total > 0 ? Math.min(1, done / total) : 0;
|
|
283
|
+
const filled = Math.floor(width * ratio);
|
|
284
|
+
process.stderr.write(`\r${label}: ${Math.round(ratio * 100)}% [${"█".repeat(filled)}${"░".repeat(width - filled)}] ${done.toLocaleString()}${total ? `/${total.toLocaleString()}` : ""}`);
|
|
285
|
+
if (total && done >= total) process.stderr.write("\n");
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export async function scan(source, options = {}) {
|
|
289
|
+
let sourcePath = resolve(source);
|
|
290
|
+
let sourceStat = await stat(sourcePath);
|
|
291
|
+
let discoveredFiles = null;
|
|
292
|
+
if (!sourceStat.isDirectory()) {
|
|
293
|
+
const fields = [];
|
|
294
|
+
const seenFields = new Set();
|
|
295
|
+
const usedIds = new Set();
|
|
296
|
+
const records = [];
|
|
297
|
+
let count = 0;
|
|
298
|
+
for await (const [rawId, record] of jsonRecords(sourcePath, options.idKey)) {
|
|
299
|
+
count++;
|
|
300
|
+
for (const field of Object.keys(record)) if (!seenFields.has(field)) { seenFields.add(field); fields.push(field); }
|
|
301
|
+
records.push({ id: uniqueId(String(rawId), usedIds), sourceIndex: count - 1 });
|
|
302
|
+
if (count % 1000 === 0) progress("Scanning JSON records", count, 0);
|
|
303
|
+
}
|
|
304
|
+
if (!count) throw new RidaError("RIDA needs at least one record");
|
|
305
|
+
if (!fields.length) throw new RidaError("RIDA records must contain at least one field");
|
|
306
|
+
progress("Scanning JSON records", count, count);
|
|
307
|
+
return { sourcePath, files: [], records, fields, mapPaths: {}, recordCount: records.length, singleJSON: true, idKey: options.idKey };
|
|
308
|
+
}
|
|
309
|
+
if (options.recordsDir) {
|
|
310
|
+
sourcePath = resolve(sourcePath, options.recordsDir);
|
|
311
|
+
sourceStat = await stat(sourcePath);
|
|
312
|
+
if (!sourceStat.isDirectory()) throw new RidaError(`Records folder does not exist: ${sourcePath}`);
|
|
313
|
+
} else {
|
|
314
|
+
const entries = await readdir(sourcePath, { withFileTypes: true });
|
|
315
|
+
const directFiles = entries.filter((entry) => entry.isFile() && /\.json$/i.test(entry.name) && !entry.name.startsWith("."));
|
|
316
|
+
const directJSON = directFiles.length > 0;
|
|
317
|
+
const groups = [];
|
|
318
|
+
if (directJSON) groups.push({ path: sourcePath, files: directFiles.map((entry) => join(sourcePath, entry.name)) });
|
|
319
|
+
for (const entry of entries) {
|
|
320
|
+
if (!entry.isDirectory() || entry.name.startsWith(".")) continue;
|
|
321
|
+
const groupPath = join(sourcePath, entry.name);
|
|
322
|
+
try { groups.push({ path: groupPath, files: await walkJSON(groupPath) }); } catch (error) { if (!(error instanceof RidaError)) throw error; }
|
|
323
|
+
}
|
|
324
|
+
if (groups.length > 1 && !options.combineGroups) {
|
|
325
|
+
throw new RidaError(`This folder has ${groups.length} JSON groups. Choose one with --records-dir, or use --combine-groups.`);
|
|
326
|
+
}
|
|
327
|
+
if (groups.length === 1 && !options.combineGroups) {
|
|
328
|
+
sourcePath = groups[0].path;
|
|
329
|
+
discoveredFiles = groups[0].files;
|
|
330
|
+
} else if (options.combineGroups) {
|
|
331
|
+
discoveredFiles = groups.flatMap((group) => group.files);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
if (![undefined, "common", "union"].includes(options.combinedSchema)) throw new RidaError("Combined schema must be common or union");
|
|
335
|
+
const files = discoveredFiles ?? await walkJSON(sourcePath);
|
|
336
|
+
files.sort((a, b) => lexical(relative(sourcePath, a).split(/[\\/]/).join("/"), relative(sourcePath, b).split(/[\\/]/).join("/")));
|
|
337
|
+
const mapping = options.numbered === false ? null : numberedIds(files, sourcePath);
|
|
338
|
+
const records = [];
|
|
339
|
+
const fields = [];
|
|
340
|
+
const seenFields = new Set();
|
|
341
|
+
const usedIds = new Set();
|
|
342
|
+
for (let i = 0; i < files.length; i++) {
|
|
343
|
+
let record = await readRecordFile(files[i]);
|
|
344
|
+
let id = options.numbered === false ? basename(files[i], extname(files[i])) : mapping.ids.get(files[i]);
|
|
345
|
+
if (options.numbered === false) id = uniqueId(id, usedIds);
|
|
346
|
+
records.push({ id: options.numbered === false ? id : (usedIds.add(id), id), file: files[i] });
|
|
347
|
+
for (const field of Object.keys(record)) if (!seenFields.has(field)) { seenFields.add(field); fields.push(field); }
|
|
348
|
+
if ((i + 1) % 1000 === 0 || i + 1 === files.length) progress("Scanning JSON records", i + 1, files.length);
|
|
349
|
+
}
|
|
350
|
+
if (options.combineGroups && options.combinedSchema === "common") {
|
|
351
|
+
const groupFields = new Map();
|
|
352
|
+
for (let i = 0; i < files.length; i++) {
|
|
353
|
+
const relativePath = relative(sourcePath, files[i]);
|
|
354
|
+
const firstFolder = relativePath.includes("/") ? relativePath.split("/")[0] : ".";
|
|
355
|
+
if (!groupFields.has(firstFolder)) groupFields.set(firstFolder, new Set());
|
|
356
|
+
for (const field of Object.keys(await readRecordFile(files[i]))) groupFields.get(firstFolder).add(field);
|
|
357
|
+
}
|
|
358
|
+
const [first, ...rest] = [...groupFields.values()];
|
|
359
|
+
const common = new Set([...first].filter((field) => rest.every((fields) => fields.has(field))));
|
|
360
|
+
for (let i = fields.length - 1; i >= 0; i--) if (!common.has(fields[i])) fields.splice(i, 1);
|
|
361
|
+
}
|
|
362
|
+
if (!fields.length) throw new RidaError("RIDA records must contain at least one field");
|
|
363
|
+
return { sourcePath, files, records, fields, mapPaths: mapping?.paths ?? {}, recordCount: records.length };
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
export async function build(source, output, options = {}) {
|
|
367
|
+
const codec = options.codec ?? "deflate-per-field";
|
|
368
|
+
if (!["deflate-per-field", "plain"].includes(codec)) throw new RidaError("Codec must be deflate-per-field or plain");
|
|
369
|
+
if (![undefined, "auto", "dense", "sparse"].includes(options.mode)) throw new RidaError("Mode must be auto, dense, or sparse");
|
|
370
|
+
if (options.encrypt && options.key) throw new RidaError("Choose either --encrypt for a new key or --key to reuse a key");
|
|
371
|
+
const compression = Number(options.compression ?? 9);
|
|
372
|
+
if (!Number.isInteger(compression) || compression < 0 || compression > 9) throw new RidaError("Catalog compression must be from 0 to 9");
|
|
373
|
+
const key = options.encrypt ? randomBytes(32) : keyBytes(options.key);
|
|
374
|
+
const plan = await scan(source, options);
|
|
375
|
+
const target = resolve(output);
|
|
376
|
+
const plannedOutputs = [target, companion(target, "Index.rida")];
|
|
377
|
+
if (key) plannedOutputs.push(companion(target, "Key.rida"));
|
|
378
|
+
if (Object.keys(plan.mapPaths).length) plannedOutputs.push(companion(target, "Map.json"));
|
|
379
|
+
if (await Promise.all(plannedOutputs.map(async (path) => { try { await stat(path); return path; } catch { return null; } })).then((values) => values.find(Boolean))) {
|
|
380
|
+
throw new RidaError(`An output file already exists. Choose another output name: ${target}`);
|
|
381
|
+
}
|
|
382
|
+
await mkdir(dirname(target), { recursive: true });
|
|
383
|
+
const tempData = `${target}.data-tmp-${process.pid}`;
|
|
384
|
+
const tempDense = `${target}.dense-tmp-${process.pid}`;
|
|
385
|
+
const tempSparse = `${target}.sparse-tmp-${process.pid}`;
|
|
386
|
+
const tempDenseOffsets = `${target}.dense-offsets-tmp-${process.pid}`;
|
|
387
|
+
const tempSparseOffsets = `${target}.sparse-offsets-tmp-${process.pid}`;
|
|
388
|
+
let dataLength = 0;
|
|
389
|
+
const digest = createHash("sha256");
|
|
390
|
+
const sourceIterator = plan.singleJSON ? jsonRecords(plan.sourcePath, plan.idKey)[Symbol.asyncIterator]() : null;
|
|
391
|
+
const temporary = await open(tempData, "wx");
|
|
392
|
+
const denseFile = await open(tempDense, "wx");
|
|
393
|
+
const sparseFile = await open(tempSparse, "wx");
|
|
394
|
+
const denseOffsetsFile = await open(tempDenseOffsets, "wx");
|
|
395
|
+
const sparseOffsetsFile = await open(tempSparseOffsets, "wx");
|
|
396
|
+
let denseRowsLength = 0;
|
|
397
|
+
let sparseRowsLength = 0;
|
|
398
|
+
let denseOffset = 0;
|
|
399
|
+
let sparseOffset = 0;
|
|
400
|
+
try {
|
|
401
|
+
for (let index = 0; index < plan.records.length; index++) {
|
|
402
|
+
const { file } = plan.records[index];
|
|
403
|
+
let record;
|
|
404
|
+
if (sourceIterator) {
|
|
405
|
+
const next = await sourceIterator.next();
|
|
406
|
+
if (next.done) throw new RidaError("Source JSON changed between scanning and building");
|
|
407
|
+
record = next.value[1];
|
|
408
|
+
} else record = await readRecordFile(file);
|
|
409
|
+
const recordDataStart = dataLength;
|
|
410
|
+
const denseParts = [encodeVarint(recordDataStart)];
|
|
411
|
+
const presentParts = [];
|
|
412
|
+
let presentCount = 0;
|
|
413
|
+
let previousFieldId = 0;
|
|
414
|
+
for (let fieldId = 0; fieldId < plan.fields.length; fieldId++) {
|
|
415
|
+
const field = plan.fields[fieldId];
|
|
416
|
+
if (!Object.hasOwn(record, field)) { denseParts.push(encodeVarint(0)); continue; }
|
|
417
|
+
const block = encodeValue(record[field], codec, key);
|
|
418
|
+
denseParts.push(encodeVarint(block.length));
|
|
419
|
+
presentParts.push(encodeVarint(fieldId - previousFieldId), encodeVarint(block.length));
|
|
420
|
+
previousFieldId = fieldId;
|
|
421
|
+
presentCount++;
|
|
422
|
+
await temporary.write(block);
|
|
423
|
+
digest.update(block);
|
|
424
|
+
dataLength += block.length;
|
|
425
|
+
}
|
|
426
|
+
const denseRow = Buffer.concat(denseParts);
|
|
427
|
+
const sparseRow = Buffer.concat([encodeVarint(recordDataStart), encodeVarint(presentCount), ...presentParts]);
|
|
428
|
+
await denseFile.write(denseRow);
|
|
429
|
+
await sparseFile.write(sparseRow);
|
|
430
|
+
denseRowsLength += denseRow.length;
|
|
431
|
+
sparseRowsLength += sparseRow.length;
|
|
432
|
+
denseOffset += denseRow.length;
|
|
433
|
+
sparseOffset += sparseRow.length;
|
|
434
|
+
const q = Buffer.alloc(8);
|
|
435
|
+
q.writeBigUInt64BE(BigInt(denseOffset));
|
|
436
|
+
await denseOffsetsFile.write(q);
|
|
437
|
+
q.writeBigUInt64BE(BigInt(sparseOffset));
|
|
438
|
+
await sparseOffsetsFile.write(q);
|
|
439
|
+
if ((index + 1) % 1000 === 0 || index + 1 === plan.records.length) progress("Writing RIDA values", index + 1, plan.records.length);
|
|
440
|
+
}
|
|
441
|
+
if (sourceIterator) {
|
|
442
|
+
const end = await sourceIterator.next();
|
|
443
|
+
if (!end.done) throw new RidaError("Source JSON changed between scanning and building");
|
|
444
|
+
}
|
|
445
|
+
await temporary.sync();
|
|
446
|
+
await denseFile.sync();
|
|
447
|
+
await sparseFile.sync();
|
|
448
|
+
await denseOffsetsFile.sync();
|
|
449
|
+
await sparseOffsetsFile.sync();
|
|
450
|
+
} catch (error) {
|
|
451
|
+
await sourceIterator?.return?.();
|
|
452
|
+
await Promise.all([temporary, denseFile, sparseFile, denseOffsetsFile, sparseOffsetsFile].map((file) => file.close().catch(() => {})));
|
|
453
|
+
await Promise.all([tempData, tempDense, tempSparse, tempDenseOffsets, tempSparseOffsets].map((path) => rm(path, { force: true })));
|
|
454
|
+
throw error;
|
|
455
|
+
}
|
|
456
|
+
await Promise.all([temporary, denseFile, sparseFile, denseOffsetsFile, sparseOffsetsFile].map((file) => file.close()));
|
|
457
|
+
let mode = options.mode;
|
|
458
|
+
if (!mode || mode === "auto") {
|
|
459
|
+
const common = 44 + compactJSON(plan.fields).length + (plan.records.length + 1) * 8;
|
|
460
|
+
mode = common + denseRowsLength <= common + sparseRowsLength ? "dense" : "sparse";
|
|
461
|
+
}
|
|
462
|
+
const sparse = mode === "sparse";
|
|
463
|
+
const selectedRows = await readFile(sparse ? tempSparse : tempDense);
|
|
464
|
+
const selectedOffsets = await readFile(sparse ? tempSparseOffsets : tempDenseOffsets);
|
|
465
|
+
const rowOffsets = [0];
|
|
466
|
+
for (let offset = 0; offset < selectedOffsets.length; offset += 8) rowOffsets.push(Number(selectedOffsets.readBigUInt64BE(offset)));
|
|
467
|
+
const chosenLength = sparse ? sparseRowsLength : denseRowsLength;
|
|
468
|
+
const ids = plan.records.map((record) => record.id);
|
|
469
|
+
const sequential = ids.every((id, index) => id === String(index + 1));
|
|
470
|
+
const schema = compactJSON(plan.fields);
|
|
471
|
+
const idBytes = sequential ? Buffer.alloc(0) : compactJSON(ids);
|
|
472
|
+
const offsets = Buffer.alloc(rowOffsets.length * 8);
|
|
473
|
+
rowOffsets.forEach((offset, index) => offsets.writeBigUInt64BE(BigInt(offset), index * 8));
|
|
474
|
+
const catalogHeader = Buffer.alloc(44);
|
|
475
|
+
CATALOG_MAGIC.copy(catalogHeader, 0);
|
|
476
|
+
catalogHeader.writeUInt32BE(1, 8);
|
|
477
|
+
catalogHeader.writeUInt32BE(sparse ? 2 : 1, 12);
|
|
478
|
+
catalogHeader.writeBigUInt64BE(BigInt(plan.records.length), 16);
|
|
479
|
+
catalogHeader.writeUInt32BE(plan.fields.length, 24);
|
|
480
|
+
catalogHeader.writeUInt32BE(schema.length, 28);
|
|
481
|
+
catalogHeader.writeUInt32BE(idBytes.length, 32);
|
|
482
|
+
catalogHeader.writeBigUInt64BE(BigInt(chosenLength), 36);
|
|
483
|
+
const rawCatalog = Buffer.concat([catalogHeader, schema, idBytes, offsets, selectedRows]);
|
|
484
|
+
const compressedCatalog = Buffer.from(await lzma.compress(rawCatalog, { preset: compression }));
|
|
485
|
+
const indexHeader = Buffer.alloc(HEADER_SIZE);
|
|
486
|
+
INDEX_MAGIC.copy(indexHeader, 0);
|
|
487
|
+
indexHeader.writeUInt32BE(1, 8);
|
|
488
|
+
indexHeader.writeUInt32BE(compressedCatalog.length, 12);
|
|
489
|
+
indexHeader.writeUInt32BE(rawCatalog.length, 16);
|
|
490
|
+
indexHeader.writeUInt32BE(plan.records.length, 20);
|
|
491
|
+
indexHeader.writeBigUInt64BE(BigInt(dataLength), 24);
|
|
492
|
+
indexHeader.writeUInt32BE(codec === "plain" ? 2 : 1, 32);
|
|
493
|
+
indexHeader.writeUInt32BE(key ? 1 : 0, 36);
|
|
494
|
+
digest.digest().copy(indexHeader, 40);
|
|
495
|
+
const indexBytes = Buffer.concat([indexHeader, compressedCatalog]);
|
|
496
|
+
const indexPath = companion(target, "Index.rida");
|
|
497
|
+
const mapPath = companion(target, "Map.json");
|
|
498
|
+
try {
|
|
499
|
+
await rename(tempData, target);
|
|
500
|
+
await writeFile(indexPath, indexBytes, { flag: "wx" });
|
|
501
|
+
if (key) {
|
|
502
|
+
const keyText = `${key.toString("base64url")}=`;
|
|
503
|
+
await writeFile(companion(target, "Key.rida"), `${keyText}\n`, { flag: "wx", mode: 0o600 });
|
|
504
|
+
}
|
|
505
|
+
if (Object.keys(plan.mapPaths).length) {
|
|
506
|
+
const fieldMap = Object.fromEntries(plan.fields.map((field, i) => [String(i + 1), field]));
|
|
507
|
+
await writeFile(mapPath, `${JSON.stringify({ version: 1, records: plan.recordCount, paths: plan.mapPaths, fields: fieldMap })}\n`, { flag: "wx" });
|
|
508
|
+
}
|
|
509
|
+
} catch (error) {
|
|
510
|
+
await rm(tempData, { force: true });
|
|
511
|
+
throw error;
|
|
512
|
+
}
|
|
513
|
+
await Promise.all([tempDense, tempSparse, tempDenseOffsets, tempSparseOffsets].map((path) => rm(path, { force: true })));
|
|
514
|
+
return { output: target, index: indexPath, map: Object.keys(plan.mapPaths).length ? mapPath : null, records: plan.recordCount, fields: plan.fields.length, mode, codec, encrypted: Boolean(key), dataBytes: dataLength, catalogBytes: compressedCatalog.length };
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
export async function inspectSource(source) {
|
|
518
|
+
const info = await scan(source);
|
|
519
|
+
return { records: info.recordCount, fields: info.fields, groups: info.mapPaths };
|
|
520
|
+
}
|
package/src/cli.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { basename, resolve } from "node:path";
|
|
4
|
+
import { RidaReader, version } from "./index.js";
|
|
5
|
+
import { build, scan } from "./builder.js";
|
|
6
|
+
|
|
7
|
+
const HELP = `RIDA Node.js tools ${version}
|
|
8
|
+
|
|
9
|
+
Usage:
|
|
10
|
+
rida scan <json-file-or-folder> [--records-dir FOLDER] [--id-key FIELD] [--named-ids]
|
|
11
|
+
rida build <json-file-or-folder> [--output FILE] [--mode auto|dense|sparse]
|
|
12
|
+
[--codec deflate-per-field|plain] [--compression 0-9]
|
|
13
|
+
[--encrypt | --key KEY_OR_FILE] [--records-dir FOLDER]
|
|
14
|
+
[--combine-groups] [--combined-schema common|union]
|
|
15
|
+
[--id-key FIELD] [--named-ids]
|
|
16
|
+
rida info <data.rida> [--index FILE] [--map FILE]
|
|
17
|
+
rida get <data.rida|URL> <record-id> <field> [--index FILE] [--map FILE] [--key KEY_OR_FILE]
|
|
18
|
+
rida get-many <data.rida|URL> <record-id> <field>... [--index FILE] [--map FILE] [--key KEY_OR_FILE]
|
|
19
|
+
rida record <data.rida|URL> <record-id> [--index FILE] [--map FILE] [--key KEY_OR_FILE]
|
|
20
|
+
rida verify <data.rida|URL> [--index FILE] [--map FILE] [--key KEY_OR_FILE]
|
|
21
|
+
|
|
22
|
+
Folder input: each visible .json file is one record object. Subfolders are
|
|
23
|
+
numbered into stable record IDs and a companion map is written when needed.
|
|
24
|
+
Single JSON input: an object maps record IDs to records; an array needs --id-key.
|
|
25
|
+
Build writes the data, Index.rida, optional Map.json, and optional Key.rida.
|
|
26
|
+
Existing output files are never overwritten.
|
|
27
|
+
`;
|
|
28
|
+
|
|
29
|
+
const VALUE_OPTIONS = new Set(["--output", "--mode", "--codec", "--compression", "--key", "--id-key", "--records-dir", "--index", "--map"]);
|
|
30
|
+
const FLAGS = new Set(["--encrypt", "--named-ids", "--combine-groups", "--help", "-h"]);
|
|
31
|
+
function parseArgs(args) {
|
|
32
|
+
const positional = [];
|
|
33
|
+
const options = {};
|
|
34
|
+
for (let i = 0; i < args.length; i++) {
|
|
35
|
+
const arg = args[i];
|
|
36
|
+
if (FLAGS.has(arg)) options[arg] = true;
|
|
37
|
+
else if (VALUE_OPTIONS.has(arg)) {
|
|
38
|
+
const value = args[++i];
|
|
39
|
+
if (!value || value.startsWith("--")) throw new Error(`Missing value after ${arg}`);
|
|
40
|
+
options[arg.slice(2).replaceAll("-", "")] = value;
|
|
41
|
+
} else if (arg.startsWith("--")) throw new Error(`Unknown option: ${arg}`);
|
|
42
|
+
else positional.push(arg);
|
|
43
|
+
}
|
|
44
|
+
return { positional, options };
|
|
45
|
+
}
|
|
46
|
+
async function resolveKey(key) {
|
|
47
|
+
if (!key) return undefined;
|
|
48
|
+
try { return await readFile(key, "utf8"); }
|
|
49
|
+
catch { return key; }
|
|
50
|
+
}
|
|
51
|
+
function output(value) { process.stdout.write(`${JSON.stringify(value, null, 2)}\n`); }
|
|
52
|
+
|
|
53
|
+
async function main() {
|
|
54
|
+
const args = process.argv.slice(2);
|
|
55
|
+
if (!args.length || ["--help", "-h", "help"].includes(args[0])) { process.stdout.write(HELP); return; }
|
|
56
|
+
if (["--version", "-v", "version"].includes(args[0])) { process.stdout.write(`${version}\n`); return; }
|
|
57
|
+
const command = args.shift();
|
|
58
|
+
const { positional, options } = parseArgs(args);
|
|
59
|
+
if (command === "scan") {
|
|
60
|
+
if (positional.length !== 1) throw new Error(`Usage: rida scan <json-file-or-folder>\n\n${HELP}`);
|
|
61
|
+
const result = await scan(positional[0], { idKey: options.idkey, recordsDir: options.recordsdir, numbered: !options["--named-ids"], combineGroups: options["--combine-groups"], combinedSchema: options.combinedschema ?? "union" });
|
|
62
|
+
output({ records: result.recordCount, fields: result.fields.length, fieldNames: result.fields, mappedFolders: Object.keys(result.mapPaths).length });
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (command === "build") {
|
|
66
|
+
if (positional.length !== 1) throw new Error(`Usage: rida build <json-file-or-folder> [options]\n\n${HELP}`);
|
|
67
|
+
const source = positional[0];
|
|
68
|
+
const outputPath = options.output ?? `${basename(resolve(source)).replace(/\.json$/i, "")}.rida`;
|
|
69
|
+
const result = await build(source, outputPath, {
|
|
70
|
+
idKey: options.idkey,
|
|
71
|
+
recordsDir: options.recordsdir,
|
|
72
|
+
combineGroups: Boolean(options["--combine-groups"]),
|
|
73
|
+
combinedSchema: options.combinedschema ?? "union",
|
|
74
|
+
numbered: !options["--named-ids"],
|
|
75
|
+
mode: options.mode ?? "auto",
|
|
76
|
+
codec: options.codec ?? "deflate-per-field",
|
|
77
|
+
compression: options.compression ?? 9,
|
|
78
|
+
encrypt: Boolean(options["--encrypt"]),
|
|
79
|
+
key: await resolveKey(options.key),
|
|
80
|
+
});
|
|
81
|
+
output(result);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const minimum = { info: 1, get: 3, "get-many": 3, record: 2, verify: 1 }[command];
|
|
85
|
+
if (!minimum || positional.length < minimum) throw new Error(`Invalid ${command ?? ""} command.\n\n${HELP}`);
|
|
86
|
+
const [data, recordId, ...fields] = positional;
|
|
87
|
+
const reader = await RidaReader.open(data, { index: options.index, map: options.map, key: await resolveKey(options.key) });
|
|
88
|
+
try {
|
|
89
|
+
if (command === "info") output({ format: "RIDA", version, records: reader.recordCount, fields: reader.fields.length, valueCodec: reader.valueCodec, indexMode: reader.mode, encrypted: reader.info.encrypted, dataBytes: reader.info.dataLength, fieldNames: reader.fields });
|
|
90
|
+
else if (command === "get") output(await reader.get(recordId, fields[0]));
|
|
91
|
+
else if (command === "get-many") output(await reader.getMany(recordId, fields));
|
|
92
|
+
else if (command === "record") output(await reader.getRecord(recordId));
|
|
93
|
+
else output({ valid: await reader.verifyData(), dataBytes: reader.info.dataLength });
|
|
94
|
+
} finally { await reader.close(); }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
main().catch((error) => {
|
|
98
|
+
process.stderr.write(`rida: ${error.message}\n`);
|
|
99
|
+
process.exitCode = 1;
|
|
100
|
+
});
|