@ethersphere/bee-js 8.2.0 → 8.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/mjs/bee.js CHANGED
@@ -1,4 +1,3 @@
1
- import { ReferenceType } from '@ethersphere/swarm-cid';
2
1
  import { Objects, System } from 'cafe-utility';
3
2
  import { makeSigner } from "./chunk/signer.js";
4
3
  import { downloadSingleOwnerChunk, uploadSingleOwnerChunkData } from "./chunk/soc.js";
@@ -62,7 +61,9 @@ export class Bee {
62
61
  baseURL: this.url,
63
62
  timeout: options?.timeout ?? false,
64
63
  headers: options?.headers,
65
- onRequest: options?.onRequest
64
+ onRequest: options?.onRequest,
65
+ httpAgent: options?.httpAgent,
66
+ httpsAgent: options?.httpsAgent
66
67
  };
67
68
  }
68
69
  /**
@@ -245,14 +246,14 @@ export class Bee {
245
246
  contentType,
246
247
  ...options
247
248
  };
248
- return addCidConversionFunction(await bzz.uploadFile(this.getRequestOptionsForCall(requestOptions), fileData, postageBatchId, fileName, fileOptions), ReferenceType.MANIFEST);
249
+ return addCidConversionFunction(await bzz.uploadFile(this.getRequestOptionsForCall(requestOptions), fileData, postageBatchId, fileName, fileOptions), 'manifest');
249
250
  } else if (isReadable(data) && options?.tag && !options.size) {
250
251
  // TODO: Needed until https://github.com/ethersphere/bee/issues/2317 is resolved
251
252
  const result = await bzz.uploadFile(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, name, options);
252
253
  await this.updateTag(options.tag, result.reference);
253
- return addCidConversionFunction(result, ReferenceType.MANIFEST);
254
+ return addCidConversionFunction(result, 'manifest');
254
255
  } else {
255
- return addCidConversionFunction(await bzz.uploadFile(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, name, options), ReferenceType.MANIFEST);
256
+ return addCidConversionFunction(await bzz.uploadFile(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, name, options), 'manifest');
256
257
  }
257
258
  }
258
259
  /**
@@ -269,7 +270,7 @@ export class Bee {
269
270
  */
270
271
  async downloadFile(reference, path = '', options) {
271
272
  assertRequestOptions(options);
272
- reference = makeReferenceOrEns(reference, ReferenceType.MANIFEST);
273
+ reference = makeReferenceOrEns(reference, 'manifest');
273
274
  return bzz.downloadFile(this.getRequestOptionsForCall(options), reference, path);
274
275
  }
275
276
  /**
@@ -286,7 +287,7 @@ export class Bee {
286
287
  */
287
288
  async downloadReadableFile(reference, path = '', options) {
288
289
  assertRequestOptions(options);
289
- reference = makeReferenceOrEns(reference, ReferenceType.MANIFEST);
290
+ reference = makeReferenceOrEns(reference, 'manifest');
290
291
  return bzz.downloadFileReadable(this.getRequestOptionsForCall(options), reference, path);
291
292
  }
