commit c1e3f688d5b030a9cebb6d2e9f1931baa31780fc
parent 19572d990a4114e1da8d9bd803a55e62e2afead5
Author: Lorenz A <me@lorenzackermann.xyz>
Date: Mon, 8 Dec 2025 08:07:33 +0000
Bug 2004225 - [devtools] Turn stream-utils.js into an ES class. r=devtools-reviewers,nchevobbe
Differential Revision: https://phabricator.services.mozilla.com/D275238
Diffstat:
1 file changed, 51 insertions(+), 51 deletions(-)
diff --git a/devtools/shared/transport/stream-utils.js b/devtools/shared/transport/stream-utils.js
@@ -60,49 +60,49 @@ function copyStream(input, output, length) {
return copier.copy();
}
-function StreamCopier(input, output, length) {
- EventEmitter.decorate(this);
- this._id = StreamCopier._nextId++;
- this.input = input;
- // Save off the base output stream, since we know it's async as we've required
- this.baseAsyncOutput = output;
- if (IOUtil.outputStreamIsBuffered(output)) {
- this.output = output;
- } else {
- this.output = Cc[
- "@mozilla.org/network/buffered-output-stream;1"
- ].createInstance(Ci.nsIBufferedOutputStream);
- this.output.init(output, BUFFER_SIZE);
+class StreamCopier {
+ static _nextId = 0;
+
+ constructor(input, output, length) {
+ EventEmitter.decorate(this);
+ this._id = StreamCopier._nextId++;
+ this.input = input;
+ // Save off the base output stream, since we know it's async as we've required
+ this.baseAsyncOutput = output;
+ if (IOUtil.outputStreamIsBuffered(output)) {
+ this.output = output;
+ } else {
+ this.output = Cc[
+ "@mozilla.org/network/buffered-output-stream;1"
+ ].createInstance(Ci.nsIBufferedOutputStream);
+ this.output.init(output, BUFFER_SIZE);
+ }
+ this._length = length;
+ this._amountLeft = length;
+ let _resolve;
+ let _reject;
+ this._deferred = new Promise((resolve, reject) => {
+ _resolve = resolve;
+ _reject = reject;
+ });
+ this._deferred.resolve = _resolve;
+ this._deferred.reject = _reject;
+
+ this._copy = this._copy.bind(this);
+ this._flush = this._flush.bind(this);
+ this._destroy = this._destroy.bind(this);
+
+ // Copy promise's then method up to this object.
+ // Allows the copier to offer a promise interface for the simple succeed or
+ // fail scenarios, but also emit events (due to the EventEmitter) for other
+ // states, like progress.
+ this.then = this._deferred.then.bind(this._deferred);
+ this.then(this._destroy, this._destroy);
+
+ // Stream ready callback starts as |_copy|, but may switch to |_flush| at end
+ // if flushing would block the output stream.
+ this._streamReadyCallback = this._copy;
}
- this._length = length;
- this._amountLeft = length;
- let _resolve;
- let _reject;
- this._deferred = new Promise((resolve, reject) => {
- _resolve = resolve;
- _reject = reject;
- });
- this._deferred.resolve = _resolve;
- this._deferred.reject = _reject;
-
- this._copy = this._copy.bind(this);
- this._flush = this._flush.bind(this);
- this._destroy = this._destroy.bind(this);
-
- // Copy promise's then method up to this object.
- // Allows the copier to offer a promise interface for the simple succeed or
- // fail scenarios, but also emit events (due to the EventEmitter) for other
- // states, like progress.
- this.then = this._deferred.then.bind(this._deferred);
- this.then(this._destroy, this._destroy);
-
- // Stream ready callback starts as |_copy|, but may switch to |_flush| at end
- // if flushing would block the output stream.
- this._streamReadyCallback = this._copy;
-}
-StreamCopier._nextId = 0;
-
-StreamCopier.prototype = {
copy() {
// Dispatch to the next tick so that it's possible to attach a progress
// event listener, even for extremely fast copies (like when testing).
@@ -114,7 +114,7 @@ StreamCopier.prototype = {
}
});
return this;
- },
+ }
_copy() {
const bytesAvailable = this.input.available();
@@ -146,14 +146,14 @@ StreamCopier.prototype = {
this._debug("Waiting for input stream");
this.input.asyncWait(this, 0, 0, Services.tm.currentThread);
- },
+ }
_emitProgress() {
this.emit("progress", {
bytesSent: this._length - this._amountLeft,
totalBytes: this._length,
});
- },
+ }
_flush() {
try {
@@ -172,7 +172,7 @@ StreamCopier.prototype = {
throw e;
}
this._deferred.resolve();
- },
+ }
_destroy() {
this._destroy = null;
@@ -180,24 +180,24 @@ StreamCopier.prototype = {
this._flush = null;
this.input = null;
this.output = null;
- },
+ }
// nsIInputStreamCallback
onInputStreamReady() {
this._streamReadyCallback();
- },
+ }
// nsIOutputStreamCallback
onOutputStreamReady() {
this._streamReadyCallback();
- },
+ }
_debug(msg) {
// Prefix logs with the copier ID, which makes logs much easier to
// understand when several copiers are running simultaneously
dumpv("Copier: " + this._id + " " + msg);
- },
-};
+ }
+}
/**
* Read from a stream, one byte at a time, up to the next |delimiter|