The JS runtimes
Published in

The JS runtimes

Reading line-by-line in Deno

Purpose

Classes and functions

import { TextProtoReader } from "https://deno.land/std/textproto/mod.ts"
const tp=new TextProtoReader(r: BufReader);
readLine(): Promise<string | null>

Pipeline

Source -> Reader -> BufReader -> TextProtoReader

Sources to Reader

String

import { StringReader } from "https://deno.land/std/io/readers.ts"const str='A\nB\nC\n';
const sr=new StringReader(str);

File

await Deno.open("/var/tmp/a.log", {read: true});

Streams

import { readerFromStreamReader } from "https://deno.land/std/io/streams.ts"const fetchRes=await fetch("https://google.com");
const streamReader=readerFromStreamReader(fetchRes.body!.getReader());
//streamReader can be given to the next stage in pipeline i.e. BufReader}

Reader to BufReader

//STRINGimport { BufReader } from "https://deno.land/std/io/bufio.ts"
import { StringReader } from "https://deno.land/std/io/readers.ts"
const str='A\nB\nC\n';
const sr=new StringReader(str);
new BufReader(sr);
//FILEimport { BufReader } from "https://deno.land/std/io/bufio.ts"
new BufReader(await Deno.open("/var/tmp/a.log", {read: true}));
//STREAMimport { readerFromStreamReader } from "https://deno.land/std/io/streams.ts"const fetchRes=await fetch("https://google.com");
const streamReader=readerFromStreamReader(fetchRes.body!.getReader());
new BufReader(streamReader);

BufReader to TextProtoReader

new TextProtoReader(new BufReader(/*Reader from the source*/));

Examples

import { TextProtoReader } from "https://deno.land/std/textproto/mod.ts"
import { BufReader } from "https://deno.land/std/io/bufio.ts"
import { StringReader } from "https://deno.land/std/io/readers.ts";
const str='A\nB\nC\n';
const tp=new TextProtoReader(new BufReader(new StringReader(str)));
await tp.readLine();
//A
await tp.readLine();
//B
await tp.readLine();
//C
import { TextProtoReader } from "https://deno.land/std/textproto/mod.ts"
import { BufReader } from "https://deno.land/std/io/bufio.ts"
const file='/var/tmp/a.log';
const tp=new TextProtoReader(new BufReader(await Deno.open(file, {read: true})));
//read first three lines only
console.log(await tp.readLine());
//DEBUG JS - args []
//ignore this line
console.log(await tp.readLine());
//DEBUG TS - ">>> exec start" {"rootNames":["https://deno.land/x/doze@1.0/mod.ts","file:///Users/mayankc/Work/source/deno-vs-nodejs/delayworker.ts","https://deno.land/x/doze@1.0/doze.ts"]}
//do something with this line
console.log(await tp.readLine());
//DEBUG TS - {"allowJs":true,"esModuleInterop":true,"experimentalDecorators":true,"incremental":true,"isolatedModules":true,"lib":["deno.window"],"module":"esnext","strict":true,"target":"esnext","tsBuildInfoFile":"deno:///.tsbuildinfo","emitDecoratorMetadata":false,"jsx":"react","inlineSourceMap":true,"outDir":"deno://","removeComments":true}
//do something different with this line
//No need to read more
import { TextProtoReader } from "https://deno.land/std/textproto/mod.ts"
import { BufReader } from "https://deno.land/std/io/bufio.ts"
import { readerFromStreamReader } from "https://deno.land/std/io/streams.ts"
const fetchRes=await fetch("https://abc.com");
const tp=new TextProtoReader(new BufReader(readerFromStreamReader(fetchRes.body!.getReader())));
await tp.readLine();
//<!doctype html>
await tp.readLine();
//<html lang="en">
await tp.readLine();
//<head>
import { TextProtoReader } from "https://deno.land/std/textproto/mod.ts"
import { BufReader } from "https://deno.land/std/io/bufio.ts"
import { readerFromStreamReader } from "https://deno.land/std/io/streams.ts"
const fetchRes=await fetch("https://raw.githubusercontent.com/mayankchoubey/deno-doze/main/doze.ts");
const tp=new TextProtoReader(new BufReader(readerFromStreamReader(fetchRes.body!.getReader())));
let line;
while(line=await tp.readLine())
console.log(line);

--

--

Articles on the popular JS runtimes, Node.js, Deno, and Bun

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store