-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
171 lines (156 loc) · 4.26 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
var
AWS = require('aws-sdk'),
headersS3Keys = {},
_ = require("lodash"),
debug = require("util").debuglog("s3layer"),
EventEmitter = require('events')
var allowedOutputHeaders = [
"last-modified",
"etag",
"content-type",
"content-length",
"accept-ranges",
"content-range"
];
let lastRequestId = 0
function S3Layer(config) {
AWS.config.update(config.AWS);
var S3 = new AWS.S3();
const events = new EventEmitter()
const middleware = function(req, res, nextMiddleware) {
var
url = req.url,
getReq = {},
headers = req.headers,
processed = false;
const requestId = ++lastRequestId
events.emit('start', {
requestId,
headers,
url
})
var processResponse = function(err, resultInfo) {
if(processed) {
return;
}
processed = true;
if(err) {
return nextMiddleware(err);
}
if(typeof resultInfo == "string") {
resultInfo = {
key: resultInfo
};
}
if(!resultInfo || !resultInfo.key) {
// can't get key, next
return nextMiddleware();
}
if(headers["if-modified-since"]) {
try {
// browser should set valid Date
getReq.IfModifiedSince = new Date(headers["if-modified-since"]);
}
catch(ex) {
}
}
if(headers["if-none-match"]) {
getReq.IfNoneMatch = headers["if-none-match"];
}
if(headers["range"]) {
getReq.Range = headers["range"];
}
getReq.Bucket = resultInfo.bucket || config.bucket;
getReq.Key = resultInfo.key;
debug("S3 object request", JSON.stringify(getReq));
var req = S3.getObject(getReq);
var requestSent = false;
var lastStatusCode;
let headWritten = false
req.on("httpHeaders", function(statusCode, headers) {
debug("Got S3 headers", statusCode, JSON.stringify(headers))
events.emit('headers', {
requestId,
statusCode,
headers
})
if(requestSent || headWritten) {
// prevent Headers Already Sent error
return;
}
lastStatusCode = statusCode;
if(statusCode == 503) {
// ignore 503 errors(Service Unavailable) because aws lib retry request
// if recieved this code, until success code received
return;
}
var outHeaders = {};
allowedOutputHeaders.forEach(function(allowedHeaderName) {
if(headers[allowedHeaderName]) {
outHeaders[allowedHeaderName] = headers[allowedHeaderName];
}
});
if(config.modifyHeaders) {
config.modifyHeaders({
headers: headers,
outHeaders: outHeaders,
key: resultInfo.key
});
}
if(resultInfo.headers) {
outHeaders = _.extend(outHeaders, resultInfo.headers);
}
res.writeHead(statusCode, outHeaders);
headWritten = true
});
const stream = req.createReadStream()
stream.pipe(res)
res.on('finish', () => {
events.emit('done', {
requestId,
statusCode: lastStatusCode
})
})
req.on('httpDone', function(chunk) {
if(requestSent) {
return;
}
debug('request done for', JSON.stringify(resultInfo), lastStatusCode)
if(lastStatusCode >= 500 && lastStatusCode <= 599) {
// ignore 5xx errors sending
return;
}
requestSent = true;
});
req.on('error', err => {
debug('got error from s3', err.message)
if(err.code === 'NoSuchKey') {
res.end()
}
})
stream.on('error', err => {
if(err.code === 'NotModified') {
debug('file not modified, end the response')
res.end()
} else {
debug('got stream error', err.message)
}
})
};
var promise = config.getS3Key({
req: req,
res: res,
url: url
}, processResponse);
if(promise && promise.then) {
promise.then(function(resultInfo) {
processResponse(null, resultInfo);
}).catch(function(err) {
processResponse(err);
});
}
};
middleware.events = events
return middleware
}
module.exports = exports = S3Layer;