Skip to content

Update body parsing and file streaming exemples#1128

Closed
BV-WebDev wants to merge 1 commit intouNetworking:masterfrom
BV-WebDev:patch-1
Closed

Update body parsing and file streaming exemples#1128
BV-WebDev wants to merge 1 commit intouNetworking:masterfrom
BV-WebDev:patch-1

Conversation

@BV-WebDev
Copy link
Contributor

@BV-WebDev BV-WebDev commented Oct 27, 2024

The body parsing and file streaming examples were difficult to understand and adapt to my needs.
Here I updated them to be simpler and better documented.

  • Rewrites "JsonPost.js" to "ParseRequestBody.js".
  • Merges "VideoStreamer.js" and "VideoStreamerSync.js" into "FileStreaming.js".
  • Removes unnecessary toArrayBuffer() helper.
  • Removes useless getWriteOffset() call.
  • Closes the response on stream error.
  • Fixes multiple onWritable() registration.
  • Fixes missing cork() call.

@uNetworkingAB
Copy link
Contributor

Don't remove examples. Esp. not when the substitute is slower:

alexhultman@MacBookAir uSockets % ./http_load_test 256 localhost 9001 16 1
Using pipeline factor of 16
Using post with body
request size 1744
Running benchmark now...
Req/sec: 508488.000000
Req/sec: 510892.000000
Req/sec: 511276.000000
Req/sec: 513292.000000

alexhultman@MacBookAir uSockets % ./http_load_test 256 localhost 9001 16 1
Using pipeline factor of 16
Using post with body
request size 1744
Running benchmark now...
Req/sec: 482788.000000
Req/sec: 489336.000000
Req/sec: 489724.000000

It might look as "unoptimized code" to you, but it's not. Sure, there is a missing call to cork(), so you could add that call.

@BV-WebDev
Copy link
Contributor Author

I updated the body parsing example, you should get better performance.
"unoptimized code" was maybe not the right word, I was talking about the size of the code.
As these are examples, I think it’s important that they be easily understandable.

About changes:
Rewriting "JsonPost.js" to "ParseJsonOrFormBody.js" and add URL-encoded form body parsing example.
Merging and rewriting "VideoStreamer.js" and "VideoStreamerSync.js" to "FileStreaming.js".

@uNetworkingAB
Copy link
Contributor

I was talking about the size of the code.
As these are examples, I think it’s important that they be easily understandable.

We don't optimize for size, we optimize for speed. And examples should show the best way for speed. If you want to optimize for size, use Express.

@BV-WebDev
Copy link
Contributor Author

We don't optimize for size, we optimize for speed. And examples should show the best way for speed. If you want to optimize for size, use Express.

I see, but it's not really newcomer friendly, the body parsing example was the one that makes me doubt to use uWS.js, it makes it look so hard just to parse the body.
In any case, the updated examples should have the same performances while being simpler.

If performance is realy the main point and as parsing request body is an important feature, we could add a parseBody or onParsedBody function directly on the HttpResponse c++ wrapper.
So Node.js would only have to process the final fulfilled ArrayBuffer.
And we use the onData function for uploading or client side streaming.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants