@easy-cms/storage-vercel-blob 0.39.0 → 0.41.0
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/dist/index.d.ts +3 -0
- package/dist/index.js +53 -1
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { StorageAdapter } from '@easy-cms/core';
|
|
2
2
|
import { put, del, get } from '@vercel/blob';
|
|
3
|
+
import { generateClientTokenFromReadWriteToken } from '@vercel/blob/client';
|
|
3
4
|
|
|
4
5
|
interface VercelBlobStorageOptions {
|
|
5
6
|
/** The store's read-write token. Default: `BLOB_READ_WRITE_TOKEN` (set when a store is connected). */
|
|
@@ -17,6 +18,8 @@ interface VercelBlobClient {
|
|
|
17
18
|
put: typeof put;
|
|
18
19
|
del: typeof del;
|
|
19
20
|
get: typeof get;
|
|
21
|
+
/** Optional: makes the token a browser uploads a large file with. */
|
|
22
|
+
clientToken?: typeof generateClientTokenFromReadWriteToken;
|
|
20
23
|
}
|
|
21
24
|
/**
|
|
22
25
|
* Stores uploaded files in Vercel Blob, for sites on Vercel (whose disk doesn't keep files).
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import { EasyCMSError } from "@easy-cms/core";
|
|
3
3
|
import { del, get, put } from "@vercel/blob";
|
|
4
|
+
import { generateClientTokenFromReadWriteToken } from "@vercel/blob/client";
|
|
5
|
+
var API = "https://vercel.com/api/blob";
|
|
6
|
+
var API_VERSION = "12";
|
|
4
7
|
function storeIdOf(token) {
|
|
5
8
|
const id = token.split("_")[3] ?? "";
|
|
6
9
|
if (!id) throw new Error("Vercel Blob: BLOB_READ_WRITE_TOKEN is not a read-write token");
|
|
7
10
|
return id;
|
|
8
11
|
}
|
|
9
|
-
function vercelBlobStorage(options = {}, client = { put, del, get }) {
|
|
12
|
+
function vercelBlobStorage(options = {}, client = { put, del, get, clientToken: generateClientTokenFromReadWriteToken }) {
|
|
10
13
|
const access = options.access ?? "public";
|
|
11
14
|
const prefix = options.prefix ?? "";
|
|
12
15
|
const configured = () => options.token ?? process.env.BLOB_READ_WRITE_TOKEN;
|
|
@@ -37,6 +40,55 @@ function vercelBlobStorage(options = {}, client = { put, del, get }) {
|
|
|
37
40
|
const body = new Uint8Array(await new Response(found.stream).arrayBuffer());
|
|
38
41
|
return { body, size: body.byteLength };
|
|
39
42
|
},
|
|
43
|
+
// Large files from the browser: a token for this one file, its size and type, for a while.
|
|
44
|
+
async uploadURL(key, { contentType, size, expiresIn }) {
|
|
45
|
+
const pathname = `${prefix}${key}`;
|
|
46
|
+
const clientToken = await (client.clientToken ?? generateClientTokenFromReadWriteToken)({
|
|
47
|
+
token: token(),
|
|
48
|
+
pathname,
|
|
49
|
+
maximumSizeInBytes: size,
|
|
50
|
+
allowedContentTypes: [contentType],
|
|
51
|
+
addRandomSuffix: false,
|
|
52
|
+
validUntil: Date.now() + expiresIn * 1e3
|
|
53
|
+
});
|
|
54
|
+
return {
|
|
55
|
+
url: `${API}/?${new URLSearchParams({ pathname })}`,
|
|
56
|
+
method: "PUT",
|
|
57
|
+
headers: {
|
|
58
|
+
authorization: `Bearer ${clientToken}`,
|
|
59
|
+
"x-api-version": API_VERSION,
|
|
60
|
+
"x-vercel-blob-access": access,
|
|
61
|
+
"x-content-type": contentType,
|
|
62
|
+
"x-add-random-suffix": "0"
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
},
|
|
66
|
+
// The start of a file, and its size, without reading it all (to check its type).
|
|
67
|
+
async getStart(key, bytes) {
|
|
68
|
+
const value = configured();
|
|
69
|
+
if (!value) return null;
|
|
70
|
+
const found = await client.get(`${prefix}${key}`, { access, token: value });
|
|
71
|
+
if (found?.statusCode !== 200) return null;
|
|
72
|
+
const reader = found.stream.getReader();
|
|
73
|
+
const chunks = [];
|
|
74
|
+
let read = 0;
|
|
75
|
+
while (read < bytes) {
|
|
76
|
+
const { done, value: chunk } = await reader.read();
|
|
77
|
+
if (done) break;
|
|
78
|
+
chunks.push(chunk);
|
|
79
|
+
read += chunk.byteLength;
|
|
80
|
+
}
|
|
81
|
+
await reader.cancel().catch(() => {
|
|
82
|
+
});
|
|
83
|
+
const body = new Uint8Array(Math.min(read, bytes));
|
|
84
|
+
let offset = 0;
|
|
85
|
+
for (const chunk of chunks) {
|
|
86
|
+
const part = chunk.subarray(0, body.length - offset);
|
|
87
|
+
body.set(part, offset);
|
|
88
|
+
offset += part.byteLength;
|
|
89
|
+
}
|
|
90
|
+
return { body, size: found.blob.size ?? read };
|
|
91
|
+
},
|
|
40
92
|
async delete(key) {
|
|
41
93
|
await client.del(`${prefix}${key}`, { token: token() });
|
|
42
94
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@easy-cms/storage-vercel-blob",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.41.0",
|
|
4
4
|
"description": "Vercel Blob upload storage for Easy CMS",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -19,10 +19,10 @@
|
|
|
19
19
|
"@vercel/blob": "^2.8.0"
|
|
20
20
|
},
|
|
21
21
|
"peerDependencies": {
|
|
22
|
-
"@easy-cms/core": "^0.
|
|
22
|
+
"@easy-cms/core": "^0.41.0"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@easy-cms/core": "^0.
|
|
25
|
+
"@easy-cms/core": "^0.41.0"
|
|
26
26
|
},
|
|
27
27
|
"engines": {
|
|
28
28
|
"node": ">=22.12"
|