@strapi/provider-upload-aws-s3 0.0.0-next.fd9757603c653ca239c45d6e28ab536d2dae0b39 → 0.0.0-next.fdac61dd05ca665168f51f655f1d165b55ec4231

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.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAE1C,OAAO,EAIL,yBAAyB,EAEzB,oCAAoC,EACpC,iCAAiC,EACjC,cAAc,EACd,eAAe,EAChB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAK5D,MAAM,WAAW,IAAI;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,GAAG,EAAE,MAAM,CAAC;IACZ,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iBAAiB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC5C,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,MAAM,mBAAmB,GAAG,CAC9B,oC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E,cAAc,CAAA;CAAE,CAAC,GAAG;IAC3E,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,CAAC,CAAC,EAAE,MAAM,GAAG,GAAG,CAAC;CAClB,CAAC;;+DA0B2D,WAAW;;2BAwCzC,IAAI,gBAAgB,GAAG,GAAG,QAAQ;YAAE,GAAG,EAAE,MAAM,CAAA;SAAE,CAAC;2BAsBxD,IAAI;qBAGV,IAAI;qBAGJ,IAAI,sBAAsB,QAAQ,yBAAyB,CAAC;;;AArE/E,wBA+EE"}
package/dist/index.js CHANGED
@@ -1,161 +1,207 @@
1
- "use strict";
2
- const fp = require("lodash/fp");
3
- const clientS3 = require("@aws-sdk/client-s3");
4
- const s3RequestPresigner = require("@aws-sdk/s3-request-presigner");
5
- const libStorage = require("@aws-sdk/lib-storage");
1
+ 'use strict';
2
+
3
+ var fp = require('lodash/fp');
4
+ var clientS3 = require('@aws-sdk/client-s3');
5
+ var s3RequestPresigner = require('@aws-sdk/s3-request-presigner');
6
+ var libStorage = require('@aws-sdk/lib-storage');
7
+
6
8
  const ENDPOINT_PATTERN = /^(.+\.)?s3[.-]([a-z0-9-]+)\./;
