Update:Remove read-chunk dependency

This commit is contained in:
advplyr 2022-06-07 19:44:38 -05:00
parent 78079b2e60
commit 416db7c981
11 changed files with 155 additions and 87 deletions

View file

@ -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;
};

View file

@ -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;
};

View file

@ -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)
}
}

View file

@ -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')

View file

@ -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 {