@bradensbay/globals-core 0.2.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 (98) hide show
  1. package/README.md +69 -0
  2. package/dist/src/allocator.d.ts +78 -0
  3. package/dist/src/allocator.d.ts.map +1 -0
  4. package/dist/src/allocator.js +211 -0
  5. package/dist/src/allocator.js.map +1 -0
  6. package/dist/src/arena.d.ts +81 -0
  7. package/dist/src/arena.d.ts.map +1 -0
  8. package/dist/src/arena.js +224 -0
  9. package/dist/src/arena.js.map +1 -0
  10. package/dist/src/checksum.d.ts +5 -0
  11. package/dist/src/checksum.d.ts.map +1 -0
  12. package/dist/src/checksum.js +35 -0
  13. package/dist/src/checksum.js.map +1 -0
  14. package/dist/src/draft.d.ts +48 -0
  15. package/dist/src/draft.d.ts.map +1 -0
  16. package/dist/src/draft.js +427 -0
  17. package/dist/src/draft.js.map +1 -0
  18. package/dist/src/errors.d.ts +56 -0
  19. package/dist/src/errors.d.ts.map +1 -0
  20. package/dist/src/errors.js +69 -0
  21. package/dist/src/errors.js.map +1 -0
  22. package/dist/src/external.d.ts +52 -0
  23. package/dist/src/external.d.ts.map +1 -0
  24. package/dist/src/external.js +68 -0
  25. package/dist/src/external.js.map +1 -0
  26. package/dist/src/hamt.d.ts +87 -0
  27. package/dist/src/hamt.d.ts.map +1 -0
  28. package/dist/src/hamt.js +643 -0
  29. package/dist/src/hamt.js.map +1 -0
  30. package/dist/src/history.d.ts +45 -0
  31. package/dist/src/history.d.ts.map +1 -0
  32. package/dist/src/history.js +49 -0
  33. package/dist/src/history.js.map +1 -0
  34. package/dist/src/index.d.ts +55 -0
  35. package/dist/src/index.d.ts.map +1 -0
  36. package/dist/src/index.js +38 -0
  37. package/dist/src/index.js.map +1 -0
  38. package/dist/src/inspect.d.ts +74 -0
  39. package/dist/src/inspect.d.ts.map +1 -0
  40. package/dist/src/inspect.js +127 -0
  41. package/dist/src/inspect.js.map +1 -0
  42. package/dist/src/layout.d.ts +130 -0
  43. package/dist/src/layout.d.ts.map +1 -0
  44. package/dist/src/layout.js +149 -0
  45. package/dist/src/layout.js.map +1 -0
  46. package/dist/src/liveness.d.ts +48 -0
  47. package/dist/src/liveness.d.ts.map +1 -0
  48. package/dist/src/liveness.js +92 -0
  49. package/dist/src/liveness.js.map +1 -0
  50. package/dist/src/owner.d.ts +130 -0
  51. package/dist/src/owner.d.ts.map +1 -0
  52. package/dist/src/owner.js +368 -0
  53. package/dist/src/owner.js.map +1 -0
  54. package/dist/src/reader.d.ts +122 -0
  55. package/dist/src/reader.d.ts.map +1 -0
  56. package/dist/src/reader.js +325 -0
  57. package/dist/src/reader.js.map +1 -0
  58. package/dist/src/readers.d.ts +60 -0
  59. package/dist/src/readers.d.ts.map +1 -0
  60. package/dist/src/readers.js +122 -0
  61. package/dist/src/readers.js.map +1 -0
  62. package/dist/src/retained.d.ts +39 -0
  63. package/dist/src/retained.d.ts.map +1 -0
  64. package/dist/src/retained.js +99 -0
  65. package/dist/src/retained.js.map +1 -0
  66. package/dist/src/schema.d.ts +82 -0
  67. package/dist/src/schema.d.ts.map +1 -0
  68. package/dist/src/schema.js +18 -0
  69. package/dist/src/schema.js.map +1 -0
  70. package/dist/src/store.d.ts +83 -0
  71. package/dist/src/store.d.ts.map +1 -0
  72. package/dist/src/store.js +127 -0
  73. package/dist/src/store.js.map +1 -0
  74. package/dist/src/strings.d.ts +37 -0
  75. package/dist/src/strings.d.ts.map +1 -0
  76. package/dist/src/strings.js +144 -0
  77. package/dist/src/strings.js.map +1 -0
  78. package/dist/src/tags.d.ts +47 -0
  79. package/dist/src/tags.d.ts.map +1 -0
  80. package/dist/src/tags.js +53 -0
  81. package/dist/src/tags.js.map +1 -0
  82. package/dist/src/values.d.ts +90 -0
  83. package/dist/src/values.d.ts.map +1 -0
  84. package/dist/src/values.js +458 -0
  85. package/dist/src/values.js.map +1 -0
  86. package/dist/src/vector.d.ts +64 -0
  87. package/dist/src/vector.d.ts.map +1 -0
  88. package/dist/src/vector.js +387 -0
  89. package/dist/src/vector.js.map +1 -0
  90. package/dist/src/verify.d.ts +38 -0
  91. package/dist/src/verify.d.ts.map +1 -0
  92. package/dist/src/verify.js +179 -0
  93. package/dist/src/verify.js.map +1 -0
  94. package/dist/src/view.d.ts +66 -0
  95. package/dist/src/view.d.ts.map +1 -0
  96. package/dist/src/view.js +278 -0
  97. package/dist/src/view.js.map +1 -0
  98. package/package.json +28 -0
