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

Adding an option to disable HTTP streaming #3777

Merged
merged 18 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
handling string responses in the server runtime, adding tests
  • Loading branch information
Tony Sullivan committed Jun 30, 2022
commit 884fab87a4432316b3079e8b86c8983a2eec353f
3 changes: 3 additions & 0 deletions packages/astro/src/runtime/server/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ type CreateResponseFn = (body?: BodyInit | null, init?: ResponseInit) => Respons

export const createResponse: CreateResponseFn = isNodeJS
? (body, init) => {
if (typeof body === 'string') {
return new Response(body, init);
}
if (typeof StreamingCompatibleResponse === 'undefined') {
return new (createResponseClass())(body, init);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/vite-plugin-astro-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ async function writeWebResponse(res: http.ServerResponse, webResponse: Response)
} else if (body instanceof Readable) {
body.pipe(res);
return;
} else if (typeof body === 'string') {
res.write(body);
} else {
const reader = body.getReader();
while (true) {
Expand Down
64 changes: 64 additions & 0 deletions packages/astro/test/streaming.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,67 @@ describe('Streaming', () => {
});
});
});

describe('Streaming disabled', () => {
if (isWindows) return;

/** @type {import('./test-utils').Fixture} */
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/streaming/',
adapter: testAdapter(),
experimental: {
ssr: true,
},
server: {
streaming: false,
}
});
});

describe('Development', () => {
/** @type {import('./test-utils').DevServer} */
let devServer;

before(async () => {
devServer = await fixture.startDevServer();
});

after(async () => {
await devServer.stop();
});

it('Body is not chunked', async () => {
let res = await fixture.fetch('/');

expect(res.status).to.equal(200);
expect(res.headers.get('content-type')).to.equal('text/html');

let body = await res.text();
expect(body.startsWith('<!DOCTYPE html>')).to.equal(true);
});
});

describe('Production', () => {
before(async () => {
await fixture.build();
});

it('Can get the full html body', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http:https://example.com/');
const response = await app.render(request);

expect(response.status).to.equal(200);
expect(response.headers.get('content-type')).to.equal('text/html');

const html = await response.text();
const $ = cheerio.load(html);

expect($('header h1')).to.have.a.lengthOf(1);
expect($('ul li')).to.have.a.lengthOf(10);
});
});
});