chiitiler 1.21.3 → 1.22.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.
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
+ chiitiler doesn't override the `User-Agent` on outbound HTTP requests by default, so the runtime's default is used (Node sends `node`). Some tile providers (e.g. OpenStreetMap) require an identifying User-Agent — set one 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` | (none) |
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,10 @@ 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 userAgent = getUserAgent();
13
+ const res = await fetch(uri, {
14
+ headers: userAgent ? { 'User-Agent': userAgent } : undefined,
15
+ });
12
16
  if (!res.ok) {
13
17
  console.log(`failed to fetch ${uri}`);
14
18
  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,12 @@ 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 userAgent = getUserAgent();
87
+ const headers = new Headers();
88
+ if (userAgent)
89
+ headers.set('User-Agent', userAgent);
90
+ const fetchSource = new FetchSource(pmtilesUri, headers);
91
+ pmtiles = new PMTiles(fetchSource);
86
92
  pmtilesCache.set(pmtilesUri, pmtiles);
87
93
  }
88
94
  }
@@ -0,0 +1,11 @@
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
+ /**
7
+ * Returns the configured User-Agent, or undefined when none is set (in which
8
+ * case no User-Agent header should be added).
9
+ */
10
+ declare function getUserAgent(): string | undefined;
11
+ export { getUserAgent, setUserAgent };
@@ -0,0 +1,20 @@
1
+ // User-Agent for all outbound HTTP requests (http(s):// and pmtiles://http(s)://).
2
+ // By default we don't override the User-Agent at all (the runtime's default is
3
+ // used). Set one explicitly via --user-agent, CHIITILER_USER_AGENT, or
4
+ // setUserAgent() — e.g. some tile providers (OpenStreetMap) require one.
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
+ /**
14
+ * Returns the configured User-Agent, or undefined when none is set (in which
15
+ * case no User-Agent header should be added).
16
+ */
17
+ function getUserAgent() {
18
+ return overrideUserAgent ?? process.env.CHIITILER_USER_AGENT;
19
+ }
20
+ 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.3",
4
+ "version": "1.22.1",
5
5
  "description": "Tiny map rendering server for MapLibre Style Spec",
6
6
  "main": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",