mirror of
https://github.com/protomaps/PMTiles.git
synced 2026-02-04 10:51:07 +00:00
Lambda: attach CORS headers for all 2xx-4xx responses
This commit is contained in:
@@ -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({
|
||||||
connectionTimeout: 500,
|
requestHandler: new NodeHttpHandler({
|
||||||
socketTimeout: 500
|
connectionTimeout: 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,24 +198,24 @@ export const handler = async (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (header.tileType) {
|
|
||||||
case TileType.Mvt:
|
|
||||||
// part of the list of Cloudfront compressible types.
|
|
||||||
headers["Content-Type"] = "application/vnd.mapbox-vector-tile";
|
|
||||||
break;
|
|
||||||
case TileType.Png:
|
|
||||||
headers["Content-Type"] = "image/png";
|
|
||||||
break;
|
|
||||||
case TileType.Jpeg:
|
|
||||||
headers["Content-Type"] = "image/jpeg";
|
|
||||||
break;
|
|
||||||
case TileType.Webp:
|
|
||||||
headers["Content-Type"] = "image/webp";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
const tile_result = await p.getZxy(tile[0], tile[1], tile[2]);
|
const tile_result = await p.getZxy(tile[0], tile[1], tile[2]);
|
||||||
if (tile_result) {
|
if (tile_result) {
|
||||||
|
switch (header.tileType) {
|
||||||
|
case TileType.Mvt:
|
||||||
|
// part of the list of Cloudfront compressible types.
|
||||||
|
headers["Content-Type"] = "application/vnd.mapbox-vector-tile";
|
||||||
|
break;
|
||||||
|
case TileType.Png:
|
||||||
|
headers["Content-Type"] = "image/png";
|
||||||
|
break;
|
||||||
|
case TileType.Jpeg:
|
||||||
|
headers["Content-Type"] = "image/jpeg";
|
||||||
|
break;
|
||||||
|
case TileType.Webp:
|
||||||
|
headers["Content-Type"] = "image/webp";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user