Skip to content

Commit

Permalink
feat(response): added support for next.js ssr response data in Uint8A…
Browse files Browse the repository at this point in the history
…rray
  • Loading branch information
H4ad committed Jul 22, 2022
1 parent 6055f8c commit 9202ed0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/network/request.ts
Expand Up @@ -31,7 +31,7 @@ export interface ServerlessRequestProps {
/**
* The body from the event source
*/
body?: Buffer;
body?: Buffer | Uint8Array;

/**
* The IP Address from caller
Expand Down Expand Up @@ -81,5 +81,5 @@ export class ServerlessRequest extends IncomingMessage {
}

ip?: string;
body?: Buffer;
body?: Buffer | Uint8Array;
}
7 changes: 6 additions & 1 deletion src/network/response.ts
Expand Up @@ -11,11 +11,16 @@ const HEADERS = Symbol('Response headers');
function getString(data: Buffer | string | unknown) {
if (Buffer.isBuffer(data)) return data.toString('utf8');
else if (typeof data === 'string') return data;
else if (data instanceof Uint8Array) return new TextDecoder().decode(data);
else throw new Error(`response.write() of unexpected type: ${typeof data}`);
}

function addData(stream: ServerlessResponse, data: Uint8Array | string) {
if (Buffer.isBuffer(data) || typeof data === 'string')
if (
Buffer.isBuffer(data) ||
typeof data === 'string' ||
data instanceof Uint8Array
)
stream[BODY].push(Buffer.from(data));
else throw new Error(`response.write() of unexpected type: ${typeof data}`);
}
Expand Down
23 changes: 21 additions & 2 deletions test/network/response.spec.ts
Expand Up @@ -59,6 +59,13 @@ describe('ServerlessResponse', () => {
body: '{"test": true}' as any,
});

const requestWithUintArray = new ServerlessRequest({
...defaultParams,
body: Uint8Array.from(
Array.from('{"test": true}').map(c => c.charCodeAt(0)),
),
});

const requestHead = new ServerlessRequest({
...defaultParams,
method: 'HEAD',
Expand All @@ -72,6 +79,7 @@ describe('ServerlessResponse', () => {
[request, 0, true],
[requestWithBody, requestWithBody.body!.length, true],
[requestWithBodyString, requestWithBodyString.body!.length, true],
[requestWithUintArray, requestWithUintArray.body!.length, true],
[requestHead, 0, false],
];

Expand Down Expand Up @@ -107,9 +115,16 @@ describe('ServerlessResponse', () => {
});

it('should can pipe response and return correct data', async () => {
const options: [value: string | Buffer, expectedValue: string][] = [
const options: [
value: string | Buffer | Uint8Array,
expectedValue: string,
][] = [
['test', 'test'],
[Buffer.from('{"yo": true}', 'utf-8'), '{"yo": true}'],
[
Uint8Array.from(Array.from('{"test": true}').map(c => c.charCodeAt(0))),
'{"test": true}',
],
];

for (const [testedData, expectedValue] of options) {
Expand Down Expand Up @@ -163,7 +178,11 @@ describe('ServerlessResponse', () => {
});

it('should call correctly the callback with valid data in response', () => {
const options = ['test', Buffer.from('testB', 'utf-8')];
const options = [
'test',
Buffer.from('testB', 'utf-8'),
Uint8Array.from(Array.from('{"test": true}').map(c => c.charCodeAt(0))),
];

for (const testedData of options) {
const response = new ServerlessResponse({
Expand Down

0 comments on commit 9202ed0

Please sign in to comment.