Modules

Scramjet modules are node modules that allow you to easily reuse a chain of transforms and even publish it for others to use. In this article we'll drill down on ways to write and use modules.

Module usage

Let's start with absolute basics, how modules are used based on scramjet-hello-module:

A module needs to be installed with this simple shell command:

$ npm i scramjet-hello-module

Then we use it by passing it to the use method of any Scramjet stream.

DataStream
  .from(someSource)
  .use("scramjet-hello-module", argument1);

Modules will work also on any standard node.js stream:

import hello from "scramjet-hello-module";

hello(myStream, argument1); // returns a new stream

You can also use modules that are not published in NPM or in node_modules:

  stream.use("./my-parse-module") // will look for a file in the same directory as the file where it's used.
  stream.use("/path/to/some/module") // will look for a certain path
  stream.use("my-module/submodule") // will in node_modules/my-module/submodule.js

Finally sometimes you may want to use multiple modules:

DataStream.from(someSource)
  .use("./find-names")
  .use("scramjet-hello-module", argument1)
  // ... and so on.
;

Developing a module

As an example we'll use a module that converts the stream entries to numbers and filters out non-numbers.

Basics first - it's all in a simple file:

module.exports = require("scramjet").createTransformModule(
  stream => stream
    .map(x => parseFloat(x))
    .filter(x => isNaN(x))
)

If you save it as ./only-numbers you can use it in your code to filter out non-numbers from any of your streams in the specific project you put your file in. That's it, you're done, use it and have fun...

But what if you need to use this module in another project?

Fair question. Why not make an actual node module out of it - what you need is a package.json and somewhere to store it - a git repo works well.

Create a directory and put your only-numbers.js file.

# create your local git repo
$ git init .

# add your remote (create it on github or wherever you like)
$ git remote add origin [email protected]:yourname/yourmodule.git

# Answer some questions here and you'll get a nice package json.
# make sure that the answer to "entry point: (index.js)" is "only-numbers.js"
$ npm init

# Add commit and push
$ git add . && git commit -m "Initial commit" && git push

You'll end up with a module in git that should have the following files:

\
 |- .git/               # this is where your git files sit
 |- only-numbers.js     # this is your actual scramjet module
 |- package.json        # this is where your git files sit

Can I fork something to develop my module quicker?

Sure! Here's a repo made especially for you: signicode/scramjet-module-hello.

What if my module is good for public?

I congratulate you sir! Now this is fairly simple:

$ npm publish

But please consider doing a couple things first:

  • Read the NPM guide on publishing packages - this will make you package stand out.
  • Consider adding some checks like Snyk so that you keep your package up to date.