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

qsftp

v0.6.2

Published

An amqp to sftp relay

Downloads

46

Readme

qsftp

An amqp to sftp relay

Installation

npm install --save qsftp

Prequisits

  1. An sftp server
  2. A RabbitMQ broker
  3. Familiarity with Rascal

Setup

Step 1. Configure a subscriber using rascal

{
    "rascal": {
        "vhosts": {
            "/": {
                "exchanges": {
                    "e1": {}
                },
                "queues": {
                    "q1": {}
                },
                "bindings": {
                    "b1": {
                        "source": "e1",
                        "destination": "q1"
                    }
                }
            }
        },
        "subscriptions": {
            "s1": {
                "queue": "q1"
            }
        }
    }
}

Step 2. Configure the qsftp middleware

    "qsftp": {
        "routes": {
            "book_loan_v1": {
                "sequence": ["messageToTemplateVars",  "messageToPath", "uploadToSftpServer"],
                "warez": {
                    "messageToPath": {
                        "options": {
                            "template": "examples/{{message.properties.messageId}}.txt"
                        }
                    },
                    "uploadToSftpServer": {
                        "options": {
                            "hostname": "localhost",
                            "port": 10022,
                            "username": "fred",
                            "password": "password"
                        }
                    }
                }
            }
        }
    }

Step 3. Wire up rascal and your middleware

var rascal = require('rascal')
var ware = require('ware');
var qsftp = require('qsftp')
var config = require('./config.json')

rascal.createBroker(rascal.withDefaultConfig(config.rascal), function(err, broker) {
    if (err) return bail(err)

    qsftp.init(config.qsftp.routes.book_loan_v1, {}, function(err, warez) {
        if (err) return bail(err)

        var middleware = ware().use(warez)

        broker.subscribe('s1', function(err, subscription) {
            if (err) return bail(err)
            subscription.on('message', function(message, content, ackOrNack) {
                middleware.run({}, message, content, function(err) {
                    if (err) return bail(err)
                    ackOrNack()
                })
            })
        })
    })
})

From this point on, any message sent to the queue will be uploaded to the remote sftp server

Middleware

qsftp comes with some out of the box middleware but it's easy to substitute or include your own. All you need is a module that exports a single "initialiser" function, responsible for initialising the middleware, e.g.

module.exports = function(config, context, callback) {
    callback(null, function(flowScope, message, content, next) {
        // Do stuff
    })
}

The initialiser function should expect three arguments, config, context and a callback. "config" will contain any options specified in the qsftp warez configuration, "context" is in case you need shared state between your middleware and "callback" should provide the middleware function to qsftp

The middleware function should expect four arguments, flowScope, message, content and next. "flowScope" is a context object scoped to this run of the middleware, "message" is the raw message received from amqp, "content" is the message content (json, text or buffer) and "next" is the callback to continue to the next middleware in the sequence.

You specify your middleware when initialising qsftp

qsftp.init(config, { myCustomMiddleware: myCustomMiddleware }, function(err, warez) {
    // ...
})

Provided Middleware

messageToTemplateVars

Copies the raw message and content to flowScope.qsftp.templateVars for use in later middleware

messageTimestampToTemplateVars

Converts a timestamp (milliseconds since 01/01/1970) into a date string and stores it in flowScope.qsftp.templateVars[destintation] for use in later middleware.

{
    "messageTimestampToTemplateVars": {
        "options": {
            "source": "/properties/headers/timestamp",
            "destination": "date",
            "template": "YYYY/MM/DD"
        }
    }
}

Source is json pointers. Destination is a simple key. The template is a moment date format string.

contentDispositionToTemplateVars

Parses the contentDisposition header to to templateVars. The contentDisposition must be set as an attribute of the headers object, which is itself an attribute of the properties object. See the Rascal documentation for how to set message headers when publishing. Once set the content disposition can be used when rendering the upload path, e.g.

{
    "messageToPath": {
        "options": {
            "template": "uploads/{{contentDisposition.filename}}"
        }
    }
}

messageToPath

Extracts a path (for the sftp upload) by passing the template variables through a hogan.js template. The rendered value is stored in flowScope.qsftp.path

{
    "messageToPath": {
        "options": {
            "template": "examples/{{message.properties.messageId}}.txt"
        }
    }
}

uploadToSftpServer

Uploads the message content to an sftp server using the path specified in flowScope.qsftp.path.

{
    "uploadToSftpServer": {
        "options": {
            "hostname": "localhost",
            "port": 10022,
            "username": "fred",
            "password": "password",
            "folder": "/base"
        }
    }
}