Skip to content

Commit

Permalink
Rewrite Proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
ingalls committed Sep 21, 2024
1 parent 85e0b6a commit 3a8733c
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 164 deletions.
3 changes: 0 additions & 3 deletions api/lib/control/tilejson.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import undici from 'undici';
import { Readable } from 'node:stream';
import type { BBox } from 'geojson';
import type { Response } from 'express';
import { pipeline, Writable, PassThrough } from 'node:stream';
import { pipeline as promisePipeline } from 'node:stream/promises';
import { pointOnFeature } from '@turf/point-on-feature';
import { bboxPolygon } from '@turf/bbox-polygon';
import { Static, Type } from '@sinclair/typebox'
Expand Down
101 changes: 51 additions & 50 deletions api/package-lock.json

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

1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@sinclair/typebox": "^0.33.0",
"@tak-ps/node-cot": "^12.0.0",
"@tak-ps/node-tak": "^8.0.0",
"@turf/bbox": "^7.1.0",
"@turf/bbox-polygon": "^7.1.0",
"@turf/meta": "^7.0.0",
"@turf/point-on-feature": "^7.1.0",
Expand Down
47 changes: 19 additions & 28 deletions api/routes/basemap.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import path from 'node:path';
import { bbox } from '@turf/bbox';
import Err from '@openaddresses/batch-error';
import TileJSON, { TileJSONType } from '../lib/control/tilejson.js';
import Auth, { AuthUserAccess, ResourceCreationScope } from '../lib/auth.js';
import Cacher from '../lib/cacher.js';
import busboy from 'busboy';
import Config from '../lib/config.js';
import { Response } from 'express';
import xml2js from 'xml2js';
import { Readable } from 'node:stream';
import stream2buffer from '../lib/stream.js';
import bboxPolygon from '@turf/bbox-polygon';
import { Param } from '@openaddresses/batch-generic'
Expand Down Expand Up @@ -338,7 +339,8 @@ export default async function router(schema: Schema, config: Config) {
}),
query: Type.Object({
token: Type.Optional(Type.String()),
})
}),
res: TileJSONType
}, async (req, res) => {
try {
const user = await Auth.as_user(config, req, { token: true });
Expand All @@ -354,14 +356,16 @@ export default async function router(schema: Schema, config: Config) {
let url = config.API_URL + `/api/basemap/${basemap.id}/tiles/{z}/{x}/{y}`;
if (req.query.token) url = url + `?token=${req.query.token}`;

return res.json({
tilejson: "2.2.0",
name: basemap.name,
minzoom: basemap.minzoom,
maxzoom: basemap.maxzoom,
format: basemap.format,
tiles: [ String(url) ]
const json = TileJSON.json({
...basemap,
bounds: basemap.bounds ? bbox(basemap.bounds) : undefined,
center: basemap.center ? basemap.center.coordinates : undefined,
url
});

console.error(json);

return res.json(json);
} catch (err) {
return Err.respond(err, res);
}
Expand Down Expand Up @@ -392,26 +396,13 @@ export default async function router(schema: Schema, config: Config) {
throw new Err(400, null, 'You don\'t have permission to access this resource');
}

const url = new URL(basemap.url
.replace('{$z}', req.params.z)
.replace('{$x}', req.params.x)
.replace('{$y}', req.params.y)
return TileJSON.tile(
basemap,
req.params.z,
req.params.x,
req.params.y,
res
);

const proxy = await fetch(url)

res.status(proxy.status);
for (const h of [
'content-type',
'content-length',
'content-encoding'
]) {
const ph = proxy.headers.get(h);
if (ph) res.append(h, ph);
}

// @ts-expect-error Doesnt meet TS def
return Readable.fromWeb(proxy.body).pipe(res);
} catch (err) {
return Err.respond(err, res);
}
Expand Down
12 changes: 6 additions & 6 deletions api/web/package-lock.json

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

1 change: 0 additions & 1 deletion api/web/src/stores/overlays/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ export default class Overlay {
}

for (const l of this.styles) {
console.error(this.name, l)
if (opts.before) {
this._map.addLayer(l, opts.before);
} else {
Expand Down
Loading

0 comments on commit 3a8733c

Please sign in to comment.