mirror of
https://github.com/swc-project/swc.git
synced 2024-12-21 04:32:01 +03:00
34 lines
858 B
TypeScript
34 lines
858 B
TypeScript
// Loaded from https://deno.land/x/compress@v0.3.8/utils/uint8.ts
|
|
|
|
|
|
export async function readMsg(reader: Deno.Reader): Promise<Uint8Array | null> {
|
|
const arr: Uint8Array[] = [];
|
|
const n = 100;
|
|
let readed: number | null;
|
|
while (true) {
|
|
const p: Uint8Array = new Uint8Array(n);
|
|
readed = await reader.read(p);
|
|
if (readed === null) break;
|
|
if (readed < n) {
|
|
arr.push(p.subarray(0, readed));
|
|
break;
|
|
} else {
|
|
arr.push(p);
|
|
}
|
|
}
|
|
if (readed === null) return null;
|
|
const result = concatUint8Array(arr);
|
|
return result;
|
|
}
|
|
|
|
export function concatUint8Array(arr: Uint8Array[]): Uint8Array {
|
|
const length = arr.reduce((pre, next) => pre + next.length, 0);
|
|
const result = new Uint8Array(length);
|
|
let offset = 0;
|
|
for (const v of arr) {
|
|
result.set(v, offset);
|
|
offset += v.length;
|
|
}
|
|
return result;
|
|
}
|