question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

writing-a-plugin "Call the callback function only when the current file (stream/buffer) is completely consumed"

See original GitHub issue

What were you expecting to happen?

Following the verbal guidance here -

“Call the callback function only when the current file (stream/buffer) is completely consumed”

then in case of file.IsStream() I expected to need to wait for the ‘end’ event from the inner transform before calling the callback().

What actually happened?

As shown in the code example on that man page, it only works if callback() is called immediately.

Please post a sample of your gulpfile (preferably reduced to just the bit that’s not working)

    innerTransform.on('end', () => {
      cb(null, file) // gulp writes an empty file doing it this way
    })

Actually implementing the above code (instead of calling the callback immediately), resulted in an empty file being written, although the inner transform had dutifully pushed all the data.

What version of gulp are you using? gulp 4

What versions of npm and node are you using? npm 6.9.0 gulp 4.0.2


I am unable to convince myself that

when the current file (stream/buffer) is completely consumed

could mean anything other than the end event has been emitted. Furthermore, the wording seems perfect from a design point of view, and fits with all the usage of callbacks that I know.

How to reconcile the words and the actual design?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
donpedrocommented, Oct 7, 2019

@craigphicks that’s just an empty transformFunction; I could have just left the function out completely, as I’m not actually doing any transformation; I’m really just using newStream as a passthrough so I can use it for newFile. More here: https://www.npmjs.com/package/through2#transformfunction

Thanks for the good word! Be aware that gulp-api-adapter is very much under construction; it’s being developed to work with our gulp-etl project, but hopefully it’ll have some utility in the wider gulp world.

Hopefully it’ll be usable and stable later this week…

0reactions
jonatanlindencommented, Apr 29, 2021

Have you actually tried your code, and gotten another result? When I run my take of it (wrapping it in a function with cb as a parameter), I get the same behaviour as with my example: with size(), bogusname is empty, without it, it is not.

(I believe stream.end() indicate that no more data will be written to the stream, but it does not really affect the readable part of the stream. Maybe?)

The point was that gulp-size also seems to be written with the understanding that the callback function should be called after the vinyl content’s stream has been processed. Like this:

if (file.isStream()) {
...
  file.contents.pipe(new StreamCounter())
    .on('finish', function () {
      finish(null, this.bytes);
    });
}

Where the function finish calls the callback function.

I guess it could be possible to construct an even smaller example exposing the behaviour, using gulp-size as a starting point.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Writing a plugin
Call the callback function only when the current file (stream/buffer) is completely consumed. If an error is encountered, pass it as the first...
Read more >
How do you create a file from a string in Gulp? - Stack Overflow
Call the callback function only when the current file (stream/buffer) is completely consumed. If an error is encountered, pass it as the first...
Read more >
JavaScript Callback Functions – What are Callbacks in JS and ...
This is valid in JavaScript and we call it a “callback”. So a function that is passed to another function as a parameter...
Read more >
Stream | Node.js v19.3.0 Documentation
It is usually not necessary to use the node:stream module to consume streams. ... All the data from readable goes into 'file.txt', //...
Read more >
Introduction: callbacks - The Modern JavaScript Tutorial
function loadScript(src, callback) { let script = document. ... Now if we want to call new functions from the script, we should write...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found