@@ -0,0 +1,68 @@
1
+ import { ExternalRef } from "./values.js";
2
+ export class ExternalTier {
3
+ #nextHandle = 1;
4
+ #owned = new Map();
5
+ #transport;
6
+ #cache = new Map();
7
+ /**
8
+ * @param transport How a reader fetches a value it does not own. Omitted in the owner,
9
+ * which holds every value itself.
10
+ */
11
+ constructor(transport) {
12
+ this.#transport = transport;
13
+ }
14
+ /** Owner side. Store a value and get a handle that can go into shared state. */
15
+ put(value) {
16
+ const handle = this.#nextHandle;
17
+ this.#nextHandle += 1;
18
+ this.#owned.set(handle, value);
19
+ return new ExternalRef(handle);
20
+ }
21
+ /** Owner side. Drop a value. Reading its handle afterwards rejects. */
22
+ drop(reference) {
23
+ const handle = typeof reference === "number" ? reference : reference.handle;
24
+ this.#cache.delete(handle);
25
+ return this.#owned.delete(handle);
26
+ }
27
+ /**
28
+ * Read a value. Asynchronous in every process, including the owner, so that moving code
29
+ * between the owner and a window does not change whether it compiles.
30
+ */
31
+ async get(reference) {
32
+ const handle = typeof reference === "number" ? reference : reference.handle;
33
+ if (this.#owned.has(handle))
34
+ return this.#owned.get(handle);
35
+ const pending = this.#cache.get(handle);
36
+ if (pending !== undefined)
37
+ return pending;
38
+ if (this.#transport === undefined) {
39
+ throw new Error(`external handle ${handle} is not held here and no transport was supplied. In a ` +
40
+ "reader, construct the tier with the transport the integration provides.");
41
+ }
42
+ const request = this.#transport.fetch(handle);
43
+ this.#cache.set(handle, request);
44
+ try {
45
+ return await request;
46
+ }
47
+ catch (error) {
48
+ this.#cache.delete(handle);
49
+ throw error;
50
+ }
51
+ }
52
+ /** Owner side. Serve a fetch from another process. */
53
+ serve(handle) {
54
+ if (!this.#owned.has(handle)) {
55
+ throw new Error(`external handle ${handle} is not held by this owner`);
56
+ }
57
+ return this.#owned.get(handle);
58
+ }
59
+ /** Forget every cached replica. Called when the owner generation changes. */
60
+ invalidate() {
61
+ this.#cache.clear();
62
+ }
63
+ get size() {
64
+ return this.#owned.size;
65
+ }
66
+ }
67
+ export { ExternalRef };
68
+ //# sourceMappingURL=external.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"external.js","sourceRoot":"","sources":["../../src/external.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AA8B1C,MAAM,OAAO,YAAY;IACvB,WAAW,GAAG,CAAC,CAAC;IACP,MAAM,GAAG,IAAI,GAAG,EAAmB,CAAC;IACpC,UAAU,CAAgC;IAC1C,MAAM,GAAG,IAAI,GAAG,EAA4B,CAAC;IAEtD;;;OAGG;IACH,YAAY,SAA6B;QACvC,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;IAC9B,CAAC;IAED,gFAAgF;IAChF,GAAG,CAAC,KAAc;QAChB,MAAM,MAAM,GAAG,IAAI,CAAC,WAAW,CAAC;QAChC,IAAI,CAAC,WAAW,IAAI,CAAC,CAAC;QACtB,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;QAC/B,OAAO,IAAI,WAAW,CAAC,MAAM,CAAC,CAAC;IACjC,CAAC;IAED,uEAAuE;IACvE,IAAI,CAAC,SAA+B;QAClC,MAAM,MAAM,GAAG,OAAO,SAAS,KAAK,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC;QAC5E,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAC3B,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC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AAU;QACR,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IACtB,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC;IAC1B,CAAC;CACF;AAED,OAAO,EAAE,WAAW,EAAE,CAAC"}
@@ -0,0 +1,87 @@
1
+ import type { SharedArena } from "./arena.js";
2
+ import type { Allocator } from "./allocator.js";
3
+ import type { Slot } from "./values.js";
4
+ /**
5
+ * A hash array mapped trie, used for objects, maps, and sets.
6
+ *
7
+ * The point of it is structural sharing. Setting one field of an object with ten thousand
8
+ * keys copies the path from the root to that key, which is at most seven nodes, rather than
9
+ * the whole record. That is what keeps write cost logarithmic and makes retention of several
10
+ * versions affordable.
11
+ *
12
+ * Node layout, at the payload offset the allocator returns:
13
+ *
14
+ * word 0 kind, BITMAP or COLLISION
15
+ * BITMAP word 1 dataMap, word 2 nodeMap
16
+ * then popcount(dataMap) entries of three words: key tag, key payload, and a
17
+ * value slot packed as two words
18
+ * then popcount(nodeMap) child offsets, one word each
19
+ * COLLISION word 1 entry count, word 2 the shared hash
20
+ * then count entries of four words: key tag, key payload, value tag, value
21
+ * payload
22
+ *
23
+ * An explicit kind word costs four bytes per node and buys two things: collision nodes are
24
+ * distinguishable without stealing a bitmap value, and a wild offset that happens to land on
25
+ * plausible bitmaps is rejected rather than walked.
26
+ */
27
+ export declare const HAMT_BITMAP = 1213022720;
28
+ export declare const HAMT_COLLISION = 1213022721;
29
+ export declare const EMPTY_NODE = 0;
30
+ /** The hash a key slot contributes to the trie. */
31
+ export declare function keyHash(arena: SharedArena, key: Slot): number;
32
+ /**
33
+ * Key equality, SameValueZero.
34
+ *
35
+ * Interned strings compare by offset, which is the whole reason for interning: an object
36
+ * property lookup is an integer compare rather than a character walk. Doubles have to be
37
+ * compared by value, because two equal doubles live in two different blocks.
38
+ */
39
+ export declare function keyEquals(arena: SharedArena, a: Slot, b: Slot): boolean;
40
+ export interface HamtContext {
41
+ readonly arena: SharedArena;
42
+ readonly allocator: Allocator;
43
+ /** Nodes replaced on the copied path. The commit retires them. */
44
+ readonly retired: number[];
45
+ /**
46
+ * Every block this operation allocated.
47
+ *
48
+ * A commit that fails part way releases these, so a rejected write leaks nothing. It is a
49
+ * separate list from the retired one on purpose: retired blocks belong to the previous
50
+ * version and must survive a rollback, allocated ones must not.
51
+ */
52
+ readonly allocated: number[];
53
+ }
54
+ export declare function emptyNode(context: HamtContext): number;
55
+ /** Look a key up. Returns undefined when it is absent, which a null value slot cannot be. */
56
+ export declare function hamtGet(arena: SharedArena, node: number, key: Slot, hash: number): Slot | undefined;
57
+ /**
58
+ * Insert or replace a key, returning the new root of this subtree.
59
+ *
60
+ * Every node on the path from the root to the key is copied and the original is pushed onto
61
+ * the retired list. Nothing already published is mutated, which is what makes a reader
62
+ * holding an older root safe without any coordination.
63
+ */
64
+ export declare function hamtAssoc(context: HamtContext, node: number, key: Slot, hash: number, value: Slot, shift?: number): number;
65
+ /** Remove a key. Returns the node unchanged when the key is absent. */
66
+ export declare function hamtDissoc(context: HamtContext, node: number, key: Slot, hash: number, shift?: number): number;
67
+ export interface HamtEntry {
68
+ readonly key: Slot;
69
+ readonly value: Slot;
70
+ }
71
+ /** Every entry in the subtree. Order is the trie order, which is stable for a given set. */
72
+ export declare function hamtEntries(arena: SharedArena, node: number, out?: HamtEntry[], depth?: number, budget?: number): HamtEntry[];
73
+ export declare function hamtSize(arena: SharedArena, node: number, depth?: number): number;
74
+ /** Every node in the subtree, for retiring a whole structure the writer is discarding. */
75
+ export declare function hamtNodes(arena: SharedArena, node: number, out?: number[], depth?: number, budget?: number): number[];
76
+ /** Property names of an object node, as strings. */
77
+ export declare function hamtKeyStrings(arena: SharedArena, node: number): string[];
78
+ /**
79
+ * Look a string key up without interning it first.
80
+ *
81
+ * A reader cannot intern, because interning is a write, so it cannot compare keys by offset
82
+ * the way the writer does. It can still walk the trie by hash and compare only the one
83
+ * record it lands on, which is the same logarithmic walk with a character comparison at the
84
+ * end instead of an integer one.
85
+ */
86
+ export declare function hamtGetString(arena: SharedArena, node: number, text: string, hash: number): Slot | undefined;
87
+ //# sourceMappingURL=hamt.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hamt.d.ts","sourceRoot":"","sources":["../../src/hamt.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAC9C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAKhD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAExC;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,eAAO,MAAM,WAAW,aAAa,CAAC;AACtC,eAAO,MAAM,cAAc,aAAa,CAAC;AAazC,eAAO,MAAM,UAAU,IAAI,CAAC;AAa5B,mDAAmD;AACnD,wBAAgB,OAAO,CAAC,KAAK,EAAE,WAAW,EAAE,GAAG,EAAE,IAAI,GAAG,MAAM,CA8B7D;AAED;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,WAAW,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,GAAG,OAAO,CAqBvE;AAED,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC;IAC5B,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC;IAC9B,kEAAkE;IAClE,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,CAAC;IAC3B;;;;;;OAMG;IACH,QAAQ,CAAC,SAAS,EAAE,MAAM,EAAE,CAAC;CAC9B;AAgFD,wBAAgB,SAAS,CAAC,OAAO,EAAE,WAAW,GAAG,MAAM,CAEtD;AAED,6FAA6F;AAC7F,wBAAgB,OAAO,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAgDnG;AAmED;;;;;;G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