|
1 | | -const fs = require('graceful-fs'); |
2 | | -const directory = require('./directory'); |
3 | | -const Stream = require('stream'); |
4 | | -const { GetObjectCommand, HeadObjectCommand } = require('@aws-sdk/client-s3'); |
| 1 | +const fs = require("graceful-fs"); |
| 2 | +const directory = require("./directory"); |
| 3 | +const Stream = require("stream"); |
5 | 4 |
|
6 | 5 | module.exports = { |
7 | | - buffer: function(buffer, options) { |
| 6 | + buffer: function (buffer, options) { |
8 | 7 | const source = { |
9 | | - stream: function(offset, length) { |
| 8 | + stream: function (offset, length) { |
10 | 9 | const stream = Stream.PassThrough(); |
11 | 10 | const end = length ? offset + length : undefined; |
12 | 11 | stream.end(buffer.slice(offset, end)); |
13 | 12 | return stream; |
14 | 13 | }, |
15 | | - size: function() { |
| 14 | + size: function () { |
16 | 15 | return Promise.resolve(buffer.length); |
17 | | - } |
| 16 | + }, |
18 | 17 | }; |
19 | 18 | return directory(source, options); |
20 | 19 | }, |
21 | | - file: function(filename, options) { |
| 20 | + file: function (filename, options) { |
22 | 21 | const source = { |
23 | | - stream: function(start, length) { |
| 22 | + stream: function (start, length) { |
24 | 23 | const end = length ? start + length : undefined; |
25 | | - return fs.createReadStream(filename, {start, end}); |
| 24 | + return fs.createReadStream(filename, { start, end }); |
26 | 25 | }, |
27 | | - size: function() { |
28 | | - return new Promise(function(resolve, reject) { |
29 | | - fs.stat(filename, function(err, d) { |
30 | | - if (err) |
31 | | - reject(err); |
32 | | - else |
33 | | - resolve(d.size); |
| 26 | + size: function () { |
| 27 | + return new Promise(function (resolve, reject) { |
| 28 | + fs.stat(filename, function (err, d) { |
| 29 | + if (err) reject(err); |
| 30 | + else resolve(d.size); |
34 | 31 | }); |
35 | 32 | }); |
36 | | - } |
| 33 | + }, |
37 | 34 | }; |
38 | 35 | return directory(source, options); |
39 | 36 | }, |
40 | 37 |
|
41 | | - url: function(request, params, options) { |
42 | | - if (typeof params === 'string') |
43 | | - params = {url: params}; |
44 | | - if (!params.url) |
45 | | - throw 'URL missing'; |
| 38 | + url: function (request, params, options) { |
| 39 | + if (typeof params === "string") params = { url: params }; |
| 40 | + if (!params.url) throw "URL missing"; |
46 | 41 | params.headers = params.headers || {}; |
47 | 42 |
|
48 | 43 | const source = { |
49 | | - stream : function(offset, length) { |
| 44 | + stream: function (offset, length) { |
50 | 45 | const options = Object.create(params); |
51 | | - const end = length ? offset + length : ''; |
| 46 | + const end = length ? offset + length : ""; |
52 | 47 | options.headers = Object.create(params.headers); |
53 | | - options.headers.range = 'bytes='+offset+'-' + end; |
| 48 | + options.headers.range = "bytes=" + offset + "-" + end; |
54 | 49 | return request(options); |
55 | 50 | }, |
56 | | - size: function() { |
57 | | - return new Promise(function(resolve, reject) { |
| 51 | + size: function () { |
| 52 | + return new Promise(function (resolve, reject) { |
58 | 53 | const req = request(params); |
59 | | - req.on('response', function(d) { |
60 | | - req.abort(); |
61 | | - if (!d.headers['content-length']) |
62 | | - reject(new Error('Missing content length header')); |
63 | | - else |
64 | | - resolve(d.headers['content-length']); |
65 | | - }).on('error', reject); |
| 54 | + req |
| 55 | + .on("response", function (d) { |
| 56 | + req.abort(); |
| 57 | + if (!d.headers["content-length"]) |
| 58 | + reject(new Error("Missing content length header")); |
| 59 | + else resolve(d.headers["content-length"]); |
| 60 | + }) |
| 61 | + .on("error", reject); |
66 | 62 | }); |
67 | | - } |
| 63 | + }, |
68 | 64 | }; |
69 | 65 |
|
70 | 66 | return directory(source, options); |
71 | 67 | }, |
72 | 68 |
|
73 | | - s3 : function(client, params, options) { |
| 69 | + s3: function (client, params, options) { |
74 | 70 | const source = { |
75 | | - size: function() { |
76 | | - return new Promise(function(resolve, reject) { |
77 | | - client.headObject(params, function(err, d) { |
78 | | - if (err) |
79 | | - reject(err); |
80 | | - else |
81 | | - resolve(d.ContentLength); |
| 71 | + size: function () { |
| 72 | + return new Promise(function (resolve, reject) { |
| 73 | + client.headObject(params, function (err, d) { |
| 74 | + if (err) reject(err); |
| 75 | + else resolve(d.ContentLength); |
82 | 76 | }); |
83 | 77 | }); |
84 | 78 | }, |
85 | | - stream: function(offset, length) { |
| 79 | + stream: function (offset, length) { |
86 | 80 | const d = {}; |
87 | | - for (const key in params) |
88 | | - d[key] = params[key]; |
89 | | - const end = length ? offset + length : ''; |
90 | | - d.Range = 'bytes='+offset+'-' + end; |
| 81 | + for (const key in params) d[key] = params[key]; |
| 82 | + const end = length ? offset + length : ""; |
| 83 | + d.Range = "bytes=" + offset + "-" + end; |
91 | 84 | return client.getObject(d).createReadStream(); |
92 | | - } |
| 85 | + }, |
93 | 86 | }; |
94 | 87 |
|
95 | 88 | return directory(source, options); |
96 | 89 | }, |
97 | 90 | s3_v3: function (client, params, options) { |
| 91 | + const { |
| 92 | + GetObjectCommand, |
| 93 | + HeadObjectCommand, |
| 94 | + } = require("@aws-sdk/client-s3"); |
| 95 | + |
98 | 96 | const source = { |
99 | 97 | size: async () => { |
100 | 98 | const head = await client.send( |
@@ -130,7 +128,7 @@ module.exports = { |
130 | 128 |
|
131 | 129 | return directory(source, options); |
132 | 130 | }, |
133 | | - custom: function(source, options) { |
| 131 | + custom: function (source, options) { |
134 | 132 | return directory(source, options); |
135 | | - } |
| 133 | + }, |
136 | 134 | }; |
0 commit comments