Holesail Service Daemon + Bin Client

This commit is contained in:
Raven Scott
2026-04-09 20:42:54 -04:00
parent 3a69906046
commit c8c877f688
168 changed files with 387155 additions and 30713 deletions
@@ -1,12 +1,25 @@
/**
* Preload for `brittle-node` / Node-based booter tests:
* - `bare-type/binding.js` uses Bare-only `require.addon()`.
* - `bare-dns/binding.js` uses Bare-only `require.addon()` (transitive via holesail / DHT).
* - `bare-inspect` pulls the same; brittle → bare-assert → bare-inspect.
*
* Usage: node --require ./scripts/bare-node-test-shim.cjs …
*/
'use strict'
/** `bare-path` and some Holepunch modules expect a global `Bare` (Pear/Bare runtime). */
if (typeof globalThis.Bare === 'undefined') {
globalThis.Bare = {
platform: process.platform,
on(event, fn) {
if (event === 'exit' && typeof fn === 'function' && process.on) {
process.on('exit', fn)
}
}
}
}
const path = require('path')
const Module = require('module')
const { types } = require('node:util')
@@ -170,6 +183,25 @@ module.exports = function inspect(value, opts = {}) {
module.exports.styles = {}
`
/** Minimal stub so `require('bare-dns')` loads under Node (no native addon). */
const bareDnsBindingStub = {
initResolver() {
return 0
},
destroyResolver() {},
resolveTxt(_handle, _hostname, cb) {
if (typeof cb === 'function') {
process.nextTick(() => cb(new Error('bare-dns: stub binding in Node tests')))
}
},
lookup(_hostname, _family, _all, _req, callback) {
if (typeof callback === 'function') {
process.nextTick(() => callback(new Error('bare-dns: stub binding in Node tests'), null))
}
return null
}
}
const origLoad = Module._load
Module._load = function (request, parent, isMain) {
if (
@@ -181,13 +213,65 @@ Module._load = function (request, parent, isMain) {
) {
return bareTypeBinding
}
if (
request === './binding' &&
parent &&
typeof parent.filename === 'string' &&
parent.filename.includes(`${path.sep}bare-dns${path.sep}`) &&
parent.filename.endsWith(`${path.sep}index.js`)
) {
return bareDnsBindingStub
}
return origLoad.apply(this, arguments)
}
const ADDON_STUB_PREFIX =
'if (typeof require.addon !== "function") {\n' +
' require.addon = function bareOsNodeTestAddon() {\n' +
' return new Proxy({}, {\n' +
' get() {\n' +
' return function () { return {} }\n' +
' }\n' +
' })\n' +
' }\n' +
'}\n'
function isBareOsBindingFile(filename) {
if (typeof filename !== 'string') return false
const n = filename.replace(/\\/g, '/')
return n.endsWith('/bare-os/binding.js')
}
function isBareAddonBindingFile(filename, src) {
if (typeof filename !== 'string') return false
if (isBareOsBindingFile(filename)) return false
if (!filename.includes(`${path.sep}node_modules${path.sep}`)) return false
if (!filename.endsWith(`${path.sep}binding.js`)) return false
const s = String(src || '')
.replace(/\/\/[^\n]*/g, '')
.replace(/\/\*[\s\S]*?\*\//g, '')
.trim()
return /^\s*module\.exports\s*=\s*require\.addon\s*\(\s*\)\s*;?\s*$/.test(s)
}
const bareOsBindingNodeShimPath = path.join(
__dirname,
'bare-os-binding-node-shim.cjs'
)
const origCompile = Module.prototype._compile
Module.prototype._compile = function (content, filename) {
if (typeof filename === 'string' && isBareInspectIndex(filename)) {
return origCompile.call(this, BARE_INSPECT_NODE_SOURCE, filename)
}
if (isBareOsBindingFile(filename)) {
const src = `module.exports = require(${JSON.stringify(
bareOsBindingNodeShimPath
)})\n`
return origCompile.call(this, src, filename)
}
if (isBareAddonBindingFile(filename, content)) {
return origCompile.call(this, ADDON_STUB_PREFIX + content, filename)
}
return origCompile.call(this, content, filename)
}