-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: only import reader/writer to decrease bundle size (#69)
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
1 parent
559191d
commit 8eea129
Showing
6 changed files
with
66 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file was deleted.
Oops, something went wrong.