-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathunixfs.ts
More file actions
421 lines (367 loc) · 12.6 KB
/
Copy pathunixfs.ts
File metadata and controls
421 lines (367 loc) · 12.6 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import type {
MultihashDigest,
MultibaseEncoder,
BlockEncoder,
MultihashHasher,
Link as IPLDLink,
Version as LinkVersion,
Block as IPLDBlock,
BlockView as IPLDBlockView,
} from "multiformats"
import { Data, type IData } from "../gen/unixfs.js"
export type {
MultihashHasher,
MultibaseEncoder,
MultihashDigest,
BlockEncoder,
}
export * as Layout from "./file/layout/api"
import NodeType = Data.DataType
export { NodeType }
export type { IData, LinkVersion }
/**
* Type representing any UnixFS node.
*/
export type Node =
| Raw
| SimpleFile
| AdvancedFile
| ComplexFile
| Directory
| DirectoryShard
| ShardedDirectory
| Symlink
export type File = SimpleFile | AdvancedFile | ComplexFile
/**
* Logical representation of a file that fits a single block. Note this is only
* semantically different from a `FileChunk` and your interpretation SHOULD vary
* depending on where you encounter the node (In root of the DAG or not).
*/
export interface SimpleFile {
readonly metadata?: Metadata
readonly type: NodeType.File
readonly layout: "simple"
readonly content: Uint8Array
}
export interface Metadata {
readonly mode?: Mode
readonly mtime?: MTime
}
/**
* Logical represenatation of a file that consists of multiple blocks. Note it
* is only semantically different from a `FileShard` and your interpretation
* SHOULD vary depending on where you encounter the node (In root of the DAG
* or not).
*/
export interface AdvancedFile {
readonly metadata?: Metadata
readonly type: NodeType.File
readonly layout: "advanced"
readonly parts: ReadonlyArray<FileLink>
}
export type Chunk = Raw | FileChunk
/**
* Encodes UnixFS Raw node (a leaf node of the file DAG layout). This
* representation had been subsumed by `FileChunk` representation and
* therefor is marked as deprecated.
*
* UnixFS consumers are very likely to encounter nodes of this type, as of this
* writing JS & Go implementations can be configured to produce these nodes, in
* trickle DAG use this configuration.
*
* UnixFS producers are RECOMMENDED to either use `FileChunk` representation or
* better yet raw binary nodes (That is 0x55 multicodec) which will likely
* relpace them in the future.
*
* @see https://github.com/multiformats/multicodec/blob/master/table.csv#L39
*
* Please note that in the wild Raw nodes are likely to come with other fiels
* encoded but both encoder and decoder presented here will ignore them.
*
* @deprecated
*/
export interface Raw {
readonly type: NodeType.Raw
/**
* Raw bytes of the content
*/
readonly content: Uint8Array
}
/**
* Logical representation of a file chunk (a leaf node of the file DAG layout).
*
* When large file is added to IPFS it gets chunked into smaller pieces
* (according to the `--chunker` specified) and each chunk is encoded into this
* representation (and linked from file DAG). Please note that in practice there
* are many other representations for file chunks (leaf nodes) like `Raw` nodes
* (deprecated in favor of this representation) and raw binary nodes (That is
* 0x55 multicodec) which are on a way to surpass this representation.
*
* Please note that in protobuf representation there is only one `file` node
* type with many optional fields, however different combination of fields
* corresponds to a different semntaics and we represent each via different type.
*
* Also note that some file nodes may also have `mode` and `mtime` fields,
* which we represent via `SimpleFile` type, however in practice two are
* indistinguishable & how to interpret will only depend node is encountered
* in DAG root position or not. That is because one could take two `SimpleFile`
* nodes and represent their concatination via `AdvancedFile` simply by linking
* to them. In such scenario consumer SHOULD treat leaves as `FileChunk`s and
* ignoring their `mode` and `mtime` fileds. However if those leves are
* encontured on their own consumer SHOULD treat them as `SimpleFile`s and
* take `mode` and `mtime` fields into account.
*/
export interface FileChunk {
readonly type: NodeType.File
readonly layout: "simple"
readonly content: Uint8Array
readonly metadata?: Metadata
}
/**
* Logical representation of a file shard. When large files are chunked
* slices that span multiple blocks may be represented via file shards in
* certain DAG layouts (e.g. balanced & trickle DAGs).
*
* Please note in protobuf representation there is only one `file` node type
* with many optional fields. Different combination of those fields corresponds
* to a different semntaics. Combination of fields in this type represent a
* branch nodes in the file DAGs in which nodes beside leaves and root exist.
*
* Also note that you may encounter `FileShard`s with `mode` and `mtime` fields
* which according to our definition would be `AdvancedFile`. However just as
* with `FileChunk` / `SimpleFile`, here as well, you should treat node as
* `AdvancedFile` if you encounter it in the root position (that is to say
* regard `mode`, `mtime` field) and treat it as `FileShard` node if encountered
* in any other position (that is ignore `mode`, `mtime` fileds).
*/
export interface FileShard {
readonly type: NodeType.File
readonly layout: "advanced"
readonly parts: ReadonlyArray<FileLink>
}
export type FileLink =
| ContentDAGLink<Uint8Array>
| ContentDAGLink<Chunk>
| ContentDAGLink<FileShard>
export interface ContentDAGLink<T> extends DAGLink<T> {
/**
* Total number of bytes in the file
*/
readonly contentByteLength: number
/**
* Offset bytes in the file
*/
readonly contentByteOffset?: number
}
/**
* Represents a link to the DAG with
*/
export interface DAGLink<T = unknown> extends Phantom<T> {
/**
* *C*ontent *Id*entifier of the target DAG.
*/
readonly cid: Link<T>
/**
* Cumulative number of bytes in the target DAG, that is number of bytes in
* the block and all the blocks it links to.
*/
readonly dagByteLength: number
}
/**
* These type of nodes are not produces by referenece IPFS implementations, yet
* such file nodes could be represented and therefor defined with this type.
*
* In this file representation first chunk of the file is represented by a
* `data` field while rest of the file is represented by links.
*
* It is NOT RECOMMENDED to use this representation (which is why it's marked
* deprecated), however it is still valid representation and UnixFS consumers
* SHOULD recognize it and interpret as described.
*
* @deprecated
*/
export interface ComplexFile {
readonly type: NodeType.File
readonly layout: "complex"
readonly content: Uint8Array
readonly parts: ReadonlyArray<FileLink>
readonly metadata?: Metadata
}
/**
* This is an utility type that represents any
* kind of file which is then refined to one of
* the other definitions.
*/
export interface UnknownFile {
readonly type: NodeType.File
readonly content?: Uint8Array
readonly parts?: ReadonlyArray<FileLink>
readonly metadata?: Metadata
}
/**
* Type for either UnixFS directory representation.
*/
export type Directory = FlatDirectory | ShardedDirectory
/**
* Logacal representation of a directory that fits single block.
*/
export interface FlatDirectory {
readonly type: NodeType.Directory
readonly entries: ReadonlyArray<DirectoryEntryLink>
readonly metadata?: Metadata
}
export type DirectoryEntryLink =
| NamedDAGLink<File>
| NamedDAGLink<Directory>
| NamedDAGLink<Uint8Array>
export type DirectoryLink = DAGLink<Directory>
export interface NamedDAGLink<T> extends DAGLink<T> {
readonly name: string
}
/**
* Logical representation of directory encoded in multiple blocks (usually when
* it contains large number of entries). Such directories are represented via
* Hash Array Map Tries (HAMT).
*
* @see https://en.wikipedia.org/wiki/Hash_array_mapped_trie
*/
export interface ShardedDirectory extends DirectoryShard {}
/**
* Logical represenatation of the shard of the sharded directory. Please note
* that it only semantically different from `AdvancedDirectoryLayout`, in
* practice they are the same and interpretation should vary based on view. If
* viewed form root position it is `AdvancedDirectoryLayout` and it's `mtime`
* `mode` field to be respected, otherwise it is `DirectoryShard` and it's
* `mtime` and `mode` field to be ignored.
*/
export interface DirectoryShard {
readonly type: NodeType.HAMTShard
readonly bitfield: Uint8Array
/*
* HAMT table width (In IPFS it's usually 256)
*/
readonly fanout: uint64
/**
* Multihash code for the hashing function used (In IPFS it's [murmur3-64][])
*
* [murmur3-64]:https://github.com/multiformats/multicodec/blob/master/table.csv#L24
*/
readonly hashType: uint64
readonly entries: ReadonlyArray<ShardedDirectoryLink>
readonly metadata?: Metadata
}
export type ShardedDirectoryLink =
| NamedDAGLink<File>
| NamedDAGLink<Uint8Array>
| NamedDAGLink<Directory>
| NamedDAGLink<DirectoryShard>
/**
* Logical representation of a [symbolic link][].
*
* [symbolic link]:https://en.wikipedia.org/wiki/Symbolic_link
*/
export interface Symlink {
readonly type: NodeType.Symlink
/**
* UTF-8 encoded path to the symlink target.
*/
readonly content: ByteView<string>
readonly metadata?: Metadata
}
/**
* representing the modification time in seconds relative to the unix epoch
* 1970-01-01T00:00:00Z.
*/
export interface UnixTime {
/**
* (signed 64bit integer): represents the amount of seconds after or before
* the epoch.
*/
readonly Seconds: int64
/**
* (optional, 32bit unsigned integer ): when specified represents the
* fractional part of the mtime as the amount of nanoseconds. The valid
* range for this value are the integers [1, 999999999].
*/
readonly FractionalNanoseconds?: fixed32
}
/**
* The mode is for persisting the file permissions in [numeric notation].
* If unspecified this defaults to
* - `0755` for directories/HAMT shards
* - `0644` for all other types where applicable
*
* The nine least significant bits represent `ugo-rwx`
* The next three least significant bits represent setuid, setgid and the sticky bit.
* The remaining 20 bits are reserved for future use, and are subject to change.
* Spec implementations MUST handle bits they do not expect as follows:
* - For future-proofing the (de)serialization layer must preserve the entire
* `uint32` value during clone/copy operations, modifying only bit values that
* have a well defined meaning:
* `clonedValue = ( modifiedBits & 07777 ) | ( originalValue & 0xFFFFF000 )`
* - Implementations of this spec MUST proactively mask off bits without a
* defined meaning in the implemented version of the spec:
* `interpretedValue = originalValue & 07777`
*
* [numeric notation]:https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation
*
* @see https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_stat.h.html
*/
export type Mode = uint32
/**
* representing the modification time in seconds relative to the unix epoch
* 1970-01-01T00:00:00Z.
*/
export interface MTime {
readonly secs: number
readonly nsecs?: number
}
/**
* Represents byte encoded representation of the `Data`. It uses type parameter
* to capture the structure of the data it encodes.
*/
export interface ByteView<Data> extends Uint8Array, Phantom<Data> {}
/**
* @see https://github.com/ipfs/go-bitfield
*/
export type Bitfield = Uint8Array
// TS does not really have these, create aliases so it's aligned closer
// to protobuf spec
export type int64 = number
export type fixed32 = number
export type uint64 = number
export type uint32 = number
/**
* This is an utility type to retain unused type parameter `T`. It can be used
* as nominal type e.g. to capture semantics not represented in actual type strucutre.
*/
export interface Phantom<T> {
// This field can not be represented because field name is non-existings
// unique symbol. But given that field is optional any object will valid
// type contstraint.
[PhantomKey]?: T
}
declare const PhantomKey: unique symbol
export interface Link<
Data extends unknown = unknown,
Format extends number = number,
Alg extends number = number,
V extends LinkVersion = LinkVersion
> extends IPLDLink<Data, Format, Alg, V> {}
export interface PBLink {
Name?: string
Tsize?: number
Hash: Link
}
export interface Block<
T = unknown,
C extends number = number,
A extends number = number,
V extends LinkVersion = LinkVersion
> extends IPLDBlock<T, C, A, V> {}
export interface BlockView<
T = unknown,
C extends number = number,
A extends number = number,
V extends LinkVersion = LinkVersion
> extends IPLDBlockView<T, C, A, V> {}