tile-cacher

0.1.4 • Public • Published

tile-cacher 0.1.4

Node express-like middleware and downloader for map tile caches

pipeline status tile-cacher on NPM license developtment time contributor covenant support development

Middleware

The middleware can be used with HTTP libraries like Express and Polka. When instantiated, if connected to the Internet, it will check the actively cached tile have been cached and remove old cached tiles. It will also schedule the next check inline with the checkInterval option.

Example

examples/middleware.js

import { createReadStream } from 'fs';
import polka from 'polka';
import middleware from 'tile-cacher';
import { options } from './options.js';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

const __dirname = dirname(fileURLToPath(import.meta.url));

const app = polka();

app.get('/', (req, res) => {
  const stream = createReadStream(join(__dirname, 'index.html'));
  res.setHeader('Content-Type', 'text/html');
  stream.pipe(res);
});
app.get('/tiles/:id/:z/:x/:y.png', middleware(options));

app.listen(3030, () => {
  console.log('tile-cacher middleware listening on port 3030');
});

Tile Cache Check

Example

examples/cacheCheck.js

import { checkCache } from 'tile-cacher';
import { options } from './options.js';

checkCache(options);

Options

Options are the same for both the middlware and the cacheCheck functionality

Example

examples/options.js

export const options = {
  servers: [
    {
      id: 'opentopomap',
      url: 'https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png',
      activelyCache: [
        {
          northLat: -40.975513,
          southLat: -41.100311,
          westLon: 174.994183,
          eastLon: 175.148163,
          maxZoom: 13
        }
      ],
      cache: [
        {
          northLat: -39.975513,
          southLat: -42.100311,
          westLon: 173.994183,
          eastLon: 176.148163,
          maxZoom: 13
        }
      ],
      age: 168
    }
  ],
  checkInterval: 20,
  downloadWaitTime: 1000,
  extension: 'png',
  folder: 'cache',
  logger: {
    log: (...args) => console.log(new Date().toISOString(), 'LOG', ...args),
    warn: (...args) => console.warn(new Date().toISOString(), 'WARN', ...args),
    debug: (...args) => console.debug(new Date().toISOString(), 'DEBUG', ...args),
    info: (...args) => console.info(new Date().toISOString(), 'INFO', ...args),
    error: (...args) => console.error(new Date().toISOString(), 'ERROR', ...args)
  }
};

Development

Feel free to contribute to the code or documentation, or leave issues or suggestions on the issue tracker or email them to us. Please submit security concerns as a confidential issue.

The source is hosted on Gitlab and uses prettier, lint-staged and husky to keep things pretty. As such, when you first clone the repository, as well as installing the npm dependencies, you will also need to install husky.

# Install NPM dependencies
npm install
# Set up husky Git hooks stored in .husky
npx husky install

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

0.1.4 - 2023-03-19

Added

  • Added logger to options typings

Fixed

  • Checking for internet connectivity was throwing if not connected

0.1.3 - 2022-06-02

Added

  • Server response time logging

0.1.2 - 2022-06-02

Fixed

  • Middleware issue not passing through requested tile from remote server
  • Changed to AGPL-3.0 license

0.1.1 - 2022-06-02

Fixed

  • Fixed example code in README

0.1.0 - 2022-06-02

Added

  • Basic tile cache downloading
  • Basic middleware

Readme

Keywords

Package Sidebar

Install

npm i tile-cacher

Weekly Downloads

0

Version

0.1.4

License

AGPL-3.0

Unpacked Size

62.1 kB

Total Files

9

Last publish

Collaborators

  • bytesnz