@dxos/echo-generator 0.6.12 → 0.6.13-main.548ca8d

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