Skip to content

Commit

Permalink
fix(tsconfig, names): reverted tsconfig to nodenext resolution, chang…
Browse files Browse the repository at this point in the history
…ed naming convention on provider, removed extraeneous error classes, rolled back axios-retry to match our tsconfig settings
  • Loading branch information
atticusofsparta committed Feb 14, 2024
1 parent de5f108 commit d412d44
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 81 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@ar-io/node-sdk-client",
"name": "@ar-io/sdk",
"version": "0.0.1",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down Expand Up @@ -96,8 +96,8 @@
"repository": "https://github.com/ar-io/ar-io-sdk.git",
"dependencies": {
"arweave": "^1.14.4",
"axios": "^1.6.7",
"axios-retry": "^4.0.0",
"axios": "1.4.0",
"axios-retry": "3.7.0",
"warp-contracts": "^1.4.34",
"winston": "^3.11.0"
}
Expand Down
36 changes: 11 additions & 25 deletions src/common/ArIo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,29 @@
* 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 Arweave from 'arweave';
import axios, { AxiosInstance } from 'axios';
import axiosRetry from 'axios-retry';

import { RESPONSE_RETRY_CODES } from '../constants.js';
import { ContractStateProvider } from '../types.js';
import { BaseError } from './error.js';
import { ArIoWinstonLogger } from './logger.js';
import { DefaultLogger } from './logger.js';

export class ArIoError extends BaseError {
constructor(message: string) {
super(message);
this.name = 'ArIoError';
}
}

const RESPONSE_RETRY_CODES = new Set([429, 503]);

