@hdriel/aws-utils 1.2.2 → 1.2.3
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 +23 -2
- package/dist/index.js +23 -2
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -421,6 +421,15 @@ var getUnitBytes = (bytes2, unit) => {
|
|
|
421
421
|
return bytes2;
|
|
422
422
|
}
|
|
423
423
|
};
|
|
424
|
+
function hasNonAscii(str) {
|
|
425
|
+
return /[^\x00-\x7F]/.test(str);
|
|
426
|
+
}
|
|
427
|
+
function encodeS3Metadata(value) {
|
|
428
|
+
if (hasNonAscii(value)) {
|
|
429
|
+
return Buffer.from(value, "utf8").toString("base64");
|
|
430
|
+
}
|
|
431
|
+
return value;
|
|
432
|
+
}
|
|
424
433
|
|
|
425
434
|
// src/aws/s3/s3-file.ts
|
|
426
435
|
var import_buffer = require("buffer");
|
|
@@ -1883,10 +1892,22 @@ var S3Stream = class _S3Stream extends S3File {
|
|
|
1883
1892
|
bucket: this.bucket,
|
|
1884
1893
|
contentType: import_multer_s3.default.AUTO_CONTENT_TYPE,
|
|
1885
1894
|
metadata: (req, file, cb) => __async(this, null, function* () {
|
|
1886
|
-
const
|
|
1895
|
+
const originalName = decodeURIComponent(file.originalname);
|
|
1896
|
+
const baseMetadata = __spreadProps(__spreadValues({}, file), {
|
|
1897
|
+
directory: normalizedPath,
|
|
1898
|
+
// Encode non-ASCII characters for S3 metadata
|
|
1899
|
+
originalname: encodeS3Metadata(originalName),
|
|
1900
|
+
// Optional: Add a flag to know it's encoded
|
|
1901
|
+
// @ts-ignore
|
|
1902
|
+
"originalname-encoded": hasNonAscii(originalName) ? "base64" : "plain"
|
|
1903
|
+
});
|
|
1887
1904
|
if (customMetadata) {
|
|
1888
1905
|
const additionalMetadata = typeof customMetadata === "function" ? yield customMetadata(req, file) : customMetadata;
|
|
1889
|
-
|
|
1906
|
+
const sanitizedMetadata = {};
|
|
1907
|
+
for (const [key, value] of Object.entries(additionalMetadata)) {
|
|
1908
|
+
sanitizedMetadata[key] = typeof value === "string" ? encodeS3Metadata(value) : String(value);
|
|
1909
|
+
}
|
|
1910
|
+
Object.assign(baseMetadata, sanitizedMetadata);
|
|
1890
1911
|
}
|
|
1891
1912
|
cb(null, baseMetadata);
|
|
1892
1913
|
}),
|
package/dist/index.js
CHANGED
|
@@ -381,6 +381,15 @@ var getUnitBytes = (bytes2, unit) => {
|
|
|
381
381
|
return bytes2;
|
|
382
382
|
}
|
|
383
383
|
};
|
|
384
|
+
function hasNonAscii(str) {
|
|
385
|
+
return /[^\x00-\x7F]/.test(str);
|
|
386
|
+
}
|
|
387
|
+
function encodeS3Metadata(value) {
|
|
388
|
+
if (hasNonAscii(value)) {
|
|
389
|
+
return Buffer.from(value, "utf8").toString("base64");
|
|
390
|
+
}
|
|
391
|
+
return value;
|
|
392
|
+
}
|
|
384
393
|
|
|
385
394
|
// src/aws/s3/s3-file.ts
|
|
386
395
|
import { Buffer as Buffer2 } from "buffer";
|
|
@@ -1871,10 +1880,22 @@ var S3Stream = class _S3Stream extends S3File {
|
|
|
1871
1880
|
bucket: this.bucket,
|
|
1872
1881
|
contentType: multerS3.AUTO_CONTENT_TYPE,
|
|
1873
1882
|
metadata: (req, file, cb) => __async(this, null, function* () {
|
|
1874
|
-
const
|
|
1883
|
+
const originalName = decodeURIComponent(file.originalname);
|
|
1884
|
+
const baseMetadata = __spreadProps(__spreadValues({}, file), {
|
|
1885
|
+
directory: normalizedPath,
|
|
1886
|
+
// Encode non-ASCII characters for S3 metadata
|
|
1887
|
+
originalname: encodeS3Metadata(originalName),
|
|
1888
|
+
// Optional: Add a flag to know it's encoded
|
|
1889
|
+
// @ts-ignore
|
|
1890
|
+
"originalname-encoded": hasNonAscii(originalName) ? "base64" : "plain"
|
|
1891
|
+
});
|
|
1875
1892
|
if (customMetadata) {
|
|
1876
1893
|
const additionalMetadata = typeof customMetadata === "function" ? yield customMetadata(req, file) : customMetadata;
|
|
1877
|
-
|
|
1894
|
+
const sanitizedMetadata = {};
|
|
1895
|
+
for (const [key, value] of Object.entries(additionalMetadata)) {
|
|
1896
|
+
sanitizedMetadata[key] = typeof value === "string" ? encodeS3Metadata(value) : String(value);
|
|
1897
|
+
}
|
|
1898
|
+
Object.assign(baseMetadata, sanitizedMetadata);
|
|
1878
1899
|
}
|
|
1879
1900
|
cb(null, baseMetadata);
|
|
1880
1901
|
}),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hdriel/aws-utils",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.3",
|
|
4
4
|
"description": "Simplified AWS SDK (v3) utilities for S3 (upload, download, streaming) with TypeScript support",
|
|
5
5
|
"author": "Hadriel Benjo (https://github.com/hdriel)",
|
|
6
6
|
"type": "module",
|