|  | 
|  | 1 | +import fp from 'fastify-plugin' | 
|  | 2 | +import { Reader } from 'styled-map-package' | 
|  | 3 | + | 
|  | 4 | +/** @import {FastifyPluginAsync} from 'fastify' */ | 
|  | 5 | +/** @import {Resource, SMPStyle} from 'styled-map-package/reader.js' */ | 
|  | 6 | + | 
|  | 7 | +export const PLUGIN_NAME = 'comapeo-styled-map-package' | 
|  | 8 | + | 
|  | 9 | +/** | 
|  | 10 | + * @typedef {object} StyledMapPackagePluginOpts | 
|  | 11 | + * @property {string} filepath | 
|  | 12 | + * @property {boolean} [lazy] | 
|  | 13 | + * @property {string} [prefix] | 
|  | 14 | + * | 
|  | 15 | + */ | 
|  | 16 | + | 
|  | 17 | +/** | 
|  | 18 | + * @typedef {object} StyledMapPackagePluginDecorator | 
|  | 19 | + * @property {(baseUrl?: string) => Promise<SMPStyle>} getStyle | 
|  | 20 | + * @property {(path: string) => Promise<Resource>} getResource | 
|  | 21 | + */ | 
|  | 22 | + | 
|  | 23 | +export const plugin = fp(styledMapPackagePlugin, { | 
|  | 24 | +  fastify: '4.x', | 
|  | 25 | +  name: PLUGIN_NAME, | 
|  | 26 | +}) | 
|  | 27 | + | 
|  | 28 | +/** @type {FastifyPluginAsync<StyledMapPackagePluginOpts>} */ | 
|  | 29 | +async function styledMapPackagePlugin(fastify, opts) { | 
|  | 30 | +  let reader = opts.lazy ? null : new Reader(opts.filepath) | 
|  | 31 | + | 
|  | 32 | +  fastify.addHook('onClose', async () => { | 
|  | 33 | +    if (reader) { | 
|  | 34 | +      // Can fail to close if `opts.filepath` used for instantiation is invalid | 
|  | 35 | +      try { | 
|  | 36 | +        await reader.close() | 
|  | 37 | +      } catch (err) { | 
|  | 38 | +        fastify.log.warn('Failed to close SMP reader instance', err) | 
|  | 39 | +      } | 
|  | 40 | +    } | 
|  | 41 | +  }) | 
|  | 42 | + | 
|  | 43 | +  fastify.decorate('comapeoSmp', { | 
|  | 44 | +    async getStyle(baseUrl) { | 
|  | 45 | +      if (!reader) { | 
|  | 46 | +        reader = new Reader(opts.filepath) | 
|  | 47 | +      } | 
|  | 48 | + | 
|  | 49 | +      const base = | 
|  | 50 | +        baseUrl || new URL(opts.prefix || '', fastify.listeningOrigin).href | 
|  | 51 | + | 
|  | 52 | +      return reader.getStyle(base) | 
|  | 53 | +    }, | 
|  | 54 | + | 
|  | 55 | +    async getResource(path) { | 
|  | 56 | +      if (!reader) { | 
|  | 57 | +        reader = new Reader(opts.filepath) | 
|  | 58 | +      } | 
|  | 59 | + | 
|  | 60 | +      return reader.getResource(path) | 
|  | 61 | +    }, | 
|  | 62 | +  }) | 
|  | 63 | + | 
|  | 64 | +  fastify.register(routes, { | 
|  | 65 | +    prefix: opts.prefix, | 
|  | 66 | +  }) | 
|  | 67 | +} | 
|  | 68 | + | 
|  | 69 | +/** @type {FastifyPluginAsync} */ | 
|  | 70 | +async function routes(fastify) { | 
|  | 71 | +  if (!fastify.hasDecorator('comapeoSmp')) { | 
|  | 72 | +    throw new Error('Could not find `comapeoSmp` decorator') | 
|  | 73 | +  } | 
|  | 74 | + | 
|  | 75 | +  fastify.get('/style.json', async () => { | 
|  | 76 | +    const baseUrl = fastify.prefix | 
|  | 77 | +      ? new URL(fastify.prefix, fastify.listeningOrigin).href | 
|  | 78 | +      : fastify.listeningOrigin | 
|  | 79 | + | 
|  | 80 | +    return fastify.comapeoSmp.getStyle(baseUrl) | 
|  | 81 | +  }) | 
|  | 82 | + | 
|  | 83 | +  fastify.get('*', async (request, reply) => { | 
|  | 84 | +    /** @type {Resource} */ | 
|  | 85 | +    let resource | 
|  | 86 | +    try { | 
|  | 87 | +      // Removes the prefix that might have been registered by a consumer of the plugin | 
|  | 88 | +      const normalizedPath = fastify.prefix | 
|  | 89 | +        ? request.url.replace(fastify.prefix, '') | 
|  | 90 | +        : request.url | 
|  | 91 | + | 
|  | 92 | +      resource = await fastify.comapeoSmp.getResource(decodeURI(normalizedPath)) | 
|  | 93 | +    } catch (e) { | 
|  | 94 | +      // @ts-expect-error | 
|  | 95 | +      e.statusCode = 404 | 
|  | 96 | +      throw e | 
|  | 97 | +    } | 
|  | 98 | + | 
|  | 99 | +    reply | 
|  | 100 | +      .type(resource.contentType) | 
|  | 101 | +      .header('content-length', resource.contentLength) | 
|  | 102 | + | 
|  | 103 | +    if (resource.contentEncoding) { | 
|  | 104 | +      reply.header('content-encoding', resource.contentEncoding) | 
|  | 105 | +    } | 
|  | 106 | + | 
|  | 107 | +    return reply.send(resource.stream) | 
|  | 108 | +  }) | 
|  | 109 | +} | 
0 commit comments