@replit/river 0.200.0-rc.2 → 0.200.0-rc.4

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 (84) hide show
  1. package/README.md +21 -20
  2. package/dist/chunk-2BF4VMUZ.js +50 -0
  3. package/dist/chunk-2BF4VMUZ.js.map +1 -0
  4. package/dist/chunk-BZQQXMVF.js +401 -0
  5. package/dist/chunk-BZQQXMVF.js.map +1 -0
  6. package/dist/{chunk-4VNY34QG.js → chunk-F7Z2QQRL.js} +24 -18
  7. package/dist/chunk-F7Z2QQRL.js.map +1 -0
  8. package/dist/chunk-IMFMNIEO.js +384 -0
  9. package/dist/chunk-IMFMNIEO.js.map +1 -0
  10. package/dist/chunk-RDGHFHXN.js +658 -0
  11. package/dist/chunk-RDGHFHXN.js.map +1 -0
  12. package/dist/chunk-SI4YHBTI.js +277 -0
  13. package/dist/chunk-SI4YHBTI.js.map +1 -0
  14. package/dist/{chunk-7CKIN3JT.js → chunk-SIRRYRLQ.js} +73 -490
  15. package/dist/chunk-SIRRYRLQ.js.map +1 -0
  16. package/dist/{chunk-S5RL45KH.js → chunk-V57VWV5S.js} +80 -44
  17. package/dist/chunk-V57VWV5S.js.map +1 -0
  18. package/dist/{chunk-QMM35C3H.js → chunk-VXYHC666.js} +1 -1
  19. package/dist/chunk-VXYHC666.js.map +1 -0
  20. package/dist/client-f56a6da3.d.ts +49 -0
  21. package/dist/{connection-f900e390.d.ts → connection-11991b13.d.ts} +1 -5
  22. package/dist/connection-6031a354.d.ts +11 -0
  23. package/dist/context-73df8978.d.ts +528 -0
  24. package/dist/logging/index.cjs.map +1 -1
  25. package/dist/logging/index.d.cts +1 -1
  26. package/dist/logging/index.d.ts +1 -1
  27. package/dist/logging/index.js +1 -1
  28. package/dist/{index-10ebd26a.d.ts → message-fd349b27.d.ts} +31 -31
  29. package/dist/router/index.cjs +125 -502
  30. package/dist/router/index.cjs.map +1 -1
  31. package/dist/router/index.d.cts +11 -46
  32. package/dist/router/index.d.ts +11 -46
  33. package/dist/router/index.js +2 -4
  34. package/dist/server-9f31d98f.d.ts +42 -0
  35. package/dist/{services-970f97bb.d.ts → services-c67758fc.d.ts} +5 -602
  36. package/dist/transport/impls/uds/client.cjs +1247 -1238
  37. package/dist/transport/impls/uds/client.cjs.map +1 -1
  38. package/dist/transport/impls/uds/client.d.cts +4 -3
  39. package/dist/transport/impls/uds/client.d.ts +4 -3
  40. package/dist/transport/impls/uds/client.js +7 -13
  41. package/dist/transport/impls/uds/client.js.map +1 -1
  42. package/dist/transport/impls/uds/server.cjs +1311 -1170
  43. package/dist/transport/impls/uds/server.cjs.map +1 -1
  44. package/dist/transport/impls/uds/server.d.cts +4 -4
  45. package/dist/transport/impls/uds/server.d.ts +4 -4
  46. package/dist/transport/impls/uds/server.js +6 -6
  47. package/dist/transport/impls/ws/client.cjs +988 -987
  48. package/dist/transport/impls/ws/client.cjs.map +1 -1
  49. package/dist/transport/impls/ws/client.d.cts +6 -5
  50. package/dist/transport/impls/ws/client.d.ts +6 -5
  51. package/dist/transport/impls/ws/client.js +6 -7
  52. package/dist/transport/impls/ws/client.js.map +1 -1
  53. package/dist/transport/impls/ws/server.cjs +1192 -1066
  54. package/dist/transport/impls/ws/server.cjs.map +1 -1
  55. package/dist/transport/impls/ws/server.d.cts +4 -4
  56. package/dist/transport/impls/ws/server.d.ts +4 -4
  57. package/dist/transport/impls/ws/server.js +6 -6
  58. package/dist/transport/index.cjs +1446 -1380
  59. package/dist/transport/index.cjs.map +1 -1
  60. package/dist/transport/index.d.cts +4 -26
  61. package/dist/transport/index.d.ts +4 -26
  62. package/dist/transport/index.js +9 -9
  63. package/dist/util/testHelpers.cjs +746 -303
  64. package/dist/util/testHelpers.cjs.map +1 -1
  65. package/dist/util/testHelpers.d.cts +9 -4
  66. package/dist/util/testHelpers.d.ts +9 -4
  67. package/dist/util/testHelpers.js +36 -10
  68. package/dist/util/testHelpers.js.map +1 -1
  69. package/package.json +1 -1
  70. package/dist/chunk-47TFNAY2.js +0 -476
  71. package/dist/chunk-47TFNAY2.js.map +0 -1
  72. package/dist/chunk-4VNY34QG.js.map +0 -1
  73. package/dist/chunk-7CKIN3JT.js.map +0 -1
  74. package/dist/chunk-CZP4LK3F.js +0 -335
  75. package/dist/chunk-CZP4LK3F.js.map +0 -1
  76. package/dist/chunk-DJCW3SKT.js +0 -59
  77. package/dist/chunk-DJCW3SKT.js.map +0 -1
  78. package/dist/chunk-NQWDT6GS.js +0 -347
  79. package/dist/chunk-NQWDT6GS.js.map +0 -1
  80. package/dist/chunk-ONUXWVRC.js +0 -492
  81. package/dist/chunk-ONUXWVRC.js.map +0 -1
  82. package/dist/chunk-QMM35C3H.js.map +0 -1
  83. package/dist/chunk-S5RL45KH.js.map +0 -1
  84. package/dist/connection-3f117047.d.ts +0 -17
