components
All repositories: gitoria
5.7 KB
#!/usr/bin/env node// Usage: node check-index-keys.mjs <directory>//// READ ONLY. For every <name>.mpack in the directory (not below it) this lists the live// records whose indexed text field holds a comma, a newline, a carriage return or starts with// a double quote, and says which of them are MISSING from that field's index file// <name>.<field>.txt (mission 486: the engine before the fix dropped such entries from the// index when it was reopened). The indexed fields are the <name>.<field>.txt files beside// the .mpack; nothing is written, locked or created. Exit status 1 when any entry is missing.//// A missing entry is repaired by opening the table with the fixed hl:mpackdb: an index whose// idxstate carries no "idxFmt":2 mark is rebuilt from the data once, the data itself holds// every record.import { readFileSync, readdirSync, existsSync } from 'node:fs'import { join } from 'node:path'const dir = process.argv[2]if (!dir) {console.error('usage: node check-index-keys.mjs <directory>')process.exit(2)}// --- a small MessagePack reader (the subset the engine writes: maps, arrays, strings, numbers)function decode(buf, start) {let p = startconst val = () => {const b = buf[p++]if (b <= 0x7f) return bif (b >= 0xe0) return b - 256if ((b & 0xe0) === 0xa0) return str(b & 0x1f)if ((b & 0xf0) === 0x80) return map(b & 0x0f)if ((b & 0xf0) === 0x90) return arr(b & 0x0f)switch (b) {case 0xc0: return nullcase 0xc2: return falsecase 0xc3: return truecase 0xc4: return bin(buf[p++])case 0xc5: { const n = buf.readUInt16BE(p); p += 2; return bin(n) }case 0xc6: { const n = buf.readUInt32BE(p); p += 4; return bin(n) }case 0xca: { const v = buf.readFloatBE(p); p += 4; return v }case 0xcb: { const v = buf.readDoubleBE(p); p += 8; return v }case 0xcc: return buf[p++]case 0xcd: { const v = buf.readUInt16BE(p); p += 2; return v }case 0xce: { const v = buf.readUInt32BE(p); p += 4; return v }case 0xcf: { const v = Number(buf.readBigUInt64BE(p)); p += 8; return v }case 0xd0: return buf.readInt8(p++)case 0xd1: { const v = buf.readInt16BE(p); p += 2; return v }case 0xd2: { const v = buf.readInt32BE(p); p += 4; return v }case 0xd3: { const v = Number(buf.readBigInt64BE(p)); p += 8; return v }case 0xd9: return str(buf[p++])case 0xda: { const n = buf.readUInt16BE(p); p += 2; return str(n) }case 0xdb: { const n = buf.readUInt32BE(p); p += 4; return str(n) }case 0xdc: { const n = buf.readUInt16BE(p); p += 2; return arr(n) }case 0xdd: { const n = buf.readUInt32BE(p); p += 4; return arr(n) }case 0xde: { const n = buf.readUInt16BE(p); p += 2; return map(n) }case 0xdf: { const n = buf.readUInt32BE(p); p += 4; return map(n) }default: throw new Error('unsupported msgpack byte 0x' + b.toString(16))}}const str = (n) => { const s = buf.toString('utf8', p, p + n); p += n; return s }const bin = (n) => { p += n; return null }const arr = (n) => { const a = []; for (let i = 0; i < n; i++) a.push(val()); return a }const map = (n) => { const o = {}; for (let i = 0; i < n; i++) { const k = val(); o[k] = val() } return o }return val()}// --- an index file: the offsets it names (a quoted key is a JSON string, else split from the right)function indexOffsets(text) {const offsets = new Set()for (const line of text.split('\n')) {if (!line) continueif (line[0] === '"') {let rest = nullfor (let i = 1; i < line.length; i++) {if (line[i] === '\\') { i++; continue }if (line[i] === '"') { rest = line.slice(i + 1); break }}if (rest !== null && /^,\d+,\d+$/.test(rest)) { offsets.add(Number(rest.split(',')[1])); continue }}const c2 = line.lastIndexOf(',')const c1 = c2 > 0 ? line.lastIndexOf(',', c2 - 1) : -1if (c1 < 0) continueconst off = Number(line.slice(c1 + 1, c2))if (Number.isInteger(off)) offsets.add(off)}return offsets}const risky = (v) => typeof v === 'string' && (/[,\n\r]/.test(v) || v[0] === '"')let missingTotal = 0const all = readdirSync(dir)const names = all.filter((f) => f.endsWith('.mpack')).map((f) => f.slice(0, -'.mpack'.length)).sort()for (const name of names) {const data = readFileSync(join(dir, name + '.mpack'))let deleted = new Set()const metaPath = join(dir, name + '.meta.json')if (existsSync(metaPath)) {try { deleted = new Set(JSON.parse(readFileSync(metaPath, 'utf8')).deleted || []) } catch { /* no usable meta */ }}const fields = all.filter((f) => f.startsWith(name + '.') && f.endsWith('.txt')).map((f) => f.slice(name.length + 1, -4)).filter((f) => f && !f.includes('.'))const indexes = new Map(fields.map((f) => [f, indexOffsets(readFileSync(join(dir, name + '.' + f + '.txt'), 'utf8'))]))let live = 0, riskyCount = 0, missing = 0for (let pos = 0; pos + 4 <= data.length;) {const size = data.readUInt32LE(pos)if (size < 5 || pos + size > data.length) { console.log(`${name}.mpack: record frame at ${pos} is damaged or cut short; stopped there`); break }if (!deleted.has(pos)) {live++let rec = nulltry { rec = decode(data, pos + 4) } catch (e) { console.log(`${name}.mpack: record at ${pos} cannot be read: ${e.message}`) }if (rec && typeof rec === 'object') {for (const [field, offsets] of indexes) {if (!risky(rec[field])) continueriskyCount++const lost = !offsets.has(pos)if (lost) { missing++; missingTotal++ }console.log(`${name}.${field}: record at offset ${pos} key ${JSON.stringify(rec[field])} ${lost ? 'MISSING from the index file' : 'present'}`)}}}pos += size}console.log(`${name}: ${live} live records, indexed fields [${fields.join(', ')}], ${riskyCount} with a comma/newline/CR/leading quote, ${missing} missing from their index`)}if (!names.length) console.log('no .mpack files in ' + dir)process.exit(missingTotal ? 1 : 0)
Branches
Latest commits
- 7e428ba6Re-vendor Hybriel day72 (day73 withdrawn: minifier breaks write sets)Atlas
- a06fc778Reset virtual-list window when the host changes resetKeyAtlas
- 3aab598cRe-vendor complete Hybriel day73 set for virtual-list resetAtlas
- 337ca075MD1 gate check measures frames in the page, not milliseconds from outside, and takes the middle load (components#30)mre
- f5f131c5Record AP29 autoplay comparisons and passing gatesmre
- 51c38187Gate media autoplay on first load and after client mountmre
- a3f2223dRecord successful B44 deployment and live proofmre
- 06c1e021Record passing B44 browser rerunmre
- c9861533Record B44 component proof and browser gate blockermre
- 1b7413beIgnore detached media events and space demo option outputsmre
- ca3b4959Re-vendor complete Hybriel plugins and runtime to day44mre
- df9e7df9Last-item checks run in the browser gate (components#26 fixed on day41); media-player loop is a plain Booleanmre
- 895a5afcRe-vendor complete Hybriel plugins and runtime to day41mre
- 16006bb3Backup tar: --hard-dereference, so mpackdb sidecars are never stored as hard links (Tracker's finding)mre
- 88397fc3Re-vendor complete Hybriel plugins and runtime to day39mre
- 9b872cedRecord IC28 deployment and live icon verificationmre
- 46fefaa7Count the icons entry in the offline home-page checkmre
- c7e14cabRecord IC28 rerun and unchanged-main baseline comparisonmre
- 5e090ffdRecord IC28 review evidence and gate resultsmre
- adfe2c4dAdd shuffle, repeat and repeatOne shared iconsmre