@ethersphere/bee-js 11.0.0 → 11.1.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.
@@ -12,6 +12,10 @@ export async function streamDirectory(_bee, _dir, _postageBatchId, _onUploadProg
12
12
  throw new Error('Streaming directories is not supported in browsers!');
13
13
  }
14
14
  export async function streamFiles(bee, files, postageBatchId, onUploadProgress, options, requestOptions) {
15
+ const signal = requestOptions?.signal;
16
+ if (signal?.aborted) {
17
+ throw new Error('Request aborted');
18
+ }
15
19
  const queue = new AsyncQueue(64, 64);
16
20
  let total = 0;
17
21
  let processed = 0;
@@ -21,15 +25,28 @@ export async function streamFiles(bee, files, postageBatchId, onUploadProgress,
21
25
  postageBatchId = new BatchId(postageBatchId);
22
26
  async function onChunk(chunk) {
23
27
  await queue.enqueue(async () => {
24
- await bee.uploadChunk(postageBatchId, chunk.build(), options, requestOptions);
25
- onUploadProgress?.({
26
- total,
27
- processed: ++processed
28
- });
28
+ if (signal?.aborted) {
29
+ return;
30
+ }
31
+ try {
32
+ await bee.uploadChunk(postageBatchId, chunk.build(), options, requestOptions);
33
+ onUploadProgress?.({
34
+ total,
35
+ processed: ++processed
36
+ });
37
+ } catch (err) {
38
+ if (signal?.aborted) {
39
+ return;
40
+ }
41
+ throw err;
42
+ }
29
43
  });
30
44
  }
31
45
  const mantaray = new MantarayNode();
32
46
  for (const file of files) {
47
+ if (signal?.aborted) {
48
+ throw new Error('Request aborted');
49
+ }
33
50
  const rootChunk = await new Promise((resolve, reject) => {
34
51
  const tree = new MerkleTree(onChunk);
35
52
  let offset = 0;
@@ -38,6 +55,10 @@ export async function streamFiles(bee, files, postageBatchId, onUploadProgress,
38
55
  reject(reader.error);
39
56
  };
40
57
  const readNextChunk = async () => {
58
+ if (signal?.aborted) {
59
+ reject(new Error('Request aborted'));
60
+ return;
61
+ }
41
62
  if (offset >= file.size) {
42
63
  const rootChunk = await tree.finalize();
43
64
  resolve(rootChunk);
@@ -75,6 +96,9 @@ export async function streamFiles(bee, files, postageBatchId, onUploadProgress,
75
96
  });
76
97
  }
77
98
  }
99
+ if (signal?.aborted) {
100
+ throw new Error('Request aborted');
101
+ }
78
102
  return mantaray.saveRecursively(bee, postageBatchId, options, requestOptions);
79
103
  }
80
104
  function maybeEnrichMime(mime) {
@@ -10,6 +10,7 @@ const MAX_FAILED_ATTEMPTS = 100000;
10
10
  const DELAY_FAST = 200;
11
11
  const DELAY_SLOW = 1000;
12
12
  const DELAY_THRESHOLD = Dates.minutes(1) / DELAY_FAST;
13
+ const ABORT_ERROR_MESSAGE = 'Request aborted';
13
14
  export const DEFAULT_HTTP_CONFIG = {
14
15
  headers: {
15
16
  accept: 'application/json, text/plain, */*'
@@ -17,6 +18,11 @@ export const DEFAULT_HTTP_CONFIG = {
17
18
  maxBodyLength: Infinity,
18
19
  maxContentLength: Infinity
19
20
  };
21
+ function throwIfAborted(signal, config, responseData, responseStatus) {
22
+ if (signal?.aborted) {
23
+ throw new BeeResponseError(config.method || 'get', config.url || '<unknown>', ABORT_ERROR_MESSAGE, responseData, responseStatus, 'ERR_CANCELED');
24
+ }
25
+ }
20
26
  /**
21
27
  * Main function to make HTTP requests.
22
28
  * @param options User defined settings
@@ -24,6 +30,10 @@ export const DEFAULT_HTTP_CONFIG = {
24
30
  */
25
31
  export async function http(options, config) {
26
32
  const requestConfig = Objects.deepMerge3(DEFAULT_HTTP_CONFIG, config, options);
33
+ if (options.signal) {
34
+ requestConfig.signal = options.signal;
35
+ throwIfAborted(options.signal, config);
36
+ }
27
37
  if (requestConfig.data && typeof Buffer !== 'undefined' && Buffer.isBuffer(requestConfig.data)) {
28
38
  requestConfig.data = requestConfig.data.buffer.slice(requestConfig.data.byteOffset, requestConfig.data.byteOffset + requestConfig.data.byteLength);
29
39
  }
@@ -38,6 +48,7 @@ export async function http(options, config) {
38
48
  }
39
49
  let failedAttempts = 0;
40
50
  while (failedAttempts < MAX_FAILED_ATTEMPTS) {
51
+ throwIfAborted(options.signal, config);
41
52
  try {
42
53
  debug(`${requestConfig.method || 'get'} ${Strings.joinUrl([requestConfig.baseURL, requestConfig.url])}`, {
43
54
  headers: {
@@ -50,11 +61,16 @@ export async function http(options, config) {
50
61
  return response;
51
62
  } catch (e) {
52
63
  if (e instanceof AxiosError) {
64
+ if (e.code === 'ERR_CANCELED') {
65
+ throwIfAborted({
66
+ aborted: true
67
+ }, config, e.response?.data, e.response?.status);
68
+ }
53
69
  if (e.code === 'ECONNABORTED' && options.endlesslyRetry) {
54
70
  failedAttempts++;
55
71
  await System.sleepMillis(failedAttempts < DELAY_THRESHOLD ? DELAY_FAST : DELAY_SLOW);
56
72
  } else {
57
- throw new BeeResponseError(config.method || 'get', config.url || '<unknown>', e.message, e.response?.data, e.response?.status, e.code);
73
+ throw new BeeResponseError(config.method || 'get', config.url || '<unknown>', e.message, e.response?.data, e.response?.status, e.response?.statusText);
58
74
  }
59
75
  } else {
60
76
  throw e;
@@ -34,7 +34,8 @@ export function prepareBeeRequestOptions(value) {
34
34
  httpsAgent: object.httpsAgent,
35
35
  endlesslyRetry: Types.asOptional(x => Types.asBoolean(x, {
36
36
  name: 'endlesslyRetry'
37
- }), object.endlesslyRetry)
37
+ }), object.endlesslyRetry),
38
+ signal: object.signal
38
39
  };
39
40
  }
40
41
  export function prepareDownloadOptions(value) {
@@ -39,6 +39,7 @@ export type BeeRequestOptions = {
39
39
  httpAgent?: unknown;
40
40
  httpsAgent?: unknown;
41
41
  endlesslyRetry?: boolean;
42
+ signal?: AbortSignal;
42
43
  };
43
44
  /**
44
45
  * Options for the Bee client which affect all method calls *(unless overridden in the method call itself)*.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ethersphere/bee-js",
3
- "version": "11.0.0",
3
+ "version": "11.1.0",
4
4
  "description": "Javascript client for Bee",
5
5
  "keywords": [
6
6
  "bee",
@@ -62,7 +62,7 @@
62
62
  },
63
63
  "dependencies": {
64
64
  "axios": "^0.30.2",
65
- "cafe-utility": "^33.3.4",
65
+ "cafe-utility": "^33.6.1",
66
66
  "debug": "^4.4.1",
67
67
  "isomorphic-ws": "^4.0.1",
68
68
  "semver": "^7.3.5",