-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.js
171 lines (164 loc) · 5.11 KB
/
s3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
var crypto = require("crypto")
, expectArray = {
contents: true
}
module.exports = S3
S3.getXml = getXml
function S3(opts) {
if (!(this instanceof S3)) return new S3(opts)
Object.assign(this, {
protocol: "https",
client: require(opts.protocol === "http" ? "http" : "https"),
endpoint: "s3." + opts.region + ".amazonaws.com",
del: req.bind(this, "DELETE"),
get: req.bind(this, "GET"),
stat: req.bind(this, "HEAD")
}, opts)
}
S3.prototype = {
list: function(path, opts, next) {
return this.get("", Object.assign({
prefix: path || "",
listType: 2,
maxKeys: 10
}, opts), next)
},
put: function(path, data, opts, next) {
return req.call(this, "PUT", path, opts, next, data)
},
url: function(path, opts) {
return awsSig(this, "GET", path, opts, "X-Amz-", { host: this.endpoint }, awsDate(), "UNSIGNED-PAYLOAD").url
}
}
function hash(data) {
return crypto.createHash("sha256").update(data).digest("hex")
}
function hmac(key, data) {
return crypto.createHmac("sha256", key).update(data).digest()
}
function awsDate() {
// YYYYMMDDTHHmmssZ
return new Date().toISOString().replace(/-|:|\.\d*/g, "")
}
function awsSig(s3, method, path, _opts, optsPrefix, headers, longDate, contentHash) {
path = (path || "").replace(/^\/*/, (s3.bucket ? "/" + s3.bucket + "/" : "/"))
var ALGO = "AWS4-HMAC-SHA256"
, shortDate = longDate.slice(0, 8)
, scope = shortDate + "/" + s3.region + "/s3/aws4_request"
, sortedHeaders = Object.keys(headers).sort()
, out = {
Algorithm: ALGO,
Credential: s3.accessId + "/" + scope,
Date: longDate,
Expires: 7 * 24 * 60 * 60,
SignedHeaders: sortedHeaders.join(";").toLowerCase()
}
, opts = assignDashCase(optsPrefix ? out : {}, _opts)
, query = Object.keys(opts).map(queryEnc).sort().join("&") || ""
, canonical = [
method,
path,
query,
sortedHeaders.map(headerEnc).join("\n") + "\n",
out.SignedHeaders,
contentHash
].join("\n")
, signString = [
ALGO,
longDate,
scope,
hash(canonical)
].join("\n")
, signKey = hmac(hmac(hmac(hmac("AWS4" + s3.secret, shortDate), s3.region), "s3"), "aws4_request")
out.sig = hmac(signKey, signString).toString("hex").toLowerCase()
out.url = s3.protocol + "://" + s3.endpoint + path + (query ? "?" + query : "") + (optsPrefix ? "&X-Amz-Signature=" + out.sig : "")
out.auth = ALGO + " Credential=" + out.Credential + ", SignedHeaders=" + out.SignedHeaders + ", Signature=" + out.sig
return out
function assignDashCase(target, source) {
if (source) Object.keys(source).forEach(function(key) {
target[
key === "expires" ? "Expires" : key.replace(/[A-Z]/g, "-$&").toLowerCase().replace(/^-/, "")
] = source[key]
})
return target
}
function queryEnc(key) {
return optsPrefix + key + "=" + encodeURIComponent(opts[key])
}
function headerEnc(header) {
return header.toLowerCase() + (":" + headers[header]).replace(/ +/g, " ")
}
}
function req(method, path, opts, next, data) {
if (typeof opts === "function") {
next = opts
opts = null
}
var s3 = this
, longDate = awsDate()
, contentHash = data ? hash(data) : "UNSIGNED-PAYLOAD"
, headers = {
host: s3.endpoint,
"x-amz-date": longDate,
"x-amz-content-sha256": contentHash
}
if (data) {
headers["Content-Length"] = data.length
}
var signed = awsSig(s3, method, path, opts, "", headers, longDate, contentHash)
headers.Authorization = signed.auth
// Add after signature to exclude from signing
if (s3.userAgent) headers["User-Agent"] = s3.userAgent
if (!next) return new Promise(makeReq)
makeReq(next.bind(null, null), next)
function makeReq(resolve, reject) {
s3.client.request(signed.url, { method: method, headers: headers }, handle).end(data)
function handle(res) {
var data = ""
res.on("data", function(chunk) {
data += chunk.toString("utf8")
})
res.on("end", function() {
if (res.statusCode > 299) {
data = method !== "HEAD" && parseXml(data).error || (
path ? "The specified key does not exist." : "The specified bucket is not valid."
)
return reject(Error(data.message || data))
}
data = method === "HEAD" ? {
size: +res.headers["content-length"],
mtime: new Date(res.headers["last-modified"]),
etag: res.headers.etag
} : parseXml(data)
resolve(data.listBucketResult || data.error || data)
})
res.on("error", reject)
}
}
}
function parseXml(str) {
var key, val
, json = {}
, re = /<(\w)([-\w]+)(?:\/|[^>]*>((?:(?!<\1)[\s\S])*)<\/\1\2)>/gm
for (; (val = re.exec(str)); ) {
key = val[1].toLowerCase() + val[2]
val = val[3] != null ? parseXml(val[3]) : true
if (Array.isArray(json[key])) json[key].push(val)
else json[key] = json[key] != null ? [json[key], val] : expectArray[key] ? [val] : val
}
return key ? json : str
}
function getXml(root, json) {
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + nest(root, json || "", " xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"")
function nest(key, val, attrs) {
return "<" + key + attrs + ">" +
(isObj(val) || Array.isArray(val) ? Object.entries(val).map(entryeMap).join("") : val) +
"</" + key + ">"
}
function entryeMap(e) {
return nest(e[0], e[1], "")
}
}
function isObj(obj) {
return !!obj && obj.constructor === Object
}