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

segment-local-functions

v0.1.7

Published

Enable development and testing of Segment functions locally and in your choice of TypeScript or JavaScript. Features

Downloads

16

Readme

Segment Local Functions (SLF == Sloth)

Enable development and testing of Segment functions locally and in your choice of TypeScript or JavaScript. Features

  • Develop in your choice of TypeScript or JavaScript
  • When choosing TypeScript, enable fully typed development against tracking plans
  • Debug your functions from within your IDE, including initiating source-function runs from external URL calls (to test against your actual service providers), or initiating destination-function runs from the Segment UI (to enable easy testing with events collected by Segment)
  • Test your functions locally (for automated unit/functional testing),
  • Deployment of functions

Requirements

  • Node v12 or above
  • AWS-SAM ()
  • (optional) TypeScript
  • (optional) Nodemon, for automatically updating debug/test environments on code change
  • (optional) Ngrok, for opening up a URL to your local machine so your function can be called by your service whilst tested locally.
  • (optional) Mocha, for running the tests (this is the default framework used, but any testing framework will work)

Quick Start

Install the command-line utility:

npm i -g segment-local-functions

Create a new folder for your new function

mkdir my-test-function
cd my-test-function

Initalise a new project (and see the Detailed Usage Guide below for details on the init wizard)

sloth init

Edit your function src/function.js (for JavaScript), or src/function.ts (for TypeScript)

Edit your tests src/tests/test.js (for JavaScript), or src/tests/test.ts (for TypeScript)

If using TypeScript, compile your code

tsc -p .

Launch the local instance ready for debugging your code (you may want to run this in a new terminal as it keeps control whilst it is running)

sloth debug

If using JavaScript, run your tests as follows:

mocha src/tests/test.js

Or if using TypeScript, run your tests as follows:

mocha out/tests/test.js

From within your IDE, launch a new debug session. If using Visual Studio Code, a debug launcher is set-up. Go to Debug and select "Debug Function".

If developing a source function, test your function directly against your provider. To do this, launch the Grok-tunnel:

sloth grok

Take note of the URL returned, and point your service to call to this endpoint.

If developing a destination function, test your function via the Segment UI, to enable testing against events collected within your workspace. Again, you'll need the Grok-tunnel (if not already running):

sloth grok

Go to your Segment Workspace, under Catalog -> Functions you'll see your function listed. Go into edit and run and events will be proxied to your local instance for debugging. Note: The Segment function will probably time-out and "fail" because it isn't actually running any code (it's just being used to forward events); this is expected behaviour

Finally, to run tests locally for automated testing, kill any currently running sloth debug instance, and instead launch a test instance:

sloth test

Now, when you run your tests, you won't have to launch a debug session from your IDE; instead everything will just run immediately.

When ready to deploy your code run:

sloth deploy

Or, if you choose the GitHub workflow integration, enable the actions in your target repository.

Finally, if you choose TypeScript and also provided a tracking plan to develop against, you can update the definition against the latest tracking plan by running

sloth sync_tp

Detailed Usage Guide

TODO

Not yet implemented

  • Function parameters