@dfinity/agent
Advanced tools
Comparing version 0.13.3 to 0.14.0
@@ -43,2 +43,7 @@ import { JsonObject } from '@dfinity/candid'; | ||
disableNonce?: boolean; | ||
/** | ||
* Number of times to retry requests before throwing an error | ||
* @default 3 | ||
*/ | ||
retryTimes?: number; | ||
} | ||
@@ -54,3 +59,6 @@ export declare class HttpAgent implements Agent { | ||
private _rootKeyFetched; | ||
private _retryTimes; | ||
readonly _isAgent = true; | ||
constructor(options?: HttpAgentOptions); | ||
isLocal(): boolean; | ||
addTransform(fn: HttpAgentRequestTransformFn, priority?: number): void; | ||
@@ -63,2 +71,3 @@ getPrincipal(): Promise<Principal>; | ||
}, identity?: Identity | Promise<Identity>): Promise<SubmitResponse>; | ||
private _requestAndRetry; | ||
query(canisterId: Principal | string, fields: QueryFields, identity?: Identity | Promise<Identity>): Promise<QueryResponse>; | ||
@@ -65,0 +74,0 @@ createReadStateRequest(fields: ReadStateOptions, identity?: Identity | Promise<Identity>): Promise<any>; |
@@ -118,2 +118,4 @@ "use strict"; | ||
this._rootKeyFetched = false; | ||
this._retryTimes = 3; // Retry requests 3 times before erroring by default | ||
this._isAgent = true; | ||
if (options.source) { | ||
@@ -151,2 +153,6 @@ if (!(options.source instanceof HttpAgent)) { | ||
} | ||
// Default is 3, only set if option is provided | ||
if (options.retryTimes !== undefined) { | ||
this._retryTimes = options.retryTimes; | ||
} | ||
// Rewrite to avoid redirects | ||
@@ -166,2 +172,6 @@ if (this._host.hostname.endsWith(IC0_SUB_DOMAIN)) { | ||
} | ||
isLocal() { | ||
const hostname = this._host.hostname; | ||
return hostname === '127.0.0.1' || hostname.endsWith('localhost'); | ||
} | ||
addTransform(fn, priority = fn.priority || 0) { | ||
@@ -216,11 +226,4 @@ // Keep the pipeline sorted at all time, by priority. | ||
// calculate the requestId locally. | ||
const [response, requestId] = await Promise.all([ | ||
this._fetch('' + new URL(`/api/v2/canister/${ecid.toText()}/call`, this._host), Object.assign(Object.assign({}, transformedRequest.request), { body })), | ||
(0, request_id_1.requestIdOf)(submit), | ||
]); | ||
if (!response.ok) { | ||
throw new Error(`Server returned an error:\n` + | ||
` Code: ${response.status} (${response.statusText})\n` + | ||
` Body: ${await response.text()}\n`); | ||
} | ||
const request = this._requestAndRetry(() => this._fetch('' + new URL(`/api/v2/canister/${ecid.toText()}/call`, this._host), Object.assign(Object.assign({}, transformedRequest.request), { body }))); | ||
const [response, requestId] = await Promise.all([request, (0, request_id_1.requestIdOf)(submit)]); | ||
return { | ||
@@ -235,2 +238,22 @@ requestId, | ||
} | ||
async _requestAndRetry(request, tries = 0) { | ||
if (tries > this._retryTimes && this._retryTimes !== 0) { | ||
throw new Error(`AgentError: Exceeded configured limit of ${this._retryTimes} retry attempts. Please check your network connection or try again in a few moments`); | ||
} | ||
const response = await request(); | ||
if (!response.ok) { | ||
const responseText = await response.clone().text(); | ||
const errorMessage = `Server returned an error:\n` + | ||
` Code: ${response.status} (${response.statusText})\n` + | ||
` Body: ${responseText}\n`; | ||
if (this._retryTimes > tries) { | ||
console.warn(errorMessage + ` Retrying request.`); | ||
return await this._requestAndRetry(request, tries + 1); | ||
} | ||
else { | ||
throw new Error(errorMessage); | ||
} | ||
} | ||
return response; | ||
} | ||
async query(canisterId, fields, identity) { | ||
@@ -264,8 +287,3 @@ const id = await (identity !== undefined ? await identity : await this._identity); | ||
const body = cbor.encode(transformedRequest.body); | ||
const response = await this._fetch('' + new URL(`/api/v2/canister/${canister.toText()}/query`, this._host), Object.assign(Object.assign({}, transformedRequest.request), { body })); | ||
if (!response.ok) { | ||
throw new Error(`Server returned an error:\n` + | ||
` Code: ${response.status} (${response.statusText})\n` + | ||
` Body: ${await response.text()}\n`); | ||
} | ||
const response = await this._requestAndRetry(() => this._fetch('' + new URL(`/api/v2/canister/${canister.toText()}/query`, this._host), Object.assign(Object.assign({}, transformedRequest.request), { body }))); | ||
return cbor.decode(await response.arrayBuffer()); | ||
@@ -343,8 +361,3 @@ } | ||
: {}; | ||
const response = await this._fetch('' + new URL(`/api/v2/status`, this._host), { headers }); | ||
if (!response.ok) { | ||
throw new Error(`Server returned an error:\n` + | ||
` Code: ${response.status} (${response.statusText})\n` + | ||
` Body: ${await response.text()}\n`); | ||
} | ||
const response = await this._requestAndRetry(() => this._fetch('' + new URL(`/api/v2/status`, this._host), { headers })); | ||
return cbor.decode(await response.arrayBuffer()); | ||
@@ -351,0 +364,0 @@ } |
@@ -10,2 +10,3 @@ import { ActorSubclass } from './actor'; | ||
export * from './canisters/management'; | ||
export * from './fetch_candid'; | ||
export * from './request_id'; | ||
@@ -12,0 +13,0 @@ export * from './utils/bls'; |
@@ -38,2 +38,3 @@ "use strict"; | ||
__exportStar(require("./canisters/management"), exports); | ||
__exportStar(require("./fetch_candid"), exports); | ||
__exportStar(require("./request_id"), exports); | ||
@@ -40,0 +41,0 @@ __exportStar(require("./utils/bls"), exports); |
@@ -43,2 +43,7 @@ import { JsonObject } from '@dfinity/candid'; | ||
disableNonce?: boolean; | ||
/** | ||
* Number of times to retry requests before throwing an error | ||
* @default 3 | ||
*/ | ||
retryTimes?: number; | ||
} | ||
@@ -54,3 +59,6 @@ export declare class HttpAgent implements Agent { | ||
private _rootKeyFetched; | ||
private _retryTimes; | ||
readonly _isAgent = true; | ||
constructor(options?: HttpAgentOptions); | ||
isLocal(): boolean; | ||
addTransform(fn: HttpAgentRequestTransformFn, priority?: number): void; | ||
@@ -63,2 +71,3 @@ getPrincipal(): Promise<Principal>; | ||
}, identity?: Identity | Promise<Identity>): Promise<SubmitResponse>; | ||
private _requestAndRetry; | ||
query(canisterId: Principal | string, fields: QueryFields, identity?: Identity | Promise<Identity>): Promise<QueryResponse>; | ||
@@ -65,0 +74,0 @@ createReadStateRequest(fields: ReadStateOptions, identity?: Identity | Promise<Identity>): Promise<any>; |
@@ -87,2 +87,4 @@ import { Principal } from '@dfinity/principal'; | ||
this._rootKeyFetched = false; | ||
this._retryTimes = 3; // Retry requests 3 times before erroring by default | ||
this._isAgent = true; | ||
if (options.source) { | ||
@@ -120,2 +122,6 @@ if (!(options.source instanceof HttpAgent)) { | ||
} | ||
// Default is 3, only set if option is provided | ||
if (options.retryTimes !== undefined) { | ||
this._retryTimes = options.retryTimes; | ||
} | ||
// Rewrite to avoid redirects | ||
@@ -135,2 +141,6 @@ if (this._host.hostname.endsWith(IC0_SUB_DOMAIN)) { | ||
} | ||
isLocal() { | ||
const hostname = this._host.hostname; | ||
return hostname === '127.0.0.1' || hostname.endsWith('localhost'); | ||
} | ||
addTransform(fn, priority = fn.priority || 0) { | ||
@@ -185,11 +195,4 @@ // Keep the pipeline sorted at all time, by priority. | ||
// calculate the requestId locally. | ||
const [response, requestId] = await Promise.all([ | ||
this._fetch('' + new URL(`/api/v2/canister/${ecid.toText()}/call`, this._host), Object.assign(Object.assign({}, transformedRequest.request), { body })), | ||
requestIdOf(submit), | ||
]); | ||
if (!response.ok) { | ||
throw new Error(`Server returned an error:\n` + | ||
` Code: ${response.status} (${response.statusText})\n` + | ||
` Body: ${await response.text()}\n`); | ||
} | ||
const request = this._requestAndRetry(() => this._fetch('' + new URL(`/api/v2/canister/${ecid.toText()}/call`, this._host), Object.assign(Object.assign({}, transformedRequest.request), { body }))); | ||
const [response, requestId] = await Promise.all([request, requestIdOf(submit)]); | ||
return { | ||
@@ -204,2 +207,22 @@ requestId, | ||
} | ||
async _requestAndRetry(request, tries = 0) { | ||
if (tries > this._retryTimes && this._retryTimes !== 0) { | ||
throw new Error(`AgentError: Exceeded configured limit of ${this._retryTimes} retry attempts. Please check your network connection or try again in a few moments`); | ||
} | ||
const response = await request(); | ||
if (!response.ok) { | ||
const responseText = await response.clone().text(); | ||
const errorMessage = `Server returned an error:\n` + | ||
` Code: ${response.status} (${response.statusText})\n` + | ||
` Body: ${responseText}\n`; | ||
if (this._retryTimes > tries) { | ||
console.warn(errorMessage + ` Retrying request.`); | ||
return await this._requestAndRetry(request, tries + 1); | ||
} | ||
else { | ||
throw new Error(errorMessage); | ||
} | ||
} | ||
return response; | ||
} | ||
async query(canisterId, fields, identity) { | ||
@@ -233,8 +256,3 @@ const id = await (identity !== undefined ? await identity : await this._identity); | ||
const body = cbor.encode(transformedRequest.body); | ||
const response = await this._fetch('' + new URL(`/api/v2/canister/${canister.toText()}/query`, this._host), Object.assign(Object.assign({}, transformedRequest.request), { body })); | ||
if (!response.ok) { | ||
throw new Error(`Server returned an error:\n` + | ||
` Code: ${response.status} (${response.statusText})\n` + | ||
` Body: ${await response.text()}\n`); | ||
} | ||
const response = await this._requestAndRetry(() => this._fetch('' + new URL(`/api/v2/canister/${canister.toText()}/query`, this._host), Object.assign(Object.assign({}, transformedRequest.request), { body }))); | ||
return cbor.decode(await response.arrayBuffer()); | ||
@@ -312,8 +330,3 @@ } | ||
: {}; | ||
const response = await this._fetch('' + new URL(`/api/v2/status`, this._host), { headers }); | ||
if (!response.ok) { | ||
throw new Error(`Server returned an error:\n` + | ||
` Code: ${response.status} (${response.statusText})\n` + | ||
` Body: ${await response.text()}\n`); | ||
} | ||
const response = await this._requestAndRetry(() => this._fetch('' + new URL(`/api/v2/status`, this._host), { headers })); | ||
return cbor.decode(await response.arrayBuffer()); | ||
@@ -320,0 +333,0 @@ } |
@@ -10,2 +10,3 @@ import { ActorSubclass } from './actor'; | ||
export * from './canisters/management'; | ||
export * from './fetch_candid'; | ||
export * from './request_id'; | ||
@@ -12,0 +13,0 @@ export * from './utils/bls'; |
@@ -9,2 +9,3 @@ export * from './actor'; | ||
export * from './canisters/management'; | ||
export * from './fetch_candid'; | ||
export * from './request_id'; | ||
@@ -11,0 +12,0 @@ export * from './utils/bls'; |
{ | ||
"name": "@dfinity/agent", | ||
"version": "0.13.3", | ||
"version": "0.14.0", | ||
"author": "DFINITY Stiftung <sdk@dfinity.org>", | ||
@@ -52,4 +52,4 @@ "license": "Apache-2.0", | ||
"peerDependencies": { | ||
"@dfinity/candid": "^0.13.3", | ||
"@dfinity/principal": "^0.13.3" | ||
"@dfinity/candid": "^0.14.0", | ||
"@dfinity/principal": "^0.14.0" | ||
}, | ||
@@ -56,0 +56,0 @@ "dependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1036324
160
7609