How to create destination path for node.js stream pipe?

Mitar

I would like to pipe a node.js stream into an output file. But I would also like to make sure that output destination exists before I start piping. The problem with the following code is that event handlers from pipe are added late (after assurePath callback is called) so data is not available anymore in the input stream. I would not like to use sync fs methods to not block the whole process.

assurePath = (path, callback) ->
  path = path.split path.sep
  i = 0
  async.eachSeries path[1...path.length-1], (segment, callback) =>
    i++
    p = path[0..i].join path.sep
    fs.exists p, (exists) =>
      return callback() if exists
      fs.mkdir p, callback
  ,
    callback

saveStream = (filename, stream, callback) ->
  path = fullPath filename
  assurePath path, (error) ->
    return callback error if error

    finished = false
    stream.on('error', (error) ->
      return if finished
      finished = true
      callback error
    ).pipe(
      fs.createWriteStream path
    ).on('finish', ->
      return if finished
      finished = true
      callback()
    ).on('error', (error) ->
      return if finished
      finished = true
      callback error
    )
Leonid Beschastny

You can try pausing your stream right before calling assurePath and then resuming it in callback:

saveStream = (filename, stream, callback) ->
  stream.pause()
  path = fullPath filename
  assurePath path, (error) ->
    stream.resume()
    # ...

Calling .pause() will cause your stream to stop emitting data events. Any data that becomes available will remain in the internal buffer. Calling .resume() in assurePath callback will cause your stream to resume emitting data events.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to pipe a stream using pdfkit with node js

From Dev

Unhandled stream error in pipe in Node.js

From Dev

How do pipe (stream of Node.js) and bl (BufferList) work together?

From Dev

How to properly react to an error event in a Node.js stream pipe-line?

From Dev

How to pipe upload stream of large files to Azure Blob storage via Node.js app on Azure Websites?

From Dev

How to pipe stream to reply in hapi.js

From Dev

Node.js Request Stream Pipe Rename Filename

From Dev

Node - how can i pipe to a new READABLE stream?

From Dev

How to use pipe in levelup (node.js)?

From Dev

Node.js - Create a proxy, why is request.pipe needed?

From Dev

How to stream/pipe response object from Zombie.js

From Dev

How to stream/pipe response object from Zombie.js

From Dev

Node.js and Requestjs: Pipe a Transform stream to Request.js as a file for upload

From Dev

How to search stream for string in node.js?

From Dev

How to calculate sum of path's weight in N allowable moves defined in graph but the destination node is not fixed using Python

From Dev

What is optimal way to pipe one stream to two streams in node.js

From Dev

How to specify in Grunt.js that the destination folder path is the same as Gruntfile.js?

From Dev

How to create a Node JS Server

From Dev

Node.js. How to pass a several streams (array or chain) into pipe?

From Dev

How to check completion of a pipe chain in node.js?

From Dev

(Node.js) How to store stdout.pipe into a variable?

From Dev

Node.js: How to stop a pipe from streaming data

From Dev

Node.JS Forked Pipe

From Dev

How to stream a file from node js to angular js

From Dev

How to get the full path of an executable which is in $PATH in node.js?

From Dev

How to get the full path of an executable which is in $PATH in node.js?

From Dev

How to specify destination path using mget command

From Dev

How to use split() to remove last destination in path?

From Dev

ACE - is it possible to create a pipe using fast path?

Related Related

  1. 1

    How to pipe a stream using pdfkit with node js

  2. 2

    Unhandled stream error in pipe in Node.js

  3. 3

    How do pipe (stream of Node.js) and bl (BufferList) work together?

  4. 4

    How to properly react to an error event in a Node.js stream pipe-line?

  5. 5

    How to pipe upload stream of large files to Azure Blob storage via Node.js app on Azure Websites?

  6. 6

    How to pipe stream to reply in hapi.js

  7. 7

    Node.js Request Stream Pipe Rename Filename

  8. 8

    Node - how can i pipe to a new READABLE stream?

  9. 9

    How to use pipe in levelup (node.js)?

  10. 10

    Node.js - Create a proxy, why is request.pipe needed?

  11. 11

    How to stream/pipe response object from Zombie.js

  12. 12

    How to stream/pipe response object from Zombie.js

  13. 13

    Node.js and Requestjs: Pipe a Transform stream to Request.js as a file for upload

  14. 14

    How to search stream for string in node.js?

  15. 15

    How to calculate sum of path's weight in N allowable moves defined in graph but the destination node is not fixed using Python

  16. 16

    What is optimal way to pipe one stream to two streams in node.js

  17. 17

    How to specify in Grunt.js that the destination folder path is the same as Gruntfile.js?

  18. 18

    How to create a Node JS Server

  19. 19

    Node.js. How to pass a several streams (array or chain) into pipe?

  20. 20

    How to check completion of a pipe chain in node.js?

  21. 21

    (Node.js) How to store stdout.pipe into a variable?

  22. 22

    Node.js: How to stop a pipe from streaming data

  23. 23

    Node.JS Forked Pipe

  24. 24

    How to stream a file from node js to angular js

  25. 25

    How to get the full path of an executable which is in $PATH in node.js?

  26. 26

    How to get the full path of an executable which is in $PATH in node.js?

  27. 27

    How to specify destination path using mget command

  28. 28

    How to use split() to remove last destination in path?

  29. 29

    ACE - is it possible to create a pipe using fast path?

HotTag

Archive