payload-plugin-backup-postgres 0.5.0-alpha.0 → 0.5.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -12,6 +12,24 @@ function queryParam(req, key) {
|
|
|
12
12
|
return '';
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
|
+
function decompressGzipStream(stream) {
|
|
16
|
+
const decompression = new DecompressionStream('gzip');
|
|
17
|
+
const writer = decompression.writable.getWriter();
|
|
18
|
+
return stream.pipeThrough({
|
|
19
|
+
readable: decompression.readable,
|
|
20
|
+
writable: new WritableStream({
|
|
21
|
+
write (chunk) {
|
|
22
|
+
return writer.write(new Uint8Array(chunk));
|
|
23
|
+
},
|
|
24
|
+
close () {
|
|
25
|
+
return writer.close();
|
|
26
|
+
},
|
|
27
|
+
abort (reason) {
|
|
28
|
+
return writer.abort(reason);
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
});
|
|
32
|
+
}
|
|
15
33
|
/**
|
|
16
34
|
* Authenticated download of a backup archive. Streams via Vercel Blob `get` (private + public) and
|
|
17
35
|
* optionally anonymous `fetch` of the list `downloadUrl` for public blobs. Pass `url` and
|
|
@@ -56,12 +74,15 @@ function queryParam(req, key) {
|
|
|
56
74
|
status: 404
|
|
57
75
|
});
|
|
58
76
|
}
|
|
77
|
+
const isCompressedJson = pathname.endsWith('.json.gz');
|
|
78
|
+
const downloadStream = isCompressedJson ? decompressGzipStream(opened.stream) : opened.stream;
|
|
59
79
|
const filename = pathname.split('/').pop() || 'backup';
|
|
60
|
-
const
|
|
61
|
-
|
|
80
|
+
const downloadFilename = isCompressedJson ? filename.slice(0, -'.gz'.length) : filename;
|
|
81
|
+
const safeName = downloadFilename.replace(/["\r\n]/g, '_');
|
|
82
|
+
return new Response(downloadStream, {
|
|
62
83
|
headers: {
|
|
63
84
|
'Content-Disposition': `attachment; filename="${safeName}"`,
|
|
64
|
-
'Content-Type': opened.contentType || 'application/octet-stream'
|
|
85
|
+
'Content-Type': isCompressedJson ? 'application/json' : opened.contentType || 'application/octet-stream'
|
|
65
86
|
}
|
|
66
87
|
});
|
|
67
88
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/endpoints/paths/admin-backup-download.ts"],"sourcesContent":["import type { Endpoint, PayloadRequest } from 'payload'\n\nimport type { BackupPluginOptions } from '../../types'\n\nimport {\n getResolvedCronBackupSettings,\n resolveBackupBlobAccess,\n resolveBackupBlobToken,\n} from '../../core/backupSettings'\nimport { isBackupStorageConfigured, resolveBackupStorage } from '../../core/storage'\nimport { requireBackupAdmin } from '../shared'\n\nfunction queryParam(req: PayloadRequest, key: string): string {\n const fromPayload = req.searchParams.get(key)\n if (fromPayload) {\n return fromPayload.trim()\n }\n try {\n return new URL((req as unknown as Request).url).searchParams.get(key)?.trim() || ''\n } catch {\n return ''\n }\n}\n\n/**\n * Authenticated download of a backup archive. Streams via Vercel Blob `get` (private + public) and\n * optionally anonymous `fetch` of the list `downloadUrl` for public blobs. Pass `url` and\n * `downloadUrl` from `list()` so the correct public/private hostname is used.\n */\nexport function createAdminBackupDownloadEndpoint(options: BackupPluginOptions): Endpoint {\n return {\n handler: async (req) => {\n const auth = await requireBackupAdmin(req, options)\n if (auth instanceof Response) {\n return auth\n }\n\n const { payload } = req\n const settings = await getResolvedCronBackupSettings(payload)\n const token = resolveBackupBlobToken(settings).trim()\n if (!isBackupStorageConfigured(token)) {\n return new Response('Service unavailable', { status: 503 })\n }\n\n const pathname = queryParam(req, 'pathname')\n if (!pathname.startsWith('backups/')) {\n return new Response('Invalid pathname', { status: 400 })\n }\n\n const blobUrl = queryParam(req, 'url')\n const downloadUrl = queryParam(req, 'downloadUrl')\n\n const storage = resolveBackupStorage({\n blobAccess: resolveBackupBlobAccess(settings),\n blobToken: token,\n })\n const opened = await storage.openDownloadStream({\n downloadUrl: downloadUrl || undefined,\n pathname,\n url: blobUrl || undefined,\n })\n\n if (!opened) {\n payload.logger.warn({ pathname }, '[backup-download] Blob not found or unreadable')\n return new Response('Backup not found', { status: 404 })\n }\n\n const filename = pathname.split('/').pop() || 'backup'\n const
|
|
1
|
+
{"version":3,"sources":["../../../src/endpoints/paths/admin-backup-download.ts"],"sourcesContent":["import type { Endpoint, PayloadRequest } from 'payload'\n\nimport type { BackupPluginOptions } from '../../types'\n\nimport {\n getResolvedCronBackupSettings,\n resolveBackupBlobAccess,\n resolveBackupBlobToken,\n} from '../../core/backupSettings'\nimport { isBackupStorageConfigured, resolveBackupStorage } from '../../core/storage'\nimport { requireBackupAdmin } from '../shared'\n\nfunction queryParam(req: PayloadRequest, key: string): string {\n const fromPayload = req.searchParams.get(key)\n if (fromPayload) {\n return fromPayload.trim()\n }\n try {\n return new URL((req as unknown as Request).url).searchParams.get(key)?.trim() || ''\n } catch {\n return ''\n }\n}\n\nfunction decompressGzipStream(stream: ReadableStream<Uint8Array>): ReadableStream<Uint8Array> {\n const decompression = new DecompressionStream('gzip')\n const writer = decompression.writable.getWriter()\n return stream.pipeThrough({\n readable: decompression.readable,\n writable: new WritableStream<Uint8Array>({\n write(chunk) {\n return writer.write(new Uint8Array(chunk))\n },\n close() {\n return writer.close()\n },\n abort(reason) {\n return writer.abort(reason)\n },\n }),\n })\n}\n\n/**\n * Authenticated download of a backup archive. Streams via Vercel Blob `get` (private + public) and\n * optionally anonymous `fetch` of the list `downloadUrl` for public blobs. Pass `url` and\n * `downloadUrl` from `list()` so the correct public/private hostname is used.\n */\nexport function createAdminBackupDownloadEndpoint(options: BackupPluginOptions): Endpoint {\n return {\n handler: async (req) => {\n const auth = await requireBackupAdmin(req, options)\n if (auth instanceof Response) {\n return auth\n }\n\n const { payload } = req\n const settings = await getResolvedCronBackupSettings(payload)\n const token = resolveBackupBlobToken(settings).trim()\n if (!isBackupStorageConfigured(token)) {\n return new Response('Service unavailable', { status: 503 })\n }\n\n const pathname = queryParam(req, 'pathname')\n if (!pathname.startsWith('backups/')) {\n return new Response('Invalid pathname', { status: 400 })\n }\n\n const blobUrl = queryParam(req, 'url')\n const downloadUrl = queryParam(req, 'downloadUrl')\n\n const storage = resolveBackupStorage({\n blobAccess: resolveBackupBlobAccess(settings),\n blobToken: token,\n })\n const opened = await storage.openDownloadStream({\n downloadUrl: downloadUrl || undefined,\n pathname,\n url: blobUrl || undefined,\n })\n\n if (!opened) {\n payload.logger.warn({ pathname }, '[backup-download] Blob not found or unreadable')\n return new Response('Backup not found', { status: 404 })\n }\n\n const isCompressedJson = pathname.endsWith('.json.gz')\n const downloadStream =\n isCompressedJson ? decompressGzipStream(opened.stream) : opened.stream\n const filename = pathname.split('/').pop() || 'backup'\n const downloadFilename = isCompressedJson ? filename.slice(0, -'.gz'.length) : filename\n const safeName = downloadFilename.replace(/[\"\\r\\n]/g, '_')\n return new Response(downloadStream, {\n headers: {\n 'Content-Disposition': `attachment; filename=\"${safeName}\"`,\n 'Content-Type':\n isCompressedJson ? 'application/json' : (\n opened.contentType || 'application/octet-stream'\n ),\n },\n })\n },\n method: 'get',\n path: '/backup-postgres/admin/backup-download',\n }\n}\n"],"names":["getResolvedCronBackupSettings","resolveBackupBlobAccess","resolveBackupBlobToken","isBackupStorageConfigured","resolveBackupStorage","requireBackupAdmin","queryParam","req","key","fromPayload","searchParams","get","trim","URL","url","decompressGzipStream","stream","decompression","DecompressionStream","writer","writable","getWriter","pipeThrough","readable","WritableStream","write","chunk","Uint8Array","close","abort","reason","createAdminBackupDownloadEndpoint","options","handler","auth","Response","payload","settings","token","status","pathname","startsWith","blobUrl","downloadUrl","storage","blobAccess","blobToken","opened","openDownloadStream","undefined","logger","warn","isCompressedJson","endsWith","downloadStream","filename","split","pop","downloadFilename","slice","length","safeName","replace","headers","contentType","method","path"],"mappings":"AAIA,SACEA,6BAA6B,EAC7BC,uBAAuB,EACvBC,sBAAsB,QACjB,4BAA2B;AAClC,SAASC,yBAAyB,EAAEC,oBAAoB,QAAQ,qBAAoB;AACpF,SAASC,kBAAkB,QAAQ,YAAW;AAE9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