From e4917fab9a47b1816f0a0be18e924d850039c263 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 18 Jun 2025 07:57:33 +0300 Subject: [PATCH] fix: constrain the unixfs type Turns the unixfs type into a proper type to enable code completion and type checking. --- packages/ipfs-unixfs-exporter/test/exporter.spec.ts | 3 ++- packages/ipfs-unixfs/README.md | 2 +- packages/ipfs-unixfs/src/index.ts | 8 +++++--- packages/ipfs-unixfs/test/unixfs-format.spec.ts | 1 + 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/ipfs-unixfs-exporter/test/exporter.spec.ts b/packages/ipfs-unixfs-exporter/test/exporter.spec.ts index a48fe9bb..67326ec4 100644 --- a/packages/ipfs-unixfs-exporter/test/exporter.spec.ts +++ b/packages/ipfs-unixfs-exporter/test/exporter.spec.ts @@ -31,6 +31,7 @@ import { exporter, recursive } from '../src/index.js' import asAsyncIterable from './helpers/as-async-iterable.js' import type { PBNode } from '@ipld/dag-pb' import type { Blockstore } from 'interface-blockstore' +import type { UnixFSType } from 'ipfs-unixfs' import type { Chunker } from 'ipfs-unixfs-importer/chunker' import type { FileLayout } from 'ipfs-unixfs-importer/layout' @@ -46,7 +47,7 @@ describe('exporter', () => { smallFile = uint8ArrayConcat(await all(randomBytes(200))) }) - async function dagPut (options: { type?: string, content?: Uint8Array, links?: dagPb.PBLink[] } = {}): Promise<{ file: UnixFS, node: PBNode, cid: CID }> { + async function dagPut (options: { type?: UnixFSType, content?: Uint8Array, links?: dagPb.PBLink[] } = {}): Promise<{ file: UnixFS, node: PBNode, cid: CID }> { options.type = options.type ?? 'file' options.content = options.content ?? Uint8Array.from([0x01, 0x02, 0x03]) options.links = options.links ?? [] diff --git a/packages/ipfs-unixfs/README.md b/packages/ipfs-unixfs/README.md index d8757440..aa23bcb0 100644 --- a/packages/ipfs-unixfs/README.md +++ b/packages/ipfs-unixfs/README.md @@ -135,7 +135,7 @@ file.mtime // undefined Object.prototype.hasOwnProperty.call(file, 'mtime') // false -const dir = new UnixFS({ type: 'dir', mtime: { secs: 5n } }) +const dir = new UnixFS({ type: 'directory', mtime: { secs: 5n } }) dir.mtime // { secs: Number, nsecs: Number } ``` diff --git a/packages/ipfs-unixfs/src/index.ts b/packages/ipfs-unixfs/src/index.ts index a5a98ed9..04e03973 100644 --- a/packages/ipfs-unixfs/src/index.ts +++ b/packages/ipfs-unixfs/src/index.ts @@ -112,7 +112,7 @@ * * Object.prototype.hasOwnProperty.call(file, 'mtime') // false * - * const dir = new UnixFS({ type: 'dir', mtime: { secs: 5n } }) + * const dir = new UnixFS({ type: 'directory', mtime: { secs: 5n } }) * dir.mtime // { secs: Number, nsecs: Number } * ``` */ @@ -127,7 +127,9 @@ export interface Mtime { export type MtimeLike = Mtime | { Seconds: number, FractionalNanoseconds?: number } | [number, number] | Date -const types: Record = { +export type UnixFSType = 'raw' | 'directory' | 'file' | 'metadata' | 'symlink' | 'hamt-sharded-directory' + +const types: Record = { Raw: 'raw', Directory: 'directory', File: 'file', @@ -148,7 +150,7 @@ const DEFAULT_DIRECTORY_MODE = parseInt('0755', 8) const MAX_FANOUT = BigInt(1 << 10) export interface UnixFSOptions { - type?: string + type?: UnixFSType data?: Uint8Array blockSizes?: bigint[] hashType?: bigint diff --git a/packages/ipfs-unixfs/test/unixfs-format.spec.ts b/packages/ipfs-unixfs/test/unixfs-format.spec.ts index 15c76373..0aba16d3 100644 --- a/packages/ipfs-unixfs/test/unixfs-format.spec.ts +++ b/packages/ipfs-unixfs/test/unixfs-format.spec.ts @@ -370,6 +370,7 @@ describe('unixfs-format', () => { try { // eslint-disable-next-line no-new new UnixFS({ + // @ts-expect-error invalid type type: 'bananas' }) } catch (err: any) {