Skip to content

Commit

Permalink
Brotli compression options and jsc serealizer (#147)
Browse files Browse the repository at this point in the history
Co-authored-by: Ivan Gabaldon <[email protected]>
  • Loading branch information
Mrgaton and inetol authored Aug 26, 2024
1 parent c459316 commit cdf9abd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"@swc/cli": "~0.4.0",
"@swc/core": "~1.7.18",
"@types/bun": "~1.1.8",
"cbor-x": "~1.6.0",
"chalk": "~5.3.0",
"env-var": "~7.5.0",
"hono": "~4.5.9",
Expand Down
21 changes: 17 additions & 4 deletions src/document/compression.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import { type InputType, brotliCompress, brotliDecompress } from 'node:zlib';
import { brotliCompress, brotliDecompress, constants as zlibConstants } from 'node:zlib';
import { errorHandler } from '../server/errorHandler.ts';
import { ErrorCode } from '../types/ErrorHandler.ts';

const compressOptions: { [key: number]: number } = {
[zlibConstants.BROTLI_PARAM_QUALITY]: 11,
[zlibConstants.BROTLI_PARAM_LGWIN]: 28 // 256mb
};

export const compression = {
encode: (data: InputType): Promise<Buffer> => {
encode: (data: ArrayBuffer): Promise<Buffer> => {
return new Promise((resolve, reject) => {
brotliCompress(data, (err, buffer) => {
const isText = Buffer.from(data.slice(0, 1024 * 2)).every(
(byte) => (byte >= 32 && byte <= 126) || byte === 9 || byte === 10 || byte === 13
);

compressOptions[zlibConstants.BROTLI_PARAM_MODE] = isText
? zlibConstants.BROTLI_MODE_TEXT
: zlibConstants.BROTLI_MODE_GENERIC;

brotliCompress(data, compressOptions, (err, buffer) => {
if (err) {
reject(errorHandler.send(ErrorCode.documentCorrupted));
} else {
Expand All @@ -15,7 +28,7 @@ export const compression = {
});
},

decode: (data: InputType): Promise<Buffer> => {
decode: (data: Uint8Array): Promise<Buffer> => {
return new Promise((resolve, reject) => {
brotliDecompress(data, (err, buffer) => {
if (err) {
Expand Down
6 changes: 3 additions & 3 deletions src/document/storage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { decode, encode } from 'cbor-x';
import { deserialize, serialize } from 'bun:jsc';
import { config } from '../server.ts';
import { errorHandler } from '../server/errorHandler.ts';
import type { Document } from '../types/Document.ts';
Expand All @@ -15,10 +15,10 @@ export const storage = {
errorHandler.send(ErrorCode.documentNotFound);
}

return decode(Buffer.from(await file.arrayBuffer()));
return deserialize(Buffer.from(await file.arrayBuffer()));
},

write: async (name: string, document: Document): Promise<void> => {
await Bun.write(config.storagePath + name, encode(document));
await Bun.write(config.storagePath + name, serialize(document));
}
} as const;

0 comments on commit cdf9abd

Please sign in to comment.