Skip to content

Commit

Permalink
fix(node,middleware,compiler): version check if deployed at path
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Apr 3, 2024
1 parent 76e4a88 commit 450296e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 48 deletions.
23 changes: 11 additions & 12 deletions src/Middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// eslint-disable-next-line max-classes-per-file
import BigNumber from 'bignumber.js';
import { OperationArguments, OperationSpec } from '@azure/core-client';
import { OperationArguments, OperationOptions, OperationSpec } from '@azure/core-client';
import { userAgentPolicyName, setClientRequestIdPolicyName } from '@azure/core-rest-pipeline';
import {
genRequestQueuesPolicy, genCombineGetRequestsPolicy, genErrorFormatterPolicy,
Expand Down Expand Up @@ -120,10 +120,20 @@ export default class Middleware
retryOverallDelay?: number;
} = {},
) {
let version: string | undefined;
const getVersion = async (opts: OperationOptions): Promise<string> => {
if (version != null) return version;
version = (await this.getStatus(opts)).mdwVersion;
return version;
};

// eslint-disable-next-line constructor-super
super(url, {
allowInsecureConnection: true,
additionalPolicies: [
...ignoreVersion ? [] : [
genVersionCheckPolicy('middleware', getVersion, '1.47.0', '2.0.0'),
],
genRequestQueuesPolicy(),
genCombineGetRequestsPolicy(),
genRetryOnFailurePolicy(retryCount, retryOverallDelay),
Expand All @@ -133,16 +143,5 @@ export default class Middleware
});
this.pipeline.removePolicy({ name: userAgentPolicyName });
this.pipeline.removePolicy({ name: setClientRequestIdPolicyName });
if (!ignoreVersion) {
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', '/mdw/v2/status', getVersion, '1.47.0', '2.0.0'),
);
}
}
}
17 changes: 8 additions & 9 deletions src/Node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// eslint-disable-next-line max-classes-per-file
import BigNumber from 'bignumber.js';
import { OperationArguments, OperationSpec } from '@azure/core-client';
import { OperationArguments, OperationOptions, OperationSpec } from '@azure/core-client';
import { userAgentPolicyName, setClientRequestIdPolicyName } from '@azure/core-rest-pipeline';
import {
genRequestQueuesPolicy, genCombineGetRequestsPolicy, genErrorFormatterPolicy,
Expand Down Expand Up @@ -130,10 +130,14 @@ export default class Node extends (NodeTransformed as unknown as NodeTransformed
retryOverallDelay?: number;
} = {},
) {
const getVersion = async (opts: OperationOptions): Promise<string> => (
(await this._getCachedStatus(opts)).nodeVersion
);
// eslint-disable-next-line constructor-super
super(url, {
allowInsecureConnection: true,
additionalPolicies: [
...ignoreVersion ? [] : [genVersionCheckPolicy('node', getVersion, '6.2.0', '7.0.0')],
genRequestQueuesPolicy(),
genCombineGetRequestsPolicy(),
genRetryOnFailurePolicy(retryCount, retryOverallDelay),
Expand All @@ -145,22 +149,17 @@ export default class Node extends (NodeTransformed as unknown as NodeTransformed
});
this.pipeline.removePolicy({ name: userAgentPolicyName });
this.pipeline.removePolicy({ name: setClientRequestIdPolicyName });
if (!ignoreVersion) {
const getVersion = async (): Promise<string> => (await this._getCachedStatus()).nodeVersion;
this.pipeline.addPolicy(
genVersionCheckPolicy('node', '/v3/status', getVersion, '6.2.0', '7.0.0'),
);
}
this.intAsString = true;
}

#cachedStatusPromise?: ReturnType<Node['getStatus']>;

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

// eslint-disable-next-line rulesdir/tsdoc-syntax
/** @ts-expect-error use code generation to create node class? */
override async getStatus(
...args: Parameters<InstanceType<NodeTransformedApi>['getStatus']>
Expand Down
23 changes: 11 additions & 12 deletions src/contract/compiler/Http.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
RestError, userAgentPolicyName, setClientRequestIdPolicyName,
} from '@azure/core-rest-pipeline';
import { OperationOptions } from '@azure/core-client';
import {
Compiler as CompilerApi,
ErrorModel,
Expand Down Expand Up @@ -31,11 +32,20 @@ export default class CompilerHttp extends CompilerBase {
* @param options - Options
* @param options.ignoreVersion - Don't check compiler version
*/
constructor(compilerUrl: string, { ignoreVersion }: { ignoreVersion?: boolean } = {}) {
constructor(compilerUrl: string, { ignoreVersion = false }: { ignoreVersion?: boolean } = {}) {
super();

let version: string | undefined;
const getVersion = async (opts: OperationOptions): Promise<string> => {
if (version != null) return version;
version = (await this.api.apiVersion(opts)).apiVersion;
return version;
};

this.api = new CompilerApi(compilerUrl, {
allowInsecureConnection: true,
additionalPolicies: [
...ignoreVersion ? [] : [genVersionCheckPolicy('compiler', getVersion, '7.3.0', '9.0.0')],
genErrorFormatterPolicy((body: GeneralCompilerError | CompilerErrorApi[]) => {
let message = '';
if ('reason' in body) {
Expand All @@ -55,17 +65,6 @@ export default class CompilerHttp extends CompilerBase {
});
this.api.pipeline.removePolicy({ name: userAgentPolicyName });
this.api.pipeline.removePolicy({ name: setClientRequestIdPolicyName });
if (ignoreVersion !== true) {
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', getVersion, '7.3.0', '9.0.0'),
);
}
}

async compileBySourceCode(
Expand Down
28 changes: 17 additions & 11 deletions src/utils/autorest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RestError, PipelineResponse, PipelinePolicy } from '@azure/core-rest-pipeline';
import { AdditionalPolicyConfig, FullOperationResponse } from '@azure/core-client';
import { RestError, PipelineResponse } from '@azure/core-rest-pipeline';
import { AdditionalPolicyConfig, FullOperationResponse, OperationOptions } from '@azure/core-client';
import { pause } from './other';
import semverSatisfies from './semver-satisfies';
import { UnsupportedVersionError } from './errors';
Expand Down Expand Up @@ -94,18 +94,24 @@ export const genErrorFormatterPolicy = (

export const genVersionCheckPolicy = (
name: string,
ignorePath: string,
versionCb: () => Promise<string>,
versionCb: (options: OperationOptions) => 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 args = [await versionCb(), geVersion, ltVersion] as const;
if (!semverSatisfies(...args)) throw new UnsupportedVersionError(name, ...args);
return next(request);
): AdditionalPolicyConfig => ({
policy: {
name: 'version-check',
async sendRequest(request, next) {
if (request.headers.has('__version-check')) {
request.headers.delete('__version-check');
return next(request);
}
const options = { requestOptions: { customHeaders: { '__version-check': 'true' } } };
const args = [await versionCb(options), geVersion, ltVersion] as const;
if (!semverSatisfies(...args)) throw new UnsupportedVersionError(name, ...args);
return next(request);
},
},
position: 'perCall',
});

export const genRetryOnFailurePolicy = (
Expand Down
8 changes: 4 additions & 4 deletions test/integration/AeSdkMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,22 @@ describe('AeSdkMethods', () => {
{ policy: { name: 'logPolicy' }, options: { afterPhase: 'Sign' } },
{ policy: { name: 'serializationPolicy' }, options: { phase: 'Serialize' } },
{ policy: { name: 'deserializationPolicy' }, options: { phase: 'Deserialize' } },
{ policy: { name: 'version-check' }, options: {} },
{ policy: { name: 'request-queues' }, options: {} },
{ policy: { name: 'combine-get-requests' }, options: {} },
{ policy: { name: 'retry-on-failure' }, options: {} },
{ policy: { name: 'error-formatter' }, options: {} },
{ policy: { name: 'version-check' }, options: {} },
],
_orderedPolicies: [
{ name: 'serializationPolicy' },
{ name: 'proxyPolicy' },
{ name: 'decompressResponsePolicy' },
{ name: 'formDataPolicy' },
{ name: 'version-check' },
{ name: 'request-queues' },
{ name: 'combine-get-requests' },
{ name: 'retry-on-failure' },
{ name: 'error-formatter' },
{ name: 'version-check' },
{ name: 'deserializationPolicy' },
{ name: 'multipartPolicy' },
{ name: 'defaultRetryPolicy' },
Expand Down Expand Up @@ -107,16 +107,16 @@ describe('AeSdkMethods', () => {
{ policy: { name: 'logPolicy' }, options: { afterPhase: 'Sign' } },
{ policy: { name: 'serializationPolicy' }, options: { phase: 'Serialize' } },
{ policy: { name: 'deserializationPolicy' }, options: { phase: 'Deserialize' } },
{ policy: { name: 'error-formatter' }, options: {} },
{ policy: { name: 'version-check' }, options: {} },
{ policy: { name: 'error-formatter' }, options: {} },
],
_orderedPolicies: [
{ name: 'serializationPolicy' },
{ name: 'proxyPolicy' },
{ name: 'decompressResponsePolicy' },
{ name: 'formDataPolicy' },
{ name: 'error-formatter' },
{ name: 'version-check' },
{ name: 'error-formatter' },
{ name: 'deserializationPolicy' },
{ name: 'multipartPolicy' },
{ name: 'defaultRetryPolicy' },
Expand Down

0 comments on commit 450296e

Please sign in to comment.