ToBinaryRuntimeModule.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Alexander Akait @alexander-akait
  4. */
  5. "use strict";
  6. const RuntimeGlobals = require("../RuntimeGlobals");
  7. const RuntimeModule = require("../RuntimeModule");
  8. const Template = require("../Template");
  9. /** @typedef {import("../Compilation")} Compilation */
  10. class ToBinaryRuntimeModule extends RuntimeModule {
  11. constructor() {
  12. super("to binary");
  13. }
  14. /**
  15. * Generates runtime code for this runtime module.
  16. * @returns {string | null} runtime code
  17. */
  18. generate() {
  19. const compilation = /** @type {Compilation} */ (this.compilation);
  20. const fn = RuntimeGlobals.toBinary;
  21. const { runtimeTemplate } = compilation;
  22. // Inspired by esbuild
  23. const isNodePlatform = compilation.compiler.platform.node;
  24. const isWebPlatform = compilation.compiler.platform.web;
  25. const isNeutralPlatform = runtimeTemplate.isNeutralPlatform();
  26. const toImmutableBytes = runtimeTemplate.basicFunction("value", [
  27. runtimeTemplate.destructureObject(["buffer"], "value"),
  28. `${runtimeTemplate.renderConst()} throwErr = ${runtimeTemplate.basicFunction("", ["throw new TypeError('ArrayBuffer is immutable');"])};`,
  29. "Object.defineProperties(buffer, { immutable: { value: true }, resize: { value: throwErr }, transfer: { value: throwErr }, transferToFixedLength: { value: throwErr } });",
  30. "Object.freeze(buffer);",
  31. "return value;"
  32. ]);
  33. return Template.asString([
  34. "// define to binary helper",
  35. `${runtimeTemplate.renderConst()} toImmutableBytes = ${toImmutableBytes}`,
  36. `${fn} = ${isNeutralPlatform ? "typeof Buffer !== 'undefined' ? " : ""}${
  37. isNodePlatform || isNeutralPlatform
  38. ? `${runtimeTemplate.returningFunction("toImmutableBytes(new Uint8Array(Buffer.from(base64, 'base64')))", "base64")}`
  39. : ""
  40. } ${isNeutralPlatform ? ": " : ""}${
  41. isWebPlatform || isNeutralPlatform
  42. ? `(${runtimeTemplate.basicFunction("", [
  43. `${runtimeTemplate.renderConst()} table = new Uint8Array(128);`,
  44. "for (var i = 0; i < 64; i++) table[i < 26 ? i + 65 : i < 52 ? i + 71 : i < 62 ? i - 4 : i * 4 - 205] = i;",
  45. `return ${runtimeTemplate.basicFunction("base64", [
  46. `${runtimeTemplate.renderConst()} n = base64.length, bytes = new Uint8Array((n - (base64[n - 1] == '=') - (base64[n - 2] == '=')) * 3 / 4 | 0);`,
  47. "for (var i = 0, j = 0; i < n;) {",
  48. Template.indent([
  49. `${runtimeTemplate.renderConst()} c0 = table[base64.charCodeAt(i++)], c1 = table[base64.charCodeAt(i++)];`,
  50. `${runtimeTemplate.renderConst()} c2 = table[base64.charCodeAt(i++)], c3 = table[base64.charCodeAt(i++)];`,
  51. "bytes[j++] = (c0 << 2) | (c1 >> 4);",
  52. "bytes[j++] = (c1 << 4) | (c2 >> 2);",
  53. "bytes[j++] = (c2 << 6) | c3;"
  54. ]),
  55. "}",
  56. "return toImmutableBytes(bytes)"
  57. ])}`
  58. ])})();`
  59. : ""
  60. }`
  61. ]);
  62. }
  63. }
  64. module.exports = ToBinaryRuntimeModule;