@payloadcms/storage-vercel-blob 4.0.0-internal.f15737c → 4.0.0-internal.f2a04f7
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.
- package/README.md +10 -8
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js +17 -6
- package/dist/adapter.js.map +1 -1
- package/dist/client/VercelBlobClientUploadHandler.d.ts.map +1 -1
- package/dist/client/VercelBlobClientUploadHandler.js +3 -9
- package/dist/client/VercelBlobClientUploadHandler.js.map +1 -1
- package/dist/copyFile.d.ts +12 -0
- package/dist/copyFile.d.ts.map +1 -0
- package/dist/copyFile.js +86 -0
- package/dist/copyFile.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/package.json +15 -15
package/README.md
CHANGED
|
@@ -41,11 +41,13 @@ export default buildConfig({
|
|
|
41
41
|
})
|
|
42
42
|
```
|
|
43
43
|
|
|
44
|
-
| Option | Description
|
|
45
|
-
| -------------------- |
|
|
46
|
-
| `enabled` | Whether or not to enable the plugin
|
|
47
|
-
| `collections` | Collections to apply the Vercel Blob adapter to
|
|
48
|
-
| `addRandomSuffix` |
|
|
49
|
-
| `cacheControlMaxAge` | Cache-Control max-age in seconds
|
|
50
|
-
| `token` | Vercel Blob storage read/write token
|
|
51
|
-
| `clientUploads` | Upload directly to Vercel Blob instead of through Payload.
|
|
44
|
+
| Option | Description | Default |
|
|
45
|
+
| -------------------- | --------------------------------------------------------------------------------------- | ----------------------------- |
|
|
46
|
+
| `enabled` | Whether or not to enable the plugin | `true` |
|
|
47
|
+
| `collections` | Collections to apply the Vercel Blob adapter to | |
|
|
48
|
+
| `addRandomSuffix` | Must be `false` or omitted; enabled adapters with configured collections reject `true`. | `false` |
|
|
49
|
+
| `cacheControlMaxAge` | Cache-Control max-age in seconds | `365 * 24 * 60 * 60` (1 Year) |
|
|
50
|
+
| `token` | Vercel Blob storage read/write token | `''` |
|
|
51
|
+
| `clientUploads` | Upload directly to Vercel Blob instead of through Payload. | |
|
|
52
|
+
|
|
53
|
+
Set `addRandomSuffix` to `false` or omit it. Payload requires exact storage paths for reads, rollback, and deletion, and already allocates unique `_objectKey` folders for managed uploads. Enabled adapters with configured collections reject `true` during initialization. Disabled adapters retain their existing behavior. Changing this option does not rename existing blobs; migrate any mismatched stored paths and saved metadata together.
|
package/dist/adapter.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,OAAO,EACP,mBAAmB,
|
|
1
|
+
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,OAAO,EACP,mBAAmB,EAGpB,MAAM,wCAAwC,CAAA;AAU/C,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,6BAA6B,CAAA;AAS7E,UAAU,2BAA2B;IACnC,MAAM,EAAE,QAAQ,CAAA;IAChB,eAAe,CAAC,EAAE,OAAO,CAAA;IACzB,OAAO,EAAE,MAAM,CAAA;IACf,kBAAkB,EAAE,MAAM,CAAA;IAC1B,aAAa,CAAC,EAAE,mBAAmB,CAAA;IACnC,iBAAiB,EAAE,0BAA0B,EAAE,CAAA;IAC/C,KAAK,EAAE,MAAM,CAAA;IACb,oBAAoB,CAAC,EAAE,OAAO,CAAA;CAC/B;AAED,wBAAgB,uBAAuB,CAAC,EACtC,MAAM,EACN,eAAe,EACf,OAAO,EACP,kBAAkB,EAClB,aAAa,EACb,iBAAiB,EACjB,KAAK,EACL,oBAA4B,GAC7B,EAAE,2BAA2B,GAAG,OAAO,CAoJvC"}
|
package/dist/adapter.js
CHANGED
|
@@ -3,14 +3,29 @@ import { generateClientTokenFromReadWriteToken } from '@vercel/blob/client';
|
|
|
3
3
|
import { Forbidden } from 'payload';
|
|
4
4
|
import { assertClientUploadAllowed, createClientUploadReceipt } from 'payload/internal';
|
|
5
5
|
import { authorizeClientOverwrite } from './authorizeFileOverwrite.js';
|
|
6
|
+
import { copyVercelBlobFile } from './copyFile.js';
|
|
6
7
|
import { deleteFile } from './deleteFile.js';
|
|
7
8
|
import { generateURL } from './generateURL.js';
|
|
8
9
|
import { getFile } from './getFile.js';
|
|
9
10
|
import { uploadFile } from './uploadFile.js';
|
|
10
11
|
export function createVercelBlobAdapter({ access, addRandomSuffix, baseUrl, cacheControlMaxAge, clientUploads, collectionSources, token, useCompositePrefixes = false }) {
|
|
11
12
|
const clientUploadsAccess = typeof clientUploads === 'object' ? clientUploads.access : undefined;
|
|
13
|
+
const deleteStoredFile = ({ storageFilePath })=>deleteFile({
|
|
14
|
+
baseUrl,
|
|
15
|
+
storageFilePath,
|
|
16
|
+
token
|
|
17
|
+
});
|
|
12
18
|
return ({ collection, prefix = '' })=>({
|
|
13
19
|
name: 'vercel-blob',
|
|
20
|
+
copyFile: ({ from, req, to })=>copyVercelBlobFile({
|
|
21
|
+
access,
|
|
22
|
+
cacheControlMaxAge,
|
|
23
|
+
from,
|
|
24
|
+
req,
|
|
25
|
+
to,
|
|
26
|
+
token
|
|
27
|
+
}),
|
|
28
|
+
deleteFile: deleteStoredFile,
|
|
14
29
|
uploadInstructions: {
|
|
15
30
|
adminHandler: {
|
|
16
31
|
path: '@payloadcms/storage-vercel-blob/client#VercelBlobClientUploadHandler'
|
|
@@ -34,7 +49,7 @@ export function createVercelBlobAdapter({ access, addRandomSuffix, baseUrl, cach
|
|
|
34
49
|
filename,
|
|
35
50
|
useCompositePrefixes
|
|
36
51
|
});
|
|
37
|
-
const allowOverwrite = await authorizeClientOverwrite({
|
|
52
|
+
const allowOverwrite = collection.versions ? false : await authorizeClientOverwrite({
|
|
38
53
|
collectionPrefix: prefix,
|
|
39
54
|
collectionSources,
|
|
40
55
|
overrideAccess,
|
|
@@ -101,11 +116,7 @@ export function createVercelBlobAdapter({ access, addRandomSuffix, baseUrl, cach
|
|
|
101
116
|
prefix: urlPrefix,
|
|
102
117
|
useCompositePrefixes
|
|
103
118
|
}),
|
|
104
|
-
handleDelete:
|
|
105
|
-
baseUrl,
|
|
106
|
-
storageFilePath,
|
|
107
|
-
token
|
|
108
|
-
}),
|
|
119
|
+
handleDelete: deleteStoredFile,
|
|
109
120
|
handleUpload: async ({ data, file: { buffer, mimeType }, storageFilePath })=>{
|
|
110
121
|
const result = await uploadFile({
|
|
111
122
|
access,
|
package/dist/adapter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/adapter.ts"],"sourcesContent":["import type {\n Adapter,\n ClientUploadsConfig,\n GeneratedAdapter,\n} from '@payloadcms/plugin-cloud-storage/types'\n\nimport {\n buildUploadStoragePathData,\n resolveSignedURLKey,\n} from '@payloadcms/plugin-cloud-storage/utilities'\nimport { generateClientTokenFromReadWriteToken } from '@vercel/blob/client'\nimport { Forbidden } from 'payload'\nimport { assertClientUploadAllowed, createClientUploadReceipt } from 'payload/internal'\n\nimport type { VercelBlobCollectionSource } from './authorizeFileOverwrite.js'\n\nimport { authorizeClientOverwrite } from './authorizeFileOverwrite.js'\nimport { deleteFile } from './deleteFile.js'\nimport { generateURL } from './generateURL.js'\nimport { getFile } from './getFile.js'\nimport { uploadFile } from './uploadFile.js'\n\ninterface CreateVercelBlobAdapterArgs {\n access: 'public'\n addRandomSuffix?: boolean\n baseUrl: string\n cacheControlMaxAge: number\n clientUploads?: ClientUploadsConfig\n collectionSources: VercelBlobCollectionSource[]\n token: string\n useCompositePrefixes?: boolean\n}\n\nexport function createVercelBlobAdapter({\n access,\n addRandomSuffix,\n baseUrl,\n cacheControlMaxAge,\n clientUploads,\n collectionSources,\n token,\n useCompositePrefixes = false,\n}: CreateVercelBlobAdapterArgs): Adapter {\n const clientUploadsAccess = typeof clientUploads === 'object' ? clientUploads.access : undefined\n\n return ({ collection, prefix = '' }): GeneratedAdapter => ({\n name: 'vercel-blob',\n\n uploadInstructions: {\n adminHandler: {\n path: '@payloadcms/storage-vercel-blob/client#VercelBlobClientUploadHandler',\n },\n enabled: Boolean(clientUploads),\n generate: async ({\n collectionSlug,\n docPrefix,\n filename,\n filesize,\n mimeType,\n overrideAccess,\n req,\n }) => {\n if (\n !overrideAccess &&\n (clientUploadsAccess ? !(await clientUploadsAccess({ collectionSlug, req })) : !req.user)\n ) {\n throw new Forbidden(req.t)\n }\n\n assertClientUploadAllowed({ collection, filename, mimeType })\n\n const requested = buildUploadStoragePathData({\n collectionPrefix: prefix,\n docPrefix,\n filename,\n useCompositePrefixes,\n })\n const allowOverwrite = await authorizeClientOverwrite({\n
|
|
1
|
+
{"version":3,"sources":["../src/adapter.ts"],"sourcesContent":["import type {\n Adapter,\n ClientUploadsConfig,\n DeleteFile,\n GeneratedAdapter,\n} from '@payloadcms/plugin-cloud-storage/types'\n\nimport {\n buildUploadStoragePathData,\n resolveSignedURLKey,\n} from '@payloadcms/plugin-cloud-storage/utilities'\nimport { generateClientTokenFromReadWriteToken } from '@vercel/blob/client'\nimport { Forbidden } from 'payload'\nimport { assertClientUploadAllowed, createClientUploadReceipt } from 'payload/internal'\n\nimport type { VercelBlobCollectionSource } from './authorizeFileOverwrite.js'\n\nimport { authorizeClientOverwrite } from './authorizeFileOverwrite.js'\nimport { copyVercelBlobFile } from './copyFile.js'\nimport { deleteFile } from './deleteFile.js'\nimport { generateURL } from './generateURL.js'\nimport { getFile } from './getFile.js'\nimport { uploadFile } from './uploadFile.js'\n\ninterface CreateVercelBlobAdapterArgs {\n access: 'public'\n addRandomSuffix?: boolean\n baseUrl: string\n cacheControlMaxAge: number\n clientUploads?: ClientUploadsConfig\n collectionSources: VercelBlobCollectionSource[]\n token: string\n useCompositePrefixes?: boolean\n}\n\nexport function createVercelBlobAdapter({\n access,\n addRandomSuffix,\n baseUrl,\n cacheControlMaxAge,\n clientUploads,\n collectionSources,\n token,\n useCompositePrefixes = false,\n}: CreateVercelBlobAdapterArgs): Adapter {\n const clientUploadsAccess = typeof clientUploads === 'object' ? clientUploads.access : undefined\n const deleteStoredFile: DeleteFile = ({ storageFilePath }) =>\n deleteFile({ baseUrl, storageFilePath, token })\n\n return ({ collection, prefix = '' }): GeneratedAdapter => ({\n name: 'vercel-blob',\n\n copyFile: ({ from, req, to }) =>\n copyVercelBlobFile({ access, cacheControlMaxAge, from, req, to, token }),\n deleteFile: deleteStoredFile,\n\n uploadInstructions: {\n adminHandler: {\n path: '@payloadcms/storage-vercel-blob/client#VercelBlobClientUploadHandler',\n },\n enabled: Boolean(clientUploads),\n generate: async ({\n collectionSlug,\n docPrefix,\n filename,\n filesize,\n mimeType,\n overrideAccess,\n req,\n }) => {\n if (\n !overrideAccess &&\n (clientUploadsAccess ? !(await clientUploadsAccess({ collectionSlug, req })) : !req.user)\n ) {\n throw new Forbidden(req.t)\n }\n\n assertClientUploadAllowed({ collection, filename, mimeType })\n\n const requested = buildUploadStoragePathData({\n collectionPrefix: prefix,\n docPrefix,\n filename,\n useCompositePrefixes,\n })\n const allowOverwrite = collection.versions\n ? false\n : await authorizeClientOverwrite({\n collectionPrefix: prefix,\n collectionSources,\n overrideAccess,\n req,\n requestedCollectionSlug: collectionSlug,\n requestedFilename: requested.sanitizedFilename,\n requestedStorageFilePath: requested.storageFilePath,\n })\n const resolved = allowOverwrite\n ? {\n ...requested,\n uploadReference: {\n prefix: requested.sanitizedDocPrefix,\n signedReceipt: createClientUploadReceipt({\n allowOverwrite: true,\n collectionSlug,\n filename: requested.sanitizedFilename,\n filePrefix: requested.sanitizedDocPrefix,\n req,\n storageFilePath: requested.storageFilePath,\n }),\n },\n }\n : await resolveSignedURLKey({\n collectionPrefix: prefix,\n collectionSlug,\n docPrefix,\n filename,\n req,\n useCompositePrefixes,\n })\n\n return {\n name: 'uploadToVercelBlob',\n type: 'dispatch',\n data: {\n pathname: resolved.storageFilePath,\n token: await generateClientTokenFromReadWriteToken({\n addRandomSuffix: false,\n allowedContentTypes: mimeType ? [mimeType] : undefined,\n ...(allowOverwrite && { allowOverwrite: true }),\n cacheControlMaxAge,\n maximumSizeInBytes: filesize,\n pathname: resolved.storageFilePath,\n token,\n }),\n },\n file: {\n filename: resolved.sanitizedFilename,\n mimeType,\n size: filesize,\n uploadReference: resolved.uploadReference,\n },\n }\n },\n requiresUploadReceipt: true,\n useInAdmin: true,\n },\n\n generateURL: ({ filename, prefix: urlPrefix = '' }) =>\n generateURL({\n baseUrl,\n collectionPrefix: prefix,\n filename,\n prefix: urlPrefix,\n useCompositePrefixes,\n }),\n\n handleDelete: deleteStoredFile,\n\n handleUpload: async ({ data, file: { buffer, mimeType }, storageFilePath }) => {\n const result = await uploadFile({\n access,\n addRandomSuffix,\n buffer,\n cacheControlMaxAge,\n mimeType,\n storageFilePath,\n token,\n })\n\n if (result.filename) {\n data.filename = result.filename\n }\n\n return data\n },\n\n staticHandler: (req, { doc, headers, params: { filename, operation, uploadReference } }) =>\n getFile({\n baseUrl,\n cacheControlMaxAge,\n collection,\n collectionPrefix: prefix,\n doc,\n filename,\n incomingHeaders: headers,\n operation,\n req,\n token,\n uploadReference,\n useCompositePrefixes,\n }),\n })\n}\n"],"names":["buildUploadStoragePathData","resolveSignedURLKey","generateClientTokenFromReadWriteToken","Forbidden","assertClientUploadAllowed","createClientUploadReceipt","authorizeClientOverwrite","copyVercelBlobFile","deleteFile","generateURL","getFile","uploadFile","createVercelBlobAdapter","access","addRandomSuffix","baseUrl","cacheControlMaxAge","clientUploads","collectionSources","token","useCompositePrefixes","clientUploadsAccess","undefined","deleteStoredFile","storageFilePath","collection","prefix","name","copyFile","from","req","to","uploadInstructions","adminHandler","path","enabled","Boolean","generate","collectionSlug","docPrefix","filename","filesize","mimeType","overrideAccess","user","t","requested","collectionPrefix","allowOverwrite","versions","requestedCollectionSlug","requestedFilename","sanitizedFilename","requestedStorageFilePath","resolved","uploadReference","sanitizedDocPrefix","signedReceipt","filePrefix","type","data","pathname","allowedContentTypes","maximumSizeInBytes","file","size","requiresUploadReceipt","useInAdmin","urlPrefix","handleDelete","handleUpload","buffer","result","staticHandler","doc","headers","params","operation","incomingHeaders"],"mappings":"AAOA,SACEA,0BAA0B,EAC1BC,mBAAmB,QACd,6CAA4C;AACnD,SAASC,qCAAqC,QAAQ,sBAAqB;AAC3E,SAASC,SAAS,QAAQ,UAAS;AACnC,SAASC,yBAAyB,EAAEC,yBAAyB,QAAQ,mBAAkB;AAIvF,SAASC,wBAAwB,QAAQ,8BAA6B;AACtE,SAASC,kBAAkB,QAAQ,gBAAe;AAClD,SAASC,UAAU,QAAQ,kBAAiB;AAC5C,SAASC,WAAW,QAAQ,mBAAkB;AAC9C,SAASC,OAAO,QAAQ,eAAc;AACtC,SAASC,UAAU,QAAQ,kBAAiB;AAa5C,OAAO,SAASC,wBAAwB,EACtCC,MAAM,EACNC,eAAe,EACfC,OAAO,EACPC,kBAAkB,EAClBC,aAAa,EACbC,iBAAiB,EACjBC,KAAK,EACLC,uBAAuB,KAAK,EACA;IAC5B,MAAMC,sBAAsB,OAAOJ,kBAAkB,WAAWA,cAAcJ,MAAM,GAAGS;IACvF,MAAMC,mBAA+B,CAAC,EAAEC,eAAe,EAAE,GACvDhB,WAAW;YAAEO;YAASS;YAAiBL;QAAM;IAE/C,OAAO,CAAC,EAAEM,UAAU,EAAEC,SAAS,EAAE,EAAE,GAAwB,CAAA;YACzDC,MAAM;YAENC,UAAU,CAAC,EAAEC,IAAI,EAAEC,GAAG,EAAEC,EAAE,EAAE,GAC1BxB,mBAAmB;oBAAEM;oBAAQG;oBAAoBa;oBAAMC;oBAAKC;oBAAIZ;gBAAM;YACxEX,YAAYe;YAEZS,oBAAoB;gBAClBC,cAAc;oBACZC,MAAM;gBACR;gBACAC,SAASC,QAAQnB;gBACjBoB,UAAU,OAAO,EACfC,cAAc,EACdC,SAAS,EACTC,QAAQ,EACRC,QAAQ,EACRC,QAAQ,EACRC,cAAc,EACdb,GAAG,EACJ;oBACC,IACE,CAACa,kBACAtB,CAAA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xB;4BACNc,iBAAiBD,SAASC,eAAe;wBAC3C;oBACF;gBACF;gBACAW,uBAAuB;gBACvBC,YAAY;YACd;YAEA1D,aAAa,CAAC,EAAE+B,QAAQ,EAAEd,QAAQ0C,YAAY,EAAE,EAAE,GAChD3D,YAAY;oBACVM;oBACAgC,kBAAkBrB;oBAClBc;oBACAd,QAAQ0C;oBACRhD;gBACF;YAEFiD,cAAc9C;YAEd+C,cAAc,OAAO,EAAEV,IAAI,EAAEI,MAAM,EAAEO,MAAM,EAAE7B,QAAQ,EAAE,EAAElB,eAAe,EAAE;gBACxE,MAAMgD,SAAS,MAAM7D,WAAW;oBAC9BE;oBACAC;oBACAyD;oBACAvD;oBACA0B;oBACAlB;oBACAL;gBACF;gBAEA,IAAIqD,OAAOhC,QAAQ,EAAE;oBACnBoB,KAAKpB,QAAQ,GAAGgC,OAAOhC,QAAQ;gBACjC;gBAEA,OAAOoB;YACT;YAEAa,eAAe,CAAC3C,KAAK,EAAE4C,GAAG,EAAEC,OAAO,EAAEC,QAAQ,EAAEpC,QAAQ,EAAEqC,SAAS,EAAEtB,eAAe,EAAE,EAAE,GACrF7C,QAAQ;oBACNK;oBACAC;oBACAS;oBACAsB,kBAAkBrB;oBAClBgD;oBACAlC;oBACAsC,iBAAiBH;oBACjBE;oBACA/C;oBACAX;oBACAoC;oBACAnC;gBACF;QACJ,CAAA;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"VercelBlobClientUploadHandler.d.ts","sourceRoot":"","sources":["../../src/client/VercelBlobClientUploadHandler.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"VercelBlobClientUploadHandler.d.ts","sourceRoot":"","sources":["../../src/client/VercelBlobClientUploadHandler.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,6BAA6B;;;;;;iCAexC,CAAA"}
|
|
@@ -1,23 +1,17 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import { createClientUploadHandler } from '@payloadcms/plugin-cloud-storage/client';
|
|
3
3
|
import { put } from '@vercel/blob/client';
|
|
4
|
-
/** Last path segment only (POSIX), for keys like `folder/sub/file.png`. */ function posixBasename(key) {
|
|
5
|
-
const normalized = key.replace(/^\/+/, '');
|
|
6
|
-
const lastSlash = normalized.lastIndexOf('/');
|
|
7
|
-
return lastSlash === -1 ? normalized : normalized.slice(lastSlash + 1);
|
|
8
|
-
}
|
|
9
4
|
export const VercelBlobClientUploadHandler = createClientUploadHandler({
|
|
10
5
|
name: 'uploadToVercelBlob',
|
|
11
|
-
handler: async ({ data, file
|
|
6
|
+
handler: async ({ data, file })=>{
|
|
12
7
|
const { pathname, token } = data;
|
|
13
8
|
const result = await put(pathname, file, {
|
|
14
9
|
access: 'public',
|
|
15
10
|
contentType: file.type,
|
|
16
11
|
token
|
|
17
12
|
});
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
updateFilename(filename);
|
|
13
|
+
if (decodeURIComponent(result.pathname) !== decodeURIComponent(pathname)) {
|
|
14
|
+
throw new Error('The uploaded file was stored at an unexpected location');
|
|
21
15
|
}
|
|
22
16
|
}
|
|
23
17
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/client/VercelBlobClientUploadHandler.ts"],"sourcesContent":["'use client'\nimport { createClientUploadHandler } from '@payloadcms/plugin-cloud-storage/client'\nimport { put } from '@vercel/blob/client'\n\
|
|
1
|
+
{"version":3,"sources":["../../src/client/VercelBlobClientUploadHandler.ts"],"sourcesContent":["'use client'\nimport { createClientUploadHandler } from '@payloadcms/plugin-cloud-storage/client'\nimport { put } from '@vercel/blob/client'\n\nexport const VercelBlobClientUploadHandler = createClientUploadHandler({\n name: 'uploadToVercelBlob',\n handler: async ({ data, file }) => {\n const { pathname, token } = data as { pathname: string; token: string }\n\n const result = await put(pathname, file, {\n access: 'public',\n contentType: file.type,\n token,\n })\n\n if (decodeURIComponent(result.pathname) !== decodeURIComponent(pathname)) {\n throw new Error('The uploaded file was stored at an unexpected location')\n }\n },\n})\n"],"names":["createClientUploadHandler","put","VercelBlobClientUploadHandler","name","handler","data","file","pathname","token","result","access","contentType","type","decodeURIComponent","Error"],"mappings":"AAAA;AACA,SAASA,yBAAyB,QAAQ,0CAAyC;AACnF,SAASC,GAAG,QAAQ,sBAAqB;AAEzC,OAAO,MAAMC,gCAAgCF,0BAA0B;IACrEG,MAAM;IACNC,SAAS,OAAO,EAAEC,IAAI,EAAEC,IAAI,EAAE;QAC5B,MAAM,EAAEC,QAAQ,EAAEC,KAAK,EAAE,GAAGH;QAE5B,MAAMI,SAAS,MAAMR,IAAIM,UAAUD,MAAM;YACvCI,QAAQ;YACRC,aAAaL,KAAKM,IAAI;YACtBJ;QACF;QAEA,IAAIK,mBAAmBJ,OAAOF,QAAQ,MAAMM,mBAAmBN,WAAW;YACxE,MAAM,IAAIO,MAAM;QAClB;IACF;AACF,GAAE"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { PayloadRequest } from 'payload';
|
|
2
|
+
type Args = {
|
|
3
|
+
access: 'public';
|
|
4
|
+
cacheControlMaxAge: number;
|
|
5
|
+
from: string;
|
|
6
|
+
req?: PayloadRequest;
|
|
7
|
+
to: string;
|
|
8
|
+
token: string;
|
|
9
|
+
};
|
|
10
|
+
export declare const copyVercelBlobFile: ({ access, cacheControlMaxAge, from, req, to, token, }: Args) => Promise<void>;
|
|
11
|
+
export {};
|
|
12
|
+
//# sourceMappingURL=copyFile.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"copyFile.d.ts","sourceRoot":"","sources":["../src/copyFile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAI7C,KAAK,IAAI,GAAG;IACV,MAAM,EAAE,QAAQ,CAAA;IAChB,kBAAkB,EAAE,MAAM,CAAA;IAC1B,IAAI,EAAE,MAAM,CAAA;IACZ,GAAG,CAAC,EAAE,cAAc,CAAA;IACpB,EAAE,EAAE,MAAM,CAAA;IACV,KAAK,EAAE,MAAM,CAAA;CACd,CAAA;AAED,eAAO,MAAM,kBAAkB,0DAO5B,IAAI,KAAG,OAAO,CAAC,IAAI,CA+ErB,CAAA"}
|
package/dist/copyFile.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { BlobNotFoundError, copy, del, head, put } from '@vercel/blob';
|
|
2
|
+
export const copyVercelBlobFile = async ({ access, cacheControlMaxAge, from, req, to, token })=>{
|
|
3
|
+
if (from === to) {
|
|
4
|
+
throw new Error('Storage copy requires different source and destination keys');
|
|
5
|
+
}
|
|
6
|
+
const source = await head(from, {
|
|
7
|
+
token
|
|
8
|
+
});
|
|
9
|
+
try {
|
|
10
|
+
await head(to, {
|
|
11
|
+
token
|
|
12
|
+
});
|
|
13
|
+
throw new Error(`Storage destination already exists: ${to}`);
|
|
14
|
+
} catch (err) {
|
|
15
|
+
if (!isBlobNotFound(err)) {
|
|
16
|
+
throw err;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const sourceMaxAge = /(?:^|,)\s*max-age=(\d+)/.exec(source.cacheControl);
|
|
20
|
+
let copied = await copy(from, to, {
|
|
21
|
+
access,
|
|
22
|
+
addRandomSuffix: false,
|
|
23
|
+
allowOverwrite: false,
|
|
24
|
+
cacheControlMaxAge: sourceMaxAge ? Number(sourceMaxAge[1]) : cacheControlMaxAge,
|
|
25
|
+
contentType: source.contentType,
|
|
26
|
+
token
|
|
27
|
+
});
|
|
28
|
+
try {
|
|
29
|
+
if (copied.pathname !== to) {
|
|
30
|
+
throw new Error(`Vercel Blob assigned an unexpected destination key: ${copied.pathname}`);
|
|
31
|
+
}
|
|
32
|
+
let destination = await head(to, {
|
|
33
|
+
token
|
|
34
|
+
});
|
|
35
|
+
if (destination.size !== source.size) {
|
|
36
|
+
await del(to, {
|
|
37
|
+
ifMatch: copied.etag,
|
|
38
|
+
token
|
|
39
|
+
});
|
|
40
|
+
// The first copy is gone; a rejected fallback must not delete another upload's object.
|
|
41
|
+
copied = undefined;
|
|
42
|
+
const sourceResponse = await fetch(source.url);
|
|
43
|
+
if (!sourceResponse.ok || !sourceResponse.body) {
|
|
44
|
+
throw new Error(`Vercel Blob source is not readable: ${from}`);
|
|
45
|
+
}
|
|
46
|
+
copied = await put(to, sourceResponse.body, {
|
|
47
|
+
access,
|
|
48
|
+
addRandomSuffix: false,
|
|
49
|
+
allowOverwrite: false,
|
|
50
|
+
cacheControlMaxAge: sourceMaxAge ? Number(sourceMaxAge[1]) : cacheControlMaxAge,
|
|
51
|
+
contentType: source.contentType,
|
|
52
|
+
token
|
|
53
|
+
});
|
|
54
|
+
if (copied.pathname !== to) {
|
|
55
|
+
throw new Error(`Vercel Blob assigned an unexpected destination key: ${copied.pathname}`);
|
|
56
|
+
}
|
|
57
|
+
destination = await head(to, {
|
|
58
|
+
token
|
|
59
|
+
});
|
|
60
|
+
if (destination.size !== source.size) {
|
|
61
|
+
throw new Error(`Copied Vercel Blob object is not readable at its expected length: ${to}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
} catch (err) {
|
|
65
|
+
if (copied) {
|
|
66
|
+
try {
|
|
67
|
+
if (copied.pathname === from || !copied.etag) {
|
|
68
|
+
throw new Error('Vercel Blob copy did not return a safe destination for cleanup');
|
|
69
|
+
}
|
|
70
|
+
await del(copied.pathname, {
|
|
71
|
+
ifMatch: copied.etag,
|
|
72
|
+
token
|
|
73
|
+
});
|
|
74
|
+
} catch (cleanupError) {
|
|
75
|
+
req?.payload.logger.error({
|
|
76
|
+
err: cleanupError,
|
|
77
|
+
msg: `Failed to remove unsuccessful Vercel Blob copy at ${copied.pathname}`
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
throw err;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
const isBlobNotFound = (err)=>err instanceof BlobNotFoundError || err !== null && typeof err === 'object' && 'name' in err && err.name === 'BlobNotFoundError';
|
|
85
|
+
|
|
86
|
+
//# sourceMappingURL=copyFile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/copyFile.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\n\nimport { BlobNotFoundError, copy, del, head, put } from '@vercel/blob'\n\ntype Args = {\n access: 'public'\n cacheControlMaxAge: number\n from: string\n req?: PayloadRequest\n to: string\n token: string\n}\n\nexport const copyVercelBlobFile = async ({\n access,\n cacheControlMaxAge,\n from,\n req,\n to,\n token,\n}: Args): Promise<void> => {\n if (from === to) {\n throw new Error('Storage copy requires different source and destination keys')\n }\n\n const source = await head(from, { token })\n\n try {\n await head(to, { token })\n throw new Error(`Storage destination already exists: ${to}`)\n } catch (err) {\n if (!isBlobNotFound(err)) {\n throw err\n }\n }\n\n const sourceMaxAge = /(?:^|,)\\s*max-age=(\\d+)/.exec(source.cacheControl)\n let copied: { etag: string; pathname: string } | undefined = await copy(from, to, {\n access,\n addRandomSuffix: false,\n allowOverwrite: false,\n cacheControlMaxAge: sourceMaxAge ? Number(sourceMaxAge[1]) : cacheControlMaxAge,\n contentType: source.contentType,\n token,\n })\n\n try {\n if (copied.pathname !== to) {\n throw new Error(`Vercel Blob assigned an unexpected destination key: ${copied.pathname}`)\n }\n\n let destination = await head(to, { token })\n\n if (destination.size !== source.size) {\n await del(to, { ifMatch: copied.etag, token })\n // The first copy is gone; a rejected fallback must not delete another upload's object.\n copied = undefined\n\n const sourceResponse = await fetch(source.url)\n\n if (!sourceResponse.ok || !sourceResponse.body) {\n throw new Error(`Vercel Blob source is not readable: ${from}`)\n }\n\n copied = await put(to, sourceResponse.body, {\n access,\n addRandomSuffix: false,\n allowOverwrite: false,\n cacheControlMaxAge: sourceMaxAge ? Number(sourceMaxAge[1]) : cacheControlMaxAge,\n contentType: source.contentType,\n token,\n })\n\n if (copied.pathname !== to) {\n throw new Error(`Vercel Blob assigned an unexpected destination key: ${copied.pathname}`)\n }\n\n destination = await head(to, { token })\n\n if (destination.size !== source.size) {\n throw new Error(`Copied Vercel Blob object is not readable at its expected length: ${to}`)\n }\n }\n } catch (err) {\n if (copied) {\n try {\n if (copied.pathname === from || !copied.etag) {\n throw new Error('Vercel Blob copy did not return a safe destination for cleanup')\n }\n await del(copied.pathname, { ifMatch: copied.etag, token })\n } catch (cleanupError) {\n req?.payload.logger.error({\n err: cleanupError,\n msg: `Failed to remove unsuccessful Vercel Blob copy at ${copied.pathname}`,\n })\n }\n }\n throw err\n }\n}\n\nconst isBlobNotFound = (err: unknown): boolean =>\n err instanceof BlobNotFoundError ||\n (err !== null && typeof err === 'object' && 'name' in err && err.name === 'BlobNotFoundError')\n"],"names":["BlobNotFoundError","copy","del","head","put","copyVercelBlobFile","access","cacheControlMaxAge","from","req","to","token","Error","source","err","isBlobNotFound","sourceMaxAge","exec","cacheControl","copied","addRandomSuffix","allowOverwrite","Number","contentType","pathname","destination","size","ifMatch","etag","undefined","sourceResponse","fetch","url","ok","body","cleanupError","payload","logger","error","msg","name"],"mappings":"AAEA,SAASA,iBAAiB,EAAEC,IAAI,EAAEC,GAAG,EAAEC,IAAI,EAAEC,GAAG,QAAQ,eAAc;AAWtE,OAAO,MAAMC,qBAAqB,OAAO,EACvCC,MAAM,EACNC,kBAAkB,EAClBC,IAAI,EACJC,GAAG,EACHC,EAAE,EACFC,KAAK,EACA;IACL,IAAIH,SAASE,IAAI;QACf,MAAM,IAAIE,MAAM;IAClB;IAEA,MAAMC,SAAS,MAAMV,KAAKK,MAAM;QAAEG;IAAM;IAExC,IAAI;QACF,MAAMR,KAAKO,IAAI;YAAEC;QAAM;QACvB,MAAM,IAAIC,MAAM,CAAC,oCAAoC,EAAEF,IAAI;IAC7D,EAAE,OAAOI,KAAK;QACZ,IAAI,CAACC,eAAeD,MAAM;YACxB,MAAMA;QACR;IACF;IAEA,MAAME,eAAe,0BAA0BC,IAAI,CAACJ,OAAOK,YAAY;IACvE,IAAIC,SAAyD,MAAMlB,KAAKO,MAAME,IAAI;QAChFJ;QACAc,iBAAiB;QAC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IAAI,EAAE;oBAC5C,MAAM,IAAIhB,MAAM;gBAClB;gBACA,MAAMV,IAAIiB,OAAOK,QAAQ,EAAE;oBAAEG,SAASR,OAAOS,IAAI;oBAAEjB;gBAAM;YAC3D,EAAE,OAAOwB,cAAc;gBACrB1B,KAAK2B,QAAQC,OAAOC,MAAM;oBACxBxB,KAAKqB;oBACLI,KAAK,CAAC,kDAAkD,EAAEpB,OAAOK,QAAQ,EAAE;gBAC7E;YACF;QACF;QACA,MAAMV;IACR;AACF,EAAC;AAED,MAAMC,iBAAiB,CAACD,MACtBA,eAAed,qBACdc,QAAQ,QAAQ,OAAOA,QAAQ,YAAY,UAAUA,OAAOA,IAAI0B,IAAI,KAAK"}
|
package/dist/index.d.ts
CHANGED
|
@@ -9,7 +9,8 @@ export type VercelBlobStorageOptions = {
|
|
|
9
9
|
*/
|
|
10
10
|
access?: 'public';
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
12
|
+
* Must be false or omitted for enabled adapters with configured collections.
|
|
13
|
+
* Payload requires exact storage paths and allocates unique object folders.
|
|
13
14
|
*
|
|
14
15
|
* @default false
|
|
15
16
|
*/
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EAEnB,iBAAiB,EAClB,MAAM,wCAAwC,CAAA;AAC/C,OAAO,KAAK,EAAU,cAAc,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAW3E,MAAM,MAAM,wBAAwB,GAAG;IACrC;;;;;OAKG;IACH,MAAM,CAAC,EAAE,QAAQ,CAAA;IAEjB
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EAEnB,iBAAiB,EAClB,MAAM,wCAAwC,CAAA;AAC/C,OAAO,KAAK,EAAU,cAAc,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAW3E,MAAM,MAAM,wBAAwB,GAAG;IACrC;;;;;OAKG;IACH,MAAM,CAAC,EAAE,QAAQ,CAAA;IAEjB;;;;;OAKG;IACH,eAAe,CAAC,EAAE,OAAO,CAAA;IAEzB;;;;OAIG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAA;IAE3B;;OAEG;IACH,aAAa,CAAC,EAAE,mBAAmB,CAAA;IAEnC;;OAEG;IACH,WAAW,EAAE,OAAO,CAAC,MAAM,CAAC,oBAAoB,EAAE,IAAI,CAAC,iBAAiB,EAAE,SAAS,CAAC,GAAG,IAAI,CAAC,CAAC,CAAA;IAE7F;;;;OAIG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB;;;;;;OAMG;IACH,KAAK,EAAE,MAAM,GAAG,SAAS,CAAA;IAEzB;;;;;;;;;;;;;OAaG;IACH,oBAAoB,CAAC,EAAE,OAAO,CAAA;CAC/B,CAAA;AASD,KAAK,wBAAwB,GAAG,CAAC,qBAAqB,EAAE,wBAAwB,KAAK,cAAc,CAAA;AAEnG,eAAO,MAAM,iBAAiB,EAAE,wBA8G/B,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -39,6 +39,9 @@ export const vercelBlobStorage = (options)=>{
|
|
|
39
39
|
useCompositePrefixes: options.useCompositePrefixes
|
|
40
40
|
})(incomingConfig);
|
|
41
41
|
}
|
|
42
|
+
if (options.addRandomSuffix && storageAdapter.collections.length > 0) {
|
|
43
|
+
throw new Error('Vercel Blob addRandomSuffix must be false. Payload requires exact storage paths and already allocates unique object folders.');
|
|
44
|
+
}
|
|
42
45
|
const adapter = createVercelBlobAdapter({
|
|
43
46
|
access: optionsWithDefaults.access ?? 'public',
|
|
44
47
|
addRandomSuffix: optionsWithDefaults.addRandomSuffix,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type {\n ClientUploadsConfig,\n PluginOptions as CloudStoragePluginOptions,\n CollectionOptions,\n} from '@payloadcms/plugin-cloud-storage/types'\nimport type { Config, StorageAdapter, UploadCollectionSlug } from 'payload'\n\nimport { cloudStoragePlugin } from '@payloadcms/plugin-cloud-storage'\n\nimport { createVercelBlobAdapter } from './adapter.js'\nimport {\n getCollectionSources,\n type InternalVercelBlobStorageAdapter,\n vercelBlobStorageMetadata,\n} from './getCollectionSources.js'\n\nexport type VercelBlobStorageOptions = {\n /**\n * Access control level. Currently, only 'public' is supported.\n * Vercel plans on adding support for private blobs in the future.\n *\n * @default 'public'\n */\n access?: 'public'\n\n /**\n *
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type {\n ClientUploadsConfig,\n PluginOptions as CloudStoragePluginOptions,\n CollectionOptions,\n} from '@payloadcms/plugin-cloud-storage/types'\nimport type { Config, StorageAdapter, UploadCollectionSlug } from 'payload'\n\nimport { cloudStoragePlugin } from '@payloadcms/plugin-cloud-storage'\n\nimport { createVercelBlobAdapter } from './adapter.js'\nimport {\n getCollectionSources,\n type InternalVercelBlobStorageAdapter,\n vercelBlobStorageMetadata,\n} from './getCollectionSources.js'\n\nexport type VercelBlobStorageOptions = {\n /**\n * Access control level. Currently, only 'public' is supported.\n * Vercel plans on adding support for private blobs in the future.\n *\n * @default 'public'\n */\n access?: 'public'\n\n /**\n * Must be false or omitted for enabled adapters with configured collections.\n * Payload requires exact storage paths and allocates unique object folders.\n *\n * @default false\n */\n addRandomSuffix?: boolean\n\n /**\n * Cache-Control max-age in seconds\n *\n * @default 365 * 24 * 60 * 60 // (1 Year)\n */\n cacheControlMaxAge?: number\n\n /**\n * Upload directly to Vercel Blob instead of through Payload.\n */\n clientUploads?: ClientUploadsConfig\n\n /**\n * Collections to apply the Vercel Blob adapter to\n */\n collections: Partial<Record<UploadCollectionSlug, Omit<CollectionOptions, 'adapter'> | true>>\n\n /**\n * Whether or not to enable the plugin\n *\n * Default: true\n */\n enabled?: boolean\n /**\n * Vercel Blob storage read/write token\n *\n * Usually process.env.BLOB_READ_WRITE_TOKEN set by Vercel\n *\n * If unset, the plugin will be disabled and will fallback to local storage\n */\n token: string | undefined\n\n /**\n * When true, the collection-level prefix and document-level prefix are combined\n * (compositional). When false (default), a document prefix already within the\n * collection prefix is used as-is for new uploads; otherwise it is nested beneath it.\n * Existing files retain their stored prefixes for reads, URLs, and cleanup.\n *\n * Example with a document prefix already contained by the collection prefix:\n * - collection prefix: `uploads/`\n * - document prefix: `uploads/documents/`\n * - resulting prefix with useCompositePrefixes=true: `uploads/uploads/documents/`\n * - resulting prefix with useCompositePrefixes=false: `uploads/documents/`\n *\n * @default false\n */\n useCompositePrefixes?: boolean\n}\n\nconst defaultUploadOptions: Partial<VercelBlobStorageOptions> = {\n access: 'public',\n addRandomSuffix: false,\n cacheControlMaxAge: 60 * 60 * 24 * 365, // 1 year\n enabled: true,\n}\n\ntype VercelBlobStorageFactory = (vercelBlobStorageOpts: VercelBlobStorageOptions) => StorageAdapter\n\nexport const vercelBlobStorage: VercelBlobStorageFactory = (\n options: VercelBlobStorageOptions,\n): StorageAdapter => {\n const storeId = options.token?.match(/^vercel_blob_rw_([a-z\\d]+)_[a-z\\d]+$/i)?.[1]?.toLowerCase()\n const isPluginDisabled = options.enabled === false || !options.token\n const storageAdapter: InternalVercelBlobStorageAdapter = {\n name: 'vercel-blob',\n collections: Object.keys(options.collections),\n init: (incomingConfig: Config): Config => {\n // Don't throw if the plugin is disabled\n if (!storeId && !isPluginDisabled) {\n throw new Error(\n 'Invalid token format for Vercel Blob adapter. Should be vercel_blob_rw_<store_id>_<random_string>.',\n )\n }\n\n const optionsWithDefaults = {\n ...defaultUploadOptions,\n ...options,\n }\n\n // support overriding the base URL for emulator https://github.com/payloadcms/vercel-blob-emulator\n const baseUrl =\n process.env.STORAGE_VERCEL_BLOB_BASE_URL ||\n `https://${storeId}.${optionsWithDefaults.access}.blob.vercel-storage.com`\n\n // If the plugin is disabled or no token is provided, do not enable the plugin\n if (isPluginDisabled) {\n const collectionsWithoutAdapter: CloudStoragePluginOptions['collections'] = Object.entries(\n options.collections,\n ).reduce(\n (acc, [slug, collOptions]) => ({\n ...acc,\n [slug]: { ...(collOptions === true ? {} : collOptions), adapter: null },\n }),\n {} as Record<string, CollectionOptions>,\n )\n return cloudStoragePlugin({\n collections: collectionsWithoutAdapter,\n enabled: false,\n useCompositePrefixes: options.useCompositePrefixes,\n })(incomingConfig)\n }\n\n if (options.addRandomSuffix && storageAdapter.collections.length > 0) {\n throw new Error(\n 'Vercel Blob addRandomSuffix must be false. Payload requires exact storage paths and already allocates unique object folders.',\n )\n }\n\n const adapter = createVercelBlobAdapter({\n access: optionsWithDefaults.access ?? 'public',\n addRandomSuffix: optionsWithDefaults.addRandomSuffix,\n baseUrl,\n cacheControlMaxAge: optionsWithDefaults.cacheControlMaxAge ?? 60 * 60 * 24 * 365,\n clientUploads: optionsWithDefaults.clientUploads,\n collectionSources: getCollectionSources({\n currentAdapter: storageAdapter,\n storageAdapters: incomingConfig.storage,\n }),\n token: options.token!,\n useCompositePrefixes: options.useCompositePrefixes,\n })\n\n // Add adapter to each collection option object\n const collectionsWithAdapter: CloudStoragePluginOptions['collections'] = Object.entries(\n options.collections,\n ).reduce(\n (acc, [slug, collOptions]) => ({\n ...acc,\n [slug]: {\n ...(collOptions === true ? {} : collOptions),\n adapter,\n },\n }),\n {} as Record<string, CollectionOptions>,\n )\n\n // Set disableLocalStorage: true for collections specified in the plugin options\n const config = {\n ...incomingConfig,\n collections: (incomingConfig.collections || []).map((collection) => {\n if (!collectionsWithAdapter[collection.slug]) {\n return collection\n }\n\n return {\n ...collection,\n upload: {\n ...(typeof collection.upload === 'object' ? collection.upload : {}),\n disableLocalStorage: true,\n },\n }\n }),\n }\n\n return cloudStoragePlugin({\n collections: collectionsWithAdapter,\n useCompositePrefixes: options.useCompositePrefixes,\n })(config)\n },\n [vercelBlobStorageMetadata]: {\n collections: options.collections,\n enabled: !isPluginDisabled,\n storeId,\n useCompositePrefixes: Boolean(options.useCompositePrefixes),\n },\n }\n\n return storageAdapter\n}\n"],"names":["cloudStoragePlugin","createVercelBlobAdapter","getCollectionSources","vercelBlobStorageMetadata","defaultUploadOptions","access","addRandomSuffix","cacheControlMaxAge","enabled","vercelBlobStorage","options","storeId","token","match","toLowerCase","isPluginDisabled","storageAdapter","name","collections","Object","keys","init","incomingConfig","Error","optionsWithDefaults","baseUrl","process","env","STORAGE_VERCEL_BLOB_BASE_URL","collectionsWithoutAdapter","entries","reduce","acc","slug","collOptions","adapter","useCompositePrefixes","length","clientUploads","collectionSources","currentAdapter","storageAdapters","storage","collectionsWithAdapter","config","map","collection","upload","disableLocalStorage","Boolean"],"mappings":"AAOA,SAASA,kBAAkB,QAAQ,mCAAkC;AAErE,SAASC,uBAAuB,QAAQ,eAAc;AACtD,SACEC,oBAAoB,EAEpBC,yBAAyB,QACpB,4BAA2B;AAoElC,MAAMC,uBAA0D;IAC9DC,QAAQ;IACRC,iBAAiB;IACjBC,oBAAoB,KAAK,KAAK,KAAK;IACnCC,SAAS;AACX;AAIA,OAAO,MAAMC,oBAA8C,CACzDC;IAEA,MAAMC,UAAUD,QAAQE,KAAK,EAAEC,MAAM,0CAA0C,CAAC,EAAE,EAAEC;IACpF,MAAMC,mBAAmBL,QAAQF,OAAO,KAAK,SAAS,CAACE,QAAQE,KAAK;IACpE,MAAMI,iBAAmD;QACvDC,MAAM;QACNC,aAAaC,OAAOC,IAAI,CAACV,QAAQQ,WAAW;QAC5CG,MAAM,CAACC;YACL,wCAAwC;YACxC,IAAI,CAACX,WAAW,CAACI,kBAAkB;gBACjC,MAAM,IAAIQ,MACR;YAEJ;YAEA,MAAMC,sBAAsB;gBAC1B,GAAGpB,oBAAoB;gBACvB,GAAGM,OAAO;YACZ;YAEA,kGAAkG;YAClG,MAAMe,UACJC,QAAQC,GAAG,CAACC,4BAA4B,IACxC,CAAC,QAAQ,EAAEjB,QAAQ,CAAC,EAAEa,oBAAoBnB,M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AAc;gBACjBJ,aAAa,AAACI,CAAAA,eAAeJ,WAAW,IAAI,EAAE,AAAD,EAAG2B,GAAG,CAAC,CAACC;oBACnD,IAAI,CAACH,sBAAsB,CAACG,WAAWb,IAAI,CAAC,EAAE;wBAC5C,OAAOa;oBACT;oBAEA,OAAO;wBACL,GAAGA,UAAU;wBACbC,QAAQ;4BACN,GAAI,OAAOD,WAAWC,MAAM,KAAK,WAAWD,WAAWC,MAAM,GAAG,CAAC,CAAC;4BAClEC,qBAAqB;wBACvB;oBACF;gBACF;YACF;YAEA,OAAOhD,mBAAmB;gBACxBkB,aAAayB;gBACbP,sBAAsB1B,QAAQ0B,oBAAoB;YACpD,GAAGQ;QACL;QACA,CAACzC,0BAA0B,EAAE;YAC3Be,aAAaR,QAAQQ,WAAW;YAChCV,SAAS,CAACO;YACVJ;YACAyB,sBAAsBa,QAAQvC,QAAQ0B,oBAAoB;QAC5D;IACF;IAEA,OAAOpB;AACT,EAAC"}
|
package/package.json
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/storage-vercel-blob",
|
|
3
|
-
"version": "4.0.0-internal.
|
|
3
|
+
"version": "4.0.0-internal.f2a04f7",
|
|
4
4
|
"description": "Payload storage adapter for Vercel Blob Storage",
|
|
5
5
|
"homepage": "https://payloadcms.com",
|
|
6
|
-
"repository": {
|
|
7
|
-
"type": "git",
|
|
8
|
-
"url": "https://github.com/payloadcms/payload.git",
|
|
9
|
-
"directory": "packages/storage-vercel-blob"
|
|
10
|
-
},
|
|
11
6
|
"license": "MIT",
|
|
12
7
|
"author": "Payload <dev@payloadcms.com> (https://payloadcms.com)",
|
|
13
8
|
"maintainers": [
|
|
@@ -17,8 +12,18 @@
|
|
|
17
12
|
"url": "https://payloadcms.com"
|
|
18
13
|
}
|
|
19
14
|
],
|
|
20
|
-
"
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/payloadcms/payload.git",
|
|
18
|
+
"directory": "packages/storage-vercel-blob"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
21
23
|
"type": "module",
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"main": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
22
27
|
"exports": {
|
|
23
28
|
".": {
|
|
24
29
|
"import": "./dist/index.js",
|
|
@@ -31,20 +36,15 @@
|
|
|
31
36
|
"default": "./dist/exports/client.js"
|
|
32
37
|
}
|
|
33
38
|
},
|
|
34
|
-
"main": "./dist/index.js",
|
|
35
|
-
"types": "./dist/index.d.ts",
|
|
36
|
-
"files": [
|
|
37
|
-
"dist"
|
|
38
|
-
],
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"@vercel/blob": "2.3.1",
|
|
41
|
-
"@payloadcms/plugin-cloud-storage": "4.0.0-internal.
|
|
41
|
+
"@payloadcms/plugin-cloud-storage": "4.0.0-internal.f2a04f7"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"payload": "4.0.0-internal.
|
|
44
|
+
"payload": "4.0.0-internal.f2a04f7"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
|
-
"payload": "4.0.0-internal.
|
|
47
|
+
"payload": "4.0.0-internal.f2a04f7"
|
|
48
48
|
},
|
|
49
49
|
"engines": {
|
|
50
50
|
"node": ">=24.15.0"
|