@johnhenry/graceholder 0.0.0-stage → 0.0.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 (64) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +492 -2
  3. package/STABILITY.md +24 -0
  4. package/package.json +126 -4
  5. package/src/adapters/apollo.d.ts +13 -0
  6. package/src/adapters/apollo.js +48 -0
  7. package/src/adapters/fetch.d.ts +15 -0
  8. package/src/adapters/fetch.js +150 -0
  9. package/src/adapters/shared.d.ts +14 -0
  10. package/src/adapters/shared.js +65 -0
  11. package/src/adapters/urql.d.ts +8 -0
  12. package/src/adapters/urql.js +53 -0
  13. package/src/anchor.js +13 -0
  14. package/src/args.js +41 -0
  15. package/src/bulk/csv.js +118 -0
  16. package/src/bulk/ndjson.js +10 -0
  17. package/src/bulk/plan.js +195 -0
  18. package/src/bulk/shape.js +67 -0
  19. package/src/bulk/source.js +24 -0
  20. package/src/bulk/sql.js +366 -0
  21. package/src/bulk/stream.js +55 -0
  22. package/src/cursor.js +25 -0
  23. package/src/dbg.d.ts +720 -0
  24. package/src/detmath.js +74 -0
  25. package/src/directives.js +113 -0
  26. package/src/dist.js +90 -0
  27. package/src/errors.js +17 -0
  28. package/src/execute.js +577 -0
  29. package/src/explain.js +473 -0
  30. package/src/fast-check.d.ts +31 -0
  31. package/src/fast-check.js +58 -0
  32. package/src/generate.js +84 -0
  33. package/src/generator-types.d.ts +90 -0
  34. package/src/generators.js +361 -0
  35. package/src/image.js +55 -0
  36. package/src/index.d.ts +507 -0
  37. package/src/index.js +62 -0
  38. package/src/infer.js +888 -0
  39. package/src/key.js +30 -0
  40. package/src/learn.js +397 -0
  41. package/src/naming.js +74 -0
  42. package/src/pack-registry.js +59 -0
  43. package/src/packs/PROVENANCE.md +26 -0
  44. package/src/packs/commerce.d.ts +4 -0
  45. package/src/packs/commerce.js +83 -0
  46. package/src/packs/finance.d.ts +4 -0
  47. package/src/packs/finance.js +180 -0
  48. package/src/packs/geo.d.ts +4 -0
  49. package/src/packs/geo.js +227 -0
  50. package/src/packs/health.d.ts +4 -0
  51. package/src/packs/health.js +76 -0
  52. package/src/packs/index.d.ts +7 -0
  53. package/src/packs/index.js +9 -0
  54. package/src/rng.js +89 -0
  55. package/src/rules.js +228 -0
  56. package/src/scenario.js +120 -0
  57. package/src/schema.js +148 -0
  58. package/src/spec.js +235 -0
  59. package/src/suggest.js +138 -0
  60. package/src/typed.d.ts +723 -0
  61. package/src/vocab/PROVENANCE.md +12 -0
  62. package/src/vocab/en.js +106 -0
  63. package/src/world.js +461 -0
  64. package/src/xxh64.js +87 -0
@@ -0,0 +1,55 @@
1
+ // stream(): the root collection of a query as a lazy sync and async iterable.
2
+ import { planStream, windows } from "./plan.js";
3
+
4
+ /**
5
+ * A lazily generated collection. Iterating generates one window of items at a time, so memory stays
6
+ * bounded however many items are consumed. Every iteration starts again at the first item.
7
+ */
8
+ export class Stream {
9
+ /**
10
+ * @param {any} query
11
+ * @param {any} options
12
+ * @param {any} [world]
13
+ */
14
+ constructor(query, options, world) {
15
+ this.plan = planStream(query, options, world);
16
+ /** what toSQL needs to plan the same stream again with entity keys */
17
+ this.config = { query, options, world };
18
+ }
19
+
20
+ /** The response key of the streamed root field. */
21
+ get field() {
22
+ return this.plan.key;
23
+ }
24
+
25
+ /** Windows of items (arrays of at most `chunkSize`), lazily. */
26
+ *chunks() {
27
+ for (const w of windows(this.plan)) yield w.items;
28
+ }
29
+
30
+ *[Symbol.iterator]() {
31
+ for (const w of windows(this.plan)) yield* w.items;
32
+ }
33
+
34
+ async *[Symbol.asyncIterator]() {
35
+ for (const w of windows(this.plan)) {
36
+ for (const item of w.items) yield item;
37
+ await Promise.resolve(); // let other work run between windows
38
+ }
39
+ }
40
+ }
41
+
42
+ /**
43
+ * The root collection of a query as a lazy iterable of items. Item k equals item k of the windowed
44
+ * query `field(offset: k, first: 1)`.
45
+ * @param {any} query
46
+ * @param {any} [options]
47
+ */
48
+ export function stream(query, options = {}) {
49
+ return new Stream(query, options, undefined);
50
+ }
51
+
52
+ /** Used by createWorld().stream. */
53
+ export function streamWorld(query, options, world) {
54
+ return new Stream(query, options, world);
55
+ }
package/src/cursor.js ADDED
@@ -0,0 +1,25 @@
1
+ // Opaque, stable cursors: base64url("gc1:" + index), unpadded (docs/design.md 3.4). FROZEN at 1.0.0.
2
+ import { GraceholderError } from "./errors.js";
3
+
4
+ const PREFIX = "gc1:";
5
+
6
+ export function encodeCursor(index) {
7
+ return btoa(PREFIX + index).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
8
+ }
9
+
10
+ /** @returns {number} the non-negative integer index inside the cursor */
11
+ export function decodeCursor(cursor) {
12
+ const bad = () => new GraceholderError("bad-cursor", `invalid cursor ${JSON.stringify(cursor)}`, { hint: "use a cursor returned by graceholder" });
13
+ if (typeof cursor !== "string" || !/^[A-Za-z0-9_-]+$/.test(cursor)) throw bad();
14
+ let text;
15
+ try {
16
+ const b64 = cursor.replace(/-/g, "+").replace(/_/g, "/");
17
+ text = atob(b64 + "=".repeat((4 - (b64.length % 4)) % 4));
18
+ } catch {
19
+ throw bad();
20
+ }
21
+ if (!text.startsWith(PREFIX) || !/^[0-9]+$/.test(text.slice(PREFIX.length))) throw bad();
22
+ const n = Number(text.slice(PREFIX.length));
23
+ if (!Number.isSafeInteger(n)) throw bad();
24
+ return n;
25
+ }