@ethersphere/bee-js 3.2.0 → 3.3.2-pre.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.
Files changed (193) hide show
  1. package/dist/cjs/bee-debug.js +615 -0
  2. package/dist/cjs/bee.js +922 -0
  3. package/dist/cjs/chunk/bmt.js +55 -0
  4. package/dist/cjs/chunk/cac.js +56 -0
  5. package/dist/cjs/chunk/serialize.js +19 -0
  6. package/dist/cjs/chunk/signer.js +137 -0
  7. package/dist/cjs/chunk/soc.js +172 -0
  8. package/dist/cjs/chunk/span.js +29 -0
  9. package/dist/cjs/feed/index.js +184 -0
  10. package/dist/cjs/feed/json.js +41 -0
  11. package/dist/cjs/feed/topic.js +25 -0
  12. package/dist/cjs/feed/type.js +15 -0
  13. package/dist/cjs/index.js +35 -0
  14. package/dist/cjs/modules/bytes.js +74 -0
  15. package/dist/cjs/modules/bzz.js +131 -0
  16. package/dist/cjs/modules/chunk.js +58 -0
  17. package/dist/cjs/modules/debug/balance.js +77 -0
  18. package/dist/cjs/modules/debug/chequebook.js +167 -0
  19. package/dist/cjs/modules/debug/chunk.js +51 -0
  20. package/dist/cjs/modules/debug/connectivity.js +75 -0
  21. package/dist/cjs/modules/debug/settlements.js +45 -0
  22. package/dist/cjs/modules/debug/stamps.js +89 -0
  23. package/dist/cjs/modules/debug/states.js +47 -0
  24. package/dist/cjs/modules/debug/status.js +153 -0
  25. package/dist/cjs/modules/debug/tag.js +30 -0
  26. package/dist/cjs/modules/debug/transactions.js +81 -0
  27. package/dist/cjs/modules/feed.js +76 -0
  28. package/dist/cjs/modules/pinning.js +84 -0
  29. package/dist/cjs/modules/pss.js +55 -0
  30. package/dist/cjs/modules/soc.js +40 -0
  31. package/dist/cjs/modules/status.js +26 -0
  32. package/dist/cjs/modules/stewardship.js +41 -0
  33. package/dist/cjs/modules/tag.js +96 -0
  34. package/dist/cjs/package.json +8 -0
  35. package/dist/cjs/types/debug.js +9 -0
  36. package/dist/cjs/types/index.js +46 -0
  37. package/dist/cjs/types/ky-options.js +8 -0
  38. package/dist/cjs/types/ky-universal/common.js +8 -0
  39. package/dist/cjs/types/ky-universal/hooks.js +8 -0
  40. package/dist/cjs/types/ky-universal/retry.js +8 -0
  41. package/dist/cjs/utils/bytes.js +107 -0
  42. package/dist/cjs/utils/collection.browser.js +36 -0
  43. package/dist/cjs/utils/collection.js +70 -0
  44. package/dist/cjs/utils/collection.node.js +115 -0
  45. package/dist/cjs/utils/data.browser.js +78 -0
  46. package/dist/cjs/utils/data.js +60 -0
  47. package/dist/cjs/utils/error.js +50 -0
  48. package/dist/cjs/utils/eth.js +211 -0
  49. package/dist/cjs/utils/expose.js +44 -0
  50. package/dist/cjs/utils/file.js +49 -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 +166 -0
  55. package/dist/cjs/utils/merge.js +34 -0
  56. package/dist/cjs/utils/pss.js +18 -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 +327 -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 +1 -1
  64. package/dist/index.browser.min.js.LICENSE.txt +12 -11
  65. package/dist/index.browser.min.js.map +1 -1
  66. package/dist/mjs/bee-debug.js +609 -0
  67. package/dist/mjs/bee.js +944 -0
  68. package/dist/mjs/chunk/bmt.js +56 -0
  69. package/dist/mjs/chunk/cac.js +52 -0
  70. package/dist/mjs/chunk/serialize.js +15 -0
  71. package/dist/mjs/chunk/signer.js +131 -0
  72. package/dist/mjs/chunk/soc.js +139 -0
  73. package/dist/mjs/chunk/span.js +28 -0
  74. package/dist/mjs/feed/index.js +145 -0
  75. package/dist/mjs/feed/json.js +27 -0
  76. package/dist/mjs/feed/topic.js +21 -0
  77. package/dist/mjs/feed/type.js +10 -0
  78. package/dist/mjs/index.js +7 -0
  79. package/dist/mjs/modules/bytes.js +59 -0
  80. package/dist/mjs/modules/bzz.js +122 -0
  81. package/dist/mjs/modules/chunk.js +45 -0
  82. package/dist/mjs/modules/debug/balance.js +57 -0
  83. package/dist/mjs/modules/debug/chequebook.js +150 -0
  84. package/dist/mjs/modules/debug/chunk.js +35 -0
  85. package/dist/mjs/modules/debug/connectivity.js +45 -0
  86. package/dist/mjs/modules/debug/settlements.js +29 -0
  87. package/dist/mjs/modules/debug/stamps.js +64 -0
  88. package/dist/mjs/modules/debug/states.js +31 -0
  89. package/dist/mjs/modules/debug/status.js +134 -0
  90. package/dist/mjs/modules/debug/tag.js +16 -0
  91. package/dist/mjs/modules/debug/transactions.js +63 -0
  92. package/dist/mjs/modules/feed.js +67 -0
  93. package/dist/mjs/modules/pinning.js +66 -0
  94. package/dist/mjs/modules/pss.js +40 -0
  95. package/dist/mjs/modules/soc.js +31 -0
  96. package/dist/mjs/modules/status.js +12 -0
  97. package/dist/mjs/modules/stewardship.js +24 -0
  98. package/dist/mjs/modules/tag.js +77 -0
  99. package/dist/mjs/package.json +8 -0
  100. package/dist/mjs/types/debug.js +7 -0
  101. package/dist/mjs/types/index.js +37 -0
  102. package/dist/mjs/types/ky-options.js +7 -0
  103. package/dist/mjs/types/ky-universal/common.js +7 -0
  104. package/dist/mjs/types/ky-universal/hooks.js +7 -0
  105. package/dist/mjs/types/ky-universal/retry.js +7 -0
  106. package/dist/mjs/utils/bytes.js +101 -0
  107. package/dist/mjs/utils/collection.browser.js +19 -0
  108. package/dist/mjs/utils/collection.js +64 -0
  109. package/dist/mjs/utils/collection.node.js +74 -0
  110. package/dist/mjs/utils/data.browser.js +73 -0
  111. package/dist/mjs/utils/data.js +43 -0
  112. package/dist/mjs/utils/error.js +56 -0
  113. package/dist/mjs/utils/eth.js +199 -0
  114. package/dist/mjs/utils/expose.js +9 -0
  115. package/dist/mjs/utils/file.js +36 -0
  116. package/dist/mjs/utils/hash.js +17 -0
  117. package/dist/mjs/utils/headers.js +58 -0
  118. package/dist/mjs/utils/hex.js +154 -0
  119. package/dist/mjs/utils/http.js +155 -0
  120. package/dist/mjs/utils/merge.js +36 -0
  121. package/dist/mjs/utils/pss.js +16 -0
  122. package/dist/mjs/utils/stamps.js +17 -0
  123. package/dist/mjs/utils/stream.js +156 -0
  124. package/dist/mjs/utils/tar.js +21 -0
  125. package/dist/mjs/utils/type.js +336 -0
  126. package/dist/mjs/utils/uint64.js +23 -0
  127. package/dist/mjs/utils/url.js +57 -0
  128. package/dist/{src → types}/bee-debug.d.ts +47 -1
  129. package/dist/{src → types}/bee.d.ts +0 -0
  130. package/dist/{src → types}/chunk/bmt.d.ts +0 -0
  131. package/dist/{src → types}/chunk/cac.d.ts +0 -0
  132. package/dist/{src → types}/chunk/serialize.d.ts +0 -0
  133. package/dist/{src → types}/chunk/signer.d.ts +0 -0
  134. package/dist/{src → types}/chunk/soc.d.ts +0 -0
  135. package/dist/{src → types}/chunk/span.d.ts +0 -0
  136. package/dist/{src → types}/feed/index.d.ts +0 -0
  137. package/dist/{src → types}/feed/json.d.ts +0 -0
  138. package/dist/{src → types}/feed/topic.d.ts +0 -0
  139. package/dist/{src → types}/feed/type.d.ts +0 -0
  140. package/dist/{src → types}/index.d.ts +0 -0
  141. package/dist/{src → types}/modules/bytes.d.ts +0 -0
  142. package/dist/{src → types}/modules/bzz.d.ts +0 -0
  143. package/dist/{src → types}/modules/chunk.d.ts +0 -0
  144. package/dist/{src → types}/modules/debug/balance.d.ts +0 -0
  145. package/dist/{src → types}/modules/debug/chequebook.d.ts +0 -0
  146. package/dist/{src → types}/modules/debug/chunk.d.ts +0 -0
  147. package/dist/{src → types}/modules/debug/connectivity.d.ts +0 -0
  148. package/dist/{src → types}/modules/debug/settlements.d.ts +0 -0
  149. package/dist/{src → types}/modules/debug/stamps.d.ts +0 -0
  150. package/dist/{src → types}/modules/debug/states.d.ts +0 -0
  151. package/dist/types/modules/debug/status.d.ts +72 -0
  152. package/dist/{src → types}/modules/debug/tag.d.ts +0 -0
  153. package/dist/{src → types}/modules/debug/transactions.d.ts +0 -0
  154. package/dist/{src → types}/modules/feed.d.ts +0 -0
  155. package/dist/{src → types}/modules/pinning.d.ts +0 -0
  156. package/dist/{src → types}/modules/pss.d.ts +0 -0
  157. package/dist/{src → types}/modules/soc.d.ts +0 -0
  158. package/dist/{src → types}/modules/status.d.ts +0 -0
  159. package/dist/{src → types}/modules/stewardship.d.ts +0 -0
  160. package/dist/{src → types}/modules/tag.d.ts +0 -0
  161. package/dist/{src → types}/types/debug.d.ts +8 -0
  162. package/dist/{src → types}/types/index.d.ts +3 -2
  163. package/dist/types/types/ky-options.d.ts +221 -0
  164. package/dist/types/types/ky-universal/common.d.ts +13 -0
  165. package/dist/types/types/ky-universal/hooks.d.ts +92 -0
  166. package/dist/types/types/ky-universal/retry.d.ts +38 -0
  167. package/dist/{src → types}/utils/bytes.d.ts +0 -0
  168. package/dist/types/utils/collection.browser.d.ts +15 -0
  169. package/dist/{src → types}/utils/collection.d.ts +0 -14
  170. package/dist/types/utils/collection.node.d.ts +15 -0
  171. package/dist/{src → types}/utils/data.browser.d.ts +0 -0
  172. package/dist/{src → types}/utils/data.d.ts +0 -0
  173. package/dist/{src → types}/utils/error.d.ts +0 -0
  174. package/dist/{src → types}/utils/eth.d.ts +0 -0
  175. package/dist/{src → types}/utils/expose.d.ts +2 -1
  176. package/dist/{src → types}/utils/file.d.ts +0 -0
  177. package/dist/{src → types}/utils/hash.d.ts +0 -0
  178. package/dist/{src → types}/utils/headers.d.ts +0 -0
  179. package/dist/{src → types}/utils/hex.d.ts +0 -0
  180. package/dist/{src → types}/utils/http.d.ts +0 -0
  181. package/dist/{src → types}/utils/merge.d.ts +0 -0
  182. package/dist/{src → types}/utils/pss.d.ts +0 -0
  183. package/dist/{src → types}/utils/stamps.d.ts +0 -0
  184. package/dist/{src → types}/utils/stream.d.ts +11 -8
  185. package/dist/{src → types}/utils/tar.d.ts +0 -0
  186. package/dist/{src → types}/utils/type.d.ts +0 -0
  187. package/dist/{src → types}/utils/uint64.d.ts +0 -0
  188. package/dist/{src → types}/utils/url.d.ts +0 -0
  189. package/package.json +55 -36
  190. package/dist/index.min.js +0 -3
  191. package/dist/index.min.js.LICENSE.txt +0 -50
  192. package/dist/index.min.js.map +0 -1
  193. package/dist/src/modules/debug/status.d.ts +0 -24
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.extractUploadHeaders = exports.readFileHeaders = void 0;
4
+ const error_1 = require("./error");
5
+ /**
6
+ * Read the filename from the content-disposition header
7
+ * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
8
+ *
9
+ * @param header the content-disposition header value
10
+ *
11
+ * @returns the filename
12
+ */
13
+ function readContentDispositionFilename(header) {
14
+ if (!header) {
15
+ throw new error_1.BeeError('missing content-disposition header');
16
+ }
17
+ // Regex was found here
18
+ // https://stackoverflow.com/questions/23054475/javascript-regex-for-extracting-filename-from-content-disposition-header
19
+ const dispositionMatch = header.match(/filename\*?=['"]?(?:UTF-\d['"]*)?([^;\r\n"']*)['"]?;?/i);
20
+ if (dispositionMatch && dispositionMatch.length > 0) {
21
+ return dispositionMatch[1];
22
+ }
23
+ throw new error_1.BeeError('invalid content-disposition header');
24
+ }
25
+ function readTagUid(header) {
26
+ if (!header) {
27
+ return undefined;
28
+ }
29
+ return parseInt(header, 10);
30
+ }
31
+ function readFileHeaders(headers) {
32
+ const name = readContentDispositionFilename(headers.get('content-disposition'));
33
+ const tagUid = readTagUid(headers.get('swarm-tag-uid'));
34
+ const contentType = headers.get('content-type') || undefined;
35
+ return {
36
+ name,
37
+ tagUid,
38
+ contentType,
39
+ };
40
+ }
41
+ exports.readFileHeaders = readFileHeaders;
42
+ function extractUploadHeaders(postageBatchId, options) {
43
+ if (!postageBatchId) {
44
+ throw new error_1.BeeError('Postage BatchID has to be specified!');
45
+ }
46
+ const headers = {
47
+ 'swarm-postage-batch-id': postageBatchId,
48
+ };
49
+ if (options === null || options === void 0 ? void 0 : options.pin)
50
+ headers['swarm-pin'] = String(options.pin);
51
+ if (options === null || options === void 0 ? void 0 : options.encrypt)
52
+ headers['swarm-encrypt'] = String(options.encrypt);
53
+ if (options === null || options === void 0 ? void 0 : options.tag)
54
+ headers['swarm-tag'] = String(options.tag);
55
+ if (typeof (options === null || options === void 0 ? void 0 : options.deferred) === 'boolean')
56
+ headers['swarm-deferred-upload'] = options.deferred.toString();
57
+ return headers;
58
+ }
59
+ exports.extractUploadHeaders = extractUploadHeaders;
@@ -0,0 +1,150 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertPrefixedHexString = exports.assertHexString = exports.isPrefixedHexString = exports.isHexString = exports.intToHex = exports.bytesToHex = exports.hexToBytes = exports.makeHexString = void 0;
4
+ const bytes_1 = require("./bytes");
5
+ /**
6
+ * Creates unprefixed hex string from wide range of data.
7
+ *
8
+ * TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
9
+ *
10
+ * @param input
11
+ * @param len of the resulting HexString WITHOUT prefix!
12
+ */
13
+ function makeHexString(input, len) {
14
+ if (typeof input === 'number') {
15
+ return intToHex(input, len);
16
+ }
17
+ if (input instanceof Uint8Array) {
18
+ return bytesToHex(input, len);
19
+ }
20
+ if (typeof input === 'string') {
21
+ if (isPrefixedHexString(input)) {
22
+ const hex = input.slice(2);
23
+ if (len && hex.length !== len) {
24
+ throw new TypeError(`Length mismatch for valid hex string. Expecting length ${len}: ${hex}`);
25
+ }
26
+ return hex;
27
+ }
28
+ else {
29
+ // We use assertHexString() as there might be more reasons why a string is not valid hex string
30
+ // and usage of isHexString() would not give enough information to the user on what is going
31
+ // wrong.
32
+ assertHexString(input, len);
33
+ return input;
34
+ }
35
+ }
36
+ throw new TypeError('Not HexString compatible type!');
37
+ }
38
+ exports.makeHexString = makeHexString;
39
+ /**
40
+ * Converts a hex string to Uint8Array
41
+ *
42
+ * @param hex string input without 0x prefix!
43
+ */
44
+ function hexToBytes(hex) {
45
+ assertHexString(hex);
46
+ const bytes = (0, bytes_1.makeBytes)(hex.length / 2);
47
+ for (let i = 0; i < bytes.length; i++) {
48
+ const hexByte = hex.substr(i * 2, 2);
49
+ bytes[i] = parseInt(hexByte, 16);
50
+ }
51
+ return bytes;
52
+ }
53
+ exports.hexToBytes = hexToBytes;
54
+ /**
55
+ * Converts array of number or Uint8Array to HexString without prefix.
56
+ *
57
+ * @param bytes The input array
58
+ * @param len The length of the non prefixed HexString
59
+ */
60
+ function bytesToHex(bytes, len) {
61
+ const hexByte = (n) => n.toString(16).padStart(2, '0');
62
+ const hex = Array.from(bytes, hexByte).join('');
63
+ // TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
64
+ if (len && hex.length !== len) {
65
+ throw new TypeError(`Resulting HexString does not have expected length ${len}: ${hex}`);
66
+ }
67
+ return hex;
68
+ }
69
+ exports.bytesToHex = bytesToHex;
70
+ /**
71
+ * Converts integer number to hex string.
72
+ *
73
+ * Optionally provides '0x' prefix or padding
74
+ *
75
+ * @param int The positive integer to be converted
76
+ * @param len The length of the non prefixed HexString
77
+ */
78
+ function intToHex(int, len) {
79
+ if (!Number.isInteger(int))
80
+ throw new TypeError('the value provided is not integer');
81
+ if (int > Number.MAX_SAFE_INTEGER)
82
+ throw new TypeError('the value provided exceeds safe integer');
83
+ if (int < 0)
84
+ throw new TypeError('the value provided is a negative integer');
85
+ const hex = int.toString(16);
86
+ // TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
87
+ if (len && hex.length !== len) {
88
+ throw new TypeError(`Resulting HexString does not have expected length ${len}: ${hex}`);
89
+ }
90
+ return hex;
91
+ }
92
+ exports.intToHex = intToHex;
93
+ /**
94
+ * Type guard for HexStrings.
95
+ * Requires no 0x prefix!
96
+ *
97
+ * TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
98
+ *
99
+ * @param s string input
100
+ * @param len expected length of the HexString
101
+ */
102
+ function isHexString(s, len) {
103
+ return typeof s === 'string' && /^[0-9a-f]+$/i.test(s) && (!len || s.length === len);
104
+ }
105
+ exports.isHexString = isHexString;
106
+ /**
107
+ * Type guard for PrefixedHexStrings.
108
+ * Does enforce presence of 0x prefix!
109
+ *
110
+ * @param s string input
111
+ */
112
+ function isPrefixedHexString(s) {
113
+ return typeof s === 'string' && /^0x[0-9a-f]+$/i.test(s);
114
+ }
115
+ exports.isPrefixedHexString = isPrefixedHexString;
116
+ /**
117
+ * Verifies if the provided input is a HexString.
118
+ *
119
+ * TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
120
+ *
121
+ * @param s string input
122
+ * @param len expected length of the HexString
123
+ * @param name optional name for the asserted value
124
+ * @returns HexString or throws error
125
+ */
126
+ function assertHexString(s, len, name = 'value') {
127
+ if (!isHexString(s, len)) {
128
+ if (isPrefixedHexString(s)) {
129
+ throw new TypeError(`${name} not valid non prefixed hex string (has 0x prefix): ${s}`);
130
+ }
131
+ // Don't display length error if no length specified in order not to confuse user
132
+ const lengthMsg = len ? ` of length ${len}` : '';
133
+ throw new TypeError(`${name} not valid hex string${lengthMsg}: ${s}`);
134
+ }
135
+ }
136
+ exports.assertHexString = assertHexString;
137
+ /**
138
+ * Verifies if the provided input is a PrefixedHexString.
139
+ *
140
+ * @param s string input
141
+ * @param len expected length of the HexString
142
+ * @param name optional name for the asserted value
143
+ * @returns HexString or throws error
144
+ */
145
+ function assertPrefixedHexString(s, name = 'value') {
146
+ if (!isPrefixedHexString(s)) {
147
+ throw new TypeError(`${name} not valid prefixed hex string: ${s}`);
148
+ }
149
+ }
150
+ exports.assertPrefixedHexString = assertPrefixedHexString;
@@ -0,0 +1,166 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __rest = (this && this.__rest) || function (s, e) {
12
+ var t = {};
13
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
14
+ t[p] = s[p];
15
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
16
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
17
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
18
+ t[p[i]] = s[p[i]];
19
+ }
20
+ return t;
21
+ };
22
+ var __importDefault = (this && this.__importDefault) || function (mod) {
23
+ return (mod && mod.__esModule) ? mod : { "default": mod };
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.makeDefaultKy = exports.http = exports.filterHeaders = exports.wrapResponseClosure = exports.wrapRequestClosure = void 0;
27
+ const error_1 = require("./error");
28
+ const ky_universal_1 = __importDefault(require("ky-universal"));
29
+ const stream_1 = require("./stream");
30
+ const merge_1 = require("./merge");
31
+ const type_1 = require("./type");
32
+ const DEFAULT_KY_CONFIG = {
33
+ headers: {
34
+ accept: 'application/json, text/plain, */*',
35
+ 'user-agent': `bee-js`,
36
+ },
37
+ };
38
+ function isHttpError(e) {
39
+ return (0, type_1.isObject)(e) && typeof e.response !== 'undefined';
40
+ }
41
+ function isHttpRequestError(e) {
42
+ return (0, type_1.isObject)(e) && typeof e.request !== 'undefined';
43
+ }
44
+ function headersToObject(header) {
45
+ return [...header.entries()].reduce((obj, [key, val]) => {
46
+ obj[key] = val;
47
+ return obj;
48
+ }, {});
49
+ }
50
+ function wrapRequest(request) {
51
+ return {
52
+ url: request.url,
53
+ method: request.method.toUpperCase(),
54
+ headers: headersToObject(request.headers),
55
+ };
56
+ }
57
+ function wrapRequestClosure(cb) {
58
+ return (request) => __awaiter(this, void 0, void 0, function* () {
59
+ yield cb(wrapRequest(request));
60
+ });
61
+ }
62
+ exports.wrapRequestClosure = wrapRequestClosure;
63
+ function wrapResponseClosure(cb) {
64
+ return (request, options, response) => __awaiter(this, void 0, void 0, function* () {
65
+ yield cb({
66
+ headers: headersToObject(response.headers),
67
+ status: response.status,
68
+ statusText: response.statusText,
69
+ request: wrapRequest(request),
70
+ });
71
+ });
72
+ }
73
+ exports.wrapResponseClosure = wrapResponseClosure;
74
+ /**
75
+ * Filters out entries that has undefined value from headers object.
76
+ * Modifies the original object!
77
+ *
78
+ * @param obj
79
+ */
80
+ // eslint-disable-next-line @typescript-eslint/ban-types
81
+ function filterHeaders(obj) {
82
+ if (obj === undefined) {
83
+ return undefined;
84
+ }
85
+ (0, type_1.isStrictlyObject)(obj);
86
+ const typedObj = obj;
87
+ for (const key in typedObj) {
88
+ if (typedObj[key] === undefined) {
89
+ delete typedObj[key];
90
+ }
91
+ }
92
+ if (Object.keys(typedObj).length === 0) {
93
+ return undefined;
94
+ }
95
+ return typedObj;
96
+ }
97
+ exports.filterHeaders = filterHeaders;
98
+ /**
99
+ * Main utility function to make HTTP requests.
100
+ * @param ky
101
+ * @param config
102
+ */
103
+ function http(ky, config) {
104
+ return __awaiter(this, void 0, void 0, function* () {
105
+ try {
106
+ const { path, responseType } = config, kyConfig = __rest(config, ["path", "responseType"]);
107
+ const response = (yield ky(path, Object.assign(Object.assign({}, kyConfig), { searchParams: filterHeaders(kyConfig.searchParams) })));
108
+ switch (responseType) {
109
+ case 'stream':
110
+ if (!response.body) {
111
+ throw new error_1.BeeError('Response was expected to get data but did not get any!');
112
+ }
113
+ response.data = (0, stream_1.normalizeToReadableStream)(response.body);
114
+ break;
115
+ case 'arraybuffer':
116
+ response.data = (yield response.arrayBuffer());
117
+ break;
118
+ case 'json':
119
+ try {
120
+ response.data = (yield response.json());
121
+ }
122
+ catch (e) {
123
+ throw new error_1.BeeNotAJsonError();
124
+ }
125
+ break;
126
+ default:
127
+ break; // If responseType is not set, then no data are expected
128
+ }
129
+ return response;
130
+ }
131
+ catch (e) {
132
+ // Passthrough thrown errors
133
+ if (e instanceof error_1.BeeNotAJsonError) {
134
+ throw e;
135
+ }
136
+ if (isHttpError(e)) {
137
+ let message;
138
+ // We store the response body here as it can be read only once in Response's lifecycle so to make it exposed
139
+ // to the user in the BeeResponseError, for further analysis.
140
+ const body = yield e.response.text();
141
+ try {
142
+ // The response can be Bee's JSON with structure `{code, message}` lets try to parse it
143
+ message = JSON.parse(body).message;
144
+ }
145
+ catch (e) { }
146
+ if (message) {
147
+ throw new error_1.BeeResponseError(e.response.status, e.response, body, config, `${e.response.statusText}: ${message}`);
148
+ }
149
+ else {
150
+ throw new error_1.BeeResponseError(e.response.status, e.response, body, config, e.response.statusText);
151
+ }
152
+ }
153
+ else if (isHttpRequestError(e)) {
154
+ throw new error_1.BeeRequestError(e.message, config);
155
+ }
156
+ else {
157
+ throw new error_1.BeeError(e.message);
158
+ }
159
+ }
160
+ });
161
+ }
162
+ exports.http = http;
163
+ function makeDefaultKy(kyConfig) {
164
+ return ky_universal_1.default.create((0, merge_1.deepMerge)(DEFAULT_KY_CONFIG, kyConfig));
165
+ }
166
+ exports.makeDefaultKy = makeDefaultKy;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.deepMerge = void 0;
4
+ const type_1 = require("./type");
5
+ /**
6
+ * Function that deep merges objects
7
+ *
8
+ * @copyright https://github.com/sindresorhus/ky/blob/b3c9e88fa49d50150dbb6e6b771b4af56cb40c98/source/utils/merge.ts
9
+ * @licence MIT
10
+ * @param sources
11
+ */
12
+ function deepMerge(...sources) {
13
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
+ let returnValue = {};
15
+ for (const source of sources) {
16
+ if (Array.isArray(source)) {
17
+ if (!Array.isArray(returnValue)) {
18
+ returnValue = [];
19
+ }
20
+ returnValue = [...returnValue, ...source];
21
+ }
22
+ else if ((0, type_1.isObject)(source)) {
23
+ // eslint-disable-next-line prefer-const
24
+ for (let [key, value] of Object.entries(source)) {
25
+ if ((0, type_1.isObject)(value) && key in returnValue) {
26
+ value = deepMerge(returnValue[key], value);
27
+ }
28
+ returnValue = Object.assign(Object.assign({}, returnValue), { [key]: value });
29
+ }
30
+ }
31
+ }
32
+ return returnValue;
33
+ }
34
+ exports.deepMerge = deepMerge;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.makeMaxTarget = void 0;
4
+ const types_1 = require("../types");
5
+ /**
6
+ * Utility function that for given strings/reference takes the most specific
7
+ * target that Bee node will except.
8
+ *
9
+ * @param target is a non-prefixed hex string Bee address
10
+ * @see [Bee docs - PSS](https://docs.ethswarm.org/docs/dapps-on-swarm/pss)
11
+ */
12
+ function makeMaxTarget(target) {
13
+ if (typeof target !== 'string') {
14
+ throw new TypeError('target has to be an string!');
15
+ }
16
+ return target.slice(0, types_1.PSS_TARGET_HEX_LENGTH_MAX);
17
+ }
18
+ exports.makeMaxTarget = makeMaxTarget;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getStampUsage = void 0;
4
+ /**
5
+ * Utility function that calculates usage of postage batch based on its utilization, depth and bucket depth.
6
+ *
7
+ * Be aware for small depths (17, 18) this does not provide that much information as the provided set of distinct values
8
+ * is small.
9
+ *
10
+ * @param utilization
11
+ * @param depth
12
+ * @param bucketDepth
13
+ */
14
+ function getStampUsage({ utilization, depth, bucketDepth }) {
15
+ return utilization / Math.pow(2, depth - bucketDepth);
16
+ }
17
+ exports.getStampUsage = getStampUsage;
@@ -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;