Skip to content

Commit

Permalink
chore: update file imports now that we are using node next
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Aug 15, 2023
1 parent 9d3b79c commit d8d6fb1
Show file tree
Hide file tree
Showing 22 changed files with 252 additions and 145 deletions.
44 changes: 28 additions & 16 deletions src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import {
UploadDataItemResponse,
UploadDataItemResultMap,
UploadDataItemsResult,
} from "../types/turboTypes";
} from "../types/turboTypes.js";
import { JWKInterface } from "arbundles";
import winston from "winston";
import { createAxiosInstance } from "../utils/axiosClient";
import { ByteCount } from "../types/byteCount";
import { Winc, W } from "../types/winc";
import { signedRequestHeadersFromJwk } from "../utils/signData";
import { Payment } from "../types/payment";
import { TopUpQuote } from "../types/types";
import { createAxiosInstance } from "../utils/axiosClient.js";
import { ByteCount } from "../types/byteCount.js";
import { Winc, W } from "../types/winc.js";
import { signedRequestHeadersFromJwk } from "../utils/signData.js";
import { Payment } from "../types/payment.js";
import { TopUpQuote } from "../types/index.js";
import { PassThrough } from "stream";
import { jwkToPublicArweaveAddress } from "../utils/base64";
import { jwkToPublicArweaveAddress } from "../utils/base64.js";

