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

@palantir/compute-module

v0.2.6

Published

Typescript binding for implementing the compute module API

Downloads

276

Readme

@palantir/compute-module

npm version

Node.JS compatible implementation of the Palantir Compute Module specification.

Functions Mode

Basic usage

This library can be used untyped with vanilla JavaScript to generate registerable functions in "Functions" execution mode.

import { ComputeModule } from "@palantir/compute-module";

new ComputeModule()
  .register("addOne", async ({ value }) => ({ value: n + 1 }));
  .register("stringify", async ({ n }) => "" + n)
  .default(() => ({ error: "Unsupported query name" }));

Streaming usage

You can stream responses back from the compute module, rather than all at once. Type safety is not provided on the response here as the SDK cannot validate that the stream was of the correct type, we recommend that you only set your return value to String in these cases.

import { ComputeModule } from "@palantir/compute-module";

new ComputeModule()
  .registerStreaming("hello", async ({ world }, writeable: Writeable) => {
    writeable.write("Hello");
    writeable.write(world);
    writeable.end();
  });
  .default(() => ({ error: "Unsupported query name" }));

Schema registration

Definitions can be generated using typebox allowing the Compute Module to register functions at runtime, while maintaining typesafety at compile time.

import { ComputeModule } from "@palantir/compute-module";
import { Type } from "@sinclair/typebox";

const myModule = new ComputeModule({
  logger: console,
  definitions: {
    addOne: {
      input: Type.Object({
        value: Type.Number(),
      }),
      output: Type.Object({ value: Type.Number() }),
    },
  },
});

myModule.register("addOne", async ({ value }) => ({ value: n + 1 }));

Pipelines Mode

Retrieving aliases

Compute Modules can interact with resources in their execution environment, within Palantir Foundry these are defined as inputs and outputs on the Compute Module spec. Resource identifiers can be unique to the execution environment, so using aliases allows your code to maintain a static reference to known resources. To receive the identifier for an aliases resource, use the getResource method.

const resourceId = myModule.getResource("myResourceAlias");
const result = await someDataFetcherForId(resourceId);

General usage

The following features are available in both Pipelines and Functions mode in order to interact with Palantir Foundry:

Retrieving source credentials

Sources can be used to store secrets for use within a Compute Module, they prevent you from having to put secrets in your container or in plaintext in the job specification. Retrieving a source credential using this library is simple:

const myCredential = myModule.getCredential("MySourceApiName", "MyCredential");

Sources can be validated on startup by declaring them in the compute module options:

const myModule = new ComputeModule({
  // Will throw if MyApi with credential MyCredential has not been mounted
  sources: {
    MyApi: {
      credentials: ["MyCredential"]
    },
    // You can validate the source, without validating the credential
    AnotherApi: {}
  }
});

// ❌ Will throw a type error
myModule.getCredential("YourApi", "YourCredential");
myModule.getCredential("YourApi", "MyCredential");
myModule.getCredential("MyApi", "YourCredential");

// ✅ Passes type checking
myModule.getCredential("MyApi", "MyCredential");

// ✅ As there are no known credentials, any string can be passed to this source
myModule.getCredential("AnotherApi", "AnyString");

If not provided, getCredential will do no type validation compile-time and the instance will not validate at run-time.

Retrieving environment details

At runtime, you can retrieve details about the execution environment, which is useful for authenticating around services available:

const token =
  myModule.environment.type === "pipelines" ? myModule.environment.buildToken : undefined;

Retrieving Foundry services

At runtime, you can retrieve the api paths for known Foundry services, this allows you to call those endpoints without using a source to ingress back into the platform:

import { FoundryService } from "@palantir/compute-module";

const streamProxyApi = myModule.getServiceApi(FoundryService.STREAM_PROXY);

Developing the SDK

Building the example module

Run docker build from the top-level directory (not example-module):

docker build -f example-module/Dockerfile -t my-container-registry.palantirfoundry.com/example-module:0.0.1 .