@blamejs/core 0.4.23 → 0.4.25

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/CHANGELOG.md CHANGED
@@ -8,6 +8,8 @@ upgrading across more than a few patches at a time.
8
8
 
9
9
  ## v0.4.x
10
10
 
11
+ - **0.4.24** (2026-04-30) — b.objectStore: multipart upload + server-side encryption
12
+ - **0.4.23** (2026-04-30) — b.mail.dkim signing + calendar invites
11
13
  - **0.4.22** (2026-04-30) — b.mail attachments + inline images + plain/HTML alternatives
12
14
  - **0.4.21** (2026-04-30) — b.queue: repeat-in-queue (cron) + parent-child Flows
13
15
  - **0.4.20** (2026-04-30) — b.queue + b.jobs: priority, rate-limit, progress
@@ -30,6 +30,7 @@
30
30
  var localProto = require("./local");
31
31
  var httpPutProto = require("./http-put");
32
32
  var sigv4Proto = require("./sigv4");
33
+ var sigv4BucketOps = require("./sigv4-bucket-ops");
33
34
  var gcsProto = require("./gcs");
34
35
  var azureBlobProto = require("./azure-blob");
35
36
  var retryHelper = require("./retry");
@@ -137,4 +138,9 @@ module.exports = {
137
138
  buildBackend: buildBackend,
138
139
  PROTOCOLS: dispatcher.protocols,
139
140
  DEFERRED_PROTOCOLS: dispatcher.deferred,
141
+ // Bucket-level (lifecycle / CORS / create / delete / list) ops are
142
+ // service-scoped, not bucket-scoped — they get their own factory.
143
+ // SigV4 only; GCS / Azure bucket lifecycle differs substantially per
144
+ // cloud and is operator-managed (Terraform / CDK / Pulumi).
145
+ bucketOps: sigv4BucketOps,
140
146
  };
