Merged
Conversation
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: |
KhafraDev
approved these changes
Jul 26, 2024
Member
KhafraDev
left a comment
There was a problem hiding this comment.
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.
Collaborator
Author
|
Cool, sounds good to me! |
Merged
Closed
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
I've been toying with the
duplexoption, 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 therequestorresponseindividually?It's a bit confusing.
WDYT @KhafraDev ?