MCPcopy
hub / github.com/socketio/socket.io / add

Method add

packages/socket.io-parser/lib/index.ts:181–217  ·  packages/socket.io-parser/lib/index.ts::Decoder.add

* Decodes an encoded packet string into packet JSON. * * @param {String} obj - encoded packet

(obj: any)

Source from the content-addressed store, hash-verified

179 */
180
181 public add(obj: any) {
182 let packet;
183 if (typeof obj === class="st">"string") {
184 if (this.reconstructor) {
185 throw new Error(class="st">"got plaintext data when reconstructing a packet");
186 }
187 packet = this.decodeString(obj);
188 const isBinaryEvent = packet.type === PacketType.BINARY_EVENT;
189 if (isBinaryEvent || packet.type === PacketType.BINARY_ACK) {
190 packet.type = isBinaryEvent ? PacketType.EVENT : PacketType.ACK;
191 class="cm">// binary packet's json
192 this.reconstructor = new BinaryReconstructor(packet);
193
194 class="cm">// no attachments, labeled binary but no binary data to follow
195 if (packet.attachments === 0) {
196 super.emitReserved(class="st">"decoded", packet);
197 }
198 } else {
199 class="cm">// non-binary full packet
200 super.emitReserved(class="st">"decoded", packet);
201 }
202 } else if (isBinary(obj) || obj.base64) {
203 class="cm">// raw binary data
204 if (!this.reconstructor) {
205 throw new Error(class="st">"got binary data when not reconstructing a packet");
206 } else {
207 packet = this.reconstructor.takeBinaryData(obj);
208 if (packet) {
209 class="cm">// received final buffer
210 this.reconstructor = null;
211 super.emitReserved(class="st">"decoded", packet);
212 }
213 }
214 } else {
215 throw new Error(class="st">"Unknown type: " + obj);
216 }
217 }
218
219 /**
220 * Decode a packet String (JSON data)

Callers 15

testFunction · 0.95
parser.jsFile · 0.45
isInvalidPayloadFunction · 0.45
isInvalidAttachmentCountFunction · 0.45
buffer.jsFile · 0.45
helpers.jsFile · 0.45
index.jsFile · 0.45
index.jsFile · 0.45
setupPrimaryFunction · 0.45
toMethod · 0.45
exceptMethod · 0.45
ondataMethod · 0.45

Calls 3

decodeStringMethod · 0.95
isBinaryFunction · 0.90
takeBinaryDataMethod · 0.45

Tested by 1

testFunction · 0.76