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

krpc-node

v4.0.2

Published

Node client lib for krpc

Downloads

36

Readme

krpc-node

A node.js client library for krpc. Allows you to send commands to Kerbal Space Program from node. Browser support coming soon!

JavaScript to space via krpc!

NPM

Build status

KSP

Image by Rareden, for more, visit this forum link

Table of Contents

Example code

For more details on how to use the client, please see the above link, a quick getting started example is below:

'use strict';
let { createClient, spaceCenter } = require('krpc-node');

describe('Getting Started - async', function() {
    it('Should work when using object hierarchy', async function() {
        this.timeout(10000);
        const client = await createClient();
        try {
            let vessel = await client.send(spaceCenter.getActiveVessel());
            //Alternative syntax:
            //let vessel = await client.services.spaceCenter._getActiveVessel();
            let control = await vessel.control.get();
            let orbitalReference = await vessel.orbitalReferenceFrame.get();
            let flight = await vessel.flight(orbitalReference);
            let throttle = await control.throttle.get();
            let heading = await flight.heading.get();
            console.log({
                callType: 'Manual',
                throttle,
                heading
            });
            //Or send them in a batch (Method 1)
            let getThrottleCall = spaceCenter.controlGetThrottle(control.id);
            let getHeadingCall = spaceCenter.flightGetHeading(flight.id);
            let response = await client.send([getThrottleCall, getHeadingCall]);
            console.log({
                callType: 'Method 1',
                throttle: response.results[0].value,
                heading: response.results[1].value
            });
            //Or send them in a batch (Method 2)
            const returnFunctionOptions = { _fn: true };
            let getThrottleCall2 = await control.throttle.get(returnFunctionOptions);
            let getHeadingCall2 = await flight.heading.get(returnFunctionOptions);
            let response2 = await client.send([getThrottleCall2, getHeadingCall2]);
            console.log({
                callType: 'Method 2',
                throttle: response2.results[0].value,
                heading: response2.results[1].value
            });
        } catch (err) {
            await client.close();
            throw err;
        }
        await client.close();
    });

    //Warning! This way of calling the client will be deprecated in the next version:
    it('Should work using old school calls', async function() {
        this.timeout(10000);
        const client = await createClient({ legacy: true });
        try {
            let response = await client.send(spaceCenter.getActiveVessel());
            let vesselId = response.results[0].value;
            response = await client.send(spaceCenter.vesselGetControl(vesselId));
            let controlId = response.results[0].value;
            response = await client.send(spaceCenter.vesselGetOrbitalReferenceFrame(vesselId));
            let orbitalReference = response.results[0].value;
            response = await client.send(spaceCenter.vesselFlight(vesselId, orbitalReference));
            let flightId = response.results[0].value;
            let getThrottleCall = spaceCenter.controlGetThrottle(controlId);
            let getHeadingCall = spaceCenter.flightGetHeading(flightId);
            response = await client.send([getThrottleCall, getHeadingCall]);
            console.log({
                throttle: response.results[0].value,
                heading: response.results[1].value
            });
        } catch (err) {
            await client.close();
            throw err;
        }
        await client.close();
    });
});

You can also setup a stream that will update values on a ticker:

let { createClient, spaceCenter } = require('krp-node');
let _ = require('lodash');

describe('Stream throttle - async', function() {
    it('Should work', async function() {
        this.timeout(10000);
        const client = await createClient();
        let vessel = await client.send(spaceCenter.getActiveVessel());
        let control = await vessel.control.get();
        let orbitalReference = await vessel.orbitalReferenceFrame.get();
        let flight = await vessel.flight(orbitalReference);
        await client.connectToStreamServer();
        const returnFunctionOptions = { _fn: true };
        let getThrottleCall = await control.throttle.get(returnFunctionOptions);
        let getHeadingCall = await flight.heading.get(returnFunctionOptions);
        await client.addStream(getThrottleCall, 'Throttle');
        await client.addStream(getHeadingCall, 'Heading');
        try {
            await new Promise((resolve, reject) => {
                let counter = 0;
                let done = false;
                client.stream.on('message', streamState => {
                    if (done) {
                        return;
                    }
                    console.log(streamState);
                    counter++;
                    if (counter === 20) {
                        done = true;
                        resolve();
                    }
                });
            });
        } catch (err) {
            await client.close();
            throw err;
        }
        await client.close();
    });
});

More Examples

Checkout the examples repository for some practical examples. Alternatively the tests in this code base can also be a good source of information.

JSinSA

To learn the history of the library and see me and it in action you can also check out the video below from the JSinSA 2017 conference. For more info on the JSinSA conference, check out this link

JSinSA

Legacy

To make sure your old code works with version 4.x of the library, simply create the client as follows:

createClient({ legacy: true }, clientCreated);
function clientCreated(err, client) {
    // Z0mg Code!
}

Please note, this will be deprecated in version 5+

ToDo