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

sugo-caller

v6.0.6

Published

Caller component of SUGOS.

Downloads

37

Readme

Build Status npm Version JS Standard

Caller component of SUGOS.

SUGO-Caller works as a client of SUGO-Hub and provides accessors for remote SUGO-Actor .

Table of Contents

Requirements

Installation

$ npm install sugo-caller --save

Usage

Create a caller instance with [SUGO-Hub][sugo_hub_url] url and connect to an [SUGO-Actor][sugo_actor_url] with key.

#!/usr/bin/env node

/**
 * This is an example to use caller to connect remote actor
 */
'use strict'

const sugoCaller = require('sugo-caller')

const TARGET_ACTOR_ID = 'my-actor-01'

async function tryExample () {
  let caller = sugoCaller({
    protocol: 'https',
    hostname: 'my-sugo-hub.example.com'
  })

// Connect to the target actor
  let actor = await caller.connect(TARGET_ACTOR_ID)
  let shell = actor.get('shell') // Get bash interface

  // Trigger ls command on remote actor
  {
    let lsResult = await shell.exec('ls -la /opt/shared')
    console.log(lsResult)
  }

  // Pipe std out
  {
    let out = (chunk) => process.stdout.write(chunk)
    shell.on('stdout', out)
    await shell.spawn('tail -f /var/log/app.log') // Trigger tailing without blocking
    await new Promise((resolve) => setTimeout(() => resolve(), 3000)) // Block for duration
    shell.off('stdout', out)
  }

  // Exec reboot command
  await shell.exec('reboot')
}

tryExample().catch((err) => console.error(err))

For more detail, see API Guide

Tips

Restricting Connecting Modules

Sometime you would like to make sure that the connecting actor has right modules as expected.

You can pass a JSON-Schema to expect option when accessing a modules. If the modules does not conform to the schema, it throws an error.

#!/usr/bin/env node

/**
 * This is an example to restrict a module with json schema
 */
'use strict'

const sugoCaller = require('sugo-caller')

// JSON-Schema for expected spec info
const shellSchemaV2 = {
  type: 'object',
  properties: {
    name: { enum: [ 'shell' ] }, // Should be shell
    version: { pattern: '^2\.' } // Major version must be 2
    /* ... */
  }
}

async function tryRestrictExample () {
  let caller = sugoCaller({ /* ... */ })
  let actor = await caller.connect('my-actor-01')

  let shell
  try {
    shell = actor.get('shell', {
      // Pass a JSON-Schema to validate the module. Throws an error if invalid
      expect: shellSchemaV2
    })
  } catch (err) {
    console.error('Failed to access!!')
  }
  /* ... */
}

tryRestrictExample().catch((err) => console.error(err))

Describing a Module

You can get module spec data via .describe(moduleName) of actor connection.

#!/usr/bin/env node

/**
 * This is an example to describe a module
 */
'use strict'

const sugoCaller = require('sugo-caller')

async function tryDescribeExample () {
  let caller = sugoCaller({ /* ... */ })
  let actor = await caller.connect('my-actor-01')

  {
    let description = actor.describe('shell')
    console.log(description) // -> { name: 'shell', desc: 'Shell interface', ... }
  }

  /* ... */
}

tryDescribeExample().catch((err) => console.error(err))

Use auth

You can pass auth config to SUGO-Hub by setting auth field on the constructor.

#!/usr/bin/env node

/**
 * This is an example to use an auth
 * @see https://github.com/realglobe-Inc/sugo-hub#use-authentication
 */
'use strict'

const sugoCaller = require('sugo-caller')

async function tryAutyExample () {
  let caller = sugoCaller({
    protocol: 'https',
    hostname: 'my-sugo-hub.example.com',
    // Auth for hub
    auth: {
      // The structure of this field depends on `authenticate` logic implemented on SUGO-Hub
      token: 'a!09jkl3A'
    }
  })

// Connect to the target actor
  let actor = await caller.connect('my-actor-01')
  let shell = actor.get('shell') // Get bash interface
  /* ... */
}

tryAutyExample().catch((err) => console.error(err))

License

This software is released under the Apache-2.0 License.

Links