Skip to content

Commit

Permalink
fix: only import reader/writer to decrease bundle size (#69)
Browse files Browse the repository at this point in the history
We only use the reader/writer classes from protobuf.js so only import those - drops the size of index.min.js in protons-runtime by about 30kb(!).
  • Loading branch information
achingbrain authored Oct 12, 2022
1 parent 559191d commit 8eea129
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 48 deletions.
2 changes: 1 addition & 1 deletion packages/protons-runtime/src/decode.ts
Original file line number Diff line number Diff line change
@@ -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 <T> (buf: Uint8Array | Uint8ArrayList, codec: Codec<T>): T {
const r = reader(buf instanceof Uint8Array ? buf : buf.subarray())
Expand Down
2 changes: 1 addition & 1 deletion packages/protons-runtime/src/encode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Codec } from './codec.js'
import { writer } from './writer.js'
import { writer } from './utils.js'

export function encodeMessage <T> (message: T, codec: Codec<T>): Uint8Array {
const w = writer()
Expand Down
3 changes: 1 addition & 2 deletions packages/protons-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 0 additions & 22 deletions packages/protons-runtime/src/reader.ts

This file was deleted.

63 changes: 63 additions & 0 deletions packages/protons-runtime/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// @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) {
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) {
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())
}
22 changes: 0 additions & 22 deletions packages/protons-runtime/src/writer.ts

This file was deleted.

0 comments on commit 8eea129

Please sign in to comment.