ESM: Ported BSON, ToTable, Filetime and XKCD operations

This commit is contained in:
n1474335 2018-05-21 17:37:32 +00:00
parent eed28f67d5
commit 749b0510e7
7 changed files with 483 additions and 2 deletions

View File

@ -0,0 +1,50 @@
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import Operation from "../Operation";
import bsonjs from "bson";
import OperationError from "../errors/OperationError";
/**
* BSON deserialise operation
*/
class BSONDeserialise extends Operation {
/**
* BSONDeserialise constructor
*/
constructor() {
super();
this.name = "BSON deserialise";
this.module = "BSON";
this.description = "BSON is a computer data interchange format used mainly as a data storage and network transfer format in the MongoDB database. It is a binary form for representing simple data structures, associative arrays (called objects or documents in MongoDB), and various data types of specific interest to MongoDB. The name 'BSON' is based on the term JSON and stands for 'Binary JSON'.<br><br>Input data should be in a raw bytes format.";
this.inputType = "ArrayBuffer";
this.outputType = "string";
this.args = [];
}
/**
* @param {ArrayBuffer} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
if (!input.byteLength) return "";
const bson = new bsonjs();
try {
const data = bson.deserialize(new Buffer(input));
return JSON.stringify(data, null, 2);
} catch (err) {
throw new OperationError(err.toString());
}
}
}
export default BSONDeserialise;

View File

@ -0,0 +1,50 @@
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import Operation from "../Operation";
import bsonjs from "bson";
import OperationError from "../errors/OperationError";
/**
* BSON serialise operation
*/
class BSONSerialise extends Operation {
/**
* BSONSerialise constructor
*/
constructor() {
super();
this.name = "BSON serialise";
this.module = "BSON";
this.description = "BSON is a computer data interchange format used mainly as a data storage and network transfer format in the MongoDB database. It is a binary form for representing simple data structures, associative arrays (called objects or documents in MongoDB), and various data types of specific interest to MongoDB. The name 'BSON' is based on the term JSON and stands for 'Binary JSON'.<br><br>Input data should be valid JSON.";
this.inputType = "string";
this.outputType = "ArrayBuffer";
this.args = [];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {ArrayBuffer}
*/
run(input, args) {
if (!input) return new ArrayBuffer();
const bson = new bsonjs();
try {
const data = JSON.parse(input);
return bson.serialize(data).buffer;
} catch (err) {
throw new OperationError(err.toString());
}
}
}
export default BSONSerialise;

View File

@ -0,0 +1,189 @@
/**
* @author Mark Jones [github.com/justanothermark]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import Operation from "../Operation";
import Utils from "../Utils";
/**
* To Table operation
*/
class ToTable extends Operation {
/**
* ToTable constructor
*/
constructor() {
super();
this.name = "To Table";
this.module = "Default";
this.description = "Data can be split on different characters and rendered as an HTML or ASCII table with an optional header row.<br><br>Supports the CSV (Comma Separated Values) file format by default. Change the cell delimiter argument to <code>\\t</code> to support TSV (Tab Separated Values) or <code>|</code> for PSV (Pipe Separated Values).<br><br>You can enter as many delimiters as you like. Each character will be treat as a separate possible delimiter.";
this.inputType = "string";
this.outputType = "html";
this.args = [
{
"name": "Cell delimiters",
"type": "binaryShortString",
"value": ","
},
{
"name": "Row delimiters",
"type": "binaryShortString",
"value": "\\n\\r"
},
{
"name": "Make first row header",
"type": "boolean",
"value": false
},
{
"name": "Format",
"type": "option",
"value": ["ASCII", "HTML"]
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {html}
*/
run(input, args) {
const [cellDelims, rowDelims, firstRowHeader, format] = args;
// Process the input into a nested array of elements.
const tableData = Utils.parseCSV(input, cellDelims.split(""), rowDelims.split(""));
if (!tableData.length) return "";
// Render the data in the requested format.
switch (format) {
case "ASCII":
return asciiOutput(tableData);
case "HTML":
default:
return htmlOutput(tableData);
}
/**
* Outputs an array of data as an ASCII table.
*
* @param {string[][]} tableData
* @returns {string}
*/
function asciiOutput(tableData) {
const horizontalBorder = "-";
const verticalBorder = "|";
const crossBorder = "+";
let output = "";
const longestCells = [];
// Find longestCells value per column to pad cells equally.
tableData.forEach(function(row, index) {
row.forEach(function(cell, cellIndex) {
if (longestCells[cellIndex] === undefined || cell.length > longestCells[cellIndex]) {
longestCells[cellIndex] = cell.length;
}
});
});
// Add the top border of the table to the output.
output += outputHorizontalBorder(longestCells);
// If the first row is a header, remove the row from the data and
// add it to the output with another horizontal border.
if (firstRowHeader) {
const row = tableData.shift();
output += outputRow(row, longestCells);
output += outputHorizontalBorder(longestCells);
}
// Add the rest of the table rows.
tableData.forEach(function(row, index) {
output += outputRow(row, longestCells);
});
// Close the table with a final horizontal border.
output += outputHorizontalBorder(longestCells);
return output;
/**
* Outputs a row of correctly padded cells.
*/
function outputRow(row, longestCells) {
let rowOutput = verticalBorder;
row.forEach(function(cell, index) {
rowOutput += " " + cell + " ".repeat(longestCells[index] - cell.length) + " " + verticalBorder;
});
rowOutput += "\n";
return rowOutput;
}
/**
* Outputs a horizontal border with a different character where
* the horizontal border meets a vertical border.
*/
function outputHorizontalBorder(longestCells) {
let rowOutput = crossBorder;
longestCells.forEach(function(cellLength) {
rowOutput += horizontalBorder.repeat(cellLength + 2) + crossBorder;
});
rowOutput += "\n";
return rowOutput;
}
}
/**
* Outputs a table of data as a HTML table.
*
* @param {string[][]} tableData
* @returns {string}
*/
function htmlOutput(tableData) {
// Start the HTML output with suitable classes for styling.
let output = "<table class='table table-hover table-condensed table-bordered table-nonfluid'>";
// If the first row is a header then put it in <thead> with <th> cells.
if (firstRowHeader) {
const row = tableData.shift();
output += "<thead>";
output += outputRow(row, "th");
output += "</thead>";
}
// Output the rest of the rows in the <tbody>.
output += "<tbody>";
tableData.forEach(function(row, index) {
output += outputRow(row, "td");
});
// Close the body and table elements.
output += "</tbody></table>";
return output;
/**
* Outputs a table row.
*
* @param {string[]} row
* @param {string} cellType
*/
function outputRow(row, cellType) {
let output = "<tr>";
row.forEach(function(cell) {
output += "<" + cellType + ">" + cell + "</" + cellType + ">";
});
output += "</tr>";
return output;
}
}
}
}
export default ToTable;

View File

@ -0,0 +1,76 @@
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2017
* @license Apache-2.0
*/
import Operation from "../Operation";
import BigNumber from "bignumber.js";
import OperationError from "../errors/OperationError";
/**
* UNIX Timestamp to Windows Filetime operation
*/
class UNIXTimestampToWindowsFiletime extends Operation {
/**
* UNIXTimestampToWindowsFiletime constructor
*/
constructor() {
super();
this.name = "UNIX Timestamp to Windows Filetime";
this.module = "Default";
this.description = "Converts a UNIX timestamp to a Windows Filetime value.<br><br>A Windows Filetime is a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 UTC.<br><br>A UNIX timestamp is a 32-bit value representing the number of seconds since January 1, 1970 UTC (the UNIX epoch).<br><br>This operation also supports UNIX timestamps in milliseconds, microseconds and nanoseconds.";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
"name": "Input units",
"type": "option",
"value": ["Seconds (s)", "Milliseconds (ms)", "Microseconds (μs)", "Nanoseconds (ns)"]
},
{
"name": "Output format",
"type": "option",
"value": ["Decimal", "Hex"]
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const [units, format] = args;
if (!input) return "";
input = new BigNumber(input);
if (units === "Seconds (s)"){
input = input.multipliedBy(new BigNumber("10000000"));
} else if (units === "Milliseconds (ms)") {
input = input.multipliedBy(new BigNumber("10000"));
} else if (units === "Microseconds (μs)") {
input = input.multiplyiedBy(new BigNumber("10"));
} else if (units === "Nanoseconds (ns)") {
input = input.dividedBy(new BigNumber("100"));
} else {
throw new OperationError("Unrecognised unit");
}
input = input.plus(new BigNumber("116444736000000000"));
if (format === "Hex"){
return input.toString(16);
} else {
return input.toFixed();
}
}
}
export default UNIXTimestampToWindowsFiletime;

View File

@ -0,0 +1,76 @@
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2017
* @license Apache-2.0
*/
import Operation from "../Operation";
import BigNumber from "bignumber.js";
import OperationError from "../errors/OperationError";
/**
* Windows Filetime to UNIX Timestamp operation
*/
class WindowsFiletimeToUNIXTimestamp extends Operation {
/**
* WindowsFiletimeToUNIXTimestamp constructor
*/
constructor() {
super();
this.name = "Windows Filetime to UNIX Timestamp";
this.module = "Default";
this.description = "Converts a Windows Filetime value to a UNIX timestamp.<br><br>A Windows Filetime is a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 UTC.<br><br>A UNIX timestamp is a 32-bit value representing the number of seconds since January 1, 1970 UTC (the UNIX epoch).<br><br>This operation also supports UNIX timestamps in milliseconds, microseconds and nanoseconds.";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
"name": "Output units",
"type": "option",
"value": ["Seconds (s)", "Milliseconds (ms)", "Microseconds (μs)", "Nanoseconds (ns)"]
},
{
"name": "Input format",
"type": "option",
"value": ["Decimal", "Hex"]
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const [units, format] = args;
if (!input) return "";
if (format === "Hex") {
input = new BigNumber(input, 16);
} else {
input = new BigNumber(input);
}
input = input.minus(new BigNumber("116444736000000000"));
if (units === "Seconds (s)"){
input = input.dividedBy(new BigNumber("10000000"));
} else if (units === "Milliseconds (ms)") {
input = input.dividedBy(new BigNumber("10000"));
} else if (units === "Microseconds (μs)") {
input = input.dividedBy(new BigNumber("10"));
} else if (units === "Nanoseconds (ns)") {
input = input.multipliedBy(new BigNumber("100"));
} else {
throw new OperationError("Unrecognised unit");
}
return input.toFixed();
}
}
export default WindowsFiletimeToUNIXTimestamp;

View File

@ -0,0 +1,40 @@
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import Operation from "../Operation";
/**
* XKCD Random Number operation
*/
class XKCDRandomNumber extends Operation {
/**
* XKCDRandomNumber constructor
*/
constructor() {
super();
this.name = "XKCD Random Number";
this.module = "Default";
this.description = "RFC 1149.5 specifies 4 as the standard IEEE-vetted random number.<br><br><a href='https://xkcd.com/221/'>XKCD #221</a>";
this.inputType = "string";
this.outputType = "number";
this.args = [];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {number}
*/
run(input, args) {
return 4; // chosen by fair dice roll.
// guaranteed to be random.
}
}
export default XKCDRandomNumber;

View File

@ -28,7 +28,7 @@ import "./tests/operations/Base58";
import "./tests/operations/Base64";
import "./tests/operations/BCD";
// import "./tests/operations/BitwiseOp";
// import "./tests/operations/BSON";
import "./tests/operations/BSON";
import "./tests/operations/ByteRepr";
import "./tests/operations/CartesianProduct";
import "./tests/operations/CharEnc";
@ -37,7 +37,7 @@ import "./tests/operations/Checksum";
// import "./tests/operations/Code";
// import "./tests/operations/Compress";
// import "./tests/operations/Crypt";
// import "./tests/operations/DateTime";
import "./tests/operations/DateTime";
import "./tests/operations/Fork";
import "./tests/operations/Jump";
import "./tests/operations/ConditionalJump";