@newhomestar/sdk 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,34 @@
1
+ import { z, ZodTypeAny } from "zod";
2
+ export interface ActionDef<I extends ZodTypeAny, O extends ZodTypeAny> {
3
+ name: string;
4
+ input: I;
5
+ output: O;
6
+ handler: (input: z.infer<I>, ctx: ActionCtx) => Promise<z.infer<O>>;
7
+ }
8
+ export interface ActionCtx {
9
+ jobId: string;
10
+ progress: (percent: number, meta?: unknown) => void;
11
+ }
12
+ export declare function action<I extends ZodTypeAny, O extends ZodTypeAny>(cfg: {
13
+ name?: string;
14
+ input: I;
15
+ output: O;
16
+ handler: (input: z.infer<I>, ctx: ActionCtx) => Promise<z.infer<O>>;
17
+ }): ActionDef<I, O>;
18
+ export interface WorkerDef {
19
+ name: string;
20
+ queue: string;
21
+ actions: Record<string, ActionDef<any, any>>;
22
+ envSpec?: {
23
+ name: string;
24
+ secret: boolean;
25
+ default?: string;
26
+ }[];
27
+ }
28
+ export declare function defineWorker<T extends WorkerDef>(def: T): T;
29
+ /** Enqueue an async action and receive `{ job_id }` */
30
+ export declare function enqueue<P extends object>(actionPath: `${string}.${string}`, payload: P): Promise<{
31
+ job_id: string;
32
+ }>;
33
+ export declare function runWorker(def: WorkerDef): Promise<void>;
34
+ export type { ZodTypeAny as SchemaAny, ZodTypeAny };
package/dist/index.js ADDED
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.action = action;
7
+ exports.defineWorker = defineWorker;
8
+ exports.enqueue = enqueue;
9
+ exports.runWorker = runWorker;
10
+ const dotenv_1 = __importDefault(require("dotenv"));
11
+ const supabase_js_1 = require("@supabase/supabase-js");
12
+ if (!process.env.RUNTIME_SUPABASE_URL) {
13
+ // local dev – read .env.local
14
+ dotenv_1.default.config({ path: ".env.local", override: true });
15
+ }
16
+ function action(cfg) {
17
+ return { name: cfg.name ?? "unnamed", ...cfg };
18
+ }
19
+ function defineWorker(def) {
20
+ return def; // identity – type info retained for CLI
21
+ }
22
+ /*──────────────────────* Client‑side enqueue() *──────────────────*/
23
+ // CLIENT_SUPABASE_… vars exist in gateways / other services
24
+ const CLIENT_SUPABASE_URL = process.env.CLIENT_SUPABASE_PUBLIC_URL;
25
+ const CLIENT_SUPABASE_KEY = process.env.CLIENT_SUPABASE_SERVICE_ROLE_KEY;
26
+ let clientSupabase;
27
+ function getClient() {
28
+ if (!CLIENT_SUPABASE_URL || !CLIENT_SUPABASE_KEY)
29
+ throw new Error("CLIENT_SUPABASE_* env vars not set");
30
+ return (clientSupabase ?? (clientSupabase = (0, supabase_js_1.createClient)(CLIENT_SUPABASE_URL, CLIENT_SUPABASE_KEY)));
31
+ }
32
+ /** Enqueue an async action and receive `{ job_id }` */
33
+ async function enqueue(actionPath, payload) {
34
+ const [pipeline, action] = actionPath.split(".");
35
+ const { data, error } = await getClient().rpc("nova_enqueue", {
36
+ pipeline_name: pipeline,
37
+ action_name: action,
38
+ p_payload: payload,
39
+ });
40
+ if (error)
41
+ throw error;
42
+ return data;
43
+ }
44
+ /*──────────────── Runtime harness (Supabase RPC) ───────────────*/
45
+ const RUNTIME_SUPABASE_URL = process.env.RUNTIME_SUPABASE_URL;
46
+ const RUNTIME_SUPABASE_KEY = process.env.RUNTIME_SUPABASE_SERVICE_ROLE_KEY;
47
+ const runtime = RUNTIME_SUPABASE_URL && RUNTIME_SUPABASE_KEY
48
+ ? (0, supabase_js_1.createClient)(RUNTIME_SUPABASE_URL, RUNTIME_SUPABASE_KEY)
49
+ : undefined;
50
+ async function runWorker(def) {
51
+ if (!runtime)
52
+ throw new Error("RUNTIME_SUPABASE_* env vars not configured");
53
+ console.log(`[nova] worker '${def.name}' polling ${def.queue}`);
54
+ // infinite loop – use pgmq read RPC
55
+ while (true) {
56
+ const { data, error } = await runtime
57
+ .schema("pgmq_public")
58
+ .rpc("read", {
59
+ queue_name: def.queue,
60
+ sleep_seconds: 30,
61
+ n: 1,
62
+ });
63
+ if (error) {
64
+ console.error(error);
65
+ await delay(2000);
66
+ continue;
67
+ }
68
+ if (!data?.length) {
69
+ await delay(1000);
70
+ continue;
71
+ }
72
+ for (const msg of data) {
73
+ const { jobId, action: actName, payload, user_id } = msg.message;
74
+ const act = def.actions[actName];
75
+ if (!act) {
76
+ await nack(msg.msg_id, def.queue);
77
+ continue;
78
+ }
79
+ try {
80
+ const parsedInput = act.input.parse(payload);
81
+ const ctx = {
82
+ jobId,
83
+ progress: (percent, meta) => runtime.from("job_events").insert({ job_id: jobId, percent, meta }),
84
+ };
85
+ const result = await act.handler(parsedInput, ctx);
86
+ act.output.parse(result);
87
+ await runtime.from("jobs").update({ status: "completed", result }).eq("id", jobId);
88
+ await ack(msg.msg_id, def.queue);
89
+ }
90
+ catch (err) {
91
+ console.error(err);
92
+ await runtime.from("jobs").update({ status: "failed", error: String(err) }).eq("id", jobId);
93
+ await nack(msg.msg_id, def.queue);
94
+ }
95
+ }
96
+ }
97
+ }
98
+ async function ack(id, q) {
99
+ await runtime.schema("pgmq_public").rpc("delete", { queue_name: q, message_id: id });
100
+ }
101
+ async function nack(id, q) {
102
+ await runtime.schema("pgmq_public").rpc("nack", { queue_name: q, message_id: id });
103
+ }
104
+ function delay(ms) { return new Promise(r => setTimeout(r, ms)); }
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@newhomestar/sdk",
3
+ "version": "0.2.1",
4
+ "description": "Type-safe SDK for building Nova pipelines (workers & functions)",
5
+ "homepage": "https://github.com/newhomestar/nova-node-sdk#readme",
6
+ "bugs": {
7
+ "url": "https://github.com/newhomestar/nova-node-sdk/issues"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/newhomestar/nova-node-sdk.git"
12
+ },
13
+ "license": "ISC",
14
+ "author": "Christian Gomez",
15
+ "type": "commonjs",
16
+ "main": "dist/index.js",
17
+ "types": "dist/index.d.ts",
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "scripts": {
22
+ "build": "tsc"
23
+ },
24
+ "dependencies": {
25
+ "@supabase/supabase-js": "^2.39.0",
26
+ "dotenv": "^16.4.3",
27
+ "zod": "^3.23.8"
28
+ },
29
+ "devDependencies": {
30
+ "typescript": "^5.4.4",
31
+ "@types/node": "^20.11.17"
32
+ }
33
+ }