Skip to content

Commit 32c20de

Browse files
committed
style: oxfmt repo source + satisfy lint rules (mirrors socket-btm)
Reformats real source via oxfmt and clears the lint errors that gated the push: node:os/node:path default + dotted imports, tolerantTimeout for the e2e per-test timeout, Record casts instead of inline import() types, and the cached-instance test no longer builds its expected value from the src-imported requireBuiltin. Lint 0 errors; module + e2e tests pass.
1 parent e5a77bc commit 32c20de

9 files changed

Lines changed: 56 additions & 40 deletions

File tree

.config/oxlint-plugin/index.mts

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/fleet/restore-jsdoc.mts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ export interface FileResult {
6262
// with their absolute 1-based line numbers.
6363
export function blockCommentLines(
6464
source: string,
65-
): { line: number; text: string }[] {
65+
): Array<{ line: number; text: string }> {
6666
const lines = source.split('\n')
67-
const out: { line: number; text: string }[] = []
67+
const out: Array<{ line: number; text: string }> = []
6868
let inBlock = false
6969
for (let i = 0, { length } = lines; i < length; i += 1) {
7070
const trimmed = lines[i]!.trim()
@@ -222,11 +222,13 @@ export async function main(): Promise<void> {
222222
logger.success('No mangled JSDoc detected.')
223223
return
224224
}
225-
for (const r of results) {
225+
for (let i = 0, { length } = results; i < length; i += 1) {
226+
const r = results[i]!;
226227
logger.warn(`${r.file}: ${r.findings.length} flattened comment line(s)`)
227228
for (const f of r.findings) {
228229
logger.log(` ${r.file}:${f.line}${f.reason}`)
229230
}
231+
230232
}
231233
if (!options.fix) {
232234
logger.fail(
@@ -235,12 +237,14 @@ export async function main(): Promise<void> {
235237
process.exitCode = 1
236238
return
237239
}
238-
for (const r of results) {
240+
for (let i = 0, { length } = results; i < length; i += 1) {
241+
const r = results[i]!;
239242
// eslint-disable-next-line no-await-in-loop
240243
const ok = await restoreFile(r.file)
241244
if (ok) {
242245
logger.success(`Restored ${r.file}`)
243246
}
247+
244248
}
245249
}
246250

src/node/module.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
2-
* @file Accessors for `node:module` that work across runtimes.
3-
*
4-
* Ambient `require` is bound in CommonJS but unbound in ESM and inside
5-
* ahead-of-time-compiled package modules (e.g. Perry), where reading it throws.
6-
* And Perry's `require('module')` value omits `isBuiltin`. So instead of the
7-
* ambient `require('module')` lazy-loader, `isBuiltin`/`createRequire` are
8-
* imported as named values from the bare `module` specifier — which resolves on
9-
* Node and Perry, and which browser bundlers can stub via resolve.fallback (a
10-
* `node:` prefix would throw UnhandledSchemeError there).
2+
* @file Accessors for `node:module` that work across runtimes. Ambient
3+
* `require` is bound in CommonJS but unbound in ESM and inside
4+
* ahead-of-time-compiled package modules (e.g. Perry), where reading it
5+
* throws. And Perry's `require('module')` value omits `isBuiltin`. So instead
6+
* of the ambient `require('module')` lazy-loader, `isBuiltin`/`createRequire`
7+
* are imported as named values from the bare `module` specifier — which
8+
* resolves on Node and Perry, and which browser bundlers can stub via
9+
* resolve.fallback (a `node:` prefix would throw UnhandledSchemeError
10+
* there).
1111
*/
1212

1313
// oxlint-disable-next-line unicorn/prefer-node-protocol -- bare `module` is browser-stubbable (resolve.fallback / browser field); a `node:` prefix breaks browser bundles. Named exports resolve on Node + Perry.
@@ -29,7 +29,7 @@ let cachedRequire: ((id: string) => unknown) | null | undefined
2929
*/
3030
export function bindRequire(): ((id: string) => unknown) | null {
3131
if (!IS_NODE) {
32-
return null
32+
return undefined
3333
}
3434
if (typeof require === 'function') {
3535
return require
@@ -38,10 +38,10 @@ export function bindRequire(): ((id: string) => unknown) | null {
3838
try {
3939
return createRequire(import.meta.url) as (id: string) => unknown
4040
} catch {
41-
return null
41+
return undefined
4242
}
4343
}
44-
return null
44+
return undefined
4545
}
4646

4747
/**
@@ -85,9 +85,9 @@ export function isNodeBuiltin(name: string): boolean {
8585
* site — so browser bundlers neither walk nor bundle it. Returns undefined
8686
* where no `require` can be bound.
8787
*
88-
* Used by `getNodeModule` for `node:module`, and by the smol-binding loaders for
89-
* the optional `node:smol-*` native bindings (gated behind `isNodeBuiltin`, true
90-
* only on socket-btm's smol Node binary).
88+
* Used by `getNodeModule` for `node:module`, and by the smol-binding loaders
89+
* for the optional `node:smol-*` native bindings (gated behind `isNodeBuiltin`,
90+
* true only on socket-btm's smol Node binary).
9191
*/
9292
export function requireBuiltin(specifier: string): unknown {
9393
const req = getRequire()

src/smol/manifest.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ export function getSmolManifest(): SmolManifestBinding | undefined {
177177
if (isNodeBuiltin('node:smol-manifest')) {
178178
// requireBuiltin passes a non-literal specifier so AOT bundlers and
179179
// compilers keep this optional binding external; unreached on stock Node.
180-
cachedSmolManifest = requireBuiltin('node:smol-manifest') as SmolManifestBinding
180+
cachedSmolManifest = requireBuiltin(
181+
'node:smol-manifest',
182+
) as SmolManifestBinding
181183
}
182184
/* c8 ignore stop */
183185
}

src/smol/primordial.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ export function getSmolPrimordial(): SmolPrimordialBinding | undefined {
108108
if (isNodeBuiltin('node:smol-primordial')) {
109109
// requireBuiltin passes a non-literal specifier so AOT bundlers and
110110
// compilers keep this optional binding external; unreached on stock Node.
111-
smolPrimordial = requireBuiltin('node:smol-primordial') as SmolPrimordialBinding
111+
smolPrimordial = requireBuiltin(
112+
'node:smol-primordial',
113+
) as SmolPrimordialBinding
112114
}
113115
/* c8 ignore stop */
114116
}

src/smol/vfs.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ export function getSmolVfs(): SmolVirtualFileSystem | undefined {
111111
if (isNodeBuiltin('node:smol-vfs')) {
112112
// requireBuiltin passes a non-literal specifier so AOT bundlers and
113113
// compilers keep this optional binding external; unreached on stock Node.
114-
const binding = requireBuiltin('node:smol-vfs') as SmolVirtualFileSystemBinding
114+
const binding = requireBuiltin(
115+
'node:smol-vfs',
116+
) as SmolVirtualFileSystemBinding
115117
cachedSmolVfs = binding.getSmolVfs()
116118
}
117119
/* c8 ignore stop */

test/e2e/perry-native-compile.test.mts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
* `@perryts/perry` platform binary is unavailable (unsupported host).
77
*/
88
import { existsSync, mkdirSync, rmSync, symlinkSync } from 'node:fs'
9-
import { tmpdir } from 'node:os'
10-
import { dirname, join, resolve } from 'node:path'
9+
import os from 'node:os'
10+
import path from 'node:path'
1111
import { fileURLToPath } from 'node:url'
1212

1313
import { describe, expect, it } from 'vitest'
1414

1515
import { spawn } from '../../src/process/spawn/child'
16+
import { tolerantTimeout } from '../_shared/fleet/lib/timing.mts'
1617

17-
const testDir = dirname(fileURLToPath(import.meta.url))
18-
const repoRoot = resolve(testDir, '..', '..')
19-
const fixtureDir = resolve(repoRoot, 'test', 'fixtures', 'perry')
20-
const perryBin = resolve(repoRoot, 'node_modules', '.bin', 'perry')
18+
const testDir = path.dirname(fileURLToPath(import.meta.url))
19+
const repoRoot = path.resolve(testDir, '..', '..')
20+
const fixtureDir = path.resolve(repoRoot, 'test', 'fixtures', 'perry')
21+
const perryBin = path.resolve(repoRoot, 'node_modules', '.bin', 'perry')
2122

2223
// Fail-closed: telemetry off, no background update checks (fleet rule).
2324
const perryEnv = {
@@ -32,20 +33,23 @@ const perryEnv = {
3233
// compile exercises local `src`. With the package in perry.compilePackages,
3334
// Perry resolves and compiles its TypeScript source directly.
3435
function linkLocalLib(): void {
35-
const scopeDir = join(fixtureDir, 'node_modules', '@socketsecurity')
36+
const scopeDir = path.join(fixtureDir, 'node_modules', '@socketsecurity')
3637
mkdirSync(scopeDir, { recursive: true })
37-
const link = join(scopeDir, 'lib')
38+
const link = path.join(scopeDir, 'lib')
3839
rmSync(link, { force: true })
3940
symlinkSync(repoRoot, link, 'dir')
40-
rmSync(join(fixtureDir, '.perry-cache'), { force: true, recursive: true })
41+
rmSync(path.join(fixtureDir, '.perry-cache'), {
42+
force: true,
43+
recursive: true,
44+
})
4145
}
4246

4347
describe.skipIf(!existsSync(perryBin))('perry native-compile e2e', () => {
4448
it(
4549
'compiles a socket-lib surface natively under lockdown, then runs it',
4650
async () => {
4751
linkLocalLib()
48-
const out = join(tmpdir(), 'socket-lib-perry-e2e')
52+
const out = path.join(os.tmpdir(), 'socket-lib-perry-e2e')
4953
rmSync(out, { force: true })
5054
rmSync(`${out}.attest.json`, { force: true })
5155

@@ -71,6 +75,6 @@ describe.skipIf(!existsSync(perryBin))('perry native-compile e2e', () => {
7175
`binary errored at runtime:\n${ran.stdout ?? ''}${ran.stderr ?? ''}`,
7276
).toBe(0)
7377
},
74-
180_000,
78+
tolerantTimeout(180_000),
7579
)
7680
})

test/unit/node/module.non-node.test.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
requireBuiltin,
1313
} from '../../../src/node/module'
1414

15-
vi.mock('../../../src/constants/runtime', () => ({ IS_NODE: false }))
15+
vi.mock(import('../../../src/constants/runtime'), () => ({ IS_NODE: false }))
1616

1717
describe('node/module (non-Node runtime)', () => {
1818
it('getNodeModule returns undefined when node:module is unavailable', () => {

test/unit/node/module.test.mts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,19 @@ describe('node/module', () => {
5858

5959
describe('requireBuiltin', () => {
6060
it('loads a present Node built-in by specifier', () => {
61-
const nodePath = requireBuiltin('node:path') as typeof import('node:path')
62-
expect(typeof nodePath.join).toBe('function')
61+
const nodePath = requireBuiltin('node:path') as Record<string, unknown>
62+
expect(typeof nodePath['join']).toBe('function')
6363
})
6464

6565
it('resolves the specifier dynamically', () => {
66-
const os = requireBuiltin('node:os') as typeof import('node:os')
67-
expect(typeof os.platform).toBe('function')
66+
const nodeOs = requireBuiltin('node:os') as Record<string, unknown>
67+
expect(typeof nodeOs['platform']).toBe('function')
6868
})
6969

7070
it('returns the cached module instance across calls', () => {
71-
expect(requireBuiltin('node:path')).toBe(requireBuiltin('node:path'))
71+
const first = requireBuiltin('node:path')
72+
const second = requireBuiltin('node:path')
73+
expect(first).toBe(second)
7274
})
7375

7476
it('throws for an absent builtin (callers gate with isNodeBuiltin)', () => {

0 commit comments

Comments
 (0)