Skip to content

Commit

Permalink
use UINT32_MAX because the doc says max lengths are UINT32_MAX
Browse files Browse the repository at this point in the history
  • Loading branch information
gfx committed May 5, 2021
1 parent 78837ef commit c58b7e2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
14 changes: 6 additions & 8 deletions src/Decoder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { prettyByte } from "./utils/prettyByte";
import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec";
import { getInt64, getUint64 } from "./utils/int";
import { getInt64, getUint64, UINT32_MAX } from "./utils/int";
import { utf8DecodeJs, TEXT_DECODER_THRESHOLD, utf8DecodeTD } from "./utils/utf8";
import { createDataView, ensureUint8Array } from "./utils/typedArrays";
import { CachedKeyDecoder, KeyDecoder } from "./CachedKeyDecoder";
Expand Down Expand Up @@ -57,8 +57,6 @@ export const DataViewIndexOutOfBoundsError: typeof Error = (() => {

const MORE_DATA = new DataViewIndexOutOfBoundsError("Insufficient data");

const DEFAULT_MAX_LENGTH = 0xffff_ffff; // uint32_max

const sharedCachedKeyDecoder = new CachedKeyDecoder();

export class Decoder<ContextType = undefined> {
Expand All @@ -73,11 +71,11 @@ export class Decoder<ContextType = undefined> {
public constructor(
private readonly extensionCodec: ExtensionCodecType<ContextType> = ExtensionCodec.defaultCodec as any,
private readonly context: ContextType = undefined as any,
private readonly maxStrLength = DEFAULT_MAX_LENGTH,
private readonly maxBinLength = DEFAULT_MAX_LENGTH,
private readonly maxArrayLength = DEFAULT_MAX_LENGTH,
private readonly maxMapLength = DEFAULT_MAX_LENGTH,
private readonly maxExtLength = DEFAULT_MAX_LENGTH,
private readonly maxStrLength = UINT32_MAX,
private readonly maxBinLength = UINT32_MAX,
private readonly maxArrayLength = UINT32_MAX,
private readonly maxMapLength = UINT32_MAX,
private readonly maxExtLength = UINT32_MAX,
private readonly keyDecoder: KeyDecoder | null = sharedCachedKeyDecoder,
) {}

Expand Down
4 changes: 4 additions & 0 deletions src/utils/int.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Integer Utility

export const UINT32_MAX = 0xffff_ffff;

// DataView extension to handle int64 / uint64,
// where the actual range is 53-bits integer (a.k.a. safe integer)

Expand Down
8 changes: 4 additions & 4 deletions src/utils/utf8.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { UINT32_MAX } from "./int";

const TEXT_ENCODING_AVAILABLE =
(typeof process === "undefined" || process.env["TEXT_ENCODING"] !== "never") &&
typeof TextEncoder !== "undefined" &&
typeof TextDecoder !== "undefined";

const STR_SIZE_MAX = 0xffff_ffff; // uint32_max

export function utf8Count(str: string): number {
const strLength = str.length;

Expand Down Expand Up @@ -90,7 +90,7 @@ export function utf8EncodeJs(str: string, output: Uint8Array, outputOffset: numb

const sharedTextEncoder = TEXT_ENCODING_AVAILABLE ? new TextEncoder() : undefined;
export const TEXT_ENCODER_THRESHOLD = !TEXT_ENCODING_AVAILABLE
? STR_SIZE_MAX
? UINT32_MAX
: typeof process !== "undefined" && process.env["TEXT_ENCODING"] !== "force"
? 200
: 0;
Expand Down Expand Up @@ -158,7 +158,7 @@ export function utf8DecodeJs(bytes: Uint8Array, inputOffset: number, byteLength:

const sharedTextDecoder = TEXT_ENCODING_AVAILABLE ? new TextDecoder() : null;
export const TEXT_DECODER_THRESHOLD = !TEXT_ENCODING_AVAILABLE
? STR_SIZE_MAX
? UINT32_MAX
: typeof process !== "undefined" && process.env["TEXT_DECODER"] !== "force"
? 200
: 0;
Expand Down

0 comments on commit c58b7e2

Please sign in to comment.