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

visgl / loaders.gl / 20352515932

18 Dec 2025 09:56PM UTC coverage: 35.115% (-28.4%) from 63.485%
20352515932

push

github

web-flow
feat(loader-utils): Export is-type helpers (#3258)

1188 of 1998 branches covered (59.46%)

Branch coverage included in aggregate %.

147 of 211 new or added lines in 13 files covered. (69.67%)

30011 existing lines in 424 files now uncovered.

37457 of 108056 relevant lines covered (34.66%)

0.79 hits per line

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

47.19
/modules/crypto/src/lib/utils/base64-utils.ts
1
// loaders.gl
1✔
2
// SPDX-License-Identifier: MIT
1✔
3
// Copyright (c) vis.gl contributors
1✔
4

1✔
5
/**
1✔
6
 * `btoa()` polyfill as defined by the HTML and Infra specs, which mostly just references
1✔
7
 * RFC 4648.
1✔
8
 */
1✔
9
export function asciiToBase64(string: string): string | null {
1✔
10
  // String conversion as required by Web IDL.
1✔
11
  string = `${string}`;
1✔
12
  // "The btoa() method must throw an "InvalidCharacterError" DOMException if
1✔
13
  // data contains any character whose code point is greater than U+00FF."
1✔
14
  for (let i = 0; i < string.length; i++) {
1✔
15
    if (string.charCodeAt(i) > 255) {
4!
16
      return null;
×
17
    }
×
18
  }
4✔
19
  let out = '';
1✔
20
  for (let i = 0; i < string.length; i += 3) {
1✔
21
    /** @type {Array[4]} */
2✔
22
    const groupsOfSix: (number | undefined)[] = [undefined, undefined, undefined, undefined];
2✔
23
    groupsOfSix[0] = string.charCodeAt(i) >> 2;
2✔
24
    groupsOfSix[1] = (string.charCodeAt(i) & 0x03) << 4;
2✔
25
    if (string.length > i + 1) {
2✔
26
      groupsOfSix[1] |= string.charCodeAt(i + 1) >> 4;
1✔
27
      groupsOfSix[2] = (string.charCodeAt(i + 1) & 0x0f) << 2;
1✔
28
    }
1✔
29
    if (string.length > i + 2) {
2✔
30
      // @ts-expect-error
1✔
31
      groupsOfSix[2] |= string.charCodeAt(i + 2) >> 6;
1✔
32
      groupsOfSix[3] = string.charCodeAt(i + 2) & 0x3f;
1✔
33
    }
1✔
34
    for (let j = 0; j < groupsOfSix.length; j++) {
2✔
35
      if (typeof groupsOfSix[j] === 'undefined') {
8✔
36
        out += '=';
2✔
37
      } else {
8✔
38
        out += btoaLookup(groupsOfSix[j]);
6✔
39
      }
6✔
40
    }
8✔
41
  }
2✔
42
  return out;
1✔
43
}
1✔
44

1✔
45
/**
1✔
46
 * Implementation of atob() according to the HTML and Infra specs, except that
1✔
47
 * instead of throwing INVALID_CHARACTER_ERR we return null.
1✔
48
 *
1✔
49
 * @note Forked from https://github.com/jsdom/abab under BSD 3 clause license
1✔
50
 */
1✔
51
export function base64ToAscii(data: string): string {
1✔
UNCOV
52
  // Web IDL requires DOMStrings to just be converted using ECMAScript
×
UNCOV
53
  // ToString, which in our case amounts to using a template literal.
×
UNCOV
54
  data = `${data}`;
×
UNCOV
55
  // "Remove all ASCII whitespace from data."
×
UNCOV
56
  data = data.replace(/[ \t\n\f\r]/g, '');
×
UNCOV
57
  // "If data's code point length divides by 4 leaving no remainder, then: if data ends
×
UNCOV
58
  // with one or two U+003D (=) code points, then remove them from data."
×
UNCOV
59
  if (data.length % 4 === 0) {
×
UNCOV
60
    data = data.replace(/[=]=?$/, '');
×
UNCOV
61
  }
×
UNCOV
62
  // "If data's code point length divides by 4 leaving a remainder of 1, then return
×
UNCOV
63
  // failure."
×
UNCOV
64
  //
×
UNCOV
65
  // "If data contains a code point that is not one of
×
UNCOV
66
  //
×
UNCOV
67
  // U+002B (+)
×
UNCOV
68
  // U+002F (/)
×
UNCOV
69
  // ASCII alphanumeric
×
UNCOV
70
  //
×
UNCOV
71
  // then return failure."
×
UNCOV
72
  if (data.length % 4 === 1 || /[^+/0-9A-Za-z]/.test(data)) {
×
73
    return '';
×
74
  }
×
UNCOV
75
  // "Let output be an empty byte sequence."
×
UNCOV
76
  let output = '';
×
UNCOV
77
  // "Let buffer be an empty buffer that can have bits appended to it."
×
UNCOV
78
  //
×
UNCOV
79
  // We append bits via left-shift and or.  accumulatedBits is used to track
×
UNCOV
80
  // when we've gotten to 24 bits.
×
UNCOV
81
  let buffer = 0;
×
UNCOV
82
  let accumulatedBits = 0;
×
UNCOV
83
  // "Let position be a position variable for data, initially pointing at the
×
UNCOV
84
  // start of data."
×
UNCOV
85
  //
×
UNCOV
86
  // "While position does not point past the end of data:"
×
UNCOV
87
  for (let i = 0; i < data.length; i++) {
×
UNCOV
88
    // "Find the code point pointed to by position in the second column of
×
UNCOV
89
    // Table 1: The Base 64 Alphabet of RFC 4648. Let n be the number given in
×
UNCOV
90
    // the first cell of the same row.
×
UNCOV
91
    //
×
UNCOV
92
    // "Append to buffer the six bits corresponding to n, most significant bit
×
UNCOV
93
    // first."
×
UNCOV
94
    //
×
UNCOV
95
    // atobLookup() implements the table from RFC 4648.
×
UNCOV
96
    buffer <<= 6;
×
UNCOV
97
    // @ts-expect-error
×
UNCOV
98
    buffer |= atobLookup(data[i]);
×
UNCOV
99
    accumulatedBits += 6;
×
UNCOV
100
    // "If buffer has accumulated 24 bits, interpret them as three 8-bit
×
UNCOV
101
    // big-endian numbers. Append three bytes with values equal to those
×
UNCOV
102
    // numbers to output, in the same order, and then empty buffer."
×
UNCOV
103
    if (accumulatedBits === 24) {
×
UNCOV
104
      output += String.fromCharCode((buffer & 0xff0000) >> 16);
×
UNCOV
105
      output += String.fromCharCode((buffer & 0xff00) >> 8);
×
UNCOV
106
      output += String.fromCharCode(buffer & 0xff);
×
UNCOV
107
      buffer = accumulatedBits = 0;
×
UNCOV
108
    }
×
UNCOV
109
    // "Advance position by 1."
×
UNCOV
110
  }
×
UNCOV
111
  // "If buffer is not empty, it contains either 12 or 18 bits. If it contains
×
UNCOV
112
  // 12 bits, then discard the last four and interpret the remaining eight as
×
UNCOV
113
  // an 8-bit big-endian number. If it contains 18 bits, then discard the last
×
UNCOV
114
  // two and interpret the remaining 16 as two 8-bit big-endian numbers. Append
×
UNCOV
115
  // the one or two bytes with values equal to those one or two numbers to
×
UNCOV
116
  // output, in the same order."
×
UNCOV
117
  if (accumulatedBits === 12) {
×
UNCOV
118
    buffer >>= 4;
×
UNCOV
119
    output += String.fromCharCode(buffer);
×
UNCOV
120
  } else if (accumulatedBits === 18) {
×
121
    buffer >>= 2;
×
122
    output += String.fromCharCode((buffer & 0xff00) >> 8);
×
123
    output += String.fromCharCode(buffer & 0xff);
×
124
  }
×
UNCOV
125
  // "Return output."
×
UNCOV
126
  return output;
×
UNCOV
127
}
×
128
/**
1✔
129
 * A lookup table for atob(), which converts an ASCII character to the
1✔
130
 * corresponding six-bit number.
1✔
131
 */
1✔
132

1✔
133
const keystr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
1✔
134

1✔
UNCOV
135
function atobLookup(chr: string): number | undefined {
×
UNCOV
136
  const index = keystr.indexOf(chr);
×
UNCOV
137
  // Throw exception if character is not in the lookup string; should not be hit in tests
×
UNCOV
138
  return index < 0 ? undefined : index;
×
UNCOV
139
}
×
140

1✔
141
/**
1✔
142
 * Lookup table for btoa(), which converts a six-bit number into the
1✔
143
 * corresponding ASCII character.
1✔
144
 */
1✔
145
function btoaLookup(idx) {
6✔
146
  if (idx < 26) {
6✔
147
    return String.fromCharCode(idx + 'A'.charCodeAt(0));
3✔
148
  }
3✔
149
  if (idx < 52) {
6✔
150
    return String.fromCharCode(idx - 26 + 'a'.charCodeAt(0));
2✔
151
  }
2✔
152
  if (idx < 62) {
1✔
153
    return String.fromCharCode(idx - 52 + '0'.charCodeAt(0));
1✔
154
  }
1!
UNCOV
155
  if (idx === 62) {
×
UNCOV
156
    return '+';
×
UNCOV
157
  }
×
UNCOV
158
  if (idx === 63) {
×
UNCOV
159
    return '/';
×
UNCOV
160
  }
×
161
  // Throw INVALID_CHARACTER_ERR exception here -- won't be hit in the teststring.
×
162
  return undefined;
×
163
}
×
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