-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapi.ts
More file actions
211 lines (175 loc) · 5.32 KB
/
Copy pathapi.ts
File metadata and controls
211 lines (175 loc) · 5.32 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
import type { Chunker } from "./chunker/api.js"
import type { Writer as StreamWriter } from "../writer/api.js"
import type { LayoutEngine, NodeID } from "./layout/api.js"
import * as UnixFS from "../unixfs.js"
import type {
Block,
BlockEncoder,
MultihashHasher,
MultihashDigest,
Link,
LinkVersion,
} from "../unixfs.js"
import type { State } from "./writer.js"
export * from "../writer/api.js"
import * as ChunkerService from "./chunker.js"
export type {
Chunker,
LayoutEngine,
MultihashHasher,
MultihashDigest,
Block,
State,
}
export interface FileWriterService<Layout> extends EncoderSettings<Layout> {
writer: BlockWriter
}
export interface WriterOptions<Layout extends unknown = unknown> {
readonly settings?: EncoderSettings<Layout>
readonly metadata?: UnixFS.Metadata
}
export interface EncoderSettings<Layout extends unknown = unknown> {
/**
* Chunker which will be used to split file content into chunks.
*/
chunker: Chunker
/**
* If provided leaves will be encoded as raw blocks, unless file has a
* metadata. This is what `rawLeaves` options used to be except instead
* of boolean you pass an encoder that will be used.
*/
fileChunkEncoder: FileChunkEncoder
/**
* If provided and file contains single chunk it will be encoded with this
* encoder. This is what `reduceSingleLeafToSelf` option used to be except
* instead of boolean you pass an encoder that will be used.
*/
smallFileEncoder: FileChunkEncoder
fileEncoder: FileEncoder
/**
* Builder that will be used to build file DAG from the leaf nodes.
*/
fileLayout: LayoutEngine<Layout>
/**
* Hasher used to compute multihash for each block in the file.
*/
hasher: MultihashHasher
/**
* This function is used to create CIDs from multihashes. This is similar
* to `cidVersion` option except you give it CID creator to use.
*/
linker: Linker
}
export interface InitOptions {
unixFsFileLinkWriter?: UnixFsFileLinkWriter
}
export interface UnixFsFileLinkWriter extends StreamWriter<UnixFS.FileLink> {}
export interface Options<Layout = unknown> {
writer: BlockWriter
metadata?: UnixFS.Metadata
settings?: EncoderSettings<Layout>
initOptions?: InitOptions
}
export interface CloseOptions {
releaseLock?: boolean
closeWriter?: boolean
}
export interface BlockWriter extends StreamWriter<Block> {}
export interface WritableBlockStream {
getWriter(): BlockWriter
}
export type FileChunkEncoder =
| BlockEncoder<PB, Uint8Array>
| BlockEncoder<RAW, Uint8Array>
export interface FileEncoder {
code: PB
encode(node: UnixFS.File): Uint8Array
}
export interface Linker<V extends LinkVersion = LinkVersion> {
createLink<T extends unknown, Code extends number, Alg extends number>(
code: Code,
hash: MultihashDigest<Alg>
): Link<T, Code, Alg, V>
}
export interface EncodedFile {
id: NodeID
block: Block
link: UnixFS.FileLink
}
export type PB = 0x70
export type RAW = 0x55
/**
* Interface defines API for importable content that is just a subset of `Blob`
* interface with some optional metadata fields. This implies that you can pass
* `Blob` instances anywhere `BlobContent` is required. It also means you could
* pass `Object.assign(blob, { mtime, mode })` whenever you want to pass content
* with optional metadata.
*/
export interface BlobContent extends BlobMetadata {
readonly size: number
stream(): ReadableStream<Uint8Array>
// text(): Promise<string>
// arrayBuffer(): Promise<ArrayBuffer>
// slice(start?: number, end?: number, contentType?: string): Blob
}
/**
* Optional unixfs metadata.
*/
export interface BlobMetadata extends UnixFS.Metadata {
readonly type: string
}
/**
* `FileContent` extends `BlobContent` with a required `name` field, which makes
* diffence between two equivalent to difference between `File` and `Blob`.
*
* Interface is desigend to be compatible with `File` instances and to allow
* optional metadata by passing `Object.assign(file, { mtime, mode })`.
*/
export interface FileContent extends BlobContent {
/**
* File path relative to directory it will be imported into.
*
* **Note:** File name is actually used as a file path which is to imply it
* can contain contains `/` delimiters.
*/
readonly name: string
}
export type FileState<Layout = unknown> =
| OpenFile<Layout>
| ClosedFile<Layout>
| LinkedFile
export interface FileView<State extends FileState = FileState> {
state: State
}
export interface OpenFile<Layout = unknown> {
readonly type: "file"
readonly status: "open"
readonly metadata: UnixFS.Metadata
readonly service: FileWriterService<Layout>
writing: boolean
chunker: ChunkerService.Chunker
layout: Layout
}
export interface ClosedFile<Layout = unknown> {
readonly type: "file"
readonly status: "closed"
readonly service: FileWriterService<Layout>
readonly metadata: UnixFS.Metadata
writing: boolean
chunker: ChunkerService.Chunker
layout: Layout
}
export interface LinkedFile {
readonly type: "file"
readonly status: "linked"
state: UnixFS.FileLink
}
export interface Writer<T extends unknown = unknown> {
write(bytes: Uint8Array): Promise<Writer<T>>
close(options?: CloseOptions): Promise<UnixFS.FileLink>
}
export interface View<T extends unknown = unknown> extends Writer<T> {
readonly writer: BlockWriter
readonly settings: EncoderSettings<T>
state: State<T>
}