Skip to content

Commit cb99618

Browse files
committed
chore: add readme
1 parent a33ec24 commit cb99618

1 file changed

Lines changed: 83 additions & 1 deletion

File tree

README.md

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,88 @@ const demo = async blob => {
129129
}
130130
```
131131

132+
### Collecting UnixFS FileLinks
133+
134+
You can optionally pass a unixFsFileLinkWriter stream to capture metadata for each link (useful for indexing or tracking layout information).
135+
136+
```js
137+
import {
138+
createWriter,
139+
createFileWriter,
140+
} from '@vascosantos/unixfs'
141+
142+
import { withMaxChunkSize } from '@vascosantos/unixfs/file/chunker/fixed'
143+
import { withWidth } from '@vascosantos/unixfs/file/layout/balanced'
144+
145+
const defaultSettings = UnixFS.configure({
146+
fileChunkEncoder: raw,
147+
smallFileEncoder: raw,
148+
chunker: withMaxChunkSize(1024 * 1024),
149+
fileLayout: withWidth(1024),
150+
})
151+
152+
/**
153+
* @param {Blob} blob
154+
* @returns {Promise<import('@vascosantos/unixfs').FileLink[]>}
155+
*/
156+
async function collectUnixFsFileLinks(blob) {
157+
const fileLinks = []
158+
159+
// Create a stream to collect metadata (FileLinks)
160+
const { readable, writable } = new TransformStream()
161+
162+
// Set up the main UnixFS writer (data goes nowhere here)
163+
const unixfsWriter = createWriter({
164+
writable: new WritableStream(), // Discard actual DAG output
165+
settings: defaultSettings,
166+
})
167+
168+
// Set up the file writer with link metadata writer
169+
const unixFsFileLinkWriter = writable.getWriter()
170+
171+
const fileWriter = createFileWriter({
172+
...unixfsWriter,
173+
initOptions: {
174+
unixFsFileLinkWriter,
175+
},
176+
})
177+
178+
// Start concurrent reading of the metadata stream
179+
const fileLinkReader = readable.getReader()
180+
const readLinks = (async () => {
181+
while (true) {
182+
const { done, value } = await fileLinkReader.read()
183+
if (done) break
184+
fileLinks.push(value)
185+
}
186+
})()
187+
188+
// Pipe the blob to the file writer
189+
await blob.stream().pipeTo(
190+
new WritableStream({
191+
async write(chunk) {
192+
await fileWriter.write(chunk)
193+
},
194+
})
195+
)
196+
197+
// Finalize everything
198+
await fileWriter.close()
199+
await unixfsWriter.close()
200+
await unixFsFileLinkWriter.close()
201+
202+
// Wait for all links to be read
203+
await readLinks
204+
205+
return fileLinks
206+
}
207+
208+
// Usage
209+
const blob = new Blob(['Hello UnixFS links'])
210+
const links = await collectUnixFsFileLinks(blob)
211+
console.log(links)
212+
```
213+
132214
## License
133215

134216
Licensed under either of
@@ -144,4 +226,4 @@ Unless you explicitly state otherwise, any contribution intentionally submitted
144226
[readablestream]: https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream
145227
[car]: https://ipld.io/specs/transport/car/carv1/
146228
[`transformstream`]: https://developer.mozilla.org/en-US/docs/Web/API/TransformStream
147-
[`writablestream`]: https://developer.mozilla.org/en-US/docs/Web/API/WritableStream
229+
[`writablestream`]: https://developer.mozilla.org/en-US/docs/Web/API/WritableStream

0 commit comments

Comments
 (0)