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

fix: only import reader/writer to decrease bundle size #69

Merged
merged 3 commits into from
Oct 12, 2022
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
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.