Skip to content
Open
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
13 changes: 9 additions & 4 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,21 +145,26 @@ export class Context {
await fsPromises.unlink(tempPath).catch(noop)
throw new errors.INVALID_MAP_FILE()
} finally {
await tempReader.close().catch(noop)
await tempReader.close().catch((e) => {
console.error('Error closing temp reader:', e)
})
}

// Graceful replacement of SMP Reader when map file is updated
const readerPromise = (async () => {
const existingReaderPromise = this.#mapReaders.get(mapId)
if (existingReaderPromise) {
console.log(`Closing existing reader for map ID "${mapId}"`)
const existingReader = await existingReaderPromise
await existingReader.close().catch(noop)
await existingReader.close().catch((e) => {
console.error('Error closing existing reader:', e)
})
}
await fsPromises.cp(tempPath, mapFileUrl, { force: true })
await fsPromises.rename(tempPath, mapFileUrl)
return new Reader(fileURLToPath(mapFileUrl))
})()
this.#mapReaders.set(mapId, readerPromise)
// Wait for the file copy to complete before closing the stream
// Wait for the file rename to complete before closing the stream
await readerPromise
},
async abort(err) {
Expand Down
22 changes: 22 additions & 0 deletions test/maps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,28 @@ describe('Map Upload', () => {
const style = await styleResponse.json()
expect(style).toEqual(expectedStyle)
})

it('should not leave temp files after successful upload', async (t) => {
const { localBaseUrl, customMapPath } = await startServer(t, {
customMapPath: nonExistentPath,
})
const tmpDir = path.dirname(customMapPath)
const customMapBasename = path.basename(customMapPath)

const fileBuffer = fs.readFileSync(OSM_BRIGHT_Z6)
const response = await fetch(`${localBaseUrl}/maps/custom`, {
method: 'PUT',
body: fileBuffer,
headers: { 'Content-Type': 'application/octet-stream' },
})
expect(response.status).toBe(200)

const filesInDir = fs.readdirSync(tmpDir)
const tempFiles = filesInDir.filter(
(f) => f.startsWith(customMapBasename) && f.includes('.download-'),
)
expect(tempFiles).toHaveLength(0)
})
})

describe('Invalid Map Uploads', () => {
Expand Down
Loading