@ethersphere/bee-js 6.9.1 → 7.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 (143) hide show
  1. package/README.md +107 -65
  2. package/dist/cjs/bee.js +533 -6
  3. package/dist/cjs/chunk/cac.js +3 -3
  4. package/dist/cjs/chunk/soc.js +5 -5
  5. package/dist/cjs/chunk/span.js +2 -6
  6. package/dist/cjs/feed/identifier.js +2 -2
  7. package/dist/cjs/feed/index.js +5 -6
  8. package/dist/cjs/feed/retrievable.js +2 -2
  9. package/dist/cjs/index.js +1 -3
  10. package/dist/cjs/modules/bytes.js +3 -3
  11. package/dist/cjs/modules/bzz.js +9 -20
  12. package/dist/cjs/modules/chunk.js +2 -2
  13. package/dist/cjs/modules/debug/balance.js +4 -4
  14. package/dist/cjs/modules/debug/chequebook.js +8 -8
  15. package/dist/cjs/modules/debug/chunk.js +2 -19
  16. package/dist/cjs/modules/debug/settlements.js +2 -2
  17. package/dist/cjs/modules/debug/stake.js +3 -3
  18. package/dist/cjs/modules/debug/states.js +4 -19
  19. package/dist/cjs/modules/debug/status.js +15 -32
  20. package/dist/cjs/modules/debug/tag.js +1 -1
  21. package/dist/cjs/modules/debug/transactions.js +4 -4
  22. package/dist/cjs/modules/feed.js +6 -7
  23. package/dist/cjs/modules/pinning.js +4 -4
  24. package/dist/cjs/modules/pss.js +1 -1
  25. package/dist/cjs/modules/soc.js +1 -1
  26. package/dist/cjs/modules/status.js +1 -1
  27. package/dist/cjs/modules/stewardship.js +1 -1
  28. package/dist/cjs/package.json +4 -0
  29. package/dist/cjs/types/index.js +5 -1
  30. package/dist/cjs/utils/collection.js +7 -22
  31. package/dist/cjs/utils/collection.node.js +2 -1
  32. package/dist/cjs/utils/data.browser.js +1 -49
  33. package/dist/cjs/utils/error.js +2 -6
  34. package/dist/cjs/utils/eth.js +1 -1
  35. package/dist/cjs/utils/expose.js +1 -8
  36. package/dist/cjs/utils/file.js +2 -2
  37. package/dist/cjs/utils/http.js +6 -25
  38. package/dist/cjs/utils/tar-uploader.browser.js +26 -0
  39. package/dist/cjs/utils/tar-uploader.js +27 -0
  40. package/dist/cjs/utils/tar-writer.browser.js +17 -0
  41. package/dist/cjs/utils/tar-writer.js +21 -0
  42. package/dist/cjs/utils/tar.browser.js +65 -0
  43. package/dist/cjs/utils/tar.js +47 -21
  44. package/dist/cjs/utils/type.js +7 -3
  45. package/dist/cjs/utils/url.js +1 -6
  46. package/dist/index.browser.min.js +1 -1
  47. package/dist/index.browser.min.js.LICENSE.txt +0 -42
  48. package/dist/index.browser.min.js.map +1 -1
  49. package/dist/mjs/bee.js +533 -9
  50. package/dist/mjs/chunk/cac.js +4 -4
  51. package/dist/mjs/chunk/soc.js +6 -6
  52. package/dist/mjs/chunk/span.js +2 -6
  53. package/dist/mjs/feed/identifier.js +2 -2
  54. package/dist/mjs/feed/index.js +5 -9
  55. package/dist/mjs/feed/retrievable.js +2 -2
  56. package/dist/mjs/index.js +1 -2
  57. package/dist/mjs/modules/bytes.js +3 -3
  58. package/dist/mjs/modules/bzz.js +8 -20
  59. package/dist/mjs/modules/chunk.js +2 -2
  60. package/dist/mjs/modules/debug/balance.js +4 -4
  61. package/dist/mjs/modules/debug/chequebook.js +8 -8
  62. package/dist/mjs/modules/debug/chunk.js +1 -17
  63. package/dist/mjs/modules/debug/settlements.js +2 -2
  64. package/dist/mjs/modules/debug/stake.js +3 -3
  65. package/dist/mjs/modules/debug/states.js +4 -19
  66. package/dist/mjs/modules/debug/status.js +15 -34
  67. package/dist/mjs/modules/debug/tag.js +1 -1
  68. package/dist/mjs/modules/debug/transactions.js +4 -4
  69. package/dist/mjs/modules/feed.js +6 -7
  70. package/dist/mjs/modules/pinning.js +4 -4
  71. package/dist/mjs/modules/pss.js +1 -1
  72. package/dist/mjs/modules/soc.js +1 -1
  73. package/dist/mjs/modules/status.js +1 -1
  74. package/dist/mjs/modules/stewardship.js +1 -1
  75. package/dist/mjs/package.json +6 -2
  76. package/dist/mjs/types/index.js +4 -0
  77. package/dist/mjs/utils/collection.js +7 -22
  78. package/dist/mjs/utils/collection.node.js +2 -1
  79. package/dist/mjs/utils/data.browser.js +0 -57
  80. package/dist/mjs/utils/error.js +2 -6
  81. package/dist/mjs/utils/eth.js +1 -1
  82. package/dist/mjs/utils/expose.js +0 -1
  83. package/dist/mjs/utils/http.js +5 -2
  84. package/dist/mjs/utils/tar-uploader.browser.js +22 -0
  85. package/dist/mjs/utils/tar-uploader.js +23 -0
  86. package/dist/mjs/utils/tar-writer.browser.js +12 -0
  87. package/dist/mjs/utils/tar-writer.js +16 -0
  88. package/dist/mjs/utils/tar.browser.js +61 -0
  89. package/dist/mjs/utils/tar.js +45 -16
  90. package/dist/mjs/utils/type.js +4 -1
  91. package/dist/mjs/utils/url.js +1 -6
  92. package/dist/types/bee.d.ts +305 -4
  93. package/dist/types/chunk/soc.d.ts +2 -2
  94. package/dist/types/feed/identifier.d.ts +1 -1
  95. package/dist/types/index.d.ts +1 -3
  96. package/dist/types/modules/bytes.d.ts +3 -3
  97. package/dist/types/modules/bzz.d.ts +14 -6
  98. package/dist/types/modules/chunk.d.ts +2 -2
  99. package/dist/types/modules/debug/balance.d.ts +4 -4
  100. package/dist/types/modules/debug/chequebook.d.ts +8 -8
  101. package/dist/types/modules/debug/chunk.d.ts +1 -10
  102. package/dist/types/modules/debug/settlements.d.ts +2 -2
  103. package/dist/types/modules/debug/stake.d.ts +3 -3
  104. package/dist/types/modules/debug/states.d.ts +3 -3
  105. package/dist/types/modules/debug/status.d.ts +10 -20
  106. package/dist/types/modules/debug/tag.d.ts +1 -1
  107. package/dist/types/modules/debug/transactions.d.ts +4 -4
  108. package/dist/types/modules/feed.d.ts +6 -9
  109. package/dist/types/modules/pinning.d.ts +4 -4
  110. package/dist/types/modules/pss.d.ts +1 -1
  111. package/dist/types/modules/soc.d.ts +1 -1
  112. package/dist/types/modules/status.d.ts +1 -1
  113. package/dist/types/modules/stewardship.d.ts +1 -1
  114. package/dist/types/types/debug.d.ts +0 -3
  115. package/dist/types/types/index.d.ts +9 -17
  116. package/dist/types/utils/collection.browser.d.ts +1 -1
  117. package/dist/types/utils/collection.d.ts +3 -3
  118. package/dist/types/utils/collection.node.d.ts +1 -1
  119. package/dist/types/utils/data.browser.d.ts +0 -7
  120. package/dist/types/utils/data.d.ts +1 -2
  121. package/dist/types/utils/error.d.ts +2 -7
  122. package/dist/types/utils/eth.d.ts +1 -1
  123. package/dist/types/utils/expose.d.ts +0 -1
  124. package/dist/types/utils/tar-uploader.browser.d.ts +4 -0
  125. package/dist/types/utils/tar-uploader.d.ts +4 -0
  126. package/dist/types/utils/tar-writer.browser.d.ts +3 -0
  127. package/dist/types/utils/tar-writer.d.ts +3 -0
  128. package/dist/types/utils/tar.browser.d.ts +9 -0
  129. package/dist/types/utils/tar.d.ts +10 -2
  130. package/dist/types/utils/type.d.ts +4 -1
  131. package/package.json +4 -19
  132. package/dist/cjs/bee-debug.js +0 -590
  133. package/dist/cjs/chunk/serialize.js +0 -19
  134. package/dist/cjs/utils/stream.js +0 -146
  135. package/dist/cjs/utils/uint64.js +0 -29
  136. package/dist/mjs/bee-debug.js +0 -560
  137. package/dist/mjs/chunk/serialize.js +0 -15
  138. package/dist/mjs/utils/stream.js +0 -129
  139. package/dist/mjs/utils/uint64.js +0 -22
  140. package/dist/types/bee-debug.d.ts +0 -323
  141. package/dist/types/chunk/serialize.d.ts +0 -6
  142. package/dist/types/utils/stream.d.ts +0 -40
  143. package/dist/types/utils/uint64.d.ts +0 -4
