Skip to content

Commit

Permalink
Better response.arrayBuffer() handling in Node (#3860)
Browse files Browse the repository at this point in the history
* Better response.arrayBuffer() handling in Node

* Adds a changeset
  • Loading branch information
matthewp authored Jul 8, 2022
1 parent ed8161f commit c2c4e5c
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-pants-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes response.arrayBuffer() handling in large pages
14 changes: 11 additions & 3 deletions packages/astro/src/runtime/server/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,19 @@ function createResponseClass() {
async arrayBuffer(): Promise<ArrayBuffer> {
if (this.#isStream && isNodeJS) {
let body = this.#body as AsyncIterable<Uint8Array>;
let chunks: number[] = [];
let chunks: Uint8Array[] = [];
let len = 0;
for await (let chunk of body) {
chunks.push(...chunk);
chunks.push(chunk);
len += chunk.length;
}
return Uint8Array.from(chunks);
let ab = new Uint8Array(len);
let offset = 0;
for(const chunk of chunks) {
ab.set(chunk, offset);
offset += chunk.length;
}
return ab;
}
return super.arrayBuffer();
}
Expand Down
7 changes: 7 additions & 0 deletions packages/astro/test/fixtures/large-array/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'astro/config';
import solid from '@astrojs/solid-js';

// https://astro.build/config
export default defineConfig({
integrations: [solid()],
});
9 changes: 9 additions & 0 deletions packages/astro/test/fixtures/large-array/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/large-array-solid",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/solid-js": "workspace:*",
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createSignal } from 'solid-js';

export default function Counter({ children, largeProp }) {
const [count, setCount] = createSignal(0);
const add = () => setCount(count() + 1);
const subtract = () => setCount(count() - 1);

return (
<>
<div class="counter">
<button onClick={subtract}>-</button>
<pre>{count()}</pre>
<button onClick={add}>+</button>
</div>
<div class="counter-message">{children}</div>
</>
);
}
32 changes: 32 additions & 0 deletions packages/astro/test/fixtures/large-array/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
import Counter from '../components/Counter.jsx';
const largeArray = []
for (let i = 0; i < 600; i++) {
largeArray.push({ a: 'abc', b: 'abc', c: 'abc', d: 'abc', e: 'abc', foo: 'bar' })
}
---

<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<style>
html,
body {
font-family: system-ui;
margin: 0;
}
body {
padding: 2rem;
}
</style>
</head>
<body>
<main>
<Counter client:visible largeProp={largeArray}>
<h1>Hello, Solid!</h1>
</Counter>
</main>
</body>
</html>
35 changes: 35 additions & 0 deletions packages/astro/test/ssr-large-array.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
import testAdapter from './test-adapter.js';

describe('SSR with Large Array and client rendering', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/large-array/',
experimental: {
ssr: true,
},
adapter: testAdapter(),
});
await fixture.build();
});

it('Using response.arrayBuffer() gets the right HTML', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/');
const response = await app.render(request);
const data = await response.arrayBuffer();
const html = new TextDecoder().decode(data);

const $ = cheerio.load(html);
expect($('head meta[name="viewport"]')).to.have.a.lengthOf(1);
expect($('head link[rel="icon"]')).to.have.a.lengthOf(1);
expect($('main')).to.have.a.lengthOf(1);
expect($('astro-island')).to.have.a.lengthOf(1);
expect($('h1').text()).to.equal('Hello, Solid!');
});
});
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c2c4e5c

Please sign in to comment.