Skip to content

fix: constrain the unixfs type #435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/ipfs-unixfs-exporter/test/exporter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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 ?? []
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-unixfs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
```

Expand Down
8 changes: 5 additions & 3 deletions packages/ipfs-unixfs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
* ```
*/
Expand All @@ -127,7 +127,9 @@ export interface Mtime {

export type MtimeLike = Mtime | { Seconds: number, FractionalNanoseconds?: number } | [number, number] | Date

const types: Record<string, string> = {
export type UnixFSType = 'raw' | 'directory' | 'file' | 'metadata' | 'symlink' | 'hamt-sharded-directory'

const types: Record<string, UnixFSType> = {
Raw: 'raw',
Directory: 'directory',
File: 'file',
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/ipfs-unixfs/test/unixfs-format.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down