This repository has been archived by the owner on Oct 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thumbp.js
234 lines (208 loc) · 6.9 KB
/
thumbp.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
'use strict';
const http = require('http');
const libRequest = require('request');
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
const path_pat = /^\/thumb\/([a-f0-9]{32})$/;
const es_base = process.argv[2] || 'http://localhost:9200/dpla_alias/item';
const port = process.argv[3] || '8080';
/***
* thumbp: DPLA Thumbnail Image Proxy
* -----------------------------------
*
* Example invocation:
* $ node thumbp.js http://my-es-server:9200/dpla_alias/item 8080
*
*/
/*
* Connection
* ---
* The connection between `thumbp' server and its client, with a request and
* a response.
*/
function Connection(request, response) {
this.request = request;
this.response = response;
this.response.setHeader('Server', 'DPLA thumbp');
this.request.on('data', () => {
return; // noop
}).on('error', (e) => {
console.error(e);
this.returnError(500);
}).on('end', () => {
this.handleReqEnd();
});
this.imageURL = null;
this.itemID = null;
}
exports.Connection = Connection; // Exported for the sake of our tests
/*
* Handle the /thumb request 'end' event, when the request is complete.
*/
Connection.prototype.handleReqEnd = function() {
if (this.request.method === 'GET') {
var match_result = path_pat.exec(this.request.url);
if (match_result !== null) {
this.itemID = match_result[1];
this.lookUpImage();
} else {
this.returnError(400);
}
} else {
this.returnError(405);
}
};
/*
* Given an item ID, look up the image URL and proxy it.
*/
Connection.prototype.lookUpImage = function() {
var q_url = es_base + `/_search?q=id:${this.itemID}&fields=id,object`;
libRequest(q_url, (error, response, body) => {
this.checkSearchResponse(error, response, body);
});
};
/*
* Check the response from the Elasticsearch query request, and proceed
* to proxying the image if it's OK.
*/
Connection.prototype.checkSearchResponse = function(error, response, body) {
var item_data, // Parsed Elasticsearch response
obj, // The 'object' property in the ES response
url; // Thumbnail image URL
if (!error && response.statusCode == 200) {
try {
item_data = JSON.parse(body);
if (item_data['hits']['total'] == 0) {
this.returnError(404);
return;
}
obj = item_data['hits']['hits'][0]['fields']['object'];
if (Array.isArray(obj)) {
url = obj[0]; // [0] could be undefined (== false)
} else if (typeof obj == 'string') {
url = obj;
} else {
url = false;
}
if (url) {
this.imageURL = url;
this.proxyImage();
} else {
// Empty string or empty array, or incorrect 'object' type
console.error(
`empty / invalid object property for item ${this.itemID}`
);
this.returnError(404);
}
} catch (e) {
console.error(e.stack);
this.returnError(500);
}
} else {
console.error(error);
this.returnError(500);
}
};
/*
* Given an image URL, pipe the image to the client response.
*
* The `request' response (i.e. the response of what we're namespacing as
* `libRequest', which is the response from the provider's image resource)
* is piped into the response we're sending to our client.
*/
Connection.prototype.proxyImage = function() {
try {
libRequest({
method: 'GET',
uri: this.imageURL,
headers: {
'User-Agent': 'DPLA thumbp (https://github.com/dpla/thumbp)'
},
timeout: 10000 // ms
}).on('response', (response) => {
this.handleImageResponse(response);
}).on('error', (error) => {
this.handleImageConnectionError(error);
}).pipe(this.response);
} catch (e) {
console.error(e.stack);
this.returnError(500);
}
};
/*
* Handle the image request's 'response' event, which allows us to manipulate
* the response object before it gets piped to the response we send to our
* client in proxyImage().
*
* We customize the return code and response headers.
*/
Connection.prototype.handleImageResponse = function(imgResponse) {
// Reduce headers to just those that we want to pass through
var cl = imgResponse.headers['content-length'] || false;
var ct = imgResponse.headers['content-type'] || false;
var lm = imgResponse.headers['last-modified'] || false;
imgResponse.headers = {
'Date': imgResponse.headers['date'],
};
cl && (imgResponse.headers['Content-Length'] = cl);
ct && (imgResponse.headers['Content-Type'] = ct);
lm && (imgResponse.headers['Last-Modified'] = lm);
// We have our own ideas of which response codes are appropriate for our
// client.
switch(imgResponse.statusCode) {
case 200:
break;
case 404:
case 410:
// We treat a 410 as a 404, because our provider could correct
// the `object' property in the item's metadata, meaning the
// resource doesn't have to be "410 Gone".
console.error(`${this.imageURL} not found`);
imgResponse.statusCode = 404;
break;
default:
// Other kinds of errors are just considered "bad gateway" errors
// because we don't want to own them.
console.error(
`${this.imageURL} status ${imgResponse.statusCode}`
);
imgResponse.statusCode = 502;
break;
}
};
/*
* Handle errors with the HTTP connection to the image webserver.
*/
Connection.prototype.handleImageConnectionError = function(error) {
console.error(`Error (${error.code}) for ${this.imageURL}`);
if (error.code === 'ETIMEDOUT') {
this.returnError(504);
} else {
this.returnError(502);
}
};
Connection.prototype.returnError = function(code) {
this.response.statusCode = code;
this.response.end();
};
if (require.main === module) {
process.on('uncaughtException', (err) => {
console.error('Uncaught exception: ', err.stack);
});
if (cluster.isMaster) {
// Fork workers, 1 for each CPU.
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
}).on('online', (worker) => {
console.log(`worker ${worker.process.pid} online`);
});
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
} else {
// Worker: create a server and handle each request with a Connection.
http.createServer(function(request, response) {
new Connection(request, response);
}).listen(port);
}
}