@ethersphere/bee-js 8.2.0 → 8.3.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.
package/README.md CHANGED
@@ -13,7 +13,7 @@
13
13
 
14
14
  > Write your code in CJS, MJS or TypeScript.
15
15
 
16
- > Intended to be used with Bee version 2.1.0.
16
+ > Intended to be used with Bee version 2.2.0.
17
17
 
18
18
  ## Quick start
19
19
 
@@ -67,6 +67,20 @@ Loading this module through a script tag will make the `BeeJs` object available
67
67
 
68
68
  ## Usage
69
69
 
70
+ ### Upload via Swarm Gateway
71
+
72
+ ```js
73
+ import { Bee, NULL_STAMP, SWARM_GATEWAY_URL } from '@ethersphere/bee-js'
74
+
75
+ main()
76
+
77
+ async function main() {
78
+ const bee = new Bee(SWARM_GATEWAY_URL)
79
+ const { reference } = await bee.uploadData(NULL_STAMP, 'Hello, World!')
80
+ console.log(reference)
81
+ }
82
+ ```
83
+
70
84
  ### Create or select an existing postage batch
71
85
 
72
86
  Swarm incentivizes nodes in the network to store content, therefor all uploads require a paid
@@ -144,6 +158,18 @@ const bee = new Bee('http://localhost:1633')
144
158
  const uploadResult = await bee.uploadFilesFromDirectory(batchId, './path/to/gallery/')
