D
Deno

help

Read one byte at a time from ReadableStream

Aabi2/11/2023
What would be the most obvious way to read a single byte from a ReadableStream at a time?
CcrowlKats2/11/2023
i assume its a readablestream with the type set to "bytes"?
Aabi2/11/2023
Well, I’m not sure about the bytes part, but assuming that, what would be the best way? Only thing I can think of is a custom transform stream which enqueues the source chunks as 1-byte chunks.
CcrowlKats2/11/2023
well, if it is a bytes stream, then you can do:
const reader = stream.getReader({ mode: "byob" });
const buf = new Uint8Array(1);
await reader.read(buf);
// buf now contains the single byte
const reader = stream.getReader({ mode: "byob" });
const buf = new Uint8Array(1);
await reader.read(buf);
// buf now contains the single byte
Aabi2/11/2023
Ah there I go, thanks! alright, so in my case, the source happens to be Deno.Conn and it seams that its readable is not a readable byte stream... and i can't figure out how to convert it into one 😦 i guess this is basically what i'm looking for: https://streams.spec.whatwg.org/#example-rbs-push or rather this one: https://streams.spec.whatwg.org/#example-rs-push-backpressure
CcrowlKats2/11/2023
it should be; let me check yea it is. what is not working?
Aabi2/11/2023
const listener = Deno.listen({ port: 8888 })
for await (const conn of listener) {
const source = conn.readable
source.pipeThrough(
new TransformStream({
transform: (chunk, controller) => {
controller.enqueue(chunk)
},
}),
).getReader({ mode: "byob" })
}
const listener = Deno.listen({ port: 8888 })
for await (const conn of listener) {
const source = conn.readable
source.pipeThrough(
new TransformStream({
transform: (chunk, controller) => {
controller.enqueue(chunk)
},
}),
).getReader({ mode: "byob" })
}
then i just make a request: $ curl -v 127.0.0.1:8888
error: Uncaught TypeError: Cannot use a BYOB reader with a non-byte stream
).getReader({ mode: "byob" })
^
at setUpReadableStreamBYOBReader (internal:ext/web/06_streams.js:3344:13)
at acquireReadableStreamBYOBReader (internal:ext/web/06_streams.js:307:5)
at ReadableStream.getReader (internal:ext/web/06_streams.js:4784:16)
at file:///Users/abi/Code/ozaman/think.ts:10:5
error: Uncaught TypeError: Cannot use a BYOB reader with a non-byte stream
).getReader({ mode: "byob" })
^
at setUpReadableStreamBYOBReader (internal:ext/web/06_streams.js:3344:13)
at acquireReadableStreamBYOBReader (internal:ext/web/06_streams.js:307:5)
at ReadableStream.getReader (internal:ext/web/06_streams.js:4784:16)
at file:///Users/abi/Code/ozaman/think.ts:10:5
is it the .pipeThrough that's causing issues?
CcrowlKats2/11/2023
oh yea, that is indeed the issue sadly TransformStreams currently dont properly support byte streams :(
Aabi2/11/2023
ah well :/ then i'm going to have to take a different route i guess
CcrowlKats2/11/2023
GitHub
TransformStream byte streams · Issue #616 · whatwg/streams
@isonmad submitted a pull request #601 to support asymmetric default->byte TransformStream objects. Although only default->default and default->byte transforms are possible at ...
Aabi2/11/2023
thanks i have a tcp server, and clients will be sending both "string messages" and binary content at different times. i would like to have two readable streams, sourced from the same tcp client. one for text and one for binary. i want them to never read the same data twice. i need to be able to switch the "active mode" at any given moment. how would you go about this personally in deno? for example a client would send something like this
HELLO.
NICE WEATHER.
BLOB.
<...binary content...>
<binary sequence [13, 10, 13, 10] found>
THANKS.
BYE.
HELLO.
NICE WEATHER.
BLOB.
<...binary content...>
<binary sequence [13, 10, 13, 10] found>
THANKS.
BYE.
CcrowlKats2/11/2023
do these binary messages possibly contain the bytes for newlines? and same question for strings
Aabi2/11/2023
string messages are delimited by CRLF. binary content will contain CRLF, but that's not necessarily their delimiter. CRLF, period, CRLF, is. in other words, [13, 10, 46, 13, 10] would be the delimiter byte sequence for binary content and [13, 10] for text content.
CcrowlKats2/11/2023
hm it would be a lot easier if they both had the same delimiter, and then you could use https://deno.land/std@0.177.0/streams/mod.ts?s=DelimiterStream, and have the first byte of each message tell you the type of message, and use a transformstream to check for this indentifying byte and if its a string, decode the chunk i guess what you could do is the delimiter would be [13, 10] & instead of first byte, check if the chunk ends with [13, 10 46] to tell if its a buf or not
Aabi2/11/2023
unfortunately i didn't define the protocol, but i actually made a PR for adding some functionality to DelimiterStream, hoping it will make it past review. not sure if it would help here but... you're making a lot of sense right now, i think i'm getting a bit ahead of myself. i'll give it another go without tee'ing and transforming unnecessarily.

Looking for more? Join the community!

Recommended Posts
My deno.lock file exceeds 4000 lines of JSON nowIs this common for application projects? Is there any nice way to clean it up?importing packages breaks typesIm importing `Receiver` from https://deno.land/x/upstash_qstash@v0.3.2. but as soon as i import thisBug report: deno.land website returns a 500 error for JS filesOn https://deno.land, I noticed that the Node vs Deno comparison didn't seem to do anything. From Deno stopps at file.read()I am currently working on a way to communicate with a COM device. I can successfully open and read f[Resolved][Fresh] How to load data asynchronously after rendering a page ?Hi ! I was wondering if someone could help me understand the paradigm to load data asynchronously afSupabase Auth UI in FreshI'm building a simple web. I'd like to use Supabase's Auth UI, namely the `Auth` component, for handAny tool that will complie my TS code to JS?I have coded a website in typescript using Deno, but I want to convert it to JS code. I know the DenHow to get remote IP address when using Deno.serve?I'm using connInfo when using Deno from STD but the native http server doesn't seem to have such intTwo questions on documentationRTFM in 3... 2... 🙂 Question 1: How do i hide documentation for specific exports, i do not want Is there any simple way to keep track of the heap/stack usage of a running Deno application?I have made a Deno application and I'm curious to know if there's heap/stack issues that I should fiusing data from one deno bench in anotheris there any good way to re-use the results generated in one deno bench in another one? example: `docker compose up of Oak server throws: SyntaxError: Duplicate export of 'type'I am just learning how to use Docker, and am trying to put up a container of an Oak server. When runPublishing Deno Module with Bin (Answered)I know there's a way to include a `bin/` when publishing a module and then perform some kind of `denUse local tgz file instead of tgz from npm registryHello, Is it possible to reference a local tgz file inside a deno project (like calling `npm instalvscode LSP being a disgraceful little bitchfor no reason obvious to me, vscode and deno's lsp has been acting up lately. any ideas what could bIs there a way to fetch() with headers, WITHOUT forcing header keys to lowercase?Title. I'm trying to create an API wrapper for a service which uses "Incapsula" as their firewall orHow do I use std/log?I'm trying to create a log file. I don't seem to be able to make it work tho. ```ts import { handleop_ffi_cstr_read replacement now that deno.core.ops is gone?The latest release removed Deno.core.ops, which a library of mine relied on to use `op_ffi_cstr_readNPM Import results in Rust panicLooking to import the Airgram NPM package because I don't want to write it from scratch at this momeDeno fmt conflicting with Deno lint, when using IIFE```ts // before formating ((): void => { /* code */ })() ``` after formating with deno fmt, it adds