@dxos/util 0.6.13 → 0.6.14-main.2b6a0f3

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 (56) hide show
  1. package/dist/lib/browser/index.mjs +106 -145
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node/index.cjs +97 -135
  5. package/dist/lib/node/index.cjs.map +4 -4
  6. package/dist/lib/node/meta.json +1 -1
  7. package/dist/lib/node-esm/index.mjs +1939 -0
  8. package/dist/lib/node-esm/index.mjs.map +7 -0
  9. package/dist/lib/node-esm/meta.json +1 -0
  10. package/dist/types/src/array-to-hex.d.ts +2 -0
  11. package/dist/types/src/array-to-hex.d.ts.map +1 -0
  12. package/dist/types/src/complex.d.ts +8 -8
  13. package/dist/types/src/complex.d.ts.map +1 -1
  14. package/dist/types/src/index.d.ts +5 -3
  15. package/dist/types/src/index.d.ts.map +1 -1
  16. package/dist/types/src/platform.browser.test.d.ts +2 -0
  17. package/dist/types/src/platform.browser.test.d.ts.map +1 -0
  18. package/dist/types/src/platform.node.test.d.ts +2 -0
  19. package/dist/types/src/platform.node.test.d.ts.map +1 -0
  20. package/dist/types/src/random.d.ts +1 -1
  21. package/dist/types/src/random.d.ts.map +1 -1
  22. package/dist/types/src/weak.d.ts +4 -4
  23. package/dist/types/src/weak.d.ts.map +1 -1
  24. package/package.json +9 -9
  25. package/src/array-to-hex.ts +23 -0
  26. package/src/array.test.ts +1 -2
  27. package/src/bitfield.test.ts +1 -2
  28. package/src/callback.test.ts +1 -2
  29. package/src/complex.test.ts +1 -2
  30. package/src/complex.ts +8 -8
  31. package/src/defer.test.ts +1 -2
  32. package/src/human-hash.test.ts +1 -2
  33. package/src/index.ts +5 -3
  34. package/src/join-tables.test.ts +1 -2
  35. package/src/order.test.ts +1 -2
  36. package/src/platform.browser.test.ts +11 -0
  37. package/src/platform.node.test.ts +11 -0
  38. package/src/random.ts +1 -1
  39. package/src/reducers.test.ts +1 -2
  40. package/src/safe-instanceof.test.ts +1 -2
  41. package/src/sort.test.ts +1 -2
  42. package/src/tracer.test.ts +3 -3
  43. package/src/types.test.ts +1 -2
  44. package/src/uint8array.test.ts +1 -2
  45. package/src/weak.test.ts +4 -8
  46. package/src/weak.ts +4 -4
  47. package/dist/types/src/params.d.ts +0 -22
  48. package/dist/types/src/params.d.ts.map +0 -1
  49. package/dist/types/src/params.test.d.ts +0 -2
  50. package/dist/types/src/params.test.d.ts.map +0 -1
  51. package/dist/types/src/platform.test.d.ts +0 -2
  52. package/dist/types/src/platform.test.d.ts.map +0 -1
  53. package/src/params.test.ts +0 -38
  54. package/src/params.ts +0 -68
  55. package/src/platform.test.ts +0 -17
  56. package/src/typings.d.ts +0 -5
@@ -2,8 +2,7 @@
2
2
  // Copyright 2022 DXOS.org
3
3
  //
4
4
 
5
- import { expect } from 'chai';
6
- import { describe, test } from 'vitest';
5
+ import { describe, expect, test } from 'vitest';
7
6
 
8
7
  import { createSetDispatch } from './callback';
9
8
 
@@ -2,8 +2,7 @@
2
2
  // Copyright 2020 DXOS.org
3
3
  //
4
4
 
5
- import { expect } from 'chai';
6
- import { test } from 'vitest';
5
+ import { expect, test } from 'vitest';
7
6
 
8
7
  import { PublicKey } from '@dxos/keys';
9
8
 
package/src/complex.ts CHANGED
@@ -78,21 +78,21 @@ export class ComplexSet<T> implements Set<T> {
78
78
  return this._values.size;
79
79
  }
80
80
 
