@ethersphere/bee-js 7.1.1 → 8.0.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 (38) hide show
  1. package/README.md +16 -0
  2. package/dist/cjs/bee.js +40 -1
  3. package/dist/cjs/modules/bytes.js +1 -0
  4. package/dist/cjs/modules/bzz.js +2 -0
  5. package/dist/cjs/modules/chunk.js +6 -1
  6. package/dist/cjs/modules/grantee.js +56 -0
  7. package/dist/cjs/modules/soc.js +6 -1
  8. package/dist/cjs/utils/headers.js +3 -0
  9. package/dist/cjs/utils/http.js +5 -0
  10. package/dist/cjs/utils/tar-writer.browser.js +1 -1
  11. package/dist/cjs/utils/tar-writer.js +1 -1
  12. package/dist/cjs/utils/tar.browser.js +26 -17
  13. package/dist/cjs/utils/tar.js +44 -20
  14. package/dist/index.browser.min.js +1 -1
  15. package/dist/index.browser.min.js.map +1 -1
  16. package/dist/mjs/bee.js +43 -1
  17. package/dist/mjs/modules/bytes.js +2 -1
  18. package/dist/mjs/modules/bzz.js +4 -2
  19. package/dist/mjs/modules/chunk.js +6 -1
  20. package/dist/mjs/modules/grantee.js +52 -0
  21. package/dist/mjs/modules/soc.js +6 -1
  22. package/dist/mjs/utils/headers.js +3 -0
  23. package/dist/mjs/utils/http.js +7 -0
  24. package/dist/mjs/utils/tar-writer.browser.js +1 -1
  25. package/dist/mjs/utils/tar-writer.js +1 -1
  26. package/dist/mjs/utils/tar.browser.js +26 -17
  27. package/dist/mjs/utils/tar.js +43 -20
  28. package/dist/types/bee.d.ts +37 -4
  29. package/dist/types/chunk/soc.d.ts +3 -3
  30. package/dist/types/feed/index.d.ts +2 -2
  31. package/dist/types/feed/json.d.ts +2 -2
  32. package/dist/types/modules/chunk.d.ts +2 -2
  33. package/dist/types/modules/grantee.d.ts +7 -0
  34. package/dist/types/modules/soc.d.ts +2 -2
  35. package/dist/types/types/index.d.ts +24 -3
  36. package/dist/types/utils/tar.browser.d.ts +1 -1
  37. package/dist/types/utils/tar.d.ts +1 -1
  38. package/package.json +1 -1
package/README.md CHANGED
@@ -15,6 +15,22 @@
15
15
 
16
16
  > Intended to be used with Bee version 2.1.0.
17
17
 
18
+ ## Quick start
19
+
20
+ Start a Swarm project using TypeScript:
21
+
22
+ ```sh
23
+ npm init swarm-app@latest my-dapp node-ts
24
+ ```
25
+
26
+ or using Vite and TypeScript:
27
+
28
+ ```sh
29
+ npm init swarm-app@latest my-dapp vite-tsx
30
+ ```
31
+
32
+ Supported types are `node`, `node-esm`, `node-ts` and `vite-tsx`. Replace `my-dapp` with your project name.
33
+
18
34
  ## Install
19
35
 
