@secrecy/lib 1.7.0 → 1.8.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.
Files changed (152) hide show
  1. package/LICENSE +1 -1
  2. package/dist/lib/base-client.js +91 -0
  3. package/dist/lib/cache.js +4 -0
  4. package/dist/lib/client/SecrecryCareClient.js +19 -0
  5. package/dist/lib/client/SecrecyAppClient.js +87 -0
  6. package/dist/lib/client/SecrecyCloudClient.js +475 -0
  7. package/dist/lib/client/SecrecyDbClient.js +10 -0
  8. package/dist/lib/client/SecrecyMailClient.js +283 -0
  9. package/dist/lib/client/SecrecyPayClient.js +34 -0
  10. package/dist/lib/client/SecrecyUserClient.js +27 -0
  11. package/dist/lib/client/SecrecyWalletClient.js +50 -0
  12. package/dist/lib/client/convert/file.js +29 -0
  13. package/dist/lib/client/convert/mail.js +42 -0
  14. package/dist/lib/client/convert/node.js +100 -0
  15. package/dist/lib/client/helpers.js +103 -0
  16. package/dist/lib/client/index.js +49 -0
  17. package/dist/lib/client/storage.js +7 -0
  18. package/dist/lib/client/types/app.js +1 -0
  19. package/dist/lib/client/types/file.js +1 -0
  20. package/dist/lib/client/types/index.js +15 -0
  21. package/dist/lib/client/types/mail.js +1 -0
  22. package/dist/lib/client/types/node.js +1 -0
  23. package/dist/lib/client/types/user.js +1 -0
  24. package/dist/lib/client.js +47 -0
  25. package/dist/lib/crypto/file.js +184 -0
  26. package/dist/lib/crypto/index.js +41 -0
  27. package/dist/lib/error/client.js +10 -0
  28. package/dist/lib/error/index.js +10 -0
  29. package/dist/lib/error/server.js +27 -0
  30. package/dist/lib/index.js +10 -0
  31. package/dist/lib/minify/index.js +23 -0
  32. package/dist/lib/minify/lz4.js +517 -0
  33. package/dist/lib/sodium.js +5 -0
  34. package/dist/lib/types.js +1 -0
  35. package/dist/lib/utils/array.js +25 -0
  36. package/dist/lib/utils/base64.js +2 -0
  37. package/dist/lib/utils/popup-tools.js +180 -0
  38. package/dist/lib/utils/promise.js +20 -0
  39. package/dist/lib/utils/store-buddy.js +60 -0
  40. package/dist/lib/utils/time.js +13 -0
  41. package/dist/lib/worker/md5.js +18 -0
  42. package/dist/lib/worker/sodium.js +95 -0
  43. package/dist/lib/worker/workerCodes.js +254 -0
  44. package/dist/types/base-client.d.ts +28 -0
  45. package/dist/types/cache.d.ts +11 -0
  46. package/dist/types/client/SecrecryCareClient.d.ts +9 -0
  47. package/dist/types/client/SecrecyAppClient.d.ts +20 -0
  48. package/dist/types/client/SecrecyCloudClient.d.ts +88 -0
  49. package/dist/types/client/SecrecyDbClient.d.ts +7 -0
  50. package/dist/types/client/SecrecyMailClient.d.ts +42 -0
  51. package/dist/types/client/SecrecyPayClient.d.ts +27 -0
  52. package/dist/types/client/SecrecyUserClient.d.ts +14 -0
  53. package/dist/{client → types/client}/SecrecyWalletClient.d.ts +9 -12
  54. package/dist/types/client/convert/file.d.ts +4 -0
  55. package/dist/types/client/convert/mail.d.ts +8 -0
  56. package/dist/types/client/convert/node.d.ts +7 -0
  57. package/dist/types/client/helpers.d.ts +26 -0
  58. package/dist/types/client/index.d.ts +29 -0
  59. package/dist/{client → types/client}/storage.d.ts +2 -2
  60. package/dist/types/client/types/app.d.ts +2 -0
  61. package/dist/types/client/types/file.d.ts +6 -0
  62. package/dist/types/client/types/index.d.ts +46 -0
  63. package/dist/types/client/types/mail.d.ts +87 -0
  64. package/dist/{client/types/Node.d.ts → types/client/types/node.d.ts} +20 -25
  65. package/dist/types/client/types/user.d.ts +3 -0
  66. package/dist/types/client.d.ts +12138 -0
  67. package/dist/{crypto → types/crypto}/file.d.ts +4 -4
  68. package/dist/{crypto → types/crypto}/index.d.ts +1 -4
  69. package/dist/types/error/client.d.ts +22 -0
  70. package/dist/types/error/index.d.ts +40 -0
  71. package/dist/types/error/server.d.ts +26 -0
  72. package/dist/types/index.d.ts +13 -0
  73. package/dist/{minify → types/minify}/index.d.ts +1 -1
  74. package/dist/types/minify/lz4.d.ts +35 -0
  75. package/dist/{sodium.d.ts → types/sodium.d.ts} +1 -1
  76. package/dist/types/types.d.ts +10 -0
  77. package/dist/{utils/utils.d.ts → types/utils/array.d.ts} +0 -1
  78. package/dist/types/utils/base64.d.ts +1 -0
  79. package/dist/types/utils/popup-tools.d.ts +52 -0
  80. package/dist/types/utils/promise.d.ts +1 -0
  81. package/dist/types/utils/store-buddy.d.ts +130 -0
  82. package/dist/{worker → types/worker}/sodium.d.ts +1 -1
  83. package/package.json +48 -73
  84. package/dist/BaseClient.d.ts +0 -111
  85. package/dist/BaseClient.js +0 -506
  86. package/dist/PopupTools.d.ts +0 -17
  87. package/dist/PopupTools.js +0 -195
  88. package/dist/ZeusThunder.d.ts +0 -2
  89. package/dist/ZeusThunder.js +0 -65
  90. package/dist/cache.d.ts +0 -6
  91. package/dist/cache.js +0 -4
  92. package/dist/client/SecrecyAppClient.d.ts +0 -17
  93. package/dist/client/SecrecyAppClient.js +0 -226
  94. package/dist/client/SecrecyCloudClient.d.ts +0 -89
  95. package/dist/client/SecrecyCloudClient.js +0 -1405
  96. package/dist/client/SecrecyDbClient.d.ts +0 -48
  97. package/dist/client/SecrecyDbClient.js +0 -419
  98. package/dist/client/SecrecyMailClient.d.ts +0 -42
  99. package/dist/client/SecrecyMailClient.js +0 -1022
  100. package/dist/client/SecrecyPayClient.d.ts +0 -28
  101. package/dist/client/SecrecyPayClient.js +0 -68
  102. package/dist/client/SecrecyWalletClient.js +0 -73
  103. package/dist/client/convert/file.d.ts +0 -5
  104. package/dist/client/convert/file.js +0 -33
  105. package/dist/client/convert/mail.d.ts +0 -3
  106. package/dist/client/convert/mail.js +0 -42
  107. package/dist/client/convert/node.d.ts +0 -9
  108. package/dist/client/convert/node.js +0 -87
  109. package/dist/client/helpers.d.ts +0 -28
  110. package/dist/client/helpers.js +0 -119
  111. package/dist/client/index.d.ts +0 -34
  112. package/dist/client/index.js +0 -46
  113. package/dist/client/storage.js +0 -12
  114. package/dist/client/types/File.d.ts +0 -14
  115. package/dist/client/types/File.js +0 -3
  116. package/dist/client/types/Inputs.d.ts +0 -16
  117. package/dist/client/types/Inputs.js +0 -3
  118. package/dist/client/types/Node.js +0 -3
  119. package/dist/client/types/UserAppNotifications.d.ts +0 -6
  120. package/dist/client/types/UserAppNotifications.js +0 -3
  121. package/dist/client/types/UserAppSettings.d.ts +0 -5
  122. package/dist/client/types/UserAppSettings.js +0 -3
  123. package/dist/client/types/index.d.ts +0 -120
  124. package/dist/client/types/index.js +0 -8
  125. package/dist/client/types/selectors.d.ts +0 -400
  126. package/dist/client/types/selectors.js +0 -135
  127. package/dist/crypto/file.js +0 -195
  128. package/dist/crypto/index.js +0 -45
  129. package/dist/error.d.ts +0 -33
  130. package/dist/error.js +0 -3
  131. package/dist/index.d.ts +0 -14
  132. package/dist/index.js +0 -10
  133. package/dist/minify/index.js +0 -23
  134. package/dist/minify/lz4.d.ts +0 -5
  135. package/dist/minify/lz4.js +0 -539
  136. package/dist/sodium.js +0 -6
  137. package/dist/utils/encoders.d.ts +0 -26
  138. package/dist/utils/encoders.js +0 -18
  139. package/dist/utils/store-buddy.d.ts +0 -14
  140. package/dist/utils/store-buddy.js +0 -58
  141. package/dist/utils/time.js +0 -12
  142. package/dist/utils/utils.js +0 -47
  143. package/dist/worker/md5.js +0 -24
  144. package/dist/worker/sodium.js +0 -118
  145. package/dist/worker/workerCodes.js +0 -255
  146. package/dist/zeus/const.d.ts +0 -7
  147. package/dist/zeus/const.js +0 -1846
  148. package/dist/zeus/index.d.ts +0 -8721
  149. package/dist/zeus/index.js +0 -642
  150. /package/dist/{utils → types/utils}/time.d.ts +0 -0
  151. /package/dist/{worker → types/worker}/md5.d.ts +0 -0
  152. /package/dist/{worker → types/worker}/workerCodes.d.ts +0 -0
