chiitiler 1.21.2 → 1.22.0

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
@@ -107,6 +107,8 @@ const png = await getRenderedTileBuffer({
107
107
 
108
108
  Available renderers: `getRenderedTileBuffer`, `getRenderedClipBuffer`, `getRenderedCameraBuffer`, and their `*Stream` variants (`Sharp` instances for further piping).
109
109
 
110
+ Outbound HTTP requests send a `User-Agent` identifying chiitiler by default; override it with `setUserAgent('my-app/1.0')` or the `CHIITILER_USER_AGENT` environment variable.
111
+
110
112
  ## Configuration
111
113
 
112
114
  All options can be set via CLI flag or environment variable.
@@ -117,6 +119,7 @@ All options can be set via CLI flag or environment variable.
117
119
  | --- | --- | --- |
118
120
  | `--port <n>` | `CHIITILER_PORT` | `3000` |
119
121
  | `--debug` | `CHIITILER_DEBUG` | `false` |
122
+ | `--user-agent <ua>` | `CHIITILER_USER_AGENT` | `chiitiler (+https://github.com/Kanahiro/chiitiler)` |
120
123
  | — | `CHIITILER_PROCESSES` | `1` (set `0` for all CPUs) |
121
124
 
122
125
  ### Cache
package/dist/cli.js CHANGED
@@ -2,6 +2,7 @@ import process from 'node:process';
2
2
  import { Command } from 'commander';
3
3
  import { initServer } from './server/index.js';
4
4
  import * as caches from './cache/index.js';
5
+ import { setUserAgent } from './source/userAgent.js';
5
6
  function parseCacheStrategy(method, options) {
6
7
  // command-line option
7
8
  if (method === 'memory')
@@ -99,8 +100,12 @@ export function createProgram() {
99
100
  .option('--gcs-cache-prefix <prefix>', 'gcs cache prefix', '')
100
101
  .option('--gcs-api-endpoint <api-endpoint>', 'gcs api endpoint', '')
101
102
  .option('-p, --port <port>', 'port number')
103
+ .option('--user-agent <user-agent>', 'User-Agent header for outbound HTTP requests')
102
104
  .option('-D, --debug', 'debug mode')
103
105
  .action((options) => {
106
+ // env fallback (CHIITILER_USER_AGENT) is handled in userAgent.ts
107
+ if (options.userAgent !== undefined)
108
+ setUserAgent(options.userAgent);
104
109
  const serverOptions = {
105
110
  cache: parseCacheStrategy(options.cache, {
106
111
  cacheTtl: Number(options.cacheTtl),
package/dist/index.d.ts CHANGED
@@ -6,3 +6,4 @@ export { getRenderedCamera as getRenderedCameraStream };
6
6
  export declare function getRenderedTileBuffer(options: GetRenderedTileOptions): Promise<Buffer>;
7
7
  export { getRenderedTile as getRenderedTileStream };
8
8
  export * as ChiitilerCache from './cache/index.js';
9
+ export { setUserAgent } from './source/userAgent.js';
package/dist/index.js CHANGED
@@ -15,3 +15,4 @@ export async function getRenderedTileBuffer(options) {
15
15
  }
16
16
  export { getRenderedTile as getRenderedTileStream };
17
17
  export * as ChiitilerCache from './cache/index.js';
18
+ export { setUserAgent } from './source/userAgent.js';
@@ -1,6 +1,7 @@
1
1
  /// <reference lib="dom" />
2
2
  // for using native fetch in TypeScript
3
3
  import { noneCache } from '../cache/index.js';
4
+ import { getUserAgent } from './userAgent.js';
4
5
  async function getHttpSource(uri, cache = noneCache()) {
5
6
  // use cache only for http(s) sources
6
7
  const val = await cache.get(uri);
@@ -8,7 +9,9 @@ async function getHttpSource(uri, cache = noneCache()) {
8
9
  return val; // hit
9
10
  // miss
10
11
  try {
11
- const res = await fetch(uri);
12
+ const res = await fetch(uri, {
13
+ headers: { 'User-Agent': getUserAgent() },
14
+ });
12
15
  if (!res.ok) {
13
16
  console.log(`failed to fetch ${uri}`);
14
17
  return null;
@@ -1,9 +1,10 @@
1
1
  import * as fs from 'fs';
2
- import { PMTiles } from 'pmtiles';
2
+ import { PMTiles, FetchSource } from 'pmtiles';
3
3
  import { GetObjectCommand, } from '@aws-sdk/client-s3';
4
4
  import { LRUCache } from 'lru-cache';
5
5
  import { getS3Client } from '../s3.js';
6
6
  import { noneCache } from '../cache/index.js';
7
+ import { getUserAgent } from './userAgent.js';
7
8
  const pmtilesCache = new LRUCache({
8
9
  max: 50,
9
10
  });
@@ -82,7 +83,8 @@ async function getPmtilesSource(uri, cache = noneCache()) {
82
83
  if (val !== undefined)
83
84
  return val; // hit
84
85
  if (pmtiles === undefined) {
85
- pmtiles = new PMTiles(pmtilesUri);
86
+ const fetchSource = new FetchSource(pmtilesUri, new Headers({ 'User-Agent': getUserAgent() }));
87
+ pmtiles = new PMTiles(fetchSource);
86
88
  pmtilesCache.set(pmtilesUri, pmtiles);
87
89
  }
88
90
  }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Override the User-Agent sent with outbound HTTP requests.
3
+ * Takes precedence over the CHIITILER_USER_AGENT environment variable.
4
+ */
5
+ declare function setUserAgent(userAgent: string | undefined): void;
6
+ declare function getUserAgent(): string;
7
+ export { getUserAgent, setUserAgent };
@@ -0,0 +1,18 @@
1
+ // User-Agent for all outbound HTTP requests (http(s):// and pmtiles://http(s)://).
2
+ // Some tile providers (e.g. OpenStreetMap) reject requests without an
3
+ // identifying User-Agent, so we always send one.
4
+ const DEFAULT_USER_AGENT = 'chiitiler (+https://github.com/Kanahiro/chiitiler)';
5
+ let overrideUserAgent;
6
+ /**
7
+ * Override the User-Agent sent with outbound HTTP requests.
8
+ * Takes precedence over the CHIITILER_USER_AGENT environment variable.
9
+ */
10
+ function setUserAgent(userAgent) {
11
+ overrideUserAgent = userAgent;
12
+ }
13
+ function getUserAgent() {
14
+ return (overrideUserAgent ??
15
+ process.env.CHIITILER_USER_AGENT ??
16
+ DEFAULT_USER_AGENT);
17
+ }
18
+ export { getUserAgent, setUserAgent };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "chiitiler",
4
- "version": "1.21.2",
4
+ "version": "1.22.0",
5
5
  "description": "Tiny map rendering server for MapLibre Style Spec",
6
6
  "main": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",