@ethersphere/bee-js 10.0.0 → 10.1.0

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)) {
@@ -30,6 +30,32 @@ class Duration {
30
30
  static fromEndDate(endDate, startDate) {
31
31
  return new Duration((endDate.getTime() - (startDate ?? new Date()).getTime()) / 1000);
32
32
  }
33
+ /**
34
+ * Parses a duration string and returns a `Duration` instance.
35
+ *
36
+ * Case insensitive. E.g. both `"28h"` and `"1D"` are valid.
37
+ *
38
+ * Whitespaces are ignored. E.g. both `"5 d"` and `"2weeks"` are valid.
39
+ *
40
+ * Decimal numbers are supported. E.g. `"1.5h"` is valid.
41
+ *
42
+ * Supported units:
43
+ *
44
+ * - ms, milli, millis, millisecond, milliseconds
45
+ * - s, sec, second, seconds
46
+ * - m, min, minute, minutes
47
+ * - h, hour, hours
48
+ * - d, day, days
49
+ * - w, week, weeks
50
+ * - month, months
51
+ * - y, year, years
52
+ *
53
+ * @param duration - A string representing a duration
54
+ * @returns a `Duration` instance
55
+ */
56
+ static parseFromString(duration) {
57
+ return Duration.fromSeconds(cafe_utility_1.Dates.make(duration) / 1000);
58
+ }
33
59
  toSeconds() {
34
60
  return this.seconds;
35
61
  }
@@ -28,6 +28,31 @@ class Size {
28
28
  static fromGigabytes(gigabytes) {
29
29
  return new Size(gigabytes * 1000 * 1000 * 1000);
30
30
  }
31
+ /**
32
+ * Parses a size string and returns a `Size` instance.
33
+ *
34
+ * Case insensitive. E.g. both `"28MB"` and `"1gb"` are valid.
35
+ *
36
+ * Whitespaces are ignored. E.g. both `"512 kb"` and `"2megabytes"` are valid.
37
+ *
38
+ * Decimal numbers are supported. E.g. `"1.5gb"` is valid.
39
+ *
40
+ * Uses 1000 as the base for conversions. E.g. 1kb = 1000 bytes.
41
+ * This is consistent with the effective stamp utilization table.
42
+ *
43
+ * Supported units:
44
+ * - b, byte, bytes
45
+ * - kb, kilobyte, kilobytes
46
+ * - mb, megabyte, megabytes
47
+ * - gb, gigabyte, gigabytes
48
+ * - tb, terabyte, terabytes
49
+ *
50
+ * @param size - A string representing a size
51
+ * @returns a `Size` instance
52
+ */
53
+ static parseFromString(size) {
54
+ return Size.fromBytes(cafe_utility_1.Numbers.makeStorage(size, 1000));
55
+ }
31
56
  toBytes() {
32
57
  return this.bytes;
33
58
  }