|
| 1 | +import * as proto from 'protobufjs'; |
| 2 | +import { pack as msgpackrPack } from 'msgpackr'; |
| 3 | +import * as brotli from 'brotli-unicode'; |
| 4 | +import * as fflate from 'fflate'; |
| 5 | +import * as fs from 'fs'; |
| 6 | +import { BaseDatabasePopulator } from './base-decompressors-populator'; |
| 7 | + |
| 8 | +const COMPRESSED_PREFIX = 'Comp'; |
| 9 | +const BROTLI_PREFIX = 'BROTLI'; |
| 10 | + |
| 11 | +export class BrotliDatabasePopulator extends BaseDatabasePopulator { |
| 12 | + |
| 13 | + /** |
| 14 | + * Create keys with all types of Bolti compression |
| 15 | + */ |
| 16 | + protected async createCompressedKeys(): Promise<void> { |
| 17 | + await this.createBrotliUnicodeKeys(); |
| 18 | + await this.createBrotliASCIIKeys(); |
| 19 | + await this.createBrotliVectorKeys(); |
| 20 | + await this.createBrotliJSONKeys(); |
| 21 | + await this.createBrotliPHPUnserializedJSONKeys(); |
| 22 | + await this.createBrotliJavaSerializedObjectKeys(); |
| 23 | + await this.createBrotliMsgpackKeys(); |
| 24 | + await this.createBrotliProtobufKeys(); |
| 25 | + await this.createBrotliPickleKeys(); |
| 26 | + } |
| 27 | + |
| 28 | + private async createBrotliUnicodeKeys() { |
| 29 | + const encoder = new TextEncoder(); |
| 30 | + const prefix = `${COMPRESSED_PREFIX}:${BROTLI_PREFIX}:Unicode`; |
| 31 | + const rawValue = '漢字'; |
| 32 | + const buf = encoder.encode(rawValue); |
| 33 | + const value = Buffer.from(await brotli.compress(buf)); |
| 34 | + await this.createString(prefix, value); |
| 35 | + await this.createHash(prefix, [value]); |
| 36 | + } |
| 37 | + |
| 38 | + private async createBrotliASCIIKeys() { |
| 39 | + const prefix = `${COMPRESSED_PREFIX}:${BROTLI_PREFIX}:ASCII`; |
| 40 | + const rawValue = '\xac\xed\x00\x05t\x0a4102'; |
| 41 | + const buf = fflate.strToU8(rawValue); |
| 42 | + const value = Buffer.from(await brotli.compress(buf)); |
| 43 | + await this.createString(prefix, value); |
| 44 | + await this.createHash(prefix, [value]); |
| 45 | + } |
| 46 | + |
| 47 | + private async createBrotliVectorKeys() { |
| 48 | + const prefix = `${COMPRESSED_PREFIX}:${BROTLI_PREFIX}:Vector`; |
| 49 | + const rawValue = JSON.parse(fs.readFileSync('./test-data/decompressors/vector.json', 'utf8')); |
| 50 | + const buf = fflate.strToU8(rawValue); |
| 51 | + const value = Buffer.from(await brotli.compress(buf)); |
| 52 | + await this.createString(prefix, value); |
| 53 | + await this.createHash(prefix, [value]); |
| 54 | + } |
| 55 | + |
| 56 | + private async createBrotliJSONKeys() { |
| 57 | + const prefix = `${COMPRESSED_PREFIX}:${BROTLI_PREFIX}:JSON`; |
| 58 | + const rawValue = '{"test":"test"}'; |
| 59 | + const buf = fflate.strToU8(rawValue); |
| 60 | + const value = Buffer.from(await brotli.compress(buf)); |
| 61 | + await this.createString(prefix, value); |
| 62 | + await this.createHash(prefix, [value]); |
| 63 | + } |
| 64 | + |
| 65 | + private async createBrotliPHPUnserializedJSONKeys() { |
| 66 | + const prefix = `${COMPRESSED_PREFIX}:${BROTLI_PREFIX}:PHP`; |
| 67 | + const rawValue = 'a:2:{i:0;s:12:"Sample array";i:1;a:2:{i:0;s:5:"Apple";i:1;s:6:"Orange";}}'; |
| 68 | + const buf = fflate.strToU8(rawValue); |
| 69 | + const value = Buffer.from(await brotli.compress(buf)); |
| 70 | + await this.createString(prefix, value); |
| 71 | + await this.createHash(prefix, [value]); |
| 72 | + } |
| 73 | + |
| 74 | + private async createBrotliPickleKeys() { |
| 75 | + const prefix = `${COMPRESSED_PREFIX}:${BROTLI_PREFIX}:Pickle`; |
| 76 | + const rawValue = fs.readFileSync('./test-data/decompressors/pickleFile1.pickle'); |
| 77 | + const value = Buffer.from(await brotli.compress(rawValue)); |
| 78 | + await this.createString(prefix, value); |
| 79 | + await this.createHash(prefix, [value]); |
| 80 | + }; |
| 81 | + |
| 82 | + private async createBrotliJavaSerializedObjectKeys() { |
| 83 | + const prefix = `${COMPRESSED_PREFIX}:${BROTLI_PREFIX}:Java`; |
| 84 | + const rawValue = fs.readFileSync('./test-data/decompressors/test_serialised_obj.ser'); |
| 85 | + const rawValue2 = fs.readFileSync('./test-data/decompressors/test_annotated_obj.ser'); |
| 86 | + |
| 87 | + const value = Buffer.from(await brotli.compress(rawValue)); |
| 88 | + const value2 = Buffer.from(await brotli.compress(rawValue2)); |
| 89 | + |
| 90 | + await this.createString(prefix, value); |
| 91 | + await this.createString(prefix, value2); |
| 92 | + await this.createHash(prefix, [value,value2]); |
| 93 | + } |
| 94 | + |
| 95 | + private async createBrotliMsgpackKeys(): Promise<void> { |
| 96 | + const prefix = `${COMPRESSED_PREFIX}:${BROTLI_PREFIX}:Msgpack`; |
| 97 | + const rawValue = msgpackrPack({ |
| 98 | + hello: 'World', |
| 99 | + array: [1, 2], |
| 100 | + obj: {test: 'test'}, |
| 101 | + boolean: false, |
| 102 | + }); |
| 103 | + const value = Buffer.from(await brotli.compress(rawValue)); |
| 104 | + await this.createString(prefix, value); |
| 105 | + await this.createHash(prefix, [value]); |
| 106 | + } |
| 107 | + |
| 108 | + private createBrotliProtobufKeys(): Promise<void> { |
| 109 | + return new Promise((resolve, reject) => { |
| 110 | + const prefix = `${COMPRESSED_PREFIX}:${BROTLI_PREFIX}:Proto`; |
| 111 | + proto.load('./test-data/decompressors/awesome.proto', async (err, root) => { |
| 112 | + if (err || !root) { |
| 113 | + console.error('Error loading protobuf:', err); |
| 114 | + return reject(err); |
| 115 | + } |
| 116 | + |
| 117 | + try { |
| 118 | + const Book = root.lookupType('com.book.BookStore'); |
| 119 | + const payload = {name: 'Test name', books: {0: 'book 1', 1: 'book 2'}}; |
| 120 | + const message = Book.create(payload); |
| 121 | + const rawValue = Book.encode(message).finish(); |
| 122 | + |
| 123 | + const value = Buffer.from(await brotli.compress(rawValue)); |
| 124 | + await this.createString(prefix, value); |
| 125 | + await this.createHash(prefix, [value]); |
| 126 | + resolve(); |
| 127 | + } catch (error) { |
| 128 | + reject(error); |
| 129 | + } |
| 130 | + }); |
| 131 | + }); |
| 132 | + } |
| 133 | +} |
| 134 | + |
0 commit comments