@tinkatinka/static-map 3.0.3 → 3.0.4

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.
package/README.md CHANGED
@@ -35,6 +35,7 @@ The constructor can be called with an optional `StaticMapOptions` object, overri
35
35
  tileSize: number; // size of tiles [default: 256]
36
36
  tileMaxZoom: number; // maximum zoom available for tiles [default: 20]
37
37
  tileCache?: string; // path to a cache directory for tiles [default: `undefined`]
38
+ tileTimeout?: number; // tile fetching timeout [ms] [default: 2000]
38
39
  backgroundColor?: string; // image background color (CSS string) [default: `undefined`]
39
40
  grayscale: boolean; // apply a grayscale filter to map tiles (not overlays) [default: `false`]
40
41
  }
@@ -1,2 +1,2 @@
1
- declare const base64img: (url: string, mimetype?: string, userAgent?: string) => Promise<string>;
2
- export default base64img;
1
+ export declare function mimeFromURL(url: string): "image/png" | "image/jpeg" | "image/svg+xml" | "application/octet-stream";
2
+ export declare function base64img(url: string, userAgent?: string, timeout?: number): Promise<string>;
package/dist/base64img.js CHANGED
@@ -1,15 +1,42 @@
1
- const base64img = async (url, mimetype = 'image/png', userAgent = 'StaticMap/2') => {
2
- const response = await fetch(url, {
3
- headers: {
4
- 'User-Agent': userAgent
1
+ export function mimeFromURL(url) {
2
+ const str = url.toLowerCase();
3
+ if (/png$/.test(str)) {
4
+ return 'image/png';
5
+ }
6
+ else if (/(jpg)|(jpeg)$/.test(str)) {
7
+ return 'image/jpeg';
8
+ }
9
+ else if (/svg$/.test(str)) {
10
+ return 'image/svg+xml';
11
+ }
12
+ else {
13
+ return 'application/octet-stream';
14
+ }
15
+ }
16
+ export async function base64img(url, userAgent = 'StaticMap/3', timeout = 0) {
17
+ let timeoutId;
18
+ try {
19
+ const controller = new AbortController();
20
+ if (timeout > 1) {
21
+ timeoutId = setTimeout(() => controller.abort(), timeout);
5
22
  }
6
- });
7
- if (response.status !== 200) {
8
- throw new Error(`Server status ${response.status} for request '${url}'`);
9
- }
10
- const buf = await response.arrayBuffer();
11
- const imgstr = Buffer.from(buf).toString('base64');
12
- const dataurl = 'data:' + mimetype.trim() + ';base64,' + imgstr;
13
- return dataurl;
14
- };
15
- export default base64img;
23
+ const response = await fetch(url, {
24
+ headers: {
25
+ 'User-Agent': userAgent
26
+ },
27
+ signal: controller.signal
28
+ });
29
+ if (response.status !== 200) {
30
+ throw new Error(`Server status ${response.status} for request '${url}'`);
31
+ }
32
+ const buf = await response.arrayBuffer();
33
+ const imgstr = Buffer.from(buf).toString('base64');
34
+ const mimetype = response.headers.get('Content-Type')?.toLowerCase() ?? mimeFromURL(url);
35
+ const dataurl = 'data:' + mimetype.trim() + ';base64,' + imgstr;
36
+ return dataurl;
37
+ }
38
+ finally {
39
+ clearTimeout(timeoutId);
40
+ }
41
+ }
42
+ ;
@@ -1,4 +1,4 @@
1
- const isDefined = (value) => (value !== undefined);
1
+ const isDefined = (value) => (value !== undefined && value !== null);
2
2
  export const compactMap = (array, func) => array.map(func).filter(isDefined);
3
3
  if (!Array.prototype.compactMap) {
4
4
  Array.prototype.compactMap = function (func) {
package/dist/index.d.ts CHANGED
@@ -99,6 +99,7 @@ export interface StaticMapOptions {
99
99
  tileMaxZoom: number;
100
100
  tileCache?: string;
101
101
  userAgent?: string;
102
+ tileTimeout: number;
102
103
  backgroundColor?: string;
103
104
  grayscale: boolean;
104
105
  }
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import Canvas from 'canvas';
2
2
  import { merge } from 'lodash-es';
3
- import { TileCache } from './tilecache.js';
4
- import base64img from './base64img.js';
3
+ import { base64img } from './base64img.js';
5
4
  import { compactMap } from './compactmap.js';
5
+ import { TileCache } from './tilecache.js';
6
6
  const EARTH_RADIUS_MEAN = 6371008.8;
7
7
  const FEET_PER_METER = 3.2808399;
8
8
  const FEET_PER_MILE = 5280;
@@ -15,6 +15,7 @@ export class StaticMap {
15
15
  tileURL: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
16
16
  tileSize: 256,
17
17
  tileMaxZoom: 20,
18
+ tileTimeout: 2000,
18
19
  grayscale: false
19
20
  };
20
21
  static defaultLineOptions = {
@@ -331,7 +332,7 @@ export class StaticMap {
331
332
  if (this.cache !== undefined) {
332
333
  const td = { z: zoom, x: tileX, y: tileY };
333
334
  try {
334
- const csrc = await this.cache.pass(td, () => base64img(src, 'image/png', this.options.userAgent));
335
+ const csrc = await this.cache.pass(td, () => base64img(src, this.options.userAgent, this.options.tileTimeout));
335
336
  if (csrc !== undefined) {
336
337
  src = csrc;
337
338
  }
@@ -342,7 +343,7 @@ export class StaticMap {
342
343
  }
343
344
  }
344
345
  else {
345
- src = await base64img(src, 'image/png', this.options.userAgent);
346
+ src = await base64img(src, this.options.userAgent, this.options.tileTimeout);
346
347
  }
347
348
  const image = await Canvas.loadImage(src);
348
349
  return image;
package/dist/tilecache.js CHANGED
@@ -1,6 +1,6 @@
1
- import { existsSync, mkdirSync, readFile, writeFile } from 'fs';
1
+ import { mkdirSync } from 'fs';
2
+ import { readFile, writeFile } from 'fs/promises';
2
3
  import { join as pathJoin } from 'path';
3
- import { promisify } from 'util';
4
4
  export class TileCache {
5
5
  static fnTemplate = '{z}_{x}_{y}.png';
6
6
  path;
@@ -18,12 +18,17 @@ export class TileCache {
18
18
  }
19
19
  async read(data) {
20
20
  const p = this.tilePath(data);
21
- const r = existsSync(p) ? await promisify(readFile)(p, { encoding: 'utf-8' }) : undefined;
22
- return r;
21
+ try {
22
+ const r = await readFile(p, { encoding: 'utf-8' });
23
+ return r;
24
+ }
25
+ catch {
26
+ return undefined;
27
+ }
23
28
  }
24
29
  async write(data, src) {
25
30
  const p = this.tilePath(data);
26
- await promisify(writeFile)(p, src, { encoding: 'utf-8' });
31
+ await writeFile(p, src, { encoding: 'utf-8' });
27
32
  }
28
33
  tilePath(data) {
29
34
  const fn = TileCache.fnTemplate
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinkatinka/static-map",
3
- "version": "3.0.3",
3
+ "version": "3.0.4",
4
4
  "description": "Render static map images",
5
5
  "author": "Florian Gmeiner <florian@tinkatinka.com>",
6
6
  "license": "MIT",
@@ -8,7 +8,7 @@
8
8
  "main": "dist/index.js",
9
9
  "types": "dist/index.d.ts",
10
10
  "engines": {
11
- "node": ">=18.0.0"
11
+ "node": ">=20.0.0"
12
12
  },
13
13
  "files": [
14
14
  "dist"
@@ -32,21 +32,18 @@
32
32
  "lint": "eslint",
33
33
  "test": "tsc -p tsconfig.test.json && node --experimental-vm-modules --no-warnings --test --test-reporter=spec build/tests/index.test.js"
34
34
  },
35
- "browser": {
36
- "node-fetch": false
37
- },
38
35
  "dependencies": {
39
36
  "canvas": "^3.1.0",
40
37
  "lodash-es": "^4.17.21"
41
38
  },
42
39
  "devDependencies": {
43
40
  "@eslint/js": "^10.0.1",
44
- "@types/eslint__js": "^8.42.3",
41
+ "@stylistic/eslint-plugin": "^5.10.0",
45
42
  "@types/lodash-es": "^4.17.12",
46
- "@types/node": "^24.5.2",
43
+ "@types/node": "^25.5.0",
47
44
  "eslint": "^10.0.2",
48
45
  "type-fest": "^5.0.1",
49
- "typescript": "^5.5.4",
46
+ "typescript": "^5.9.3",
50
47
  "typescript-eslint": "^8.2.0"
51
48
  }
52
49
  }