-
Notifications
You must be signed in to change notification settings - Fork 234
Expand file tree
/
Copy pathreader.js
More file actions
570 lines (478 loc) · 13.8 KB
/
Copy pathreader.js
File metadata and controls
570 lines (478 loc) · 13.8 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
var fs = require('fs'),
meta = require('shell/meta'),
view = require('view/view'),
asyncCallback = require('misc').asyncCallback;
async = require('misc').async,
extend = require('misc').extend,
JSONPretty = require('misc').JSONPretty,
composePath = require('misc').composePath,
expandPath = require('misc').expandPath;
// Is x an object?
function isObject(x) {
return typeof x == 'object';
}
// Is x an array?
function isArray(x) {
return isObject(x) && x.constructor == [].constructor;
}
/**
* Data reader for termkit-style dataIn.
*
* Waits for mime headers, parses them, then calls begin() to construct
* an appropriate stream/data handler.
*
* Supports buffered and unbuffered input.
*
* Calls exit when the pipe closes.
*/
exports.dataReader = function (dataIn, begin, exit, error) {
var that = this;
this.dataIn = dataIn;
this.begin = begin || (function () {});
this.exit = exit || (function () {});
this.error = error || (function () {});
// Header sniffing.
this.identified = false;
this.lookahead = '';
this.headers = null;
// Output buffering.
this.buffered = false;
this.buffer = null;
this.chunks = [];
this.length = 0;
this.offset = 0;
// Link up to dataOut of last process.
dataIn.on('data', function (data) {
that.data(data);
});
// Finish processing when pipe closes.
dataIn.on('end', function () {
that.done();
});
};
exports.dataReader.prototype = {
/**
* Parse headers string into headers object.
*/
parse: function (headers) {
this.headers = new meta.headers();
this.headers.parse(headers);
},
/**
* Read stream, find MIME headers.
*/
data: function (data) {
if (!this.identified) {
// Swallow data until we encounter the MIME header delimiter.
this.lookahead += data.toString('ascii');
if (this.lookahead.indexOf("\r\n\r\n") != -1) {
// Parse headers.
var chunk = this.lookahead.split("\r\n\r\n").shift();
this.parse(chunk);
// Notify context of headers, receive handler.
this.identified = true;
this.handler = this.begin(this.headers);
this.buffered = this.handler.begin && this.handler.begin(this.headers);
// See if size is known ahead of time.
var length = this.length = parseInt(this.headers.get('Content-Length'));
if (this.buffered && !isNaN(length)) {
// Allocate large buffer.
this.buffer = new Buffer(length);
}
else {
this.length = 0;
}
// Emit left-over data.
var end = chunk.length + 4;
if (end < data.length) {
this.data(data.slice(end));
}
}
}
else {
// Send output to handler.
if (this.buffered) {
// Collect output.
if (this.buffer) {
// Append chunk to buffer.
data.copy(this.buffer, this.offset, 0, data.length);
this.offset += data.length;
}
else {
// Size not known. Push chunk onto array to grow indefinitely.
this.chunks.push(data);
// Count size for final buffer.
this.length += data.length;
}
}
else {
// Stream out data.
this.handler.data && this.handler.data(data);
}
}
},
/**
* Finish reading.
*/
done: function () {
// No data.
if (!this.handler) {
return this.exit();
}
// Send all buffered output to handler in one chunk.
if (this.buffered) {
if (!this.buffer) {
this.buffer = new Buffer(this.length);
// Join chunks.
for (i in this.chunks) {
var data = this.chunks[i];
data.copy(this.buffer, this.offset, 0, data.length)
this.offset += data.length;
}
}
this.handler.data && this.handler.data(this.buffer);
}
this.handler.end && this.handler.end(this.exit);
},
};
/**
* File reader for termkit-style dataIn.
*
* Generates mime headers, then calls begin() to construct
* an appropriate stream/data handler.
*
* Supports buffered and unbuffered input.
*
* Calls exit when the file has been read.
*/
exports.fileReader = function (file, begin, exit, error) {
var that = this;
this.file = file;
this.begin = begin || (function () {});
this.exit = exit || (function () {});
this.error = error || (function () {});
// Begin reading.
this.open();
};
exports.fileReader.prototype = {
/**
* Open file, determine type, begin reading.
*/
open: function () {
var that = this,
chunkSize = 16384,
errors = 0,
track = whenDone(function () {
that.done();
});
expandPath(this.file, track(function (file) {
fs.stat(file, track(function (err, stats) {
if (err) {
errors++;
that.error("No such file (" + that.file + ")");
return;
}
fs.open(file, 'r', track(function (err, fd) {
if (err) {
errors++;
that.error("Unable to open file (" + that.file + ")");
return;
}
var position = 0;
(function read() {
var buffer = new Buffer(chunkSize);
fs.read(fd, buffer, 0, chunkSize, position, track(function (err, bytesRead) {
if (err) {
errors++;
that.error("Error reading file (" + that.file + ")");
return;
}
var slice = buffer.slice(0, bytesRead);
// Determine headers from first slice.
if (position == 0) {
var headers = new meta.headers(),
keys = file.split('/');
headers.set({
'Content-Type': meta.sniff(file, slice),
'Content-Length': stats.size,
'Content-Disposition': [ 'attachment', { 'filename': keys.pop() } ],
});
// Get handler and begin processing.
that.handler = that.begin(headers);
that.buffered = that.handler.begin && that.handler.begin(headers);
}
// If buffered, read file and return.
if (that.buffered) {
that.buffer = new Buffer(stats.size);
fs.read(fd, that.buffer, 0, stats.size, 0, track(function (err, bytesRead) {
var slice = buffer.slice(0, bytesRead);
that.data(slice);
}));
return;
}
// Process slice.
that.data(slice);
position += bytesRead;
if (position < stats.size) {
read();
}
})); // fs.read
})(); // read
})); // fs.open
})); // fs.stat
})); // expandPath
},
/**
* Send data to handler.
*/
data: function (data) {
this.handler.data && this.handler.data(data);
},
/**
* Finish reading.
*/
done: function () {
// No data.
if (!this.handler) {
return this.exit();
}
this.handler.end && this.handler.end(this.exit);
},
};
/**
* Multiple files reader for termkit-style dataIn.
*
* Generates mime headers, then calls begin() to construct
* an appropriate stream/data handler.
*
* Supports buffered and unbuffered input.
*
* Calls exit when the file has been read.
*/
exports.filesReader = function (files, begin, exit, error) {
var that = this;
this.files = files;
this.begin = begin || (function () {});
this.exit = exit || (function () {});
this.error = error || (function () {});
this.offset = 0;
this.buffer = null;
this.open();
};
exports.filesReader.prototype = {
/**
* Open files, determine output type, begin reading.
*/
open: function () {
var that = this,
chunkSize = 16384,
errors = 0,
size = 0,
types = [],
track = whenDone(function () {
// Done inspecting files.
if (errors > 0) {
that.done();
}
else {
that.process(that.files, types, size);
}
});
// Identify the content-type of all the files
for (var i in this.files) (function (file) {
fs.stat(file, track(function (err, stats) {
if (err) {
errors++;
that.error("No such file (" + file + ")");
return;
}
size += stats.size;
// Read beginning of file for sniffing.
fs.open(file, 'r', track(function (err, fd) {
if (err) {
errors++;
that.error("Unable to open file (" + file + ")");
return;
}
var buffer = new Buffer(chunkSize);
fs.read(fd, buffer, 0, chunkSize, 0, track(function (err, bytesRead) {
if (err) {
errors++;
that.error("Error reading file (" + file + ")");
return;
}
var slice = buffer.slice(0, bytesRead);
// Determine content-type from slice.
var type = meta.sniff(file, slice);
types.push(type);
})); // fs.read
})); // fs.open
})); // fs.stat
})(this.files[i]);
// Ensure tracker completes without files.
if (this.files.length == 0) {
that.done();
}
},
/**
* Determine common mime type for files.
*/
type: function (types) {
// Single type.
if (types.length == 1) {
return types[0];
}
// Merge multiple types.
var merged = {}, hasParams = false;
for (var i in types) {
// Process params.
if (isArray(types[i])) {
var params = types[i][1];
// Only merge identical params.
for (var j in params) {
if (merged[j] === null) continue;
if (merged[j] != params[j]) {
merged[j] = null;
}
merged[j] = params[j];
}
// Strip params for merge.
types[i] = types[i][0];
}
}
// Check if there are params.
for (var j in merged) {
hasParams = true;
break;
}
function commonPrefix(types) {
// Sort types, inspect first/last strings.
types.sort();
var first = types[0],
last = types[types.length - 1],
n = Math.min(first.length, last.length),
match = '';
// Find common prefix.
while (n > 0) {
last = last.substring(0, n--);
if (first.indexOf(last) == 0) {
match = last;
break;
}
}
return match;
}
var type = 'application/octed-stream';
prefix = commonPrefix(types);
if (!(/^[^\/]+\/[^\/]+$/.test(prefix))) {
// If we only matched a type category (e.g. text/),
// coerce types to their base.
types = types.map(function (type) {
return meta.base(type);
});
prefix = commonPrefix(types);
if (!(/^[^\/]+\/[^\/]+$/.test(prefix))) {
// Replace with generic type.
type = meta.default(prefix) || 'application/octet-stream';
}
else {
type = prefix;
}
}
else {
// Only return a unified type for content types which can be composed safely.
var composable = meta.composable();
if (composable[prefix]) {
if (typeof composable[prefix] == 'string') {
type = composable[prefix];
}
else {
type = prefix;
}
}
}
// Return with or without merged params.
return hasParams ? [ type, merged ] : type;
},
/**
* Begin reading files.
*/
process: function (files, types, size) {
var that = this;
// Find common mime-type prefix.
var type = this.type(types);
// Build headers
var headers = new meta.headers();
headers.set('Content-Type', type);
headers.set('Content-Length', size);
if (files.length == 1) {
var keys = files[0].split('/');
headers.set('Content-Disposition', [ 'attachment', { 'filename': keys.pop() } ]);
}
// Get handler and begin processing.
this.handler = this.begin(headers);
this.buffered = this.handler.begin && this.handler.begin(headers);
// If buffered, create buffer of appropriate size.
if (this.buffered) {
this.buffer = new Buffer(size);
}
// File reader pass-through handler.
var handler = {
// Already determined mime type.
// Always use unbuffed filereader, so we only buffer once for all files.
begin: function () { return false; },
data: function (data) { that.data(data) },
end: function (exit) { exit(); },
};
// Helper: Process a single file.
function read(file, complete) {
var reader = new exports.fileReader(file, function (headers) {
return handler;
}, function () {
complete();
}, function (error) {
that.error(error);
});
}
// Process all files in sequence, asynchronously.
var files = files.slice();
(function nextFile() {
if (files.length) {
var file = files.shift();
read(file, nextFile);
}
else {
that.done();
}
})();
},
/**
* Send data to handler.
*/
data: function (data) {
// Send output to plugin.
if (this.buffered) {
// Append chunk to buffer.
data.copy(this.buffer, this.offset, 0, data.length);
this.offset += data.length;
}
else {
// Stream out data.
this.handler.data && this.handler.data(data);
}
},
/**
* Finish reading.
*/
done: function () {
// No data.
if (!this.handler) {
return this.exit();
}
// Send all buffered output to handler in one chunk.
if (this.buffered) {
this.handler.data && this.handler.data(this.buffer);
}
this.handler.end && this.handler.end(this.exit);
},
};