UNPKG

@chainsafe/libp2p-quic

Version:
60 lines 1.9 kB
import { AbstractStream } from '@libp2p/utils/abstract-stream'; import { Uint8ArrayList } from 'uint8arraylist'; export class QuicStream extends AbstractStream { #stream; constructor(init) { super(init); this.#stream = init.stream; this.direction = init.direction; this.log('new', this.direction, this.id); this.readyFromStream() .catch(err => { this.log.error('error reading from stream - %e', err); }); } sendNewStream(options) { } async sendData(buf, options) { this.log.trace('writing %d bytes', buf.byteLength); await this.#stream.write(buf.subarray()); this.log.trace('wrote %d bytes', buf.byteLength); } async sendReset(options) { await this.#stream.resetWrite(); await this.#stream.stopRead(); } async sendCloseWrite(options) { await this.#stream.finishWrite(); } async sendCloseRead(options) { // TODO: how to do this? } async readyFromStream() { try { while (true) { this.log.trace('reading'); const chunk = Buffer.allocUnsafe(CHUNK_SIZE); const length = await this.#stream.read(chunk); if (length == null) { break; } this.sourcePush(new Uint8ArrayList(chunk.subarray(0, length))); this.log.trace('read %d bytes', length); } } catch (err) { this.log.error('source error - %e', err); if (err.code === 'Unknown') { // clean exit this.remoteCloseRead(); return; } this.abort(err); } finally { this.remoteCloseWrite(); } } } export const CHUNK_SIZE = 4096; //# sourceMappingURL=stream.js.map