aboutsummaryrefslogtreecommitdiff
path: root/src/js/builtins/JSBufferConstructor.ts
blob: a1072ea1024c1112fe90d40c81449d661018cc6a (plain) (blame)
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
export function from(items) {
  if ($isUndefinedOrNull(items)) {
    throw new TypeError(
      "The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object.",
    );
  }

  // TODO: figure out why private symbol not found
  if (
    typeof items === "string" ||
    (typeof items === "object" &&
      ($isTypedArrayView(items) ||
        items instanceof ArrayBuffer ||
        items instanceof SharedArrayBuffer ||
        items instanceof String))
  ) {
    switch ($argumentCount()) {
      case 1: {
        return new $Buffer(items);
      }
      case 2: {
        return new $Buffer(items, $argument(1));
      }
      default: {
        return new $Buffer(items, $argument(1), $argument(2));
      }
    }
  }

  var arrayLike = $toObject(
    items,
    "The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object.",
  ) as ArrayLike<any>;

  if (!$isJSArray(arrayLike)) {
    const toPrimitive = $tryGetByIdWithWellKnownSymbol(items, "toPrimitive");

    if (toPrimitive) {
      const primitive = toPrimitive.$call(items, "string");

      if (typeof primitive === "string") {
        switch ($argumentCount()) {
          case 1: {
            return new $Buffer(primitive);
          }
          case 2: {
            return new $Buffer(primitive, $argument(1));
          }
          default: {
            return new $Buffer(primitive, $argument(1), $argument(2));
          }
        }
      }
    }

    if (!("length" in arrayLike) || $isCallable(arrayLike)) {
      throw new TypeError(
        "The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object.",
      );
    }
  }

  // Don't pass the second argument because Node's Buffer.from doesn't accept
  // a function and Uint8Array.from requires it if it exists
  // That means we cannot use $tailCallFowrardArguments here, sadly
  return new $Buffer(Uint8Array.from(arrayLike).buffer);
}

export function isBuffer(bufferlike) {
  return bufferlike instanceof $Buffer;
}