Skip to content

Commit

Permalink
fix: wrong response when repeated requests for external source
Browse files Browse the repository at this point in the history
  • Loading branch information
b6pzeusbc54tvhw5jgpyw8pwz2x6gs committed Mar 14, 2021
1 parent 6f159de commit f836be6
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ node_modules
**/.terraform/*
*.tfstate
*.tfstate.*

# IntelliJ IDEA
/.idea/
13 changes: 13 additions & 0 deletions lib/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import S3 from 'aws-sdk/clients/s3';

import { imageOptimizer, S3Config } from './image-optimizer';
import { normalizeHeaders } from './normalized-headers';
import { createDeferred } from './utils';

function generateS3Config(bucketName?: string): S3Config | undefined {
let s3: S3;
Expand Down Expand Up @@ -81,6 +82,8 @@ export async function handler(

const resBuffers: Buffer[] = [];
const resMock: any = new Writable();
const defer = createDeferred();
let didCallEnd = false;

resMock.write = (chunk: Buffer | string) => {
resBuffers.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
Expand All @@ -98,6 +101,13 @@ export async function handler(
(mockHeaders[name.toLowerCase()] = value);
resMock._implicitHeader = () => {};

resMock.originalEnd = resMock.end;
resMock.on('close', () => defer.resolve());
resMock.end = (message: any) => {
didCallEnd = true;
resMock.originalEnd(message);
};

const parsedUrl = parseUrl(reqMock.url, true);
const result = await imageOptimizer(
imageConfig,
Expand All @@ -123,6 +133,9 @@ export async function handler(
normalizedHeaders['cache-control'] = 'public, max-age=60';
}

if (didCallEnd) defer.resolve();
await defer.promise;

return {
statusCode: resMock.statusCode || 200,
body: Buffer.concat(resBuffers).toString('base64'),
Expand Down
19 changes: 19 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export interface Deferred<T = never> {
promise: Promise<T>;
resolve: (value?: T | PromiseLike<T>) => void;
reject: (reason?: any) => void;
}

export function createDeferred<T>() {
let r;
let j;

const promise = new Promise<T>(
(resolve: (value: any) => void, reject: (reason?: any) => void): void => {
r = resolve;
j = reject;
}
);

return ({ promise, resolve: r, reject: j } as unknown) as Deferred<T>;
}
2 changes: 1 addition & 1 deletion test/image-optimizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import S3 from 'aws-sdk/clients/s3';
import * as path from 'path';

import { imageOptimizer, S3Config } from '../lib/image-optimizer';
import { createDeferred } from './utils';
import { createDeferred } from '../lib/utils';
import { s3PublicDir } from './utils/s3-public-dir';
import { acceptAllFixtures, acceptWebpFixtures } from './constants';

Expand Down

0 comments on commit f836be6

Please sign in to comment.