forked from EvanOxfeld/node-unzip
-
Notifications
You must be signed in to change notification settings - Fork 116
/
index.js
101 lines (93 loc) · 2.78 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
var fs = require('graceful-fs');
var Promise = require('bluebird');
var directory = require('./directory');
var Stream = require('stream');
// Backwards compatibility for node versions < 8
if (!Stream.Writable || !Stream.Writable.prototype.destroy)
Stream = require('readable-stream');
module.exports = {
buffer: function(buffer, options) {
var source = {
stream: function(offset, length) {
var stream = Stream.PassThrough();
stream.end(buffer.slice(offset, length));
return stream;
},
size: function() {
return Promise.resolve(buffer.length);
}
};
return directory(source, options);
},
file: function(filename, options) {
var source = {
stream: function(offset,length) {
return fs.createReadStream(filename,{start: offset, end: length && offset+length});
},
size: function() {
return new Promise(function(resolve,reject) {
fs.stat(filename,function(err,d) {
if (err)
reject(err);
else
resolve(d.size);
});
});
}
};
return directory(source, options);
},
url: function(request, params, options) {
if (typeof params === 'string')
params = {url: params};
if (!params.url)
throw 'URL missing';
params.headers = params.headers || {};
var source = {
stream : function(offset,length) {
var options = Object.create(params);
options.headers = Object.create(params.headers);
options.headers.range = 'bytes='+offset+'-' + (length ? length : '');
return request(options);
},
size: function() {
return new Promise(function(resolve,reject) {
var req = request(params);
req.on('response',function(d) {
req.abort();
if (!d.headers['content-length'])
reject(new Error('Missing content length header'));
else
resolve(d.headers['content-length']);
}).on('error',reject);
});
}
};
return directory(source, options);
},
s3 : function(client,params, options) {
var source = {
size: function() {
return new Promise(function(resolve,reject) {
client.headObject(params, function(err,d) {
if (err)
reject(err);
else
resolve(d.ContentLength);
});
});
},
stream: function(offset,length) {
var d = {};
for (var key in params)
d[key] = params[key];
d.Range = 'bytes='+offset+'-' + (length ? length : '');
return client.getObject(d).createReadStream();
}
};
return directory(source, options);
},
custom: function(source, options) {
return directory(source, options);
}
};