mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-07-08 16:34:55 +02:00
Update:Remove read-chunk dependency
This commit is contained in:
parent
78079b2e60
commit
416db7c981
11 changed files with 155 additions and 87 deletions
41
server/libs/readChunk/withOpenFile.js
Normal file
41
server/libs/readChunk/withOpenFile.js
Normal 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)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue