Skip to content

Commit

Permalink
fix(node): don't remember failed version request
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Mar 15, 2024
1 parent df700c4 commit fba0e79
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
10 changes: 7 additions & 3 deletions src/Middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,14 @@ export default class Middleware
this.pipeline.removePolicy({ name: userAgentPolicyName });
this.pipeline.removePolicy({ name: setClientRequestIdPolicyName });
if (!ignoreVersion) {
const statusPromise = this.getStatus();
const versionPromise = statusPromise.then(({ mdwVersion }) => mdwVersion, (error) => error);
let version: string | undefined;
const getVersion = async (): Promise<string> => {
if (version != null) return version;
version = (await this.getStatus()).mdwVersion;
return version;
};
this.pipeline.addPolicy(
genVersionCheckPolicy('middleware', '/v2/status', versionPromise, '1.47.0', '2.0.0'),
genVersionCheckPolicy('middleware', '/v2/status', getVersion, '1.47.0', '2.0.0'),
);
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,9 @@ export default class Node extends (NodeTransformed as unknown as NodeTransformed
this.pipeline.removePolicy({ name: userAgentPolicyName });
this.pipeline.removePolicy({ name: setClientRequestIdPolicyName });
if (!ignoreVersion) {
const versionPromise = this._getCachedStatus()
.then(({ nodeVersion }) => nodeVersion, (error) => error);
const getVersion = async (): Promise<string> => (await this._getCachedStatus()).nodeVersion;
this.pipeline.addPolicy(
genVersionCheckPolicy('node', '/v3/status', versionPromise, '6.2.0', '7.0.0'),
genVersionCheckPolicy('node', '/v3/status', getVersion, '6.2.0', '7.0.0'),
);
}
this.intAsString = true;
Expand All @@ -158,8 +157,8 @@ export default class Node extends (NodeTransformed as unknown as NodeTransformed
#cachedStatusPromise?: ReturnType<Node['getStatus']>;

async _getCachedStatus(): ReturnType<Node['getStatus']> {
this.#cachedStatusPromise ??= this.getStatus();
return this.#cachedStatusPromise;
if (this.#cachedStatusPromise != null) return this.#cachedStatusPromise;
return this.getStatus();
}

// @ts-expect-error use code generation to create node class?
Expand Down
10 changes: 7 additions & 3 deletions src/contract/compiler/Http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,14 @@ export default class CompilerHttp extends CompilerBase {
this.api.pipeline.removePolicy({ name: userAgentPolicyName });
this.api.pipeline.removePolicy({ name: setClientRequestIdPolicyName });
if (ignoreVersion !== true) {
const versionPromise = this.api.apiVersion()
.then(({ apiVersion }) => apiVersion, (error) => error);
let version: string | undefined;
const getVersion = async (): Promise<string> => {
if (version != null) return version;
version = (await this.api.apiVersion()).apiVersion;
return version;
};
this.api.pipeline.addPolicy(
genVersionCheckPolicy('compiler', '/api-version', versionPromise, '7.3.0', '9.0.0'),
genVersionCheckPolicy('compiler', '/api-version', getVersion, '7.3.0', '9.0.0'),
);
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/utils/autorest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,14 @@ export const genErrorFormatterPolicy = (
export const genVersionCheckPolicy = (
name: string,
ignorePath: string,
versionPromise: Promise<string | Error>,
versionCb: () => Promise<string>,
geVersion: string,
ltVersion: string,
): PipelinePolicy => ({
name: 'version-check',
async sendRequest(request, next) {
if (new URL(request.url).pathname === ignorePath) return next(request);
const version = await versionPromise;
if (version instanceof Error) throw version;
const args = [version, geVersion, ltVersion] as const;
const args = [await versionCb(), geVersion, ltVersion] as const;
if (!semverSatisfies(...args)) throw new UnsupportedVersionError(name, ...args);
return next(request);
},
Expand Down
7 changes: 7 additions & 0 deletions test/integration/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ describe('Node client', () => {
}));
});

it('doesn\'t remember failed version request', async () => {
const n = new Node('http://example.com');
await expect(n.getTopHeader()).to.be.rejectedWith('v3/status error: 404 status code');
n.$host = url;
expect(await n.getTopHeader()).to.be.an('object');
});

describe('Node Pool', () => {
it('Throw error on using API without node', () => {
const nodes = new AeSdkBase({});
Expand Down

0 comments on commit fba0e79

Please sign in to comment.