@ethersphere/bee-js 8.1.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.
Files changed (55) hide show
  1. package/README.md +27 -1
  2. package/dist/cjs/bee.js +30 -16
  3. package/dist/cjs/chunk/soc.js +4 -6
  4. package/dist/cjs/index.js +1 -0
  5. package/dist/cjs/modules/bytes.js +18 -1
  6. package/dist/cjs/modules/chunk.js +4 -4
  7. package/dist/cjs/modules/envelope.js +23 -0
  8. package/dist/cjs/modules/feed.js +2 -2
  9. package/dist/cjs/modules/grantee.js +2 -2
  10. package/dist/cjs/modules/soc.js +2 -2
  11. package/dist/cjs/utils/cid.js +38 -0
  12. package/dist/cjs/utils/constants.js +6 -0
  13. package/dist/cjs/utils/expose.js +4 -1
  14. package/dist/cjs/utils/headers.js +11 -6
  15. package/dist/cjs/utils/http.js +0 -5
  16. package/dist/cjs/utils/stamps.js +22 -1
  17. package/dist/cjs/utils/tar.browser.js +3 -0
  18. package/dist/cjs/utils/tar.js +3 -0
  19. package/dist/cjs/utils/type.js +5 -9
  20. package/dist/index.browser.min.js +1 -1
  21. package/dist/index.browser.min.js.map +1 -1
  22. package/dist/mjs/bee.js +31 -17
  23. package/dist/mjs/chunk/soc.js +4 -6
  24. package/dist/mjs/index.js +1 -0
  25. package/dist/mjs/modules/bytes.js +16 -0
  26. package/dist/mjs/modules/chunk.js +4 -4
  27. package/dist/mjs/modules/envelope.js +33 -0
  28. package/dist/mjs/modules/feed.js +2 -2
  29. package/dist/mjs/modules/grantee.js +2 -2
  30. package/dist/mjs/modules/soc.js +2 -2
  31. package/dist/mjs/utils/cid.js +33 -0
  32. package/dist/mjs/utils/constants.js +3 -0
  33. package/dist/mjs/utils/expose.js +2 -1
  34. package/dist/mjs/utils/headers.js +10 -6
  35. package/dist/mjs/utils/http.js +0 -7
  36. package/dist/mjs/utils/stamps.js +19 -0
  37. package/dist/mjs/utils/tar.browser.js +3 -0
  38. package/dist/mjs/utils/tar.js +3 -0
  39. package/dist/mjs/utils/type.js +4 -7
  40. package/dist/types/bee.d.ts +10 -3
  41. package/dist/types/chunk/soc.d.ts +2 -2
  42. package/dist/types/index.d.ts +1 -0
  43. package/dist/types/modules/bytes.d.ts +8 -1
  44. package/dist/types/modules/chunk.d.ts +3 -3
  45. package/dist/types/modules/envelope.d.ts +2 -0
  46. package/dist/types/modules/feed.d.ts +1 -1
  47. package/dist/types/modules/soc.d.ts +1 -1
  48. package/dist/types/types/index.d.ts +12 -8
  49. package/dist/types/utils/cid.d.ts +9 -0
  50. package/dist/types/utils/constants.d.ts +4 -0
  51. package/dist/types/utils/expose.d.ts +1 -0
  52. package/dist/types/utils/headers.d.ts +1 -1
  53. package/dist/types/utils/stamps.d.ts +3 -1
  54. package/dist/types/utils/type.d.ts +3 -5
  55. package/package.json +2 -5
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");
@@ -46,6 +45,7 @@ const states = __importStar(require("./modules/debug/states"));
46
45
  const debugStatus = __importStar(require("./modules/debug/status"));
47
46
  const debugTag = __importStar(require("./modules/debug/tag"));
48
47
  const transactions = __importStar(require("./modules/debug/transactions"));
48
+ const envelope_1 = require("./modules/envelope");
49
49
  const feed_2 = require("./modules/feed");
50
50
  const grantee = __importStar(require("./modules/grantee"));
51
51
  const pinning = __importStar(require("./modules/pinning"));