20
36
  ```sh
package/dist/cjs/bee.js CHANGED
@@ -47,6 +47,7 @@ const debugStatus = __importStar(require("./modules/debug/status"));
47
47
  const debugTag = __importStar(require("./modules/debug/tag"));
48
48
  const transactions = __importStar(require("./modules/debug/transactions"));
49
49
  const feed_2 = require("./modules/feed");
50
+ const grantee = __importStar(require("./modules/grantee"));
50
51
  const pinning = __importStar(require("./modules/pinning"));
51
52
  const pss = __importStar(require("./modules/pss"));
52
53
  const status = __importStar(require("./modules/status"));
@@ -178,10 +179,48 @@ class Bee {
178
179
  (0, type_2.assertReferenceOrEns)(reference);
179
180
  return chunk.download(this.getRequestOptionsForCall(options), reference);
180
181
  }
182
+ /**
183
+ * Create a grantees list from the given array of public keys.
184
+ *
185
+ * The grantees list can be obtained with the `getGrantees` method.
186
+ *
187
+ * @param postageBatchId - The ID of the postage batch.
188
+ * @param grantees - An array of public keys representing the grantees.
189
+ * @param requestOptions - Optional request options.
190
+ * @returns A promise that resolves to a `GranteesResult` object.
191
+ */
192
+ async createGrantees(postageBatchId, grantees, requestOptions) {
193
+ (0, type_2.assertBatchId)(postageBatchId);
194
+ return grantee.createGrantees(this.getRequestOptionsForCall(requestOptions), postageBatchId, grantees);
195
+ }
196
+ /**
197
+ * Retrieves the grantees for a given reference.
198
+ *
199
+ * @param reference - The reference.
200
+ * @param requestOptions - Optional request options.
201
+ * @returns A promise that resolves to a `GetGranteesResult` object.
202
+ */
203
+ async getGrantees(reference, requestOptions) {
204
+ return grantee.getGrantees(reference, this.getRequestOptionsForCall(requestOptions));
205
+ }
206
+ /**
207
+ * Updates the grantees of a specific reference and history.
208
+ *
209
+ * @param reference - The reference.
210
+ * @param history - The history.
211
+ * @param postageBatchId - The ID of the postage batch.
212
+ * @param grantees - The grantees.
213
+ * @param requestOptions - Optional request options.
214
+ * @returns A Promise that resolves to to a `GranteesResult` object.
215
+ */
216
+ async patchGrantees(postageBatchId, reference, history, grantees, requestOptions) {
217
+ (0, type_2.assertBatchId)(postageBatchId);
218
+ return grantee.patchGrantees(postageBatchId, reference, history, { add: grantees.add || [], revoke: grantees.revoke || [] }, this.getRequestOptionsForCall(requestOptions));
219
+ }
181
220
  /**
182
221
  * Upload single file to a Bee node.
183
222
  *
184
- * **To make sure that you won't loose critical data it is highly recommended to also
223
+ * **To make sure that you won't lose critical data it is highly recommended to also
185
224
  * locally pin the data with `options.pin = true`**
186
225
  *
187
226
  * @param postageBatchId Postage BatchId to be used to upload the data with
@@ -28,6 +28,7 @@ async function upload(requestOptions, data, postageBatchId, options) {
28
28
  return {
29
29
  reference: response.data.reference,
30
30
  tagUid: response.headers['swarm-tag'] ? (0, type_1.makeTagUid)(response.headers['swarm-tag']) : undefined,
31
+ historyAddress: response.headers['swarm-act-history-address'] || '',
31
32
  };
32
33
  }
33
34
  exports.upload = upload;
@@ -45,6 +45,7 @@ async function uploadFile(requestOptions, data, postageBatchId, name, options) {
45
45
  return {
46
46
  reference: response.data.reference,
47
47
  tagUid: response.headers['swarm-tag'] ? (0, type_1.makeTagUid)(response.headers['swarm-tag']) : undefined,
48
+ historyAddress: response.headers['swarm-act-history-address'] || '',
48
49
  };
49
50
  }
50
51
  exports.uploadFile = uploadFile;
@@ -114,6 +115,7 @@ async function uploadCollection(requestOptions, collection, postageBatchId, opti
114
115
  return {
115
116
  reference: response.data.reference,
116
117
  tagUid: response.headers['swarm-tag'] ? (0, type_1.makeTagUid)(response.headers['swarm-tag']) : undefined,
118
+ historyAddress: response.headers['swarm-act-history-address'] || '',
117
119
  };
118
120
  }
119
121
  exports.uploadCollection = uploadCollection;
@@ -4,6 +4,7 @@ exports.download = 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");
7
+ const type_1 = require("../utils/type");
7
8
  const endpoint = 'chunks';
8
9
  /**
9
10
  * Upload chunk to a Bee node
@@ -28,7 +29,11 @@ async function upload(requestOptions, data, postageBatchId, options) {
28
29
  },
29
30
  responseType: 'json',
30
31
  });
31
- return response.data.reference;
32
+ return {
33
+ reference: response.data.reference,
34
+ tagUid: response.headers['swarm-tag'] ? (0, type_1.makeTagUid)(response.headers['swarm-tag']) : undefined,
35
+ historyAddress: response.headers['swarm-act-history-address'] || '',
36
+ };
32
37
  }
33
38
  exports.upload = upload;
34
39
  /**
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.patchGrantees = exports.createGrantees = exports.getGrantees = void 0;
4
+ const headers_1 = require("../utils/headers");
5
+ const http_1 = require("../utils/http");
6
+ const granteeEndpoint = 'grantee';
7
+ async function getGrantees(reference, requestOptions) {
8
+ const response = await (0, http_1.http)(requestOptions, {
9
+ method: 'get',
10
+ url: `${granteeEndpoint}/${reference}`,
11
+ responseType: 'json',
12
+ });
13
+ return {
14
+ status: response.status,
15
+ statusText: response.statusText,
16
+ data: response.data.data,
17
+ };
18
+ }
19
+ exports.getGrantees = getGrantees;
20
+ async function createGrantees(requestOptions, postageBatchId, grantees) {
21
+ const response = await (0, http_1.http)(requestOptions, {
22
+ method: 'post',
23
+ url: granteeEndpoint,
24
+ data: { grantees: grantees },
25
+ headers: {
26
+ ...(0, headers_1.extractRedundantUploadHeaders)(postageBatchId),
27
+ },
28
+ responseType: 'json',
29
+ });
30
+ return {
31
+ status: response.status,
32
+ statusText: response.statusText,
33
+ ref: response.data.ref,
34
+ historyref: response.data.historyref,
35
+ };
36
+ }
37
+ exports.createGrantees = createGrantees;
38
+ async function patchGrantees(postageBatchId, reference, historyRef, grantees, requestOptions) {
39
+ const response = await (0, http_1.http)(requestOptions, {
40
+ method: 'patch',
41
+ url: `${granteeEndpoint}/${reference}`,
42
+ data: grantees,
43
+ headers: {
44
+ ...(0, headers_1.extractRedundantUploadHeaders)(postageBatchId),
45
+ 'swarm-act-history-address': historyRef,
46
+ },
47
+ responseType: 'json',
48
+ });
49
+ return {
50
+ status: response.status,
51
+ statusText: response.statusText,
52
+ ref: response.data.ref,
53
+ historyref: response.data.historyref,
54
+ };
55
+ }
56
+ exports.patchGrantees = patchGrantees;
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.upload = void 0;
4
4
  const headers_1 = require("../utils/headers");
5
5
  const http_1 = require("../utils/http");
6
+ const type_1 = require("../utils/type");
6
7
  const socEndpoint = 'soc';
7
8
  /**
8
9
  * Upload single owner chunk (SOC) to a Bee node
@@ -27,6 +28,10 @@ async function upload(requestOptions, owner, identifier, signature, data, postag
27
28
  responseType: 'json',
28
29
  params: { sig: signature },
29
30
  });
30
- return response.data.reference;
31
+ return {
32
+ reference: response.data.reference,
33
+ tagUid: response.headers['swarm-tag'] ? (0, type_1.makeTagUid)(response.headers['swarm-tag']) : undefined,
34
+ historyAddress: response.headers['swarm-act-history-address'] || '',
35
+ };
31
36
  }
32
37
  exports.upload = upload;
@@ -46,6 +46,9 @@ function extractUploadHeaders(postageBatchId, options) {
46
46
  const headers = {
47
47
  'swarm-postage-batch-id': postageBatchId,
48
48
  };
49
+ if (options?.act) {
50
+ headers['swarm-act'] = String(options.act);
51
+ }
49
52
  if (options?.pin) {
50
53
  headers['swarm-pin'] = String(options.pin);
51
54
  }
@@ -25,6 +25,11 @@ 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
+ }
28
33
  // TODO: https://github.com/axios/axios/pull/6253
29
34
  return response;
30
35
  }
@@ -5,7 +5,7 @@ const file_1 = require("./file");
5
5
  async function writeTar(collection, tarStream) {
6
6
  for (const item of collection) {
7
7
  if (item.file) {
8
- await tarStream.beginFile(item.path, item.file.size);
8
+ tarStream.beginFile(item.path, item.file.size);
9
9
  await tarStream.appendFile(new Uint8Array(await (0, file_1.fileArrayBuffer)(item.file)));
10
10
  await tarStream.endFile();
11
11
  }
@@ -4,7 +4,7 @@ exports.writeTar = void 0;
4
4
  const fs_1 = require("fs");
5
5
  async function writeTar(collection, tarStream) {
6
6
  for (const item of collection) {
7
- await tarStream.beginFile(item.path, item.size);
7
+ tarStream.beginFile(item.path, item.size);
8
8
  if (item.fsPath) {
9
9
  const stream = (0, fs_1.createReadStream)(item.fsPath);
10
10
  for await (const chunk of stream) {
@@ -14,7 +14,7 @@ class TarStream {
14
14
  return newAcc;
15
15
  });
16
16
  }
17
- async beginFile(path, size) {
17
+ beginFile(path, size) {
18
18
  const header = createHeader(path, size);
19
19
  this.pieces.push(header);
20
20
  this.currentFileSize = 0;
@@ -24,8 +24,10 @@ class TarStream {
24
24
  this.currentFileSize += data.length;
25
25
  }
26
26
  async endFile() {
27
- const padding = 512 - (this.currentFileSize % 512);
28
- this.pieces.push(new Uint8Array(padding));
27
+ const padding = this.currentFileSize % 512 === 0 ? 0 : 512 - (this.currentFileSize % 512);
28
+ if (padding > 0) {
29
+ this.pieces.push(new Uint8Array(padding));
30
+ }
29
31
  }
30
32
  async end() {
31
33
  this.pieces.push(createEndOfArchive());
@@ -33,30 +35,37 @@ class TarStream {
33
35
  }
34
36
  exports.TarStream = TarStream;
35
37
  function createHeader(path, size) {
36
- const header = new Uint8Array(512); // Initialize header with zeros
37
38
  const encoder = new TextEncoder();
38
39
  function writeToBuffer(str, offset, length) {
39
40
  const bytes = encoder.encode(str);
40
41
  header.set(bytes.slice(0, length), offset);
41
42
  }
42
- writeToBuffer(path, 0, 100); // File name
43
- writeToBuffer('0000777', 100, 8); // File mode
44
- writeToBuffer('0001750', 108, 8); // UID
45
- writeToBuffer('0001750', 116, 8); // GID
46
- writeToBuffer(size.toString(8).padStart(11, '0') + ' ', 124, 12); // File size
47
- writeToBuffer(Math.floor(Date.now() / 1000).toString(8) + ' ', 136, 12); // Mod time
48
- writeToBuffer(' ', 148, 8); // Checksum placeholder
49
- writeToBuffer('0', 156, 1); // Typeflag
50
- writeToBuffer('ustar ', 257, 8); // Magic and version
51
- for (let i = 345; i < 512; i++) {
52
- header[i] = 0; // Fill remaining with zeros
53
- }
43
+ // Initialize header with zeros
44
+ const header = new Uint8Array();
45
+ header.fill(0, 0, 512);
46
+ // File name, truncated to 100 characters if necessary
47
+ writeToBuffer(path.slice(0, 100).padEnd(100, '\0'), 0, 100);
48
+ // File mode (octal) and null-terminated
49
+ writeToBuffer('0000777\0', 100, 8);
50
+ // UID and GID (octal) and null-terminated
51
+ writeToBuffer('0001750\0', 108, 8); // UID
52
+ writeToBuffer('0001750\0', 116, 8); // GID
53
+ // File size in octal (11 chars) and null-terminated
54
+ writeToBuffer(size.toString(8).padStart(11, '0') + '\0', 124, 12);
55
+ // Modification time in octal and null-terminated
56
+ const modTime = Math.floor(new Date().getTime() / 1000);
57
+ writeToBuffer(modTime.toString(8).padStart(11, '0') + '\0', 136, 12);
58
+ // Checksum placeholder (8 spaces)
59
+ writeToBuffer(' ', 148, 8);
60
+ // Typeflag (normal file)
61
+ writeToBuffer('0', 156, 1);
62
+ // USTAR magic and version
63
+ writeToBuffer('ustar\0\0', 257, 8);
54
64
  // Calculate checksum
55
65
  let checksum = 0;
56
66
  for (let i = 0; i < 512; i++) {
57
67
  checksum += header[i];
58
68
  }
59
- // Write checksum
60
69
  writeToBuffer(checksum.toString(8).padStart(6, '0') + '\0 ', 148, 8);
61
70
  return header;
62
71
  }
@@ -7,43 +7,67 @@ class TarStream {
7
7
  this.output = new stream_1.PassThrough();
8
8
  this.currentFileSize = 0;
9
9
  }
10
- async beginFile(path, size) {
10
+ beginFile(path, size) {
11
11
  const header = createHeader(path, size);
12
12
  this.output.write(header);
13
13
  this.currentFileSize = 0;
14
14
  }
15
15
  async appendFile(data) {
16
16
  return new Promise(resolve => {
17
- this.output.write(data, () => {
17
+ if (!this.output.write(data)) {
18
+ this.output.once('drain', () => {
19
+ resolve();
20
+ });
21
+ }
22
+ else {
18
23
  resolve();
19
- });
24
+ }
20
25
  this.currentFileSize += data.length;
21
26
  });
22
27
  }
23
28
  async endFile() {
24
- const padding = 512 - (this.currentFileSize % 512);
25
- this.output.write(Buffer.alloc(padding, 0));
29
+ const padding = this.currentFileSize % 512 === 0 ? 0 : 512 - (this.currentFileSize % 512);
30
+ if (padding > 0) {
31
+ this.output.write(Buffer.alloc(padding, 0));
32
+ }
26
33
  }
27
34
  async end() {
28
- this.output.write(createEndOfArchive());
29
- this.output.end();
35
+ return new Promise(resolve => {
36
+ this.output.write(createEndOfArchive());
37
+ this.output.end(() => {
38
+ resolve();
39
+ });
40
+ });
30
41
  }
31
42
  }
32
43
  exports.TarStream = TarStream;
33
44
  function createHeader(path, size) {
34
- const header = Buffer.alloc(512, 0); // Initialize header with zeros
35
- header.write(path, 0, 100); // File name
36
- header.write('0000777', 100, 8); // File mode
37
- header.write('0001750', 108, 8); // UID
38
- header.write('0001750', 116, 8); // GID
39
- header.write(size.toString(8).padStart(11, '0') + ' ', 124, 12); // File size
40
- header.write(Math.floor(new Date().getTime() / 1000).toString(8) + ' ', 136, 12); // Mod time
41
- header.write(' ', 148, 8); // Checksum placeholder
42
- header.write('0', 156, 1); // Typeflag
43
- header.write('ustar ', 257, 8); // Magic and version
44
- header.write('0'.repeat(8 * 12), 345, 8 * 12); // Fill remaining with zeros
45
- const checksum = header.reduce((sum, elem) => sum + elem, 0);
46
- header.write(checksum.toString(8).padStart(6, '0') + '\0 ', 148, 8); // Write checksum
45
+ // Initialize header with zeros
46
+ const header = Buffer.alloc(512, 0);
47
+ // File name, truncated to 100 characters if necessary
48
+ header.write(path.slice(0, 100).padEnd(100, '\0'), 0, 100);
49
+ // File mode (octal) and null-terminated
50
+ header.write('0000777\0', 100, 8);
51
+ // UID and GID (octal) and null-terminated
52
+ header.write('0001750\0', 108, 8); // UID
53
+ header.write('0001750\0', 116, 8); // GID
54
+ // File size in octal (11 chars) and null-terminated
55
+ header.write(size.toString(8).padStart(11, '0') + '\0', 124, 12);
56
+ // Modification time in octal and null-terminated
57
+ const modTime = Math.floor(new Date().getTime() / 1000);
58
+ header.write(modTime.toString(8).padStart(11, '0') + '\0', 136, 12);
59
+ // Checksum placeholder (8 spaces)
60
+ header.write(' ', 148, 8);
61
+ // Typeflag (normal file)
62
+ header.write('0', 156, 1);
63
+ // USTAR magic and version
64
+ header.write('ustar\0\0', 257, 8);
65
+ // Calculate checksum
66
+ let checksum = 0;
67
+ for (let i = 0; i < 512; i++) {
68
+ checksum += header[i];
69
+ }
70
+ header.write(checksum.toString(8).padStart(6, '0') + '\0 ', 148, 8);
47
71
  return header;
48
72
  }
49
73
  function createEndOfArchive() {