@@ -0,0 +1,491 @@
1
+ "use strict";
2
+ /**
3
+ * sigv4-bucket-ops — bucket-level operations for SigV4 backends.
4
+ *
5
+ * Per-object ops (put / get / list / delete / multipart) live in
6
+ * lib/object-store/sigv4.js and are bound to a single bucket at
7
+ * create() time. Bucket lifecycle ops are at a different level —
8
+ * they need a service-scoped client that addresses arbitrary
9
+ * buckets — so they get their own factory.
10
+ *
11
+ * Operators with multi-cloud ambitions reach for Terraform / CDK /
12
+ * Pulumi. The framework's bucket-ops surface is the operator-from-app
13
+ * path: create the bucket your app needs at boot, attach a lifecycle
14
+ * rule that aborts incomplete multiparts after a week, etc. Niche ops
15
+ * (Object Lock, Replication, Inventory, Notification) are deferred —
16
+ * they're well into Terraform territory.
17
+ *
18
+ * Public API:
19
+ *
20
+ * var ops = b.objectStore.bucketOps.create({
21
+ * protocol: "sigv4",
22
+ * region: "us-east-1",
23
+ * accessKeyId: env("AWS_ACCESS_KEY_ID"),
24
+ * secretAccessKey: env("AWS_SECRET_ACCESS_KEY"),
25
+ * endpoint: "https://s3.us-east-1.amazonaws.com", // optional
26
+ * pathStyle: false,
27
+ * timeoutMs: 30000,
28
+ * });
29
+ *
30
+ * await ops.create("my-bucket", { region: "eu-west-1" });
31
+ * await ops.delete("my-bucket");
32
+ * var buckets = await ops.list(); // [{ name, creationDate }]
33
+ * await ops.setLifecycle("my-bucket", [{
34
+ * id: "abort-stale-multiparts",
35
+ * status: "Enabled",
36
+ * prefix: "",
37
+ * abortIncompleteMultipartUpload: { daysAfterInitiation: 7 },
38
+ * }]);
39
+ * await ops.setCorsRules("my-bucket", [{
40
+ * allowedOrigins: ["https://app.example.com"],
41
+ * allowedMethods: ["GET", "PUT", "POST"],
42
+ * allowedHeaders: ["*"],
43
+ * exposeHeaders: ["ETag"],
44
+ * maxAgeSeconds: 3600,
45
+ * }]);
46
+ *
47
+ * Validation is Tier-A: every input shape is rejected at the call
48
+ * site rather than producing a server-side 400. Errors surface as
49
+ * ObjectStoreError with codes (BUCKET_INVALID_NAME, INVALID_LIFECYCLE,
50
+ * INVALID_CORS_RULE, BUCKET_ALREADY_OWNED, BUCKET_NOT_EMPTY, etc.).
51
+ */
52
+ var { URL } = require("url");
53
+ var nodeCrypto = require("crypto");
54
+ var sigv4 = require("./sigv4");
55
+ var safeXml = require("../parsers/safe-xml");
56
+ var safeUrl = require("../safe-url");
57
+ var httpClient = require("../http-client");
58
+ var { ObjectStoreError } = require("../framework-error");
59
+
60
+ var _err = ObjectStoreError.factory;
61
+
62
+ // S3 bucket-name rules (general purpose). Source: AWS docs
63
+ // "Bucket naming rules". Lowercase letters, digits, hyphens; 3..63
64
+ // chars; no consecutive dots; cannot end in -s3alias / -ol-s3 etc.
65
+ // We catch the common-mistake cases at config time; AWS catches the
66
+ // rest at request time.
67
+ var BUCKET_NAME_RE = /^[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]$/;
68
+
69
+ function _validateBucketName(name) {
70
+ if (typeof name !== "string" || name.length < 3 || name.length > 63) {
71
+ throw _err("BUCKET_INVALID_NAME",
72
+ "bucket name must be a string of length 3..63, got " +
73
+ (typeof name === "string" ? "length " + name.length : typeof name), true);
74
+ }
75
+ if (!BUCKET_NAME_RE.test(name)) {
76
+ throw _err("BUCKET_INVALID_NAME",
77
+ "bucket name '" + name + "' violates S3 naming rules " +
78
+ "(lowercase, digits, hyphens, dots — no leading/trailing punct)", true);
79
+ }
80
+ if (name.indexOf("..") !== -1) {
81
+ throw _err("BUCKET_INVALID_NAME",
82
+ "bucket name '" + name + "' contains consecutive dots", true);
83
+ }
84
+ }
85
+
86
+ function _xmlEscape(s) {
87
+ return String(s)
88
+ .replace(/&/g, "&amp;")
89
+ .replace(/</g, "&lt;")
90
+ .replace(/>/g, "&gt;")
91
+ .replace(/"/g, "&quot;")
92
+ .replace(/'/g, "&apos;");
93
+ }
94
+
95
+ function _md5Base64(buf) {
96
+ return nodeCrypto.createHash("md5").update(buf).digest("base64");
97
+ }
98
+
99
+ // ---- Lifecycle XML ----
100
+
101
+ var ALLOWED_STORAGE_CLASSES = [
102
+ "STANDARD", "REDUCED_REDUNDANCY", "STANDARD_IA", "ONEZONE_IA",
103
+ "INTELLIGENT_TIERING", "GLACIER", "DEEP_ARCHIVE", "GLACIER_IR",
104
+ "EXPRESS_ONEZONE",
105
+ ];
106
+
107
+ function _buildLifecycleXml(rules) {
108
+ if (!Array.isArray(rules) || rules.length === 0) {
109
+ throw _err("INVALID_LIFECYCLE",
110
+ "setLifecycle: rules must be a non-empty array", true);
111
+ }
112
+ if (rules.length > 1000) {
113
+ throw _err("INVALID_LIFECYCLE",
114
+ "setLifecycle: maximum 1000 rules per bucket (S3 spec)", true);
115
+ }
116
+ var body = '<?xml version="1.0" encoding="UTF-8"?>';
117
+ body += '<LifecycleConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">';
118
+ for (var i = 0; i < rules.length; i++) {
119
+ var rule = rules[i];
120
+ if (!rule || typeof rule !== "object") {
121
+ throw _err("INVALID_LIFECYCLE",
122
+ "rules[" + i + "] must be an object", true);
123
+ }
124
+ var status = rule.status || "Enabled";
125
+ if (status !== "Enabled" && status !== "Disabled") {
126
+ throw _err("INVALID_LIFECYCLE",
127
+ "rules[" + i + "].status must be 'Enabled' or 'Disabled'", true);
128
+ }
129
+ if (!rule.expiration && !rule.transition && !rule.abortIncompleteMultipartUpload) {
130
+ throw _err("INVALID_LIFECYCLE",
131
+ "rules[" + i + "] must specify at least one of " +
132
+ "expiration / transition / abortIncompleteMultipartUpload", true);
133
+ }
134
+ body += "<Rule>";
135
+ if (rule.id !== undefined) {
136
+ if (typeof rule.id !== "string" || rule.id.length === 0) {
137
+ throw _err("INVALID_LIFECYCLE",
138
+ "rules[" + i + "].id must be a non-empty string when set", true);
139
+ }
140
+ body += "<ID>" + _xmlEscape(rule.id) + "</ID>";
141
+ }
142
+ body += "<Filter><Prefix>" + _xmlEscape(rule.prefix || "") + "</Prefix></Filter>";
143
+ body += "<Status>" + status + "</Status>";
144
+ if (rule.expiration) {
145
+ body += "<Expiration>";
146
+ if (rule.expiration.days !== undefined) {
147
+ if (typeof rule.expiration.days !== "number" || rule.expiration.days < 1) {
148
+ throw _err("INVALID_LIFECYCLE",
149
+ "rules[" + i + "].expiration.days must be a positive integer", true);
150
+ }
151
+ body += "<Days>" + rule.expiration.days + "</Days>";
152
+ }
153
+ if (rule.expiration.date !== undefined) {
154
+ body += "<Date>" + _xmlEscape(rule.expiration.date) + "</Date>";
155
+ }
156
+ if (rule.expiration.expiredObjectDeleteMarker !== undefined) {
157
+ body += "<ExpiredObjectDeleteMarker>" +
158
+ (rule.expiration.expiredObjectDeleteMarker ? "true" : "false") +
159
+ "</ExpiredObjectDeleteMarker>";
160
+ }
161
+ body += "</Expiration>";
162
+ }
163
+ if (rule.transition) {
164
+ if (ALLOWED_STORAGE_CLASSES.indexOf(rule.transition.storageClass) === -1) {
165
+ throw _err("INVALID_LIFECYCLE",
166
+ "rules[" + i + "].transition.storageClass must be one of: " +
167
+ ALLOWED_STORAGE_CLASSES.join(", "), true);
168
+ }
169
+ body += "<Transition>";
170
+ if (rule.transition.days !== undefined) {
171
+ body += "<Days>" + rule.transition.days + "</Days>";
172
+ }
173
+ if (rule.transition.date !== undefined) {
174
+ body += "<Date>" + _xmlEscape(rule.transition.date) + "</Date>";
175
+ }
176
+ body += "<StorageClass>" + rule.transition.storageClass + "</StorageClass>";
177
+ body += "</Transition>";
178
+ }
179
+ if (rule.abortIncompleteMultipartUpload) {
180
+ var dai = rule.abortIncompleteMultipartUpload.daysAfterInitiation;
181
+ if (typeof dai !== "number" || dai < 1) {
182
+ throw _err("INVALID_LIFECYCLE",
183
+ "rules[" + i + "].abortIncompleteMultipartUpload.daysAfterInitiation " +
184
+ "must be a positive integer", true);
185
+ }
186
+ body += "<AbortIncompleteMultipartUpload>";
187
+ body += "<DaysAfterInitiation>" + dai + "</DaysAfterInitiation>";
188
+ body += "</AbortIncompleteMultipartUpload>";
189
+ }
190
+ body += "</Rule>";
191
+ }
192
+ body += "</LifecycleConfiguration>";
193
+ return body;
194
+ }
195
+
196
+ // ---- CORS XML ----
197
+
198
+ var ALLOWED_CORS_METHODS = ["GET", "PUT", "POST", "DELETE", "HEAD"];
199
+
200
+ function _buildCorsXml(rules) {
201
+ if (!Array.isArray(rules) || rules.length === 0) {
202
+ throw _err("INVALID_CORS_RULE",
203
+ "setCorsRules: rules must be a non-empty array", true);
204
+ }
205
+ if (rules.length > 100) {
206
+ throw _err("INVALID_CORS_RULE",
207
+ "setCorsRules: maximum 100 rules per bucket (S3 spec)", true);
208
+ }
209
+ var body = '<?xml version="1.0" encoding="UTF-8"?>';
210
+ body += '<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">';
211
+ for (var i = 0; i < rules.length; i++) {
212
+ var rule = rules[i];
213
+ if (!rule || typeof rule !== "object") {
214
+ throw _err("INVALID_CORS_RULE",
215
+ "rules[" + i + "] must be an object", true);
216
+ }
217
+ if (!Array.isArray(rule.allowedOrigins) || rule.allowedOrigins.length === 0) {
218
+ throw _err("INVALID_CORS_RULE",
219
+ "rules[" + i + "].allowedOrigins must be a non-empty array", true);
220
+ }
221
+ if (!Array.isArray(rule.allowedMethods) || rule.allowedMethods.length === 0) {
222
+ throw _err("INVALID_CORS_RULE",
223
+ "rules[" + i + "].allowedMethods must be a non-empty array", true);
224
+ }
225
+ for (var m = 0; m < rule.allowedMethods.length; m++) {
226
+ if (ALLOWED_CORS_METHODS.indexOf(rule.allowedMethods[m]) === -1) {
227
+ throw _err("INVALID_CORS_RULE",
228
+ "rules[" + i + "].allowedMethods[" + m + "] must be one of: " +
229
+ ALLOWED_CORS_METHODS.join(", "), true);
230
+ }
231
+ }
232
+ body += "<CORSRule>";
233
+ if (rule.id !== undefined) body += "<ID>" + _xmlEscape(rule.id) + "</ID>";
234
+ rule.allowedOrigins.forEach(function (o) {
235
+ body += "<AllowedOrigin>" + _xmlEscape(o) + "</AllowedOrigin>";
236
+ });
237
+ rule.allowedMethods.forEach(function (m) {
238
+ body += "<AllowedMethod>" + _xmlEscape(m) + "</AllowedMethod>";
239
+ });
240
+ if (Array.isArray(rule.allowedHeaders)) {
241
+ rule.allowedHeaders.forEach(function (h) {
242
+ body += "<AllowedHeader>" + _xmlEscape(h) + "</AllowedHeader>";
243
+ });
244
+ }
245
+ if (Array.isArray(rule.exposeHeaders)) {
246
+ rule.exposeHeaders.forEach(function (h) {
247
+ body += "<ExposeHeader>" + _xmlEscape(h) + "</ExposeHeader>";
248
+ });
249
+ }
250
+ if (rule.maxAgeSeconds !== undefined) {
251
+ if (typeof rule.maxAgeSeconds !== "number" || rule.maxAgeSeconds < 0) {
252
+ throw _err("INVALID_CORS_RULE",
253
+ "rules[" + i + "].maxAgeSeconds must be a non-negative number", true);
254
+ }
255
+ body += "<MaxAgeSeconds>" + rule.maxAgeSeconds + "</MaxAgeSeconds>";
256
+ }
257
+ body += "</CORSRule>";
258
+ }
259
+ body += "</CORSConfiguration>";
260
+ if (Buffer.byteLength(body, "utf8") > 64 * 1024) {
261
+ throw _err("INVALID_CORS_RULE",
262
+ "CORS configuration exceeds 64 KB (S3 spec)", true);
263
+ }
264
+ return body;
265
+ }
266
+
267
+ // ---- Public factory ----
268
+
269
+ function create(config) {
270
+ if (!config || typeof config !== "object") {
271
+ throw _err("INVALID_CONFIG", "bucketOps.create requires a config object", true);
272
+ }
273
+ if (config.protocol && config.protocol !== "sigv4") {
274
+ throw _err("INVALID_CONFIG",
275
+ "bucketOps currently only supports protocol 'sigv4'; got '" +
276
+ config.protocol + "'. GCS and Azure bucket lifecycle differs " +
277
+ "substantially per cloud and is operator-managed (Terraform / " +
278
+ "CDK / Pulumi).", true);
279
+ }
280
+ if (!config.region) throw _err("INVALID_CONFIG", "bucketOps: region is required", true);
281
+ if (!config.accessKeyId) throw _err("INVALID_CONFIG", "bucketOps: accessKeyId is required", true);
282
+ if (!config.secretAccessKey) throw _err("INVALID_CONFIG", "bucketOps: secretAccessKey is required", true);
283
+
284
+ var endpoint = config.endpoint || ("https://s3." + config.region + ".amazonaws.com");
285
+ if (endpoint.endsWith("/")) endpoint = endpoint.slice(0, -1);
286
+ var pathStyle = !!(config.pathStyle || config.forcePathStyle);
287
+ var allowedProtocols = config.allowedProtocols || safeUrl.ALLOW_HTTP_TLS;
288
+ var allowInternal = config.allowInternal != null ? config.allowInternal : null;
289
+ safeUrl.parse(endpoint, {
290
+ allowedProtocols: allowedProtocols,
291
+ errorClass: ObjectStoreError,
292
+ });
293
+ var reqOpts = { timeoutMs: config.timeoutMs, allowedProtocols: allowedProtocols };
294
+ if (allowInternal !== null) reqOpts.allowInternal = allowInternal;
295
+
296
+ function _bucketUrl(name, query) {
297
+ var u;
298
+ if (pathStyle) {
299
+ u = new URL(endpoint + "/" + name + "/");
300
+ } else {
301
+ u = new URL(endpoint);
302
+ u.hostname = name + "." + u.hostname;
303
+ u.pathname = "/";
304
+ }
305
+ if (query) {
306
+ Object.keys(query).forEach(function (k) {
307
+ u.searchParams.set(k, query[k] != null ? query[k] : "");
308
+ });
309
+ }
310
+ return u;
311
+ }
312
+
313
+ function _serviceUrl(query) {
314
+ // ListBuckets — service-level, no bucket prefix.
315
+ var u = new URL(endpoint);
316
+ u.pathname = "/";
317
+ if (query) {
318
+ Object.keys(query).forEach(function (k) { u.searchParams.set(k, query[k]); });
319
+ }
320
+ return u;
321
+ }
322
+
323
+ function _signed(method, url, payloadHash, extraHeaders) {
324
+ var signed = sigv4.signRequest({
325
+ method: method,
326
+ url: url,
327
+ headers: extraHeaders || {},
328
+ payloadHash: payloadHash,
329
+ region: config.region,
330
+ accessKeyId: config.accessKeyId,
331
+ secretAccessKey: config.secretAccessKey,
332
+ sessionToken: config.sessionToken,
333
+ });
334
+ return signed.headers;
335
+ }
336
+
337
+ function _request(method, url, headers, body) {
338
+ return httpClient.request({
339
+ method: method,
340
+ url: url,
341
+ headers: headers,
342
+ body: body,
343
+ idleTimeoutMs: reqOpts.timeoutMs,
344
+ errorClass: ObjectStoreError,
345
+ allowedProtocols: reqOpts.allowedProtocols,
346
+ ...((reqOpts.allowInternal !== undefined) ? { allowInternal: reqOpts.allowInternal } : {}),
347
+ });
348
+ }
349
+
350
+ // ---- create ----
351
+
352
+ function createBucket(name, opts) {
353
+ _validateBucketName(name);
354
+ opts = opts || {};
355
+ var targetRegion = opts.region || config.region;
356
+ var url = _bucketUrl(name);
357
+ var bodyBuf;
358
+ var extra = {};
359
+ // us-east-1 takes an empty body. Other regions need
360
+ // CreateBucketConfiguration with a LocationConstraint.
361
+ if (targetRegion && targetRegion !== "us-east-1") {
362
+ bodyBuf = Buffer.from(
363
+ '<?xml version="1.0" encoding="UTF-8"?>' +
364
+ '<CreateBucketConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">' +
365
+ '<LocationConstraint>' + _xmlEscape(targetRegion) + '</LocationConstraint>' +
366
+ '</CreateBucketConfiguration>',
367
+ "utf8"
368
+ );
369
+ extra["Content-Type"] = "application/xml";
370
+ extra["Content-Length"] = String(bodyBuf.length);
371
+ } else {
372
+ bodyBuf = Buffer.alloc(0);
373
+ extra["Content-Length"] = "0";
374
+ }
375
+ var payloadHash = sigv4.sha256Hex(bodyBuf);
376
+ var headers = _signed("PUT", url, payloadHash, extra);
377
+ return _request("PUT", url, headers, bodyBuf).then(
378
+ function () { return { created: true, name: name, region: targetRegion }; },
379
+ function (e) {
380
+ // Map S3 conflict response codes into stable framework codes.
381
+ if (e.statusCode === 409 && /BucketAlreadyOwnedByYou/.test(e.message || "")) {
382
+ throw _err("BUCKET_ALREADY_OWNED",
383
+ "bucket '" + name + "' already exists and is owned by this account", true);
384
+ }
385
+ if (e.statusCode === 409) {
386
+ throw _err("BUCKET_NAME_TAKEN",
387
+ "bucket name '" + name + "' is taken in S3's global namespace", true);
388
+ }
389
+ throw e;
390
+ }
391
+ );
392
+ }
393
+
394
+ // ---- delete ----
395
+
396
+ function deleteBucket(name) {
397
+ _validateBucketName(name);
398
+ var url = _bucketUrl(name);
399
+ var payloadHash = sigv4.sha256Hex(Buffer.alloc(0));
400
+ var headers = _signed("DELETE", url, payloadHash);
401
+ return _request("DELETE", url, headers, null).then(
402
+ function () { return true; },
403
+ function (e) {
404
+ if (e.statusCode === 404) return false;
405
+ if (e.statusCode === 409 && /BucketNotEmpty/.test(e.message || "")) {
406
+ throw _err("BUCKET_NOT_EMPTY",
407
+ "bucket '" + name + "' is not empty; delete all objects + " +
408
+ "noncurrent versions + delete-markers first", true);
409
+ }
410
+ throw e;
411
+ }
412
+ );
413
+ }
414
+
415
+ // ---- list ----
416
+
417
+ function listBuckets() {
418
+ var url = _serviceUrl();
419
+ var payloadHash = sigv4.sha256Hex(Buffer.alloc(0));
420
+ var headers = _signed("GET", url, payloadHash);
421
+ return _request("GET", url, headers, null).then(function (res) {
422
+ var doc = safeXml.parse(res.body);
423
+ var result = doc.ListAllMyBucketsResult || {};
424
+ var bucketsContainer = result.Buckets || {};
425
+ var raw = bucketsContainer.Bucket;
426
+ if (!raw) return [];
427
+ var arr = Array.isArray(raw) ? raw : [raw];
428
+ return arr.map(function (b) {
429
+ return {
430
+ name: b.Name,
431
+ creationDate: b.CreationDate ? Date.parse(b.CreationDate) : null,
432
+ region: b.BucketRegion || null,
433
+ };
434
+ });
435
+ });
436
+ }
437
+
438
+ // ---- setLifecycle ----
439
+
440
+ function setLifecycle(name, rules) {
441
+ _validateBucketName(name);
442
+ var bodyXml = _buildLifecycleXml(rules);
443
+ var bodyBuf = Buffer.from(bodyXml, "utf8");
444
+ var url = _bucketUrl(name, { lifecycle: "" });
445
+ var payloadHash = sigv4.sha256Hex(bodyBuf);
446
+ var headers = _signed("PUT", url, payloadHash, {
447
+ "Content-Type": "application/xml",
448
+ "Content-Length": String(bodyBuf.length),
449
+ "Content-MD5": _md5Base64(bodyBuf),
450
+ });
451
+ return _request("PUT", url, headers, bodyBuf).then(function () {
452
+ return { applied: true, name: name, ruleCount: rules.length };
453
+ });
454
+ }
455
+
456
+ // ---- setCorsRules ----
457
+
458
+ function setCorsRules(name, rules) {
459
+ _validateBucketName(name);
460
+ var bodyXml = _buildCorsXml(rules);
461
+ var bodyBuf = Buffer.from(bodyXml, "utf8");
462
+ var url = _bucketUrl(name, { cors: "" });
463
+ var payloadHash = sigv4.sha256Hex(bodyBuf);
464
+ var headers = _signed("PUT", url, payloadHash, {
465
+ "Content-Type": "application/xml",
466
+ "Content-Length": String(bodyBuf.length),
467
+ "Content-MD5": _md5Base64(bodyBuf),
468
+ });
469
+ return _request("PUT", url, headers, bodyBuf).then(function () {
470
+ return { applied: true, name: name, ruleCount: rules.length };
471
+ });
472
+ }
473
+
474
+ return {
475
+ protocol: "sigv4",
476
+ create: createBucket,
477
+ delete: deleteBucket,
478
+ list: listBuckets,
479
+ setLifecycle: setLifecycle,
480
+ setCorsRules: setCorsRules,
481
+ };
482
+ }
483
+
484
+ module.exports = {
485
+ create: create,
486
+ // Test-only exports for unit-testing the XML builders without
487
+ // standing up a fake S3 server.
488
+ _buildLifecycleXmlForTest: _buildLifecycleXml,
489
+ _buildCorsXmlForTest: _buildCorsXml,
490
+ _validateBucketNameForTest: _validateBucketName,
491
+ };
@@ -208,6 +208,135 @@ function _request(method, url, headers, body, opts) {
208
208
  });
209
209
  }