export class ArIo implements ContractStateProvider {
_arweave: Arweave;
_contractStateProvider: ContractStateProvider;
export class ArIO implements ContractStateProvider {
private contractStateProvider: ContractStateProvider;
http: AxiosInstance;
logger: ArIoWinstonLogger;
logger: DefaultLogger;

constructor({
arweave,
contractStateProvider,
logger = new ArIoWinstonLogger({
logger = new DefaultLogger({
level: 'debug',
logFormat: 'simple',
}),
}: {
arweave?: Arweave;
contractStateProvider: ContractStateProvider;
logger?: ArIoWinstonLogger;
logger?: DefaultLogger;
}) {
this._arweave = arweave ?? Arweave.init({}); // use default arweave instance if not provided
this._contractStateProvider = contractStateProvider;
this.contractStateProvider = contractStateProvider;
this.logger = logger;
this.http = axiosRetry(axios, {
retries: 3,
Expand All @@ -66,11 +52,11 @@ export class ArIo implements ContractStateProvider {

/**
* Fetches the state of a contract.
* @param {string} contractId - The Arweave transaction id of the contract.
* @param {string} contractTxId - The Arweave transaction id of the contract.
*/
async getContractState<ContractState>(
contractId: string,
contractTxId: string,
): Promise<ContractState> {
return await this._contractStateProvider.getContractState(contractId);
return this.contractStateProvider.getContractState(contractTxId);
}
}
41 changes: 18 additions & 23 deletions src/common/ContractStateProviders/ArNSRemoteCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,32 @@
import axios, { AxiosInstance } from 'axios';
import axiosRetry from 'axios-retry';

import { RESPONSE_RETRY_CODES } from '../../constants.js';
import { ContractStateProvider } from '../../types.js';
import { validateArweaveId } from '../../utils/index.js';
import { BadRequest, BaseError } from '../error.js';
import { ArIoWinstonLogger } from '../logger.js';
import { BadRequest } from '../error.js';
import { DefaultLogger } from '../logger.js';

export class ArNSRemoteCacheError extends BaseError {
constructor(message: string) {
super(message);
this.name = 'ArNSRemoteCacheError';
}
}

const RESPONSE_RETRY_CODES = new Set([429, 503]);
export class ArNSRemoteCache implements ContractStateProvider {
protected logger: ArIoWinstonLogger;
protected logger: DefaultLogger;
http: AxiosInstance;
constructor({
url = 'api.arns.app',
logger = new ArIoWinstonLogger({
protocol = 'https',
logger = new DefaultLogger({
level: 'debug',
logFormat: 'simple',
}),
version = 'v1',
}: {
protocol?: 'http' | 'https' | 'ws';
url?: string;
logger?: ArIoWinstonLogger;
logger?: DefaultLogger;
version?: string;
}) {
this.logger = logger;
const arnsServiceClient = axios.create({
baseURL: `${url}/${version}`,
baseURL: `${protocol}://${url}/${version}`,
});
this.http = axiosRetry(arnsServiceClient, {
retries: 3,
Expand All @@ -62,18 +57,17 @@ export class ArNSRemoteCache implements ContractStateProvider {
}

async getContractState<ContractState>(
contractId: string,
contractTxId: string,
): Promise<ContractState> {
if (!validateArweaveId(contractId)) {
throw new BadRequest(`Invalid contract id: ${contractId}`);
if (!validateArweaveId(contractTxId)) {
throw new BadRequest(`Invalid contract id: ${contractTxId}`);
}
const contractLogger = this.logger.logger.child({ contractId });
contractLogger.debug(`Fetching contract state`);
this.logger.debug(`Fetching contract state`);

const response = await this.http<ContractState>(
`/contract/${contractId}`,
`/contract/${contractTxId}`,
).catch((error) => {
contractLogger.debug(`Failed to fetch contract state: ${error}`);
this.logger.debug(`Failed to fetch contract state: ${error}`);
return error;
});

Expand All @@ -82,11 +76,12 @@ export class ArNSRemoteCache implements ContractStateProvider {
`Failed to fetch contract state. ${response?.status} ${response?.statusText()}`,
);
}
const result = await response.json();

contractLogger.debug(
this.logger.debug(
`Fetched contract state. Size: ${response?.headers?.get('content-length')} bytes.`,
);

return response.json();
return result;
}
}
2 changes: 0 additions & 2 deletions src/common/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ export class BaseError extends Error {
export class NotFound extends BaseError {
constructor(message: string) {
super(message);
this.name = 'NotFound';
}
}

export class BadRequest extends BaseError {
constructor(message: string) {
super(message);
this.name = 'BadRequest';
}
}
2 changes: 1 addition & 1 deletion src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
* 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 './ArIo.js';
export * from './ArIO.js';
export * from './ContractStateProviders/index.js';
4 changes: 2 additions & 2 deletions src/common/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
*/
import winston, { createLogger, format, transports } from 'winston';

import { ArIoLogger } from '../types.js';
import { Logger } from '../types.js';
import { version } from '../version.js';

export class ArIoWinstonLogger implements ArIoLogger {
export class DefaultLogger implements Logger {
logger: winston.Logger;
constructor({
level = 'info',
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
*/

export const ARWEAVE_TX_REGEX = new RegExp('^[a-zA-Z0-9_-]{43}$');
export const RESPONSE_RETRY_CODES = new Set([429, 503]);
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface ContractStateProvider {
getContractState<T>(contractId: string): Promise<T>;
}
/* eslint-disable @typescript-eslint/no-explicit-any */
export interface ArIoLogger {
export interface Logger {
setLogLevel: (level: string) => void;
setLogFormat: (logFormat: string) => void;
info: (message: string, ...args: any[]) => void;
Expand Down
6 changes: 3 additions & 3 deletions tests/ArIo.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ArIo } from '../src/common/ArIo.js';
import { ArIO } from '../src/common/ArIO.js';
import { ArNSRemoteCache } from '../src/common/ContractStateProviders/ArNSRemoteCache.js';

describe('ArIO Client', () => {
const remoteCacheProvider = new ArNSRemoteCache({});
const arioClient = new ArIo({
const arioClient = new ArIO({
contractStateProvider: remoteCacheProvider,
});

it('should create an ArIo client', () => {
expect(arioClient).toBeInstanceOf(ArIo);
expect(arioClient).toBeInstanceOf(ArIO);
});
});
16 changes: 8 additions & 8 deletions tests/ArNSRemoteCache.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import { ArIo } from '../src/common/ArIo.js';
import { ArIO } from '../src/common/ArIO.js';
import { ArNSRemoteCache } from '../src/common/ContractStateProviders/ArNSRemoteCache.js';
import { BadRequest } from '../src/common/error.js';

describe('ArIO Client', () => {
const remoteCacheProvider = new ArNSRemoteCache({});
const arioClient = new ArIo({
const arioClient = new ArIO({
contractStateProvider: remoteCacheProvider,
});

it('should call remote state provider', async () => {
const remoteProvider = new ArNSRemoteCache({});
const client = new ArIo({
const client = new ArIO({
contractStateProvider: remoteProvider,
});

const stubGetContractState = jest.fn();
remoteProvider.getContractState = stubGetContractState;
const contractId = ''.padEnd(43, 'a');
await client.getContractState(contractId);
expect(stubGetContractState).toHaveBeenCalledWith(contractId);
const contractTxId = ''.padEnd(43, 'a');
await client.getContractState(contractTxId);
expect(stubGetContractState).toHaveBeenCalledWith(contractTxId);
});

it('should call remote state provider and throw on bad contract id', async () => {
const contractId = ''.padEnd(42, 'a');
const contractTxId = ''.padEnd(42, 'a');
const result = await arioClient
.getContractState(contractId)
.getContractState(contractTxId)
.catch((e) => e);

expect(result).toBeInstanceOf(BadRequest);
Expand Down
5 changes: 2 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
"outDir": "./lib/esm",
"listEmittedFiles": false,
"listFiles": false,
"moduleResolution": "bundler",
"module": "esnext",
"moduleResolution": "nodenext",
"module": "nodenext",
"alwaysStrict": true,
"types": ["node", "jest"],
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"noEmit": false,
/* Additional Checks */
"pretty": true,
"noUnusedLocals": true,
Expand Down
33 changes: 23 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,13 @@
dependencies:
"@babel/helper-plugin-utils" "^7.22.5"

"@babel/runtime@^7.15.4":
version "7.23.9"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.23.9.tgz#47791a15e4603bb5f905bc0753801cf21d6345f7"
integrity sha512-0CX6F+BI2s9dkUqr08KFrAIZgNFj75rdBU/DjCyYLIaV/quFjkk6T+EJ2LkZHyZTbEV4L5p97mNkUsHl2wLFAw==
dependencies:
regenerator-runtime "^0.14.0"

"@babel/template@^7.22.15", "@babel/template@^7.23.9", "@babel/template@^7.3.3":
version "7.23.9"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.23.9.tgz#f881d0487cba2828d3259dcb9ef5005a9731011a"
Expand Down Expand Up @@ -2232,19 +2239,20 @@ available-typed-arrays@^1.0.5, available-typed-arrays@^1.0.6:
resolved "https://registry.yarnpkg.com/available-typed-arrays/-/available-typed-arrays-1.0.6.tgz#ac812d8ce5a6b976d738e1c45f08d0b00bc7d725"
integrity sha512-j1QzY8iPNPG4o4xmO3ptzpRxTciqD3MgEHtifP/YnJpIo58Xu+ne4BejlbkuaLfXn/nz6HFiw29bLpj2PNMdGg==

axios-retry@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/axios-retry/-/axios-retry-4.0.0.tgz#d5cb8ea1db18e05ce6f08aa5fe8b2663bba48e60"
integrity sha512-F6P4HVGITD/v4z9Lw2mIA24IabTajvpDZmKa6zq/gGwn57wN5j1P3uWrAV0+diqnW6kTM2fTqmWNfgYWGmMuiA==
axios-retry@3.7.0:
version "3.7.0"
resolved "https://registry.yarnpkg.com/axios-retry/-/axios-retry-3.7.0.tgz#d5007755d257b97e08d846089976f838b9db9ef9"
integrity sha512-ZTnCkJbRtfScvwiRnoVskFAfvU0UG3xNcsjwTR0mawSbIJoothxn67gKsMaNAFHRXJ1RmuLhmZBzvyXi3+9WyQ==
dependencies:
"@babel/runtime" "^7.15.4"
is-retry-allowed "^2.2.0"

axios@^1.6.7:
version "1.6.7"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.6.7.tgz#7b48c2e27c96f9c68a2f8f31e2ab19f59b06b0a7"
integrity sha512-/hDJGff6/c7u0hDkvkGxR/oy6CbCs8ziCsC7SqmhjfozqiJGc8Z11wrv9z9lYfY4K8l+H9TpjcMDX0xOZmx+RA==
axios@1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.4.0.tgz#38a7bf1224cd308de271146038b551d725f0be1f"
integrity sha512-S4XCWMEmzvo64T9GfvQDOXgYRDJ/wsSZc7Jvdgx5u1sd0JwsuPLqb3SYmusag+edF6ziyMensPVqLTSc1PiSEA==
dependencies:
follow-redirects "^1.15.4"
follow-redirects "^1.15.0"
form-data "^4.0.0"
proxy-from-env "^1.1.0"

Expand Down Expand Up @@ -3977,7 +3985,7 @@ fn.name@1.x.x:
resolved "https://registry.yarnpkg.com/fn.name/-/fn.name-1.1.0.tgz#26cad8017967aea8731bc42961d04a3d5988accc"
integrity sha512-GRnmB5gPyJpAhTQdSZTSp9uaPSvl09KoYcMQtsB9rQoOmzs9dH6ffeccH+Z+cv6P68Hu5bC6JjRh4Ah/mHSNRw==

follow-redirects@^1.0.0, follow-redirects@^1.15.4:
follow-redirects@^1.0.0, follow-redirects@^1.15.0:
version "1.15.5"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.5.tgz#54d4d6d062c0fa7d9d17feb008461550e3ba8020"
integrity sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw==
Expand Down Expand Up @@ -7324,6 +7332,11 @@ redeyed@~2.1.0:
dependencies:
esprima "~4.0.0"

regenerator-runtime@^0.14.0:
version "0.14.1"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.14.1.tgz#356ade10263f685dda125100cd862c1db895327f"
integrity sha512-dYnhHh0nJoMfnkZs6GmmhFknAGRrLznOu5nc9ML+EJxGvrx6H7teuevqVqCuPcPK//3eDrrjQhehXVx9cnkGdw==

regexp.prototype.flags@^1.5.1:
version "1.5.2"
resolved "https://registry.yarnpkg.com/regexp.prototype.flags/-/regexp.prototype.flags-1.5.2.tgz#138f644a3350f981a858c44f6bb1a61ff59be334"
Expand Down

0 comments on commit d412d44

Please sign in to comment.