Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
alice-was-here committed Jul 8, 2024
1 parent c0493dd commit b71f8b0
Showing 2 changed files with 52 additions and 54 deletions.
104 changes: 51 additions & 53 deletions lib/Open/index.js
Original file line number Diff line number Diff line change
@@ -1,100 +1,98 @@
const fs = require('graceful-fs');
const directory = require('./directory');
const Stream = require('stream');
const { GetObjectCommand, HeadObjectCommand } = require('@aws-sdk/client-s3');
const fs = require("graceful-fs");
const directory = require("./directory");
const Stream = require("stream");

module.exports = {
buffer: function(buffer, options) {
buffer: function (buffer, options) {
const source = {
stream: function(offset, length) {
stream: function (offset, length) {
const stream = Stream.PassThrough();
const end = length ? offset + length : undefined;
stream.end(buffer.slice(offset, end));
return stream;
},
size: function() {
size: function () {
return Promise.resolve(buffer.length);
}
},
};
return directory(source, options);
},
file: function(filename, options) {
file: function (filename, options) {
const source = {
stream: function(start, length) {
stream: function (start, length) {
const end = length ? start + length : undefined;
return fs.createReadStream(filename, {start, end});
return fs.createReadStream(filename, { start, end });
},
size: function() {
return new Promise(function(resolve, reject) {
fs.stat(filename, function(err, d) {
if (err)
reject(err);
else
resolve(d.size);
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';
url: function (request, params, options) {
if (typeof params === "string") params = { url: params };
if (!params.url) throw "URL missing";
params.headers = params.headers || {};

const source = {
stream : function(offset, length) {
stream: function (offset, length) {
const options = Object.create(params);
const end = length ? offset + length : '';
const end = length ? offset + length : "";
options.headers = Object.create(params.headers);
options.headers.range = 'bytes='+offset+'-' + end;
options.headers.range = "bytes=" + offset + "-" + end;
return request(options);
},
size: function() {
return new Promise(function(resolve, reject) {
size: function () {
return new Promise(function (resolve, reject) {
const 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);
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) {
s3: function (client, params, options) {
const source = {
size: function() {
return new Promise(function(resolve, reject) {
client.headObject(params, function(err, d) {
if (err)
reject(err);
else
resolve(d.ContentLength);
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) {
stream: function (offset, length) {
const d = {};
for (const key in params)
d[key] = params[key];
const end = length ? offset + length : '';
d.Range = 'bytes='+offset+'-' + end;
for (const key in params) d[key] = params[key];
const end = length ? offset + length : "";
d.Range = "bytes=" + offset + "-" + end;
return client.getObject(d).createReadStream();
}
},
};

return directory(source, options);
},
s3_v3: function (client, params, options) {
const {
GetObjectCommand,
HeadObjectCommand,
} = require("@aws-sdk/client-s3");

const source = {
size: async () => {
const head = await client.send(
@@ -130,7 +128,7 @@ module.exports = {

return directory(source, options);
},
custom: function(source, options) {
custom: function (source, options) {
return directory(source, options);
}
},
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@
"tap": "^12.7.0",
"temp": ">= 0.4.0 < 1"
},
"peerDependencies": {
"optionalDependencies": {
"@aws-sdk/client-s3": "^3.0.0"
},
"directories": {

0 comments on commit b71f8b0

Please sign in to comment.