Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 0 additions & 31 deletions .eslintrc

This file was deleted.

24 changes: 0 additions & 24 deletions .eslintrc.json

This file was deleted.

4 changes: 2 additions & 2 deletions .github/workflows/deploy-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
node-version: "24"
cache: "npm"

- name: Install system dependencies
run: |
Expand Down
27 changes: 14 additions & 13 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@ name: CI

on:
push:
branches: [ "main" ]
branches: ["main"]
pull_request:
branches: [ "main" ]
branches: ["main"]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x, 22.x, 24.x, 25.x]
node-version: [22.x, 24.x, 26.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm test
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: "npm"
- run: npm ci
- run: npm run lint
- run: npm run typecheck
- run: npm run test:coverage
- run: npm run test:docs
6 changes: 3 additions & 3 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
types: [created]

permissions:
id-token: write # Required for OIDC
id-token: write # Required for OIDC
contents: read

jobs:
Expand All @@ -16,8 +16,8 @@ jobs:

- uses: actions/setup-node@v4
with:
node-version: '20'
registry-url: 'https://registry.npmjs.org'
node-version: "24"
registry-url: "https://registry.npmjs.org"

# Ensure npm 11.5.1 or later is installed
- name: Update npm
Expand Down
50 changes: 21 additions & 29 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 'Bump Version and Release'
name: "Bump Version and Release"

on:
workflow_run:
Expand All @@ -10,60 +10,52 @@ on:

jobs:
bump-version:
name: 'Bump Version on main'
name: "Bump Version on main"
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}

steps:
- name: 'Checkout source code'
uses: 'actions/checkout@v4'
- name: "Checkout source code"
uses: "actions/checkout@v4"
with:
ref: main

- name: 'Setup Node.js'
uses: 'actions/setup-node@v4'
- name: "Setup Node.js"
uses: "actions/setup-node@v4"
with:
node-version: '20'
node-version: "24"

- name: 'cat package.json'
- name: "cat package.json"
run: cat ./package.json

- name: 'Automated Version Bump'
- name: "Automated Version Bump"
id: version-bump
uses: 'phips28/gh-action-bump-version@v11.0.7'
uses: "phips28/gh-action-bump-version@v11.0.7"
with:
tag-prefix: 'v'
major-wording: 'major,Major,breaking,Breaking'
minor-wording: 'minor,Minor,feat,Feat,feature,Feature'
patch-wording: 'patch,Patch,fix,Fix,fixes,Fixes'
rc-wording: 'rc,pre,beta,alpha' # For pre-releases
commit-message: 'ci: bump version to {{version}} [skip ci]' # Skips re-triggering
push: true # Pushes the commit/tag back to main
tag-prefix: "v"
major-wording: "major,Major,breaking,Breaking"
minor-wording: "minor,Minor,feat,Feat,feature,Feature"
patch-wording: "patch,Patch,fix,Fix,fixes,Fixes"
rc-wording: "rc,pre,beta,alpha" # For pre-releases
commit-message: "ci: bump version to {{version}} [skip ci]" # Skips re-triggering
push: true # Pushes the commit/tag back to main
env:
GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }}

- name: 'cat package.json'
- name: "cat package.json"
run: cat ./package.json

- name: 'Output Step'
- name: "Output Step"
env:
NEW_TAG: ${{ steps.version-bump.outputs.newTag }}
run: echo "new tag $NEW_TAG"

- name: 'Install dependencies'
run: npm ci

- name: 'Build browser bundle'
run: npm run build:docs

- name: 'Create GitHub Release'
- name: "Create GitHub Release"
if: ${{ steps.version-bump.outputs.newTag }}
uses: 'softprops/action-gh-release@v2'
uses: "softprops/action-gh-release@v2"
with:
tag_name: ${{ steps.version-bump.outputs.newTag }}
name: Release ${{ steps.version-bump.outputs.newTag }}
generate_release_notes: true
files: |
docs-dist/image-stitch.bundle.js
env:
GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }}
19 changes: 19 additions & 0 deletions .oxlintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$schema": "./node_modules/oxlint/configuration_schema.json",
"categories": { "correctness": "off" },
"rules": {
"typescript/await-thenable": "error",
"typescript/no-floating-promises": "error",
"typescript/no-misused-promises": "error",
"typescript/require-await": "error",
"typescript/unbound-method": "error",
"typescript/prefer-promise-reject-errors": "error",
"typescript/restrict-template-expressions": "error",
"typescript/no-base-to-string": "error",
"typescript/no-redundant-type-constituents": "error",
"typescript/no-for-in-array": "error",
"typescript/only-throw-error": "error",
"typescript/no-implied-eval": "error"
},
"ignorePatterns": ["dist", "docs-dist", "coverage", "docs"]
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2025 Jonathan Burnhams

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
77 changes: 62 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Leaflet-node

