-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfile.js
More file actions
146 lines (135 loc) · 3.08 KB
/
Copy pathfile.js
File metadata and controls
146 lines (135 loc) · 3.08 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
import * as API from "./file/api.js"
import * as UnixFS from "./codec.js"
import * as Writer from "./file/writer.js"
import * as Task from "actor"
import { panic } from "./writer/util.js"
import * as FixedSize from "./file/chunker/fixed.js"
import { sha256 } from "multiformats/hashes/sha2"
import { CID } from "multiformats/cid"
import * as Balanced from "./file/layout/balanced.js"
export * from "./file/api.js"
/**
* @returns {API.EncoderSettings}
*/
export const defaults = () => ({
chunker: FixedSize,
fileChunkEncoder: UnixFSLeaf,
smallFileEncoder: UnixFSLeaf,
fileEncoder: UnixFS,
fileLayout: Balanced.withWidth(174),
hasher: sha256,
linker: { createLink: CID.createV1 },
})
/**
* @template {unknown} Layout
* @param {Partial<API.EncoderSettings<Layout>>} config
* @returns {API.EncoderSettings<Layout>}
*/
export const configure = (config) => ({
...defaults(),
...config,
})
export const UnixFSLeaf = {
code: UnixFS.code,
name: UnixFS.name,
encode: UnixFS.encodeFileChunk,
}
export const UnixFSRawLeaf = {
code: UnixFS.code,
name: UnixFS.name,
encode: UnixFS.encodeRaw,
}
/**
* @template Layout
* @param {API.Options<Layout>} options
* @returns {API.View<Layout>}
*/
export const create = ({
writer,
metadata = {},
settings = defaults(),
initOptions = {},
}) =>
new FileWriterView(
Writer.init(writer, metadata, configure(settings), initOptions)
)
/**
* @template T
* @param {API.View<T>} view
* @param {Uint8Array} bytes
* @return {Promise<API.View<T>>}
*/
export const write = async (view, bytes) => {
await perform(view, Task.send({ type: "write", bytes }))
return view
}
/**
* @template T
* @param {API.View<T>} view
* @param {API.CloseOptions} options
*/
export const close = async (
view,
{ releaseLock = false, closeWriter = false } = {}
) => {
await perform(view, Task.send({ type: "close" }))
const { state } = view
if (state.status === "linked") {
if (closeWriter) {
await view.state.writer.close()
} else if (releaseLock) {
view.state.writer.releaseLock()
}
return state.link
/* c8 ignore next 5 */
} else {
panic(
`Expected writer to be in 'linked' state after close, but it is in "${state.status}" instead`
)
}
}
/**
* @template T
* @param {API.View<T>} view
* @param {Task.Effect<Writer.Message>} effect
*/
const perform = (view, effect) =>
Task.fork(
Task.loop(effect, (message) => {
const { state, effect } = Writer.update(message, view.state)
view.state = state
return effect
})
)
/**
* @template Layout
* @implements {API.View<Layout>}
*/
class FileWriterView {
/**
* @param {Writer.State<Layout>} state
*/
constructor(state) {
this.state = state
}
get writer() {
return this.state.writer
}
get settings() {
return this.state.config
}
/**
* @param {Uint8Array} bytes
* @returns {Promise<API.View<Layout>>}
*/
write(bytes) {
return write(this, bytes)
}
/**
* @param {API.CloseOptions} [options]
* @returns {Promise<UnixFS.FileLink>}
*/
close(options) {
return close(this, options)
}
}