Skip to content

Commit e4917fa

Browse files
committed
fix: constrain the unixfs type
Turns the unixfs type into a proper type to enable code completion and type checking.
1 parent 28a9c68 commit e4917fa

File tree

4 files changed

+9
-5
lines changed

4 files changed

+9
-5
lines changed

packages/ipfs-unixfs-exporter/test/exporter.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { exporter, recursive } from '../src/index.js'
3131
import asAsyncIterable from './helpers/as-async-iterable.js'
3232
import type { PBNode } from '@ipld/dag-pb'
3333
import type { Blockstore } from 'interface-blockstore'
34+
import type { UnixFSType } from 'ipfs-unixfs'
3435
import type { Chunker } from 'ipfs-unixfs-importer/chunker'
3536
import type { FileLayout } from 'ipfs-unixfs-importer/layout'
3637

@@ -46,7 +47,7 @@ describe('exporter', () => {
4647
smallFile = uint8ArrayConcat(await all(randomBytes(200)))
4748
})
4849

49-
async function dagPut (options: { type?: string, content?: Uint8Array, links?: dagPb.PBLink[] } = {}): Promise<{ file: UnixFS, node: PBNode, cid: CID }> {
50+
async function dagPut (options: { type?: UnixFSType, content?: Uint8Array, links?: dagPb.PBLink[] } = {}): Promise<{ file: UnixFS, node: PBNode, cid: CID }> {
5051
options.type = options.type ?? 'file'
5152
options.content = options.content ?? Uint8Array.from([0x01, 0x02, 0x03])
5253
options.links = options.links ?? []

packages/ipfs-unixfs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ file.mtime // undefined
135135

136136
Object.prototype.hasOwnProperty.call(file, 'mtime') // false
137137

138-
const dir = new UnixFS({ type: 'dir', mtime: { secs: 5n } })
138+
const dir = new UnixFS({ type: 'directory', mtime: { secs: 5n } })
139139
dir.mtime // { secs: Number, nsecs: Number }
140140
```
141141

packages/ipfs-unixfs/src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
*
113113
* Object.prototype.hasOwnProperty.call(file, 'mtime') // false
114114
*
115-
* const dir = new UnixFS({ type: 'dir', mtime: { secs: 5n } })
115+
* const dir = new UnixFS({ type: 'directory', mtime: { secs: 5n } })
116116
* dir.mtime // { secs: Number, nsecs: Number }
117117
* ```
118118
*/
@@ -127,7 +127,9 @@ export interface Mtime {
127127

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

130-
const types: Record<string, string> = {
130+
export type UnixFSType = 'raw' | 'directory' | 'file' | 'metadata' | 'symlink' | 'hamt-sharded-directory'
131+
132+
const types: Record<string, UnixFSType> = {
131133
Raw: 'raw',
132134
Directory: 'directory',
133135
File: 'file',
@@ -148,7 +150,7 @@ const DEFAULT_DIRECTORY_MODE = parseInt('0755', 8)
148150
const MAX_FANOUT = BigInt(1 << 10)
149151

150152
export interface UnixFSOptions {
151-
type?: string
153+
type?: UnixFSType
152154
data?: Uint8Array
153155
blockSizes?: bigint[]
154156
hashType?: bigint

packages/ipfs-unixfs/test/unixfs-format.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ describe('unixfs-format', () => {
370370
try {
371371
// eslint-disable-next-line no-new
372372
new UnixFS({
373+
// @ts-expect-error invalid type
373374
type: 'bananas'
374375
})
375376
} catch (err: any) {

0 commit comments

Comments
 (0)