diff --git a/bun.lockb b/bun.lockb index 17193c9..0c157ea 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 862f108..a6e23ba 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/document/compression.ts b/src/document/compression.ts index eac7373..c56e6d6 100644 --- a/src/document/compression.ts +++ b/src/document/compression.ts @@ -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 => { + encode: (data: ArrayBuffer): Promise => { 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 { @@ -15,7 +28,7 @@ export const compression = { }); }, - decode: (data: InputType): Promise => { + decode: (data: Uint8Array): Promise => { return new Promise((resolve, reject) => { brotliDecompress(data, (err, buffer) => { if (err) { diff --git a/src/document/storage.ts b/src/document/storage.ts index 41b4946..0067560 100644 --- a/src/document/storage.ts +++ b/src/document/storage.ts @@ -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'; @@ -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 => { - await Bun.write(config.storagePath + name, encode(document)); + await Bun.write(config.storagePath + name, serialize(document)); } } as const;