-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
feat: inline Response.arrayBuffer inside load functions during ssr #10535
Changes from all commits
d81597e
bcffb55
e9a0930
2d7e40c
11eeb3e
87f4c0f
7eea57e
a41b485
5d1e3cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/kit': minor | ||
--- | ||
|
||
feat: inline `response.arrayBuffer()` during ssr |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -189,6 +189,25 @@ export async function load_data({ | |
return data; | ||
} | ||
|
||
/** | ||
* @param {ArrayBuffer} buffer | ||
* @returns {string} | ||
*/ | ||
function b64_encode(buffer) { | ||
if (globalThis.Buffer) { | ||
return Buffer.from(buffer).toString('base64'); | ||
} | ||
|
||
const little_endian = new Uint8Array(new Uint16Array([1]).buffer)[0] > 0; | ||
|
||
// The Uint16Array(Uint8Array(...)) ensures the code points are padded with 0's | ||
return btoa( | ||
new TextDecoder(little_endian ? 'utf-16le' : 'utf-16be').decode( | ||
new Uint16Array(new Uint8Array(buffer)) | ||
) | ||
); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in my own tests using kit's existing base64 worked well without needing all this. But maybe I was missing a use case. https://github.com/sveltejs/kit/blob/master/packages/kit/src/runtime/server/page/crypto.js#L210-L239 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Node 20.6.1 |
||
|
||
/** | ||
* @param {Pick<import('@sveltejs/kit').RequestEvent, 'fetch' | 'url' | 'request' | 'route'>} event | ||
* @param {import('types').SSRState} state | ||
|
@@ -245,38 +264,33 @@ export function create_universal_fetch(event, state, fetched, csr, resolve_opts) | |
|
||
const proxy = new Proxy(response, { | ||
get(response, key, _receiver) { | ||
async function text() { | ||
const body = await response.text(); | ||
|
||
if (!body || typeof body === 'string') { | ||
const status_number = Number(response.status); | ||
if (isNaN(status_number)) { | ||
throw new Error( | ||
`response.status is not a number. value: "${ | ||
response.status | ||
}" type: ${typeof response.status}` | ||
); | ||
} | ||
|
||
fetched.push({ | ||
url: same_origin ? url.href.slice(event.url.origin.length) : url.href, | ||
method: event.request.method, | ||
request_body: /** @type {string | ArrayBufferView | undefined} */ ( | ||
input instanceof Request && cloned_body | ||
? await stream_to_string(cloned_body) | ||
: init?.body | ||
), | ||
request_headers: cloned_headers, | ||
response_body: body, | ||
response | ||
}); | ||
} | ||
|
||
if (dependency) { | ||
dependency.body = body; | ||
/** | ||
* @param {string} body | ||
* @param {boolean} is_b64 | ||
*/ | ||
async function push_fetched(body, is_b64) { | ||
const status_number = Number(response.status); | ||
if (isNaN(status_number)) { | ||
throw new Error( | ||
`response.status is not a number. value: "${ | ||
response.status | ||
}" type: ${typeof response.status}` | ||
); | ||
} | ||
|
||
return body; | ||
fetched.push({ | ||
url: same_origin ? url.href.slice(event.url.origin.length) : url.href, | ||
method: event.request.method, | ||
request_body: /** @type {string | ArrayBufferView | undefined} */ ( | ||
input instanceof Request && cloned_body | ||
? await stream_to_string(cloned_body) | ||
: init?.body | ||
), | ||
request_headers: cloned_headers, | ||
response_body: body, | ||
response, | ||
is_b64 | ||
}); | ||
} | ||
|
||
if (key === 'arrayBuffer') { | ||
|
@@ -287,13 +301,28 @@ export function create_universal_fetch(event, state, fetched, csr, resolve_opts) | |
dependency.body = new Uint8Array(buffer); | ||
} | ||
|
||
// TODO should buffer be inlined into the page (albeit base64'd)? | ||
// any conditions in which it shouldn't be? | ||
if (buffer instanceof ArrayBuffer) { | ||
await push_fetched(b64_encode(buffer), true); | ||
} | ||
|
||
return buffer; | ||
}; | ||
} | ||
|
||
async function text() { | ||
const body = await response.text(); | ||
|
||
if (!body || typeof body === 'string') { | ||
await push_fetched(body, false); | ||
} | ||
|
||
if (dependency) { | ||
dependency.body = body; | ||
} | ||
|
||
return body; | ||
} | ||
|
||
if (key === 'text') { | ||
return text; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export async function load({ fetch }) { | ||
const res = await fetch('/load/fetch-arraybuffer-b64/data'); | ||
|
||
const l = await fetch('/load/fetch-arraybuffer-b64/data', { | ||
body: Uint8Array.from(Array(256).fill(0), (_, i) => i), | ||
method: 'POST' | ||
}); | ||
|
||
return { | ||
data: res.arrayBuffer(), | ||
data_long: l.arrayBuffer() | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<script> | ||
export let data; | ||
|
||
$: arr = [...new Uint8Array(data.data)]; | ||
|
||
let ok = 'Ok'; | ||
|
||
$: { | ||
const p = new Uint8Array(data.data_long); | ||
ok = p.length === 256 ? 'Ok' : 'Wrong length'; | ||
|
||
if (p.length === 256) { | ||
for (let i = 0; i < p.length; i++) { | ||
if (p[i] !== i) { | ||
ok = `Expected ${i} but got ${p[i]}`; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
<span class="test-content">{JSON.stringify(arr)}</span> | ||
|
||
<br /> | ||
|
||
{ok} | ||
<span style="word-wrap: break-word;"> | ||
{JSON.stringify([...new Uint8Array(data.data_long)])} | ||
</span> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const GET = () => { | ||
return new Response(new Uint8Array([1, 2, 3, 4])); | ||
}; | ||
|
||
export const POST = async ({ request }) => { | ||
return new Response(await request.arrayBuffer()); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uint8Array.from
could be optimized under the hood?I think this is equivalent
Uint8Array.from(atob(text), (c) => c.charCodeAt(0))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did some testing on node v20.5.1 Ryzen 9 5950X 32GB
Uint8Array.from(atob(text), (c) => c.charCodeAt(0))
takes ~74ms to decode 1MB (out) of datatakes ~50ms
And using the for loop, ~17ms
Uint8Array.from(atob(text), (c) => c.charCodeAt(0))
is the only one with any major gc