How to use the brotli.compress function in brotli

To help you get started, we’ve selected a few brotli examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github codemanki / cloudscraper / test / test-brotli.js View on Github external
(zlib.brotliCompressSync ? it.skip : it)('[external] decompress() should produce the expected result', function () {
    const input = helper.getFixture('captcha.html');
    // Try increasing the timeout if this fails on your system.
    const data = require('brotli').compress(Buffer.from(input, 'utf8'));
    const result = brotli.decompress(Buffer.from(data));

    expect(result).to.be.instanceof(Buffer);
    expect(result.toString('utf8')).to.equal(input);
  });
github cb1kenobi / snooplogg / scripts / gen-brightness-lookup.js View on Github external
results[brightness] = [];
			}
			results[brightness].push(r, g, b);
		}
	}
}

console.log('Writing lookup files...');
for (brightness of Object.keys(results)) {
	let colors = results[brightness];
	let before;
	let after;

	do {
		before = Buffer.from(colors);
		after = brotli.compress(before);
		if (!after) {
			// when brightness=255, there's only 1 color (3 bytes) and it's not enough bytes to make brotli happy
			colors = colors.concat(colors.slice(0, 3));
		}
	} while (!after);

	const blen = before.length;
	const alen = after.length;

	totalBefore += blen;
	totalAfter += alen;

	console.log('%s\t%s => %s\t(%s\%)', brightness, blen, alen, Math.round((blen - alen) / blen * 1000) / 10);

	fs.writeFileSync(
		`${outputDir}/${brightness}.br`,
github airtasker / proxay / src / persistence.ts View on Github external
function unserialiseBuffer(persisted: PersistedBuffer): Buffer {
  let buffer;
  switch (persisted.encoding) {
    case "base64":
      buffer = Buffer.from(persisted.data, "base64");
      break;
    case "utf8":
      buffer = Buffer.from(persisted.data, "utf8");
      if (persisted.compression === "br") {
        // TODO: Find a workaround for the new compressed message not necessarily
        // being identical to what was originally sent (update Content-Length?).
        const compressed = brotli.compress(buffer);
        if (compressed) {
          buffer = Buffer.from(compressed);
        } else {
          throw new Error(`Brotli compression failed!`);
        }
      }
      if (persisted.compression === "gzip") {
        // TODO: Find a workaround for the new compressed message not necessarily
        // being identical to what was originally sent (update Content-Length?).
        buffer = gzipSync(buffer);
      }
      break;
    default:
      throw new Error(`Unsupported encoding!`);
  }
  return buffer;
github ionic-team / stencil / test / .scripts / file-size-profile.js View on Github external
function getBuildFileSize(filePath) {
  try {
    const content = fs.readFileSync(filePath);
    let fileName = path.basename(filePath);

    let brotliSize;
    let gzipSize;
    let minifiedSize;

    if (content.length > 0) {
      const brotliResult = brotli.compress(content);
      brotliSize = brotliResult ? brotliResult.length : 0;
      gzipSize = zlib.gzipSync(content, { level: 9 }).length;
      minifiedSize = fs.statSync(filePath).size;
    } else {
      brotliSize = gzipSize = minifiedSize = 0;
    }
    totalBrotli += brotliSize;
    totalGzip += gzipSize;
    totalMinify += minifiedSize;

    return render(fileName, brotliSize, gzipSize, minifiedSize);

  } catch (e) {
    console.error(e);
    return '';
  }
github DevExpress / testcafe-hammerhead / src / processing / encoding / index.ts View on Github external
export async function encodeContent (content: string, encoding: string, charset: Charset) {
    content = charsetEncoder.encode(content, charset.get(), { addBOM: charset.isFromBOM() });

    if (encoding === GZIP_CONTENT_ENCODING)
        return gzip(content);

    if (encoding === DEFLATE_CONTENT_ENCODING)
        return deflate(content);

    if (encoding === BROTLI_CONTENT_ENCODING)
        return Buffer.from(require('brotli').compress(content));

    return content;
}
github airtasker / proxay / src / persistence.spec.ts View on Github external
249,
  104,
  255,
  33,
  203,
  179
]);

const UTF8_REQUEST_BROTLI = Buffer.from(
  brotli.compress(Buffer.from(UTF8_REQUEST, "utf8"))!
);
const UTF8_RESPONSE_BROTLI = Buffer.from(
  brotli.compress(Buffer.from(UTF8_RESPONSE, "utf8"))!
);
const BINARY_REQUEST_BROTLI = Buffer.from(brotli.compress(BINARY_REQUEST)!);
const BINARY_RESPONSE_BROTLI = Buffer.from(brotli.compress(BINARY_RESPONSE)!);

const UTF8_REQUEST_GZIP = gzipSync(Buffer.from(UTF8_REQUEST, "utf8"));
const UTF8_RESPONSE_GZIP = gzipSync(Buffer.from(UTF8_RESPONSE, "utf8"));
const BINARY_REQUEST_GZIP = gzipSync(BINARY_REQUEST);
const BINARY_RESPONSE_GZIP = gzipSync(BINARY_RESPONSE);

describe("Persistence", () => {
  it("persists utf-8", () => {
    expect(
      persistTape({
        request: {
          method: "GET",
          path: "/path",
          headers: {},
          body: Buffer.from(UTF8_REQUEST, "utf8")
        },
github khlieng / dispatch / client / gulpfile.js View on Github external
return through.obj(function(file, enc, callback) {
    if (file.isNull()) {
      return callback(null, file);
    }

    if (file.isStream()) {
      this.emit(
        'error',
        new gutil.PluginError('brotli', 'Streams not supported')
      );
    } else if (file.isBuffer()) {
      file.path += '.br';
      file.contents = Buffer.from(br.compress(file.contents, opts).buffer);
      return callback(null, file);
    }
  });
}
github vuejs / vue-next / scripts / build.js View on Github external
function checkSize(target) {
  const pkgDir = path.resolve(`packages/${target}`)
  const esmProdBuild = `${pkgDir}/dist/${target}.global.prod.js`
  if (fs.existsSync(esmProdBuild)) {
    const file = fs.readFileSync(esmProdBuild)
    const minSize = (file.length / 1024).toFixed(2) + 'kb'
    const gzipped = gzipSync(file)
    const gzippedSize = (gzipped.length / 1024).toFixed(2) + 'kb'
    const compressed = compress(file)
    const compressedSize = (compressed.length / 1024).toFixed(2) + 'kb'
    console.log(
      `${chalk.gray(
        chalk.bold(target)
      )} min:${minSize} / gzip:${gzippedSize} / brotli:${compressedSize}`
    )
  }
}

brotli

A port of the Brotli compression algorithm as used in WOFF2

MIT
Latest version published 2 years ago

Package Health Score

73 / 100
Full package analysis