@mdbase-dev/sdk 0.1.36 → 0.1.38

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,24 @@
1
+ import { type ClientOptions, type MdbaseClient } from "./client.js";
2
+ export { directBackendOrigin } from "./direct-origin.js";
3
+ export { createDirectGrantStore, type DirectGrantStore, type DirectBrowserGrant } from "./direct-grant-store.js";
4
+ export interface DirectOptions {
5
+ server: string;
6
+ /** Opaque grant returned by pairDirect for this server. */
7
+ token: string;
8
+ app: ClientOptions["app"];
9
+ signal?: AbortSignal;
10
+ }
11
+ /**
12
+ * Redeem an operator-issued, single-use code at the selected backend and return its opaque browser grant.
13
+ *
14
+ * An `unavailable` failure with reason `code_not_sent` means the server was never
15
+ * reached: the code is unused and can be submitted again. Any other failure after
16
+ * the connection opened leaves the outcome unknown.
17
+ */
18
+ export declare function pairDirect({ server, code, signal }: {
19
+ server: string;
20
+ code: string;
21
+ signal?: AbortSignal;
22
+ }): Promise<string>;
23
+ /** Browser-only connection to an explicitly paired HTTPS backend. */
24
+ export declare function connectDirect({ server, token, app, signal }: DirectOptions): Promise<MdbaseClient>;
package/dist/direct.js ADDED
@@ -0,0 +1,159 @@
1
+ import { connect } from "./client.js";
2
+ import { mdbaseError } from "./errors.js";
3
+ import { framedPort, MAX_FRAME } from "./transport/port.js";
4
+ import { directBackendOrigin } from "./direct-origin.js";
5
+ import { activePins, directCredential, pairingGrant } from "./direct-credential.js";
6
+ import { openDirectMessages } from "./direct-messages.js";
7
+ export { directBackendOrigin } from "./direct-origin.js";
8
+ export { createDirectGrantStore } from "./direct-grant-store.js";
9
+ // Reaching the server can wait on the person: a browser may ask for permission to
10
+ // use the local network before it connects. The server's reply cannot.
11
+ const CONNECT_MS = 60_000;
12
+ const REPLY_MS = 10_000;
13
+ function handshakeDeadline(signal, fail) {
14
+ const abort = () => fail(mdbaseError("cancelled", "Direct handshake cancelled"));
15
+ const expire = () => fail(mdbaseError("unavailable", "Direct handshake timed out"));
16
+ let timer = setTimeout(expire, CONNECT_MS);
17
+ signal?.addEventListener("abort", abort, { once: true });
18
+ return {
19
+ /** The transport is open and the first message is on its way. */
20
+ sent() {
21
+ clearTimeout(timer);
22
+ timer = setTimeout(expire, REPLY_MS);
23
+ },
24
+ clear() {
25
+ clearTimeout(timer);
26
+ signal?.removeEventListener("abort", abort);
27
+ },
28
+ };
29
+ }
30
+ /**
31
+ * Redeem an operator-issued, single-use code at the selected backend and return its opaque browser grant.
32
+ *
33
+ * An `unavailable` failure with reason `code_not_sent` means the server was never
34
+ * reached: the code is unused and can be submitted again. Any other failure after
35
+ * the connection opened leaves the outcome unknown.
36
+ */
37
+ export async function pairDirect({ server, code, signal }) {
38
+ server = directBackendOrigin(server);
39
+ if (typeof code !== "string" || code.length !== 8 || !/^\d{8}$/.test(code))
40
+ throw mdbaseError("invalid_request", "Expected an eight-digit pairing code");
41
+ if (signal?.aborted)
42
+ throw mdbaseError("cancelled", "Pairing cancelled");
43
+ return new Promise((resolve, reject) => {
44
+ let sent = false;
45
+ function finish() {
46
+ code = "";
47
+ deadline.clear();
48
+ messages.close();
49
+ }
50
+ function fail(error) {
51
+ finish();
52
+ reject(!sent && error.code === "unavailable" && !error.reason
53
+ ? mdbaseError("unavailable", "Pairing server was not reached", "code_not_sent") : error);
54
+ }
55
+ const messages = openDirectMessages(server, { operation: "pair" }, {
56
+ ready() {
57
+ sent = true;
58
+ deadline.sent();
59
+ try {
60
+ messages.send(code);
61
+ code = "";
62
+ }
63
+ catch {
64
+ fail(mdbaseError("unavailable", "Pairing connection failed"));
65
+ }
66
+ },
67
+ message(token) {
68
+ try {
69
+ resolve(pairingGrant(server, token));
70
+ }
71
+ catch (error) {
72
+ reject(error);
73
+ }
74
+ finish();
75
+ },
76
+ failed: fail,
77
+ });
78
+ const deadline = handshakeDeadline(signal, fail);
79
+ });
80
+ }
81
+ /** Browser-only connection to an explicitly paired HTTPS backend. */
82
+ export async function connectDirect({ server, token, app, signal }) {
83
+ server = directBackendOrigin(server);
84
+ let { bearer, calendar } = directCredential(server, token);
85
+ token = "";
86
+ const hashes = activePins(calendar);
87
+ // A stream may already contain pushes after hello. Resume delivery once the
88
+ // ordinary client has installed its Session handlers (also on failed attachment).
89
+ let release;
90
+ const attached = new Promise(resolve => { release = resolve; });
91
+ const connector = {
92
+ description: "paired direct",
93
+ open(hello, abortSignal) {
94
+ return new Promise((resolve, reject) => {
95
+ const channel = {
96
+ onclose: null, ondata: null,
97
+ write: bytes => messages.send(bytes),
98
+ close: () => messages.close(),
99
+ };
100
+ const port = framedPort(channel);
101
+ let receivedHello = false;
102
+ const messages = openDirectMessages(server, { operation: "replica", hashes }, {
103
+ ready() {
104
+ try {
105
+ // TLS can finish after a clock adjustment or the end of the calendar.
106
+ const stillValid = activePins(calendar);
107
+ if (!hashes.every(pin => stillValid.includes(pin))) {
108
+ throw mdbaseError("unavailable", "Certificate slot changed during handshake");
109
+ }
110
+ deadline.sent();
111
+ messages.send(bearer);
112
+ bearer = "";
113
+ port.send(hello);
114
+ }
115
+ catch {
116
+ channel.onclose?.(mdbaseError("unavailable", "Direct connection failed"));
117
+ }
118
+ },
119
+ message(data) {
120
+ if (!(data instanceof Uint8Array) || data.length < 5 || data.length > MAX_FRAME + 4 ||
121
+ new DataView(data.buffer, data.byteOffset).getUint32(0) !== data.length - 4) {
122
+ channel.onclose?.(mdbaseError("invalid_request", "Invalid direct replica record"));
123
+ }
124
+ else {
125
+ channel.ondata?.(data);
126
+ return attached;
127
+ }
128
+ },
129
+ failed(error) {
130
+ if (receivedHello)
131
+ void attached.then(() => channel.onclose?.(error));
132
+ else
133
+ channel.onclose?.(error);
134
+ },
135
+ });
136
+ const deadline = handshakeDeadline(abortSignal, error => channel.onclose?.(error));
137
+ port.onclose = error => {
138
+ bearer = "";
139
+ deadline.clear();
140
+ reject(error ?? mdbaseError("unavailable", "Direct connection closed"));
141
+ };
142
+ port.onframe = helloResponse => {
143
+ receivedHello = true;
144
+ deadline.clear();
145
+ port.onframe = null;
146
+ port.onclose = null;
147
+ resolve({ port, helloResponse });
148
+ };
149
+ });
150
+ },
151
+ };
152
+ try {
153
+ return await connect({ connector, app, signal, reconnect: false });
154
+ }
155
+ finally {
156
+ release();
157
+ }
158
+ }
159
+ //# sourceMappingURL=direct.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"direct.js","sourceRoot":"","sources":["../src/direct.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAyC,MAAM,aAAa,CAAC;AAC7E,OAAO,EAAE,WAAW,EAAoB,MAAM,aAAa,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,SAAS,EAAoC,MAAM,qBAAqB,CAAC;AAC9F,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACpF,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,EAAE,sBAAsB,EAAkD,MAAM,yBAAyB,CAAC;AAUjH,kFAAkF;AAClF,uEAAuE;AACvE,MAAM,UAAU,GAAG,MAAM,CAAC;AAC1B,MAAM,QAAQ,GAAG,MAAM,CAAC;AAExB,SAAS,iBAAiB,CAAC,MAA+B,EAAE,IAAkC;IAC5F,MAAM,KAAK,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,WAAW,EAAE,4BAA4B,CAAC,CAAC,CAAC;IACjF,MAAM,MAAM,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,aAAa,EAAE,4BAA4B,CAAC,CAAC,CAAC;IACpF,IAAI,KAAK,GAAG,UAAU,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IAC3C,MAAM,EAAE,gBAAgB,CAAC,OAAO,EAAE,KAAK,EAAE,EAAE,IA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AG,IAAI,CAAC;oBACrB,QAAQ,CAAC,KAAK,EAAE,CAAC;oBACjB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;oBACpB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;oBACpB,OAAO,CAAC,EAAE,IAAI,EAAE,aAAa,EAAE,CAAC,CAAC;gBACnC,CAAC,CAAC;YACJ,CAAC,CAAC,CAAC;QACL,CAAC;KACF,CAAC;IACF,IAAI,CAAC;QAAC,OAAO,MAAM,OAAO,CAAC,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC,CAAC;IAAC,CAAC;YACnE,CAAC;QAAC,OAAO,EAAE,CAAC;IAAC,CAAC;AACxB,CAAC"}
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "schema": "mdbase-sdk/npm-publication/1",
3
- "commit": "273d38ff26c88e77e98b3b8dee0d27bd110053ae",
3
+ "commit": "600b845bdc94b4d3b0eee2208d1564d7a5b3b007",
4
4
  "environment": "production",
