33import { CHANNEL_EVENTS } from '../lib/constants'
44
55export type Msg < T > = {
6- join_ref : string
7- ref : string
6+ join_ref ? : string | null
7+ ref ? : string | null
88 topic : string
99 event : string
1010 payload : T
@@ -42,19 +42,22 @@ export default class Serializer {
4242 }
4343
4444 private _binaryEncodePush ( message : Msg < ArrayBuffer > ) {
45- const { join_ref, ref, event, topic, payload } = message
46- const metaLength = this . META_LENGTH + join_ref . length + ref . length + topic . length + event . length
45+ const { event, topic, payload } = message
46+ const ref = message . ref ?? ''
47+ const joinRef = message . join_ref ?? ''
48+
49+ const metaLength = this . META_LENGTH + joinRef . length + ref . length + topic . length + event . length
4750
4851 const header = new ArrayBuffer ( this . HEADER_LENGTH + metaLength )
4952 let view = new DataView ( header )
5053 let offset = 0
5154
5255 view . setUint8 ( offset ++ , this . KINDS . push ) // kind
53- view . setUint8 ( offset ++ , join_ref . length )
56+ view . setUint8 ( offset ++ , joinRef . length )
5457 view . setUint8 ( offset ++ , ref . length )
5558 view . setUint8 ( offset ++ , topic . length )
5659 view . setUint8 ( offset ++ , event . length )
57- Array . from ( join_ref , ( char ) => view . setUint8 ( offset ++ , char . charCodeAt ( 0 ) ) )
60+ Array . from ( joinRef , ( char ) => view . setUint8 ( offset ++ , char . charCodeAt ( 0 ) ) )
5861 Array . from ( ref , ( char ) => view . setUint8 ( offset ++ , char . charCodeAt ( 0 ) ) )
5962 Array . from ( topic , ( char ) => view . setUint8 ( offset ++ , char . charCodeAt ( 0 ) ) )
6063 Array . from ( event , ( char ) => view . setUint8 ( offset ++ , char . charCodeAt ( 0 ) ) )
@@ -75,13 +78,15 @@ export default class Serializer {
7578 }
7679
7780 private _encodeBinaryUserBroadcastPush ( message : Msg < { event : string } & { [ key : string ] : any } > ) {
78- const { join_ref, ref, topic } = message
81+ const topic = message . topic
82+ const ref = message . ref ?? ''
83+ const joinRef = message . join_ref ?? ''
7984 const userEvent = message . payload . event
8085 const userPayload = message . payload ?. payload ?? new ArrayBuffer ( 0 )
8186
8287 const metaLength =
8388 this . USER_BROADCAST_PUSH_META_LENGTH +
84- join_ref . length +
89+ joinRef . length +
8590 ref . length +
8691 topic . length +
8792 userEvent . length
@@ -91,12 +96,12 @@ export default class Serializer {
9196 let offset = 0
9297
9398 view . setUint8 ( offset ++ , this . KINDS . userBroadcastPush ) // kind
94- view . setUint8 ( offset ++ , join_ref . length )
99+ view . setUint8 ( offset ++ , joinRef . length )
95100 view . setUint8 ( offset ++ , ref . length )
96101 view . setUint8 ( offset ++ , topic . length )
97102 view . setUint8 ( offset ++ , userEvent . length )
98103 view . setUint8 ( offset ++ , this . BINARY_ENCODING )
99- Array . from ( join_ref , ( char ) => view . setUint8 ( offset ++ , char . charCodeAt ( 0 ) ) )
104+ Array . from ( joinRef , ( char ) => view . setUint8 ( offset ++ , char . charCodeAt ( 0 ) ) )
100105 Array . from ( ref , ( char ) => view . setUint8 ( offset ++ , char . charCodeAt ( 0 ) ) )
101106 Array . from ( topic , ( char ) => view . setUint8 ( offset ++ , char . charCodeAt ( 0 ) ) )
102107 Array . from ( userEvent , ( char ) => view . setUint8 ( offset ++ , char . charCodeAt ( 0 ) ) )
@@ -109,7 +114,9 @@ export default class Serializer {
109114 }
110115
111116 private _encodeJsonUserBroadcastPush ( message : Msg < { event : string } & { [ key : string ] : any } > ) {
112- const { join_ref, ref, topic } = message
117+ const topic = message . topic
118+ const ref = message . ref ?? ''
119+ const joinRef = message . join_ref ?? ''
113120 const userEvent = message . payload . event
114121 const userPayload = message . payload ?. payload ?? { }
115122
@@ -118,7 +125,7 @@ export default class Serializer {
118125
119126 const metaLength =
120127 this . USER_BROADCAST_PUSH_META_LENGTH +
121- join_ref . length +
128+ joinRef . length +
122129 ref . length +
123130 topic . length +
124131 userEvent . length
@@ -128,12 +135,12 @@ export default class Serializer {
128135 let offset = 0
129136
130137 view . setUint8 ( offset ++ , this . KINDS . userBroadcastPush ) // kind
131- view . setUint8 ( offset ++ , join_ref . length )
138+ view . setUint8 ( offset ++ , joinRef . length )
132139 view . setUint8 ( offset ++ , ref . length )
133140 view . setUint8 ( offset ++ , topic . length )
134141 view . setUint8 ( offset ++ , userEvent . length )
135142 view . setUint8 ( offset ++ , this . JSON_ENCODING )
136- Array . from ( join_ref , ( char ) => view . setUint8 ( offset ++ , char . charCodeAt ( 0 ) ) )
143+ Array . from ( joinRef , ( char ) => view . setUint8 ( offset ++ , char . charCodeAt ( 0 ) ) )
137144 Array . from ( ref , ( char ) => view . setUint8 ( offset ++ , char . charCodeAt ( 0 ) ) )
138145 Array . from ( topic , ( char ) => view . setUint8 ( offset ++ , char . charCodeAt ( 0 ) ) )
139146 Array . from ( userEvent , ( char ) => view . setUint8 ( offset ++ , char . charCodeAt ( 0 ) ) )
0 commit comments