• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

inclusion-numerique / coop-mediation-numerique / 240dc2e0-7f8b-417f-9969-2331dc3cf6e8

01 Apr 2026 04:06PM UTC coverage: 10.684% (+3.7%) from 6.94%
240dc2e0-7f8b-417f-9969-2331dc3cf6e8

push

circleci

web-flow
Merge pull request #469 from inclusion-numerique/dev

MEP 2026-01-01

709 of 10542 branches covered (6.73%)

Branch coverage included in aggregate %.

25 of 399 new or added lines in 37 files covered. (6.27%)

820 existing lines in 91 files now uncovered.

2174 of 16443 relevant lines covered (13.22%)

2.0 hits per line

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

0.0
/apps/web/src/utils/encodeSerializableState.ts
1
import pako from 'pako'
2
import superjson from 'superjson'
3

4
/**
5
 * Utility functions to encode and decode form state for safe transmission via URL.
6
 *
7
 * This module provides two functions, `encodeSerializableState` and `decodeSerializableState`,
8
 * which are intended to be used for serializing form state to a compressed string
9
 * and deserializing it back to its original form. This is particularly useful for passing
10
 * non-sensitive form data through URLs, ensuring the data remains unreadable to users.
11
 *
12
 * Example use case:
13
 * - Encode form state into a compressed string and append it as a query parameter in a URL.
14
 * - Decode the compressed string from the URL to prefill the form with the original data.
15
 */
16

17
export type EncodedState<T> = string & { __encodedStateType: T }
18

19
/**
20
 * Converts a Uint8Array to a url safe Base64 string.
21
 */
UNCOV
22
const uint8ArrayToBase64 = (uint8Array: Uint8Array): string => {
×
UNCOV
23
  const binaryString = [...uint8Array]
×
UNCOV
24
    .map((byte) => String.fromCodePoint(byte))
×
25
    .join('')
26
  return btoa(binaryString)
27
    .replaceAll('+', '-') // Replace "+" with "-"
28
    .replaceAll('/', '_') // Replace "/" with "_"
29
    .replace(/=+$/, '') // Remove "=" padding
30
}
31

32
/**
33
 * Converts a url safe Base64 string back to a Uint8Array.
34
 */
UNCOV
35
const base64ToUint8Array = (base64String: string): Uint8Array => {
×
UNCOV
36
  const binaryString = atob(
×
37
    base64String
38
      .replaceAll('-', '+') // Replace "-" with "+"
39
      .replaceAll('_', '/'), // Replace "_" with "/"
40
  )
UNCOV
41
  return Uint8Array.from(binaryString, (char) => {
×
UNCOV
42
    const code = char.codePointAt(0)
×
UNCOV
43
    if (code === undefined) {
×
44
      throw new Error('Invalid character in Base64 string')
×
45
    }
UNCOV
46
    return code
×
47
  })
48
}
49

UNCOV
50
export const encodeSerializableState = <T>(state: T): EncodedState<T> => {
×
UNCOV
51
  const jsonString = superjson.stringify(state)
×
UNCOV
52
  const compressed = pako.deflate(jsonString)
×
53

UNCOV
54
  return uint8ArrayToBase64(compressed) as EncodedState<T> // Base64-URL-safe encoding
×
55
}
56

UNCOV
57
export const decodeSerializableState = <T>(
×
58
  encodedState: EncodedState<T>,
59
  defaultValue: T, // Needed if decoding fails
60
): T => {
UNCOV
61
  try {
×
UNCOV
62
    const compressed = base64ToUint8Array(encodedState) // Base64-URL-safe decoding
×
UNCOV
63
    const jsonString = pako.inflate(compressed, { to: 'string' })
×
UNCOV
64
    return superjson.parse<T>(jsonString)
×
65
  } catch {
UNCOV
66
    return defaultValue
×
67
  }
68
}
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