@@ -34,7 +34,6 @@ __export(router_exports, {
34
34
  createClientHandshakeOptions: () => createClientHandshakeOptions,
35
35
  createServer: () => createServer,
36
36
  createServerHandshakeOptions: () => createServerHandshakeOptions,
37
- diffServerSchema: () => diffServerSchema,
38
37
  serializeSchema: () => serializeSchema
39
38
  });
40
39
  module.exports = __toCommonJS(router_exports);
@@ -241,468 +240,6 @@ var ServiceScaffold = class {
241
240
  }
242
241
  };
243
242
 
244
- // router/diff.ts
245
- function diffServerSchema(oldServer, newServer, options) {
246
- const allServices = /* @__PURE__ */ new Set([
247
- ...Object.keys(oldServer.services),
248
- ...Object.keys(newServer.services)
249
- ]);
250
- const breakages = {};
251
- for (const serviceName of allServices) {
252
- const oldService = oldServer.services[serviceName];
253
- const newService = newServer.services[serviceName];
254
- const breakage = diffService(oldService, newService, options);
255
- if (breakage) {
256
- breakages[serviceName] = breakage;
257
- }
258
- }
259
- if (Object.keys(breakages).length) {
260
- return { serviceBreakages: breakages };
261
- }
262
- return null;
263
- }
264
- function diffService(oldService, newService, options) {
265
- if (!newService) {
266
- return options?.allowServiceRemoval ? null : { reason: "removed" };
267
- }
268
- if (!oldService) {
269
- return null;
270
- }
271
- const allProcedures = /* @__PURE__ */ new Set([
272
- ...Object.keys(oldService.procedures),
273
- ...Object.keys(newService.procedures)
274
- ]);
275
- const breakages = {};
276
- for (const procedureName of allProcedures) {
277
- const aProcedure = oldService.procedures[procedureName];
278
- const bProcedure = newService.procedures[procedureName];
279
- const breakage = diffProcedure(aProcedure, bProcedure, options);
280
- if (breakage) {
281
- breakages[procedureName] = breakage;
282
- }
283
- }
284
- if (Object.keys(breakages).length) {
285
- return { reason: "modified", procedureBreakages: breakages };
286
- }
287
- return null;
288
- }
289
- function diffProcedure(oldProcedure, newProcedure, options) {
290
- if (!newProcedure) {
291
- return options?.allowProcedureRemoval ? null : { reason: "removed" };
292
- }
293
- if (!oldProcedure) {
294
- return null;
295
- }
296
- if (oldProcedure.type !== newProcedure.type) {
297
- return {
298
- reason: "type-changed",
299
- oldType: oldProcedure.type,
300
- newType: newProcedure.type
301
- };
302
- }
303
- const inputBreakage = diffProcedureField(
304
- oldProcedure.input,
305
- newProcedure.input,
306
- "client"
307
- );
308
- const initBreakage = diffProcedureField(
309
- oldProcedure.init,
310
- newProcedure.init,
311
- "client"
312
- );
313
- const outputBreakage = diffProcedureField(
314
- oldProcedure.output,
315
- newProcedure.output,
316
- "server"
317
- );
318
- if (inputBreakage ?? initBreakage ?? outputBreakage) {
319
- const result = {
320
- reason: "modified"
321
- };
322
- if (inputBreakage) {
323
- result.input = inputBreakage;
324
- }
325
- if (initBreakage) {
326
- result.init = initBreakage;
327
- }
328
- if (outputBreakage) {
329
- result.output = outputBreakage;
330
- }
331
- return result;
332
- }
333
- return null;
334
- }
335
- function diffProcedureField(oldSchema, newSchema, origin) {
336
- if (!oldSchema && !newSchema) {
337
- return null;
338
- }
339
- const diffBreakage = diffRequired(oldSchema, newSchema, origin, false, false);
340
- if (diffBreakage) {
341
- return diffBreakage;
342
- }
343
- if (!oldSchema || !newSchema) {
344
- throw new Error("Appease typescript, this should never happen");
345
- }
346
- return diffJSONSchema(oldSchema, newSchema, origin);
347
- }
348
- function diffRequired(oldSchema, newSchema, origin, oldRequired, newRequired) {
349
- if (!newSchema && !oldSchema) {
350
- throw new Error("Both old and new schema are undefined");
351
- }
352
- if (!newSchema) {
353
- if (!oldRequired && origin == "server") {
354
- return null;
355
- }
356
- return { reason: "removed-required" };
357
- }
358
- if (!oldSchema) {
359
- if (newRequired && origin === "client") {
360
- return { reason: "new-required" };
361
- }
362
- return null;
363
- }
364
- if (origin === "client" && !oldRequired && newRequired) {
365
- return { reason: "new-required" };
366
- }
367
- if (origin === "server" && oldRequired && !newRequired) {
368
- return { reason: "removed-required" };
369
- }
370
- return null;
371
- }
372
- function diffJSONSchema(oldSchema, newSchema, origin) {
373
- if (oldSchema.type !== newSchema.type) {
374
- return {
375
- reason: "type-changed",
376
- oldType: getReportingType(oldSchema),
377
- newType: getReportingType(newSchema)
378
- };
379
- }
380
- if (getReportingType(oldSchema) !== getReportingType(newSchema)) {
381
- return {
382
- reason: "type-changed",
383
- oldType: getReportingType(oldSchema),
384
- newType: getReportingType(newSchema)
385
- };
386
- }
387
- if ("const" in oldSchema && "const" in newSchema && oldSchema.const !== newSchema.const) {
388
- return {
389
- reason: "type-changed",
390
- oldType: `${getReportingType(oldSchema)}-const-${oldSchema.const}`,
391
- newType: `${getReportingType(newSchema)}-const-${newSchema.const}`
392
- };
393
- }
394
- if ("const" in oldSchema && !("const" in newSchema) && origin === "server") {
395
- return {
396
- reason: "type-changed",
397
- oldType: `${getReportingType(oldSchema)}-const-${oldSchema.const}`,
398
- newType: getReportingType(newSchema)
399
- };
400
- }
401
- if ("const" in newSchema && !("const" in oldSchema) && origin === "client") {
402
- return {
403
- reason: "type-changed",
404
- oldType: getReportingType(oldSchema),
405
- newType: `${getReportingType(newSchema)}-const-${newSchema.const}`
406
- };
407
- }
408
- const breakages = {};
409
- if ("$ref" in newSchema) {
410
- if (newSchema.$ref !== oldSchema.$ref) {
411
- return {
412
- reason: "type-changed",
413
- oldType: getReportingType(oldSchema),
414
- newType: getReportingType(newSchema)
415
- };
416
- }
417
- } else if ("not" in newSchema) {
418
- const notBreakage = diffJSONSchema(
419
- oldSchema.not,
420
- newSchema.not,
421
- origin
422
- );
423
- if (notBreakage) {
424
- breakages.not = notBreakage;
425
- }
426
- } else if ("anyOf" in newSchema) {
427
- const oldAnyOfStringified = oldSchema.anyOf.map((el) => JSON.stringify(el)).sort();
428
- const newAnyOfStringified = newSchema.anyOf.map((el) => JSON.stringify(el)).sort();
429
- const anyOfBreakages = {};
430
- for (let i = 0; i < oldAnyOfStringified.length; i++) {
431
- if (newAnyOfStringified.includes(oldAnyOfStringified[i])) {
432
- continue;
433
- }
434
- if (!newAnyOfStringified[i]) {
435
- if (origin === "server") {
436
- continue;
437
- }
438
- anyOfBreakages[`old-${i}`] = { reason: "removed-required" };
439
- } else {
440
- const breakage = diffJSONSchema(
441
- JSON.parse(oldAnyOfStringified[i]),
442
- JSON.parse(newAnyOfStringified[i]),
443
- origin
444
- );
445
- if (breakage) {
446
- anyOfBreakages[`old-${i}`] = breakage;
447
- }
448
- }
449
- }
450
- for (let i = 0; i < newAnyOfStringified.length; i++) {
451
- if (oldAnyOfStringified.includes(newAnyOfStringified[i])) {
452
- continue;
453
- }
454
- if (!oldAnyOfStringified[i]) {
455
- if (origin === "client") {
456
- continue;
457
- }
458
- anyOfBreakages[`new-${i}`] = { reason: "new-required" };
459
- } else {
460
- const breakage = diffJSONSchema(
461
- JSON.parse(oldAnyOfStringified[i]),
462
- JSON.parse(newAnyOfStringified[i]),
463
- origin
464
- );
465
- if (breakage) {
466
- anyOfBreakages[`new-${i}`] = breakage;
467
- }
468
- }
469
- }
470
- if (Object.keys(anyOfBreakages).length > 0) {
471
- breakages.anyOf = {
472
- reason: "field-breakage",
473
- fieldBreakages: anyOfBreakages
474
- };
475
- }
476
- } else if ("oneOf" in newSchema) {
477
- throw new Error("oneOf is not supported, typebox does not emit it");
478
- } else if ("allOf" in newSchema) {
479
- if (newSchema.allOf.length !== oldSchema.allOf.length) {
480
- breakages.allOf = {
481
- reason: "type-changed",
482
- oldType: `${oldSchema.allOf}`,
483
- newType: `${newSchema.allOf}`
484
- };
485
- } else {
486
- for (let i = 0; i < newSchema.allOf.length; i++) {
487
- const breakage = diffJSONSchema(
488
- oldSchema.allOf[i],
489
- newSchema.allOf[i],
490
- origin
491
- );
492
- if (breakage) {
493
- breakages.allOf = breakage;
494
- break;
495
- }
496
- }
497
- }
498
- } else if (newSchema.type === "array") {
499
- const itemsBreakages = diffJSONSchema(
500
- oldSchema.items,
501
- newSchema.items,
502
- origin
503
- );
504
- if (itemsBreakages) {
505
- breakages.items = itemsBreakages;
506
- }
507
- if (oldSchema.minItems < newSchema.minItems) {
508
- if (origin === "client") {
509
- breakages.minItems = {
510
- reason: "type-changed",
511
- oldType: `${oldSchema.minItems}`,
512
- newType: `${newSchema.minItems}`
513
- };
514
- }
515
- } else if (oldSchema.minItems > newSchema.minItems) {
516
- if (origin === "server") {
517
- breakages.minItems = {
518
- reason: "type-changed",
519
- oldType: `${oldSchema.minItems}`,
520
- newType: `${newSchema.minItems}`
521
- };
522
- }
523
- }
524
- if (oldSchema.maxItems < newSchema.maxItems) {
525
- if (origin === "server") {
526
- breakages.maxItems = {
527
- reason: "type-changed",
528
- oldType: `${oldSchema.maxItems}`,
529
- newType: `${newSchema.maxItems}`
530
- };
531
- }
532
- } else if (oldSchema.maxItems > newSchema.maxItems) {
533
- if (origin === "client") {
534
- breakages.maxItems = {
535
- reason: "type-changed",
536
- oldType: `${oldSchema.maxItems}`,
537
- newType: `${newSchema.maxItems}`
538
- };
539
- }
540
- }
541
- if (!oldSchema.uniqueItems && newSchema.uniqueItems && origin === "client") {
542
- breakages.uniqueItems = {
543
- reason: "type-changed",
544
- oldType: `${!!oldSchema.uniqueItems}`,
545
- newType: `${!!newSchema.uniqueItems}`
546
- };
547
- }
548
- if ("contains" in newSchema !== "contains" in oldSchema) {
549
- if ("contains" in newSchema && !("contains" in oldSchema) && origin === "client") {
550
- breakages.contains = {
551
- reason: "type-changed",
552
- oldType: "no-contains",
553
- newType: "contains"
554
- };
555
- }
556
- } else if ("contains" in newSchema) {
557
- const containsBreakage = diffJSONSchema(
558
- oldSchema.contains,
559
- newSchema.contains,
560
- origin
561
- );
562
- if (containsBreakage) {
563
- breakages.contains = containsBreakage;
564
- }
565
- }
566
- if (oldSchema.minContains < newSchema.minContains) {
567
- if (origin === "client") {
568
- breakages.minContains = {
569
- reason: "type-changed",
570
- oldType: `${oldSchema.minContains}`,
571
- newType: `${newSchema.minContains}`
572
- };
573
- }
574
- } else if (oldSchema.minContains > newSchema.minContains) {
575
- if (origin === "server") {
576
- breakages.minContains = {
577
- reason: "type-changed",
578
- oldType: `${oldSchema.minContains}`,
579
- newType: `${newSchema.minContains}`
580
- };
581
- }
582
- }
583
- if (oldSchema.maxContains < newSchema.maxContains) {
584
- if (origin === "server") {
585
- breakages.maxContains = {
586
- reason: "type-changed",
587
- oldType: `${oldSchema.maxContains}`,
588
- newType: `${newSchema.maxContains}`
589
- };
590
- }
591
- } else if (oldSchema.maxContains > newSchema.maxContains) {
592
- if (origin === "client") {
593
- breakages.maxContains = {
594
- reason: "type-changed",
595
- oldType: `${oldSchema.maxContains}`,
596
- newType: `${newSchema.maxContains}`
597
- };
598
- }
599
- }
600
- } else if (newSchema.type === "object") {
601
- if ("properties" in newSchema !== "properties" in oldSchema) {
602
- return {
603
- reason: "type-changed",
604
- oldType: "properties" in oldSchema ? "probably-object" : "probably-record",
605
- newType: "properties" in newSchema ? "probably-object" : "probably-record"
606
- };
607
- }
608
- if ("properties" in newSchema) {
609
- const propertiesBreakages = diffObjectProperties(
610
- oldSchema.properties,
611
- newSchema.properties,
612
- origin,
613
- oldSchema.required,
614
- newSchema.required
615
- );
616
- if (Object.keys(propertiesBreakages).length) {
617
- breakages.properties = {
618
- reason: "field-breakage",
619
- fieldBreakages: propertiesBreakages
620
- };
621
- }
622
- }
623
- if ("patternProperties" in newSchema) {
624
- const patternPropertiesBreakages = diffObjectProperties(
625
- oldSchema.patternProperties,
626
- newSchema.patternProperties,
627
- origin,
628
- oldSchema.required,
629
- newSchema.required
630
- );
631
- if (Object.keys(patternPropertiesBreakages).length) {
632
- breakages.patternProperties = {
633
- reason: "field-breakage",
634
- fieldBreakages: patternPropertiesBreakages
635
- };
636
- }
637
- }
638
- if ("additionalProperties" in newSchema || "additionalProperties" in oldSchema) {
639
- throw new Error("additionalProperties is not supported");
640
- }
641
- if ("minProperties" in newSchema || "minProperties" in oldSchema) {
642
- throw new Error("minProperties is not supported");
643
- }
644
- if ("maxProperties" in newSchema || "maxProperties" in oldSchema) {
645
- throw new Error("maxProperties is not supported");
646
- }
647
- }
648
- if (Object.keys(breakages).length) {
649
- return {
650
- reason: "field-breakage",
651
- fieldBreakages: breakages
652
- };
653
- }
654
- return null;
655
- }
656
- function diffObjectProperties(oldProperties, newProperties, origin, oldRequiredProperties = [], newRequiredProperties = []) {
657
- const allProperties = /* @__PURE__ */ new Set([
658
- ...Object.keys(oldProperties),
659
- ...Object.keys(newProperties)
660
- ]);
661
- const breakages = {};
662
- for (const propertyName of allProperties) {
663
- const requiredBreakage = diffRequired(
664
- oldProperties[propertyName],
665
- newProperties[propertyName],
666
- origin,
667
- oldRequiredProperties.includes(propertyName),
668
- newRequiredProperties.includes(propertyName)
669
- );
670
- if (requiredBreakage) {
671
- breakages[propertyName] = requiredBreakage;
672
- } else if (oldProperties[propertyName] && newProperties[propertyName]) {
673
- const propertyBreakage = diffJSONSchema(
674
- oldProperties[propertyName],
675
- newProperties[propertyName],
676
- origin
677
- );
678
- if (propertyBreakage) {
679
- breakages[propertyName] = propertyBreakage;
680
- }
681
- }
682
- }
683
- return breakages;
684
- }
685
- function getReportingType(schema) {
686
- if ("not" in schema) {
687
- return "not";
688
- }
689
- if ("anyOf" in schema) {
690
- return "anyOf";
691
- }
692
- if ("allOf" in schema) {
693
- return "allOf";
694
- }
695
- if ("$ref" in schema) {
696
- return "$ref";
697
- }
698
- if (schema.type && typeof schema.type === "string") {
699
- return schema.type;
700
- }
701
- throw new Error(
702
- "Subschema not supported, probably a conditional subschema. Check logs."
703
- );
704
- }
705
-
706
243
  // router/result.ts
