Skip to content

Commit

Permalink
refactor: create storage, memcache and kv repositories and interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
zackpollard committed Aug 12, 2024
1 parent 6562d59 commit 9f09c0f
Show file tree
Hide file tree
Showing 13 changed files with 386 additions and 127 deletions.
196 changes: 186 additions & 10 deletions tiles/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tiles/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"typescript": "^5.5.2",
"vite-node": "1.5.0",
"vite-plugin-node-polyfills": "^0.22.0",
"wrangler": "^3.60.3"
"wrangler": "^3.70.0"
},
"dependencies": {
"@influxdata/influxdb-client": "^1.34.0"
Expand Down
5 changes: 5 additions & 0 deletions tiles/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface WorkerEnv extends Omit<Env, 'ENVIRONMENT' | 'PMTILES_FILE_NAME' | 'PMTILES_FILE_HASH'> {
ENVIRONMENT: string;
PMTILES_FILE_NAME: string;
PMTILES_FILE_HASH: string;
}
35 changes: 14 additions & 21 deletions tiles/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
import { Metrics } from './monitor';
import { PMTiles } from './pmtiles/pmtiles';
import { Header } from './pmtiles/types';
import { tileJSON } from './pmtiles/utils';
import { R2Source } from './r2';
import { CloudflareKVRepository, MemCacheRepository, R2StorageRepository } from './repository';

const URL_MATCHER = /^\/v(?<VERSION>[0-9]+)((?=)|(?<JSON>\.json)|\/(?<Z>\d+)\/(?<X>\d+)\/(?<Y>\d+).mvt)$/;

/* eslint-disable no-var */
declare global {
var headerCache: Map<string, Header>;
var source: R2Source;
}
/* eslint-enable no-var */

if (!globalThis.headerCache) {
globalThis.headerCache = new Map<string, Header>();
}

type PMTilesParams = {
requestType: 'tile' | 'json' | undefined;
url: URL;
Expand Down Expand Up @@ -49,7 +37,11 @@ export function parseUrl(request: Request): PMTilesParams {
return { requestType: undefined, url };
}

async function handleRequest(request: Request<unknown, IncomingRequestCfProperties>, env: Env, ctx: ExecutionContext) {
async function handleRequest(
request: Request<unknown, IncomingRequestCfProperties>,
env: WorkerEnv,
ctx: ExecutionContext,
) {
const metrics = Metrics.getMetrics();
const cacheResponse = async (response: Response): Promise<Response> => {
if (!response.body) throw new Error('Response body is undefined');
Expand All @@ -63,7 +55,7 @@ async function handleRequest(request: Request<unknown, IncomingRequestCfProperti
if (!tile) return new Response('Tile not found', { status: 404 });
respHeaders.set('Content-Type', 'application/x-protobuf');
respHeaders.set('content-encoding', 'gzip');
return cacheResponse(new Response(tile.data, { headers: respHeaders, status: 200, encodeBody: 'manual' }));
return cacheResponse(new Response(tile, { headers: respHeaders, status: 200, encodeBody: 'manual' }));
};

async function handleJsonRequest(respHeaders: Headers) {
Expand Down Expand Up @@ -93,10 +85,14 @@ async function handleRequest(request: Request<unknown, IncomingRequestCfProperti
});
}

const memCacheRepository = new MemCacheRepository();
const kvRepository = new CloudflareKVRepository(env.KV);
const storageRepository = new R2StorageRepository(env.BUCKET, env.PMTILES_FILE_NAME);

const pmTiles = await metrics.monitorAsyncFunction({ name: 'pmtiles_init' }, PMTiles.init)(
source,
globalThis.headerCache,
env.KV,
storageRepository,
memCacheRepository,
kvRepository,
ctx,
);

Expand Down Expand Up @@ -133,9 +129,6 @@ async function handleRequest(request: Request<unknown, IncomingRequestCfProperti
export default {
async fetch(request, env, ctx): Promise<Response> {
const metrics = Metrics.initialiseMetrics('tiles', request, ctx, env);
if (!globalThis.source) {
globalThis.source = new R2Source(env);
}

return metrics.monitorAsyncFunction({ name: 'handle_request' }, handleRequest)(request, env, ctx);
},
Expand Down
15 changes: 15 additions & 0 deletions tiles/src/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export interface IKeyValueRepository {
put(key: string, value: string): Promise<void>;
get(key: string): Promise<string | undefined>;
getAsStream(key: string): Promise<ReadableStream | undefined>;
}

export interface IMemCacheRepository {
set<T>(key: string, value: T): void;
get<T>(key: string): T | undefined;
}

export interface IStorageRepository {
get(range: { length: number; offset: number }): Promise<ArrayBuffer>;
getFileName(): string;
}
Loading

0 comments on commit 9f09c0f

Please sign in to comment.