Skip to content

Commit 9613a97

Browse files
committed
docs(dlx): add @example blocks to dlx module exports
1 parent d85cbb6 commit 9613a97

8 files changed

Lines changed: 272 additions & 0 deletions

File tree

src/dlx/binary.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,15 @@ export interface DlxMetadata {
191191

192192
/**
193193
* Clean expired entries from the DLX cache.
194+
*
195+
* @example
196+
* ```typescript
197+
* // Remove cache entries older than the default TTL
198+
* const removed = await cleanDlxCache()
199+
*
200+
* // Remove entries older than 1 hour
201+
* const removed2 = await cleanDlxCache(60 * 60 * 1000)
202+
* ```
194203
*/
195204
export async function cleanDlxCache(
196205
maxAge: number = DLX_BINARY_CACHE_TTL,
@@ -260,6 +269,15 @@ export async function cleanDlxCache(
260269

261270
/**
262271
* Download and execute a binary from a URL with caching.
272+
*
273+
* @example
274+
* ```typescript
275+
* const result = await dlxBinary(['--version'], {
276+
* url: 'https://example.com/tool-linux-x64',
277+
* name: 'tool',
278+
* })
279+
* await result.spawnPromise
280+
* ```
263281
*/
264282
export async function dlxBinary(
265283
args: readonly string[] | string[],
@@ -417,6 +435,15 @@ export async function dlxBinary(
417435
* Similar to downloadPackage from dlx-package.
418436
*
419437
* @returns Object containing the path to the cached binary and whether it was downloaded
438+
*
439+
* @example
440+
* ```typescript
441+
* const { binaryPath, downloaded } = await downloadBinary({
442+
* url: 'https://example.com/tool-linux-x64',
443+
* name: 'tool',
444+
* })
445+
* console.log(`Binary at: ${binaryPath}, fresh: ${downloaded}`)
446+
* ```
420447
*/
421448
export async function downloadBinary(
422449
options: Omit<DlxBinaryOptions, 'spawnOptions'>,
@@ -512,6 +539,15 @@ export async function downloadBinary(
512539
* - integrity: SRI format sha512-<base64> (verified post-download)
513540
*
514541
* The sha256 option is preferred as it fails early during download if the checksum doesn't match.
542+
*
543+
* @example
544+
* ```typescript
545+
* const integrity = await downloadBinaryFile(
546+
* 'https://example.com/tool-linux-x64',
547+
* '/tmp/dlx-cache/tool'
548+
* )
549+
* console.log(`Integrity: ${integrity}`)
550+
* ```
515551
*/
516552
export async function downloadBinaryFile(
517553
url: string,
@@ -608,6 +644,15 @@ export async function downloadBinaryFile(
608644
* @param spawnOptions Spawn options for execution
609645
* @param spawnExtra Extra spawn configuration
610646
* @returns The spawn promise for the running process
647+
*
648+
* @example
649+
* ```typescript
650+
* const { binaryPath } = await downloadBinary({
651+
* url: 'https://example.com/tool-linux-x64',
652+
* name: 'tool',
653+
* })
654+
* const result = executeBinary(binaryPath, ['--help'])
655+
* ```
611656
*/
612657
export function executeBinary(
613658
binaryPath: string,
@@ -647,20 +692,40 @@ export function executeBinary(
647692
* Get the DLX binary cache directory path.
648693
* Returns normalized path for cross-platform compatibility.
649694
* Uses same directory as dlx-package for unified DLX storage.
695+
*
696+
* @example
697+
* ```typescript
698+
* const cachePath = getDlxCachePath()
699+
* ```
650700
*/
651701
export function getDlxCachePath(): string {
652702
return getSocketDlxDir()
653703
}
654704

655705
/**
656706
* Get metadata file path for a cached binary.
707+
*
708+
* @example
709+
* ```typescript
710+
* const metaPath = getBinaryCacheMetadataPath('/tmp/dlx-cache/a1b2c3d4')
711+
* // '/tmp/dlx-cache/a1b2c3d4/.dlx-metadata.json'
712+
* ```
657713
*/
658714
export function getBinaryCacheMetadataPath(cacheEntryPath: string): string {
659715
return getPath().join(cacheEntryPath, '.dlx-metadata.json')
660716
}
661717

662718
/**
663719
* Check if a cached binary is still valid.
720+
*
721+
* @example
722+
* ```typescript
723+
* const ttl = 7 * 24 * 60 * 60 * 1000
724+
* const valid = await isBinaryCacheValid('/tmp/dlx-cache/a1b2c3d4', ttl)
725+
* if (!valid) {
726+
* // Re-download the binary
727+
* }
728+
* ```
664729
*/
665730
export async function isBinaryCacheValid(
666731
cacheEntryPath: string,
@@ -696,6 +761,14 @@ export async function isBinaryCacheValid(
696761

697762
/**
698763
* Get information about cached binaries.
764+
*
765+
* @example
766+
* ```typescript
767+
* const entries = await listDlxCache()
768+
* for (const entry of entries) {
769+
* console.log(`${entry.name}: ${entry.size} bytes`)
770+
* }
771+
* ```
699772
*/
700773
export async function listDlxCache(): Promise<
701774
Array<{
@@ -773,6 +846,17 @@ export async function listDlxCache(): Promise<
773846
* Write metadata for a cached binary.
774847
* Uses unified schema shared with C++ decompressor and CLI dlxBinary.
775848
* Schema documentation: See DlxMetadata interface in this file (exported).
849+
*
850+
* @example
851+
* ```typescript
852+
* await writeBinaryCacheMetadata(
853+
* '/tmp/dlx-cache/a1b2c3d4',
854+
* 'a1b2c3d4',
855+
* 'https://example.com/tool',
856+
* 'sha512-abc123...',
857+
* 15000000
858+
* )
859+
* ```
776860
*/
777861
export async function writeBinaryCacheMetadata(
778862
cacheEntryPath: string,

src/dlx/cache.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ function getCrypto() {
3838
* https://github.com/npm/cli/blob/v11.6.2/workspaces/libnpmexec/lib/index.js#L233-L244
3939
* Implementation: packages.map().sort().join('\n') → SHA-512 → slice(0,16)
4040
* npx hashes the package spec (name@version), not just name
41+
*
42+
* @example
43+
* ```typescript
44+
* const key = generateCacheKey('prettier@3.0.0')
45+
* // e.g. 'a1b2c3d4e5f67890'
46+
* ```
4147
*/
4248
export function generateCacheKey(spec: string): string {
4349
const crypto = getCrypto()

src/dlx/detect.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ function readPackageJson(packageJsonPath: string): object | null {
144144
*
145145
* @param filePath - Path within DLX cache (~/.socket/_dlx/)
146146
* @returns Detection result
147+
*
148+
* @example
149+
* ```typescript
150+
* const result = detectDlxExecutableType('/tmp/.socket/_dlx/a1b2c3d4/tool')
151+
* console.log(result.type) // 'package' or 'binary'
152+
* ```
147153
*/
148154
export function detectDlxExecutableType(
149155
filePath: string,
@@ -210,6 +216,14 @@ export function detectExecutableType(
210216
*
211217
* @param filePath - Local filesystem path (not in DLX cache)
212218
* @returns Detection result
219+
*
220+
* @example
221+
* ```typescript
222+
* const result = detectLocalExecutableType('/usr/local/bin/tool')
223+
* if (result.type === 'package') {
224+
* console.log('Node.js package at:', result.packageJsonPath)
225+
* }
226+
* ```
213227
*/
214228
export function detectLocalExecutableType(
215229
filePath: string,
@@ -253,6 +267,13 @@ export function detectLocalExecutableType(
253267
*
254268
* @param filePath - Path to check
255269
* @returns True if file has .js, .mjs, or .cjs extension
270+
*
271+
* @example
272+
* ```typescript
273+
* isJsFilePath('index.js') // true
274+
* isJsFilePath('lib.mjs') // true
275+
* isJsFilePath('tool.exe') // false
276+
* ```
256277
*/
257278
export function isJsFilePath(filePath: string): boolean {
258279
const path = getPath()
@@ -265,6 +286,12 @@ export function isJsFilePath(filePath: string): boolean {
265286
*
266287
* @param filePath - Path to check
267288
* @returns True if detected as native binary (not Node.js package)
289+
*
290+
* @example
291+
* ```typescript
292+
* isNativeBinary('/usr/local/bin/tool') // true
293+
* isNativeBinary('/tmp/project/index.js') // false
294+
* ```
268295
*/
269296
export function isNativeBinary(filePath: string): boolean {
270297
return detectExecutableType(filePath).type === 'binary'
@@ -275,6 +302,12 @@ export function isNativeBinary(filePath: string): boolean {
275302
*
276303
* @param filePath - Path to check
277304
* @returns True if detected as Node.js package
305+
*
306+
* @example
307+
* ```typescript
308+
* isNodePackage('/tmp/project/index.js') // true
309+
* isNodePackage('/usr/local/bin/tool') // false
310+
* ```
278311
*/
279312
export function isNodePackage(filePath: string): boolean {
280313
return detectExecutableType(filePath).type === 'package'

src/dlx/dir.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ function getFs() {
3030

3131
/**
3232
* Clear all DLX package installations.
33+
*
34+
* @example
35+
* ```typescript
36+
* await clearDlx()
37+
* ```
3338
*/
3439
export async function clearDlx(): Promise<void> {
3540
const packages = await listDlxPackagesAsync()
@@ -38,6 +43,11 @@ export async function clearDlx(): Promise<void> {
3843

3944
/**
4045
* Clear all DLX package installations synchronously.
46+
*
47+
* @example
48+
* ```typescript
49+
* clearDlxSync()
50+
* ```
4151
*/
4252
export function clearDlxSync(): void {
4353
const packages = listDlxPackages()
@@ -48,6 +58,13 @@ export function clearDlxSync(): void {
4858

4959
/**
5060
* Check if the DLX directory exists.
61+
*
62+
* @example
63+
* ```typescript
64+
* if (dlxDirExists()) {
65+
* console.log('DLX directory is present')
66+
* }
67+
* ```
5168
*/
5269
export function dlxDirExists(): boolean {
5370
const fs = getFs()
@@ -56,6 +73,13 @@ export function dlxDirExists(): boolean {
5673

5774
/**
5875
* Check if the DLX directory exists asynchronously.
76+
*
77+
* @example
78+
* ```typescript
79+
* if (await dlxDirExistsAsync()) {
80+
* console.log('DLX directory is present')
81+
* }
82+
* ```
5983
*/
6084
export async function dlxDirExistsAsync(): Promise<boolean> {
6185
const fs = getFs()
@@ -69,13 +93,23 @@ export async function dlxDirExistsAsync(): Promise<boolean> {
6993

7094
/**
7195
* Ensure the DLX directory exists, creating it if necessary.
96+
*
97+
* @example
98+
* ```typescript
99+
* await ensureDlxDir()
100+
* ```
72101
*/
73102
export async function ensureDlxDir(): Promise<void> {
74103
await safeMkdir(getSocketDlxDir())
75104
}
76105

77106
/**
78107
* Ensure the DLX directory exists synchronously, creating it if necessary.
108+
*
109+
* @example
110+
* ```typescript
111+
* ensureDlxDirSync()
112+
* ```
79113
*/
80114
export function ensureDlxDirSync(): void {
81115
safeMkdirSync(getSocketDlxDir())

src/dlx/manifest.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ export interface DlxManifestOptions {
124124

125125
/**
126126
* Type guard for binary entries.
127+
*
128+
* @example
129+
* ```typescript
130+
* const entry = manifest.getManifestEntry('https://example.com/tool')
131+
* if (entry && isBinaryEntry(entry)) {
132+
* console.log(entry.details.integrity)
133+
* }
134+
* ```
127135
*/
128136
export function isBinaryEntry(
129137
entry: ManifestEntry,
@@ -133,6 +141,14 @@ export function isBinaryEntry(
133141

134142
/**
135143
* Type guard for package entries.
144+
*
145+
* @example
146+
* ```typescript
147+
* const entry = manifest.getManifestEntry('@socketsecurity/cli@^2.0.0')
148+
* if (entry && isPackageEntry(entry)) {
149+
* console.log(entry.details.installed_version)
150+
* }
151+
* ```
136152
*/
137153
export function isPackageEntry(
138154
entry: ManifestEntry,

0 commit comments

Comments
 (0)