@nextlyhq/storage-vercel-blob 0.0.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.
- package/LICENSE +22 -0
- package/README.md +90 -0
- package/dist/index.cjs +399 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +727 -0
- package/dist/index.d.ts +727 -0
- package/dist/index.mjs +394 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +76 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
import { put, del, head, list } from '@vercel/blob';
|
|
2
|
+
|
|
3
|
+
// src/adapter.ts
|
|
4
|
+
var VercelBlobStorageAdapter = class {
|
|
5
|
+
/**
|
|
6
|
+
* Create a new Vercel Blob storage adapter.
|
|
7
|
+
*
|
|
8
|
+
* @param config - Vercel Blob storage configuration
|
|
9
|
+
* @throws Error if token is not provided
|
|
10
|
+
*/
|
|
11
|
+
constructor(config) {
|
|
12
|
+
this.config = config;
|
|
13
|
+
const token = config.token || process.env.BLOB_READ_WRITE_TOKEN || "";
|
|
14
|
+
if (!token) {
|
|
15
|
+
throw new Error(
|
|
16
|
+
"@nextly/storage-vercel-blob: token is required.\n\nEither set the BLOB_READ_WRITE_TOKEN environment variable or pass token in config.\nGet your token from: Vercel Dashboard > Storage > Blob > Tokens"
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
this.resolvedConfig = {
|
|
20
|
+
token,
|
|
21
|
+
addRandomSuffix: config.addRandomSuffix ?? true,
|
|
22
|
+
cacheControlMaxAge: config.cacheControlMaxAge ?? 31536e3,
|
|
23
|
+
access: config.access ?? "public",
|
|
24
|
+
storeId: config.storeId,
|
|
25
|
+
allowOverwrite: config.allowOverwrite ?? false,
|
|
26
|
+
multipartThreshold: config.multipartThreshold ?? 5 * 1024 * 1024
|
|
27
|
+
// 5MB
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
resolvedConfig;
|
|
31
|
+
// ============================================================
|
|
32
|
+
// Core IStorageAdapter Methods
|
|
33
|
+
// ============================================================
|
|
34
|
+
/**
|
|
35
|
+
* Upload file to Vercel Blob.
|
|
36
|
+
*
|
|
37
|
+
* Files are stored with globally distributed CDN backing.
|
|
38
|
+
* By default, a random suffix is added to prevent filename collisions.
|
|
39
|
+
*
|
|
40
|
+
* @param buffer - File content as Buffer
|
|
41
|
+
* @param options - Upload options (filename, mimeType, folder, collection)
|
|
42
|
+
* @returns Upload result with URL and storage path
|
|
43
|
+
*/
|
|
44
|
+
async upload(buffer, options) {
|
|
45
|
+
const mimeType = (options.contentType || options.mimeType || "").toLowerCase().trim();
|
|
46
|
+
const filename = (options.filename || "").toLowerCase();
|
|
47
|
+
const ext = filename.includes(".") ? filename.slice(filename.lastIndexOf(".") + 1) : "";
|
|
48
|
+
const isSvg = mimeType === "image/svg+xml" || ext === "svg" || ext === "svgz";
|
|
49
|
+
const isHtml = mimeType === "text/html" || mimeType === "application/xhtml+xml" || ext === "html" || ext === "htm" || ext === "xhtml";
|
|
50
|
+
if (isSvg || isHtml) {
|
|
51
|
+
const kind = isSvg ? "SVG" : "HTML";
|
|
52
|
+
throw new Error(
|
|
53
|
+
`[nextly/storage-vercel-blob] ${kind} uploads are rejected on Vercel Blob \u2014 the platform cannot serve them with attachment-disposition or a restrictive CSP, so they would be stored XSS. Use the S3 / R2 adapter for ${kind} files, or convert to a raster format (PNG/WebP).`
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
const pathname = this.buildPathname(options.filename, options.folder);
|
|
57
|
+
const result = await put(pathname, buffer, {
|
|
58
|
+
access: this.resolvedConfig.access,
|
|
59
|
+
token: this.resolvedConfig.token,
|
|
60
|
+
contentType: options.contentType || options.mimeType,
|
|
61
|
+
addRandomSuffix: this.resolvedConfig.addRandomSuffix,
|
|
62
|
+
cacheControlMaxAge: this.resolvedConfig.cacheControlMaxAge
|
|
63
|
+
});
|
|
64
|
+
return {
|
|
65
|
+
url: result.url,
|
|
66
|
+
path: result.url
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Delete file from Vercel Blob.
|
|
71
|
+
*
|
|
72
|
+
* @param filePath - Full blob URL to delete
|
|
73
|
+
*/
|
|
74
|
+
async delete(filePath) {
|
|
75
|
+
await del(filePath, {
|
|
76
|
+
token: this.resolvedConfig.token
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Delete multiple files from Vercel Blob in chunked batches.
|
|
81
|
+
*
|
|
82
|
+
* Processes deletions in chunks of 10 concurrent calls using Promise.allSettled,
|
|
83
|
+
* collecting successes and failures without short-circuiting.
|
|
84
|
+
*
|
|
85
|
+
* @param filePaths - Array of full blob URLs to delete
|
|
86
|
+
* @returns BulkDeleteResult with successful and failed arrays
|
|
87
|
+
*/
|
|
88
|
+
async bulkDelete(filePaths) {
|
|
89
|
+
const successful = [];
|
|
90
|
+
const failed = [];
|
|
91
|
+
const chunkSize = 10;
|
|
92
|
+
for (let i = 0; i < filePaths.length; i += chunkSize) {
|
|
93
|
+
const chunk = filePaths.slice(i, i + chunkSize);
|
|
94
|
+
const results = await Promise.allSettled(
|
|
95
|
+
chunk.map((fp) => del(fp, { token: this.resolvedConfig.token }))
|
|
96
|
+
);
|
|
97
|
+
results.forEach((result, idx) => {
|
|
98
|
+
const fp = chunk[idx];
|
|
99
|
+
if (result.status === "fulfilled") {
|
|
100
|
+
successful.push(fp);
|
|
101
|
+
} else {
|
|
102
|
+
failed.push({
|
|
103
|
+
filePath: fp,
|
|
104
|
+
error: result.reason instanceof Error ? result.reason.message : String(result.reason)
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
return { successful, failed };
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Check if file exists in Vercel Blob.
|
|
113
|
+
*
|
|
114
|
+
* Uses the head() function which is efficient for existence checks.
|
|
115
|
+
*
|
|
116
|
+
* @param filePath - Full blob URL to check
|
|
117
|
+
* @returns true if file exists, false otherwise
|
|
118
|
+
*/
|
|
119
|
+
async exists(filePath) {
|
|
120
|
+
try {
|
|
121
|
+
await head(filePath, {
|
|
122
|
+
token: this.resolvedConfig.token
|
|
123
|
+
});
|
|
124
|
+
return true;
|
|
125
|
+
} catch {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Get public URL for Vercel Blob file.
|
|
131
|
+
*
|
|
132
|
+
* Vercel Blob paths ARE the public URLs - they're already full HTTPS URLs
|
|
133
|
+
* with CDN distribution.
|
|
134
|
+
*
|
|
135
|
+
* @param filePath - Full blob URL
|
|
136
|
+
* @returns The same URL (Vercel Blob paths are already public URLs)
|
|
137
|
+
*/
|
|
138
|
+
getPublicUrl(filePath) {
|
|
139
|
+
return filePath;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Get storage type identifier.
|
|
143
|
+
*/
|
|
144
|
+
getType() {
|
|
145
|
+
return "vercel-blob";
|
|
146
|
+
}
|
|
147
|
+
// ============================================================
|
|
148
|
+
// Optional IStorageAdapter Methods
|
|
149
|
+
// ============================================================
|
|
150
|
+
/**
|
|
151
|
+
* Get adapter info including capabilities.
|
|
152
|
+
*
|
|
153
|
+
* @returns Adapter info with type, name, and capability flags
|
|
154
|
+
*/
|
|
155
|
+
getInfo() {
|
|
156
|
+
return {
|
|
157
|
+
type: "vercel-blob",
|
|
158
|
+
name: "VercelBlobStorageAdapter",
|
|
159
|
+
supportsSignedUrls: false,
|
|
160
|
+
// Vercel Blob URLs are public by default
|
|
161
|
+
supportsClientUploads: true
|
|
162
|
+
// Via handleUpload API
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Get file metadata from Vercel Blob.
|
|
167
|
+
*
|
|
168
|
+
* Retrieves file information including size, content type, and upload date.
|
|
169
|
+
*
|
|
170
|
+
* @param filePath - Full blob URL
|
|
171
|
+
* @returns File metadata or null if file not found
|
|
172
|
+
*/
|
|
173
|
+
async getMetadata(filePath) {
|
|
174
|
+
try {
|
|
175
|
+
const result = await head(filePath, {
|
|
176
|
+
token: this.resolvedConfig.token
|
|
177
|
+
});
|
|
178
|
+
const filename = result.pathname.split("/").pop() || result.pathname;
|
|
179
|
+
return {
|
|
180
|
+
id: filePath,
|
|
181
|
+
filename,
|
|
182
|
+
originalFilename: filename,
|
|
183
|
+
mimeType: result.contentType,
|
|
184
|
+
size: result.size,
|
|
185
|
+
url: result.url,
|
|
186
|
+
createdAt: result.uploadedAt.toISOString()
|
|
187
|
+
};
|
|
188
|
+
} catch {
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Generate pre-signed URL for client-side uploads.
|
|
194
|
+
*
|
|
195
|
+
* Vercel Blob uses a different pattern for client uploads - they require
|
|
196
|
+
* the handleUpload API which handles token generation server-side.
|
|
197
|
+
*
|
|
198
|
+
* This method is not implemented because Vercel Blob's client upload flow
|
|
199
|
+
* requires server-side route handlers with handleUpload().
|
|
200
|
+
*
|
|
201
|
+
* @see https://vercel.com/docs/storage/vercel-blob/client-upload
|
|
202
|
+
* @throws Error always - client uploads require handleUpload API
|
|
203
|
+
*/
|
|
204
|
+
getPresignedUploadUrl(_key, _mimeType, _expiresIn) {
|
|
205
|
+
return Promise.reject(
|
|
206
|
+
new Error(
|
|
207
|
+
"@nextly/storage-vercel-blob: Client uploads require the handleUpload API.\n\nVercel Blob uses a different pattern for client-side uploads that involves:\n1. A server-side route handler with handleUpload()\n2. The @vercel/blob/client upload() function\n\nSee: https://vercel.com/docs/storage/vercel-blob/client-upload\n\nFor server-side uploads, use the standard upload() method instead."
|
|
208
|
+
)
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
// ============================================================
|
|
212
|
+
// Additional Methods
|
|
213
|
+
// ============================================================
|
|
214
|
+
/**
|
|
215
|
+
* List blobs with optional prefix filter.
|
|
216
|
+
*
|
|
217
|
+
* Useful for browsing or batch operations on stored files.
|
|
218
|
+
*
|
|
219
|
+
* @param prefix - Optional prefix to filter results
|
|
220
|
+
* @param options - List options (limit, cursor)
|
|
221
|
+
* @returns List of blob metadata
|
|
222
|
+
*/
|
|
223
|
+
async listBlobs(prefix, options) {
|
|
224
|
+
const result = await list({
|
|
225
|
+
token: this.resolvedConfig.token,
|
|
226
|
+
prefix,
|
|
227
|
+
limit: options?.limit,
|
|
228
|
+
cursor: options?.cursor
|
|
229
|
+
});
|
|
230
|
+
return {
|
|
231
|
+
blobs: result.blobs.map((blob) => ({
|
|
232
|
+
url: blob.url,
|
|
233
|
+
pathname: blob.pathname,
|
|
234
|
+
size: blob.size,
|
|
235
|
+
uploadedAt: blob.uploadedAt
|
|
236
|
+
})),
|
|
237
|
+
hasMore: result.hasMore,
|
|
238
|
+
cursor: result.cursor
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
// ============================================================
|
|
242
|
+
// Helper Methods
|
|
243
|
+
// ============================================================
|
|
244
|
+
/**
|
|
245
|
+
* Build pathname for Vercel Blob upload.
|
|
246
|
+
*
|
|
247
|
+
* Creates pathname in format: {folder}/{filename}
|
|
248
|
+
* If no folder is provided, uses the filename directly.
|
|
249
|
+
*
|
|
250
|
+
* @param filename - Original filename (will be sanitized)
|
|
251
|
+
* @param folder - Optional folder/prefix for organizing uploads
|
|
252
|
+
* @returns Generated pathname
|
|
253
|
+
* @throws Error if folder fails sanitization
|
|
254
|
+
*/
|
|
255
|
+
buildPathname(filename, folder) {
|
|
256
|
+
const sanitized = this.sanitizeFilename(filename);
|
|
257
|
+
if (!folder) return sanitized;
|
|
258
|
+
return `${this.sanitizeFolder(folder)}/${sanitized}`;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Sanitize filename to prevent path traversal issues.
|
|
262
|
+
*
|
|
263
|
+
* @param filename - Original filename
|
|
264
|
+
* @returns Sanitized filename
|
|
265
|
+
*/
|
|
266
|
+
sanitizeFilename(filename) {
|
|
267
|
+
const basename = filename.split(/[/\\]/).pop() || filename;
|
|
268
|
+
return basename.replace(/[^a-zA-Z0-9._-]/g, "-");
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Reject folder values that would let a caller
|
|
272
|
+
* escape the configured prefix via path traversal or weird
|
|
273
|
+
* separators. Vercel Blob is flat-keyed so `/` is just part of the
|
|
274
|
+
* pathname, but an attacker who controls `folder` could still:
|
|
275
|
+
*
|
|
276
|
+
* - Use `..` to chain a relative-traversal-style key.
|
|
277
|
+
* - Use `\` (Windows separator) to confuse downstream tooling.
|
|
278
|
+
* - Use control chars / null bytes to truncate the key.
|
|
279
|
+
* - Use a leading `/` to look like an absolute path.
|
|
280
|
+
*
|
|
281
|
+
* Internal `/` segments stay allowed because callers legitimately
|
|
282
|
+
* use nested folders (`docs/2026/...`); a leading `/`, empty
|
|
283
|
+
* segments (`docs//x`), or `..` segments are rejected.
|
|
284
|
+
*
|
|
285
|
+
* @throws Error with a stable message so the storage facade can
|
|
286
|
+
* surface a 400 instead of a 500.
|
|
287
|
+
*/
|
|
288
|
+
sanitizeFolder(folder) {
|
|
289
|
+
const trimmed = folder.replace(/\/+$/, "");
|
|
290
|
+
if (!trimmed) {
|
|
291
|
+
throw new Error("Folder must be a non-empty string.");
|
|
292
|
+
}
|
|
293
|
+
if (/[\\\0\x01-\x1f\x7f]/.test(trimmed)) {
|
|
294
|
+
throw new Error(
|
|
295
|
+
"Folder contains forbidden characters (backslash, control chars, or null bytes)."
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
if (trimmed.startsWith("/")) {
|
|
299
|
+
throw new Error("Folder must not start with `/`.");
|
|
300
|
+
}
|
|
301
|
+
const segments = trimmed.split("/");
|
|
302
|
+
for (const segment of segments) {
|
|
303
|
+
if (segment === "") {
|
|
304
|
+
throw new Error("Folder must not contain empty segments (`//`).");
|
|
305
|
+
}
|
|
306
|
+
if (segment === "..") {
|
|
307
|
+
throw new Error("Folder must not contain `..` segments.");
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return trimmed;
|
|
311
|
+
}
|
|
312
|
+
// ============================================================
|
|
313
|
+
// Public Accessors
|
|
314
|
+
// ============================================================
|
|
315
|
+
/**
|
|
316
|
+
* Get the configured token (masked for logging).
|
|
317
|
+
*/
|
|
318
|
+
getTokenMasked() {
|
|
319
|
+
const token = this.resolvedConfig.token;
|
|
320
|
+
if (token.length <= 8) return "****";
|
|
321
|
+
return `${token.slice(0, 4)}...${token.slice(-4)}`;
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Check if random suffix is enabled.
|
|
325
|
+
*/
|
|
326
|
+
hasRandomSuffix() {
|
|
327
|
+
return this.resolvedConfig.addRandomSuffix;
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Get configured cache control max age.
|
|
331
|
+
*/
|
|
332
|
+
getCacheControlMaxAge() {
|
|
333
|
+
return this.resolvedConfig.cacheControlMaxAge;
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
// src/plugin.ts
|
|
338
|
+
function vercelBlobStorage(config) {
|
|
339
|
+
if (config.enabled === false) {
|
|
340
|
+
return {
|
|
341
|
+
name: "vercel-blob-storage",
|
|
342
|
+
type: "vercel-blob",
|
|
343
|
+
collections: {},
|
|
344
|
+
adapter: null
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
const adapter = new VercelBlobStorageAdapter(config);
|
|
348
|
+
const plugin = {
|
|
349
|
+
name: "vercel-blob-storage",
|
|
350
|
+
type: "vercel-blob",
|
|
351
|
+
collections: config.collections,
|
|
352
|
+
adapter,
|
|
353
|
+
/**
|
|
354
|
+
* Generate upload URL for client-side uploads.
|
|
355
|
+
*
|
|
356
|
+
* Note: Vercel Blob uses a different pattern for client uploads.
|
|
357
|
+
* Instead of pre-signed URLs, it uses the handleUpload() API which
|
|
358
|
+
* requires server-side route handlers.
|
|
359
|
+
*
|
|
360
|
+
* This method throws an error explaining the correct approach.
|
|
361
|
+
*
|
|
362
|
+
* @see https://vercel.com/docs/storage/vercel-blob/client-upload
|
|
363
|
+
*/
|
|
364
|
+
getClientUploadUrl(_filename, _mimeType, _collection) {
|
|
365
|
+
return Promise.reject(
|
|
366
|
+
new Error(
|
|
367
|
+
"@nextly/storage-vercel-blob: Client uploads require the handleUpload API.\n\nVercel Blob uses a different pattern than S3 for client-side uploads:\n\n1. Create a server-side route handler with handleUpload()\n2. Use upload() from @vercel/blob/client on the frontend\n\nSee: https://vercel.com/docs/storage/vercel-blob/client-upload"
|
|
368
|
+
)
|
|
369
|
+
);
|
|
370
|
+
},
|
|
371
|
+
/**
|
|
372
|
+
* Get signed download URL for file access.
|
|
373
|
+
*
|
|
374
|
+
* Note: Vercel Blob URLs are public by default and do not support
|
|
375
|
+
* signed/temporary URLs. All uploaded blobs are accessible via their
|
|
376
|
+
* public URL.
|
|
377
|
+
*
|
|
378
|
+
* @param path - Storage path (full blob URL)
|
|
379
|
+
* @returns The same URL (Vercel Blob URLs are public)
|
|
380
|
+
*/
|
|
381
|
+
getSignedDownloadUrl(path) {
|
|
382
|
+
return Promise.resolve(path);
|
|
383
|
+
}
|
|
384
|
+
};
|
|
385
|
+
return plugin;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// src/index.ts
|
|
389
|
+
var PACKAGE_NAME = "@nextly/storage-vercel-blob";
|
|
390
|
+
var PACKAGE_VERSION = "0.1.0";
|
|
391
|
+
|
|
392
|
+
export { PACKAGE_NAME, PACKAGE_VERSION, VercelBlobStorageAdapter, vercelBlobStorage };
|
|
393
|
+
//# sourceMappingURL=index.mjs.map
|
|
394
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/adapter.ts","../src/plugin.ts","../src/index.ts"],"names":[],"mappings":";;;AAuDO,IAAM,2BAAN,MAA0D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAS/D,YAAo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AAA,IAC7B;AAAA,GACF;AAEA,EAAA,OAAO,MAAA;AACT;;;AC1FO,IAAM,YAAA,GAAe;AACrB,IAAM,eAAA,GAAkB","file":"index.mjs","sourcesContent":["/**\n * Vercel Blob Storage Adapter\n *\n * Stores files on Vercel Blob Storage, a globally distributed object storage\n * service powered by Cloudflare R2. Optimized for Vercel deployments with\n * built-in CDN distribution.\n *\n * Features:\n * - Globally distributed CDN-backed storage\n * - Automatic file management with unique filenames\n * - Token-based authentication\n * - Client-side upload support (via handleUpload API)\n * - Serverless-friendly (no filesystem access required)\n *\n * @example Basic usage\n * ```typescript\n * const adapter = new VercelBlobStorageAdapter({\n * token: process.env.BLOB_READ_WRITE_TOKEN,\n * collections: { media: true }\n * });\n *\n * const result = await adapter.upload(buffer, {\n * filename: 'photo.jpg',\n * mimeType: 'image/jpeg'\n * });\n * // result.url: \"https://abc123.public.blob.vercel-storage.com/photo-xyz.jpg\"\n * ```\n */\n\nimport { put, del, head, list } from \"@vercel/blob\";\nimport type {\n IStorageAdapter,\n UploadOptions,\n UploadResult,\n StorageAdapterInfo,\n ClientUploadData,\n FileMetadata,\n BulkDeleteResult,\n} from \"nextly/storage\";\n\nimport type {\n VercelBlobStorageConfig,\n ResolvedVercelBlobConfig,\n} from \"./types\";\n\n// ============================================================\n// Vercel Blob Storage Adapter\n// ============================================================\n\n/**\n * Vercel Blob Storage Adapter\n *\n * Implements the IStorageAdapter interface for Vercel Blob Storage.\n * Provides file upload, deletion, existence checks, and metadata retrieval.\n */\nexport class VercelBlobStorageAdapter implements IStorageAdapter {\n private resolvedConfig: ResolvedVercelBlobConfig;\n\n /**\n * Create a new Vercel Blob storage adapter.\n *\n * @param config - Vercel Blob storage configuration\n * @throws Error if token is not provided\n */\n constructor(private config: VercelBlobStorageConfig) {\n // Resolve token from config or environment\n const token = config.token || process.env.BLOB_READ_WRITE_TOKEN || \"\";\n\n if (!token) {\n throw new Error(\n \"@nextly/storage-vercel-blob: token is required.\\n\\n\" +\n \"Either set the BLOB_READ_WRITE_TOKEN environment variable or pass token in config.\\n\" +\n \"Get your token from: Vercel Dashboard > Storage > Blob > Tokens\"\n );\n }\n\n // Resolve config with defaults\n this.resolvedConfig = {\n token,\n addRandomSuffix: config.addRandomSuffix ?? true,\n cacheControlMaxAge: config.cacheControlMaxAge ?? 31536000,\n access: config.access ?? \"public\",\n storeId: config.storeId,\n allowOverwrite: config.allowOverwrite ?? false,\n multipartThreshold: config.multipartThreshold ?? 5 * 1024 * 1024, // 5MB\n };\n }\n\n // ============================================================\n // Core IStorageAdapter Methods\n // ============================================================\n\n /**\n * Upload file to Vercel Blob.\n *\n * Files are stored with globally distributed CDN backing.\n * By default, a random suffix is added to prevent filename collisions.\n *\n * @param buffer - File content as Buffer\n * @param options - Upload options (filename, mimeType, folder, collection)\n * @returns Upload result with URL and storage path\n */\n async upload(buffer: Buffer, options: UploadOptions): Promise<UploadResult> {\n // Hard-reject SVG and HTML uploads on Vercel Blob. The platform\n // does not support per-object response headers (no Content-\n // Disposition: attachment, no CSP), so an attacker who can\n // persuade an admin to upload `evil.svg` (or `.html`) gets a\n // stored XSS that fires on every viewer hit. S3 / R2 / similar\n // adapters can enforce attachment-disposition; Vercel Blob cannot.\n const mimeType = (options.contentType || options.mimeType || \"\")\n .toLowerCase()\n .trim();\n const filename = (options.filename || \"\").toLowerCase();\n const ext = filename.includes(\".\")\n ? filename.slice(filename.lastIndexOf(\".\") + 1)\n : \"\";\n const isSvg =\n mimeType === \"image/svg+xml\" || ext === \"svg\" || ext === \"svgz\";\n const isHtml =\n mimeType === \"text/html\" ||\n mimeType === \"application/xhtml+xml\" ||\n ext === \"html\" ||\n ext === \"htm\" ||\n ext === \"xhtml\";\n if (isSvg || isHtml) {\n const kind = isSvg ? \"SVG\" : \"HTML\";\n throw new Error(\n `[nextly/storage-vercel-blob] ${kind} uploads are rejected on Vercel ` +\n `Blob — the platform cannot serve them with attachment-disposition ` +\n `or a restrictive CSP, so they would be stored XSS. Use the S3 / R2 ` +\n `adapter for ${kind} files, or convert to a raster format (PNG/WebP).`\n );\n }\n\n const pathname = this.buildPathname(options.filename, options.folder);\n\n const result = await put(pathname, buffer, {\n access: this.resolvedConfig.access,\n token: this.resolvedConfig.token,\n contentType: options.contentType || options.mimeType,\n addRandomSuffix: this.resolvedConfig.addRandomSuffix,\n cacheControlMaxAge: this.resolvedConfig.cacheControlMaxAge,\n });\n\n // Vercel Blob uses the URL as the path identifier\n return {\n url: result.url,\n path: result.url,\n };\n }\n\n /**\n * Delete file from Vercel Blob.\n *\n * @param filePath - Full blob URL to delete\n */\n async delete(filePath: string): Promise<void> {\n await del(filePath, {\n token: this.resolvedConfig.token,\n });\n }\n\n /**\n * Delete multiple files from Vercel Blob in chunked batches.\n *\n * Processes deletions in chunks of 10 concurrent calls using Promise.allSettled,\n * collecting successes and failures without short-circuiting.\n *\n * @param filePaths - Array of full blob URLs to delete\n * @returns BulkDeleteResult with successful and failed arrays\n */\n async bulkDelete(filePaths: string[]): Promise<BulkDeleteResult> {\n const successful: string[] = [];\n const failed: Array<{ filePath: string; error: string }> = [];\n const chunkSize = 10;\n\n for (let i = 0; i < filePaths.length; i += chunkSize) {\n const chunk = filePaths.slice(i, i + chunkSize);\n const results = await Promise.allSettled(\n chunk.map(fp => del(fp, { token: this.resolvedConfig.token }))\n );\n\n results.forEach((result, idx) => {\n const fp = chunk[idx];\n if (result.status === \"fulfilled\") {\n successful.push(fp);\n } else {\n failed.push({\n filePath: fp,\n error:\n result.reason instanceof Error\n ? result.reason.message\n : String(result.reason),\n });\n }\n });\n }\n\n return { successful, failed };\n }\n\n /**\n * Check if file exists in Vercel Blob.\n *\n * Uses the head() function which is efficient for existence checks.\n *\n * @param filePath - Full blob URL to check\n * @returns true if file exists, false otherwise\n */\n async exists(filePath: string): Promise<boolean> {\n try {\n await head(filePath, {\n token: this.resolvedConfig.token,\n });\n return true;\n } catch {\n // Vercel Blob throws an error if the blob doesn't exist\n return false;\n }\n }\n\n /**\n * Get public URL for Vercel Blob file.\n *\n * Vercel Blob paths ARE the public URLs - they're already full HTTPS URLs\n * with CDN distribution.\n *\n * @param filePath - Full blob URL\n * @returns The same URL (Vercel Blob paths are already public URLs)\n */\n getPublicUrl(filePath: string): string {\n // Vercel Blob paths are already full URLs\n return filePath;\n }\n\n /**\n * Get storage type identifier.\n */\n getType(): \"vercel-blob\" {\n return \"vercel-blob\";\n }\n\n // ============================================================\n // Optional IStorageAdapter Methods\n // ============================================================\n\n /**\n * Get adapter info including capabilities.\n *\n * @returns Adapter info with type, name, and capability flags\n */\n getInfo(): StorageAdapterInfo {\n return {\n type: \"vercel-blob\",\n name: \"VercelBlobStorageAdapter\",\n supportsSignedUrls: false, // Vercel Blob URLs are public by default\n supportsClientUploads: true, // Via handleUpload API\n };\n }\n\n /**\n * Get file metadata from Vercel Blob.\n *\n * Retrieves file information including size, content type, and upload date.\n *\n * @param filePath - Full blob URL\n * @returns File metadata or null if file not found\n */\n async getMetadata(filePath: string): Promise<FileMetadata | null> {\n try {\n const result = await head(filePath, {\n token: this.resolvedConfig.token,\n });\n\n // Extract filename from pathname\n const filename = result.pathname.split(\"/\").pop() || result.pathname;\n\n return {\n id: filePath,\n filename,\n originalFilename: filename,\n mimeType: result.contentType,\n size: result.size,\n url: result.url,\n createdAt: result.uploadedAt.toISOString(),\n };\n } catch {\n // File not found\n return null;\n }\n }\n\n /**\n * Generate pre-signed URL for client-side uploads.\n *\n * Vercel Blob uses a different pattern for client uploads - they require\n * the handleUpload API which handles token generation server-side.\n *\n * This method is not implemented because Vercel Blob's client upload flow\n * requires server-side route handlers with handleUpload().\n *\n * @see https://vercel.com/docs/storage/vercel-blob/client-upload\n * @throws Error always - client uploads require handleUpload API\n */\n getPresignedUploadUrl(\n _key: string,\n _mimeType: string,\n _expiresIn?: number\n ): Promise<ClientUploadData> {\n return Promise.reject(\n new Error(\n \"@nextly/storage-vercel-blob: Client uploads require the handleUpload API.\\n\\n\" +\n \"Vercel Blob uses a different pattern for client-side uploads that involves:\\n\" +\n \"1. A server-side route handler with handleUpload()\\n\" +\n \"2. The @vercel/blob/client upload() function\\n\\n\" +\n \"See: https://vercel.com/docs/storage/vercel-blob/client-upload\\n\\n\" +\n \"For server-side uploads, use the standard upload() method instead.\"\n )\n );\n }\n\n // ============================================================\n // Additional Methods\n // ============================================================\n\n /**\n * List blobs with optional prefix filter.\n *\n * Useful for browsing or batch operations on stored files.\n *\n * @param prefix - Optional prefix to filter results\n * @param options - List options (limit, cursor)\n * @returns List of blob metadata\n */\n async listBlobs(\n prefix?: string,\n options?: { limit?: number; cursor?: string }\n ): Promise<{\n blobs: Array<{\n url: string;\n pathname: string;\n size: number;\n uploadedAt: Date;\n }>;\n hasMore: boolean;\n cursor?: string;\n }> {\n const result = await list({\n token: this.resolvedConfig.token,\n prefix,\n limit: options?.limit,\n cursor: options?.cursor,\n });\n\n return {\n blobs: result.blobs.map(blob => ({\n url: blob.url,\n pathname: blob.pathname,\n size: blob.size,\n uploadedAt: blob.uploadedAt,\n })),\n hasMore: result.hasMore,\n cursor: result.cursor,\n };\n }\n\n // ============================================================\n // Helper Methods\n // ============================================================\n\n /**\n * Build pathname for Vercel Blob upload.\n *\n * Creates pathname in format: {folder}/{filename}\n * If no folder is provided, uses the filename directly.\n *\n * @param filename - Original filename (will be sanitized)\n * @param folder - Optional folder/prefix for organizing uploads\n * @returns Generated pathname\n * @throws Error if folder fails sanitization\n */\n private buildPathname(filename: string, folder?: string): string {\n const sanitized = this.sanitizeFilename(filename);\n if (!folder) return sanitized;\n return `${this.sanitizeFolder(folder)}/${sanitized}`;\n }\n\n /**\n * Sanitize filename to prevent path traversal issues.\n *\n * @param filename - Original filename\n * @returns Sanitized filename\n */\n private sanitizeFilename(filename: string): string {\n const basename = filename.split(/[/\\\\]/).pop() || filename;\n return basename.replace(/[^a-zA-Z0-9._-]/g, \"-\");\n }\n\n /**\n * Reject folder values that would let a caller\n * escape the configured prefix via path traversal or weird\n * separators. Vercel Blob is flat-keyed so `/` is just part of the\n * pathname, but an attacker who controls `folder` could still:\n *\n * - Use `..` to chain a relative-traversal-style key.\n * - Use `\\` (Windows separator) to confuse downstream tooling.\n * - Use control chars / null bytes to truncate the key.\n * - Use a leading `/` to look like an absolute path.\n *\n * Internal `/` segments stay allowed because callers legitimately\n * use nested folders (`docs/2026/...`); a leading `/`, empty\n * segments (`docs//x`), or `..` segments are rejected.\n *\n * @throws Error with a stable message so the storage facade can\n * surface a 400 instead of a 500.\n */\n private sanitizeFolder(folder: string): string {\n const trimmed = folder.replace(/\\/+$/, \"\"); // tolerate one trailing `/`\n if (!trimmed) {\n throw new Error(\"Folder must be a non-empty string.\");\n }\n // eslint-disable-next-line no-control-regex -- the whole point is to reject control chars\n if (/[\\\\\\0\\x01-\\x1f\\x7f]/.test(trimmed)) {\n throw new Error(\n \"Folder contains forbidden characters (backslash, control chars, or null bytes).\"\n );\n }\n if (trimmed.startsWith(\"/\")) {\n throw new Error(\"Folder must not start with `/`.\");\n }\n const segments = trimmed.split(\"/\");\n for (const segment of segments) {\n if (segment === \"\") {\n throw new Error(\"Folder must not contain empty segments (`//`).\");\n }\n if (segment === \"..\") {\n throw new Error(\"Folder must not contain `..` segments.\");\n }\n }\n return trimmed;\n }\n\n // ============================================================\n // Public Accessors\n // ============================================================\n\n /**\n * Get the configured token (masked for logging).\n */\n getTokenMasked(): string {\n const token = this.resolvedConfig.token;\n if (token.length <= 8) return \"****\";\n return `${token.slice(0, 4)}...${token.slice(-4)}`;\n }\n\n /**\n * Check if random suffix is enabled.\n */\n hasRandomSuffix(): boolean {\n return this.resolvedConfig.addRandomSuffix;\n }\n\n /**\n * Get configured cache control max age.\n */\n getCacheControlMaxAge(): number {\n return this.resolvedConfig.cacheControlMaxAge;\n }\n}\n","/**\n * Vercel Blob Storage Plugin\n *\n * Factory function that creates a storage plugin for Vercel Blob Storage.\n * Returns a StoragePlugin that can be registered with MediaStorage.\n *\n * @example Basic usage\n * ```typescript\n * import { vercelBlobStorage } from '@nextly/storage-vercel-blob'\n * import { defineConfig } from 'nextly/config'\n *\n * export default defineConfig({\n * storage: [\n * vercelBlobStorage({\n * token: process.env.BLOB_READ_WRITE_TOKEN,\n * collections: {\n * media: true\n * }\n * })\n * ]\n * })\n * ```\n *\n * @example With client uploads (recommended for Vercel)\n * ```typescript\n * vercelBlobStorage({\n * collections: {\n * media: {\n * clientUploads: true // Bypass 4.5MB serverless limit\n * }\n * }\n * })\n * ```\n *\n * @example With collection-specific configuration\n * ```typescript\n * vercelBlobStorage({\n * addRandomSuffix: true,\n * cacheControlMaxAge: 86400, // 1 day\n * collections: {\n * // Simple enable with defaults\n * media: true,\n *\n * // Full configuration\n * documents: {\n * prefix: 'docs/',\n * addRandomSuffix: false,\n * allowOverwrite: true\n * }\n * }\n * })\n * ```\n *\n * @packageDocumentation\n */\n\nimport type { StoragePlugin, ClientUploadData } from \"nextly/storage\";\n\nimport { VercelBlobStorageAdapter } from \"./adapter\";\nimport type { VercelBlobStorageConfig } from \"./types\";\n\n// ============================================================\n// Plugin Factory Function\n// ============================================================\n\n/**\n * Create a Vercel Blob storage plugin for Nextly.\n *\n * This factory function creates a StoragePlugin that can be added to\n * the `storage` array in `nextly.config.ts`. Vercel Blob is optimized\n * for Vercel deployments with:\n *\n * - Global CDN distribution\n * - Simple token-based authentication\n * - Client-side upload support (via handleUpload API)\n * - Automatic file management\n *\n * @param config - Vercel Blob storage configuration\n * @returns A StoragePlugin that MediaStorage can register\n *\n * @throws Error if token is not provided (via adapter)\n */\nexport function vercelBlobStorage(\n config: VercelBlobStorageConfig\n): StoragePlugin {\n // Handle disabled plugin\n // When disabled, return a plugin with no collections and null adapter\n // MediaStorage.registerPlugin() checks for null adapter and skips registration\n if (config.enabled === false) {\n return {\n name: \"vercel-blob-storage\",\n type: \"vercel-blob\",\n collections: {},\n adapter: null as unknown as StoragePlugin[\"adapter\"],\n };\n }\n\n // Create the Vercel Blob adapter\n // Adapter constructor validates required config (token)\n const adapter = new VercelBlobStorageAdapter(config);\n\n // Build and return the plugin\n const plugin: StoragePlugin = {\n name: \"vercel-blob-storage\",\n type: \"vercel-blob\",\n collections: config.collections,\n adapter,\n\n /**\n * Generate upload URL for client-side uploads.\n *\n * Note: Vercel Blob uses a different pattern for client uploads.\n * Instead of pre-signed URLs, it uses the handleUpload() API which\n * requires server-side route handlers.\n *\n * This method throws an error explaining the correct approach.\n *\n * @see https://vercel.com/docs/storage/vercel-blob/client-upload\n */\n getClientUploadUrl(\n _filename: string,\n _mimeType: string,\n _collection: string\n ): Promise<ClientUploadData> {\n // Vercel Blob doesn't use pre-signed URLs like S3\n // Instead, it requires using the handleUpload API with @vercel/blob/client\n return Promise.reject(\n new Error(\n \"@nextly/storage-vercel-blob: Client uploads require the handleUpload API.\\n\\n\" +\n \"Vercel Blob uses a different pattern than S3 for client-side uploads:\\n\\n\" +\n \"1. Create a server-side route handler with handleUpload()\\n\" +\n \"2. Use upload() from @vercel/blob/client on the frontend\\n\\n\" +\n \"See: https://vercel.com/docs/storage/vercel-blob/client-upload\"\n )\n );\n },\n\n /**\n * Get signed download URL for file access.\n *\n * Note: Vercel Blob URLs are public by default and do not support\n * signed/temporary URLs. All uploaded blobs are accessible via their\n * public URL.\n *\n * @param path - Storage path (full blob URL)\n * @returns The same URL (Vercel Blob URLs are public)\n */\n getSignedDownloadUrl(path: string): Promise<string> {\n // Vercel Blob URLs are public by default\n // No signed URL support - just return the public URL\n return Promise.resolve(path);\n },\n };\n\n return plugin;\n}\n","/**\n * @nextly/storage-vercel-blob\n *\n * Vercel Blob storage adapter for Nextly CMS.\n * Optimized for Vercel deployments with client-side upload support\n * to bypass serverless function size limits (4.5MB).\n *\n * @example Basic usage\n * ```typescript\n * import { vercelBlobStorage } from '@nextly/storage-vercel-blob'\n * import { defineConfig } from 'nextly/config'\n *\n * export default defineConfig({\n * storage: [\n * vercelBlobStorage({\n * token: process.env.BLOB_READ_WRITE_TOKEN,\n * collections: {\n * media: true\n * }\n * })\n * ]\n * })\n * ```\n *\n * @example With client uploads (recommended for Vercel)\n * ```typescript\n * vercelBlobStorage({\n * collections: {\n * media: {\n * clientUploads: true // Bypass 4.5MB serverless limit\n * }\n * }\n * })\n * ```\n *\n * @packageDocumentation\n */\n\n// ============================================================\n// Vercel Blob Storage Plugin Export (Primary API)\n// ============================================================\n\nexport { vercelBlobStorage } from \"./plugin\";\n\n// ============================================================\n// Vercel Blob Storage Adapter Export\n// ============================================================\n\nexport { VercelBlobStorageAdapter } from \"./adapter\";\n\n// ============================================================\n// Vercel Blob Types Export\n// ============================================================\n\nexport type {\n VercelBlobStorageConfig,\n VercelBlobCollectionConfig,\n VercelBlobCollectionStorageMap,\n ResolvedVercelBlobConfig,\n} from \"./types\";\n\n// ============================================================\n// Package Metadata\n// ============================================================\n\nexport const PACKAGE_NAME = \"@nextly/storage-vercel-blob\";\nexport const PACKAGE_VERSION = \"0.1.0\";\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nextlyhq/storage-vercel-blob",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Vercel Blob storage adapter for Nextly",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "dist/index.cjs",
|
|
8
|
+
"module": "dist/index.mjs",
|
|
9
|
+
"types": "dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.mjs",
|
|
14
|
+
"require": "./dist/index.cjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=20.0.0"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@vercel/blob": "^2.0.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/node": "^20.0.0",
|
|
28
|
+
"@vitest/coverage-v8": "^4.0.8",
|
|
29
|
+
"@vitest/ui": "^4.0.8",
|
|
30
|
+
"eslint": "^9.34.0",
|
|
31
|
+
"tsup": "^8.5.0",
|
|
32
|
+
"typescript": "^5.9.3",
|
|
33
|
+
"vite-tsconfig-paths": "^5.1.4",
|
|
34
|
+
"vitest": "^4.0.8",
|
|
35
|
+
"@nextlyhq/eslint-config": "0.0.1",
|
|
36
|
+
"@nextlyhq/tsconfig": "0.0.1",
|
|
37
|
+
"nextly": "0.0.1"
|
|
38
|
+
},
|
|
39
|
+
"keywords": [
|
|
40
|
+
"nextly",
|
|
41
|
+
"storage",
|
|
42
|
+
"vercel",
|
|
43
|
+
"blob",
|
|
44
|
+
"vercel-blob",
|
|
45
|
+
"media",
|
|
46
|
+
"upload",
|
|
47
|
+
"serverless"
|
|
48
|
+
],
|
|
49
|
+
"repository": {
|
|
50
|
+
"type": "git",
|
|
51
|
+
"url": "git+https://github.com/nextlyhq/nextly.git",
|
|
52
|
+
"directory": "packages/storage-vercel-blob"
|
|
53
|
+
},
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public",
|
|
56
|
+
"registry": "https://registry.npmjs.org/",
|
|
57
|
+
"provenance": true
|
|
58
|
+
},
|
|
59
|
+
"homepage": "https://nextlyhq.com",
|
|
60
|
+
"bugs": {
|
|
61
|
+
"url": "https://github.com/nextlyhq/nextly/issues"
|
|
62
|
+
},
|
|
63
|
+
"author": "Nextly <contact@nextlyhq.com> (https://nextlyhq.com)",
|
|
64
|
+
"scripts": {
|
|
65
|
+
"build": "tsup",
|
|
66
|
+
"dev": "tsup --watch",
|
|
67
|
+
"check-types": "tsc --noEmit",
|
|
68
|
+
"lint": "eslint . --max-warnings 0",
|
|
69
|
+
"lint:fix": "eslint . --fix",
|
|
70
|
+
"test": "vitest run --passWithNoTests",
|
|
71
|
+
"test:watch": "vitest",
|
|
72
|
+
"test:ui": "vitest --ui",
|
|
73
|
+
"test:coverage": "vitest run --coverage",
|
|
74
|
+
"clean": "rimraf dist"
|
|
75
|
+
}
|
|
76
|
+
}
|