-
Notifications
You must be signed in to change notification settings - Fork 3
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: add functionality to serve offline fallback map #450
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9f11a05
implement offline fallback map fastify plugin
achou11 e716cf6
export other maps fastify plugins
achou11 4c9bfa1
update createStyleJsonResponseHeaders()
achou11 91f1a1a
remove unnecessary async keyword
achou11 15ae238
remove unnecessary comments
achou11 ac21cf6
be more explicit about check for file type
achou11 be88383
be case-insensitive when checking file extension
achou11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import path from 'path' | ||
import fs from 'fs/promises' | ||
import FastifyStatic from '@fastify/static' | ||
import fp from 'fastify-plugin' | ||
|
||
import { | ||
NotFoundError, | ||
createStyleJsonResponseHeaders, | ||
getFastifyServerAddress, | ||
} from '../utils.js' | ||
|
||
export const PLUGIN_NAME = 'mapeo-static-maps' | ||
|
||
export const plugin = fp(offlineFallbackMapPlugin, { | ||
fastify: '4.x', | ||
name: PLUGIN_NAME, | ||
}) | ||
|
||
/** | ||
* @typedef {object} OfflineFallbackMapPluginOpts | ||
* @property {string} [prefix] | ||
* @property {string} styleJsonPath | ||
* @property {string} sourcesDir | ||
*/ | ||
|
||
/** | ||
* @typedef {object} FallbackMapPluginDecorator | ||
* @property {(serverAddress: string) => Promise<any>} getResolvedStyleJson | ||
* @property {() => Promise<import('node:fs').Stats>} getStyleJsonStats | ||
*/ | ||
|
||
/** @type {import('fastify').FastifyPluginAsync<OfflineFallbackMapPluginOpts>} */ | ||
async function offlineFallbackMapPlugin(fastify, opts) { | ||
const { styleJsonPath, sourcesDir } = opts | ||
|
||
fastify.decorate( | ||
'mapeoFallbackMap', | ||
/** @type {FallbackMapPluginDecorator} */ | ||
({ | ||
async getResolvedStyleJson(serverAddress) { | ||
const rawStyleJson = await fs.readFile(styleJsonPath, 'utf-8') | ||
const styleJson = JSON.parse(rawStyleJson) | ||
|
||
const sources = styleJson.sources || {} | ||
|
||
const sourcesDirFiles = await fs.readdir(sourcesDir, { | ||
withFileTypes: true, | ||
}) | ||
|
||
for (const file of sourcesDirFiles) { | ||
if (!file.isFile()) continue | ||
|
||
if (file.name === 'style.json') continue | ||
|
||
const extension = path.extname(file.name).toLowerCase() | ||
if (!(extension === '.json' || extension === '.geojson')) continue | ||
EvanHahn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const sourceName = path.basename(file.name, extension) + '-source' | ||
|
||
sources[sourceName] = { | ||
type: 'geojson', | ||
data: new URL(`${opts.prefix || ''}/${file.name}`, serverAddress) | ||
.href, | ||
} | ||
} | ||
|
||
styleJson.sources = sources | ||
|
||
return styleJson | ||
}, | ||
getStyleJsonStats() { | ||
return fs.stat(styleJsonPath) | ||
}, | ||
}) | ||
) | ||
|
||
fastify.register(routes, { | ||
prefix: opts.prefix, | ||
styleJsonPath: opts.styleJsonPath, | ||
sourcesDir: opts.sourcesDir, | ||
}) | ||
} | ||
|
||
/** @type {import('fastify').FastifyPluginAsync<OfflineFallbackMapPluginOpts, import('fastify').RawServerDefault, import('@fastify/type-provider-typebox').TypeBoxTypeProvider>} */ | ||
async function routes(fastify, opts) { | ||
const { sourcesDir } = opts | ||
|
||
fastify.register(FastifyStatic, { | ||
root: sourcesDir, | ||
decorateReply: false, | ||
}) | ||
|
||
fastify.get('/style.json', async (req, rep) => { | ||
const serverAddress = await getFastifyServerAddress(req.server.server) | ||
|
||
let stats, styleJson | ||
|
||
try { | ||
const results = await Promise.all([ | ||
fastify.mapeoFallbackMap.getStyleJsonStats(), | ||
fastify.mapeoFallbackMap.getResolvedStyleJson(serverAddress), | ||
]) | ||
|
||
stats = results[0] | ||
styleJson = results[1] | ||
} catch (err) { | ||
throw new NotFoundError(`id = fallback, style.json`) | ||
} | ||
|
||
rep.headers(createStyleJsonResponseHeaders(stats.mtime)) | ||
|
||
return styleJson | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export { plugin as MapeoStaticMapsFastifyPlugin } from './fastify-plugins/maps/static-maps.js' | ||
export { plugin as MapeoOfflineFallbackMapFastifyPlugin } from './fastify-plugins/maps/offline-fallback-map.js' | ||
export { plugin as MapeoMapsFastifyPlugin } from './fastify-plugins/maps/index.js' | ||
export { FastifyController } from './fastify-controller.js' | ||
export { MapeoManager } from './mapeo-manager.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added as a dev dependency but if we want to be more strict about using this specifically, can change to direct or peer dep.
wasn't too keen on the offline map being bundled as of the npm package so i figured it would make sense to let the consumer decide to add the map as a dependency in its own project