Skip to content

Commit a186e14

Browse files
authored
Merge pull request #132 from wchaws/dev2
chore: disable mime.lookup
2 parents 1584d31 + 69bbc6f commit a186e14

File tree

8 files changed

+24
-19
lines changed

8 files changed

+24
-19
lines changed

source/new-image-handler/package.json

-2
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,12 @@
4646
"typescript": "^4.6.4"
4747
},
4848
"dependencies": {
49-
"@types/mime-types": "^2.1.1",
5049
"aws-sdk": "^2.1130.0",
5150
"http-errors": "^1.8.1",
5251
"koa": "^2.13.4",
5352
"koa-bodyparser": "^4.3.0",
5453
"koa-logger": "^3.2.1",
5554
"koa-router": "^10.1.1",
56-
"mime-types": "^2.1.35",
5755
"sharp": "^0.30.4"
5856
},
5957
"bundledDependencies": [],

source/new-image-handler/src/index-lambda.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export const handler = WrapError(async (event: APIGatewayProxyEventV2): Promise<
1414
if (event.rawPath === '/' || event.rawPath === '/ping') {
1515
return resp(200, 'ok');
1616
} else if (event.rawPath === '/_debug') {
17-
return resp(400, debug());
17+
console.log(debug());
18+
return resp(400, 'Please check your server logs for more details!');
1819
}
1920

2021
const accept = event.headers.Accept ?? event.headers.accept ?? '';
@@ -31,11 +32,11 @@ export const handler = WrapError(async (event: APIGatewayProxyEventV2): Promise<
3132
context.features[Features.AutoWebp] = autoWebp;
3233
const { data, type } = await processor.process(context);
3334

34-
return resp(200, data, type);
35+
return resp(200, data, type, context.headers);
3536
} else {
36-
const { buffer, type } = await bs.get(uri, bypass);
37+
const { buffer, type, headers } = await bs.get(uri, bypass);
3738

38-
return resp(200, buffer, type);
39+
return resp(200, buffer, type, headers);
3940
}
4041
});
4142

@@ -44,7 +45,7 @@ function bypass() {
4445
throw new HttpErrors[403]('Please visit s3 directly');
4546
}
4647

47-
function resp(code: number, body: any, type?: string): APIGatewayProxyResultV2 {
48+
function resp(code: number, body: any, type?: string, headers?: { [key: string]: any }): APIGatewayProxyResultV2 {
4849
const isBase64Encoded = Buffer.isBuffer(body);
4950
let data: string = '';
5051
if (isBase64Encoded) {
@@ -60,7 +61,7 @@ function resp(code: number, body: any, type?: string): APIGatewayProxyResultV2 {
6061
return {
6162
isBase64Encoded,
6263
statusCode: code,
63-
headers: { 'Content-Type': type ?? 'text/plain' },
64+
headers: Object.assign({ 'Content-Type': type ?? 'text/plain' }, headers),
6465
body: data,
6566
};
6667
}

source/new-image-handler/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ router.get(['/', '/ping'], async (ctx) => {
5050
});
5151

5252
router.get(['/debug', '/_debug'], async (ctx) => {
53+
console.log(debug());
5354
ctx.status = 400;
54-
ctx.body = debug();
55+
ctx.body = 'Please check your server logs for more details!';
5556
});
5657

5758
router.get('/(.*)', async (ctx) => {

source/new-image-handler/src/processor/image/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as mime from 'mime-types';
21
import * as sharp from 'sharp';
32
import { Features, IAction, InvalidArgument, IProcessContext, IProcessor, IProcessResponse } from '../../processor';
43
import { IBufferStore } from '../../store';
@@ -158,7 +157,7 @@ export class ImageProcessor implements IProcessor {
158157
return { data: ctx.info, type: 'application/json' };
159158
} else {
160159
const { data, info } = await ctx.image.toBuffer({ resolveWithObject: true });
161-
return { data: data, type: (mime.lookup(info.format) || info.format) };
160+
return { data: data, type: 'image/' + info.format };
162161
}
163162
}
164163

source/new-image-handler/src/store.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ export class LocalStore implements IBufferStore {
4444
return {
4545
buffer: await fs.promises.readFile(p),
4646
type: filetype(p),
47-
headers: {},
47+
headers: {
48+
'Etag': 'fake-etag',
49+
'Last-Modified': 'fake-last-modified',
50+
'Cache-Control': 'no-cache',
51+
},
4852
};
4953
}
5054

@@ -71,7 +75,11 @@ export class S3Store implements IBufferStore {
7175
return {
7276
buffer: res.Body as Buffer,
7377
type: res.ContentType ?? '',
74-
headers: { 'Etag': res.ETag, 'Last-Modified': res.LastModified },
78+
headers: {
79+
'Etag': res.ETag,
80+
'Last-Modified': res.LastModified,
81+
'Cache-Control': res.CacheControl,
82+
},
7583
};
7684
};
7785
throw new Error('S3 response body is not a Buffer type');

source/new-image-handler/test/index-lambda.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ test('index-lambda.ts example.jpg?x-oss-process=image/resize,w_100/quality,q_50'
7373
expect(res.isBase64Encoded).toBeTruthy();
7474
expect(res.statusCode).toBe(200);
7575
expect(res.headers['Content-Type']).toBe('image/jpeg');
76+
expect(res.headers['Last-Modified']).toBe('fake-last-modified');
77+
expect(res.headers['Cache-Control']).toBe('no-cache');
7678

7779
const metadata = await sharp(Buffer.from(res.body, 'base64')).metadata();
7880

source/new-image-handler/test/processor/index.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ test('f.jpg?x-oss-process=image/resize,w_100/auto-orient,0', async () => {
235235
expect(type).toBe('image/jpeg');
236236
expect(metadata.width).toBe(100);
237237
expect(metadata.height).toBe(78);
238+
expect(ctx.headers['Last-Modified']).toBe('fake-last-modified');
238239
});
239240

240241
test('f.jpg?x-oss-process=image/resize,w_100/auto-orient,1', async () => {

source/new-image-handler/yarn.lock

+1-6
Original file line numberDiff line numberDiff line change
@@ -1195,11 +1195,6 @@
11951195
"@types/koa-compose" "*"
11961196
"@types/node" "*"
11971197

1198-
"@types/mime-types@^2.1.1":
1199-
version "2.1.1"
1200-
resolved "https://registry.yarnpkg.com/@types/mime-types/-/mime-types-2.1.1.tgz#d9ba43490fa3a3df958759adf69396c3532cf2c1"
1201-
integrity sha512-vXOTGVSLR2jMw440moWTC7H19iUyLtP3Z1YTj7cSsubOICinjMxFeb/V57v9QdyyPGbbWolUFSSmSiRSn94tFw==
1202-
12031198
"@types/mime@^1":
12041199
version "1.3.2"
12051200
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-1.3.2.tgz#93e25bf9ee75fe0fd80b594bc4feb0e862111b5a"
@@ -4534,7 +4529,7 @@ [email protected]:
45344529
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70"
45354530
integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==
45364531

4537-
mime-types@^2.1.12, mime-types@^2.1.18, mime-types@^2.1.35, mime-types@~2.1.19, mime-types@~2.1.24, mime-types@~2.1.34:
4532+
mime-types@^2.1.12, mime-types@^2.1.18, mime-types@~2.1.19, mime-types@~2.1.24, mime-types@~2.1.34:
45384533
version "2.1.35"
45394534
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.35.tgz#381a871b62a734450660ae3deee44813f70d959a"
45404535
integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==

0 commit comments

Comments
 (0)