Skip to content

Commit

Permalink
fix: support maps that may be removed or replaced in map server plugin (
Browse files Browse the repository at this point in the history
  • Loading branch information
achou11 authored Oct 21, 2024
1 parent a7cec42 commit 5d06a93
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
"sodium-universal": "^4.0.0",
"start-stop-state-machine": "^1.2.0",
"streamx": "^2.19.0",
"styled-map-package": "^1.1.0",
"styled-map-package": "^2.0.0",
"sub-encoder": "^2.1.1",
"throttle-debounce": "^5.0.0",
"tiny-typed-emitter": "^2.1.0",
Expand Down
14 changes: 11 additions & 3 deletions src/fastify-plugins/maps.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import { fetch } from 'undici'
import { Server as SMPServerPlugin } from 'styled-map-package'
import { ReaderWatch, Server as SMPServerPlugin } from 'styled-map-package'

import { noop } from '../utils.js'
import { NotFoundError, ENOENTError } from './utils.js'
Expand All @@ -25,6 +25,10 @@ export async function plugin(fastify, opts) {
if (opts.customMapPath) {
const { customMapPath } = opts

const customMapReader = new ReaderWatch(customMapPath)

fastify.addHook('onClose', () => customMapReader.close().catch(noop))

fastify.get(`/${CUSTOM_MAP_PREFIX}/info`, async () => {
const baseUrl = new URL(fastify.prefix, fastify.listeningOrigin)

Expand Down Expand Up @@ -78,13 +82,17 @@ export async function plugin(fastify, opts) {

fastify.register(SMPServerPlugin, {
prefix: CUSTOM_MAP_PREFIX,
filepath: customMapPath,
reader: customMapReader,
})
}

const fallbackMapReader = new ReaderWatch(opts.fallbackMapPath)

fastify.addHook('onClose', () => fallbackMapReader.close().catch(noop))

fastify.register(SMPServerPlugin, {
prefix: FALLBACK_MAP_PREFIX,
filepath: opts.fallbackMapPath,
reader: fallbackMapReader,
})

fastify.get('/style.json', async (_request, reply) => {
Expand Down

0 comments on commit 5d06a93

Please sign in to comment.