7
- function isUrlFromBucket(fileUrl, bucketName, baseUrl = "") {
8
- const url = new URL(fileUrl);
9
- if (baseUrl) {
10
- return false;
11
- }
12
- const { bucket } = getBucketFromAwsUrl(fileUrl);
13
- if (bucket) {
14
- return bucket === bucketName;
15
- }
16
- return url.host.startsWith(`${bucketName}.`) || url.pathname.includes(`/${bucketName}/`);
9
+ function isUrlFromBucket(fileUrl, bucketName, baseUrl = '') {
10
+ const url = new URL(fileUrl);
11
+ // Check if the file URL is using a base URL (e.g. a CDN).
12
+ // In this case do not sign the URL.
13
+ if (baseUrl) {
14
+ return false;
15
+ }
16
+ const { bucket } = getBucketFromAwsUrl(fileUrl);
17
+ if (bucket) {
18
+ return bucket === bucketName;
19
+ }
20
+ // File URL might be of an S3-compatible provider. (or an invalid URL)
21
+ // In this case, check if the bucket name appears in the URL host or path.
22
+ // e.g. https://minio.example.com/bucket-name/object-key
23
+ // e.g. https://bucket.nyc3.digitaloceanspaces.com/folder/img.png
24
+ return url.host.startsWith(`${bucketName}.`) || url.pathname.includes(`/${bucketName}/`);
17
25
  }
18
- function getBucketFromAwsUrl(fileUrl) {
19
- const url = new URL(fileUrl);
20
- if (url.protocol === "s3:") {
21
- const bucket = url.host;
22
- if (!bucket) {
23
- return { err: `Invalid S3 url: no bucket: ${url}` };
26
+ /**
27
+ * Parse the bucket name from a URL.
28
+ * See all URL formats in https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-bucket-intro.html
29
+ *
30
+ * @param {string} fileUrl - the URL to parse
31
+ * @returns {object} result
32
+ * @returns {string} result.bucket - the bucket name
33
+ * @returns {string} result.err - if any
34
+ */ function getBucketFromAwsUrl(fileUrl) {
35
+ const url = new URL(fileUrl);
36
+ // S3://<bucket-name>/<key>
37
+ if (url.protocol === 's3:') {
38
+ const bucket = url.host;
39
+ if (!bucket) {
40
+ return {
41
+ err: `Invalid S3 url: no bucket: ${url}`
42
+ };
43
+ }
44
+ return {
45
+ bucket
46
+ };
24
47
  }
25
- return { bucket };
26
- }
27
- if (!url.host) {
28
- return { err: `Invalid S3 url: no hostname: ${url}` };
29
- }
30
- const matches = url.host.match(ENDPOINT_PATTERN);
31
- if (!matches) {
32
- return { err: `Invalid S3 url: hostname does not appear to be a valid S3 endpoint: ${url}` };
33
- }
34
- const prefix = matches[1];
35
- if (!prefix) {
36
- if (url.pathname === "/") {
37
- return { bucket: null };
48
+ if (!url.host) {
49
+ return {
50
+ err: `Invalid S3 url: no hostname: ${url}`
51
+ };
38
52
  }
39
- const index2 = url.pathname.indexOf("/", 1);
40
- if (index2 === -1) {
41
- return { bucket: url.pathname.substring(1) };
53
+ const matches = url.host.match(ENDPOINT_PATTERN);
54
+ if (!matches) {
55
+ return {
56
+ err: `Invalid S3 url: hostname does not appear to be a valid S3 endpoint: ${url}`
57
+ };
42
58
  }
43
- if (index2 === url.pathname.length - 1) {
44
- return { bucket: url.pathname.substring(1, index2) };
59
+ const prefix = matches[1];
60
+ // https://s3.amazonaws.com/<bucket-name>
61
+ if (!prefix) {
62
+ if (url.pathname === '/') {
63
+ return {
64
+ bucket: null
65
+ };
66
+ }
67
+ const index = url.pathname.indexOf('/', 1);
68
+ // https://s3.amazonaws.com/<bucket-name>
69
+ if (index === -1) {
70
+ return {
71
+ bucket: url.pathname.substring(1)
72
+ };
73
+ }
74
+ // https://s3.amazonaws.com/<bucket-name>/
75
+ if (index === url.pathname.length - 1) {
76
+ return {
77
+ bucket: url.pathname.substring(1, index)
78
+ };
79
+ }
80
+ // https://s3.amazonaws.com/<bucket-name>/key
81
+ return {
82
+ bucket: url.pathname.substring(1, index)
83
+ };
45
84
  }
46
- return { bucket: url.pathname.substring(1, index2) };
47
- }
48
- return { bucket: prefix.substring(0, prefix.length - 1) };
49
- }
50
- const extractCredentials = (options) => {
51
- if (options.accessKeyId && options.secretAccessKey) {
52
- return {
53
- accessKeyId: options.accessKeyId,
54
- secretAccessKey: options.secretAccessKey
55
- };
56
- }
57
- if (options.s3Options?.accessKeyId && options.s3Options.secretAccessKey) {
58
- process.emitWarning(
59
- "Credentials passed directly to s3Options is deprecated and will be removed in a future release. Please wrap them inside a credentials object."
60
- );
61
- return {
62
- accessKeyId: options.s3Options.accessKeyId,
63
- secretAccessKey: options.s3Options.secretAccessKey
64
- };
65
- }
66
- if (options.s3Options?.credentials) {
85
+ // https://<bucket-name>.s3.amazonaws.com/
67
86
  return {
68
- accessKeyId: options.s3Options.credentials.accessKeyId,
69
- secretAccessKey: options.s3Options.credentials.secretAccessKey
87
+ bucket: prefix.substring(0, prefix.length - 1)
70
88
  };
71
- }
72
- return null;
73
- };
74
- const assertUrlProtocol = (url) => {
75
- return /^\w*:\/\//.test(url);
89
+ }
90
+ const extractCredentials = (options)=>{
91
+ if (options.s3Options?.credentials) {
92
+ return {
93
+ accessKeyId: options.s3Options.credentials.accessKeyId,
94
+ secretAccessKey: options.s3Options.credentials.secretAccessKey
95
+ };
96
+ }
97
+ return null;
76
98
  };
77
- const getConfig = ({ baseUrl, rootPath, s3Options, ...legacyS3Options }) => {
78
- if (Object.keys(legacyS3Options).length > 0) {
79
- process.emitWarning(
80
- "S3 configuration options passed at root level of the plugin's providerOptions is deprecated and will be removed in a future release. Please wrap them inside the 's3Options:{}' property."
81
- );
82
- }
83
- const credentials = extractCredentials({ s3Options, ...legacyS3Options });
84
- const config = {
85
- ...s3Options,
86
- ...legacyS3Options,
87
- ...credentials ? { credentials } : {}
88
- };
89
- config.params.ACL = fp.getOr(clientS3.ObjectCannedACL.public_read, ["params", "ACL"], config);
90
- return config;
99
+
100
+ const assertUrlProtocol = (url)=>{
101
+ // Regex to test protocol like "http://", "https://"
102
+ return /^\w*:\/\//.test(url);
91
103
  };
92
- const index = {
93
- init({ baseUrl, rootPath, s3Options, ...legacyS3Options }) {
94
- const config = getConfig({ baseUrl, rootPath, s3Options, ...legacyS3Options });
95
- const s3Client = new clientS3.S3Client(config);
96
- const filePrefix = rootPath ? `${rootPath.replace(/\/+$/, "")}/` : "";
97
- const getFileKey = (file) => {
98
- const path = file.path ? `${file.path}/` : "";
99
- return `${filePrefix}${path}${file.hash}${file.ext}`;
100
- };
101
- const upload = async (file, customParams = {}) => {
102
- const fileKey = getFileKey(file);
103
- const uploadObj = new libStorage.Upload({
104
- client: s3Client,
105
- params: {
106
- Bucket: config.params.Bucket,
107
- Key: fileKey,
108
- Body: file.stream || Buffer.from(file.buffer, "binary"),
109
- ACL: config.params.ACL,
110
- ContentType: file.mime,
111
- ...customParams
112
- }
113
- });
114
- const upload2 = await uploadObj.done();
115
- if (assertUrlProtocol(upload2.Location)) {
116
- file.url = baseUrl ? `${baseUrl}/${fileKey}` : upload2.Location;
117
- } else {
118
- file.url = `https://${upload2.Location}`;
119
- }
104
+ const getConfig = ({ baseUrl, rootPath, s3Options, ...legacyS3Options })=>{
105
+ if (Object.keys(legacyS3Options).length > 0) {
106
+ process.emitWarning("S3 configuration options passed at root level of the plugin's providerOptions is deprecated and will be removed in a future release. Please wrap them inside the 's3Options:{}' property.");
107
+ }
108
+ const credentials = extractCredentials({
109
+ s3Options,
110
+ ...legacyS3Options
111
+ });
112
+ const config = {
113
+ ...s3Options,
114
+ ...legacyS3Options,
115
+ ...credentials ? {
116
+ credentials
117
+ } : {}
120
118
  };
121
- return {
122
- isPrivate() {
123
- return config.params.ACL === "private";
124
- },
125
- async getSignedUrl(file, customParams) {
126
- if (!isUrlFromBucket(file.url, config.params.Bucket, baseUrl)) {
127
- return { url: file.url };
128
- }
129
- const fileKey = getFileKey(file);
130
- const url = await s3RequestPresigner.getSignedUrl(
131
- s3Client,
132
- new clientS3.GetObjectCommand({
133
- Bucket: config.params.Bucket,
134
- Key: fileKey,
135
- ...customParams
136
- }),
137
- {
138
- expiresIn: fp.getOr(15 * 60, ["params", "signedUrlExpires"], config)
139
- }
140
- );
141
- return { url };
142
- },
143
- uploadStream(file, customParams = {}) {
144
- return upload(file, customParams);
145
- },
146
- upload(file, customParams = {}) {
147
- return upload(file, customParams);
148
- },
149
- delete(file, customParams = {}) {
150
- const command = new clientS3.DeleteObjectCommand({
151
- Bucket: config.params.Bucket,
152
- Key: getFileKey(file),
153
- ...customParams
119
+ config.params.ACL = fp.getOr(clientS3.ObjectCannedACL.public_read, [
120
+ 'params',
121
+ 'ACL'
122
+ ], config);
123
+ return config;
124
+ };
125
+ var index = {
126
+ init ({ baseUrl, rootPath, s3Options, ...legacyS3Options }) {
127
+ // TODO V5 change config structure to avoid having to do this
128
+ const config = getConfig({
129
+ baseUrl,
130
+ rootPath,
131
+ s3Options,
132
+ ...legacyS3Options
154
133
  });
155
- return s3Client.send(command);
156
- }
157
- };
158
- }
134
+ const s3Client = new clientS3.S3Client(config);
135
+ const filePrefix = rootPath ? `${rootPath.replace(/\/+$/, '')}/` : '';
136
+ const getFileKey = (file)=>{
137
+ const path = file.path ? `${file.path}/` : '';
138
+ return `${filePrefix}${path}${file.hash}${file.ext}`;
139
+ };
140
+ const upload = async (file, customParams = {})=>{
141
+ const fileKey = getFileKey(file);
142
+ const uploadObj = new libStorage.Upload({
143
+ client: s3Client,
144
+ params: {
145
+ Bucket: config.params.Bucket,
146
+ Key: fileKey,
147
+ Body: file.stream || Buffer.from(file.buffer, 'binary'),
148
+ ACL: config.params.ACL,
149
+ ContentType: file.mime,
150
+ ...customParams
151
+ }
152
+ });
153
+ const upload = await uploadObj.done();
154
+ if (assertUrlProtocol(upload.Location)) {
155
+ file.url = baseUrl ? `${baseUrl}/${fileKey}` : upload.Location;
156
+ } else {
157
+ // Default protocol to https protocol
158
+ file.url = `https://${upload.Location}`;
159
+ }
160
+ };
161
+ return {
162
+ isPrivate () {
163
+ return config.params.ACL === 'private';
164
+ },
165
+ async getSignedUrl (file, customParams) {
166
+ // Do not sign the url if it does not come from the same bucket.
167
+ if (!isUrlFromBucket(file.url, config.params.Bucket, baseUrl)) {
168
+ return {
169
+ url: file.url
170
+ };
171
+ }
172
+ const fileKey = getFileKey(file);
173
+ const url = await s3RequestPresigner.getSignedUrl(// @ts-expect-error - TODO fix client type
174
+ s3Client, new clientS3.GetObjectCommand({
175
+ Bucket: config.params.Bucket,
176
+ Key: fileKey,
177
+ ...customParams
178
+ }), {
179
+ expiresIn: fp.getOr(15 * 60, [
180
+ 'params',
181
+ 'signedUrlExpires'
182
+ ], config)
183
+ });
184
+ return {
185
+ url
186
+ };
187
+ },
188
+ uploadStream (file, customParams = {}) {
189
+ return upload(file, customParams);
190
+ },
191
+ upload (file, customParams = {}) {
192
+ return upload(file, customParams);
193
+ },
194
+ delete (file, customParams = {}) {
195
+ const command = new clientS3.DeleteObjectCommand({
196
+ Bucket: config.params.Bucket,
197
+ Key: getFileKey(file),
198
+ ...customParams
199
+ });
200
+ return s3Client.send(command);
201
+ }
202
+ };
203
+ }
159
204
  };
205
+
160
206
  module.exports = index;
161
207
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/utils.ts","../src/index.ts"],"sourcesContent":["import type { AwsCredentialIdentity } from '@aws-sdk/types';\nimport type { InitOptions } from '.';\n\nconst ENDPOINT_PATTERN = /^(.+\\.)?s3[.-]([a-z0-9-]+)\\./;\n\ninterface BucketInfo {\n bucket?: string | null;\n err?: string;\n}\n\nexport function isUrlFromBucket(fileUrl: string, bucketName: string, baseUrl = ''): boolean {\n const url = new URL(fileUrl);\n\n // Check if the file URL is using a base URL (e.g. a CDN).\n // In this case do not sign the URL.\n if (baseUrl) {\n return false;\n }\n\n const { bucket } = getBucketFromAwsUrl(fileUrl);\n\n if (bucket) {\n return bucket === bucketName;\n }\n\n // File URL might be of an S3-compatible provider. (or an invalid URL)\n // In this case, check if the bucket name appears in the URL host or path.\n // e.g. https://minio.example.com/bucket-name/object-key\n // e.g. https://bucket.nyc3.digitaloceanspaces.com/folder/img.png\n return url.host.startsWith(`${bucketName}.`) || url.pathname.includes(`/${bucketName}/`);\n}\n\n/**\n * Parse the bucket name from a URL.\n * See all URL formats in https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-bucket-intro.html\n *\n * @param {string} fileUrl - the URL to parse\n * @returns {object} result\n * @returns {string} result.bucket - the bucket name\n * @returns {string} result.err - if any\n */\nfunction getBucketFromAwsUrl(fileUrl: string): BucketInfo {\n const url = new URL(fileUrl);\n\n // S3://<bucket-name>/<key>\n if (url.protocol === 's3:') {\n const bucket = url.host;\n\n if (!bucket) {\n return { err: `Invalid S3 url: no bucket: ${url}` };\n }\n return { bucket };\n }\n\n if (!url.host) {\n return { err: `Invalid S3 url: no hostname: ${url}` };\n }\n\n const matches = url.host.match(ENDPOINT_PATTERN);\n if (!matches) {\n return { err: `Invalid S3 url: hostname does not appear to be a valid S3 endpoint: ${url}` };\n }\n\n const prefix = matches[1];\n // https://s3.amazonaws.com/<bucket-name>\n if (!prefix) {\n if (url.pathname === '/') {\n return { bucket: null };\n }\n\n const index = url.pathname.indexOf('/', 1);\n\n // https://s3.amazonaws.com/<bucket-name>\n if (index === -1) {\n return { bucket: url.pathname.substring(1) };\n }\n\n // https://s3.amazonaws.com/<bucket-name>/\n if (index === url.pathname.length - 1) {\n return { bucket: url.pathname.substring(1, index) };\n }\n\n // https://s3.amazonaws.com/<bucket-name>/key\n return { bucket: url.pathname.substring(1, index) };\n }\n\n // https://<bucket-name>.s3.amazonaws.com/\n return { bucket: prefix.substring(0, prefix.length - 1) };\n}\n\n// TODO Remove this in V5 since we will only support the new config structure\nexport const extractCredentials = (options: InitOptions): AwsCredentialIdentity | null => {\n // legacy\n if (options.accessKeyId && options.secretAccessKey) {\n return {\n accessKeyId: options.accessKeyId,\n secretAccessKey: options.secretAccessKey,\n };\n }\n // Legacy\n if (options.s3Options?.accessKeyId && options.s3Options.secretAccessKey) {\n process.emitWarning(\n 'Credentials passed directly to s3Options is deprecated and will be removed in a future release. Please wrap them inside a credentials object.'\n );\n return {\n accessKeyId: options.s3Options.accessKeyId,\n secretAccessKey: options.s3Options.secretAccessKey,\n };\n }\n // V5\n if (options.s3Options?.credentials) {\n return {\n accessKeyId: options.s3Options.credentials.accessKeyId,\n secretAccessKey: options.s3Options.credentials.secretAccessKey,\n };\n }\n return null;\n};\n","import type { ReadStream } from 'node:fs';\nimport { getOr } from 'lodash/fp';\nimport {\n S3Client,\n GetObjectCommand,\n DeleteObjectCommand,\n DeleteObjectCommandOutput,\n PutObjectCommandInput,\n CompleteMultipartUploadCommandOutput,\n AbortMultipartUploadCommandOutput,\n S3ClientConfig,\n ObjectCannedACL,\n} from '@aws-sdk/client-s3';\nimport type { AwsCredentialIdentity } from '@aws-sdk/types';\nimport { getSignedUrl } from '@aws-sdk/s3-request-presigner';\nimport { Upload } from '@aws-sdk/lib-storage';\nimport { extractCredentials, isUrlFromBucket } from './utils';\n\nexport interface File {\n name: string;\n alternativeText?: string;\n caption?: string;\n width?: number;\n height?: number;\n formats?: Record<string, unknown>;\n hash: string;\n ext?: string;\n mime: string;\n size: number;\n sizeInBytes: number;\n url: string;\n previewUrl?: string;\n path?: string;\n provider?: string;\n provider_metadata?: Record<string, unknown>;\n stream?: ReadStream;\n buffer?: Buffer;\n}\n\nexport type UploadCommandOutput = (\n | CompleteMultipartUploadCommandOutput\n | AbortMultipartUploadCommandOutput\n) & {\n Location: string;\n};\n\nexport interface AWSParams {\n Bucket: string; // making it required\n ACL?: ObjectCannedACL;\n signedUrlExpires?: number;\n}\n\nexport interface DefaultOptions extends S3ClientConfig {\n // TODO Remove this in V5\n accessKeyId?: AwsCredentialIdentity['accessKeyId'];\n secretAccessKey?: AwsCredentialIdentity['secretAccessKey'];\n // Keep this for V5\n credentials?: AwsCredentialIdentity;\n params?: AWSParams;\n [k: string]: any;\n}\n\nexport type InitOptions = (DefaultOptions | { s3Options: DefaultOptions }) & {\n baseUrl?: string;\n rootPath?: string;\n [k: string]: any;\n};\n\nconst assertUrlProtocol = (url: string) => {\n // Regex to test protocol like \"http://\", \"https://\"\n return /^\\w*:\\/\\//.test(url);\n};\n\nconst getConfig = ({ baseUrl, rootPath, s3Options, ...legacyS3Options }: InitOptions) => {\n if (Object.keys(legacyS3Options).length > 0) {\n process.emitWarning(\n \"S3 configuration options passed at root level of the plugin's providerOptions is deprecated and will be removed in a future release. Please wrap them inside the 's3Options:{}' property.\"\n );\n }\n const credentials = extractCredentials({ s3Options, ...legacyS3Options });\n const config = {\n ...s3Options,\n ...legacyS3Options,\n ...(credentials ? { credentials } : {}),\n };\n\n config.params.ACL = getOr(ObjectCannedACL.public_read, ['params', 'ACL'], config);\n\n return config;\n};\n\nexport default {\n init({ baseUrl, rootPath, s3Options, ...legacyS3Options }: InitOptions) {\n // TODO V5 change config structure to avoid having to do this\n const config = getConfig({ baseUrl, rootPath, s3Options, ...legacyS3Options });\n const s3Client = new S3Client(config);\n const filePrefix = rootPath ? `${rootPath.replace(/\\/+$/, '')}/` : '';\n\n const getFileKey = (file: File) => {\n const path = file.path ? `${file.path}/` : '';\n return `${filePrefix}${path}${file.hash}${file.ext}`;\n };\n\n const upload = async (file: File, customParams: Partial<PutObjectCommandInput> = {}) => {\n const fileKey = getFileKey(file);\n const uploadObj = new Upload({\n client: s3Client,\n params: {\n Bucket: config.params.Bucket,\n Key: fileKey,\n Body: file.stream || Buffer.from(file.buffer as any, 'binary'),\n ACL: config.params.ACL,\n ContentType: file.mime,\n ...customParams,\n },\n });\n\n const upload = (await uploadObj.done()) as UploadCommandOutput;\n\n if (assertUrlProtocol(upload.Location)) {\n file.url = baseUrl ? `${baseUrl}/${fileKey}` : upload.Location;\n } else {\n // Default protocol to https protocol\n file.url = `https://${upload.Location}`;\n }\n };\n\n return {\n isPrivate() {\n return config.params.ACL === 'private';\n },\n\n async getSignedUrl(file: File, customParams: any): Promise<{ url: string }> {\n // Do not sign the url if it does not come from the same bucket.\n if (!isUrlFromBucket(file.url, config.params.Bucket, baseUrl)) {\n return { url: file.url };\n }\n const fileKey = getFileKey(file);\n\n const url = await getSignedUrl(\n s3Client,\n new GetObjectCommand({\n Bucket: config.params.Bucket,\n Key: fileKey,\n ...customParams,\n }),\n {\n expiresIn: getOr(15 * 60, ['params', 'signedUrlExpires'], config),\n }\n );\n\n return { url };\n },\n uploadStream(file: File, customParams = {}) {\n return upload(file, customParams);\n },\n upload(file: File, customParams = {}) {\n return upload(file, customParams);\n },\n delete(file: File, customParams = {}): Promise<DeleteObjectCommandOutput> {\n const command = new DeleteObjectCommand({\n Bucket: config.params.Bucket,\n Key: getFileKey(file),\n ...customParams,\n });\n return s3Client.send(command);\n },\n };\n },\n};\n"],"names":["index","getOr","ObjectCannedACL","S3Client","Upload","upload","getSignedUrl","GetObjectCommand","DeleteObjectCommand"],"mappings":";;;;;AAGA,MAAM,mBAAmB;AAOlB,SAAS,gBAAgB,SAAiB,YAAoB,UAAU,IAAa;AACpF,QAAA,MAAM,IAAI,IAAI,OAAO;AAI3B,MAAI,SAAS;AACJ,WAAA;AAAA,EACT;AAEA,QAAM,E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AAY,eAAe,IAAI;AACnC,eAAA,OAAO,MAAM,YAAY;AAAA,MAClC;AAAA,MACA,OAAO,MAAY,eAAe,IAAI;AAC7B,eAAA,OAAO,MAAM,YAAY;AAAA,MAClC;AAAA,MACA,OAAO,MAAY,eAAe,IAAwC;AAClE,cAAA,UAAU,IAAIO,6BAAoB;AAAA,UACtC,QAAQ,OAAO,OAAO;AAAA,UACtB,KAAK,WAAW,IAAI;AAAA,UACpB,GAAG;AAAA,QAAA,CACJ;AACM,eAAA,SAAS,KAAK,OAAO;AAAA,MAC9B;AAAA,IAAA;AAAA,EAEJ;AACF;;"}
1
+ {"version":3,"file":"index.js","sources":["../src/utils.ts","../src/index.ts"],"sourcesContent":["import type { AwsCredentialIdentity } from '@aws-sdk/types';\nimport type { InitOptions } from '.';\n\nconst ENDPOINT_PATTERN = /^(.+\\.)?s3[.-]([a-z0-9-]+)\\./;\n\ninterface BucketInfo {\n bucket?: string | null;\n err?: string;\n}\n\nexport function isUrlFromBucket(fileUrl: string, bucketName: string, baseUrl = ''): boolean {\n const url = new URL(fileUrl);\n\n // Check if the file URL is using a base URL (e.g. a CDN).\n // In this case do not sign the URL.\n if (baseUrl) {\n return false;\n }\n\n const { bucket } = getBucketFromAwsUrl(fileUrl);\n\n if (bucket) {\n return bucket === bucketName;\n }\n\n // File URL might be of an S3-compatible provider. (or an invalid URL)\n // In this case, check if the bucket name appears in the URL host or path.\n // e.g. https://minio.example.com/bucket-name/object-key\n // e.g. https://bucket.nyc3.digitaloceanspaces.com/folder/img.png\n return url.host.startsWith(`${bucketName}.`) || url.pathname.includes(`/${bucketName}/`);\n}\n\n/**\n * Parse the bucket name from a URL.\n * See all URL formats in https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-bucket-intro.html\n *\n * @param {string} fileUrl - the URL to parse\n * @returns {object} result\n * @returns {string} result.bucket - the bucket name\n * @returns {string} result.err - if any\n */\nfunction getBucketFromAwsUrl(fileUrl: string): BucketInfo {\n const url = new URL(fileUrl);\n\n // S3://<bucket-name>/<key>\n if (url.protocol === 's3:') {\n const bucket = url.host;\n\n if (!bucket) {\n return { err: `Invalid S3 url: no bucket: ${url}` };\n }\n return { bucket };\n }\n\n if (!url.host) {\n return { err: `Invalid S3 url: no hostname: ${url}` };\n }\n\n const matches = url.host.match(ENDPOINT_PATTERN);\n if (!matches) {\n return { err: `Invalid S3 url: hostname does not appear to be a valid S3 endpoint: ${url}` };\n }\n\n const prefix = matches[1];\n // https://s3.amazonaws.com/<bucket-name>\n if (!prefix) {\n if (url.pathname === '/') {\n return { bucket: null };\n }\n\n const index = url.pathname.indexOf('/', 1);\n\n // https://s3.amazonaws.com/<bucket-name>\n if (index === -1) {\n return { bucket: url.pathname.substring(1) };\n }\n\n // https://s3.amazonaws.com/<bucket-name>/\n if (index === url.pathname.length - 1) {\n return { bucket: url.pathname.substring(1, index) };\n }\n\n // https://s3.amazonaws.com/<bucket-name>/key\n return { bucket: url.pathname.substring(1, index) };\n }\n\n // https://<bucket-name>.s3.amazonaws.com/\n return { bucket: prefix.substring(0, prefix.length - 1) };\n}\n\nexport const extractCredentials = (options: InitOptions): AwsCredentialIdentity | null => {\n if (options.s3Options?.credentials) {\n return {\n accessKeyId: options.s3Options.credentials.accessKeyId,\n secretAccessKey: options.s3Options.credentials.secretAccessKey,\n };\n }\n return null;\n};\n","import type { ReadStream } from 'node:fs';\nimport { getOr } from 'lodash/fp';\nimport {\n S3Client,\n GetObjectCommand,\n DeleteObjectCommand,\n DeleteObjectCommandOutput,\n PutObjectCommandInput,\n CompleteMultipartUploadCommandOutput,\n AbortMultipartUploadCommandOutput,\n S3ClientConfig,\n ObjectCannedACL,\n} from '@aws-sdk/client-s3';\nimport type { AwsCredentialIdentity } from '@aws-sdk/types';\nimport { getSignedUrl } from '@aws-sdk/s3-request-presigner';\nimport { Upload } from '@aws-sdk/lib-storage';\nimport { extractCredentials, isUrlFromBucket } from './utils';\n\nexport interface File {\n name: string;\n alternativeText?: string;\n caption?: string;\n width?: number;\n height?: number;\n formats?: Record<string, unknown>;\n hash: string;\n ext?: string;\n mime: string;\n size: number;\n sizeInBytes: number;\n url: string;\n previewUrl?: string;\n path?: string;\n provider?: string;\n provider_metadata?: Record<string, unknown>;\n stream?: ReadStream;\n buffer?: Buffer;\n}\n\nexport type UploadCommandOutput = (\n | CompleteMultipartUploadCommandOutput\n | AbortMultipartUploadCommandOutput\n) & {\n Location: string;\n};\n\nexport interface AWSParams {\n Bucket: string; // making it required\n ACL?: ObjectCannedACL;\n signedUrlExpires?: number;\n}\n\nexport interface DefaultOptions extends S3ClientConfig {\n // TODO Remove this in V5\n accessKeyId?: AwsCredentialIdentity['accessKeyId'];\n secretAccessKey?: AwsCredentialIdentity['secretAccessKey'];\n // Keep this for V5\n credentials?: AwsCredentialIdentity;\n params?: AWSParams;\n [k: string]: any;\n}\n\nexport type InitOptions = (DefaultOptions | { s3Options: DefaultOptions }) & {\n baseUrl?: string;\n rootPath?: string;\n [k: string]: any;\n};\n\nconst assertUrlProtocol = (url: string) => {\n // Regex to test protocol like \"http://\", \"https://\"\n return /^\\w*:\\/\\//.test(url);\n};\n\nconst getConfig = ({ baseUrl, rootPath, s3Options, ...legacyS3Options }: InitOptions) => {\n if (Object.keys(legacyS3Options).length > 0) {\n process.emitWarning(\n \"S3 configuration options passed at root level of the plugin's providerOptions is deprecated and will be removed in a future release. Please wrap them inside the 's3Options:{}' property.\"\n );\n }\n const credentials = extractCredentials({ s3Options, ...legacyS3Options });\n const config = {\n ...s3Options,\n ...legacyS3Options,\n ...(credentials ? { credentials } : {}),\n };\n\n config.params.ACL = getOr(ObjectCannedACL.public_read, ['params', 'ACL'], config);\n\n return config;\n};\n\nexport default {\n init({ baseUrl, rootPath, s3Options, ...legacyS3Options }: InitOptions) {\n // TODO V5 change config structure to avoid having to do this\n const config = getConfig({ baseUrl, rootPath, s3Options, ...legacyS3Options });\n const s3Client = new S3Client(config);\n const filePrefix = rootPath ? `${rootPath.replace(/\\/+$/, '')}/` : '';\n\n const getFileKey = (file: File) => {\n const path = file.path ? `${file.path}/` : '';\n return `${filePrefix}${path}${file.hash}${file.ext}`;\n };\n\n const upload = async (file: File, customParams: Partial<PutObjectCommandInput> = {}) => {\n const fileKey = getFileKey(file);\n const uploadObj = new Upload({\n client: s3Client,\n params: {\n Bucket: config.params.Bucket,\n Key: fileKey,\n Body: file.stream || Buffer.from(file.buffer as any, 'binary'),\n ACL: config.params.ACL,\n ContentType: file.mime,\n ...customParams,\n },\n });\n\n const upload = (await uploadObj.done()) as UploadCommandOutput;\n\n if (assertUrlProtocol(upload.Location)) {\n file.url = baseUrl ? `${baseUrl}/${fileKey}` : upload.Location;\n } else {\n // Default protocol to https protocol\n file.url = `https://${upload.Location}`;\n }\n };\n\n return {\n isPrivate() {\n return config.params.ACL === 'private';\n },\n\n async getSignedUrl(file: File, customParams: any): Promise<{ url: string }> {\n // Do not sign the url if it does not come from the same bucket.\n if (!isUrlFromBucket(file.url, config.params.Bucket, baseUrl)) {\n return { url: file.url };\n }\n const fileKey = getFileKey(file);\n\n const url = await getSignedUrl(\n // @ts-expect-error - TODO fix client type\n s3Client,\n new GetObjectCommand({\n Bucket: config.params.Bucket,\n Key: fileKey,\n ...customParams,\n }),\n {\n expiresIn: getOr(15 * 60, ['params', 'signedUrlExpires'], config),\n }\n );\n\n return { url };\n },\n uploadStream(file: File, customParams = {}) {\n return upload(file, customParams);\n },\n upload(file: File, customParams = {}) {\n return upload(file, customParams);\n },\n delete(file: File, customParams = {}): Promise<DeleteObjectCommandOutput> {\n const command = new DeleteObjectCommand({\n Bucket: config.params.Bucket,\n Key: getFileKey(file),\n ...customParams,\n });\n return s3Client.send(command);\n },\n };\n },\n};\n"],"names":["ENDPOINT_PATTERN","isUrlFromBucket","fileUrl","bucketName","baseUrl","url","URL","bucket","getBucketFromAwsUrl","host","startsWith","pathname","includes","protocol","err","matches","match","prefix","index","indexOf","substring","length","extractCredentials","options","s3Options","credentials","accessKeyId","secretAccessKey","assertUrlProtocol","test","getConfig","rootPath","legacyS3Options","Object","keys","process","emitWarning","config","params","ACL","getOr","ObjectCannedACL","public_read","init","s3Client","S3Client","filePrefix","replace","getFileKey","file","path","hash","ext","upload","customParams","fileKey","uploadObj","Upload","client","Bucket","Key","Body","stream","Buffer","from","buffer","ContentType","mime","done","Location","isPrivate","getSignedUrl","GetObjectCommand","expiresIn","uploadStream","delete","command","DeleteObjectCommand","send"],"mappings":";;;;;;;AAGA,MAAMA,gBAAmB,GAAA,8BAAA;AAOlB,SAASC,eAAgBC,CAAAA,OAAe,EAAEC,UAAkB,EAAEC,UAAU,EAAE,EAAA;IAC/E,MAAMC,GAAAA,GAAM,IAAIC,GAAIJ,CAAAA,OAAAA,CAAAA;;;AAIpB,IAAA,IAAIE,OAAS,EAAA;QACX,OAAO,KAAA;AACT;AAEA,IAAA,MAAM,EAAEG,MAAM,EAAE,GAAGC,mBAAoBN,CAAAA,OAAAA,CAAAA;AAEvC,IAAA,IAAIK,MAAQ,EAAA;AACV,QAAA,OAAOA,MAAWJ,KAAAA,UAAAA;AACpB;;;;;IAMA,OAAOE,GAAAA,CAAII,IAAI,CAACC,UAAU,CAAC,CAAC,EAAEP,WAAW,CAAC,CAAC,KAAKE,GAAIM,CAAAA,QAAQ,CAACC,QAAQ,CAAC,CAAC,CAAC,EAAET,UAAW,CAAA,CAAC,CAAC,CAAA;AACzF;AAEA;;;;;;;;IASA,SAASK,oBAAoBN,OAAe,EAAA;IAC1C,MAAMG,GAAAA,GAAM,IAAIC,GAAIJ,CAAAA,OAAAA,CAAAA;;IAGpB,IAAIG,GAAAA,CAAIQ,QAAQ,KAAK,KAAO,EAAA;QAC1B,MAAMN,MAAAA,GAASF,IAAII,IAAI;AAEvB,QAAA,IAAI,CAACF,MAAQ,EAAA;YACX,OAAO;AAAEO,gBAAAA,GAAAA,EAAK,CAAC,2BAA2B,EAAET,GAAAA,CAAI;AAAE,aAAA;AACpD;QACA,OAAO;AAAEE,YAAAA;AAAO,SAAA;AAClB;IAEA,IAAI,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A;AAAI,iBAAA;AACf,aAAA;AACAqE,YAAAA,YAAAA,CAAAA,CAAazB,IAAU,EAAEK,YAAe,GAAA,EAAE,EAAA;AACxC,gBAAA,OAAOD,OAAOJ,IAAMK,EAAAA,YAAAA,CAAAA;AACtB,aAAA;AACAD,YAAAA,MAAAA,CAAAA,CAAOJ,IAAU,EAAEK,YAAe,GAAA,EAAE,EAAA;AAClC,gBAAA,OAAOD,OAAOJ,IAAMK,EAAAA,YAAAA,CAAAA;AACtB,aAAA;AACAqB,YAAAA,MAAAA,CAAAA,CAAO1B,IAAU,EAAEK,YAAe,GAAA,EAAE,EAAA;gBAClC,MAAMsB,OAAAA,GAAU,IAAIC,4BAAoB,CAAA;oBACtClB,MAAQtB,EAAAA,MAAAA,CAAOC,MAAM,CAACqB,MAAM;AAC5BC,oBAAAA,GAAAA,EAAKZ,UAAWC,CAAAA,IAAAA,CAAAA;AAChB,oBAAA,GAAGK;AACL,iBAAA,CAAA;gBACA,OAAOV,QAAAA,CAASkC,IAAI,CAACF,OAAAA,CAAAA;AACvB;AACF,SAAA;AACF;AACF,CAAE;;;;"}
package/dist/index.mjs CHANGED
@@ -1,162 +1,205 @@
1
- import { getOr } from "lodash/fp";
2
- import { S3Client, GetObjectCommand, DeleteObjectCommand, ObjectCannedACL } from "@aws-sdk/client-s3";
3
- import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
4
- import { Upload } from "@aws-sdk/lib-storage";
1
+ import { getOr } from 'lodash/fp';
2
+ import { S3Client, GetObjectCommand, DeleteObjectCommand, ObjectCannedACL } from '@aws-sdk/client-s3';
3
+ import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
4
+ import { Upload } from '@aws-sdk/lib-storage';
5
+
5
6
  const ENDPOINT_PATTERN = /^(.+\.)?s3[.-]([a-z0-9-]+)\./;
6
- function isUrlFromBucket(fileUrl, bucketName, baseUrl = "") {
7
- const url = new URL(fileUrl);
8
- if (baseUrl) {
9
- return false;
10
- }
11
- const { bucket } = getBucketFromAwsUrl(fileUrl);
12
- if (bucket) {
13
- return bucket === bucketName;
14
- }
15
- return url.host.startsWith(`${bucketName}.`) || url.pathname.includes(`/${bucketName}/`);
7
+ function isUrlFromBucket(fileUrl, bucketName, baseUrl = '') {
8
+ const url = new URL(fileUrl);
9
+ // Check if the file URL is using a base URL (e.g. a CDN).
10
+ // In this case do not sign the URL.
11
+ if (baseUrl) {
12
+ return false;
13
+ }
14
+ const { bucket } = getBucketFromAwsUrl(fileUrl);
15
+ if (bucket) {
16
+ return bucket === bucketName;
17
+ }
18
+ // File URL might be of an S3-compatible provider. (or an invalid URL)
19
+ // In this case, check if the bucket name appears in the URL host or path.
20
+ // e.g. https://minio.example.com/bucket-name/object-key
21
+ // e.g. https://bucket.nyc3.digitaloceanspaces.com/folder/img.png
22
+ return url.host.startsWith(`${bucketName}.`) || url.pathname.includes(`/${bucketName}/`);
16
23
  }
17
- function getBucketFromAwsUrl(fileUrl) {
18
- const url = new URL(fileUrl);
19
- if (url.protocol === "s3:") {
20
- const bucket = url.host;
21
- if (!bucket) {
22
- return { err: `Invalid S3 url: no bucket: ${url}` };
24
+ /**
25
+ * Parse the bucket name from a URL.
26
+ * See all URL formats in https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-bucket-intro.html
27
+ *
28
+ * @param {string} fileUrl - the URL to parse
29
+ * @returns {object} result
30
+ * @returns {string} result.bucket - the bucket name
31
+ * @returns {string} result.err - if any
32
+ */ function getBucketFromAwsUrl(fileUrl) {
33
+ const url = new URL(fileUrl);
34
+ // S3://<bucket-name>/<key>
35
+ if (url.protocol === 's3:') {
36
+ const bucket = url.host;
37
+ if (!bucket) {
38
+ return {
39
+ err: `Invalid S3 url: no bucket: ${url}`
40
+ };
41
+ }
42
+ return {
43
+ bucket
44
+ };
23
45
  }
24
- return { bucket };
25
- }
26
- if (!url.host) {
27
- return { err: `Invalid S3 url: no hostname: ${url}` };
28
- }
29
- const matches = url.host.match(ENDPOINT_PATTERN);
30
- if (!matches) {
31
- return { err: `Invalid S3 url: hostname does not appear to be a valid S3 endpoint: ${url}` };
32
- }
33
- const prefix = matches[1];
34
- if (!prefix) {
35
- if (url.pathname === "/") {
36
- return { bucket: null };
46
+ if (!url.host) {
47
+ return {
48
+ err: `Invalid S3 url: no hostname: ${url}`
49
+ };
37
50
  }
38
- const index2 = url.pathname.indexOf("/", 1);
39
- if (index2 === -1) {
40
- return { bucket: url.pathname.substring(1) };
51
+ const matches = url.host.match(ENDPOINT_PATTERN);
52
+ if (!matches) {
53
+ return {
54
+ err: `Invalid S3 url: hostname does not appear to be a valid S3 endpoint: ${url}`
55
+ };
41
56
  }
42
- if (index2 === url.pathname.length - 1) {
43
- return { bucket: url.pathname.substring(1, index2) };
57
+ const prefix = matches[1];
58
+ // https://s3.amazonaws.com/<bucket-name>
59
+ if (!prefix) {
60
+ if (url.pathname === '/') {
61
+ return {
62
+ bucket: null
63
+ };
64
+ }
65
+ const index = url.pathname.indexOf('/', 1);
66
+ // https://s3.amazonaws.com/<bucket-name>
67
+ if (index === -1) {
68
+ return {
69
+ bucket: url.pathname.substring(1)
70
+ };
71
+ }
72
+ // https://s3.amazonaws.com/<bucket-name>/
73
+ if (index === url.pathname.length - 1) {
74
+ return {
75
+ bucket: url.pathname.substring(1, index)
76
+ };
77
+ }
78
+ // https://s3.amazonaws.com/<bucket-name>/key
79
+ return {
80
+ bucket: url.pathname.substring(1, index)
81
+ };
44
82
  }
45
- return { bucket: url.pathname.substring(1, index2) };
46
- }
47
- return { bucket: prefix.substring(0, prefix.length - 1) };
48
- }
49
- const extractCredentials = (options) => {
50
- if (options.accessKeyId && options.secretAccessKey) {
51
- return {
52
- accessKeyId: options.accessKeyId,
53
- secretAccessKey: options.secretAccessKey
54
- };
55
- }
56
- if (options.s3Options?.accessKeyId && options.s3Options.secretAccessKey) {
57
- process.emitWarning(
58
- "Credentials passed directly to s3Options is deprecated and will be removed in a future release. Please wrap them inside a credentials object."
59
- );
83
+ // https://<bucket-name>.s3.amazonaws.com/
60
84
  return {
61
- accessKeyId: options.s3Options.accessKeyId,
62
- secretAccessKey: options.s3Options.secretAccessKey
85
+ bucket: prefix.substring(0, prefix.length - 1)
63
86
  };
64
- }
65
- if (options.s3Options?.credentials) {
66
- return {
67
- accessKeyId: options.s3Options.credentials.accessKeyId,
68
- secretAccessKey: options.s3Options.credentials.secretAccessKey
69
- };
70
- }
71
- return null;
72
- };
73
- const assertUrlProtocol = (url) => {
74
- return /^\w*:\/\//.test(url);
87
+ }
88
+ const extractCredentials = (options)=>{
89
+ if (options.s3Options?.credentials) {
90
+ return {
91
+ accessKeyId: options.s3Options.credentials.accessKeyId,
92
+ secretAccessKey: options.s3Options.credentials.secretAccessKey
93
+ };
94
+ }
95
+ return null;
75
96
  };
76
- const getConfig = ({ baseUrl, rootPath, s3Options, ...legacyS3Options }) => {
77
- if (Object.keys(legacyS3Options).length > 0) {
78
- process.emitWarning(
79
- "S3 configuration options passed at root level of the plugin's providerOptions is deprecated and will be removed in a future release. Please wrap them inside the 's3Options:{}' property."
80
- );
81
- }
82
- const credentials = extractCredentials({ s3Options, ...legacyS3Options });
83
- const config = {
84
- ...s3Options,
85
- ...legacyS3Options,
86
- ...credentials ? { credentials } : {}
87
- };
88
- config.params.ACL = getOr(ObjectCannedACL.public_read, ["params", "ACL"], config);
89
- return config;
97
+
98
+ const assertUrlProtocol = (url)=>{
99
+ // Regex to test protocol like "http://", "https://"
100
+ return /^\w*:\/\//.test(url);
90
101
  };
91
- const index = {
92
- init({ baseUrl, rootPath, s3Options, ...legacyS3Options }) {
93
- const config = getConfig({ baseUrl, rootPath, s3Options, ...legacyS3Options });
94
- const s3Client = new S3Client(config);
95
- const filePrefix = rootPath ? `${rootPath.replace(/\/+$/, "")}/` : "";
96
- const getFileKey = (file) => {
97
- const path = file.path ? `${file.path}/` : "";
98
- return `${filePrefix}${path}${file.hash}${file.ext}`;
99
- };
100
- const upload = async (file, customParams = {}) => {
101
- const fileKey = getFileKey(file);
102
- const uploadObj = new Upload({
103
- client: s3Client,
104
- params: {
105
- Bucket: config.params.Bucket,
106
- Key: fileKey,
107
- Body: file.stream || Buffer.from(file.buffer, "binary"),
108
- ACL: config.params.ACL,
109
- ContentType: file.mime,
110
- ...customParams
111
- }
112
- });
113
- const upload2 = await uploadObj.done();
114
- if (assertUrlProtocol(upload2.Location)) {
115
- file.url = baseUrl ? `${baseUrl}/${fileKey}` : upload2.Location;
116
- } else {
117
- file.url = `https://${upload2.Location}`;
118
- }
119
- };
120
- return {
121
- isPrivate() {
122
- return config.params.ACL === "private";
123
- },
124
- async getSignedUrl(file, customParams) {
125
- if (!isUrlFromBucket(file.url, config.params.Bucket, baseUrl)) {
126
- return { url: file.url };
127
- }
128
- const fileKey = getFileKey(file);
129
- const url = await getSignedUrl(
130
- s3Client,
131
- new GetObjectCommand({
132
- Bucket: config.params.Bucket,
133
- Key: fileKey,
134
- ...customParams
135
- }),
136
- {
137
- expiresIn: getOr(15 * 60, ["params", "signedUrlExpires"], config)
138
- }
139
- );
140
- return { url };
141
- },
142
- uploadStream(file, customParams = {}) {
143
- return upload(file, customParams);
144
- },
145
- upload(file, customParams = {}) {
146
- return upload(file, customParams);
147
- },
148
- delete(file, customParams = {}) {
149
- const command = new DeleteObjectCommand({
150
- Bucket: config.params.Bucket,
151
- Key: getFileKey(file),
152
- ...customParams
153
- });
154
- return s3Client.send(command);
155
- }
102
+ const getConfig = ({ baseUrl, rootPath, s3Options, ...legacyS3Options })=>{
103
+ if (Object.keys(legacyS3Options).length > 0) {
104
+ process.emitWarning("S3 configuration options passed at root level of the plugin's providerOptions is deprecated and will be removed in a future release. Please wrap them inside the 's3Options:{}' property.");
105
+ }
106
+ const credentials = extractCredentials({
107
+ s3Options,
108
+ ...legacyS3Options
109
+ });
110
+ const config = {
111
+ ...s3Options,
112
+ ...legacyS3Options,
113
+ ...credentials ? {
114
+ credentials
115
+ } : {}
156
116
  };
157
- }
117
+ config.params.ACL = getOr(ObjectCannedACL.public_read, [
118
+ 'params',
119
+ 'ACL'
120
+ ], config);
121
+ return config;
158
122
  };
159
- export {
160
- index as default
123
+ var index = {
124
+ init ({ baseUrl, rootPath, s3Options, ...legacyS3Options }) {
125
+ // TODO V5 change config structure to avoid having to do this
126
+ const config = getConfig({
127
+ baseUrl,
128
+ rootPath,
129
+ s3Options,
130
+ ...legacyS3Options
131
+ });
132
+ const s3Client = new S3Client(config);
133
+ const filePrefix = rootPath ? `${rootPath.replace(/\/+$/, '')}/` : '';
134
+ const getFileKey = (file)=>{
135
+ const path = file.path ? `${file.path}/` : '';
136
+ return `${filePrefix}${path}${file.hash}${file.ext}`;
137
+ };
138
+ const upload = async (file, customParams = {})=>{
139
+ const fileKey = getFileKey(file);
140
+ const uploadObj = new Upload({
141
+ client: s3Client,
142
+ params: {
143
+ Bucket: config.params.Bucket,
144
+ Key: fileKey,
145
+ Body: file.stream || Buffer.from(file.buffer, 'binary'),
146
+ ACL: config.params.ACL,
147
+ ContentType: file.mime,
148
+ ...customParams
149
+ }
150
+ });
151
+ const upload = await uploadObj.done();
152
+ if (assertUrlProtocol(upload.Location)) {
153
+ file.url = baseUrl ? `${baseUrl}/${fileKey}` : upload.Location;
154
+ } else {
155
+ // Default protocol to https protocol
156
+ file.url = `https://${upload.Location}`;
157
+ }
158
+ };
159
+ return {
160
+ isPrivate () {
161
+ return config.params.ACL === 'private';
162
+ },
163
+ async getSignedUrl (file, customParams) {
164
+ // Do not sign the url if it does not come from the same bucket.
165
+ if (!isUrlFromBucket(file.url, config.params.Bucket, baseUrl)) {
166
+ return {
167
+ url: file.url
168
+ };
169
+ }
170
+ const fileKey = getFileKey(file);
171
+ const url = await getSignedUrl(// @ts-expect-error - TODO fix client type
172
+ s3Client, new GetObjectCommand({
173
+ Bucket: config.params.Bucket,
174
+ Key: fileKey,
175
+ ...customParams
176
+ }), {
177
+ expiresIn: getOr(15 * 60, [
178
+ 'params',
179
+ 'signedUrlExpires'
180
+ ], config)
181
+ });
182
+ return {
183
+ url
184
+ };
185
+ },
186
+ uploadStream (file, customParams = {}) {
187
+ return upload(file, customParams);
188
+ },
189
+ upload (file, customParams = {}) {
190
+ return upload(file, customParams);
191
+ },
192
+ delete (file, customParams = {}) {
193
+ const command = new DeleteObjectCommand({
194
+ Bucket: config.params.Bucket,
195
+ Key: getFileKey(file),
196
+ ...customParams
197
+ });
198
+ return s3Client.send(command);
199
+ }
200
+ };
201
+ }
161
202
  };
203
+
204
+ export { index as default };
162
205
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../src/utils.ts","../src/index.ts"],"sourcesContent":["import type { AwsCredentialIdentity } from '@aws-sdk/types';\nimport type { InitOptions } from '.';\n\nconst ENDPOINT_PATTERN = /^(.+\\.)?s3[.-]([a-z0-9-]+)\\./;\n\ninterface BucketInfo {\n bucket?: string | null;\n err?: string;\n}\n\nexport function isUrlFromBucket(fileUrl: string, bucketName: string, baseUrl = ''): boolean {\n const url = new URL(fileUrl);\n\n // Check if the file URL is using a base URL (e.g. a CDN).\n // In this case do not sign the URL.\n if (baseUrl) {\n return false;\n }\n\n const { bucket } = getBucketFromAwsUrl(fileUrl);\n\n if (bucket) {\n return bucket === bucketName;\n }\n\n // File URL might be of an S3-compatible provider. (or an invalid URL)\n // In this case, check if the bucket name appears in the URL host or path.\n // e.g. https://minio.example.com/bucket-name/object-key\n // e.g. https://bucket.nyc3.digitaloceanspaces.com/folder/img.png\n return url.host.startsWith(`${bucketName}.`) || url.pathname.includes(`/${bucketName}/`);\n}\n\n/**\n * Parse the bucket name from a URL.\n * See all URL formats in https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-bucket-intro.html\n *\n * @param {string} fileUrl - the URL to parse\n * @returns {object} result\n * @returns {string} result.bucket - the bucket name\n * @returns {string} result.err - if any\n */\nfunction getBucketFromAwsUrl(fileUrl: string): BucketInfo {\n const url = new URL(fileUrl);\n\n // S3://<bucket-name>/<key>\n if (url.protocol === 's3:') {\n const bucket = url.host;\n\n if (!bucket) {\n return { err: `Invalid S3 url: no bucket: ${url}` };\n }\n return { bucket };\n }\n\n if (!url.host) {\n return { err: `Invalid S3 url: no hostname: ${url}` };\n }\n\n const matches = url.host.match(ENDPOINT_PATTERN);\n if (!matches) {\n return { err: `Invalid S3 url: hostname does not appear to be a valid S3 endpoint: ${url}` };\n }\n\n const prefix = matches[1];\n // https://s3.amazonaws.com/<bucket-name>\n if (!prefix) {\n if (url.pathname === '/') {\n return { bucket: null };\n }\n\n const index = url.pathname.indexOf('/', 1);\n\n // https://s3.amazonaws.com/<bucket-name>\n if (index === -1) {\n return { bucket: url.pathname.substring(1) };\n }\n\n // https://s3.amazonaws.com/<bucket-name>/\n if (index === url.pathname.length - 1) {\n return { bucket: url.pathname.substring(1, index) };\n }\n\n // https://s3.amazonaws.com/<bucket-name>/key\n return { bucket: url.pathname.substring(1, index) };\n }\n\n // https://<bucket-name>.s3.amazonaws.com/\n return { bucket: prefix.substring(0, prefix.length - 1) };\n}\n\n// TODO Remove this in V5 since we will only support the new config structure\nexport const extractCredentials = (options: InitOptions): AwsCredentialIdentity | null => {\n // legacy\n if (options.accessKeyId && options.secretAccessKey) {\n return {\n accessKeyId: options.accessKeyId,\n secretAccessKey: options.secretAccessKey,\n };\n }\n // Legacy\n if (options.s3Options?.accessKeyId && options.s3Options.secretAccessKey) {\n process.emitWarning(\n 'Credentials passed directly to s3Options is deprecated and will be removed in a future release. Please wrap them inside a credentials object.'\n );\n return {\n accessKeyId: options.s3Options.accessKeyId,\n secretAccessKey: options.s3Options.secretAccessKey,\n };\n }\n // V5\n if (options.s3Options?.credentials) {\n return {\n accessKeyId: options.s3Options.credentials.accessKeyId,\n secretAccessKey: options.s3Options.credentials.secretAccessKey,\n };\n }\n return null;\n};\n","import type { ReadStream } from 'node:fs';\nimport { getOr } from 'lodash/fp';\nimport {\n S3Client,\n GetObjectCommand,\n DeleteObjectCommand,\n DeleteObjectCommandOutput,\n PutObjectCommandInput,\n CompleteMultipartUploadCommandOutput,\n AbortMultipartUploadCommandOutput,\n S3ClientConfig,\n ObjectCannedACL,\n} from '@aws-sdk/client-s3';\nimport type { AwsCredentialIdentity } from '@aws-sdk/types';\nimport { getSignedUrl } from '@aws-sdk/s3-request-presigner';\nimport { Upload } from '@aws-sdk/lib-storage';\nimport { extractCredentials, isUrlFromBucket } from './utils';\n\nexport interface File {\n name: string;\n alternativeText?: string;\n caption?: string;\n width?: number;\n height?: number;\n formats?: Record<string, unknown>;\n hash: string;\n ext?: string;\n mime: string;\n size: number;\n sizeInBytes: number;\n url: string;\n previewUrl?: string;\n path?: string;\n provider?: string;\n provider_metadata?: Record<string, unknown>;\n stream?: ReadStream;\n buffer?: Buffer;\n}\n\nexport type UploadCommandOutput = (\n | CompleteMultipartUploadCommandOutput\n | AbortMultipartUploadCommandOutput\n) & {\n Location: string;\n};\n\nexport interface AWSParams {\n Bucket: string; // making it required\n ACL?: ObjectCannedACL;\n signedUrlExpires?: number;\n}\n\nexport interface DefaultOptions extends S3ClientConfig {\n // TODO Remove this in V5\n accessKeyId?: AwsCredentialIdentity['accessKeyId'];\n secretAccessKey?: AwsCredentialIdentity['secretAccessKey'];\n // Keep this for V5\n credentials?: AwsCredentialIdentity;\n params?: AWSParams;\n [k: string]: any;\n}\n\nexport type InitOptions = (DefaultOptions | { s3Options: DefaultOptions }) & {\n baseUrl?: string;\n rootPath?: string;\n [k: string]: any;\n};\n\nconst assertUrlProtocol = (url: string) => {\n // Regex to test protocol like \"http://\", \"https://\"\n return /^\\w*:\\/\\//.test(url);\n};\n\nconst getConfig = ({ baseUrl, rootPath, s3Options, ...legacyS3Options }: InitOptions) => {\n if (Object.keys(legacyS3Options).length > 0) {\n process.emitWarning(\n \"S3 configuration options passed at root level of the plugin's providerOptions is deprecated and will be removed in a future release. Please wrap them inside the 's3Options:{}' property.\"\n );\n }\n const credentials = extractCredentials({ s3Options, ...legacyS3Options });\n const config = {\n ...s3Options,\n ...legacyS3Options,\n ...(credentials ? { credentials } : {}),\n };\n\n config.params.ACL = getOr(ObjectCannedACL.public_read, ['params', 'ACL'], config);\n\n return config;\n};\n\nexport default {\n init({ baseUrl, rootPath, s3Options, ...legacyS3Options }: InitOptions) {\n // TODO V5 change config structure to avoid having to do this\n const config = getConfig({ baseUrl, rootPath, s3Options, ...legacyS3Options });\n const s3Client = new S3Client(config);\n const filePrefix = rootPath ? `${rootPath.replace(/\\/+$/, '')}/` : '';\n\n const getFileKey = (file: File) => {\n const path = file.path ? `${file.path}/` : '';\n return `${filePrefix}${path}${file.hash}${file.ext}`;\n };\n\n const upload = async (file: File, customParams: Partial<PutObjectCommandInput> = {}) => {\n const fileKey = getFileKey(file);\n const uploadObj = new Upload({\n client: s3Client,\n params: {\n Bucket: config.params.Bucket,\n Key: fileKey,\n Body: file.stream || Buffer.from(file.buffer as any, 'binary'),\n ACL: config.params.ACL,\n ContentType: file.mime,\n ...customParams,\n },\n });\n\n const upload = (await uploadObj.done()) as UploadCommandOutput;\n\n if (assertUrlProtocol(upload.Location)) {\n file.url = baseUrl ? `${baseUrl}/${fileKey}` : upload.Location;\n } else {\n // Default protocol to https protocol\n file.url = `https://${upload.Location}`;\n }\n };\n\n return {\n isPrivate() {\n return config.params.ACL === 'private';\n },\n\n async getSignedUrl(file: File, customParams: any): Promise<{ url: string }> {\n // Do not sign the url if it does not come from the same bucket.\n if (!isUrlFromBucket(file.url, config.params.Bucket, baseUrl)) {\n return { url: file.url };\n }\n const fileKey = getFileKey(file);\n\n const url = await getSignedUrl(\n s3Client,\n new GetObjectCommand({\n Bucket: config.params.Bucket,\n Key: fileKey,\n ...customParams,\n }),\n {\n expiresIn: getOr(15 * 60, ['params', 'signedUrlExpires'], config),\n }\n );\n\n return { url };\n },\n uploadStream(file: File, customParams = {}) {\n return upload(file, customParams);\n },\n upload(file: File, customParams = {}) {\n return upload(file, customParams);\n },\n delete(file: File, customParams = {}): Promise<DeleteObjectCommandOutput> {\n const command = new DeleteObjectCommand({\n Bucket: config.params.Bucket,\n Key: getFileKey(file),\n ...customParams,\n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A,YACL,GAAG;AAAA,UAAA,CACJ;AAAA,UACD;AAAA,YACE,WAAW,MAAM,KAAK,IAAI,CAAC,UAAU,kBAAkB,GAAG,MAAM;AAAA,UAClE;AAAA,QAAA;AAGF,eAAO,EAAE,IAAI;AAAA,MACf;AAAA,MACA,aAAa,MAAY,eAAe,IAAI;AACnC,eAAA,OAAO,MAAM,YAAY;AAAA,MAClC;AAAA,MACA,OAAO,MAAY,eAAe,IAAI;AAC7B,eAAA,OAAO,MAAM,YAAY;AAAA,MAClC;AAAA,MACA,OAAO,MAAY,eAAe,IAAwC;AAClE,cAAA,UAAU,IAAI,oBAAoB;AAAA,UACtC,QAAQ,OAAO,OAAO;AAAA,UACtB,KAAK,WAAW,IAAI;AAAA,UACpB,GAAG;AAAA,QAAA,CACJ;AACM,eAAA,SAAS,KAAK,OAAO;AAAA,MAC9B;AAAA,IAAA;AAAA,EAEJ;AACF;"}
1
+ {"version":3,"file":"index.mjs","sources":["../src/utils.ts","../src/index.ts"],"sourcesContent":["import type { AwsCredentialIdentity } from '@aws-sdk/types';\nimport type { InitOptions } from '.';\n\nconst ENDPOINT_PATTERN = /^(.+\\.)?s3[.-]([a-z0-9-]+)\\./;\n\ninterface BucketInfo {\n bucket?: string | null;\n err?: string;\n}\n\nexport function isUrlFromBucket(fileUrl: string, bucketName: string, baseUrl = ''): boolean {\n const url = new URL(fileUrl);\n\n // Check if the file URL is using a base URL (e.g. a CDN).\n // In this case do not sign the URL.\n if (baseUrl) {\n return false;\n }\n\n const { bucket } = getBucketFromAwsUrl(fileUrl);\n\n if (bucket) {\n return bucket === bucketName;\n }\n\n // File URL might be of an S3-compatible provider. (or an invalid URL)\n // In this case, check if the bucket name appears in the URL host or path.\n // e.g. https://minio.example.com/bucket-name/object-key\n // e.g. https://bucket.nyc3.digitaloceanspaces.com/folder/img.png\n return url.host.startsWith(`${bucketName}.`) || url.pathname.includes(`/${bucketName}/`);\n}\n\n/**\n * Parse the bucket name from a URL.\n * See all URL formats in https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-bucket-intro.html\n *\n * @param {string} fileUrl - the URL to parse\n * @returns {object} result\n * @returns {string} result.bucket - the bucket name\n * @returns {string} result.err - if any\n */\nfunction getBucketFromAwsUrl(fileUrl: string): BucketInfo {\n const url = new URL(fileUrl);\n\n // S3://<bucket-name>/<key>\n if (url.protocol === 's3:') {\n const bucket = url.host;\n\n if (!bucket) {\n return { err: `Invalid S3 url: no bucket: ${url}` };\n }\n return { bucket };\n }\n\n if (!url.host) {\n return { err: `Invalid S3 url: no hostname: ${url}` };\n }\n\n const matches = url.host.match(ENDPOINT_PATTERN);\n if (!matches) {\n return { err: `Invalid S3 url: hostname does not appear to be a valid S3 endpoint: ${url}` };\n }\n\n const prefix = matches[1];\n // https://s3.amazonaws.com/<bucket-name>\n if (!prefix) {\n if (url.pathname === '/') {\n return { bucket: null };\n }\n\n const index = url.pathname.indexOf('/', 1);\n\n // https://s3.amazonaws.com/<bucket-name>\n if (index === -1) {\n return { bucket: url.pathname.substring(1) };\n }\n\n // https://s3.amazonaws.com/<bucket-name>/\n if (index === url.pathname.length - 1) {\n return { bucket: url.pathname.substring(1, index) };\n }\n\n // https://s3.amazonaws.com/<bucket-name>/key\n return { bucket: url.pathname.substring(1, index) };\n }\n\n // https://<bucket-name>.s3.amazonaws.com/\n return { bucket: prefix.substring(0, prefix.length - 1) };\n}\n\nexport const extractCredentials = (options: InitOptions): AwsCredentialIdentity | null => {\n if (options.s3Options?.credentials) {\n return {\n accessKeyId: options.s3Options.credentials.accessKeyId,\n secretAccessKey: options.s3Options.credentials.secretAccessKey,\n };\n }\n return null;\n};\n","import type { ReadStream } from 'node:fs';\nimport { getOr } from 'lodash/fp';\nimport {\n S3Client,\n GetObjectCommand,\n DeleteObjectCommand,\n DeleteObjectCommandOutput,\n PutObjectCommandInput,\n CompleteMultipartUploadCommandOutput,\n AbortMultipartUploadCommandOutput,\n S3ClientConfig,\n ObjectCannedACL,\n} from '@aws-sdk/client-s3';\nimport type { AwsCredentialIdentity } from '@aws-sdk/types';\nimport { getSignedUrl } from '@aws-sdk/s3-request-presigner';\nimport { Upload } from '@aws-sdk/lib-storage';\nimport { extractCredentials, isUrlFromBucket } from './utils';\n\nexport interface File {\n name: string;\n alternativeText?: string;\n caption?: string;\n width?: number;\n height?: number;\n formats?: Record<string, unknown>;\n hash: string;\n ext?: string;\n mime: string;\n size: number;\n sizeInBytes: number;\n url: string;\n previewUrl?: string;\n path?: string;\n provider?: string;\n provider_metadata?: Record<string, unknown>;\n stream?: ReadStream;\n buffer?: Buffer;\n}\n\nexport type UploadCommandOutput = (\n | CompleteMultipartUploadCommandOutput\n | AbortMultipartUploadCommandOutput\n) & {\n Location: string;\n};\n\nexport interface AWSParams {\n Bucket: string; // making it required\n ACL?: ObjectCannedACL;\n signedUrlExpires?: number;\n}\n\nexport interface DefaultOptions extends S3ClientConfig {\n // TODO Remove this in V5\n accessKeyId?: AwsCredentialIdentity['accessKeyId'];\n secretAccessKey?: AwsCredentialIdentity['secretAccessKey'];\n // Keep this for V5\n credentials?: AwsCredentialIdentity;\n params?: AWSParams;\n [k: string]: any;\n}\n\nexport type InitOptions = (DefaultOptions | { s3Options: DefaultOptions }) & {\n baseUrl?: string;\n rootPath?: string;\n [k: string]: any;\n};\n\nconst assertUrlProtocol = (url: string) => {\n // Regex to test protocol like \"http://\", \"https://\"\n return /^\\w*:\\/\\//.test(url);\n};\n\nconst getConfig = ({ baseUrl, rootPath, s3Options, ...legacyS3Options }: InitOptions) => {\n if (Object.keys(legacyS3Options).length > 0) {\n process.emitWarning(\n \"S3 configuration options passed at root level of the plugin's providerOptions is deprecated and will be removed in a future release. Please wrap them inside the 's3Options:{}' property.\"\n );\n }\n const credentials = extractCredentials({ s3Options, ...legacyS3Options });\n const config = {\n ...s3Options,\n ...legacyS3Options,\n ...(credentials ? { credentials } : {}),\n };\n\n config.params.ACL = getOr(ObjectCannedACL.public_read, ['params', 'ACL'], config);\n\n return config;\n};\n\nexport default {\n init({ baseUrl, rootPath, s3Options, ...legacyS3Options }: InitOptions) {\n // TODO V5 change config structure to avoid having to do this\n const config = getConfig({ baseUrl, rootPath, s3Options, ...legacyS3Options });\n const s3Client = new S3Client(config);\n const filePrefix = rootPath ? `${rootPath.replace(/\\/+$/, '')}/` : '';\n\n const getFileKey = (file: File) => {\n const path = file.path ? `${file.path}/` : '';\n return `${filePrefix}${path}${file.hash}${file.ext}`;\n };\n\n const upload = async (file: File, customParams: Partial<PutObjectCommandInput> = {}) => {\n const fileKey = getFileKey(file);\n const uploadObj = new Upload({\n client: s3Client,\n params: {\n Bucket: config.params.Bucket,\n Key: fileKey,\n Body: file.stream || Buffer.from(file.buffer as any, 'binary'),\n ACL: config.params.ACL,\n ContentType: file.mime,\n ...customParams,\n },\n });\n\n const upload = (await uploadObj.done()) as UploadCommandOutput;\n\n if (assertUrlProtocol(upload.Location)) {\n file.url = baseUrl ? `${baseUrl}/${fileKey}` : upload.Location;\n } else {\n // Default protocol to https protocol\n file.url = `https://${upload.Location}`;\n }\n };\n\n return {\n isPrivate() {\n return config.params.ACL === 'private';\n },\n\n async getSignedUrl(file: File, customParams: any): Promise<{ url: string }> {\n // Do not sign the url if it does not come from the same bucket.\n if (!isUrlFromBucket(file.url, config.params.Bucket, baseUrl)) {\n return { url: file.url };\n }\n const fileKey = getFileKey(file);\n\n const url = await getSignedUrl(\n // @ts-expect-error - TODO fix client type\n s3Client,\n new GetObjectCommand({\n Bucket: config.params.Bucket,\n Key: fileKey,\n ...customParams,\n }),\n {\n expiresIn: getOr(15 * 60, ['params', 'signedUrlExpires'], config),\n }\n );\n\n return { url };\n },\n uploadStream(file: File, customParams = {}) {\n return upload(file, customParams);\n },\n upload(file: File, customParams = {}) {\n return upload(file, customParams);\n },\n delete(file: File, customParams = {}): Promise<DeleteObjectCommandOutput> {\n const command = new DeleteObjectCommand({\n Bucket: config.params.Bucket,\n Key: getFileKey(file),\n ...customParams,\n });\n return s3Client.send(command);\n },\n };\n },\n};\n"],"names":["ENDPOINT_PATTERN","isUrlFromBucket","fileUrl","bucketName","baseUrl","url","URL","bucket","getBucketFromAwsUrl","host","startsWith","pathname","includes","protocol","err","matches","match","prefix","index","indexOf","substring","length","extractCredentials","options","s3Options","credentials","accessKeyId","secretAccessKey","assertUrlProtocol","test","getConfig","rootPath","legacyS3Options","Object","keys","process","emitWarning","config","params","ACL","getOr","ObjectCannedACL","public_read","init","s3Client","S3Client","filePrefix","replace","getFileKey","file","path","hash","ext","upload","customParams","fileKey","uploadObj","Upload","client","Bucket","Key","Body","stream","Buffer","from","buffer","ContentType","mime","done","Location","isPrivate","getSignedUrl","GetObjectCommand","expiresIn","uploadStream","delete","command","DeleteObjectCommand","send"],"mappings":";;;;;AAGA,MAAMA,gBAAmB,GAAA,8BAAA;AAOlB,SAASC,eAAgBC,CAAAA,OAAe,EAAEC,UAAkB,EAAEC,UAAU,EAAE,EAAA;IAC/E,MAAMC,GAAAA,GAAM,IAAIC,GAAIJ,CAAAA,OAAAA,CAAAA;;;AAIpB,IAAA,IAAIE,OAAS,EAAA;QACX,OAAO,KAAA;AACT;AAEA,IAAA,MAAM,EAAEG,MAAM,EAAE,GAAGC,mBAAoBN,CAAAA,OAAAA,CAAAA;AAEvC,IAAA,IAAIK,MAAQ,EAAA;AACV,QAAA,OAAOA,MAAWJ,KAAAA,UAAAA;AACpB;;;;;IAMA,OAAOE,GAAAA,CAAII,IAAI,CAACC,UAAU,CAAC,CAAC,EAAEP,WAAW,CAAC,CAAC,KAAKE,GAAIM,CAAAA,QAAQ,CAACC,QAAQ,CAAC,CAAC,CAAC,EAAET,UAAW,CAAA,CAAC,CAAC,CAAA;AACzF;AAEA;;;;;;;;IASA,SAASK,oBAAoBN,OAAe,EAAA;IAC1C,MAAMG,GAAAA,GAAM,IAAIC,GAAIJ,CAAAA,OAAAA,CAAAA;;IAGpB,IAAIG,GAAAA,CAAIQ,QAAQ,KAAK,KAAO,EAAA;QAC1B,MAAMN,MAAAA,GAASF,IAAII,IAAI;A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A0BjF,CAAC"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAC5D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,GAAG,CAAC;AASrC,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,OAAO,SAAK,GAAG,OAAO,CAoB1F;AA4DD,eAAO,MAAM,kBAAkB,YAAa,WAAW,KAAG,qBAAqB,GAAG,IAQjF,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/provider-upload-aws-s3",
3
- "version": "0.0.0-next.fd9757603c653ca239c45d6e28ab536d2dae0b39",
3
+ "version": "0.0.0-next.fdac61dd05ca665168f51f655f1d165b55ec4231",
4
4
  "description": "AWS S3 provider for strapi upload",
5
5
  "keywords": [
6
6
  "upload",
@@ -34,15 +34,17 @@
34
34
  "source": "./src/index.ts",
35
35
  "types": "./dist/index.d.ts",
36
36
  "files": [
37
- "./dist"
37
+ "dist/"
38
38
  ],
39
39
  "scripts": {
40
- "build": "pack-up build",
40
+ "build": "run -T npm-run-all clean --parallel build:code build:types",
41
+ "build:code": "run -T rollup -c",
42
+ "build:types": "run -T tsc -p tsconfig.build.json --emitDeclarationOnly",
41
43
  "clean": "run -T rimraf ./dist",
42
44
  "lint": "run -T eslint .",
43
45
  "test:unit": "run -T jest",
44
46
  "test:unit:watch": "run -T jest --watch",
45
- "watch": "pack-up watch"
47
+ "watch": "run -T rollup -c -w"
46
48
  },
47
49
  "dependencies": {
48
50
  "@aws-sdk/client-s3": "3.600.0",
@@ -52,14 +54,12 @@
52
54
  "lodash": "4.17.21"
53
55
  },
54
56
  "devDependencies": {
55
- "@strapi/pack-up": "4.23.0",
56
57
  "@types/jest": "29.5.2",
57
- "eslint-config-custom": "0.0.0-next.fd9757603c653ca239c45d6e28ab536d2dae0b39",
58
- "tsconfig": "0.0.0-next.fd9757603c653ca239c45d6e28ab536d2dae0b39"
58
+ "eslint-config-custom": "0.0.0-next.fdac61dd05ca665168f51f655f1d165b55ec4231",
59
+ "tsconfig": "0.0.0-next.fdac61dd05ca665168f51f655f1d165b55ec4231"
59
60
  },
60
61
  "engines": {
61
- "node": ">=18.0.0 <=20.x.x",
62
+ "node": ">=18.0.0 <=22.x.x",
62
63
  "npm": ">=6.0.0"
63
- },
64
- "gitHead": "fd9757603c653ca239c45d6e28ab536d2dae0b39"
64
+ }
65
65
  }