/**
* Postinstall script to patch lib0 for Bare runtime compatibility.
*
* lib0 (a dependency of yjs) uses conditional exports that resolve to
* `node:crypto` and `node:perf_hooks` under the `node` condition. Bare
* runtime includes the `node` condition but doesn't provide these builtins
* natively — they must be mapped via import maps to bare-crypto and
* bare-performance.
*
* This script adds the necessary import map to lib0's package.json so that
* Bare resolves `node:crypto` -> `bare-crypto` and `node:perf_hooks` ->
* `bare-performance` within lib0's module scope.
*/
// Using CJS-style dynamic import since this runs under Node.js (npm postinstall)
// but package.json has "type": "module"
import { readFileSync, writeFileSync, existsSync } from 'node:fs'
import { join } from 'node:path'
const lib0PkgPath = join(new URL('.', import.meta.url).pathname, '..', 'node_modules', 'lib0', 'package.json')
if (!existsSync(lib0PkgPath)) {
console.log('patch-lib0: lib0 not found, skipping')
process.exit(0)
}
const pkg = JSON.parse(readFileSync(lib0PkgPath, 'utf8'))
const needsPatch = !pkg.imports || !pkg.imports['node:crypto']
if (!needsPatch) {
console.log('patch-lib0: already patched, skipping')
process.exit(0)
}
pkg.imports = Object.assign(pkg.imports || {}, {
'node:crypto': {
bare: 'bare-crypto',
default: 'node:crypto'
},
'node:perf_hooks': {
bare: 'bare-performance',
default: 'node:perf_hooks'
}
})
writeFileSync(lib0PkgPath, JSON.stringify(pkg, null, 2) + '\n')
console.log('patch-lib0: patched lib0/package.json for Bare runtime compatibility')