Skip to content

Commit

Permalink
Move common type guards to core-util
Browse files Browse the repository at this point in the history
A few type guards had duplicate definitions in multiple packages. This change moves the common functions to `core-util`.

Fixes Azure#21734
  • Loading branch information
dgetu committed Aug 10, 2022
1 parent 74bc6b3 commit bc7378e
Show file tree
Hide file tree
Showing 75 changed files with 135 additions and 346 deletions.
22 changes: 12 additions & 10 deletions common/tools/dev-tool/src/util/samples/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,16 +321,18 @@ export async function makeSamplesFactory(
*/
function postProcess(moduleText: string | Buffer): string {
const content = Buffer.isBuffer(moduleText) ? moduleText.toString("utf8") : moduleText;
return content
.replace(new RegExp(`^\\s*\\*\\s*@${AZSDK_META_TAG_PREFIX}.*\n`, "gm"), "")
// We also need to clean up extra blank lines that might be left behind by
// removing azsdk tags. These regular expressions are extremely frustrating
// because they deal almost exclusively in the literal "/" and "*" characters.
.replace(/(\s+\*)+\//s, "\n */")
// Clean up blank lines at the beginning
.replace(/\/\*\*(\s+\*)*/s, `/**\n *`)
// Finally remove empty doc comments.
.replace(/\s*\/\*\*(\s+\*)*\/\s*/s, "\n\n");
return (
content
.replace(new RegExp(`^\\s*\\*\\s*@${AZSDK_META_TAG_PREFIX}.*\n`, "gm"), "")
// We also need to clean up extra blank lines that might be left behind by
// removing azsdk tags. These regular expressions are extremely frustrating
// because they deal almost exclusively in the literal "/" and "*" characters.
.replace(/(\s+\*)+\//s, "\n */")
// Clean up blank lines at the beginning
.replace(/\/\*\*(\s+\*)*/s, `/**\n *`)
// Finally remove empty doc comments.
.replace(/\s*\/\*\*(\s+\*)*\/\s*/s, "\n\n")
);
}

// We use a tempdir at the outer layer to avoid creating dirty trees
Expand Down
23 changes: 19 additions & 4 deletions common/tools/dev-tool/src/util/testProxyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ const log = createPrinter("test-proxy");
const CONTAINER_NAME = "js-azsdk-test-proxy";

export async function startProxyTool(): Promise<void> {
log.info(`Attempting to start test proxy at http://localhost:${process.env.TEST_PROXY_HTTP_PORT ?? 5000} & https://localhost:${process.env.TEST_PROXY_HTTPS_PORT ?? 5001}.\n`);
log.info(
`Attempting to start test proxy at http://localhost:${
process.env.TEST_PROXY_HTTP_PORT ?? 5000
} & https://localhost:${process.env.TEST_PROXY_HTTPS_PORT ?? 5001}.\n`
);

const subprocess = spawn(await getDockerRunCommand(), [], {
shell: true,
Expand Down Expand Up @@ -59,13 +63,24 @@ async function getDockerRunCommand() {
const testProxyRecordingsLocation = "/srv/testproxy";
const allowLocalhostAccess = "--add-host host.docker.internal:host-gateway";
const imageToLoad = `azsdkengsys.azurecr.io/engsys/testproxy-lin:${await getImageTag()}`;
return `docker run --rm --name ${CONTAINER_NAME} -v ${repoRoot}:${testProxyRecordingsLocation} -p ${process.env.TEST_PROXY_HTTPS_PORT ?? 5001}:5001 -p ${process.env.TEST_PROXY_HTTP_PORT ?? 5000}:5000 ${allowLocalhostAccess} ${imageToLoad}`;
return `docker run --rm --name ${CONTAINER_NAME} -v ${repoRoot}:${testProxyRecordingsLocation} -p ${
process.env.TEST_PROXY_HTTPS_PORT ?? 5001
}:5001 -p ${
process.env.TEST_PROXY_HTTP_PORT ?? 5000
}:5000 ${allowLocalhostAccess} ${imageToLoad}`;
}

export async function isProxyToolActive(): Promise<boolean> {
try {
await makeRequest(`http://localhost:${process.env.TEST_PROXY_HTTP_PORT ?? 5000}/info/available`, {});
log.info(`Proxy tool seems to be active at http://localhost:${process.env.TEST_PROXY_HTTP_PORT ?? 5000}\n`);
await makeRequest(
`http://localhost:${process.env.TEST_PROXY_HTTP_PORT ?? 5000}/info/available`,
{}
);
log.info(
`Proxy tool seems to be active at http://localhost:${
process.env.TEST_PROXY_HTTP_PORT ?? 5000
}\n`
);
return true;
} catch (error: any) {
return false;
Expand Down
10 changes: 8 additions & 2 deletions common/tools/dev-tool/src/util/testUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { isProxyToolActive } from "./testProxyUtils";
import concurrently, { Command as ConcurrentlyCommand, ConcurrentlyCommandInput, ConcurrentlyOptions } from "concurrently";
import concurrently, {
Command as ConcurrentlyCommand,
ConcurrentlyCommandInput,
ConcurrentlyOptions,
} from "concurrently";
import { createPrinter } from "./printer";

const log = createPrinter("preparing-proxy-tool");
Expand All @@ -15,7 +19,9 @@ async function shouldRunProxyTool(): Promise<boolean> {
// No need to run a new one if it is already active
// Especially, CI uses this path
log.info(
`Proxy tool seems to be active, not attempting to start the test proxy at http://localhost:${process.env.TEST_PROXY_HTTP_PORT ?? 5000} & https://localhost:${process.env.TEST_PROXY_HTTPS_PORT ?? 5001}.\n`
`Proxy tool seems to be active, not attempting to start the test proxy at http://localhost:${
process.env.TEST_PROXY_HTTP_PORT ?? 5000
} & https://localhost:${process.env.TEST_PROXY_HTTPS_PORT ?? 5001}.\n`
);
}
return !isActive;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,18 +286,17 @@ export = {
const symbol = typeChecker
.getTypeAtLocation(converter.get(node as TSESTree.Node))
.getSymbol();
const overloads =
symbol?.declarations
? symbol.declarations
.filter(
(declaration: Declaration): boolean =>
reverter.get(declaration as TSNode) !== undefined
)
.map((declaration: Declaration): FunctionExpression => {
const method = reverter.get(declaration as TSNode) as MethodDefinition;
return method.value;
})
: [];
const overloads = symbol?.declarations
? symbol.declarations
.filter(
(declaration: Declaration): boolean =>
reverter.get(declaration as TSNode) !== undefined
)
.map((declaration: Declaration): FunctionExpression => {
const method = reverter.get(declaration as TSNode) as MethodDefinition;
return method.value;
})
: [];
evaluateOverloads(
overloads,
converter,
Expand Down Expand Up @@ -326,13 +325,12 @@ export = {
const symbol = typeChecker
.getTypeAtLocation(converter.get(node as TSESTree.Node))
.getSymbol();
const overloads =
symbol?.declarations
? symbol.declarations.map(
(declaration: Declaration): FunctionDeclaration =>
reverter.get(declaration as TSNode) as FunctionDeclaration
)
: [];
const overloads = symbol?.declarations
? symbol.declarations.map(
(declaration: Declaration): FunctionDeclaration =>
reverter.get(declaration as TSNode) as FunctionDeclaration
)
: [];
evaluateOverloads(
overloads,
converter,
Expand Down
2 changes: 1 addition & 1 deletion sdk/agrifood/agrifood-farming-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"tslib": "^2.2.0"
},
"devDependencies": {
"@azure/core-util": "^1.0.0",
"@azure/core-util": "^1.0.1",
"@azure/dev-tool": "^1.0.0",
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
"@azure/identity": "^2.0.1",
Expand Down
2 changes: 1 addition & 1 deletion sdk/appconfiguration/app-configuration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"@azure/core-rest-pipeline": "^1.6.0",
"@azure/core-tracing": "^1.0.0",
"@azure/core-auth": "^1.3.0",
"@azure/core-util": "^1.0.0",
"@azure/core-util": "^1.0.1",
"tslib": "^2.2.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
SecretReferenceValue,
secretReferenceContentType,
} from "../secretReference";
import { isDefined } from "./typeguards";
import { isDefined } from "@azure/core-util";

/**
* Formats the etag so it can be used with a If-Match/If-None-Match header
Expand Down
47 changes: 0 additions & 47 deletions sdk/appconfiguration/app-configuration/src/internal/typeguards.ts

This file was deleted.

2 changes: 1 addition & 1 deletion sdk/communication/communication-chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"@azure-tools/test-credential": "^1.0.0",
"@azure-tools/test-recorder": "^2.0.0",
"@azure/communication-identity": "^1.1.0-beta.2",
"@azure/core-util": "^1.0.0",
"@azure/core-util": "^1.0.1",
"@azure/dev-tool": "^1.0.0",
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
"@microsoft/api-extractor": "7.18.11",
Expand Down
2 changes: 1 addition & 1 deletion sdk/communication/communication-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"@azure/core-auth": "^1.3.0",
"@azure/core-rest-pipeline": "^1.3.2",
"@azure/core-tracing": "1.0.0-preview.13",
"@azure/core-util": "^1.0.0",
"@azure/core-util": "^1.0.1",
"events": "^3.0.0",
"jwt-decode": "^3.1.2",
"tslib": "^2.2.0"
Expand Down
2 changes: 1 addition & 1 deletion sdk/communication/communication-identity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"devDependencies": {
"@azure-tools/test-credential": "^1.0.0",
"@azure-tools/test-recorder": "^2.0.0",
"@azure/core-util": "^1.0.0",
"@azure/core-util": "^1.0.1",
"@azure/dev-tool": "^1.0.0",
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
"@azure/identity": "^2.0.1",
Expand Down
2 changes: 1 addition & 1 deletion sdk/communication/communication-sms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"uuid": "^8.3.0"
},
"devDependencies": {
"@azure/core-util": "^1.0.0-beta.1",
"@azure/core-util": "^1.0.1-beta.1",
"@azure/dev-tool": "^1.0.0",
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
"@azure/identity": "^2.0.1",
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-amqp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"dependencies": {
"@azure/abort-controller": "^1.0.0",
"@azure/core-auth": "^1.3.0",
"@azure/core-util": "^1.0.0",
"@azure/core-util": "^1.0.1",
"@azure/logger": "^1.0.0",
"buffer": "^6.0.0",
"events": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-amqp/src/auth/tokenProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
isNamedKeyCredential,
isSASCredential,
} from "@azure/core-auth";
import { isObjectWithProperties } from "../util/typeGuards";
import { isObjectWithProperties } from "@azure/core-util";
import jssha from "jssha";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.

import { WebSocketImpl } from "rhea-promise";
import { isDefined } from "../util/typeGuards";
import { isDefined } from "@azure/core-util";
import { parseConnectionString } from "../util/utils";

/**
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-amqp/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* eslint-disable eqeqeq */

import { AmqpError, AmqpResponseStatusCode, isAmqpError as rheaIsAmqpError } from "rhea-promise";
import { isDefined, isObjectWithProperties } from "./util/typeGuards";
import { isDefined, isObjectWithProperties } from "@azure/core-util";
import { isNode, isNumber, isString } from "../src/util/utils";
import { isError } from "@azure/core-util";

Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-amqp/src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.

import { createClientLogger } from "@azure/logger";
import { isObjectWithProperties } from "./util/typeGuards";
import { isObjectWithProperties } from "@azure/core-util";

/**
* The \@azure/logger configuration for this package.
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-amqp/src/requestResponseLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from "rhea-promise";
import { Constants, StandardAbortMessage } from "./util/constants";
import { logErrorStackTrace, logger } from "./log";
import { isDefined } from "./util/typeGuards";
import { isDefined } from "@azure/core-util";

/**
* Describes the options that can be specified while sending a request.
Expand Down
48 changes: 1 addition & 47 deletions sdk/core/core-amqp/src/util/typeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,7 @@
// Licensed under the MIT license.

import { SasTokenProvider } from "../auth/tokenProvider";

/**
* Helper TypeGuard that checks if something is defined or not.
* @param thing - Anything
* @internal
*/
export function isDefined<T>(thing: T | undefined | null): thing is T {
return typeof thing !== "undefined" && thing !== null;
}

/**
* Helper TypeGuard that checks if the input is an object with the specified properties.
* Note: The properties may be inherited.
* @param thing - Anything.
* @param properties - The name of the properties that should appear in the object.
* @internal
*/
export function isObjectWithProperties<Thing, PropertyName extends string>(
thing: Thing,
properties: PropertyName[]
): thing is Thing & Record<PropertyName, unknown> {
if (!isDefined(thing) || typeof thing !== "object") {
return false;
}

for (const property of properties) {
if (!objectHasProperty(thing, property)) {
return false;
}
}

return true;
}

/**
* Helper TypeGuard that checks if the input is an object with the specified property.
* Note: The property may be inherited.
* @param thing - Any object.
* @param property - The name of the property that should appear in the object.
* @internal
*/
export function objectHasProperty<Thing, PropertyName extends string>(
thing: Thing,
property: PropertyName
): thing is Thing & Record<PropertyName, unknown> {
return typeof thing === "object" && property in (thing as Record<string, unknown>);
}
import { isObjectWithProperties } from "@azure/core-util";

/**
* Typeguard that checks if the input is a SasTokenProvider.
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-amqp/src/util/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AbortError, AbortSignalLike } from "@azure/abort-controller";
import { CancellableAsyncLock, CancellableAsyncLockImpl } from "./lock";
import { StandardAbortMessage } from "./constants";
import { WebSocketImpl } from "rhea-promise";
import { isDefined } from "./typeGuards";
import { isDefined } from "@azure/core-util";

/**
* @internal
Expand Down
2 changes: 2 additions & 0 deletions sdk/core/core-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
"sideEffects": false,
"dependencies": {
"@azure/abort-controller": "^1.0.0",
"@azure/core-util": "^1.0.1",
"@azure/dev-tool": "^1.0.0",
"tslib": "^2.2.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-auth/src/azureNamedKeyCredential.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { isObjectWithProperties } from "./typeguards";
import { isObjectWithProperties } from "@azure/core-util";

/**
* Represents a credential defined by a static API name and key.
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-auth/src/azureSASCredential.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { isObjectWithProperties } from "./typeguards";
import { isObjectWithProperties } from "@azure/core-util";

/**
* Represents a credential defined by a static shared access signature.
Expand Down
Loading

0 comments on commit bc7378e

Please sign in to comment.