@mapwhit/tileserver 3.5.3 → 3.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/tiles.js +22 -3
  2. package/package.json +2 -1
package/lib/tiles.js CHANGED
@@ -1,7 +1,8 @@
1
1
  const fs = require('node:fs');
2
2
  const path = require('node:path');
3
- const zlib = require('node:zlib');
3
+ const { createHash } = require('node:crypto');
4
4
 
5
+ const fresh = require('fresh');
5
6
  const Router = require('@pirxpilot/router');
6
7
  const mbtiles = require('@mapwhit/mbtiles');
7
8
 
@@ -67,10 +68,22 @@ function serveData(mountPath, options, item) {
67
68
  if (options.removeETag) {
68
69
  delete headers['ETag']; // do not trust the tile ETag -- regenerate
69
70
  }
71
+ if (!options.noETag && !headers['ETag']) {
72
+ headers['ETag'] = etag(tile);
73
+ }
70
74
  headers['Content-Type'] ??= 'application/x-protobuf';
71
- headers['Content-Length'] = tile.byteLength;
72
75
  res.setHeaders(new Headers(headers));
73
- res.end(tile);
76
+ if (
77
+ fresh(req.headers, {
78
+ etag: headers['ETag'],
79
+ 'last-modified': headers['Last-Modified']
80
+ })
81
+ ) {
82
+ res.writeHead(304).end();
83
+ } else {
84
+ res.setHeader('Content-Length', tile.byteLength);
85
+ res.end(tile);
86
+ }
74
87
  }
75
88
 
76
89
  function sendData(req, res) {
@@ -111,3 +124,9 @@ function initZoomRanges(min, max) {
111
124
  }
112
125
  return ranges;
113
126
  }
127
+
128
+ function etag(buffer) {
129
+ const hash = createHash('sha1').update(buffer).digest('base64').slice(0, -1);
130
+ const len = buffer.byteLength.toString(16);
131
+ return `"${len}-${hash}"`;
132
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mapwhit/tileserver",
3
- "version": "3.5.3",
3
+ "version": "3.6.1",
4
4
  "description": "Map tile server for JSON GL styles - serving vector tiles",
5
5
  "bin": {
6
6
  "tileserver": "bin/tileserver",
@@ -22,6 +22,7 @@
22
22
  "@mapwhit/mbtiles": "^1.0.1",
23
23
  "@pirxpilot/connect": "~4",
24
24
  "@pirxpilot/router": "~1",
25
+ "fresh": "~2",
25
26
  "ms": "~2",
26
27
  "rc": "~1",
27
28
  "server-timings": "~2"