@palantir/compute-module
v0.2.6
Published
Typescript binding for implementing the compute module API
Downloads
276
Readme
@palantir/compute-module
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 .