Skip to content

Instantly share code, notes, and snippets.

@wreulicke
Last active March 22, 2023 06:47
Show Gist options
  • Save wreulicke/e05b42ba79f42768a54f3b2a9cb7c416 to your computer and use it in GitHub Desktop.
Save wreulicke/e05b42ba79f42768a54f3b2a9cb7c416 to your computer and use it in GitHub Desktop.
Minimal packet parser for socket.io
export const open = "0"
export const close = "1"
export const ping = "2"
export const pong = "3"
export const message = "4"
export const upgrade = "5"
export const noop = "6"
const packetTypes = {
[open]: "open",
[close]: "close",
[ping]: "ping",
[pong]: "pong",
[message]: "message",
[upgrade]: "upgrade",
[noop]: "noop"
}
export function createMessage(data){
return message + data
}
export function decode(data) {
if (data.length == 0) {
return {
type: "invalid"
}
}
const type = packetTypes[data[0]]
return {
type: type || "invalid",
data: data.slice(1)
}
}
const data = "40/nsp"
import {decode as decodeEngineIO} from "./engine.io-packet.js"
import {decode as decodeSocketIO} from "./socket.io-packet.js"
const engineIOPacket = decodeEngineIO(data)
if (engineIOPacket.type === "invalid") {
throw new Error("invalid packet")
}
const socketIOPacket = decodeSocketIO(engineIOPacket.data)
if (socketIOPacket.type === "invalid") {
throw new Error("invalid packet")
}
console.log(JSON.stringify(socketIOPacket))
// {"type": "connect", nsp: "/nsp"}
MIT License
Copyright (c) 2020 wreulicke
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
import { createMessage } from "./engine.io-packet.js"
export const connect = "0"
export const disconnect = "1"
export const event = "2"
export const ack = "3"
export const error = "4"
// binary is not supported
// export const binaryEvent = "5"
// export const binaryAck = "6"
const packetTypes = {
[connect]: "connect",
[disconnect]: "disconnect",
[event]: "event",
[ack]: "ack",
[error]: "error",
}
export function connectPacket(namespace) {
return createMessage(connect + namespace)
}
export function createEvent(namespace, data) {
return createMessage(`${event}${namespace},${JSON.stringify(data)}`)
}
function tryParse(data) {
try {
return JSON.parse(data)
} catch (e) {
return false
}
}
export function decode(data) {
const type = packetTypes[data[0]]
if (type == null) {
return {
type: "invalid"
}
}
const payload = data.slice(1)
const namespaceIdx = payload.indexOf(",")
if (namespaceIdx === -1) {
return {
type,
nsp: "/"
}
}
// TODO: Write code here if we need support to parse id in packet
const nsp = payload.slice(0, namespaceIdx)
const parseResult = tryParse(payload.slice(namespaceIdx + 1))
return {
type,
nsp,
data: parseResult || undefined
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment