Skip to content
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 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export { CUSTOM_MAP_ID, DEFAULT_MAP_ID } from './lib/constants.js'

export type {
MapInfo,
MapInfoResponse,
MapShareState,
MapShareStateUpdate,
DownloadStateUpdate,
Expand Down
20 changes: 12 additions & 8 deletions src/routes/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '../lib/constants.js'
import { errors } from '../lib/errors.js'
import { addTrailingSlash, noop } from '../lib/utils.js'
import type { MapInfoResponse } from '../types.js'

type MapRequest = IRequestStrict & {
params: {
Expand All @@ -27,14 +28,17 @@ export function MapsRouter({ base = '/' }, ctx: Context) {

const router = IttyRouter<IRequestStrict>({ base })

router.get<MapRequest>(`/:mapId/info`, async (request) => {
const info = await ctx.getMapInfo(request.params.mapId)
return {
created: info.mapCreatedAt,
size: info.estimatedSizeBytes,
name: info.mapName,
}
})
router.get<MapRequest>(
`/:mapId/info`,
async (request): Promise<MapInfoResponse> => {
const info = await ctx.getMapInfo(request.params.mapId)
return {
created: info.mapCreatedAt,
size: info.estimatedSizeBytes,
name: info.mapName,
}
},
)

const uploadHandler: RequestHandler<MapRequest> = async (request) => {
const writable = ctx.createMapWritableStream(request.params.mapId)
Expand Down
13 changes: 13 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ const MapInfo = T.Object({
}),
})

export const MapInfoResponse = T.Object({
created: T.Number({
description: 'Timestamp (ms since epoch) when the map was created',
}),
size: T.Number({ description: 'Estimated size of the map data in bytes' }),
name: T.String({ description: 'The name of the map' }),
})

export type MapInfoResponse = Static<typeof MapInfoResponse>

const MapShareBase = T.Intersect([
T.Object({
receiverDeviceId: T.String({
Expand All @@ -126,6 +136,9 @@ export type MapShareState = DistributiveIntersection<
Static<typeof MapShareStateUpdate>
>

/**
* @deprecated We don't yet return this type from any API, and it may be removed in a future release. Use MapInfoResponse instead.
*/
export type MapInfo = Static<typeof MapInfo>

export type FetchContext = {
Expand Down
24 changes: 13 additions & 11 deletions test/maps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import { setTimeout as delay } from 'node:timers/promises'
import { fileURLToPath } from 'node:url'

import { Reader } from 'styled-map-package'
import Value from 'typebox/value'
import { describe, it, expect, vi } from 'vitest'

import { MapInfoResponse } from '../src/types.js'
import {
DEMOTILES_Z2,
goOffline,
Expand Down Expand Up @@ -60,6 +62,7 @@ describe('Maps API', () => {
const response = await fetch(`${localBaseUrl}/maps/custom/info`)
expect(response.status).toBe(200)
const info = await response.json()
expect(Value.Check(MapInfoResponse, info)).toBe(true)
expect(info).toHaveProperty('created')
expect(info).toHaveProperty('size')
expect(info).toHaveProperty('name')
Expand All @@ -73,6 +76,7 @@ describe('Maps API', () => {
const response = await fetch(`${localBaseUrl}/maps/fallback/info`)
expect(response.status).toBe(200)
const info = await response.json()
expect(Value.Check(MapInfoResponse, info)).toBe(true)
expect(info).toHaveProperty('created')
expect(info).toHaveProperty('size')
expect(info).toHaveProperty('name')
Expand Down Expand Up @@ -580,8 +584,9 @@ describe('Map Upload', () => {
// Mock fs.createWriteStream to return a writable that errors on write,
// simulating a disk-full scenario
const originalCreateWriteStream = fs.createWriteStream
const spy = vi.spyOn(fs, 'createWriteStream').mockImplementation(
(...args: any[]) => {
const spy = vi
.spyOn(fs, 'createWriteStream')
.mockImplementation((...args: any[]) => {
const stream = originalCreateWriteStream.apply(fs, args as any)
stream._write = (
_chunk: any,
Expand All @@ -591,8 +596,7 @@ describe('Map Upload', () => {
callback(new Error('ENOSPC: no space left on device'))
}
return stream
},
)
})
t.onTestFinished(() => spy.mockRestore())

const fileBuffer = fs.readFileSync(OSM_BRIGHT_Z6)
Expand Down Expand Up @@ -627,17 +631,15 @@ describe('Map Upload', () => {
// Mock fs.createWriteStream to return a writable that writes successfully
// but errors on close (_final), simulating a flush/sync failure
const originalCreateWriteStream = fs.createWriteStream
const spy = vi.spyOn(fs, 'createWriteStream').mockImplementation(
(...args: any[]) => {
const spy = vi
.spyOn(fs, 'createWriteStream')
.mockImplementation((...args: any[]) => {
const stream = originalCreateWriteStream.apply(fs, args as any)
stream._final = (
callback: (error?: Error | null) => void,
) => {
stream._final = (callback: (error?: Error | null) => void) => {
callback(new Error('EIO: i/o error'))
}
return stream
},
)
})
t.onTestFinished(() => spy.mockRestore())

const fileBuffer = fs.readFileSync(OSM_BRIGHT_Z6)
Expand Down