• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

LukaJCB / ts-mls / 23114835530

15 Mar 2026 04:50PM UTC coverage: 96.386% (-0.03%) from 96.417%
23114835530

Pull #361

github

web-flow
Merge 89c5b9462 into 1f20b9f86
Pull Request #361: Implement 64MB upper limit for decoded payload size

418 of 421 branches covered (99.29%)

Branch coverage included in aggregate %.

1 of 2 new or added lines in 1 file covered. (50.0%)

2356 of 2457 relevant lines covered (95.89%)

153697.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.36
/src/codec/tlsDecoder.ts
1
import { CodecError } from "../mlsError.js"
2

3
/** @public */
4
export type Decoder<T> = (b: Uint8Array, offset: number) => [T, number] | undefined
5

6
/** @public */
7
export function decode<T>(dec: Decoder<T>, t: Uint8Array, maxDecodedSize: number = 64000000): T | undefined {
8
  if (t.length > maxDecodedSize)
359✔
NEW
9
    throw new CodecError("Payload larger than max allowed size, increase maxDecodedSize if you want to decode this")
×
10
  return dec(t, 0)?.[0]
359✔
11
}
12

13
export function mapDecoder<T, U>(dec: Decoder<T>, f: (t: T) => U): Decoder<U> {
14
  return (b, offset) => {
15,853✔
15
    const x = dec(b, offset)
53,092✔
16
    if (x !== undefined) {
53,092✔
17
      const [t, l] = x
53,092✔
18
      return [f(t), l]
53,092✔
19
    }
20
  }
21
}
22

23
export function mapDecodersOption<T extends unknown[], R>(
24
  rsDecoder: { [K in keyof T]: Decoder<T[K]> },
25
  f: (...args: T) => R | undefined,
26
): Decoder<R> {
27
  return (b, offset) => {
3✔
28
    const initial = mapDecoders(rsDecoder, f)(b, offset)
1✔
29
    if (initial === undefined) return undefined
1✔
30
    else {
31
      const [r, len] = initial
1✔
32
      return r !== undefined ? [r, len] : undefined
1✔
33
    }
34
  }
35
}
36

37
export function mapDecoders<T extends unknown[], R>(
38
  rsDecoder: { [K in keyof T]: Decoder<T[K]> },
39
  f: (...args: T) => R,
40
): Decoder<R> {
41
  return (b, offset) => {
1,464✔
42
    const result = rsDecoder.reduce<
116,617✔
43
      | {
44
          values: unknown[]
45
          offset: number
46
          totalLength: number
47
        }
48
      | undefined
49
    >(
50
      (acc, decoder) => {
51
        if (!acc) return undefined
348,406✔
52

53
        const decoded = decoder(b, acc.offset)
348,406✔
54
        if (!decoded) return undefined
348,406✔
55

56
        const [value, length] = decoded
348,406✔
57
        return {
348,406✔
58
          values: [...acc.values, value],
59
          offset: acc.offset + length,
60
          totalLength: acc.totalLength + length,
61
        }
62
      },
63
      { values: [], offset, totalLength: 0 },
64
    )
65

66
    if (!result) return
116,617✔
67
    return [f(...(result.values as T)), result.totalLength]
116,617✔
68
  }
69
}
70

71
export function mapDecoderOption<T, U>(dec: Decoder<T>, f: (t: T) => U | undefined): Decoder<U> {
72
  return (b, offset) => {
398✔
73
    const x = dec(b, offset)
67,113✔
74
    if (x !== undefined) {
67,113✔
75
      const [t, l] = x
67,113✔
76
      const u = f(t)
67,113✔
77
      return u !== undefined ? [u, l] : undefined
67,113✔
78
    }
79
  }
80
}
81

82
export function flatMapDecoder<T, U>(dec: Decoder<T>, f: (t: T) => Decoder<U>): Decoder<U> {
83
  return flatMapDecoderAndMap(dec, f, (_t, u) => u)
19,764✔
84
}
85

86
export function orDecoder<T, U>(decT: Decoder<T>, decU: Decoder<U>): Decoder<T | U> {
87
  return (b, offset) => {
3✔
88
    const t = decT(b, offset)
4,783✔
89
    return t ? t : decU(b, offset)
4,783✔
90
  }
91
}
92

93
function flatMapDecoderAndMap<T, U, V>(dec: Decoder<T>, f: (t: T) => Decoder<U>, g: (t: T, u: U) => V): Decoder<V> {
94
  return (b, offset) => {
2,302✔
95
    const decodedT = dec(b, offset)
19,859✔
96
    if (decodedT !== undefined) {
19,859✔
97
      const [t, len] = decodedT
19,802✔
98
      const rUDecoder = f(t)
19,802✔
99
      const decodedU = rUDecoder(b, offset + len)
19,802✔
100
      if (decodedU !== undefined) {
19,802✔
101
        const [u, len2] = decodedU
19,802✔
102
        return [g(t, u), len + len2]
19,802✔
103
      }
104
    }
105
  }
106
}
107

108
export function succeedDecoder<T>(t: T): Decoder<T> {
109
  return () => [t, 0] as const
41✔
110
}
111

112
export function failDecoder<T>(): Decoder<T> {
113
  return () => undefined
×
114
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc