Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Brotli compression options and jsc serealizer #147

Merged
merged 5 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;