@ardrive/turbo-sdk 1.0.0-alpha.9 → 1.0.0-beta.2

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 (98) hide show
  1. package/README.md +169 -28
  2. package/bundles/web.bundle.min.js +55776 -0
  3. package/lib/cjs/common/currency.js +42 -0
  4. package/lib/cjs/common/factory.js +6 -2
  5. package/lib/cjs/common/http.js +16 -29
  6. package/lib/cjs/common/index.js +1 -0
  7. package/lib/cjs/common/payment.js +56 -47
  8. package/lib/cjs/common/turbo.js +24 -40
  9. package/lib/cjs/common/upload.js +27 -34
  10. package/lib/cjs/node/factory.js +8 -2
  11. package/lib/cjs/node/index.js +1 -1
  12. package/lib/cjs/node/signer.js +42 -22
  13. package/lib/cjs/package.json +1 -0
  14. package/lib/cjs/utils/axiosClient.js +18 -9
  15. package/lib/cjs/utils/readableStream.js +13 -22
  16. package/lib/cjs/web/factory.js +8 -2
  17. package/lib/cjs/web/index.js +1 -1
  18. package/lib/cjs/web/signer.js +24 -31
  19. package/lib/esm/common/currency.js +27 -0
  20. package/lib/esm/common/factory.js +6 -2
  21. package/lib/esm/common/http.js +16 -29
  22. package/lib/esm/common/index.js +1 -0
  23. package/lib/esm/common/payment.js +56 -47
  24. package/lib/esm/common/turbo.js +24 -40
  25. package/lib/esm/common/upload.js +27 -34
  26. package/lib/esm/node/factory.js +8 -2
  27. package/lib/esm/node/index.js +1 -1
  28. package/lib/esm/node/signer.js +42 -22
  29. package/lib/esm/utils/axiosClient.js +18 -9
  30. package/lib/esm/utils/readableStream.js +13 -22
  31. package/lib/esm/web/factory.js +8 -2
  32. package/lib/esm/web/index.js +1 -1
  33. package/lib/esm/web/signer.js +24 -31
  34. package/lib/types/common/currency.d.ts +43 -0
  35. package/lib/types/common/currency.d.ts.map +1 -0
  36. package/lib/{cjs → types}/common/factory.d.ts +2 -1
  37. package/lib/types/common/factory.d.ts.map +1 -0
  38. package/lib/{cjs → types}/common/http.d.ts +4 -3
  39. package/lib/types/common/http.d.ts.map +1 -0
  40. package/lib/{esm → types}/common/index.d.ts +2 -0
  41. package/lib/types/common/index.d.ts.map +1 -0
  42. package/lib/{esm/types/arweave.d.ts → types/common/jwk.d.ts} +1 -0
  43. package/lib/types/common/jwk.d.ts.map +1 -0
  44. package/lib/{esm → types}/common/payment.d.ts +14 -6
  45. package/lib/types/common/payment.d.ts.map +1 -0
  46. package/lib/{cjs → types}/common/turbo.d.ts +13 -11
  47. package/lib/types/common/turbo.d.ts.map +1 -0
  48. package/lib/{esm → types}/common/upload.d.ts +4 -4
  49. package/lib/types/common/upload.d.ts.map +1 -0
  50. package/lib/{esm → types}/node/factory.d.ts +2 -1
  51. package/lib/types/node/factory.d.ts.map +1 -0
  52. package/lib/{esm → types}/node/index.d.ts +2 -1
  53. package/lib/types/node/index.d.ts.map +1 -0
  54. package/lib/{cjs → types}/node/signer.d.ts +10 -4
  55. package/lib/types/node/signer.d.ts.map +1 -0
  56. package/lib/{cjs/types/turbo.d.ts → types/types.d.ts} +46 -11
  57. package/lib/types/types.d.ts.map +1 -0
  58. package/lib/{esm → types}/utils/axiosClient.d.ts +3 -2
  59. package/lib/types/utils/axiosClient.d.ts.map +1 -0
  60. package/lib/{cjs → types}/utils/base64.d.ts +3 -2
  61. package/lib/types/utils/base64.d.ts.map +1 -0
  62. package/lib/{esm → types}/utils/errors.d.ts +1 -0
  63. package/lib/types/utils/errors.d.ts.map +1 -0
  64. package/lib/{cjs → types}/utils/readableStream.d.ts +3 -1
  65. package/lib/types/utils/readableStream.d.ts.map +1 -0
  66. package/lib/{cjs → types}/web/factory.d.ts +2 -1
  67. package/lib/types/web/factory.d.ts.map +1 -0
  68. package/lib/{cjs → types}/web/index.d.ts +2 -1
  69. package/lib/types/web/index.d.ts.map +1 -0
  70. package/lib/{cjs → types}/web/signer.d.ts +9 -4
  71. package/lib/types/web/signer.d.ts.map +1 -0
  72. package/package.json +13 -15
  73. package/lib/cjs/common/index.d.ts +0 -19
  74. package/lib/cjs/common/payment.d.ts +0 -41
  75. package/lib/cjs/common/upload.d.ts +0 -29
  76. package/lib/cjs/node/factory.d.ts +0 -22
  77. package/lib/cjs/node/index.d.ts +0 -20
  78. package/lib/cjs/types/arweave.d.ts +0 -29
  79. package/lib/cjs/types/index.d.ts +0 -18
  80. package/lib/cjs/types/index.js +0 -34
  81. package/lib/cjs/utils/axiosClient.d.ts +0 -23
  82. package/lib/cjs/utils/errors.d.ts +0 -22
  83. package/lib/esm/common/factory.d.ts +0 -21
  84. package/lib/esm/common/http.d.ts +0 -44
  85. package/lib/esm/common/turbo.d.ts +0 -73
  86. package/lib/esm/node/signer.d.ts +0 -36
  87. package/lib/esm/types/index.d.ts +0 -18
  88. package/lib/esm/types/index.js +0 -18
  89. package/lib/esm/types/turbo.d.ts +0 -149
  90. package/lib/esm/utils/base64.d.ts +0 -9
  91. package/lib/esm/utils/readableStream.d.ts +0 -22
  92. package/lib/esm/web/factory.d.ts +0 -22
  93. package/lib/esm/web/index.d.ts +0 -20
  94. package/lib/esm/web/signer.d.ts +0 -37
  95. /package/lib/cjs/{types/arweave.js → common/jwk.js} +0 -0
  96. /package/lib/cjs/{types/turbo.js → types.js} +0 -0
  97. /package/lib/esm/{types/arweave.js → common/jwk.js} +0 -0
  98. /package/lib/esm/{types/turbo.js → types.js} +0 -0
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JPY = exports.BRL = exports.HKD = exports.SGD = exports.INR = exports.AUD = exports.CAD = exports.GBP = exports.EUR = exports.USD = exports.TwoDecimalCurrency = exports.ZeroDecimalCurrency = void 0;
4
+ class ZeroDecimalCurrency {
5
+ constructor(amount, type) {
6
+ this.amount = amount;
7
+ this.type = type;
8
+ }
9
+ }
10
+ exports.ZeroDecimalCurrency = ZeroDecimalCurrency;
11
+ class TwoDecimalCurrency {
12
+ constructor(a, type) {
13
+ this.a = a;
14
+ this.type = type;
15
+ }
16
+ get amount() {
17
+ return this.a * 100;
18
+ }
19
+ }
20
+ exports.TwoDecimalCurrency = TwoDecimalCurrency;
21
+ // Two decimal currencies that are supported by the Turbo API
22
+ const USD = (usd) => new TwoDecimalCurrency(usd, 'usd');
23
+ exports.USD = USD;
24
+ const EUR = (eur) => new TwoDecimalCurrency(eur, 'eur');
25
+ exports.EUR = EUR;
26
+ const GBP = (gbp) => new TwoDecimalCurrency(gbp, 'gbp');
27
+ exports.GBP = GBP;
28
+ const CAD = (cad) => new TwoDecimalCurrency(cad, 'cad');
29
+ exports.CAD = CAD;
30
+ const AUD = (aud) => new TwoDecimalCurrency(aud, 'aud');
31
+ exports.AUD = AUD;
32
+ const INR = (inr) => new TwoDecimalCurrency(inr, 'inr');
33
+ exports.INR = INR;
34
+ const SGD = (sgd) => new TwoDecimalCurrency(sgd, 'sgd');
35
+ exports.SGD = SGD;
36
+ const HKD = (hkd) => new TwoDecimalCurrency(hkd, 'hkd');
37
+ exports.HKD = HKD;
38
+ const BRL = (brl) => new TwoDecimalCurrency(brl, 'brl');
39
+ exports.BRL = BRL;
40
+ // Zero decimal currencies that are supported by the Turbo API
41
+ const JPY = (jpy) => new ZeroDecimalCurrency(jpy, 'jpy');
42
+ exports.JPY = JPY;
@@ -6,8 +6,12 @@ const turbo_js_1 = require("./turbo.js");
6
6
  const upload_js_1 = require("./upload.js");
