Skip to content

Commit fa5108e

Browse files
committed
using node:timers setImmediate instead of relying on the global provided one as is deprecated in some systems
1 parent 277b294 commit fa5108e

File tree

3 files changed

+160
-156
lines changed

3 files changed

+160
-156
lines changed

fd-slicer.js

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var Writable = stream.Writable;
77
var PassThrough = stream.PassThrough;
88
var Pend = require('pend');
99
var EventEmitter = require('events').EventEmitter;
10+
var setImmediate = require("timers").setImmediate;
1011

1112
exports.createFromBuffer = createFromBuffer;
1213
exports.createFromFd = createFromFd;
@@ -25,39 +26,39 @@ function FdSlicer(fd, options) {
2526
this.autoClose = !!options.autoClose;
2627
}
2728

28-
FdSlicer.prototype.read = function(buffer, offset, length, position, callback) {
29+
FdSlicer.prototype.read = function (buffer, offset, length, position, callback) {
2930
var self = this;
30-
self.pend.go(function(cb) {
31-
fs.read(self.fd, buffer, offset, length, position, function(err, bytesRead, buffer) {
31+
self.pend.go(function (cb) {
32+
fs.read(self.fd, buffer, offset, length, position, function (err, bytesRead, buffer) {
3233
cb();
3334
callback(err, bytesRead, buffer);
3435
});
3536
});
3637
};
3738

38-
FdSlicer.prototype.write = function(buffer, offset, length, position, callback) {
39+
FdSlicer.prototype.write = function (buffer, offset, length, position, callback) {
3940
var self = this;
40-
self.pend.go(function(cb) {
41-
fs.write(self.fd, buffer, offset, length, position, function(err, written, buffer) {
41+
self.pend.go(function (cb) {
42+
fs.write(self.fd, buffer, offset, length, position, function (err, written, buffer) {
4243
cb();
4344
callback(err, written, buffer);
4445
});
4546
});
4647
};
4748

48-
FdSlicer.prototype.createReadStream = function(options) {
49+
FdSlicer.prototype.createReadStream = function (options) {
4950
return new ReadStream(this, options);
5051
};
5152

52-
FdSlicer.prototype.createWriteStream = function(options) {
53+
FdSlicer.prototype.createWriteStream = function (options) {
5354
return new WriteStream(this, options);
5455
};
5556

56-
FdSlicer.prototype.ref = function() {
57+
FdSlicer.prototype.ref = function () {
5758
this.refCount += 1;
5859
};
5960

60-
FdSlicer.prototype.unref = function() {
61+
FdSlicer.prototype.unref = function () {
6162
var self = this;
6263
self.refCount -= 1;
6364

@@ -91,7 +92,7 @@ function ReadStream(context, options) {
9192
this.destroyed = false;
9293
}
9394

94-
ReadStream.prototype._read = function(n) {
95+
ReadStream.prototype._read = function (n) {
9596
var self = this;
9697
if (self.destroyed) return;
9798

@@ -105,10 +106,10 @@ ReadStream.prototype._read = function(n) {
105106
self.context.unref();
106107
return;
107108
}
108-
self.context.pend.go(function(cb) {
109+
self.context.pend.go(function (cb) {
109110
if (self.destroyed) return cb();
110111
var buffer = Buffer.allocUnsafe(toRead);
111-
fs.read(self.context.fd, buffer, 0, toRead, self.pos, function(err, bytesRead) {
112+
fs.read(self.context.fd, buffer, 0, toRead, self.pos, function (err, bytesRead) {
112113
if (err) {
113114
self.destroy(err);
114115
} else if (bytesRead === 0) {
@@ -124,7 +125,7 @@ ReadStream.prototype._read = function(n) {
124125
});
125126
};
126127

127-
ReadStream.prototype.destroy = function(err) {
128+
ReadStream.prototype.destroy = function (err) {
128129
if (this.destroyed) return;
129130
err = err || new Error("stream destroyed");
130131
this.destroyed = true;
@@ -149,7 +150,7 @@ function WriteStream(context, options) {
149150
this.on('finish', this.destroy.bind(this));
150151
}
151152

152-
WriteStream.prototype._write = function(buffer, encoding, callback) {
153+
WriteStream.prototype._write = function (buffer, encoding, callback) {
153154
var self = this;
154155
if (self.destroyed) return;
155156

@@ -160,9 +161,9 @@ WriteStream.prototype._write = function(buffer, encoding, callback) {
160161
callback(err);
161162
return;
162163
}
163-
self.context.pend.go(function(cb) {
164+
self.context.pend.go(function (cb) {
164165
if (self.destroyed) return cb();
165-
fs.write(self.context.fd, buffer, 0, buffer.length, self.pos, function(err, bytes) {
166+
fs.write(self.context.fd, buffer, 0, buffer.length, self.pos, function (err, bytes) {
166167
if (err) {
167168
self.destroy();
168169
cb();
@@ -178,7 +179,7 @@ WriteStream.prototype._write = function(buffer, encoding, callback) {
178179
});
179180
};
180181

181-
WriteStream.prototype.destroy = function() {
182+
WriteStream.prototype.destroy = function () {
182183
if (this.destroyed) return;
183184
this.destroyed = true;
184185
this.context.unref();
@@ -194,7 +195,7 @@ function BufferSlicer(buffer, options) {
194195
this.maxChunkSize = options.maxChunkSize || Number.MAX_SAFE_INTEGER;
195196
}
196197

197-
BufferSlicer.prototype.read = function(buffer, offset, length, position, callback) {
198+
BufferSlicer.prototype.read = function (buffer, offset, length, position, callback) {
198199
if (!(0 <= offset && offset <= buffer.length)) throw new RangeError("offset outside buffer: 0 <= " + offset + " <= " + buffer.length);
199200
if (position < 0) throw new RangeError("position is negative: " + position);
200201
if (offset + length > buffer.length) {
@@ -210,25 +211,25 @@ BufferSlicer.prototype.read = function(buffer, offset, length, position, callbac
210211
if (length <= 0) {
211212
// After any clamping, we're fully out of bounds or otherwise have nothing to do.
212213
// This isn't an error; it's just zero bytes written.
213-
setImmediate(function() {
214+
setImmediate(function () {
214215
callback(null, 0);
215216
});
216217
return;
217218
}
218219
this.buffer.copy(buffer, offset, position, position + length);
219-
setImmediate(function() {
220+
setImmediate(function () {
220221
callback(null, length);
221222
});
222223
};
223224

224-
BufferSlicer.prototype.write = function(buffer, offset, length, position, callback) {
225+
BufferSlicer.prototype.write = function (buffer, offset, length, position, callback) {
225226
buffer.copy(this.buffer, position, offset, offset + length);
226-
setImmediate(function() {
227+
setImmediate(function () {
227228
callback(null, length, buffer);
228229
});
229230
};
230231

231-
BufferSlicer.prototype.createReadStream = function(options) {
232+
BufferSlicer.prototype.createReadStream = function (options) {
232233
options = options || {};
233234
var readStream = new PassThrough(options);
234235
readStream.destroyed = false;
@@ -254,13 +255,13 @@ BufferSlicer.prototype.createReadStream = function(options) {
254255
}
255256

256257
readStream.end();
257-
readStream.destroy = function() {
258+
readStream.destroy = function () {
258259
readStream.destroyed = true;
259260
};
260261
return readStream;
261262
};
262263

263-
BufferSlicer.prototype.createWriteStream = function(options) {
264+
BufferSlicer.prototype.createWriteStream = function (options) {
264265
var bufferSlicer = this;
265266
options = options || {};
266267
var writeStream = new Writable(options);
@@ -269,7 +270,7 @@ BufferSlicer.prototype.createWriteStream = function(options) {
269270
writeStream.bytesWritten = 0;
270271
writeStream.pos = writeStream.start;
271272
writeStream.destroyed = false;
272-
writeStream._write = function(buffer, encoding, callback) {
273+
writeStream._write = function (buffer, encoding, callback) {
273274
if (writeStream.destroyed) return;
274275

275276
var end = writeStream.pos + buffer.length;
@@ -287,17 +288,17 @@ BufferSlicer.prototype.createWriteStream = function(options) {
287288
writeStream.emit('progress');
288289
callback();
289290
};
290-
writeStream.destroy = function() {
291+
writeStream.destroy = function () {
291292
writeStream.destroyed = true;
292293
};
293294
return writeStream;
294295
};
295296

296-
BufferSlicer.prototype.ref = function() {
297+
BufferSlicer.prototype.ref = function () {
297298
this.refCount += 1;
298299
};
299300

300-
BufferSlicer.prototype.unref = function() {
301+
BufferSlicer.prototype.unref = function () {
301302
this.refCount -= 1;
302303

303304
if (this.refCount < 0) {

0 commit comments

Comments
 (0)