npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fork-func

v0.2.1

Published

Executes a function in a child process, as easy as calling a ordinary function ;-)

Downloads

22

Readme

fork-func

Executes a function in a child process, as easy as simply calling it ;-)

usage

    
    npm install fork-func --save
      
    
    fork = require 'fork-func'
    
    fork './myFunc', args..., (error, result) ->  # external
    fork myFunc, args..., (error, result) ->      # by reference
      

calling external source

      
    # function script './heavy-func'
    module.exports = (args...) ->
         # some heavy, long running stuff
         return result
         
         
    # main script './main'
    fork = require 'fork-func'
    
    fork './heavy-func', arg0, arg1, ..., (error, result) ->
        if error
            console.log error
        else                                          
            console.log result  
                           

calling functions by reference
You also can execute a function, existing somewhere in your code, in a child process.
This only works, if the function doesn't use references to an outer scope and is done by performing a toSreing() on the function and using eval() in the child process to execute the function.

      
    # blocking function
    wait = (delay, msg) ->
        start = Date.now()
        while Date.now() - start < delay
            null
        delay + 'ms later ... ' + msg
         
         
    fork = require 'fork-func'
    
    fork wait, 1000, 'not blocked', (error, result) ->
        if error
            console.log error
        else                                          
            console.log result  
            
    # logs: 1000ms later ... not blocked                
                           

signature

    
    cp = fork pathOrFunc, args..., callback
    

pathOrFunc
You can pass an absolute or relative path to a module, exporting a function. If relative, it must be relative to the calling module like you would do in require. You can also reference node_modules by their name like you would do in require.
If the function is a named export of a module you can append the name separated by :: to the path.
For example, if you have a module './a' exporting a function b, you can do:

fork './a::b', arg0, arg1, ..., callback

If you pass a function, this function will be called in a child process.

args...
Any number of arguments, you want to pass to the function. The values you use must be serializable by JSON.stringify.

callback
A function expecting two arguments. First a possible error and second the result of the function call.

return
The child-process instance is returned. You can use it, to kill the processor or do other stuff.

That's it ;-)
But wait...
What, if the function we want to call itself is asynchronous and the result is unknown, when the function returns. For example, can we use setTimeout?

async

    
    # function script './heavy-async-func' - could also be inlined
    module exports = (delay, args...) ->
        setTimeout () ->
            # we now know, what we want to return
        , delay
        null # we don't know, what to return
        

We can simply do this:

    
    # main script './main'
    fork = require 'fork-func'
    
    fork.async './heavy-async-func', 1000, arg1, ..., (error, result) ->
        if error
            console.log error
        else                                          
            console.log result  
    

Looks similar to the synchronous version, except that we call async on fork-func, doesn't it?
But we have to do one more thing:

    
    # function script './heavy-async-func' - could also be inlined
    module exports = (delay, callback) ->
        setTimeout () ->
            # if we had an error
            # error = 'we had a bad error!'
            callback error, 'we now know, what we want to return'
        , delay
        null # we don't know, what to return
    
    # the passed callback has the same signature as the callback passed to fork-func:
    
    callback = (error, result) ->
    

You can expect (and have to use) that callback in your function when you call fork-func.async.
And whatever your function returns will be ignored (i currently can't find any reasonable task for the returned value, within this asynchronous variant).

Ok, one more sugar.
You can decorate any object with simpler to use functions like so:

pimp

      
    obj = {}
    fork.pimp obj, './heavy-func', async 
    
    # creates a method 'heavyFunc' on obj which you can call without the path argument
    
    # async = true creates a fork.async version
    # async = false creates a fork.sync version
    # async = undefined/null creates the normal fork version
    
    obj.heavyFunc arg0, arg1, ..., callback
    
    # as you realized, kebab-case will be converted to camelCase
    # and as you expect, if a named function is called ('./a::b'), that name (b) is used
    
    # if thats not enough, you can specify a custom name:
    
    fork.pimp obj, 'tallFunc', './heavy-func'
    
    # will create obj.tallFunc
    

The following definitely doesn't make sense!
I had the use case to require a js or coffee file which exports a config and changed over time. I wanted to require the new file but got the same instance as before. The result was cached by nodes module system.
With fork-func you could do it easily in a child process to get a fresh config ;-)
But, as i noticed later, you better can do delete require.cache[path]!!!
So, for me the sync variant is definitely useless.

sync

    
    getCfg = (path) -> require path
        
    cfg = fork.sync getCfg, path  # path must be absolute!!! 
             

(I will keep the sync method, only for remembering me not to implement senseless stuff caused by not knowing the api ;-) )

P.S.: fork-func tries to capture errors by either serializing the name, message and stack of a child process error or delegating an ipc error or one, witch is thrown on child process creation.

Enjoy!

License

fork-func is free and unencumbered public domain software. For more information, see http://unlicense.org/ or the accompanying UNLICENSE file.