This code provides utilities for encoding and decoding out-of-band (OOB) data, a method used to send special instructions or metadata within a network stream.
npm run import -- "quakejs utilities"
function _formatOOB(data) {
var str = '\xff\xff\xff\xff' + data + '\x00';
var buffer = new ArrayBuffer(str.length);
var view = new Uint8Array(buffer);
for (var i = 0; i < str.length; i++) {
view[i] = str.charCodeAt(i);
}
return buffer;
};
function _stripOOB(buffer) {
var view = new DataView(buffer);
if (view.getInt32(0) !== -1) {
return null;
}
var str = '';
var i_start = 4; /* ignore leading -1 */
var i_end = buffer.byteLength;
/* ignore trailing whitespace */
while (i_end > i_start && view.getUint8(i_end - 1) <= ' '.charCodeAt(0)) {
--i_end;
}
for (var i = i_start; i < i_end; i++) {
var c = String.fromCharCode(view.getUint8(i));
str += c;
}
return str;
};
module.exports = {
_formatOOB,
_stripOOB
}
/**
* Formats input data to include OOB (out of bounds) markers.
*
* @param {string} data - the input data to format.
* @returns {ArrayBuffer} the formatted ArrayBuffer.
*/
function formatOOB(data) {
// Use a single line to create the formatted string
const formattedData = '\xff\xff\xff\xff' + data + '\x00';
// Use a more modern approach with TypedArray
const uint8Array = new Uint8Array(formattedData.length);
for (let i = 0; i < formattedData.length; i++) {
uint8Array[i] = formattedData.charCodeAt(i);
}
// Return the ArrayBuffer view
return uint8Array.buffer;
}
/**
* Strips OOB (out of bounds) markers from the provided ArrayBuffer.
*
* @param {ArrayBuffer} buffer - the input ArrayBuffer to strip.
* @returns {string|null} the stripped string, or null if no valid data is found.
*/
function stripOOB(buffer) {
// Convert the ArrayBuffer to a DataView for easier access
const view = new DataView(buffer);
// Check for valid data at the start of the buffer
if (view.getInt32(0) === -1n) {
// If no valid data, return null
return null;
}
// Calculate the start and end indices for the valid data
let start = 4; /* ignore leading -1 */ // TODO: remove hardcoded value
let end = buffer.byteLength;
while (end > start && view.getUint8(end - 1) <=''.charCodeAt(0)) {
--end;
}
// Read the valid data from the buffer
let result = '';
for (let i = start; i < end; i++) {
result += String.fromCharCode(view.getUint8(i));
}
return result;
}
module.exports = {
formatOOB,
stripOOB
};
This code defines two functions for handling out-of-band (OOB) data, a common technique used in network communication to send special instructions or metadata alongside regular data.
_formatOOB(data)
:
data
as input.\xff\xff\xff\xff
) and a null terminator (\x00
) to the string.ArrayBuffer
.ArrayBuffer
containing the formatted OOB data._stripOOB(buffer)
:
ArrayBuffer
as input.DataView
to access the buffer's contents as numbers.-1
). If not, it returns null
, indicating invalid OOB data.In essence, these functions provide a way to encode and decode messages that need to be treated differently from regular data within a network stream.