Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ jobs:
no-wasm-simd: '1'
secrets: inherit

test-with-milo:
name: Test with Node.js ${{ matrix.node-version }} on ${{ matrix.runs-on }} with Milo
strategy:
fail-fast: false
max-parallel: 0
matrix:
node-version: ['22', '24', '25', '26']
runs-on: ['ubuntu-latest']
uses: ./.github/workflows/nodejs.yml
with:
node-version: ${{ matrix.node-version }}
runs-on: ${{ matrix.runs-on }}
milo: '1'
secrets: inherit

test-without-intl:
name: Test with Node.js ${{ matrix.node-version }} compiled --without-intl
strategy:
Expand Down Expand Up @@ -315,6 +330,7 @@ jobs:
- test
- test-types
- test-with-no-wasm-simd
- test-with-milo
- test-without-intl
- test-fuzzing
- lint
Expand Down
19 changes: 19 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ on:
type: string
required: false
default: ''
milo:
type: string
required: false
default: ''

permissions:
contents: read
Expand Down Expand Up @@ -85,6 +89,7 @@ jobs:
CI: true
NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }}
UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }}
UNDICI_USE_MILO: ${{ inputs.milo }}

- name: Test node-test
run: npm run test:node-test
Expand All @@ -93,6 +98,7 @@ jobs:
CI: true
NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }}
UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }}
UNDICI_USE_MILO: ${{ inputs.milo }}

- name: Test fetch
run: npm run test:fetch
Expand All @@ -101,6 +107,7 @@ jobs:
CI: true
NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }}
UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }}
UNDICI_USE_MILO: ${{ inputs.milo }}

- name: Test node-fetch
run: npm run test:node-fetch
Expand All @@ -109,6 +116,7 @@ jobs:
CI: true
NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }}
UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }}
UNDICI_USE_MILO: ${{ inputs.milo }}

- name: Test cache
run: npm run test:cache
Expand All @@ -117,6 +125,7 @@ jobs:
CI: true
NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }}
UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }}
UNDICI_USE_MILO: ${{ inputs.milo }}

- name: Test cache-interceptor with sqlite
run: npm run test:cache-interceptor
Expand All @@ -126,6 +135,7 @@ jobs:
NODE_OPTIONS: --experimental-sqlite
NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }}
UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }}
UNDICI_USE_MILO: ${{ inputs.milo }}

- name: Test cache-tests
run: npm run test:cache-tests
Expand All @@ -134,6 +144,7 @@ jobs:
CI: true
NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }}
UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }}
UNDICI_USE_MILO: ${{ inputs.milo }}

- name: Test cookies
run: npm run test:cookies
Expand All @@ -142,6 +153,7 @@ jobs:
CI: true
NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }}
UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }}
UNDICI_USE_MILO: ${{ inputs.milo }}

- name: Test interceptors
run: npm run test:interceptors
Expand All @@ -150,6 +162,7 @@ jobs:
CI: true
NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }}
UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }}
UNDICI_USE_MILO: ${{ inputs.milo }}

- name: Test eventsource
run: npm run test:eventsource
Expand All @@ -158,6 +171,7 @@ jobs:
CI: true
NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }}
UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }}
UNDICI_USE_MILO: ${{ inputs.milo }}

- name: Test infra
run: npm run test:infra
Expand All @@ -166,6 +180,7 @@ jobs:
CI: true
NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }}
UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }}
UNDICI_USE_MILO: ${{ inputs.milo }}

- name: Test subresource-integrity
run: npm run test:subresource-integrity
Expand All @@ -174,6 +189,7 @@ jobs:
CI: true
NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }}
UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }}
UNDICI_USE_MILO: ${{ inputs.milo }}

- name: Test websocket
run: npm run test:websocket
Expand All @@ -182,6 +198,7 @@ jobs:
CI: true
NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }}
UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }}
UNDICI_USE_MILO: ${{ inputs.milo }}

- name: Test jest
run: npm run test:jest
Expand All @@ -190,6 +207,7 @@ jobs:
CI: true
NODE_V8_COVERAGE: ''
UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }}
UNDICI_USE_MILO: ${{ inputs.milo }}

- name: Test wpt
run: npm run test:wpt
Expand All @@ -198,6 +216,7 @@ jobs:
CI: true
NODE_V8_COVERAGE: ${{ inputs.codecov == true && './coverage/tmp' || '' }}
UNDICI_NO_WASM_SIMD: ${{ inputs['no-wasm-simd'] }}
UNDICI_USE_MILO: ${{ inputs.milo }}

- name: Coverage summary
if: inputs.codecov == true
Expand Down
53 changes: 51 additions & 2 deletions lib/dispatcher/parser-h1.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,42 @@ class H1Parser {
return null
}

// The peer closed the connection. If the parser was paused by
// backpressure, finish parsing before signalling EOF, as the llhttp
// parser does, otherwise a complete body that is waiting to be completed
// is reported as a truncated message. Backpressure is advisory here: the
// response keeps buffering delivered bytes, so resume across pauses and
// drain whatever is still buffered on the socket.
if (this.paused) {
let data
do {
this.milo.resume(this.parser)
this.paused = false

if (this.directBodyCompletePending) {
this.completeDirectBody()
}

if (this.paused || !this.parser) {
data = EMPTY_BUF
} else {
// Parse even when nothing is buffered: a response suspended after
// its headers, such as a bodiless 204/304, completes that way.
data = this.socket.read() || EMPTY_BUF
this.execute(data)
}
} while (this.paused && data.length > 0)

if (!this.parser) {
return null
}

if (this.paused) {
this.milo.resume(this.parser)
this.paused = false
}
}

if (this.directBodyRemaining > 0) {
return new ResponseContentLengthMismatchError()
}
Expand Down Expand Up @@ -827,6 +863,18 @@ class H1Parser {
this.contentLength =
bodyKind === BODY_KIND_CONTENT_LENGTH ? contentLength : -1

// Responses to HEAD and 204/304 responses never carry content. A
// non-zero Content-Length on a 204/304 means the server may also have
// sent content, which would then be parsed as the next response on
// this connection. Accept the response, but do not reuse the
// connection. Milo itself rejects Transfer-Encoding on these.
if (request.method === 'HEAD' || statusCode === 204 || statusCode === 304) {
if (request.method !== 'HEAD' && this.contentLength > 0) {
this.shouldKeepAlive = false
}
this.contentLength = -1
}

if (this.statusCode >= 200) {
const bodyTimeout =
request.bodyTimeout != null
Expand Down Expand Up @@ -1027,7 +1075,7 @@ class H1Parser {
}

onUpgrade (head) {
const { upgrade, client, socket, headers, statusCode } = this
const { upgrade, client, socket, headers, statusCode, statusText } = this

assert(upgrade)
assert(client[kSocket] === socket)
Expand Down Expand Up @@ -1063,8 +1111,9 @@ class H1Parser {
)

try {
request.onRequestUpgrade(statusCode, headers, socket)
request.onRequestUpgrade(statusCode, headers, socket, statusText)
} catch (err) {
util.errorRequest(client, request, err)
util.destroy(socket, err)
}

Expand Down
21 changes: 17 additions & 4 deletions test/content-length.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const net = require('node:net')
const { once } = require('node:events')
const { Readable } = require('node:stream')
const { maybeWrapStream, consts } = require('./utils/async-iterators')
const { miloEnabled } = require('./utils/milo')

test('request invalid content-length', async (t) => {
t = tspl(t, { plan: 7 })
Expand Down Expand Up @@ -515,7 +516,19 @@ function dispatchPaused (client) {
}

for (const statusCode of [204, 304]) {
test(`response ${statusCode} with content-length is not a mismatch but closes the connection`, async (t) => {
// Milo follows RFC 9110 strictly and fails the response instead. Remove
// these skips once https://github.com/ShogunPanda/milo/issues/63 is resolved.
const miloSkip = (framing) => {
if (!miloEnabled) {
return false
}
if (framing === 'transfer-encoding') {
return 'Milo rejects Transfer-Encoding on 204 and 304 responses'
}
return statusCode === 204 && 'Milo rejects Content-Length on 204 responses'
}

test(`response ${statusCode} with content-length is not a mismatch but closes the connection`, { skip: miloSkip('content-length') }, async (t) => {
let connections = 0
const server = createRawServer(t, (socket) => {
connections++
Expand Down Expand Up @@ -548,7 +561,7 @@ for (const statusCode of [204, 304]) {
assert.ok(disconnects.every((code) => code === 'UND_ERR_INFO'))
})

test(`response ${statusCode} with content-length: 0 keeps the connection`, async (t) => {
test(`response ${statusCode} with content-length: 0 keeps the connection`, { skip: miloSkip('content-length') }, async (t) => {
let connections = 0
const server = createRawServer(t, (socket) => {
connections++
Expand Down Expand Up @@ -577,7 +590,7 @@ for (const statusCode of [204, 304]) {

for (const framing of ['content-length', 'transfer-encoding']) {
for (const blocking of [true, false]) {
test(`response ${statusCode} with ${framing} does not let its content become a pipelined response (blocking: ${blocking})`, async (t) => {
test(`response ${statusCode} with ${framing} does not let its content become a pipelined response (blocking: ${blocking})`, { skip: miloSkip(framing) }, async (t) => {
const forged = 'HTTP/1.1 200 OK\r\ncontent-length: 6\r\n\r\nFORGED'
let connections = 0
const server = createRawServer(t, (socket) => {
Expand Down Expand Up @@ -616,7 +629,7 @@ for (const statusCode of [204, 304]) {
}
}

test(`response ${statusCode} with content-length completes when the connection closes while paused`, async (t) => {
test(`response ${statusCode} with content-length completes when the connection closes while paused`, { skip: miloSkip('content-length') }, async (t) => {
const server = createRawServer(t, (socket) => {
socket.once('data', () => {
socket.end(`HTTP/1.1 ${statusCode} X\r\ncontent-length: 100\r\nconnection: close\r\n\r\n`)
Expand Down
3 changes: 2 additions & 1 deletion test/issue-4389.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { once } = require('node:events')
const { createServer: createHttpServer } = require('node:http')
const { createServer: createSocketServer } = require('node:net')
const { fetch, Agent } = require('..')
const { miloEnabled } = require('./utils/milo')

describe('Interceptor should not break multi-value headers', () => {
test('Interceptor should not break Set-Cookie', async (t) => {
Expand All @@ -29,7 +30,7 @@ describe('Interceptor should not break multi-value headers', () => {

test('Interceptor should not break other multi-value header', {
// Milo intentionally does not support HTTP/1.0 responses.
skip: process.env.UNDICI_USE_MILO === 'true' && 'Milo does not support HTTP/1.0 responses'
skip: miloEnabled && 'Milo does not support HTTP/1.0 responses'
}, async (t) => {
const server = createSocketServer((socket) => {
socket.write('HTTP/1.0 204 No Content\r\n')
Expand Down
10 changes: 10 additions & 0 deletions test/utils/milo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict'

const { getMiloEnabled } = require('../../lib/dispatcher/parser-h1.js')

// Whether the HTTP/1.1 clients in this process use the experimental Milo
// parser, as decided by UNDICI_USE_MILO. Use it to skip tests for behaviors
// that Milo intentionally does not support.
module.exports = {
miloEnabled: getMiloEnabled()
}
Loading