@xeonr/functions
v0.4.1
Published
Client SDK and CLI for Xeonr Functions — create, configure, deploy, promote and inspect functions over the public API.
Readme
@xeonr/functions
Client SDK and CLI for Xeonr Functions — create, configure, deploy, promote and inspect functions programmatically.
Push Node.js, Go or static source, get https://<name>.fns.xnr.app, and have it
start on the first request and idle back to zero. Console:
functions.xeonr.io.
import { createClient } from "@xeonr/functions";
const fns = createClient({
endpoint: "https://functions.xeonr.io",
token: process.env.XEONR_FN_TOKEN, // xk_… (or a legacy fnt_…)
});
// An API key carries its own team, so `ownerTeam` is only needed for a signed-in
// caller: `createClient({ ownerTeam: "urn:xeonr:auth:team:…" })`, or `forTeam()`.
const fn = await fns.functions.create({ displayName: "My API", kind: "node" });
const deploy = await fn.deploy("./"); // archives, uploads, waits, activates
deploy.check(); // throws if the build failed
console.log(fn.url); // https://brave-otter.fns.xnr.appThe SDK speaks the public API, xeonr.functions.v2 (FunctionsService,
VersionsService, BuildLogsService, StatsService, FilesService,
RepositoriesService, ConnectionsService, ApiKeysService, ActivityService), over
Connect.
Same thing from a shell:
XEONR_FN_TOKEN=xk_… npx xeonr-fn ship "My API" ./ --kind nodeWhat changed in 0.4.1
Additive. branchd (branchd.run) is a git provider: fns.git.connectBranchd()
connects a team, Provider gains "branchd" and AuthModeName gains
"branchd_team", and fn.selectRepository({ connectionId, repo }) moves a
function onto another of its team's connections (from GitLab to branchd).
What changed in 0.4.0
0.4.0 moves to the xeonr.functions.v2 API, and it is a breaking release; the
server no longer serves xeonr.functions.v1, so 0.3.x stops working against it.
| Was (0.3) | Is (0.4) |
|---|---|
| fns.raw.function, fns.raw.team | fns.raw.functions, .versions, .buildLogs, .stats, .files, .repositories, .connections, .apiKeys, .activity — one generated client per v2 service |
| fns.teams.createToken() → { token, secret } | fns.teams.createApiKey() → { apiKey, secret } (CreateApiKey) |
| fns.teams.tokens() / deleteToken({ tokenId }) | fns.teams.apiKeys() / revokeApiKey({ apiKeyId }) |
| Role "owner" \| "editor" \| "viewer", TeamRole | Role "editor" \| "viewer", ApiKeyRole |
| fn.promote() / fn.rollback() → the function | → the version now active (SetActiveVersion returns the version) |
| listPage({ pageToken }) → nextPageToken | listPage({ cursor }) → nextCursor |
| fns.config(), ServerConfig, the /config.json fetch | gone: fn.url, or await fns.url(nameOrId), reads Function.url |
| fn.stats() → StatPoint[] | → StatBucket[]: start a Date, counters numbers |
| *Unix int64 times (createdAtUnix, …) | google.protobuf.Timestamp (createdAt, …); the SDK's own views take and return Date, and toDate() converts a record's |
| ConnectionView.installationId a number | a string |
| fn.update({}) a no-op, env: {} ignored | update({}) refused; env: {} clears every variable |
| AuditEvent, TeamToken, FunctionService, TeamService | ActivityEvent, ApiKey, and the nine v2 services |
Also new: functions.create({ source }) (a repository at creation;
fn.sourceError when it could not be applied), fn.url, fn.createdAt /
updatedAt, updateFunctionRequest(), toDate() / toTimestamp(), and the
SourceMode / RepositoryOutcome enums. Fixed: ensureFunction() and ship()
no longer create a duplicate on every run (they searched by display name, which
the server's search never matched).
The one thing to know
A deploy answers before its build finishes. CreateVersion returns as soon
as the source is stored: a container version comes back BUILDING and
activates itself minutes later, when the build succeeds. A client that treats that
response as the outcome will report success for a build that fails a minute
after it stopped watching.
So deploy() waits by default, polling the build log — which is also how you
get the log — and returns a settled result:
const deploy = await fn.deploy("./", { onLog: (t) => process.stdout.write(t) });
deploy.ok // did it build and go live?
deploy.status // "ready" | "failed" | "building" | …
deploy.failureMessage // the api's reason, when it failed
deploy.log // the whole build log
deploy.check() // throw BuildFailedError unless okA failed build is a value, not an exception — the RPC succeeded and told you
the build failed. Transport and permission failures throw; build outcomes return.
Pass { wait: false } to get the BUILDING version straight back and watch it
yourself with fn.versions.ref(id).follow().
{ timeout } runs out into a BuildTimeoutError that says plainly that the
build was not cancelled — it is still running, and will still activate itself.
Install
pnpm add @xeonr/functions @bufbuild/protobuf @connectrpc/connectThe two are peer dependencies so a project holding its own copy of protobuf-es does not end up with two schema registries. Node 20+. The package ships ESM and CommonJS, and works in the browser.
Authenticating
| Caller | How |
|---|---|
| CI, scripts, this CLI | a team API key — createClient({ token: "xk_…" }) |
| a browser, same-origin | the console's session cookie — createBrowserClient() |
Mint a key in the console or, as a signed-in user, with
teams.createApiKey(). Keys are Xeonr Auth API keys: the secret is returned
once and auth stores only a hash. Tokens functions issued itself before keys
moved to auth (fnt_…) still work, and are listed beside xk_… keys with
legacyPrefix: "fnt_".
A key reaches every service a deploy needs (functions, versions, build logs,
stats, files, repositories, connections). ApiKeysService and
ActivityService — everything under fns.teams — require a signed-in user,
deliberately: a key that could mint more keys would make revocation
meaningless.
Teams
Teams are Xeonr Auth teams, shared across the platform — not objects this product owns. Create a team, rename it, delete it, or change who is in it and what they may do at auth.xeonr.io/teams. None of that is reachable from this SDK, by design: there is one place teams live.
Here, a team is only ever a reference, and its value is a URN:
const scoped = fns.forTeam("urn:xeonr:auth:team:2f1c9c1e-…");
await scoped.functions.create({ displayName: "My API", kind: "node" });
// or per call
for await (const f of fns.functions.list({ ownerTeam: urn })) …Every wire field and option that names a team is ownerTeam and takes that URN.
CreateFunction from a signed-in user requires one, and so does every call
under fns.teams — bind the client, pass it per call, or (for functions) use
an API key, which carries exactly one team. The retired t-… ids from the old
product-local team model are refused by shape, with a message that says so.
What is still here is the product's own team-scoped surface, under
fns.teams:
const { apiKey, secret } = await fns.teams.createApiKey({ name: "ci", role: "editor" }); // secret shown once
await fns.teams.createApiKey({
name: "release", role: "editor",
description: "tags from the release pipeline", // optional
expiresAt: new Date("2027-01-01"), // optional; default never
});
await fns.teams.apiKeys(); // live keys, every page
await fns.teams.apiKeys({ includeRevoked: true }); // and revoked ones
await fns.teams.revokeApiKey({ apiKeyId: apiKey.apiKeyId });
for await (const e of fns.teams.activity()) … // the audit feed, newest firstWhat you can do
// find things
for await (const f of fns.functions.list({ search: "otter" })) … // every page; search is on the assigned name
const { total } = await fns.functions.listPage({ pageSize: 1 }); // count every match
const fn = await fns.functions.get("brave-otter"); // id or assigned name
fn.url; // https://brave-otter.fns.xnr.app
// configure — only the fields you pass are touched (they are the update mask)
await fn.update({
scaling: { minInstances: 0, maxInstances: 10, concurrency: 50 },
build: { packages: ["ffmpeg"], port: 8080 },
env: { LOG_LEVEL: "debug" }, // replaced wholesale; {} clears
secrets: { API_KEY: "…" }, // merged; never readable again
});
// deploy, from wherever the source is
await fn.deploy("./my-app"); // a directory (Node)
await fn.deploy(new URL(artifactUrl)); // a zip by URL (anywhere)
await fn.deploy({ "index.js": "…" }); // a file map (anywhere)
await fn.deploy(zipBytes); // a ready zip
await fn.deploy(); // the workspace as it stands
await fn.deployFromGit({ ref: "v1.2.3" }); // the connected repo
await (await fn.versions.get(id)).redeploy(); // a version's snapshot again
// traffic (SetActiveVersion): both return the version now active
await fn.promote(versionId); // deploy AND rollback: it is a pointer flip
await fn.rollback(); // the previous build
// history and output
for await (const v of fn.versions.list()) …
await fn.versions.ref(id).buildLog();
for await (const chunk of fn.versions.ref(id).follow()) …
// files — the workspace, or a version's frozen snapshot
await fn.files.write("server.js", source);
await fn.files.readText("package.json");
await fn.files.at(versionId).list("/", { recursive: true });
// traffic stats, API keys, activity, git connections
await fn.stats({ rangeSec: 3600 }); // buckets with a Date `start`
await fns.teams.apiKeys();
for await (const e of fns.teams.activity()) …
await fns.git.connections();Anything the SDK does not model is one property away: fns.raw.functions,
.versions, .buildLogs, .stats, .files, .repositories, .connections,
.apiKeys and .activity are the generated Connect clients of the v2
services, throwing the same error taxonomy. The schema itself is
@xeonr/functions/proto. Records read from them carry times as
google.protobuf.Timestamp; toDate(record.createdAt) reads one as a Date.
What the SDK owns, so you don't
| Thing | Why it is here |
|---|---|
| Waiting for a build | the deploy response is not the outcome — see above |
| Following a build log | GetBuildLog offset polling, resumed by nextOffsetBytes, ending on complete |
| Archiving a source tree | the api reads the zip as a filesystem, so paths must be at the root — zip -r app.zip my-app produces a tree the buildpacks cannot detect |
| Downloading a remote zip | the api never fetches on your behalf, so a URL is fetched here, size-capped while streaming, and checked for being a zip before it is uploaded |
| Excluding node_modules | the cap is 64 MiB and the buildpack installs from your lockfile; sending dependencies is never right |
| JSON on the wire | the api accepts the Connect protocol in binary or JSON; the SDK pins JSON in both runtimes (connect-node would default to binary), because it is what the public API documents and passes through any proxy |
| Enums as words | "node", "ready", "editor" — not KIND_NODE, and never a lowercased enum name, which is how the console once matched nothing |
| fn-… vs a name | get() takes either, because the name is what a human has and the id is what a script stored |
| Pagination | every list() is an async iterable that follows nextCursor until it is empty (pages are 50 by default, 500 at most), with a stall guard |
| Partial updates | update() names exactly the fields you pass in UpdateFunction's updateMask, so saving scaling cannot clear your git config |
| Times as Date | the wire's Timestamps become Date on the SDK's own surface (fn.createdAt, version.createdAt, stat buckets, connections, a key's expiresAt) |
| unimplemented detection | isUnimplemented(err) — this SDK tracks the published proto, a given deployment may be behind it |
Testing without deploying
import { fakeFunctions } from "@xeonr/functions/testing";
const fake = fakeFunctions();
fake.builds = { outcome: "fail", polls: 2, failureMessage: "no start script" };
const fn = await fake.client.functions.create({ displayName: "x", kind: "node" });
const deploy = await fn.deploy({ "package.json": "{}" }, { pollInterval: 0 });
deploy.ok; // false
deploy.failureMessage; // "no start script"A real client over an in-memory implementation of every v2 service — real
protocol, the same JSON codec, no network. builds scripts what the next build
does, so the slow and unhappy paths are reachable in a unit test. It refuses
what the server refuses: an empty or unknown updateMask path, a
CreateVersion with no source, activating a version that has not built, a page
limit over 500. Lists page at 50, so cursor-following is exercised too.
Recipes
@xeonr/functions/recipes — opinions, separately importable:
import { ship, ensureFunction } from "@xeonr/functions/recipes";
// create if missing, deploy, wait, report the URL — the whole CI story
const { url, deploy } = await ship(fns, "My API", "./", { kind: "node" });
deploy.check();CLI
xeonr-fn list xeonr-fn logs <fn> [version]
xeonr-fn get <fn> xeonr-fn promote <fn> <version>
xeonr-fn create <name> --kind node xeonr-fn rollback <fn>
xeonr-fn deploy <fn> [dir] xeonr-fn delete <fn>
xeonr-fn ship <name> [dir] --kind go xeonr-fn stats <fn>
xeonr-fn versions <fn> xeonr-fn activity
xeonr-fn token <name> --role editorCredentials come from XEONR_FN_TOKEN, never a flag, so they stay out of shell
history and CI logs. XEONR_FN_ENDPOINT and XEONR_FN_TEAM override the
defaults; XEONR_FN_TEAM and --team take a team URN. There is no teams
command — teams are managed at
auth.xeonr.io/teams.
Build output goes to stderr and the answer to stdout, so --json | jq
works while a build is streaming. --json prints the api's own protojson —
"kind": "KIND_NODE", int64 as a string, times as RFC 3339 — not this SDK's
in-memory shape. promote and rollback print the version now active.
Exit codes: 0 fine, 1 a failed build or a refused call, 2 a usage mistake.
Deploying a zip from a URL
await fn.deploy(new URL("https://…/build-1234.zip"));xeonr-fn deploy my-api https://…/build-1234.zipThe download happens here, not on the server. CreateVersion takes bytes and
the api never makes an outbound request on your behalf — a URL it fetched would
be an SSRF surface pointed at its own network. So the SDK retrieves the archive,
caps it at 64 MiB while streaming rather than after, checks it actually starts
like a zip, and uploads the bytes.
That last check is worth the code: an expired presigned link or a login redirect
answers 200 with an HTML page, and without it those bytes travel to the api and
come back as an invalid_argument about the archive — blaming the archive rather
than the link. Instead:
… did not return a zip archive (got 50 bytes starting "<!doctype html><title>Sign in</title>…").
Check the link has not expired.A URL object is the explicit form. A string is a directory path unless it
parses as http(s)://, so a config value like process.env.SOURCE does the
right thing instead of being looked up as a directory named https://….
There is no credential handling — presigned URLs are the norm. If the artifact needs a header, fetch it yourself and pass the bytes:
const zip = new Uint8Array(await fetch(url, { headers }).then((r) => r.arrayBuffer()));
await fn.deploy(zip);Development
pnpm generate # write src/gen from the published proto module
pnpm generate:local # … or from ../../protocol/modules/xeonr-functions
pnpm test # unit suite, no network
pnpm typecheck
pnpm lint
pnpm build # esm + cjs, then verify the built packagemake sdk from the repo root runs all of it (with PROTO_INPUT=… for a local
protocol checkout), and make ci includes it.
The generated client is not committed (api.protos-in-protocol).
src/gen/ is gitignored and written by buf generate with the local
protoc-gen-es, from the public module proto.prod.wtf/xeonr/functions:main
(the xeonr/functions/v2 package only; the private console and edge API is
never part of this package). typecheck, lint, test and build generate
first when src/gen is missing, and CI always generates, with the BSR
credential in ~/.netrc. The published tarball carries the generated code
compiled into dist, so the SDK version still pins one schema revision —
which is what makes fns.raw mean something — and a consumer needs no buf and
no credential.
PROTO_INPUT points any of these at another input, e.g. a protocol checkout
elsewhere: PROTO_INPUT=/path/to/protocol/modules/xeonr-functions pnpm
generate. A tree written by generate:local is kept until you run pnpm
generate again.
Releasing
Bump version in package.json and merge it to main. That is the whole
release. CI publishes to npm and then tags what it published as sdk-v<version>.
The version is never derived from a tag: a tag that sets the version could
publish contents whose own manifest says something else, so package.json is the
source of truth and the tag is the record. "Has the version changed" is answered
by asking the registry rather than by diffing, which stays right across squashed
merges, reverts, re-runs, and a publish that failed halfway.
A server change merged to main runs these jobs too — the publish finds the
version already on the registry and skips, so an SDK release stays independent of
a server release despite sharing a repo.
The tag prefix is sdk-v, not v: this repo also holds the api and the edge,
where a bare v0.2.0 would read as a release of the whole product.
