@ardrive/turbo-sdk 1.0.0-alpha.8 → 1.0.0-beta.1

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.
Files changed (92) hide show
  1. package/README.md +130 -23
  2. package/bundles/web.bundle.min.js +55668 -0
  3. package/lib/cjs/common/factory.js +6 -2
  4. package/lib/cjs/common/http.js +16 -29
  5. package/lib/cjs/common/payment.js +29 -52
  6. package/lib/cjs/common/turbo.js +20 -47
  7. package/lib/cjs/common/upload.js +21 -34
  8. package/lib/cjs/node/factory.js +8 -2
  9. package/lib/cjs/node/index.js +1 -1
  10. package/lib/cjs/node/signer.js +9 -20
  11. package/lib/cjs/package.json +1 -0
  12. package/lib/cjs/utils/axiosClient.js +18 -9
  13. package/lib/cjs/utils/readableStream.js +11 -22
  14. package/lib/cjs/web/factory.js +8 -2
  15. package/lib/cjs/web/index.js +1 -1
  16. package/lib/cjs/web/signer.js +19 -31
  17. package/lib/esm/common/factory.js +6 -2
  18. package/lib/esm/common/http.js +16 -29
  19. package/lib/esm/common/payment.js +29 -52
  20. package/lib/esm/common/turbo.js +20 -47
  21. package/lib/esm/common/upload.js +21 -34
  22. package/lib/esm/node/factory.js +8 -2
  23. package/lib/esm/node/index.js +1 -1
  24. package/lib/esm/node/signer.js +9 -20
  25. package/lib/esm/utils/axiosClient.js +18 -9
  26. package/lib/esm/utils/readableStream.js +11 -22
  27. package/lib/esm/web/factory.js +8 -2
  28. package/lib/esm/web/index.js +1 -1
  29. package/lib/esm/web/signer.js +19 -31
  30. package/lib/{cjs → types}/common/factory.d.ts +2 -1
  31. package/lib/types/common/factory.d.ts.map +1 -0
  32. package/lib/{cjs → types}/common/http.d.ts +4 -3
  33. package/lib/types/common/http.d.ts.map +1 -0
  34. package/lib/{esm → types}/common/index.d.ts +1 -0
  35. package/lib/types/common/index.d.ts.map +1 -0
  36. package/lib/{esm/types/arweave.d.ts → types/common/jwk.d.ts} +1 -0
  37. package/lib/types/common/jwk.d.ts.map +1 -0
  38. package/lib/{esm → types}/common/payment.d.ts +2 -2
  39. package/lib/types/common/payment.d.ts.map +1 -0
  40. package/lib/{cjs → types}/common/turbo.d.ts +6 -5
  41. package/lib/types/common/turbo.d.ts.map +1 -0
  42. package/lib/{esm → types}/common/upload.d.ts +2 -2
  43. package/lib/types/common/upload.d.ts.map +1 -0
  44. package/lib/{esm → types}/node/factory.d.ts +2 -1
  45. package/lib/types/node/factory.d.ts.map +1 -0
  46. package/lib/{esm → types}/node/index.d.ts +2 -1
  47. package/lib/types/node/index.d.ts.map +1 -0
  48. package/lib/{cjs → types}/node/signer.d.ts +3 -2
  49. package/lib/types/node/signer.d.ts.map +1 -0
  50. package/lib/{cjs/types/turbo.d.ts → types/types.d.ts} +4 -3
  51. package/lib/types/types.d.ts.map +1 -0
  52. package/lib/{esm → types}/utils/axiosClient.d.ts +3 -2
  53. package/lib/types/utils/axiosClient.d.ts.map +1 -0
  54. package/lib/{cjs → types}/utils/base64.d.ts +3 -2
  55. package/lib/types/utils/base64.d.ts.map +1 -0
  56. package/lib/{esm → types}/utils/errors.d.ts +1 -0
  57. package/lib/types/utils/errors.d.ts.map +1 -0
  58. package/lib/{cjs → types}/utils/readableStream.d.ts +1 -0
  59. package/lib/types/utils/readableStream.d.ts.map +1 -0
  60. package/lib/{cjs → types}/web/factory.d.ts +2 -1
  61. package/lib/types/web/factory.d.ts.map +1 -0
  62. package/lib/{cjs → types}/web/index.d.ts +2 -1
  63. package/lib/types/web/index.d.ts.map +1 -0
  64. package/lib/{cjs → types}/web/signer.d.ts +3 -2
  65. package/lib/types/web/signer.d.ts.map +1 -0
  66. package/package.json +15 -15
  67. package/lib/cjs/common/index.d.ts +0 -19
  68. package/lib/cjs/common/payment.d.ts +0 -41
  69. package/lib/cjs/common/upload.d.ts +0 -29
  70. package/lib/cjs/node/factory.d.ts +0 -22
  71. package/lib/cjs/node/index.d.ts +0 -20
  72. package/lib/cjs/types/arweave.d.ts +0 -29
  73. package/lib/cjs/types/index.d.ts +0 -18
  74. package/lib/cjs/types/index.js +0 -34
  75. package/lib/cjs/utils/axiosClient.d.ts +0 -23
  76. package/lib/cjs/utils/errors.d.ts +0 -22
  77. package/lib/esm/common/factory.d.ts +0 -21
  78. package/lib/esm/common/http.d.ts +0 -44
  79. package/lib/esm/common/turbo.d.ts +0 -73
  80. package/lib/esm/node/signer.d.ts +0 -36
  81. package/lib/esm/types/index.d.ts +0 -18
  82. package/lib/esm/types/index.js +0 -18
  83. package/lib/esm/types/turbo.d.ts +0 -149
  84. package/lib/esm/utils/base64.d.ts +0 -9
  85. package/lib/esm/utils/readableStream.d.ts +0 -22
  86. package/lib/esm/web/factory.d.ts +0 -22
  87. package/lib/esm/web/index.d.ts +0 -20
  88. package/lib/esm/web/signer.d.ts +0 -37
  89. /package/lib/cjs/{types/arweave.js → common/jwk.js} +0 -0
  90. /package/lib/cjs/{types/turbo.js → types.js} +0 -0
  91. /package/lib/esm/{types/arweave.js → common/jwk.js} +0 -0
  92. /package/lib/esm/{types/turbo.js → types.js} +0 -0
