bee-js icon indicating copy to clipboard operation
bee-js copied to clipboard

[v2.0.0][Node.js][stream] `uploadFile` stalls with 8MB file, uploading enclosing directory with `uploadFilesFromDirectory` succeeds

Open Cafe137 opened this issue 3 years ago • 8 comments

I have not debugged it much yet, but I encountered this issue in swarm-cli.

I have an 8MB binary file for testing purposes. When I try to upload it, the bee.uploadFile never settles.

When I upload the directory containing the 8MB binary file as well as some other smaller files, the operation succeeds (bee.uploadFilesFromDirectory).

curl -X POST http://localhost:1633/bzz?name=8mb.bin -H "accept: application/json, text/plain, */*" -H "content-length: 8000000" -H "content-type: application/octet-stream" -H "swarm-postage-batch-id: 6f4622d44519709f66ae1e6c52f2000c232e30cb867713a983660e49c85a5552" -H "user-agent: bee-js/2.0.0"

Cafe137 avatar Oct 05 '21 13:10 Cafe137

I am uploading to an Ubuntu 20.04 VPS with Bee-Factory running 1.1 with one queen and one worker.

Cafe137 avatar Oct 05 '21 13:10 Cafe137

Also there are no logs in Bee indicating that anything is happening at all

Cafe137 avatar Oct 05 '21 14:10 Cafe137

Thanks for the report. Will investigate!

AuHau avatar Oct 06 '21 16:10 AuHau

image

First I tried uploading RANDOM uncompressable 8MB -> I cancelled because it gets nowhere

Then put it in a dir, uploaded dir -> succeeds very shortly

Uploaded single file README.md which is small -> succeeds immediately

If I upload it via curl, it succeeds very shortly

curl -X POST "http://localhost:1633/bzz?name=8mb.bin" -H "accept: application/json, text/plain, */*" -H "content-type: application/octet-stream" -H "swarm-postage-batch-id: d535b3315354becdc7d843ad7b9f6ff1dd31ae23a99906cbff491da2b0cfd13a" -H "user-agent: bee-js/2.0.0" --data @rand/8mb.bin

Cafe137 avatar Oct 08 '21 08:10 Cafe137

I tried to get a simple ky POST working with ReadStream, but ky expects ReadableStream - the two are not compatible.

import { createReadStream } from 'fs'
import ky from 'ky-universal'

const stream = createReadStream('chunk.bin')

const instance = ky.create({
  prefixUrl: 'http://localhost:1633',
  timeout: false,
})

instance.post('bzz?name=chunk.bin', {
  headers: {
    'swarm-postage-batch-id': 'd535b3315354becdc7d843ad7b9f6ff1dd31ae23a99906cbff491da2b0cfd13a',
    'content-length': '8000000',
    'content-type': 'application/octet-stream',
  },
  body: stream,
})

Cafe137 avatar Oct 11 '21 08:10 Cafe137

Well, did it really errored out? Or what is exactly the problem? Types?

ky is typed for the WHATWG ReadableStream but on NodeJS it uses node-fetch which supports Readable (and hence ReadStream which is extended from Readable) and hence the types are not really correct.

AuHau avatar Oct 11 '21 08:10 AuHau

Yeah so TypeScript kept yelling at me that the types are not compatible, but when I ran it anyways (maybe a slightly modified version of the code) I ran into a runtime error too. Bee-js 2.0 works well with Buffer instead ReadStream, so I am focusing on getting my PR ready that way, then I can continue investigating this issue.

Cafe137 avatar Oct 11 '21 09:10 Cafe137

Hmmm, honestly I think there some other problem as it should work and it works currently in the Bee-js. If ky would not be able to read the stream, not even the first chunk would leave (for me it was actually multiple chunks)...

But yeah, let's do the workaround as discussed, only please don't use Buffer as you would to polyfill it for browsers. Or you can use it for NodeJS part, but not in browsers if you think, but generally I guess better would be to use Uint8Array (I guess you will have to employ Blob for that as well).

AuHau avatar Oct 11 '21 09:10 AuHau