main.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getMetadata = getMetadata;
  6. exports.processBuffer = processBuffer;
  7. exports.decodeBuffer = decodeBuffer;
  8. var _jestWorker = require("next/dist/compiled/jest-worker");
  9. var path = _interopRequireWildcard(require("path"));
  10. var _utils = require("../../../shared/lib/utils");
  11. var _os = require("os");
  12. function _getRequireWildcardCache() {
  13. if (typeof WeakMap !== "function") return null;
  14. var cache = new WeakMap();
  15. _getRequireWildcardCache = function() {
  16. return cache;
  17. };
  18. return cache;
  19. }
  20. function _interopRequireWildcard(obj) {
  21. if (obj && obj.__esModule) {
  22. return obj;
  23. }
  24. if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
  25. return {
  26. default: obj
  27. };
  28. }
  29. var cache = _getRequireWildcardCache();
  30. if (cache && cache.has(obj)) {
  31. return cache.get(obj);
  32. }
  33. var newObj = {};
  34. var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
  35. for(var key in obj){
  36. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  37. var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
  38. if (desc && (desc.get || desc.set)) {
  39. Object.defineProperty(newObj, key, desc);
  40. } else {
  41. newObj[key] = obj[key];
  42. }
  43. }
  44. }
  45. newObj.default = obj;
  46. if (cache) {
  47. cache.set(obj, newObj);
  48. }
  49. return newObj;
  50. }
  51. const getWorker = (0, _utils).execOnce(()=>new _jestWorker.Worker(path.resolve(__dirname, "impl"), {
  52. enableWorkerThreads: true,
  53. // There will be at most 6 workers needed since each worker will take
  54. // at least 1 operation type.
  55. numWorkers: Math.max(1, Math.min((0, _os).cpus().length - 1, 6)),
  56. computeWorkerKey: (method)=>method
  57. }));
  58. async function getMetadata(buffer) {
  59. const worker = getWorker();
  60. const { width , height } = await worker.decodeBuffer(buffer);
  61. return {
  62. width,
  63. height
  64. };
  65. }
  66. async function processBuffer(buffer, operations, encoding, quality) {
  67. const worker = getWorker();
  68. let imageData = await worker.decodeBuffer(buffer);
  69. for (const operation of operations){
  70. if (operation.type === "rotate") {
  71. imageData = await worker.rotate(imageData, operation.numRotations);
  72. } else if (operation.type === "resize") {
  73. const opt = {
  74. image: imageData,
  75. width: 0,
  76. height: 0
  77. };
  78. if (operation.width && imageData.width && imageData.width > operation.width) {
  79. opt.width = operation.width;
  80. }
  81. if (operation.height && imageData.height && imageData.height > operation.height) {
  82. opt.height = operation.height;
  83. }
  84. if (opt.width > 0 || opt.height > 0) {
  85. imageData = await worker.resize(opt);
  86. }
  87. }
  88. }
  89. switch(encoding){
  90. case "jpeg":
  91. return Buffer.from(await worker.encodeJpeg(imageData, {
  92. quality
  93. }));
  94. case "webp":
  95. return Buffer.from(await worker.encodeWebp(imageData, {
  96. quality
  97. }));
  98. case "avif":
  99. const avifQuality = quality - 20;
  100. return Buffer.from(await worker.encodeAvif(imageData, {
  101. quality: Math.max(avifQuality, 0)
  102. }));
  103. case "png":
  104. return Buffer.from(await worker.encodePng(imageData));
  105. default:
  106. throw Error(`Unsupported encoding format`);
  107. }
  108. }
  109. async function decodeBuffer(buffer) {
  110. const worker = getWorker();
  111. const imageData = await worker.decodeBuffer(buffer);
  112. return imageData;
  113. }
  114. //# sourceMappingURL=main.js.map