@upcoming/bee-js 9.7.0 → 9.9.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
@@ -1212,6 +1212,21 @@ export class Bee {
1212
1212
  const depth = getDepthForSize(size, encryption, erasureCodeLevel);
1213
1213
  return getStampCost(depth, amount);
1214
1214
  }
1215
+ async extendStorage(postageBatchId, size, duration, options, encryption, erasureCodeLevel) {
1216
+ const batch = await this.getPostageBatch(postageBatchId, options);
1217
+ const depth = getDepthForSize(size, encryption, erasureCodeLevel);
1218
+ const chainState = await this.getChainState(options);
1219
+ const depthDelta = depth - batch.depth;
1220
+ const multiplier = depthDelta === 0 ? 1n : 2n ** BigInt(depthDelta);
1221
+ const additionalAmount = getAmountForDuration(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1222
+ const targetAmount = duration.isZero() ? BigInt(batch.amount) * multiplier : (BigInt(batch.amount) + additionalAmount) * multiplier;
1223
+ const amountDelta = targetAmount - BigInt(batch.amount);
1224
+ const transactionId = await this.topUpBatch(batch.batchID, amountDelta, options);
1225
+ if (depthDelta) {
1226
+ return this.diluteBatch(batch.batchID, depth, options);
1227
+ }
1228
+ return transactionId;
1229
+ }
1215
1230
  async extendStorageSize(postageBatchId, size, options, encryption, erasureCodeLevel) {
1216
1231
  const batch = await this.getPostageBatch(postageBatchId, options);
1217
1232
  const depth = getDepthForSize(size, encryption, erasureCodeLevel);
@@ -1219,7 +1234,7 @@ export class Bee {
1219
1234
  if (delta <= 0) {
1220
1235
  throw new BeeArgumentError('New depth has to be greater than the original depth', depth);
1221
1236
  }
1222
- await this.topUpBatch(batch.batchID, BigInt(batch.amount) * 2n ** BigInt(delta - 1) + 1n, options);
1237
+ await this.topUpBatch(batch.batchID, BigInt(batch.amount) * (2n ** BigInt(delta) - 1n) + 1n, options);
1223
1238
  return this.diluteBatch(batch.batchID, depth, options);
1224
1239
  }
1225
1240
  async extendStorageDuration(postageBatchId, duration, options) {
@@ -1231,11 +1246,11 @@ export class Bee {
1231
1246
  async getExtensionCost(postageBatchId, size, duration, options, encryption, erasureCodeLevel) {
1232
1247
  const batch = await this.getPostageBatch(postageBatchId, options);
1233
1248
  const chainState = await this.getChainState(options);
1234
- const amount = getAmountForDuration(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1249
+ const amount = duration.isZero() ? 0n : getAmountForDuration(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1235
1250
  const depth = getDepthForSize(size, encryption, erasureCodeLevel);
1236
- const currentValue = getStampCost(batch.depth, batch.amount);
1237
- const newValue = getStampCost(depth, amount);
1238
- return newValue.minus(currentValue);
1251
+ const currentCost = getStampCost(batch.depth, batch.amount);
1252
+ const newCost = getStampCost(depth, BigInt(batch.amount) + amount);
1253
+ return newCost.minus(currentCost);
1239
1254
  }
1240
1255
  async getSizeExtensionCost(postageBatchId, size, options, encryption, erasureCodeLevel) {
1241
1256
  const batch = await this.getPostageBatch(postageBatchId, options);
@@ -1244,9 +1259,9 @@ export class Bee {
1244
1259
  if (delta <= 0) {
1245
1260
  throw new BeeArgumentError('New depth has to be greater than the original depth', depth);
1246
1261
  }
1247
- const currentPaid = getStampCost(batch.depth, batch.amount);
1248
- const newPaid = getStampCost(depth, batch.amount);
1249
- return newPaid.minus(currentPaid);
1262
+ const currentCost = getStampCost(batch.depth, batch.amount);
1263
+ const newCost = getStampCost(depth, batch.amount);
1264
+ return newCost.minus(currentCost);
1250
1265
  }
1251
1266
  async getDurationExtensionCost(postageBatchId, duration, options) {
1252
1267
  const batch = await this.getPostageBatch(postageBatchId, options);
@@ -1,6 +1,8 @@
1
1
  import { Binary, MerkleTree, Optional, Uint8ArrayReader } from 'cafe-utility';
2
+ import _debug from 'debug';
2
3
  import { NULL_ADDRESS } from "../index.js";
3
4
  import { Reference } from "../utils/typed-bytes.js";
5
+ const debug = _debug('bee-js:manifest');
4
6
  const ENCODER = new TextEncoder();
5
7
  const DECODER = new TextDecoder();
6
8
  const TYPE_VALUE = 2;
@@ -60,6 +62,11 @@ export class Fork {
60
62
  data.push(new Uint8Array(30 - this.prefix.length));
61
63
  }
62
64
  data.push(this.node.selfAddress);
65
+ debug('marshalling fork', {
66
+ prefixLength: this.prefix.length,
67
+ prefix: DECODER.decode(this.prefix),
68
+ address: Binary.uint8ArrayToHex(this.node.selfAddress)
69
+ });
63
70
  if (this.node.metadata) {
64
71
  const metadataBytes = Binary.padEndToMultiple(new Uint8Array([0x00, 0x00, ...ENCODER.encode(JSON.stringify(this.node.metadata))]), 32, 0x0a);
65
72
  const metadataLengthBytes = Binary.numberToUint16(metadataBytes.length - 2, 'BE');
@@ -72,8 +79,17 @@ export class Fork {
72
79
  const type = Binary.uint8ToNumber(reader.read(1));
73
80
  const prefixLength = Binary.uint8ToNumber(reader.read(1));
74
81
  const prefix = reader.read(prefixLength);
75
- reader.read(30 - prefixLength);
82
+ if (prefixLength < 30) {
83
+ reader.read(30 - prefixLength);
84
+ }
76
85
  const selfAddress = reader.read(addressLength);
86
+ debug('unmarshalling fork', {
87
+ type,
88
+ prefixLength,
89
+ prefix: DECODER.decode(prefix),
90
+ addressLength,
91
+ address: Binary.uint8ArrayToHex(selfAddress)
92
+ });
77
93
  let metadata = undefined;
78
94
  if (isType(type, TYPE_WITH_METADATA)) {
79
95
  const metadataLength = Binary.uint16ToNumber(reader.read(2), 'BE');
@@ -210,7 +226,7 @@ export class MantarayNode {
210
226
  throw new Error('MantarayNode#unmarshal invalid version hash');
211
227
  }
212
228
  const targetAddressLength = Binary.uint8ToNumber(reader.read(1));
213
- const targetAddress = targetAddressLength === 0 ? NULL_ADDRESS : reader.read(targetAddressLength);
229
+ const targetAddress = targetAddressLength ? reader.read(targetAddressLength) : NULL_ADDRESS;
214
230
  const node = new MantarayNode({
215
231
  selfAddress,
216
232
  targetAddress,
@@ -219,7 +235,7 @@ export class MantarayNode {
219
235
  const forkBitmap = reader.read(32);
220
236
  for (let i = 0; i < 256; i++) {
221
237
  if (Binary.getBit(forkBitmap, i, 'LE')) {
222
- const newFork = Fork.unmarshal(reader, targetAddressLength);
238
+ const newFork = Fork.unmarshal(reader, selfAddress.length);
223
239
  node.forks.set(i, newFork);
224
240
  newFork.node.parent = node;
225
241
  }
@@ -232,6 +248,10 @@ export class MantarayNode {
232
248
  addFork(path, reference, metadata) {
233
249
  this.selfAddress = null;
234
250
  path = path instanceof Uint8Array ? path : ENCODER.encode(path);
251
+ debug('adding fork', {
252
+ path: DECODER.decode(path),
253
+ reference: new Reference(reference).represent()
254
+ });
235
255
  // TODO: this should not be ignored
236
256
  // eslint-disable-next-line @typescript-eslint/no-this-alias
237
257
  let tip = this;
@@ -39,6 +39,7 @@ export async function streamDirectory(bee, dir, postageBatchId, onUploadProgress
39
39
  for (const file of files) {
40
40
  total += totalChunks(file.size);
41
41
  }
42
+ let hasIndexHtml = false;
42
43
  async function onChunk(chunk) {
43
44
  await queue.enqueue(async () => {
44
45
  await bee.uploadChunk(postageBatchId, chunk.build(), options, requestOptions);
@@ -69,10 +70,20 @@ export async function streamDirectory(bee, dir, postageBatchId, onUploadProgress
69
70
  Filename: filename
70
71
  });
71
72
  if (file.path === 'index.html') {
72
- mantaray.addFork('/', NULL_ADDRESS, {
73
- 'website-index-document': 'index.html'
74
- });
73
+ hasIndexHtml = true;
74
+ }
75
+ }
76
+ if (hasIndexHtml || options?.indexDocument || options?.errorDocument) {
77
+ const metadata = {};
78
+ if (options?.indexDocument) {
79
+ metadata['website-index-document'] = options.indexDocument;
80
+ } else if (hasIndexHtml) {
81
+ metadata['website-index-document'] = 'index.html';
82
+ }
83
+ if (options?.errorDocument) {
84
+ metadata['website-error-document'] = options.errorDocument;
75
85
  }
86
+ mantaray.addFork('/', NULL_ADDRESS, metadata);
76
87
  }
77
88
  return mantaray.saveRecursively(bee, postageBatchId, options, requestOptions);
78
89
  }
@@ -2,8 +2,8 @@ import { Dates } from 'cafe-utility';
2
2
  export class Duration {
3
3
  constructor(seconds) {
4
4
  this.seconds = Math.ceil(seconds);
5
- if (seconds <= 0) {
6
- throw Error('Duration must be greater than 0');
5
+ if (seconds < 0) {
6
+ throw Error('Duration cannot be negative');
7
7
  }
8
8
  }
9
9
  static fromMilliseconds(milliseconds) {
@@ -48,4 +48,8 @@ export class Duration {
48
48
  represent() {
49
49
  return Dates.secondsToHumanTime(this.seconds);
50
50
  }
51
- }
51
+ isZero() {
52
+ return this.seconds === 0;
53
+ }
54
+ }
55
+ Duration.ZERO = new Duration(0);
@@ -1,6 +1,8 @@
1
1
  import axios from 'axios';
2
2
  import { Dates, Objects, Strings, System } from 'cafe-utility';
3
+ import _debug from 'debug';
3
4
  import { BeeResponseError } from "../index.js";
5
+ const debug = _debug('bee-js:http');
4
6
  const {
5
7
  AxiosError
6
8
  } = axios;
@@ -37,6 +39,12 @@ export async function http(options, config) {
37
39
  let failedAttempts = 0;
38
40
  while (failedAttempts < MAX_FAILED_ATTEMPTS) {
39
41
  try {
42
+ debug(`${requestConfig.method || 'get'} ${Strings.joinUrl([requestConfig.baseURL, requestConfig.url])}`, {
43
+ headers: {
44
+ ...requestConfig.headers
45
+ },
46
+ params: requestConfig.params
47
+ });
40
48
  maybeRunOnRequestHook(options, requestConfig);
41
49
  const response = await axios(requestConfig);
42
50
  return response;
@@ -193,7 +193,7 @@ export declare class Bee {
193
193
  */
194
194
  uploadFiles(postageBatchId: BatchId | Uint8Array | string, fileList: FileList | File[], options?: CollectionUploadOptions, requestOptions?: BeeRequestOptions): Promise<UploadResult>;
195
195
  hashDirectory(dir: string): Promise<Reference>;
196
- streamDirectory(postageBatchId: BatchId | Uint8Array | string, dir: string, onUploadProgress?: (progress: UploadProgress) => void, options?: UploadOptions, requestOptions?: BeeRequestOptions): Promise<UploadResult>;
196
+ streamDirectory(postageBatchId: BatchId | Uint8Array | string, dir: string, onUploadProgress?: (progress: UploadProgress) => void, options?: CollectionUploadOptions, requestOptions?: BeeRequestOptions): Promise<UploadResult>;
197
197
  streamFiles(postageBatchId: BatchId | Uint8Array | string, files: File[] | FileList, onUploadProgress?: (progress: UploadProgress) => void, options?: UploadOptions, requestOptions?: BeeRequestOptions): Promise<UploadResult>;
198
198
  /**
199
199
  * Upload Collection that you can assembly yourself.
@@ -689,6 +689,7 @@ export declare class Bee {
689
689
  createPostageBatch(amount: NumberString | string | bigint, depth: number, options?: PostageBatchOptions, requestOptions?: BeeRequestOptions): Promise<BatchId>;
690
690
  buyStorage(size: Size, duration: Duration, options?: PostageBatchOptions, requestOptions?: BeeRequestOptions, encryption?: boolean, erasureCodeLevel?: RedundancyLevel): Promise<BatchId>;
691
691
  getStorageCost(size: Size, duration: Duration, options?: BeeRequestOptions, encryption?: boolean, erasureCodeLevel?: RedundancyLevel): Promise<BZZ>;
692
+ extendStorage(postageBatchId: BatchId | Uint8Array | string, size: Size, duration: Duration, options?: BeeRequestOptions, encryption?: boolean, erasureCodeLevel?: RedundancyLevel): Promise<BatchId>;
692
693
  extendStorageSize(postageBatchId: BatchId | Uint8Array | string, size: Size, options?: BeeRequestOptions, encryption?: boolean, erasureCodeLevel?: RedundancyLevel): Promise<BatchId>;
693
694
  extendStorageDuration(postageBatchId: BatchId | Uint8Array | string, duration: Duration, options?: BeeRequestOptions): Promise<BatchId>;
694
695
  getExtensionCost(postageBatchId: BatchId | Uint8Array | string, size: Size, duration: Duration, options?: BeeRequestOptions, encryption?: boolean, erasureCodeLevel?: RedundancyLevel): Promise<BZZ>;
@@ -1,6 +1,6 @@
1
- import { Bee, BeeRequestOptions, UploadOptions, UploadResult } from '..';
1
+ import { Bee, BeeRequestOptions, CollectionUploadOptions, UploadOptions, UploadResult } from '..';
2
2
  import { BatchId } from './typed-bytes';
3
3
  import { UploadProgress } from './upload-progress';
4
4
  export declare function hashDirectory(dir: string): Promise<import("./typed-bytes").Reference>;
5
- export declare function streamDirectory(bee: Bee, dir: string, postageBatchId: BatchId | string | Uint8Array, onUploadProgress?: (progress: UploadProgress) => void, options?: UploadOptions, requestOptions?: BeeRequestOptions): Promise<UploadResult>;
5
+ export declare function streamDirectory(bee: Bee, dir: string, postageBatchId: BatchId | string | Uint8Array, onUploadProgress?: (progress: UploadProgress) => void, options?: CollectionUploadOptions, requestOptions?: BeeRequestOptions): Promise<UploadResult>;
6
6
  export declare function streamFiles(_bee: Bee, _files: File[] | FileList, _postageBatchId: BatchId, _onUploadProgress?: (progress: UploadProgress) => void, _options?: UploadOptions, _requestOptions?: BeeRequestOptions): Promise<UploadResult>;
@@ -1,5 +1,6 @@
1
1
  export declare class Duration {
2
2
  private seconds;
3
+ static ZERO: Duration;
3
4
  private constructor();
4
5
  static fromMilliseconds(milliseconds: number): Duration;
5
6
  static fromSeconds(seconds: number): Duration;
@@ -15,4 +16,5 @@ export declare class Duration {
15
16
  toYears(): number;
16
17
  toEndDate(startDate?: Date): Date;
17
18
  represent(): string;
19
+ isZero(): boolean;
18
20
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@upcoming/bee-js",
3
- "version": "9.7.0",
3
+ "version": "9.9.0",
4
4
  "description": "Javascript client for Bee",
5
5
  "keywords": [
6
6
  "bee",
@@ -63,6 +63,7 @@
63
63
  "dependencies": {
64
64
  "axios": "^0.30.0",
65
65
  "cafe-utility": "^31.0.0",
66
+ "debug": "^4.4.1",
66
67
  "isomorphic-ws": "^4.0.1",
67
68
  "semver": "^7.3.5",
68
69
  "ws": "^8.7.0"
@@ -78,6 +79,7 @@
78
79
  "@commitlint/config-conventional": "^17.4.2",
79
80
  "@jest/types": "^29.6.3",
80
81
  "@naholyr/cross-env": "^1.0.0",
82
+ "@types/debug": "^4.1.12",
81
83
  "@types/jest": "^29.5.13",
82
84
  "@types/node": "^18.11.11",
83
85
  "@types/semver": "^7.3.9",