Skip to content

Commit 0ef9c30

Browse files
committed
fix: continue supporting legacy API endpoints
1 parent 11f12c9 commit 0ef9c30

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

Diff for: src/server.ts

+36-14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { HTTPMethod } from './types.ts'
1313
import { isNodeError, Logger } from './util.ts'
1414

1515
const API_URL_PATH = /\/api\/v1\/blobs\/(?<site_id>[^/]+)\/(?<store_name>[^/]+)\/?(?<key>[^?]*)/
16+
const LEGACY_API_URL_PATH = /\/api\/v1\/sites\/(?<site_id>[^/]+)\/blobs\/?(?<key>[^?]*)/
17+
const LEGACY_DEFAULT_STORE = 'production'
1618

1719
export enum Operation {
1820
DELETE = 'delete',
@@ -99,11 +101,11 @@ export class BlobsServer {
99101
async delete(req: http.IncomingMessage, res: http.ServerResponse) {
100102
const apiMatch = this.parseAPIRequest(req)
101103

102-
if (apiMatch) {
104+
if (apiMatch?.useSignedURL) {
103105
return this.sendResponse(req, res, 200, JSON.stringify({ url: apiMatch.url.toString() }))
104106
}
105107

106-
const url = new URL(req.url ?? '', this.address)
108+
const url = new URL(apiMatch?.url ?? req.url ?? '', this.address)
107109
const { dataPath, key, metadataPath } = this.getLocalPaths(url)
108110

109111
if (!dataPath || !key) {
@@ -390,22 +392,42 @@ export class BlobsServer {
390392

391393
const apiURLMatch = req.url.match(API_URL_PATH)
392394

393-
if (!apiURLMatch) {
394-
return null
395+
if (apiURLMatch) {
396+
const key = apiURLMatch.groups?.key
397+
const siteID = apiURLMatch.groups?.site_id as string
398+
const storeName = apiURLMatch.groups?.store_name as string
399+
const urlPath = [siteID, storeName, key].filter(Boolean) as string[]
400+
const url = new URL(`/${urlPath.join('/')}?signature=${this.tokenHash}`, this.address)
401+
402+
return {
403+
key,
404+
siteID,
405+
storeName,
406+
url,
407+
useSignedURL: req.headers.accept === 'application/json;type=signed-url',
408+
}
395409
}
396410

397-
const key = apiURLMatch.groups?.key
398-
const siteID = apiURLMatch.groups?.site_id as string
399-
const storeName = apiURLMatch.groups?.store_name as string
400-
const urlPath = [siteID, storeName, key].filter(Boolean) as string[]
401-
const url = new URL(`/${urlPath.join('/')}?signature=${this.tokenHash}`, this.address)
411+
const legacyAPIURLMatch = req.url.match(LEGACY_API_URL_PATH)
402412

403-
return {
404-
key,
405-
siteID,
406-
storeName,
407-
url,
413+
if (legacyAPIURLMatch) {
414+
const fullURL = new URL(req.url, this.address)
415+
const storeName = fullURL.searchParams.get('context') ?? LEGACY_DEFAULT_STORE
416+
const key = legacyAPIURLMatch.groups?.key
417+
const siteID = legacyAPIURLMatch.groups?.site_id as string
418+
const urlPath = [siteID, storeName, key].filter(Boolean) as string[]
419+
const url = new URL(`/${urlPath.join('/')}?signature=${this.tokenHash}`, this.address)
420+
421+
return {
422+
key,
423+
siteID,
424+
storeName,
425+
url,
426+
useSignedURL: true,
427+
}
408428
}
429+
430+
return null
409431
}
410432

411433
sendResponse(req: http.IncomingMessage, res: http.ServerResponse, status: number, body?: string) {

0 commit comments

Comments
 (0)