@@ -1,12 +1,3 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  import { createAxiosInstance } from '../utils/axiosClient.js';
11
2
  import { FailedRequestError } from '../utils/errors.js';
12
3
  export class TurboHTTPService {
@@ -18,28 +9,24 @@ export class TurboHTTPService {
18
9
  retryConfig,
19
10
  });
20
11
  }
21
- get({ endpoint, signal, allowedStatuses = [200, 202], headers, }) {
22
- return __awaiter(this, void 0, void 0, function* () {
23
- const { status, statusText, data } = yield this.axios.get(endpoint, {
24
- headers,
25
- signal,
26
- });
27
- if (!allowedStatuses.includes(status)) {
28
- throw new FailedRequestError(status, statusText);
29
- }
30
- return data;
12
+ async get({ endpoint, signal, allowedStatuses = [200, 202], headers, }) {
13
+ const { status, statusText, data } = await this.axios.get(endpoint, {
14
+ headers,
15
+ signal,
31
16
  });
17
+ if (!allowedStatuses.includes(status)) {
18
+ throw new FailedRequestError(status, statusText);
19
+ }
20
+ return data;
32
21
  }
33
- post({ endpoint, signal, allowedStatuses = [200, 202], headers, data, }) {
34
- return __awaiter(this, void 0, void 0, function* () {
35
- const { status, statusText, data: response, } = yield this.axios.post(endpoint, data, {
36
- headers,
37
- signal,
38
- });
39
- if (!allowedStatuses.includes(status)) {
40
- throw new FailedRequestError(status, statusText);
41
- }
42
- return response;
22
+ async post({ endpoint, signal, allowedStatuses = [200, 202], headers, data, }) {
23
+ const { status, statusText, data: response, } = await this.axios.post(endpoint, data, {
24
+ headers,
25
+ signal,
43
26
  });
27
+ if (!allowedStatuses.includes(status)) {
28
+ throw new FailedRequestError(status, statusText);
29
+ }
30
+ return response;
44
31
  }
45
32
  }
@@ -1,12 +1,3 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  import { TurboHTTPService } from './http.js';
11
2
  export class TurboUnauthenticatedPaymentService {
12
3
  constructor({ url = 'https://payment.ardrive.dev', retryConfig, }) {
@@ -15,48 +6,36 @@ export class TurboUnauthenticatedPaymentService {
15
6
  retryConfig,
16
7
  });
17
8
  }
18
- getFiatRates() {
19
- return __awaiter(this, void 0, void 0, function* () {
20
- return this.httpService.get({
21
- endpoint: '/rates',
22
- });
9
+ async getFiatRates() {
10
+ return this.httpService.get({
11
+ endpoint: '/rates',
23
12
  });
24
13
  }
25
- getFiatToAR({ currency, }) {
26
- return __awaiter(this, void 0, void 0, function* () {
27
- return this.httpService.get({
28
- endpoint: `/rates/${currency}`,
29
- });
14
+ async getFiatToAR({ currency, }) {
15
+ return this.httpService.get({
16
+ endpoint: `/rates/${currency}`,
30
17
  });
31
18
  }
32
- getSupportedCountries() {
33
- return __awaiter(this, void 0, void 0, function* () {
34
- return this.httpService.get({
35
- endpoint: '/countries',
36
- });
19
+ async getSupportedCountries() {
20
+ return this.httpService.get({
21
+ endpoint: '/countries',
37
22
  });
38
23
  }
39
- getSupportedCurrencies() {
40
- return __awaiter(this, void 0, void 0, function* () {
41
- return this.httpService.get({
42
- endpoint: '/currencies',
43
- });
24
+ async getSupportedCurrencies() {
25
+ return this.httpService.get({
26
+ endpoint: '/currencies',
44
27
  });
45
28
  }
46
- getUploadCosts({ bytes, }) {
47
- return __awaiter(this, void 0, void 0, function* () {
48
- const fetchPricePromises = bytes.map((byteCount) => this.httpService.get({
49
- endpoint: `/price/bytes/${byteCount}`,
50
- }));
51
- const wincCostsForBytes = yield Promise.all(fetchPricePromises);
52
- return wincCostsForBytes;
53
- });
29
+ async getUploadCosts({ bytes, }) {
30
+ const fetchPricePromises = bytes.map((byteCount) => this.httpService.get({
31
+ endpoint: `/price/bytes/${byteCount}`,
32
+ }));
33
+ const wincCostsForBytes = await Promise.all(fetchPricePromises);
34
+ return wincCostsForBytes;
54
35
  }
55
- getWincForFiat({ amount, currency }) {
56
- return __awaiter(this, void 0, void 0, function* () {
57
- return this.httpService.get({
58
- endpoint: `/price/${currency}/${amount}`,
59
- });
36
+ async getWincForFiat({ amount, currency }) {
37
+ return this.httpService.get({
38
+ endpoint: `/price/${currency}/${amount}`,
60
39
  });
61
40
  }
62
41
  }
@@ -66,16 +45,14 @@ export class TurboAuthenticatedPaymentService extends TurboUnauthenticatedPaymen
66
45
  super({ url, retryConfig });
67
46
  this.signer = signer;
68
47
  }
69
- getBalance() {
70
- return __awaiter(this, void 0, void 0, function* () {
71
- const headers = yield this.signer.generateSignedRequestHeaders();
72
- const balance = yield this.httpService.get({
73
- endpoint: '/balance',
74
- headers,
75
- allowedStatuses: [200, 404],
76
- });
77
- // 404's don't return a balance, so default to 0
78
- return balance.winc ? balance : { winc: '0' };
48
+ async getBalance() {
49
+ const headers = await this.signer.generateSignedRequestHeaders();
50
+ const balance = await this.httpService.get({
51
+ endpoint: '/balance',
52
+ headers,
53
+ allowedStatuses: [200, 404],
79
54
  });
55
+ // 404's don't return a balance, so default to 0
56
+ return balance.winc ? balance : { winc: '0' };
80
57
  }
81
58
  }
@@ -1,12 +1,3 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  import { TurboUnauthenticatedPaymentService } from './payment.js';
11
2
  import { TurboUnauthenticatedUploadService } from './upload.js';
12
3
  export class TurboUnauthenticatedClient {
@@ -17,10 +8,8 @@ export class TurboUnauthenticatedClient {
17
8
  /**
18
9
  * Returns the supported fiat currency conversion rate for 1AR based on current market prices.
19
10
  */
20
- getFiatToAR({ currency, }) {
21
- return __awaiter(this, void 0, void 0, function* () {
22
- return this.paymentService.getFiatToAR({ currency });
23
- });
11
+ async getFiatToAR({ currency, }) {
12
+ return this.paymentService.getFiatToAR({ currency });
24
13
  }
25
14
  /**
26
15
  * Returns the latest conversion rates to purchase 1GiB of data for all supported currencies, including all adjustments and fees.
@@ -28,52 +17,40 @@ export class TurboUnauthenticatedClient {
28
17
  * Note: this does not take into account varying adjustments and promotions for different sizes of data. If you want to calculate the total
29
18
  * cost in 'winc' for a given number of bytes, use getUploadCosts.
30
19
  */
31
- getFiatRates() {
32
- return __awaiter(this, void 0, void 0, function* () {
33
- return this.paymentService.getFiatRates();
34
- });
20
+ async getFiatRates() {
21
+ return this.paymentService.getFiatRates();
35
22
  }
36
23
  /**
37
24
  * Returns a comprehensive list of supported countries that can purchase credits through the Turbo Payment Service.
38
25
  */
39
- getSupportedCountries() {
40
- return __awaiter(this, void 0, void 0, function* () {
41
- return this.paymentService.getSupportedCountries();
42
- });
26
+ async getSupportedCountries() {
27
+ return this.paymentService.getSupportedCountries();
43
28
  }
44
29
  /**
45
30
  * Returns a list of all supported fiat currencies.
46
31
  */
47
- getSupportedCurrencies() {
48
- return __awaiter(this, void 0, void 0, function* () {
49
- return this.paymentService.getSupportedCurrencies();
50
- });
32
+ async getSupportedCurrencies() {
33
+ return this.paymentService.getSupportedCurrencies();
51
34
  }
52
35
  /**
53
36
  * Determines the price in 'winc' to upload one data item of a specific size in bytes, including all Turbo cost adjustments and fees.
54
37
  */
55
- getUploadCosts({ bytes, }) {
56
- return __awaiter(this, void 0, void 0, function* () {
57
- return this.paymentService.getUploadCosts({ bytes });
58
- });
38
+ async getUploadCosts({ bytes, }) {
39
+ return this.paymentService.getUploadCosts({ bytes });
59
40
  }
60
41
  /**
61
42
  * Determines the amount of 'winc' that would be returned for a given currency and amount, including all Turbo cost adjustments and fees.
62
43
  */
63
- getWincForFiat({ amount, currency, }) {
64
- return __awaiter(this, void 0, void 0, function* () {
65
- return this.paymentService.getWincForFiat({ amount, currency });
66
- });
44
+ async getWincForFiat({ amount, currency, }) {
45
+ return this.paymentService.getWincForFiat({ amount, currency });
67
46
  }
68
47
  /**
69
48
  * Uploads a signed data item to the Turbo Upload Service.
70
49
  */
71
- uploadSignedDataItem({ dataItemStreamFactory, signal, }) {
72
- return __awaiter(this, void 0, void 0, function* () {
73
- return this.uploadService.uploadSignedDataItem({
74
- dataItemStreamFactory,
75
- signal,
76
- });
50
+ async uploadSignedDataItem({ dataItemStreamFactory, signal, }) {
51
+ return this.uploadService.uploadSignedDataItem({
52
+ dataItemStreamFactory,
53
+ signal,
77
54
  });
78
55
  }
79
56
  }
@@ -84,17 +61,13 @@ export class TurboAuthenticatedClient extends TurboUnauthenticatedClient {
84
61
  /**
85
62
  * Returns the current balance of the user's wallet in 'winc'.
86
63
  */
87
- getBalance() {
88
- return __awaiter(this, void 0, void 0, function* () {
89
- return this.paymentService.getBalance();
90
- });
64
+ async getBalance() {
65
+ return this.paymentService.getBalance();
91
66
  }
92
67
  /**
93
68
  * Signs and uploads raw data to the Turbo Upload Service.
94
69
  */
95
- uploadFile({ fileStreamFactory, signal, }) {
96
- return __awaiter(this, void 0, void 0, function* () {
97
- return this.uploadService.uploadFile({ fileStreamFactory, signal });
98
- });
70
+ async uploadFile({ fileStreamFactory, signal, }) {
71
+ return this.uploadService.uploadFile({ fileStreamFactory, signal });
99
72
  }
100
73
  }
@@ -1,12 +1,3 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  import { TurboHTTPService } from './http.js';
11
2
  export class TurboUnauthenticatedUploadService {
12
3
  constructor({ url = 'https://upload.ardrive.dev', retryConfig, }) {
@@ -15,17 +6,15 @@ export class TurboUnauthenticatedUploadService {
15
6
  retryConfig,
16
7
  });
17
8
  }
18
- uploadSignedDataItem({ dataItemStreamFactory, signal, }) {
19
- return __awaiter(this, void 0, void 0, function* () {
20
- // TODO: add p-limit constraint or replace with separate upload class
21
- return this.httpService.post({
22
- endpoint: `/tx`,
23
- signal,
24
- data: dataItemStreamFactory(),
25
- headers: {
26
- 'content-type': 'application/octet-stream',
27
- },
28
- });
9
+ async uploadSignedDataItem({ dataItemStreamFactory, signal, }) {
10
+ // TODO: add p-limit constraint or replace with separate upload class
11
+ return this.httpService.post({
12
+ endpoint: `/tx`,
13
+ signal,
14
+ data: dataItemStreamFactory(),
15
+ headers: {
16
+ 'content-type': 'application/octet-stream',
17
+ },
29
18
  });
30
19
  }
31
20
  }
@@ -35,20 +24,18 @@ export class TurboAuthenticatedUploadService extends TurboUnauthenticatedUploadS
35
24
  super({ url, retryConfig });
36
25
  this.signer = signer;
37
26
  }
38
- uploadFile({ fileStreamFactory, signal, }) {
39
- return __awaiter(this, void 0, void 0, function* () {
40
- const signedDataItem = yield this.signer.signDataItem({
41
- fileStreamFactory,
42
- });
43
- // TODO: add p-limit constraint or replace with separate upload class
44
- return this.httpService.post({
45
- endpoint: `/tx`,
46
- signal,
47
- data: signedDataItem,
48
- headers: {
49
- 'content-type': 'application/octet-stream',
50
- },
51
- });
27
+ async uploadFile({ fileStreamFactory, signal, }) {
28
+ const signedDataItem = await this.signer.signDataItem({
29
+ fileStreamFactory,
30
+ });
31
+ // TODO: add p-limit constraint or replace with separate upload class
32
+ return this.httpService.post({
33
+ endpoint: `/tx`,
34
+ signal,
35
+ data: signedDataItem,
36
+ headers: {
37
+ 'content-type': 'application/octet-stream',
38
+ },
52
39
  });
53
40
  }
54
41
  }
@@ -20,8 +20,14 @@ import { TurboNodeArweaveSigner } from './signer.js';
20
20
  export class TurboFactory extends TurboBaseFactory {
21
21
  static authenticated({ privateKey, paymentServiceConfig = {}, uploadServiceConfig = {}, }) {
22
22
  const signer = new TurboNodeArweaveSigner({ privateKey });
23
- const paymentService = new TurboAuthenticatedPaymentService(Object.assign(Object.assign({}, paymentServiceConfig), { signer }));
24
- const uploadService = new TurboAuthenticatedUploadService(Object.assign(Object.assign({}, uploadServiceConfig), { signer }));
23
+ const paymentService = new TurboAuthenticatedPaymentService({
24
+ ...paymentServiceConfig,
25
+ signer,
26
+ });
27
+ const uploadService = new TurboAuthenticatedUploadService({
28
+ ...uploadServiceConfig,
29
+ signer,
30
+ });
25
31
  return new TurboAuthenticatedClient({
26
32
  uploadService,
27
33
  paymentService,
@@ -16,5 +16,5 @@
16
16
  */
17
17
  export * from './factory.js';
18
18
  export * from './signer.js';
19
+ export * from '../types.js';
19
20
  export * from '../common/index.js';
20
- export * from '../types/index.js';
@@ -1,12 +1,3 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  /**
11
2
  * Copyright (C) 2022-2023 Permanent Data Solutions, Inc. All Rights Reserved.
12
3
  *
@@ -39,16 +30,14 @@ export class TurboNodeArweaveSigner {
39
30
  return streamSigner(stream1, stream2, this.signer);
40
31
  }
41
32
  // NOTE: this might be better in a parent class or elsewhere - easy enough to leave in here now and does require specific environment version of crypto
42
- generateSignedRequestHeaders() {
43
- return __awaiter(this, void 0, void 0, function* () {
44
- const nonce = randomBytes(16).toString('hex');
45
- const buffer = Buffer.from(nonce);
46
- const signature = yield Arweave.crypto.sign(this.privateKey, buffer);
47
- return {
48
- 'x-public-key': this.privateKey.n,
49
- 'x-nonce': nonce,
50
- 'x-signature': toB64Url(Buffer.from(signature)),
51
- };
52
- });
33
+ async generateSignedRequestHeaders() {
34
+ const nonce = randomBytes(16).toString('hex');
35
+ const buffer = Buffer.from(nonce);
36
+ const signature = await Arweave.crypto.sign(this.privateKey, buffer);
37
+ return {
38
+ 'x-public-key': this.privateKey.n,
39
+ 'x-nonce': nonce,
40
+ 'x-signature': toB64Url(Buffer.from(signature)),
41
+ };
53
42
  }
54
43
  }
@@ -14,17 +14,26 @@
14
14
  * You should have received a copy of the GNU Affero General Public License
15
15
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
  */
17
- import axios from 'axios';
18
- import * as rax from 'retry-axios';
17
+ import axios, { CanceledError } from 'axios';
18
+ import axiosRetry from 'axios-retry';
19
19
  export const createAxiosInstance = ({ axiosConfig = {}, retryConfig = {
20
- backoffType: 'exponential',
21
- retry: 3,
22
- onRetryAttempt: (err) => {
23
- const cfg = rax.getConfig(err);
24
- console.debug(`Retry attempt #${cfg === null || cfg === void 0 ? void 0 : cfg.currentRetryAttempt}`);
20
+ retryDelay: axiosRetry.exponentialDelay,
21
+ retries: 3,
22
+ retryCondition: (error) => {
23
+ return (!(error instanceof CanceledError) &&
24
+ axiosRetry.isNetworkOrIdempotentRequestError(error));
25
+ },
26
+ onRetry: (retryCount, error) => {
27
+ console.debug(`Request failed, ${error}. Retry attempt #${retryCount}...`);
25
28
  },
26
29
  }, }) => {
27
- const axiosInstance = axios.create(Object.assign(Object.assign({}, axiosConfig), { validateStatus: () => true }));
28
- axiosInstance.defaults.raxConfig = Object.assign({ instance: axiosInstance }, retryConfig);
30
+ const axiosInstance = axios.create({
31
+ ...axiosConfig,
32
+ validateStatus: () => true, // don't throw on non-200 status codes
33
+ });
34
+ // eslint-disable-next-line
35
+ if (retryConfig.retries && retryConfig.retries > 0) {
36
+ axiosRetry(axiosInstance, retryConfig);
37
+ }
29
38
  return axiosInstance;
30
39
  };
@@ -1,24 +1,13 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- export function readableStreamToBuffer({ stream, }) {
11
- return __awaiter(this, void 0, void 0, function* () {
12
- const reader = stream.getReader();
13
- const chunks = [];
14
- let done = false;
15
- while (!done) {
16
- const { done: streamDone, value } = yield reader.read();
17
- done = streamDone;
18
- if (!done) {
19
- chunks.push(value);
20
- }
1
+ export async function readableStreamToBuffer({ stream, }) {
2
+ const reader = stream.getReader();
3
+ const chunks = [];
4
+ let done = false;
5
+ while (!done) {
6
+ const { done: streamDone, value } = await reader.read();
7
+ done = streamDone;
8
+ if (!done) {
9
+ chunks.push(value);
21
10
  }
22
- return Buffer.concat(chunks);
23
- });
11
+ }
12
+ return Buffer.concat(chunks);
24
13
  }
@@ -20,8 +20,14 @@ import { TurboWebArweaveSigner } from './signer.js';
20
20
  export class TurboFactory extends TurboBaseFactory {
21
21
  static authenticated({ privateKey, paymentServiceConfig = {}, uploadServiceConfig = {}, }) {
22
22
  const signer = new TurboWebArweaveSigner({ privateKey });
23
- const paymentService = new TurboAuthenticatedPaymentService(Object.assign(Object.assign({}, paymentServiceConfig), { signer }));
24
- const uploadService = new TurboAuthenticatedUploadService(Object.assign(Object.assign({}, uploadServiceConfig), { signer }));
23
+ const paymentService = new TurboAuthenticatedPaymentService({
24
+ ...paymentServiceConfig,
25
+ signer,
26
+ });
27
+ const uploadService = new TurboAuthenticatedUploadService({
28
+ ...uploadServiceConfig,
29
+ signer,
30
+ });
25
31
  return new TurboAuthenticatedClient({
26
32
  uploadService,
27
33
  paymentService,
@@ -17,4 +17,4 @@
17
17
  export * from './factory.js';
18
18
  export * from './signer.js';
19
19
  export * from '../common/index.js';
20
- export * from '../types/index.js';
20
+ export * from '../types.js';
@@ -1,12 +1,3 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  /**
11
2
  * Copyright (C) 2022-2023 Permanent Data Solutions, Inc. All Rights Reserved.
12
3
  *
@@ -33,30 +24,27 @@ export class TurboWebArweaveSigner {
33
24
  this.privateKey = privateKey;
34
25
  this.signer = new ArweaveSigner(this.privateKey);
35
26
  }
36
- signDataItem({ fileStreamFactory, }) {
37
- return __awaiter(this, void 0, void 0, function* () {
38
- // TODO: converts the readable stream to a buffer bc incrementally signing ReadableStreams is not trivial
39
- const buffer = yield readableStreamToBuffer({
40
- stream: fileStreamFactory(),
41
- // TODO: add payload size to get performance improvements
42
- });
43
- const signedDataItem = createData(buffer, this.signer);
44
- yield signedDataItem.sign(this.signer);
45
- return signedDataItem.getRaw();
27
+ async signDataItem({ fileStreamFactory, }) {
28
+ // TODO: converts the readable stream to a buffer bc incrementally signing ReadableStreams is not trivial
29
+ const buffer = await readableStreamToBuffer({
30
+ stream: fileStreamFactory(),
31
+ // TODO: add payload size to get performance improvements
46
32
  });
33
+ const signedDataItem = createData(buffer, this.signer);
34
+ await signedDataItem.sign(this.signer);
35
+ return signedDataItem.getRaw();
47
36
  }
48
37
  // NOTE: this might be better in a parent class or elsewhere - easy enough to leave in here now and does require specific environment version of crypto
49
- generateSignedRequestHeaders() {
50
- return __awaiter(this, void 0, void 0, function* () {
51
- const nonce = randomBytes(16).toString('hex');
52
- const buffer = Buffer.from(nonce);
53
- // a bit hacky - but easiest way to solve web signing issues while still building for cjs
54
- const signature = yield Arweave.default.crypto.sign(this.privateKey, buffer, {});
55
- return {
56
- 'x-public-key': this.privateKey.n,
57
- 'x-nonce': nonce,
58
- 'x-signature': toB64Url(Buffer.from(signature)),
59
- };
60
- });
38
+ async generateSignedRequestHeaders() {
39
+ // a bit hacky - but arweave exports cause issues in tests vs. browser
40
+ const arweave = Arweave.default ?? Arweave;
41
+ const nonce = randomBytes(16).toString('hex');
42
+ const buffer = Buffer.from(nonce);
43
+ const signature = await arweave.crypto.sign(this.privateKey, buffer, {});
44
+ return {
45
+ 'x-public-key': this.privateKey.n,
46
+ 'x-nonce': nonce,
47
+ 'x-signature': toB64Url(Buffer.from(signature)),
48
+ };
61
49
  }
62
50
  }
@@ -14,8 +14,9 @@
14
14
  * You should have received a copy of the GNU Affero General Public License
15
15
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
  */
17
- import { TurboPublicConfiguration } from '../types/turbo.js';
17
+ import { TurboPublicConfiguration } from '../types.js';
18
18
  import { TurboUnauthenticatedClient } from './turbo.js';
19
19
  export declare class TurboBaseFactory {
20
20
  static unauthenticated({ paymentServiceConfig, uploadServiceConfig, }?: TurboPublicConfiguration): TurboUnauthenticatedClient;
21
21
  }
22
+ //# sourceMappingURL=factory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"factory.d.ts","sourceRoot":"","sources":["../../../src/common/factory.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAEvD,OAAO,EAAE,0BAA0B,EAAE,MAAM,YAAY,CAAC;AAGxD,qBAAa,gBAAgB;IAC3B,MAAM,CAAC,eAAe,CAAC,EACrB,oBAAyB,EACzB,mBAAwB,GACzB,GAAE,wBAA6B;CAYjC"}
@@ -18,15 +18,15 @@
18
18
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19
19
  */
20
20
  import { AxiosInstance } from 'axios';
21
- import { RetryConfig } from 'retry-axios';
21
+ import { IAxiosRetryConfig } from 'axios-retry';
22
22
  import { Readable } from 'stream';
23
23
  import { ReadableStream } from 'stream/web';
24
- import { TurboHTTPServiceInterface, TurboSignedRequestHeaders } from '../types/turbo.js';
24
+ import { TurboHTTPServiceInterface, TurboSignedRequestHeaders } from '../types.js';
25
25
  export declare class TurboHTTPService implements TurboHTTPServiceInterface {
26
26
  protected axios: AxiosInstance;
27
27
  constructor({ url, retryConfig, }: {
28
28
  url: string;
29
- retryConfig?: RetryConfig;
29
+ retryConfig?: IAxiosRetryConfig;
30
30
  });
31
31
  get<T>({ endpoint, signal, allowedStatuses, headers, }: {
32
32
  endpoint: string;
@@ -42,3 +42,4 @@ export declare class TurboHTTPService implements TurboHTTPServiceInterface {
42
42
  data: Readable | Buffer | ReadableStream;
43
43
  }): Promise<T>;
44
44
  }
45
+ //# sourceMappingURL=http.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../../src/common/http.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AACtC,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAClC,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAE5C,OAAO,EACL,yBAAyB,EACzB,yBAAyB,EAC1B,MAAM,aAAa,CAAC;AAIrB,qBAAa,gBAAiB,YAAW,yBAAyB;IAChE,SAAS,CAAC,KAAK,EAAE,aAAa,CAAC;gBACnB,EACV,GAAG,EACH,WAAW,GACZ,EAAE;QACD,GAAG,EAAE,MAAM,CAAC;QACZ,WAAW,CAAC,EAAE,iBAAiB,CAAC;KACjC;IAQK,GAAG,CAAC,CAAC,EAAE,EACX,QAAQ,EACR,MAAM,EACN,eAA4B,EAC5B,OAAO,GACR,EAAE;QACD,QAAQ,EAAE,MAAM,CAAC;QACjB,MAAM,CAAC,EAAE,WAAW,CAAC;QACrB,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC;QAC3B,OAAO,CAAC,EAAE,OAAO,CAAC,yBAAyB,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;KACvE,GAAG,OAAO,CAAC,CAAC,CAAC;IAaR,IAAI,CAAC,CAAC,EAAE,EACZ,QAAQ,EACR,MAAM,EACN,eAA4B,EAC5B,OAAO,EACP,IAAI,GACL,EAAE;QACD,QAAQ,EAAE,MAAM,CAAC;QACjB,MAAM,CAAC,EAAE,WAAW,CAAC;QACrB,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC;QAC3B,OAAO,CAAC,EAAE,OAAO,CAAC,yBAAyB,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QACtE,IAAI,EAAE,QAAQ,GAAG,MAAM,GAAG,cAAc,CAAC;KAC1C,GAAG,OAAO,CAAC,CAAC,CAAC;CAgBf"}
@@ -17,3 +17,4 @@
17
17
  export * from './upload.js';
18
18
  export * from './payment.js';
19
19
  export * from './turbo.js';
20
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/common/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC"}