210
210
 
211
+ // ---- Multipart-upload constants ----
212
+
213
+ // S3 spec floor for non-final part size. Below this the API rejects
214
+ // CompleteMultipartUpload with EntityTooSmall. The framework refuses
215
+ // configurations below this floor at create() time so operators don't
216
+ // see surprising failures only on large uploads.
217
+ var MIN_PART_SIZE_BYTES = 5 * 1024 * 1024;
218
+ // S3 spec ceiling on part count. CompleteMultipartUpload rejects
219
+ // uploads with more than 10000 parts.
220
+ var MAX_PARTS = 10000;
221
+ // Auto-multipart trigger: buffered bodies under this stay single-PUT.
222
+ // Streams always go multipart since size isn't known up-front.
223
+ var DEFAULT_MULTIPART_THRESHOLD_BYTES = 64 * 1024 * 1024;
224
+ // Conservative default part size — large enough to keep round-trip
225
+ // overhead small relative to payload, small enough to fit comfortably
226
+ // in a 4-way-concurrent upload's memory footprint.
227
+ var DEFAULT_PART_SIZE_BYTES = 16 * 1024 * 1024;
228
+ var DEFAULT_PART_CONCURRENCY = 4;
229
+
230
+ // ---- SSE option handling ----
231
+
232
+ function _resolveSseHeaders(sse) {
233
+ if (sse === undefined || sse === null) return null;
234
+ var type;
235
+ var keyId = null;
236
+ if (typeof sse === "string") {
237
+ type = sse;
238
+ } else if (sse && typeof sse === "object") {
239
+ type = sse.type;
240
+ keyId = sse.keyId || null;
241
+ } else {
242
+ throw _err("INVALID_SSE",
243
+ "opts.sse must be a string ('AES256' | 'aws:kms') or " +
244
+ "{ type, keyId }, got " + typeof sse, true);
245
+ }
246
+ if (type !== "AES256" && type !== "aws:kms") {
247
+ throw _err("INVALID_SSE",
248
+ "opts.sse type must be 'AES256' or 'aws:kms', got '" + type + "'", true);
249
+ }
250
+ var h = { "x-amz-server-side-encryption": type };
251
+ if (type === "aws:kms" && keyId) {
252
+ h["x-amz-server-side-encryption-aws-kms-key-id"] = String(keyId);
253
+ }
254
+ return { type: type, keyId: keyId, headers: h };
255
+ }
256
+
257
+ function _verifySseResponse(sseRequested, resHeaders) {
258
+ // Operators who specified an SSE policy expect the bucket / object to
259
+ // honor it. If the server silently dropped the header (mis-configured
260
+ // bucket policy, unsupported endpoint, etc.) the request looks like a
261
+ // success but the at-rest data is unencrypted. Surface this as a
262
+ // hard failure rather than a silent compliance hole.
263
+ if (!sseRequested) return;
264
+ var got = resHeaders["x-amz-server-side-encryption"];
265
+ if (!got) {
266
+ throw _err("SSE_NOT_APPLIED",
267
+ "opts.sse was '" + sseRequested.type + "' but server did not " +
268
+ "apply server-side encryption (no x-amz-server-side-encryption " +
269
+ "response header)", true);
270
+ }
271
+ if (got !== sseRequested.type) {
272
+ throw _err("SSE_MISMATCH",
273
+ "opts.sse requested '" + sseRequested.type + "' but server " +
274
+ "applied '" + got + "'", true);
275
+ }
276
+ }
277
+
278
+ // ---- Multipart helpers ----
279
+
280
+ // Build the CompleteMultipartUpload request body. Parts must be in
281
+ // ascending PartNumber order. ETags from S3 include surrounding
282
+ // quotes — preserve them exactly.
283
+ function _buildCompleteMultipartXml(parts) {
284
+ var body = "<CompleteMultipartUpload>";
285
+ for (var i = 0; i < parts.length; i++) {
286
+ body += "<Part>";
287
+ body += "<PartNumber>" + parts[i].partNumber + "</PartNumber>";
288
+ body += "<ETag>" + parts[i].etag + "</ETag>";
289
+ body += "</Part>";
290
+ }
291
+ body += "</CompleteMultipartUpload>";
292
+ return body;
293
+ }
294
+
295
+ // Read a Readable stream into fixed-size buffers. Yields one Buffer
296
+ // per part (size <= partSize). The final part may be smaller. The
297
+ // stream is consumed exactly once.
298
+ async function _readStreamParts(readable, partSize) {
299
+ var parts = [];
300
+ var pending = [];
301
+ var pendingBytes = 0;
302
+ for await (var chunk of readable) {
303
+ var buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
304
+ pending.push(buf);
305
+ pendingBytes += buf.length;
306
+ while (pendingBytes >= partSize) {
307
+ var combined = Buffer.concat(pending, pendingBytes);
308
+ parts.push(combined.slice(0, partSize));
309
+ var leftover = combined.slice(partSize);
310
+ pending = leftover.length > 0 ? [leftover] : [];
311
+ pendingBytes = leftover.length;
312
+ }
313
+ }
314
+ if (pendingBytes > 0) {
315
+ parts.push(Buffer.concat(pending, pendingBytes));
316
+ }
317
+ return parts;
318
+ }
319
+
320
+ // Run an array of async tasks with bounded parallelism. Preserves
321
+ // result order by index.
322
+ async function _bounded(items, concurrency, runner) {
323
+ var results = new Array(items.length);
324
+ var i = 0;
325
+ async function worker() {
326
+ while (true) {
327
+ var idx = i++;
328
+ if (idx >= items.length) return;
329
+ results[idx] = await runner(items[idx], idx);
330
+ }
331
+ }
332
+ var workers = [];
333
+ for (var w = 0; w < Math.min(concurrency, items.length); w++) {
334
+ workers.push(worker());
335
+ }
336
+ await Promise.all(workers);
337
+ return results;
338
+ }
339
+
211
340
  // ---- Public adapter factory ----
