@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.
- 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 +15 -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
package/lib/esm/common/http.js
CHANGED
@@ -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
|
-
|
23
|
-
|
24
|
-
|
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
|
-
|
35
|
-
|
36
|
-
|
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
|
20
|
-
|
21
|
-
endpoint: '/rates',
|
22
|
-
});
|
9
|
+
async getFiatRates() {
|
10
|
+
return this.httpService.get({
|
11
|
+
endpoint: '/rates',
|
23
12
|
});
|
24
13
|
}
|
25
|
-
getFiatToAR({ currency, }) {
|
26
|
-
return
|
27
|
-
|
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
|
34
|
-
|
35
|
-
endpoint: '/countries',
|
36
|
-
});
|
19
|
+
async getSupportedCountries() {
|
20
|
+
return this.httpService.get({
|
21
|
+
endpoint: '/countries',
|
37
22
|
});
|
38
23
|
}
|
39
|
-
getSupportedCurrencies() {
|
40
|
-
return
|
41
|
-
|
42
|
-
endpoint: '/currencies',
|
43
|
-
});
|
24
|
+
async getSupportedCurrencies() {
|
25
|
+
return this.httpService.get({
|
26
|
+
endpoint: '/currencies',
|
44
27
|
});
|
45
28
|
}
|
46
|
-
getUploadCosts({ bytes, }) {
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
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
|
57
|
-
|
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
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
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
|
}
|
package/lib/esm/common/turbo.js
CHANGED
@@ -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
|
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
|
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
|
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
|
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
|
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
|
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
|
73
|
-
|
74
|
-
|
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
|
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
|
97
|
-
return this.uploadService.uploadFile({ fileStreamFactory, signal });
|
98
|
-
});
|
70
|
+
async uploadFile({ fileStreamFactory, signal, }) {
|
71
|
+
return this.uploadService.uploadFile({ fileStreamFactory, signal });
|
99
72
|
}
|
100
73
|
}
|
package/lib/esm/common/upload.js
CHANGED
@@ -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
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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
|
}
|
package/lib/esm/node/factory.js
CHANGED
@@ -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(
|
24
|
-
|
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,
|
package/lib/esm/node/index.js
CHANGED
package/lib/esm/node/signer.js
CHANGED
@@ -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
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
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
|
17
|
+
import axios, { CanceledError } from 'axios';
|
18
|
+
import axiosRetry from 'axios-retry';
|
19
19
|
export const createAxiosInstance = ({ axiosConfig = {}, retryConfig = {
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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(
|
28
|
-
|
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
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
-
|
23
|
-
|
11
|
+
}
|
12
|
+
return Buffer.concat(chunks);
|
24
13
|
}
|
package/lib/esm/web/factory.js
CHANGED
@@ -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(
|
24
|
-
|
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,
|
package/lib/esm/web/index.js
CHANGED
package/lib/esm/web/signer.js
CHANGED
@@ -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
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
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
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
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
|
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 {
|
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
|
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?:
|
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"}
|
@@ -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"}
|