From 6a674cf94d82a794103d5d6b4ffb7967e3082a83 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 12 Oct 2022 11:23:08 +0100 Subject: [PATCH 1/3] fix: only import reader/writer to decrease bundle size We only use the reader/writer classes from protobuf.js so only import those - drops the size of `index.min.js` by about 30kb(!). --- packages/protons-runtime/src/decode.ts | 2 +- packages/protons-runtime/src/encode.ts | 2 +- packages/protons-runtime/src/index.ts | 3 +- packages/protons-runtime/src/reader.ts | 22 --------- packages/protons-runtime/src/utils.ts | 65 ++++++++++++++++++++++++++ packages/protons-runtime/src/writer.ts | 22 --------- 6 files changed, 68 insertions(+), 48 deletions(-) delete mode 100644 packages/protons-runtime/src/reader.ts create mode 100644 packages/protons-runtime/src/utils.ts delete mode 100644 packages/protons-runtime/src/writer.ts diff --git a/packages/protons-runtime/src/decode.ts b/packages/protons-runtime/src/decode.ts index f1bddfd..3d9356b 100644 --- a/packages/protons-runtime/src/decode.ts +++ b/packages/protons-runtime/src/decode.ts @@ -1,6 +1,6 @@ import type { Uint8ArrayList } from 'uint8arraylist' import type { Codec } from './codec.js' -import { reader } from './reader.js' +import { reader } from './utils.js' export function decodeMessage (buf: Uint8Array | Uint8ArrayList, codec: Codec): T { const r = reader(buf instanceof Uint8Array ? buf : buf.subarray()) diff --git a/packages/protons-runtime/src/encode.ts b/packages/protons-runtime/src/encode.ts index 61c4166..7a8b8b5 100644 --- a/packages/protons-runtime/src/encode.ts +++ b/packages/protons-runtime/src/encode.ts @@ -1,5 +1,5 @@ import type { Codec } from './codec.js' -import { writer } from './writer.js' +import { writer } from './utils.js' export function encodeMessage (message: T, codec: Codec): Uint8Array { const w = writer() diff --git a/packages/protons-runtime/src/index.ts b/packages/protons-runtime/src/index.ts index 44860e8..64155c8 100644 --- a/packages/protons-runtime/src/index.ts +++ b/packages/protons-runtime/src/index.ts @@ -18,8 +18,7 @@ export { export { enumeration } from './codecs/enum.js' export { message } from './codecs/message.js' -export { reader } from './reader.js' -export { writer } from './writer.js' +export { reader, writer } from './utils.js' export type { Codec, EncodeOptions } from './codec.js' export interface Writer { diff --git a/packages/protons-runtime/src/reader.ts b/packages/protons-runtime/src/reader.ts deleted file mode 100644 index ebf8857..0000000 --- a/packages/protons-runtime/src/reader.ts +++ /dev/null @@ -1,22 +0,0 @@ -import pb from 'protobufjs/minimal.js' -import type { Reader } from './index.js' - -const PBReader = pb.Reader - -// monkey patch the reader to add native bigint support -const methods = [ - 'uint64', 'int64', 'sint64', 'fixed64', 'sfixed64' -] -methods.forEach(method => { - // @ts-expect-error - const original = PBReader.prototype[method] - // @ts-expect-error - PBReader.prototype[method] = function (): bigint { - return BigInt(original.call(this).toString()) - } -}) - -export function reader (buf: Uint8Array): Reader { - // @ts-expect-error class is monkey patched - return PBReader.create(buf) -} diff --git a/packages/protons-runtime/src/utils.ts b/packages/protons-runtime/src/utils.ts new file mode 100644 index 0000000..d1dc60f --- /dev/null +++ b/packages/protons-runtime/src/utils.ts @@ -0,0 +1,65 @@ +// @ts-expect-error no types +import ReaderClass from 'protobufjs/src/reader.js' +// @ts-expect-error no types +import ReaderBufferClass from 'protobufjs/src/reader_buffer.js' +// @ts-expect-error no types +import WriterClass from 'protobufjs/src/writer.js' +// @ts-expect-error no types +import WriterBufferClass from 'protobufjs/src/writer_buffer.js' +// @ts-expect-error no types +import util from 'protobufjs/src/util/minimal.js' +import type { Reader, Writer } from './index.js' + +function configure() { + util._configure() + ReaderClass._configure(ReaderBufferClass) + WriterClass._configure(WriterBufferClass) +} + +// Set up buffer utility according to the environment +configure() + +// monkey patch the reader to add native bigint support +const methods = [ + 'uint64', 'int64', 'sint64', 'fixed64', 'sfixed64' +] + +function patchReader (obj: any): any { + for (const method of methods) { + if (obj[method] == null) { + console.info('no', method) + continue + } + + const original = obj[method] + obj[method] = function (): bigint { + return BigInt(original.call(this).toString()) + } + } + + return obj +} + +export function reader (buf: Uint8Array): Reader { + return patchReader(new ReaderClass(buf)) +} + +function patchWriter (obj: any): any { + for (const method of methods) { + if (obj[method] == null) { + console.info('no', method) + continue + } + + const original = obj[method] + obj[method] = function (val: bigint) { + return original.call(this, val.toString()) + } + } + + return obj +} + +export function writer (): Writer { + return patchWriter(WriterClass.create()) +} \ No newline at end of file diff --git a/packages/protons-runtime/src/writer.ts b/packages/protons-runtime/src/writer.ts deleted file mode 100644 index d4ac8e9..0000000 --- a/packages/protons-runtime/src/writer.ts +++ /dev/null @@ -1,22 +0,0 @@ -import pb from 'protobufjs/minimal.js' -import type { Writer } from './index.js' - -const PBWriter = pb.Writer - -// monkey patch the writer to add native bigint support -const methods = [ - 'uint64', 'int64', 'sint64', 'fixed64', 'sfixed64' -] -methods.forEach(method => { - // @ts-expect-error - const original = PBWriter.prototype[method] - // @ts-expect-error - PBWriter.prototype[method] = function (val: bigint): pb.Writer { - return original.call(this, val.toString()) - } -}) - -export function writer (): Writer { - // @ts-expect-error class is monkey patched - return PBWriter.create() -} From 531e4e8ca6ab37a94c0131441e4a5e1f9cd73806 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 12 Oct 2022 11:24:41 +0100 Subject: [PATCH 2/3] chore: whitespace --- packages/protons-runtime/src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/protons-runtime/src/utils.ts b/packages/protons-runtime/src/utils.ts index d1dc60f..946dc55 100644 --- a/packages/protons-runtime/src/utils.ts +++ b/packages/protons-runtime/src/utils.ts @@ -62,4 +62,4 @@ function patchWriter (obj: any): any { export function writer (): Writer { return patchWriter(WriterClass.create()) -} \ No newline at end of file +} From 5387d149e79feaa5251deafbf08ed1a50f7a60c6 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 12 Oct 2022 11:26:01 +0100 Subject: [PATCH 3/3] chore: linting --- packages/protons-runtime/src/utils.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/protons-runtime/src/utils.ts b/packages/protons-runtime/src/utils.ts index 946dc55..cce23c9 100644 --- a/packages/protons-runtime/src/utils.ts +++ b/packages/protons-runtime/src/utils.ts @@ -10,7 +10,7 @@ import WriterBufferClass from 'protobufjs/src/writer_buffer.js' import util from 'protobufjs/src/util/minimal.js' import type { Reader, Writer } from './index.js' -function configure() { +function configure () { util._configure() ReaderClass._configure(ReaderBufferClass) WriterClass._configure(WriterBufferClass) @@ -27,7 +27,6 @@ const methods = [ function patchReader (obj: any): any { for (const method of methods) { if (obj[method] == null) { - console.info('no', method) continue } @@ -47,7 +46,6 @@ export function reader (buf: Uint8Array): Reader { function patchWriter (obj: any): any { for (const method of methods) { if (obj[method] == null) { - console.info('no', method) continue }