@@ -88,6 +88,8 @@ class Bee {
88
88
  timeout: options?.timeout ?? false,
89
89
  headers: options?.headers,
90
90
  onRequest: options?.onRequest,
91
+ httpAgent: options?.httpAgent,
92
+ httpsAgent: options?.httpsAgent,
91
93
  };
92
94
  }
93
95
  /**
@@ -110,6 +112,16 @@ class Bee {
110
112
  }
111
113
  return bytes.upload(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, options);
112
114
  }
115
+ /**
116
+ * Requests content length for a `/bytes` reference
117
+ *
118
+ * @see [Bee API reference - `HEAD /bytes/`](https://docs.ethswarm.org/api/#tag/Bytes/paths/~1bytes~1%7Breference%7D/head)
119
+ */
120
+ async probeData(reference, options) {
121
+ (0, type_2.assertRequestOptions)(options);
122
+ (0, type_2.assertReferenceOrEns)(reference);
123
+ return bytes.head(this.getRequestOptionsForCall(options), reference);
124
+ }
113
125
  /**
114
126
  * Download data as a byte array
115
127
  *
@@ -151,8 +163,7 @@ class Bee {
151
163
  * @see [Bee docs - Upload and download](https://docs.ethswarm.org/docs/develop/access-the-swarm/upload-and-download)
152
164
  * @see [Bee API reference - `POST /chunks`](https://docs.ethswarm.org/api/#tag/Chunk/paths/~1chunks/post)
153
165
  */
