diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /devtools/client/netmonitor/src/components/messages/parsers/sockjs/index.js | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/client/netmonitor/src/components/messages/parsers/sockjs/index.js')
-rw-r--r-- | devtools/client/netmonitor/src/components/messages/parsers/sockjs/index.js | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/devtools/client/netmonitor/src/components/messages/parsers/sockjs/index.js b/devtools/client/netmonitor/src/components/messages/parsers/sockjs/index.js new file mode 100644 index 0000000000..b688b84b36 --- /dev/null +++ b/devtools/client/netmonitor/src/components/messages/parsers/sockjs/index.js @@ -0,0 +1,56 @@ +/* + * SockJS is a browser JavaScript library that provides a WebSocket-like object. + * SockJS gives you a coherent, cross-browser, Javascript API which creates a low latency, + * full duplex, cross-domain communication channel between the browser and the web server. + * + * Copyright (c) 2011-2018 The sockjs-client Authors. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of the library source tree. + * + * https://github.com/sockjs/sockjs-client + */ + +"use strict"; + +function parseSockJS(msg) { + const type = msg.slice(0, 1); + const content = msg.slice(1); + + // first check for messages that don't need a payload + switch (type) { + case "o": + return { type: "open" }; + case "h": + return { type: "heartbeat" }; + } + + let payload; + if (content) { + try { + payload = JSON.parse(content); + } catch (e) { + return null; + } + } + + if (typeof payload === "undefined") { + return null; + } + + switch (type) { + case "a": + return { type: "message", data: payload }; + case "m": + return { type: "message", data: payload }; + case "c": + const [code, message] = payload; + return { type: "close", code, message }; + default: + return null; + } +} + +module.exports = { + parseSockJS, +}; |