GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix fetch `duplex` docs by Ethan-Arrowood · Pull Request #3422 · nodejs/undici

Conversation

@Ethan-Arrowood

@Ethan-Arrowood Ethan-Arrowood commented Jul 26, 2024

Copy link
Copy Markdown
Collaborator

I've been toying with the duplex option, and I noticed our docs are a little confusing. The implementation forces me to use 'half' for the value, but it seem like a full duplex operation... unless the 'full' duplex refers to just the request or response individually?

It's a bit confusing.

WDYT @KhafraDev ?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Ethan-Arrowood
Fix fetch duplex docs
f725194
@Ethan-Arrowood

Ethan-Arrowood commented Jul 26, 2024

Copy link
Copy Markdown
Collaborator Author

I wrote up this example:

import http from 'node:http';
import events from 'node:events';
import timers from 'node:timers/promises';

// Create a ReadableStream
const stream = new ReadableStream({
  async start(controller) {
		const chunks = ['Hello', ' ', 'World', '!'];
		for (const chunk of chunks) {
			controller.enqueue(chunk);
			await timers.setTimeout(500);
		}
		controller.close();
  }
});

const server = http.createServer((req, res) => {
	// Gather important information about the request
	const requestInfo = {
		method: req.method,
		url: req.url,
		headers: req.headers,
	};

	// Print the request information
	console.log('Incoming Request:');
	console.log(JSON.stringify(requestInfo, null, 2));

	if (req.method === 'POST') {
		res.writeHead(200, {
			// 'Content-Type': 'text/plain',
			'transfer-encoding': 'chunked'
		})

		req.on('data', (chunk) => {
			console.log('Server Received Chunk:', chunk);
			res.write(chunk);
		})

		req.on('end', ()=> {
			console.log('Server Request Ended');
			res.end();
		})
	} else {
			res.writeHead(200, { 'Content-Type': 'text/plain' });
			res.end('200 OK');
	}
});

server.listen(3000);

await events.once(server, 'listening');

const data = {
  async *[Symbol.asyncIterator]() {
    yield 'hello'
    yield 'world'
  },
}

const response = await fetch('http://localhost:3000', {
  method: 'POST',
  body: stream,
  duplex: 'half'
});

const reader = response.body.getReader();

while (true) {
	const { done, value } = await reader.read();
	if (done) break;
	console.log('Client Received Chunk:', value);
}

server.close();

And it prints out:

Incoming Request:
{
  "method": "POST",
  "url": "/",
  "headers": {
    "host": "localhost:3000",
    "connection": "keep-alive",
    "accept": "*/*",
    "accept-language": "*",
    "sec-fetch-mode": "cors",
    "user-agent": "node",
    "accept-encoding": "gzip, deflate",
    "transfer-encoding": "chunked"
  }
}
Server Received Chunk: <Buffer 48 65 6c 6c 6f>
Client Received Chunk: Uint8Array(5) [ 72, 101, 108, 108, 111 ]
Server Received Chunk: <Buffer 20>
Client Received Chunk: Uint8Array(1) [ 32 ]
Server Received Chunk: <Buffer 57 6f 72 6c 64>
Client Received Chunk: Uint8Array(5) [ 87, 111, 114, 108, 100 ]
Server Received Chunk: <Buffer 21>
Client Received Chunk: Uint8Array(1) [ 33 ]
Server Request Ended
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

KhafraDev
KhafraDev approved these changes Jul 26, 2024
View reviewed changes

@KhafraDev KhafraDev left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fetch requests are meant to be half-duplex and the duplex option was added to eventually add support for full-duplex requests. Requests in undici are always full-duplex, so our fetch inherited this behavior.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Ethan-Arrowood reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@Ethan-Arrowood

Ethan-Arrowood commented Jul 26, 2024

Copy link
Copy Markdown
Collaborator Author

Cool, sounds good to me!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Ethan-Arrowood
Ethan-Arrowood merged commit 0f45d42 into main Jul 26, 2024
@Uzlopak
Uzlopak deleted the fix-fetch-duplex-docs branch August 1, 2024 09:57
@github-actions github-actions Bot mentioned this pull request Dec 3, 2024
[Release] v7.1.0 #3922
Merged
This was referenced Dec 16, 2024
[Release] v7.1.1 #3952
Closed
[Release] v7.2.0 #3962
Merged
@github-actions github-actions Bot mentioned this pull request Mar 12, 2025
[Release] v6.21.2 #4092
Closed
@github-actions github-actions Bot mentioned this pull request May 12, 2025
[Release] v6.21.3 #4211
Closed
This was referenced Apr 18, 2026
Retire hono app InfinityBowman/corates#489
Merged
Minor improv thevladbog/quokkaq#53
Merged
@coderabbitai coderabbitai Bot mentioned this pull request Jun 19, 2026
fix: FTA score sim-cff-event-adapter KensioSoftware/yulin#52
Merged
This was referenced Jul 28, 2026
Static analysis preview: aa.1 against main — do not merge aphichat1835-coder/Questbotdiscord#16
Draft
feat(backend-next): port the cache adapters c15t/c15t#978
Merged
This was referenced Aug 22, 2026
test: migrate loose regressions to parity corpus PerryTS/perry#8611
Merged
feat(provider): add custom request headers ThinkInAIXYZ/deepchat#2208
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

1 more reviewer
@KhafraDev KhafraDev KhafraDev approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@Ethan-Arrowood @KhafraDev
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.