utility.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. 'use strict';
  2. const events = require('events');
  3. const is = require('./is');
  4. const sharp = require('../build/Release/sharp.node');
  5. /**
  6. * An Object containing nested boolean values representing the available input and output formats/methods.
  7. * @member
  8. * @example
  9. * console.log(sharp.format);
  10. * @returns {Object}
  11. */
  12. const format = sharp.format();
  13. /**
  14. * An Object containing the version numbers of libvips and its dependencies.
  15. * @member
  16. * @example
  17. * console.log(sharp.versions);
  18. */
  19. let versions = {
  20. vips: sharp.libvipsVersion()
  21. };
  22. try {
  23. versions = require('../vendor/versions.json');
  24. } catch (err) {}
  25. /**
  26. * Gets or, when options are provided, sets the limits of _libvips'_ operation cache.
  27. * Existing entries in the cache will be trimmed after any change in limits.
  28. * This method always returns cache statistics,
  29. * useful for determining how much working memory is required for a particular task.
  30. *
  31. * @example
  32. * const stats = sharp.cache();
  33. * @example
  34. * sharp.cache( { items: 200 } );
  35. * sharp.cache( { files: 0 } );
  36. * sharp.cache(false);
  37. *
  38. * @param {Object|boolean} [options=true] - Object with the following attributes, or boolean where true uses default cache settings and false removes all caching
  39. * @param {number} [options.memory=50] - is the maximum memory in MB to use for this cache
  40. * @param {number} [options.files=20] - is the maximum number of files to hold open
  41. * @param {number} [options.items=100] - is the maximum number of operations to cache
  42. * @returns {Object}
  43. */
  44. function cache (options) {
  45. if (is.bool(options)) {
  46. if (options) {
  47. // Default cache settings of 50MB, 20 files, 100 items
  48. return sharp.cache(50, 20, 100);
  49. } else {
  50. return sharp.cache(0, 0, 0);
  51. }
  52. } else if (is.object(options)) {
  53. return sharp.cache(options.memory, options.files, options.items);
  54. } else {
  55. return sharp.cache();
  56. }
  57. }
  58. cache(true);
  59. /**
  60. * Gets or, when a concurrency is provided, sets
  61. * the number of threads _libvips'_ should create to process each image.
  62. * The default value is the number of CPU cores.
  63. * A value of `0` will reset to this default.
  64. *
  65. * The maximum number of images that can be processed in parallel
  66. * is limited by libuv's `UV_THREADPOOL_SIZE` environment variable.
  67. *
  68. * This method always returns the current concurrency.
  69. *
  70. * @example
  71. * const threads = sharp.concurrency(); // 4
  72. * sharp.concurrency(2); // 2
  73. * sharp.concurrency(0); // 4
  74. *
  75. * @param {number} [concurrency]
  76. * @returns {number} concurrency
  77. */
  78. function concurrency (concurrency) {
  79. return sharp.concurrency(is.integer(concurrency) ? concurrency : null);
  80. }
  81. /**
  82. * An EventEmitter that emits a `change` event when a task is either:
  83. * - queued, waiting for _libuv_ to provide a worker thread
  84. * - complete
  85. * @member
  86. * @example
  87. * sharp.queue.on('change', function(queueLength) {
  88. * console.log('Queue contains ' + queueLength + ' task(s)');
  89. * });
  90. */
  91. const queue = new events.EventEmitter();
  92. /**
  93. * Provides access to internal task counters.
  94. * - queue is the number of tasks this module has queued waiting for _libuv_ to provide a worker thread from its pool.
  95. * - process is the number of resize tasks currently being processed.
  96. *
  97. * @example
  98. * const counters = sharp.counters(); // { queue: 2, process: 4 }
  99. *
  100. * @returns {Object}
  101. */
  102. function counters () {
  103. return sharp.counters();
  104. }
  105. /**
  106. * Get and set use of SIMD vector unit instructions.
  107. * Requires libvips to have been compiled with liborc support.
  108. *
  109. * Improves the performance of `resize`, `blur` and `sharpen` operations
  110. * by taking advantage of the SIMD vector unit of the CPU, e.g. Intel SSE and ARM NEON.
  111. *
  112. * @example
  113. * const simd = sharp.simd();
  114. * // simd is `true` if the runtime use of liborc is currently enabled
  115. * @example
  116. * const simd = sharp.simd(false);
  117. * // prevent libvips from using liborc at runtime
  118. *
  119. * @param {boolean} [simd=true]
  120. * @returns {boolean}
  121. */
  122. function simd (simd) {
  123. return sharp.simd(is.bool(simd) ? simd : null);
  124. }
  125. simd(true);
  126. /**
  127. * Decorate the Sharp class with utility-related functions.
  128. * @private
  129. */
  130. module.exports = function (Sharp) {
  131. [
  132. cache,
  133. concurrency,
  134. counters,
  135. simd
  136. ].forEach(function (f) {
  137. Sharp[f.name] = f;
  138. });
  139. Sharp.format = format;
  140. Sharp.versions = versions;
  141. Sharp.queue = queue;
  142. };