292
293
  /**
@@ -312,7 +313,7 @@ export class Bee {
312
313
  assertCollectionUploadOptions(options);
313
314
  }
314
315
  const data = await makeCollectionFromFileList(fileList);
315
- return addCidConversionFunction(await bzz.uploadCollection(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, options), ReferenceType.MANIFEST);
316
+ return addCidConversionFunction(await bzz.uploadCollection(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, options), 'manifest');
316
317
  }
317
318
  /**
318
319
  * Upload Collection that you can assembly yourself.
@@ -329,7 +330,7 @@ export class Bee {
329
330
  assertBatchId(postageBatchId);
330
331
  assertCollection(collection);
331
332
  if (options) assertCollectionUploadOptions(options);
332
- return addCidConversionFunction(await bzz.uploadCollection(this.getRequestOptionsForCall(this.requestOptions), collection, postageBatchId, options), ReferenceType.MANIFEST);
333
+ return addCidConversionFunction(await bzz.uploadCollection(this.getRequestOptionsForCall(this.requestOptions), collection, postageBatchId, options), 'manifest');
333
334
  }
334
335
  /**
335
336
  * Upload collection of files.
@@ -352,7 +353,7 @@ export class Bee {
352
353
  assertRequestOptions(requestOptions);
353
354
  if (options) assertCollectionUploadOptions(options);
354
355
  const data = await makeCollectionFromFS(dir);
355
- return addCidConversionFunction(await bzz.uploadCollection(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, options), ReferenceType.MANIFEST);
356
+ return addCidConversionFunction(await bzz.uploadCollection(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, options), 'manifest');
356
357
  }
357
358
  /**
358
359
  * Create a new Tag which is meant for tracking progres of syncing data across network.
@@ -716,7 +717,7 @@ export class Bee {
716
717
  const reference = await createFeedManifest(this.getRequestOptionsForCall(options), canonicalOwner, canonicalTopic, stamp);
717
718
  return addCidConversionFunction({
718
719
  reference
719
- }, ReferenceType.FEED);
720
+ }, 'feed');
720
721
  }
721
722
  /**
722
723
  * Make a new feed reader for downloading feed updates.
package/dist/mjs/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { Bee } from "./bee.js";
2
2
  export { SUPPORTED_BEE_VERSION, SUPPORTED_BEE_VERSION_EXACT } from "./modules/debug/status.js";
3
3
  export * from "./types/index.js";
4
+ export * from "./utils/constants.js";
4
5
  export * from "./utils/error.js";
5
6
  export * as Utils from "./utils/expose.js";
6
7
  export { Bee };
@@ -10,7 +10,7 @@ export async function getGrantees(reference, requestOptions) {
10
10
  return {
11
11
  status: response.status,
12
12
  statusText: response.statusText,
13
- data: response.data.data
13
+ data: response.data
14
14
  };
15
15
  }
16
16
  export async function createGrantees(requestOptions, postageBatchId, grantees) {
@@ -18,7 +18,7 @@ export async function createGrantees(requestOptions, postageBatchId, grantees) {
18
18
  method: 'post',
19
19
  url: granteeEndpoint,
20
20
  data: {
21
- grantees: grantees
21
+ grantees
22
22
  },
23
23
  headers: {
24
24
  ...extractRedundantUploadHeaders(postageBatchId)
@@ -0,0 +1,33 @@
1
+ import { Binary } from 'cafe-utility';
2
+ export const SWARM_MANIFEST_CODEC = 0xfa;
3
+ export const SWARM_FEED_CODEC = 0xfb;
4
+ const CODEC_TABLE = {
5
+ [SWARM_MANIFEST_CODEC]: 'manifest',
6
+ [SWARM_FEED_CODEC]: 'feed'
7
+ };
8
+ export function convertReferenceToCid(reference, type) {
9
+ if (reference.length !== 64) {
10
+ throw TypeError('Only 32-byte, non-encrypted references are supported');
11
+ }
12
+ const base32 = 'b';
13
+ const version = new Uint8Array([1]);
14
+ const codec = new Uint8Array([type === 'feed' ? SWARM_FEED_CODEC : SWARM_MANIFEST_CODEC]);
15
+ const unknown = new Uint8Array([1]);
16
+ const sha256 = new Uint8Array([27]);
17
+ const size = new Uint8Array([32]);
18
+ const header = Binary.uint8ArrayToBase32(Binary.concatBytes(version, codec, unknown, sha256, size)).replace(/\=+$/, '');
19
+ const hash = Binary.uint8ArrayToBase32(Binary.hexToUint8Array(reference)).replace(/\=+$/, '');
20
+ return `${base32}${header}${hash}`.toLowerCase();
21
+ }
22
+ export function convertCidToReference(cid) {
23
+ const bytes = Binary.base32ToUint8Array(cid.toUpperCase().slice(1));
24
+ const codec = bytes[1];
25
+ if (!CODEC_TABLE[codec]) {
26
+ throw new Error('Unknown codec');
27
+ }
28
+ const reference = bytes.slice(-32);
29
+ return {
30
+ type: CODEC_TABLE[codec],
31
+ reference: Binary.uint8ArrayToHex(reference)
32
+ };
33
+ }
@@ -0,0 +1,3 @@
1
+ export const SWARM_GATEWAY_URL = 'https://api.gateway.ethswarm.org';
2
+ export const NULL_STAMP = '0000000000000000000000000000000000000000000000000000000000000000';
3
+ export const NULL_TOPIC = '0000000000000000000000000000000000000000000000000000000000000000';
@@ -7,5 +7,4 @@ export { keccak256Hash } from "./hash.js";
7
7
  export { makeMaxTarget } from "./pss.js";
8
8
  export { getAmountForTtl, getDepthForCapacity, getStampCostInBzz, getStampCostInPlur, getStampEffectiveBytes, getStampMaximumCapacityBytes, getStampTtlSeconds, getStampUsage } from "./stamps.js";
9
9
  export { approximateOverheadForRedundancyLevel, getRedundancyStat, getRedundancyStats } from "./redundancy.js";
10
- export const NULL_STAMP = '0000000000000000000000000000000000000000000000000000000000000000';
11
- export const NULL_TOPIC = '0000000000000000000000000000000000000000000000000000000000000000';
10
+ export { convertCidToReference, convertReferenceToCid } from "./cid.js";
@@ -21,13 +21,6 @@ export async function http(options, config) {
21
21
  const requestConfig = Objects.deepMerge3(DEFAULT_HTTP_CONFIG, config, options);
22
22
  maybeRunOnRequestHook(options, requestConfig);
23
23
  const response = await axios(requestConfig);
24
- // Axios does not parse array of strings as JSON
25
- if (Array.isArray(response.data) && response.data.every(element => typeof element === 'string')) {
26
- const array = response.data;
27
- response.data = {
28
- data: array
29
- };
30
- }
31
24
  // TODO: https://github.com/axios/axios/pull/6253
32
25
  return response;
33
26
  } catch (e) {
@@ -12,6 +12,9 @@ export class TarStream {
12
12
  });
13
13
  }
14
14
  beginFile(path, size) {
15
+ if (path.length > 100) {
16
+ throw new Error(`File name too long: ${path}`);
17
+ }
15
18
  const header = createHeader(path, size);
16
19
  this.pieces.push(header);
17
20
  this.currentFileSize = 0;
@@ -5,6 +5,9 @@ export class TarStream {
5
5
  this.currentFileSize = 0;
6
6
  }
7
7
  beginFile(path, size) {
8
+ if (path.length > 100) {
9
+ throw new Error(`File name too long: ${path}`);
10
+ }
8
11
  const header = createHeader(path, size);
9
12
  this.output.write(header);
10
13
  this.currentFileSize = 0;
@@ -1,6 +1,6 @@
1
- import { decodeCid, encodeReference } from '@ethersphere/swarm-cid';
2
1
  import { Readable } from 'stream';
3
2
  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
+ import { convertCidToReference, convertReferenceToCid } from "./cid.js";
4
4
  import { BeeArgumentError, BeeError } from "./error.js";
5
5
  import { isFile } from "./file.js";
6
6
  import { assertHexString, assertPrefixedHexString, isHexString } from "./hex.js";
@@ -110,7 +110,7 @@ export function makeReferenceOrEns(value, expectedCidType) {
110
110
  throw new TypeError('ReferenceCidOrEns has to be a string!');
111
111
  }
112
112
  try {
113
- const result = decodeCid(value);
113
+ const result = convertCidToReference(value);
114
114
  if (result.type !== expectedCidType) {
115
115
  throw new BeeError(`CID was expected to be of type ${expectedCidType}, but got instead ${result.type ?? 'non-Swarm CID'}`);
116
116
  }
@@ -124,13 +124,13 @@ export function makeReferenceOrEns(value, expectedCidType) {
124
124
  /**
125
125
  * Function that adds getter which converts the reference into CID base32 encoded string.
126
126
  * @param result
127
- * @param cidType Type as described in the @ethersphere/swarm-cids-js -> ReferenceType
127
+ * @param cidType feed or manifest
128
128
  */
