@highstate/contract 0.21.1 → 0.27.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.
package/dist/index.js CHANGED
@@ -256,6 +256,22 @@ function isEntityIncludeRef(value) {
256
256
  const entity = value.entity;
257
257
  return typeof entity === "function" || isEntity(entity);
258
258
  }
259
+ var objectEntity = {
260
+ type: "system.object.v1",
261
+ schema: z2.object().loose(),
262
+ model: {
263
+ type: "system.object.v1",
264
+ definitionHash: 0,
265
+ schema: z2.object().loose().toJSONSchema(),
266
+ meta: {
267
+ title: "Object",
268
+ description: "The common ancestor of all entities. Any entity can be assigned to this entity.",
269
+ color: "#9e9e9e",
270
+ icon: "mdi:cube",
271
+ iconColor: "#9e9e9e"
272
+ }
273
+ }
274
+ };
259
275
  function defineEntity(options) {
260
276
  try {
261
277
  entityModelSchema.shape.type.parse(options.type);
@@ -292,28 +308,47 @@ function defineEntity(options) {
292
308
  shape[includeRef.field] = schema;
293
309
  return shape;
294
310
  }, {});
295
- let finalSchema = Object.values(options.extends ?? {}).reduce((schema, entity) => z2.intersection(schema, entity.schema), options.schema);
311
+ const inheritedExtensions = [...Object.values(options.extends ?? {}), objectEntity].filter((entity) => entity.type !== options.type);
312
+ const uniqueExtensions = Array.from(new Map(inheritedExtensions.map((entity) => [entity.type, entity])).values());
313
+ const schemaExtensions = uniqueExtensions.filter((entity) => entity.type !== objectEntity.type);
314
+ const strictIntersectionBase = (schema) => {
315
+ if (schema instanceof z2.ZodObject) {
316
+ return schema.strict();
317
+ }
318
+ return schema;
319
+ };
320
+ let finalSchema = schemaExtensions.reduce((schema, entity) => z2.intersection(schema, strictIntersectionBase(entity.schema)), strictIntersectionBase(options.schema));
296
321
  if (includeRefs.length > 0) {
297
- finalSchema = z2.intersection(finalSchema, z2.object(inclusionShape));
322
+ finalSchema = z2.intersection(finalSchema, strictIntersectionBase(z2.object(inclusionShape)));
323
+ }
324
+ finalSchema = z2.intersection(finalSchema, strictIntersectionBase(entityWithMetaSchema));
325
+ const optionalMultipleInclusions = includeRefs.filter((includeRef) => includeRef.multiple && !includeRef.required);
326
+ if (optionalMultipleInclusions.length > 0) {
327
+ finalSchema = finalSchema.overwrite((value) => {
328
+ const record = value;
329
+ for (const inclusion of optionalMultipleInclusions) {
330
+ record[inclusion.field] ??= [];
331
+ }
332
+ return value;
333
+ });
298
334
  }
299
- finalSchema = z2.intersection(finalSchema, entityWithMetaSchema);
300
335
  const directInclusions = () => includeRefs.map((includeRef) => ({
301
336
  type: includeRef.getEntity().type,
302
337
  required: includeRef.required,
303
338
  multiple: includeRef.multiple,
304
339
  field: includeRef.field
305
340
  }));
306
- const directExtensions = Object.values(options.extends ?? {}).map((entity) => entity.type);
341
+ const directExtensions = uniqueExtensions.map((entity) => entity.type);
307
342
  const getInclusions = () => {
308
343
  const incs = [...directInclusions()];
309
- for (const entity of Object.values(options.extends ?? {})) {
344
+ for (const entity of uniqueExtensions) {
310
345
  if (entity.model.inclusions) {
311
346
  incs.push(...entity.model.inclusions);
312
347
  }
313
348
  }
314
349
  return incs;
315
350
  };
316
- const extensions = Object.values(options.extends ?? {}).reduce((exts, entity) => {
351
+ const extensions = uniqueExtensions.reduce((exts, entity) => {
317
352
  exts.push(...entity.model.extensions ?? [], entity.type);
318
353
  return exts;
319
354
  }, []);
@@ -1416,9 +1451,65 @@ var unitWorkerSchema = z11.object({
1416
1451
  var workerRunOptionsSchema = z11.object({
1417
1452
  projectId: z11.cuid2(),
1418
1453
  workerVersionId: z11.cuid2(),
1454
+ workerInstanceId: z11.cuid2(),
1419
1455
  apiUrl: z11.url(),
1456
+ dataEndpoint: z11.string().min(1),
1420
1457
  apiKey: z11.string()
1421
1458
  });
1459
+ // src/runtime.ts
1460
+ import { z as z12 } from "zod";
1461
+ var runtimeConfigGetInputSchema = z12.void();
1462
+ var runtimeConfigGetOutputSchema = unitConfigSchema;
1463
+ var runtimeResultSubmitInputSchema = z12.record(z12.string(), z12.unknown());
1464
+ var runtimeResultSubmitOutputSchema = z12.object({});
1465
+ var runtimeSidecarFileSchema = z12.object({
1466
+ path: z12.string(),
1467
+ content: z12.string(),
1468
+ secret: z12.boolean().default(false),
1469
+ mode: z12.number().int().optional()
1470
+ });
1471
+ var runtimeSidecarPortSchema = z12.object({
1472
+ name: z12.string(),
1473
+ containerPort: z12.number().int().positive().max(65535),
1474
+ protocol: z12.literal("tcp").default("tcp")
1475
+ });
1476
+ var runtimeSidecarReadinessSchema = z12.discriminatedUnion("type", [
1477
+ z12.object({
1478
+ type: z12.literal("tcp"),
1479
+ port: z12.string(),
1480
+ timeoutSeconds: z12.number().int().positive().default(30)
1481
+ }),
1482
+ z12.object({
1483
+ type: z12.literal("http"),
1484
+ port: z12.string(),
1485
+ path: z12.string(),
1486
+ statuses: z12.number().int().positive().array().default([200]),
1487
+ timeoutSeconds: z12.number().int().positive().default(30)
1488
+ }),
1489
+ z12.object({
1490
+ type: z12.literal("log"),
1491
+ pattern: z12.string(),
1492
+ timeoutSeconds: z12.number().int().positive().default(30)
1493
+ })
1494
+ ]);
1495
+ var runtimeSidecarStartInputSchema = z12.object({
1496
+ identity: z12.string().regex(/^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$/),
1497
+ image: z12.string(),
1498
+ command: z12.string().array().optional(),
1499
+ args: z12.string().array().default([]),
1500
+ env: z12.record(z12.string(), z12.string()).default({}),
1501
+ files: runtimeSidecarFileSchema.array().default([]),
1502
+ ports: runtimeSidecarPortSchema.array().default([]),
1503
+ readiness: runtimeSidecarReadinessSchema.optional()
1504
+ });
1505
+ var runtimeSidecarStartOutputSchema = z12.object({
1506
+ id: z12.string(),
1507
+ host: z12.string(),
1508
+ ports: z12.record(z12.string(), z12.object({
1509
+ host: z12.string(),
1510
+ port: z12.number().int().positive().max(65535)
1511
+ }))
1512
+ });
1422
1513
  // src/utils.ts
1423
1514
  import { isNonNullish as isNonNullish2, pickBy as pickBy2 } from "remeda";
1424
1515
  function text(strings, ...values) {
@@ -1469,9 +1560,9 @@ function stripNullish(obj) {
1469
1560
  }
1470
1561
 
1471
1562
  // src/index.ts
1472
- import { z as z12 } from "zod";
1563
+ import { z as z13 } from "zod";
1473
1564
  export {
1474
- z12 as z,
1565
+ z13 as z,
1475
1566
  yamlValueSchema,
1476
1567
  workerRunOptionsSchema,
1477
1568
  versionedNameSchema,
@@ -1497,7 +1588,16 @@ export {
1497
1588
  serviceAccountMetaSchema,
1498
1589
  selectInput,
1499
1590
  secretSchema,
1591
+ runtimeSidecarStartOutputSchema,
1592
+ runtimeSidecarStartInputSchema,
1593
+ runtimeSidecarReadinessSchema,
1594
+ runtimeSidecarPortSchema,
1595
+ runtimeSidecarFileSchema,
1500
1596
  runtimeSchema,
1597
+ runtimeResultSubmitOutputSchema,
1598
+ runtimeResultSubmitInputSchema,
1599
+ runtimeConfigGetOutputSchema,
1600
+ runtimeConfigGetInputSchema,
1501
1601
  resetEvaluation,
1502
1602
  registerKnownAbbreviations,
1503
1603
  positionSchema,
@@ -1507,6 +1607,7 @@ export {
1507
1607
  pageBlockSchema,
1508
1608
  originalCreate,
1509
1609
  objectMetaSchema,
1610
+ objectEntity,
1510
1611
  kind,
1511
1612
  isUnitModel,
1512
1613
  isSecret,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@highstate/contract",
3
- "version": "0.21.1",
3
+ "version": "0.27.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -190,6 +190,17 @@ describe("defineComponent", () => {
190
190
  },
191
191
  ])
192
192
 
193
+ expect(
194
+ server.schema.parse({
195
+ $meta: {
196
+ type: "common.server.v1",
197
+ identity: "server-1",
198
+ },
199
+ tag: "prod",
200
+ endpoint: "127.0.0.1",
201
+ }).resource,
202
+ ).toEqual([])
203
+
193
204
  expect(
194
205
  server.schema.safeParse({
195
206
  $meta: {
package/src/entity.ts CHANGED
@@ -383,6 +383,37 @@ type AddSchemaInclusions<
383
383
  ? TSchema
384
384
  : z.ZodIntersection<TSchema, z.ZodObject<InclusionShape<TImplements>>>
385
385
 
386
+ /**
387
+ * The common ancestor of all entities. Any entity can be assigned to this entity.
388
+ */
389
+ export const objectEntity = {
390
+ type: "system.object.v1",
391
+ schema: z.object().loose(),
392
+ model: {
393
+ type: "system.object.v1",
394
+ definitionHash: 0,
395
+ schema: z.object().loose().toJSONSchema(),
396
+ meta: {
397
+ title: "Object",
398
+ description:
399
+ "The common ancestor of all entities. Any entity can be assigned to this entity.",
400
+ color: "#9e9e9e",
401
+ icon: "mdi:cube",
402
+ iconColor: "#9e9e9e",
403
+ },
404
+ },
405
+ } as unknown as Entity<"system.object.v1", { "system.object.v1": true }, z.ZodUnknown>
406
+
407
+ type DefineEntityTypeExtensions<
408
+ TImplementedTypes extends EntityTypes,
409
+ TExtends extends Record<string, Entity>,
410
+ > = AddTypeExtensions<TImplementedTypes, TExtends> & (typeof objectEntity)[typeof implementedTypes]
411
+
412
+ type DefineEntitySchemaExtensions<
413
+ TSchema extends z.ZodTypeAny,
414
+ TExtends extends Record<string, Entity>,
415
+ > = AddSchemaExtensions<TSchema, TExtends>
416
+
386
417
  export function defineEntity<
387
418
  TType extends VersionedName,
388
419
  TSchema extends z.ZodType,
@@ -392,8 +423,8 @@ export function defineEntity<
392
423
  options: EntityOptions<TType, TSchema, TExtends, TIncludes>,
393
424
  ): Entity<
394
425
  TType,
395
- Simplify<AddTypeExtensions<{ [K in TType]: true }, TExtends>>,
396
- MakeEntitySchema<AddSchemaExtensions<AddSchemaInclusions<TSchema, TIncludes>, TExtends>>,
426
+ DefineEntityTypeExtensions<{ [K in TType]: true }, TExtends>,
427
+ MakeEntitySchema<DefineEntitySchemaExtensions<AddSchemaInclusions<TSchema, TIncludes>, TExtends>>,
397
428
  DefineEntityValue<TSchema, TExtends, AddIncludeExtensions<TIncludes, TExtends>>,
398
429
  DefineEntityInput<TSchema, TExtends, AddIncludeExtensions<TIncludes, TExtends>>,
399
430
  AddIncludeExtensions<TIncludes, TExtends>
@@ -455,16 +486,49 @@ export function defineEntity<
455
486
  {} as Record<string, z.ZodTypeAny>,
456
487
  )
457
488
 
458
- let finalSchema = Object.values(options.extends ?? {}).reduce(
459
- (schema, entity) => z.intersection(schema, entity.schema),
460
- options.schema as z.ZodType,
489
+ const inheritedExtensions = [...Object.values(options.extends ?? {}), objectEntity].filter(
490
+ entity => entity.type !== options.type,
491
+ )
492
+
493
+ const uniqueExtensions = Array.from(
494
+ new Map(inheritedExtensions.map(entity => [entity.type, entity])).values(),
495
+ )
496
+
497
+ const schemaExtensions = uniqueExtensions.filter(entity => entity.type !== objectEntity.type)
498
+
499
+ const strictIntersectionBase = (schema: z.ZodTypeAny): z.ZodTypeAny => {
500
+ if (schema instanceof z.ZodObject) {
501
+ return schema.strict()
502
+ }
503
+
504
+ return schema
505
+ }
506
+
507
+ let finalSchema = schemaExtensions.reduce(
508
+ (schema, entity) => z.intersection(schema, strictIntersectionBase(entity.schema)),
509
+ strictIntersectionBase(options.schema as z.ZodTypeAny),
461
510
  )
462
511
 
463
512
  if (includeRefs.length > 0) {
464
- finalSchema = z.intersection(finalSchema, z.object(inclusionShape))
513
+ finalSchema = z.intersection(finalSchema, strictIntersectionBase(z.object(inclusionShape)))
465
514
  }
466
515
 
467
- finalSchema = z.intersection(finalSchema, entityWithMetaSchema)
516
+ finalSchema = z.intersection(finalSchema, strictIntersectionBase(entityWithMetaSchema))
517
+
518
+ const optionalMultipleInclusions = includeRefs.filter(
519
+ includeRef => includeRef.multiple && !includeRef.required,
520
+ )
521
+ if (optionalMultipleInclusions.length > 0) {
522
+ finalSchema = finalSchema.overwrite(value => {
523
+ const record = value as Record<string, unknown>
524
+
525
+ for (const inclusion of optionalMultipleInclusions) {
526
+ record[inclusion.field] ??= []
527
+ }
528
+
529
+ return value
530
+ })
531
+ }
468
532
 
469
533
  const directInclusions = () =>
470
534
  includeRefs.map(includeRef => ({
@@ -473,12 +537,12 @@ export function defineEntity<
473
537
  multiple: includeRef.multiple,
474
538
  field: includeRef.field,
475
539
  }))
476
- const directExtensions = Object.values(options.extends ?? {}).map(entity => entity.type)
540
+ const directExtensions = uniqueExtensions.map(entity => entity.type)
477
541
 
478
542
  const getInclusions = () => {
479
543
  const incs = [...directInclusions()]
480
544
 
481
- for (const entity of Object.values(options.extends ?? {})) {
545
+ for (const entity of uniqueExtensions) {
482
546
  if (entity.model.inclusions) {
483
547
  incs.push(...entity.model.inclusions)
484
548
  }
@@ -487,7 +551,7 @@ export function defineEntity<
487
551
  return incs
488
552
  }
489
553
 
490
- const extensions = Object.values(options.extends ?? {}).reduce((exts, entity) => {
554
+ const extensions = uniqueExtensions.reduce((exts, entity) => {
491
555
  exts.push(...(entity.model.extensions ?? []), entity.type)
492
556
 
493
557
  return exts
package/src/index.ts CHANGED
@@ -12,6 +12,7 @@ export {
12
12
  defineEntity,
13
13
  entityModelSchema,
14
14
  getEntityId,
15
+ objectEntity,
15
16
  } from "./entity"
16
17
 
17
18
  export * from "./instance"
@@ -23,6 +24,7 @@ export * from "./terminal"
23
24
  export * from "./page"
24
25
  export * from "./trigger"
25
26
  export * from "./worker"
27
+ export * from "./runtime"
26
28
  export * from "./cuidv2d"
27
29
 
28
30
  export {
package/src/runtime.ts ADDED
@@ -0,0 +1,74 @@
1
+ import { z } from "zod"
2
+ import { unitConfigSchema } from "./pulumi"
3
+
4
+ export const runtimeConfigGetInputSchema = z.void()
5
+
6
+ export const runtimeConfigGetOutputSchema = unitConfigSchema
7
+
8
+ export const runtimeResultSubmitInputSchema = z.record(z.string(), z.unknown())
9
+
10
+ export const runtimeResultSubmitOutputSchema = z.object({})
11
+
12
+ export const runtimeSidecarFileSchema = z.object({
13
+ path: z.string(),
14
+ content: z.string(),
15
+ secret: z.boolean().default(false),
16
+ mode: z.number().int().optional(),
17
+ })
18
+
19
+ export const runtimeSidecarPortSchema = z.object({
20
+ name: z.string(),
21
+ containerPort: z.number().int().positive().max(65535),
22
+ protocol: z.literal("tcp").default("tcp"),
23
+ })
24
+
25
+ export const runtimeSidecarReadinessSchema = z.discriminatedUnion("type", [
26
+ z.object({
27
+ type: z.literal("tcp"),
28
+ port: z.string(),
29
+ timeoutSeconds: z.number().int().positive().default(30),
30
+ }),
31
+ z.object({
32
+ type: z.literal("http"),
33
+ port: z.string(),
34
+ path: z.string(),
35
+ statuses: z.number().int().positive().array().default([200]),
36
+ timeoutSeconds: z.number().int().positive().default(30),
37
+ }),
38
+ z.object({
39
+ type: z.literal("log"),
40
+ pattern: z.string(),
41
+ timeoutSeconds: z.number().int().positive().default(30),
42
+ }),
43
+ ])
44
+
45
+ export const runtimeSidecarStartInputSchema = z.object({
46
+ identity: z.string().regex(/^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$/),
47
+ image: z.string(),
48
+ command: z.string().array().optional(),
49
+ args: z.string().array().default([]),
50
+ env: z.record(z.string(), z.string()).default({}),
51
+ files: runtimeSidecarFileSchema.array().default([]),
52
+ ports: runtimeSidecarPortSchema.array().default([]),
53
+ readiness: runtimeSidecarReadinessSchema.optional(),
54
+ })
55
+
56
+ export const runtimeSidecarStartOutputSchema = z.object({
57
+ id: z.string(),
58
+ host: z.string(),
59
+ ports: z.record(
60
+ z.string(),
61
+ z.object({
62
+ host: z.string(),
63
+ port: z.number().int().positive().max(65535),
64
+ }),
65
+ ),
66
+ })
67
+
68
+ export type RuntimeConfigGetInput = z.infer<typeof runtimeConfigGetInputSchema>
69
+ export type RuntimeConfigGetOutput = z.infer<typeof runtimeConfigGetOutputSchema>
70
+ export type RuntimeResultSubmitInput = z.infer<typeof runtimeResultSubmitInputSchema>
71
+ export type RuntimeResultSubmitOutput = z.infer<typeof runtimeResultSubmitOutputSchema>
72
+ export type RuntimeSidecarReadiness = z.infer<typeof runtimeSidecarReadinessSchema>
73
+ export type RuntimeSidecarStartInput = z.infer<typeof runtimeSidecarStartInputSchema>
74
+ export type RuntimeSidecarStartOutput = z.infer<typeof runtimeSidecarStartOutputSchema>
package/src/worker.ts CHANGED
@@ -19,11 +19,21 @@ export const workerRunOptionsSchema = z.object({
19
19
  */
20
20
  workerVersionId: z.cuid2(),
21
21
 
22
+ /**
23
+ * The ID of the concrete worker instance.
24
+ */
25
+ workerInstanceId: z.cuid2(),
26
+
22
27
  /**
23
28
  * The URL of the backend API to connect to.
24
29
  */
25
30
  apiUrl: z.url(),
26
31
 
32
+ /**
33
+ * The host and port of the worker data endpoint reachable by the frontend gateway.
34
+ */
35
+ dataEndpoint: z.string().min(1),
36
+
27
37
  /**
28
38
  * The API key used to authenticate the worker with the backend.
29
39
  */