Skip to content

Commit

Permalink
fix: fastify plugin should work with prefixed routes (#22)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Chou <andrewchou@fastmail.com>
  • Loading branch information
gmaclennan and achou11 authored Oct 3, 2024
1 parent 1997ed1 commit a7fd764
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
1 change: 1 addition & 0 deletions bin/smp-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function serve({ port = 3000, filepath }) {

server.register(smpServer, {
filepath: path.relative(process.cwd(), filepath),
prefix: '/map',
})
return server.listen({ port })
}
5 changes: 4 additions & 1 deletion lib/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,8 @@ function getUrl(smpUri, baseUrl) {
throw new Error(`Invalid SMP URI: ${smpUri}`)
}
if (typeof baseUrl !== 'string') return smpUri
return smpUri.replace(URI_BASE, baseUrl + '/')
if (!baseUrl.endsWith('/')) {
baseUrl += '/'
}
return smpUri.replace(URI_BASE, baseUrl)
}
21 changes: 13 additions & 8 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/**
* @typedef {object} PluginOptions
* @property {string} filepath
* @property {boolean} [lazy=false]
*/
import Reader from './reader.js'

/** @import { FastifyPluginCallback, FastifyReply } from 'fastify' */
/** @import { Resource } from './reader.js' */

/**
* @typedef {object} PluginOptions
* @property {boolean} [lazy=false]
* @property {string} [prefix]
* @property {string} filepath Path to styled map package (`.smp`) file
*/

/**
* @param {FastifyReply} reply
* @param {Resource} resource
Expand All @@ -29,7 +31,7 @@ function sendResource(reply, resource) {
*
* @type {FastifyPluginCallback<PluginOptions>}
*/
export default function (fastify, { filepath, lazy = false }, done) {
export default function (fastify, { lazy = false, filepath }, done) {
/** @type {Reader | undefined} */
let reader
if (!lazy) {
Expand All @@ -40,18 +42,21 @@ export default function (fastify, { filepath, lazy = false }, done) {
if (!reader) {
reader = new Reader(filepath)
}
return reader.getStyle(fastify.listeningOrigin)
const baseUrl = new URL(fastify.prefix, fastify.listeningOrigin)
return reader.getStyle(baseUrl.href)
})

fastify.get('*', async (request, reply) => {
if (!reader) {
reader = new Reader(filepath)
}
// @ts-expect-error - not worth the hassle of type casting this
const path = request.params['*']

/** @type {Resource} */
let resource
try {
resource = await reader.getResource(decodeURI(request.url))
resource = await reader.getResource(path)
} catch (e) {
// @ts-ignore
e.statusCode = 404
Expand Down
2 changes: 1 addition & 1 deletion map-viewer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

const map = new maplibregl.Map({
container: 'map',
style: 'style.json',
style: 'map/style.json',
})

map.on('error', (e) => {
Expand Down

0 comments on commit a7fd764

Please sign in to comment.