129
129
  export function addCidConversionFunction(result, cidType) {
130
130
  return {
131
131
  ...result,
132
132
  cid() {
133
- return encodeReference(result.reference, cidType).toString();
133
+ return convertReferenceToCid(result.reference, cidType);
134
134
  }
135
135
  };
136
136
  }
@@ -1,6 +1,7 @@
1
1
  import { Bee } from './bee';
2
2
  export { SUPPORTED_BEE_VERSION, SUPPORTED_BEE_VERSION_EXACT } from './modules/debug/status';
3
3
  export * from './types';
4
+ export * from './utils/constants';
4
5
  export * from './utils/error';
5
6
  export * as Utils from './utils/expose';
6
7
  export { Bee };
@@ -7,9 +7,6 @@ import type { BeeError } from '../utils/error';
7
7
  import type { EthAddress, HexEthAddress } from '../utils/eth';
8
8
  import type { HexString } from '../utils/hex';
9
9
  export * from './debug';
10
- export interface Dictionary<T> {
11
- [Key: string]: T;
12
- }
13
10
  export declare const SPAN_SIZE = 8;
14
11
  export declare const SECTION_SIZE = 32;
15
12
  export declare const BRANCHES = 128;
@@ -73,6 +70,8 @@ export type BeeRequestOptions = {
73
70
  timeout?: number | false;
74
71
  headers?: Record<string, string>;
75
72
  onRequest?: (request: BeeRequest) => void;
73
+ httpAgent?: any;
74
+ httpsAgent?: any;
76
75
  };
