@dxos/echo-generator 0.6.13 → 0.6.14-main.1366248

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.
@@ -0,0 +1,703 @@
1
+ import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
+
3
+ // packages/core/echo/echo-generator/src/data.ts
4
+ import { next as A } from "@dxos/automerge/automerge";
5
+ import { createDocAccessor } from "@dxos/client/echo";
6
+ import { createStoredSchema, getObjectAnnotation as getObjectAnnotation2, ref, toJsonSchema, EchoObject, MutableSchema as MutableSchema2, ObjectAnnotationId, S } from "@dxos/echo-schema";
7
+ import { faker as faker2 } from "@dxos/random";
8
+
9
+ // packages/core/echo/echo-generator/src/generator.ts
10
+ import { Filter } from "@dxos/client/echo";
11
+ import { create, MutableSchema, getObjectAnnotation, getSchema, isReactiveObject } from "@dxos/echo-schema";
12
+ import { invariant } from "@dxos/invariant";
13
+ import { faker } from "@dxos/random";
14
+
15
+ // packages/core/echo/echo-generator/src/util.ts
16
+ var range = (fn, length) => Array.from({
17
+ length
18
+ }).map((_, i) => fn(i)).filter(Boolean);
19
+ var randomText = (length) => {
20
+ let result = "";
21
+ const characters = "abcdefghijklmnopqrstuvwxyz";
22
+ const charactersLength = characters.length;
23
+ for (let index = 0; index < length; index++) {
24
+ result += characters.charAt(Math.floor(Math.random() * charactersLength));
25
+ }
26
+ return result;
27
+ };
28
+
29
+ // packages/core/echo/echo-generator/src/generator.ts
30
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/echo/echo-generator/src/generator.ts";
31
+ var TestObjectGenerator = class {
32
+ // prettier-ignore
33
+ constructor(_schemas, _generators, _provider) {
34
+ this._schemas = _schemas;
35
+ this._generators = _generators;
36
+ this._provider = _provider;
37
+ }
38
+ get schemas() {
39
+ return Object.values(this._schemas);
40
+ }
41
+ getSchema(type) {
42
+ return this.schemas.find((schema) => getObjectAnnotation(schema).typename === type);
43
+ }
44
+ setSchema(type, schema) {
45
+ this._schemas[type] = schema;
46
+ }
47
+ async createObject({ types } = {}) {
48
+ const type = faker.helpers.arrayElement(types ?? Object.keys(this._schemas));
49
+ const data = await this._generators[type](this._provider);
50
+ if (isReactiveObject(data)) {
51
+ return data;
52
+ }
53
+ const schema = this.getSchema(type);
54
+ return schema ? create(schema, data) : create(data);
55
+ }
56
+ // TODO(burdon): Create batch.
57
+ // TODO(burdon): Based on dependencies (e.g., organization before contact).
58
+ async createObjects(map) {
59
+ const tasks = Object.entries(map).map(([type, count]) => {
60
+ return range(() => this.createObject({
61
+ types: [
62
+ type
63
+ ]
64
+ }), count);
65
+ }).flatMap((t) => t);
66
+ return Promise.all(tasks);
67
+ }
68
+ };
69
+ var SpaceObjectGenerator = class extends TestObjectGenerator {
70
+ constructor(_space, schemaMap, generators, _mutations) {
71
+ super(schemaMap, generators, async (type) => {
72
+ const schema = this.getSchema(type);
73
+ return (schema && (await this._space.db.query(Filter.schema(schema)).run()).objects) ?? [];
74
+ });
75
+ this._space = _space;
76
+ this._mutations = _mutations;
77
+ Object.entries(schemaMap).forEach(([type, dynamicSchema]) => {
78
+ const schema = this._maybeRegisterSchema(type, dynamicSchema);
79
+ this.setSchema(type, schema);
80
+ });
81
+ }
82
+ addSchemas() {
83
+ const result = [];
84
+ for (const [typename, schema] of Object.entries(this._schemas)) {
85
+ result.push(this._maybeRegisterSchema(typename, schema));
86
+ }
87
+ return result;
88
+ }
89
+ async createObject({ types } = {}) {
90
+ return this._space.db.add(await super.createObject({
91
+ types
92
+ }));
93
+ }
94
+ _maybeRegisterSchema(typename, schema) {
95
+ if (schema instanceof MutableSchema) {
96
+ const existingSchema = this._space.db.schemaRegistry.getSchema(typename);
97
+ if (existingSchema != null) {
98
+ return existingSchema;
99
+ }
100
+ this._space.db.add(schema.storedSchema);
101
+ return this._space.db.schemaRegistry.registerSchema(schema.storedSchema);
102
+ } else {
103
+ const existingSchema = this._space.db.graph.schemaRegistry.getSchema(typename);
104
+ if (existingSchema != null) {
105
+ return existingSchema;
106
+ }
107
+ this._space.db.graph.schemaRegistry.addSchema([
108
+ schema
109
+ ]);
110
+ return schema;
111
+ }
112
+ }
113
+ async mutateObject(object, params) {
114
+ invariant(this._mutations, "Mutations not defined.", {
115
+ F: __dxlog_file,
116
+ L: 131,
117
+ S: this,
118
+ A: [
119
+ "this._mutations",
120
+ "'Mutations not defined.'"
121
+ ]
122
+ });
123
+ const type = getObjectAnnotation(getSchema(object)).typename;
124
+ invariant(type && this._mutations?.[type], "Invalid object type.", {
125
+ F: __dxlog_file,
126
+ L: 133,
127
+ S: this,
128
+ A: [
129
+ "type && this._mutations?.[type]",
130
+ "'Invalid object type.'"
131
+ ]
132
+ });
133
+ await this._mutations[type](object, params);
134
+ }
135
+ async mutateObjects(objects, params) {
136
+ for (const object of objects) {
137
+ await this.mutateObject(object, params);
138
+ }
139
+ }
140
+ };
141
+
142
+ // packages/core/echo/echo-generator/src/data.ts
143
+ var Status = [
144
+ "pending",
145
+ "active",
146
+ "done"
147
+ ];
148
+ var Priority = [
149
+ 1,
150
+ 2,
151
+ 3,
152
+ 4,
153
+ 5
154
+ ];
155
+ var TestSchemaType;
156
+ (function(TestSchemaType2) {
157
+ TestSchemaType2["document"] = "example.com/type/document";
158
+ TestSchemaType2["organization"] = "example.com/type/organization";
159
+ TestSchemaType2["contact"] = "example.com/type/contact";
160
+ TestSchemaType2["project"] = "example.com/type/project";
161
+ })(TestSchemaType || (TestSchemaType = {}));
162
+ var createDynamicSchema = (typename, fields) => {
163
+ const typeSchema = S.partial(S.Struct(fields)).pipe(EchoObject(typename, "1.0.0"));
164
+ const typeAnnotation = getObjectAnnotation2(typeSchema);
165
+ const schemaToStore = createStoredSchema({
166
+ typename,
167
+ version: "0.1.0"
168
+ });
169
+ const updatedSchema = typeSchema.annotations({
170
+ [ObjectAnnotationId]: {
171
+ ...typeAnnotation,
172
+ schemaId: schemaToStore.id
173
+ }
174
+ });
175
+ schemaToStore.jsonSchema = toJsonSchema(updatedSchema);
176
+ return new MutableSchema2(schemaToStore);
177
+ };
178
+ var testSchemas = () => {
179
+ const document = createDynamicSchema("example.com/type/document", {
180
+ title: S.String.annotations({
181
+ description: "title of the document"
182
+ }),
183
+ content: S.String
184
+ });
185
+ const organization = createDynamicSchema("example.com/type/organization", {
186
+ name: S.String.annotations({
187
+ description: "name of the company or organization"
188
+ }),
189
+ website: S.String.annotations({
190
+ description: "public website URL"
191
+ }),
192
+ description: S.String.annotations({
193
+ description: "short summary of the company"
194
+ })
195
+ });
196
+ const contact = createDynamicSchema("example.com/type/contact", {
197
+ name: S.String.annotations({
198
+ description: "name of the person"
199
+ }),
200
+ email: S.String,
201
+ org: ref(organization),
202
+ lat: S.Number,
203
+ lng: S.Number
204
+ });
205
+ const project = createDynamicSchema("example.com/type/project", {
206
+ name: S.String.annotations({
207
+ description: "name of the project"
208
+ }),
209
+ description: S.String,
210
+ website: S.String,
211
+ repo: S.String,
212
+ status: S.String,
213
+ priority: S.Number,
214
+ active: S.Boolean,
215
+ org: ref(organization)
216
+ });
217
+ return {
218
+ ["example.com/type/document"]: document,
219
+ ["example.com/type/organization"]: organization,
220
+ ["example.com/type/contact"]: contact,
221
+ ["example.com/type/project"]: project
222
+ };
223
+ };
224
+ var testObjectGenerators = {
225
+ ["example.com/type/document"]: async () => ({
226
+ title: faker2.lorem.sentence(3),
227
+ content: faker2.lorem.sentences({
228
+ min: 1,
229
+ max: faker2.number.int({
230
+ min: 1,
231
+ max: 3
232
+ })
233
+ })
234
+ }),
235
+ ["example.com/type/organization"]: async () => ({
236
+ name: faker2.company.name(),
237
+ website: faker2.datatype.boolean({
238
+ probability: 0.3
239
+ }) ? faker2.internet.url() : void 0,
240
+ description: faker2.lorem.sentences()
241
+ }),
242
+ ["example.com/type/contact"]: async (provider) => {
243
+ const organizations = await provider?.("example.com/type/organization");
244
+ const location = faker2.datatype.boolean() ? faker2.helpers.arrayElement(locations) : void 0;
245
+ return {
246
+ name: faker2.person.fullName(),
247
+ email: faker2.datatype.boolean({
248
+ probability: 0.5
249
+ }) ? faker2.internet.email() : void 0,
250
+ org: organizations?.length && faker2.datatype.boolean({
251
+ probability: 0.3
252
+ }) ? faker2.helpers.arrayElement(organizations) : void 0,
253
+ ...location
254
+ };
255
+ },
256
+ ["example.com/type/project"]: async () => ({
257
+ name: faker2.commerce.productName(),
258
+ repo: faker2.datatype.boolean({
259
+ probability: 0.3
260
+ }) ? faker2.internet.url() : void 0,
261
+ status: faker2.helpers.arrayElement(Status),
262
+ priority: faker2.helpers.arrayElement(Priority),
263
+ active: faker2.datatype.boolean()
264
+ })
265
+ };
266
+ var testObjectMutators = {
267
+ ["example.com/type/document"]: async (object, params) => {
268
+ const accessor = createDocAccessor(object, [
269
+ "content"
270
+ ]);
271
+ for (let i = 0; i < params.count; i++) {
272
+ const length = object.content?.content?.length ?? 0;
273
+ accessor.handle.change((doc) => {
274
+ A.splice(doc, accessor.path.slice(), 0, params.maxContentLength >= length ? 0 : params.mutationSize, randomText(params.mutationSize));
275
+ });
276
+ }
277
+ },
278
+ ["example.com/type/organization"]: async () => {
279
+ throw new Error("Method not implemented.");
280
+ },
281
+ ["example.com/type/contact"]: async () => {
282
+ throw new Error("Method not implemented.");
283
+ },
284
+ ["example.com/type/project"]: async () => {
285
+ throw new Error("Method not implemented.");
286
+ }
287
+ };
288
+ var createTestObjectGenerator = () => new TestObjectGenerator(testSchemas(), testObjectGenerators);
289
+ var createSpaceObjectGenerator = (space) => new SpaceObjectGenerator(space, testSchemas(), testObjectGenerators, testObjectMutators);
290
+ var locations = [
291
+ {
292
+ lat: 139.74946157054467,
293
+ lng: 35.686962764371174
294
+ },
295
+ {
296
+ lat: -73.98196278740681,
297
+ lng: 40.75192492259464
298
+ },
299
+ {
300
+ lat: -99.1329340602939,
301
+ lng: 19.444388301415472
302
+ },
303
+ {
304
+ lat: 72.85504343876647,
305
+ lng: 19.0189362343566
306
+ },
307
+ {
308
+ lat: -46.62696583905523,
309
+ lng: -23.55673372837896
310
+ },
311
+ {
312
+ lat: 77.22805816860182,
313
+ lng: 28.671938757181522
314
+ },
315
+ {
316
+ lat: 121.43455881982015,
317
+ lng: 31.218398311228327
318
+ },
319
+ {
320
+ lat: 88.32272979950551,
321
+ lng: 22.49691515689642
322
+ },
323
+ {
324
+ lat: 90.40663360810754,
325
+ lng: 23.725005570312817
326
+ },
327
+ {
328
+ lat: -58.399477232331435,
329
+ lng: -34.600555749907414
330
+ },
331
+ {
332
+ lat: -118.18192636994041,
333
+ lng: 33.99192410876543
334
+ },
335
+ {
336
+ lat: 66.98806305137339,
337
+ lng: 24.87193814681484
338
+ },
339
+ {
340
+ lat: 31.248022361126118,
341
+ lng: 30.051906205103705
342
+ },
343
+ {
344
+ lat: -43.22696665284366,
345
+ lng: -22.923077315615956
346
+ },
347
+ {
348
+ lat: 135.4581989565952,
349
+ lng: 34.75198107491417
350
+ },
351
+ {
352
+ lat: 116.38633982565943,
353
+ lng: 39.93083808990906
354
+ },
355
+ {
356
+ lat: 120.9802713035424,
357
+ lng: 14.606104813440538
358
+ },
359
+ {
360
+ lat: 37.6135769672714,
361
+ lng: 55.75410998124818
362
+ },
363
+ {
364
+ lat: 29.008055727002613,
365
+ lng: 41.10694201243979
366
+ },
367
+ {
368
+ lat: 2.33138946713035,
369
+ lng: 48.86863878981461
370
+ },
371
+ {
372
+ lat: 126.99778513820195,
373
+ lng: 37.56829495838895
374
+ },
375
+ {
376
+ lat: 3.3895852125984334,
377
+ lng: 6.445207512093191
378
+ },
379
+ {
380
+ lat: 106.82749176247012,
381
+ lng: -6.172471846798885
382
+ },
383
+ {
384
+ lat: -87.75200083270931,
385
+ lng: 41.83193651927843
386
+ },
387
+ {
388
+ lat: 113.32306427226172,
389
+ lng: 23.14692716047989
390
+ },
391
+ {
392
+ lat: -0.11866770247593195,
393
+ lng: 51.5019405883275
394
+ },
395
+ {
396
+ lat: -77.05200795343472,
397
+ lng: -12.04606681752557
398
+ },
399
+ {
400
+ lat: 51.42239817500899,
401
+ lng: 35.673888627001304
402
+ },
403
+ {
404
+ lat: 15.313026023171744,
405
+ lng: -4.327778243275986
406
+ },
407
+ {
408
+ lat: -74.08528981377441,
409
+ lng: 4.598369421147822
410
+ },
411
+ {
412
+ lat: 114.1201772298325,
413
+ lng: 22.554316369677963
414
+ },
415
+ {
416
+ lat: 114.26807118958311,
417
+ lng: 30.581977209337822
418
+ },
419
+ {
420
+ lat: 114.18306345846304,
421
+ lng: 22.30692675357551
422
+ },
423
+ {
424
+ lat: 117.19807322410043,
425
+ lng: 39.13197212310894
426
+ },
427
+ {
428
+ lat: 80.27805287890033,
429
+ lng: 13.091933670856292
430
+ },
431
+ {
432
+ lat: 121.568333333333,
433
+ lng: 25.0358333333333
434
+ },
435
+ {
436
+ lat: 77.55806386521755,
437
+ lng: 12.97194099507442
438
+ },
439
+ {
440
+ lat: 100.51469879369489,
441
+ lng: 13.751945064087977
442
+ },
443
+ {
444
+ lat: 74.34807892054346,
445
+ lng: 31.56191739488844
446
+ },
447
+ {
448
+ lat: 106.59303578916195,
449
+ lng: 29.566922888044644
450
+ },
451
+ {
452
+ lat: 78.47800771287751,
453
+ lng: 17.401928991511454
454
+ },
455
+ {
456
+ lat: -70.66898671317483,
457
+ lng: -33.448067956934096
458
+ },
459
+ {
460
+ lat: -80.22605193945003,
461
+ lng: 25.789556555021534
462
+ },
463
+ {
464
+ lat: -43.916950376804834,
465
+ lng: -19.91308016391116
466
+ },
467
+ {
468
+ lat: -3.6852975446125242,
469
+ lng: 40.40197212311381
470
+ },
471
+ {
472
+ lat: -75.17194183200792,
473
+ lng: 40.001919022526465
474
+ },
475
+ {
476
+ lat: 72.57805776168215,
477
+ lng: 23.031998775062675
478
+ },
479
+ {
480
+ lat: 106.69308136207889,
481
+ lng: 10.781971309193409
482
+ },
483
+ {
484
+ lat: -79.42196665298843,
485
+ lng: 43.70192573640844
486
+ },
487
+ {
488
+ lat: 103.85387481909902,
489
+ lng: 1.2949793251059418
490
+ },
491
+ {
492
+ lat: 13.23248118266855,
493
+ lng: -8.836340255012658
494
+ },
495
+ {
496
+ lat: 44.391922914564134,
497
+ lng: 33.34059435615865
498
+ },
499
+ {
500
+ lat: 2.181424460619155,
501
+ lng: 41.385245438547486
502
+ },
503
+ {
504
+ lat: 88.32994665421205,
505
+ lng: 22.580390440861947
506
+ },
507
+ {
508
+ lat: -96.84196278749818,
509
+ lng: 32.82196968167733
510
+ },
511
+ {
512
+ lat: 123.44802765120869,
513
+ lng: 41.80692512604918
514
+ },
515
+ {
516
+ lat: 32.532233380011576,
517
+ lng: 15.590024084277673
518
+ },
519
+ {
520
+ lat: 73.84805776168719,
521
+ lng: 18.531963374654026
522
+ },
523
+ {
524
+ lat: 151.1832339501475,
525
+ lng: -33.91806510862875
526
+ },
527
+ {
528
+ lat: 30.314074200315076,
529
+ lng: 59.94096036375191
530
+ },
531
+ {
532
+ lat: 91.79802154756635,
533
+ lng: 22.33193814680459
534
+ },
535
+ {
536
+ lat: 113.74277634138707,
537
+ lng: 23.050834758613007
538
+ },
539
+ {
540
+ lat: -84.40189524187565,
541
+ lng: 33.83195971260585
542
+ },
543
+ {
544
+ lat: -71.07195953218684,
545
+ lng: 42.33190600170229
546
+ },
547
+ {
548
+ lat: 46.770795798688255,
549
+ lng: 24.642779007816443
550
+ },
551
+ {
552
+ lat: -95.341925149146,
553
+ lng: 29.821920243188856
554
+ },
555
+ {
556
+ lat: 105.8480683412422,
557
+ lng: 21.035273107737055
558
+ },
559
+ {
560
+ lat: -77.01136443943716,
561
+ lng: 38.901495235087054
562
+ },
563
+ {
564
+ lat: -103.33198008081848,
565
+ lng: 20.671961950508944
566
+ },
567
+ {
568
+ lat: 144.97307037590406,
569
+ lng: -37.81808545369631
570
+ },
571
+ {
572
+ lat: 29.948050030391755,
573
+ lng: 31.201965205759393
574
+ },
575
+ {
576
+ lat: 104.06807363094873,
577
+ lng: 30.671945877957796
578
+ },
579
+ {
580
+ lat: -83.0820016464927,
581
+ lng: 42.33190600170229
582
+ },
583
+ {
584
+ lat: 96.16473175266185,
585
+ lng: 16.785299963188777
586
+ },
587
+ {
588
+ lat: 108.89305043760862,
589
+ lng: 34.27697130928732
590
+ },
591
+ {
592
+ lat: -51.20195790450316,
593
+ lng: -30.048068770722466
594
+ },
595
+ {
596
+ lat: 121.465,
597
+ lng: 25.0127777777778
598
+ },
599
+ {
600
+ lat: 72.83809356897484,
601
+ lng: 21.20192960187819
602
+ },
603
+ {
604
+ lat: 109.60911291406296,
605
+ lng: 23.09653464659317
606
+ },
607
+ {
608
+ lat: -4.041994118507091,
609
+ lng: 5.321942826098564
610
+ },
611
+ {
612
+ lat: -47.91799814700306,
613
+ lng: -15.781394372878992
614
+ },
615
+ {
616
+ lat: 32.862445782356644,
617
+ lng: 39.929184444075474
618
+ },
619
+ {
620
+ lat: -100.33193064232995,
621
+ lng: 25.671940995125283
622
+ },
623
+ {
624
+ lat: 139.60202098994017,
625
+ lng: 35.43065615270891
626
+ },
627
+ {
628
+ lat: 118.77802846499208,
629
+ lng: 32.05196500231233
630
+ },
631
+ {
632
+ lat: -73.58524281670213,
633
+ lng: 45.50194506421502
634
+ },
635
+ {
636
+ lat: 106.7180927553083,
637
+ lng: 26.581988806001448
638
+ },
639
+ {
640
+ lat: -34.91755136960728,
641
+ lng: -8.073699467249241
642
+ },
643
+ {
644
+ lat: 126.64803904445057,
645
+ lng: 45.75192980542715
646
+ },
647
+ {
648
+ lat: -38.58192718342411,
649
+ lng: -3.7480720258257634
650
+ },
651
+ {
652
+ lat: -112.07193755969467,
653
+ lng: 33.5419257363676
654
+ },
655
+ {
656
+ lat: 117.67001623440774,
657
+ lng: 24.520375385531167
658
+ },
659
+ {
660
+ lat: -38.48193328693924,
661
+ lng: -12.968026046044827
662
+ },
663
+ {
664
+ lat: 129.00810170722048,
665
+ lng: 35.09699877511093
666
+ },
667
+ {
668
+ lat: -122.41716877355225,
669
+ lng: 37.76919562968743
670
+ },
671
+ {
672
+ lat: 28.028063865019476,
673
+ lng: -26.16809888138414
674
+ },
675
+ {
676
+ lat: 13.399602764700546,
677
+ lng: 52.523764522251156
678
+ },
679
+ {
680
+ lat: 3.048606670909237,
681
+ lng: 36.765010656628135
682
+ },
683
+ {
684
+ lat: 125.75274485499392,
685
+ lng: 39.02138455800434
686
+ },
687
+ {
688
+ lat: 12.481312562873995,
689
+ lng: 41.89790148509894
690
+ }
691
+ ];
692
+ export {
693
+ Priority,
694
+ SpaceObjectGenerator,
695
+ Status,
696
+ TestObjectGenerator,
697
+ TestSchemaType,
698
+ createSpaceObjectGenerator,
699
+ createTestObjectGenerator,
700
+ randomText,
701
+ range
702
+ };
703
+ //# sourceMappingURL=index.mjs.map