AtlatestRepositorysigil-quic
sigil-quic / tree / scriptsinject-construction-spy.mjs
1
// Remote-only constructor observations and faults at later operations.2
// Instrument baseline/fixed copies identically; never rewrite shipped sources.3
import { readFileSync, writeFileSync } from 'node:fs';5
const [kind, input, output] = process.argv.slice(2);6
if (!['handshake', 'connection'].includes(kind) || !input || !output || input === output) {7
throw new Error('Usage: inject-construction-spy.mjs handshake|connection INPUT OUTPUT');8
}9
let source = readFileSync(input, 'utf8');10
if (source.includes('construction-spy')) throw new Error('Already instrumented');11
function replaceOnce(text, from, to) {12
if (text.split(from).length !== 2) throw new Error('Constructor site changed: ' + from);13
return text.replace(from, () => to);14
}15
if (kind === 'handshake') {16
const start = source.indexOf(' (define (make-tls-server protocols parameters chain private-key)');17
const end = source.indexOf(' ;; CID expectations originate', start);18
if (start < 0 || end < start) throw new Error('TLS server boundary changed');19
let body = source.slice(start, end);20
for (const [from, to] of [21
['(bytevector-copy private-key)', '(construction-spy-key-copy private-key)'],22
['(make-tls-transcript)', '(construction-spy-transcript)'],23
['(make-bytevector 4 0)', '(construction-spy-buffer 4 0)'],24
]) body = replaceOnce(body, from, to);25
source = source.slice(0, start) + body + source.slice(end);26
} else {27
for (const [from, to] of [28
['(initial-keys (quic-connection-original-id connection) peer)',29
"(construction-spy-initial (quic-role connection) 'read (quic-connection-original-id connection) peer)"],30
['(initial-keys (quic-connection-original-id connection) local)',31
"(construction-spy-initial (quic-role connection) 'write (quic-connection-original-id connection) local)"],32
['(make-tls-client protocols name', '(construction-spy-client make-tls-client protocols name'],33
['(make-tls-server protocols (parameters-with-ids', '(construction-spy-server make-tls-server protocols (parameters-with-ids'],34
['(tls-bind-peer! (quic-connection-tls connection) original-id peer-id #f)',35
'(construction-spy-bind tls-bind-peer! (quic-connection-tls connection) original-id peer-id #f)'],36
]) source = replaceOnce(source, from, to);37
}38
source = replaceOnce(source, '(import (sigil crypto)',39
'(import (sigil quic tests construction spy)\n (sigil crypto)');40
writeFileSync(output,41
';;; TEST ONLY: observed construction ownership; never ship this variant.\n' + source);