-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbson.ts
96 lines (82 loc) · 2.87 KB
/
bson.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import * as bson from 'bson';
import { ParameterLookup, SqliteJsonValue } from '@powersync/service-sync-rules';
import { ReplicaId } from './BucketStorageBatch.js';
type NodeBuffer = Buffer<ArrayBuffer>;
/**
* Use for internal (bucket storage) data, where we control each field.
*/
export const BSON_DESERIALIZE_INTERNAL_OPTIONS: bson.DeserializeOptions = {
// use bigint instead of Long
useBigInt64: true
};
/**
* Use for data from external sources, which could contain arbitrary fields.
*/
export const BSON_DESERIALIZE_DATA_OPTIONS: bson.DeserializeOptions = {
useBigInt64: true
};
/**
* Lookup serialization must be number-agnostic. I.e. normalize numbers, instead of preserving numbers.
* @param lookup
*/
export const serializeLookupBuffer = (lookup: ParameterLookup): NodeBuffer => {
return bson.serialize({ l: lookup.values }) as NodeBuffer;
};
export const serializeLookup = (lookup: ParameterLookup) => {
return new bson.Binary(serializeLookupBuffer(lookup));
};
export const deserializeParameterLookup = (lookup: bson.Binary) => {
const parsed = bson.deserialize(lookup.buffer, BSON_DESERIALIZE_INTERNAL_OPTIONS).l as SqliteJsonValue[];
return parsed;
};
export const getLookupBucketDefinitionName = (lookup: bson.Binary) => {
const parsed = deserializeParameterLookup(lookup);
return parsed[0] as string;
};
/**
* True if this is a bson.UUID.
*
* Works even with multiple copies of the bson package.
*/
export const isUUID = (value: any): value is bson.UUID => {
if (value == null || typeof value != 'object') {
return false;
}
const uuid = value as bson.UUID;
return uuid._bsontype == 'Binary' && uuid.sub_type == bson.Binary.SUBTYPE_UUID;
};
export const serializeReplicaId = (id: ReplicaId): NodeBuffer => {
return bson.serialize({ id }) as NodeBuffer;
};
export const deserializeReplicaId = (id: Buffer): ReplicaId => {
const deserialized = deserializeBson(id);
return deserialized.id;
};
/**
* Deserialize BSON - can be used for BSON containing arbitrary user data.
*/
export const deserializeBson = (buffer: Uint8Array): bson.Document => {
return bson.deserialize(buffer, BSON_DESERIALIZE_DATA_OPTIONS);
};
export const serializeBson = (document: any): NodeBuffer => {
return bson.serialize(document) as NodeBuffer;
};
/**
* Returns true if two ReplicaId values are the same (serializes to the same BSON value).
*/
export const replicaIdEquals = (a: ReplicaId, b: ReplicaId) => {
if (a === b) {
return true;
} else if (typeof a == 'string' && typeof b == 'string') {
return a == b;
} else if (isUUID(a) && isUUID(b)) {
return a.equals(b);
} else if (a == null && b == null) {
return true;
} else if ((b == null && a != null) || (a == null && b != null)) {
return false;
} else {
// There are many possible primitive values, this covers them all
return serializeReplicaId(a).equals(serializeReplicaId(b));
}
};