81
- [Symbol.iterator](): IterableIterator<T> {
81
+ [Symbol.iterator](): SetIterator<T> {
82
82
  return this._values.values();
83
83
  }
84
84
 
85
- *entries(): IterableIterator<[T, T]> {
85
+ *entries(): SetIterator<[T, T]> {
86
86
  for (const value of this._values.values()) {
87
87
  yield [value, value];
88
88
  }
89
89
  }
90
90
 
91
- keys(): IterableIterator<T> {
91
+ keys(): SetIterator<T> {
92
92
  return this[Symbol.iterator]();
93
93
  }
94
94
 
95
- values(): IterableIterator<T> {
95
+ values(): SetIterator<T> {
96
96
  return this[Symbol.iterator]();
97
97
  }
98
98
 
@@ -218,22 +218,22 @@ export class ComplexMap<K, V> implements Map<K, V> {
218
218
  return this._keys.size;
219
219
  }
220
220
 
221
- *[Symbol.iterator](): IterableIterator<[K, V]> {
221
+ *[Symbol.iterator](): SetIterator<[K, V]> {
222
222
  for (const [primitive, key] of this._keys) {
223
223
  const value = this._values.get(primitive) ?? raise(new Error('Map corrupted.'));
224
224
  yield [key, value];
225
225
  }
226
226
  }
227
227
 
228
- entries(): IterableIterator<[K, V]> {
228
+ entries(): SetIterator<[K, V]> {
229
229
  return this[Symbol.iterator]();
230
230
  }
231
231
 
232
- keys(): IterableIterator<K> {
232
+ keys(): SetIterator<K> {
233
233
  return this._keys.values();
234
234
  }
235
235
 
236
- values(): IterableIterator<V> {
236
+ values(): SetIterator<V> {
237
237
  return this._values.values();
238
238
  }
239
239
 
package/src/defer.test.ts CHANGED
@@ -2,8 +2,7 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { expect } from 'chai';
6
- import { describe, test } from 'vitest';
5
+ import { describe, expect, test } from 'vitest';
7
6
 
8
7
  import { defer, deferAsync } from './defer';
9
8
 
@@ -2,8 +2,7 @@
2
2
  // Copyright 2020 DXOS.org
3
3
  //
4
4
 
5
- import { expect } from 'chai';
6
- import { test } from 'vitest';
5
+ import { expect, test } from 'vitest';
7
6
 
8
7
  import { createKeyPair, createId } from '@dxos/crypto';
9
8
  import { PublicKey } from '@dxos/keys';
package/src/index.ts CHANGED
@@ -7,6 +7,8 @@ export * from './assign';
7
7
  export * from './binder';
8
8
  export * from './bitfield';
9
9
  export * from './callback';
10
+ export * from './callback-collection';
11
+ export * from './chunk-array';
10
12
  export * from './circular-buffer';
11
13
  export * from './complex';
12
14
  export * from './defer';
@@ -21,7 +23,6 @@ export * from './json';
21
23
  export * from './map';
22
24
  export * from './map-values';
23
25
  export * from './order';
24
- export * from './params';
25
26
  export * from './pick';
26
27
  export * from './platform';
27
28
  export * from './random';
@@ -30,14 +31,15 @@ export * from './reducers';
30
31
  export * from './safe-await';
31
32
  export * from './safe-instanceof';
32
33
  export * from './safe-parse-json';
34
+ export * from './sliding-window-summary';
33
35
  export * from './sort';
36
+ export * from './sum';
34
37
  export * from './throw-unhandled-error';
35
38
  export * from './to-fallback';
36
39
  export * from './tracer';
37
40
  export * from './types';
38
41
  export * from './uint8array';
39
- export * from './sum';
40
42
  export * from './weak';
41
43
  export * from './sliding-window-summary';
42
- export * from './callback-collection';
43
44
  export * from './chunk-array';
45
+ export * from './array-to-hex';
@@ -2,8 +2,7 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { expect } from 'chai';
6
- import { describe, test } from 'vitest';
5
+ import { describe, expect, test } from 'vitest';
7
6
 
8
7
  import { joinTables } from './join-tables';
9
8
 
package/src/order.test.ts CHANGED
@@ -2,8 +2,7 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { expect } from 'chai';
6
- import { describe, test } from 'vitest';
5
+ import { describe, expect, test } from 'vitest';
7
6
 
8
7
  import { inferObjectOrder, inferRecordOrder } from './order';
9
8
 
@@ -0,0 +1,11 @@
1
+ //
2
+ // Copyright 2020 DXOS.org
3
+ //
4
+
5
+ import { expect, test } from 'vitest';
6
+
7
+ import { isNode } from './platform';
8
+
9
+ test('knows when running in node', () => {
10
+ expect(isNode()).to.be.false;
11
+ });
@@ -0,0 +1,11 @@
1
+ //
2
+ // Copyright 2020 DXOS.org
3
+ //
4
+
5
+ import { expect, test } from 'vitest';
6
+
7
+ import { isNode } from './platform';
8
+
9
+ test('knows when running in node', () => {
10
+ expect(isNode()).to.be.true;
11
+ });
package/src/random.ts CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  // TODO(burdon): Random class utility.
6
6
  // TODO(burdon): Make object { min, max }.
7
- export const randomInt = (max: number, min: number) => {
7
+ export const randomInt = (max: number, min = 0) => {
8
8
  min = Math.ceil(min);
9
9
  max = Math.floor(max);
10
10
  return Math.floor(Math.random() * (max - min + 1)) + min;
@@ -2,8 +2,7 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { expect } from 'chai';
6
- import { describe, test } from 'vitest';
5
+ import { describe, expect, test } from 'vitest';
7
6
 
8
7
  import { median, numericalValues } from './reducers';
9
8
 
@@ -2,8 +2,7 @@
2
2
  // Copyright 2022 DXOS.org
3
3
  //
4
4
 
5
- import { expect } from 'chai';
6
- import { describe, test } from 'vitest';
5
+ import { describe, expect, test } from 'vitest';
7
6
 
8
7
  import { safeInstanceof } from './safe-instanceof';
9
8
 
package/src/sort.test.ts CHANGED
@@ -2,8 +2,7 @@
2
2
  // Copyright 2022 DXOS.org
3
3
  //
4
4
 
5
- import { expect } from 'chai';
6
- import { describe, test } from 'vitest';
5
+ import { describe, expect, test } from 'vitest';
7
6
 
8
7
  import { compareMulti, compareScalar, compareObject, compareString } from './sort';
9
8
 
@@ -2,9 +2,9 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { expect } from 'chai';
6
- import { describe, test } from 'vitest';
5
+ import { describe, expect, test } from 'vitest';
7
6
 
7
+ import { randomInt } from './random';
8
8
  import { createBucketReducer, numericalValues, reduceGroupBy, reduceSeries, reduceSet } from './reducers';
9
9
  import { Tracer } from './tracer';
10
10
 
@@ -22,7 +22,7 @@ describe('Tracer', () => {
22
22
  const n = 20;
23
23
  for (let i = 0; i < n; i++) {
24
24
  tracer.emit(key);
25
- await sleep(Math.random() * 10);
25
+ await sleep(randomInt(10));
26
26
  }
27
27
 
28
28
  const events = tracer.get('test')!;
package/src/types.test.ts CHANGED
@@ -2,8 +2,7 @@
2
2
  // Copyright 2022 DXOS.org
3
3
  //
4
4
 
5
- import { expect } from 'chai';
6
- import { describe, test } from 'vitest';
5
+ import { describe, expect, test } from 'vitest';
7
6
 
8
7
  import { nonNullable } from './types';
9
8
 
@@ -2,8 +2,7 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { expect } from 'chai';
6
- import { test, describe } from 'vitest';
5
+ import { test, expect, describe } from 'vitest';
7
6
 
8
7
  import { arrayToBuffer, arrayToString, bufferToArray, stringToArray } from './uint8array';
9
8
 
package/src/weak.test.ts CHANGED
@@ -2,16 +2,14 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { expect } from 'chai';
6
- import { test, describe } from 'vitest';
7
- import waitForExpect from 'wait-for-expect';
5
+ import { test, expect, describe } from 'vitest';
8
6
 
9
7
  import { WeakDictionary } from './weak';
10
8
 
11
9
  describe('WeakDictionary', () => {
12
10
  // Skipped because it takes a long time for garbage collection to kick in (~8 sec)
13
11
  // but it works otherwise.
14
- test.skip('unref item gets garbage collected', async () => {
12
+ test.skip('unref item gets garbage collected', { timeout: 20_000 }, async () => {
15
13
  const map = new WeakDictionary<string, any>();
16
14
  const key = 'key';
17
15
 
@@ -32,10 +30,8 @@ describe('WeakDictionary', () => {
32
30
 
33
31
  setValue();
34
32
  // Garbage collection should remove the item because no references exist.
35
- await waitForExpect(() => {
36
- expect(map.size).to.equal(0);
37
- }, 20000);
33
+ await expect.poll(() => map.size, { timeout: 20_000 }).toEqual(0);
38
34
 
39
35
  expect(map.has(key)).to.equal(false);
40
- }, 20_000);
36
+ });
41
37
  });
package/src/weak.ts CHANGED
@@ -16,17 +16,17 @@ export class WeakDictionary<K, V extends object> implements Map<K, V> {
16
16
  entries?.forEach(([key, value]) => this._register(key, value));
17
17
  }
18
18
 
19
- *entries(): IterableIterator<[K, V]> {
19
+ *entries(): SetIterator<[K, V]> {
20
20
  for (const [key, value] of this._internal) {
21
21
  yield [key, value.deref()!];
22
22
  }
23
23
  }
24
24
 
25
- keys(): IterableIterator<K> {
25
+ keys(): SetIterator<K> {
26
26
  return this._internal.keys();
27
27
  }
28
28
 
29
- *values(): IterableIterator<V> {
29
+ *values(): SetIterator<V> {
30
30
  for (const value of this._internal.values()) {
31
31
  const deref = value.deref();
32
32
  if (!deref) {
@@ -36,7 +36,7 @@ export class WeakDictionary<K, V extends object> implements Map<K, V> {
36
36
  }
37
37
  }
38
38
 
39
- *[Symbol.iterator](): IterableIterator<[K, V]> {
39
+ *[Symbol.iterator](): SetIterator<[K, V]> {
40
40
  for (const [key, value] of this._internal) {
41
41
  yield [key, value.deref()!];
42
42
  }
@@ -1,22 +0,0 @@
1
- import type * as S from '@effect/schema/Schema';
2
- import { type Annotable } from '@effect/schema/Schema';
3
- type ParamKeyAnnotationType = string;
4
- export declare const ParamKeyAnnotation: (value: ParamKeyAnnotationType) => <S extends Annotable.All>(self: S) => Annotable.Self<S>;
5
- /**
6
- * HTTP params parser.
7
- */
8
- export declare class Params<T extends Record<string, any>> {
9
- private readonly _schema;
10
- constructor(_schema: S.Struct<T>);
11
- /**
12
- * Parse URL params.
13
- * @param url
14
- */
15
- parse(url: URL): T;
16
- /**
17
- * Update URL with params.
18
- */
19
- params(url: URL, values: T): URL;
20
- }
21
- export {};
22
- //# sourceMappingURL=params.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"params.d.ts","sourceRoot":"","sources":["../../../src/params.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,KAAK,CAAC,MAAM,uBAAuB,CAAC;AAEhD,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAMvD,KAAK,sBAAsB,GAAG,MAAM,CAAC;AAErC,eAAO,MAAM,kBAAkB,UACrB,sBAAsB,MAC7B,CAAC,SAAS,SAAS,CAAC,GAAG,QAAQ,CAAC,KAAG,SAAS,CAAC,IAAI,CAAC,CAAC,CACC,CAAC;AAExD;;GAEG;AACH,qBAAa,MAAM,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IACnC,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAAP,OAAO,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;IAEjD;;;OAGG;IACH,KAAK,CAAC,GAAG,EAAE,GAAG,GAAG,CAAC;IAqBlB;;OAEG;IACH,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,GAAG,GAAG;CAYjC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=params.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"params.test.d.ts","sourceRoot":"","sources":["../../../src/params.test.ts"],"names":[],"mappings":""}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=platform.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"platform.test.d.ts","sourceRoot":"","sources":["../../../src/platform.test.ts"],"names":[],"mappings":""}
@@ -1,38 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { Schema as S } from '@effect/schema';
6
- import { expect } from 'chai';
7
- import { describe, test } from 'vitest';
8
-
9
- import { ParamKeyAnnotation, Params } from './params';
10
-
11
- const InvitationUrl = S.Struct({
12
- accessToken: S.String,
13
- deviceInvitationCode: S.String.pipe(ParamKeyAnnotation('deviceInvitationCode')),
14
- spaceInvitationCode: S.String,
15
- experimental: S.Boolean,
16
- timeout: S.Number,
17
- });
18
-
19
- describe('Params', () => {
20
- test('parse', () => {
21
- const props = new Params(InvitationUrl);
22
-
23
- const values = props.parse(
24
- new URL('http://localhost?access_token=100&deviceInvitationCode=200&experimental=1&timeout=100'),
25
- );
26
- expect(values).to.deep.eq({
27
- accessToken: '100',
28
- deviceInvitationCode: '200',
29
- experimental: true,
30
- timeout: 100,
31
- });
32
-
33
- const url = props.params(new URL('http://localhost'), values);
34
- expect(url.toString()).to.eq(
35
- 'http://localhost/?access_token=100&deviceInvitationCode=200&experimental=true&timeout=100',
36
- );
37
- });
38
- });
package/src/params.ts DELETED
@@ -1,68 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { AST } from '@effect/schema';
6
- // eslint-disable-next-line import/no-duplicates
7
- import type * as S from '@effect/schema/Schema';
8
- // eslint-disable-next-line import/no-duplicates
9
- import { type Annotable } from '@effect/schema/Schema';
10
- import { type Some } from 'effect/Option';
11
- import { decamelize } from 'xcase';
12
-
13
- // TODO(burdon): Change annotations.
14
-
15
- type ParamKeyAnnotationType = string;
16
- const ParamKeyAnnotationId = Symbol.for('@dxos/schema/annotation/ParamKeyAnnotation');
17
- export const ParamKeyAnnotation =
18
- (value: ParamKeyAnnotationType) =>
19
- <S extends Annotable.All>(self: S): Annotable.Self<S> =>
20
- self.annotations({ [ParamKeyAnnotationId]: value });
21
-
22
- /**
23
- * HTTP params parser.
24
- */
25
- export class Params<T extends Record<string, any>> {
26
- constructor(private readonly _schema: S.Struct<T>) {}
27
-
28
- /**
29
- * Parse URL params.
30
- * @param url
31
- */
32
- parse(url: URL): T {
33
- return Object.entries(this._schema.fields).reduce<Record<string, any>>((acc, [key, type]) => {
34
- let v = url.searchParams.get(decamelize(key));
35
- if (v == null) {
36
- v = url.searchParams.get(key);
37
- }
38
-
39
- if (v != null) {
40
- if (AST.isNumberKeyword(type.ast)) {
41
- acc[key] = parseInt(v);
42
- } else if (AST.isBooleanKeyword(type.ast)) {
43
- acc[key] = v === 'true' || v === '1';
44
- } else {
45
- acc[key] = v;
46
- }
47
- }
48
-
49
- return acc;
50
- }, {}) as T;
51
- }
52
-
53
- /**
54
- * Update URL with params.
55
- */
56
- params(url: URL, values: T): URL {
57
- Object.entries(values).forEach(([key, value]) => {
58
- const type = this._schema.fields[key];
59
- if (type && value != null) {
60
- const { value: alt } = AST.getAnnotation(ParamKeyAnnotationId)(type.ast) as Some<ParamKeyAnnotationType>;
61
- const k = alt ?? decamelize(key);
62
- url.searchParams.set(k, String(value));
63
- }
64
- });
65
-
66
- return url;
67
- }
68
- }
@@ -1,17 +0,0 @@
1
- //
2
- // Copyright 2020 DXOS.org
3
- //
4
-
5
- import { expect } from 'chai';
6
- import { test } from 'vitest';
7
-
8
- import { isNode } from './platform';
9
-
10
- // TODO(dmaretskyi): Broken with vitest conversion.
11
- test.skip('knows when running in node', () => {
12
- if (mochaExecutor.environment === 'nodejs') {
13
- expect(isNode()).to.be.true;
14
- } else {
15
- expect(isNode()).to.be.false;
16
- }
17
- });
package/src/typings.d.ts DELETED
@@ -1,5 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- declare module 'xcase';