Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide more examples #4

Open
ShookLyngs opened this issue Nov 5, 2023 · 1 comment
Open

Provide more examples #4

ShookLyngs opened this issue Nov 5, 2023 · 1 comment

Comments

@ShookLyngs
Copy link

ShookLyngs commented Nov 5, 2023

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!

@corrideat
Copy link
Member

Hi @ShookLyngs!

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';

const ABtoU8 = new TransformStream({
	start() {},
	transform(chunk, controller) {
		controller.enqueue(new Uint8Array(chunk));
	},
});
encodeMultipartMessage('boundary', [
	{
		headers: new Headers({ 'test-header': 'example-value' }),
		body: new TextEncoder().encode('sample body'),
	},
]).pipeThrough(ABtoU8);
new Response(ABtoU8.readable)
	.arrayBuffer()
	.then((msg) => new Uint8Array(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';

const reader = encodeMultipartMessage('boundary', [
	{
		headers: new Headers({ 'test-header': 'example-value' }),
		body: new TextEncoder().encode('sample body'),
	},
]).getReader();

const chunks = [];
for (;;) {
	const { done, value } = await reader.read();
	if (done) break;
	chunks.push(chunk);
}

let position = 0;
const size = chunks.reduce((acc, cv) => acc + cv.byteLength, 0);
const buffer = new ArrayBuffer(size);
const combined = new Uint8Array(buffer);

for (let chunk of chunks) {
    combined.set(new Uint8Array(chunk), position);
    position += chunk.byteLength;
}

// Result is in `combined`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants