@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.
package/dist/mjs/utils/bytes.js
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
|
-
import { Binary, Types } from 'cafe-utility';
|
|
1
|
+
import { Binary, Objects, Types } from 'cafe-utility';
|
|
2
|
+
import _debug from 'debug';
|
|
3
|
+
const debug = _debug('bee-js:bytes');
|
|
2
4
|
const DECODER = new TextDecoder();
|
|
3
5
|
const ENCODER = new TextEncoder();
|
|
4
6
|
export class Bytes {
|
|
5
7
|
constructor(bytes, byteLength) {
|
|
8
|
+
if (!bytes) {
|
|
9
|
+
throw Error(`Bytes#constructor: constructor parameter is falsy: ${bytes}`);
|
|
10
|
+
}
|
|
6
11
|
if (bytes instanceof Bytes) {
|
|
7
12
|
this.bytes = bytes.bytes;
|
|
8
13
|
} else if (typeof bytes === 'string') {
|
|
@@ -11,8 +16,20 @@ export class Bytes {
|
|
|
11
16
|
}));
|
|
12
17
|
} else if (bytes instanceof ArrayBuffer) {
|
|
13
18
|
this.bytes = new Uint8Array(bytes);
|
|
14
|
-
} else {
|
|
19
|
+
} else if (bytes instanceof Uint8Array) {
|
|
15
20
|
this.bytes = bytes;
|
|
21
|
+
} else {
|
|
22
|
+
const unknownInput = bytes;
|
|
23
|
+
const toHex = Objects.getDeep(unknownInput, 'toHex');
|
|
24
|
+
if (Types.isFunction(toHex)) {
|
|
25
|
+
const hex = toHex.call(unknownInput);
|
|
26
|
+
this.bytes = Binary.hexToUint8Array(Types.asHexString(hex, {
|
|
27
|
+
name: 'Bytes#constructor(bytes)'
|
|
28
|
+
}));
|
|
29
|
+
} else {
|
|
30
|
+
debug('bytes', bytes);
|
|
31
|
+
throw new Error(`Bytes#constructor: unsupported type: ${typeof bytes}`);
|
|
32
|
+
}
|
|
16
33
|
}
|
|
17
34
|
this.length = this.bytes.length;
|
|
18
35
|
if (byteLength) {
|