D
Deno

help

How to read all text from stdin.

Ppomdtr10/6/2023
It uses to be simple with the Deta.readAll function. Now it is deprecated, link to https://deno.land/std/streams/read_all.ts?s=readAll (which is also deprecated). The deprecation message says: Use ReadableStream and toArrayBuffer instead. A specific example would be better. Basically, I want
const text = await Deno.readTextFile("/dev/stdin");
const text = await Deno.readTextFile("/dev/stdin");
but working on windows Ok I found it. Here it is for future reference
import { toArrayBuffer } from "https://deno.land/std@0.203.0/streams/mod.ts";

const input = new TextDecoder().decode(await toArrayBuffer(Deno.stdin.readable))

console.log(input);
import { toArrayBuffer } from "https://deno.land/std@0.203.0/streams/mod.ts";

const input = new TextDecoder().decode(await toArrayBuffer(Deno.stdin.readable))

console.log(input);
DAHDeno AI Helper10/6/2023
To read all text from stdin in Deno, you can use the Deno.stdin variable which provides a ReadableStream interface. Here is an example from the documentation:
const decoder = new TextDecoder();
for await (const chunk of Deno.stdin.readable) {
const text = decoder.decode(chunk);
// do something with the text
}
const decoder = new TextDecoder();
for await (const chunk of Deno.stdin.readable) {
const text = decoder.decode(chunk);
// do something with the text
}
In this example, Deno.stdin.readable is a ReadableStream that you can asynchronously iterate over to get chunks of data from stdin. Each chunk is a Uint8Array that you can decode into a string using TextDecoder. Please note that this code will read from stdin until it encounters an EOF (End of File) marker, which signals that there is no more data to read. In a terminal, you can usually send an EOF marker by pressing Ctrl+D (on Unix-like systems) or Ctrl+Z (on Windows). This method should work across different platforms including Windows. However, please note that the behavior might be slightly different depending on the terminal or command line interface you are using.
IioB10/6/2023
Since you're already importing from the std, might as well use toText instead!
import { toText } from "https://deno.land/std@0.203.0/streams/mod.ts";

const input = await toText(Deno.stdin.readable)

console.log(input);
import { toText } from "https://deno.land/std@0.203.0/streams/mod.ts";

const input = await toText(Deno.stdin.readable)

console.log(input);
Ppomdtr10/6/2023
thanks! toJson is also quite useful
IioB10/6/2023
👍
EEsente10/6/2023
const input = await new Response(Deno.stdin.readable).json();
const input = await new Response(Deno.stdin.readable).json();
Ppomdtr10/9/2023
wow this looks weird

Looking for more? Join the community!

Recommended Posts
Is it possible to use Deno KV on subhosting?I'm using Deno subhosting and i would like to know if it's possible to use KV on subhostingError only happens when I'm on deno deployPlease can someone explain why with my web application when deployed in deno deploy I get this respoDeno Deploy - is it possible to have 2 servers running?Maybe Im using it completely wrong, but locally when i develop I have two Deno servers running: BacNPM package randomly brokeI've been using the imagekit package for my fresh project, and it's been working fine since I instalsubpackage imports with npm specifierse.g. `import colors from "npm:tailwindcss/colors"` currently fails, is this not supposed to work? SIs it possible to use Cache-Control headers to control Deno's cacheI've seen via an issue ( https://github.com/denoland/deno/issues/16296 ) that Deno stores its metadaDeno Deploy Queue trouble in productionI'm encountering an issue with Deno Queue in my Deno Deploy project, and I'm hoping to get some guidDeno compile target errorHello, I am trying to compile a Deno api using `deno --unstable ./path/to/api/index.ts --output ./paHow to avoid the method POST to executed in the URL I am redirected to?How to avoid the method POST to executed in the URL I am redirected to? routes/admin/login.tsx ```thow to use Untar without deprecated functions?`Untar` from std/archives requires an instance of `Reader` - apparently most functions return some RHelp, does the middleware example in the Deno Fresh documentation not work for anyone else?I tried to follow the example in Deno Fresh about middlewares, but it doesn't work, the content `ctxDeno Cache locationHello, I am trying to cache my dependencies during the compile action using Github Actions, where doHow to build rusty_v8 with old v8 version?Hello all ! I am trying to build rusty_v8 (https://github.com/denoland/rusty_v8) with an old versionCan someone explain WebSocketStream to me?Whenever I try to connect using WebSocketStream and the host doesn't respond, my program inevitably deno doc: error: fp-tsthat's pretty much all I have :/ Running `deno doc Server.ts` only yields `error: fp-ts`. Can I enabI want to add my package xmlbuilder2 to deno.landBut somebody already added it, albeit with his own forked repository (now non-existent, thus unusabl