Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement POST/PUT support for protocol file uploads with enhanced stream handling #3

Open
wants to merge 17 commits into
base: v1
Choose a base branch
from

Conversation

akhileshthite
Copy link
Member

Implementing the readBody from:
https://github.com/AgregoreWeb/agregore-browser/blob/master/app/protocols/fetch-to-handler.js

IPFS handler:

It's publishing but when I try to access the IPFS CID, it downloads a file instead of opening the content, the file looks like this:

------WebKitFormBoundarynTM5GoLOUy5xJ7Dv
Content-Disposition: form-data; name="file"; filename="index.html"
Content-Type: text/html


------WebKitFormBoundarynTM5GoLOUy5xJ7Dv--

Hyper handler:

That's also publishing but when clicked on the hyper URL it shows empty index.

Error in [object File]: TypeError: input.subarray(...).equals is not a function at multipartFormDataParser (node:internal/deps/undici/undici:3830:84) at node:internal/deps/undici/undici:4247:34 at successSteps (node:internal/deps/undici/undici:4288:27) at consumeBody (node:internal/deps/undici/undici:4294:9) at _Request.formData (node:internal/deps/undici/undici:4242:18) at Object.putFiles [as handler]

@akhileshthite akhileshthite added the enhancement New feature or request label Aug 6, 2024
@akhileshthite akhileshthite self-assigned this Aug 6, 2024
Copy link
Collaborator

@RangerMauve RangerMauve left a comment

Choose a reason for hiding this comment

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

What are you using to upload files? If you're using a <form> or FormData then you need to parse the body. e.g.: https://github.com/RangerMauve/hypercore-fetch/blob/master/index.js#L402

I currently parse the form data using the built in Request API in node.js

@@ -13,6 +13,22 @@ async function initializeHyperSDK(options) {
return fetch;
}

// Helper function to read and handle body data, especially for 'PUT' and 'POST'
async function readBody(bodyStream) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

you shouldn't be concatenating the response on a single buffer like that, it'll lag your computer on large files. You should return the stream of the body in the callback instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

With large files:

Error in divine_reflections.mp4: HypercoreError: BAD_ARGUMENT: Appended block exceeds the maximum suggested block size at Hypercore.append

data.push(chunk);
}

const fileBuffer = uint8ArrayConcat(data); // Concatenate all chunks into a single buffer
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of doing a concat you should pass a byte stream. Else it'll lag on large files

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Works with large files

@akhileshthite
Copy link
Member Author

akhileshthite commented Aug 7, 2024

What are you using to upload files? If you're using a <form> or FormData then you need to parse the body. e.g.: https://github.com/RangerMauve/hypercore-fetch/blob/master/index.js#L402

I currently parse the form data using the built in Request API in node.js

I'm testing it on the agregore tutorials:

peersky://p2p/upload/index:
https://github.com/p2plabsxyz/peersky-browser/tree/upload/src/pages/p2p/upload

peersky://p2p/editor/index:
https://github.com/p2plabsxyz/peersky-browser/tree/upload/src/pages/p2p/editor

Getting this after implementing the changes:

Handling request: POST hyper://localhost/?key=drag-and-drop
Headers: {
  'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) peersky-browser/1.0.0 Chrome/126.0.6478.183 Electron/31.3.0 Safari/537.36',
  Accept: '*/*'
}
Body content read for non-multipart request.
Response received: 200
Handling request: PUT hyper://w5yhg6uct7tip4y9nazk8o51b8fs4nnfk6fnxeci1o4ck1eoxxby/
Headers: {
  'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) peersky-browser/1.0.0 Chrome/126.0.6478.183 Electron/31.3.0 Safari/537.36',
  'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundarysXA9s7VnYGTXyccE',
  Accept: '*/*'
}
Error handling form data: TypeError: request.formData is not a function
    at handleMultipartFormData (file:///Users/akhileshthite/Desktop/peersky-browser/src/protocols/hyper-handler.js:19:36)
    at AsyncFunction.protocolHandler (file:///Users/akhileshthite/Desktop/peersky-browser/src/protocols/hyper-handler.js:75:31)
Failed to handle Hyper request: TypeError: request.formData is not a function
    at handleMultipartFormData (file:///Users/akhileshthite/Desktop/peersky-browser/src/protocols/hyper-handler.js:19:36)
    at AsyncFunction.protocolHandler (file:///Users/akhileshthite/Desktop/peersky-browser/src/protocols/hyper-handler.js:75:31)

On the page:

Error in [object FileList]:
hyper://w5yhg6uct7tip4y9nazk8o51b8fs4nnfk6fnxeci1o4ck1eoxxby/

Index of /
../

@RangerMauve
Copy link
Collaborator

I think you should spend a bit more time debugging the stack trace to see why the error is happening.

@akhileshthite akhileshthite changed the title Implement readBody: Busboy Multipart Data Errors feat: implement POST/PUT support for protocol file uploads with enhanced stream handling Dec 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants