@ethersphere/bee-js 10.0.0 → 10.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,19 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.parseSizeToBytes = exports.Bytes = void 0;
4
7
  const cafe_utility_1 = require("cafe-utility");
8
+ const debug_1 = __importDefault(require("debug"));
9
+ const debug = (0, debug_1.default)('bee-js:bytes');
5
10
  const DECODER = new TextDecoder();
6
11
  const ENCODER = new TextEncoder();
7
12
  class Bytes {
8
13
  constructor(bytes, byteLength) {
14
+ if (!bytes) {
15
+ throw Error(`Bytes#constructor: constructor parameter is falsy: ${bytes}`);
16
+ }
9
17
  if (bytes instanceof Bytes) {
10
18
  this.bytes = bytes.bytes;
11
19
  }
@@ -15,9 +23,21 @@ class Bytes {
15
23
  else if (bytes instanceof ArrayBuffer) {
16
24
  this.bytes = new Uint8Array(bytes);
17
25
  }
18
- else {
26
+ else if (bytes instanceof Uint8Array) {
19
27
  this.bytes = bytes;
20
28
  }
29
+ else {
30
+ const unknownInput = bytes;
31
+ const toHex = cafe_utility_1.Objects.getDeep(unknownInput, 'toHex');
32
+ if (cafe_utility_1.Types.isFunction(toHex)) {
33
+ const hex = toHex.call(unknownInput);
34
+ this.bytes = cafe_utility_1.Binary.hexToUint8Array(cafe_utility_1.Types.asHexString(hex, { name: 'Bytes#constructor(bytes)' }));
35
+ }
36
+ else {
37
+ debug('bytes', bytes);
38
+ throw new Error(`Bytes#constructor: unsupported type: ${typeof bytes}`);
39
+ }
40
+ }
21
41
  this.length = this.bytes.length;
22
42
  if (byteLength) {
23
43
  if (Array.isArray(byteLength)) {