@dxos/echo-generator 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.
@@ -0,0 +1,704 @@
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, MutableSchema as MutableSchema2, EchoObject, ObjectAnnotationId, effectToJsonSchema, getObjectAnnotation as getObjectAnnotation2, 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 { 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.schema.getSchemaByTypename(typename);
97
+ if (existingSchema != null) {
98
+ return existingSchema;
99
+ }
100
+ this._space.db.add(schema.serializedSchema);
101
+ return this._space.db.schema.registerSchema(schema.serializedSchema);
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: 132,
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: 134,
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 = create2(StoredSchema, {
166
+ typename,
167
+ version: "1.0.0",
168
+ jsonSchema: {}
169
+ });
170
+ const updatedSchema = typeSchema.annotations({
171
+ [ObjectAnnotationId]: {
172
+ ...typeAnnotation,
173
+ schemaId: schemaToStore.id
174
+ }
175
+ });
176
+ schemaToStore.jsonSchema = effectToJsonSchema(updatedSchema);
177
+ return new MutableSchema2(schemaToStore);
178
+ };
179
+ var testSchemas = () => {
180
+ const document = createDynamicSchema("example.com/type/document", {
181
+ title: S.String.annotations({
182
+ description: "title of the document"
183
+ }),
184
+ content: S.String
185
+ });
186
+ const organization = createDynamicSchema("example.com/type/organization", {
187
+ name: S.String.annotations({
188
+ description: "name of the company or organization"
189
+ }),
190
+ website: S.String.annotations({
191
+ description: "public website URL"
192
+ }),
193
+ description: S.String.annotations({
194
+ description: "short summary of the company"
195
+ })
196
+ });
197
+ const contact = createDynamicSchema("example.com/type/contact", {
198
+ name: S.String.annotations({
199
+ description: "name of the person"
200
+ }),
201
+ email: S.String,
202
+ org: ref(organization),
203
+ lat: S.Number,
204
+ lng: S.Number
205
+ });
206
+ const project = createDynamicSchema("example.com/type/project", {
207
+ name: S.String.annotations({
208
+ description: "name of the project"
209
+ }),
210
+ description: S.String,
211
+ website: S.String,
212
+ repo: S.String,
213
+ status: S.String,
214
+ priority: S.Number,
215
+ active: S.Boolean,
216
+ org: ref(organization)
217
+ });
218
+ return {
219
+ ["example.com/type/document"]: document,
220
+ ["example.com/type/organization"]: organization,
221
+ ["example.com/type/contact"]: contact,
222
+ ["example.com/type/project"]: project
223
+ };
224
+ };
225
+ var testObjectGenerators = {
226
+ ["example.com/type/document"]: async () => ({
227
+ title: faker2.lorem.sentence(3),
228
+ content: faker2.lorem.sentences({
229
+ min: 1,
230
+ max: faker2.number.int({
231
+ min: 1,
232
+ max: 3
233
+ })
234
+ })
235
+ }),
236
+ ["example.com/type/organization"]: async () => ({
237
+ name: faker2.company.name(),
238
+ website: faker2.datatype.boolean({
239
+ probability: 0.3
240
+ }) ? faker2.internet.url() : void 0,
241
+ description: faker2.lorem.sentences()
242
+ }),
243
+ ["example.com/type/contact"]: async (provider) => {
244
+ const organizations = await provider?.("example.com/type/organization");
245
+ const location = faker2.datatype.boolean() ? faker2.helpers.arrayElement(locations) : void 0;
246
+ return {
247
+ name: faker2.person.fullName(),
248
+ email: faker2.datatype.boolean({
249
+ probability: 0.5
250
+ }) ? faker2.internet.email() : void 0,
251
+ org: organizations?.length && faker2.datatype.boolean({
252
+ probability: 0.3
253
+ }) ? faker2.helpers.arrayElement(organizations) : void 0,
254
+ ...location
255
+ };
256
+ },
257
+ ["example.com/type/project"]: async () => ({
258
+ name: faker2.commerce.productName(),
259
+ repo: faker2.datatype.boolean({
260
+ probability: 0.3
261
+ }) ? faker2.internet.url() : void 0,
262
+ status: faker2.helpers.arrayElement(Status),
263
+ priority: faker2.helpers.arrayElement(Priority),
264
+ active: faker2.datatype.boolean()
265
+ })
266
+ };
267
+ var testObjectMutators = {
268
+ ["example.com/type/document"]: async (object, params) => {
269
+ const accessor = createDocAccessor(object, [
270
+ "content"
271
+ ]);
272
+ for (let i = 0; i < params.count; i++) {
273
+ const length = object.content?.content?.length ?? 0;
274
+ accessor.handle.change((doc) => {
275
+ A.splice(doc, accessor.path.slice(), 0, params.maxContentLength >= length ? 0 : params.mutationSize, randomText(params.mutationSize));
276
+ });
277
+ }
278
+ },
279
+ ["example.com/type/organization"]: async () => {
280
+ throw new Error("Method not implemented.");
281
+ },
282
+ ["example.com/type/contact"]: async () => {
283
+ throw new Error("Method not implemented.");
284
+ },
285
+ ["example.com/type/project"]: async () => {
286
+ throw new Error("Method not implemented.");
287
+ }
288
+ };
289
+ var createTestObjectGenerator = () => new TestObjectGenerator(testSchemas(), testObjectGenerators);
290
+ var createSpaceObjectGenerator = (space) => new SpaceObjectGenerator(space, testSchemas(), testObjectGenerators, testObjectMutators);
291
+ var locations = [
292
+ {
293
+ lat: 139.74946157054467,
294
+ lng: 35.686962764371174
295
+ },
296
+ {
297
+ lat: -73.98196278740681,
298
+ lng: 40.75192492259464
299
+ },
300
+ {
301
+ lat: -99.1329340602939,
302
+ lng: 19.444388301415472
303
+ },
304
+ {
305
+ lat: 72.85504343876647,
306
+ lng: 19.0189362343566
307
+ },
308
+ {
309
+ lat: -46.62696583905523,
310
+ lng: -23.55673372837896
311
+ },
312
+ {
313
+ lat: 77.22805816860182,
314
+ lng: 28.671938757181522
315
+ },
316
+ {
317
+ lat: 121.43455881982015,
318
+ lng: 31.218398311228327
319
+ },
320
+ {
321
+ lat: 88.32272979950551,
322
+ lng: 22.49691515689642
323
+ },
324
+ {
325
+ lat: 90.40663360810754,
326
+ lng: 23.725005570312817
327
+ },
328
+ {
329
+ lat: -58.399477232331435,
330
+ lng: -34.600555749907414
331
+ },
332
+ {
333
+ lat: -118.18192636994041,
334
+ lng: 33.99192410876543
335
+ },
336
+ {
337
+ lat: 66.98806305137339,
338
+ lng: 24.87193814681484
339
+ },
340
+ {
341
+ lat: 31.248022361126118,
342
+ lng: 30.051906205103705
343
+ },
344
+ {
345
+ lat: -43.22696665284366,
346
+ lng: -22.923077315615956
347
+ },
348
+ {
349
+ lat: 135.4581989565952,
350
+ lng: 34.75198107491417
351
+ },
352
+ {
353
+ lat: 116.38633982565943,
354
+ lng: 39.93083808990906
355
+ },
356
+ {
357
+ lat: 120.9802713035424,
358
+ lng: 14.606104813440538
359
+ },
360
+ {
361
+ lat: 37.6135769672714,
362
+ lng: 55.75410998124818
363
+ },
364
+ {
365
+ lat: 29.008055727002613,
366
+ lng: 41.10694201243979
367
+ },
368
+ {
369
+ lat: 2.33138946713035,
370
+ lng: 48.86863878981461
371
+ },
372
+ {
373
+ lat: 126.99778513820195,
374
+ lng: 37.56829495838895
375
+ },
376
+ {
377
+ lat: 3.3895852125984334,
378
+ lng: 6.445207512093191
379
+ },
380
+ {
381
+ lat: 106.82749176247012,
382
+ lng: -6.172471846798885
383
+ },
384
+ {
385
+ lat: -87.75200083270931,
386
+ lng: 41.83193651927843
387
+ },
388
+ {
389
+ lat: 113.32306427226172,
390
+ lng: 23.14692716047989
391
+ },
392
+ {
393
+ lat: -0.11866770247593195,
394
+ lng: 51.5019405883275
395
+ },
396
+ {
397
+ lat: -77.05200795343472,
398
+ lng: -12.04606681752557
399
+ },
400
+ {
401
+ lat: 51.42239817500899,
402
+ lng: 35.673888627001304
403
+ },
404
+ {
405
+ lat: 15.313026023171744,
406
+ lng: -4.327778243275986
407
+ },
408
+ {
409
+ lat: -74.08528981377441,
410
+ lng: 4.598369421147822
411
+ },
412
+ {
413
+ lat: 114.1201772298325,
414
+ lng: 22.554316369677963
415
+ },
416
+ {
417
+ lat: 114.26807118958311,
418
+ lng: 30.581977209337822
419
+ },
420
+ {
421
+ lat: 114.18306345846304,
422
+ lng: 22.30692675357551
423
+ },
424
+ {
425
+ lat: 117.19807322410043,
426
+ lng: 39.13197212310894
427
+ },
428
+ {
429
+ lat: 80.27805287890033,
430
+ lng: 13.091933670856292
431
+ },
432
+ {
433
+ lat: 121.568333333333,
434
+ lng: 25.0358333333333
435
+ },
436
+ {
437
+ lat: 77.55806386521755,
438
+ lng: 12.97194099507442
439
+ },
440
+ {
441
+ lat: 100.51469879369489,
442
+ lng: 13.751945064087977
443
+ },
444
+ {
445
+ lat: 74.34807892054346,
446
+ lng: 31.56191739488844
447
+ },
448
+ {
449
+ lat: 106.59303578916195,
450
+ lng: 29.566922888044644
451
+ },
452
+ {
453
+ lat: 78.47800771287751,
454
+ lng: 17.401928991511454
455
+ },
456
+ {
457
+ lat: -70.66898671317483,
458
+ lng: -33.448067956934096
459
+ },
460
+ {
461
+ lat: -80.22605193945003,
462
+ lng: 25.789556555021534
463
+ },
464
+ {
465
+ lat: -43.916950376804834,
466
+ lng: -19.91308016391116
467
+ },
468
+ {
469
+ lat: -3.6852975446125242,
470
+ lng: 40.40197212311381
471
+ },
472
+ {
473
+ lat: -75.17194183200792,
474
+ lng: 40.001919022526465
475
+ },
476
+ {
477
+ lat: 72.57805776168215,
478
+ lng: 23.031998775062675
479
+ },
480
+ {
481
+ lat: 106.69308136207889,
482
+ lng: 10.781971309193409
483
+ },
484
+ {
485
+ lat: -79.42196665298843,
486
+ lng: 43.70192573640844
487
+ },
488
+ {
489
+ lat: 103.85387481909902,
490
+ lng: 1.2949793251059418
491
+ },
492
+ {
493
+ lat: 13.23248118266855,
494
+ lng: -8.836340255012658
495
+ },
496
+ {
497
+ lat: 44.391922914564134,
498
+ lng: 33.34059435615865
499
+ },
500
+ {
501
+ lat: 2.181424460619155,
502
+ lng: 41.385245438547486
503
+ },
504
+ {
505
+ lat: 88.32994665421205,
506
+ lng: 22.580390440861947
507
+ },
508
+ {
509
+ lat: -96.84196278749818,
510
+ lng: 32.82196968167733
511
+ },
512
+ {
513
+ lat: 123.44802765120869,
514
+ lng: 41.80692512604918
515
+ },
516
+ {
517
+ lat: 32.532233380011576,
518
+ lng: 15.590024084277673
519
+ },
520
+ {
521
+ lat: 73.84805776168719,
522
+ lng: 18.531963374654026
523
+ },
524
+ {
525
+ lat: 151.1832339501475,
526
+ lng: -33.91806510862875
527
+ },
528
+ {
529
+ lat: 30.314074200315076,
530
+ lng: 59.94096036375191
531
+ },
532
+ {
533
+ lat: 91.79802154756635,
534
+ lng: 22.33193814680459
535
+ },
536
+ {
537
+ lat: 113.74277634138707,
538
+ lng: 23.050834758613007
539
+ },
540
+ {
541
+ lat: -84.40189524187565,
542
+ lng: 33.83195971260585
543
+ },
544
+ {
545
+ lat: -71.07195953218684,
546
+ lng: 42.33190600170229
547
+ },
548
+ {
549
+ lat: 46.770795798688255,
550
+ lng: 24.642779007816443
551
+ },
552
+ {
553
+ lat: -95.341925149146,
554
+ lng: 29.821920243188856
555
+ },
556
+ {
557
+ lat: 105.8480683412422,
558
+ lng: 21.035273107737055
559
+ },
560
+ {
561
+ lat: -77.01136443943716,
562
+ lng: 38.901495235087054
563
+ },
564
+ {
565
+ lat: -103.33198008081848,
566
+ lng: 20.671961950508944
567
+ },
568
+ {
569
+ lat: 144.97307037590406,
570
+ lng: -37.81808545369631
571
+ },
572
+ {
573
+ lat: 29.948050030391755,
574
+ lng: 31.201965205759393
575
+ },
576
+ {
577
+ lat: 104.06807363094873,
578
+ lng: 30.671945877957796
579
+ },
580
+ {
581
+ lat: -83.0820016464927,
582
+ lng: 42.33190600170229
583
+ },
584
+ {
585
+ lat: 96.16473175266185,
586
+ lng: 16.785299963188777
587
+ },
588
+ {
589
+ lat: 108.89305043760862,
590
+ lng: 34.27697130928732
591
+ },
592
+ {
593
+ lat: -51.20195790450316,
594
+ lng: -30.048068770722466
595
+ },
596
+ {
597
+ lat: 121.465,
598
+ lng: 25.0127777777778
599
+ },
600
+ {
601
+ lat: 72.83809356897484,
602
+ lng: 21.20192960187819
603
+ },
604
+ {
605
+ lat: 109.60911291406296,
606
+ lng: 23.09653464659317
607
+ },
608
+ {
609
+ lat: -4.041994118507091,
610
+ lng: 5.321942826098564
611
+ },
612
+ {
613
+ lat: -47.91799814700306,
614
+ lng: -15.781394372878992
615
+ },
616
+ {
617
+ lat: 32.862445782356644,
618
+ lng: 39.929184444075474
619
+ },
620
+ {
621
+ lat: -100.33193064232995,
622
+ lng: 25.671940995125283
623
+ },
624
+ {
625
+ lat: 139.60202098994017,
626
+ lng: 35.43065615270891
627
+ },
628
+ {
629
+ lat: 118.77802846499208,
630
+ lng: 32.05196500231233
631
+ },
632
+ {
633
+ lat: -73.58524281670213,
634
+ lng: 45.50194506421502
635
+ },
636
+ {
637
+ lat: 106.7180927553083,
638
+ lng: 26.581988806001448
639
+ },
640
+ {
641
+ lat: -34.91755136960728,
642
+ lng: -8.073699467249241
643
+ },
644
+ {
645
+ lat: 126.64803904445057,
646
+ lng: 45.75192980542715
647
+ },
648
+ {
649
+ lat: -38.58192718342411,
650
+ lng: -3.7480720258257634
651
+ },
652
+ {
653
+ lat: -112.07193755969467,
654
+ lng: 33.5419257363676
655
+ },
656
+ {
657
+ lat: 117.67001623440774,
658
+ lng: 24.520375385531167
659
+ },
660
+ {
661
+ lat: -38.48193328693924,
662
+ lng: -12.968026046044827
663
+ },
664
+ {
665
+ lat: 129.00810170722048,
666
+ lng: 35.09699877511093
667
+ },
668
+ {
669
+ lat: -122.41716877355225,
670
+ lng: 37.76919562968743
671
+ },
672
+ {
673
+ lat: 28.028063865019476,
674
+ lng: -26.16809888138414
675
+ },
676
+ {
677
+ lat: 13.399602764700546,
678
+ lng: 52.523764522251156
679
+ },
680
+ {
681
+ lat: 3.048606670909237,
682
+ lng: 36.765010656628135
683
+ },
684
+ {
685
+ lat: 125.75274485499392,
686
+ lng: 39.02138455800434
687
+ },
688
+ {
689
+ lat: 12.481312562873995,
690
+ lng: 41.89790148509894
691
+ }
692
+ ];
693
+ export {
694
+ Priority,
695
+ SpaceObjectGenerator,
696
+ Status,
697
+ TestObjectGenerator,
698
+ TestSchemaType,
699
+ createSpaceObjectGenerator,
700
+ createTestObjectGenerator,
701
+ randomText,
702
+ range
703
+ };
704
+ //# sourceMappingURL=index.mjs.map