@ardrive/turbo-sdk 1.0.0-alpha.9 → 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.
- package/README.md +130 -23
- package/bundles/web.bundle.min.js +55668 -0
- package/lib/cjs/common/factory.js +6 -2
- package/lib/cjs/common/http.js +16 -29
- package/lib/cjs/common/payment.js +29 -52
- package/lib/cjs/common/turbo.js +20 -47
- package/lib/cjs/common/upload.js +21 -34
- package/lib/cjs/node/factory.js +8 -2
- package/lib/cjs/node/index.js +1 -1
- package/lib/cjs/node/signer.js +9 -20
- package/lib/cjs/package.json +1 -0
- package/lib/cjs/utils/axiosClient.js +18 -9
- package/lib/cjs/utils/readableStream.js +11 -22
- package/lib/cjs/web/factory.js +8 -2
- package/lib/cjs/web/index.js +1 -1
- package/lib/cjs/web/signer.js +19 -31
- package/lib/esm/common/factory.js +6 -2
- package/lib/esm/common/http.js +16 -29
- package/lib/esm/common/payment.js +29 -52
- package/lib/esm/common/turbo.js +20 -47
- package/lib/esm/common/upload.js +21 -34
- package/lib/esm/node/factory.js +8 -2
- package/lib/esm/node/index.js +1 -1
- package/lib/esm/node/signer.js +9 -20
- package/lib/esm/utils/axiosClient.js +18 -9
- package/lib/esm/utils/readableStream.js +11 -22
- package/lib/esm/web/factory.js +8 -2
- package/lib/esm/web/index.js +1 -1
- package/lib/esm/web/signer.js +19 -31
- package/lib/{cjs → types}/common/factory.d.ts +2 -1
- package/lib/types/common/factory.d.ts.map +1 -0
- package/lib/{cjs → types}/common/http.d.ts +4 -3
- package/lib/types/common/http.d.ts.map +1 -0
- package/lib/{esm → types}/common/index.d.ts +1 -0
- package/lib/types/common/index.d.ts.map +1 -0
- package/lib/{esm/types/arweave.d.ts → types/common/jwk.d.ts} +1 -0
- package/lib/types/common/jwk.d.ts.map +1 -0
- package/lib/{esm → types}/common/payment.d.ts +2 -2
- package/lib/types/common/payment.d.ts.map +1 -0
- package/lib/{cjs → types}/common/turbo.d.ts +6 -5
- package/lib/types/common/turbo.d.ts.map +1 -0
- package/lib/{esm → types}/common/upload.d.ts +2 -2
- package/lib/types/common/upload.d.ts.map +1 -0
- package/lib/{esm → types}/node/factory.d.ts +2 -1
- package/lib/types/node/factory.d.ts.map +1 -0
- package/lib/{esm → types}/node/index.d.ts +2 -1
- package/lib/types/node/index.d.ts.map +1 -0
- package/lib/{cjs → types}/node/signer.d.ts +3 -2
- package/lib/types/node/signer.d.ts.map +1 -0
- package/lib/{cjs/types/turbo.d.ts → types/types.d.ts} +4 -3
- package/lib/types/types.d.ts.map +1 -0
- package/lib/{esm → types}/utils/axiosClient.d.ts +3 -2
- package/lib/types/utils/axiosClient.d.ts.map +1 -0
- package/lib/{cjs → types}/utils/base64.d.ts +3 -2
- package/lib/types/utils/base64.d.ts.map +1 -0
- package/lib/{esm → types}/utils/errors.d.ts +1 -0
- package/lib/types/utils/errors.d.ts.map +1 -0
- package/lib/{cjs → types}/utils/readableStream.d.ts +1 -0
- package/lib/types/utils/readableStream.d.ts.map +1 -0
- package/lib/{cjs → types}/web/factory.d.ts +2 -1
- package/lib/types/web/factory.d.ts.map +1 -0
- package/lib/{cjs → types}/web/index.d.ts +2 -1
- package/lib/types/web/index.d.ts.map +1 -0
- package/lib/{cjs → types}/web/signer.d.ts +3 -2
- package/lib/types/web/signer.d.ts.map +1 -0
- package/package.json +13 -15
- package/lib/cjs/common/index.d.ts +0 -19
- package/lib/cjs/common/payment.d.ts +0 -41
- package/lib/cjs/common/upload.d.ts +0 -29
- package/lib/cjs/node/factory.d.ts +0 -22
- package/lib/cjs/node/index.d.ts +0 -20
- package/lib/cjs/types/arweave.d.ts +0 -29
- package/lib/cjs/types/index.d.ts +0 -18
- package/lib/cjs/types/index.js +0 -34
- package/lib/cjs/utils/axiosClient.d.ts +0 -23
- package/lib/cjs/utils/errors.d.ts +0 -22
- package/lib/esm/common/factory.d.ts +0 -21
- package/lib/esm/common/http.d.ts +0 -44
- package/lib/esm/common/turbo.d.ts +0 -73
- package/lib/esm/node/signer.d.ts +0 -36
- package/lib/esm/types/index.d.ts +0 -18
- package/lib/esm/types/index.js +0 -18
- package/lib/esm/types/turbo.d.ts +0 -149
- package/lib/esm/utils/base64.d.ts +0 -9
- package/lib/esm/utils/readableStream.d.ts +0 -22
- package/lib/esm/web/factory.d.ts +0 -22
- package/lib/esm/web/index.d.ts +0 -20
- package/lib/esm/web/signer.d.ts +0 -37
- /package/lib/cjs/{types/arweave.js → common/jwk.js} +0 -0
- /package/lib/cjs/{types/turbo.js → types.js} +0 -0
- /package/lib/esm/{types/arweave.js → common/jwk.js} +0 -0
- /package/lib/esm/{types/turbo.js → types.js} +0 -0
@@ -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(
|
10
|
-
|
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,
|
package/lib/cjs/common/http.js
CHANGED
@@ -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
|
-
|
26
|
-
|
27
|
-
|
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
|
-
|
38
|
-
|
39
|
-
|
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;
|
@@ -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");
|
@@ -18,48 +9,36 @@ class TurboUnauthenticatedPaymentService {
|
|
18
9
|
retryConfig,
|
19
10
|
});
|
20
11
|
}
|
21
|
-
getFiatRates() {
|
22
|
-
return
|
23
|
-
|
24
|
-
endpoint: '/rates',
|
25
|
-
});
|
12
|
+
async getFiatRates() {
|
13
|
+
return this.httpService.get({
|
14
|
+
endpoint: '/rates',
|
26
15
|
});
|
27
16
|
}
|
28
|
-
getFiatToAR({ currency, }) {
|
29
|
-
return
|
30
|
-
|
31
|
-
endpoint: `/rates/${currency}`,
|
32
|
-
});
|
17
|
+
async getFiatToAR({ currency, }) {
|
18
|
+
return this.httpService.get({
|
19
|
+
endpoint: `/rates/${currency}`,
|
33
20
|
});
|
34
21
|
}
|
35
|
-
getSupportedCountries() {
|
36
|
-
return
|
37
|
-
|
38
|
-
endpoint: '/countries',
|
39
|
-
});
|
22
|
+
async getSupportedCountries() {
|
23
|
+
return this.httpService.get({
|
24
|
+
endpoint: '/countries',
|
40
25
|
});
|
41
26
|
}
|
42
|
-
getSupportedCurrencies() {
|
43
|
-
return
|
44
|
-
|
45
|
-
endpoint: '/currencies',
|
46
|
-
});
|
27
|
+
async getSupportedCurrencies() {
|
28
|
+
return this.httpService.get({
|
29
|
+
endpoint: '/currencies',
|
47
30
|
});
|
48
31
|
}
|
49
|
-
getUploadCosts({ bytes, }) {
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
return wincCostsForBytes;
|
56
|
-
});
|
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;
|
57
38
|
}
|
58
|
-
getWincForFiat({ amount, currency }) {
|
59
|
-
return
|
60
|
-
|
61
|
-
endpoint: `/price/${currency}/${amount}`,
|
62
|
-
});
|
39
|
+
async getWincForFiat({ amount, currency }) {
|
40
|
+
return this.httpService.get({
|
41
|
+
endpoint: `/price/${currency}/${amount}`,
|
63
42
|
});
|
64
43
|
}
|
65
44
|
}
|
@@ -70,17 +49,15 @@ class TurboAuthenticatedPaymentService extends TurboUnauthenticatedPaymentServic
|
|
70
49
|
super({ url, retryConfig });
|
71
50
|
this.signer = signer;
|
72
51
|
}
|
73
|
-
getBalance() {
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
allowedStatuses: [200, 404],
|
80
|
-
});
|
81
|
-
// 404's don't return a balance, so default to 0
|
82
|
-
return balance.winc ? balance : { winc: '0' };
|
52
|
+
async getBalance() {
|
53
|
+
const headers = await this.signer.generateSignedRequestHeaders();
|
54
|
+
const balance = await this.httpService.get({
|
55
|
+
endpoint: '/balance',
|
56
|
+
headers,
|
57
|
+
allowedStatuses: [200, 404],
|
83
58
|
});
|
59
|
+
// 404's don't return a balance, so default to 0
|
60
|
+
return balance.winc ? balance : { winc: '0' };
|
84
61
|
}
|
85
62
|
}
|
86
63
|
exports.TurboAuthenticatedPaymentService = TurboAuthenticatedPaymentService;
|
package/lib/cjs/common/turbo.js
CHANGED
@@ -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");
|
@@ -20,10 +11,8 @@ class TurboUnauthenticatedClient {
|
|
20
11
|
/**
|
21
12
|
* Returns the supported fiat currency conversion rate for 1AR based on current market prices.
|
22
13
|
*/
|
23
|
-
getFiatToAR({ currency, }) {
|
24
|
-
return
|
25
|
-
return this.paymentService.getFiatToAR({ currency });
|
26
|
-
});
|
14
|
+
async getFiatToAR({ currency, }) {
|
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.
|
@@ -31,52 +20,40 @@ class TurboUnauthenticatedClient {
|
|
31
20
|
* Note: this does not take into account varying adjustments and promotions for different sizes of data. If you want to calculate the total
|
32
21
|
* cost in 'winc' for a given number of bytes, use getUploadCosts.
|
33
22
|
*/
|
34
|
-
getFiatRates() {
|
35
|
-
return
|
36
|
-
return this.paymentService.getFiatRates();
|
37
|
-
});
|
23
|
+
async getFiatRates() {
|
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
|
-
getSupportedCountries() {
|
43
|
-
return
|
44
|
-
return this.paymentService.getSupportedCountries();
|
45
|
-
});
|
29
|
+
async getSupportedCountries() {
|
30
|
+
return this.paymentService.getSupportedCountries();
|
46
31
|
}
|
47
32
|
/**
|
48
33
|
* Returns a list of all supported fiat currencies.
|
49
34
|
*/
|
50
|
-
getSupportedCurrencies() {
|
51
|
-
return
|
52
|
-
return this.paymentService.getSupportedCurrencies();
|
53
|
-
});
|
35
|
+
async getSupportedCurrencies() {
|
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
|
-
getUploadCosts({ bytes, }) {
|
59
|
-
return
|
60
|
-
return this.paymentService.getUploadCosts({ bytes });
|
61
|
-
});
|
41
|
+
async getUploadCosts({ bytes, }) {
|
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
|
68
|
-
return this.paymentService.getWincForFiat({ amount, currency });
|
69
|
-
});
|
47
|
+
async getWincForFiat({ amount, currency, }) {
|
48
|
+
return this.paymentService.getWincForFiat({ amount, currency });
|
70
49
|
}
|
71
50
|
/**
|
72
51
|
* Uploads a signed data item to the Turbo Upload Service.
|
73
52
|
*/
|
74
|
-
uploadSignedDataItem({ dataItemStreamFactory, signal, }) {
|
75
|
-
return
|
76
|
-
|
77
|
-
|
78
|
-
signal,
|
79
|
-
});
|
53
|
+
async uploadSignedDataItem({ dataItemStreamFactory, signal, }) {
|
54
|
+
return this.uploadService.uploadSignedDataItem({
|
55
|
+
dataItemStreamFactory,
|
56
|
+
signal,
|
80
57
|
});
|
81
58
|
}
|
82
59
|
}
|
@@ -88,18 +65,14 @@ class TurboAuthenticatedClient extends TurboUnauthenticatedClient {
|
|
88
65
|
/**
|
89
66
|
* Returns the current balance of the user's wallet in 'winc'.
|
90
67
|
*/
|
91
|
-
getBalance() {
|
92
|
-
return
|
93
|
-
return this.paymentService.getBalance();
|
94
|
-
});
|
68
|
+
async getBalance() {
|
69
|
+
return this.paymentService.getBalance();
|
95
70
|
}
|
96
71
|
/**
|
97
72
|
* Signs and uploads raw data to the Turbo Upload Service.
|
98
73
|
*/
|
99
|
-
uploadFile({ fileStreamFactory, signal, }) {
|
100
|
-
return
|
101
|
-
return this.uploadService.uploadFile({ fileStreamFactory, signal });
|
102
|
-
});
|
74
|
+
async uploadFile({ fileStreamFactory, signal, }) {
|
75
|
+
return this.uploadService.uploadFile({ fileStreamFactory, signal });
|
103
76
|
}
|
104
77
|
}
|
105
78
|
exports.TurboAuthenticatedClient = TurboAuthenticatedClient;
|
package/lib/cjs/common/upload.js
CHANGED
@@ -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,15 @@ class TurboUnauthenticatedUploadService {
|
|
18
9
|
retryConfig,
|
19
10
|
});
|
20
11
|
}
|
21
|
-
uploadSignedDataItem({ dataItemStreamFactory, signal, }) {
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
},
|
31
|
-
});
|
12
|
+
async uploadSignedDataItem({ dataItemStreamFactory, signal, }) {
|
13
|
+
// TODO: add p-limit constraint or replace with separate upload class
|
14
|
+
return this.httpService.post({
|
15
|
+
endpoint: `/tx`,
|
16
|
+
signal,
|
17
|
+
data: dataItemStreamFactory(),
|
18
|
+
headers: {
|
19
|
+
'content-type': 'application/octet-stream',
|
20
|
+
},
|
32
21
|
});
|
33
22
|
}
|
34
23
|
}
|
@@ -39,20 +28,18 @@ class TurboAuthenticatedUploadService extends TurboUnauthenticatedUploadService
|
|
39
28
|
super({ url, retryConfig });
|
40
29
|
this.signer = signer;
|
41
30
|
}
|
42
|
-
uploadFile({ fileStreamFactory, signal, }) {
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
},
|
55
|
-
});
|
31
|
+
async uploadFile({ fileStreamFactory, signal, }) {
|
32
|
+
const signedDataItem = await this.signer.signDataItem({
|
33
|
+
fileStreamFactory,
|
34
|
+
});
|
35
|
+
// TODO: add p-limit constraint or replace with separate upload class
|
36
|
+
return this.httpService.post({
|
37
|
+
endpoint: `/tx`,
|
38
|
+
signal,
|
39
|
+
data: signedDataItem,
|
40
|
+
headers: {
|
41
|
+
'content-type': 'application/octet-stream',
|
42
|
+
},
|
56
43
|
});
|
57
44
|
}
|
58
45
|
}
|
package/lib/cjs/node/factory.js
CHANGED
@@ -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(
|
27
|
-
|
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,
|
package/lib/cjs/node/index.js
CHANGED
@@ -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);
|
package/lib/cjs/node/signer.js
CHANGED
@@ -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
|
};
|
@@ -45,17 +36,15 @@ class TurboNodeArweaveSigner {
|
|
45
36
|
return (0, arbundles_1.streamSigner)(stream1, stream2, this.signer);
|
46
37
|
}
|
47
38
|
// 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
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
};
|
58
|
-
});
|
39
|
+
async generateSignedRequestHeaders() {
|
40
|
+
const nonce = (0, node_crypto_1.randomBytes)(16).toString('hex');
|
41
|
+
const buffer = Buffer.from(nonce);
|
42
|
+
const signature = await index_js_1.default.crypto.sign(this.privateKey, buffer);
|
43
|
+
return {
|
44
|
+
'x-public-key': this.privateKey.n,
|
45
|
+
'x-nonce': nonce,
|
46
|
+
'x-signature': (0, base64_js_1.toB64Url)(Buffer.from(signature)),
|
47
|
+
};
|
59
48
|
}
|
60
49
|
}
|
61
50
|
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 =
|
47
|
-
const
|
46
|
+
const axios_1 = __importStar(require("axios"));
|
47
|
+
const axios_retry_1 = __importDefault(require("axios-retry"));
|
48
48
|
const createAxiosInstance = ({ axiosConfig = {}, retryConfig = {
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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(
|
57
|
-
|
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,17 @@
|
|
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
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
chunks.push(value);
|
23
|
-
}
|
4
|
+
async function readableStreamToBuffer({ stream, }) {
|
5
|
+
const reader = stream.getReader();
|
6
|
+
const chunks = [];
|
7
|
+
let done = false;
|
8
|
+
while (!done) {
|
9
|
+
const { done: streamDone, value } = await reader.read();
|
10
|
+
done = streamDone;
|
11
|
+
if (!done) {
|
12
|
+
chunks.push(value);
|
24
13
|
}
|
25
|
-
|
26
|
-
|
14
|
+
}
|
15
|
+
return Buffer.concat(chunks);
|
27
16
|
}
|
28
17
|
exports.readableStreamToBuffer = readableStreamToBuffer;
|
package/lib/cjs/web/factory.js
CHANGED
@@ -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.TurboWebArweaveSigner({ privateKey });
|
26
|
-
const paymentService = new index_js_1.TurboAuthenticatedPaymentService(
|
27
|
-
|
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,
|
package/lib/cjs/web/index.js
CHANGED
@@ -33,4 +33,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
33
33
|
__exportStar(require("./factory.js"), exports);
|
34
34
|
__exportStar(require("./signer.js"), exports);
|
35
35
|
__exportStar(require("../common/index.js"), exports);
|
36
|
-
__exportStar(require("../types
|
36
|
+
__exportStar(require("../types.js"), exports);
|
package/lib/cjs/web/signer.js
CHANGED
@@ -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,31 +30,28 @@ class TurboWebArweaveSigner {
|
|
39
30
|
this.privateKey = privateKey;
|
40
31
|
this.signer = new arbundles_1.ArweaveSigner(this.privateKey);
|
41
32
|
}
|
42
|
-
signDataItem({ fileStreamFactory, }) {
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
// TODO: add payload size to get performance improvements
|
48
|
-
});
|
49
|
-
const signedDataItem = (0, arbundles_1.createData)(buffer, this.signer);
|
50
|
-
yield signedDataItem.sign(this.signer);
|
51
|
-
return signedDataItem.getRaw();
|
33
|
+
async signDataItem({ fileStreamFactory, }) {
|
34
|
+
// TODO: converts the readable stream to a buffer bc incrementally signing ReadableStreams is not trivial
|
35
|
+
const buffer = await (0, readableStream_js_1.readableStreamToBuffer)({
|
36
|
+
stream: fileStreamFactory(),
|
37
|
+
// TODO: add payload size to get performance improvements
|
52
38
|
});
|
39
|
+
const signedDataItem = (0, arbundles_1.createData)(buffer, this.signer);
|
40
|
+
await signedDataItem.sign(this.signer);
|
41
|
+
return signedDataItem.getRaw();
|
53
42
|
}
|
54
43
|
// 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
|
55
|
-
generateSignedRequestHeaders() {
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
});
|
44
|
+
async generateSignedRequestHeaders() {
|
45
|
+
// a bit hacky - but arweave exports cause issues in tests vs. browser
|
46
|
+
const arweave = arweave_1.default.default ?? arweave_1.default;
|
47
|
+
const nonce = (0, node_crypto_1.randomBytes)(16).toString('hex');
|
48
|
+
const buffer = Buffer.from(nonce);
|
49
|
+
const signature = await arweave.crypto.sign(this.privateKey, buffer, {});
|
50
|
+
return {
|
51
|
+
'x-public-key': this.privateKey.n,
|
52
|
+
'x-nonce': nonce,
|
53
|
+
'x-signature': (0, base64_js_1.toB64Url)(Buffer.from(signature)),
|
54
|
+
};
|
67
55
|
}
|
68
56
|
}
|
69
57
|
exports.TurboWebArweaveSigner = TurboWebArweaveSigner;
|
@@ -3,8 +3,12 @@ import { TurboUnauthenticatedClient } from './turbo.js';
|
|
3
3
|
import { TurboUnauthenticatedUploadService } from './upload.js';
|
4
4
|
export class TurboBaseFactory {
|
5
5
|
static unauthenticated({ paymentServiceConfig = {}, uploadServiceConfig = {}, } = {}) {
|
6
|
-
const paymentService = new TurboUnauthenticatedPaymentService(
|
7
|
-
|
6
|
+
const paymentService = new TurboUnauthenticatedPaymentService({
|
7
|
+
...paymentServiceConfig,
|
8
|
+
});
|
9
|
+
const uploadService = new TurboUnauthenticatedUploadService({
|
10
|
+
...uploadServiceConfig,
|
11
|
+
});
|
8
12
|
return new TurboUnauthenticatedClient({
|
9
13
|
uploadService,
|
10
14
|
paymentService,
|