-
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.
feat: runtime size limits for arrays and maps (#128)
It's possible to limit the size of arrays and maps at compile time: ```protobuf message MyMessage { repeated uint32 repeatedField = 1 [(protons.options).limit = 10]; map<string, string> stringMap = 2 [(protons.options).limit = 10]; } ``` This PR adds the ability to do it at runtime too: ```TypeScript const message = MyMessage.decode(buf, { limits: { repeatedField: 10, stringMap: 10 } }) ```
- Loading branch information
1 parent
3234bb6
commit a737d05
Showing
27 changed files
with
625 additions
and
195 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
import { createCodec, CODEC_TYPES, type EncodeOptions, type Codec } from '../codec.js' | ||
import type { Reader, Writer } from '../index.js' | ||
import { createCodec, CODEC_TYPES, type EncodeFunction, type DecodeFunction, type Codec } from '../codec.js' | ||
|
||
export interface Factory<A, T> { | ||
new (obj: A): T | ||
} | ||
|
||
export function message <T> (encode: (obj: Partial<T>, writer: Writer, opts?: EncodeOptions) => void, decode: (reader: Reader, length?: number) => T): Codec<T> { | ||
export function message <T> (encode: EncodeFunction<T>, decode: DecodeFunction<T>): Codec<T> { | ||
return createCodec('message', CODEC_TYPES.LENGTH_DELIMITED, encode, decode) | ||
} |
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { createReader } from './utils/reader.js' | ||
import type { Codec } from './codec.js' | ||
import type { Codec, DecodeOptions } from './codec.js' | ||
import type { Uint8ArrayList } from 'uint8arraylist' | ||
|
||
export function decodeMessage <T> (buf: Uint8Array | Uint8ArrayList, codec: Codec<T>): T { | ||
export function decodeMessage <T> (buf: Uint8Array | Uint8ArrayList, codec: Codec<T>, opts?: DecodeOptions<T>): T { | ||
const reader = createReader(buf) | ||
|
||
return codec.decode(reader) | ||
return codec.decode(reader, undefined, opts) | ||
} |
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 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
Oops, something went wrong.