11import type * as Path from "../path.ts" ;
22import type { Reader , Writer } from "../stream.ts" ;
3+ import { GroupOrder } from "./group.ts" ;
34import { Location } from "./location.js" ;
45import * as Message from "./message.ts" ;
56import * as Namespace from "./namespace.ts" ;
67import { Parameters } from "./parameters.ts" ;
78
9+ const FETCH_END = 0x03 ;
10+
811export const FetchType = {
912 Standalone : 0x1 ,
1013 Relative : 0x2 ,
@@ -35,10 +38,10 @@ export class Fetch {
3538
3639 requestId : bigint ;
3740 subscriberPriority : number ;
38- groupOrder : number ;
41+ groupOrder : GroupOrder ;
3942 fetchType : FetchType ;
4043
41- constructor ( requestId : bigint , subscriberPriority : number , groupOrder : number , fetchType : FetchType ) {
44+ constructor ( requestId : bigint , subscriberPriority : number , groupOrder : GroupOrder , fetchType : FetchType ) {
4245 this . requestId = requestId ;
4346 this . subscriberPriority = subscriberPriority ;
4447 this . groupOrder = groupOrder ;
@@ -48,7 +51,7 @@ export class Fetch {
4851 async #encode( w : Writer ) : Promise < void > {
4952 await w . u62 ( this . requestId ) ;
5053 await w . u8 ( this . subscriberPriority ) ;
51- await w . u8 ( this . groupOrder ) ;
54+ await this . groupOrder . encode ( w ) ;
5255 await w . u53 ( this . fetchType . type ) ;
5356 if ( this . fetchType . type === FetchType . Standalone ) {
5457 await Namespace . encode ( w , this . fetchType . namespace ) ;
@@ -79,7 +82,7 @@ export class Fetch {
7982 static async #decode( r : Reader ) : Promise < Fetch > {
8083 const requestId = await r . u62 ( ) ;
8184 const subscriberPriority = await r . u8 ( ) ;
82- const groupOrder = await r . u8 ( ) ;
85+ const groupOrder = await GroupOrder . decode ( r ) ;
8386 const fetchType = await r . u53 ( ) ;
8487
8588 if ( fetchType === FetchType . Standalone ) {
@@ -127,11 +130,11 @@ export class FetchOk {
127130 static id = 0x18 ;
128131
129132 requestId : bigint ;
130- groupOrder : number ;
133+ groupOrder : GroupOrder ;
131134 endOfTrack : boolean ;
132135 endLocation : Location ;
133136
134- constructor ( requestId : bigint , groupOrder : number , endOfTrack : boolean , endLocation : Location ) {
137+ constructor ( requestId : bigint , groupOrder : GroupOrder , endOfTrack : boolean , endLocation : Location ) {
135138 this . requestId = requestId ;
136139 this . groupOrder = groupOrder ;
137140 this . endOfTrack = endOfTrack ;
@@ -140,7 +143,7 @@ export class FetchOk {
140143
141144 async #encode( w : Writer ) : Promise < void > {
142145 await w . u62 ( this . requestId ) ;
143- await w . u8 ( this . groupOrder ) ;
146+ await this . groupOrder . encode ( w ) ;
144147 await w . bool ( this . endOfTrack ) ;
145148 this . endLocation . encode ( w ) ;
146149 await w . u53 ( 0 ) ; // no parameters
@@ -156,7 +159,7 @@ export class FetchOk {
156159
157160 static async #decode( r : Reader ) : Promise < FetchOk > {
158161 const requestId = await r . u62 ( ) ;
159- const groupOrder = await r . u8 ( ) ;
162+ const groupOrder = await GroupOrder . decode ( r ) ;
160163 const endOfTrack = await r . bool ( ) ;
161164 const endLocation = await Location . decode ( r ) ;
162165 await Parameters . decode ( r ) ; // ignore parameters
@@ -225,3 +228,88 @@ export class FetchCancel {
225228 return new FetchCancel ( requestId ) ;
226229 }
227230}
231+
232+ export class FetchHeader {
233+ static id = 0x5 ;
234+
235+ requestId : bigint ;
236+
237+ constructor ( requestId : bigint ) {
238+ this . requestId = requestId ;
239+ }
240+
241+ async encode ( w : Writer ) : Promise < void > {
242+ await w . u62 ( this . requestId ) ;
243+ }
244+
245+ static async decode ( r : Reader ) : Promise < FetchHeader > {
246+ const requestId = await r . u62 ( ) ;
247+ return new FetchHeader ( requestId ) ;
248+ }
249+ }
250+
251+ export class FetchObject {
252+ groupId : number ;
253+ subgroupId : number ;
254+ objectId : number ;
255+ publisherPriority : number ;
256+ payload ?: Uint8Array ;
257+
258+ constructor (
259+ groupId : number ,
260+ subgroupId : number ,
261+ objectId : number ,
262+ publisherPriority : number ,
263+ payload ?: Uint8Array ,
264+ ) {
265+ this . groupId = groupId ;
266+ this . subgroupId = subgroupId ;
267+ this . objectId = objectId ;
268+ this . publisherPriority = publisherPriority ;
269+ this . payload = payload ;
270+ }
271+
272+ async encode ( w : Writer ) : Promise < void > {
273+ await w . u53 ( this . groupId ) ;
274+ await w . u53 ( this . subgroupId ) ;
275+ await w . u53 ( this . objectId ) ;
276+ await w . u8 ( this . publisherPriority ) ;
277+ await w . u53 ( 0 ) ; // no extension headers
278+
279+ if ( this . payload !== undefined ) {
280+ await w . u53 ( this . payload . byteLength ) ;
281+ if ( this . payload . byteLength === 0 ) {
282+ await w . u53 ( 0 ) ; // status = normal
283+ } else {
284+ await w . write ( this . payload ) ;
285+ }
286+ } else {
287+ await w . u53 ( 0 ) ; // no payload, length = 0
288+ await w . u53 ( FETCH_END ) ; // no payload, status = end
289+ }
290+ }
291+
292+ static async decode ( r : Reader ) : Promise < FetchObject > {
293+ const groupId = await r . u53 ( ) ;
294+ const subgroupId = await r . u53 ( ) ;
295+ const objectId = await r . u53 ( ) ;
296+ const publisherPriority = await r . u8 ( ) ;
297+ const payloadLength = await r . u53 ( ) ;
298+
299+ let payload : Uint8Array | undefined ;
300+ if ( payloadLength === 0 ) {
301+ const status = await r . u53 ( ) ;
302+ if ( status === 0 ) {
303+ payload = new Uint8Array ( 0 ) ;
304+ } else if ( status === FETCH_END ) {
305+ payload = undefined ;
306+ } else {
307+ throw new Error ( `unexpected status: ${ status } ` ) ;
308+ }
309+ } else {
310+ payload = await r . read ( payloadLength ) ;
311+ }
312+
313+ return new FetchObject ( groupId , subgroupId , objectId , publisherPriority , payload ) ;
314+ }
315+ }
0 commit comments