@helia/verified-fetch
Version:
A fetch-like API for obtaining verified & trustless IPFS content on the web
45 lines • 1.85 kB
JavaScript
import { AbortError } from '@libp2p/interface';
import { CustomProgressEvent } from 'progress-events';
import { NoContentError } from '../errors.js';
/**
* Converts an async iterator of Uint8Array bytes to a stream and returns the first chunk of bytes
*/
export async function getStreamFromAsyncIterable(iterator, path, logger, options) {
const log = logger.forComponent('helia:verified-fetch:get-stream-from-async-iterable');
const reader = iterator[Symbol.asyncIterator]();
const { value: firstChunk, done } = await reader.next();
if (done === true) {
log.error('no content found for path', path);
throw new NoContentError();
}
const stream = new ReadableStream({
async start(controller) {
// the initial value is already available
options?.onProgress?.(new CustomProgressEvent('verified-fetch:request:progress:chunk'));
controller.enqueue(firstChunk);
},
async pull(controller) {
const { value, done } = await reader.next();
if (options?.signal?.aborted) {
controller.error(new AbortError(options.signal.reason ?? 'signal aborted by user'));
controller.close();
return;
}
if (done === true) {
if (value != null) {
options?.onProgress?.(new CustomProgressEvent('verified-fetch:request:progress:chunk'));
controller.enqueue(value);
}
controller.close();
return;
}
options?.onProgress?.(new CustomProgressEvent('verified-fetch:request:progress:chunk'));
controller.enqueue(value);
}
});
return {
stream,
firstChunk
};
}
//# sourceMappingURL=get-stream-from-async-iterable.js.map