@@ -0,0 +1,517 @@
1
+ // lz4-ts @license BSD-3-Clause / Copyright (c) 2015, Pierre Curto / 2016, oov. All rights reserved.
2
+ /**
3
+ * Copyright (c) 2015, Pierre Curto
4
+ * Copyright (c) 2016, oov
5
+ * All rights reserved.
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions are met:
9
+ *
10
+ * * Redistributions of source code must retain the above copyright notice, this
11
+ * list of conditions and the following disclaimer.
12
+ *
13
+ * * Redistributions in binary form must reproduce the above copyright notice,
14
+ * this list of conditions and the following disclaimer in the documentation
15
+ * and/or other materials provided with the distribution.
16
+ *
17
+ * * Neither the name of xxHash nor the names of its
18
+ * contributors may be used to endorse or promote products derived from
19
+ * this software without specific prior written permission.
20
+ *
21
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+ const errInvalidSource = new Error('invalid source');
33
+ const errShortBuffer = new Error('short buffer');
34
+ // T he following constants are used to setup the compression algorithm.
35
+ const minMatch = 4; // th e minimum size of the match sequence size (4 bytes)
36
+ const winSizeLog = 16; // LZ4 64Kb window size limit
37
+ const winSize = 1 << winSizeLog;
38
+ const winMask = winSize - 1; // 64Kb window of previous data for dependent blocks
39
+ // hashS izeLog determines the size of the hash table used to quickly find a previous match position.
40
+ // Its value influences the compression speed and memory usage, the lower the faster,
41
+ // but a t the expense of the compression ratio.
42
+ // 16 se ems to be the best compromise.
43
+ const hashSizeLog = 16;
44
+ const hashSize = 1 << hashSizeLog;
45
+ const hashShift = minMatch * 8 - hashSizeLog;
46
+ const mfLimit = 8 + minMatch; // The last match cannot start within the last 12 bytes.
47
+ const skipStrength = 6; // variable s tep for fast scan
48
+ const hasher = 2654435761 | 0; // prime number used to hash minMatch
49
+ // https://dev eloper.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Math/imul#Polyfill
50
+ function imulPolyfill(a, b) {
51
+ const ah = (a >>> 16) & 0xffff;
52
+ const al = a & 0xffff;
53
+ const bh = (b >>> 16) & 0xffff;
54
+ const bl = b & 0xffff;
55
+ // the shift by 0 fixes the sign on the high part
56
+ // the final |0 converts the unsigned value into a signed value
57
+ return (al * bl + (((ah * bl + al * bh) << 16) >>> 0)) | 0;
58
+ }
59
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
60
+ const imul = Math.imul ? Math.imul : imulPolyfill;
61
+ function getUint32(a, i) {
62
+ return a[i + 3] | (a[i + 2] << 8) | (a[i + 1] << 16) | (a[i] << 24);
63
+ }
64
+ function copy(dest, src, di, si, len) {
65
+ for (let i = 0; i < len; ++i) {
66
+ dest[di++] = src[si++];
67
+ }
68
+ }
69
+ export function calcUncompressedLen(src) {
70
+ const sn = src.length;
71
+ if (sn === 0) {
72
+ return 0;
73
+ }
74
+ let si = 0;
75
+ let di = 0;
76
+ while (true) {
77
+ // literals and match lengths (token)
78
+ let lLen = src[si] >> 4;
79
+ let mLen = src[si] & 0xf;
80
+ if (++si === sn) {
81
+ throw errInvalidSource;
82
+ }
83
+ // literals
84
+ if (lLen > 0) {
85
+ if (lLen === 0xf) {
86
+ while (src[si] === 0xff) {
87
+ lLen += 0xff;
88
+ if (++si === sn) {
89
+ throw errInvalidSource;
90
+ }
91
+ }
92
+ lLen += src[si];
93
+ if (++si === sn) {
94
+ throw errInvalidSource;
95
+ }
96
+ }
97
+ di += lLen;
98
+ si += lLen;
99
+ if (si >= sn) {
100
+ return di;
101
+ }
102
+ }
103
+ si += 2;
104
+ if (si >= sn) {
105
+ throw errInvalidSource;
106
+ }
107
+ const offset = src[si - 2] | (src[si - 1] << 8);
108
+ if (di - offset < 0 || offset === 0) {
109
+ throw errInvalidSource;
110
+ }
111
+ // match
112
+ if (mLen === 0xf) {
113
+ while (src[si] === 0xff) {
114
+ mLen += 0xff;
115
+ if (++si === sn) {
116
+ throw errInvalidSource;
117
+ }
118
+ }
119
+ mLen += src[si];
120
+ if (++si === sn) {
121
+ throw errInvalidSource;
122
+ }
123
+ }
124
+ // minimum match length is 4
125
+ mLen += 4;
126
+ // copy the match (NB. match is at least 4 bytes long)
127
+ for (; mLen >= offset; mLen -= offset) {
128
+ di += offset;
129
+ }
130
+ di += mLen;
131
+ }
132
+ }
133
+ export function uncompressBlock(src, dest) {
134
+ const sn = src.length;
135
+ const dn = dest.length;
136
+ if (sn === 0) {
137
+ return 0;
138
+ }
139
+ let si = 0;
140
+ let di = 0;
141
+ while (true) {
142
+ // literals and match lengths (token)
143
+ let lLen = src[si] >> 4;
144
+ let mLen = src[si] & 0xf;
145
+ if (++si === sn) {
146
+ throw errInvalidSource;
147
+ }
148
+ // literals
149
+ if (lLen > 0) {
150
+ if (lLen === 0xf) {
151
+ while (src[si] === 0xff) {
152
+ lLen += 0xff;
153
+ if (++si === sn) {
154
+ throw errInvalidSource;
155
+ }
156
+ }
157
+ lLen += src[si];
158
+ if (++si === sn) {
159
+ throw errInvalidSource;
160
+ }
161
+ }
162
+ if (dn - di < lLen || si + lLen > sn) {
163
+ throw errShortBuffer;
164
+ }
165
+ copy(dest, src, di, si, lLen);
166
+ di += lLen;
167
+ si += lLen;
168
+ if (si >= sn) {
169
+ return di;
170
+ }
171
+ }
172
+ si += 2;
173
+ if (si >= sn) {
174
+ throw errInvalidSource;
175
+ }
176
+ const offset = src[si - 2] | (src[si - 1] << 8);
177
+ if (di - offset < 0 || offset === 0) {
178
+ throw errInvalidSource;
179
+ }
180
+ // match
181
+ if (mLen === 0xf) {
182
+ while (src[si] === 0xff) {
183
+ mLen += 0xff;
184
+ if (++si === sn) {
185
+ throw errInvalidSource;
186
+ }
187
+ }
188
+ mLen += src[si];
189
+ if (++si === sn) {
190
+ throw errInvalidSource;
191
+ }
192
+ }
193
+ // minimum match length is 4
194
+ mLen += 4;
195
+ if (dn - di <= mLen) {
196
+ throw errShortBuffer;
197
+ }
198
+ // copy the match (NB. match is at least 4 bytes long)
199
+ for (; mLen >= offset; mLen -= offset) {
200
+ copy(dest, dest, di, di - offset, offset);
201
+ di += offset;
202
+ }
203
+ copy(dest, dest, di, di - offset, mLen);
204
+ di += mLen;
205
+ }
206
+ }
207
+ export function compressBlockBound(n) {
208
+ return n + ((n / 255) | 0) + 16;
209
+ }
210
+ export function compressBlock(src, dest, soffset) {
211
+ const sn = src.length - mfLimit;
212
+ const dn = dest.length;
213
+ if (sn <= 0 || dn === 0 || soffset >= sn) {
214
+ return 0;
215
+ }
216
+ let si = 0;
217
+ let di = 0;
218
+ // fast scan strategy:
219
+ // we only need a hash table to store the last sequences (4 bytes)
220
+ const hashTable = new Uint32Array(hashSize);
221
+ // Initialise the hash table with the first 64Kb of the input buffer
222
+ // (used when compressing dependent blocks)
223
+ while (si < soffset) {
224
+ const h = imul(getUint32(src, si), hasher) >>> hashShift;
225
+ hashTable[h] = ++si;
226
+ }
227
+ let anchor = si;
228
+ let fma = 1 << skipStrength;
229
+ while (si < sn - minMatch) {
230
+ // hash the next 4 bytes (sequence)...
231
+ const h = imul(getUint32(src, si), hasher) >>> hashShift;
232
+ // -1 to separate existing entries from new ones
233
+ const ref = hashTable[h] - 1;
234
+ // . ..and store the position of the hash in the hash table (+1 to compensate the -1 upon saving)
235
+ hashTable[h] = si + 1;
236
+ // no need to check the last 3 bytes in the first literal 4 bytes as
237
+ // this guarantees that the next match, if any, is compressed with
238
+ // a lower size, since to have some compression we must have:
239
+ // ll+ml-overlap > 1 + (ll -15)/255 + (ml-4-15)/255 + 2 (uncompressed size>compressed size)
240
+ // => ll+ml>3+2*overlap => ll+ml>= 4+2*overlap
241
+ // and by definition we do have:
242
+ // l l >= 1, ml >= 4
243
+ // => ll+ml >= 5
244
+ // => so overlap must be 0
245
+ // the sequence is new, out of bound (6 4kb) or not valid: tr y next sequence
246
+ if (ref < 0 || // (fma & ((1 << skipStrength) - 1)) < 4 || // this code seems has a big penalty for size...
247
+ (si - ref) >> winSizeLog > 0 ||
248
+ src[ref] !== src[si] ||
249
+ src[ref + 1] !== src[si + 1] ||
250
+ src[ref + 2] !== src[si + 2] ||
251
+ src[ref + 3] !== src[si + 3]) {
252
+ // variable step: improves performance on non-compressible data
253
+ si += fma >> skipStrength;
254
+ ++fma;
255
+ continue;
256
+ }
257
+ // match found
258
+ fma = 1 << skipStrength;
259
+ const lLen = si - anchor;
260
+ const offset = si - ref;
261
+ // encode match length part 1
262
+ si += minMatch;
263
+ let mLen = si; // match length has minMatch al ready
264
+ while (si <= sn && src[si] === src[si - offset]) {
265
+ si++;
266
+ }
267
+ mLen = si - mLen;
268
+ if (mLen < 0xf) {
269
+ dest[di] = mLen;
270
+ }
271
+ else {
272
+ dest[di] = 0xf;
273
+ }
274
+ // encode literals length
275
+ if (lLen < 0xf) {
276
+ dest[di] |= lLen << 4;
277
+ }
278
+ else {
279
+ dest[di] |= 0xf0;
280
+ if (++di === dn) {
281
+ throw errShortBuffer;
282
+ }
283
+ let l = lLen - 0xf;
284
+ for (; l >= 0xff; l -= 0xff) {
285
+ dest[di] = 0xff;
286
+ if (++di === dn) {
287
+ throw errShortBuffer;
288
+ }
289
+ }
290
+ dest[di] = l & 0xff;
291
+ }
292
+ if (++di === dn) {
293
+ throw errShortBuffer;
294
+ }
295
+ // literals
296
+ if (di + lLen >= dn) {
297
+ throw errShortBuffer;
298
+ }
299
+ copy(dest, src, di, anchor, lLen);
300
+ di += lLen;
301
+ anchor = si;
302
+ // encode offset
303
+ di += 2;
304
+ if (di >= dn) {
305
+ throw errShortBuffer;
306
+ }
307
+ dest[di - 2] = offset;
308
+ dest[di - 1] = offset >> 8;
309
+ // encode match length part 2
310
+ if (mLen >= 0xf) {
311
+ for (mLen -= 0xf; mLen >= 0xff; mLen -= 0xff) {
312
+ dest[di] = 0xff;
313
+ if (++di === dn) {
314
+ throw errShortBuffer;
315
+ }
316
+ }
317
+ dest[di] = mLen;
318
+ if (++di === dn) {
319
+ throw errShortBuffer;
320
+ }
321
+ }
322
+ }
323
+ if (anchor === 0) {
324
+ // incompressible
325
+ return 0;
326
+ }
327
+ // last literals
328
+ let lLen = src.length - anchor;
329
+ if (lLen < 0xf) {
330
+ dest[di] = lLen << 4;
331
+ }
332
+ else {
333
+ dest[di] = 0xf0;
334
+ if (++di === dn) {
335
+ throw errShortBuffer;
336
+ }
337
+ for (lLen -= 0xf; lLen >= 0xff; lLen -= 0xff) {
338
+ dest[di] = 0xff;
339
+ if (++di === dn) {
340
+ throw errShortBuffer;
341
+ }
342
+ }
343
+ dest[di] = lLen;
344
+ }
345
+ if (++di === dn) {
346
+ throw errShortBuffer;
347
+ }
348
+ // write literals
349
+ const lastLen = src.length - anchor;
350
+ const n = di + lastLen;
351
+ if (n > dn) {
352
+ throw errShortBuffer;
353
+ }
354
+ else if (n >= sn) {
355
+ // incompressible
356
+ return 0;
357
+ }
358
+ copy(dest, src, di, anchor, lastLen);
359
+ di += lastLen;
360
+ return di;
361
+ }
362
+ export function compressBlockHC(src, dest, soffset) {
363
+ const sn = src.length - mfLimit;
364
+ const dn = dest.length;
365
+ if (sn <= 0 || dn === 0 || soffset >= sn) {
366
+ return 0;
367
+ }
368
+ let si = 0;
369
+ let di = 0;
370
+ // Hash Chain strategy:
371
+ // we need a hash table and a chain table
372
+ // the chain table cannot contain more entries than the window size (64Kb entries)
373
+ const hashTable = new Uint32Array(hashSize);
374
+ const chainTable = new Uint32Array(winSize);
375
+ // Initialise the hash table with the first 64Kb of the input buffer
376
+ // (used when compressing dependent blocks)
377
+ while (si < soffset) {
378
+ const h = imul(getUint32(src, si), hasher) >>> hashShift;
379
+ chainTable[si & winMask] = hashTable[h];
380
+ hashTable[h] = ++si;
381
+ }
382
+ let anchor = si;
383
+ while (si < sn - minMatch) {
384
+ // hash the next 4 bytes (sequence)...
385
+ const h = imul(getUint32(src, si), hasher) >>> hashShift;
386
+ // follow the chain until out of window and g ive the longest match
387
+ let mLen = 0;
388
+ let offset = 0;
389
+ for (let next = hashTable[h] - 1; next > 0 && next > si - winSize; next = chainTable[next & winMask] - 1) {
390
+ // the first (mLen==0) or next byte (mLen>=minMatch) at current match length must match to improve on the match length
391
+ if (src[next + mLen] === src[si + mLen]) {
392
+ for (let ml = 0;; ++ml) {
393
+ if (src[next + ml] !== src[si + ml] || si + ml > sn) {
394
+ // found a longer match, keep its position and length
395
+ if (mLen < ml && ml >= minMatch) {
396
+ mLen = ml;
397
+ offset = si - next;
398
+ }
399
+ break;
400
+ }
401
+ }
402
+ }
403
+ }
404
+ chainTable[si & winMask] = hashTable[h];
405
+ hashTable[h] = si + 1;
406
+ // no match found
407
+ if (mLen === 0) {
408
+ ++si;
409
+ continue;
410
+ }
411
+ let si2 = si + 1;
412
+ const ml = si + mLen;
413
+ // match found
414
+ // update hash/ chain tables with overlaping bytes:
415
+ // si already hashed, add everything from si+1 up to the match length
416
+ while (si2 < ml) {
417
+ const h = imul(getUint32(src, si2), hasher) >>> hashShift;
418
+ chainTable[si2 & winMask] = hashTable[h];
419
+ hashTable[h] = ++si2;
420
+ }
421
+ const lLen = si - anchor;
422
+ si += mLen;
423
+ mLen -= minMatch; // match length does not include minMatch
424
+ if (mLen < 0xf) {
425
+ dest[di] = mLen;
426
+ }
427
+ else {
428
+ dest[di] = 0xf;
429
+ }
430
+ // encode literals length
431
+ if (lLen < 0xf) {
432
+ dest[di] |= lLen << 4;
433
+ }
434
+ else {
435
+ dest[di] |= 0xf0;
436
+ if (++di === dn) {
437
+ throw errShortBuffer;
438
+ }
439
+ let l = lLen - 0xf;
440
+ for (; l >= 0xff; l -= 0xff) {
441
+ dest[di] = 0xff;
442
+ if (++di === dn) {
443
+ throw errShortBuffer;
444
+ }
445
+ }
446
+ dest[di] = l & 0xff;
447
+ }
448
+ if (++di === dn) {
449
+ throw errShortBuffer;
450
+ }
451
+ // literals
452
+ if (di + lLen >= dn) {
453
+ throw errShortBuffer;
454
+ }
455
+ copy(dest, src, di, anchor, lLen);
456
+ di += lLen;
457
+ anchor = si;
458
+ // encode offset
459
+ di += 2;
460
+ if (di >= dn) {
461
+ throw errShortBuffer;
462
+ }
463
+ dest[di - 2] = offset;
464
+ dest[di - 1] = offset >> 8;
465
+ // encode match length part 2
466
+ if (mLen >= 0xf) {
467
+ for (mLen -= 0xf; mLen >= 0xff; mLen -= 0xff) {
468
+ dest[di] = 0xff;
469
+ if (++di === dn) {
470
+ throw errShortBuffer;
471
+ }
472
+ }
473
+ dest[di] = mLen;
474
+ if (++di === dn) {
475
+ throw errShortBuffer;
476
+ }
477
+ }
478
+ }
479
+ if (anchor === 0) {
480
+ // incompressible
481
+ return 0;
482
+ }
483
+ // last literals
484
+ let lLen = src.length - anchor;
485
+ if (lLen < 0xf) {
486
+ dest[di] = lLen << 4;
487
+ }
488
+ else {
489
+ dest[di] = 0xf0;
490
+ if (++di === dn) {
491
+ throw errShortBuffer;
492
+ }
493
+ for (lLen -= 0xf; lLen >= 0xff; lLen -= 0xff) {
494
+ dest[di] = 0xff;
495
+ if (++di === dn) {
496
+ throw errShortBuffer;
497
+ }
498
+ }
499
+ dest[di] = lLen;
500
+ }
501
+ if (++di === dn) {
502
+ throw errShortBuffer;
503
+ }
504
+ // write literals
505
+ const lastLen = src.length - anchor;
506
+ const n = di + lastLen;
507
+ if (n > dn) {
508
+ throw errShortBuffer;
509
+ }
510
+ else if (n >= sn) {
511
+ // incompressible
512
+ return 0;
513
+ }
514
+ copy(dest, src, di, anchor, lastLen);
515
+ di += lastLen;
516
+ return di;
517
+ }
@@ -0,0 +1,5 @@
1
+ import sodium from 'libsodium-wrappers-sumo';
2
+ export async function setup() {
3
+ await sodium.ready;
4
+ }
5
+ export { sodium };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,25 @@
1
+ export function concatenate(...arrays) {
2
+ let totalLength = 0;
3
+ for (const arr of arrays) {
4
+ totalLength += arr.length;
5
+ }
6
+ const result = new Uint8Array(totalLength);
7
+ let offset = 0;
8
+ for (const arr of arrays) {
9
+ result.set(arr, offset);
10
+ offset += arr.length;
11
+ }
12
+ return result;
13
+ }
14
+ export function* enumerate(iterable) {
15
+ let i = 0;
16
+ for (const x of iterable) {
17
+ yield [i, x];
18
+ i++;
19
+ }
20
+ }
21
+ export function* chunks(arr, n) {
22
+ for (let i = 0; i < arr.length; i += n) {
23
+ yield arr.slice(i, i + n);
24
+ }
25
+ }
@@ -0,0 +1,2 @@
1
+ const decoder = new TextDecoder('utf8');
2
+ export const bufferToBase64 = (u8) => btoa(decoder.decode(u8));