707
244
  var import_typebox2 = require("@sinclair/typebox");
708
245
  var ErrResultSchema = (t) => import_typebox2.Type.Object({
@@ -1109,7 +646,15 @@ var Procedure = {
1109
646
 
1110
647
  // transport/message.ts
1111
648
  var import_typebox4 = require("@sinclair/typebox");
649
+
650
+ // transport/id.ts
1112
651
  var import_nanoid = require("nanoid");
652
+ var alphabet = (0, import_nanoid.customAlphabet)(
653
+ "1234567890abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ"
654
+ );
655
+ var generateId = () => alphabet(12);
656
+
657
+ // transport/message.ts
1113
658
  var TransportMessageSchema = (t) => import_typebox4.Type.Object({
1114
659
  id: import_typebox4.Type.String(),
1115
660
  from: import_typebox4.Type.String(),
@@ -1143,18 +688,29 @@ var ControlMessageHandshakeRequestSchema = import_typebox4.Type.Object({
1143
688
  * used by the server to know whether this is a new or a reestablished connection, and whether it
1144
689
  * is compatible with what it already has.
1145
690
  */
1146
- expectedSessionState: import_typebox4.Type.Optional(
1147
- import_typebox4.Type.Object({
1148
- /**
1149
- * reconnect is set to true if the client explicitly wants to reestablish an existing
1150
- * connection.
1151
- */
1152
- reconnect: import_typebox4.Type.Boolean(),
1153
- nextExpectedSeq: import_typebox4.Type.Integer()
1154
- })
1155
- ),
691
+ expectedSessionState: import_typebox4.Type.Object({
692
+ // what the client expects the server to send next
693
+ nextExpectedSeq: import_typebox4.Type.Integer(),
694
+ // TODO: remove optional once we know all servers
695
+ // are nextSentSeq here
696
+ // what the server expects the client to send next
697
+ nextSentSeq: import_typebox4.Type.Optional(import_typebox4.Type.Integer())
698
+ }),
1156
699
  metadata: import_typebox4.Type.Optional(import_typebox4.Type.Unknown())
1157
700
  });
701
+ var HandshakeErrorRetriableResponseCodes = import_typebox4.Type.Union([
702
+ import_typebox4.Type.Literal("SESSION_STATE_MISMATCH")
703
+ ]);
704
+ var HandshakeErrorFatalResponseCodes = import_typebox4.Type.Union([
705
+ import_typebox4.Type.Literal("MALFORMED_HANDSHAKE_META"),
706
+ import_typebox4.Type.Literal("MALFORMED_HANDSHAKE"),
707
+ import_typebox4.Type.Literal("PROTOCOL_VERSION_MISMATCH"),
708
+ import_typebox4.Type.Literal("REJECTED_BY_CUSTOM_HANDLER")
709
+ ]);
710
+ var HandshakeErrorResponseCodes = import_typebox4.Type.Union([
711
+ HandshakeErrorRetriableResponseCodes,
712
+ HandshakeErrorFatalResponseCodes
713
+ ]);
1158
714
  var ControlMessageHandshakeResponseSchema = import_typebox4.Type.Object({
1159
715
  type: import_typebox4.Type.Literal("HANDSHAKE_RESP"),
1160
716
  status: import_typebox4.Type.Union([
@@ -1164,7 +720,10 @@ var ControlMessageHandshakeResponseSchema = import_typebox4.Type.Object({
1164
720
  }),
1165
721
  import_typebox4.Type.Object({
1166
722
  ok: import_typebox4.Type.Literal(false),
1167
- reason: import_typebox4.Type.String()
723
+ reason: import_typebox4.Type.String(),
724
+ // TODO: remove optional once we know all servers
725
+ // are sending code here
726
+ code: import_typebox4.Type.Optional(HandshakeErrorResponseCodes)
1168
727
  })
1169
728
  ])
1170
729
  });
@@ -1177,6 +736,31 @@ var ControlMessagePayloadSchema = import_typebox4.Type.Union([
1177
736
  var OpaqueTransportMessageSchema = TransportMessageSchema(
1178
737
  import_typebox4.Type.Unknown()
1179
738
  );
739
+ function closeStreamMessage(streamId) {
740
+ return {
741
+ streamId,
742
+ controlFlags: 8 /* StreamClosedBit */,
743
+ payload: {
744
+ type: "CLOSE"
745
+ }
746
+ };
747
+ }
748
+ function requestCloseStreamMessage(streamId) {
749
+ return {
750
+ streamId,
751
+ controlFlags: 16 /* StreamCloseRequestBit */,
752
+ payload: {
753
+ type: "CLOSE"
754
+ }
755
+ };
756
+ }
757
+ function abortMessage(streamId, payload) {
758
+ return {
759
+ streamId,
760
+ controlFlags: 4 /* StreamAbortBit */,
761
+ payload
762
+ };
763
+ }
1180
764
  function isStreamOpen(controlFlag) {
1181
765
  return (
1182
766
  /* eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison */
@@ -1202,14 +786,11 @@ function isStreamAbort(controlFlag) {
1202
786
  );
1203
787
  }
1204
788
 
1205
- // router/client.ts
1206
- var import_nanoid2 = require("nanoid");
1207
-
1208
789
  // tracing/index.ts
1209
790
  var import_api = require("@opentelemetry/api");
1210
791
 
1211
792
  // package.json
1212
- var version = "0.200.0-rc.2";
793
+ var version = "0.200.0-rc.4";
1213
794
 
1214
795
  // tracing/index.ts
1215
796
  function getPropagationContext(ctx) {
@@ -1305,7 +886,7 @@ function createClient(transport, serverId, providedClientOptions = {}) {
1305
886
  }
1306
887
  const clientOptions = { ...defaultClientOptions, ...providedClientOptions };
1307
888
  if (clientOptions.eagerlyConnect) {
1308
- void transport.connect(serverId);
889
+ transport.connect(serverId);
1309
890
  }
1310
891
  return _createRecursiveProxy((opts) => {
1311
892
  const [serviceName, procName, procMethod] = [...opts.path];
@@ -1315,8 +896,8 @@ function createClient(transport, serverId, providedClientOptions = {}) {
1315
896
  );
1316
897
  }
1317
898
  const [init, callOptions] = opts.args;
1318
- if (clientOptions.connectOnInvoke && !transport.connections.has(serverId)) {
1319
- void transport.connect(serverId);
899
+ if (clientOptions.connectOnInvoke && !transport.sessions.has(serverId)) {
900
+ transport.connect(serverId);
1320
901
  }
1321
902
  if (procMethod !== "rpc" && procMethod !== "subscribe" && procMethod !== "stream" && procMethod !== "upload") {
1322
903
  throw new Error(
@@ -1336,7 +917,7 @@ function createClient(transport, serverId, providedClientOptions = {}) {
1336
917
  }
1337
918
  function handleProc(procType, transport, serverId, init, serviceName, procedureName, abortSignal) {
1338
919
  const procClosesWithInit = procType === "rpc" || procType === "subscription";
1339
- const streamId = (0, import_nanoid2.nanoid)();
920
+ const streamId = generateId();
1340
921
  const { span, ctx } = createProcTelemetryInfo(
1341
922
  transport,
1342
923
  procType,
@@ -1356,14 +937,14 @@ function handleProc(procType, transport, serverId, init, serviceName, procedureN
1356
937
  inputWriter.onClose(() => {
1357
938
  span.addEvent("inputWriter closed");
1358
939
  if (!procClosesWithInit && cleanClose) {
1359
- transport.sendCloseControl(serverId, streamId);
940
+ transport.send(serverId, closeStreamMessage(streamId));
1360
941
  }
1361
942
  if (outputReader.isClosed()) {
1362
943
  cleanup();
1363
944
  }
1364
945
  });
1365
946
  const outputReader = new ReadStreamImpl(() => {
1366
- transport.sendRequestCloseControl(serverId, streamId);
947
+ transport.send(serverId, requestCloseStreamMessage(streamId));
1367
948
  });
1368
949
  outputReader.onClose(() => {
1369
950
  span.addEvent("outputReader closed");
@@ -1393,13 +974,15 @@ function handleProc(procType, transport, serverId, init, serviceName, procedureN
1393
974
  outputReader.triggerClose();
1394
975
  }
1395
976
  inputWriter.close();
1396
- transport.sendAbort(
977
+ transport.send(
1397
978
  serverId,
1398
- streamId,
1399
- Err({
1400
- code: ABORT_CODE,
1401
- message: "Aborted by client"
1402
- })
979
+ abortMessage(
980
+ streamId,
981
+ Err({
982
+ code: ABORT_CODE,
983
+ message: "Aborted by client"
984
+ })
985
+ )
1403
986
  );
1404
987
  }
1405
988
  function onMessage(msg) {
@@ -1638,7 +1221,9 @@ var RiverServer = class {
1638
1221
  controlFlags,
1639
1222
  initPayload,
1640
1223
  from,
1641
- tracingCtx
1224
+ sessionId,
1225
+ tracingCtx,
1226
+ protocolVersion
1642
1227
  }) {
1643
1228
  this.openStreams.add(streamId);
1644
1229
  let cleanClose = true;
@@ -1701,7 +1286,7 @@ var RiverServer = class {
1701
1286
  if (isStreamCloseRequest(msg.controlFlags)) {
1702
1287
  outputWriter.triggerCloseRequest();
1703
1288
  }
1704
- if (isStreamAbort(msg.controlFlags)) {
1289
+ if (isStreamAbortBackwardsCompat(msg.controlFlags, protocolVersion)) {
1705
1290
  let abortResult;
1706
1291
  if (import_value2.Value.Check(InputErrResultSchema, msg.payload)) {
1707
1292
  abortResult = msg.payload;
@@ -1768,7 +1353,7 @@ var RiverServer = class {
1768
1353
  })
1769
1354
  );
1770
1355
  }
1771
- if (isStreamClose(msg.controlFlags)) {
1356
+ if (isStreamCloseBackwardsCompat(msg.controlFlags, protocolVersion)) {
1772
1357
  inputReader.triggerClose();
1773
1358
  }
1774
1359
  };
@@ -1787,25 +1372,37 @@ var RiverServer = class {
1787
1372
  });
1788
1373
  onFinishedCallbacks.length = 0;
1789
1374
  };
1375
+ const procClosesWithResponse = procedure.type === "rpc" || procedure.type === "upload";
1790
1376
  const inputReader = new ReadStreamImpl(() => {
1791
- this.transport.sendRequestCloseControl(from, streamId);
1377
+ this.transport.send(from, requestCloseStreamMessage(streamId));
1792
1378
  });
1793
1379
  inputReader.onClose(() => {
1380
+ if (protocolVersion === "v1.1") {
1381
+ if (!procClosesWithResponse && !outputWriter.isClosed()) {
1382
+ outputWriter.close();
1383
+ }
1384
+ }
1794
1385
  if (outputWriter.isClosed()) {
1795
1386
  cleanup();
1796
1387
  }
1797
1388
  });
1798
- const procClosesWithResponse = procedure.type === "rpc" || procedure.type === "upload";
1799
1389
  const outputWriter = new WriteStreamImpl((response) => {
1800
1390
  this.transport.send(from, {
1801
1391
  streamId,
1802
- controlFlags: procClosesWithResponse ? 8 /* StreamClosedBit */ : 0,
1392
+ controlFlags: procClosesWithResponse ? getStreamCloseBackwardsCompat(protocolVersion) : 0,
1803
1393
  payload: response
1804
1394
  });
1805
1395
  });
1806
1396
  outputWriter.onClose(() => {
1807
1397
  if (!procClosesWithResponse && cleanClose) {
1808
- this.transport.sendCloseControl(from, streamId);
1398
+ const message = closeStreamMessage(streamId);
1399
+ message.controlFlags = getStreamCloseBackwardsCompat(protocolVersion);
1400
+ this.transport.send(from, closeStreamMessage(streamId));
1401
+ }
1402
+ if (protocolVersion === "v1.1") {
1403
+ if (!inputReader.isClosed()) {
1404
+ inputReader.triggerClose();
1405
+ }
1809
1406
  }
1810
1407
  if (inputReader.isClosed()) {
1811
1408
  cleanup();
@@ -1822,7 +1419,7 @@ var RiverServer = class {
1822
1419
  })
1823
1420
  );
1824
1421
  };
1825
- if (isStreamClose(controlFlags)) {
1422
+ if (isStreamCloseBackwardsCompat(controlFlags, protocolVersion)) {
1826
1423
  inputReader.triggerClose();
1827
1424
  } else if (procedure.type === "rpc" || procedure.type === "subscription") {
1828
1425
  this.log?.warn(`${procedure.type} sent an init without a stream close`, {
@@ -1833,6 +1430,7 @@ var RiverServer = class {
1833
1430
  const serviceContextWithTransportInfo = {
1834
1431
  ...this.getContext(service, serviceName),
1835
1432
  from,
1433
+ sessionId,
1836
1434
  metadata: sessionMetadata,
1837
1435
  abortController: handlerAbortController,
1838
1436
  clientAbortSignal: clientAbortController.signal,
@@ -1988,7 +1586,9 @@ var RiverServer = class {
1988
1586
  );
1989
1587
  return null;
1990
1588
  }
1991
- const sessionMetadata = this.transport.sessionHandshakeMetadata.get(session);
1589
+ const sessionMetadata = this.transport.sessionHandshakeMetadata.get(
1590
+ session.to
1591
+ );
1992
1592
  if (!sessionMetadata) {
1993
1593
  const errMessage = `session doesn't have handshake metadata`;
1994
1594
  this.log?.error(errMessage, {
@@ -2129,7 +1729,9 @@ var RiverServer = class {
2129
1729
  controlFlags: initMessage.controlFlags,
2130
1730
  tracingCtx: initMessage.tracing,
2131
1731
  initPayload: initMessage.payload,
2132
- from: initMessage.from
1732
+ from: initMessage.from,
1733
+ sessionId: session.id,
1734
+ protocolVersion: session.protocolVersion
2133
1735
  };
2134
1736
  }
2135
1737
  abortStream(to, streamId, payload) {
@@ -2139,7 +1741,11 @@ var RiverServer = class {
2139
1741
  this.serverAbortedStreams.set(to, abortedForSession);
2140
1742
  }
2141
1743
  abortedForSession.add(streamId);
2142
- this.transport.sendAbort(to, streamId, payload);
1744
+ this.transport.send(
1745
+ to,
1746
+ // TODO remove once clients migrate to v2
1747
+ this.transport.sessions.get(to)?.protocolVersion === "v1.1" ? closeStreamMessage(streamId) : abortMessage(streamId, payload)
1748
+ );
2143
1749
  }
2144
1750
  };
2145
1751
  var LRUSet = class {
@@ -2164,6 +1770,24 @@ var LRUSet = class {
2164
1770
  return this.items.has(item);
2165
1771
  }
2166
1772
  };
1773
+ function isStreamAbortBackwardsCompat(controlFlags, protocolVersion) {
1774
+ if (protocolVersion === "v1.1") {
1775
+ return false;
1776
+ }
1777
+ return isStreamAbort(controlFlags);
1778
+ }
1779
+ function isStreamCloseBackwardsCompat(controlFlags, protocolVersion) {
1780
+ if (protocolVersion === "v1.1") {
1781
+ return isStreamAbort(controlFlags);
1782
+ }
1783
+ return isStreamClose(controlFlags);
1784
+ }
1785
+ function getStreamCloseBackwardsCompat(protocolVersion) {
1786
+ if (protocolVersion === "v1.1") {
1787
+ return 4 /* StreamAbortBit */;
1788
+ }
1789
+ return 8 /* StreamClosedBit */;
1790
+ }
2167
1791
  function createServer(transport, services, providedServerOptions) {
2168
1792
  return new RiverServer(
2169
1793
  transport,
@@ -2197,7 +1821,6 @@ function createServerHandshakeOptions(schema, validate) {
2197
1821
  createClientHandshakeOptions,
2198
1822
  createServer,
2199
1823
  createServerHandshakeOptions,
2200
- diffServerSchema,
2201
1824
  serializeSchema
2202
1825
  });
2203
1826
  //# sourceMappingURL=index.cjs.map