You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, the lib is very cool! But I find it lacks of usage/example about how to use the encodeMultipartMessage method.
Could you please provide more code examples on how to encode multipart messages, and convert the resulting ReadableStream into a utf-8 renderable string?
Thanks!
The text was updated successfully, but these errors were encountered:
Thanks for opening this issue. We will look into adding some more documentation, including examples.
In the meantime, you can find examples in the tests/ directory. A quick (but not necessarily the most efficient) way of converting a ReadableStream to an Uint8Array is through the Response class:
import{encodeMultipartMessage}from'@exact-realty/multipart-parser';constABtoU8=newTransformStream({start(){},transform(chunk,controller){controller.enqueue(newUint8Array(chunk));},});encodeMultipartMessage('boundary',[{headers: newHeaders({'test-header': 'example-value'}),body: newTextEncoder().encode('sample body'),},]).pipeThrough(ABtoU8);newResponse(ABtoU8.readable).arrayBuffer().then((msg)=>newUint8Array(msg)).then(console.log);// This is the resulting Uint8Array
I say it's not the most efficient because this is converting between Uint8Array and ArrayBuffer multiple times, at ABtoU8, then at Response and then at .arrayBuffer().
A better way would be something like:
import{encodeMultipartMessage}from'@exact-realty/multipart-parser';constreader=encodeMultipartMessage('boundary',[{headers: newHeaders({'test-header': 'example-value'}),body: newTextEncoder().encode('sample body'),},]).getReader();constchunks=[];for(;;){const{ done, value }=awaitreader.read();if(done)break;chunks.push(chunk);}letposition=0;constsize=chunks.reduce((acc,cv)=>acc+cv.byteLength,0);constbuffer=newArrayBuffer(size);constcombined=newUint8Array(buffer);for(letchunkofchunks){combined.set(newUint8Array(chunk),position);position+=chunk.byteLength;}// Result is in `combined`
Hi, the lib is very cool! But I find it lacks of usage/example about how to use the encodeMultipartMessage method.
Could you please provide more code examples on how to encode multipart messages, and convert the resulting ReadableStream into a utf-8 renderable string?
Thanks!
The text was updated successfully, but these errors were encountered: