1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
/*
* Copyright (c) 2018 Deepak Kumar
*
* MIT License
*
* 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.
*
* https://github.com/stomp-js/stompjs
* https://github.com/stomp-js/stompjs/blob/develop/src/frame.ts
*/
"use strict";
const {
BYTE,
} = require("resource://devtools/client/netmonitor/src/components/messages/parsers/stomp/byte.js");
/**
* Frame class represents a STOMP frame.
*
* @internal
*/
class FrameImpl {
/**
* Frame constructor. `command`, `headers` and `body` are available as properties.
*
* @internal
*/
constructor(params) {
const {
command,
headers,
body,
binaryBody,
escapeHeaderValues,
skipContentLengthHeader,
} = params;
this.command = command;
this.headers = Object.assign({}, headers || {});
if (binaryBody) {
this._binaryBody = binaryBody;
this.isBinaryBody = true;
} else {
this._body = body || "";
this.isBinaryBody = false;
}
this.escapeHeaderValues = escapeHeaderValues || false;
this.skipContentLengthHeader = skipContentLengthHeader || false;
}
/**
* body of the frame
*/
get body() {
if (!this._body && this.isBinaryBody) {
this._body = new TextDecoder().decode(this._binaryBody);
}
return this._body;
}
/**
* body as Uint8Array
*/
get binaryBody() {
if (!this._binaryBody && !this.isBinaryBody) {
this._binaryBody = new TextEncoder().encode(this._body);
}
return this._binaryBody;
}
/**
* deserialize a STOMP Frame from raw data.
*
* @internal
*/
static fromRawFrame(rawFrame, escapeHeaderValues) {
const headers = {};
const trim = str => str.replace(/^\s+|\s+$/g, "");
// In case of repeated headers, as per standards, first value need to be used
for (const header of rawFrame.headers.reverse()) {
const key = trim(header[0]);
let value = trim(header[1]);
if (
escapeHeaderValues &&
rawFrame.command !== "CONNECT" &&
rawFrame.command !== "CONNECTED"
) {
value = FrameImpl.hdrValueUnEscape(value);
}
headers[key] = value;
}
return new FrameImpl({
command: rawFrame.command,
headers,
binaryBody: rawFrame.binaryBody,
escapeHeaderValues,
});
}
/**
* @internal
*/
toString() {
return this.serializeCmdAndHeaders();
}
/**
* serialize this Frame in a format suitable to be passed to WebSocket.
* If the body is string the output will be string.
* If the body is binary (i.e. of type Unit8Array) it will be serialized to ArrayBuffer.
*
* @internal
*/
serialize() {
const cmdAndHeaders = this.serializeCmdAndHeaders();
if (this.isBinaryBody) {
return FrameImpl.toUnit8Array(cmdAndHeaders, this._binaryBody).buffer;
}
return cmdAndHeaders + this._body + BYTE.NULL;
}
serializeCmdAndHeaders() {
const lines = [this.command];
if (this.skipContentLengthHeader) {
delete this.headers["content-length"];
}
for (const name of Object.keys(this.headers || {})) {
const value = this.headers[name];
if (
this.escapeHeaderValues &&
this.command !== "CONNECT" &&
this.command !== "CONNECTED"
) {
lines.push(`${name}:${FrameImpl.hdrValueEscape(`${value}`)}`);
} else {
lines.push(`${name}:${value}`);
}
}
if (
this.isBinaryBody ||
(!this.isBodyEmpty() && !this.skipContentLengthHeader)
) {
lines.push(`content-length:${this.bodyLength()}`);
}
return lines.join(BYTE.LF) + BYTE.LF + BYTE.LF;
}
isBodyEmpty() {
return this.bodyLength() === 0;
}
bodyLength() {
const binaryBody = this.binaryBody;
return binaryBody ? binaryBody.length : 0;
}
/**
* Compute the size of a UTF-8 string by counting its number of bytes
* (and not the number of characters composing the string)
*/
static sizeOfUTF8(s) {
return s ? new TextEncoder().encode(s).length : 0;
}
static toUnit8Array(cmdAndHeaders, binaryBody) {
const uint8CmdAndHeaders = new TextEncoder().encode(cmdAndHeaders);
const nullTerminator = new Uint8Array([0]);
const uint8Frame = new Uint8Array(
uint8CmdAndHeaders.length + binaryBody.length + nullTerminator.length
);
uint8Frame.set(uint8CmdAndHeaders);
uint8Frame.set(binaryBody, uint8CmdAndHeaders.length);
uint8Frame.set(
nullTerminator,
uint8CmdAndHeaders.length + binaryBody.length
);
return uint8Frame;
}
/**
* Serialize a STOMP frame as per STOMP standards, suitable to be sent to the STOMP broker.
*
* @internal
*/
static marshall(params) {
const frame = new FrameImpl(params);
return frame.serialize();
}
/**
* Escape header values
*/
static hdrValueEscape(str) {
return str
.replace(/\\/g, "\\\\")
.replace(/\r/g, "\\r")
.replace(/\n/g, "\\n")
.replace(/:/g, "\\c");
}
/**
* UnEscape header values
*/
static hdrValueUnEscape(str) {
return str
.replace(/\\r/g, "\r")
.replace(/\\n/g, "\n")
.replace(/\\c/g, ":")
.replace(/\\\\/g, "\\");
}
}
module.exports = { Frame: FrameImpl };
|