@ethersphere/bee-js 6.0.0-pre.0 → 6.0.0-pre.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.
Files changed (193) hide show
  1. package/README.md +18 -17
  2. package/dist/cjs/bee-debug.js +569 -0
  3. package/dist/cjs/bee.js +873 -0
  4. package/dist/cjs/chunk/bmt.js +55 -0
  5. package/dist/cjs/chunk/cac.js +56 -0
  6. package/dist/cjs/chunk/serialize.js +19 -0
  7. package/dist/cjs/chunk/signer.js +126 -0
  8. package/dist/cjs/chunk/soc.js +159 -0
  9. package/dist/cjs/chunk/span.js +29 -0
  10. package/dist/cjs/feed/identifier.js +35 -0
  11. package/dist/cjs/feed/index.js +116 -0
  12. package/dist/cjs/feed/json.js +28 -0
  13. package/dist/cjs/feed/retrievable.js +58 -0
  14. package/dist/cjs/feed/topic.js +25 -0
  15. package/dist/cjs/feed/type.js +15 -0
  16. package/dist/cjs/index.js +39 -0
  17. package/dist/cjs/modules/bytes.js +62 -0
  18. package/dist/cjs/modules/bzz.js +125 -0
  19. package/dist/cjs/modules/chunk.js +48 -0
  20. package/dist/cjs/modules/debug/balance.js +60 -0
  21. package/dist/cjs/modules/debug/chequebook.js +142 -0
  22. package/dist/cjs/modules/debug/chunk.js +38 -0
  23. package/dist/cjs/modules/debug/connectivity.js +54 -0
  24. package/dist/cjs/modules/debug/settlements.js +32 -0
  25. package/dist/cjs/modules/debug/stake.js +57 -0
  26. package/dist/cjs/modules/debug/stamps.js +68 -0
  27. package/dist/cjs/modules/debug/states.js +64 -0
  28. package/dist/cjs/modules/debug/status.js +148 -0
  29. package/dist/cjs/modules/debug/tag.js +19 -0
  30. package/dist/cjs/modules/debug/transactions.js +64 -0
  31. package/dist/cjs/modules/feed.js +66 -0
  32. package/dist/cjs/modules/pinning.js +63 -0
  33. package/dist/cjs/modules/pss.js +44 -0
  34. package/dist/cjs/modules/soc.js +32 -0
  35. package/dist/cjs/modules/status.js +15 -0
  36. package/dist/cjs/modules/stewardship.js +28 -0
  37. package/dist/cjs/modules/tag.js +77 -0
  38. package/dist/cjs/package.json +8 -0
  39. package/dist/cjs/types/debug.js +10 -0
  40. package/dist/cjs/types/index.js +51 -0
  41. package/dist/cjs/utils/bytes.js +121 -0
  42. package/dist/cjs/utils/collection.browser.js +23 -0
  43. package/dist/cjs/utils/collection.js +59 -0
  44. package/dist/cjs/utils/collection.node.js +71 -0
  45. package/dist/cjs/utils/data.browser.js +61 -0
  46. package/dist/cjs/utils/data.js +45 -0
  47. package/dist/cjs/utils/error.js +16 -0
  48. package/dist/cjs/utils/eth.js +199 -0
  49. package/dist/cjs/utils/expose.js +44 -0
  50. package/dist/cjs/utils/file.js +38 -0
  51. package/dist/cjs/utils/hash.js +21 -0
  52. package/dist/cjs/utils/headers.js +59 -0
  53. package/dist/cjs/utils/hex.js +150 -0
  54. package/dist/cjs/utils/http.js +30 -0
  55. package/dist/cjs/utils/pss.js +18 -0
  56. package/dist/cjs/utils/reference.js +36 -0
  57. package/dist/cjs/utils/stamps.js +17 -0
  58. package/dist/cjs/utils/stream.js +146 -0
  59. package/dist/cjs/utils/tar.js +25 -0
  60. package/dist/cjs/utils/type.js +434 -0
  61. package/dist/cjs/utils/uint64.js +29 -0
  62. package/dist/cjs/utils/url.js +56 -0
  63. package/dist/index.browser.min.js +3 -0
  64. package/dist/index.browser.min.js.LICENSE.txt +50 -0
  65. package/dist/index.browser.min.js.map +1 -0
  66. package/dist/{src → mjs}/bee-debug.js +101 -185
  67. package/dist/{src → mjs}/bee.js +81 -234
  68. package/dist/{src → mjs}/chunk/bmt.js +4 -10
  69. package/dist/{src → mjs}/chunk/cac.js +0 -3
  70. package/dist/{src → mjs}/chunk/signer.js +1 -18
  71. package/dist/{src → mjs}/chunk/soc.js +17 -29
  72. package/dist/{src → mjs}/chunk/span.js +2 -5
  73. package/dist/{src → mjs}/feed/identifier.js +0 -6
  74. package/dist/{src → mjs}/feed/index.js +24 -27
  75. package/dist/{src → mjs}/feed/json.js +2 -5
  76. package/dist/{src → mjs}/feed/retrievable.js +7 -21
  77. package/dist/{src → mjs}/feed/topic.js +0 -2
  78. package/dist/{src → mjs}/index.js +2 -2
  79. package/dist/{src → mjs}/modules/bytes.js +16 -19
  80. package/dist/{src → mjs}/modules/bzz.js +33 -40
  81. package/dist/{src → mjs}/modules/chunk.js +11 -13
  82. package/dist/mjs/modules/debug/balance.js +53 -0
  83. package/dist/mjs/modules/debug/chequebook.js +135 -0
  84. package/dist/{src → mjs}/modules/debug/chunk.js +10 -12
  85. package/dist/mjs/modules/debug/connectivity.js +45 -0
  86. package/dist/mjs/modules/debug/settlements.js +27 -0
  87. package/dist/mjs/modules/debug/stake.js +51 -0
  88. package/dist/mjs/modules/debug/stamps.js +61 -0
  89. package/dist/mjs/modules/debug/states.js +58 -0
  90. package/dist/{src → mjs}/modules/debug/status.js +46 -37
  91. package/dist/mjs/modules/debug/tag.js +15 -0
  92. package/dist/mjs/modules/debug/transactions.js +59 -0
  93. package/dist/{src → mjs}/modules/feed.js +15 -21
  94. package/dist/mjs/modules/pinning.js +56 -0
  95. package/dist/{src → mjs}/modules/pss.js +7 -9
  96. package/dist/{src → mjs}/modules/soc.js +6 -7
  97. package/dist/mjs/modules/status.js +11 -0
  98. package/dist/mjs/modules/stewardship.js +23 -0
  99. package/dist/mjs/modules/tag.js +72 -0
  100. package/dist/mjs/package.json +8 -0
  101. package/dist/{src → mjs}/types/debug.js +0 -1
  102. package/dist/{src → mjs}/types/index.js +0 -4
  103. package/dist/{src → mjs}/utils/bytes.js +8 -17
  104. package/dist/{src/utils/collection-browser.js → mjs/utils/collection.browser.js} +0 -1
  105. package/dist/{src → mjs}/utils/collection.js +0 -12
  106. package/dist/{src/utils/collection-node.js → mjs/utils/collection.node.js} +0 -13
  107. package/dist/{src → mjs}/utils/data.browser.js +2 -11
  108. package/dist/{src → mjs}/utils/data.js +0 -7
  109. package/dist/mjs/utils/error.js +11 -0
  110. package/dist/{src → mjs}/utils/eth.js +6 -28
  111. package/dist/{src → mjs}/utils/expose.js +1 -1
  112. package/dist/{src → mjs}/utils/file.js +4 -9
  113. package/dist/{src → mjs}/utils/hash.js +0 -1
  114. package/dist/{src → mjs}/utils/headers.js +5 -13
  115. package/dist/{src → mjs}/utils/hex.js +6 -25
  116. package/dist/mjs/utils/http.js +22 -0
  117. package/dist/{src → mjs}/utils/pss.js +0 -2
  118. package/dist/{src → mjs}/utils/reference.js +2 -5
  119. package/dist/{src → mjs}/utils/stream.js +3 -26
  120. package/dist/{src → mjs}/utils/tar.js +2 -5
  121. package/dist/{src → mjs}/utils/type.js +23 -80
  122. package/dist/{src → mjs}/utils/uint64.js +0 -1
  123. package/dist/{src → mjs}/utils/url.js +2 -10
  124. package/dist/types/bee-debug.d.ts +71 -46
  125. package/dist/types/bee.d.ts +34 -58
  126. package/dist/types/chunk/bmt.d.ts +1 -1
  127. package/dist/types/chunk/cac.d.ts +1 -1
  128. package/dist/types/chunk/soc.d.ts +9 -9
  129. package/dist/types/chunk/span.d.ts +1 -1
  130. package/dist/types/feed/index.d.ts +10 -10
  131. package/dist/types/feed/json.d.ts +2 -2
  132. package/dist/types/feed/retrievable.d.ts +2 -2
  133. package/dist/types/feed/type.d.ts +1 -1
  134. package/dist/types/index.d.ts +13 -2
  135. package/dist/types/modules/bytes.d.ts +5 -5
  136. package/dist/types/modules/bzz.d.ts +8 -8
  137. package/dist/types/modules/chunk.d.ts +4 -4
  138. package/dist/types/modules/debug/balance.d.ts +9 -9
  139. package/dist/types/modules/debug/chequebook.d.ts +17 -17
  140. package/dist/types/modules/debug/chunk.d.ts +5 -5
  141. package/dist/types/modules/debug/connectivity.d.ts +7 -7
  142. package/dist/types/modules/debug/settlements.d.ts +5 -5
  143. package/dist/types/modules/debug/stake.d.ts +21 -0
  144. package/dist/types/modules/debug/stamps.d.ts +7 -7
  145. package/dist/types/modules/debug/states.d.ts +7 -7
  146. package/dist/types/modules/debug/status.d.ts +21 -15
  147. package/dist/types/modules/debug/tag.d.ts +3 -3
  148. package/dist/types/modules/debug/transactions.d.ts +5 -5
  149. package/dist/types/modules/feed.d.ts +3 -3
  150. package/dist/types/modules/pinning.d.ts +7 -7
  151. package/dist/types/modules/pss.d.ts +3 -3
  152. package/dist/types/modules/soc.d.ts +2 -2
  153. package/dist/types/modules/status.d.ts +3 -3
  154. package/dist/types/modules/stewardship.d.ts +3 -3
  155. package/dist/types/modules/tag.d.ts +6 -6
  156. package/dist/types/types/debug.d.ts +35 -2
  157. package/dist/types/types/index.d.ts +45 -88
  158. package/dist/types/utils/error.d.ts +0 -26
  159. package/dist/types/utils/eth.d.ts +6 -6
  160. package/dist/types/utils/expose.d.ts +4 -7
  161. package/dist/types/utils/hash.d.ts +1 -1
  162. package/dist/types/utils/headers.d.ts +1 -1
  163. package/dist/types/utils/hex.d.ts +4 -4
  164. package/dist/types/utils/http.d.ts +12 -21
  165. package/dist/types/utils/stream.d.ts +1 -1
  166. package/dist/types/utils/type.d.ts +3 -2
  167. package/dist/types/utils/uint64.d.ts +1 -1
  168. package/package.json +59 -49
  169. package/dist/index.browser.js +0 -33
  170. package/dist/index.browser.js.map +0 -7
  171. package/dist/src/modules/debug/balance.js +0 -57
  172. package/dist/src/modules/debug/chequebook.js +0 -150
  173. package/dist/src/modules/debug/connectivity.js +0 -45
  174. package/dist/src/modules/debug/settlements.js +0 -29
  175. package/dist/src/modules/debug/stamps.js +0 -64
  176. package/dist/src/modules/debug/states.js +0 -46
  177. package/dist/src/modules/debug/tag.js +0 -16
  178. package/dist/src/modules/debug/transactions.js +0 -69
  179. package/dist/src/modules/pinning.js +0 -60
  180. package/dist/src/modules/status.js +0 -12
  181. package/dist/src/modules/stewardship.js +0 -24
  182. package/dist/src/modules/tag.js +0 -77
  183. package/dist/src/utils/error.js +0 -47
  184. package/dist/src/utils/http.js +0 -162
  185. package/dist/src/utils/merge.js +0 -36
  186. package/dist/src/utils/sleep.js +0 -8
  187. package/dist/types/utils/merge.d.ts +0 -8
  188. package/dist/types/utils/sleep.d.ts +0 -6
  189. /package/dist/{src → mjs}/chunk/serialize.js +0 -0
  190. /package/dist/{src → mjs}/feed/type.js +0 -0
  191. /package/dist/{src → mjs}/utils/stamps.js +0 -0
  192. /package/dist/types/utils/{collection-browser.d.ts → collection.browser.d.ts} +0 -0
  193. /package/dist/types/utils/{collection-node.d.ts → collection.node.d.ts} +0 -0
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.normalizeToReadableStream = exports.readableWebToNode = exports.readableNodeToWeb = exports.isNodeReadable = exports.isReadableStream = exports.isReadable = void 0;
4
+ const stream_1 = require("stream");
5
+ const type_1 = require("./type");
6
+ const web_streams_polyfill_1 = require("web-streams-polyfill");
7
+ const NodeReadable = stream_1.Readable || class {
8
+ };
9
+ /**
10
+ * Validates if passed object is either browser's ReadableStream
11
+ * or Node's Readable.
12
+ *
13
+ * @param entry
14
+ */
15
+ function isReadable(entry) {
16
+ return isReadableStream(entry) || isNodeReadable(entry);
17
+ }
18
+ exports.isReadable = isReadable;
19
+ function isReadableStream(entry) {
20
+ if (!(0, type_1.isStrictlyObject)(entry)) {
21
+ return false;
22
+ }
23
+ const browserReadable = entry;
24
+ if (typeof browserReadable.getReader === 'function' &&
25
+ browserReadable.locked !== undefined &&
26
+ typeof browserReadable.cancel === 'function' &&
27
+ typeof browserReadable.pipeTo === 'function' &&
28
+ typeof browserReadable.pipeThrough === 'function') {
29
+ return true;
30
+ }
31
+ return false;
32
+ }
33
+ exports.isReadableStream = isReadableStream;
34
+ function isNodeReadable(entry) {
35
+ if (!(0, type_1.isStrictlyObject)(entry)) {
36
+ return false;
37
+ }
38
+ const nodeReadable = entry;
39
+ if (typeof nodeReadable.pipe === 'function' && nodeReadable.readable && typeof nodeReadable._read === 'function') {
40
+ return true;
41
+ }
42
+ return false;
43
+ }
44
+ exports.isNodeReadable = isNodeReadable;
45
+ /**
46
+ * Function that converts Node's Readable into WHATWG ReadableStream
47
+ *
48
+ * Taken over from https://github.com/gwicke/node-web-streams/blob/master/lib/conversions.js
49
+ * Because it uses forked web-streams-polyfill that are outdated.
50
+ *
51
+ * @author https://github.com/gwicke
52
+ * @licence Apache License 2.0 https://github.com/gwicke/node-web-streams/blob/master/LICENSE
53
+ * @param nodeStream
54
+ */
55
+ function readableNodeToWeb(nodeStream) {
56
+ return new web_streams_polyfill_1.ReadableStream({
57
+ start(controller) {
58
+ nodeStream.pause();
59
+ nodeStream.on('data', chunk => {
60
+ if (Buffer.isBuffer(chunk)) {
61
+ controller.enqueue(new Uint8Array(chunk.buffer));
62
+ }
63
+ else {
64
+ controller.enqueue(chunk);
65
+ }
66
+ nodeStream.pause();
67
+ });
68
+ nodeStream.on('end', () => controller.close());
69
+ nodeStream.on('error', e => controller.error(e));
70
+ },
71
+ pull() {
72
+ nodeStream.resume();
73
+ },
74
+ cancel() {
75
+ nodeStream.pause();
76
+ },
77
+ });
78
+ }
79
+ exports.readableNodeToWeb = readableNodeToWeb;
80
+ /**
81
+ * Taken over from https://github.com/gwicke/node-web-streams/blob/master/lib/conversions.js
82
+ * Because it uses forked web-streams-polyfill that are outdated.
83
+ *
84
+ * @author https://github.com/gwicke
85
+ * @licence Apache License 2.0 https://github.com/gwicke/node-web-streams/blob/master/LICENSE
86
+ */
87
+ class NodeReadableWrapper extends NodeReadable {
88
+ constructor(webStream, options) {
89
+ super(options);
90
+ this._webStream = webStream;
91
+ this._reader = webStream.getReader();
92
+ this._reading = false;
93
+ }
94
+ _read() {
95
+ if (this._reading) {
96
+ return;
97
+ }
98
+ this._reading = true;
99
+ const doRead = () => {
100
+ this._reader.read().then(res => {
101
+ if (res.done) {
102
+ this.push(null);
103
+ return;
104
+ }
105
+ if (this.push(res.value)) {
106
+ return doRead();
107
+ }
108
+ else {
109
+ this._reading = false;
110
+ }
111
+ });
112
+ };
113
+ doRead();
114
+ }
115
+ }
116
+ /**
117
+ * Function that converts WHATWG ReadableStream into Node's Readable
118
+ *
119
+ * Taken over from https://github.com/gwicke/node-web-streams/blob/master/lib/conversions.js
120
+ * Because it uses forked web-streams-polyfill that is outdated.
121
+ *
122
+ * **Warning!**
123
+ * If you want to use this function in browser you have to polyfill `stream` package with your bundler.
124
+ *
125
+ * @author https://github.com/gwicke
126
+ * @licence Apache License 2.0 https://github.com/gwicke/node-web-streams/blob/master/LICENSE
127
+ * @param webStream
128
+ * @param options
129
+ */
130
+ function readableWebToNode(webStream, options) {
131
+ if (!stream_1.Readable) {
132
+ throw new Error("The Node's Readable is not available! If you are running this in browser you have to polyfill 'stream' package!");
133
+ }
134
+ return new NodeReadableWrapper(webStream, options);
135
+ }
136
+ exports.readableWebToNode = readableWebToNode;
137
+ function normalizeToReadableStream(stream) {
138
+ if (isNodeReadable(stream)) {
139
+ return readableNodeToWeb(stream);
140
+ }
141
+ else if (isReadableStream(stream)) {
142
+ return stream;
143
+ }
144
+ throw new TypeError('Passed stream is not Node Readable nor ReadableStream!');
145
+ }
146
+ exports.normalizeToReadableStream = normalizeToReadableStream;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.makeTar = void 0;
7
+ const tar_js_1 = __importDefault(require("tar-js"));
8
+ // converts a string to utf8 Uint8Array and returns it as a string-like
9
+ // object that `tar.append` accepts as path
10
+ function fixUnicodePath(path) {
11
+ const codes = new TextEncoder().encode(path);
12
+ return {
13
+ length: codes.length,
14
+ charCodeAt: index => codes[index],
15
+ };
16
+ }
17
+ function makeTar(data) {
18
+ const tar = new tar_js_1.default();
19
+ for (const entry of data) {
20
+ const path = fixUnicodePath(entry.path);
21
+ tar.append(path, entry.data);
22
+ }
23
+ return tar.out;
24
+ }
25
+ exports.makeTar = makeTar;
@@ -0,0 +1,434 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertTransactionHash = exports.makeTagUid = exports.assertAllTagsOptions = exports.assertFileData = exports.assertData = exports.assertCashoutOptions = exports.assertTransactionOptions = exports.assertPostageBatchOptions = exports.assertPublicKey = exports.assertPssMessageHandler = exports.assertAddressPrefix = exports.assertTag = exports.isTag = exports.assertCollectionUploadOptions = exports.assertFileUploadOptions = exports.assertUploadOptions = exports.assertRequestOptions = exports.assertBatchId = exports.assertAddress = exports.addCidConversionFunction = exports.makeReferenceOrEns = exports.assertReferenceOrEns = exports.assertReference = exports.assertPositiveInteger = exports.assertNonNegativeInteger = exports.assertInteger = exports.assertBoolean = exports.assertStrictlyObject = exports.isError = exports.isStrictlyObject = exports.isObject = exports.isInteger = exports.isUint8Array = void 0;
4
+ const swarm_cid_1 = require("@ethersphere/swarm-cid");
5
+ const types_1 = require("../types");
6
+ const error_1 = require("./error");
7
+ const file_1 = require("./file");
8
+ const hex_1 = require("./hex");
9
+ const stream_1 = require("./stream");
10
+ function isUint8Array(obj) {
11
+ return obj instanceof Uint8Array;
12
+ }
13
+ exports.isUint8Array = isUint8Array;
14
+ function isInteger(value) {
15
+ return ((typeof value === 'string' && /^-?(0|[1-9][0-9]*)$/g.test(value)) ||
16
+ (typeof value === 'number' &&
17
+ value > Number.MIN_SAFE_INTEGER &&
18
+ value < Number.MAX_SAFE_INTEGER &&
19
+ Number.isInteger(value)));
20
+ }
21
+ exports.isInteger = isInteger;
22
+ function isObject(value) {
23
+ return value !== null && typeof value === 'object';
24
+ }
25
+ exports.isObject = isObject;
26
+ /**
27
+ * Generally it is discouraged to use `object` type, but in this case I think
28
+ * it is best to do so as it is possible to easily convert from `object`to other
29
+ * types, which will be usually the case after asserting that the object is
30
+ * strictly object. With for example Record<string, unknown> you have to first
31
+ * cast it to `unknown` which I think bit defeat the purpose.
32
+ *
33
+ * @param value
34
+ */
35
+ // eslint-disable-next-line @typescript-eslint/ban-types
36
+ function isStrictlyObject(value) {
37
+ return isObject(value) && !Array.isArray(value);
38
+ }
39
+ exports.isStrictlyObject = isStrictlyObject;
40
+ /**
41
+ * Asserts if object is Error
42
+ *
43
+ * @param e
44
+ */
45
+ function isError(e) {
46
+ return e instanceof Error;
47
+ }
48
+ exports.isError = isError;
49
+ // eslint-disable-next-line @typescript-eslint/ban-types
50
+ function assertStrictlyObject(value, name = 'value') {
51
+ if (!isStrictlyObject(value)) {
52
+ throw new TypeError(`${name} has to be an object that is not null nor array!`);
53
+ }
54
+ }
55
+ exports.assertStrictlyObject = assertStrictlyObject;
56
+ function assertBoolean(value, name = 'value') {
57
+ if (value !== true && value !== false)
58
+ throw new TypeError(`${name} is not boolean`);
59
+ }
60
+ exports.assertBoolean = assertBoolean;
61
+ function assertInteger(value, name = 'value') {
62
+ if (!isInteger(value))
63
+ throw new TypeError(`${name} is not integer`);
64
+ }
65
+ exports.assertInteger = assertInteger;
66
+ function assertNonNegativeInteger(value, name = 'Value') {
67
+ assertInteger(value, name);
68
+ if (Number(value) < 0)
69
+ throw new error_1.BeeArgumentError(`${name} has to be bigger or equal to zero`, value);
70
+ }
71
+ exports.assertNonNegativeInteger = assertNonNegativeInteger;
72
+ function assertPositiveInteger(value, name = 'Value') {
73
+ assertInteger(value, name);
74
+ if (Number(value) <= 0)
75
+ throw new error_1.BeeArgumentError(`${name} has to be bigger then zero`, value);
76
+ }
77
+ exports.assertPositiveInteger = assertPositiveInteger;
78
+ function assertReference(value) {
79
+ try {
80
+ (0, hex_1.assertHexString)(value, types_1.REFERENCE_HEX_LENGTH);
81
+ }
82
+ catch (e) {
83
+ (0, hex_1.assertHexString)(value, types_1.ENCRYPTED_REFERENCE_HEX_LENGTH);
84
+ }
85
+ }
86
+ exports.assertReference = assertReference;
87
+ function assertReferenceOrEns(value) {
88
+ if (typeof value !== 'string') {
89
+ throw new TypeError('ReferenceOrEns has to be a string!');
90
+ }
91
+ if ((0, hex_1.isHexString)(value)) {
92
+ assertReference(value);
93
+ return;
94
+ }
95
+ /**
96
+ * a.asdf - VALID
97
+ * test.eth - VALID
98
+ * ADAM.ETH - VALID
99
+ * ADAM UHLIR.ETH - INVALID
100
+ * test.whatever.eth - VALID
101
+ * -adg.ets - INVALID
102
+ * adg-.ets - INVALID
103
+ * as-a.com - VALID
104
+ * ethswarm.org - VALID
105
+ * http://asdf.asf - INVALID
106
+ * řš+ýí.šě+ř.čě - VALID
107
+ * tsg.asg?asg - INVALID
108
+ * tsg.asg:1599 - INVALID
109
+ * ethswarm.something- - INVALID
110
+ * ethswarm.-something - INVALID
111
+ * ethswarm.some-thing - VALID
112
+ *
113
+ * The idea of this regex is to match strings that are 1 to 63 characters long and do not start or end with dash character
114
+ *
115
+ * This part matches 2-63 character string that does not start or end with -
116
+ * [^-.\/?:\s][^.\/?:\s]{0,61}[^-.\/?:\s] <regexp1>
117
+ *
118
+ * For 1 character long string we use the part after |
119
+ * [^-.\/?:\s] <regexp2>
120
+ *
121
+ * This is terminated in a group with . character an repeated at least once
122
+ * (<regexp1>|<regexp2>\.)+
123
+ *
124
+ * This covers everything but top level domain which is 2 to 63 characters long so we can just use the <regexp2> again
125
+ * ^(<regexp1>|<regexp2>\.)+<regexp1>$
126
+ */
127
+ const DOMAIN_REGEX = /^(?:(?:[^-.\/?:\s][^.\/?:\s]{0,61}[^-.\/?:\s]|[^-.\/?:\s]{1,2})\.)+[^-.\/?:\s][^.\/?:\s]{0,61}[^-.\/?:\s]$/;
128
+ // We are doing best-effort validation of domain here. The proper way would be to do validation using IDNA UTS64 standard
129
+ // but that would give us high penalty to our dependencies as the library (idna-uts46-hx) that does this validation and translation
130
+ // adds 160kB minified size which is significant. We expects that full validation will be done on Bee side.
131
+ if (!DOMAIN_REGEX.test(value)) {
132
+ throw new TypeError('ReferenceOrEns is not valid Reference, but also not valid ENS domain.');
133
+ }
134
+ }
135
+ exports.assertReferenceOrEns = assertReferenceOrEns;
136
+ /**
137
+ * Function that mainly converts Swarm CID into hex encoded Swarm Reference
138
+ *
139
+ * @param value
140
+ * @param expectedCidType
141
+ */
142
+ function makeReferenceOrEns(value, expectedCidType) {
143
+ if (typeof value !== 'string') {
144
+ throw new TypeError('ReferenceCidOrEns has to be a string!');
145
+ }
146
+ try {
147
+ const result = (0, swarm_cid_1.decodeCid)(value);
148
+ if (result.type !== expectedCidType) {
149
+ throw new error_1.BeeError(`CID was expected to be of type ${expectedCidType}, but got instead ${result.type ?? 'non-Swarm CID'}`);
150
+ }
151
+ return result.reference;
152
+ }
153
+ catch (e) {
154
+ if (e instanceof error_1.BeeError)
155
+ throw e;
156
+ }
157
+ assertReferenceOrEns(value);
158
+ return value;
159
+ }
160
+ exports.makeReferenceOrEns = makeReferenceOrEns;
161
+ /**
162
+ * Function that adds getter which converts the reference into CID base32 encoded string.
163
+ * @param result
164
+ * @param cidType Type as described in the @ethersphere/swarm-cids-js -> ReferenceType
165
+ */
166
+ function addCidConversionFunction(result, cidType) {
167
+ return {
168
+ ...result,
169
+ cid() {
170
+ return (0, swarm_cid_1.encodeReference)(result.reference, cidType).toString();
171
+ },
172
+ };
173
+ }
174
+ exports.addCidConversionFunction = addCidConversionFunction;
175
+ function assertAddress(value) {
176
+ (0, hex_1.assertHexString)(value, types_1.ADDRESS_HEX_LENGTH, 'Address');
177
+ }
178
+ exports.assertAddress = assertAddress;
179
+ function assertBatchId(value) {
180
+ (0, hex_1.assertHexString)(value, types_1.BATCH_ID_HEX_LENGTH, 'BatchId');
181
+ }
182
+ exports.assertBatchId = assertBatchId;
183
+ function assertRequestOptions(value, name = 'RequestOptions') {
184
+ if (value === undefined) {
185
+ return;
186
+ }
187
+ if (!isStrictlyObject(value)) {
188
+ throw new TypeError(`${name} has to be an object!`);
189
+ }
190
+ const options = value;
191
+ if (options.retry) {
192
+ assertNonNegativeInteger(options.retry, `${name}.retry`);
193
+ }
194
+ if (options.timeout) {
195
+ assertNonNegativeInteger(options.timeout, `${name}.timeout`);
196
+ }
197
+ }
198
+ exports.assertRequestOptions = assertRequestOptions;
199
+ function assertUploadOptions(value, name = 'UploadOptions') {
200
+ if (!isStrictlyObject(value)) {
201
+ throw new TypeError(`${name} has to be an object!`);
202
+ }
203
+ assertRequestOptions(value, name);
204
+ const options = value;
205
+ if (options.pin && typeof options.pin !== 'boolean') {
206
+ throw new TypeError(`options.pin property in ${name} has to be boolean or undefined!`);
207
+ }
208
+ if (options.encrypt && typeof options.encrypt !== 'boolean') {
209
+ throw new TypeError(`options.encrypt property in ${name} has to be boolean or undefined!`);
210
+ }
211
+ if (options.tag) {
212
+ if (typeof options.tag !== 'number') {
213
+ throw new TypeError(`options.tag property in ${name} has to be number or undefined!`);
214
+ }
215
+ assertNonNegativeInteger(options.tag, 'options.tag');
216
+ }
217
+ }
218
+ exports.assertUploadOptions = assertUploadOptions;
219
+ function assertFileUploadOptions(value) {
220
+ assertUploadOptions(value, 'FileUploadOptions');
221
+ const options = value;
222
+ if (options.size) {
223
+ if (typeof options.size !== 'number') {
224
+ throw new TypeError('tag property in FileUploadOptions has to be number or undefined!');
225
+ }
226
+ assertNonNegativeInteger(options.size, 'options.size');
227
+ }
228
+ if (options.contentType && typeof options.contentType !== 'string') {
229
+ throw new TypeError('contentType property in FileUploadOptions has to be string or undefined!');
230
+ }
231
+ }
232
+ exports.assertFileUploadOptions = assertFileUploadOptions;
233
+ function assertCollectionUploadOptions(value) {
234
+ assertUploadOptions(value, 'CollectionUploadOptions');
235
+ const options = value;
236
+ if (options.indexDocument && typeof options.indexDocument !== 'string') {
237
+ throw new TypeError('indexDocument property in CollectionUploadOptions has to be string or undefined!');
238
+ }
239
+ if (options.errorDocument && typeof options.errorDocument !== 'string') {
240
+ throw new TypeError('errorDocument property in CollectionUploadOptions has to be string or undefined!');
241
+ }
242
+ }
243
+ exports.assertCollectionUploadOptions = assertCollectionUploadOptions;
244
+ function isTag(value) {
245
+ if (!isStrictlyObject(value)) {
246
+ return false;
247
+ }
248
+ const tag = value;
249
+ const numberProperties = ['total', 'processed', 'synced', 'uid'];
250
+ const correctNumberProperties = numberProperties.every(numberProperty => typeof tag[numberProperty] === 'number');
251
+ if (!correctNumberProperties || !tag.startedAt || typeof tag.startedAt !== 'string') {
252
+ return false;
253
+ }
254
+ return true;
255
+ }
256
+ exports.isTag = isTag;
257
+ function assertTag(value) {
258
+ if (!isStrictlyObject(value)) {
259
+ throw new TypeError('Tag is not an object!');
260
+ }
261
+ const tag = value;
262
+ const numberProperties = ['total', 'processed', 'synced', 'uid'];
263
+ for (const numberProperty of numberProperties) {
264
+ if (!tag[numberProperty]) {
265
+ throw new TypeError(`Tag's property '${numberProperty}' has to be specified!`);
266
+ }
267
+ if (typeof tag[numberProperty] !== 'number') {
268
+ throw new TypeError(`Tag's property '${numberProperty}' has to be number!`);
269
+ }
270
+ }
271
+ if (!tag.startedAt) {
272
+ throw new TypeError("Tag's property 'startedAt' has to be specified!");
273
+ }
274
+ if (typeof tag.startedAt !== 'string') {
275
+ throw new TypeError("Tag's property 'startedAt' has to be string!");
276
+ }
277
+ }
278
+ exports.assertTag = assertTag;
279
+ function assertAddressPrefix(value) {
280
+ (0, hex_1.assertHexString)(value, undefined, 'AddressPrefix');
281
+ if (value.length > types_1.PSS_TARGET_HEX_LENGTH_MAX) {
282
+ throw new error_1.BeeArgumentError(`AddressPrefix must have length of ${types_1.PSS_TARGET_HEX_LENGTH_MAX} at most! Got string with ${value.length}`, value);
283
+ }
284
+ }
285
+ exports.assertAddressPrefix = assertAddressPrefix;
286
+ function assertPssMessageHandler(value) {
287
+ if (!isStrictlyObject(value)) {
288
+ throw new TypeError('PssMessageHandler has to be object!');
289
+ }
290
+ const handler = value;
291
+ if (typeof handler.onMessage !== 'function') {
292
+ throw new TypeError('onMessage property of PssMessageHandler has to be function!');
293
+ }
294
+ if (typeof handler.onError !== 'function') {
295
+ throw new TypeError('onError property of PssMessageHandler has to be function!');
296
+ }
297
+ }
298
+ exports.assertPssMessageHandler = assertPssMessageHandler;
299
+ function assertPublicKey(value) {
300
+ (0, hex_1.assertHexString)(value, types_1.PUBKEY_HEX_LENGTH, 'PublicKey');
301
+ }
302
+ exports.assertPublicKey = assertPublicKey;
303
+ function assertPostageBatchOptions(value) {
304
+ if (value === undefined) {
305
+ return;
306
+ }
307
+ assertStrictlyObject(value);
308
+ const options = value;
309
+ assertRequestOptions(options, 'PostageBatchOptions');
310
+ if (options?.gasPrice) {
311
+ assertNonNegativeInteger(options.gasPrice);
312
+ }
313
+ if (options?.immutableFlag !== undefined) {
314
+ assertBoolean(options.immutableFlag);
315
+ }
316
+ if (options?.waitForUsable !== undefined) {
317
+ assertBoolean(options.waitForUsable);
318
+ }
319
+ if (options?.waitForUsableTimeout !== undefined) {
320
+ assertNonNegativeInteger(options.waitForUsableTimeout, 'options.waitForUsableTimeout');
321
+ }
322
+ }
323
+ exports.assertPostageBatchOptions = assertPostageBatchOptions;
324
+ function assertTransactionOptions(value, name = 'TransactionOptions') {
325
+ if (value === undefined) {
326
+ return;
327
+ }
328
+ assertStrictlyObject(value, name);
329
+ const options = value;
330
+ if (options?.gasLimit) {
331
+ assertNonNegativeInteger(options.gasLimit, name);
332
+ }
333
+ if (options?.gasPrice) {
334
+ assertNonNegativeInteger(options.gasPrice, name);
335
+ }
336
+ }
337
+ exports.assertTransactionOptions = assertTransactionOptions;
338
+ function assertCashoutOptions(value) {
339
+ if (value === undefined) {
340
+ return;
341
+ }
342
+ assertStrictlyObject(value);
343
+ const options = value;
344
+ assertRequestOptions(options, 'CashoutOptions');
345
+ assertTransactionOptions(options, 'CashoutOptions');
346
+ }
347
+ exports.assertCashoutOptions = assertCashoutOptions;
348
+ /**
349
+ * Check whether the given parameter is valid data to upload
350
+ * @param value
351
+ * @throws TypeError if not valid
352
+ */
353
+ function assertData(value) {
354
+ if (typeof value !== 'string' && !(value instanceof Uint8Array)) {
355
+ throw new TypeError('Data must be either string or Uint8Array!');
356
+ }
357
+ }
358
+ exports.assertData = assertData;
359
+ /**
360
+ * Check whether the given parameter is a correct file representation to file upload.
361
+ * @param value
362
+ * @throws TypeError if not valid
363
+ */
364
+ function assertFileData(value) {
365
+ if (typeof value !== 'string' && !(value instanceof Uint8Array) && !(0, file_1.isFile)(value) && !(0, stream_1.isReadable)(value)) {
366
+ throw new TypeError('Data must be either string, Readable, Uint8Array or File!');
367
+ }
368
+ }
369
+ exports.assertFileData = assertFileData;
370
+ /**
371
+ * Checks whether optional options for AllTags query are valid
372
+ * @param options
373
+ */
374
+ function assertAllTagsOptions(entry) {
375
+ if (entry !== undefined && !isStrictlyObject(entry)) {
376
+ throw new TypeError('options has to be an object or undefined!');
377
+ }
378
+ assertRequestOptions(entry, 'AllTagsOptions');
379
+ const options = entry;
380
+ if (options?.limit !== undefined) {
381
+ if (typeof options.limit !== 'number') {
382
+ throw new TypeError('AllTagsOptions.limit has to be a number or undefined!');
383
+ }
384
+ if (options.limit < types_1.TAGS_LIMIT_MIN) {
385
+ throw new error_1.BeeArgumentError(`AllTagsOptions.limit has to be at least ${types_1.TAGS_LIMIT_MIN}`, options.limit);
386
+ }
387
+ if (options.limit > types_1.TAGS_LIMIT_MAX) {
388
+ throw new error_1.BeeArgumentError(`AllTagsOptions.limit has to be at most ${types_1.TAGS_LIMIT_MAX}`, options.limit);
389
+ }
390
+ }
391
+ if (options?.offset !== undefined) {
392
+ assertNonNegativeInteger(options.offset, 'AllTagsOptions.offset');
393
+ }
394
+ }
395
+ exports.assertAllTagsOptions = assertAllTagsOptions;
396
+ /**
397
+ * Utility functions that return Tag UID
398
+ * @param tagUid
399
+ */
400
+ function makeTagUid(tagUid) {
401
+ if (tagUid === undefined || tagUid === null) {
402
+ throw new TypeError('TagUid was expected but got undefined or null instead!');
403
+ }
404
+ if (isTag(tagUid)) {
405
+ return tagUid.uid;
406
+ }
407
+ else if (typeof tagUid === 'number') {
408
+ assertNonNegativeInteger(tagUid, 'UID');
409
+ return tagUid;
410
+ }
411
+ else if (typeof tagUid === 'string') {
412
+ const int = parseInt(tagUid);
413
+ if (isNaN(int)) {
414
+ throw new TypeError('Passed tagUid string is not valid integer!');
415
+ }
416
+ if (int < 0) {
417
+ throw new TypeError(`TagUid was expected to be positive non-negative integer! Got ${int}`);
418
+ }
419
+ return int;
420
+ }
421
+ throw new TypeError('tagUid has to be either Tag or a number (UID)!');
422
+ }
423
+ exports.makeTagUid = makeTagUid;
424
+ function assertTransactionHash(transactionHash) {
425
+ if (typeof transactionHash !== 'string') {
426
+ throw new TypeError('TransactionHash has to be a string!');
427
+ }
428
+ (0, hex_1.assertPrefixedHexString)(transactionHash, 'TransactionHash');
429
+ // Hash is 64 long + '0x' prefix = 66
430
+ if (transactionHash.length !== 66) {
431
+ throw new TypeError('TransactionHash has to be prefixed hex string with total length 66 (prefix including)');
432
+ }
433
+ }
434
+ exports.assertTransactionHash = assertTransactionHash;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.readUint64BigEndian = exports.writeUint64BigEndian = exports.writeUint64LittleEndian = void 0;
4
+ const bytes_1 = require("./bytes");
5
+ // TODO handle bigger values than 32 bit
6
+ // For now it's good enough because we only use these functions
7
+ // sequential feed indexes.
8
+ function writeUint64LittleEndian(value, bytes = (0, bytes_1.makeBytes)(8)) {
9
+ const dataView = new DataView(bytes.buffer);
10
+ const valueLower32 = value & 0xffffffff;
11
+ const littleEndian = true;
12
+ dataView.setUint32(0, valueLower32, littleEndian);
13
+ dataView.setUint32(4, 0, littleEndian);
14
+ return bytes;
15
+ }
16
+ exports.writeUint64LittleEndian = writeUint64LittleEndian;
17
+ function writeUint64BigEndian(value, bytes = (0, bytes_1.makeBytes)(8)) {
18
+ const dataView = new DataView(bytes.buffer);
19
+ const valueLower32 = value & 0xffffffff;
20
+ dataView.setUint32(0, 0);
21
+ dataView.setUint32(4, valueLower32);
22
+ return bytes;
23
+ }
24
+ exports.writeUint64BigEndian = writeUint64BigEndian;
25
+ function readUint64BigEndian(bytes) {
26
+ const dataView = new DataView(bytes.buffer);
27
+ return dataView.getUint32(4);
28
+ }
29
+ exports.readUint64BigEndian = readUint64BigEndian;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.stripLastSlash = exports.assertBeeUrl = exports.isValidBeeUrl = void 0;
4
+ const error_1 = require("./error");
5
+ const type_1 = require("./type");
6
+ function isNodeJsError(e) {
7
+ return (0, type_1.isObject)(e) && typeof e.code === 'string';
8
+ }
9
+ /**
10
+ * Validates that passed string is valid URL of Bee.
11
+ * We support only HTTP and HTTPS protocols.
12
+ *
13
+ * @param url
14
+ */
15
+ function isValidBeeUrl(url) {
16
+ try {
17
+ if (typeof url !== 'string') {
18
+ return false;
19
+ }
20
+ const urlObject = new URL(url);
21
+ // There can be wide range of protocols passed.
22
+ return urlObject.protocol === 'http:' || urlObject.protocol === 'https:';
23
+ }
24
+ catch (e) {
25
+ // URL constructor throws TypeError if not valid URL
26
+ // TODO: Drop the `.code` hack for NodeJS environment: https://github.com/ethersphere/bee-js/issues/204
27
+ if (e instanceof TypeError || (isNodeJsError(e) && e.code === 'ERR_INVALID_URL')) {
28
+ return false;
29
+ }
30
+ throw e;
31
+ }
32
+ }
33
+ exports.isValidBeeUrl = isValidBeeUrl;
34
+ /**
35
+ * Validates that passed string is valid URL of Bee, if not it throws BeeArgumentError.
36
+ * We support only HTTP and HTTPS protocols.
37
+ * @param url
38
+ * @throws BeeArgumentError if non valid URL
39
+ */
40
+ function assertBeeUrl(url) {
41
+ if (!isValidBeeUrl(url)) {
42
+ throw new error_1.BeeArgumentError('URL is not valid!', url);
43
+ }
44
+ }
45
+ exports.assertBeeUrl = assertBeeUrl;
46
+ /**
47
+ * Removes trailing slash out of the given string.
48
+ * @param url
49
+ */
50
+ function stripLastSlash(url) {
51
+ if (url.endsWith('/')) {
52
+ return url.slice(0, -1);
53
+ }
54
+ return url;
55
+ }
56
+ exports.stripLastSlash = stripLastSlash;