swc/bundler/tests/.cache/deno/f021cf3a9bc498aef7c7438a0d7d790c0d6f9e8d.ts
강동윤 bbaf619f63
fix(bundler): Fix bugs (#1437)
swc_bundler:
 - [x] Fix wrapped esms. (denoland/deno#9307)
 - [x] Make test secure.
2021-03-02 17:33:03 +09:00

40 lines
989 B
TypeScript

// Loaded from https://deno.land/std@0.77.0/io/streams.ts
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
/** Create a `Writer` from a `WritablseStreamDefaultReader`. */
export function fromStreamWriter(
streamWriter: WritableStreamDefaultWriter<Uint8Array>,
): Deno.Writer {
return {
async write(p: Uint8Array): Promise<number> {
await streamWriter.ready;
await streamWriter.write(p);
return p.length;
},
};
}
/** Create a `Reader` from a `ReadableSteramDefaultReader`. */
export function fromStreamReader(
streamReader: ReadableStreamDefaultReader<Uint8Array>,
): Deno.Reader {
const buffer = new Deno.Buffer();
return {
async read(p: Uint8Array): Promise<number | null> {
if (buffer.empty()) {
const res = await streamReader.read();
if (res.done) {
return null; // EOF
}
await Deno.writeAll(buffer, res.value);
}
return buffer.read(p);
},
};
}