@moq/net 0.3.2 → 0.3.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 (100) hide show
  1. package/broadcast.d.ts.map +1 -1
  2. package/broadcast.js +3 -2
  3. package/broadcast.js.map +1 -1
  4. package/connection/accept.d.ts.map +1 -1
  5. package/connection/accept.js +3 -0
  6. package/connection/accept.js.map +1 -1
  7. package/connection/browser.d.ts.map +1 -1
  8. package/connection/browser.js +3 -0
  9. package/connection/browser.js.map +1 -1
  10. package/connection/connect.d.ts.map +1 -1
  11. package/connection/connect.js +11 -7
  12. package/connection/connect.js.map +1 -1
  13. package/group.d.ts +19 -1
  14. package/group.d.ts.map +1 -1
  15. package/group.js +35 -11
  16. package/group.js.map +1 -1
  17. package/ietf/adapter.d.ts.map +1 -1
  18. package/ietf/adapter.js +76 -45
  19. package/ietf/adapter.js.map +1 -1
  20. package/ietf/aliases.d.ts +73 -5
  21. package/ietf/aliases.d.ts.map +1 -1
  22. package/ietf/aliases.js +121 -14
  23. package/ietf/aliases.js.map +1 -1
  24. package/ietf/fetch.d.ts +17 -0
  25. package/ietf/fetch.d.ts.map +1 -1
  26. package/ietf/fetch.js +21 -0
  27. package/ietf/fetch.js.map +1 -1
  28. package/ietf/filter.d.ts +87 -0
  29. package/ietf/filter.d.ts.map +1 -0
  30. package/ietf/filter.js +292 -0
  31. package/ietf/filter.js.map +1 -0
  32. package/ietf/namespace.d.ts +6 -0
  33. package/ietf/namespace.d.ts.map +1 -1
  34. package/ietf/namespace.js +27 -2
  35. package/ietf/namespace.js.map +1 -1
  36. package/ietf/object.d.ts +44 -2
  37. package/ietf/object.d.ts.map +1 -1
  38. package/ietf/object.js +74 -9
  39. package/ietf/object.js.map +1 -1
  40. package/ietf/parameters.d.ts +16 -4
  41. package/ietf/parameters.d.ts.map +1 -1
  42. package/ietf/parameters.js +54 -10
  43. package/ietf/parameters.js.map +1 -1
  44. package/ietf/properties.d.ts +5 -0
  45. package/ietf/properties.d.ts.map +1 -1
  46. package/ietf/properties.js +5 -0
  47. package/ietf/properties.js.map +1 -1
  48. package/ietf/publisher.d.ts.map +1 -1
  49. package/ietf/publisher.js +375 -27
  50. package/ietf/publisher.js.map +1 -1
  51. package/ietf/subscribe.d.ts +38 -7
  52. package/ietf/subscribe.d.ts.map +1 -1
  53. package/ietf/subscribe.js +94 -29
  54. package/ietf/subscribe.js.map +1 -1
  55. package/ietf/subscriber.d.ts.map +1 -1
  56. package/ietf/subscriber.js +148 -35
  57. package/ietf/subscriber.js.map +1 -1
  58. package/ietf/version.d.ts +7 -1
  59. package/ietf/version.d.ts.map +1 -1
  60. package/ietf/version.js +7 -0
  61. package/ietf/version.js.map +1 -1
  62. package/internal.d.ts +16 -1
  63. package/internal.d.ts.map +1 -1
  64. package/internal.js.map +1 -1
  65. package/lite/announce.d.ts.map +1 -1
  66. package/lite/announce.js +7 -7
  67. package/lite/announce.js.map +1 -1
  68. package/lite/publisher.d.ts.map +1 -1
  69. package/lite/publisher.js +4 -2
  70. package/lite/publisher.js.map +1 -1
  71. package/lite/setup.d.ts.map +1 -1
  72. package/lite/setup.js +2 -1
  73. package/lite/setup.js.map +1 -1
  74. package/lite/subscriber.d.ts +1 -6
  75. package/lite/subscriber.d.ts.map +1 -1
  76. package/lite/subscriber.js +98 -23
  77. package/lite/subscriber.js.map +1 -1
  78. package/lite/version.d.ts +4 -0
  79. package/lite/version.d.ts.map +1 -1
  80. package/lite/version.js +15 -0
  81. package/lite/version.js.map +1 -1
  82. package/origin.d.ts +1 -1
  83. package/origin.d.ts.map +1 -1
  84. package/origin.js +1 -1
  85. package/origin.js.map +1 -1
  86. package/package.json +4 -3
  87. package/stream.d.ts.map +1 -1
  88. package/stream.js +3 -5
  89. package/stream.js.map +1 -1
  90. package/track.d.ts +38 -0
  91. package/track.d.ts.map +1 -1
  92. package/track.js +104 -25
  93. package/track.js.map +1 -1
  94. package/util/utf8.d.ts +3 -0
  95. package/util/utf8.d.ts.map +1 -0
  96. package/util/utf8.js +7 -0
  97. package/util/utf8.js.map +1 -0
  98. package/zod.d.ts +1 -1
  99. package/zod.d.ts.map +1 -1
  100. package/zod.js.map +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"properties.js","sourceRoot":"","sources":["../../src/ietf/properties.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,EAAoB,OAAO,EAAE,MAAM,cAAc,CAAC;AAEzD,wFAAwF;AACxF,qFAAqF;AACrF,0DAA0D;AAC1D,MAAM,SAAS,GAAG,KAAK,CAAC;AAExB,2FAA2F;AAC3F,sFAAsF;AACtF,kFAAkF;AAClF,wCAAwC;AACxC,MAAM,6BAA6B,GAAG,KAAK,CAAC;AAgB5C,wFAAwF;AACxF,iDAAiD;AACjD,GAAG;AACH,wEAAwE;AACxE,MAAM,CAAC,KAAK,UAAU,MAAM,CAAC,CAAS,EAAE,UAAsB,EAAE,OAAoB;IACnF,wFAAwF;IACxF,yFAAyF;IACzF,yFAAyF;IACzF,uDAAuD;IACvD,IAAI,OAAO,KAAK,OAAO,CAAC,QAAQ,IAAI,OAAO,KAAK,OAAO,CAAC,QAAQ,IAAI,OAAO,KAAK,OAAO,CAAC,QAAQ,EAAE,CAAC;QAClG,OAAO;IACR,CAAC;IAED,IAAI,QAAQ,GAAG,EAAE,CAAC;IAElB,IAAI,UAAU,CAAC,SAAS,KAAK,SAAS,EAAE,CAAC;QACxC,MAAM,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QACvB,MAAM,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,CAAC;QAC1C,QAAQ,GAAG,SAAS,CAAC;IACtB,CAAC;IAED,IAAI,UAAU,CAAC,UAAU,KAAK,SAAS,EAAE,CAAC;QACzC,MAAM,CAAC,CAAC,GAAG,CAAC,6BAA6B,GAAG,QAAQ,CAAC,CAAC;QACtD,MAAM,CA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FAAkF;gBAClF,kEAAkE;gBAClE,IAAI,KAAK,GAAG,EAAE,IAAI,KAAK,GAAG,EAAE,EAAE,CAAC;oBAC9B,MAAM,IAAI,KAAK,CAAC,wBAAwB,KAAK,EAAE,CAAC,CAAC;gBAClD,CAAC;gBACD,UAAU,CAAC,UAAU,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;YACvC,CAAC;QACF,CAAC;aAAM,CAAC;YACP,kCAAkC;YAClC,MAAM,GAAG,GAAG,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC;YAC1B,MAAM,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACnB,CAAC;IACF,CAAC;IAED,OAAO,UAAU,CAAC;AACnB,CAAC","sourcesContent":["import type { Reader, Writer } from \"../stream.ts\";\nimport { Timescale } from \"../time.ts\";\nimport { type IetfVersion, Version } from \"./version.ts\";\n\n// TIMESCALE (0x08), from the MOQ Properties registry shared with draft-ietf-moq-loc-04.\n// Track scope: it declares the units of every object Timestamp on the track, and its\n// presence is what opts the track into timestamps at all.\nconst TIMESCALE = 0x08n;\n\n// DEFAULT_PUBLISHER_GROUP_ORDER (0x22), the publisher's delivery preference for the track.\n// It shares its number with the GROUP_ORDER *message parameter*, which is a different\n// registry: that one is only legal in SUBSCRIBE, PUBLISH_OK, and FETCH, where the\n// subscriber states its own preference.\nconst DEFAULT_PUBLISHER_GROUP_ORDER = 0x22n;\n\n/// The Track Properties block carried at the end of SUBSCRIBE_OK, PUBLISH, and FETCH_OK.\nexport interface Properties {\n\t/// The track's Timescale, which declares the units of every object Timestamp on it.\n\t///\n\t/// `undefined` declares no timeline, so the subscriber times objects by arrival.\n\ttimescale?: Timescale;\n\n\t/// The publisher's preference for prioritizing groups within a subscription,\n\t/// Ascending (0x1) or Descending (0x2).\n\t///\n\t/// `undefined` means the draft default, Ascending.\n\tgroupOrder?: number;\n}\n\n/// Write the block, which is the final field of the message: no count and no length, so\n/// the caller must not append anything after it.\n///\n/// Properties are serialized in ascending order by type, delta-encoded.\nexport async function encode(w: Writer, properties: Properties, version: IetfVersion): Promise<void> {\n\t// Draft-16 carries the same block under the name Track Extensions, but we only write it\n\t// from draft-17 on: a draft-16 peer running an older build rejects any trailing bytes it\n\t// doesn't parse, and draft-16 never registered TIMESCALE (0x08). We still read the block\n\t// on draft-16, so a peer that sends one is understood.\n\tif (version === Version.DRAFT_14 || version === Version.DRAFT_15 || version === Version.DRAFT_16) {\n\t\treturn;\n\t}\n\n\tlet prevType = 0n;\n\n\tif (properties.timescale !== undefined) {\n\t\tawait w.u62(TIMESCALE);\n\t\tawait w.u62(BigInt(properties.timescale));\n\t\tprevType = TIMESCALE;\n\t}\n\n\tif (properties.groupOrder !== undefined) {\n\t\tawait w.u62(DEFAULT_PUBLISHER_GROUP_ORDER - prevType);\n\t\tawait w.u62(BigInt(properties.groupOrder));\n\t}\n}\n\n/// Parse Track Properties from the remaining bytes of a message.\n///\n/// Track Properties are delta-encoded Key-Value-Pairs (same format as\n/// Message Parameters) but with NO count prefix: they extend to the\n/// end of the message payload.\n///\n/// Unlike an unknown message parameter, an unknown property is skipped rather than fatal,\n/// which is what lets a relay forward properties it does not implement.\n///\n/// Drafts before 16 have no such block, so this reads nothing and leaves the reader alone.\nexport async function decode(r: Reader, version: IetfVersion): Promise<Properties> {\n\t// Draft-16 calls the block Track Extensions, draft-17+ Track Properties. Same encoding, so\n\t// read either rather than faulting the message for trailing bytes.\n\tif (version === Version.DRAFT_14 || version === Version.DRAFT_15) {\n\t\treturn {};\n\t}\n\n\tconst properties: Properties = {};\n\tlet prevType = 0n;\n\tlet i = 0;\n\n\twhile (!(await r.done())) {\n\t\tconst delta = await r.u62();\n\t\tconst abs = i === 0 ? delta : prevType + delta;\n\t\tprevType = abs;\n\t\ti++;\n\n\t\tif (abs % 2n === 0n) {\n\t\t\t// Even type: single varint value\n\t\t\tconst value = await r.u62();\n\t\t\tif (abs === TIMESCALE && value > 0n) {\n\t\t\t\t// A zero timescale is invalid; treat it as no declaration rather than\n\t\t\t\t// failing the whole message over one property we could have ignored.\n\t\t\t\tproperties.timescale = Timescale(Number(value));\n\t\t\t} else if (abs === DEFAULT_PUBLISHER_GROUP_ORDER) {\n\t\t\t\t// Only Ascending (0x1) and Descending (0x2) are defined here. Unlike the draft-14\n\t\t\t\t// fields, 0x0 has no \"publisher decides\" meaning to fall back on.\n\t\t\t\tif (value < 1n || value > 2n) {\n\t\t\t\t\tthrow new Error(`unknown group order: ${value}`);\n\t\t\t\t}\n\t\t\t\tproperties.groupOrder = Number(value);\n\t\t\t}\n\t\t} else {\n\t\t\t// Odd type: length-prefixed bytes\n\t\t\tconst len = await r.u53();\n\t\t\tawait r.read(len);\n\t\t}\n\t}\n\n\treturn properties;\n}\n"]}