154
- async uploadChunk(postageBatchId, data, options, requestOptions) {
155
- (0, type_2.assertBatchId)(postageBatchId);
166
+ async uploadChunk(stamp, data, options, requestOptions) {
156
167
  (0, type_2.assertRequestOptions)(requestOptions);
157
168
  if (!(data instanceof Uint8Array)) {
158
169
  throw new TypeError('Data has to be Uint8Array instance!');
@@ -166,7 +177,7 @@ class Bee {
166
177
  if (options) {
167
178
  (0, type_2.assertUploadOptions)(options);
168
179
  }
169
- return chunk.upload(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, options);
180
+ return chunk.upload(this.getRequestOptionsForCall(requestOptions), data, stamp, options);
170
181
  }
171
182
  /**
172
183
  * Download chunk as a byte array
@@ -255,16 +266,16 @@ class Bee {
255
266
  const fileName = name ?? data.name;
256
267
  const contentType = data.type;
257
268
  const fileOptions = { contentType, ...options };
258
- 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');
259
270
  }
260
271
  else if ((0, type_2.isReadable)(data) && options?.tag && !options.size) {
261
272
  // TODO: Needed until https://github.com/ethersphere/bee/issues/2317 is resolved
262
273
  const result = await bzz.uploadFile(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, name, options);
263
274
  await this.updateTag(options.tag, result.reference);
264
- return (0, type_2.addCidConversionFunction)(result, swarm_cid_1.ReferenceType.MANIFEST);
275
+ return (0, type_2.addCidConversionFunction)(result, 'manifest');
265
276
  }
266
277
  else {
267
- 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');
268
279
  }
269
280
  }
270
281
  /**
@@ -281,7 +292,7 @@ class Bee {
281
292
  */
282
293
  async downloadFile(reference, path = '', options) {
283
294
  (0, type_2.assertRequestOptions)(options);
284
- reference = (0, type_2.makeReferenceOrEns)(reference, swarm_cid_1.ReferenceType.MANIFEST);
295
+ reference = (0, type_2.makeReferenceOrEns)(reference, 'manifest');
285
296
  return bzz.downloadFile(this.getRequestOptionsForCall(options), reference, path);
286
297
  }
287
298
  /**
@@ -298,7 +309,7 @@ class Bee {
298
309
  */
299
310
  async downloadReadableFile(reference, path = '', options) {
300
311
  (0, type_2.assertRequestOptions)(options);
301
- reference = (0, type_2.makeReferenceOrEns)(reference, swarm_cid_1.ReferenceType.MANIFEST);
312
+ reference = (0, type_2.makeReferenceOrEns)(reference, 'manifest');
302
313
  return bzz.downloadFileReadable(this.getRequestOptionsForCall(options), reference, path);
303
314
  }
304
315
  /**
@@ -324,7 +335,7 @@ class Bee {
324
335
  (0, type_2.assertCollectionUploadOptions)(options);
325
336
  }
326
337
  const data = await (0, collection_1.makeCollectionFromFileList)(fileList);
327
- 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');
328
339
  }
329
340
  /**
330
341
  * Upload Collection that you can assembly yourself.
@@ -342,7 +353,7 @@ class Bee {
342
353
  (0, collection_1.assertCollection)(collection);
343
354
  if (options)
344
355
  (0, type_2.assertCollectionUploadOptions)(options);
345
- 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');
346
357
  }
347
358
  /**
348
359
  * Upload collection of files.
@@ -366,7 +377,7 @@ class Bee {
366
377
  if (options)
367
378
  (0, type_2.assertCollectionUploadOptions)(options);
368
379
  const data = await (0, collection_node_1.makeCollectionFromFS)(dir);
369
- 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');
370
381
  }
371
382
  /**
372
383
  * Create a new Tag which is meant for tracking progres of syncing data across network.
@@ -727,14 +738,13 @@ class Bee {
727
738
  * @see [Bee docs - Feeds](https://docs.ethswarm.org/docs/develop/tools-and-features/feeds)
728
739
  * @see [Bee API reference - `POST /feeds`](https://docs.ethswarm.org/api/#tag/Feed/paths/~1feeds~1{owner}~1{topic}/post)
729
740
  */
730
- async createFeedManifest(postageBatchId, type, topic, owner, options) {
741
+ async createFeedManifest(stamp, type, topic, owner, options) {
731
742
  (0, type_2.assertRequestOptions)(options);
732
743
  (0, type_1.assertFeedType)(type);
733
- (0, type_2.assertBatchId)(postageBatchId);
734
744
  const canonicalTopic = (0, topic_1.makeTopic)(topic);
735
745
  const canonicalOwner = (0, eth_1.makeHexEthAddress)(owner);
736
- const reference = await (0, feed_2.createFeedManifest)(this.getRequestOptionsForCall(options), canonicalOwner, canonicalTopic, postageBatchId);
737
- return (0, type_2.addCidConversionFunction)({ reference }, swarm_cid_1.ReferenceType.FEED);
746
+ const reference = await (0, feed_2.createFeedManifest)(this.getRequestOptionsForCall(options), canonicalOwner, canonicalTopic, stamp);
747
+ return (0, type_2.addCidConversionFunction)({ reference }, 'feed');
738
748
  }
739
749
  /**
740
750
  * Make a new feed reader for downloading feed updates.
@@ -883,6 +893,10 @@ class Bee {
883
893
  upload: soc_1.uploadSingleOwnerChunkData.bind(null, this.getRequestOptionsForCall(options), canonicalSigner),
884
894
  };
885
895
  }
896
+ async createEnvelope(postageBatchId, reference, options) {
897
+ (0, type_2.assertRequestOptions)(options);
898
+ return (0, envelope_1.postEnvelope)(this.getRequestOptionsForCall(options), postageBatchId, reference);
899
+ }
886
900
  /**
887
901
  * Ping the Bee node to see if there is a live Bee node on the given URL.
888
902
  *
@@ -31,7 +31,6 @@ const bytes_1 = require("../utils/bytes");
31
31
  const error_1 = require("../utils/error");
32
32
  const hash_1 = require("../utils/hash");
33
33
  const hex_1 = require("../utils/hex");
34
- const type_1 = require("../utils/type");
35
34
  const bmt_1 = require("./bmt");
36
35
  const cac_1 = require("./cac");
37
36
  const signer_1 = require("./signer");
@@ -119,12 +118,12 @@ exports.makeSingleOwnerChunk = makeSingleOwnerChunk;
119
118
  * @param postageBatchId Postage BatchId that will be assigned to uploaded data
120
119
  * @param options Upload options
121
120
  */
122
- async function uploadSingleOwnerChunk(requestOptions, chunk, postageBatchId, options) {
121
+ async function uploadSingleOwnerChunk(requestOptions, chunk, stamp, options) {
123
122
  const owner = (0, hex_1.bytesToHex)(chunk.owner());
124
123
  const identifier = (0, hex_1.bytesToHex)(chunk.identifier());
125
124
  const signature = (0, hex_1.bytesToHex)(chunk.signature());
126
125
  const data = cafe_utility_1.Binary.concatBytes(chunk.span(), chunk.payload());
127
- return socAPI.upload(requestOptions, owner, identifier, signature, data, postageBatchId, options);
126
+ return socAPI.upload(requestOptions, owner, identifier, signature, data, stamp, options);
128
127
  }
129
128
  exports.uploadSingleOwnerChunk = uploadSingleOwnerChunk;
130
129
  /**
@@ -137,11 +136,10 @@ exports.uploadSingleOwnerChunk = uploadSingleOwnerChunk;
137
136
  * @param data The chunk data
138
137
  * @param options
139
138
  */
140
- async function uploadSingleOwnerChunkData(requestOptions, signer, postageBatchId, identifier, data, options) {
141
- (0, type_1.assertAddress)(postageBatchId);
139
+ async function uploadSingleOwnerChunkData(requestOptions, signer, stamp, identifier, data, options) {
142
140
  const cac = (0, cac_1.makeContentAddressedChunk)(data);
143
141
  const soc = await makeSingleOwnerChunk(cac, identifier, signer);
144
- return uploadSingleOwnerChunk(requestOptions, soc, postageBatchId, options);
142
+ return uploadSingleOwnerChunk(requestOptions, soc, stamp, options);
145
143
  }
146
144
  exports.uploadSingleOwnerChunkData = uploadSingleOwnerChunkData;
147
145
  /**
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"));
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.downloadReadable = exports.download = exports.upload = void 0;
3
+ exports.downloadReadable = exports.download = exports.head = exports.upload = void 0;
4
4
  const bytes_1 = require("../utils/bytes");
5
5
  const headers_1 = require("../utils/headers");
6
6
  const http_1 = require("../utils/http");
@@ -32,6 +32,23 @@ async function upload(requestOptions, data, postageBatchId, options) {
32
32
  };
33
33
  }
34
34
  exports.upload = upload;
35
+ /**
36
+ * Requests content length for a reference
37
+ *
38
+ * @param requestOptions Options for making requests
39
+ * @param hash Bee content reference
40
+ */
41
+ async function head(requestOptions, hash) {
42
+ const response = await (0, http_1.http)(requestOptions, {
43
+ url: `${endpoint}/${hash}`,
44
+ method: 'head',
45
+ responseType: 'json',
46
+ });
47
+ return {
48
+ contentLength: parseInt(response.headers['content-length']),
49
+ };
50
+ }
51
+ exports.head = head;
35
52
  /**
36
53
  * Download data as a byte array
37
54
  *
@@ -14,18 +14,18 @@ const endpoint = 'chunks';
14
14
  * Upload expects the chuck data to be set accordingly.
15
15
  *
16
16
  * @param requestOptions Options for making requests
17
- * @param data Chunk data to be uploaded
18
- * @param postageBatchId Postage BatchId that will be assigned to uploaded data
17
+ * @param data Chunk data to be uploaded
18
+ * @param stamp BatchId or marshaled stamp to be used for the upload
19
19
  * @param options Additional options like tag, encryption, pinning
20
20
  */
21
- async function upload(requestOptions, data, postageBatchId, options) {
21
+ async function upload(requestOptions, data, stamp, options) {
22
22
  const response = await (0, http_1.http)(requestOptions, {
23
23
  method: 'post',
24
24
  url: `${endpoint}`,
25
25
  data,
26
26
  headers: {
27
27
  'content-type': 'application/octet-stream',
28
- ...(0, headers_1.extractUploadHeaders)(postageBatchId, options),
28
+ ...(0, headers_1.extractUploadHeaders)(stamp, options),
29
29
  },
30
30
  responseType: 'json',
31
31
  });
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.postEnvelope = void 0;
4
+ const cafe_utility_1 = require("cafe-utility");
5
+ const http_1 = require("../utils/http");
6
+ const ENVELOPE_ENDPOINT = 'envelope';
7
+ async function postEnvelope(requestOptions, postageBatchId, reference) {
8
+ const { data } = await (0, http_1.http)(requestOptions, {
9
+ method: 'post',
10
+ responseType: 'json',
11
+ url: `${ENVELOPE_ENDPOINT}/${reference}`,
12
+ headers: {
13
+ 'swarm-postage-batch-id': postageBatchId,
14
+ },
15
+ });
16
+ return {
17
+ issuer: cafe_utility_1.Binary.hexToUint8Array(cafe_utility_1.Types.asHexString(data.issuer, { name: 'issuer', byteLength: 20 })),
18
+ index: cafe_utility_1.Binary.hexToUint8Array(cafe_utility_1.Types.asHexString(data.index, { name: 'index', byteLength: 8 })),
19
+ timestamp: cafe_utility_1.Binary.hexToUint8Array(cafe_utility_1.Types.asHexString(data.timestamp, { name: 'timestamp', byteLength: 8 })),
20
+ signature: cafe_utility_1.Binary.hexToUint8Array(cafe_utility_1.Types.asHexString(data.signature, { name: 'signature', byteLength: 65 })),
21
+ };
22
+ }
23
+ exports.postEnvelope = postEnvelope;
@@ -14,12 +14,12 @@ const feedEndpoint = 'feeds';
14
14
  * @param postageBatchId Postage BatchId to be used to create the Feed Manifest
15
15
  * @param options Additional options, like type (default: 'sequence')
16
16
  */
17
- async function createFeedManifest(requestOptions, owner, topic, postageBatchId) {
17
+ async function createFeedManifest(requestOptions, owner, topic, stamp) {
18
18
  const response = await (0, http_1.http)(requestOptions, {
19
19
  method: 'post',
20
20
  responseType: 'json',
21
21
  url: `${feedEndpoint}/${owner}/${topic}`,
22
- headers: (0, headers_1.extractUploadHeaders)(postageBatchId),
22
+ headers: (0, headers_1.extractUploadHeaders)(stamp),
23
23
  });
24
24
  return response.data.reference;
25
25
  }
@@ -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
  },
@@ -16,14 +16,14 @@ const socEndpoint = 'soc';
16
16
  * @param postageBatchId Postage BatchId that will be assigned to uploaded data
17
17
  * @param options Additional options like tag, encryption, pinning
18
18
  */
19
- async function upload(requestOptions, owner, identifier, signature, data, postageBatchId, options) {
19
+ async function upload(requestOptions, owner, identifier, signature, data, stamp, options) {
20
20
  const response = await (0, http_1.http)(requestOptions, {
21
21
  method: 'post',
22
22
  url: `${socEndpoint}/${owner}/${identifier}`,
23
23
  data,
24
24
  headers: {
25
25
  'content-type': 'application/octet-stream',
26
- ...(0, headers_1.extractUploadHeaders)(postageBatchId, options),
26
+ ...(0, headers_1.extractUploadHeaders)(stamp, options),
27
27
  },
28
28
  responseType: 'json',
29
29
  params: { sig: signature },
@@ -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.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,3 +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
+ 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; } });
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.extractDownloadHeaders = exports.extractRedundantUploadHeaders = exports.extractUploadHeaders = exports.readFileHeaders = void 0;
4
+ const cafe_utility_1 = require("cafe-utility");
4
5
  const error_1 = require("./error");
5
6
  /**
6
7
  * Read the filename from the content-disposition header
@@ -39,13 +40,17 @@ function readFileHeaders(headers) {
39
40
  };
40
41
  }
41
42
  exports.readFileHeaders = readFileHeaders;
42
- function extractUploadHeaders(postageBatchId, options) {
43
- if (!postageBatchId) {
44
- throw new error_1.BeeError('Postage BatchID has to be specified!');
43
+ function extractUploadHeaders(stamp, options) {
44
+ if (!stamp) {
45
+ throw new error_1.BeeError('Stamp has to be specified!');
46
+ }
47
+ const headers = {};
48
+ if (stamp instanceof Uint8Array) {
49
+ headers['swarm-postage-stamp'] = cafe_utility_1.Binary.uint8ArrayToHex(stamp);
50
+ }
51
+ else {
52
+ headers['swarm-postage-batch-id'] = stamp;
45
53
  }
46
- const headers = {
47
- 'swarm-postage-batch-id': postageBatchId,
48
- };
49
54
  if (options?.act) {
50
55
  headers['swarm-act'] = String(options.act);
51
56
  }
@@ -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
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getDepthForCapacity = exports.getAmountForTtl = exports.getStampTtlSeconds = exports.getStampCostInBzz = exports.getStampCostInPlur = exports.getStampEffectiveBytes = exports.getStampMaximumCapacityBytes = exports.getStampUsage = void 0;
3
+ exports.marshalStamp = exports.convertEnvelopeToMarshaledStamp = exports.getDepthForCapacity = exports.getAmountForTtl = exports.getStampTtlSeconds = exports.getStampCostInBzz = exports.getStampCostInPlur = exports.getStampEffectiveBytes = exports.getStampMaximumCapacityBytes = exports.getStampUsage = void 0;
4
+ const cafe_utility_1 = require("cafe-utility");
4
5
  /**
5
6
  * Utility function that calculates usage of postage batch based on its utilization, depth and bucket depth.
6
7
  *
@@ -112,3 +113,23 @@ function getDepthForCapacity(gigabytes) {
112
113
  return gigabytes <= 1 ? 18 : Math.ceil(Math.log2(Math.ceil(gigabytes)) + 18);
113
114
  }
114
115
  exports.getDepthForCapacity = getDepthForCapacity;
116
+ function convertEnvelopeToMarshaledStamp(batchID, envelope) {
117
+ return marshalStamp(envelope.signature, cafe_utility_1.Binary.hexToUint8Array(batchID), envelope.timestamp, envelope.index);
118
+ }
119
+ exports.convertEnvelopeToMarshaledStamp = convertEnvelopeToMarshaledStamp;
120
+ function marshalStamp(signature, batchID, timestamp, index) {
121
+ if (signature.length !== 65) {
122
+ throw Error('invalid signature length');
123
+ }
124
+ if (batchID.length !== 32) {
125
+ throw Error('invalid batch ID length');
126
+ }
127
+ if (timestamp.length !== 8) {
128
+ throw Error('invalid timestamp length');
129
+ }
130
+ if (index.length !== 8) {
131
+ throw Error('invalid index length');
132
+ }
133
+ return cafe_utility_1.Binary.concatBytes(batchID, index, timestamp, signature);
134
+ }
135
+ exports.marshalStamp = marshalStamp;
@@ -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
- 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 = exports.isReadable = void 0;
4
- const swarm_cid_1 = require("@ethersphere/swarm-cid");
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;
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");
@@ -11,10 +11,6 @@ function isReadable(obj) {
11
11
  return typeof stream_1.Readable !== 'undefined' && obj instanceof stream_1.Readable;
12
12
  }
13
13
  exports.isReadable = isReadable;
14
- function isUint8Array(obj) {
15
- return obj instanceof Uint8Array;
16
- }
17
- exports.isUint8Array = isUint8Array;
18
14
  function isInteger(value) {
19
15
  return ((typeof value === 'string' && /^-?(0|[1-9][0-9]*)$/g.test(value)) ||
20
16
  (typeof value === 'number' &&
@@ -138,7 +134,7 @@ function makeReferenceOrEns(value, expectedCidType) {
138
134
  throw new TypeError('ReferenceCidOrEns has to be a string!');
139
135
  }
140
136
  try {
141
- const result = (0, swarm_cid_1.decodeCid)(value);
137
+ const result = (0, cid_1.convertCidToReference)(value);
142
138
  if (result.type !== expectedCidType) {
143
139
  throw new error_1.BeeError(`CID was expected to be of type ${expectedCidType}, but got instead ${result.type ?? 'non-Swarm CID'}`);
144
140
  }
@@ -155,13 +151,13 @@ exports.makeReferenceOrEns = makeReferenceOrEns;
155
151
  /**
156
152
  * Function that adds getter which converts the reference into CID base32 encoded string.
157
153
  * @param result
158
- * @param cidType Type as described in the @ethersphere/swarm-cids-js -> ReferenceType
154
+ * @param cidType feed or manifest
159
155
  */
160
156
  function addCidConversionFunction(result, cidType) {
161
157
  return {
162
158
  ...result,
163
159
  cid() {
164
- return (0, swarm_cid_1.encodeReference)(result.reference, cidType).toString();
160
+ return (0, cid_1.convertReferenceToCid)(result.reference, cidType);
165
161
  },
166
162
  };
167
163
  }