马宇豪
2024-07-16 f591c27b57e2418c9495bc02ae8cfff84d35bc18
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
const COMMA = ',';
const COLON = ':';
const LEFT_SQUARE_BRACKET = '[';
const RIGHT_SQUARE_BRACKET = ']';
const LEFT_CURLY_BRACKET = '{';
const RIGHT_CURLY_BRACKET = '}';
 
// Recursively encodes the supplied object according to the canonical JSON form
// as specified at http://wiki.laptop.org/go/Canonical_JSON. It's a restricted
// dialect of JSON in which keys are lexically sorted, floats are not allowed,
// and only double quotes and backslashes are escaped.
function canonicalize(object) {
  const buffer = [];
  if (typeof object === 'string') {
    buffer.push(canonicalizeString(object));
  } else if (typeof object === 'boolean') {
    buffer.push(JSON.stringify(object));
  } else if (Number.isInteger(object)) {
    buffer.push(JSON.stringify(object));
  } else if (object === null) {
    buffer.push(JSON.stringify(object));
  } else if (Array.isArray(object)) {
    buffer.push(LEFT_SQUARE_BRACKET);
    let first = true;
    object.forEach((element) => {
      if (!first) {
        buffer.push(COMMA);
      }
      first = false;
      buffer.push(canonicalize(element));
    });
    buffer.push(RIGHT_SQUARE_BRACKET);
  } else if (typeof object === 'object') {
    buffer.push(LEFT_CURLY_BRACKET);
    let first = true;
    Object.keys(object)
      .sort()
      .forEach((property) => {
        if (!first) {
          buffer.push(COMMA);
        }
        first = false;
        buffer.push(canonicalizeString(property));
        buffer.push(COLON);
        buffer.push(canonicalize(object[property]));
      });
    buffer.push(RIGHT_CURLY_BRACKET);
  } else {
    throw new TypeError('cannot encode ' + object.toString());
  }
 
  return buffer.join('');
}
 
// String canonicalization consists of escaping backslash (\) and double
// quote (") characters and wrapping the resulting string in double quotes.
function canonicalizeString(string) {
  const escapedString = string.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
  return '"' + escapedString + '"';
}
 
module.exports = {
  canonicalize,
};