Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: simplify fastify maps plugin and add support for styled map packages #896

Merged
merged 4 commits into from
Oct 9, 2024
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
1,543 changes: 1,456 additions & 87 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,10 @@
"yazl": "^2.5.1"
},
"dependencies": {
"@comapeo/fallback-smp": "^1.0.0",
"@comapeo/schema": "1.0.0",
"@digidem/types": "^2.3.0",
"@electron/asar": "^3.2.8",
"@fastify/error": "^3.4.1",
"@fastify/static": "^7.0.3",
"@fastify/type-provider-typebox": "^4.1.0",
"@hyperswarm/secret-stream": "^6.6.3",
"@mapeo/crypto": "1.0.0-alpha.10",
Expand Down Expand Up @@ -192,6 +191,7 @@
"sodium-universal": "^4.0.0",
"start-stop-state-machine": "^1.2.0",
"streamx": "^2.19.0",
"styled-map-package": "^1.1.0",
"sub-encoder": "^2.1.1",
"throttle-debounce": "^5.0.0",
"tiny-typed-emitter": "^2.1.0",
Expand Down
65 changes: 65 additions & 0 deletions src/fastify-plugins/maps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { fetch } from 'undici'
import { Server as SMPServerPlugin } from 'styled-map-package'

import { noop } from '../utils.js'

/** @import { FastifyPluginAsync } from 'fastify' */

export const CUSTOM_MAP_PREFIX = 'custom'
export const FALLBACK_MAP_PREFIX = 'fallback'

/**
* @typedef {Object} MapsPluginOpts
*
* @property {string | URL} defaultOnlineStyleUrl
* @property {string} [customMapPath]
* @property {string} fallbackMapPath
*/

/** @type {FastifyPluginAsync<MapsPluginOpts>} */
export async function plugin(fastify, opts) {
if (opts.customMapPath) {
fastify.register(SMPServerPlugin, {
prefix: CUSTOM_MAP_PREFIX,
filepath: opts.customMapPath,
})
}

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

fastify.get('/style.json', async (_request, reply) => {
const baseUrl = new URL(fastify.prefix, fastify.listeningOrigin)

// Important for using as a base for creating new URL objects
if (!baseUrl.href.endsWith('/')) {
baseUrl.href += '/'
}

/** @type {Array<string | URL>}*/
const styleUrls = [
opts.defaultOnlineStyleUrl,
new URL(`${FALLBACK_MAP_PREFIX}/style.json`, baseUrl),
]

if (opts.customMapPath) {
styleUrls.unshift(new URL(`${CUSTOM_MAP_PREFIX}/style.json`, baseUrl))
}

for (const url of styleUrls) {
const resp = await fetch(url, { method: 'HEAD' }).catch(noop)

if (resp && resp.status === 200) {
return reply
.headers({
'cache-control': 'no-cache',
})
.redirect(url.toString())
}
}

return reply.status(404).send()
})
}
173 changes: 0 additions & 173 deletions src/fastify-plugins/maps/index.js

This file was deleted.

114 changes: 0 additions & 114 deletions src/fastify-plugins/maps/offline-fallback-map.js

This file was deleted.

Loading
Loading