77
76
  export interface BeeOptions extends BeeRequestOptions {
78
77
  /**
@@ -96,7 +95,6 @@ export interface UploadResultWithCid extends UploadResult {
96
95
  * Function that converts the reference into Swarm CIDs
97
96
  *
98
97
  * @throws TypeError if the reference is encrypted reference (eq. 128 chars long) which is not supported in CID
99
- * @see https://github.com/ethersphere/swarm-cid-js
100
98
  */
101
99
  cid: () => string;
102
100
  }
@@ -120,7 +118,6 @@ export interface UploadResult {
120
118
  export interface UploadOptions {
121
119
  /**
122
120
  * If set to true, an ACT will be created for the uploaded data.
123
- *
124
121
  */
125
122
  act?: boolean;
126
123
  /**
@@ -380,8 +377,6 @@ export interface FeedManifestResult {
380
377
  reference: Reference;
381
378
  /**
382
379
  * Function that converts the reference into Swarm Feed CID.
383
- *
384
- * @see https://github.com/ethersphere/swarm-cid-js
385
380
  */
386
381
  cid: () => string;
387
382
  }
@@ -0,0 +1,9 @@
1
+ import { Reference } from '..';
2
+ export declare const SWARM_MANIFEST_CODEC = 250;
3
+ export declare const SWARM_FEED_CODEC = 251;
4
+ export type DecodedCID = {
5
+ type: 'feed' | 'manifest';
6
+ reference: Reference;
7
+ };
8
+ export declare function convertReferenceToCid(reference: Reference | string, type: 'feed' | 'manifest'): string;
9
+ export declare function convertCidToReference(cid: string): DecodedCID;
@@ -0,0 +1,4 @@
1
+ import { BatchId, Topic } from '..';
2
+ export declare const SWARM_GATEWAY_URL = "https://api.gateway.ethswarm.org";
3
+ export declare const NULL_STAMP: BatchId;
4
+ export declare const NULL_TOPIC: Topic;
@@ -1,12 +1,10 @@
1
- import { BatchId, Topic } from '..';
2
1
  export { getCollectionSize } from './collection';
3
2
  export { getFolderSize } from './collection.node';
4
- export { assertBytes, assertFlexBytes, Bytes, bytesAtOffset, bytesEqual, FlexBytes, flexBytesAtOffset, isBytes, isFlexBytes, } from './bytes';
5
- export { assertHexString, assertPrefixedHexString, bytesToHex, HexString, hexToBytes, intToHex, isHexString, makeHexString, PrefixedHexString, } from './hex';
6
- export { capitalizeAddressERC55, EthAddress, ethToSwarmAddress, fromLittleEndian, isHexEthAddress, makeEthAddress, makeEthereumWalletSigner, makeHexEthAddress, toLittleEndian, } from './eth';
3
+ export { Bytes, FlexBytes, assertBytes, assertFlexBytes, bytesAtOffset, bytesEqual, flexBytesAtOffset, isBytes, isFlexBytes, } from './bytes';
4
+ export { HexString, PrefixedHexString, assertHexString, assertPrefixedHexString, bytesToHex, hexToBytes, intToHex, isHexString, makeHexString, } from './hex';
5
+ export { EthAddress, capitalizeAddressERC55, ethToSwarmAddress, fromLittleEndian, isHexEthAddress, makeEthAddress, makeEthereumWalletSigner, makeHexEthAddress, toLittleEndian, } from './eth';
7
6
  export { keccak256Hash } from './hash';
8
7
  export { makeMaxTarget } from './pss';
9
8
  export { getAmountForTtl, getDepthForCapacity, getStampCostInBzz, getStampCostInPlur, getStampEffectiveBytes, getStampMaximumCapacityBytes, getStampTtlSeconds, getStampUsage, } from './stamps';
10
9
  export { approximateOverheadForRedundancyLevel, getRedundancyStat, getRedundancyStats } from './redundancy';
11
- export declare const NULL_STAMP: BatchId;
12
- export declare const NULL_TOPIC: Topic;
10
+ export { convertCidToReference, convertReferenceToCid } from './cid';
@@ -1,5 +1,4 @@
1
1
  /// <reference types="node" />
2
- import { ReferenceType } from '@ethersphere/swarm-cid';
3
2
  import { Readable } from 'stream';
4
3
  import { Address, AddressPrefix, AllTagsOptions, BatchId, BeeRequestOptions, CashoutOptions, CollectionUploadOptions, FileUploadOptions, NumberString, PostageBatchOptions, PssMessageHandler, PublicKey, Reference, ReferenceOrEns, Tag, TransactionHash, TransactionOptions, UploadOptions } from '../types';
5
4
  export declare function isReadable(obj: unknown): obj is Readable;
@@ -25,15 +24,15 @@ export declare function assertReferenceOrEns(value: unknown): asserts value is R
25
24
  * @param value
26
25
  * @param expectedCidType
27
26
  */
28
- export declare function makeReferenceOrEns(value: unknown, expectedCidType: ReferenceType): ReferenceOrEns;
27
+ export declare function makeReferenceOrEns(value: unknown, expectedCidType: 'feed' | 'manifest'): ReferenceOrEns;
29
28
  /**
30
29
  * Function that adds getter which converts the reference into CID base32 encoded string.
31
30
  * @param result
32
- * @param cidType Type as described in the @ethersphere/swarm-cids-js -> ReferenceType
31
+ * @param cidType feed or manifest
33
32
  */
34
33
  export declare function addCidConversionFunction<T extends {
35
34
  reference: string;
36
- }>(result: T, cidType: ReferenceType): T & {
35
+ }>(result: T, cidType: 'feed' | 'manifest'): T & {
37
36
  cid: () => string;
38
37
  };
39
38
  export declare function assertAddress(value: unknown): asserts value is Address;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ethersphere/bee-js",
3
- "version": "8.2.0",
3
+ "version": "8.3.0",
4
4
  "description": "Javascript client for Bee",
5
5
  "keywords": [
6
6
  "bee",
@@ -61,9 +61,8 @@
61
61
  "bee": "npx @fairdatasociety/fdp-play@3.2.0 start --detach --fresh"
62
62
  },
63
63
  "dependencies": {
64
- "@ethersphere/swarm-cid": "^0.1.0",
65
64
  "axios": "^0.28.1",
66
- "cafe-utility": "^23.7.0",
65
+ "cafe-utility": "^23.10.0",
67
66
  "elliptic": "^6.5.4",
68
67
  "isomorphic-ws": "^4.0.1",
69
68
  "js-sha3": "^0.8.0",
@@ -79,7 +78,6 @@
79
78
  "@babel/preset-typescript": "^7.18.6",
80
79
  "@commitlint/cli": "^17.0.2",
81
80
  "@commitlint/config-conventional": "^17.4.2",
82
- "@fluffy-spoon/substitute": "^1.208.0",
83
81
  "@jest/types": "^29.6.3",
84
82
  "@naholyr/cross-env": "^1.0.0",
85
83
  "@types/elliptic": "^6.4.14",
@@ -100,7 +98,6 @@
100
98
  "husky": "^8.0.1",
101
99
  "jest": "^29.7.0",
102
100
  "nock": "^13.3.0",
103
- "playwright-test": "^8.1.2",
104
101
  "prettier": "^2.6.2",
105
102
  "rimraf": "^3.0.2",
106
103
  "terser-webpack-plugin": "^5.3.1",