Skip to content

Commit 26d42f7

Browse files
authored
fix: rename instead of copy temp files during import (#25)
* fix: rename instead of copy temp files during import * debug * add patches to close readers * attempt to fix close issue
1 parent 480db54 commit 26d42f7

4 files changed

Lines changed: 68 additions & 4 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
diff --git a/node_modules/styled-map-package/.DS_Store b/node_modules/styled-map-package/.DS_Store
2+
new file mode 100644
3+
index 0000000..4b95902
4+
Binary files /dev/null and b/node_modules/styled-map-package/.DS_Store differ
5+
diff --git a/node_modules/styled-map-package/dist/reader.js b/node_modules/styled-map-package/dist/reader.js
6+
index 75482d9..430c90d 100644
7+
--- a/node_modules/styled-map-package/dist/reader.js
8+
+++ b/node_modules/styled-map-package/dist/reader.js
9+
@@ -117,7 +117,8 @@ class Reader {
10+
async close() {
11+
if (this.#closePromise) return this.#closePromise;
12+
this.#closePromise = (async () => {
13+
- const zip = await this.#zipPromise;
14+
+ const zip = await this.#zipPromise.catch(noop);
15+
+ if (!zip) return;
16+
await zip.close();
17+
})();
18+
return this.#closePromise;

patches/yauzl-promise+4.0.0.patch

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
diff --git a/node_modules/yauzl-promise/lib/index.js b/node_modules/yauzl-promise/lib/index.js
2+
index 8cb0e6c..a1aa47b 100644
3+
--- a/node_modules/yauzl-promise/lib/index.js
4+
+++ b/node_modules/yauzl-promise/lib/index.js
5+
@@ -54,7 +54,12 @@ async function open(path, options) {
6+
const {size} = await fstatAsync(reader.fd);
7+
8+
const zip = new Zip(INTERNAL_SYMBOL, reader, size, options);
9+
- await zip._init();
10+
+ try {
11+
+ await zip._init();
12+
+ } catch (err) {
13+
+ await reader.close();
14+
+ throw err;
15+
+ }
16+
return zip;
17+
}
18+

src/context.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,21 +145,27 @@ export class Context {
145145
await fsPromises.unlink(tempPath).catch(noop)
146146
throw new errors.INVALID_MAP_FILE()
147147
} finally {
148-
await tempReader.close().catch(noop)
148+
await tempReader.close().catch((e) => {
149+
console.error('Error closing temp reader:', e)
150+
})
149151
}
150152

151153
// Graceful replacement of SMP Reader when map file is updated
152154
const readerPromise = (async () => {
153155
const existingReaderPromise = this.#mapReaders.get(mapId)
154156
if (existingReaderPromise) {
157+
console.log(`Closing existing reader for map ID "${mapId}"`)
155158
const existingReader = await existingReaderPromise
156-
await existingReader.close().catch(noop)
159+
await existingReader.opened().catch(noop) // Ensure reader is fully opened before closing
160+
await existingReader.close().catch((e) => {
161+
console.error('Error closing existing reader:', e)
162+
})
157163
}
158-
await fsPromises.cp(tempPath, mapFileUrl, { force: true })
164+
await fsPromises.rename(tempPath, mapFileUrl)
159165
return new Reader(fileURLToPath(mapFileUrl))
160166
})()
161167
this.#mapReaders.set(mapId, readerPromise)
162-
// Wait for the file copy to complete before closing the stream
168+
// Wait for the file rename to complete before closing the stream
163169
await readerPromise
164170
},
165171
async abort(err) {

test/maps.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,28 @@ describe('Map Upload', () => {
569569
const style = await styleResponse.json()
570570
expect(style).toEqual(expectedStyle)
571571
})
572+
573+
it('should not leave temp files after successful upload', async (t) => {
574+
const { localBaseUrl, customMapPath } = await startServer(t, {
575+
customMapPath: nonExistentPath,
576+
})
577+
const tmpDir = path.dirname(customMapPath)
578+
const customMapBasename = path.basename(customMapPath)
579+
580+
const fileBuffer = fs.readFileSync(OSM_BRIGHT_Z6)
581+
const response = await fetch(`${localBaseUrl}/maps/custom`, {
582+
method: 'PUT',
583+
body: fileBuffer,
584+
headers: { 'Content-Type': 'application/octet-stream' },
585+
})
586+
expect(response.status).toBe(200)
587+
588+
const filesInDir = fs.readdirSync(tmpDir)
589+
const tempFiles = filesInDir.filter(
590+
(f) => f.startsWith(customMapBasename) && f.includes('.download-'),
591+
)
592+
expect(tempFiles).toHaveLength(0)
593+
})
572594
})
573595

574596
describe('Invalid Map Uploads', () => {

0 commit comments

Comments
 (0)