chore(examples): Converting Deno Streams to Web Streams (#2765)

This commit is contained in:
ayame113 2022-10-11 01:11:17 +09:00 committed by GitHub
parent b5de3bda7e
commit f16b031960
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 18 deletions

View File

@ -5,10 +5,8 @@
* @module
*/
import { copy } from "../streams/conversion.ts";
const filenames = Deno.args;
for (const filename of filenames) {
const file = await Deno.open(filename);
await copy(file, Deno.stdout);
file.close();
await file.readable.pipeTo(Deno.stdout.writable, { preventClose: true });
}

View File

@ -8,9 +8,4 @@
const url_ = Deno.args[0];
const res = await fetch(url_);
// TODO(ry) Re-enable streaming in this example.
// Originally we did: await Deno.copy(res.body, Deno.stdout);
// But maybe more JS-y would be: res.pipeTo(Deno.stdout);
const body = new Uint8Array(await res.arrayBuffer());
await Deno.stdout.write(body);
await res.body?.pipeTo(Deno.stdout.writable);

View File

@ -6,7 +6,7 @@
*/
import { parse } from "../flags/mod.ts";
import { readStringDelim } from "../io/buffer.ts";
import { TextDelimiterStream } from "../streams/delimiter.ts";
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncFunction.
const AsyncFunction = Object.getPrototypeOf(async function () {})
@ -33,7 +33,7 @@ OPTIONS:
ARGS:
<code>`;
export type XevalFunc = (v: string) => void;
export type XevalFunc = (v: string) => Promise<unknown>;
export interface XevalOptions {
delimiter?: string;
@ -42,11 +42,14 @@ export interface XevalOptions {
const DEFAULT_DELIMITER = "\n";
export async function xeval(
reader: Deno.Reader,
readable: ReadableStream<Uint8Array>,
xevalFunc: XevalFunc,
{ delimiter = DEFAULT_DELIMITER }: XevalOptions = {},
) {
for await (const chunk of readStringDelim(reader, delimiter)) {
const chunks = readable
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextDelimiterStream(delimiter));
for await (const chunk of chunks) {
// Ignore empty chunks.
if (chunk.length > 0) {
await xevalFunc(chunk);
@ -89,7 +92,7 @@ async function main() {
const xEvalFunc = new AsyncFunction(replVar, code);
await xeval(Deno.stdin, xEvalFunc, { delimiter });
await xeval(Deno.stdin.readable, xEvalFunc, { delimiter });
}
if (import.meta.main) {

View File

@ -1,22 +1,33 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { xeval } from "./xeval.ts";
import { StringReader } from "../io/readers.ts";
import { assertEquals, assertStringIncludes } from "../testing/asserts.ts";
import { dirname, fromFileUrl } from "../path/mod.ts";
const moduleDir = dirname(fromFileUrl(import.meta.url));
function createReadableStream(str: string) {
return new ReadableStream<Uint8Array>({
start(controller) {
controller.enqueue(new TextEncoder().encode(str));
controller.close();
},
});
}
Deno.test("xevalSuccess", async function () {
const chunks: string[] = [];
await xeval(new StringReader("a\nb\nc"), ($): number => chunks.push($));
await xeval(
createReadableStream("a\nb\nc"),
($) => Promise.resolve(chunks.push($)),
);
assertEquals(chunks, ["a", "b", "c"]);
});
Deno.test("xevalDelimiter", async function () {
const chunks: string[] = [];
await xeval(
new StringReader("!MADMADAMADAM!"),
($): number => chunks.push($),
createReadableStream("!MADMADAMADAM!"),
($) => Promise.resolve(chunks.push($)),
{
delimiter: "MADAM",
},