[![npm version](https://img.shields.io/npm/v/leaflet-node.svg)](https://www.npmjs.com/package/leaflet-node)
[![Node.js Version](https://img.shields.io/badge/node-%3E%3D20.0.0-brightgreen.svg)](https://nodejs.org)
[![TypeScript](https://img.shields.io/badge/TypeScript-5.6-blue.svg)](https://www.typescriptlang.org/)
[![CI](https://github.com/jburnhams/leaflet-node/actions/workflows/node.js.yml/badge.svg)](https://github.com/jburnhams/leaflet-node/actions/workflows/node.js.yml)
[![Node.js Version](https://img.shields.io/badge/node-%3E%3D22-brightgreen.svg)](https://nodejs.org)
[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)

Leaflet-node brings the full Leaflet map API to Node.js so you can render maps without a browser.<br>
Reuse the same map setup for server-side image generation, automated tests, and CI pipelines.
Expand All @@ -11,7 +12,7 @@ Reuse the same map setup for server-side image generation, automated tests, and

### Requirements

- Node.js >= 20
- Node.js 22.22+, 24.15+ or 26+ (the same range as [jsdom](https://github.com/jsdom/jsdom))
- Linux users need glibc >= 2.18 for the bundled [`@napi-rs/canvas`](https://github.com/Brooooooklyn/canvas)
- Peer dependency: [`leaflet`](https://www.npmjs.com/package/leaflet) ^1.9.0

Expand All @@ -37,14 +38,37 @@ const map = L.map(container);
map.setView([51.505538, -0.090005], 13);

L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap contributors'
attribution: '© OpenStreetMap contributors',
}).addTo(map);

map.setSize(600, 400);
await new Promise((resolve) => setTimeout(resolve, 1000));
await map.saveImage('map.png');
```

## API

Importing `leaflet-node` sets up a jsdom window, a native canvas and Leaflet itself, then returns the
normal Leaflet namespace. Everything you know from Leaflet works as usual; leaflet-node adds:

| API | What it does |
| ------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `map.setSize(width, height)` | Sets the map's pixel size (jsdom has no layout engine). |
| `map.toBuffer(format?, quality?)` | Renders the current view to a PNG or JPEG `Buffer`. `quality` (0 to 1) applies to JPEG only. |
| `map.saveImage(filename, { format?, quality? })` | Renders the view and writes it to disk. Resolves with the filename. |
| `initializeEnvironment(options)` | Changes the defaults for maps created afterwards: `mapSize` (default 1024×1024), `enableAnimations` (default `false`), `userAgent` (the jsdom `navigator.userAgent`), `fontAssetBasePath`. |
| `setFontAssetBasePath(path)` | Points the fallback font loader at a custom font file or directory. |

```ts
import L, { initializeEnvironment } from 'leaflet-node';

initializeEnvironment({ mapSize: { width: 512, height: 512 } });
const map = L.map(document.createElement('div')); // 512×512 unless you call setSize
```

`leaflet-node/testing` adds `createTestMap`, `cleanupTestMaps`, `waitForTiles` and `waitForMapReady`
(see [Tile loading helpers](#tile-loading-helpers)).

## Testing with Jest

Leaflet-node can run inside Jest with either the default `jsdom` environment for API-only tests or the `node` environment for full canvas rendering.
Expand Down Expand Up @@ -95,16 +119,14 @@ To suppress these warnings, you can:
NODE_OPTIONS='--experimental-vm-modules' jest
```

2. Or configure Jest to use isolated modules:
2. Or, with ts-jest, enable isolated modules:
```js
// jest.config.js
export default {
testEnvironment: 'node',
globals: {
'ts-jest': {
isolatedModules: true
}
}
transform: {
'^.+\\.tsx?$': ['ts-jest', { isolatedModules: true }],
},
};
```

Expand Down Expand Up @@ -195,23 +217,48 @@ setFontAssetBasePath('/absolute/path/to/NotoSans-Regular.ttf');
The base path can point directly to a font file or to a directory containing the
bundled `NotoSans-Regular.ttf` asset.

## Proxy configuration
## Network requests

Leaflet-node honours standard proxy environment variables:
Tiles and remote images are fetched with [undici](https://github.com/nodejs/undici). Each request:

- sends `User-Agent: leaflet-node (+https://github.com/jburnhams/leaflet-node)`. Public tile servers
such as OpenStreetMap's require an identifying User-Agent; check your provider's
[usage policy](https://operations.osmfoundation.org/policies/tiles/) before rendering at volume.
- times out after 30 seconds, which fires the image's `error` event (and Leaflet's `tileerror`).

### Proxy configuration

Leaflet-node honours the standard proxy environment variables:

```bash
export HTTPS_PROXY=http://proxy.example.com:8080
export HTTP_PROXY=http://proxy.example.com:8080
export HTTP_PROXY=http://proxy.example.com:8080 # http:// URLs
export HTTPS_PROXY=http://proxy.example.com:8080 # https:// URLs (falls back to HTTP_PROXY)
export ALL_PROXY=http://proxy.example.com:8080 # used when neither of the above is set
export NO_PROXY=localhost,.internal.example.com # hosts that bypass the proxy
```

Set them before running exports to route tile downloads through your proxy.
Lower-case names work too. Changes are picked up on the next request.

## Performance tips

- **Reuse map instances** – keep a single map around and update the view instead of creating a new `L.map()` for every render.
- **Batch exports** – kick off multiple independent exports with `Promise.all()` when your server has headroom.
- **Manage memory** – always call `map.remove()` and drop references once you are done with a map so Node.js can reclaim resources.

## Development

```bash
npm ci
npm run lint # Biome lint + format check, then oxlint's type-aware rules
npm run format # Biome: apply formatting
npm run typecheck # TypeScript 7
npm run test:coverage # builds with tsdown, then runs Vitest with coverage
npm run test:docs # builds the docs site and checks the rendered examples
```

`tests/jest-fixture` runs the built package under `jest-environment-jsdom` to make sure it keeps
working inside consumers' Jest suites.

---

**Questions?** Open an issue on [GitHub](https://github.com/jburnhams/leaflet-node/issues).
Loading
Loading