impl.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.decodeBuffer = decodeBuffer;
  6. exports.rotate = rotate;
  7. exports.resize = resize;
  8. exports.encodeJpeg = encodeJpeg;
  9. exports.encodeWebp = encodeWebp;
  10. exports.encodeAvif = encodeAvif;
  11. exports.encodePng = encodePng;
  12. var _semver = _interopRequireDefault(require("next/dist/compiled/semver"));
  13. var _codecs = require("./codecs");
  14. var _imageData = _interopRequireDefault(require("./image_data"));
  15. function _interopRequireDefault(obj) {
  16. return obj && obj.__esModule ? obj : {
  17. default: obj
  18. };
  19. }
  20. // Fixed in Node.js 16.5.0 and newer.
  21. // See https://github.com/nodejs/node/pull/39337
  22. // Eventually, remove this delay when engines is updated.
  23. // See https://github.com/vercel/next.js/blob/1bcc923439f495a1717421e06af7e64c6003072c/packages/next/package.json#L249-L251
  24. const FIXED_VERSION = "16.5.0";
  25. const DELAY_MS = 1000;
  26. let _promise;
  27. function delayOnce(ms) {
  28. if (!_promise) {
  29. _promise = new Promise((resolve)=>{
  30. setTimeout(resolve, ms);
  31. });
  32. }
  33. return _promise;
  34. }
  35. function maybeDelay() {
  36. const isAppleM1 = process.arch === "arm64" && process.platform === "darwin";
  37. if (isAppleM1 && _semver.default.lt(process.version, FIXED_VERSION)) {
  38. return delayOnce(DELAY_MS);
  39. }
  40. return Promise.resolve();
  41. }
  42. async function decodeBuffer(_buffer) {
  43. var ref;
  44. const buffer = Buffer.from(_buffer);
  45. const firstChunk = buffer.slice(0, 16);
  46. const firstChunkString = Array.from(firstChunk).map((v)=>String.fromCodePoint(v)).join("");
  47. const key = (ref = Object.entries(_codecs.codecs).find(([, { detectors }])=>detectors.some((detector)=>detector.exec(firstChunkString)))) == null ? void 0 : ref[0];
  48. if (!key) {
  49. throw Error(`Buffer has an unsupported format`);
  50. }
  51. const encoder = _codecs.codecs[key];
  52. const mod = await encoder.dec();
  53. const rgba = mod.decode(new Uint8Array(buffer));
  54. return rgba;
  55. }
  56. async function rotate(image, numRotations) {
  57. image = _imageData.default.from(image);
  58. const m = await _codecs.preprocessors["rotate"].instantiate();
  59. return await m(image.data, image.width, image.height, {
  60. numRotations
  61. });
  62. }
  63. async function resize({ image , width , height }) {
  64. image = _imageData.default.from(image);
  65. const p = _codecs.preprocessors["resize"];
  66. const m = await p.instantiate();
  67. await maybeDelay();
  68. return await m(image.data, image.width, image.height, {
  69. ...p.defaultOptions,
  70. width,
  71. height
  72. });
  73. }
  74. async function encodeJpeg(image, { quality }) {
  75. image = _imageData.default.from(image);
  76. const e = _codecs.codecs["mozjpeg"];
  77. const m = await e.enc();
  78. await maybeDelay();
  79. const r = await m.encode(image.data, image.width, image.height, {
  80. ...e.defaultEncoderOptions,
  81. quality
  82. });
  83. return Buffer.from(r);
  84. }
  85. async function encodeWebp(image, { quality }) {
  86. image = _imageData.default.from(image);
  87. const e = _codecs.codecs["webp"];
  88. const m = await e.enc();
  89. await maybeDelay();
  90. const r = await m.encode(image.data, image.width, image.height, {
  91. ...e.defaultEncoderOptions,
  92. quality
  93. });
  94. return Buffer.from(r);
  95. }
  96. async function encodeAvif(image, { quality }) {
  97. image = _imageData.default.from(image);
  98. const e = _codecs.codecs["avif"];
  99. const m = await e.enc();
  100. await maybeDelay();
  101. const val = e.autoOptimize.min || 62;
  102. const r = await m.encode(image.data, image.width, image.height, {
  103. ...e.defaultEncoderOptions,
  104. // Think of cqLevel as the "amount" of quantization (0 to 62),
  105. // so a lower value yields higher quality (0 to 100).
  106. cqLevel: Math.round(val - quality / 100 * val)
  107. });
  108. return Buffer.from(r);
  109. }
  110. async function encodePng(image) {
  111. image = _imageData.default.from(image);
  112. const e = _codecs.codecs["oxipng"];
  113. const m = await e.enc();
  114. await maybeDelay();
  115. const r = await m.encode(image.data, image.width, image.height, {
  116. ...e.defaultEncoderOptions
  117. });
  118. return Buffer.from(r);
  119. }
  120. //# sourceMappingURL=impl.js.map