Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

44 строки
1.2 KiB

  1. var brotli = require('./build/encode');
  2. /**
  3. * Compresses the given buffer
  4. * The second parameter is optional and specifies whether the buffer is
  5. * text or binary data (the default is binary).
  6. * Returns null on error
  7. */
  8. module.exports = function(buffer, opts) {
  9. // default to binary data
  10. var quality = 11;
  11. var mode = 0;
  12. var lgwin = 22;
  13. if (typeof opts === 'boolean') {
  14. mode = opts ? 0 : 1;
  15. } else if (typeof opts === 'object') {
  16. quality = opts.quality || 11;
  17. mode = opts.mode || 0;
  18. lgwin = opts.lgwin || 22;
  19. }
  20. // allocate input buffer and copy data to it
  21. var buf = brotli._malloc(buffer.length);
  22. brotli.HEAPU8.set(buffer, buf);
  23. // allocate output buffer (same size + some padding to be sure it fits), and encode
  24. var outBuf = brotli._malloc(buffer.length + 1024);
  25. var encodedSize = brotli._encode(quality, lgwin, mode, buffer.length, buf, buffer.length, outBuf);
  26. var outBuffer = null;
  27. if (encodedSize !== -1) {
  28. // allocate and copy data to an output buffer
  29. outBuffer = new Uint8Array(encodedSize);
  30. outBuffer.set(brotli.HEAPU8.subarray(outBuf, outBuf + encodedSize));
  31. }
  32. // free malloc'd buffers
  33. brotli._free(buf);
  34. brotli._free(outBuf);
  35. return outBuffer;
  36. };