145
159
  ```
146
160
 
161
+ ### Customize http/https agent and headers
162
+
163
+ ```js
164
+ const bee = new Bee('http://localhost:1633', {
165
+ httpAgent: new http.Agent({ keepAlive: true }),
166
+ httpsAgent: new https.Agent({ keepAlive: true }),
167
+ headers: {
168
+ Authorization: 'Basic ' + Buffer.from('username:password').toString('base64'),
169
+ },
170
+ })
171
+ ```
172
+
147
173
  [**Check out our examples repo for some more ideas on how to use `bee-js`**](https://github.com/ethersphere/examples-js)
148
174
 
149
175
  ## Documentation
package/dist/cjs/bee.js CHANGED
@@ -24,7 +24,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.Bee = void 0;
27
- const swarm_cid_1 = require("@ethersphere/swarm-cid");
28
27
  const cafe_utility_1 = require("cafe-utility");
29
28
  const signer_1 = require("./chunk/signer");
30
29
  const soc_1 = require("./chunk/soc");
@@ -89,6 +88,8 @@ class Bee {
89
88
  timeout: options?.timeout ?? false,
90
89
  headers: options?.headers,
91
90
  onRequest: options?.onRequest,
91
+ httpAgent: options?.httpAgent,
92
+ httpsAgent: options?.httpsAgent,
92
93
  };
93
94
  }
94
95
  /**
@@ -265,16 +266,16 @@ class Bee {
265
266
  const fileName = name ?? data.name;
266
267
  const contentType = data.type;
267
268
  const fileOptions = { contentType, ...options };
268
- return (0, type_2.addCidConversionFunction)(await bzz.uploadFile(this.getRequestOptionsForCall(requestOptions), fileData, postageBatchId, fileName, fileOptions), swarm_cid_1.ReferenceType.MANIFEST);
269
+ return (0, type_2.addCidConversionFunction)(await bzz.uploadFile(this.getRequestOptionsForCall(requestOptions), fileData, postageBatchId, fileName, fileOptions), 'manifest');
269
270
  }
270
271
  else if ((0, type_2.isReadable)(data) && options?.tag && !options.size) {
271
272
  // TODO: Needed until https://github.com/ethersphere/bee/issues/2317 is resolved
272
273
  const result = await bzz.uploadFile(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, name, options);
273
274
  await this.updateTag(options.tag, result.reference);
274
- return (0, type_2.addCidConversionFunction)(result, swarm_cid_1.ReferenceType.MANIFEST);
275
+ return (0, type_2.addCidConversionFunction)(result, 'manifest');
275
276
  }
276
277
  else {
277
- return (0, type_2.addCidConversionFunction)(await bzz.uploadFile(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, name, options), swarm_cid_1.ReferenceType.MANIFEST);
278
+ return (0, type_2.addCidConversionFunction)(await bzz.uploadFile(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, name, options), 'manifest');
278
279
  }
279
280
  }
280
281
  /**
@@ -291,7 +292,7 @@ class Bee {
291
292
  */
292
293
  async downloadFile(reference, path = '', options) {
293
294
  (0, type_2.assertRequestOptions)(options);
294
- reference = (0, type_2.makeReferenceOrEns)(reference, swarm_cid_1.ReferenceType.MANIFEST);
295
+ reference = (0, type_2.makeReferenceOrEns)(reference, 'manifest');
295
296
  return bzz.downloadFile(this.getRequestOptionsForCall(options), reference, path);
296
297
  }
297
298
  /**
@@ -308,7 +309,7 @@ class Bee {
308
309
  */
309
310
  async downloadReadableFile(reference, path = '', options) {
310
311
  (0, type_2.assertRequestOptions)(options);
311
- reference = (0, type_2.makeReferenceOrEns)(reference, swarm_cid_1.ReferenceType.MANIFEST);
312
+ reference = (0, type_2.makeReferenceOrEns)(reference, 'manifest');
312
313
  return bzz.downloadFileReadable(this.getRequestOptionsForCall(options), reference, path);
313
314
  }
314
315
  /**
@@ -334,7 +335,7 @@ class Bee {
334
335
  (0, type_2.assertCollectionUploadOptions)(options);
335
336
  }
336
337
  const data = await (0, collection_1.makeCollectionFromFileList)(fileList);
337
- return (0, type_2.addCidConversionFunction)(await bzz.uploadCollection(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, options), swarm_cid_1.ReferenceType.MANIFEST);
338
+ return (0, type_2.addCidConversionFunction)(await bzz.uploadCollection(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, options), 'manifest');
338
339
  }
339
340
  /**
340
341
  * Upload Collection that you can assembly yourself.
@@ -352,7 +353,7 @@ class Bee {
352
353
  (0, collection_1.assertCollection)(collection);
353
354
  if (options)
354
355
  (0, type_2.assertCollectionUploadOptions)(options);
355
- return (0, type_2.addCidConversionFunction)(await bzz.uploadCollection(this.getRequestOptionsForCall(this.requestOptions), collection, postageBatchId, options), swarm_cid_1.ReferenceType.MANIFEST);
356
+ return (0, type_2.addCidConversionFunction)(await bzz.uploadCollection(this.getRequestOptionsForCall(this.requestOptions), collection, postageBatchId, options), 'manifest');
356
357
  }
357
358
  /**
358
359
  * Upload collection of files.
@@ -376,7 +377,7 @@ class Bee {
376
377
  if (options)
377
378
  (0, type_2.assertCollectionUploadOptions)(options);
378
379
  const data = await (0, collection_node_1.makeCollectionFromFS)(dir);
379
- return (0, type_2.addCidConversionFunction)(await bzz.uploadCollection(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, options), swarm_cid_1.ReferenceType.MANIFEST);
380
+ return (0, type_2.addCidConversionFunction)(await bzz.uploadCollection(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, options), 'manifest');
380
381
  }
381
382
  /**
382
383
  * Create a new Tag which is meant for tracking progres of syncing data across network.
@@ -743,7 +744,7 @@ class Bee {
743
744
  const canonicalTopic = (0, topic_1.makeTopic)(topic);
744
745
  const canonicalOwner = (0, eth_1.makeHexEthAddress)(owner);
745
746
  const reference = await (0, feed_2.createFeedManifest)(this.getRequestOptionsForCall(options), canonicalOwner, canonicalTopic, stamp);
746
- return (0, type_2.addCidConversionFunction)({ reference }, swarm_cid_1.ReferenceType.FEED);
747
+ return (0, type_2.addCidConversionFunction)({ reference }, 'feed');
747
748
  }
748
749
  /**
749
750
  * Make a new feed reader for downloading feed updates.
package/dist/cjs/index.js CHANGED
@@ -33,5 +33,6 @@ var status_1 = require("./modules/debug/status");
33
33
  Object.defineProperty(exports, "SUPPORTED_BEE_VERSION", { enumerable: true, get: function () { return status_1.SUPPORTED_BEE_VERSION; } });
34
34
  Object.defineProperty(exports, "SUPPORTED_BEE_VERSION_EXACT", { enumerable: true, get: function () { return status_1.SUPPORTED_BEE_VERSION_EXACT; } });
35
35
  __exportStar(require("./types"), exports);
36
+ __exportStar(require("./utils/constants"), exports);
36
37
  __exportStar(require("./utils/error"), exports);
37
38
  exports.Utils = __importStar(require("./utils/expose"));
@@ -13,7 +13,7 @@ async function getGrantees(reference, requestOptions) {
13
13
  return {
14
14
  status: response.status,
15
15
  statusText: response.statusText,
16
- data: response.data.data,
16
+ data: response.data,
17
17
  };
18
18
  }
19
19
  exports.getGrantees = getGrantees;
@@ -21,7 +21,7 @@ async function createGrantees(requestOptions, postageBatchId, grantees) {
21
21
  const response = await (0, http_1.http)(requestOptions, {
22
22
  method: 'post',
23
23
  url: granteeEndpoint,
24
- data: { grantees: grantees },
24
+ data: { grantees },
25
25
  headers: {
26
26
  ...(0, headers_1.extractRedundantUploadHeaders)(postageBatchId),
27
27
  },
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertCidToReference = exports.convertReferenceToCid = exports.SWARM_FEED_CODEC = exports.SWARM_MANIFEST_CODEC = void 0;
4
+ const cafe_utility_1 = require("cafe-utility");
5
+ exports.SWARM_MANIFEST_CODEC = 0xfa;
6
+ exports.SWARM_FEED_CODEC = 0xfb;
7
+ const CODEC_TABLE = {
8
+ [exports.SWARM_MANIFEST_CODEC]: 'manifest',
9
+ [exports.SWARM_FEED_CODEC]: 'feed',
10
+ };
11
+ function convertReferenceToCid(reference, type) {
12
+ if (reference.length !== 64) {
13
+ throw TypeError('Only 32-byte, non-encrypted references are supported');
14
+ }
15
+ const base32 = 'b';
16
+ const version = new Uint8Array([1]);
17
+ const codec = new Uint8Array([type === 'feed' ? exports.SWARM_FEED_CODEC : exports.SWARM_MANIFEST_CODEC]);
18
+ const unknown = new Uint8Array([1]);
19
+ const sha256 = new Uint8Array([27]);
20
+ const size = new Uint8Array([32]);
21
+ const header = cafe_utility_1.Binary.uint8ArrayToBase32(cafe_utility_1.Binary.concatBytes(version, codec, unknown, sha256, size)).replace(/\=+$/, '');
22
+ const hash = cafe_utility_1.Binary.uint8ArrayToBase32(cafe_utility_1.Binary.hexToUint8Array(reference)).replace(/\=+$/, '');
23
+ return `${base32}${header}${hash}`.toLowerCase();
24
+ }
25
+ exports.convertReferenceToCid = convertReferenceToCid;
26
+ function convertCidToReference(cid) {
27
+ const bytes = cafe_utility_1.Binary.base32ToUint8Array(cid.toUpperCase().slice(1));
28
+ const codec = bytes[1];
29
+ if (!CODEC_TABLE[codec]) {
30
+ throw new Error('Unknown codec');
31
+ }
32
+ const reference = bytes.slice(-32);
33
+ return {
34
+ type: CODEC_TABLE[codec],
35
+ reference: cafe_utility_1.Binary.uint8ArrayToHex(reference),
36
+ };
37
+ }
38
+ exports.convertCidToReference = convertCidToReference;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NULL_TOPIC = exports.NULL_STAMP = exports.SWARM_GATEWAY_URL = void 0;
4
+ exports.SWARM_GATEWAY_URL = 'https://api.gateway.ethswarm.org';
5
+ exports.NULL_STAMP = '0000000000000000000000000000000000000000000000000000000000000000';
6
+ exports.NULL_TOPIC = '0000000000000000000000000000000000000000000000000000000000000000';
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NULL_TOPIC = exports.NULL_STAMP = exports.getRedundancyStats = exports.getRedundancyStat = exports.approximateOverheadForRedundancyLevel = exports.getStampUsage = exports.getStampTtlSeconds = exports.getStampMaximumCapacityBytes = exports.getStampEffectiveBytes = exports.getStampCostInPlur = exports.getStampCostInBzz = exports.getDepthForCapacity = exports.getAmountForTtl = exports.makeMaxTarget = exports.keccak256Hash = exports.toLittleEndian = exports.makeHexEthAddress = exports.makeEthereumWalletSigner = exports.makeEthAddress = exports.isHexEthAddress = exports.fromLittleEndian = exports.ethToSwarmAddress = exports.capitalizeAddressERC55 = exports.makeHexString = exports.isHexString = exports.intToHex = exports.hexToBytes = exports.bytesToHex = exports.assertPrefixedHexString = exports.assertHexString = exports.isFlexBytes = exports.isBytes = exports.flexBytesAtOffset = exports.bytesEqual = exports.bytesAtOffset = exports.assertFlexBytes = exports.assertBytes = exports.getFolderSize = exports.getCollectionSize = void 0;
3
+ exports.convertReferenceToCid = exports.convertCidToReference = exports.getRedundancyStats = exports.getRedundancyStat = exports.approximateOverheadForRedundancyLevel = exports.getStampUsage = exports.getStampTtlSeconds = exports.getStampMaximumCapacityBytes = exports.getStampEffectiveBytes = exports.getStampCostInPlur = exports.getStampCostInBzz = exports.getDepthForCapacity = exports.getAmountForTtl = exports.makeMaxTarget = exports.keccak256Hash = exports.toLittleEndian = exports.makeHexEthAddress = exports.makeEthereumWalletSigner = exports.makeEthAddress = exports.isHexEthAddress = exports.fromLittleEndian = exports.ethToSwarmAddress = exports.capitalizeAddressERC55 = exports.makeHexString = exports.isHexString = exports.intToHex = exports.hexToBytes = exports.bytesToHex = exports.assertPrefixedHexString = exports.assertHexString = exports.isFlexBytes = exports.isBytes = exports.flexBytesAtOffset = exports.bytesEqual = exports.bytesAtOffset = exports.assertFlexBytes = exports.assertBytes = exports.getFolderSize = exports.getCollectionSize = void 0;
4
4
  var collection_1 = require("./collection");
5
5
  Object.defineProperty(exports, "getCollectionSize", { enumerable: true, get: function () { return collection_1.getCollectionSize; } });
6
6
  var collection_node_1 = require("./collection.node");
@@ -47,5 +47,6 @@ var redundancy_1 = require("./redundancy");
47
47
  Object.defineProperty(exports, "approximateOverheadForRedundancyLevel", { enumerable: true, get: function () { return redundancy_1.approximateOverheadForRedundancyLevel; } });
48
48
  Object.defineProperty(exports, "getRedundancyStat", { enumerable: true, get: function () { return redundancy_1.getRedundancyStat; } });
49
49
  Object.defineProperty(exports, "getRedundancyStats", { enumerable: true, get: function () { return redundancy_1.getRedundancyStats; } });
50
- exports.NULL_STAMP = '0000000000000000000000000000000000000000000000000000000000000000';
51
- exports.NULL_TOPIC = '0000000000000000000000000000000000000000000000000000000000000000';
50
+ var cid_1 = require("./cid");
51
+ Object.defineProperty(exports, "convertCidToReference", { enumerable: true, get: function () { return cid_1.convertCidToReference; } });
52
+ Object.defineProperty(exports, "convertReferenceToCid", { enumerable: true, get: function () { return cid_1.convertReferenceToCid; } });
@@ -25,11 +25,6 @@ async function http(options, config) {
25
25
  const requestConfig = cafe_utility_1.Objects.deepMerge3(exports.DEFAULT_HTTP_CONFIG, config, options);
26
26
  maybeRunOnRequestHook(options, requestConfig);
27
27
  const response = await (0, axios_1.default)(requestConfig);
28
- // Axios does not parse array of strings as JSON
29
- if (Array.isArray(response.data) && response.data.every(element => typeof element === 'string')) {
30
- const array = response.data;
31
- response.data = { data: array };
32
- }
33
28
  // TODO: https://github.com/axios/axios/pull/6253
34
29
  return response;
35
30
  }
@@ -15,6 +15,9 @@ class TarStream {
15
15
  });
16
16
  }
17
17
  beginFile(path, size) {
18
+ if (path.length > 100) {
19
+ throw new Error(`File name too long: ${path}`);
20
+ }
18
21
  const header = createHeader(path, size);
19
22
  this.pieces.push(header);
20
23
  this.currentFileSize = 0;
@@ -8,6 +8,9 @@ class TarStream {
8
8
  this.currentFileSize = 0;
9
9
  }
10
10
  beginFile(path, size) {
11
+ if (path.length > 100) {
12
+ throw new Error(`File name too long: ${path}`);
13
+ }
11
14
  const header = createHeader(path, size);
12
15
  this.output.write(header);
13
16
  this.currentFileSize = 0;
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
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.isReadable = void 0;
4
- const swarm_cid_1 = require("@ethersphere/swarm-cid");
5
4
  const stream_1 = require("stream");
6
5
  const types_1 = require("../types");
6
+ const cid_1 = require("./cid");
7
7
  const error_1 = require("./error");
8
8
  const file_1 = require("./file");
9
9
  const hex_1 = require("./hex");
@@ -134,7 +134,7 @@ function makeReferenceOrEns(value, expectedCidType) {
134
134
  throw new TypeError('ReferenceCidOrEns has to be a string!');
135
135
  }
136
136
  try {
137
- const result = (0, swarm_cid_1.decodeCid)(value);
137
+ const result = (0, cid_1.convertCidToReference)(value);
138
138
  if (result.type !== expectedCidType) {
139
139
  throw new error_1.BeeError(`CID was expected to be of type ${expectedCidType}, but got instead ${result.type ?? 'non-Swarm CID'}`);
140
140
  }
@@ -151,13 +151,13 @@ exports.makeReferenceOrEns = makeReferenceOrEns;
151
151
  /**
152
152
  * Function that adds getter which converts the reference into CID base32 encoded string.
153
153
  * @param result
154
- * @param cidType Type as described in the @ethersphere/swarm-cids-js -> ReferenceType
154
+ * @param cidType feed or manifest
155
155
  */
156
156
  function addCidConversionFunction(result, cidType) {
157
157
  return {
158
158
  ...result,
159
159
  cid() {
160
- return (0, swarm_cid_1.encodeReference)(result.reference, cidType).toString();
160
+ return (0, cid_1.convertReferenceToCid)(result.reference, cidType);
161
161
  },
162
162
  };
163
163
  }