File: utils.js

package info (click to toggle)
thunderbird 1%3A91.13.0-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,953,400 kB
  • sloc: cpp: 6,084,049; javascript: 4,790,441; ansic: 3,341,496; python: 862,958; asm: 366,542; xml: 204,277; java: 152,477; sh: 111,436; makefile: 21,388; perl: 15,312; yacc: 4,583; objc: 3,026; lex: 1,720; exp: 762; pascal: 635; awk: 564; sql: 453; php: 436; lisp: 432; ruby: 99; sed: 69; csh: 45
file content (75 lines) | stat: -rw-r--r-- 2,123 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Simple wasm parser to replace "sourceMappingURL" section.

function updateSourceMappingURLSection(buffer, sourceMapUrl) {
  function readVarUint8(buf, pos) {
    let b = buf[pos++];
    let shift = 0;
    let result = 0;
    while (b & 0x80) {
      result |= (b & 0x7f) << shift;
      shift += 7;
      b = buf[pos++];
    }
    return {
      value: result | (b << shift),
      pos
    };
  }
  function readWasmString(buf, pos) {
    const { pos: next, value: len } = readVarUint8(buf, pos);
    const result = String.fromCharCode.apply(
      null,
      buf.subarray(next, next + len)
    );
    return { value: result, pos: next + len };
  }
  function toVarUint(n) {
    const buf = [];
    while (n > 127) {
      buf.push((n & 0x7f) | 0x80);
      n >>>= 7;
    }
    buf.push(n);
    return buf;
  }
  function toWasmString(s) {
    const buf = toVarUint(s.length);
    for (let i = 0; i < s.length; i++) {
      buf.push(s.charCodeAt(i));
    }
    return buf;
  }

  // Appending/replacing sourceMappingURL section based on
  // https://github.com/WebAssembly/design/pull/1051
  const mappingSectionBody = toWasmString("sourceMappingURL").concat(
    toWasmString(sourceMapUrl)
  );
  const mappingSection = toVarUint(0).concat(
    toVarUint(mappingSectionBody.length),
    mappingSectionBody
  );
  const data = new Uint8Array(buffer);
  let start = data.length,
    end = data.length;
  for (let i = 8; i < data.length; ) {
    const { pos: next, value: id } = readVarUint8(data, i);
    const { pos: next2, value: size } = readVarUint8(data, next);
    if (id == 0 && readWasmString(data, next2).value === "sourceMappingURL") {
      start = i;
      end = next2 + size;
      break;
    }
    i = next2 + size;
  }
  const result = new Uint8Array(
    start + (data.length - end) + mappingSection.length
  );
  result.set(data.subarray(0, start));
  result.set(new Uint8Array(mappingSection), start);
  result.set(data.subarray(end), start + mappingSection.length);
  return result.buffer;
}