ipfs-inactive / js-ipfs-http-client

[ARCHIVED] now part of the https://github.com/ipfs/js-ipfs repo
Other
1.05k stars 299 forks source link

getReadableStream returns Readable with length 0 after migration to the latest version #1213

Closed Rkrushanovskij closed 4 years ago

Rkrushanovskij commented 4 years ago

After migration to the latest ipfs-http-client version I faced troubles with getReadableStream. Versions: 32.0.1 -> 40.1.0

I am uploading file to IPFS from FS using: ipfs.addFromFs(path, options) and getting such result:

[
  {
    path: 'test.txt',
    hash: 'Qmcx3cKnJg4dvCZpktXD14D6zFY1E8YqpTZW95BhAVc7KB',
    size: 72
  }
]

AFAIU it means that file uploaded successfully and this hash have some content (non-zero size).

I am trying to get readable stream from IPFS for this file but returned stream is 0 length: ipfs.getReadableStream(hash)

{
  path: 'Qmcx3cKnJg4dvCZpktXD14D6zFY1E8YqpTZW95BhAVc7KB',
  content: Readable {
    _readableState: ReadableState {
      objectMode: false,
      highWaterMark: 16384,
      buffer: BufferList { head: null, tail: null, length: 0 },
      length: 0,
      pipes: null,
      pipesCount: 0,
      flowing: null,
      ended: false,
      endEmitted: false,
      reading: false,
      sync: true,
      needReadable: false,
      emittedReadable: false,
      readableListening: false,
      resumeScheduled: false,
      paused: true,
      emitClose: true,
      autoDestroy: false,
      destroyed: false,
      defaultEncoding: 'utf8',
      awaitDrain: 0,
      readingMore: false,
      decoder: null,
      encoding: null
    },
    readable: true,
    _read: [AsyncFunction: read],
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined
  }
}

It is strange that same hash that during uploading had size 72 now displayed as zero-length.

Rkrushanovskij commented 4 years ago

Hello? :(

alanshaw commented 4 years ago

Are you using content._readableState.length? Firstly, I would suggest not depending on this value, by convention underscore prefixed members are considered private and are not usually part of the public API.

Secondly, streams by their nature are not guaranteed to know their length before they are consumed. There is nothing in the public API for streams that allows you to know this.

You could consume the stream and then you'd know the length or you can use ipfs.ls to get size information for a given file.

Hope that helps.