@@ -5,7 +5,7 @@ const endpoint = 'pss';
5
5
  /**
6
6
  * Send to recipient or target with Postal Service for Swarm
7
7
  *
8
- * @param kyOptions Ky Options for making requests
8
+ * @param requestOptions Options for making requests
9
9
  * @param topic Topic name
10
10
  * @param target Target message address prefix
11
11
  * @param data
@@ -4,7 +4,7 @@ const socEndpoint = 'soc';
4
4
  /**
5
5
  * Upload single owner chunk (SOC) to a Bee node
6
6
  *
7
- * @param ky Ky instance
7
+ * @param requestOptions Options for making requests
8
8
  * @param owner Owner's ethereum address in hex
9
9
  * @param identifier Arbitrary identifier in hex
10
10
  * @param signature Signature in hex
@@ -2,7 +2,7 @@ import { http } from "../utils/http.js";
2
2
  /**
3
3
  * Ping the base bee URL. If connection was not successful throw error
4
4
  *
5
- * @param kyOptions Ky instance for given Bee class instance
5
+ * @param requestOptions Options for making requests
6
6
  */
7
7
  export async function checkConnection(requestOptions) {
8
8
  await http(requestOptions, {
@@ -2,7 +2,7 @@ import { http } from "../utils/http.js";
2
2
  const stewardshipEndpoint = 'stewardship';
3
3
  /**
4
4
  * Reupload locally pinned data
5
- * @param ky Ky instance
5
+ * @param requestOptions Options for making requests
6
6
  * @param reference
7
7
  * @param options
8
8
  * @throws BeeResponseError if not locally pinned or invalid data
@@ -2,7 +2,11 @@
2
2
  "type": "module",
3
3
  "browser": {
4
4
  "stream": false,
5
- "./utils/data.js": "./utils/data.browser.js",
6
- "./utils/collection.node.js": "./utils/collection.browser.js"
5
+ "fs": false,
6
+ "./utils/tar.js": "./utils/tar.browser.js",
7
+ "./utils/tar-writer.js": "./utils/tar-writer.browser.js",
8
+ "./utils/tar-uploader.js": "./utils/tar-uploader.browser.js",
9
+ "./utils/data.js": "./utils/data.browser.js",
10
+ "./utils/collection.node.js": "./utils/collection.browser.js"
7
11
  }
8
12
  }
@@ -15,6 +15,10 @@ export const ENCRYPTED_REFERENCE_BYTES_LENGTH = 64;
15
15
  * Minimal depth that can be used for creation of postage batch
16
16
  */
17
17
  export const STAMPS_DEPTH_MIN = 17;
18
+ /**
19
+ * Minimal amount that can be used for creation of postage batch
20
+ */
21
+ export const STAMPS_AMOUNT_MIN = 24000 * 24 * 60 * 12;
18
22
  /**
19
23
  * Maximal depth that can be used for creation of postage batch
20
24
  */
@@ -1,11 +1,9 @@
1
1
  import { BeeArgumentError } from "./error.js";
2
- import { fileArrayBuffer } from "./file.js";
3
- import { isUint8Array } from "./type.js";
4
2
  export function isCollection(data) {
5
3
  if (!Array.isArray(data)) {
6
4
  return false;
7
5
  }
8
- return data.every(entry => typeof entry === 'object' && entry.data && entry.path && isUint8Array(entry.data));
6
+ return data.every(entry => typeof entry === 'object' && entry.path && entry.size);
9
7
  }
10
8
  export function assertCollection(data) {
11
9
  if (!isCollection(data)) {
@@ -22,17 +20,11 @@ function makeFilePath(file) {
22
20
  throw new TypeError('file is not valid File object');
23
21
  }
24
22
  export async function makeCollectionFromFileList(fileList) {
25
- const collection = [];
26
- for (let i = 0; i < fileList.length; i++) {
27
- const file = fileList[i];
28
- if (file) {
29
- collection.push({
30
- path: makeFilePath(file),
31
- data: new Uint8Array(await fileArrayBuffer(file))
32
- });
33
- }
34
- }
35
- return collection;
23
+ return Array.from(fileList).map(file => ({
24
+ path: makeFilePath(file),
25
+ size: file.size,
26
+ file
27
+ }));
36
28
  }
37
29
  /**
38
30
  * Calculate cumulative size of files
@@ -41,12 +33,5 @@ export async function makeCollectionFromFileList(fileList) {
41
33
  * @returns size in bytes
42
34
  */
43
35
  export function getCollectionSize(fileList) {
44
- let sum = 0;
45
- for (let i = 0; i < fileList.length; i++) {
46
- const file = fileList[i];
47
- if (file) {
48
- sum += file.size;
49
- }
50
- }
51
- return sum;
36
+ return Array.from(fileList).reduce((sum, file) => sum + file.size, 0);
52
37
  }
@@ -26,7 +26,8 @@ async function buildCollectionRelative(dir, relativePath) {
26
26
  if (entry.isFile()) {
27
27
  collection.push({
28
28
  path: entryPath,
29
- data: new Uint8Array(await fs.promises.readFile(fullPath))
29
+ size: (await fs.promises.stat(fullPath)).size,
30
+ fsPath: fullPath
30
31
  });
31
32
  } else if (entry.isDirectory()) {
32
33
  collection = [...(await buildCollectionRelative(dir, entryPath)), ...collection];
@@ -1,60 +1,3 @@
1
- import { isNodeReadable, isReadableStream } from "./stream.js";
2
- /**
3
- * Validates input and converts to Uint8Array
4
- *
5
- * @param data any string, ArrayBuffer or Uint8Array
6
- */
7
- export async function prepareData(data) {
8
- if (typeof data === 'string') return new Blob([data], {
9
- type: 'text/plain'
10
- });
11
- if (data instanceof Uint8Array || data instanceof ArrayBuffer) {
12
- return new Blob([data], {
13
- type: 'application/octet-stream'
14
- });
15
- }
16
- if (data instanceof Blob) {
17
- return data;
18
- }
19
- // Currently it is not possible to stream requests from browsers
20
- // there are already first experiments on this field (Chromium)
21
- // but till it is fully implemented across browsers-land we have to
22
- // buffer the data before sending the requests.
23
- if (isNodeReadable(data)) {
24
- return new Promise(resolve => {
25
- const buffers = [];
26
- data.on('data', d => {
27
- buffers.push(d);
28
- });
29
- data.on('end', () => {
30
- resolve(new Blob(buffers, {
31
- type: 'application/octet-stream'
32
- }));
33
- });
34
- });
35
- }
36
- if (isReadableStream(data)) {
37
- return new Promise(async resolve => {
38
- const reader = data.getReader();
39
- const buffers = [];
40
- let done, value;
41
- do {
42
- ;
43
- ({
44
- done,
45
- value
46
- } = await reader.read());
47
- if (!done) {
48
- buffers.push(value);
49
- }
50
- } while (!done);
51
- resolve(new Blob(buffers, {
52
- type: 'application/octet-stream'
53
- }));
54
- });
55
- }
56
- throw new TypeError('unknown data type');
57
- }
58
1
  export async function prepareWebsocketData(data) {
59
2
  if (typeof data === 'string') return new TextEncoder().encode(data);
60
3
  if (data instanceof ArrayBuffer) return new Uint8Array(data);
@@ -10,13 +10,9 @@ export class BeeArgumentError extends BeeError {
10
10
  }
11
11
  }
12
12
  export class BeeResponseError extends BeeError {
13
- constructor(message, code, axiosStatus, status, config, request, response) {
13
+ constructor(message, status, statusText) {
14
14
  super(message);
15
- this.code = code;
16
- this.axiosStatus = axiosStatus;
17
15
  this.status = status;
18
- this.config = config;
19
- this.request = request;
20
- this.response = response;
16
+ this.statusText = statusText;
21
17
  }
22
18
  }
@@ -4,8 +4,8 @@ const {
4
4
  keccak256,
5
5
  sha3_256
6
6
  } = pkg;
7
- import { hexToBytes, intToHex, makeHexString, assertHexString } from "./hex.js";
8
7
  import { assertBytes } from "./bytes.js";
8
+ import { assertHexString, hexToBytes, intToHex, makeHexString } from "./hex.js";
9
9
  const ETH_ADDR_BYTES_LENGTH = 20;
10
10
  const ETH_ADDR_HEX_LENGTH = 40;
11
11
  export function makeEthAddress(address) {
@@ -3,7 +3,6 @@ export { getFolderSize } from "./collection.node.js";
3
3
  export { assertBytes, assertFlexBytes, bytesAtOffset, bytesEqual, flexBytesAtOffset, isBytes, isFlexBytes } from "./bytes.js";
4
4
  export { assertHexString, assertPrefixedHexString, bytesToHex, hexToBytes, intToHex, isHexString, makeHexString } from "./hex.js";
5
5
  export { ethToSwarmAddress, fromLittleEndian, isHexEthAddress, makeEthAddress, makeEthereumWalletSigner, makeHexEthAddress, toLittleEndian } from "./eth.js";
6
- export { isNodeReadable, isReadable, isReadableStream, normalizeToReadableStream, readableNodeToWeb, readableWebToNode } from "./stream.js";
7
6
  export { keccak256Hash } from "./hash.js";
8
7
  export { makeMaxTarget } from "./pss.js";
9
8
  export { getAmountForTtl, getDepthForCapacity, getStampCostInBzz, getStampCostInPlur, getStampEffectiveBytes, getStampMaximumCapacityBytes, getStampTtlSeconds, getStampUsage } from "./stamps.js";
@@ -1,6 +1,9 @@
1
- import axios, { AxiosError } from 'axios';
1
+ import axios from 'axios';
2
2
  import { Objects, Strings } from 'cafe-utility';
3
3
  import { BeeResponseError } from "../index.js";
4
+ const {
5
+ AxiosError
6
+ } = axios;
4
7
  export const DEFAULT_HTTP_CONFIG = {
5
8
  headers: {
6
9
  accept: 'application/json, text/plain, */*'
@@ -22,7 +25,7 @@ export async function http(options, config) {
22
25
  return response;
23
26
  } catch (e) {
24
27
  if (e instanceof AxiosError) {
25
- throw new BeeResponseError(e.message, e.code, e.status, e.response?.status, e.config, e.request, e.response);
28
+ throw new BeeResponseError(e.message, e.status, e.code);
26
29
  }
27
30
  throw e;
28
31
  }
@@ -0,0 +1,22 @@
1
+ import { extractCollectionUploadHeaders } from "../modules/bzz.js";
2
+ import { http } from "./http.js";
3
+ import { TarStream } from "./tar.js";
4
+ import { writeTar } from "./tar-writer.js";
5
+ const bzzEndpoint = 'bzz';
6
+ export async function uploadTar(requestOptions, collection, postageBatchId, options) {
7
+ const tarStream = new TarStream();
8
+ await writeTar(collection, tarStream);
9
+ await tarStream.end();
10
+ const response = await http(requestOptions, {
11
+ method: 'post',
12
+ url: bzzEndpoint,
13
+ data: tarStream.output,
14
+ responseType: 'json',
15
+ headers: {
16
+ 'content-type': 'application/x-tar',
17
+ 'swarm-collection': 'true',
18
+ ...extractCollectionUploadHeaders(postageBatchId, options)
19
+ }
20
+ });
21
+ return response;
22
+ }
@@ -0,0 +1,23 @@
1
+ import { extractCollectionUploadHeaders } from "../modules/bzz.js";
2
+ import { http } from "./http.js";
3
+ import { TarStream } from "./tar.js";
4
+ import { writeTar } from "./tar-writer.js";
5
+ const bzzEndpoint = 'bzz';
6
+ export async function uploadTar(requestOptions, collection, postageBatchId, options) {
7
+ const tarStream = new TarStream();
8
+ const responsePromise = http(requestOptions, {
9
+ method: 'post',
10
+ url: bzzEndpoint,
11
+ data: tarStream.output,
12
+ responseType: 'json',
13
+ headers: {
14
+ 'content-type': 'application/x-tar',
15
+ 'swarm-collection': 'true',
16
+ ...extractCollectionUploadHeaders(postageBatchId, options)
17
+ }
18
+ });
19
+ await writeTar(collection, tarStream);
20
+ await tarStream.end();
21
+ const response = await responsePromise;
22
+ return response;
23
+ }
@@ -0,0 +1,12 @@
1
+ import { fileArrayBuffer } from "./file.js";
2
+ export async function writeTar(collection, tarStream) {
3
+ for (const item of collection) {
4
+ if (item.file) {
5
+ await tarStream.beginFile(item.path, item.file.size);
6
+ await tarStream.appendFile(new Uint8Array(await fileArrayBuffer(item.file)));
7
+ await tarStream.endFile();
8
+ } else {
9
+ throw new Error('Invalid collection item');
10
+ }
11
+ }
12
+ }
@@ -0,0 +1,16 @@
1
+ import { createReadStream } from 'fs';
2
+ export async function writeTar(collection, tarStream) {
3
+ for (const item of collection) {
4
+ await tarStream.beginFile(item.path, item.size);
5
+ if (item.fsPath) {
6
+ const stream = createReadStream(item.fsPath);
7
+ for await (const chunk of stream) {
8
+ await tarStream.appendFile(chunk);
9
+ }
10
+ await tarStream.endFile();
11
+ stream.close();
12
+ } else {
13
+ throw new Error('Invalid collection item');
14
+ }
15
+ }
16
+ }
@@ -0,0 +1,61 @@
1
+ export class TarStream {
2
+ constructor() {
3
+ this.pieces = [];
4
+ this.currentFileSize = 0;
5
+ }
6
+ get output() {
7
+ return this.pieces.reduce((acc, piece) => {
8
+ const newAcc = new Uint8Array(acc.length + piece.length);
9
+ newAcc.set(acc);
10
+ newAcc.set(piece, acc.length);
11
+ return newAcc;
12
+ });
13
+ }
14
+ async beginFile(path, size) {
15
+ const header = createHeader(path, size);
16
+ this.pieces.push(header);
17
+ this.currentFileSize = 0;
18
+ }
19
+ async appendFile(data) {
20
+ this.pieces.push(data);
21
+ this.currentFileSize += data.length;
22
+ }
23
+ async endFile() {
24
+ const padding = 512 - this.currentFileSize % 512;
25
+ this.pieces.push(new Uint8Array(padding));
26
+ }
27
+ async end() {
28
+ this.pieces.push(createEndOfArchive());
29
+ }
30
+ }
31
+ function createHeader(path, size) {
32
+ const header = new Uint8Array(512); // Initialize header with zeros
33
+ const encoder = new TextEncoder();
34
+ function writeToBuffer(str, offset, length) {
35
+ const bytes = encoder.encode(str);
36
+ header.set(bytes.slice(0, length), offset);
37
+ }
38
+ writeToBuffer(path, 0, 100); // File name
39
+ writeToBuffer('0000777', 100, 8); // File mode
40
+ writeToBuffer('0001750', 108, 8); // UID
41
+ writeToBuffer('0001750', 116, 8); // GID
42
+ writeToBuffer(size.toString(8).padStart(11, '0') + ' ', 124, 12); // File size
43
+ writeToBuffer(Math.floor(Date.now() / 1000).toString(8) + ' ', 136, 12); // Mod time
44
+ writeToBuffer(' ', 148, 8); // Checksum placeholder
45
+ writeToBuffer('0', 156, 1); // Typeflag
46
+ writeToBuffer('ustar ', 257, 8); // Magic and version
47
+ for (let i = 345; i < 512; i++) {
48
+ header[i] = 0; // Fill remaining with zeros
49
+ }
50
+ // Calculate checksum
51
+ let checksum = 0;
52
+ for (let i = 0; i < 512; i++) {
53
+ checksum += header[i];
54
+ }
55
+ // Write checksum
56
+ writeToBuffer(checksum.toString(8).padStart(6, '0') + '\0 ', 148, 8);
57
+ return header;
58
+ }
59
+ function createEndOfArchive() {
60
+ return new Uint8Array(1024);
61
+ }
@@ -1,18 +1,47 @@
1
- import Tar from 'tar-js';
2
- // converts a string to utf8 Uint8Array and returns it as a string-like
3
- // object that `tar.append` accepts as path
4
- function fixUnicodePath(path) {
5
- const codes = new TextEncoder().encode(path);
6
- return {
7
- length: codes.length,
8
- charCodeAt: index => codes[index]
9
- };
10
- }
11
- export function makeTar(data) {
12
- const tar = new Tar(1);
13
- for (const entry of data) {
14
- const path = fixUnicodePath(entry.path);
15
- tar.append(path, entry.data);
1
+ import { PassThrough } from 'stream';
2
+ export class TarStream {
3
+ constructor() {
4
+ this.output = new PassThrough();
5
+ this.currentFileSize = 0;
6
+ }
7
+ async beginFile(path, size) {
8
+ const header = createHeader(path, size);
9
+ this.output.write(header);
10
+ this.currentFileSize = 0;
11
+ }
12
+ async appendFile(data) {
13
+ return new Promise(resolve => {
14
+ this.output.write(data, () => {
15
+ resolve();
16
+ });
17
+ this.currentFileSize += data.length;
18
+ });
19
+ }
20
+ async endFile() {
21
+ const padding = 512 - this.currentFileSize % 512;
22
+ this.output.write(Buffer.alloc(padding, 0));
16
23
  }
17
- return tar.out;
24
+ async end() {
25
+ this.output.write(createEndOfArchive());
26
+ this.output.end();
27
+ }
28
+ }
29
+ function createHeader(path, size) {
30
+ const header = Buffer.alloc(512, 0); // Initialize header with zeros
31
+ header.write(path, 0, 100); // File name
32
+ header.write('0000777', 100, 8); // File mode
33
+ header.write('0001750', 108, 8); // UID
34
+ header.write('0001750', 116, 8); // GID
35
+ header.write(size.toString(8).padStart(11, '0') + ' ', 124, 12); // File size
36
+ header.write(Math.floor(new Date().getTime() / 1000).toString(8) + ' ', 136, 12); // Mod time
37
+ header.write(' ', 148, 8); // Checksum placeholder
38
+ header.write('0', 156, 1); // Typeflag
39
+ header.write('ustar ', 257, 8); // Magic and version
40
+ header.write('0'.repeat(8 * 12), 345, 8 * 12); // Fill remaining with zeros
41
+ const checksum = header.reduce((sum, elem) => sum + elem, 0);
42
+ header.write(checksum.toString(8).padStart(6, '0') + '\0 ', 148, 8); // Write checksum
43
+ return header;
44
+ }
45
+ function createEndOfArchive() {
46
+ return Buffer.alloc(1024, 0);
18
47
  }
@@ -1,9 +1,12 @@
1
1
  import { decodeCid, encodeReference } from '@ethersphere/swarm-cid';
2
+ import { Readable } from 'stream';
2
3
  import { ADDRESS_HEX_LENGTH, BATCH_ID_HEX_LENGTH, ENCRYPTED_REFERENCE_HEX_LENGTH, PSS_TARGET_HEX_LENGTH_MAX, PUBKEY_HEX_LENGTH, REFERENCE_HEX_LENGTH, TAGS_LIMIT_MAX, TAGS_LIMIT_MIN } from "../types/index.js";
3
4
  import { BeeArgumentError, BeeError } from "./error.js";
4
5
  import { isFile } from "./file.js";
5
6
  import { assertHexString, assertPrefixedHexString, isHexString } from "./hex.js";
6
- import { isReadable } from "./stream.js";
7
+ export function isReadable(obj) {
8
+ return typeof Readable !== 'undefined' && obj instanceof Readable;
9
+ }
7
10
  export function isUint8Array(obj) {
8
11
  return obj instanceof Uint8Array;
9
12
  }
@@ -18,12 +18,7 @@ export function isValidBeeUrl(url) {
18
18
  // There can be wide range of protocols passed.
19
19
  return urlObject.protocol === 'http:' || urlObject.protocol === 'https:';
20
20
  } catch (e) {
21
- // URL constructor throws TypeError if not valid URL
22
- // TODO: Drop the `.code` hack for NodeJS environment: https://github.com/ethersphere/bee-js/issues/204
23
- if (e instanceof TypeError || isNodeJsError(e) && e.code === 'ERR_INVALID_URL') {
24
- return false;
25
- }
26
- throw e;
21
+ return false;
27
22
  }
28
23
  }
29
24
  /**