Skip to content

Commit

Permalink
Add bbox calc, contentLength calc if header missing
Browse files Browse the repository at this point in the history
  • Loading branch information
underbluewaters committed Feb 22, 2024
1 parent 49a1985 commit 75fda81
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
Binary file added packages/geojson-inspector/bun.lockb
Binary file not shown.
3 changes: 3 additions & 0 deletions packages/geojson-inspector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
"@cloudflare/workers-types": "^4.20240208.0",
"typescript": "^5.0.4",
"wrangler": "^3.0.0"
},
"dependencies": {
"@turf/bbox": "^6.5.0"
}
}
12 changes: 5 additions & 7 deletions packages/geojson-inspector/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

import { FailedInspectorResponse, InspectorResponse } from './types';
import calcBBox from '@turf/bbox';

export interface Env {
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
Expand Down Expand Up @@ -51,7 +52,6 @@ export default {
});
const endT = performance.now();
let failedResponse: FailedInspectorResponse;
console.log(response.headers);
// if the fetch fails return an error
if (!response.ok) {
if (response.status === 404) {
Expand Down Expand Up @@ -84,11 +84,8 @@ export default {
} else {
let contentLength = parseInt(response.headers.get('content-length') || '0');
if (contentLength === 0) {
console.log(response.headers);
// @ts-ignore
console.log(response.body.byteLength);
// @ts-ignore
contentLength = response.body ? response.body.byteLength : 0;
const blob = await response.clone().blob();
contentLength = blob.size;
}
const contentType = response.headers.get('content-type');
const cacheControl = response.headers.get('cache-control');
Expand Down Expand Up @@ -125,13 +122,14 @@ export default {
return new Response(
JSON.stringify({
location,
contentLength: contentLength ? parseInt(contentLength) : 0,
contentLength,
contentType: contentType || '',
cacheControl: cacheControl || '',
latency: endT - startT,
rootType,
featureCount: rootType === 'FeatureCollection' ? geojson.features.length : 1,
geometryType: rootType === 'FeatureCollection' ? geojson.features[0]?.geometry?.type || 'Unknown' : geojson.geometry.type,
bbox: calcBBox(geojson),
} as InspectorResponse),
{
status: 200,
Expand Down
1 change: 1 addition & 0 deletions packages/geojson-inspector/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface SuccessfulInspectorResponse extends BaseInspectorResponse {
featureCount: number;
geometryType: 'Point' | 'MultiPoint' | 'LineString' | 'MultiLineString' | 'Polygon' | 'MultiPolygon';
rootType: 'FeatureCollection' | 'Feature';
bbox: number[];
}

export type InspectorResponse = FailedInspectorResponse | SuccessfulInspectorResponse;
Expand Down

0 comments on commit 75fda81

Please sign in to comment.