ChilliSniff
ChilliSniff3y ago

Request.body.getReader() into Uint8Array typed array

is it possible to convert a readableStream into a Uint8Array ?
6 Replies
ChilliSniff
ChilliSniffOP3y ago
the way i am doing it now is to write a file and then read it as Uint8Array
ensureFileSync(s_path);

const reader = o_request?.body?.getReader();

// await Deno.writeTextFile(s_path, "test");
const f = await Deno.open(s_path, {
create: true,
write: true,
});

await Deno.copy(readerFromStreamReader(reader), f);
await f.close();
let o_stat = await Deno.stat(s_path);
console.log(o_stat);

const file = await Deno.open(s_path);
const p = 0; // your position
await Deno.seek(file.rid, p, Deno.SeekMode.Start);

const buf = new Uint8Array(o_stat.size); // you can read more and then access a particular slice of the buffer
const bytesRead = await file.read(buf);
console.log(bytesRead);
console.log(buf);
ensureFileSync(s_path);

const reader = o_request?.body?.getReader();

// await Deno.writeTextFile(s_path, "test");
const f = await Deno.open(s_path, {
create: true,
write: true,
});

await Deno.copy(readerFromStreamReader(reader), f);
await f.close();
let o_stat = await Deno.stat(s_path);
console.log(o_stat);

const file = await Deno.open(s_path);
const p = 0; // your position
await Deno.seek(file.rid, p, Deno.SeekMode.Start);

const buf = new Uint8Array(o_stat.size); // you can read more and then access a particular slice of the buffer
const bytesRead = await file.read(buf);
console.log(bytesRead);
console.log(buf);
i found out that i can do it this way
let o_array_buffer = await o_request?.arrayBuffer();
let a_n_iu8 = new Uint8Array(o_array_buffer, 10);
let o_array_buffer = await o_request?.arrayBuffer();
let a_n_iu8 = new Uint8Array(o_array_buffer, 10);
but i dont get why my Uint8Array has a length of 118...
Uint8Array(118) [
75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 65, 66, 67,
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82,
83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
113, 114, 115, 116, 65, 66, 67, 68, 69, 70,
... 18 more items
]
Uint8Array(118) [
75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 65, 66, 67,
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82,
83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
113, 114, 115, 116, 65, 66, 67, 68, 69, 70,
... 18 more items
]
nayeemrmn
nayeemrmn3y ago
@kt3k Shouldn't https://deno.land/std@0.157.0/streams/conversion.ts?s=readAll do this? Reader | ReadableStream
kt3k
kt3k3y ago
I agree with having utility for that (read all contents from ReadableStream) though I'm not sure if we should overload it to readAll
nayeemrmn
nayeemrmn3y ago
But I thought streams was mostly whatwg streams replacement for io, I was surprised to see it didn't already do that when I was about to link it here
kt3k
kt3k3y ago
Ok. Fair Enough. Let's add readable stream support to readAll. (The answer to the original question) @( ¬‿¬) I think you can use new Uint8Array(await o_request.arrayBuffer())
ChilliSniff
ChilliSniffOP3y ago
thanks