Skip to content

Commit

Permalink
add method to MapeoManager
Browse files Browse the repository at this point in the history
  • Loading branch information
achou11 committed Mar 12, 2024
1 parent 78a852f commit 4143832
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/mapeo-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { drizzle } from 'drizzle-orm/better-sqlite3'
import { migrate } from 'drizzle-orm/better-sqlite3/migrator'
import Hypercore from 'hypercore'
import { TypedEmitter } from 'tiny-typed-emitter'
import pTimeout from 'p-timeout'

import { IndexWriter } from './index-writer/index.js'
import {
Expand Down Expand Up @@ -794,6 +795,11 @@ export class MapeoManager extends TypedEmitter {

this.#activeProjects.delete(projectPublicId)
}

async getMapStyleJsonUrl() {
await pTimeout(this.#fastify.ready(), { milliseconds: 1000 })
return this.#fastify.mapeoMaps.getStyleJsonUrl()
}
}

// We use the `protomux` property of connected peers internally, but we don't
Expand Down
56 changes: 56 additions & 0 deletions test-e2e/manager-fastify-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ import Fastify from 'fastify'

import { MapeoManager } from '../src/mapeo-manager.js'
import { FastifyController } from '../src/fastify-controller.js'
import { plugin as StaticMapsPlugin } from '../src/fastify-plugins/maps/static-maps.js'
import { plugin as MapServerPlugin } from '../src/fastify-plugins/maps/index.js'

const BLOB_FIXTURES_DIR = fileURLToPath(
new URL('../tests/fixtures/blob-api/', import.meta.url)
)

const MAP_FIXTURES_PATH = new URL('../tests/fixtures/maps', import.meta.url)
.pathname

const projectMigrationsFolder = new URL('../drizzle/project', import.meta.url)
.pathname
const clientMigrationsFolder = new URL('../drizzle/client', import.meta.url)
Expand Down Expand Up @@ -281,6 +286,57 @@ test('retrieving icons using url', async (t) => {
await exceptionPromise2
})

test('retrieving style.json using stable url', async (t) => {
const clock = FakeTimers.install({ shouldAdvanceTime: true })
t.teardown(() => clock.uninstall())

const fastify = Fastify()

fastify.register(StaticMapsPlugin, {
prefix: 'static',
staticRootDir: MAP_FIXTURES_PATH,
})
fastify.register(MapServerPlugin, {
prefix: 'maps',
})

const fastifyController = new FastifyController({ fastify })
const manager = new MapeoManager({
rootKey: KeyManager.generateRootKey(),
projectMigrationsFolder,
clientMigrationsFolder,
dbFolder: ':memory:',
coreStorage: () => new RAM(),
fastify,
})

const exceptionPromise1 = t.exception(async () => {
await manager.getMapStyleJsonUrl()
}, 'cannot retrieve style json url before HTTP server starts')

clock.tick(100_000)
await exceptionPromise1

fastifyController.start({ port: 1234 })

const styleJsonUrl = await manager.getMapStyleJsonUrl()

t.ok(new URL(styleJsonUrl))

const response = await fetch(styleJsonUrl)

t.is(response.status, 200, 'response status ok')

await fastifyController.stop()

const exceptionPromise2 = t.exception(async () => {
await manager.getMapStyleJsonUrl()
}, 'cannot retrieve style json url after HTTP server closes')

clock.tick(100_000)
await exceptionPromise2
})

/**
* @param {string} url
*/
Expand Down

0 comments on commit 4143832

Please sign in to comment.