-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.ts
More file actions
307 lines (264 loc) · 9.11 KB
/
storage.ts
File metadata and controls
307 lines (264 loc) · 9.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// oxlint-disable max-lines
// oxlint-disable max-params
// oxlint-disable no-await-in-loop
// oxlint-disable class-methods-use-this
// oxlint-disable require-await
import { Readable } from "node:stream";
import type { ReadableStream as ReadableStreamWeb } from "node:stream/web";
import { StorageAdapterErrors, type StorageAdapter } from "@storybooker/core/adapter";
import { del, head, list, put, type PutBlobResult } from "@vercel/blob";
export class VercelBlobService implements StorageAdapter {
#token: string;
#baseUrl?: string;
constructor(token: string, baseUrl?: string) {
this.#token = token;
this.#baseUrl = baseUrl;
}
metadata: StorageAdapter["metadata"] = {
name: "Vercel Storage",
};
createContainer: StorageAdapter["createContainer"] = async (_containerId, _options) => {
// Vercel Blob doesn't have explicit container creation
// Containers are implicit through path prefixes
};
deleteContainer: StorageAdapter["deleteContainer"] = async (containerId, options) => {
try {
const containerPrefix = this.#getContainerPrefix(containerId);
// List all blobs with the container prefix
const { blobs } = await list({
abortSignal: options.abortSignal,
prefix: containerPrefix,
token: this.#token,
...(this.#baseUrl && { baseUrl: this.#baseUrl }),
});
if (blobs.length === 0) {
throw new StorageAdapterErrors.ContainerDoesNotExistError(containerId);
}
// Delete all blobs in the container
const urls = blobs.map((blob) => blob.url);
if (urls.length > 0) {
await del(urls, {
abortSignal: options.abortSignal,
token: this.#token,
...(this.#baseUrl && { baseUrl: this.#baseUrl }),
});
}
} catch (error) {
if (error instanceof StorageAdapterErrors.ContainerDoesNotExistError) {
throw error;
}
throw new StorageAdapterErrors.ContainerDoesNotExistError(containerId, error);
}
};
hasContainer: StorageAdapter["hasContainer"] = async (containerId, _options) => {
try {
const containerPrefix = this.#getContainerPrefix(containerId);
// Check if any blobs exist with this prefix
const { blobs } = await list({
limit: 1,
prefix: containerPrefix,
token: this.#token,
...(this.#baseUrl && { baseUrl: this.#baseUrl }),
});
return blobs.length > 0;
} catch {
return false;
}
};
listContainers: StorageAdapter["listContainers"] = async (_options) => {
try {
// List all blobs and extract unique container prefixes
const { blobs } = await list({
token: this.#token,
...(this.#baseUrl && { baseUrl: this.#baseUrl }),
});
const containers = new Set<string>();
for (const blob of blobs) {
const { pathname } = blob;
const firstSlashIndex = pathname.indexOf("/", 1); // Skip the leading slash
if (firstSlashIndex > 0) {
const containerId = pathname.slice(1, firstSlashIndex);
containers.add(containerId);
}
}
return [...containers];
} catch (error) {
throw new StorageAdapterErrors.CustomError(undefined, `Failed to list containers: ${error}`);
}
};
deleteFiles: StorageAdapter["deleteFiles"] = async (containerId, filePathsOrPrefix, options) => {
try {
const urls: string[] = [];
if (typeof filePathsOrPrefix === "string") {
// Delete by prefix
const fullPrefix = this.#getFilePath(containerId, filePathsOrPrefix);
const { blobs } = await list({
prefix: fullPrefix,
token: this.#token,
...(this.#baseUrl && { baseUrl: this.#baseUrl }),
});
urls.push(...blobs.map((blob) => blob.url));
} else {
// Delete specific files
for (const filepath of filePathsOrPrefix) {
const fullPath = this.#getFilePath(containerId, filepath);
try {
const blob = await head(fullPath, {
token: this.#token,
...(this.#baseUrl && { baseUrl: this.#baseUrl }),
});
urls.push(blob.url);
} catch {
// File doesn't exist, skip it
options.logger?.debug?.(`File not found, skipping: ${fullPath}`);
}
}
}
if (urls.length > 0) {
await del(urls, {
token: this.#token,
...(this.#baseUrl && { baseUrl: this.#baseUrl }),
});
}
} catch (error) {
throw new StorageAdapterErrors.CustomError(
undefined,
`Failed to delete files in container ${containerId}: ${error}`,
);
}
};
uploadFiles: StorageAdapter["uploadFiles"] = async (containerId, files, options) => {
const { errors } = await promisePool(
files.map(({ content, path, mimeType }) => async (): Promise<PutBlobResult> => {
return await this.#uploadFile(containerId, path, content, mimeType, options.abortSignal);
}),
20, // Concurrency limit
);
if (errors.length > 0) {
options.logger.error(`Failed to upload ${errors.length} files. Errors:`, errors);
}
};
hasFile: StorageAdapter["hasFile"] = async (containerId, filepath, _options) => {
try {
const fullPath = this.#getFilePath(containerId, filepath);
await head(fullPath, {
token: this.#token,
...(this.#baseUrl && { baseUrl: this.#baseUrl }),
});
return true;
} catch {
return false;
}
};
downloadFile: StorageAdapter["downloadFile"] = async (containerId, filepath, options) => {
try {
const fullPath = this.#getFilePath(containerId, filepath);
const blob = await head(fullPath, {
token: this.#token,
...(this.#baseUrl && { baseUrl: this.#baseUrl }),
});
// Create a ReadableStream from the blob URL
const response = await fetch(blob.url, {
signal: options.abortSignal,
});
if (!response.ok) {
throw new Error(`Failed to download file: ${response.statusText}`);
}
if (!response.body) {
throw new StorageAdapterErrors.FileMalformedError(
containerId,
filepath,
"No readable stream body found.",
);
}
return {
content: response.body,
mimeType: blob.contentType || "application/octet-stream",
path: filepath,
};
} catch (error) {
throw new StorageAdapterErrors.FileDoesNotExistError(containerId, filepath, error);
}
};
// Helper methods
#getContainerPrefix(containerId: string): string {
return `${containerId}/`;
}
#getFilePath(containerId: string, filepath: string): string {
return `${containerId}/${filepath}`;
}
async #uploadFile(
containerId: string,
filepath: string,
content: Blob | string | ReadableStream,
mimeType: string,
abortSignal?: AbortSignal,
): Promise<PutBlobResult> {
const fullPath = this.#getFilePath(containerId, filepath);
if (typeof content === "string") {
return await put(fullPath, content, {
abortSignal,
access: "public",
contentType: mimeType,
token: this.#token,
...(this.#baseUrl && { baseUrl: this.#baseUrl }),
});
}
if (content instanceof Blob) {
return await put(fullPath, content, {
abortSignal,
access: "public",
contentType: mimeType,
token: this.#token,
...(this.#baseUrl && { baseUrl: this.#baseUrl }),
});
}
if (content instanceof ReadableStream) {
// Convert ReadableStream to Node.js Readable, then to Blob
const readable = Readable.fromWeb(content as ReadableStreamWeb);
const chunks: Uint8Array[] = [];
for await (const chunk of readable) {
chunks.push(chunk);
}
const buffer = Buffer.concat(chunks);
const blob = new Blob([buffer], { type: mimeType });
return await put(fullPath, blob, {
abortSignal,
access: "public",
contentType: mimeType,
token: this.#token,
...(this.#baseUrl && { baseUrl: this.#baseUrl }),
});
}
throw new Error(`Unknown content type: ${typeof content}`);
}
}
async function promisePool<Result>(
tasks: (() => Promise<Result>)[],
concurrencyLimit: number,
): Promise<{ errors: unknown[]; results: Result[] }> {
const promises: Promise<Result>[] = [];
const errors: unknown[] = [];
const executing = new Set();
for (const task of tasks) {
// Start the taskPromise
const promise = Promise.resolve().then(task);
promises.push(promise);
// Add to executing set
executing.add(promise);
// When the promise settles, remove it from executing
const cleanup = (): boolean => executing.delete(promise);
promise.then(cleanup).catch((error: unknown) => {
errors.push(error);
cleanup();
});
// If the number of running promises hit concurrencyLimit, wait for one to finish
if (executing.size >= concurrencyLimit) {
// oxlint-disable-next-line no-await-in-loop
await Promise.race(executing);
}
}
// Wait for all remaining tasks to finish
const results = await Promise.all(promises);
return { errors, results };
}