Skip to content

Commit 651a7e5

Browse files
committed
fix(environment): lazy-load bun lockfile parser
Changed @socketregistry/hyrious__bun.lockb import from module-load time to runtime lazy-loading. The parser is now only loaded when actually parsing a bun.lockb file, not when the module loads. This prevents MODULE_NOT_FOUND errors in environments where the parser isn't needed (e.g., when using npm/pnpm/yarn lockfiles). Fixes issue where socketbin packages failed to run because the bun lockfile parser was unnecessarily required at module load time.
1 parent a6a353a commit 651a7e5

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

packages/cli/src/utils/ecosystem/environment.mts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,20 @@ import browserslist from 'browserslist'
3232
import semver from 'semver'
3333

3434
const require = createRequire(import.meta.url)
35-
// Type definition is incorrect - exports object with parse method, not namespace.
36-
const { parse: parseBunLockb } =
37-
require('@socketregistry/hyrious__bun.lockb/index.cjs') as {
38-
parse: (buf: Uint8Array | ArrayBuffer) => string
35+
36+
// Lazy-load Bun lockfile parser only when needed.
37+
let parseBunLockb: ((buf: Uint8Array | ArrayBuffer) => string) | undefined
38+
39+
function getParseBunLockb() {
40+
if (!parseBunLockb) {
41+
// Type definition is incorrect - exports object with parse method, not namespace.
42+
const parser = require('@socketregistry/hyrious__bun.lockb/index.cjs') as {
43+
parse: (buf: Uint8Array | ArrayBuffer) => string
44+
}
45+
parseBunLockb = parser.parse
3946
}
47+
return parseBunLockb
48+
}
4049

4150
import { whichBin } from '@socketsecurity/lib/bin'
4251
import {
@@ -211,7 +220,7 @@ const readLockFileByAgent: Map<Agent, ReadLockFile> = (() => {
211220
const lockBuffer = await binaryReader(lockPath)
212221
if (lockBuffer) {
213222
try {
214-
return parseBunLockb(lockBuffer)
223+
return getParseBunLockb()(lockBuffer)
215224
} catch {}
216225
}
217226
// To print a Yarn lockfile to your console without writing it to disk

0 commit comments

Comments
 (0)