Skip to content

Commit

Permalink
fix: Remove dependency on obsolete archive-type
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrzesik committed Jul 26, 2022
1 parent 6b871e5 commit 9ca5bcd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
13 changes: 10 additions & 3 deletions download.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const fsp = require('fs').promises;
const path = require('path');
const { URL } = require('url');
const contentDisposition = require('content-disposition');
const archiveType = require('archive-type');
const decompress = require('decompress');
const filenamify = require('filenamify');
const getStream = require('get-stream');
Expand All @@ -28,6 +27,14 @@ const pEvent = require('p-event');
const FileType = require('file-type');
const extName = require('ext-name');

// This function is inspired by now abandoned module from https://github.com/kevva/archive-type repository
const archiveTypeFromBuffer = async (input) => {
const archiveExtensions = new Set(['7z', 'bz2', 'gz', 'rar', 'tar', 'zip', 'xz', 'gz']);

const ret = await FileType.fromBuffer(input);
return archiveExtensions.has(ret && ret.ext) ? ret : null;
};

const filenameFromPath = (res) => path.basename(new URL(res.requestUrl).pathname);

const getExtFromMime = (res) => {
Expand Down Expand Up @@ -97,13 +104,13 @@ module.exports = (uri, output, opts) => {
const [data, res] = result;

if (!output) {
return opts.extract && archiveType(data) ? decompress(data, opts) : data;
return opts.extract && (await archiveTypeFromBuffer(data)) ? decompress(data, opts) : data;
}

const filename = opts.filename || filenamify(await getFilename(res, data));
const outputFilepath = path.join(output, filename);

if (opts.extract && archiveType(data)) {
if (opts.extract && (await archiveTypeFromBuffer(data))) {
return decompress(data, path.dirname(outputFilepath), opts);
}

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"homepage": "https://github.com/serverless/utils#readme",
"author": "Serverless, Inc.",
"dependencies": {
"archive-type": "^4.0.0",
"chalk": "^4.1.2",
"ci-info": "^3.3.2",
"cli-progress-footer": "^2.3.2",
Expand All @@ -16,7 +15,7 @@
"event-emitter": "^0.3.5",
"ext": "^1.6.0",
"ext-name": "^5.0.0",
"file-type": "^16.5.3",
"file-type": "^16.5.4",
"filenamify": "^4.3.0",
"get-stream": "^6.0.1",
"got": "^11.8.5",
Expand Down

0 comments on commit 9ca5bcd

Please sign in to comment.