Skip to content

Commit 3e511d3

Browse files
committed
fix: use cross-OS paths
1 parent 5941642 commit 3e511d3

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

src/server.test.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ afterEach(() => {
2828
})
2929

3030
const siteID = '9a003659-aaaa-0000-aaaa-63d3720d8621'
31-
const key = '54321'
3231
const token = 'my-very-secret-token'
3332

3433
test('Reads and writes from the file system', async () => {
@@ -52,17 +51,21 @@ test('Reads and writes from the file system', async () => {
5251
name: 'Netlify',
5352
}
5453

55-
await blobs.set(key, 'value 1')
56-
expect(await blobs.get(key)).toBe('value 1')
54+
await blobs.set('simple-key', 'value 1')
55+
expect(await blobs.get('simple-key')).toBe('value 1')
5756

58-
await blobs.set(key, 'value 2', { metadata })
59-
expect(await blobs.get(key)).toBe('value 2')
57+
await blobs.set('simple-key', 'value 2', { metadata })
58+
expect(await blobs.get('simple-key')).toBe('value 2')
6059

61-
const entry = await blobs.getWithMetadata(key)
60+
await blobs.set('parent/child', 'value 3')
61+
expect(await blobs.get('parent/child')).toBe('value 3')
62+
expect(await blobs.get('parent')).toBe(null)
63+
64+
const entry = await blobs.getWithMetadata('simple-key')
6265
expect(entry.metadata).toEqual(metadata)
6366

64-
await blobs.delete(key)
65-
expect(await blobs.get(key)).toBe(null)
67+
await blobs.delete('simple-key')
68+
expect(await blobs.get('simple-key')).toBe(null)
6669

6770
await server.stop()
6871
await fs.rm(directory.path, { force: true, recursive: true })
@@ -88,6 +91,7 @@ test('Separates keys from different stores', async () => {
8891
token,
8992
siteID,
9093
})
94+
const key = 'my-key'
9195

9296
await store1.set(key, 'value 1 for store 1')
9397
await store2.set(key, 'value 1 for store 2')
@@ -113,7 +117,7 @@ test('If a token is set, rejects any requests with an invalid `authorization` he
113117
siteID,
114118
})
115119

116-
await expect(async () => await blobs.get(key)).rejects.toThrowError(
120+
await expect(async () => await blobs.get('some-key')).rejects.toThrowError(
117121
'Netlify Blobs has generated an internal error: 403 response',
118122
)
119123

src/server.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createReadStream, createWriteStream, promises as fs } from 'node:fs'
22
import http from 'node:http'
33
import { tmpdir } from 'node:os'
4-
import { dirname, join, relative, resolve } from 'node:path'
4+
import { dirname, join, relative, resolve, sep } from 'node:path'
55

66
import { ListResponse } from './backend/list.ts'
77
import { decodeMetadata, encodeMetadata, METADATA_HEADER_EXTERNAL, METADATA_HEADER_INTERNAL } from './metadata.ts'
@@ -117,7 +117,7 @@ export class BlobsServer {
117117
const stream = createReadStream(dataPath)
118118

119119
stream.on('error', (error: NodeJS.ErrnoException) => {
120-
if (error.code === 'ENOENT') {
120+
if (error.code === 'EISDIR' || error.code === 'ENOENT') {
121121
return this.sendResponse(req, res, 404)
122122
}
123123

@@ -191,7 +191,6 @@ export class BlobsServer {
191191
await fs.mkdir(dirname(metadataPath), { recursive: true })
192192
await fs.writeFile(metadataPath, JSON.stringify(metadata))
193193
} catch (error) {
194-
console.error(error)
195194
this.logDebug('Error when writing data:', error)
196195

197196
return this.sendResponse(req, res, 500)
@@ -319,7 +318,13 @@ export class BlobsServer {
319318
for (const entry of entries) {
320319
const entryPath = join(path, entry)
321320
const stat = await fs.stat(entryPath)
322-
const key = relative(rootPath, entryPath)
321+
322+
let key = relative(rootPath, entryPath)
323+
324+
// Normalize keys to use `/` as delimiter regardless of OS.
325+
if (sep !== '/') {
326+
key = key.split(sep).join('/')
327+
}
323328

324329
// To match the key against the prefix, we start by creating a "mask",
325330
// which consists of the subset of the key up to the length of the

0 commit comments

Comments
 (0)