@upcoming/bee-js 0.15.0 → 0.16.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
@@ -1145,24 +1145,24 @@ export class Bee {
1145
1145
  }
1146
1146
  return stamp;
1147
1147
  }
1148
- async buyStorage(gigabytes, duration, options, requestOptions) {
1148
+ async buyStorage(size, duration, options, requestOptions) {
1149
1149
  const chainState = await this.getChainState(requestOptions);
1150
1150
  const amount = getAmountForDuration(duration, chainState.currentPrice);
1151
- const depth = getDepthForSize(gigabytes);
1151
+ const depth = getDepthForSize(size);
1152
1152
  if (options) {
1153
1153
  options = preparePostageBatchOptions(options);
1154
1154
  }
1155
1155
  return this.createPostageBatch(amount, depth, options, requestOptions);
1156
1156
  }
1157
- async getStorageCost(gigabytes, duration, options) {
1157
+ async getStorageCost(size, duration, options) {
1158
1158
  const chainState = await this.getChainState(options);
1159
1159
  const amount = getAmountForDuration(duration, chainState.currentPrice);
1160
- const depth = getDepthForSize(gigabytes);
1160
+ const depth = getDepthForSize(size);
1161
1161
  return getStampCost(depth, amount);
1162
1162
  }
1163
- async extendStorageSize(postageBatchId, gigabytes, options) {
1163
+ async extendStorageSize(postageBatchId, size, options) {
1164
1164
  const batch = await this.getPostageBatch(postageBatchId, options);
1165
- const depth = getDepthForSize(gigabytes);
1165
+ const depth = getDepthForSize(size);
1166
1166
  const delta = depth - batch.depth;
1167
1167
  if (delta <= 0) {
1168
1168
  throw new BeeArgumentError('New depth has to be greater than the original depth', depth);
@@ -1176,18 +1176,18 @@ export class Bee {
1176
1176
  const amount = getAmountForDuration(duration, chainState.currentPrice);
1177
1177
  return this.topUpBatch(batch.batchID, amount, options);
1178
1178
  }
1179
- async getExtensionCost(postageBatchId, gigabytes, duration, options) {
1179
+ async getExtensionCost(postageBatchId, size, duration, options) {
1180
1180
  const batch = await this.getPostageBatch(postageBatchId, options);
1181
1181
  const chainState = await this.getChainState(options);
1182
1182
  const amount = getAmountForDuration(duration, chainState.currentPrice);
1183
- const depth = getDepthForSize(gigabytes);
1183
+ const depth = getDepthForSize(size);
1184
1184
  const currentValue = getStampCost(batch.depth, batch.amount);
1185
1185
  const newValue = getStampCost(depth, amount);
1186
1186
  return newValue.minus(currentValue);
1187
1187
  }
1188
- async getSizeExtensionCost(postageBatchId, gigabytes, options) {
1188
+ async getSizeExtensionCost(postageBatchId, size, options) {
1189
1189
  const batch = await this.getPostageBatch(postageBatchId, options);
1190
- const depth = getDepthForSize(gigabytes);
1190
+ const depth = getDepthForSize(size);
1191
1191
  const delta = depth - batch.depth;
1192
1192
  if (delta <= 0) {
1193
1193
  throw new BeeArgumentError('New depth has to be greater than the original depth', depth);
package/dist/mjs/index.js CHANGED
@@ -10,6 +10,7 @@ export * from "./utils/constants.js";
10
10
  export { Duration } from "./utils/duration.js";
11
11
  export * from "./utils/error.js";
12
12
  export * as Utils from "./utils/expose.js";
13
+ export { Size } from "./utils/size.js";
13
14
  export * from "./utils/tokens.js";
14
15
  export * from "./utils/typed-bytes.js";
15
16
  export { Bee, BeeDev, Stamper };
@@ -1,6 +1,7 @@
1
1
  import { Types } from 'cafe-utility';
2
2
  import { Duration } from "../../utils/duration.js";
3
3
  import { http } from "../../utils/http.js";
4
+ import { Size } from "../../utils/size.js";
4
5
  import { getStampEffectiveBytes, getStampTheoreticalBytes, getStampUsage } from "../../utils/stamps.js";
5
6
  import { asNumberString } from "../../utils/type.js";
6
7
  import { BatchId, EthAddress } from "../../utils/typed-bytes.js";
@@ -100,9 +101,10 @@ export async function getAllPostageBatches(requestOptions) {
100
101
  name: 'immutableFlag'
101
102
  }),
102
103
  usage,
103
- size: getStampEffectiveBytes(depth),
104
- remainingSize: Math.ceil(getStampEffectiveBytes(depth) * (1 - usage)),
105
- theoreticalSize: getStampTheoreticalBytes(depth),
104
+ usageText: `${Math.round(usage * 100)}%`,
105
+ size: Size.fromBytes(getStampEffectiveBytes(depth)),
106
+ remainingSize: Size.fromBytes(Math.ceil(getStampEffectiveBytes(depth) * (1 - usage))),
107
+ theoreticalSize: Size.fromBytes(getStampTheoreticalBytes(depth)),
106
108
  duration
107
109
  };
108
110
  });
@@ -153,9 +155,10 @@ export async function getPostageBatch(requestOptions, postageBatchId) {
153
155
  name: 'immutableFlag'
154
156
  }),
155
157
  usage,
156
- size: getStampEffectiveBytes(depth),
157
- remainingSize: Math.ceil(getStampEffectiveBytes(depth) * (1 - usage)),
158
- theoreticalSize: getStampTheoreticalBytes(depth),
158
+ usageText: `${Math.round(usage * 100)}%`,
159
+ size: Size.fromBytes(getStampEffectiveBytes(depth)),
160
+ remainingSize: Size.fromBytes(Math.ceil(getStampEffectiveBytes(depth) * (1 - usage))),
161
+ theoreticalSize: Size.fromBytes(getStampTheoreticalBytes(depth)),
159
162
  duration
160
163
  };
161
164
  }
@@ -1,3 +1,4 @@
1
+ import { System } from 'cafe-utility';
1
2
  import WebSocket from 'isomorphic-ws';
2
3
  import { uploadSingleOwnerChunk } from "../chunk/soc.js";
3
4
  const endpoint = 'gsoc';
@@ -6,6 +7,9 @@ export async function send(requestOptions, soc, stamp, options) {
6
7
  }
7
8
  export function subscribe(url, reference, headers) {
8
9
  const wsUrl = url.replace(/^http/i, 'ws');
10
+ if (System.whereAmI() === 'browser') {
11
+ return new WebSocket(`${wsUrl}/${endpoint}/subscribe/${reference.toHex()}`);
12
+ }
9
13
  return new WebSocket(`${wsUrl}/${endpoint}/subscribe/${reference.toHex()}`, {
10
14
  headers
11
15
  });
@@ -1,3 +1,4 @@
1
+ import { System } from 'cafe-utility';
1
2
  import WebSocket from 'isomorphic-ws';
2
3
  import { prepareRequestHeaders } from "../utils/headers.js";
3
4
  import { http } from "../utils/http.js";
@@ -33,6 +34,9 @@ export async function send(requestOptions, topic, target, data, postageBatchId,
33
34
  */
34
35
  export function subscribe(url, topic, headers) {
35
36
  const wsUrl = url.replace(/^http/i, 'ws');
37
+ if (System.whereAmI() === 'browser') {
38
+ return new WebSocket(`${wsUrl}/${endpoint}/subscribe/${topic.toHex()}`);
39
+ }
36
40
  return new WebSocket(`${wsUrl}/${endpoint}/subscribe/${topic.toHex()}`, {
37
41
  headers
38
42
  });
@@ -0,0 +1,31 @@
1
+ import { Numbers } from 'cafe-utility';
2
+ /**
3
+ * Represents a size in bytes.
4
+ *
5
+ * Uses 1000 instead of 1024 for converting between units.
6
+ * This is to stay consistent with the Swarm papers
7
+ * on theoretical and effective storage capacity.
8
+ */
9
+ export class Size {
10
+ constructor(bytes) {
11
+ this.bytes = Math.ceil(bytes);
12
+ if (bytes < 0) {
13
+ throw Error('Size must be at least 0');
14
+ }
15
+ }
16
+ static fromBytes(bytes) {
17
+ return new Size(bytes);
18
+ }
19
+ static fromGigabytes(gigabytes) {
20
+ return new Size(gigabytes * 1000 * 1000 * 1000);
21
+ }
22
+ toBytes() {
23
+ return this.bytes;
24
+ }
25
+ toGigabytes() {
26
+ return this.bytes / 1000 / 1000 / 1000;
27
+ }
28
+ toFormattedString() {
29
+ return Numbers.convertBytes(this.bytes, 1000);
30
+ }
31
+ }
@@ -92,14 +92,14 @@ export function getAmountForDuration(duration, pricePerBlock, blockTime = 5) {
92
92
  return BigInt(duration.toSeconds()) / BigInt(blockTime) * BigInt(pricePerBlock);
93
93
  }
94
94
  /**
95
- * Utility function that calculates the depth required for a postage batch to achieve the specified effective size in gigabytes.
95
+ * Utility function that calculates the depth required for a postage batch to achieve the specified effective size
96
96
  *
97
- * @param gigabytes
97
+ * @param size The effective size of the postage batch
98
98
  * @returns
99
99
  */
100
- export function getDepthForSize(gigabytes) {
101
- for (const [depth, size] of effectiveSizeBreakpoints) {
102
- if (gigabytes <= size) {
100
+ export function getDepthForSize(size) {
101
+ for (const [depth, sizeBreakpoint] of effectiveSizeBreakpoints) {
102
+ if (size.toGigabytes() <= sizeBreakpoint) {
103
103
  return depth;
104
104
  }
105
105
  }
@@ -6,6 +6,7 @@ import type { AllSettlements, BalanceResponse, BeeOptions, BeeRequestOptions, Be
6
6
  import { AllTagsOptions, Collection, PostageBatchOptions, TransactionOptions, UploadResult } from './types';
7
7
  import { Bytes } from './utils/bytes';
8
8
  import { Duration } from './utils/duration';
9
+ import { Size } from './utils/size';
9
10
  import { BZZ } from './utils/tokens';
10
11
  import { BatchId, EthAddress, FeedIndex, Identifier, PeerAddress, PrivateKey, PublicKey, Reference, Topic, TransactionId } from './utils/typed-bytes';
11
12
  import { UploadProgress } from './utils/upload-progress';
@@ -649,12 +650,12 @@ export declare class Bee {
649
650
  * @see [Bee Debug API reference - `POST /stamps`](https://docs.ethswarm.org/debug-api/#tag/Postage-Stamps/paths/~1stamps~1{amount}~1{depth}/post)
650
651
  */
651
652
  createPostageBatch(amount: NumberString | string | bigint, depth: number, options?: PostageBatchOptions, requestOptions?: BeeRequestOptions): Promise<BatchId>;
652
- buyStorage(gigabytes: number, duration: Duration, options?: PostageBatchOptions, requestOptions?: BeeRequestOptions): Promise<BatchId>;
653
- getStorageCost(gigabytes: number, duration: Duration, options?: BeeRequestOptions): Promise<BZZ>;
654
- extendStorageSize(postageBatchId: BatchId | Uint8Array | string, gigabytes: number, options?: BeeRequestOptions): Promise<BatchId>;
653
+ buyStorage(size: Size, duration: Duration, options?: PostageBatchOptions, requestOptions?: BeeRequestOptions): Promise<BatchId>;
654
+ getStorageCost(size: Size, duration: Duration, options?: BeeRequestOptions): Promise<BZZ>;
655
+ extendStorageSize(postageBatchId: BatchId | Uint8Array | string, size: Size, options?: BeeRequestOptions): Promise<BatchId>;
655
656
  extendStorageDuration(postageBatchId: BatchId | Uint8Array | string, duration: Duration, options?: BeeRequestOptions): Promise<BatchId>;
656
- getExtensionCost(postageBatchId: BatchId | Uint8Array | string, gigabytes: number, duration: Duration, options?: BeeRequestOptions): Promise<BZZ>;
657
- getSizeExtensionCost(postageBatchId: BatchId | Uint8Array | string, gigabytes: number, options?: BeeRequestOptions): Promise<BZZ>;
657
+ getExtensionCost(postageBatchId: BatchId | Uint8Array | string, size: Size, duration: Duration, options?: BeeRequestOptions): Promise<BZZ>;
658
+ getSizeExtensionCost(postageBatchId: BatchId | Uint8Array | string, size: Size, options?: BeeRequestOptions): Promise<BZZ>;
658
659
  getDurationExtensionCost(postageBatchId: BatchId | Uint8Array | string, duration: Duration, options?: BeeRequestOptions): Promise<BZZ>;
659
660
  /**
660
661
  * Topup a fresh amount of BZZ to given Postage Batch.
@@ -10,6 +10,7 @@ export * from './utils/constants';
10
10
  export { Duration } from './utils/duration';
11
11
  export * from './utils/error';
12
12
  export * as Utils from './utils/expose';
13
+ export { Size } from './utils/size';
13
14
  export * from './utils/tokens';
14
15
  export * from './utils/typed-bytes';
15
16
  export { Bee, BeeDev, Stamper };
@@ -21,6 +22,7 @@ declare global {
21
22
  Stamper: typeof import('./stamper/stamper').Stamper;
22
23
  Utils: typeof import('./utils/expose');
23
24
  Duration: typeof import('./utils/duration').Duration;
25
+ Size: typeof import('./utils/size').Size;
24
26
  BeeError: typeof import('./utils/error').BeeError;
25
27
  BeeArgumentError: typeof import('./utils/error').BeeArgumentError;
26
28
  BeeResponseError: typeof import('./utils/error').BeeResponseError;
@@ -5,6 +5,7 @@ import type { FeedPayloadResult, FeedReferenceResult, FeedUpdateOptions } from '
5
5
  import { Bytes } from '../utils/bytes';
6
6
  import { Duration } from '../utils/duration';
7
7
  import type { BeeError } from '../utils/error';
8
+ import { Size } from '../utils/size';
8
9
  import { BatchId, EthAddress, Identifier, PrivateKey, PublicKey, Reference, Topic, TransactionId } from '../utils/typed-bytes';
9
10
  export * from './debug';
10
11
  export declare const SECTION_SIZE = 32;
@@ -394,17 +395,21 @@ export interface PostageBatch {
394
395
  */
395
396
  usage: number;
396
397
  /**
397
- * Effective size in bytes
398
+ * Human readable usage text, like "50%" or "100%", no fractions
398
399
  */
399
- size: number;
400
+ usageText: string;
401
+ /**
402
+ * Effective size
403
+ */
404
+ size: Size;
400
405
  /**
401
- * Estimated remaining size in bytes
406
+ * Estimated remaining size
402
407
  */
403
- remainingSize: number;
408
+ remainingSize: Size;
404
409
  /**
405
410
  * Theoretical size in bytes
406
411
  */
407
- theoreticalSize: number;
412
+ theoreticalSize: Size;
408
413
  }
409
414
  export interface BatchBucket {
410
415
  bucketID: number;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Represents a size in bytes.
3
+ *
4
+ * Uses 1000 instead of 1024 for converting between units.
5
+ * This is to stay consistent with the Swarm papers
6
+ * on theoretical and effective storage capacity.
7
+ */
8
+ export declare class Size {
9
+ private bytes;
10
+ private constructor();
11
+ static fromBytes(bytes: number): Size;
12
+ static fromGigabytes(gigabytes: number): Size;
13
+ toBytes(): number;
14
+ toGigabytes(): number;
15
+ toFormattedString(): string;
16
+ }
@@ -1,6 +1,7 @@
1
1
  import { EnvelopeWithBatchId, NumberString } from '../types';
2
2
  import { Bytes } from './bytes';
3
3
  import { Duration } from './duration';
4
+ import { Size } from './size';
4
5
  import { BZZ } from './tokens';
5
6
  /**
6
7
  * Utility function that calculates usage of postage batch based on its utilization, depth and bucket depth.
@@ -49,11 +50,11 @@ export declare function getStampDuration(amount: NumberString | string | bigint,
49
50
  */
50
51
  export declare function getAmountForDuration(duration: Duration, pricePerBlock: number, blockTime?: number): bigint;
51
52
  /**
52
- * Utility function that calculates the depth required for a postage batch to achieve the specified effective size in gigabytes.
53
+ * Utility function that calculates the depth required for a postage batch to achieve the specified effective size
53
54
  *
54
- * @param gigabytes
55
+ * @param size The effective size of the postage batch
55
56
  * @returns
56
57
  */
57
- export declare function getDepthForSize(gigabytes: number): number;
58
+ export declare function getDepthForSize(size: Size): number;
58
59
  export declare function convertEnvelopeToMarshaledStamp(envelope: EnvelopeWithBatchId): Bytes;
59
60
  export declare function marshalStamp(signature: Uint8Array, batchId: Uint8Array, timestamp: Uint8Array, index: Uint8Array): Bytes;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@upcoming/bee-js",
3
- "version": "0.15.0",
3
+ "version": "0.16.0",
4
4
  "description": "Javascript client for Bee",
5
5
  "keywords": [
6
6
  "bee",
@@ -63,7 +63,7 @@
63
63
  },
64
64
  "dependencies": {
65
65
  "axios": "^0.28.1",
66
- "cafe-utility": "^27.12.1",
66
+ "cafe-utility": "^27.14.0",
67
67
  "isomorphic-ws": "^4.0.1",
68
68
  "semver": "^7.3.5",
69
69
  "ws": "^8.7.0"