-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.ts
More file actions
73 lines (68 loc) · 2.75 KB
/
path.ts
File metadata and controls
73 lines (68 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* @file Lazy-loader for socket-btm's `node:smol-path` — native fast paths for
* the hot path-string primitives (`dirname`, `normalize`, …) and, per the
* socket-btm `node-smol-path` Phase 4 plan, batched filesystem ops (`access`,
* an in-C++ `findUp`). Returns `undefined` on stock Node, non-Node runtimes,
* and on socket-btm binaries that haven't shipped the binding yet; callers
* fall back to the JS implementation. Result is cached. The binding does not
* exist yet (the plan is unbuilt) — this accessor is the seam so that when it
* lands, only this file changes and `paths/walk`, `fs/access`, `fs/find-up`
* light up natively. Today `getSmolPath()` is always `undefined` and the JS
* paths run.
*/
import { isNodeBuiltin } from '../node/module'
import type { PathLike } from 'node:fs'
/**
* Native path / filesystem fast-path surface. Only the operations socket-lib's
* helpers shim are typed; the binding may expose more. Every method is optional
* so a partial rollout (e.g. `dirname` ships before `access`) still type-checks
* at the shim sites.
*/
export interface SmolPathBinding {
/**
* `path.dirname` over the one-byte Fast API. ASCII fast path; two-byte inputs
* route to the equivalent of `path.dirname`.
*/
dirname?: ((p: string) => string) | undefined
/**
* `path.normalize` over the one-byte Fast API.
*/
normalize?: ((p: string) => string) | undefined
/**
* `fs.accessSync`-equivalent returning a boolean instead of throwing — skips
* the V8 error-object materialization the JS wrapper pays on every negative
* check. `mode` is an `fs.constants` bit.
*/
access?: ((path: PathLike, mode?: number | undefined) => boolean) | undefined
/**
* In-C++ find-up: walk `startDir`'s ancestors, return the first dir
* containing any of `names` (as a file unless `onlyDirectories`), or
* `undefined`. Collapses the N JS↔native crossings of the JS walk into one.
*/
findUp?:
| ((
startDir: string,
names: readonly string[],
options?: { onlyDirectories?: boolean | undefined } | undefined,
) => string | undefined)
| undefined
}
let smolPathCache: SmolPathBinding | undefined
let smolPathProbed = false
/**
* Returns the `node:smol-path` binding when running on a smol Node binary that
* ships it; otherwise `undefined`. Cached across calls.
*
* @returns The native binding, or `undefined` to signal "use the JS fallback".
*/
export function getSmolPath(): SmolPathBinding | undefined {
if (!smolPathProbed) {
smolPathProbed = true
/* c8 ignore start - smol Node binary only. */
if (isNodeBuiltin('node:smol-path')) {
smolPathCache = require('node:smol-path') as SmolPathBinding
}
/* c8 ignore stop */
}
return smolPathCache
}