-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support base64 encoding in Netlify Functions (#3592)
* Adding support for base64 encoded responses in Netlify Functions * chore: add changeset * removing the regex check for a more simple header-based check * nit: cleaning up the readme a bit
- Loading branch information
Tony Sullivan
authored
Jun 15, 2022
1 parent
8ed924d
commit 0ddcef2
Showing
7 changed files
with
169 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/integrations/netlify/test/functions/base64-response.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { expect } from 'chai'; | ||
import { loadFixture, testIntegration } from './test-utils.js'; | ||
import netlifyAdapter from '../../dist/index.js'; | ||
|
||
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), | ||
binaryMediaTypes: ['font/otf'] | ||
}), | ||
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'); | ||
}); | ||
|
||
it('Can define custom binaryMediaTypes', 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/font', | ||
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 font'); | ||
}); | ||
}); |
11 changes: 11 additions & 0 deletions
11
packages/integrations/netlify/test/functions/fixtures/base64-response/src/pages/font.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
export function get() { | ||
const buffer = Buffer.from('base64 test font', 'utf-8') | ||
|
||
return new Response(buffer, { | ||
status: 200, | ||
headers: { | ||
'Content-Type': 'font/otf' | ||
} | ||
}); | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/integrations/netlify/test/functions/fixtures/base64-response/src/pages/image.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
export function get() { | ||
const buffer = Buffer.from('base64 test string', 'utf-8') | ||
|
||
return new Response(buffer, { | ||
status: 200, | ||
headers: { | ||
'content-type': 'image/jpeg;foo=foo' | ||
} | ||
}); | ||
} |