7
7
  class TurboBaseFactory {
8
8
  static unauthenticated({ paymentServiceConfig = {}, uploadServiceConfig = {}, } = {}) {
9
- const paymentService = new payment_js_1.TurboUnauthenticatedPaymentService(Object.assign({}, paymentServiceConfig));
10
- const uploadService = new upload_js_1.TurboUnauthenticatedUploadService(Object.assign({}, uploadServiceConfig));
9
+ const paymentService = new payment_js_1.TurboUnauthenticatedPaymentService({
10
+ ...paymentServiceConfig,
11
+ });
12
+ const uploadService = new upload_js_1.TurboUnauthenticatedUploadService({
13
+ ...uploadServiceConfig,
14
+ });
11
15
  return new turbo_js_1.TurboUnauthenticatedClient({
12
16
  uploadService,
13
17
  paymentService,
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.TurboHTTPService = void 0;
13
4
  const axiosClient_js_1 = require("../utils/axiosClient.js");
@@ -21,29 +12,25 @@ class TurboHTTPService {
21
12
  retryConfig,
22
13
  });
23
14
  }
24
- get({ endpoint, signal, allowedStatuses = [200, 202], headers, }) {
25
- return __awaiter(this, void 0, void 0, function* () {
26
- const { status, statusText, data } = yield this.axios.get(endpoint, {
27
- headers,
28
- signal,
29
- });
30
- if (!allowedStatuses.includes(status)) {
31
- throw new errors_js_1.FailedRequestError(status, statusText);
32
- }
33
- return data;
15
+ async get({ endpoint, signal, allowedStatuses = [200, 202], headers, }) {
16
+ const { status, statusText, data } = await this.axios.get(endpoint, {
17
+ headers,
18
+ signal,
34
19
  });
20
+ if (!allowedStatuses.includes(status)) {
21
+ throw new errors_js_1.FailedRequestError(status, statusText);
22
+ }
23
+ return data;
35
24
  }
36
- post({ endpoint, signal, allowedStatuses = [200, 202], headers, data, }) {
37
- return __awaiter(this, void 0, void 0, function* () {
38
- const { status, statusText, data: response, } = yield this.axios.post(endpoint, data, {
39
- headers,
40
- signal,
41
- });
42
- if (!allowedStatuses.includes(status)) {
43
- throw new errors_js_1.FailedRequestError(status, statusText);
44
- }
45
- return response;
25
+ async post({ endpoint, signal, allowedStatuses = [200, 202], headers, data, }) {
26
+ const { status, statusText, data: response, } = await this.axios.post(endpoint, data, {
27
+ headers,
28
+ signal,
46
29
  });
30
+ if (!allowedStatuses.includes(status)) {
31
+ throw new errors_js_1.FailedRequestError(status, statusText);
32
+ }
33
+ return response;
47
34
  }
48
35
  }
49
36
  exports.TurboHTTPService = TurboHTTPService;
@@ -33,3 +33,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
33
33
  __exportStar(require("./upload.js"), exports);
34
34
  __exportStar(require("./payment.js"), exports);
35
35
  __exportStar(require("./turbo.js"), exports);
36
+ __exportStar(require("./currency.js"), exports);
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.TurboAuthenticatedPaymentService = exports.TurboUnauthenticatedPaymentService = void 0;
13
4
  const http_js_1 = require("./http.js");
@@ -19,48 +10,59 @@ class TurboUnauthenticatedPaymentService {
19
10
  });
20
11
  }
21
12
  getFiatRates() {
22
- return __awaiter(this, void 0, void 0, function* () {
23
- return this.httpService.get({
24
- endpoint: '/rates',
25
- });
13
+ return this.httpService.get({
14
+ endpoint: '/rates',
26
15
  });
27
16
  }
28
17
  getFiatToAR({ currency, }) {
29
- return __awaiter(this, void 0, void 0, function* () {
30
- return this.httpService.get({
31
- endpoint: `/rates/${currency}`,
32
- });
18
+ return this.httpService.get({
19
+ endpoint: `/rates/${currency}`,
33
20
  });
34
21
  }
35
22
  getSupportedCountries() {
36
- return __awaiter(this, void 0, void 0, function* () {
37
- return this.httpService.get({
38
- endpoint: '/countries',
39
- });
23
+ return this.httpService.get({
24
+ endpoint: '/countries',
40
25
  });
41
26
  }
42
27
  getSupportedCurrencies() {
43
- return __awaiter(this, void 0, void 0, function* () {
44
- return this.httpService.get({
45
- endpoint: '/currencies',
46
- });
28
+ return this.httpService.get({
29
+ endpoint: '/currencies',
47
30
  });
48
31
  }
49
- getUploadCosts({ bytes, }) {
50
- return __awaiter(this, void 0, void 0, function* () {
51
- const fetchPricePromises = bytes.map((byteCount) => this.httpService.get({
52
- endpoint: `/price/bytes/${byteCount}`,
53
- }));
54
- const wincCostsForBytes = yield Promise.all(fetchPricePromises);
55
- return wincCostsForBytes;
32
+ async getUploadCosts({ bytes, }) {
33
+ const fetchPricePromises = bytes.map((byteCount) => this.httpService.get({
34
+ endpoint: `/price/bytes/${byteCount}`,
35
+ }));
36
+ const wincCostsForBytes = await Promise.all(fetchPricePromises);
37
+ return wincCostsForBytes;
38
+ }
39
+ getWincForFiat({ amount, }) {
40
+ const { amount: paymentAmount, type: currencyType } = amount;
41
+ return this.httpService.get({
42
+ endpoint: `/price/${currencyType}/${paymentAmount}`,
56
43
  });
57
44
  }
58
- getWincForFiat({ amount, currency }) {
59
- return __awaiter(this, void 0, void 0, function* () {
60
- return this.httpService.get({
61
- endpoint: `/price/${currency}/${amount}`,
62
- });
45
+ appendPromoCodesToQuery(promoCodes) {
46
+ const promoCodesQuery = promoCodes.join(',');
47
+ return promoCodesQuery ? `?promoCode=${promoCodesQuery}` : '';
48
+ }
49
+ async getCheckout({ amount, owner, promoCodes = [] }, headers) {
50
+ const { amount: paymentAmount, type: currencyType } = amount;
51
+ const endpoint = `/top-up/checkout-session/${owner}/${currencyType}/${paymentAmount}${this.appendPromoCodesToQuery(promoCodes)}`;
52
+ const { adjustments, paymentSession, topUpQuote } = await this.httpService.get({
53
+ endpoint,
54
+ headers,
63
55
  });
56
+ return {
57
+ winc: topUpQuote.winstonCreditAmount,
58
+ adjustments,
59
+ url: paymentSession.url,
60
+ paymentAmount: topUpQuote.paymentAmount,
61
+ quotedPaymentAmount: topUpQuote.quotedPaymentAmount,
62
+ };
63
+ }
64
+ createCheckoutSession(params) {
65
+ return this.getCheckout(params);
64
66
  }
65
67
  }
66
68
  exports.TurboUnauthenticatedPaymentService = TurboUnauthenticatedPaymentService;
@@ -70,17 +72,24 @@ class TurboAuthenticatedPaymentService extends TurboUnauthenticatedPaymentServic
70
72
  super({ url, retryConfig });
71
73
  this.signer = signer;
72
74
  }
73
- getBalance() {
74
- return __awaiter(this, void 0, void 0, function* () {
75
- const headers = yield this.signer.generateSignedRequestHeaders();
76
- const balance = yield this.httpService.get({
77
- endpoint: '/balance',
78
- headers,
79
- allowedStatuses: [200, 404],
80
- });
81
- // 404's don't return a balance, so default to 0
82
- return balance.winc ? balance : { winc: '0' };
75
+ async getBalance() {
76
+ const headers = await this.signer.generateSignedRequestHeaders();
77
+ const balance = await this.httpService.get({
78
+ endpoint: '/balance',
79
+ headers,
80
+ allowedStatuses: [200, 404],
83
81
  });
82
+ // 404's don't return a balance, so default to 0
83
+ return balance.winc ? balance : { winc: '0' };
84
+ }
85
+ async getWincForFiat({ amount, promoCodes = [], }) {
86
+ return this.httpService.get({
87
+ endpoint: `/price/${amount.type}/${amount.amount}${this.appendPromoCodesToQuery(promoCodes)}`,
88
+ headers: await this.signer.generateSignedRequestHeaders(),
89
+ });
90
+ }
91
+ async createCheckoutSession(params) {
92
+ return this.getCheckout(params, await this.signer.generateSignedRequestHeaders());
84
93
  }
85
94
  }
86
95
  exports.TurboAuthenticatedPaymentService = TurboAuthenticatedPaymentService;
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.TurboAuthenticatedClient = exports.TurboUnauthenticatedClient = void 0;
13
4
  const payment_js_1 = require("./payment.js");
@@ -21,9 +12,7 @@ class TurboUnauthenticatedClient {
21
12
  * Returns the supported fiat currency conversion rate for 1AR based on current market prices.
22
13
  */
23
14
  getFiatToAR({ currency, }) {
24
- return __awaiter(this, void 0, void 0, function* () {
25
- return this.paymentService.getFiatToAR({ currency });
26
- });
15
+ return this.paymentService.getFiatToAR({ currency });
27
16
  }
28
17
  /**
29
18
  * Returns the latest conversion rates to purchase 1GiB of data for all supported currencies, including all adjustments and fees.
@@ -32,53 +21,48 @@ class TurboUnauthenticatedClient {
32
21
  * cost in 'winc' for a given number of bytes, use getUploadCosts.
33
22
  */
34
23
  getFiatRates() {
35
- return __awaiter(this, void 0, void 0, function* () {
36
- return this.paymentService.getFiatRates();
37
- });
24
+ return this.paymentService.getFiatRates();
38
25
  }
39
26
  /**
40
27
  * Returns a comprehensive list of supported countries that can purchase credits through the Turbo Payment Service.
41
28
  */
42
29
  getSupportedCountries() {
43
- return __awaiter(this, void 0, void 0, function* () {
44
- return this.paymentService.getSupportedCountries();
45
- });
30
+ return this.paymentService.getSupportedCountries();
46
31
  }
47
32
  /**
48
33
  * Returns a list of all supported fiat currencies.
49
34
  */
50
35
  getSupportedCurrencies() {
51
- return __awaiter(this, void 0, void 0, function* () {
52
- return this.paymentService.getSupportedCurrencies();
53
- });
36
+ return this.paymentService.getSupportedCurrencies();
54
37
  }
55
38
  /**
56
39
  * Determines the price in 'winc' to upload one data item of a specific size in bytes, including all Turbo cost adjustments and fees.
57
40
  */
58
41
  getUploadCosts({ bytes, }) {
59
- return __awaiter(this, void 0, void 0, function* () {
60
- return this.paymentService.getUploadCosts({ bytes });
61
- });
42
+ return this.paymentService.getUploadCosts({ bytes });
62
43
  }
63
44
  /**
64
45
  * Determines the amount of 'winc' that would be returned for a given currency and amount, including all Turbo cost adjustments and fees.
65
46
  */
66
- getWincForFiat({ amount, currency, }) {
67
- return __awaiter(this, void 0, void 0, function* () {
68
- return this.paymentService.getWincForFiat({ amount, currency });
69
- });
47
+ getWincForFiat(params) {
48
+ return this.paymentService.getWincForFiat(params);
70
49
  }
71
50
  /**
72
51
  * Uploads a signed data item to the Turbo Upload Service.
73
52
  */
74
- uploadSignedDataItem({ dataItemStreamFactory, signal, }) {
75
- return __awaiter(this, void 0, void 0, function* () {
76
- return this.uploadService.uploadSignedDataItem({
77
- dataItemStreamFactory,
78
- signal,
79
- });
53
+ uploadSignedDataItem({ dataItemStreamFactory, dataItemSizeFactory, signal, }) {
54
+ return this.uploadService.uploadSignedDataItem({
55
+ dataItemStreamFactory,
56
+ dataItemSizeFactory,
57
+ signal,
80
58
  });
81
59
  }
60
+ /**
61
+ * Creates a Turbo Checkout Session for a given amount and currency.
62
+ */
63
+ createCheckoutSession(params) {
64
+ return this.paymentService.createCheckoutSession(params);
65
+ }
82
66
  }
83
67
  exports.TurboUnauthenticatedClient = TurboUnauthenticatedClient;
84
68
  class TurboAuthenticatedClient extends TurboUnauthenticatedClient {
@@ -89,16 +73,16 @@ class TurboAuthenticatedClient extends TurboUnauthenticatedClient {
89
73
  * Returns the current balance of the user's wallet in 'winc'.
90
74
  */
91
75
  getBalance() {
92
- return __awaiter(this, void 0, void 0, function* () {
93
- return this.paymentService.getBalance();
94
- });
76
+ return this.paymentService.getBalance();
95
77
  }
96
78
  /**
97
79
  * Signs and uploads raw data to the Turbo Upload Service.
98
80
  */
99
- uploadFile({ fileStreamFactory, signal, }) {
100
- return __awaiter(this, void 0, void 0, function* () {
101
- return this.uploadService.uploadFile({ fileStreamFactory, signal });
81
+ uploadFile({ fileStreamFactory, fileSizeFactory, signal, }) {
82
+ return this.uploadService.uploadFile({
83
+ fileStreamFactory,
84
+ fileSizeFactory,
85
+ signal,
102
86
  });
103
87
  }
104
88
  }
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.TurboAuthenticatedUploadService = exports.TurboUnauthenticatedUploadService = void 0;
13
4
  const http_js_1 = require("./http.js");
@@ -18,17 +9,17 @@ class TurboUnauthenticatedUploadService {
18
9
  retryConfig,
19
10
  });
20
11
  }
21
- uploadSignedDataItem({ dataItemStreamFactory, signal, }) {
22
- return __awaiter(this, void 0, void 0, function* () {
23
- // TODO: add p-limit constraint or replace with separate upload class
24
- return this.httpService.post({
25
- endpoint: `/tx`,
26
- signal,
27
- data: dataItemStreamFactory(),
28
- headers: {
29
- 'content-type': 'application/octet-stream',
30
- },
31
- });
12
+ async uploadSignedDataItem({ dataItemStreamFactory, dataItemSizeFactory, signal, }) {
13
+ const fileSize = dataItemSizeFactory();
14
+ // TODO: add p-limit constraint or replace with separate upload class
15
+ return this.httpService.post({
16
+ endpoint: `/tx`,
17
+ signal,
18
+ data: dataItemStreamFactory(),
19
+ headers: {
20
+ 'content-type': 'application/octet-stream',
21
+ 'content-length': `${fileSize}`,
22
+ },
32
23
  });
33
24
  }
34
25
  }
@@ -39,20 +30,22 @@ class TurboAuthenticatedUploadService extends TurboUnauthenticatedUploadService
39
30
  super({ url, retryConfig });
40
31
  this.signer = signer;
41
32
  }
42
- uploadFile({ fileStreamFactory, signal, }) {
43
- return __awaiter(this, void 0, void 0, function* () {
44
- const signedDataItem = yield this.signer.signDataItem({
45
- fileStreamFactory,
46
- });
47
- // TODO: add p-limit constraint or replace with separate upload class
48
- return this.httpService.post({
49
- endpoint: `/tx`,
50
- signal,
51
- data: signedDataItem,
52
- headers: {
53
- 'content-type': 'application/octet-stream',
54
- },
55
- });
33
+ async uploadFile({ fileStreamFactory, fileSizeFactory, signal, }) {
34
+ const { dataItemStreamFactory, dataItemSizeFactory } = await this.signer.signDataItem({
35
+ fileStreamFactory,
36
+ fileSizeFactory,
37
+ });
38
+ const signedDataItem = dataItemStreamFactory();
39
+ const fileSize = dataItemSizeFactory();
40
+ // TODO: add p-limit constraint or replace with separate upload class
41
+ return this.httpService.post({
42
+ endpoint: `/tx`,
43
+ signal,
44
+ data: signedDataItem,
45
+ headers: {
46
+ 'content-type': 'application/octet-stream',
47
+ 'content-length': `${fileSize}`,
48
+ },
56
49
  });
57
50
  }
58
51
  }
@@ -23,8 +23,14 @@ const signer_js_1 = require("./signer.js");
23
23
  class TurboFactory extends factory_js_1.TurboBaseFactory {
24
24
  static authenticated({ privateKey, paymentServiceConfig = {}, uploadServiceConfig = {}, }) {
25
25
  const signer = new signer_js_1.TurboNodeArweaveSigner({ privateKey });
26
- const paymentService = new index_js_1.TurboAuthenticatedPaymentService(Object.assign(Object.assign({}, paymentServiceConfig), { signer }));
27
- const uploadService = new index_js_1.TurboAuthenticatedUploadService(Object.assign(Object.assign({}, uploadServiceConfig), { signer }));
26
+ const paymentService = new index_js_1.TurboAuthenticatedPaymentService({
27
+ ...paymentServiceConfig,
28
+ signer,
29
+ });
30
+ const uploadService = new index_js_1.TurboAuthenticatedUploadService({
31
+ ...uploadServiceConfig,
32
+ signer,
33
+ });
28
34
  return new index_js_1.TurboAuthenticatedClient({
29
35
  uploadService,
30
36
  paymentService,
@@ -32,5 +32,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
32
32
  */
33
33
  __exportStar(require("./factory.js"), exports);
34
34
  __exportStar(require("./signer.js"), exports);
35
+ __exportStar(require("../types.js"), exports);
35
36
  __exportStar(require("../common/index.js"), exports);
36
- __exportStar(require("../types/index.js"), exports);
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
4
  };
@@ -39,23 +30,52 @@ class TurboNodeArweaveSigner {
39
30
  this.privateKey = privateKey;
40
31
  this.signer = new arbundles_1.ArweaveSigner(this.privateKey);
41
32
  }
42
- signDataItem({ fileStreamFactory, }) {
33
+ async signDataItem({ fileStreamFactory, fileSizeFactory, }) {
43
34
  // TODO: replace with our own signer implementation
44
35
  const [stream1, stream2] = [fileStreamFactory(), fileStreamFactory()];
45
- return (0, arbundles_1.streamSigner)(stream1, stream2, this.signer);
36
+ const signedDataItem = await (0, arbundles_1.streamSigner)(stream1, stream2, this.signer);
37
+ // TODO: support target, anchor, and tags
38
+ const signedDataItemSize = this.calculateSignedDataHeadersSize({
39
+ dataSize: fileSizeFactory(),
40
+ });
41
+ return {
42
+ dataItemStreamFactory: () => signedDataItem,
43
+ dataItemSizeFactory: () => signedDataItemSize,
44
+ };
46
45
  }
47
46
  // 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
48
- generateSignedRequestHeaders() {
49
- return __awaiter(this, void 0, void 0, function* () {
50
- const nonce = (0, node_crypto_1.randomBytes)(16).toString('hex');
51
- const buffer = Buffer.from(nonce);
52
- const signature = yield index_js_1.default.crypto.sign(this.privateKey, buffer);
53
- return {
54
- 'x-public-key': this.privateKey.n,
55
- 'x-nonce': nonce,
56
- 'x-signature': (0, base64_js_1.toB64Url)(Buffer.from(signature)),
57
- };
58
- });
47
+ async generateSignedRequestHeaders() {
48
+ const nonce = (0, node_crypto_1.randomBytes)(16).toString('hex');
49
+ const buffer = Buffer.from(nonce);
50
+ const signature = await index_js_1.default.crypto.sign(this.privateKey, buffer);
51
+ return {
52
+ 'x-public-key': this.privateKey.n,
53
+ 'x-nonce': nonce,
54
+ 'x-signature': (0, base64_js_1.toB64Url)(Buffer.from(signature)),
55
+ };
56
+ }
57
+ // TODO: make dynamic that accepts anchor and target and tags to return the size of the headers + data
58
+ // reference https://github.com/ArweaveTeam/arweave-standards/blob/master/ans/ANS-104.md#13-dataitem-format
59
+ calculateSignedDataHeadersSize({ dataSize }) {
60
+ const anchor = 1; // + whatever they provide (max of 33)
61
+ const target = 1; // + whatever they provide (max of 33)
62
+ const tags = 0;
63
+ const signatureLength = 512;
64
+ const ownerLength = 512;
65
+ const signatureTypeLength = 2;
66
+ const numOfTagsLength = 8; // https://github.com/Bundlr-Network/arbundles/blob/master/src/tags.ts#L191-L198
67
+ const numOfTagsBytesLength = 8;
68
+ return [
69
+ anchor,
70
+ target,
71
+ tags,
72
+ signatureLength,
73
+ ownerLength,
74
+ signatureTypeLength,
75
+ numOfTagsLength,
76
+ numOfTagsBytesLength,
77
+ dataSize,
78
+ ].reduce((totalSize, currentSize) => (totalSize += currentSize));
59
79
  }
60
80
  }
61
81
  exports.TurboNodeArweaveSigner = TurboNodeArweaveSigner;
@@ -0,0 +1 @@
1
+ {"type": "commonjs"}
@@ -43,18 +43,27 @@ exports.createAxiosInstance = void 0;
43
43
  * You should have received a copy of the GNU Affero General Public License
44
44
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
45
45
  */
46
- const axios_1 = __importDefault(require("axios"));
47
- const rax = __importStar(require("retry-axios"));
46
+ const axios_1 = __importStar(require("axios"));
47
+ const axios_retry_1 = __importDefault(require("axios-retry"));
48
48
  const createAxiosInstance = ({ axiosConfig = {}, retryConfig = {
49
- backoffType: 'exponential',
50
- retry: 3,
51
- onRetryAttempt: (err) => {
52
- const cfg = rax.getConfig(err);
53
- console.debug(`Retry attempt #${cfg === null || cfg === void 0 ? void 0 : cfg.currentRetryAttempt}`);
49
+ retryDelay: axios_retry_1.default.exponentialDelay,
50
+ retries: 3,
51
+ retryCondition: (error) => {
52
+ return (!(error instanceof axios_1.CanceledError) &&
53
+ axios_retry_1.default.isNetworkOrIdempotentRequestError(error));
54
+ },
55
+ onRetry: (retryCount, error) => {
56
+ console.debug(`Request failed, ${error}. Retry attempt #${retryCount}...`);
54
57
  },
55
58
  }, }) => {
56
- const axiosInstance = axios_1.default.create(Object.assign(Object.assign({}, axiosConfig), { validateStatus: () => true }));
57
- axiosInstance.defaults.raxConfig = Object.assign({ instance: axiosInstance }, retryConfig);
59
+ const axiosInstance = axios_1.default.create({
60
+ ...axiosConfig,
61
+ validateStatus: () => true, // don't throw on non-200 status codes
62
+ });
63
+ // eslint-disable-next-line
64
+ if (retryConfig.retries && retryConfig.retries > 0) {
65
+ (0, axios_retry_1.default)(axiosInstance, retryConfig);
66
+ }
58
67
  return axiosInstance;
59
68
  };
60
69
  exports.createAxiosInstance = createAxiosInstance;
@@ -1,28 +1,19 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.readableStreamToBuffer = void 0;
13
- function readableStreamToBuffer({ stream, }) {
14
- return __awaiter(this, void 0, void 0, function* () {
15
- const reader = stream.getReader();
16
- const chunks = [];
17
- let done = false;
18
- while (!done) {
19
- const { done: streamDone, value } = yield reader.read();
20
- done = streamDone;
21
- if (!done) {
22
- chunks.push(value);
23
- }
4
+ async function readableStreamToBuffer({ stream, size, }) {
5
+ const reader = stream.getReader();
6
+ const buffer = Buffer.alloc(size);
7
+ let offset = 0;
8
+ let done = false;
9
+ while (!done) {
10
+ const { done: streamDone, value } = await reader.read();
11
+ done = streamDone;
12
+ if (!done) {
13
+ buffer.set(value, offset);
14
+ offset += value.byteLength;
24
15
  }
25
- return Buffer.concat(chunks);
26
- });
16
+ }
17
+ return buffer;
27
18
  }
28
19
  exports.readableStreamToBuffer = readableStreamToBuffer;