0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

Fixes API route compat with Node 14 (#2936)

* Fixes API route compat with Node 14

* Changeset
This commit is contained in:
Matthew Phillips 2022-03-29 16:59:06 -04:00 committed by GitHub
parent 23b8ee2738
commit f048cdff70
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 7 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes non-GET API routes in dev with Node 14

View file

@ -15,6 +15,7 @@ import serverErrorTemplate from '../template/5xx.js';
import { RouteCache } from '../core/render/route-cache.js'; import { RouteCache } from '../core/render/route-cache.js';
import { fixViteErrorMessage } from '../core/errors.js'; import { fixViteErrorMessage } from '../core/errors.js';
import { createRequest } from '../core/request.js'; import { createRequest } from '../core/request.js';
import { Readable } from 'stream';
interface AstroPluginOptions { interface AstroPluginOptions {
config: AstroConfig; config: AstroConfig;
@ -44,12 +45,17 @@ async function writeWebResponse(res: http.ServerResponse, webResponse: Response)
const { status, headers, body } = webResponse; const { status, headers, body } = webResponse;
res.writeHead(status, Object.fromEntries(headers.entries())); res.writeHead(status, Object.fromEntries(headers.entries()));
if (body) { if (body) {
const reader = body.getReader(); if(body instanceof Readable) {
while (true) { body.pipe(res);
const { done, value } = await reader.read(); return;
if (done) break; } else {
if (value) { const reader = body.getReader();
res.write(value); while (true) {
const { done, value } = await reader.read();
if (done) break;
if (value) {
res.write(value);
}
} }
} }
} }
@ -134,7 +140,7 @@ async function handleRequest(
await new Promise((resolve) => { await new Promise((resolve) => {
req.setEncoding('utf-8'); req.setEncoding('utf-8');
req.on('data', (bts) => bytes.push(bts)); req.on('data', (bts) => bytes.push(bts));
req.on('close', resolve); req.on('end', resolve);
}); });
body = new TextEncoder().encode(bytes.join('')).buffer; body = new TextEncoder().encode(bytes.join('')).buffer;
} }

View file

@ -8,3 +8,13 @@ export function get() {
]) ])
}; };
} }
export async function post(params, request) {
const body = await request.text();
return new Response(body === `some data` ? `ok` : `not ok`, {
status: 200,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
}

View file

@ -36,4 +36,25 @@ describe('API routes in SSR', () => {
const body = await response.json(); const body = await response.json();
expect(body.length).to.equal(3); expect(body.length).to.equal(3);
}); });
describe('Dev', () => {
let devServer;
before(async () => {
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('Can POST to API routes', async () => {
const response = await fixture.fetch('/food.json', {
method: 'POST',
body: `some data`
})
expect(response.status).to.equal(200);
const text = await response.text();
expect(text).to.equal(`ok`);
});
});
}); });