Skip to content

pzmarzly/varlink-js

Repository files navigation

Varlink protocol implemented in JS/TS/NodeJS

  • ✅ client and server code
  • ✅ no external dependencies
  • ✅ passes official certification tests
  • ✅ type-safe
  • ✅ async-first
  • ✅ connection pooling
  • ✅ compatible with Node.js and Bun.sh
  • ✅ supports TCP and Unix sockets
  • ❌ no input validation
  • ❌ no parsing and generating of schema IDL
  • ❌ writing servers is a bit clunky (you have to implement org.varlink.service by hand)
  • ❌ no socket activation
  • ❌ no proxy mode or HTTP mode (but should be easy to add)

Usage

Installation:

npm install varlink-js

Client:

import { VarlinkClient } from "varlink-js/client/client";
import { SocketClientSideTransport } from "varlink-js/transport/node-socket";

const transport = new SocketClientSideTransport({
  host: "127.0.0.1",
  port: 12345,
  timeout: 10_000,
});
const client = new VarlinkClient(transport);

// Call any method by name (no type safety)
import { VarlinkDynamicMethod } from "varlink-js/protocol/protocol";
const method = new VarlinkDynamicMethod("org.varlink.service.GetInterfaceDescription");
const response1 = await client.call(method, { interface: "org.varlink.service" });
console.log(response1.description); // any

// Or use type-safe wrappers
// (Currently NOT autogenerated)
import { OrgVarlinkService } from "varlink-js/schemas/org.varlink.service.varlink";
const response2 = await client.call(OrgVarlinkService.GetInterfaceDescription, { interface: "org.varlink.service" });
console.log(response2.description); // string

Server:

import { VarlinkServer } from "varlink-js/server/server";
import { SocketServerSideTransport } from "varlink-js/transport/node-socket";
import { OrgVarlinkService } from "varlink-js/schemas/org.varlink.service.varlink";

const transport = new SocketServerSideTransport({
  timeout: 10_000,
  port: 12345,
});
const server = new VarlinkServer(transport);
server.registerCall(OrgVarlinkService.GetInfo, async (_input) => ({
  vendor: "varlink-js",
  product: "varlink-js",
  version: "0.0.1",
  url: "https://example.com",
  interfaces: ["org.varlink.service"],
}));
await server.start();
console.log(transport.address());

See client.test.ts and reference-server.ts for full usage.

Certification

# JS client, JS server
bun run test

# JS client, Python server
python3 -m varlink.tests.test_certification --varlink=tcp:127.0.0.1:12345
PORT=12345 bun run test

# JS server, Python client
PORT=12345 bun run src/server/run-reference-server.ts
python3 -m varlink.tests.test_certification --client --varlink=tcp:127.0.0.1:12345

License

MIT

About

Varlink client and server for Node.js and Bun.sh

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published