Comparing version 0.2.5 to 0.2.6
{ | ||
"name": "s3u", | ||
"version": "0.2.5", | ||
"version": "0.2.6", | ||
"description": "S3 URL manipulation helper similar to standard URL class", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -27,8 +27,8 @@ export class S3Url { | ||
setBucket(bucket: string): void; | ||
setBucketPosition(position: 'hostname' | 'pathname'): void; | ||
setCdn(cdn: boolean): void; | ||
setDomain(domain: string): void; | ||
setKey(key: string): void; | ||
setRegion(region: string): void; | ||
setBucket(bucket: string): this; | ||
setBucketPosition(position: 'hostname' | 'pathname'): this; | ||
setCdn(cdn: boolean): this; | ||
setDomain(domain: string): this; | ||
setKey(key: string): this; | ||
setRegion(region: string): this; | ||
@@ -35,0 +35,0 @@ sign(opts?: { |
@@ -26,2 +26,27 @@ 'use strict'; | ||
async buildSignedUrl({ | ||
accessKeyId, | ||
secretAccessKey, | ||
expires, | ||
method, | ||
s3Url, | ||
}) { | ||
const signedUrl = await super.buildSignedUrl({ | ||
accessKeyId, | ||
secretAccessKey, | ||
expires, | ||
method, | ||
// DO uses the same signature for CDN and normal endpoints | ||
s3Url: s3Url.clone({ cdn: false }), | ||
}); | ||
if (s3Url.cdn) { | ||
return this.parseUrl({ url: signedUrl }) | ||
.setCdn(true) | ||
.href; | ||
} | ||
return signedUrl; | ||
} | ||
parseBucket(hostname, s3Url) { | ||
@@ -28,0 +53,0 @@ const hostnameParts = hostname.split('.'); |
@@ -7,3 +7,3 @@ 'use strict'; | ||
const { decodeS3Key, encodeS3Key } = require('./utils/s3key'); | ||
const { bufferToHex, hash, hmac } = require('./utils/crypto'); | ||
const { bufferToHex, hmacSha256, sha256 } = require('./utils/crypto'); | ||
@@ -67,7 +67,7 @@ class S3Provider { | ||
const signString = [algo, time, scope, await hash(request)].join('\n'); | ||
const signString = [algo, time, scope, await sha256(request)].join('\n'); | ||
const signPromise = [date, s3Url.region, 's3', 'aws4_request', signString] | ||
.reduce( | ||
(promise, data) => promise.then((prev) => hmac(data, prev)), | ||
(promise, data) => promise.then((prev) => hmacSha256(data, prev)), | ||
Promise.resolve('AWS4' + secretAccessKey) | ||
@@ -74,0 +74,0 @@ ); |
@@ -93,2 +93,3 @@ 'use strict'; | ||
this.cdn = cdn; | ||
return this; | ||
} | ||
@@ -98,2 +99,3 @@ | ||
this.domain = domain; | ||
return this; | ||
} | ||
@@ -103,2 +105,3 @@ | ||
this.key = key; | ||
return this; | ||
} | ||
@@ -108,6 +111,7 @@ | ||
this.region = region; | ||
return this; | ||
} | ||
toString() { | ||
return this.makeUrl(); | ||
return this.href; | ||
} | ||
@@ -114,0 +118,0 @@ |
@@ -5,7 +5,7 @@ 'use strict'; | ||
module.exports = { bufferToHex, hash, hmac }; | ||
module.exports = { bufferToHex, hmacSha256, sha256 }; | ||
const encoder = new TextEncoder(); | ||
async function hmac(message, secret) { | ||
async function hmacSha256(message, secret) { | ||
const cryptoKey = await window.crypto.subtle.importKey( | ||
@@ -21,3 +21,3 @@ 'raw', | ||
async function hash(message) { | ||
async function sha256(message) { | ||
return window.crypto.subtle.digest('SHA-256', toBuffer(message)); | ||
@@ -24,0 +24,0 @@ } |
@@ -5,5 +5,5 @@ 'use strict'; | ||
module.exports = { bufferToHex, hash, hmac }; | ||
module.exports = { bufferToHex, hmacSha256, sha256 }; | ||
async function hmac(message, secret) { | ||
async function hmacSha256(message, secret) { | ||
return crypto | ||
@@ -15,3 +15,3 @@ .createHmac('sha256', secret) | ||
async function hash(message) { | ||
async function sha256(message) { | ||
return crypto | ||
@@ -18,0 +18,0 @@ .createHash('sha256') |
22416
623