@verft/sdk 0.0.0-stage → 1.0.0

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,168 @@
1
+ import { decode } from "./encoding.js";
2
+ import { encodeArguments } from "./functions.js";
3
+ import { Watched, thrownLater } from "./readable.js";
4
+ export function wire(target, args) {
5
+ return "sql" in target
6
+ ? { tag: "ClientQuery", value: { sql: target.sql, parameters: target.parameters } }
7
+ : { tag: "Query", value: { name: target.name, arguments: encodeArguments(target, args) } };
8
+ }
9
+ export function keyOfSubscription(to) {
10
+ return to.tag === "Query" ? JSON.stringify([to.tag, to.value.name, keyOf(to.value.arguments)]) : JSON.stringify([to.tag, to.value.sql, keyOf(to.value.parameters)]);
11
+ }
12
+ export function revisions(optimistic) {
13
+ const recorded = [];
14
+ const update = (target, args, edit) => {
15
+ recorded.push({ key: keyOfSubscription(wire(target, args)), edit: ("sql" in target ? args : edit) });
16
+ };
17
+ optimistic({ update });
18
+ return recorded;
19
+ }
20
+ export const pending = { state: "pending" };
21
+ export class Standing extends Watched {
22
+ constructor(to, copy, unsubscribe, edits) {
23
+ super(pending);
24
+ this.confirmed = pending;
25
+ this.waiting = undefined;
26
+ this.due = 0;
27
+ this.sent = false;
28
+ this.delivered = false;
29
+ this.to = to;
30
+ this.key = keyOfSubscription(to);
31
+ this.copy = copy;
32
+ this.apply = copy();
33
+ this.unsubscribe = unsubscribe;
34
+ this.edits = edits;
35
+ }
36
+ deliver(delivered) {
37
+ this.delivered = true;
38
+ if (this.confirmed.state === "undecodable" && delivered.tag === "Rows")
39
+ return;
40
+ try {
41
+ this.confirm(this.apply(delivered));
42
+ }
43
+ catch (error) {
44
+ this.confirm({ state: "undecodable", error });
45
+ }
46
+ }
47
+ endsWith(delivered) {
48
+ return delivered.tag === "Failed" && (this.to.tag === "ClientQuery" || delivered.value.tag === "NotFound");
49
+ }
50
+ awaitFirst(due, deadline) {
51
+ this.stopWaiting();
52
+ this.due = due;
53
+ this.waiting = deadline(due);
54
+ }
55
+ unanswered() {
56
+ if (this.confirmed.state !== "pending")
57
+ return;
58
+ this.confirm({ state: "failed", failure: { tag: "Indeterminate" } });
59
+ this.notify();
60
+ }
61
+ stopWaiting() {
62
+ this.waiting?.();
63
+ this.waiting = undefined;
64
+ }
65
+ refuse(reason) {
66
+ this.confirm({ state: "refused", reason });
67
+ }
68
+ reset() {
69
+ this.apply = this.copy();
70
+ this.sent = false;
71
+ this.delivered = false;
72
+ this.confirm(pending);
73
+ }
74
+ rebase() {
75
+ let shown = this.confirmed;
76
+ for (const revisions of this.edits.values()) {
77
+ for (const { key, edit } of revisions) {
78
+ if (key !== this.key || shown.state !== "ready")
79
+ continue;
80
+ try {
81
+ shown = { state: "ready", value: edit(shown.value) };
82
+ }
83
+ catch (error) {
84
+ thrownLater(error);
85
+ }
86
+ }
87
+ }
88
+ this.set(shown);
89
+ }
90
+ confirm(result) {
91
+ this.stopWaiting();
92
+ this.confirmed = result;
93
+ this.rebase();
94
+ }
95
+ }
96
+ export function returned(fn, value) {
97
+ const decoded = decode(fn.returns, value);
98
+ return (fn.error ? { ok: true, value: decoded } : decoded);
99
+ }
100
+ export function moduleError(fn, failure) {
101
+ if (failure.tag !== "ModuleError" || !fn.error)
102
+ return undefined;
103
+ return { ok: false, error: decode(fn.error, failure.value) };
104
+ }
105
+ function unexpected(delivered) {
106
+ throw new TypeError(`the server delivered ${delivered.tag} for a Subscription of the other kind`);
107
+ }
108
+ export function replies(query) {
109
+ return () => (delivered) => {
110
+ switch (delivered.tag) {
111
+ case "Reply":
112
+ return { state: "ready", value: returned(query, delivered.value) };
113
+ case "Failed": {
114
+ const error = moduleError(query, delivered.value);
115
+ return error === undefined ? { state: "failed", failure: delivered.value } : { state: "ready", value: error };
116
+ }
117
+ default:
118
+ return unexpected(delivered);
119
+ }
120
+ };
121
+ }
122
+ function keyOf(bytes) {
123
+ let key = "";
124
+ for (const byte of bytes)
125
+ key += String.fromCharCode(byte);
126
+ return key;
127
+ }
128
+ export function rows(row) {
129
+ return () => {
130
+ const held = new Map();
131
+ const add = (bytes) => {
132
+ const key = keyOf(bytes);
133
+ const existing = held.get(key);
134
+ if (existing)
135
+ existing.count++;
136
+ else
137
+ held.set(key, { row: decode(row, bytes), count: 1 });
138
+ };
139
+ const remove = (bytes) => {
140
+ const key = keyOf(bytes);
141
+ const existing = held.get(key);
142
+ if (!existing)
143
+ throw new TypeError("the server removed a row the local copy does not hold");
144
+ if (--existing.count === 0)
145
+ held.delete(key);
146
+ };
147
+ const ready = () => ({
148
+ state: "ready",
149
+ value: [...held.values()].flatMap(({ row, count }) => Array(count).fill(row)),
150
+ });
151
+ return (delivered) => {
152
+ switch (delivered.tag) {
153
+ case "Snapshot":
154
+ held.clear();
155
+ delivered.value.forEach(add);
156
+ return ready();
157
+ case "Rows":
158
+ delivered.value.removed.forEach(remove);
159
+ delivered.value.added.forEach(add);
160
+ return ready();
161
+ case "Failed":
162
+ return { state: "failed", failure: delivered.value };
163
+ default:
164
+ return unexpected(delivered);
165
+ }
166
+ };
167
+ };
168
+ }
@@ -0,0 +1,23 @@
1
+ import { type AccountActivityLog } from "./activity.js";
2
+ import type { CallOptions, Connection, Status } from "./connection.js";
3
+ import type { Action, Query, Reply } from "./functions.js";
4
+ import type { Landing } from "./landing.js";
5
+ import type { Caller } from "./protocol.js";
6
+ import type { Local, Result } from "./subscriptions.js";
7
+ import type { ClientQuery } from "./tables.js";
8
+ export interface Current<T> {
9
+ readonly current: T;
10
+ }
11
+ export declare function setConnection(connection: Connection): void;
12
+ export declare function useConnection(): Connection;
13
+ type NotAFunction = {
14
+ readonly [Symbol.hasInstance]?: never;
15
+ };
16
+ export declare function useSubscription<A, R, E>(query: Query<A, R, E>, args: (NoInfer<A> & NotAFunction) | (() => NoInfer<A>)): Current<Result<Reply<R, E>>>;
17
+ export declare function useSubscription<R>(query: ClientQuery<R>): Current<Result<R[]>>;
18
+ export declare function useStatus(): Current<Status>;
19
+ export declare function useCaller(): Current<Caller | null>;
20
+ export declare function useLanding(): Current<Landing>;
21
+ export declare function useAction<A, R, E>(action: Action<A, R, E>): (args: NoInfer<A>, optimistic?: (local: Local) => void, options?: CallOptions) => Promise<Reply<R, E>>;
22
+ export declare function useAccountActivity(limit?: number): Current<AccountActivityLog>;
23
+ export {};
package/dist/svelte.js ADDED
@@ -0,0 +1,57 @@
1
+ import { createContext } from "svelte";
2
+ import { createSubscriber } from "svelte/reactivity";
3
+ import { accountActivityLog } from "./activity.js";
4
+ const [current, provide, provided] = createContext();
5
+ export function setConnection(connection) {
6
+ provide(connection);
7
+ }
8
+ export function useConnection() {
9
+ if (!provided())
10
+ throw new Error("a Verft function was used in a component without setConnection above it");
11
+ return current();
12
+ }
13
+ function following(latest) {
14
+ let followed;
15
+ let subscribe = () => { };
16
+ return {
17
+ get current() {
18
+ const readable = latest();
19
+ if (readable !== followed) {
20
+ followed = readable;
21
+ subscribe = createSubscriber(readable.onChange);
22
+ }
23
+ subscribe();
24
+ return readable.get();
25
+ },
26
+ };
27
+ }
28
+ export function useSubscription(target, args) {
29
+ const connection = useConnection();
30
+ let shared;
31
+ return following(() => {
32
+ const latest = connection.shared(target, typeof args === "function" ? args() : args);
33
+ if (latest.key !== shared?.key)
34
+ shared = latest;
35
+ return shared;
36
+ });
37
+ }
38
+ export function useStatus() {
39
+ const connection = useConnection();
40
+ return following(() => connection.status);
41
+ }
42
+ export function useCaller() {
43
+ const connection = useConnection();
44
+ return following(() => connection.caller);
45
+ }
46
+ export function useLanding() {
47
+ const connection = useConnection();
48
+ return following(() => connection.landing);
49
+ }
50
+ export function useAction(action) {
51
+ const connection = useConnection();
52
+ return (args, optimistic, options) => connection.action(action, args, optimistic, options);
53
+ }
54
+ export function useAccountActivity(limit) {
55
+ const log = accountActivityLog(useConnection(), limit);
56
+ return following(() => log);
57
+ }
@@ -0,0 +1,17 @@
1
+ import { type Codec, type Fields } from "./encoding.js";
2
+ export interface Table<R, P> {
3
+ readonly name: string;
4
+ readonly row: Codec<R>;
5
+ readonly columns: Fields<P>;
6
+ }
7
+ export declare function table<R, P>(name: string, row: Codec<R>, columns: Fields<P>): Table<R, P>;
8
+ export interface Parameter {
9
+ readonly encoded: Uint8Array;
10
+ }
11
+ export declare function bind<T>(column: Codec<T>, value: NoInfer<T>): Parameter;
12
+ export interface ClientQuery<R> {
13
+ readonly sql: string;
14
+ readonly parameters: Uint8Array;
15
+ readonly row: Codec<R>;
16
+ }
17
+ export declare function select<R, P>(returns: Table<R, P>): (text: TemplateStringsArray, ...parameters: Parameter[]) => ClientQuery<R>;
package/dist/tables.js ADDED
@@ -0,0 +1,18 @@
1
+ import { Writer, encode } from "./encoding.js";
2
+ export function table(name, row, columns) {
3
+ return { name, row, columns };
4
+ }
5
+ export function bind(column, value) {
6
+ return { encoded: encode(column, value) };
7
+ }
8
+ export function select(returns) {
9
+ const name = `"${returns.name.split('"').join('""')}"`;
10
+ return (text, ...parameters) => {
11
+ const rest = text.reduce((sql, piece, index) => `${sql}$${index}${piece}`);
12
+ const writer = new Writer();
13
+ writer.u32(parameters.length);
14
+ for (const parameter of parameters)
15
+ writer.counted(parameter.encoded);
16
+ return { sql: `SELECT ${name}.* FROM ${name} ${rest}`.trimEnd(), parameters: writer.finish(), row: returns.row };
17
+ };
18
+ }
package/dist/vue.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ import { type MaybeRefOrGetter, type Plugin, type Ref } from "vue";
2
+ import { type AccountActivityLog } from "./activity.js";
3
+ import type { CallOptions, Connection, Status } from "./connection.js";
4
+ import type { Action, Query, Reply } from "./functions.js";
5
+ import type { Landing } from "./landing.js";
6
+ import type { Caller } from "./protocol.js";
7
+ import type { Local, Result } from "./subscriptions.js";
8
+ import type { ClientQuery } from "./tables.js";
9
+ export declare const verft: Plugin<[Connection]>;
10
+ export declare function useConnection(): Connection;
11
+ export declare function useSubscription<A, R, E>(query: Query<A, R, E>, args: MaybeRefOrGetter<NoInfer<A>>): Readonly<Ref<Result<Reply<R, E>>>>;
12
+ export declare function useSubscription<R>(query: ClientQuery<R>): Readonly<Ref<Result<R[]>>>;
13
+ export declare function useStatus(): Readonly<Ref<Status>>;
14
+ export declare function useCaller(): Readonly<Ref<Caller | null>>;
15
+ export declare function useLanding(): Readonly<Ref<Landing>>;
16
+ export declare function useAction<A, R, E>(action: Action<A, R, E>): (args: NoInfer<A>, optimistic?: (local: Local) => void, options?: CallOptions) => Promise<Reply<R, E>>;
17
+ export declare function useAccountActivity(limit?: number): Readonly<Ref<AccountActivityLog>>;
package/dist/vue.js ADDED
@@ -0,0 +1,55 @@
1
+ import { computed, inject, onMounted, shallowRef, toValue, watch } from "vue";
2
+ import { accountActivityLog } from "./activity.js";
3
+ const current = Symbol("verft");
4
+ export const verft = {
5
+ install(app, connection) {
6
+ app.provide(current, connection);
7
+ },
8
+ };
9
+ export function useConnection() {
10
+ const connection = inject(current, null);
11
+ if (connection === null)
12
+ throw new Error("a Verft composable was used in an app without the verft plugin");
13
+ return connection;
14
+ }
15
+ function useReadable(readable) {
16
+ const value = shallowRef(readable().get());
17
+ onMounted(() => {
18
+ watch(readable, (followed, _, onCleanup) => {
19
+ const update = () => {
20
+ value.value = followed.get();
21
+ };
22
+ onCleanup(followed.onChange(update));
23
+ update();
24
+ }, { immediate: true });
25
+ });
26
+ return value;
27
+ }
28
+ export function useSubscription(target, args) {
29
+ const connection = useConnection();
30
+ const shared = computed((previous) => {
31
+ const latest = connection.shared(target, toValue(args));
32
+ return latest.key === previous?.key ? previous : latest;
33
+ });
34
+ return useReadable(() => shared.value);
35
+ }
36
+ export function useStatus() {
37
+ const connection = useConnection();
38
+ return useReadable(() => connection.status);
39
+ }
40
+ export function useCaller() {
41
+ const connection = useConnection();
42
+ return useReadable(() => connection.caller);
43
+ }
44
+ export function useLanding() {
45
+ const connection = useConnection();
46
+ return useReadable(() => connection.landing);
47
+ }
48
+ export function useAction(action) {
49
+ const connection = useConnection();
50
+ return (args, optimistic, options) => connection.action(action, args, optimistic, options);
51
+ }
52
+ export function useAccountActivity(limit) {
53
+ const log = accountActivityLog(useConnection(), limit);
54
+ return useReadable(() => log);
55
+ }
package/package.json CHANGED
@@ -1,6 +1,74 @@
1
1
  {
2
2
  "name": "@verft/sdk",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "description": "The client of Verft Apps: typed Actions, Queries and live Subscriptions, with React, React Native, Vue and Svelte bindings",
5
+ "homepage": "https://docs.verft.cloud",
6
+ "license": "SEE LICENSE IN LICENSE",
7
+ "type": "module",
8
+ "main": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ },
15
+ "./react": {
16
+ "types": "./dist/react.d.ts",
17
+ "default": "./dist/react.js"
18
+ },
19
+ "./react-native": {
20
+ "types": "./dist/react-native.d.ts",
21
+ "default": "./dist/react-native.js"
22
+ },
23
+ "./vue": {
24
+ "types": "./dist/vue.d.ts",
25
+ "default": "./dist/vue.js"
26
+ },
27
+ "./svelte": {
28
+ "types": "./dist/svelte.d.ts",
29
+ "default": "./dist/svelte.js"
30
+ }
31
+ },
32
+ "files": [
33
+ "dist",
34
+ "LICENSE",
35
+ "README.md"
36
+ ],
37
+ "sideEffects": false,
38
+ "publishConfig": {
39
+ "access": "public",
40
+ "registry": "https://registry.npmjs.org/"
41
+ },
42
+ "scripts": {
43
+ "build": "tsc -p .",
44
+ "prepack": "tsc -p ."
45
+ },
46
+ "peerDependencies": {
47
+ "react": ">=18",
48
+ "svelte": ">=5.40",
49
+ "vue": ">=3.5"
50
+ },
51
+ "peerDependenciesMeta": {
52
+ "react": {
53
+ "optional": true
54
+ },
55
+ "svelte": {
56
+ "optional": true
57
+ },
58
+ "vue": {
59
+ "optional": true
60
+ }
61
+ },
62
+ "devDependencies": {
63
+ "@types/node": "26.6.6",
64
+ "@types/react": "19.3.0",
65
+ "@types/react-dom": "19.3.0",
66
+ "happy-dom": "21.0.4",
67
+ "react": "19.3.1",
68
+ "react-dom": "19.3.1",
69
+ "react-test-renderer": "19.3.1",
70
+ "svelte": "5.57.3",
71
+ "typescript": "7.0.2",
72
+ "vue": "3.5.45"
73
+ }
74
+ }