From 79c87fb04a321aa9abadcbc046867d8b92c9ae51 Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Fri, 11 Nov 2022 20:21:22 +0800 Subject: [PATCH] MIME type fixups for CDN compression --- serverless/aws/src/index.ts | 14 ++++++++------ serverless/cloudflare/src/index.ts | 4 ++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/serverless/aws/src/index.ts b/serverless/aws/src/index.ts index 461691b..4827336 100644 --- a/serverless/aws/src/index.ts +++ b/serverless/aws/src/index.ts @@ -19,8 +19,9 @@ import https from "https"; import s3client from "/var/runtime/node_modules/aws-sdk/clients/s3.js"; const keepAliveAgent = new https.Agent({ keepAlive: true }); + +// the region should default to the same one as the function const s3 = new s3client({ - region: process.env.BUCKET_REGION!, httpOptions: { agent: keepAliveAgent }, }); @@ -145,7 +146,7 @@ export const handler = async ( if (process.env.CORS) { headers["Access-Control-Allow-Origin"] = process.env.CORS; } - // TODO: extension enforcement and MIME types + // TODO: extension enforcement and MIME types, metadata and TileJSON const source = new S3Source(name); const p = new PMTiles(source, CACHE); @@ -153,15 +154,16 @@ export const handler = async ( const header = await p.getHeader(); // TODO optimize by checking min/max zoom, return 404 - headers["Content-Type"] = "application/vnd.vector-tile"; + // part of the list of Cloudfront compressible types. + headers["Content-Type"] = "application/vnd.mapbox-vector-tile"; - const tile = await p.getZxy(0, 0, 0); - if (tile) { + const tile_result = await p.getZxy(tile[0], tile[1], tile[2]); + if (tile_result) { // returns uncompressed response // TODO: may need to special case API gateway to return compressed response with gzip content-encoding header return apiResp( 200, - Buffer.from(tile.data).toString("base64"), + Buffer.from(tile_result.data).toString("base64"), true, headers ); diff --git a/serverless/cloudflare/src/index.ts b/serverless/cloudflare/src/index.ts index a36e6b8..61d048c 100644 --- a/serverless/cloudflare/src/index.ts +++ b/serverless/cloudflare/src/index.ts @@ -186,7 +186,7 @@ export default { case TileType.Mvt: cacheable_headers.set( "Content-Type", - "application/vnd.vector-tile" + "application/x-protobuf" ); break; case TileType.Png: @@ -214,7 +214,7 @@ export default { } } - // TODO: metadata responses + // TODO: metadata responses, tileJSON return new Response("Invalid URL", { status: 404 }); }, };