oxedyne/fe2o3/fe2o3_net/lens/js/test/adapter/preload.mjs
2.3 KiB, 1 run
created by r1870400018:65614, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | // preload.mjs -- runs Daimond's own `debugshare.test.mjs`, unchanged and read-only, against the |
| 2 | // extracted writer core instead of Daimond's `debugshare.js`. |
| 3 | // |
| 4 | // node --import ./test/adapter/preload.mjs <daimond>/www/js/debugshare.test.mjs |
| 5 | // |
| 6 | // Daimond's test reads `debugshare.js` from its own directory and evaluates it as a classic script |
| 7 | // in a simulated tab. This preload answers that one read with `daimond_debugshare.js` (Daimond's |
| 8 | // app layer over the core), after putting the core where that script finds it. Every other file the |
| 9 | // test reads (`stamp.js`, `daimond.js`, `gateway.js`) is Daimond's, untouched. |
| 10 | // |
| 11 | // One block of that test checks that the scrubber was pasted identically into `debugshare.js` and |
| 12 | // `dev/lens.mjs`. With one copy in `redact.js` there is nothing to keep identical, so the two reads |
| 13 | // are both answered from that one block, and those three checks pass by construction; Daimond |
| 14 | // would delete them on adopting the core. |
| 15 | import fs from 'node:fs'; |
| 16 | import { syncBuiltinESMExports } from 'node:module'; |
| 17 | import { dirname, join } from 'node:path'; |
| 18 | import { fileURLToPath } from 'node:url'; |
| 19 | |
| 20 | const HERE = dirname(fileURLToPath(import.meta.url)); |
| 21 | globalThis.__lensCore = await import('../../writer.js'); |
| 22 | |
| 23 | const adapter = fs.readFileSync(join(HERE, 'daimond_debugshare.js'), 'utf8'); |
| 24 | const redact = fs.readFileSync(join(HERE, '..', '..', 'redact.js'), 'utf8'); |
| 25 | const START = '// ── THE CONTENT SCRUBBER (SHARED BLOCK) ──'; |
| 26 | const END = '// ── END OF THE SHARED SCRUBBER BLOCK ──'; |
| 27 | const block = redact.slice(redact.indexOf(START), redact.indexOf(END)); |
| 28 | if (block.indexOf('*/') !== -1) throw new Error('the scrubber block cannot be quoted inside a comment'); |
| 29 | |
| 30 | // The client's copy sits one tab in, being inside an IIFE; the lens copy sits at the margin. |
| 31 | const client = adapter + '\n/* The core\'s scrubber block, quoted for Daimond\'s two-copies check:\n' |
| 32 | + block.split('\n').map((l) => '\t' + l).join('\n') + END + '\n*/\n'; |
| 33 | const lens = '\n' + block + END + '\n'; |
| 34 | |
| 35 | const real = fs.readFileSync; |
| 36 | fs.readFileSync = function (p, ...rest) { |
| 37 | const s = typeof p === 'string' ? p : String((p && p.pathname) || p); |
| 38 | if (/\/www\/js\/debugshare\.js$/.test(s)) return client; |
| 39 | if (/\/dev\/lens\.mjs$/.test(s)) return lens; |
| 40 | return real.call(this, p, ...rest); |
| 41 | }; |
| 42 | syncBuiltinESMExports(); |