5
5
  "trust": {
6
6
  "package": "@mdbase-dev/sdk",
@@ -12,12 +12,12 @@
12
12
  "sourceVersion": "0.1.0-beta.141"
13
13
  },
14
14
  "runtime": {
15
- "sha256": "9105a7424c2eff35a429ace9cd96e6256fc05de28e90e0e92967a43e4d29d1ba",
16
- "bytes": 3982069,
17
- "maxBytes": 3982069,
15
+ "sha256": "1b70aad8903a333cbe422e6b57e8677929c6b556651e69f2a0e7bff1f1ddb52c",
16
+ "bytes": 4004324,
17
+ "maxBytes": 4004324,
18
18
  "source": {
19
19
  "repository": "mdbase-dev/mdbase-connect",
20
- "commit": "6891ac960fc525b5a1da0700915324255a71adcd"
20
+ "commit": "74f2d1018b66ab3ae99bc0afb5fb3a8009b25e23"
21
21
  }
22
22
  }
23
23
  }
Binary file
package/package.json CHANGED
@@ -1,11 +1,15 @@
1
1
  {
2
2
  "name": "@mdbase-dev/sdk",
3
- "version": "0.1.36",
3
+ "version": "0.1.38",
4
4
  "description": "TypeScript SDK: live queries, optimistic submits, receipts, status, holds, files and presence over the replica client API.",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "sideEffects": false,
8
8
  "exports": {
9
+ "./direct": {
10
+ "types": "./dist/direct.d.ts",
11
+ "import": "./dist/direct.js"
12
+ },
9
13
  ".": {
10
14
  "types": "./dist/index.d.ts",
11
15
  "import": "./dist/index.js"