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

Adds support base64 encoding in Netlify Functions #3592

Merged
merged 5 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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/hot-pumas-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/netlify': patch
---

Adds support for base64 encoded responses in Netlify Functions
4 changes: 4 additions & 0 deletions packages/integrations/netlify/src/netlify-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ interface Args {}
export const createExports = (manifest: SSRManifest, args: Args) => {
const app = new App(manifest);

const base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;

const handler: Handler = async (event) => {
const { httpMethod, headers, rawUrl, body: requestBody, isBase64Encoded } = event;
const init: RequestInit = {
Expand All @@ -35,12 +37,14 @@ export const createExports = (manifest: SSRManifest, args: Args) => {

const response: Response = await app.render(request);
const responseBody = await response.text();
const responseIsBase64 = base64Regex.test(responseBody);

const responseHeaders = Object.fromEntries(response.headers.entries());
const fnResponse: any = {
statusCode: response.status,
headers: responseHeaders,
body: responseBody,
isBase64Encoded: responseIsBase64,
};

// Special-case set-cookie which has to be set an different way :/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { expect } from 'chai';
import { load as cheerioLoad } from 'cheerio';
import { loadFixture, testIntegration } from './test-utils.js';
import netlifyAdapter from '../../dist/index.js';
import { fileURLToPath } from 'url';

describe('Base64 Responses', () => {
/** @type {import('../../../astro/test/test-utils').Fixture} */
let fixture;

before(async () => {
fixture = await loadFixture({
root: new URL('./fixtures/base64-response/', import.meta.url).toString(),
experimental: {
ssr: true,
},
adapter: netlifyAdapter({
dist: new URL('./fixtures/base64-response/dist/', import.meta.url),
}),
site: `http://example.com`,
integrations: [testIntegration()],
});
await fixture.build();
});

it('Can return base64 encoded strings', async () => {
const entryURL = new URL(
'./fixtures/base64-response/.netlify/functions-internal/entry.mjs',
import.meta.url
);
const { handler } = await import(entryURL);
const resp = await handler({
httpMethod: 'GET',
headers: {},
rawUrl: 'http://example.com/image',
body: '{}',
isBase64Encoded: false,
});
expect(resp.statusCode, 'successful response').to.equal(200);
expect(resp.isBase64Encoded, 'includes isBase64Encoded flag').to.be.true;

const buffer = Buffer.from(resp.body, 'base64');
expect(buffer.toString(), 'decoded base64 string matches').to.equal('base64 test string');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

export function get() {
const buffer = Buffer.from('base64 test string', 'utf-8')

return new Response(buffer.toString('base64'), {
status: 200
});
}