BSONSerialise.mjs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * @author n1474335 [n1474335@gmail.com]
  3. * @copyright Crown Copyright 2018
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation.mjs";
  7. import bson from "bson";
  8. import OperationError from "../errors/OperationError.mjs";
  9. /**
  10. * BSON serialise operation
  11. */
  12. class BSONSerialise extends Operation {
  13. /**
  14. * BSONSerialise constructor
  15. */
  16. constructor() {
  17. super();
  18. this.name = "BSON serialise";
  19. this.module = "Serialise";
  20. 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.";
  21. this.infoURL = "https://wikipedia.org/wiki/BSON";
  22. this.inputType = "string";
  23. this.outputType = "ArrayBuffer";
  24. this.args = [];
  25. }
  26. /**
  27. * @param {string} input
  28. * @param {Object[]} args
  29. * @returns {ArrayBuffer}
  30. */
  31. run(input, args) {
  32. if (!input) return new ArrayBuffer();
  33. try {
  34. const data = JSON.parse(input);
  35. return bson.serialize(data).buffer;
  36. } catch (err) {
  37. throw new OperationError(err.toString());
  38. }
  39. }
  40. }
  41. export default BSONSerialise;