UNPKG

blake3

Version:

BLAKE3 hashing for JavaScript: native Node bindings (where available) and WebAssembly

175 lines (174 loc) 8.68 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const webpack_1 = __importDefault(require("webpack")); const path_1 = require("path"); const fs_1 = require("fs"); const serve_handler_1 = __importDefault(require("serve-handler")); const puppeteer_1 = __importDefault(require("puppeteer")); const http_1 = require("http"); const test_helpers_1 = require("./base/test-helpers"); const os_1 = require("os"); const chai_1 = require("chai"); // Much of the browser code is also used in Node's wasm. We test things more // thoroughly there because tests are easier to write and debug, these tests // are primarily for sanity and checking browser-specific behavior. describe('browser', () => { const testDir = path_1.resolve(os_1.tmpdir(), 'blake3-browser-test'); let server; let page; /** * Builds the browser lib into the testDir. */ function buildWebpack() { return __awaiter(this, void 0, void 0, function* () { try { fs_1.mkdirSync(testDir); } catch (_a) { // already exists, probably } fs_1.writeFileSync(path_1.resolve(testDir, 'entry-src.js'), `import("blake3/browser").then(b3 => window.blake3 = b3);`); const stats = yield new Promise((res, rej) => webpack_1.default({ mode: 'development', entry: path_1.resolve(testDir, 'entry-src.js'), output: { path: testDir, filename: 'main.js', }, resolve: { alias: { 'blake3/browser': path_1.resolve(__dirname, '../', 'browser.js'), }, }, }, (err, stats) => (err ? rej(err) : res(stats)))); if (stats.hasErrors()) { throw stats.toString('errors-only'); } fs_1.writeFileSync(path_1.resolve(testDir, 'index.html'), ` <script src="/main.js"></script> <script>window.inputs = ${JSON.stringify(test_helpers_1.inputs)}</script> `); }); } function serve() { return __awaiter(this, void 0, void 0, function* () { server = http_1.createServer((req, res) => serve_handler_1.default(req, res, { public: testDir })); yield new Promise(resolve => server.listen(0, resolve)); }); } before(function () { return __awaiter(this, void 0, void 0, function* () { yield buildWebpack(); yield serve(); this.timeout(20 * 1000); const { port } = server.address(); const browser = yield puppeteer_1.default.launch(); page = yield browser.newPage(); yield page.goto(`http://localhost:${port}`); yield page.waitForFunction('!!window.blake3'); }); }); it('hashes a string', () => __awaiter(void 0, void 0, void 0, function* () { const result = yield page.evaluate('blake3.hash(inputs.large.input).toString("hex")'); chai_1.expect(result).to.equal(test_helpers_1.inputs.large.hash.toString('hex')); })); describe('input encoding', () => { it('hashes a uint8array', () => __awaiter(void 0, void 0, void 0, function* () { const contents = [...new Uint8Array(Buffer.from(test_helpers_1.inputs.hello.input))]; const result = yield page.evaluate(`blake3.hash(new Uint8Array([${contents.join(',')}])).toString("hex")`); chai_1.expect(result).to.equal(test_helpers_1.inputs.hello.hash.toString('hex')); })); it('hashes a string', () => __awaiter(void 0, void 0, void 0, function* () { const result = yield page.evaluate('blake3.hash(inputs.large.input).toString("hex")'); chai_1.expect(result).to.equal(test_helpers_1.inputs.large.hash.toString('hex')); })); it('customizes output length', () => __awaiter(void 0, void 0, void 0, function* () { const result = yield page.evaluate('blake3.hash(inputs.hello.input, { length: 16 }).toString("hex")'); chai_1.expect(result).to.equal(test_helpers_1.inputs.hello.hash.slice(0, 16).toString('hex')); })); }); describe('output encoding', () => { const tcases = [ { encoding: 'hex', expected: test_helpers_1.inputs.hello.hash.toString('hex') }, { encoding: 'base64', expected: test_helpers_1.inputs.hello.hash.toString('base64') }, { encoding: 'utf8', expected: test_helpers_1.inputs.hello.hash.toString('utf8') }, ]; tcases.forEach(({ encoding, expected }) => it(encoding, () => __awaiter(void 0, void 0, void 0, function* () { const result = yield page.evaluate(`blake3.hash(inputs.hello.input).toString("${encoding}")`); chai_1.expect(result).to.deep.equal(expected); }))); it('raw', () => __awaiter(void 0, void 0, void 0, function* () { const result = (yield page.evaluate(`blake3.hash(inputs.hello.input)`)); const actual = Buffer.alloc(32); for (let i = 0; i < actual.length; i++) { actual[i] = result[i]; // it comes as a plain object, we need to convert it to a buffer } chai_1.expect(actual).to.deep.equal(test_helpers_1.inputs.hello.hash); })); }); describe('hash class', () => { it('digests', () => __awaiter(void 0, void 0, void 0, function* () { const result = yield page.evaluate(`(() => { const hash = blake3.createHash(); ${[...Buffer.from(test_helpers_1.inputs.hello.input)] .map(byte => `hash.update(new Uint8Array([${byte}]));`) .join('\n')} return hash.digest('hex'); })()`); chai_1.expect(result).to.equal(test_helpers_1.inputs.hello.hash.toString('hex')); })); it('customizes the output length', () => __awaiter(void 0, void 0, void 0, function* () { const result = yield page.evaluate(`(() => { const hash = blake3.createHash(); hash.update(${JSON.stringify(test_helpers_1.inputs.hello.input)}); return hash.digest('hex', { length: 16 }); })()`); chai_1.expect(result).to.equal(test_helpers_1.inputs.hello.hash.slice(0, 16).toString('hex')); })); it('returns a hash instance from digest', () => __awaiter(void 0, void 0, void 0, function* () { const result = yield page.evaluate(`(() => { const hash = blake3.createHash(); ${[...Buffer.from(test_helpers_1.inputs.hello.input)] .map(byte => `hash.update(new Uint8Array([${byte}]));`) .join('\n')} return hash.digest('hex'); })()`); chai_1.expect(result).to.equal(test_helpers_1.inputs.hello.hash.toString('hex')); })); }); describe('reader', () => { it('is sane with a Hash', () => __awaiter(void 0, void 0, void 0, function* () { const result = yield page.evaluate(`(() => { const hash = blake3.createHash(); hash.update("hello"); return blake3.using(hash.reader(), reader => [ reader.read(48).toString('hex'), reader.toArray().toString('hex'), reader.toString('hex'), ]); })()`); chai_1.expect(result).to.deep.equal([ test_helpers_1.hello48.toString('hex'), test_helpers_1.inputs.hello.hash.toString('hex'), test_helpers_1.inputs.hello.hash.toString('hex'), ]); })); }); after(() => { page === null || page === void 0 ? void 0 : page.browser().close(); server === null || server === void 0 ? void 0 : server.close(); }); }); //# sourceMappingURL=browser.test.js.map