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

Allow binary data to be returned from api routes in SSG #6180

Merged
merged 2 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/tame-poems-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Allow binary data to be returned from api routes in SSG
5 changes: 3 additions & 2 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ async function generatePath(
route: pageData.route,
});

let body: string;
let body: string | Uint8Array;
let encoding: BufferEncoding | undefined;
if (pageData.route.type === 'endpoint') {
const endpointHandler = mod as unknown as EndpointHandler;
Expand All @@ -392,7 +392,8 @@ async function generatePath(
throwIfRedirectNotAllowed(result.response, opts.settings.config);
// If there's no body, do nothing
if (!result.response.body) return;
body = await result.response.text();
const ab = await result.response.arrayBuffer();
body = new Uint8Array(ab);
} else {
body = result.body;
encoding = result.encoding;
Expand Down
8 changes: 8 additions & 0 deletions packages/astro/test/api-routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,12 @@ describe('API routes', () => {
});
});
});

describe('Binary data', () => {
it('can be returned from a response', async () => {
const dat = await fixture.readFile('/binary.dat', null);
expect(dat.length).to.equal(1);
expect(dat[0]).to.equal(0xff);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { APIRoute } from 'astro';

export const get: APIRoute = async function () {
return new Response(new Uint8Array([0xff]));
};
2 changes: 1 addition & 1 deletion packages/astro/test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export async function loadFixture(inlineConfig) {
},
pathExists: (p) => fs.existsSync(new URL(p.replace(/^\//, ''), config.outDir)),
readFile: (filePath, encoding) =>
fs.promises.readFile(new URL(filePath.replace(/^\//, ''), config.outDir), encoding ?? 'utf8'),
fs.promises.readFile(new URL(filePath.replace(/^\//, ''), config.outDir), encoding === undefined ? 'utf8' : encoding),
readdir: (fp) => fs.promises.readdir(new URL(fp.replace(/^\//, ''), config.outDir)),
glob: (p) =>
fastGlob(p, {
Expand Down