From 416db7c9813d7e20dc9d335834f8d700cf905635 Mon Sep 17 00:00:00 2001 From: advplyr Date: Tue, 7 Jun 2022 19:44:38 -0500 Subject: [PATCH] Update:Remove read-chunk dependency --- package-lock.json | 84 ------------------- package.json | 1 - server/libs/readChunk/index.js | 42 ++++++++++ server/libs/readChunk/pify.js | 70 ++++++++++++++++ server/libs/readChunk/withOpenFile.js | 41 +++++++++ .../libs/{requestIp.js => requestIp/index.js} | 0 server/libs/{ => requestIp}/isJs.js | 0 .../index.js} | 0 .../libs/{uaParserJs.js => uaParser/index.js} | 0 server/managers/CoverManager.js | 2 +- server/managers/PlaybackSessionManager.js | 2 +- 11 files changed, 155 insertions(+), 87 deletions(-) create mode 100644 server/libs/readChunk/index.js create mode 100644 server/libs/readChunk/pify.js create mode 100644 server/libs/readChunk/withOpenFile.js rename server/libs/{requestIp.js => requestIp/index.js} (100%) rename server/libs/{ => requestIp}/isJs.js (100%) rename server/libs/{sanitizeHtml.js => sanitizeHtml/index.js} (100%) rename server/libs/{uaParserJs.js => uaParser/index.js} (100%) diff --git a/package-lock.json b/package-lock.json index d17584c8..3f0e5fc7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,7 +27,6 @@ "node-ffprobe": "^3.0.0", "node-stream-zip": "^1.15.0", "proper-lockfile": "^4.1.2", - "read-chunk": "^3.1.0", "recursive-readdir-async": "^1.1.8", "socket.io": "^4.4.1", "xml2js": "^0.4.23" @@ -1514,22 +1513,6 @@ "node": ">=8" } }, - "node_modules/p-finally": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/p-finally/-/p-finally-1.0.0.tgz", - "integrity": "sha1-P7z7FbiZpEEjs0ttzBi3JDNqLK4=", - "engines": { - "node": ">=4" - } - }, - "node_modules/p-try": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz", - "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==", - "engines": { - "node": ">=6" - } - }, "node_modules/parseurl": { "version": "1.3.3", "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", @@ -1551,14 +1534,6 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" }, - "node_modules/pify": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/pify/-/pify-4.0.1.tgz", - "integrity": "sha512-uB80kBFb/tfd68bVleG9T5GGsGPjJrLAUpR5PZIrhBnIaRTQRjqdJSsIKkOP6OAIFbj7GOrcudc5pNjZ+geV2g==", - "engines": { - "node": ">=6" - } - }, "node_modules/process-nextick-args": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", @@ -1642,18 +1617,6 @@ "node": ">= 0.8" } }, - "node_modules/read-chunk": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/read-chunk/-/read-chunk-3.2.0.tgz", - "integrity": "sha512-CEjy9LCzhmD7nUpJ1oVOE6s/hBkejlcJEgLQHVnQznOSilOPb+kpKktlLfFDK3/WP43+F80xkUTM2VOkYoSYvQ==", - "dependencies": { - "pify": "^4.0.1", - "with-open-file": "^0.1.6" - }, - "engines": { - "node": ">=6" - } - }, "node_modules/readable-stream": { "version": "3.6.0", "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.0.tgz", @@ -1997,19 +1960,6 @@ "which": "bin/which" } }, - "node_modules/with-open-file": { - "version": "0.1.7", - "resolved": "https://registry.npmjs.org/with-open-file/-/with-open-file-0.1.7.tgz", - "integrity": "sha512-ecJS2/oHtESJ1t3ZfMI3B7KIDKyfN0O16miWxdn30zdh66Yd3LsRFebXZXq6GU4xfxLf6nVxp9kIqElb5fqczA==", - "dependencies": { - "p-finally": "^1.0.0", - "p-try": "^2.1.0", - "pify": "^4.0.1" - }, - "engines": { - "node": ">=6" - } - }, "node_modules/wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", @@ -3199,16 +3149,6 @@ "resolved": "https://registry.npmjs.org/p-cancelable/-/p-cancelable-2.1.1.tgz", "integrity": "sha512-BZOr3nRQHOntUjTrH8+Lh54smKHoHyur8We1V8DSMVrl5A2malOOwuJRnKRDjSnkoeBh4at6BwEnb5I7Jl31wg==" }, - "p-finally": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/p-finally/-/p-finally-1.0.0.tgz", - "integrity": "sha1-P7z7FbiZpEEjs0ttzBi3JDNqLK4=" - }, - "p-try": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz", - "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==" - }, "parseurl": { "version": "1.3.3", "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", @@ -3224,11 +3164,6 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" }, - "pify": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/pify/-/pify-4.0.1.tgz", - "integrity": "sha512-uB80kBFb/tfd68bVleG9T5GGsGPjJrLAUpR5PZIrhBnIaRTQRjqdJSsIKkOP6OAIFbj7GOrcudc5pNjZ+geV2g==" - }, "process-nextick-args": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", @@ -3291,15 +3226,6 @@ "unpipe": "1.0.0" } }, - "read-chunk": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/read-chunk/-/read-chunk-3.2.0.tgz", - "integrity": "sha512-CEjy9LCzhmD7nUpJ1oVOE6s/hBkejlcJEgLQHVnQznOSilOPb+kpKktlLfFDK3/WP43+F80xkUTM2VOkYoSYvQ==", - "requires": { - "pify": "^4.0.1", - "with-open-file": "^0.1.6" - } - }, "readable-stream": { "version": "3.6.0", "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.0.tgz", @@ -3559,16 +3485,6 @@ "isexe": "^2.0.0" } }, - "with-open-file": { - "version": "0.1.7", - "resolved": "https://registry.npmjs.org/with-open-file/-/with-open-file-0.1.7.tgz", - "integrity": "sha512-ecJS2/oHtESJ1t3ZfMI3B7KIDKyfN0O16miWxdn30zdh66Yd3LsRFebXZXq6GU4xfxLf6nVxp9kIqElb5fqczA==", - "requires": { - "p-finally": "^1.0.0", - "p-try": "^2.1.0", - "pify": "^4.0.1" - } - }, "wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", diff --git a/package.json b/package.json index c1a7c6c9..abcf238f 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,6 @@ "node-ffprobe": "^3.0.0", "node-stream-zip": "^1.15.0", "proper-lockfile": "^4.1.2", - "read-chunk": "^3.1.0", "recursive-readdir-async": "^1.1.8", "socket.io": "^4.4.1", "xml2js": "^0.4.23" diff --git a/server/libs/readChunk/index.js b/server/libs/readChunk/index.js new file mode 100644 index 00000000..27bf492c --- /dev/null +++ b/server/libs/readChunk/index.js @@ -0,0 +1,42 @@ +'use strict'; + +// https://github.com/sindresorhus/read-chunk + +const fs = require('fs'); +const pify = require('./pify'); +const withOpenFile = require('./withOpenFile'); + +const fsReadP = pify(fs.read, { multiArgs: true }); + +const readChunk = (filePath, startPosition, length) => { + const buffer = Buffer.alloc(length); + + return withOpenFile(filePath, 'r', fileDescriptor => + fsReadP(fileDescriptor, buffer, 0, length, startPosition) + ) + .then(([bytesRead, buffer]) => { + if (bytesRead < length) { + buffer = buffer.slice(0, bytesRead); + } + + return buffer; + }); +}; + +module.exports = readChunk; +// TODO: Remove this for the next major release +module.exports.default = readChunk; + +module.exports.sync = (filePath, startPosition, length) => { + let buffer = Buffer.alloc(length); + + const bytesRead = withOpenFile.sync(filePath, 'r', fileDescriptor => + fs.readSync(fileDescriptor, buffer, 0, length, startPosition) + ); + + if (bytesRead < length) { + buffer = buffer.slice(0, bytesRead); + } + + return buffer; +}; diff --git a/server/libs/readChunk/pify.js b/server/libs/readChunk/pify.js new file mode 100644 index 00000000..0168d75b --- /dev/null +++ b/server/libs/readChunk/pify.js @@ -0,0 +1,70 @@ +'use strict' + +// https://github.com/sindresorhus/pify + +const processFn = (fn, options) => function (...args) { + const P = options.promiseModule; + + return new P((resolve, reject) => { + if (options.multiArgs) { + args.push((...result) => { + if (options.errorFirst) { + if (result[0]) { + reject(result); + } else { + result.shift(); + resolve(result); + } + } else { + resolve(result); + } + }); + } else if (options.errorFirst) { + args.push((error, result) => { + if (error) { + reject(error); + } else { + resolve(result); + } + }); + } else { + args.push(resolve); + } + + fn.apply(this, args); + }); +}; + +module.exports = (input, options) => { + options = Object.assign({ + exclude: [/.+(Sync|Stream)$/], + errorFirst: true, + promiseModule: Promise + }, options); + + const objType = typeof input; + if (!(input !== null && (objType === 'object' || objType === 'function'))) { + throw new TypeError(`Expected \`input\` to be a \`Function\` or \`Object\`, got \`${input === null ? 'null' : objType}\``); + } + + const filter = key => { + const match = pattern => typeof pattern === 'string' ? key === pattern : pattern.test(key); + return options.include ? options.include.some(match) : !options.exclude.some(match); + }; + + let ret; + if (objType === 'function') { + ret = function (...args) { + return options.excludeMain ? input(...args) : processFn(input, options).apply(this, args); + }; + } else { + ret = Object.create(Object.getPrototypeOf(input)); + } + + for (const key in input) { // eslint-disable-line guard-for-in + const property = input[key]; + ret[key] = typeof property === 'function' && filter(key) ? processFn(property, options) : property; + } + + return ret; +}; diff --git a/server/libs/readChunk/withOpenFile.js b/server/libs/readChunk/withOpenFile.js new file mode 100644 index 00000000..76bf626f --- /dev/null +++ b/server/libs/readChunk/withOpenFile.js @@ -0,0 +1,41 @@ +'use strict' + +const fs = require('fs') +const pify = require('./pify') +const pTry = (fn, ...arguments_) => new Promise(resolve => { + resolve(fn(...arguments_)); +}) +const pFinally = (promise, onFinally) => { + onFinally = onFinally || (() => { }); + + return promise.then( + val => new Promise(resolve => { + resolve(onFinally()); + }).then(() => val), + err => new Promise(resolve => { + resolve(onFinally()); + }).then(() => { + throw err; + }) + ); +}; + + +const fsP = pify(fs) + +module.exports = (...args) => { + const callback = args.pop() + return fsP + .open(...args) + .then(fd => pFinally(pTry(callback, fd), _ => fsP.close(fd))) +} + +module.exports.sync = (...args) => { + const callback = args.pop() + const fd = fs.openSync(...args) + try { + return callback(fd) + } finally { + fs.closeSync(fd) + } +} diff --git a/server/libs/requestIp.js b/server/libs/requestIp/index.js similarity index 100% rename from server/libs/requestIp.js rename to server/libs/requestIp/index.js diff --git a/server/libs/isJs.js b/server/libs/requestIp/isJs.js similarity index 100% rename from server/libs/isJs.js rename to server/libs/requestIp/isJs.js diff --git a/server/libs/sanitizeHtml.js b/server/libs/sanitizeHtml/index.js similarity index 100% rename from server/libs/sanitizeHtml.js rename to server/libs/sanitizeHtml/index.js diff --git a/server/libs/uaParserJs.js b/server/libs/uaParser/index.js similarity index 100% rename from server/libs/uaParserJs.js rename to server/libs/uaParser/index.js diff --git a/server/managers/CoverManager.js b/server/managers/CoverManager.js index 20a53d89..a8982ad1 100644 --- a/server/managers/CoverManager.js +++ b/server/managers/CoverManager.js @@ -2,7 +2,7 @@ const fs = require('fs-extra') const Path = require('path') const axios = require('axios') const Logger = require('../Logger') -const readChunk = require('read-chunk') +const readChunk = require('../libs/readChunk') const imageType = require('image-type') const filePerms = require('../utils/filePerms') diff --git a/server/managers/PlaybackSessionManager.js b/server/managers/PlaybackSessionManager.js index b1f04dff..e5a08e20 100644 --- a/server/managers/PlaybackSessionManager.js +++ b/server/managers/PlaybackSessionManager.js @@ -8,7 +8,7 @@ const Stream = require('../objects/Stream') const Logger = require('../Logger') const fs = require('fs-extra') -const uaParserJs = require('../libs/uaParserJs') +const uaParserJs = require('../libs/uaParser') const requestIp = require('../libs/requestIp') class PlaybackSessionManager {