/
index.js
236 lines (178 loc) · 8.59 KB
/
index.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
var es = require('event-stream')
, gutil = require('gulp-util')
, AWS = require('aws-sdk')
, path = require('path')
, mime = require('mime')
, hasha = require('hasha')
, _ = require('underscore')
, helper = require('./src/helper.js')
, PluginError = gutil.PluginError
, gulpPrefixer
;
const PLUGIN_NAME = 'gulp-s3-upload';
gulpPrefixer = function (AWS) {
/*
`putObjectParams` now takes in the S3 putObject parameters:
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property
- will have a catch for `bucket` vs `Bucket`
- Will filter out `Body` and `Key` because that is handled by the script and keyTransform
*/
return function (options) {
var stream, _s3 = new AWS.S3()
, the_bucket = options.Bucket || options.bucket
;
if(!the_bucket) {
throw new PluginError(PLUGIN_NAME, "Missing S3 bucket name!");
}
// *NEW* in v1.1.0 - Async File Uploading
stream = es.map(function (file, callback) {
var _stream = this,
keyTransform, keyname, keyparts, filename,
mimetype, mime_lookup_name, metadata
;
file_counts.total++;
if(file.isNull()) {
// Do nothing if no contents
return callback(null);
}
if(file.isStream()) {
// Not supporting streams (for now?).
return callback(new gutil.PluginError(PLUGIN_NAME, 'No stream support.'));
}
// =====================================================
// ============= METHOD TRANSFORMS & LOOKUPS ===========
// =====================================================
// === Key transform ===
// Allow for either keyTransform or nameTransform.
// We're using Key to be consistent with AWS-S3.
keyTransform = options.keyTransform || options.nameTransform;
if(keyTransform) {
// Allow the transform function to take the complete path
// in case the user wants to change the path of the file, too.
keyname = keyTransform(file.relative);
} else {
// otherwise keep it exactly parallel
keyparts = helper.parsePath(file.relative);
keyname = helper.buildName(keyparts.dirname, keyparts.basename + keyparts.extname);
}
keyname = keyname.replace(/\\/g, "/"); // JIC Windows (uses backslashes)
// === Mime Lookup/Transform ============================
mime_lookup_name = keyname;
if (options.mimeTypeLookup) {
mime_lookup_name = options.mimeTypeLookup(keyname);
}
mimetype = mime.lookup(mime_lookup_name);
// === Charset ==========================================
// JIC text files get garbled. Appends to mimetype.
// `charset` field gets filtered out later.
if (options.charset && mimetype == 'text/html') {
mimetype += ';charset=' + options.charset;
}
// === metadataMap =====================================
// Map files (using the keyname) to a metadata object.
// ONLY if `options.Metadata` is undefined.
if (!options.Metadata && options.metadataMap) {
if (helper.isMetadataMapFn(options.metadataMap)) {
metadata = options.metadataMap(keyname);
} else {
metadata = options.metadataMap;
}
}
// ETag Hash Comparison ================================
// *NEW* in 1.1.0; do a local hash comparison to reduce
// the overhead from calling upload anyway.
// Add the option for a different algorithm, JIC for
// some reason the algorithm is not MD5.
// Available algorithms are those available w/
// default node `crypto` plugin. (run `crypto.getCiphers()`)
if(!options.etag_hash) {
// If not defined, default to md5
options.etag_hash = 'md5';
}
hasha.fromFile(path.join(file.base, file.relative),
{'algorithm': options.etag_hash}, function (hasha_err, hash) {
if(hasha_err) {
return callback(new gutil.PluginError(PLUGIN_NAME, "S3 hasha Error: " + err.stack));
}
// *Note: options.Metadata is not filtered out later.
_s3.headObject({
'Bucket': the_bucket,
'Key': keyname
}, function (getErr, getData) {
var objOpts;
if(getErr && getErr.statusCode !== 404) {
return callback(new gutil.PluginError(PLUGIN_NAME, "S3 headObject Error: " + getErr.stack));
}
if(getData && getData.ETag === '"' + hash + '"') {
// AWS ETag doesn't match local ETag
file_counts.unchanged++;
gutil.log(gutil.colors.gray("No Change ..... "), keyname);
callback(null);
} else {
objOpts = helper.filterOptions(options);
objOpts.Bucket = the_bucket;
objOpts.Key = keyname;
objOpts.Body = file.contents;
objOpts.ContentType = mimetype;
objOpts.Metadata = metadata;
if (options.uploadNewFilesOnly && !getData || !options.uploadNewFilesOnly) {
gutil.log(gutil.colors.cyan("Uploading ..... "), keyname);
_s3.putObject(objOpts, function (err, data) {
if (err) {
return callback(new gutil.PluginError(PLUGIN_NAME, "S3 putObject Error: " + err.stack));
}
if (getData) {
if (getData.ETag !== data.ETag) {
gutil.log(gutil.colors.yellow("Updated ....... "), keyname);
file_counts.updated++;
} else {
gutil.log(gutil.colors.gray("No Change ..... "), keyname);
file_counts.unchanged++;
}
} else {
// doesn't exist in bucket, the object is new to the bucket
gutil.log(gutil.colors.green("Uploaded! ..... "), keyname);
file_counts.new++;
}
callback(null);
});
}
}
});
});
});
return stream;
};
};
// ===== EXPORTING MAIN PLUGIN FUNCTION =====
// `config` now takes the paramters from the AWS-SDK constructor:
// http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html#constructor-property
module.exports = function (config) {
var aws_config = config || {};
// Maintain backwards compatibility with legacy key and secret options
if(config.key) {
aws_config.accessKeyId = config.key;
}
if(config.secret) {
aws_config.secretAccessKey = config.secret;
}
// Intentionally not mandating the accessKeyId and secretAccessKey as they
// will be loaded automatically by the SDK from either environment variables
// or the credentials file.
// http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/node-configuring.html
// Configure the proxy if an environment variable is present.
if(process.env.HTTPS_PROXY) {
gutil.log("setting https proxy to %s", process.env.HTTPS_PROXY);
if(!aws_config.httpOptions) {
aws_config.httpOptions = {};
}
var HttpsProxyAgent = require('https-proxy-agent');
aws_config.httpOptions.agent = new HttpsProxyAgent(process.env.HTTPS_PROXY);
}
// Update the global AWS config if we have any overrides
if(Object.keys(aws_config).length) {
AWS.config.update(aws_config);
}
AWS.config.update(aws_config);
return gulpPrefixer(AWS);
};