tileblaster 1.0.9 → 1.0.11

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.
@@ -0,0 +1,57 @@
1
+ // convert
2
+ module.exports = function convert({ opts, data }, next){
3
+ const sharp = this.lib.sharp;
4
+ const debug = this.lib.debug;
5
+
6
+ // check if sharp is available
7
+ if (typeof sharp === "undefined") {
8
+ debug.warn("Convert: Sharp dependency is missing.");
9
+ return next();
10
+ };
11
+
12
+ // check if tile should be optimized
13
+ if (!["png","jpeg","jpg","gif"].includes(data.tile.type)) {
14
+ debug.warn("Convert: Unsupported file type: %s", data.tile.type);
15
+ return next();
16
+ };
17
+
18
+ Promise.allSettled(["jpeg","png"].map(function(method){
19
+ return new Promise(function(resolve, reject) {
20
+ if (!opts[method]) return reject();
21
+
22
+ // copy primary tile
23
+ const tile = { ...data.tile };
24
+
25
+ // fixme: find suitable tile in tiles
26
+ sharp(tile.buffer).toFormat(method, opts[method]).toBuffer(function(err, buffer, info){
27
+ if (err) {
28
+ debug.error("Convert: %s", err);
29
+ return reject();
30
+ };
31
+
32
+ // fix tile
33
+ tile.buffer = buffer;
34
+ tile.path = tile.path+"."+method;
35
+ tile.type = method;
36
+ tile.mimetype = "image/"+method;
37
+ tile.headers = { ...tile.headers };
38
+
39
+ // add to tile stack
40
+ data.tiles.unshift(tile);
41
+
42
+ // set primary if smaller
43
+ if (buffer.length < data.tile.buffer.length) {
44
+ debug.info("Converted '%s': -%db", tile.path.magenta, data.tile.buffer.length-buffer.length);
45
+ data.tile = tile;
46
+ };
47
+
48
+ resolve();
49
+
50
+ });
51
+
52
+ });
53
+ })).then(function(){
54
+ next();
55
+ });
56
+
57
+ };
@@ -84,7 +84,7 @@ if (worker.isMainThread) {
84
84
  });
85
85
 
86
86
  // check if there are jobs
87
- if (jobs.length === 0) return debug.warn("Nothing to optimize for '%s': -%db", data.tile.path.magenta), next();
87
+ if (jobs.length === 0) return debug.warn("Nothing to optimize for '%s': -%db", (data.tile.path||"").magenta), next();
88
88
 
89
89
  // execute
90
90
  Promise.allSettled(jobs).then(function(){
package/docs/config.md CHANGED
@@ -275,6 +275,21 @@ Edit vectortiles
275
275
 
276
276
  **Not yet implemented** FIXME
277
277
 
278
+ #### `convert`
279
+
280
+ Convert raster tiles to differen Formats.
281
+ The resulting tile with the smallest size becomes the main tile.
282
+
283
+ ``` js
284
+ {
285
+ builtin: "convert",
286
+ jpeg: {
287
+ quality: 90,
288
+ mozjpeg: true,
289
+ },
290
+ }
291
+ ```
292
+
278
293
  #### `modernize`
279
294
 
280
295
  Convert JPEG or PNG raster tiles to WebP or AVIF.
package/lib/retrieve.js CHANGED
@@ -1,4 +1,4 @@
1
- const phin = require("phin");
1
+ const phn = require("phn");
2
2
 
3
3
  // prepare agents with keepalive enabled
4
4
  const agents = {
@@ -6,10 +6,10 @@ const agents = {
6
6
  https: new require("node:https").Agent({ keepAlive: true }),
7
7
  };
8
8
 
9
- // agentify phin
10
- const retrieve = module.exports = async function retrieve(opts, fn) {
9
+ // agentify phn
10
+ const retrieve = module.exports = async function retrieve(opts) {
11
11
  if (typeof opts === "string") opts = { url: opts }; // ensure opts is object
12
12
  if (!opts.hasOwnProperty("core")) opts.core = {}; // ensure core is present
13
13
  if (!opts.core.hasOwnProperty("agent")) opts.core.agent = agents[opts.url.slice(0,opts.url.indexOf(":"))]; // set agent by protocol
14
- return phin(opts, fn);
14
+ return phn(opts);
15
15
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tileblaster",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "description": "a quick and versatile map tile caching proxy",
5
5
  "main": "tileblaster.js",
6
6
  "bin": {
@@ -26,7 +26,7 @@
26
26
  "klaw": "^4.1.0",
27
27
  "minimist": "^1.2.8",
28
28
  "node-watch": "^0.7.4",
29
- "phin": "^3.7.0",
29
+ "phn": "^0.0.1",
30
30
  "quu": "^0.4.3"
31
31
  },
32
32
  "optionalDependencies": {
package/tileblaster.js CHANGED
@@ -33,7 +33,7 @@ const tileblaster = module.exports = function tileblaster(config){
33
33
 
34
34
  // load builtins
35
35
  self.builtins = {};
36
- self.loadBuiltins([ "cors", "parse", "check", "noop", "tileserver", "versatiles", "pmtiles", "mbtiles", "edit", "compress", "cache", "deliver", "dump", "modernize", "optimize", "sharp" ]);
36
+ self.loadBuiltins([ "cors", "parse", "check", "noop", "tileserver", "versatiles", "pmtiles", "mbtiles", "edit", "compress", "cache", "deliver", "dump", "modernize", "optimize", "convert", "sharp" ]);
37
37
 
38
38
  // assemble task lists for maps
39
39
  self.maps = {};