Lambda: attach CORS headers for all 2xx-4xx responses

This commit is contained in:
Brandon Liu
2022-11-24 13:06:09 +08:00
parent 19de8191c9
commit 4e68e8eb13

View File

@@ -20,10 +20,12 @@ import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
import { NodeHttpHandler } from "@aws-sdk/node-http-handler"; import { NodeHttpHandler } from "@aws-sdk/node-http-handler";
// the region should default to the same one as the function // the region should default to the same one as the function
const s3client = new S3Client({requestHandler:new NodeHttpHandler({ const s3client = new S3Client({
requestHandler: new NodeHttpHandler({
connectionTimeout: 500, connectionTimeout: 500,
socketTimeout: 500 socketTimeout: 500,
})}); }),
});
async function nativeDecompress( async function nativeDecompress(
buf: ArrayBuffer, buf: ArrayBuffer,
@@ -155,12 +157,6 @@ export const handler = async (
return apiResp(500, "Invalid event configuration"); return apiResp(500, "Invalid event configuration");
} }
const { ok, name, tile, ext } = tile_path(path, process.env.TILE_PATH);
if (!ok) {
return apiResp(400, "Invalid tile URL");
}
var headers: Headers = {}; var headers: Headers = {};
// TODO: metadata and TileJSON // TODO: metadata and TileJSON
@@ -168,12 +164,18 @@ export const handler = async (
headers["Access-Control-Allow-Origin"] = process.env.CORS; headers["Access-Control-Allow-Origin"] = process.env.CORS;
} }
const { ok, name, tile, ext } = tile_path(path, process.env.TILE_PATH);
if (!ok) {
return apiResp(400, "Invalid tile URL", false, headers);
}
const source = new S3Source(name); const source = new S3Source(name);
const p = new PMTiles(source, CACHE, nativeDecompress); const p = new PMTiles(source, CACHE, nativeDecompress);
try { try {
const header = await p.getHeader(); const header = await p.getHeader();
if (tile[0] < header.minZoom || tile[0] > header.maxZoom) { if (tile[0] < header.minZoom || tile[0] > header.maxZoom) {
return apiResp(404, ""); return apiResp(404, "", false, headers);
} }
for (const pair of [ for (const pair of [
@@ -196,6 +198,8 @@ export const handler = async (
} }
} }
const tile_result = await p.getZxy(tile[0], tile[1], tile[2]);
if (tile_result) {
switch (header.tileType) { switch (header.tileType) {
case TileType.Mvt: case TileType.Mvt:
// part of the list of Cloudfront compressible types. // part of the list of Cloudfront compressible types.
@@ -212,8 +216,6 @@ export const handler = async (
break; break;
} }
const tile_result = await p.getZxy(tile[0], tile[1], tile[2]);
if (tile_result) {
if (is_api_gateway) { if (is_api_gateway) {
// this is wasted work, but we need to force API Gateway to interpret the Lambda response as binary // this is wasted work, but we need to force API Gateway to interpret the Lambda response as binary
// without depending on clients sending matching Accept: headers in the request. // without depending on clients sending matching Accept: headers in the request.
@@ -239,9 +241,9 @@ export const handler = async (
} }
} catch (e) { } catch (e) {
if ((e as Error).name === "AccessDenied") { if ((e as Error).name === "AccessDenied") {
return apiResp(403, "Bucket access unauthorized"); return apiResp(403, "Bucket access unauthorized", false, headers);
} }
throw e; throw e;
} }
return apiResp(404, "Invalid URL"); return apiResp(404, "Invalid URL", false, headers);
}; };