@roeehrl/tinode-sdk 0.25.1-sqlite.1
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/LICENSE +201 -0
- package/README.md +47 -0
- package/package.json +76 -0
- package/src/access-mode.js +567 -0
- package/src/cbuffer.js +244 -0
- package/src/cbuffer.test.js +107 -0
- package/src/comm-error.js +14 -0
- package/src/config.js +71 -0
- package/src/connection.js +537 -0
- package/src/db.js +1021 -0
- package/src/drafty.js +2758 -0
- package/src/drafty.test.js +1600 -0
- package/src/fnd-topic.js +123 -0
- package/src/index.js +29 -0
- package/src/index.native.js +35 -0
- package/src/large-file.js +325 -0
- package/src/me-topic.js +480 -0
- package/src/meta-builder.js +283 -0
- package/src/storage-sqlite.js +1081 -0
- package/src/tinode.js +2382 -0
- package/src/topic.js +2160 -0
- package/src/utils.js +309 -0
- package/src/utils.test.js +456 -0
- package/types/index.d.ts +1227 -0
- package/umd/tinode.dev.js +6856 -0
- package/umd/tinode.dev.js.map +1 -0
- package/umd/tinode.prod.js +2 -0
- package/umd/tinode.prod.js.map +1 -0
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Helper class for constructing {@link Tinode.GetQuery}.
|
|
3
|
+
*
|
|
4
|
+
* @copyright 2015-2023 Tinode LLC.
|
|
5
|
+
*/
|
|
6
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
listToRanges,
|
|
10
|
+
normalizeRanges
|
|
11
|
+
} from './utils';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Helper class for constructing {@link Tinode.GetQuery}.
|
|
15
|
+
*
|
|
16
|
+
* @class MetaGetBuilder
|
|
17
|
+
* @memberof Tinode
|
|
18
|
+
*
|
|
19
|
+
* @param {Tinode.Topic} parent topic which instantiated this builder.
|
|
20
|
+
*/
|
|
21
|
+
export default class MetaGetBuilder {
|
|
22
|
+
constructor(parent) {
|
|
23
|
+
this.topic = parent;
|
|
24
|
+
this.what = {};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Get timestamp of the most recent desc update.
|
|
28
|
+
#get_desc_ims() {
|
|
29
|
+
return this.topic._deleted ? undefined : this.topic.updated;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Get timestamp of the most recent subs update.
|
|
33
|
+
#get_subs_ims() {
|
|
34
|
+
if (this.topic.isP2PType()) {
|
|
35
|
+
return this.#get_desc_ims();
|
|
36
|
+
}
|
|
37
|
+
return this.topic._deleted ? undefined : this.topic._lastSubsUpdate;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Add query parameters to fetch messages within explicit limits.
|
|
41
|
+
* @memberof Tinode.MetaGetBuilder#
|
|
42
|
+
*
|
|
43
|
+
* @param {number=} since - messages newer than this (inclusive);
|
|
44
|
+
* @param {number=} before - older than this (exclusive)
|
|
45
|
+
* @param {number=} limit - number of messages to fetch
|
|
46
|
+
* @returns {Tinode.MetaGetBuilder} <code>this</code> object.
|
|
47
|
+
*/
|
|
48
|
+
withData(since, before, limit) {
|
|
49
|
+
this.what['data'] = {
|
|
50
|
+
since: since,
|
|
51
|
+
before: before,
|
|
52
|
+
limit: limit
|
|
53
|
+
};
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Add query parameters to fetch messages newer than the latest saved message.
|
|
58
|
+
* @memberof Tinode.MetaGetBuilder#
|
|
59
|
+
*
|
|
60
|
+
* @param {number=} limit - number of messages to fetch
|
|
61
|
+
*
|
|
62
|
+
* @returns {Tinode.MetaGetBuilder} <code>this</code> object.
|
|
63
|
+
*/
|
|
64
|
+
withLaterData(limit) {
|
|
65
|
+
return this.withData(this.topic._maxSeq > 0 ? this.topic._maxSeq + 1 : undefined, undefined, limit);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Add query parameters to fetch messages within ID ranges.
|
|
69
|
+
* @memberof Tinode.MetaGetBuilder#
|
|
70
|
+
*
|
|
71
|
+
* @param {Array.<SeqRange>} ranges - ranges of seq IDs to fetch.
|
|
72
|
+
* @param {number=} limit - maximum number of messages to fetch.
|
|
73
|
+
* @returns {Tinode.MetaGetBuilder} <code>this</code> object.
|
|
74
|
+
*/
|
|
75
|
+
withDataRanges(ranges, limit) {
|
|
76
|
+
this.what['data'] = {
|
|
77
|
+
ranges: normalizeRanges(ranges, this.topic._maxSeq),
|
|
78
|
+
limit: limit
|
|
79
|
+
};
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Add query parameters to fetch messages by an array of IDs.
|
|
84
|
+
* @memberof Tinode.MetaGetBuilder#
|
|
85
|
+
*
|
|
86
|
+
* @param {number[]} list - array of seq IDs to fetch.
|
|
87
|
+
* @returns {Tinode.MetaGetBuilder} <code>this</code> object.
|
|
88
|
+
*/
|
|
89
|
+
withDataList(list) {
|
|
90
|
+
return this.withDataRanges(listToRanges(list));
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Add query parameters to fetch messages older than the earliest saved message.
|
|
94
|
+
* @memberof Tinode.MetaGetBuilder#
|
|
95
|
+
*
|
|
96
|
+
* @param {number=} limit - maximum number of messages to fetch.
|
|
97
|
+
*
|
|
98
|
+
* @returns {Tinode.MetaGetBuilder} <code>this</code> object.
|
|
99
|
+
*/
|
|
100
|
+
withEarlierData(limit) {
|
|
101
|
+
return this.withData(undefined, this.topic._minSeq > 0 ? this.topic._minSeq : undefined, limit);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Add query parameters to fetch topic description if it's newer than the given timestamp.
|
|
105
|
+
* @memberof Tinode.MetaGetBuilder#
|
|
106
|
+
*
|
|
107
|
+
* @param {Date=} ims - fetch messages newer than this timestamp.
|
|
108
|
+
*
|
|
109
|
+
* @returns {Tinode.MetaGetBuilder} <code>this</code> object.
|
|
110
|
+
*/
|
|
111
|
+
withDesc(ims) {
|
|
112
|
+
this.what['desc'] = {
|
|
113
|
+
ims: ims
|
|
114
|
+
};
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Add query parameters to fetch topic description if it's newer than the last update.
|
|
119
|
+
* @memberof Tinode.MetaGetBuilder#
|
|
120
|
+
*
|
|
121
|
+
* @returns {Tinode.MetaGetBuilder} <code>this</code> object.
|
|
122
|
+
*/
|
|
123
|
+
withLaterDesc() {
|
|
124
|
+
return this.withDesc(this.#get_desc_ims());
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Add query parameters to fetch subscriptions.
|
|
128
|
+
* @memberof Tinode.MetaGetBuilder#
|
|
129
|
+
*
|
|
130
|
+
* @param {Date=} ims - fetch subscriptions modified more recently than this timestamp
|
|
131
|
+
* @param {number=} limit - maximum number of subscriptions to fetch.
|
|
132
|
+
* @param {string=} userOrTopic - user ID or topic name to fetch for fetching one subscription.
|
|
133
|
+
*
|
|
134
|
+
* @returns {Tinode.MetaGetBuilder} <code>this</code> object.
|
|
135
|
+
*/
|
|
136
|
+
withSub(ims, limit, userOrTopic) {
|
|
137
|
+
const opts = {
|
|
138
|
+
ims: ims,
|
|
139
|
+
limit: limit
|
|
140
|
+
};
|
|
141
|
+
if (this.topic.getType() == 'me') {
|
|
142
|
+
opts.topic = userOrTopic;
|
|
143
|
+
} else {
|
|
144
|
+
opts.user = userOrTopic;
|
|
145
|
+
}
|
|
146
|
+
this.what['sub'] = opts;
|
|
147
|
+
return this;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Add query parameters to fetch a single subscription.
|
|
151
|
+
* @memberof Tinode.MetaGetBuilder#
|
|
152
|
+
*
|
|
153
|
+
* @param {Date=} ims - fetch subscriptions modified more recently than this timestamp
|
|
154
|
+
* @param {string=} userOrTopic - user ID or topic name to fetch for fetching one subscription.
|
|
155
|
+
*
|
|
156
|
+
* @returns {Tinode.MetaGetBuilder} <code>this</code> object.
|
|
157
|
+
*/
|
|
158
|
+
withOneSub(ims, userOrTopic) {
|
|
159
|
+
return this.withSub(ims, undefined, userOrTopic);
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Add query parameters to fetch a single subscription if it's been updated since the last update.
|
|
163
|
+
* @memberof Tinode.MetaGetBuilder#
|
|
164
|
+
*
|
|
165
|
+
* @param {string=} userOrTopic - user ID or topic name to fetch for fetching one subscription.
|
|
166
|
+
*
|
|
167
|
+
* @returns {Tinode.MetaGetBuilder} <code>this</code> object.
|
|
168
|
+
*/
|
|
169
|
+
withLaterOneSub(userOrTopic) {
|
|
170
|
+
return this.withOneSub(this.topic._lastSubsUpdate, userOrTopic);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Add query parameters to fetch subscriptions updated since the last update.
|
|
174
|
+
* @memberof Tinode.MetaGetBuilder#
|
|
175
|
+
*
|
|
176
|
+
* @param {number=} limit - maximum number of subscriptions to fetch.
|
|
177
|
+
*
|
|
178
|
+
* @returns {Tinode.MetaGetBuilder} <code>this</code> object.
|
|
179
|
+
*/
|
|
180
|
+
withLaterSub(limit) {
|
|
181
|
+
return this.withSub(this.#get_subs_ims(), limit);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Add query parameters to fetch topic tags.
|
|
185
|
+
* @memberof Tinode.MetaGetBuilder#
|
|
186
|
+
*
|
|
187
|
+
* @returns {Tinode.MetaGetBuilder} <code>this</code> object.
|
|
188
|
+
*/
|
|
189
|
+
withTags() {
|
|
190
|
+
this.what['tags'] = true;
|
|
191
|
+
return this;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Add query parameters to fetch user's credentials. <code>'me'</code> topic only.
|
|
195
|
+
* @memberof Tinode.MetaGetBuilder#
|
|
196
|
+
*
|
|
197
|
+
* @returns {Tinode.MetaGetBuilder} <code>this</code> object.
|
|
198
|
+
*/
|
|
199
|
+
withCred() {
|
|
200
|
+
if (this.topic.getType() == 'me') {
|
|
201
|
+
this.what['cred'] = true;
|
|
202
|
+
} else {
|
|
203
|
+
this.topic._tinode.logger("ERROR: Invalid topic type for MetaGetBuilder:withCreds", this.topic.getType());
|
|
204
|
+
}
|
|
205
|
+
return this;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Add query parameters to fetch topic tags.
|
|
209
|
+
* @memberof Tinode.MetaGetBuilder#
|
|
210
|
+
*
|
|
211
|
+
* @returns {Tinode.MetaGetBuilder} <code>this</code> object.
|
|
212
|
+
*/
|
|
213
|
+
withAux() {
|
|
214
|
+
this.what['aux'] = true;
|
|
215
|
+
return this;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Add query parameters to fetch deleted messages within explicit limits. Any/all parameters can be null.
|
|
219
|
+
* @memberof Tinode.MetaGetBuilder#
|
|
220
|
+
*
|
|
221
|
+
* @param {number=} since - ids of messages deleted since this 'del' id (inclusive)
|
|
222
|
+
* @param {number=} limit - number of deleted message ids to fetch
|
|
223
|
+
*
|
|
224
|
+
* @returns {Tinode.MetaGetBuilder} <code>this</code> object.
|
|
225
|
+
*/
|
|
226
|
+
withDel(since, limit) {
|
|
227
|
+
if (since || limit) {
|
|
228
|
+
this.what['del'] = {
|
|
229
|
+
since: since,
|
|
230
|
+
limit: limit
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
return this;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Add query parameters to fetch messages deleted after the saved <code>'del'</code> id.
|
|
237
|
+
* @memberof Tinode.MetaGetBuilder#
|
|
238
|
+
*
|
|
239
|
+
* @param {number=} limit - number of deleted message ids to fetch
|
|
240
|
+
*
|
|
241
|
+
* @returns {Tinode.MetaGetBuilder} <code>this</code> object.
|
|
242
|
+
*/
|
|
243
|
+
withLaterDel(limit) {
|
|
244
|
+
// Specify 'since' only if we have already received some messages. If
|
|
245
|
+
// we have no locally cached messages then we don't care if any messages were deleted.
|
|
246
|
+
return this.withDel(this.topic._maxSeq > 0 ? this.topic._maxDel + 1 : undefined, limit);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Extract subquery: get an object that contains specified subquery.
|
|
251
|
+
* @memberof Tinode.MetaGetBuilder#
|
|
252
|
+
* @param {string} what - subquery to return: one of 'data', 'sub', 'desc', 'tags', 'cred', 'del'.
|
|
253
|
+
* @returns {Object} requested subquery or <code>undefined</code>.
|
|
254
|
+
*/
|
|
255
|
+
extract(what) {
|
|
256
|
+
return this.what[what];
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Construct parameters.
|
|
261
|
+
* @memberof Tinode.MetaGetBuilder#
|
|
262
|
+
*
|
|
263
|
+
* @returns {Tinode.GetQuery} Get query
|
|
264
|
+
*/
|
|
265
|
+
build() {
|
|
266
|
+
const what = [];
|
|
267
|
+
let params = {};
|
|
268
|
+
['data', 'sub', 'desc', 'tags', 'cred', 'aux', 'del'].forEach((key) => {
|
|
269
|
+
if (this.what.hasOwnProperty(key)) {
|
|
270
|
+
what.push(key);
|
|
271
|
+
if (Object.getOwnPropertyNames(this.what[key]).length > 0) {
|
|
272
|
+
params[key] = this.what[key];
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
if (what.length > 0) {
|
|
277
|
+
params.what = what.join(' ');
|
|
278
|
+
} else {
|
|
279
|
+
params = undefined;
|
|
280
|
+
}
|
|
281
|
+
return params;
|
|
282
|
+
}
|
|
283
|
+
}
|