@oxidezap/baileyrs 0.3.17 → 0.4.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.
- package/README.md +17 -3
- package/lib/Compatibility/legacy-store/adapter.js +56 -20
- package/lib/Compatibility/legacy-store/multi-file.js +1 -0
- package/lib/Compatibility/legacy-store/routing.d.ts +1 -0
- package/lib/Compatibility/legacy-store/routing.js +7 -0
- package/lib/Compatibility/legacy-store/snapshot.js +20 -1
- package/lib/Compatibility/legacy-store/types.d.ts +2 -0
- package/lib/Types/Auth.d.ts +1 -0
- package/lib/Utils/atomic-store.d.ts +6 -5
- package/lib/Utils/atomic-store.js +36 -1
- package/lib/Utils/optional-peer.js +1 -1
- package/lib/Utils/transactional-file-store.js +1 -0
- package/lib/Utils/use-memory-store.js +11 -1
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -100,6 +100,15 @@ Memory storage is temporary. For persistent sessions, supply a byte-preserving
|
|
|
100
100
|
existing device record, including its registration ID, after a failed read.
|
|
101
101
|
This entrypoint uses no filesystem auth or Node streams.
|
|
102
102
|
|
|
103
|
+
With bridge `0.27.0`, a custom `JsStoreCallbacks` store must implement
|
|
104
|
+
`commitSnapshot(name, state, entries, maintainIndex)`. In the same transaction
|
|
105
|
+
used for `commitPatch`, replace all `mutation_mac` rows whose keys start with
|
|
106
|
+
`name + ':'`, write `sync_version[name]` and update the matching keys in
|
|
107
|
+
`meta[mutation_mac_keys]` when requested. An empty `entries` array clears the
|
|
108
|
+
collection's MACs. Resolve after persistence and propagate any commit error.
|
|
109
|
+
The built-in memory and file stores implement this callback and can reopen
|
|
110
|
+
their existing auth data.
|
|
111
|
+
|
|
103
112
|
Standalone AES-backed media retry helpers require `initWasmEngine()` before use
|
|
104
113
|
if no socket has initialized the engine, after `initSync()` on hosts. With a
|
|
105
114
|
custom runtime, pass `makeMediaCryptoRuntime(runtime)` as the final argument to
|
|
@@ -122,9 +131,14 @@ URLs for PRs and commits. They are unreleased code with version
|
|
|
122
131
|
## Migrating from upstream Baileys
|
|
123
132
|
|
|
124
133
|
Custom upstream `{ creds, keys }` auth states require `keys.bridgeTransaction`
|
|
125
|
-
with backend-wide isolation and durable atomic commits.
|
|
126
|
-
|
|
127
|
-
|
|
134
|
+
with backend-wide isolation and durable atomic commits. Provide
|
|
135
|
+
`listKeys(type, prefix)` on the transaction view so snapshot commits can find
|
|
136
|
+
every matching row in `bridge-mutation-mac`, including historical rows missing
|
|
137
|
+
from the index. `useLegacyMultiFileAuthState` supplies both methods. Keep
|
|
138
|
+
persisting `creds.update` with your loader's `saveCreds`.
|
|
139
|
+
|
|
140
|
+
The adapter stores status-scoped Signal sessions and identities in the
|
|
141
|
+
`bridge-native-*` records. Include these records in auth backups.
|
|
128
142
|
|
|
129
143
|
For an existing upstream JSON directory, use `useLegacyMultiFileAuthState`.
|
|
130
144
|
`useMultiFileAuthState` uses native binary storage and does not import upstream JSON.
|
|
@@ -6,10 +6,14 @@ import { NativeStore, RouteKind } from './constants.js';
|
|
|
6
6
|
import { decodeNativeEnvelope, encodeNativeEnvelope, encodeNativeOnlyEnvelope, envelopeMatchesLegacy, toBytes } from './common.js';
|
|
7
7
|
import { createLegacyCodecs } from './codecs/index.js';
|
|
8
8
|
import { createDeviceProjection, parseDeviceRecordKey } from './device.js';
|
|
9
|
-
import { isProjectedStoreName, legacyKey, parseNativeStoreName, resolveRoute } from './routing.js';
|
|
9
|
+
import { isProjectedStoreName, isStatusSignalAddress, legacyKey, parseNativeStoreName, resolveRoute } from './routing.js';
|
|
10
10
|
import { NATIVE_ONLY_PROJECTION } from './types.js';
|
|
11
11
|
const emptyBucket = () => Object.create(null);
|
|
12
12
|
const ownValue = (bucket, key) => Object.prototype.hasOwnProperty.call(bucket, key) ? bucket[key] : null;
|
|
13
|
+
// The pinned Baileys store has no key for the SDK's separate status Signal scope.
|
|
14
|
+
const projectionKey = (store, key) => (store === NativeStore.SESSION || store === NativeStore.IDENTITY) && isStatusSignalAddress(key)
|
|
15
|
+
? null
|
|
16
|
+
: legacyKey(store, key);
|
|
13
17
|
function uniqueKeys(keys, label) {
|
|
14
18
|
const seen = new Set();
|
|
15
19
|
for (const key of keys) {
|
|
@@ -40,6 +44,7 @@ function withPendingCallbacks(store) {
|
|
|
40
44
|
delete: (...args) => track(Promise.resolve(store.delete(...args))),
|
|
41
45
|
deleteMany: (...args) => track(Promise.resolve(store.deleteMany(...args))),
|
|
42
46
|
commitPatch: (...args) => track(Promise.resolve(store.commitPatch(...args))),
|
|
47
|
+
commitSnapshot: (...args) => track(Promise.resolve(store.commitSnapshot(...args))),
|
|
43
48
|
storeReceivedTcToken: (...args) => track(Promise.resolve(store.storeReceivedTcToken(...args))),
|
|
44
49
|
touchTcTokenSenderTimestamp: (...args) => track(Promise.resolve(store.touchTcTokenSenderTimestamp(...args))),
|
|
45
50
|
async flush() {
|
|
@@ -104,17 +109,22 @@ export async function wrapLegacyStore(state, saveCreds, logger) {
|
|
|
104
109
|
const route = resolveRoute(store);
|
|
105
110
|
if (route.kind !== RouteKind.PROJECTED)
|
|
106
111
|
throw new TypeError(`${store} is not a projected store`);
|
|
107
|
-
const translated = requested.map(key => [key,
|
|
108
|
-
|
|
112
|
+
const translated = requested.map(key => [key, projectionKey(store, key)]);
|
|
113
|
+
const legacyIds = translated.map(([, key]) => key).filter(key => key !== null);
|
|
114
|
+
uniqueKeys(legacyIds, `${store} legacy-key projection`);
|
|
109
115
|
const [nativeBucket, legacyBucket] = await Promise.all([
|
|
110
116
|
readBucket(route.nativeType, requested),
|
|
111
|
-
readBucket(route.legacyType,
|
|
117
|
+
readBucket(route.legacyType, legacyIds)
|
|
112
118
|
]);
|
|
113
119
|
const mirrorUpdates = emptyBucket();
|
|
114
120
|
const imported = new Map();
|
|
115
121
|
for (const [nativeKey, translatedKey] of translated) {
|
|
116
|
-
const legacyValue = ownValue(legacyBucket, translatedKey);
|
|
117
122
|
const envelope = decodeNativeEnvelope(ownValue(nativeBucket, nativeKey));
|
|
123
|
+
if (translatedKey === null) {
|
|
124
|
+
imported.set(nativeKey, envelope?.payload ?? null);
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
const legacyValue = ownValue(legacyBucket, translatedKey);
|
|
118
128
|
if (envelope && envelopeMatchesLegacy(envelope, legacyValue)) {
|
|
119
129
|
imported.set(nativeKey, envelope.payload);
|
|
120
130
|
continue;
|
|
@@ -191,18 +201,20 @@ export async function wrapLegacyStore(state, saveCreds, logger) {
|
|
|
191
201
|
throw new TypeError(`${store} is not a projected store`);
|
|
192
202
|
const nativeBucket = emptyBucket();
|
|
193
203
|
const legacyBucket = emptyBucket();
|
|
194
|
-
const projected = entries.map(([key, value]) =>
|
|
195
|
-
key
|
|
196
|
-
value
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
}));
|
|
200
|
-
uniqueKeys(
|
|
201
|
-
const nativeOnly = projected.
|
|
202
|
-
const existingLegacy =
|
|
203
|
-
? emptyBucket()
|
|
204
|
-
: await readBucket(route.legacyType, nativeOnly.map(({ translatedKey }) => translatedKey));
|
|
204
|
+
const projected = entries.map(([key, value]) => {
|
|
205
|
+
const translatedKey = projectionKey(store, key);
|
|
206
|
+
const legacyValue = translatedKey === null ? NATIVE_ONLY_PROJECTION : codecs[store].toLegacy(key, value);
|
|
207
|
+
return { key, value, translatedKey, legacyValue };
|
|
208
|
+
});
|
|
209
|
+
const legacyIds = projected.map(({ translatedKey }) => translatedKey).filter(key => key !== null);
|
|
210
|
+
uniqueKeys(legacyIds, `${store} legacy-key projection`);
|
|
211
|
+
const nativeOnly = projected.flatMap(({ legacyValue, translatedKey }) => legacyValue === NATIVE_ONLY_PROJECTION && translatedKey !== null ? [translatedKey] : []);
|
|
212
|
+
const existingLegacy = await readBucket(route.legacyType, nativeOnly);
|
|
205
213
|
for (const { key, value, translatedKey, legacyValue } of projected) {
|
|
214
|
+
if (translatedKey === null) {
|
|
215
|
+
nativeBucket[key] = encodeNativeOnlyEnvelope(value, null);
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
206
218
|
if (legacyValue === NATIVE_ONLY_PROJECTION) {
|
|
207
219
|
nativeBucket[key] = encodeNativeOnlyEnvelope(value, ownValue(existingLegacy, translatedKey));
|
|
208
220
|
continue;
|
|
@@ -265,13 +277,16 @@ export async function wrapLegacyStore(state, saveCreds, logger) {
|
|
|
265
277
|
throw new TypeError(`${store} is not a projected store`);
|
|
266
278
|
const nativeBucket = emptyBucket();
|
|
267
279
|
const legacyBucket = emptyBucket();
|
|
268
|
-
const translated = requested.map(key =>
|
|
280
|
+
const translated = requested.map(key => projectionKey(store, key)).filter(key => key !== null);
|
|
269
281
|
uniqueKeys(translated, `${store} legacy-key projection`);
|
|
270
282
|
for (const key of requested)
|
|
271
283
|
nativeBucket[key] = null;
|
|
272
284
|
for (const key of translated)
|
|
273
285
|
legacyBucket[key] = null;
|
|
274
|
-
|
|
286
|
+
const updates = { [route.nativeType]: nativeBucket };
|
|
287
|
+
if (translated.length > 0)
|
|
288
|
+
updates[route.legacyType] = legacyBucket;
|
|
289
|
+
await persist(updates);
|
|
275
290
|
}
|
|
276
291
|
async function removeMany(storeValue, requested) {
|
|
277
292
|
if (requested.length === 0)
|
|
@@ -300,8 +315,22 @@ export async function wrapLegacyStore(state, saveCreds, logger) {
|
|
|
300
315
|
}
|
|
301
316
|
}, saveCreds, logger);
|
|
302
317
|
const rows = new Map();
|
|
303
|
-
for (const
|
|
304
|
-
|
|
318
|
+
for (const selection of stores) {
|
|
319
|
+
const { store } = selection;
|
|
320
|
+
if ('prefix' in selection) {
|
|
321
|
+
if (typeof transactionalKeys.listKeys !== 'function')
|
|
322
|
+
throw new TypeError('legacy app-state snapshots require listKeys(type, prefix) inside keys.bridgeTransaction');
|
|
323
|
+
const { prefix } = selection;
|
|
324
|
+
const requested = await transactionalKeys.listKeys(resolveRoute(selection.store).nativeType, prefix);
|
|
325
|
+
if (!Array.isArray(requested))
|
|
326
|
+
throw new TypeError('legacy listKeys must return a key array');
|
|
327
|
+
for (const key of requested)
|
|
328
|
+
if (typeof key !== 'string' || !key.startsWith(prefix))
|
|
329
|
+
throw new TypeError('legacy listKeys returned a MAC key outside the snapshot collection');
|
|
330
|
+
await adapter.deleteMany(store, requested);
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
for (const [key, value] of await adapter.getMany(store, selection.keys))
|
|
305
334
|
rows.set(rowKey(store, key), value);
|
|
306
335
|
}
|
|
307
336
|
const before = new Map(rows);
|
|
@@ -331,6 +360,13 @@ export async function wrapLegacyStore(state, saveCreds, logger) {
|
|
|
331
360
|
{ store: 'meta', keys: maintainIndex ? ['mutation_mac_keys'] : [] }
|
|
332
361
|
]).commitPatch(name, stateBytes, removed, added, maintainIndex);
|
|
333
362
|
},
|
|
363
|
+
commitSnapshot(name, stateBytes, entries, maintainIndex) {
|
|
364
|
+
return atomicMethods([
|
|
365
|
+
{ store: 'sync_version', keys: [name] },
|
|
366
|
+
{ store: 'mutation_mac', prefix: `${name}:` },
|
|
367
|
+
{ store: 'meta', keys: maintainIndex ? ['mutation_mac_keys'] : [] }
|
|
368
|
+
]).commitSnapshot(name, stateBytes, entries, maintainIndex);
|
|
369
|
+
},
|
|
334
370
|
touchTcTokenSenderTimestamp(jid, timestamp) {
|
|
335
371
|
return atomicMethods([
|
|
336
372
|
{ store: 'tc_token', keys: [jid] },
|
|
@@ -68,6 +68,7 @@ export async function useLegacyMultiFileAuthState(folder, options = {}) {
|
|
|
68
68
|
]));
|
|
69
69
|
},
|
|
70
70
|
set: data => backend.set(data),
|
|
71
|
+
...(backend.listKeys ? { listKeys: (type, prefix) => backend.listKeys(type, prefix) } : {}),
|
|
71
72
|
...(backend.bridgeTransaction
|
|
72
73
|
? {
|
|
73
74
|
bridgeTransaction: (work) => backend.bridgeTransaction(keys => work(normalizeKeys(keys)))
|
|
@@ -3,6 +3,7 @@ import type { ProjectedStoreName } from './constants.js';
|
|
|
3
3
|
import type { StoreRoute } from './types.js';
|
|
4
4
|
export declare function parseNativeStoreName(value: string): NativeStoreName;
|
|
5
5
|
export declare function resolveRoute(store: NativeStoreName): StoreRoute;
|
|
6
|
+
export declare function isStatusSignalAddress(address: string): boolean;
|
|
6
7
|
export declare function legacyKey(store: ProjectedStoreName, key: string): string;
|
|
7
8
|
export declare function nativeKey(store: ProjectedStoreName, key: string): string;
|
|
8
9
|
export declare function projectedStoreForLegacyType(type: string): ProjectedStoreName | null;
|
|
@@ -48,6 +48,13 @@ function legacySignalAddress(address) {
|
|
|
48
48
|
: `${parsed.user}${SignalAddressSyntax.DOMAIN_TYPE}${parsed.domainType}`;
|
|
49
49
|
return `${user}${SignalAddressSyntax.SIGNAL_DEVICE}${parsed.device}`;
|
|
50
50
|
}
|
|
51
|
+
export function isStatusSignalAddress(address) {
|
|
52
|
+
const suffix = '_status.0';
|
|
53
|
+
if (!address.endsWith(suffix))
|
|
54
|
+
return false;
|
|
55
|
+
const { domainType } = parseBridgeSignalAddress(`${address.slice(0, -suffix.length)}.0`);
|
|
56
|
+
return domainType === SignalDomainType.PN || domainType === SignalDomainType.LID;
|
|
57
|
+
}
|
|
51
58
|
/** A sender key is `<chatJid>:<signalAddress>`, and the chat is a group, status
|
|
52
59
|
* or a broadcast list. Which chats fan out through sender keys is the core's
|
|
53
60
|
* namespace, so the boundary validates the JID shape, not the domain. No JID
|
|
@@ -4,7 +4,8 @@ import { readFile, readdir } from 'node:fs/promises';
|
|
|
4
4
|
import { join } from 'node:path';
|
|
5
5
|
import { createSnapshotTransactions } from '../../Utils/snapshot-transactions.js';
|
|
6
6
|
import { defaultSyncDir } from '../../Utils/durable-directory.js';
|
|
7
|
-
import { rowKey } from '../../Utils/atomic-store.js';
|
|
7
|
+
import { rowKey, rowPrefix } from '../../Utils/atomic-store.js';
|
|
8
|
+
import { LegacyMultiFile } from './constants.js';
|
|
8
9
|
import { BufferJSON } from '../../Utils/generics.js';
|
|
9
10
|
/** All legacy and native-mirror writes share the same persistent snapshot.
|
|
10
11
|
* The original JSON files are migration backups after the first commit.
|
|
@@ -24,6 +25,24 @@ export const legacySnapshotKeys = async (folder, fileName, options = {}) => {
|
|
|
24
25
|
const decode = (bytes) => bytes ? JSON.parse(Buffer.from(bytes).toString(), BufferJSON.reviver) : null;
|
|
25
26
|
const transactionView = (rows, mutated) => ({
|
|
26
27
|
get: async (type, ids) => Object.fromEntries(ids.map(id => [id, decode(rows.get(rowKey('legacy', fileName(type, id))))])),
|
|
28
|
+
listKeys: (type, prefix) => {
|
|
29
|
+
// MAC row keys have the form collection:hex. Their suffix survives legacy filename encoding.
|
|
30
|
+
const suffixLength = LegacyMultiFile.EXTENSION.length;
|
|
31
|
+
const filenamePrefix = fileName(type, prefix).slice(0, -suffixLength);
|
|
32
|
+
const encodedPrefix = rowPrefix('legacy', filenamePrefix);
|
|
33
|
+
const matches = [];
|
|
34
|
+
for (const key of rows.keys()) {
|
|
35
|
+
if (!key.startsWith(encodedPrefix))
|
|
36
|
+
continue;
|
|
37
|
+
const [, filename] = JSON.parse(key);
|
|
38
|
+
if (!filename.endsWith(LegacyMultiFile.EXTENSION))
|
|
39
|
+
continue;
|
|
40
|
+
const suffix = filename.slice(filenamePrefix.length, -suffixLength);
|
|
41
|
+
if (!suffix.includes(LegacyMultiFile.COLON_REPLACEMENT))
|
|
42
|
+
matches.push(prefix + suffix);
|
|
43
|
+
}
|
|
44
|
+
return matches;
|
|
45
|
+
},
|
|
27
46
|
set: async (data) => {
|
|
28
47
|
for (const [type, values] of Object.entries(data))
|
|
29
48
|
for (const [id, value] of Object.entries(values ?? {})) {
|
|
@@ -11,6 +11,8 @@ type Awaitable<T> = T | PromiseLike<T>;
|
|
|
11
11
|
export type LegacyKeysStore = {
|
|
12
12
|
get(type: string, ids: string[]): Awaitable<Record<string, unknown>>;
|
|
13
13
|
set(data: Record<string, Record<string, unknown>>): Awaitable<void>;
|
|
14
|
+
/** Enumerate all matching IDs inside the current transaction for snapshot replacement. */
|
|
15
|
+
listKeys?(type: string, prefix: string): Awaitable<string[]>;
|
|
14
16
|
bridgeTransaction?<T>(work: (keys: LegacyKeysStore) => Promise<T>): Promise<T>;
|
|
15
17
|
};
|
|
16
18
|
export interface WrappedLegacyStore extends JsStoreCallbacks {
|
package/lib/Types/Auth.d.ts
CHANGED
|
@@ -111,6 +111,7 @@ type Awaitable<T> = T | Promise<T>;
|
|
|
111
111
|
export type SignalKeyStore = {
|
|
112
112
|
/** Backend transaction, isolated against every writer in the device scope.
|
|
113
113
|
* Must commit all mutations durably or none, including native mirror rows.
|
|
114
|
+
* Snapshot replacement uses the transaction view's `listKeys(type, prefix)`.
|
|
114
115
|
* The upstream adapter-local transaction helper does not meet this contract. */
|
|
115
116
|
bridgeTransaction?<T>(work: (keys: LegacyKeysStore) => Promise<T>): Promise<T>;
|
|
116
117
|
get<T extends keyof SignalDataTypeMap>(type: T, ids: string[]): Awaitable<{
|
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
import type { JsStoreCallbacks } from '@oxidezap/whatsapp-rust-bridge';
|
|
2
|
-
export type AtomicStoreCallbacks = Pick<JsStoreCallbacks, 'commitPatch' | 'touchTcTokenSenderTimestamp' | 'storeReceivedTcToken'>;
|
|
2
|
+
export type AtomicStoreCallbacks = Pick<JsStoreCallbacks, 'commitPatch' | 'commitSnapshot' | 'touchTcTokenSenderTimestamp' | 'storeReceivedTcToken'>;
|
|
3
3
|
export type OrdinaryStoreCallbacks = Omit<JsStoreCallbacks, keyof AtomicStoreCallbacks> & {
|
|
4
4
|
flush?(): Promise<void>;
|
|
5
5
|
};
|
|
6
6
|
export type AtomicRows = Map<string, Uint8Array>;
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* stages the writes, so a change costs the rows it touches.
|
|
8
|
+
* A transaction view can stage writes against its own rows instead of copying
|
|
9
|
+
* the whole store into an `AtomicRows` map.
|
|
11
10
|
*/
|
|
12
11
|
export interface AtomicRowView {
|
|
13
12
|
get(key: string): Uint8Array | undefined;
|
|
14
13
|
has(key: string): boolean;
|
|
15
14
|
set(key: string, value: Uint8Array): unknown;
|
|
16
15
|
delete(key: string): boolean;
|
|
16
|
+
deletePrefix(store: string, prefix: string): void;
|
|
17
17
|
}
|
|
18
18
|
export declare const rowKey: (store: string, key: string) => string;
|
|
19
|
+
export declare const rowPrefix: (store: string, prefix: string) => string;
|
|
19
20
|
export declare const signedTimestamp: (value: string) => bigint;
|
|
20
21
|
export type TokenRow = {
|
|
21
22
|
token: number[];
|
|
@@ -25,7 +26,7 @@ export type TokenRow = {
|
|
|
25
26
|
export declare const decodeTokenRow: (bytes: Uint8Array) => TokenRow;
|
|
26
27
|
export declare const encodeTokenRow: (row: TokenRow) => Uint8Array;
|
|
27
28
|
/** The caller supplies a real transaction covering all reads and writes. */
|
|
28
|
-
export declare const atomicCallbacks: (transaction: (change: (rows: AtomicRowView) => void) => Promise<void>) => AtomicStoreCallbacks;
|
|
29
|
+
export declare const atomicCallbacks: (transaction: (change: (rows: AtomicRows | AtomicRowView) => void) => Promise<void>) => AtomicStoreCallbacks;
|
|
29
30
|
/** Validate migration requirements before hydration or other storage I/O. */
|
|
30
31
|
export declare function assertStoreCallbacks(store: unknown): asserts store is JsStoreCallbacks;
|
|
31
32
|
//# sourceMappingURL=atomic-store.d.ts.map
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export const rowKey = (store, key) => JSON.stringify([store, key]);
|
|
2
|
+
// Canonical row keys encode a JSON tuple. The closing quote and bracket end the key.
|
|
3
|
+
export const rowPrefix = (store, prefix) => rowKey(store, prefix).slice(0, -2);
|
|
2
4
|
const encode = (value) => new TextEncoder().encode(JSON.stringify(value));
|
|
3
5
|
const decode = (value) => JSON.parse(new TextDecoder().decode(value));
|
|
4
6
|
export const signedTimestamp = (value) => {
|
|
@@ -65,6 +67,38 @@ export const atomicCallbacks = (transaction) => ({
|
|
|
65
67
|
}
|
|
66
68
|
});
|
|
67
69
|
},
|
|
70
|
+
async commitSnapshot(name, state, entries, maintainIndex) {
|
|
71
|
+
const version = Uint8Array.from(state);
|
|
72
|
+
const prefix = `${name}:`;
|
|
73
|
+
const replacement = entries.map(([key, bytes]) => {
|
|
74
|
+
if (!key.startsWith(prefix))
|
|
75
|
+
throw new TypeError('snapshot MAC key must belong to its collection');
|
|
76
|
+
return [key, Uint8Array.from(bytes)];
|
|
77
|
+
});
|
|
78
|
+
await transaction(rows => {
|
|
79
|
+
if (rows instanceof Map) {
|
|
80
|
+
const encodedPrefix = rowPrefix('mutation_mac', prefix);
|
|
81
|
+
for (const key of rows.keys())
|
|
82
|
+
if (key.startsWith(encodedPrefix))
|
|
83
|
+
rows.delete(key);
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
rows.deletePrefix('mutation_mac', prefix);
|
|
87
|
+
}
|
|
88
|
+
for (const [key, bytes] of replacement)
|
|
89
|
+
rows.set(rowKey('mutation_mac', key), bytes);
|
|
90
|
+
rows.set(rowKey('sync_version', name), version);
|
|
91
|
+
if (maintainIndex) {
|
|
92
|
+
const index = readIndex(rows, 'mutation_mac_keys');
|
|
93
|
+
for (const key of index)
|
|
94
|
+
if (key.startsWith(prefix))
|
|
95
|
+
index.delete(key);
|
|
96
|
+
for (const [key] of replacement)
|
|
97
|
+
index.add(key);
|
|
98
|
+
rows.set(rowKey('meta', 'mutation_mac_keys'), encode([...index]));
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
},
|
|
68
102
|
touchTcTokenSenderTimestamp(jid, timestamp) {
|
|
69
103
|
const incoming = signedTimestamp(timestamp);
|
|
70
104
|
return transaction(rows => {
|
|
@@ -106,11 +140,12 @@ export function assertStoreCallbacks(store) {
|
|
|
106
140
|
'set',
|
|
107
141
|
'delete',
|
|
108
142
|
'commitPatch',
|
|
143
|
+
'commitSnapshot',
|
|
109
144
|
'touchTcTokenSenderTimestamp',
|
|
110
145
|
'storeReceivedTcToken'
|
|
111
146
|
]) {
|
|
112
147
|
if (typeof store[method] !== 'function')
|
|
113
|
-
throw new TypeError(`auth store requires ${method};
|
|
148
|
+
throw new TypeError(`auth store requires ${method}; implement the bridge storage callbacks or use useMemoryStore()`);
|
|
114
149
|
}
|
|
115
150
|
}
|
|
116
151
|
//# sourceMappingURL=atomic-store.js.map
|
|
@@ -24,7 +24,7 @@ const isAbsent = (error) => error?.code === 'MODULE_NOT_FOUND';
|
|
|
24
24
|
export const loadOptionalPeer = (specifier, loader) => {
|
|
25
25
|
const effectiveLoader = loader ??
|
|
26
26
|
(typeof globalThis.process
|
|
27
|
-
?.getBuiltinModule === 'function'
|
|
27
|
+
?.getBuiltinModule === 'function' && import.meta.url
|
|
28
28
|
? nodePeerLoader(import.meta.url)
|
|
29
29
|
: undefined);
|
|
30
30
|
if (!effectiveLoader)
|
|
@@ -150,6 +150,7 @@ export const transactionalFileStore = async (folder, ordinary, syncDir, options
|
|
|
150
150
|
listKeys: admit(result.listKeys),
|
|
151
151
|
deletePrefix: admit(result.deletePrefix),
|
|
152
152
|
commitPatch: admit(result.commitPatch),
|
|
153
|
+
commitSnapshot: admit(result.commitSnapshot),
|
|
153
154
|
storeReceivedTcToken: admit(result.storeReceivedTcToken),
|
|
154
155
|
touchTcTokenSenderTimestamp: admit(result.touchTcTokenSenderTimestamp)
|
|
155
156
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { atomicCallbacks } from './atomic-store.js';
|
|
1
|
+
import { atomicCallbacks, rowKey, rowPrefix } from './atomic-store.js';
|
|
2
2
|
import { markNativeMemoryStore } from '../Compatibility/internal/native-memory-store.js';
|
|
3
3
|
/**
|
|
4
4
|
* Creates a purely in-memory authentication store.
|
|
@@ -50,6 +50,16 @@ export function useMemoryStore(options = {}) {
|
|
|
50
50
|
staged.set(rawKey, undefined);
|
|
51
51
|
writes.push([rawKey, undefined]);
|
|
52
52
|
return existed;
|
|
53
|
+
},
|
|
54
|
+
deletePrefix(store, prefix) {
|
|
55
|
+
const encodedPrefix = rowPrefix(store, prefix);
|
|
56
|
+
for (const key of staged.keys())
|
|
57
|
+
if (key.startsWith(encodedPrefix))
|
|
58
|
+
rows.delete(key);
|
|
59
|
+
for (const key of stores.get(store)?.keys() ?? []) {
|
|
60
|
+
if (key.startsWith(prefix) && !staged.has(rowKey(store, key)))
|
|
61
|
+
rows.delete(rowKey(store, key));
|
|
62
|
+
}
|
|
53
63
|
}
|
|
54
64
|
};
|
|
55
65
|
change(rows);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oxidezap/baileyrs",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.4.0",
|
|
5
5
|
"description": "A Rust-powered WhatsApp Web library for JavaScript, with a Baileys-compatible API",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"whatsapp",
|
|
@@ -96,9 +96,9 @@
|
|
|
96
96
|
"typecheck:bench": "tsc -p bench/tsconfig.json"
|
|
97
97
|
},
|
|
98
98
|
"dependencies": {
|
|
99
|
-
"@oxidezap/whatsapp-rust-bridge": "0.
|
|
99
|
+
"@oxidezap/whatsapp-rust-bridge": "0.27.1",
|
|
100
100
|
"events": "^3.3.0",
|
|
101
|
-
"fs-native-extensions": "1.5.
|
|
101
|
+
"fs-native-extensions": "1.5.4",
|
|
102
102
|
"long": "^5.3.2"
|
|
103
103
|
},
|
|
104
104
|
"devDependencies": {
|
|
@@ -121,8 +121,8 @@
|
|
|
121
121
|
"typescript-compat-auditor": "npm:typescript@6.0.3",
|
|
122
122
|
"undici": "7.30.2",
|
|
123
123
|
"vite": "8.3.8",
|
|
124
|
-
"vitest": "5.1.
|
|
125
|
-
"wrangler": "4.
|
|
124
|
+
"vitest": "5.1.1",
|
|
125
|
+
"wrangler": "4.166.0"
|
|
126
126
|
},
|
|
127
127
|
"peerDependencies": {
|
|
128
128
|
"@hapi/boom": "^9.1.0 || ^10.0.0",
|