export abstract class BaseTurboClient implements TurboService {
export abstract class TurboClient implements TurboService {
readonly jwk: JWKInterface | undefined;
readonly paymentService: AxiosInstance;
readonly uploadService: AxiosInstance;
Expand All @@ -38,26 +38,38 @@ export abstract class BaseTurboClient implements TurboService {
constructor({
paymentUrl = "https://payment.ardrive.dev",
uploadUrl = "https://upload.ardrive.dev",
retries = 3,
retryConfig,
jwk,
}: TurboSettings) {
this.paymentService = createAxiosInstance({
config: {
logger: this.logger,
axiosConfig: {
baseURL: `${paymentUrl}/v1`,
validateStatus: () => true,
},
retries,
retryConfig,
});
this.uploadService = createAxiosInstance({
config: {
logger: this.logger,
axiosConfig: {
baseURL: `${uploadUrl}/v1`,
validateStatus: () => true,
},
retries,
retryConfig,
});
this.jwk = jwk;
}

async getRates(): Promise<Record<string, number>> {
const { status, statusText, data } = await this.paymentService.get(
`/rates`,
);

if (status !== 200) {
throw new Error(statusText);
}

return data;
}

async getWincEstimationForByteCount(byteCount: ByteCount): Promise<Winc> {
const { status, statusText, data } = await this.paymentService.get(
`/price/bytes/${byteCount}`,
Expand Down
23 changes: 23 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import TurboNodeClient from "./node/index.js";
import { TurboSettings } from "./types/turboTypes.js";
import TurboWebClient from "./web/index.js";

export class TurboFactory {
static init(settings: TurboSettings = {}) {
if (
typeof window !== "undefined" &&
typeof window.document !== "undefined"
) {
return new TurboWebClient(settings);
} else if (
typeof global !== "undefined" &&
global.process &&
global.process.versions &&
global.process.versions.node
) {
return new TurboNodeClient(settings);
} else {
throw new Error("Unknown environment.");
}
}
}
26 changes: 16 additions & 10 deletions src/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { ArweaveSigner, streamSigner } from "arbundles";
import { BaseTurboClient } from "../common";
import { TurboFileUpload, TurboSettings } from "../types/turboTypes";
import { TurboClient } from "../common/index.js";
import {
TurboDataItemStream,
TurboFileUpload,
TurboSettings,
} from "../types/turboTypes.js";
import internal from "stream";
import fs from "fs";

class NodeTurboClient extends BaseTurboClient {
class TurboNodeClient extends TurboClient {
constructor(settings: TurboSettings) {
super(settings);
}
Expand All @@ -21,13 +25,15 @@ class NodeTurboClient extends BaseTurboClient {
// concurrently upload files
// TODO: use p-limit to avoid overwhelming resources
const signedDataItemStreams = files.map(async (file: TurboFileUpload) => {
const [fileStream1, fileStream2] = [
fs.createReadStream(file.filePath),
fs.createReadStream(file.filePath),
];
let dataItemStream1: TurboDataItemStream;
let dataItemSTream2: TurboDataItemStream;

dataItemStream1 = fs.createReadStream(file.filePath);
dataItemSTream2 = fs.createReadStream(file.filePath);

const signedDataItem = await streamSigner(
fileStream1,
fileStream2,
dataItemStream1,
dataItemSTream2,
signer,
file.options,
);
Expand All @@ -38,4 +44,4 @@ class NodeTurboClient extends BaseTurboClient {
}
}

export { NodeTurboClient as TurboClient };
export default TurboNodeClient;
2 changes: 1 addition & 1 deletion src/types/byteCount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { PositiveFiniteInteger } from "./positiveFiniteInteger";
import { PositiveFiniteInteger } from "./positiveFiniteInteger.js";

export function ByteCount(value: number): PositiveFiniteInteger {
return new PositiveFiniteInteger(value);
Expand Down
2 changes: 1 addition & 1 deletion src/types/credits.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from "chai";

import { AR } from "./credits";
import { AR } from "./credits.js";

describe("AR class", () => {
describe("from function", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/types/credits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
import { BigNumber } from "bignumber.js";

import { W, Winc } from "./winc";
import { W, Winc } from "./winc.js";

export class AR {
constructor(readonly winc: Winc) {}
Expand Down
10 changes: 5 additions & 5 deletions src/types/types.ts → src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
export * from "./positiveFiniteInteger";
export * from "./equatable";
export * from "./credits";
export * from "./winc";
export * from "./byteCount";
export * from "./positiveFiniteInteger.js";
export * from "./equatable.js";
export * from "./credits.js";
export * from "./winc.js";
export * from "./byteCount.js";

export type Base64String = string;
export type PublicArweaveAddress = Base64String;
Expand Down
4 changes: 2 additions & 2 deletions src/types/payment.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from "chai";
import { InvalidPaymentAmount } from "../utils/errors";
import { Payment } from "./payment";
import { InvalidPaymentAmount } from "../utils/errors.js";
import { Payment } from "./payment.js";

describe("Payment class", () => {
describe("constructor", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/types/payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import {
InvalidPaymentAmount,
PaymentAmountTooLarge,
PaymentAmountTooSmall,
} from "../utils/errors";
import { PaymentAmount, CurrencyType, CurrencyLimitations } from "./types";
} from "../utils/errors.js";
import { PaymentAmount, CurrencyType, CurrencyLimitations } from "./index.js";

interface PaymentConstructorParams {
amount: PaymentAmount;
Expand Down
2 changes: 1 addition & 1 deletion src/types/positiveFiniteInteger.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from "chai";

import { PositiveFiniteInteger } from "./positiveFiniteInteger";
import { PositiveFiniteInteger } from "./positiveFiniteInteger.js";

describe("PositiveFiniteInteger class", () => {
describe("constructor", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/types/positiveFiniteInteger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Equatable } from "./equatable";
import { Equatable } from "./equatable.js";

export class PositiveFiniteInteger implements Equatable<PositiveFiniteInteger> {
constructor(private readonly positiveFiniteInteger: number) {
Expand Down
25 changes: 17 additions & 8 deletions src/types/turboTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { TransactionInterface } from "arweave/node/lib/transaction";
import { TransactionInterface } from "arweave/node/lib/transaction.js";
import {
ByteCount,
TopUpQuote,
TransactionId,
UserAddress,
Winc,
} from "./types";
} from "./index.js";
import { JWKInterface } from "arbundles";
import { Payment } from "./payment";
import { Payment } from "./payment.js";
import { Readable } from "stream";
import { RetryConfig } from "retry-axios";

export interface PaymentIntent {
id: string;
Expand Down Expand Up @@ -65,19 +67,26 @@ export interface TurboUploadService {
export interface TurboService extends TurboPaymentService, TurboUploadService {}

export type TurboFilePath = {
filePath: string;
filePath: URL;
};

export type TurboRawData = {
data: ReadableStream | Blob;
export type TurboDataItem = {
data: TurboRawDataItem;
stream: TurboDataItemStream;
};

export type TurboDataItemStream = Readable;
export type TurboRawDataItem = Blob | Uint8Array;

export type TurboUploadOptions = {
options: Partial<TransactionInterface>;
};

export type TurboFileUpload = TurboFilePath & Partial<TurboUploadOptions>;
export type TurboBlobUpload = TurboRawData & Partial<TurboUploadOptions>;
export type TurboStreamUpload = Pick<TurboDataItem, "stream"> &
Partial<TurboUploadOptions>;
export type TurboBlobUpload = Pick<TurboDataItem, "data"> &
Partial<TurboUploadOptions>;

export type TurboRequestHeaders = {
"x-public-key": string;
Expand Down Expand Up @@ -110,6 +119,6 @@ export type AuthTurboSettings = {
export type TurboSettings = Partial<{
paymentUrl: string;
uploadUrl: string;
retries: number;
retryConfig?: RetryConfig;
}> &
Partial<AuthTurboSettings>;
2 changes: 1 addition & 1 deletion src/types/winc.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from "chai";

import { Winc } from "./winc";
import { Winc } from "./winc.js";

describe("Winc class", () => {
describe("constructor", () => {
Expand Down
41 changes: 25 additions & 16 deletions src/utils/axiosClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,34 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import axios, { AxiosInstance, AxiosRequestConfig } from "axios";
import axiosRetry from "axios-retry";
import * as rax from "retry-axios";
import winston from "winston";

export interface CreateAxiosInstanceParams {
config: AxiosRequestConfig;
retries: number;
retryDelay: (retryNumber: number) => number;
export interface AxiosInstanceParameters {
axiosConfig?: Omit<AxiosRequestConfig, "validateStatus">;
retryConfig?: rax.RetryConfig;
logger: winston.Logger;
}

export const createAxiosInstance = ({
config = {},
retries = 3,
retryDelay = axiosRetry.exponentialDelay,
}: Partial<CreateAxiosInstanceParams>): AxiosInstance => {
const axiosInstance = axios.create(config);
if (retries > 0) {
axiosRetry(axiosInstance, {
retries,
retryDelay,
});
}
logger,
axiosConfig = {},
retryConfig = {
backoffType: "exponential",
retry: 3,
onRetryAttempt: (err) => {
const cfg = rax.getConfig(err);
logger.debug(`Retry attempt #${cfg?.currentRetryAttempt}`);
},
},
}: AxiosInstanceParameters): AxiosInstance => {
const axiosInstance = axios.create({
...axiosConfig,
validateStatus: () => true, // don't throw on non-200 status codes
});
axiosInstance.defaults.raxConfig = {
instance: axiosInstance,
...retryConfig,
};
return axiosInstance;
};
4 changes: 2 additions & 2 deletions src/utils/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { createHash } from "crypto";
import { Base64String, PublicArweaveAddress } from "../types/types";
import { JWKInterface } from "./jwkTypes";
import { Base64String, PublicArweaveAddress } from "../types/index.js";
import { JWKInterface } from "./jwkTypes.js";
import Arweave from "arweave";

export function jwkToPublicArweaveAddress(
Expand Down
2 changes: 2 additions & 0 deletions src/utils/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const isBrowser = () =>
typeof window !== "undefined" && typeof window.document !== "undefined";
2 changes: 1 addition & 1 deletion src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { UserAddress, CurrencyType, PaymentAmount } from "../types/types";
import { UserAddress, CurrencyType, PaymentAmount } from "../types/index.js";

export class UserNotFoundWarning extends Error {
constructor(userAddress: UserAddress) {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/signData.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from "chai";
import { toB64Url } from "./base64";
import { signData } from "./signData";
import { toB64Url } from "./base64.js";
import { signData } from "./signData.js";
import Arweave from "arweave";

describe("signData", () => {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/signData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import { JWKInterface } from "arbundles";
import { Buffer } from "buffer";
import { randomBytes } from "crypto";
import { toB64Url } from "./base64";
import { toB64Url } from "./base64.js";
import Arweave from "arweave";
import { stringToBuffer } from "arweave/node/lib/utils";
import { TurboRequestHeaders } from "../types/turboTypes";
import { stringToBuffer } from "arweave/node/lib/utils.js";
import { TurboRequestHeaders } from "../types/turboTypes.js";

export async function signData(
jwk: JWKInterface,
Expand Down
Loading

0 comments on commit d8d6fb1

Please sign in to comment.