Skip to content

Commit 1aa10e3

Browse files
committed
Merge remote-tracking branch 'upstream/main'
* upstream/main: fix: use explicit flag for when use has interacted with stream (nodejs#3361) refactor: simplify signal handling (nodejs#3362) fix: consider bytes read when dumping (nodejs#3360) websocket: don't use pooled buffer in mask pool (nodejs#3357) Revert "fix: post request signal (nodejs#3354)" (nodejs#3359) fix: post request signal (nodejs#3354) build(deps): bump node from `075a5cc` to `9af472b` in /build (nodejs#3355)
2 parents 2e522b4 + 59fc6d5 commit 1aa10e3

File tree

4 files changed

+16
-20
lines changed

4 files changed

+16
-20
lines changed

build/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM node:22-alpine3.19@sha256:075a5cc188c3c9a49acacd481a9e8a3c9abf4223f02c658e37fdb8e9fe2c4664
1+
FROM node:22-alpine3.19@sha256:9af472b2578996eb3d6affbcb82fdee6f086da2c43121e75038a4a70317f784f
22

33
ARG UID=1000
44
ARG GID=1000

lib/api/api-request.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,6 @@ class RequestHandler {
6464
} else if (this.abort) {
6565
this.abort(this.reason)
6666
}
67-
68-
if (this.removeAbortListener) {
69-
this.res?.off('close', this.removeAbortListener)
70-
this.removeAbortListener()
71-
this.removeAbortListener = null
72-
}
7367
})
7468
}
7569
}
@@ -110,6 +104,7 @@ class RequestHandler {
110104

111105
if (this.removeAbortListener) {
112106
res.on('close', this.removeAbortListener)
107+
this.removeAbortListener = null
113108
}
114109

115110
this.callback = null
@@ -152,7 +147,6 @@ class RequestHandler {
152147
}
153148

154149
if (this.removeAbortListener) {
155-
res?.off('close', this.removeAbortListener)
156150
this.removeAbortListener()
157151
this.removeAbortListener = null
158152
}

lib/api/readable.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const kContentLength = Symbol('kContentLength')
1616
const kMethod = Symbol('kMethod')
1717
const kStatusCode = Symbol('kStatusCode')
1818
const kHeaders = Symbol('kHeaders')
19+
const kUsed = Symbol('kUsed')
20+
const kBytesRead = Symbol('kBytesRead')
1921

2022
const noop = () => {}
2123

@@ -40,9 +42,11 @@ class BodyReadable extends Readable {
4042

4143
this[kAbort] = abort
4244
this[kConsume] = null
45+
this[kBytesRead] = 0
4346
this[kBody] = null
47+
this[kUsed] = false
4448
this[kContentType] = contentType
45-
this[kContentLength] = contentLength
49+
this[kContentLength] = Number.isFinite(contentLength) ? contentLength : null
4650
this[kMethod] = method
4751
this[kStatusCode] = statusCode
4852
this[kHeaders] = headers
@@ -70,7 +74,7 @@ class BodyReadable extends Readable {
7074
return this
7175
}
7276

73-
destroy (err) {
77+
_destroy (err, callback) {
7478
if (!err && !this._readableState.endEmitted) {
7579
err = new RequestAbortedError()
7680
}
@@ -79,15 +83,11 @@ class BodyReadable extends Readable {
7983
this[kAbort]()
8084
}
8185

82-
return super.destroy(err)
83-
}
84-
85-
_destroy (err, callback) {
8686
// Workaround for Node "bug". If the stream is destroyed in same
8787
// tick as it is created, then a user who is waiting for a
8888
// promise (i.e micro tick) for installing a 'error' listener will
8989
// never get a chance and will always encounter an unhandled exception.
90-
if (!this[kReading]) {
90+
if (!this[kUsed]) {
9191
setImmediate(() => {
9292
callback(err)
9393
})
@@ -99,6 +99,7 @@ class BodyReadable extends Readable {
9999
on (ev, ...args) {
100100
if (ev === 'data' || ev === 'readable') {
101101
this[kReading] = true
102+
this[kUsed] = true
102103
}
103104
return super.on(ev, ...args)
104105
}
@@ -123,6 +124,8 @@ class BodyReadable extends Readable {
123124
}
124125

125126
push (chunk) {
127+
this[kBytesRead] += chunk ? chunk.length : 0
128+
126129
if (this[kConsume] && chunk !== null) {
127130
consumePush(this[kConsume], chunk)
128131
return this[kReading] ? super.push(chunk) : true
@@ -156,7 +159,7 @@ class BodyReadable extends Readable {
156159
}
157160

158161
async dump (opts) {
159-
let limit = Number.isFinite(opts?.limit) ? opts.limit : 128 * 1024
162+
const limit = Number.isFinite(opts?.limit) ? opts.limit : 128 * 1024
160163
const signal = opts?.signal
161164

162165
if (signal != null && (typeof signal !== 'object' || !('aborted' in signal))) {
@@ -170,7 +173,7 @@ class BodyReadable extends Readable {
170173
}
171174

172175
return await new Promise((resolve, reject) => {
173-
if (this[kContentLength] > limit) {
176+
if (this[kContentLength] > limit || this[kBytesRead] > limit) {
174177
this.destroy(new AbortError())
175178
}
176179

@@ -190,8 +193,7 @@ class BodyReadable extends Readable {
190193
})
191194
.on('error', noop)
192195
.on('data', function (chunk) {
193-
limit -= chunk.length
194-
if (limit <= 0) {
196+
if (this[kBytesRead] > limit) {
195197
this.destroy()
196198
}
197199
})

lib/core/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ function addAbortListener (signal, listener) {
534534
signal.addEventListener('abort', listener, { once: true })
535535
return () => signal.removeEventListener('abort', listener)
536536
}
537-
signal.addListener('abort', listener)
537+
signal.once('abort', listener)
538538
return () => signal.removeListener('abort', listener)
539539
}
540540

0 commit comments

Comments
 (0)