@mastra/s3 0.0.0-feat-change-lifecycle-20260212011237

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.cjs ADDED
@@ -0,0 +1,593 @@
1
+ 'use strict';
2
+
3
+ var clientS3 = require('@aws-sdk/client-s3');
4
+ var workspace = require('@mastra/core/workspace');
5
+
6
+ // src/filesystem/index.ts
7
+ var MIME_TYPES = {
8
+ // Text
9
+ ".txt": "text/plain",
10
+ ".md": "text/markdown",
11
+ ".markdown": "text/markdown",
12
+ ".html": "text/html",
13
+ ".htm": "text/html",
14
+ ".css": "text/css",
15
+ ".csv": "text/csv",
16
+ ".xml": "text/xml",
17
+ // Code
18
+ ".js": "text/javascript",
19
+ ".mjs": "text/javascript",
20
+ ".ts": "text/typescript",
21
+ ".tsx": "text/typescript",
22
+ ".jsx": "text/javascript",
23
+ ".json": "application/json",
24
+ ".yaml": "text/yaml",
25
+ ".yml": "text/yaml",
26
+ ".py": "text/x-python",
27
+ ".rb": "text/x-ruby",
28
+ ".sh": "text/x-shellscript",
29
+ ".bash": "text/x-shellscript",
30
+ // Images
31
+ ".png": "image/png",
32
+ ".jpg": "image/jpeg",
33
+ ".jpeg": "image/jpeg",
34
+ ".gif": "image/gif",
35
+ ".svg": "image/svg+xml",
36
+ ".webp": "image/webp",
37
+ ".ico": "image/x-icon",
38
+ // Documents
39
+ ".pdf": "application/pdf",
40
+ // Archives
41
+ ".zip": "application/zip",
42
+ ".gz": "application/gzip",
43
+ ".tar": "application/x-tar"
44
+ };
45
+ function getMimeType(path) {
46
+ const ext = path.toLowerCase().match(/\.[^.]+$/)?.[0];
47
+ return ext ? MIME_TYPES[ext] ?? "application/octet-stream" : "application/octet-stream";
48
+ }
49
+ function isNotFoundError(error) {
50
+ if (!error || typeof error !== "object" || !("name" in error)) return false;
51
+ const name = error.name;
52
+ return name === "NotFound" || name === "NoSuchKey" || name === "404";
53
+ }
54
+ function isAccessDeniedError(error) {
55
+ if (!error || typeof error !== "object") return false;
56
+ const err = error;
57
+ return err.name === "AccessDenied" || err.$metadata?.httpStatusCode === 403;
58
+ }
59
+ function trimSlashes(s) {
60
+ let start = 0;
61
+ let end = s.length;
62
+ while (start < end && s[start] === "/") start++;
63
+ while (end > start && s[end - 1] === "/") end--;
64
+ return s.slice(start, end);
65
+ }
66
+ var S3Filesystem = class extends workspace.MastraFilesystem {
67
+ id;
68
+ name = "S3Filesystem";
69
+ provider = "s3";
70
+ readOnly;
71
+ status = "pending";
72
+ // Display metadata for UI
73
+ displayName;
74
+ icon = "s3";
75
+ description;
76
+ bucket;
77
+ region;
78
+ accessKeyId;
79
+ secretAccessKey;
80
+ endpoint;
81
+ forcePathStyle;
82
+ prefix;
83
+ _client = null;
84
+ constructor(options) {
85
+ super({ ...options, name: "S3Filesystem" });
86
+ this.id = options.id ?? `s3-fs-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
87
+ this.bucket = options.bucket;
88
+ this.region = options.region;
89
+ this.accessKeyId = options.accessKeyId;
90
+ this.secretAccessKey = options.secretAccessKey;
91
+ this.endpoint = options.endpoint;
92
+ this.forcePathStyle = options.forcePathStyle ?? !!options.endpoint;
93
+ this.prefix = options.prefix ? trimSlashes(options.prefix) + "/" : "";
94
+ this.icon = options.icon ?? this.detectIconFromEndpoint(options.endpoint);
95
+ this.displayName = options.displayName ?? this.getDefaultDisplayName(this.icon);
96
+ this.description = options.description;
97
+ this.readOnly = options.readOnly;
98
+ }
99
+ /**
100
+ * Get mount configuration for E2B sandbox.
101
+ * Returns S3-compatible config that works with s3fs-fuse.
102
+ */
103
+ getMountConfig() {
104
+ const config = {
105
+ type: "s3",
106
+ bucket: this.bucket,
107
+ region: this.region,
108
+ endpoint: this.endpoint
109
+ };
110
+ if (this.accessKeyId && this.secretAccessKey) {
111
+ config.accessKeyId = this.accessKeyId;
112
+ config.secretAccessKey = this.secretAccessKey;
113
+ }
114
+ if (this.readOnly) {
115
+ config.readOnly = true;
116
+ }
117
+ return config;
118
+ }
119
+ /**
120
+ * Get filesystem info for status reporting.
121
+ */
122
+ getInfo() {
123
+ return {
124
+ id: this.id,
125
+ name: this.name,
126
+ provider: this.provider,
127
+ status: this.status,
128
+ error: this.error,
129
+ readOnly: this.readOnly,
130
+ icon: this.icon,
131
+ metadata: {
132
+ bucket: this.bucket,
133
+ region: this.region,
134
+ ...this.endpoint && { endpoint: this.endpoint },
135
+ ...this.prefix && { prefix: this.prefix }
136
+ }
137
+ };
138
+ }
139
+ /**
140
+ * Handle an error, checking for access denied and updating status accordingly.
141
+ * Returns the error for re-throwing.
142
+ */
143
+ handleError(error) {
144
+ if (isAccessDeniedError(error)) {
145
+ this.status = "error";
146
+ this.error = "Access denied - check credentials and bucket permissions";
147
+ }
148
+ return error;
149
+ }
150
+ /**
151
+ * Get instructions describing this S3 filesystem.
152
+ * Used by agents to understand storage semantics.
153
+ */
154
+ getInstructions() {
155
+ const providerName = this.displayName || "S3";
156
+ const access = this.readOnly ? "Read-only" : "Persistent";
157
+ return `${providerName} storage in bucket "${this.bucket}". ${access} storage - files are retained across sessions.`;
158
+ }
159
+ /**
160
+ * Detect the appropriate icon based on the S3 endpoint.
161
+ */
162
+ detectIconFromEndpoint(endpoint) {
163
+ if (!endpoint) {
164
+ return "aws-s3";
165
+ }
166
+ let hostname;
167
+ try {
168
+ const url = new URL(endpoint);
169
+ hostname = url.hostname.toLowerCase();
170
+ } catch {
171
+ hostname = endpoint.toLowerCase();
172
+ }
173
+ if (hostname === "r2.cloudflarestorage.com" || hostname.endsWith(".r2.cloudflarestorage.com") || hostname.endsWith(".cloudflare.com")) {
174
+ return "r2";
175
+ }
176
+ if (hostname === "storage.googleapis.com" || hostname.endsWith(".storage.googleapis.com") || hostname.endsWith(".googleapis.com")) {
177
+ return "gcs";
178
+ }
179
+ if (hostname === "blob.core.windows.net" || hostname.endsWith(".blob.core.windows.net") || hostname.endsWith(".azure.com")) {
180
+ return "azure";
181
+ }
182
+ if (hostname.includes("minio")) {
183
+ return "minio";
184
+ }
185
+ return "s3";
186
+ }
187
+ /**
188
+ * Get a user-friendly display name based on the icon/provider.
189
+ */
190
+ getDefaultDisplayName(icon) {
191
+ switch (icon) {
192
+ case "aws-s3":
193
+ return "AWS S3";
194
+ case "r2":
195
+ case "cloudflare":
196
+ case "cloudflare-r2":
197
+ return "Cloudflare R2";
198
+ case "gcs":
199
+ case "google-cloud":
200
+ case "google-cloud-storage":
201
+ return "Google Cloud Storage";
202
+ case "azure":
203
+ case "azure-blob":
204
+ return "Azure Blob";
205
+ case "minio":
206
+ return "MinIO";
207
+ case "s3":
208
+ return "S3";
209
+ default:
210
+ return void 0;
211
+ }
212
+ }
213
+ getClient() {
214
+ if (this._client) return this._client;
215
+ const hasCredentials = this.accessKeyId && this.secretAccessKey;
216
+ this._client = new clientS3.S3Client({
217
+ region: this.region,
218
+ credentials: hasCredentials ? {
219
+ accessKeyId: this.accessKeyId,
220
+ secretAccessKey: this.secretAccessKey
221
+ } : (
222
+ // Anonymous access for public buckets - use empty credentials
223
+ // to prevent SDK from trying to find credentials elsewhere
224
+ { accessKeyId: "", secretAccessKey: "" }
225
+ ),
226
+ endpoint: this.endpoint,
227
+ forcePathStyle: this.forcePathStyle,
228
+ // Skip signing for anonymous access (public buckets).
229
+ // No-op signer passes the request through unsigned. Uses `any` because
230
+ // the correct type (HttpRequest from @smithy/types) is not a direct dependency.
231
+ ...hasCredentials ? {} : { signer: { sign: async (request) => request } }
232
+ });
233
+ return this._client;
234
+ }
235
+ /**
236
+ * Ensure the filesystem is initialized and return the S3 client.
237
+ * Uses base class ensureReady() for status management, then returns client.
238
+ */
239
+ async getReadyClient() {
240
+ await this.ensureReady();
241
+ return this.getClient();
242
+ }
243
+ toKey(path) {
244
+ const cleanPath = path.replace(/^\/+/, "");
245
+ return this.prefix + cleanPath;
246
+ }
247
+ // ---------------------------------------------------------------------------
248
+ // File Operations
249
+ // ---------------------------------------------------------------------------
250
+ async readFile(path, options) {
251
+ const client = await this.getReadyClient();
252
+ try {
253
+ const response = await client.send(
254
+ new clientS3.GetObjectCommand({
255
+ Bucket: this.bucket,
256
+ Key: this.toKey(path)
257
+ })
258
+ );
259
+ const body = await response.Body?.transformToByteArray();
260
+ if (!body) throw new workspace.FileNotFoundError(path);
261
+ const buffer = Buffer.from(body);
262
+ if (options?.encoding) {
263
+ return buffer.toString(options.encoding);
264
+ }
265
+ return buffer;
266
+ } catch (error) {
267
+ if (isNotFoundError(error)) {
268
+ throw new workspace.FileNotFoundError(path);
269
+ }
270
+ throw this.handleError(error);
271
+ }
272
+ }
273
+ async writeFile(path, content, options) {
274
+ const client = await this.getReadyClient();
275
+ if (options?.overwrite === false && await this.exists(path)) {
276
+ throw new workspace.FileExistsError(path);
277
+ }
278
+ const body = typeof content === "string" ? Buffer.from(content, "utf-8") : Buffer.from(content);
279
+ const contentType = getMimeType(path);
280
+ await client.send(
281
+ new clientS3.PutObjectCommand({
282
+ Bucket: this.bucket,
283
+ Key: this.toKey(path),
284
+ Body: body,
285
+ ContentType: contentType
286
+ })
287
+ );
288
+ }
289
+ async appendFile(path, content) {
290
+ let existing = "";
291
+ try {
292
+ existing = await this.readFile(path, { encoding: "utf-8" });
293
+ } catch (error) {
294
+ if (error instanceof workspace.FileNotFoundError) ; else {
295
+ throw error;
296
+ }
297
+ }
298
+ const appendContent = typeof content === "string" ? content : Buffer.from(content).toString("utf-8");
299
+ await this.writeFile(path, existing + appendContent);
300
+ }
301
+ async deleteFile(path, options) {
302
+ const isDir = await this.isDirectory(path);
303
+ if (isDir) {
304
+ await this.rmdir(path, { recursive: true, force: options?.force });
305
+ return;
306
+ }
307
+ const client = await this.getReadyClient();
308
+ try {
309
+ await client.send(
310
+ new clientS3.DeleteObjectCommand({
311
+ Bucket: this.bucket,
312
+ Key: this.toKey(path)
313
+ })
314
+ );
315
+ } catch (error) {
316
+ if (options?.force) return;
317
+ if (isNotFoundError(error)) {
318
+ throw new workspace.FileNotFoundError(path);
319
+ }
320
+ throw this.handleError(error);
321
+ }
322
+ }
323
+ async copyFile(src, dest, options) {
324
+ const client = await this.getReadyClient();
325
+ if (options?.overwrite === false && await this.exists(dest)) {
326
+ throw new workspace.FileExistsError(dest);
327
+ }
328
+ try {
329
+ await client.send(
330
+ new clientS3.CopyObjectCommand({
331
+ Bucket: this.bucket,
332
+ CopySource: `${this.bucket}/${encodeURIComponent(this.toKey(src)).replace(/%2F/g, "/")}`,
333
+ Key: this.toKey(dest)
334
+ })
335
+ );
336
+ } catch (error) {
337
+ if (isNotFoundError(error)) {
338
+ throw new workspace.FileNotFoundError(src);
339
+ }
340
+ throw this.handleError(error);
341
+ }
342
+ }
343
+ async moveFile(src, dest, options) {
344
+ await this.copyFile(src, dest, options);
345
+ await this.deleteFile(src, { force: true });
346
+ }
347
+ // ---------------------------------------------------------------------------
348
+ // Directory Operations
349
+ // ---------------------------------------------------------------------------
350
+ async mkdir(_path, _options) {
351
+ }
352
+ async rmdir(path, options) {
353
+ if (!options?.recursive) {
354
+ const entries = await this.readdir(path);
355
+ if (entries.length > 0) {
356
+ throw new Error(`Directory not empty: ${path}`);
357
+ }
358
+ return;
359
+ }
360
+ const client = await this.getReadyClient();
361
+ const prefix = this.toKey(path).replace(/\/$/, "") + "/";
362
+ let continuationToken;
363
+ do {
364
+ const listResponse = await client.send(
365
+ new clientS3.ListObjectsV2Command({
366
+ Bucket: this.bucket,
367
+ Prefix: prefix,
368
+ ContinuationToken: continuationToken
369
+ })
370
+ );
371
+ if (listResponse.Contents && listResponse.Contents.length > 0) {
372
+ const deleteResponse = await client.send(
373
+ new clientS3.DeleteObjectsCommand({
374
+ Bucket: this.bucket,
375
+ Delete: {
376
+ Objects: listResponse.Contents.filter((obj) => !!obj.Key).map((obj) => ({
377
+ Key: obj.Key
378
+ }))
379
+ }
380
+ })
381
+ );
382
+ if (deleteResponse.Errors && deleteResponse.Errors.length > 0) {
383
+ throw new Error(`Failed to delete ${deleteResponse.Errors.length} object(s) in ${path}`);
384
+ }
385
+ }
386
+ continuationToken = listResponse.NextContinuationToken;
387
+ } while (continuationToken);
388
+ }
389
+ async readdir(path, options) {
390
+ const client = await this.getReadyClient();
391
+ const prefix = this.toKey(path).replace(/\/$/, "");
392
+ const searchPrefix = prefix ? prefix + "/" : "";
393
+ const entries = [];
394
+ const seenDirs = /* @__PURE__ */ new Set();
395
+ let continuationToken;
396
+ do {
397
+ const response = await client.send(
398
+ new clientS3.ListObjectsV2Command({
399
+ Bucket: this.bucket,
400
+ Prefix: searchPrefix,
401
+ Delimiter: options?.recursive ? void 0 : "/",
402
+ ContinuationToken: continuationToken
403
+ })
404
+ );
405
+ if (response.Contents) {
406
+ for (const obj of response.Contents) {
407
+ const key = obj.Key;
408
+ if (!key || key === searchPrefix) continue;
409
+ const relativePath = key.slice(searchPrefix.length);
410
+ if (!relativePath) continue;
411
+ if (relativePath.endsWith("/")) {
412
+ const dirName = relativePath.slice(0, -1);
413
+ if (!seenDirs.has(dirName)) {
414
+ seenDirs.add(dirName);
415
+ entries.push({ name: dirName, type: "directory" });
416
+ }
417
+ continue;
418
+ }
419
+ const name = options?.recursive ? relativePath : relativePath.split("/")[0];
420
+ if (!name) continue;
421
+ if (options?.extension) {
422
+ const extensions = Array.isArray(options.extension) ? options.extension : [options.extension];
423
+ if (!extensions.some((ext) => name.endsWith(ext))) {
424
+ continue;
425
+ }
426
+ }
427
+ entries.push({
428
+ name,
429
+ type: "file",
430
+ size: obj.Size
431
+ });
432
+ }
433
+ }
434
+ if (response.CommonPrefixes) {
435
+ for (const prefixObj of response.CommonPrefixes) {
436
+ if (!prefixObj.Prefix) continue;
437
+ const dirName = prefixObj.Prefix.slice(searchPrefix.length).replace(/\/$/, "");
438
+ if (dirName && !seenDirs.has(dirName)) {
439
+ seenDirs.add(dirName);
440
+ entries.push({ name: dirName, type: "directory" });
441
+ }
442
+ }
443
+ }
444
+ continuationToken = response.NextContinuationToken;
445
+ } while (continuationToken);
446
+ return entries;
447
+ }
448
+ // ---------------------------------------------------------------------------
449
+ // Path Operations
450
+ // ---------------------------------------------------------------------------
451
+ async exists(path) {
452
+ const key = this.toKey(path);
453
+ if (!key) return true;
454
+ const client = await this.getReadyClient();
455
+ try {
456
+ await client.send(
457
+ new clientS3.HeadObjectCommand({
458
+ Bucket: this.bucket,
459
+ Key: key
460
+ })
461
+ );
462
+ return true;
463
+ } catch (error) {
464
+ if (!isNotFoundError(error)) throw this.handleError(error);
465
+ }
466
+ const response = await client.send(
467
+ new clientS3.ListObjectsV2Command({
468
+ Bucket: this.bucket,
469
+ Prefix: key.replace(/\/$/, "") + "/",
470
+ MaxKeys: 1
471
+ })
472
+ );
473
+ return (response.Contents?.length ?? 0) > 0;
474
+ }
475
+ async stat(path) {
476
+ const key = this.toKey(path);
477
+ if (!key) {
478
+ return {
479
+ name: "",
480
+ path,
481
+ type: "directory",
482
+ size: 0,
483
+ createdAt: /* @__PURE__ */ new Date(),
484
+ modifiedAt: /* @__PURE__ */ new Date()
485
+ };
486
+ }
487
+ const client = await this.getReadyClient();
488
+ try {
489
+ const response = await client.send(
490
+ new clientS3.HeadObjectCommand({
491
+ Bucket: this.bucket,
492
+ Key: key
493
+ })
494
+ );
495
+ const name = path.split("/").pop() ?? "";
496
+ return {
497
+ name,
498
+ path,
499
+ type: "file",
500
+ size: response.ContentLength ?? 0,
501
+ createdAt: response.LastModified ?? /* @__PURE__ */ new Date(),
502
+ modifiedAt: response.LastModified ?? /* @__PURE__ */ new Date()
503
+ };
504
+ } catch (error) {
505
+ if (!isNotFoundError(error)) throw this.handleError(error);
506
+ const isDir = await this.isDirectory(path);
507
+ if (isDir) {
508
+ const name = path.split("/").filter(Boolean).pop() ?? "";
509
+ return {
510
+ name,
511
+ path,
512
+ type: "directory",
513
+ size: 0,
514
+ createdAt: /* @__PURE__ */ new Date(),
515
+ modifiedAt: /* @__PURE__ */ new Date()
516
+ };
517
+ }
518
+ throw new workspace.FileNotFoundError(path);
519
+ }
520
+ }
521
+ async isFile(path) {
522
+ const key = this.toKey(path);
523
+ if (!key) return false;
524
+ const client = await this.getReadyClient();
525
+ try {
526
+ await client.send(
527
+ new clientS3.HeadObjectCommand({
528
+ Bucket: this.bucket,
529
+ Key: key
530
+ })
531
+ );
532
+ return true;
533
+ } catch (error) {
534
+ if (!isNotFoundError(error)) throw this.handleError(error);
535
+ return false;
536
+ }
537
+ }
538
+ async isDirectory(path) {
539
+ const key = this.toKey(path);
540
+ if (!key) return true;
541
+ const client = await this.getReadyClient();
542
+ const response = await client.send(
543
+ new clientS3.ListObjectsV2Command({
544
+ Bucket: this.bucket,
545
+ Prefix: key.replace(/\/$/, "") + "/",
546
+ MaxKeys: 1
547
+ })
548
+ );
549
+ return (response.Contents?.length ?? 0) > 0;
550
+ }
551
+ // ---------------------------------------------------------------------------
552
+ // Lifecycle (overrides base class protected methods)
553
+ // ---------------------------------------------------------------------------
554
+ /**
555
+ * Initialize the S3 client.
556
+ * Status management is handled by the base class.
557
+ */
558
+ async init() {
559
+ const client = this.getClient();
560
+ try {
561
+ await client.send(new clientS3.HeadBucketCommand({ Bucket: this.bucket }));
562
+ } catch (error) {
563
+ const statusCode = error.$metadata?.httpStatusCode;
564
+ const createError = (message2) => {
565
+ const err = new Error(message2);
566
+ if (statusCode) err.status = statusCode;
567
+ return err;
568
+ };
569
+ if (isAccessDeniedError(error)) {
570
+ throw createError(`Access denied to bucket "${this.bucket}" - check credentials and permissions`);
571
+ }
572
+ if (isNotFoundError(error)) {
573
+ throw createError(`Bucket "${this.bucket}" not found`);
574
+ }
575
+ const message = error instanceof Error ? error.message : String(error);
576
+ if (statusCode) {
577
+ throw createError(`Failed to access bucket "${this.bucket}" (HTTP ${statusCode}): ${message}`);
578
+ }
579
+ throw error;
580
+ }
581
+ }
582
+ /**
583
+ * Clean up the S3 client.
584
+ * Status management is handled by the base class.
585
+ */
586
+ async destroy() {
587
+ this._client = null;
588
+ }
589
+ };
590
+
591
+ exports.S3Filesystem = S3Filesystem;
592
+ //# sourceMappingURL=index.cjs.map
593
+ //# sourceMappingURL=index.cjs.map