1
+ {"version":3,"file":"properties.js","sourceRoot":"","sources":["../../src/ietf/properties.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,EAAoB,OAAO,EAAE,MAAM,cAAc,CAAC;AAEzD,wFAAwF;AACxF,qFAAqF;AACrF,0DAA0D;AAC1D,MAAM,SAAS,GAAG,KAAK,CAAC;AAExB,2FAA2F;AAC3F,sFAAsF;AACtF,kFAAkF;AAClF,wCAAwC;AACxC,MAAM,6BAA6B,GAAG,KAAK,CAAC;AAE5C;;;GAGG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,IAAI,CAAC;AAgB/B,wFAAwF;AACxF,iDAAiD;AACjD,GAAG;AACH,wEAAwE;AACxE,MAAM,CAAC,KAAK,UAAU,MAAM,CAAC,CAAS,EAAE,UAAsB,EAAE,OAAoB;IACnF,wFAAwF;IACxF,yFAAyF;IACzF,yFAAyF;IACzF,uDAAuD;IACvD,IAAI,OAAO,KAAK,OAAO,CAAC,QAAQ,IAAI,OAAO,KAAK,OAAO,CAAC,QAAQ,IAAI,OAAO,KAAK,OAAO,CAAC,QAAQ,EAAE,CAAC;QAClG,OAAO;IACR,CAAC;IAED,IAAI,QAAQ,GAAG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,EAAE,CAAC;QAEJ,IAAI,GAAG,GAAG,EAAE,KAAK,EAAE,EAAE,CAAC;YACrB,iCAAiC;YACjC,MAAM,KAAK,GAAG,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC;YAC5B,IAAI,GAAG,KAAK,SAAS,IAAI,KAAK,GAAG,EAAE,EAAE,CAAC;gBACrC,sEAAsE;gBACtE,qEAAqE;gBACrE,UAAU,CAAC,SAAS,GAAG,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;YACjD,CAAC;iBAAM,IAAI,GAAG,KAAK,6BAA6B,EAAE,CAAC;gBAClD,kFAAkF;gBAClF,kEAAkE;gBAClE,IAAI,KAAK,GAAG,EAAE,IAAI,KAAK,GAAG,EAAE,EAAE,CAAC;oBAC9B,MAAM,IAAI,KAAK,CAAC,wBAAwB,KAAK,EAAE,CAAC,CAAC;gBAClD,CAAC;gBACD,UAAU,CAAC,UAAU,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;YACvC,CAAC;QACF,CAAC;aAAM,CAAC;YACP,kCAAkC;YAClC,MAAM,GAAG,GAAG,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC;YAC1B,MAAM,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACnB,CAAC;IACF,CAAC;IAED,OAAO,UAAU,CAAC;AACnB,CAAC","sourcesContent":["import type { Reader, Writer } from \"../stream.ts\";\nimport { Timescale } from \"../time.ts\";\nimport { type IetfVersion, Version } from \"./version.ts\";\n\n// TIMESCALE (0x08), from the MOQ Properties registry shared with draft-ietf-moq-loc-04.\n// Track scope: it declares the units of every object Timestamp on the track, and its\n// presence is what opts the track into timestamps at all.\nconst TIMESCALE = 0x08n;\n\n// DEFAULT_PUBLISHER_GROUP_ORDER (0x22), the publisher's delivery preference for the track.\n// It shares its number with the GROUP_ORDER *message parameter*, which is a different\n// registry: that one is only legal in SUBSCRIBE, PUBLISH_OK, and FETCH, where the\n// subscriber states its own preference.\nconst DEFAULT_PUBLISHER_GROUP_ORDER = 0x22n;\n\n/**\n * DEFAULT_PUBLISHER_GROUP_ORDER's descending value: the newest group first, which is what\n * we serve, matching moq-lite.\n */\nexport const DESCENDING = 0x02;\n\n/// The Track Properties block carried at the end of SUBSCRIBE_OK, PUBLISH, and FETCH_OK.\nexport interface Properties {\n\t/// The track's Timescale, which declares the units of every object Timestamp on it.\n\t///\n\t/// `undefined` declares no timeline, so the subscriber times objects by arrival.\n\ttimescale?: Timescale;\n\n\t/// The publisher's preference for prioritizing groups within a subscription,\n\t/// Ascending (0x1) or Descending (0x2).\n\t///\n\t/// `undefined` means the draft default, Ascending.\n\tgroupOrder?: number;\n}\n\n/// Write the block, which is the final field of the message: no count and no length, so\n/// the caller must not append anything after it.\n///\n/// Properties are serialized in ascending order by type, delta-encoded.\nexport async function encode(w: Writer, properties: Properties, version: IetfVersion): Promise<void> {\n\t// Draft-16 carries the same block under the name Track Extensions, but we only write it\n\t// from draft-17 on: a draft-16 peer running an older build rejects any trailing bytes it\n\t// doesn't parse, and draft-16 never registered TIMESCALE (0x08). We still read the block\n\t// on draft-16, so a peer that sends one is understood.\n\tif (version === Version.DRAFT_14 || version === Version.DRAFT_15 || version === Version.DRAFT_16) {\n\t\treturn;\n\t}\n\n\tlet prevType = 0n;\n\n\tif (properties.timescale !== undefined) {\n\t\tawait w.u62(TIMESCALE);\n\t\tawait w.u62(BigInt(properties.timescale));\n\t\tprevType = TIMESCALE;\n\t}\n\n\tif (properties.groupOrder !== undefined) {\n\t\tawait w.u62(DEFAULT_PUBLISHER_GROUP_ORDER - prevType);\n\t\tawait w.u62(BigInt(properties.groupOrder));\n\t}\n}\n\n/// Parse Track Properties from the remaining bytes of a message.\n///\n/// Track Properties are delta-encoded Key-Value-Pairs (same format as\n/// Message Parameters) but with NO count prefix: they extend to the\n/// end of the message payload.\n///\n/// Unlike an unknown message parameter, an unknown property is skipped rather than fatal,\n/// which is what lets a relay forward properties it does not implement.\n///\n/// Drafts before 16 have no such block, so this reads nothing and leaves the reader alone.\nexport async function decode(r: Reader, version: IetfVersion): Promise<Properties> {\n\t// Draft-16 calls the block Track Extensions, draft-17+ Track Properties. Same encoding, so\n\t// read either rather than faulting the message for trailing bytes.\n\tif (version === Version.DRAFT_14 || version === Version.DRAFT_15) {\n\t\treturn {};\n\t}\n\n\tconst properties: Properties = {};\n\tlet prevType = 0n;\n\tlet i = 0;\n\n\twhile (!(await r.done())) {\n\t\tconst delta = await r.u62();\n\t\tconst abs = i === 0 ? delta : prevType + delta;\n\t\tprevType = abs;\n\t\ti++;\n\n\t\tif (abs % 2n === 0n) {\n\t\t\t// Even type: single varint value\n\t\t\tconst value = await r.u62();\n\t\t\tif (abs === TIMESCALE && value > 0n) {\n\t\t\t\t// A zero timescale is invalid; treat it as no declaration rather than\n\t\t\t\t// failing the whole message over one property we could have ignored.\n\t\t\t\tproperties.timescale = Timescale(Number(value));\n\t\t\t} else if (abs === DEFAULT_PUBLISHER_GROUP_ORDER) {\n\t\t\t\t// Only Ascending (0x1) and Descending (0x2) are defined here. Unlike the draft-14\n\t\t\t\t// fields, 0x0 has no \"publisher decides\" meaning to fall back on.\n\t\t\t\tif (value < 1n || value > 2n) {\n\t\t\t\t\tthrow new Error(`unknown group order: ${value}`);\n\t\t\t\t}\n\t\t\t\tproperties.groupOrder = Number(value);\n\t\t\t}\n\t\t} else {\n\t\t\t// Odd type: length-prefixed bytes\n\t\t\tconst len = await r.u53();\n\t\t\tawait r.read(len);\n\t\t}\n\t}\n\n\treturn properties;\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"publisher.d.ts","sourceRoot":"","sources":["../../src/ietf/publisher.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,SAAS,MAAM,iBAAiB,CAAC;AAGlD,OAAO,KAAK,IAAI,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,KAAK,MAAM,EAAU,MAAM,cAAc,CAAC;AAGnD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,C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M,cAAc,CAAC;AAInD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,OAAO,MAAM,cAAc,CAAC;AASxC,OAAO,EAAE,KAAK,SAAS,EAA+B,MAAM,gBAAgB,CAAC;AAC7E,OAAO,EACN,KAAK,kBAAkB,EAIvB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAe,KAAK,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAiGlE;;;;;GAKG;AACH,qBAAa,SAAS;;IAcrB;;;;OAIG;IACH,YAAY,EACX,IAAI,EACJ,OAAO,EACP,oBAAoB,EACpB,OAAO,GACP,EAAE;QACF,iDAAiD;QACjD,IAAI,EAAE,YAAY,CAAC;QACnB,gEAAgE;QAChE,OAAO,EAAE,OAAO,CAAC;QACjB,0EAA0E;QAC1E,oBAAoB,EAAE,OAAO,CAAC;QAC9B,4DAA4D;QAC5D,OAAO,CAAC,EAAE,OAAO,CAAC,IAAI,CAAC;KACvB,EAKA;IAED;;;;OAIG;IACH,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,EAAE,SAAS,EAAE,SAAS,CAAC,QAAQ,QActD;IAED;;;;;OAKG;IACG,YAAY,CAAC,GAAG,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,iBA8LhD;IA2LD;;;;;;;;;;OAUG;IACG,qBAAqB,CAAC,GAAG,EAAE,kBAAkB,EAAE,MAAM,EAAE,MAAM,iBA+HlE;IAED;;;;;;;;;;;OAWG;IACG,oBAAoB,kBA6EzB;IAkID;;;;OAIG;IACG,qBAAqB,CAAC,GAAG,EAAE,kBAAkB,EAAE,MAAM,EAAE,MAAM,iBAuBlE;IAED;;;;OAIG;IACH,KAAK,SAOJ;CACD"}
package/ietf/publisher.js CHANGED
@@ -5,8 +5,11 @@ import * as Path from "../path.js";
5
5
  import { Writer } from "../stream.js";
6
6
  import { withTimeout } from "../util/timeout.js";
7
7
  import * as Cluster from "./cluster.js";
8
- import { Frame, Group as GroupMessage } from "./object.js";
8
+ import { FetchHeader } from "./fetch.js";
9
+ import * as Filter from "./filter.js";
10
+ import { FetchFrame, Frame, Group as GroupMessage } from "./object.js";
9
11
  import { fromWire } from "./priority.js";
12
+ import * as Properties from "./properties.js";
10
13
  import { PublishDone } from "./publish.js";
11
14
  import { PublishNamespace, PublishNamespaceDone, PublishNamespaceOk } from "./publish_namespace.js";
12
15
  import { RequestError, RequestOk } from "./request.js";
@@ -18,6 +21,11 @@ import { Version } from "./version.js";
18
21
  const RETRY_BASE = 100;
19
22
  /** Ceiling on that wait. The loop retries for the life of the session, so it must not spin. */
20
23
  const RETRY_MAX = 5000;
24
+ /** PUBLISH_DONE statuses this implementation emits. Stable across drafts 14 through 19. */
25
+ const PUBLISH_DONE_STATUS = {
26
+ INTERNAL_ERROR: 0x0,
27
+ TRACK_ENDED: 0x2,
28
+ };
21
29
  /**
22
30
  * How long one advertisement may take to be answered. Matches the Rust publisher, and the
23
31
  * peer accepting the stream is only half the exchange: one it never answers on holds the
@@ -67,10 +75,12 @@ export class Publisher {
67
75
  throw new Error("closed");
68
76
  broadcasts.set(path, broadcast);
69
77
  });
70
- // Remove the broadcast from the lookup when it's closed.
78
+ // Remove the broadcast from the lookup when it's closed, unless the path was republished.
71
79
  void broadcast.closed.then(() => {
72
80
  this.#broadcasts.mutate((broadcasts) => {
73
- broadcasts?.delete(path);
81
+ if (broadcasts?.get(path) === broadcast) {
82
+ broadcasts.delete(path);
83
+ }
74
84
  });
75
85
  });
76
86
  }
@@ -107,11 +117,37 @@ export class Publisher {
107
117
  stream.close();
108
118
  return;
109
119
  }
110
- const track = broadcast.subscribe(msg.trackName, { priority: fromWire(msg.subscriberPriority) });
120
+ const priority = fromWire(msg.subscriberPriority);
121
+ const track = broadcast.subscribe(msg.trackName, { priority });
122
+ // Hoisted so the cleanup below reaches it: the fork happens before SUBSCRIBE_OK is
123
+ // written, and a write that throws must not strand the sink it attached.
124
+ let cache;
111
125
  try {
112
- // Declaring the timescale is what opts the track into timestamps; every object
113
- // Timestamp below is in these units.
114
126
  const timescale = (await track.info()).timescale;
127
+ // The filter and any fill are relative to the live edge, so snapshot it once: the
128
+ // fill ends exactly where a Next Object subscription begins, which is what lets
129
+ // the draft's current-group join (Next Object plus a StartGroup=1 fill) cover the
130
+ // group with no gap and no overlap.
131
+ const edge = liveEdge(track);
132
+ const range = subscribeRange(msg, edge, version);
133
+ // The wire request tells an upstream what we need; the cursor is what actually
134
+ // trims this subscriber, since the producer fans every cached group out to every
135
+ // sink regardless. An absent start joins at the latest group, which is what
136
+ // moq-lite means by joining a live track.
137
+ track.update({
138
+ priority,
139
+ startGroup: range.start && Number(range.start.group),
140
+ endGroup: range.end && Number(range.end.group),
141
+ });
142
+ const startGroup = range.start ? Number(range.start.group) : track.latest();
143
+ if (startGroup !== undefined)
144
+ track.startAt(startGroup);
145
+ // A fill reads the group cache through its own consumer, independent of the
146
+ // subscription's cursor. Forked from this subscriber rather than resolved through
147
+ // the broadcast again: a dynamic serve is one request per peer subscription, so
148
+ // asking the broadcast would mint a second producer nobody has accepted.
149
+ const fill = msg.fill && Filter.isDraft20(version) ? fillRange(msg.fill, msg.filter, edge.largest) : undefined;
150
+ cache = fill && fill.kind !== "empty" ? track.fork({ priority }) : undefined;
115
151
  // Send SUBSCRIBE_OK
116
152
  await stream.writer.u53(SubscribeOk.id);
117
153
  const ok = new SubscribeOk({
@@ -119,7 +155,17 @@ export class Publisher {
119
155
  ? msg.requestId
120
156
  : undefined,
121
157
  trackAlias: msg.requestId,
122
- timescale,
158
+ // Required once the track has content; a fill-requesting subscriber sizes its
159
+ // backfill against this.
160
+ largest: edge.largest && { groupId: edge.largest.group, objectId: edge.largest.object },
161
+ properties: msg.propertiesWanted
162
+ ? // Declaring the timescale is what opts the track into timestamps; every
163
+ // object Timestamp below is in these units. We serve the newest group
164
+ // first, matching moq-lite.
165
+ { timescale, groupOrder: Properties.DESCENDING }
166
+ : // INCLUDE_PROPERTIES=0. The block stays present but empty, which also means
167
+ // the track opts out of timestamps for this subscriber.
168
+ {},
123
169
  });
124
170
  await ok.encode(stream.writer, version);
125
171
  console.debug(`publish ok: broadcast=${name} track=${track.name}`);
@@ -143,25 +189,64 @@ export class Publisher {
143
189
  const group = await track.recvGroup();
144
190
  if (!group)
145
191
  return;
146
- void this.#runGroup({ requestId: msg.requestId, group, timescale, unsubscribed });
147
- }
148
- })();
149
- await Promise.race([serving, stream.reader.closed]);
150
- console.debug(`publish done: broadcast=${name} track=${track.name}`);
151
- // v14-v16: send PublishDone before closing
152
- if (version === Version.DRAFT_14 || version === Version.DRAFT_15 || version === Version.DRAFT_16) {
153
- try {
154
- await stream.writer.u53(PublishDone.id);
155
- const done = new PublishDone({
192
+ // Past the filter's end. Dropped here rather than through `endAt`, which
193
+ // parks a capped group instead: this range is fixed for the life of the
194
+ // subscription, so a group above it is never coming back in, and holding
195
+ // one keeps the loop from ever ending. A producer that publishes beyond
196
+ // the end and then closes would otherwise strand PUBLISH_DONE.
197
+ if (range.end !== undefined && BigInt(group.sequence) > range.end.group) {
198
+ group.close();
199
+ continue;
200
+ }
201
+ void this.#runGroup({
156
202
  requestId: msg.requestId,
157
- statusCode: 200,
158
- reasonPhrase: "OK",
203
+ group,
204
+ timescale,
205
+ stamped: msg.propertiesWanted,
206
+ slice: groupSlice(range, group.sequence),
207
+ unsubscribed,
159
208
  });
160
- await done.encode(stream.writer, version);
161
- }
162
- catch {
163
- // Stream might already be closed by peer
164
209
  }
210
+ })();
211
+ // The fill (when one was requested) runs alongside on its own fetch stream; its
212
+ // failures reset that stream and never touch the subscription.
213
+ const filling = fill && cache
214
+ ? this.#runFill({
215
+ requestId: msg.requestId,
216
+ priority,
217
+ fill,
218
+ cache,
219
+ timescale,
220
+ stamped: msg.propertiesWanted,
221
+ unsubscribed,
222
+ })
223
+ : Promise.resolve();
224
+ let publishError;
225
+ try {
226
+ await Promise.race([Promise.all([serving, filling]), stream.reader.closed]);
227
+ }
228
+ catch (err) {
229
+ publishError = error(err);
230
+ }
231
+ console.debug(`publish done: broadcast=${name} track=${track.name}`);
232
+ if (publishError) {
233
+ console.warn(`publish error: broadcast=${name} track=${track.name} error=${reason(publishError)}`);
234
+ }
235
+ // PUBLISH_DONE is required for every supported draft. The peer may have already
236
+ // closed its side to unsubscribe, in which case there is nowhere to send it.
237
+ try {
238
+ await stream.writer.u53(PublishDone.id);
239
+ const done = new PublishDone({
240
+ requestId: version === Version.DRAFT_14 || version === Version.DRAFT_15 || version === Version.DRAFT_16
241
+ ? msg.requestId
242
+ : undefined,
243
+ statusCode: publishError ? PUBLISH_DONE_STATUS.INTERNAL_ERROR : PUBLISH_DONE_STATUS.TRACK_ENDED,
244
+ reasonPhrase: publishError ? "internal error" : "track ended",
245
+ });
246
+ await done.encode(stream.writer, version);
247
+ }
248
+ catch {
249
+ // Stream might already be closed by peer.
165
250
  }
166
251
  // Only now is the close below ours. Claiming it any earlier would read a peer FIN
167
252
  // that lands while PublishDone is still going out as our own completion, leaving
@@ -176,13 +261,16 @@ export class Publisher {
176
261
  }
177
262
  finally {
178
263
  track.close();
264
+ // Idempotent, and the backstop for a throw between the fork and the fill starting:
265
+ // #runFill closes it itself on every path it reaches.
266
+ cache?.close();
179
267
  }
180
268
  }
181
269
  /**
182
270
  * Runs a group and sends its frames using ObjectStream (Subgroup delivery mode).
183
271
  */
184
272
  async #runGroup(options) {
185
- const { requestId, group, timescale, unsubscribed } = options;
273
+ const { requestId, group, timescale, stamped, slice, unsubscribed } = options;
186
274
  try {
187
275
  // One stream per group is faster than a peer at its limit can retire them, so this
188
276
  // is the one path that doesn't wait for a slot: the transport would serve the opens
@@ -203,21 +291,47 @@ export class Publisher {
203
291
  subGroupId: 0,
204
292
  publisherPriority: 0,
205
293
  flags: {
206
- hasExtensions: true,
294
+ // The object properties carry the timestamp, so there is nothing to write
295
+ // when the track declared no units to read one in.
296
+ hasExtensions: stamped,
207
297
  hasSubgroup: false,
208
298
  hasSubgroupObject: false,
209
299
  hasEnd: true,
210
300
  hasPriority: true,
301
+ // Only honest when the stream really starts at the group's first object;
302
+ // a trimmed head starts partway through.
303
+ firstObject: slice.skip === 0,
211
304
  },
212
305
  });
213
306
  await header.encode(stream, this.#session.version);
214
307
  try {
308
+ // The first written object goes on the wire as its absolute id, so a trimmed
309
+ // head shows the true numbering rather than a silently renumbered group.
310
+ let first = true;
311
+ // The next object id that could be written, which starts at the filter rather
312
+ // than at zero. A backwards range (`startObject` above `endObject` in the same
313
+ // group) is empty, and starting here is what lets the check below see that
314
+ // before the read parks on an object the range already excludes.
315
+ let next = slice.skip;
215
316
  for (;;) {
216
- const frame = await Promise.race([group.readFrame(), stream.closed]);
317
+ // The filter ends inside this group: everything at `until` and beyond is
318
+ // outside the requested range, so stop without waiting for the group's end.
319
+ if (slice.until !== undefined && next >= slice.until)
320
+ break;
321
+ // Reading from the filter's start drops the objects below it, including any the
322
+ // group's cache evicted: they are outside the requested range, so losing them is
323
+ // not the gap that would otherwise reset this stream and forfeit the rest of the
324
+ // group. An eviction at or above the start is a real gap and still throws.
325
+ const frame = await Promise.race([group.readFrameSequence({ from: slice.skip }), stream.closed]);
217
326
  if (!frame)
218
327
  break;
328
+ next = frame.sequence + 1;
329
+ if (slice.until !== undefined && frame.sequence >= slice.until)
330
+ break;
219
331
  const obj = new Frame({ payload: frame.payload, timestamp: frame.timestamp });
220
- await obj.encode(stream, header.flags, timescale, this.#session.version);
332
+ const delta = first ? frame.sequence : 0;
333
+ first = false;
334
+ await obj.encode(stream, header.flags, timescale, this.#session.version, delta);
221
335
  }
222
336
  stream.close();
223
337
  }
@@ -229,6 +343,95 @@ export class Publisher {
229
343
  group.close();
230
344
  }
231
345
  }
346
+ /**
347
+ * Serve a draft-20 fill on its own fetch stream: the requested range, read from the
348
+ * group cache, capped at the Largest Object snapshot.
349
+ *
350
+ * A fill is a promise once requested. An empty range opens no stream, but a range we
351
+ * cannot serve still opens one and resets it right after the FETCH_HEADER, the draft's
352
+ * fill-failure signal. Nothing here touches the subscription either way.
353
+ */
354
+ async #runFill(options) {
355
+ const { requestId, fill, cache, timescale, stamped, unsubscribed } = options;
356
+ const version = this.#session.version;
357
+ // Everything is inside the try so the cache fork is released on every path out,
358
+ // including a transport failure while opening the stream.
359
+ let stream;
360
+ try {
361
+ if (fill.kind === "empty")
362
+ return;
363
+ stream = await Writer.tryOpen(this.#quic, { cancel: unsubscribed, version });
364
+ if (!stream) {
365
+ console.debug(`fill stream failed to open: fill=${requestId}`);
366
+ return;
367
+ }
368
+ await stream.u53(FetchHeader.type);
369
+ await new FetchHeader({ requestId }).encode(stream, version);
370
+ if (fill.kind !== "group") {
371
+ throw new Error("a fill spanning several groups is not supported");
372
+ }
373
+ const group = takeGroup(cache, Number(fill.sequence));
374
+ try {
375
+ await this.#writeFillGroup(stream, group, fill, timescale, stamped, unsubscribed);
376
+ }
377
+ finally {
378
+ group.close();
379
+ }
380
+ stream.close();
381
+ console.debug(`fill complete: fill=${requestId}`);
382
+ }
383
+ catch (err) {
384
+ // A fill never fails the subscription; its own stream carries the failure.
385
+ const e = error(err);
386
+ console.debug(`fill failed, resetting its stream: fill=${requestId} error=${reason(e)}`);
387
+ stream?.reset(e);
388
+ }
389
+ finally {
390
+ cache.close();
391
+ }
392
+ }
393
+ /**
394
+ * Write one cached group's frames as draft-20 fetch objects.
395
+ *
396
+ * The cap is the Largest Object snapshot: the group may keep growing, but everything
397
+ * past the snapshot belongs to the subscription, not the fill.
398
+ */
399
+ async #writeFillGroup(stream, group, fill, timescale, stamped, unsubscribed) {
400
+ let first = true;
401
+ let next = 0n;
402
+ // The subscriber leaving has to end the fill too. An absolute filter ending below the
403
+ // edge with no end object leaves `until` unset, so this reads until the group closes,
404
+ // which a still-open past group may never do. Watching only the fetch stream would
405
+ // then pin the group and the cache subscription behind it for the life of the track.
406
+ let left = false;
407
+ const cancelled = unsubscribed.then(() => {
408
+ left = true;
409
+ return undefined;
410
+ });
411
+ for (;;) {
412
+ // The group may keep growing, but everything at `until` and beyond belongs to the
413
+ // subscription, so stop without waiting for the group's end.
414
+ if (fill.until !== undefined && next >= fill.until)
415
+ break;
416
+ // Reading from the fill's start drops everything below it, evicted objects included;
417
+ // see the same read in #runGroup.
418
+ const frame = await Promise.race([
419
+ group.readFrameSequence({ from: Number(fill.skip) }),
420
+ stream.closed,
421
+ cancelled,
422
+ ]);
423
+ if (left)
424
+ throw new Error("unsubscribed before the fill finished");
425
+ if (!frame)
426
+ break;
427
+ next = BigInt(frame.sequence) + 1n;
428
+ if (fill.until !== undefined && BigInt(frame.sequence) >= fill.until)
429
+ break;
430
+ const obj = new FetchFrame({ payload: frame.payload, timestamp: stamped ? frame.timestamp : undefined });
431
+ await obj.encode(stream, { group: Number(fill.sequence), object: frame.sequence, first }, timescale, this.#session.version);
432
+ first = false;
433
+ }
434
+ }
232
435
  /**
233
436
  * Handles an incoming SUBSCRIBE_NAMESPACE on a bidi stream.
234
437
  *
@@ -596,4 +799,149 @@ export class Publisher {
596
799
  });
597
800
  }
598
801
  }
802
+ /**
803
+ * Take one group out of a cache view, without waiting.
804
+ *
805
+ * A fill's group is at or below the Largest Object snapshot, so it is either still in the
806
+ * retained window or gone for good: nothing republishes an old sequence, and waiting for one
807
+ * would park for the life of the track. Scanning past it, or running dry, is a cache miss,
808
+ * which the caller turns into the draft's fill-failure reset.
809
+ */
810
+ function takeGroup(cache, sequence) {
811
+ for (;;) {
812
+ const group = cache.tryRecvGroup();
813
+ if (!group)
814
+ throw new Error(`group not found: ${sequence}`);
815
+ if (group.sequence === sequence)
816
+ return group;
817
+ group.close();
818
+ if (group.sequence > sequence)
819
+ throw new Error(`group not found: ${sequence}`);
820
+ }
821
+ }
822
+ /** `a - b`, saturating at zero the way the draft's unsigned arithmetic does. */
823
+ function saturatingSub(a, b) {
824
+ return a > b ? a - b : 0n;
825
+ }
826
+ /** Snapshot the live edge of a track. */
827
+ function liveEdge(track) {
828
+ const latest = track.latest();
829
+ if (latest === undefined)
830
+ return {};
831
+ const largest = track.largest();
832
+ if (!largest)
833
+ return { latest: BigInt(latest), next: { group: BigInt(latest), object: 0n } };
834
+ const group = BigInt(largest.group);
835
+ const object = BigInt(largest.frame);
836
+ return { latest: BigInt(latest), largest: { group, object }, next: { group, object: object + 1n } };
837
+ }
838
+ /**
839
+ * Resolve a SUBSCRIBE's Location Filter into the range to serve.
840
+ *
841
+ * Only draft-20 is honored. Earlier drafts have a Filter Type tag whose absolute forms we
842
+ * never served, and starting to interpret them now would change what an existing peer
843
+ * receives; draft-20 is also the first version whose relative forms can name a past group
844
+ * without the subscriber knowing Largest Object.
845
+ */
846
+ function subscribeRange(msg, edge, version) {
847
+ if (!Filter.isDraft20(version)) {
848
+ if (msg.filter.kind !== "nextObject" && msg.filter.kind !== "unfiltered") {
849
+ console.warn(`filter not supported before draft-20, ignoring: ${msg.filter.kind}`);
850
+ }
851
+ return {};
852
+ }
853
+ return filterRange(msg.filter, edge);
854
+ }
855
+ /** The Locations a single Location Filter selects, resolved against the live edge. */
856
+ function filterRange(filter, edge) {
857
+ switch (filter.kind) {
858
+ // No restriction. moq-lite starts at the beginning of the latest group, which is the
859
+ // join point it is built around; a subscription passes objects as they are published,
860
+ // so an absent filter is not a request to replay history.
861
+ case "unfiltered":
862
+ return {};
863
+ // `{Largest.Group, Largest.Object + 1}`. Everything below it, including the already
864
+ // published head of the current group, is outside the requested range, so the join is
865
+ // mid-group by construction. The draft pairs this with a fill when the subscriber
866
+ // wants the head; see {@link Publisher.runFill}.
867
+ case "nextObject":
868
+ return { start: edge.next };
869
+ // `{Largest.Group + 1 - groups, 0}`: 0 is the next group and 1 is the current one.
870
+ // Counted from `Largest.Group`, which sits below the newest group while that group has
871
+ // no objects yet; only with no largest at all does the newest group stand in for it.
872
+ case "relative": {
873
+ const base = edge.largest?.group ?? edge.latest;
874
+ if (base === undefined)
875
+ return {};
876
+ return { start: { group: saturatingSub(base + 1n, filter.groups), object: 0n } };
877
+ }
878
+ case "absolute":
879
+ return {
880
+ start: { group: filter.startGroup, object: filter.startObject },
881
+ end: filter.endGroup === undefined ? undefined : { group: filter.endGroup, object: filter.endObject },
882
+ };
883
+ }
884
+ }
885
+ /**
886
+ * Trim a group to the filter's object bounds, so nothing outside the requested range is
887
+ * sent. Interior groups are served whole.
888
+ */
889
+ function groupSlice(range, sequence) {
890
+ const at = BigInt(sequence);
891
+ return {
892
+ skip: range.start?.group === at ? Number(range.start.object) : 0,
893
+ until: range.end?.group === at && range.end.object !== undefined ? Number(range.end.object) + 1 : undefined,
894
+ };
895
+ }
896
+ /**
897
+ * Resolve a fill request using the Fetch rules: relative to Largest Object and never
898
+ * extending beyond it. An omitted Location Filter inherits the subscription's.
899
+ */
900
+ function fillRange(fill, subscription, largest) {
901
+ // A Range Filter narrows which objects pass, which we do not implement; serving the
902
+ // unfiltered range instead would deliver objects the peer excluded, so refuse it.
903
+ if (fill.rangeFilters)
904
+ return { kind: "unsupported" };
905
+ const filter = fill.filter ?? subscription;
906
+ // Nothing published (or no precise edge to cap at) means no fill is servable; an empty
907
+ // range opens no stream.
908
+ if (!largest)
909
+ return { kind: "empty" };
910
+ let start;
911
+ switch (filter.kind) {
912
+ // A Fetch without a filter is the whole track up to Largest Object.
913
+ case "unfiltered":
914
+ start = { group: 0n, object: 0n };
915
+ break;
916
+ // One past the edge, which for a Fetch is always empty.
917
+ case "nextObject":
918
+ return { kind: "empty" };
919
+ case "relative":
920
+ start = { group: saturatingSub(largest.group + 1n, filter.groups), object: 0n };
921
+ break;
922
+ case "absolute":
923
+ start = { group: filter.startGroup, object: filter.startObject };
924
+ break;
925
+ }
926
+ // Cap the requested end at Largest Object.
927
+ let end = { group: largest.group, object: largest.object };
928
+ if (filter.kind === "absolute" && filter.endGroup !== undefined) {
929
+ const below = filter.endGroup < largest.group ||
930
+ (filter.endGroup === largest.group && filter.endObject !== undefined && filter.endObject < largest.object);
931
+ if (below)
932
+ end = { group: filter.endGroup, object: filter.endObject };
933
+ }
934
+ if (start.group > end.group ||
935
+ (start.group === end.group && end.object !== undefined && end.object < start.object)) {
936
+ return { kind: "empty" };
937
+ }
938
+ if (start.group !== end.group)
939
+ return { kind: "unsupported" };
940
+ return {
941
+ kind: "group",
942
+ sequence: start.group,
943
+ skip: start.object,
944
+ until: end.object === undefined ? undefined : end.object + 1n,
945
+ };
946
+ }
599
947
  //# sourceMappingURL=publisher.js.map