@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.
- package/README.md +107 -65
- package/dist/cjs/bee.js +533 -6
- package/dist/cjs/chunk/cac.js +3 -3
- package/dist/cjs/chunk/soc.js +5 -5
- package/dist/cjs/chunk/span.js +2 -6
- package/dist/cjs/feed/identifier.js +2 -2
- package/dist/cjs/feed/index.js +5 -6
- package/dist/cjs/feed/retrievable.js +2 -2
- package/dist/cjs/index.js +1 -3
- package/dist/cjs/modules/bytes.js +3 -3
- package/dist/cjs/modules/bzz.js +9 -20
- package/dist/cjs/modules/chunk.js +2 -2
- package/dist/cjs/modules/debug/balance.js +4 -4
- package/dist/cjs/modules/debug/chequebook.js +8 -8
- package/dist/cjs/modules/debug/chunk.js +2 -19
- package/dist/cjs/modules/debug/settlements.js +2 -2
- package/dist/cjs/modules/debug/stake.js +3 -3
- package/dist/cjs/modules/debug/states.js +4 -19
- package/dist/cjs/modules/debug/status.js +15 -32
- package/dist/cjs/modules/debug/tag.js +1 -1
- package/dist/cjs/modules/debug/transactions.js +4 -4
- package/dist/cjs/modules/feed.js +6 -7
- package/dist/cjs/modules/pinning.js +4 -4
- package/dist/cjs/modules/pss.js +1 -1
- package/dist/cjs/modules/soc.js +1 -1
- package/dist/cjs/modules/status.js +1 -1
- package/dist/cjs/modules/stewardship.js +1 -1
- package/dist/cjs/package.json +4 -0
- package/dist/cjs/types/index.js +5 -1
- package/dist/cjs/utils/collection.js +7 -22
- package/dist/cjs/utils/collection.node.js +2 -1
- package/dist/cjs/utils/data.browser.js +1 -49
- package/dist/cjs/utils/error.js +2 -6
- package/dist/cjs/utils/eth.js +1 -1
- package/dist/cjs/utils/expose.js +1 -8
- package/dist/cjs/utils/file.js +2 -2
- package/dist/cjs/utils/http.js +6 -25
- package/dist/cjs/utils/tar-uploader.browser.js +26 -0
- package/dist/cjs/utils/tar-uploader.js +27 -0
- package/dist/cjs/utils/tar-writer.browser.js +17 -0
- package/dist/cjs/utils/tar-writer.js +21 -0
- package/dist/cjs/utils/tar.browser.js +65 -0
- package/dist/cjs/utils/tar.js +47 -21
- package/dist/cjs/utils/type.js +7 -3
- package/dist/cjs/utils/url.js +1 -6
- package/dist/index.browser.min.js +1 -1
- package/dist/index.browser.min.js.LICENSE.txt +0 -42
- package/dist/index.browser.min.js.map +1 -1
- package/dist/mjs/bee.js +533 -9
- package/dist/mjs/chunk/cac.js +4 -4
- package/dist/mjs/chunk/soc.js +6 -6
- package/dist/mjs/chunk/span.js +2 -6
- package/dist/mjs/feed/identifier.js +2 -2
- package/dist/mjs/feed/index.js +5 -9
- package/dist/mjs/feed/retrievable.js +2 -2
- package/dist/mjs/index.js +1 -2
- package/dist/mjs/modules/bytes.js +3 -3
- package/dist/mjs/modules/bzz.js +8 -20
- package/dist/mjs/modules/chunk.js +2 -2
- package/dist/mjs/modules/debug/balance.js +4 -4
- package/dist/mjs/modules/debug/chequebook.js +8 -8
- package/dist/mjs/modules/debug/chunk.js +1 -17
- package/dist/mjs/modules/debug/settlements.js +2 -2
- package/dist/mjs/modules/debug/stake.js +3 -3
- package/dist/mjs/modules/debug/states.js +4 -19
- package/dist/mjs/modules/debug/status.js +15 -34
- package/dist/mjs/modules/debug/tag.js +1 -1
- package/dist/mjs/modules/debug/transactions.js +4 -4
- package/dist/mjs/modules/feed.js +6 -7
- package/dist/mjs/modules/pinning.js +4 -4
- package/dist/mjs/modules/pss.js +1 -1
- package/dist/mjs/modules/soc.js +1 -1
- package/dist/mjs/modules/status.js +1 -1
- package/dist/mjs/modules/stewardship.js +1 -1
- package/dist/mjs/package.json +6 -2
- package/dist/mjs/types/index.js +4 -0
- package/dist/mjs/utils/collection.js +7 -22
- package/dist/mjs/utils/collection.node.js +2 -1
- package/dist/mjs/utils/data.browser.js +0 -57
- package/dist/mjs/utils/error.js +2 -6
- package/dist/mjs/utils/eth.js +1 -1
- package/dist/mjs/utils/expose.js +0 -1
- package/dist/mjs/utils/http.js +5 -2
- package/dist/mjs/utils/tar-uploader.browser.js +22 -0
- package/dist/mjs/utils/tar-uploader.js +23 -0
- package/dist/mjs/utils/tar-writer.browser.js +12 -0
- package/dist/mjs/utils/tar-writer.js +16 -0
- package/dist/mjs/utils/tar.browser.js +61 -0
- package/dist/mjs/utils/tar.js +45 -16
- package/dist/mjs/utils/type.js +4 -1
- package/dist/mjs/utils/url.js +1 -6
- package/dist/types/bee.d.ts +305 -4
- package/dist/types/chunk/soc.d.ts +2 -2
- package/dist/types/feed/identifier.d.ts +1 -1
- package/dist/types/index.d.ts +1 -3
- package/dist/types/modules/bytes.d.ts +3 -3
- package/dist/types/modules/bzz.d.ts +14 -6
- package/dist/types/modules/chunk.d.ts +2 -2
- package/dist/types/modules/debug/balance.d.ts +4 -4
- package/dist/types/modules/debug/chequebook.d.ts +8 -8
- package/dist/types/modules/debug/chunk.d.ts +1 -10
- package/dist/types/modules/debug/settlements.d.ts +2 -2
- package/dist/types/modules/debug/stake.d.ts +3 -3
- package/dist/types/modules/debug/states.d.ts +3 -3
- package/dist/types/modules/debug/status.d.ts +10 -20
- package/dist/types/modules/debug/tag.d.ts +1 -1
- package/dist/types/modules/debug/transactions.d.ts +4 -4
- package/dist/types/modules/feed.d.ts +6 -9
- package/dist/types/modules/pinning.d.ts +4 -4
- package/dist/types/modules/pss.d.ts +1 -1
- package/dist/types/modules/soc.d.ts +1 -1
- package/dist/types/modules/status.d.ts +1 -1
- package/dist/types/modules/stewardship.d.ts +1 -1
- package/dist/types/types/debug.d.ts +0 -3
- package/dist/types/types/index.d.ts +9 -17
- package/dist/types/utils/collection.browser.d.ts +1 -1
- package/dist/types/utils/collection.d.ts +3 -3
- package/dist/types/utils/collection.node.d.ts +1 -1
- package/dist/types/utils/data.browser.d.ts +0 -7
- package/dist/types/utils/data.d.ts +1 -2
- package/dist/types/utils/error.d.ts +2 -7
- package/dist/types/utils/eth.d.ts +1 -1
- package/dist/types/utils/expose.d.ts +0 -1
- package/dist/types/utils/tar-uploader.browser.d.ts +4 -0
- package/dist/types/utils/tar-uploader.d.ts +4 -0
- package/dist/types/utils/tar-writer.browser.d.ts +3 -0
- package/dist/types/utils/tar-writer.d.ts +3 -0
- package/dist/types/utils/tar.browser.d.ts +9 -0
- package/dist/types/utils/tar.d.ts +10 -2
- package/dist/types/utils/type.d.ts +4 -1
- package/package.json +4 -19
- package/dist/cjs/bee-debug.js +0 -590
- package/dist/cjs/chunk/serialize.js +0 -19
- package/dist/cjs/utils/stream.js +0 -146
- package/dist/cjs/utils/uint64.js +0 -29
- package/dist/mjs/bee-debug.js +0 -560
- package/dist/mjs/chunk/serialize.js +0 -15
- package/dist/mjs/utils/stream.js +0 -129
- package/dist/mjs/utils/uint64.js +0 -22
- package/dist/types/bee-debug.d.ts +0 -323
- package/dist/types/chunk/serialize.d.ts +0 -6
- package/dist/types/utils/stream.d.ts +0 -40
- package/dist/types/utils/uint64.d.ts +0 -4
package/dist/mjs/modules/pss.js
CHANGED
|
@@ -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
|
|
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
|
package/dist/mjs/modules/soc.js
CHANGED
|
@@ -4,7 +4,7 @@ const socEndpoint = 'soc';
|
|
|
4
4
|
/**
|
|
5
5
|
* Upload single owner chunk (SOC) to a Bee node
|
|
6
6
|
*
|
|
7
|
-
* @param
|
|
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
|
|
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
|
|
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
|
package/dist/mjs/package.json
CHANGED
|
@@ -2,7 +2,11 @@
|
|
|
2
2
|
"type": "module",
|
|
3
3
|
"browser": {
|
|
4
4
|
"stream": false,
|
|
5
|
-
|
|
6
|
-
|
|
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
|
}
|
package/dist/mjs/types/index.js
CHANGED
|
@@ -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.
|
|
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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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);
|
package/dist/mjs/utils/error.js
CHANGED
|
@@ -10,13 +10,9 @@ export class BeeArgumentError extends BeeError {
|
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
export class BeeResponseError extends BeeError {
|
|
13
|
-
constructor(message,
|
|
13
|
+
constructor(message, status, statusText) {
|
|
14
14
|
super(message);
|
|
15
|
-
this.code = code;
|
|
16
|
-
this.axiosStatus = axiosStatus;
|
|
17
15
|
this.status = status;
|
|
18
|
-
this.
|
|
19
|
-
this.request = request;
|
|
20
|
-
this.response = response;
|
|
16
|
+
this.statusText = statusText;
|
|
21
17
|
}
|
|
22
18
|
}
|
package/dist/mjs/utils/eth.js
CHANGED
|
@@ -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) {
|
package/dist/mjs/utils/expose.js
CHANGED
|
@@ -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";
|
package/dist/mjs/utils/http.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import 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.
|
|
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
|
+
}
|
package/dist/mjs/utils/tar.js
CHANGED
|
@@ -1,18 +1,47 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
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
|
}
|
package/dist/mjs/utils/type.js
CHANGED
|
@@ -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
|
-
|
|
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
|
}
|
package/dist/mjs/utils/url.js
CHANGED
|
@@ -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
|
-
|
|
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
|
/**
|