UNPKG

@libp2p/multistream-select

Version:
16 lines 703 B
import { InvalidMessageError } from '@libp2p/interface'; import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'; import { toString as uint8ArrayToString } from 'uint8arrays/to-string'; const NewLine = uint8ArrayFromString('\n'); /** * Read a length-prefixed string from the passed stream, stripping the final newline character */ export async function readString(reader, options) { const buf = await reader.read(options); const arr = buf.subarray(); if (arr.byteLength === 0 || arr[arr.length - 1] !== NewLine[0]) { throw new InvalidMessageError('Missing newline'); } return uint8ArrayToString(arr).trimEnd(); } //# sourceMappingURL=multistream.js.map