@peers-app/peers-sdk 0.8.11 → 0.8.17

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.
@@ -60,6 +60,8 @@ export declare class PersistentVarsTable extends Table<IPersistentVar> {
60
60
  export declare function PersistentVars(dataContext?: DataContext): PersistentVarsTable;
61
61
  export type PersistentVar<T> = Observable<T> & {
62
62
  loadingPromise: Promise<PersistentVar<T>>;
63
+ /** Delete the persistent variable from the database and reset to default value */
64
+ delete: () => Promise<void>;
63
65
  };
64
66
  export declare function getPersistentVar(name: string, dataContext?: DataContext): Promise<IPersistentVar | undefined>;
65
67
  interface IPersistentVarOptionsBase {
@@ -155,6 +155,14 @@ function persistentVarFactory(name, opts) {
155
155
  pvarCache.set(cacheKey, persistentVar);
156
156
  let isSecret = opts.isSecret;
157
157
  let rec = undefined;
158
+ let deleteVarImpl = undefined;
159
+ // Assign the delete method - it will wait for loading and then call the implementation
160
+ persistentVar.delete = async () => {
161
+ await persistentVar.loadingPromise;
162
+ if (deleteVarImpl) {
163
+ await deleteVarImpl();
164
+ }
165
+ };
158
166
  persistentVar.loadingPromise = new Promise(async (resolve) => {
159
167
  try {
160
168
  const userContext = opts?.userContext || await (0, user_context_singleton_1.getUserContext)();
@@ -224,37 +232,36 @@ function persistentVarFactory(name, opts) {
224
232
  rec.value.value = value;
225
233
  const dc = getDataContext();
226
234
  const table = PersistentVars(dc);
227
- // delete if value equals default value
228
- if ((0, lodash_1.isEqual)(rec.value.value, defaultValue)) {
229
- if (rec.persistentVarId) {
230
- if (name === 'colorModePreference') {
231
- console.log(`deleted persistent var ${name} from db:`, rec.value.value);
232
- }
233
- await table.delete(rec);
235
+ try {
236
+ rec = await table.save(rec);
237
+ if (name === 'colorModePreference') {
238
+ console.log(`Saved var ${name} to db:`, rec.value.value);
234
239
  }
235
- rec = undefined;
236
240
  }
237
- else {
238
- try {
239
- rec = await table.save(rec);
240
- if (name === 'colorModePreference') {
241
- console.log(`Saved var ${name} to db:`, rec.value.value);
242
- }
241
+ catch (err) {
242
+ const errMsg = err?.message || String(err) || '';
243
+ if (errMsg.includes('UNIQUE constraint failed')) {
244
+ console.warn(`Detected UNIQUE constraint failed error when saving persistent var, reloading and retrying: ${name}`);
245
+ rec = await loadRecFromDb();
246
+ persistentVar(rec.value.value);
243
247
  }
244
- catch (err) {
245
- const errMsg = err?.message || String(err) || '';
246
- if (errMsg.includes('UNIQUE constraint failed')) {
247
- console.warn(`Detected UNIQUE constraint failed error when saving persistent var, reloading and retrying: ${name}`);
248
- rec = await loadRecFromDb();
249
- persistentVar(rec.value.value);
250
- }
251
- else {
252
- console.error('Error saving persistent var', { name, value, rec, err });
253
- throw err;
254
- }
248
+ else {
249
+ console.error('Error saving persistent var', { name, value, rec, err });
250
+ throw err;
255
251
  }
256
252
  }
257
253
  }
254
+ deleteVarImpl = async () => {
255
+ const dc = getDataContext();
256
+ const table = PersistentVars(dc);
257
+ if (rec?.persistentVarId) {
258
+ await table.delete(rec);
259
+ rec = undefined;
260
+ }
261
+ if (defaultValue !== undefined) {
262
+ persistentVar(defaultValue);
263
+ }
264
+ };
258
265
  // subscribe to db changes
259
266
  userContext.subscribeToDataChangedAcrossAllGroups(exports.persistentVarsMetaData.name, async (evt) => {
260
267
  const dbRec = evt.data.dataObject;
@@ -0,0 +1,69 @@
1
+ /**
2
+ * binary-peer-connection-v2.ts
3
+ *
4
+ * Unified binary transport layer that replaces both binary-peer-connection.ts
5
+ * and streamed-socket.ts. Provides two cleanly separated modes:
6
+ *
7
+ * 1. **RPC mode** (emit/on): request/response interactions with automatic
8
+ * encoding (txEncode for objects, passthrough for Uint8Array) and transparent
9
+ * chunking for large payloads.
10
+ *
11
+ * 2. **Stream mode** (sendRawBytes/onRawBytes): continuous byte transfer for
12
+ * file syncing, database downloads, log streaming, etc. Each sendRawBytes
13
+ * call = exactly one onRawBytes callback (atomic delivery after reassembly).
14
+ *
15
+ * Both modes share a single sendFrame() function that handles backpressure
16
+ * and event-loop yielding (critical for Go sidecar WebRTC pipeline).
17
+ */
18
+ import { Connection } from "./connection";
19
+ import { Device } from "./device";
20
+ import { ISocket } from "./socket.type";
21
+ type ITrustLevelFn = Connection['getTrustLevel'];
22
+ /**
23
+ * Generic binary peer interface supporting WebRTC, libp2p, and WebSocket transports.
24
+ * Provides a unified abstraction for binary data channel communication.
25
+ */
26
+ export interface IBinaryPeer {
27
+ on(event: 'data', handler: (data: Buffer | Uint8Array) => void): void;
28
+ on(event: 'close', handler: () => void): void;
29
+ on(event: 'drain', handler: () => void): void;
30
+ send(data: string | Uint8Array | Buffer): void;
31
+ destroy(): void;
32
+ /**
33
+ * Returns the number of bytes currently buffered waiting to be sent.
34
+ * Used for backpressure detection. Returns 0 if not supported.
35
+ */
36
+ getBufferedAmount?(): number;
37
+ }
38
+ export type IWebRTCPeer = IBinaryPeer;
39
+ export type ILibp2pPeer = IBinaryPeer;
40
+ export interface WrapBinaryPeerOptions {
41
+ /** Protocol identifier used for server address (e.g., 'wrtc', 'libp2p', or 'ws') */
42
+ protocol: 'wrtc' | 'libp2p' | 'ws';
43
+ /**
44
+ * Mark the transport as already secure (encrypted at transport layer).
45
+ * When true, sets secureLocal and secureRemote to skip application-level encryption.
46
+ */
47
+ markTransportSecure?: boolean;
48
+ }
49
+ /**
50
+ * Creates an ISocket from a binary peer. This is the core v2 factory function.
51
+ * Replaces both createBinaryPeerSocket (v1) and StreamedSocket.
52
+ */
53
+ export declare function createBinaryPeerSocket(connectionId: string, peer: IBinaryPeer, protocol?: string): ISocket;
54
+ /**
55
+ * Wraps a binary peer (WebRTC, libp2p, or WebSocket) into a Connection.
56
+ * This is the main implementation that handles all binary protocols.
57
+ */
58
+ export declare function wrapBinaryPeer(connectionId: string, peer: IBinaryPeer, localDevice: Device, initiator: boolean, options: WrapBinaryPeerOptions, getTrustLevel?: ITrustLevelFn): Connection;
59
+ /**
60
+ * Wraps a WebRTC peer into a Connection.
61
+ * WebRTC is encrypted at the transport layer (DTLS-SRTP).
62
+ */
63
+ export declare function wrapWrtc(connectionId: string, peer: IWebRTCPeer, localDevice: Device, initiator: boolean, getTrustLevel?: ITrustLevelFn): Connection;
64
+ /**
65
+ * Wraps a libp2p peer into a Connection.
66
+ * libp2p is encrypted at the transport layer (noise protocol).
67
+ */
68
+ export declare function wrapLibp2p(connectionId: string, peer: ILibp2pPeer, localDevice: Device, initiator: boolean, getTrustLevel?: ITrustLevelFn): Connection;
69
+ export {};