212
341
 
213
342
  function create(config) {
@@ -220,6 +349,30 @@ function create(config) {
220
349
  var endpoint = config.endpoint || ("https://s3." + config.region + ".amazonaws.com");
221
350
  if (endpoint.endsWith("/")) endpoint = endpoint.slice(0, -1);
222
351
  var pathStyle = !!(config.pathStyle || config.forcePathStyle);
352
+
353
+ var partSize = config.partSizeBytes != null
354
+ ? config.partSizeBytes
355
+ : DEFAULT_PART_SIZE_BYTES;
356
+ if (typeof partSize !== "number" || !isFinite(partSize) || partSize < MIN_PART_SIZE_BYTES) {
357
+ throw _err("INVALID_CONFIG",
358
+ "sigv4: partSizeBytes must be a number >= " + MIN_PART_SIZE_BYTES +
359
+ " (S3 minimum part size), got " + partSize, true);
360
+ }
361
+ var multipartThreshold = config.multipartThresholdBytes != null
362
+ ? config.multipartThresholdBytes
363
+ : DEFAULT_MULTIPART_THRESHOLD_BYTES;
364
+ if (typeof multipartThreshold !== "number" || !isFinite(multipartThreshold) || multipartThreshold < 0) {
365
+ throw _err("INVALID_CONFIG",
366
+ "sigv4: multipartThresholdBytes must be a non-negative finite number, got " +
367
+ multipartThreshold, true);
368
+ }
369
+ var partConcurrency = config.partConcurrency != null
370
+ ? config.partConcurrency
371
+ : DEFAULT_PART_CONCURRENCY;
372
+ if (typeof partConcurrency !== "number" || partConcurrency < 1 || !isFinite(partConcurrency)) {
373
+ throw _err("INVALID_CONFIG",
374
+ "sigv4: partConcurrency must be a positive finite number, got " + partConcurrency, true);
375
+ }
223
376
  // HTTPS-only by default — AWS S3, R2, MinIO-over-https. Operators with
224
377
  // an internal cleartext S3-compatible endpoint (test fixtures, local
225
378
  // dev MinIO) opt in via config.allowedProtocols.
@@ -274,19 +427,150 @@ function create(config) {
274
427
  }
275
428
 
276
429
  function put(key, body, opts) {
277
- var url = _keyToUrl(key);
430
+ opts = opts || {};
431
+ var sseRequested = _resolveSseHeaders(opts.sse);
432
+ // Streams always go multipart — size isn't known up-front. Buffers
433
+ // dispatch to multipart when they exceed the threshold; operators
434
+ // can force the single-PUT path with `multipart: false` (small
435
+ // bodies in unit tests, or to keep the request count to one).
436
+ var isStream = body && typeof body === "object" && typeof body.pipe === "function";
437
+ if (isStream) {
438
+ if (opts.multipart === false) {
439
+ return Promise.reject(_err("STREAM_REQUIRES_MULTIPART",
440
+ "put(stream) requires multipart upload (set opts.multipart !== false)", true));
441
+ }
442
+ return _multipartPut(key, body, opts, sseRequested);
443
+ }
278
444
  var buf = Buffer.isBuffer(body) ? body : Buffer.from(typeof body === "string" ? body : "", "utf8");
445
+ if (opts.multipart !== false &&
446
+ (opts.multipart === true || buf.length > multipartThreshold)) {
447
+ return _multipartPut(key, buf, opts, sseRequested);
448
+ }
449
+ return _singlePut(key, buf, opts, sseRequested);
450
+ }
451
+
452
+ function _singlePut(key, buf, opts, sseRequested) {
453
+ var url = _keyToUrl(key);
279
454
  var payloadHash = sha256Hex(buf);
280
- var contentType = (opts && opts.contentType) || "application/octet-stream";
281
- var headers = _makeSigned("PUT", url, payloadHash, {
455
+ var contentType = opts.contentType || "application/octet-stream";
456
+ var extra = {
282
457
  "Content-Type": contentType,
283
458
  "Content-Length": String(buf.length),
284
- });
459
+ };
460
+ if (sseRequested) Object.assign(extra, sseRequested.headers);
461
+ var headers = _makeSigned("PUT", url, payloadHash, extra);
285
462
  return _request("PUT", url, headers, buf, reqOpts).then(function (res) {
463
+ _verifySseResponse(sseRequested, res.headers);
286
464
  return { size: buf.length, etag: res.headers.etag };
287
465
  });
288
466
  }
289
467
 
468
+ async function _multipartPut(key, body, opts, sseRequested) {
469
+ var contentType = opts.contentType || "application/octet-stream";
470
+
471
+ // Slice into parts. For Buffers we slice up-front; for streams we
472
+ // read sequentially into part-sized buffers (memory bounded).
473
+ var parts;
474
+ if (Buffer.isBuffer(body)) {
475
+ parts = [];
476
+ for (var off = 0; off < body.length; off += partSize) {
477
+ parts.push(body.slice(off, Math.min(off + partSize, body.length)));
478
+ }
479
+ // Edge case: empty buffer → one zero-length part. S3 rejects
480
+ // multipart with zero parts; rather than handling this corner
481
+ // we route empty buffers through single-PUT instead.
482
+ if (parts.length === 0) parts = [Buffer.alloc(0)];
483
+ } else {
484
+ parts = await _readStreamParts(body, partSize);
485
+ if (parts.length === 0) parts = [Buffer.alloc(0)];
486
+ }
487
+ if (parts.length > MAX_PARTS) {
488
+ throw _err("TOO_MANY_PARTS",
489
+ "multipart upload would require " + parts.length + " parts " +
490
+ "(S3 max " + MAX_PARTS + "); increase partSizeBytes", true);
491
+ }
492
+
493
+ // 1. Initiate
494
+ var url = _keyToUrl(key);
495
+ var initiateUrl = new URL(url.href);
496
+ initiateUrl.searchParams.set("uploads", "");
497
+ var initiateExtra = {
498
+ "Content-Type": contentType,
499
+ "Content-Length": "0",
500
+ };
501
+ if (sseRequested) Object.assign(initiateExtra, sseRequested.headers);
502
+ var initiateHeaders = _makeSigned("POST", initiateUrl, sha256Hex(Buffer.alloc(0)), initiateExtra);
503
+ var initRes = await _request("POST", initiateUrl, initiateHeaders, Buffer.alloc(0), reqOpts);
504
+ _verifySseResponse(sseRequested, initRes.headers);
505
+ var initDoc = safeXml.parse(initRes.body, LIST_PARSE_OPTS);
506
+ var uploadId = initDoc.InitiateMultipartUploadResult &&
507
+ initDoc.InitiateMultipartUploadResult.UploadId;
508
+ if (!uploadId) {
509
+ throw _err("MULTIPART_INIT_FAILED",
510
+ "S3 InitiateMultipartUpload response missing UploadId", false);
511
+ }
512
+
513
+ var totalSize = 0;
514
+ var uploadedEtags;
515
+
516
+ try {
517
+ // 2. Upload parts (concurrency-bounded)
518
+ uploadedEtags = await _bounded(parts, partConcurrency, async function (partBuf, idx) {
519
+ var partNumber = idx + 1;
520
+ var partUrl = new URL(url.href);
521
+ partUrl.searchParams.set("partNumber", String(partNumber));
522
+ partUrl.searchParams.set("uploadId", uploadId);
523
+ var partHeaders = _makeSigned("PUT", partUrl, sha256Hex(partBuf), {
524
+ "Content-Length": String(partBuf.length),
525
+ });
526
+ var partRes = await _request("PUT", partUrl, partHeaders, partBuf, reqOpts);
527
+ if (!partRes.headers.etag) {
528
+ throw _err("MULTIPART_PART_FAILED",
529
+ "UploadPart response missing ETag for part " + partNumber, false);
530
+ }
531
+ totalSize += partBuf.length;
532
+ return { partNumber: partNumber, etag: partRes.headers.etag };
533
+ });
534
+
535
+ // 3. Complete
536
+ var completeUrl = new URL(url.href);
537
+ completeUrl.searchParams.set("uploadId", uploadId);
538
+ var completeBody = Buffer.from(_buildCompleteMultipartXml(uploadedEtags), "utf8");
539
+ var completeHeaders = _makeSigned("POST", completeUrl, sha256Hex(completeBody), {
540
+ "Content-Type": "application/xml",
541
+ "Content-Length": String(completeBody.length),
542
+ });
543
+ var completeRes = await _request("POST", completeUrl, completeHeaders, completeBody, reqOpts);
544
+ // S3 may return 200 OK with an error body on CompleteMultipartUpload —
545
+ // surface that as a hard error rather than a silent success.
546
+ var completeDoc = safeXml.parse(completeRes.body, LIST_PARSE_OPTS);
547
+ if (completeDoc.Error) {
548
+ throw _err("MULTIPART_COMPLETE_FAILED",
549
+ "CompleteMultipartUpload returned error: " +
550
+ (completeDoc.Error.Code || "unknown") + " " +
551
+ (completeDoc.Error.Message || ""), false);
552
+ }
553
+ // SSE was already verified on the InitiateMultipartUpload
554
+ // response — that's the request that establishes the upload's
555
+ // encryption policy server-side. The CompleteMultipartUpload
556
+ // response may or may not echo the header depending on vendor;
557
+ // re-verifying here would double-fault on otherwise-fine setups.
558
+ var result = completeDoc.CompleteMultipartUploadResult || {};
559
+ return { size: totalSize, etag: result.ETag || completeRes.headers.etag, multipart: true };
560
+ } catch (e) {
561
+ // Abort cleans up server-side storage for the partial upload.
562
+ // Failures here are silently swallowed — the caller's original
563
+ // error is what they need to see, not a secondary cleanup error.
564
+ try {
565
+ var abortUrl = new URL(url.href);
566
+ abortUrl.searchParams.set("uploadId", uploadId);
567
+ var abortHeaders = _makeSigned("DELETE", abortUrl, sha256Hex(Buffer.alloc(0)));
568
+ await _request("DELETE", abortUrl, abortHeaders, null, reqOpts);
569
+ } catch (_e) { /* primary error wins */ }
570
+ throw e;
571
+ }
572
+ }
573
+
290
574
  function get(key) {
291
575
  var url = _keyToUrl(key);
292
576
  var headers = _makeSigned("GET", url, sha256Hex(Buffer.alloc(0)));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blamejs/core",
3
- "version": "0.4.23",
3
+ "version": "0.4.25",
4
4
  "description": "The Node framework that owns its stack.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "blamejs contributors",