lib0 0.2.60 → 0.2.62
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 +46 -0
- package/array.d.ts +2 -0
- package/array.d.ts.map +1 -1
- package/array.js +36 -0
- package/array.test.d.ts +1 -0
- package/array.test.d.ts.map +1 -1
- package/broadcastchannel.d.ts.map +1 -1
- package/broadcastchannel.js +2 -1
- package/crypto.d.ts +22 -0
- package/crypto.d.ts.map +1 -0
- package/crypto.js +185 -0
- package/crypto.test.d.ts +6 -0
- package/crypto.test.d.ts.map +1 -0
- package/dist/{array-acefe0f2.cjs → array-42d0736b.cjs} +42 -2
- package/dist/array-42d0736b.cjs.map +1 -0
- package/dist/array.cjs +4 -1
- package/dist/array.cjs.map +1 -1
- package/dist/array.d.ts +2 -0
- package/dist/array.d.ts.map +1 -1
- package/dist/array.test.d.ts +1 -0
- package/dist/array.test.d.ts.map +1 -1
- package/dist/{broadcastchannel-02adb071.cjs → broadcastchannel-8d21c21c.cjs} +4 -3
- package/dist/broadcastchannel-8d21c21c.cjs.map +1 -0
- package/dist/broadcastchannel.cjs +6 -5
- package/dist/broadcastchannel.cjs.map +1 -1
- package/dist/broadcastchannel.d.ts.map +1 -1
- package/dist/{buffer-5ebf4ddb.cjs → buffer-22bb9fae.cjs} +2 -2
- package/dist/{buffer-5ebf4ddb.cjs.map → buffer-22bb9fae.cjs.map} +1 -1
- package/dist/buffer.cjs +5 -4
- package/dist/buffer.cjs.map +1 -1
- package/dist/cache.cjs +3 -2
- package/dist/cache.cjs.map +1 -1
- package/dist/component.cjs +4 -3
- package/dist/component.cjs.map +1 -1
- package/dist/crypto.cjs +231 -0
- package/dist/crypto.cjs.map +1 -0
- package/dist/crypto.d.ts +22 -0
- package/dist/crypto.d.ts.map +1 -0
- package/dist/crypto.test.d.ts +6 -0
- package/dist/crypto.test.d.ts.map +1 -0
- package/dist/decoding.cjs +5 -4
- package/dist/decoding.cjs.map +1 -1
- package/dist/{diff-85090e89.cjs → diff-ee50c530.cjs} +2 -2
- package/dist/{diff-85090e89.cjs.map → diff-ee50c530.cjs.map} +1 -1
- package/dist/diff.cjs +4 -3
- package/dist/diff.cjs.map +1 -1
- package/dist/encoding.cjs +5 -4
- package/dist/encoding.cjs.map +1 -1
- package/dist/{environment-9ecc1776.cjs → environment-f6386808.cjs} +2 -2
- package/dist/{environment-9ecc1776.cjs.map → environment-f6386808.cjs.map} +1 -1
- package/dist/environment.cjs +4 -3
- package/dist/environment.cjs.map +1 -1
- package/dist/{function-0da7a13f.cjs → function-40d38a5b.cjs} +2 -2
- package/dist/{function-0da7a13f.cjs.map → function-40d38a5b.cjs.map} +1 -1
- package/dist/function.cjs +3 -2
- package/dist/function.cjs.map +1 -1
- package/dist/index.cjs +9 -9
- package/dist/list.cjs +3 -2
- package/dist/list.cjs.map +1 -1
- package/dist/{logging-00ff6aaf.cjs → logging-ba24d049.cjs} +5 -4
- package/dist/logging-ba24d049.cjs.map +1 -0
- package/dist/logging.cjs +5 -4
- package/dist/logging.cjs.map +1 -1
- package/dist/logging.d.ts.map +1 -1
- package/dist/observable.cjs +1 -1
- package/dist/{prng-7e1c5b0a.cjs → prng-4493ba85.cjs} +2 -2
- package/dist/{prng-7e1c5b0a.cjs.map → prng-4493ba85.cjs.map} +1 -1
- package/dist/prng.cjs +6 -5
- package/dist/prng.cjs.map +1 -1
- package/dist/rollup.config.d.ts +1 -1
- package/dist/testing.cjs +25 -15
- package/dist/testing.cjs.map +1 -1
- package/dist/testing.d.ts +1 -0
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.test.d.ts +2 -2
- package/dist/testing.test.d.ts.map +1 -1
- package/dist/webcrypto.browser.cjs +12 -0
- package/dist/webcrypto.browser.cjs.map +1 -0
- package/dist/webcrypto.browser.d.ts +3 -0
- package/dist/webcrypto.browser.d.ts.map +1 -0
- package/dist/webcrypto.node.cjs +12 -0
- package/dist/webcrypto.node.cjs.map +1 -0
- package/dist/webcrypto.node.d.ts +3 -0
- package/dist/webcrypto.node.d.ts.map +1 -0
- package/dist/{websocket-57086be8.cjs → websocket-a48155c2.cjs} +1 -1
- package/dist/{websocket-57086be8.cjs.map → websocket-a48155c2.cjs.map} +1 -1
- package/dist/websocket.cjs +2 -2
- package/logging.d.ts.map +1 -1
- package/logging.js +2 -1
- package/package.json +19 -1
- package/rollup.config.d.ts +1 -1
- package/test.js +2 -0
- package/testing.d.ts +1 -0
- package/testing.d.ts.map +1 -1
- package/testing.js +16 -8
- package/testing.test.d.ts +2 -2
- package/testing.test.d.ts.map +1 -1
- package/webcrypto.browser.d.ts +3 -0
- package/webcrypto.browser.d.ts.map +1 -0
- package/webcrypto.browser.js +4 -0
- package/webcrypto.node.d.ts +3 -0
- package/webcrypto.node.d.ts.map +1 -0
- package/webcrypto.node.js +5 -0
- package/dist/array-acefe0f2.cjs.map +0 -1
- package/dist/broadcastchannel-02adb071.cjs.map +0 -1
- package/dist/logging-00ff6aaf.cjs.map +0 -1
package/README.md
CHANGED
|
@@ -93,6 +93,8 @@ import module from 'lib0/[module].js'
|
|
|
93
93
|
<b><code>array.equalFlat(a: Array<ELEM>, b: Array<ELEM>): boolean</code></b><br>
|
|
94
94
|
<b><code>array.flatten(arr: Array<Array<ELEM>>): Array<ELEM></code></b><br>
|
|
95
95
|
<b><code>array.isArray</code></b><br>
|
|
96
|
+
<b><code>array.unique(arr: Array<T>): Array<T></code></b><br>
|
|
97
|
+
<b><code>array.uniqueBy(arr: Array<T>, mapper: function(T):M): Array<T></code></b><br>
|
|
96
98
|
</dl>
|
|
97
99
|
</details>
|
|
98
100
|
<details><summary><b>[lib0/binary]</b> Binary data constants.</summary>
|
|
@@ -243,6 +245,30 @@ Using getAsync & setIfUndefined ensures that the init function is only calle
|
|
|
243
245
|
<b><code>conditions.undefinedToNull</code></b><br>
|
|
244
246
|
</dl>
|
|
245
247
|
</details>
|
|
248
|
+
<details><summary><b>[lib0/crypto]</b> </summary>
|
|
249
|
+
<pre>import * as crypto from 'lib0/crypto'</pre>
|
|
250
|
+
<dl>
|
|
251
|
+
<b><code>y(data: string | Uint8Array): Uint8Array</code></b><br>
|
|
252
|
+
<b><code>ymmetricKey(secret: string | Uint8Array, salt: string | Uint8Array, opts: Object, opts.extractable: boolean, opts.usages: Array<'sign'|'verify'|'encrypt'|'decrypt'>): PromiseLike<CryptoKey></code></b><br>
|
|
253
|
+
<b><code>ymmetricKey()</code></b><br>
|
|
254
|
+
<b><code>eAsymmetricKey(opts: Object, opts.extractable: boolean, opts.usages: Array<'sign'|'verify'|'encrypt'|'decrypt'>)</code></b><br>
|
|
255
|
+
<b><code>eAsymmetricKey()</code></b><br>
|
|
256
|
+
<b><code>ey(key: CryptoKey)</code></b><br>
|
|
257
|
+
<b><code>ey()</code></b><br>
|
|
258
|
+
<b><code>ymmetricKey(jwk: any, opts: Object, opts.extractable: boolean, opts.usages: Array<'sign'|'verify'|'encrypt'|'decrypt'>)</code></b><br>
|
|
259
|
+
<b><code>ymmetricKey()</code></b><br>
|
|
260
|
+
<b><code>symmetricKey(jwk: any, opts: Object, opts.extractable: boolean, opts.usages: Array<'sign'|'verify'|'encrypt'|'decrypt'>)</code></b><br>
|
|
261
|
+
<b><code>symmetricKey()</code></b><br>
|
|
262
|
+
<b><code>(data: Uint8Array, key: CryptoKey): PromiseLike<Uint8Array></code></b><br>
|
|
263
|
+
<b><code>()</code></b><br>
|
|
264
|
+
<b><code>(data: Uint8Array, key: CryptoKey): PromiseLike<Uint8Array></code></b><br>
|
|
265
|
+
<b><code>()</code></b><br>
|
|
266
|
+
<b><code>(data: Uint8Array, privateKey: CryptoKey): PromiseLike<Uint8Array></code></b><br>
|
|
267
|
+
<b><code>()</code></b><br>
|
|
268
|
+
<b><code>(signature: Uint8Array, data: Uint8Array, publicKey: CryptoKey): PromiseLike<boolean></code></b><br>
|
|
269
|
+
<b><code>()</code></b><br>
|
|
270
|
+
</dl>
|
|
271
|
+
</details>
|
|
246
272
|
<details><summary><b>[lib0/decoding]</b> Efficient schema-less binary decoding with support for variable length encoding.</summary>
|
|
247
273
|
<pre>import * as decoding from 'lib0/decoding'</pre>
|
|
248
274
|
|
|
@@ -1282,6 +1308,7 @@ export const testMyFirstTest = tc => {
|
|
|
1282
1308
|
<b><code>testing.compareObjects(a: Object<K,V>, b: Object<K,V>, m: string)</code></b><br>
|
|
1283
1309
|
<b><code>testing.compare(a: T, b: T, message: string?, customCompare: function(any,T,T,string,any):boolean)</code></b><br>
|
|
1284
1310
|
<b><code>testing.assert(condition: boolean, message: string?)</code></b><br>
|
|
1311
|
+
<b><code>testing.promiseRejected(f: function():Promise<any>)</code></b><br>
|
|
1285
1312
|
<b><code>testing.fails(f: function():void)</code></b><br>
|
|
1286
1313
|
<b><code>testing.runTests(tests: Object<string, Object<string, function(module:testing.TestCase):void|Promise<any>>>)</code></b><br>
|
|
1287
1314
|
<b><code>testing.fail(reason: string)</code></b><br>
|
|
@@ -1326,6 +1353,25 @@ export const testMyFirstTest = tc => {
|
|
|
1326
1353
|
<b><code>url.encodeQueryParams(params: Object<string,string>): string</code></b><br>
|
|
1327
1354
|
</dl>
|
|
1328
1355
|
</details>
|
|
1356
|
+
<details><summary><b>[lib0/webcrypto.browser]</b> </summary>
|
|
1357
|
+
<pre>import * as webcrypto.browser from 'lib0/webcrypto.browser'</pre>
|
|
1358
|
+
<dl>
|
|
1359
|
+
<b><code></code></b><br>
|
|
1360
|
+
<b><code>()</code></b><br>
|
|
1361
|
+
<b><code>omValues</code></b><br>
|
|
1362
|
+
<b><code>omValues()</code></b><br>
|
|
1363
|
+
</dl>
|
|
1364
|
+
</details>
|
|
1365
|
+
<details><summary><b>[lib0/webcrypto.node]</b> </summary>
|
|
1366
|
+
<pre>import * as webcrypto.node from 'lib0/webcrypto.node'</pre>
|
|
1367
|
+
<dl>
|
|
1368
|
+
<b><code></code></b><br>
|
|
1369
|
+
<b><code>()</code></b><br>
|
|
1370
|
+
<b><code>to.subtle: any</code></b><br>
|
|
1371
|
+
<b><code>omValues</code></b><br>
|
|
1372
|
+
<b><code>omValues()</code></b><br>
|
|
1373
|
+
</dl>
|
|
1374
|
+
</details>
|
|
1329
1375
|
<details><summary><b>[lib0/websocket]</b> Tiny websocket connection handler.</summary>
|
|
1330
1376
|
<pre>import * as websocket from 'lib0/websocket'</pre>
|
|
1331
1377
|
|
package/array.d.ts
CHANGED
|
@@ -21,4 +21,6 @@ export function some<S>(arr: S[], f: (arg0: S, arg1: number, arg2: S[]) => boole
|
|
|
21
21
|
export function equalFlat<ELEM>(a: ELEM[], b: ELEM[]): boolean;
|
|
22
22
|
export function flatten<ELEM>(arr: ELEM[][]): ELEM[];
|
|
23
23
|
export const isArray: (arg: any) => arg is any[];
|
|
24
|
+
export function unique<T_1>(arr: T_1[]): T_1[];
|
|
25
|
+
export function uniqueBy<T_1, M>(arr: T_1[], mapper: (arg0: T_1) => M): T_1[];
|
|
24
26
|
//# sourceMappingURL=array.d.ts.map
|
package/array.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array.d.ts","sourceRoot":"","sources":["array.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"array.d.ts","sourceRoot":"","sources":["array.js"],"names":[],"mappings":"AAeO,qCAAuC;AAMvC,iCAAiD;AAOjD,qCAAqD;AASrD,uDAIN;AAED;;;;;;;GAOG;AACH;;;;;EAA8B;AAYvB,+DAHmB,MAAM,mBAAe,OAAO,GAC1C,OAAO,CAE0B;AAWtC,qDAHgB,MAAM,gBAAY,OAAO,GACpC,OAAO,CAEwB;AASpC,uDAFK,OAAO,CAEqF;AAOjG,qDAAoE;AAE3E,iDAAoC;AAO7B,+CAAyC;AASzC,8EAkBN"}
|
package/array.js
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
* @module array
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
+
import * as set from './set.js'
|
|
8
|
+
|
|
7
9
|
/**
|
|
8
10
|
* Return the last element of an array. The element must exist
|
|
9
11
|
*
|
|
@@ -89,3 +91,37 @@ export const equalFlat = (a, b) => a.length === b.length && every(a, (item, inde
|
|
|
89
91
|
export const flatten = arr => arr.reduce((acc, val) => acc.concat(val), [])
|
|
90
92
|
|
|
91
93
|
export const isArray = Array.isArray
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* @template T
|
|
97
|
+
* @param {Array<T>} arr
|
|
98
|
+
* @return {Array<T>}
|
|
99
|
+
*/
|
|
100
|
+
export const unique = arr => from(set.from(arr))
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* @template T
|
|
104
|
+
* @template M
|
|
105
|
+
* @param {Array<T>} arr
|
|
106
|
+
* @param {function(T):M} mapper
|
|
107
|
+
* @return {Array<T>}
|
|
108
|
+
*/
|
|
109
|
+
export const uniqueBy = (arr, mapper) => {
|
|
110
|
+
/**
|
|
111
|
+
* @type {Set<M>}
|
|
112
|
+
*/
|
|
113
|
+
const happened = set.create()
|
|
114
|
+
/**
|
|
115
|
+
* @type {Array<T>}
|
|
116
|
+
*/
|
|
117
|
+
const result = []
|
|
118
|
+
for (let i = 0; i < arr.length; i++) {
|
|
119
|
+
const el = arr[i]
|
|
120
|
+
const mapped = mapper(el)
|
|
121
|
+
if (!happened.has(mapped)) {
|
|
122
|
+
happened.add(mapped)
|
|
123
|
+
result.push(el)
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return result
|
|
127
|
+
}
|
package/array.test.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export function testAppend(tc: t.TestCase): void;
|
|
2
2
|
export function testflatten(tc: t.TestCase): void;
|
|
3
3
|
export function testIsArray(tc: t.TestCase): void;
|
|
4
|
+
export function testUnique(tc: t.TestCase): void;
|
|
4
5
|
import * as t from "./testing.js";
|
|
5
6
|
//# sourceMappingURL=array.test.d.ts.map
|
package/array.test.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array.test.d.ts","sourceRoot":"","sources":["array.test.js"],"names":[],"mappings":"AAOO,+BAFI,EAAE,QAAQ,QAMpB;AAKM,gCAFI,EAAE,QAAQ,QAKpB;AAKM,gCAFI,EAAE,QAAQ,QASpB"}
|
|
1
|
+
{"version":3,"file":"array.test.d.ts","sourceRoot":"","sources":["array.test.js"],"names":[],"mappings":"AAOO,+BAFI,EAAE,QAAQ,QAMpB;AAKM,gCAFI,EAAE,QAAQ,QAKpB;AAKM,gCAFI,EAAE,QAAQ,QASpB;AAKM,+BAFI,EAAE,QAAQ,QAOpB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"broadcastchannel.d.ts","sourceRoot":"","sources":["broadcastchannel.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"broadcastchannel.d.ts","sourceRoot":"","sources":["broadcastchannel.js"],"names":[],"mappings":"AAsFO,gCAHI,MAAM,YACG,GAAG,QAAE,GAAG,KAAE,GAAG,UAAb,GAAG,QAAE,GAAG,KAAE,GAAG,CAKhC;AASM,kCAHI,MAAM,YACG,GAAG,QAAE,GAAG,KAAE,GAAG,WAWhC;AAUM,8BAJI,MAAM,QACN,GAAG,WACH,GAAG,QAMb;;UA/Fa,WAAa,GAAG,QAAE,GAAG,KAAE,GAAG,CAAC;QAC3B,GAAG"}
|
package/broadcastchannel.js
CHANGED
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
// @todo before next major: use Uint8Array instead as buffer object
|
|
19
19
|
|
|
20
20
|
import * as map from './map.js'
|
|
21
|
+
import * as set from './set.js'
|
|
21
22
|
import * as buffer from './buffer.js'
|
|
22
23
|
import * as storage from './storage.js'
|
|
23
24
|
|
|
@@ -64,7 +65,7 @@ const BC = typeof BroadcastChannel === 'undefined' ? LocalStoragePolyfill : Broa
|
|
|
64
65
|
*/
|
|
65
66
|
const getChannel = room =>
|
|
66
67
|
map.setIfUndefined(channels, room, () => {
|
|
67
|
-
const subs =
|
|
68
|
+
const subs = set.create()
|
|
68
69
|
const bc = new BC(room)
|
|
69
70
|
/* istanbul ignore next */
|
|
70
71
|
/**
|
package/crypto.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export function deriveSymmetricKey(secret: string | Uint8Array, salt: string | Uint8Array, { extractable, usages }?: {
|
|
2
|
+
extractable?: boolean | undefined;
|
|
3
|
+
usages?: ("decrypt" | "encrypt" | "sign" | "verify")[] | undefined;
|
|
4
|
+
}): PromiseLike<CryptoKey>;
|
|
5
|
+
export function generateAsymmetricKey({ extractable, usages }?: {
|
|
6
|
+
extractable?: boolean | undefined;
|
|
7
|
+
usages?: ("decrypt" | "encrypt" | "sign" | "verify")[] | undefined;
|
|
8
|
+
}): Promise<CryptoKeyPair>;
|
|
9
|
+
export function exportKey(key: CryptoKey): Promise<JsonWebKey>;
|
|
10
|
+
export function importSymmetricKey(jwk: any, { extractable, usages }?: {
|
|
11
|
+
extractable?: boolean | undefined;
|
|
12
|
+
usages?: ("decrypt" | "encrypt" | "sign" | "verify")[] | undefined;
|
|
13
|
+
}): Promise<CryptoKey>;
|
|
14
|
+
export function importAsymmetricKey(jwk: any, { extractable, usages }?: {
|
|
15
|
+
extractable?: boolean | undefined;
|
|
16
|
+
usages?: ("decrypt" | "encrypt" | "sign" | "verify")[] | undefined;
|
|
17
|
+
}): Promise<CryptoKey>;
|
|
18
|
+
export function encrypt(data: Uint8Array, key: CryptoKey): PromiseLike<Uint8Array>;
|
|
19
|
+
export function decrypt(data: Uint8Array, key: CryptoKey): PromiseLike<Uint8Array>;
|
|
20
|
+
export function sign(data: Uint8Array, privateKey: CryptoKey): PromiseLike<Uint8Array>;
|
|
21
|
+
export function verify(signature: Uint8Array, data: Uint8Array, publicKey: CryptoKey): PromiseLike<boolean>;
|
|
22
|
+
//# sourceMappingURL=crypto.d.ts.map
|
package/crypto.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["crypto.js"],"names":[],"mappings":"AA0BO,2CAPI,MAAM,GAAG,UAAU,QACnB,MAAM,GAAG,UAAU;IAEJ,WAAW;IACwB,MAAM;IACvD,YAAY,SAAS,CAAC,CA4BjC;AAQM;IAHmB,WAAW;IACwB,MAAM;2BAUhE;AAMI,+BAFI,SAAS,uBAEkD;AAS/D,wCALI,GAAG;IAEY,WAAW;IACwB,MAAM;uBAGI;AAShE,yCALI,GAAG;IAEY,WAAW;IACwB,MAAM;uBAGkC;AAW9F,8BAJI,UAAU,OACV,SAAS,GACR,YAAY,UAAU,CAAC,CAkBlC;AAWM,8BAJI,UAAU,OACV,SAAS,GACR,YAAY,UAAU,CAAC,CAclC;AAWM,2BAJI,UAAU,cACV,SAAS,GACR,YAAY,UAAU,CAAC,CAUa;AAYzC,kCALI,UAAU,QACV,UAAU,aACV,SAAS,GACR,YAAY,OAAO,CAAC,CAW7B"}
|
package/crypto.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/* eslint-env browser */
|
|
2
|
+
|
|
3
|
+
import * as encoding from './encoding.js'
|
|
4
|
+
import * as decoding from './decoding.js'
|
|
5
|
+
import * as string from './string.js'
|
|
6
|
+
import * as webcrypto from 'lib0/webcrypto'
|
|
7
|
+
|
|
8
|
+
/* istanbul ignore next */
|
|
9
|
+
/**
|
|
10
|
+
* @param {string | Uint8Array} data
|
|
11
|
+
* @return {Uint8Array}
|
|
12
|
+
*/
|
|
13
|
+
const toBinary = data => typeof data === 'string' ? string.encodeUtf8(data) : data
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @experimental The API is not final!
|
|
17
|
+
*
|
|
18
|
+
* Derive an symmetric key using the Password-Based-Key-Derivation-Function-2.
|
|
19
|
+
*
|
|
20
|
+
* @param {string | Uint8Array} secret
|
|
21
|
+
* @param {string | Uint8Array} salt
|
|
22
|
+
* @param {Object} opts
|
|
23
|
+
* @param {boolean} [opts.extractable]
|
|
24
|
+
* @param {Array<'sign'|'verify'|'encrypt'|'decrypt'>} [opts.usages]
|
|
25
|
+
* @return {PromiseLike<CryptoKey>}
|
|
26
|
+
*/
|
|
27
|
+
export const deriveSymmetricKey = (secret, salt, { extractable = false, usages = ['encrypt', 'decrypt'] } = {}) => {
|
|
28
|
+
const binSecret = toBinary(secret)
|
|
29
|
+
const binSalt = toBinary(salt)
|
|
30
|
+
return webcrypto.subtle.importKey(
|
|
31
|
+
'raw',
|
|
32
|
+
binSecret,
|
|
33
|
+
'PBKDF2',
|
|
34
|
+
false,
|
|
35
|
+
['deriveKey']
|
|
36
|
+
).then(keyMaterial =>
|
|
37
|
+
webcrypto.subtle.deriveKey(
|
|
38
|
+
{
|
|
39
|
+
name: 'PBKDF2',
|
|
40
|
+
salt: binSalt, // NIST recommends at least 64 bits
|
|
41
|
+
iterations: 600000, // OWASP recommends 600k iterations
|
|
42
|
+
hash: 'SHA-256'
|
|
43
|
+
},
|
|
44
|
+
keyMaterial,
|
|
45
|
+
{
|
|
46
|
+
name: 'AES-GCM',
|
|
47
|
+
length: 256
|
|
48
|
+
},
|
|
49
|
+
extractable,
|
|
50
|
+
usages
|
|
51
|
+
)
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/* istanbul ignore next */
|
|
56
|
+
/**
|
|
57
|
+
* @param {Object} opts
|
|
58
|
+
* @param {boolean} [opts.extractable]
|
|
59
|
+
* @param {Array<'sign'|'verify'|'encrypt'|'decrypt'>} [opts.usages]
|
|
60
|
+
*/
|
|
61
|
+
export const generateAsymmetricKey = ({ extractable = false, usages = ['sign', 'verify'] } = {}) =>
|
|
62
|
+
webcrypto.subtle.generateKey(
|
|
63
|
+
{
|
|
64
|
+
name: 'ECDSA',
|
|
65
|
+
namedCurve: 'P-384'
|
|
66
|
+
},
|
|
67
|
+
extractable,
|
|
68
|
+
usages
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
/* istanbul ignore next */
|
|
72
|
+
/**
|
|
73
|
+
* @param {CryptoKey} key - Symmetric or Asymmetric key
|
|
74
|
+
*/
|
|
75
|
+
export const exportKey = key => webcrypto.subtle.exportKey('jwk', key)
|
|
76
|
+
|
|
77
|
+
/* istanbul ignore next */
|
|
78
|
+
/**
|
|
79
|
+
* @param {any} jwk
|
|
80
|
+
* @param {Object} opts
|
|
81
|
+
* @param {boolean} [opts.extractable]
|
|
82
|
+
* @param {Array<'sign'|'verify'|'encrypt'|'decrypt'>} [opts.usages]
|
|
83
|
+
*/
|
|
84
|
+
export const importSymmetricKey = (jwk, { extractable = false, usages = ['encrypt', 'decrypt'] } = {}) =>
|
|
85
|
+
webcrypto.subtle.importKey('jwk', jwk, 'PBKDF2', extractable, usages)
|
|
86
|
+
|
|
87
|
+
/* istanbul ignore next */
|
|
88
|
+
/**
|
|
89
|
+
* @param {any} jwk
|
|
90
|
+
* @param {Object} opts
|
|
91
|
+
* @param {boolean} [opts.extractable]
|
|
92
|
+
* @param {Array<'sign'|'verify'|'encrypt'|'decrypt'>} [opts.usages]
|
|
93
|
+
*/
|
|
94
|
+
export const importAsymmetricKey = (jwk, { extractable = false, usages = ['encrypt', 'decrypt'] } = {}) =>
|
|
95
|
+
webcrypto.subtle.importKey('jwk', jwk, { name: 'ECDSA', namedCurve: 'P-384' }, extractable, usages)
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* @experimental The API is not final!
|
|
99
|
+
*
|
|
100
|
+
* Encrypt some data using AES-GCM method.
|
|
101
|
+
*
|
|
102
|
+
* @param {Uint8Array} data data to be encrypted
|
|
103
|
+
* @param {CryptoKey} key
|
|
104
|
+
* @return {PromiseLike<Uint8Array>} encrypted, base64 encoded message
|
|
105
|
+
*/
|
|
106
|
+
export const encrypt = (data, key) => {
|
|
107
|
+
const iv = webcrypto.getRandomValues(new Uint8Array(16)) // 92bit is enough. 128bit is recommended if space is not an issue.
|
|
108
|
+
return webcrypto.subtle.encrypt(
|
|
109
|
+
{
|
|
110
|
+
name: 'AES-GCM',
|
|
111
|
+
iv
|
|
112
|
+
},
|
|
113
|
+
key,
|
|
114
|
+
data
|
|
115
|
+
).then(cipher => {
|
|
116
|
+
const encryptedDataEncoder = encoding.createEncoder()
|
|
117
|
+
// iv may be sent in the clear to the other peers
|
|
118
|
+
encoding.writeUint8Array(encryptedDataEncoder, iv)
|
|
119
|
+
encoding.writeVarUint8Array(encryptedDataEncoder, new Uint8Array(cipher))
|
|
120
|
+
return encoding.toUint8Array(encryptedDataEncoder)
|
|
121
|
+
})
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* @experimental The API is not final!
|
|
126
|
+
*
|
|
127
|
+
* Decrypt some data using AES-GCM method.
|
|
128
|
+
*
|
|
129
|
+
* @param {Uint8Array} data
|
|
130
|
+
* @param {CryptoKey} key
|
|
131
|
+
* @return {PromiseLike<Uint8Array>} decrypted buffer
|
|
132
|
+
*/
|
|
133
|
+
export const decrypt = (data, key) => {
|
|
134
|
+
const dataDecoder = decoding.createDecoder(data)
|
|
135
|
+
const iv = decoding.readUint8Array(dataDecoder, 16)
|
|
136
|
+
const cipher = decoding.readVarUint8Array(dataDecoder)
|
|
137
|
+
return webcrypto.subtle.decrypt(
|
|
138
|
+
{
|
|
139
|
+
name: 'AES-GCM',
|
|
140
|
+
iv
|
|
141
|
+
},
|
|
142
|
+
key,
|
|
143
|
+
cipher
|
|
144
|
+
).then(data => new Uint8Array(data))
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @experimental The API is not final!
|
|
149
|
+
*
|
|
150
|
+
* Sign a message
|
|
151
|
+
*
|
|
152
|
+
* @param {Uint8Array} data
|
|
153
|
+
* @param {CryptoKey} privateKey
|
|
154
|
+
* @return {PromiseLike<Uint8Array>} signature
|
|
155
|
+
*/
|
|
156
|
+
export const sign = (data, privateKey) =>
|
|
157
|
+
webcrypto.subtle.sign(
|
|
158
|
+
{
|
|
159
|
+
name: 'ECDSA',
|
|
160
|
+
hash: { name: 'SHA-384' }
|
|
161
|
+
},
|
|
162
|
+
privateKey,
|
|
163
|
+
data
|
|
164
|
+
).then(signature => new Uint8Array(signature))
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* @experimental The API is not final!
|
|
168
|
+
*
|
|
169
|
+
* Sign a message
|
|
170
|
+
*
|
|
171
|
+
* @param {Uint8Array} signature
|
|
172
|
+
* @param {Uint8Array} data
|
|
173
|
+
* @param {CryptoKey} publicKey
|
|
174
|
+
* @return {PromiseLike<boolean>} signature
|
|
175
|
+
*/
|
|
176
|
+
export const verify = (signature, data, publicKey) =>
|
|
177
|
+
webcrypto.subtle.verify(
|
|
178
|
+
{
|
|
179
|
+
name: 'ECDSA',
|
|
180
|
+
hash: { name: 'SHA-384' }
|
|
181
|
+
},
|
|
182
|
+
publicKey,
|
|
183
|
+
signature,
|
|
184
|
+
data
|
|
185
|
+
)
|
package/crypto.test.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export function testReapeatEncryption(tc: t.TestCase): Promise<void>;
|
|
2
|
+
export function testEncryptionPerformance(tc: t.TestCase): Promise<void>;
|
|
3
|
+
export function testConsistentKeyGeneration(_tc: t.TestCase): Promise<void>;
|
|
4
|
+
export function testSigning(tc: t.TestCase): Promise<void>;
|
|
5
|
+
import * as t from "./testing.js";
|
|
6
|
+
//# sourceMappingURL=crypto.test.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.test.d.ts","sourceRoot":"","sources":["crypto.test.js"],"names":[],"mappings":"AAQO,0CAFI,EAAE,QAAQ,iBA+BpB;AAKM,8CAFI,EAAE,QAAQ,iBAsCpB;AAKM,iDAFI,EAAE,QAAQ,iBA0CpB;AAKM,gCAFI,EAAE,QAAQ,iBAapB"}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var set = require('./set-b596ef38.cjs');
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
6
|
* Utility module to work with Arrays.
|
|
5
7
|
*
|
|
@@ -92,6 +94,40 @@ const flatten = arr => arr.reduce((acc, val) => acc.concat(val), []);
|
|
|
92
94
|
|
|
93
95
|
const isArray = Array.isArray;
|
|
94
96
|
|
|
97
|
+
/**
|
|
98
|
+
* @template T
|
|
99
|
+
* @param {Array<T>} arr
|
|
100
|
+
* @return {Array<T>}
|
|
101
|
+
*/
|
|
102
|
+
const unique = arr => from(set.from(arr));
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* @template T
|
|
106
|
+
* @template M
|
|
107
|
+
* @param {Array<T>} arr
|
|
108
|
+
* @param {function(T):M} mapper
|
|
109
|
+
* @return {Array<T>}
|
|
110
|
+
*/
|
|
111
|
+
const uniqueBy = (arr, mapper) => {
|
|
112
|
+
/**
|
|
113
|
+
* @type {Set<M>}
|
|
114
|
+
*/
|
|
115
|
+
const happened = set.create();
|
|
116
|
+
/**
|
|
117
|
+
* @type {Array<T>}
|
|
118
|
+
*/
|
|
119
|
+
const result = [];
|
|
120
|
+
for (let i = 0; i < arr.length; i++) {
|
|
121
|
+
const el = arr[i];
|
|
122
|
+
const mapped = mapper(el);
|
|
123
|
+
if (!happened.has(mapped)) {
|
|
124
|
+
happened.add(mapped);
|
|
125
|
+
result.push(el);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return result
|
|
129
|
+
};
|
|
130
|
+
|
|
95
131
|
var array = /*#__PURE__*/Object.freeze({
|
|
96
132
|
__proto__: null,
|
|
97
133
|
last: last,
|
|
@@ -103,7 +139,9 @@ var array = /*#__PURE__*/Object.freeze({
|
|
|
103
139
|
some: some,
|
|
104
140
|
equalFlat: equalFlat,
|
|
105
141
|
flatten: flatten,
|
|
106
|
-
isArray: isArray
|
|
142
|
+
isArray: isArray,
|
|
143
|
+
unique: unique,
|
|
144
|
+
uniqueBy: uniqueBy
|
|
107
145
|
});
|
|
108
146
|
|
|
109
147
|
exports.appendTo = appendTo;
|
|
@@ -117,4 +155,6 @@ exports.from = from;
|
|
|
117
155
|
exports.isArray = isArray;
|
|
118
156
|
exports.last = last;
|
|
119
157
|
exports.some = some;
|
|
120
|
-
|
|
158
|
+
exports.unique = unique;
|
|
159
|
+
exports.uniqueBy = uniqueBy;
|
|
160
|
+
//# sourceMappingURL=array-42d0736b.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"array-42d0736b.cjs","sources":["../array.js"],"sourcesContent":["/**\n * Utility module to work with Arrays.\n *\n * @module array\n */\n\nimport * as set from './set.js'\n\n/**\n * Return the last element of an array. The element must exist\n *\n * @template L\n * @param {Array<L>} arr\n * @return {L}\n */\nexport const last = arr => arr[arr.length - 1]\n\n/**\n * @template C\n * @return {Array<C>}\n */\nexport const create = () => /** @type {Array<C>} */ ([])\n\n/**\n * @template D\n * @param {Array<D>} a\n * @return {Array<D>}\n */\nexport const copy = a => /** @type {Array<D>} */ (a.slice())\n\n/**\n * Append elements from src to dest\n *\n * @template M\n * @param {Array<M>} dest\n * @param {Array<M>} src\n */\nexport const appendTo = (dest, src) => {\n for (let i = 0; i < src.length; i++) {\n dest.push(src[i])\n }\n}\n\n/**\n * Transforms something array-like to an actual Array.\n *\n * @function\n * @template T\n * @param {ArrayLike<T>|Iterable<T>} arraylike\n * @return {T}\n */\nexport const from = Array.from\n\n/**\n * True iff condition holds on every element in the Array.\n *\n * @function\n * @template ITEM\n *\n * @param {Array<ITEM>} arr\n * @param {function(ITEM, number, Array<ITEM>):boolean} f\n * @return {boolean}\n */\nexport const every = (arr, f) => arr.every(f)\n\n/**\n * True iff condition holds on some element in the Array.\n *\n * @function\n * @template S\n * @param {Array<S>} arr\n * @param {function(S, number, Array<S>):boolean} f\n * @return {boolean}\n */\nexport const some = (arr, f) => arr.some(f)\n\n/**\n * @template ELEM\n *\n * @param {Array<ELEM>} a\n * @param {Array<ELEM>} b\n * @return {boolean}\n */\nexport const equalFlat = (a, b) => a.length === b.length && every(a, (item, index) => item === b[index])\n\n/**\n * @template ELEM\n * @param {Array<Array<ELEM>>} arr\n * @return {Array<ELEM>}\n */\nexport const flatten = arr => arr.reduce((acc, val) => acc.concat(val), [])\n\nexport const isArray = Array.isArray\n\n/**\n * @template T\n * @param {Array<T>} arr\n * @return {Array<T>}\n */\nexport const unique = arr => from(set.from(arr))\n\n/**\n * @template T\n * @template M\n * @param {Array<T>} arr\n * @param {function(T):M} mapper\n * @return {Array<T>}\n */\nexport const uniqueBy = (arr, mapper) => {\n /**\n * @type {Set<M>}\n */\n const happened = set.create()\n /**\n * @type {Array<T>}\n */\n const result = []\n for (let i = 0; i < arr.length; i++) {\n const el = arr[i]\n const mapped = mapper(el)\n if (!happened.has(mapped)) {\n happened.add(mapped)\n result.push(el)\n }\n }\n return result\n}\n"],"names":["set.from","set.create"],"mappings":";;;;AAAA;AACA;AACA;AACA;AACA;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,IAAI,GAAG,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,EAAC;AAC9C;AACA;AACA;AACA;AACA;AACY,MAAC,MAAM,GAAG,+BAA+B,EAAE,EAAC;AACxD;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,IAAI,GAAG,CAAC,6BAA6B,CAAC,CAAC,KAAK,EAAE,EAAC;AAC5D;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,QAAQ,GAAG,CAAC,IAAI,EAAE,GAAG,KAAK;AACvC,EAAE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;AACvC,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAC;AACrB,GAAG;AACH,EAAC;AACD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,IAAI,GAAG,KAAK,CAAC,KAAI;AAC9B;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,KAAK,GAAG,CAAC,GAAG,EAAE,CAAC,KAAK,GAAG,CAAC,KAAK,CAAC,CAAC,EAAC;AAC7C;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,IAAI,GAAG,CAAC,GAAG,EAAE,CAAC,KAAK,GAAG,CAAC,IAAI,CAAC,CAAC,EAAC;AAC3C;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,SAAS,GAAG,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM,IAAI,KAAK,CAAC,CAAC,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,IAAI,KAAK,CAAC,CAAC,KAAK,CAAC,EAAC;AACxG;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,OAAO,GAAG,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,GAAG,KAAK,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,EAAE,EAAC;AAC3E;AACY,MAAC,OAAO,GAAG,KAAK,CAAC,QAAO;AACpC;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,MAAM,GAAG,GAAG,IAAI,IAAI,CAACA,QAAQ,CAAC,GAAG,CAAC,EAAC;AAChD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,QAAQ,GAAG,CAAC,GAAG,EAAE,MAAM,KAAK;AACzC;AACA;AACA;AACA,EAAE,MAAM,QAAQ,GAAGC,UAAU,GAAE;AAC/B;AACA;AACA;AACA,EAAE,MAAM,MAAM,GAAG,GAAE;AACnB,EAAE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;AACvC,IAAI,MAAM,EAAE,GAAG,GAAG,CAAC,CAAC,EAAC;AACrB,IAAI,MAAM,MAAM,GAAG,MAAM,CAAC,EAAE,EAAC;AAC7B,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE;AAC/B,MAAM,QAAQ,CAAC,GAAG,CAAC,MAAM,EAAC;AAC1B,MAAM,MAAM,CAAC,IAAI,CAAC,EAAE,EAAC;AACrB,KAAK;AACL,GAAG;AACH,EAAE,OAAO,MAAM;AACf;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/array.cjs
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
require('./set-b596ef38.cjs');
|
|
6
|
+
var array = require('./array-42d0736b.cjs');
|
|
6
7
|
|
|
7
8
|
|
|
8
9
|
|
|
@@ -16,4 +17,6 @@ exports.from = array.from;
|
|
|
16
17
|
exports.isArray = array.isArray;
|
|
17
18
|
exports.last = array.last;
|
|
18
19
|
exports.some = array.some;
|
|
20
|
+
exports.unique = array.unique;
|
|
21
|
+
exports.uniqueBy = array.uniqueBy;
|
|
19
22
|
//# sourceMappingURL=array.cjs.map
|
package/dist/array.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"array.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;"}
|
package/dist/array.d.ts
CHANGED
|
@@ -21,4 +21,6 @@ export function some<S>(arr: S[], f: (arg0: S, arg1: number, arg2: S[]) => boole
|
|
|
21
21
|
export function equalFlat<ELEM>(a: ELEM[], b: ELEM[]): boolean;
|
|
22
22
|
export function flatten<ELEM>(arr: ELEM[][]): ELEM[];
|
|
23
23
|
export const isArray: (arg: any) => arg is any[];
|
|
24
|
+
export function unique<T_1>(arr: T_1[]): T_1[];
|
|
25
|
+
export function uniqueBy<T_1, M>(arr: T_1[], mapper: (arg0: T_1) => M): T_1[];
|
|
24
26
|
//# sourceMappingURL=array.d.ts.map
|
package/dist/array.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array.d.ts","sourceRoot":"","sources":["../array.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"array.d.ts","sourceRoot":"","sources":["../array.js"],"names":[],"mappings":"AAeO,qCAAuC;AAMvC,iCAAiD;AAOjD,qCAAqD;AASrD,uDAIN;AAED;;;;;;;GAOG;AACH;;;;;EAA8B;AAYvB,+DAHmB,MAAM,mBAAe,OAAO,GAC1C,OAAO,CAE0B;AAWtC,qDAHgB,MAAM,gBAAY,OAAO,GACpC,OAAO,CAEwB;AASpC,uDAFK,OAAO,CAEqF;AAOjG,qDAAoE;AAE3E,iDAAoC;AAO7B,+CAAyC;AASzC,8EAkBN"}
|
package/dist/array.test.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export function testAppend(tc: t.TestCase): void;
|
|
2
2
|
export function testflatten(tc: t.TestCase): void;
|
|
3
3
|
export function testIsArray(tc: t.TestCase): void;
|
|
4
|
+
export function testUnique(tc: t.TestCase): void;
|
|
4
5
|
import * as t from "./testing.js";
|
|
5
6
|
//# sourceMappingURL=array.test.d.ts.map
|
package/dist/array.test.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array.test.d.ts","sourceRoot":"","sources":["../array.test.js"],"names":[],"mappings":"AAOO,+BAFI,EAAE,QAAQ,QAMpB;AAKM,gCAFI,EAAE,QAAQ,QAKpB;AAKM,gCAFI,EAAE,QAAQ,QASpB"}
|
|
1
|
+
{"version":3,"file":"array.test.d.ts","sourceRoot":"","sources":["../array.test.js"],"names":[],"mappings":"AAOO,+BAFI,EAAE,QAAQ,QAMpB;AAKM,gCAFI,EAAE,QAAQ,QAKpB;AAKM,gCAFI,EAAE,QAAQ,QASpB;AAKM,+BAFI,EAAE,QAAQ,QAOpB"}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var map = require('./map-28a001c9.cjs');
|
|
4
|
-
var
|
|
4
|
+
var set = require('./set-b596ef38.cjs');
|
|
5
|
+
var encoding = require('./buffer-22bb9fae.cjs');
|
|
5
6
|
var storage = require('./storage.cjs');
|
|
6
7
|
|
|
7
8
|
/* eslint-env browser */
|
|
@@ -49,7 +50,7 @@ const BC = typeof BroadcastChannel === 'undefined' ? LocalStoragePolyfill : Broa
|
|
|
49
50
|
*/
|
|
50
51
|
const getChannel = room =>
|
|
51
52
|
map.setIfUndefined(channels, room, () => {
|
|
52
|
-
const subs =
|
|
53
|
+
const subs = set.create();
|
|
53
54
|
const bc = new BC(room);
|
|
54
55
|
/* istanbul ignore next */
|
|
55
56
|
/**
|
|
@@ -116,4 +117,4 @@ exports.broadcastchannel = broadcastchannel;
|
|
|
116
117
|
exports.publish = publish;
|
|
117
118
|
exports.subscribe = subscribe;
|
|
118
119
|
exports.unsubscribe = unsubscribe;
|
|
119
|
-
//# sourceMappingURL=broadcastchannel-
|
|
120
|
+
//# sourceMappingURL=broadcastchannel-8d21c21c.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"broadcastchannel-8d21c21c.cjs","sources":["../broadcastchannel.js"],"sourcesContent":["/* eslint-env browser */\n\n/**\n * Helpers for cross-tab communication using broadcastchannel with LocalStorage fallback.\n *\n * ```js\n * // In browser window A:\n * broadcastchannel.subscribe('my events', data => console.log(data))\n * broadcastchannel.publish('my events', 'Hello world!') // => A: 'Hello world!' fires synchronously in same tab\n *\n * // In browser window B:\n * broadcastchannel.publish('my events', 'hello from tab B') // => A: 'hello from tab B'\n * ```\n *\n * @module broadcastchannel\n */\n\n// @todo before next major: use Uint8Array instead as buffer object\n\nimport * as map from './map.js'\nimport * as set from './set.js'\nimport * as buffer from './buffer.js'\nimport * as storage from './storage.js'\n\n/**\n * @typedef {Object} Channel\n * @property {Set<function(any, any):any>} Channel.subs\n * @property {any} Channel.bc\n */\n\n/**\n * @type {Map<string, Channel>}\n */\nconst channels = new Map()\n\n/* istanbul ignore next */\nclass LocalStoragePolyfill {\n /**\n * @param {string} room\n */\n constructor (room) {\n this.room = room\n /**\n * @type {null|function({data:ArrayBuffer}):void}\n */\n this.onmessage = null\n storage.onChange(e => e.key === room && this.onmessage !== null && this.onmessage({ data: buffer.fromBase64(e.newValue || '') }))\n }\n\n /**\n * @param {ArrayBuffer} buf\n */\n postMessage (buf) {\n storage.varStorage.setItem(this.room, buffer.toBase64(buffer.createUint8ArrayFromArrayBuffer(buf)))\n }\n}\n\n/* istanbul ignore next */\n// Use BroadcastChannel or Polyfill\nconst BC = typeof BroadcastChannel === 'undefined' ? LocalStoragePolyfill : BroadcastChannel\n\n/**\n * @param {string} room\n * @return {Channel}\n */\nconst getChannel = room =>\n map.setIfUndefined(channels, room, () => {\n const subs = set.create()\n const bc = new BC(room)\n /* istanbul ignore next */\n /**\n * @param {{data:ArrayBuffer}} e\n */\n bc.onmessage = e => subs.forEach(sub => sub(e.data, 'broadcastchannel'))\n return {\n bc, subs\n }\n })\n\n/**\n * Subscribe to global `publish` events.\n *\n * @function\n * @param {string} room\n * @param {function(any, any):any} f\n */\nexport const subscribe = (room, f) => {\n getChannel(room).subs.add(f)\n return f\n}\n\n/**\n * Unsubscribe from `publish` global events.\n *\n * @function\n * @param {string} room\n * @param {function(any, any):any} f\n */\nexport const unsubscribe = (room, f) => {\n const channel = getChannel(room)\n const unsubscribed = channel.subs.delete(f)\n /* istanbul ignore else */\n if (unsubscribed && channel.subs.size === 0) {\n channel.bc.close()\n channels.delete(room)\n }\n return unsubscribed\n}\n\n/**\n * Publish data to all subscribers (including subscribers on this tab)\n *\n * @function\n * @param {string} room\n * @param {any} data\n * @param {any} [origin]\n */\nexport const publish = (room, data, origin = null) => {\n const c = getChannel(room)\n c.bc.postMessage(data)\n c.subs.forEach(sub => sub(data, origin))\n}\n"],"names":["storage.onChange","buffer.fromBase64","storage.varStorage","buffer.toBase64","buffer.createUint8ArrayFromArrayBuffer","map.setIfUndefined","set.create"],"mappings":"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"}
|
|
@@ -3,14 +3,15 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
require('./map-28a001c9.cjs');
|
|
6
|
-
require('./
|
|
6
|
+
require('./set-b596ef38.cjs');
|
|
7
|
+
require('./buffer-22bb9fae.cjs');
|
|
7
8
|
require('./storage.cjs');
|
|
8
|
-
var broadcastchannel = require('./broadcastchannel-
|
|
9
|
+
var broadcastchannel = require('./broadcastchannel-8d21c21c.cjs');
|
|
9
10
|
require('./string-ad04f734.cjs');
|
|
10
|
-
require('./environment-
|
|
11
|
+
require('./environment-f6386808.cjs');
|
|
11
12
|
require('./conditions-fb475c70.cjs');
|
|
12
|
-
require('./function-
|
|
13
|
-
require('./array-
|
|
13
|
+
require('./function-40d38a5b.cjs');
|
|
14
|
+
require('./array-42d0736b.cjs');
|
|
14
15
|
require('./object-fe9c0021.cjs');
|
|
15
16
|
require('./binary-ac8e39e2.cjs');
|
|
16
17
|
require('./math-08e068f9.cjs');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"broadcastchannel.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"broadcastchannel.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"broadcastchannel.d.ts","sourceRoot":"","sources":["../broadcastchannel.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"broadcastchannel.d.ts","sourceRoot":"","sources":["../broadcastchannel.js"],"names":[],"mappings":"AAsFO,gCAHI,MAAM,YACG,GAAG,QAAE,GAAG,KAAE,GAAG,UAAb,GAAG,QAAE,GAAG,KAAE,GAAG,CAKhC;AASM,kCAHI,MAAM,YACG,GAAG,QAAE,GAAG,KAAE,GAAG,WAWhC;AAUM,8BAJI,MAAM,QACN,GAAG,WACH,GAAG,QAMb;;UA/Fa,WAAa,GAAG,QAAE,GAAG,KAAE,GAAG,CAAC;QAC3B,GAAG"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var string = require('./string-ad04f734.cjs');
|
|
4
|
-
var environment = require('./environment-
|
|
4
|
+
var environment = require('./environment-f6386808.cjs');
|
|
5
5
|
var binary = require('./binary-ac8e39e2.cjs');
|
|
6
6
|
var math = require('./math-08e068f9.cjs');
|
|
7
7
|
var number = require('./number-e62129bc.cjs');
|
|
@@ -1812,4 +1812,4 @@ exports.writeVarInt = writeVarInt;
|
|
|
1812
1812
|
exports.writeVarString = writeVarString;
|
|
1813
1813
|
exports.writeVarUint = writeVarUint;
|
|
1814
1814
|
exports.writeVarUint8Array = writeVarUint8Array;
|
|
1815
|
-
//# sourceMappingURL=buffer-
|
|
1815
|
+
//# sourceMappingURL=buffer-22bb9fae.cjs.map
|