Skip to content

Commit

Permalink
[storage blob] Change from isNode to isNodeCompatible
Browse files Browse the repository at this point in the history
  • Loading branch information
mpodwysocki committed Mar 27, 2024
1 parent b4ef861 commit 14377bf
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 84 deletions.
3 changes: 3 additions & 0 deletions sdk/core/core-util/review/core-util.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ export function isError(e: unknown): e is Error;
// @public
export const isNode: boolean;

// @public
export const isNodeCompatible: boolean;

// @public
export function isObject(input: unknown): input is UnknownObject;

Expand Down
5 changes: 5 additions & 0 deletions sdk/core/core-util/src/checkEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ export const isNode =
!isDeno &&
!isBun;

/**
* A constant that indicates whether the environment the code is running is a Node.JS compatible environment.
*/
export const isNodeCompatible = isNode || isDeno || isBun;

/**
* A constant that indicates whether the environment the code is running is in React-Native.
*/
Expand Down
1 change: 1 addition & 0 deletions sdk/core/core-util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export {
isBrowser,
isBun,
isNode,
isNodeCompatible,
isDeno,
isReactNative,
isWebWorker,
Expand Down
6 changes: 3 additions & 3 deletions sdk/storage/storage-blob/src/BlobBatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
PipelineResponse,
SendRequest,
} from "@azure/core-rest-pipeline";
import { isNode } from "@azure/core-util";
import { isNodeCompatible } from "@azure/core-util";
import { AnonymousCredential } from "./credentials/AnonymousCredential";
import { BlobClient, BlobDeleteOptions, BlobSetTierOptions } from "./Clients";
import { AccessTier } from "./generatedModels";
Expand Down Expand Up @@ -161,7 +161,7 @@ export class BlobBatch {

if (
typeof urlOrBlobClient === "string" &&
((isNode && credentialOrOptions instanceof StorageSharedKeyCredential) ||
((isNodeCompatible && credentialOrOptions instanceof StorageSharedKeyCredential) ||
credentialOrOptions instanceof AnonymousCredential ||
isTokenCredential(credentialOrOptions))
) {
Expand Down Expand Up @@ -265,7 +265,7 @@ export class BlobBatch {

if (
typeof urlOrBlobClient === "string" &&
((isNode && credentialOrTier instanceof StorageSharedKeyCredential) ||
((isNodeCompatible && credentialOrTier instanceof StorageSharedKeyCredential) ||
credentialOrTier instanceof AnonymousCredential ||
isTokenCredential(credentialOrTier))
) {
Expand Down
7 changes: 4 additions & 3 deletions sdk/storage/storage-blob/src/BlobDownloadResponse.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { isNode } from "@azure/core-util";
import { BlobImmutabilityPolicyMode } from "./generatedModels";

import { isNodeCompatible } from "@azure/core-util";
import { BlobImmutabilityPolicyMode } from "./generatedModels";
import {
BlobDownloadHeaders,
BlobType,
Expand Down Expand Up @@ -500,7 +500,8 @@ export class BlobDownloadResponse implements BlobDownloadResponseParsed {
* @readonly
*/
public get readableStreamBody(): NodeJS.ReadableStream | undefined {
return isNode ? this.blobDownloadStream : undefined;
// TODO: Replace with isNodeCompatible once it's available
return isNodeCompatible ? this.blobDownloadStream : undefined;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions sdk/storage/storage-blob/src/BlobServiceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.
import { TokenCredential, isTokenCredential } from "@azure/core-auth";
import { getDefaultProxySettings } from "@azure/core-rest-pipeline";
import { isNode } from "@azure/core-util";
import { isNodeCompatible } from "@azure/core-util";
import { AbortSignalLike } from "@azure/abort-controller";
import {
ServiceGetUserDelegationKeyHeaders,
Expand Down Expand Up @@ -360,7 +360,7 @@ export class BlobServiceClient extends StorageClient {
options = options || {};
const extractedCreds = extractConnectionStringParts(connectionString);
if (extractedCreds.kind === "AccountConnString") {
if (isNode) {
if (isNodeCompatible) {
const sharedKeyCredential = new StorageSharedKeyCredential(
extractedCreds.accountName!,
extractedCreds.accountKey,
Expand Down Expand Up @@ -451,7 +451,7 @@ export class BlobServiceClient extends StorageClient {
if (isPipelineLike(credentialOrPipeline)) {
pipeline = credentialOrPipeline;
} else if (
(isNode && credentialOrPipeline instanceof StorageSharedKeyCredential) ||
(isNodeCompatible && credentialOrPipeline instanceof StorageSharedKeyCredential) ||
credentialOrPipeline instanceof AnonymousCredential ||
isTokenCredential(credentialOrPipeline)
) {
Expand Down
31 changes: 17 additions & 14 deletions sdk/storage/storage-blob/src/Clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import {
TransferProgressEvent,
} from "@azure/core-rest-pipeline";
import { isTokenCredential, TokenCredential } from "@azure/core-auth";
import { isNode } from "@azure/core-util";
import { isNodeCompatible } from "@azure/core-util";
import { PollOperationState } from "@azure/core-lro";
import { randomUUID } from "@azure/core-util";
import { Readable } from "stream";

import { BlobDownloadResponse } from "./BlobDownloadResponse";
import { BlobQueryResponse } from "./BlobQueryResponse";
import { AnonymousCredential } from "./credentials/AnonymousCredential";
Expand Down Expand Up @@ -996,7 +995,8 @@ export class BlobClient extends StorageClient {
url = urlOrConnectionString;
pipeline = credentialOrPipelineOrContainerName;
} else if (
(isNode && credentialOrPipelineOrContainerName instanceof StorageSharedKeyCredential) ||
(isNodeCompatible &&
credentialOrPipelineOrContainerName instanceof StorageSharedKeyCredential) ||
credentialOrPipelineOrContainerName instanceof AnonymousCredential ||
isTokenCredential(credentialOrPipelineOrContainerName)
) {
Expand Down Expand Up @@ -1027,7 +1027,7 @@ export class BlobClient extends StorageClient {

const extractedCreds = extractConnectionStringParts(urlOrConnectionString);
if (extractedCreds.kind === "AccountConnString") {
if (isNode) {
if (isNodeCompatible) {
const sharedKeyCredential = new StorageSharedKeyCredential(
extractedCreds.accountName!,
extractedCreds.accountKey,
Expand Down Expand Up @@ -1210,7 +1210,7 @@ export class BlobClient extends StorageClient {
ifTags: options.conditions?.tagConditions,
},
requestOptions: {
onDownloadProgress: isNode ? undefined : options.onProgress, // for Node.js, progress is reported by RetriableReadableStream
onDownloadProgress: isNodeCompatible ? undefined : options.onProgress, // for Node.js, progress is reported by RetriableReadableStream
},
range: offset === 0 && !count ? undefined : rangeToString({ offset, count }),
rangeGetContentMD5: options.rangeGetContentMD5,
Expand All @@ -1228,7 +1228,7 @@ export class BlobClient extends StorageClient {
objectReplicationSourceProperties: parseObjectReplicationRecord(res.objectReplicationRules),
};
// Return browser response immediately
if (!isNode) {
if (!isNodeCompatible) {
return wrappedRes;
}

Expand Down Expand Up @@ -2533,7 +2533,8 @@ export class AppendBlobClient extends BlobClient {
url = urlOrConnectionString;
pipeline = credentialOrPipelineOrContainerName;
} else if (
(isNode && credentialOrPipelineOrContainerName instanceof StorageSharedKeyCredential) ||
(isNodeCompatible &&
credentialOrPipelineOrContainerName instanceof StorageSharedKeyCredential) ||
credentialOrPipelineOrContainerName instanceof AnonymousCredential ||
isTokenCredential(credentialOrPipelineOrContainerName)
) {
Expand Down Expand Up @@ -2561,7 +2562,7 @@ export class AppendBlobClient extends BlobClient {

const extractedCreds = extractConnectionStringParts(urlOrConnectionString);
if (extractedCreds.kind === "AccountConnString") {
if (isNode) {
if (isNodeCompatible) {
const sharedKeyCredential = new StorageSharedKeyCredential(
extractedCreds.accountName!,
extractedCreds.accountKey,
Expand Down Expand Up @@ -3517,7 +3518,8 @@ export class BlockBlobClient extends BlobClient {
url = urlOrConnectionString;
pipeline = credentialOrPipelineOrContainerName;
} else if (
(isNode && credentialOrPipelineOrContainerName instanceof StorageSharedKeyCredential) ||
(isNodeCompatible &&
credentialOrPipelineOrContainerName instanceof StorageSharedKeyCredential) ||
credentialOrPipelineOrContainerName instanceof AnonymousCredential ||
isTokenCredential(credentialOrPipelineOrContainerName)
) {
Expand Down Expand Up @@ -3548,7 +3550,7 @@ export class BlockBlobClient extends BlobClient {

const extractedCreds = extractConnectionStringParts(urlOrConnectionString);
if (extractedCreds.kind === "AccountConnString") {
if (isNode) {
if (isNodeCompatible) {
const sharedKeyCredential = new StorageSharedKeyCredential(
extractedCreds.accountName!,
extractedCreds.accountKey,
Expand Down Expand Up @@ -3642,7 +3644,7 @@ export class BlockBlobClient extends BlobClient {
options: BlockBlobQueryOptions = {},
): Promise<BlobDownloadResponseModel> {
ensureCpkIfSpecified(options.customerProvidedKey, this.isHttps);
if (!isNode) {
if (!isNodeCompatible) {
throw new Error("This operation currently is only supported in Node.js.");
}

Expand Down Expand Up @@ -3994,7 +3996,7 @@ export class BlockBlobClient extends BlobClient {
options: BlockBlobParallelUploadOptions = {},
): Promise<BlobUploadCommonResponse> {
return tracingClient.withSpan("BlockBlobClient-uploadData", options, async (updatedOptions) => {
if (isNode) {
if (isNodeCompatible) {
let buffer: Buffer;
if (data instanceof Buffer) {
buffer = data;
Expand Down Expand Up @@ -4783,7 +4785,8 @@ export class PageBlobClient extends BlobClient {
url = urlOrConnectionString;
pipeline = credentialOrPipelineOrContainerName;
} else if (
(isNode && credentialOrPipelineOrContainerName instanceof StorageSharedKeyCredential) ||
(isNodeCompatible &&
credentialOrPipelineOrContainerName instanceof StorageSharedKeyCredential) ||
credentialOrPipelineOrContainerName instanceof AnonymousCredential ||
isTokenCredential(credentialOrPipelineOrContainerName)
) {
Expand Down Expand Up @@ -4811,7 +4814,7 @@ export class PageBlobClient extends BlobClient {

const extractedCreds = extractConnectionStringParts(urlOrConnectionString);
if (extractedCreds.kind === "AccountConnString") {
if (isNode) {
if (isNodeCompatible) {
const sharedKeyCredential = new StorageSharedKeyCredential(
extractedCreds.accountName!,
extractedCreds.accountKey,
Expand Down
7 changes: 4 additions & 3 deletions sdk/storage/storage-blob/src/ContainerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getDefaultProxySettings,
RequestBodyType as HttpRequestBody,
} from "@azure/core-rest-pipeline";
import { isNode } from "@azure/core-util";
import { isNodeCompatible } from "@azure/core-util";
import { TokenCredential, isTokenCredential } from "@azure/core-auth";
import { PagedAsyncIterableIterator, PageSettings } from "@azure/core-paging";
import { AnonymousCredential } from "./credentials/AnonymousCredential";
Expand Down Expand Up @@ -659,7 +659,8 @@ export class ContainerClient extends StorageClient {
url = urlOrConnectionString;
pipeline = credentialOrPipelineOrContainerName;
} else if (
(isNode && credentialOrPipelineOrContainerName instanceof StorageSharedKeyCredential) ||
(isNodeCompatible &&
credentialOrPipelineOrContainerName instanceof StorageSharedKeyCredential) ||
credentialOrPipelineOrContainerName instanceof AnonymousCredential ||
isTokenCredential(credentialOrPipelineOrContainerName)
) {
Expand All @@ -683,7 +684,7 @@ export class ContainerClient extends StorageClient {

const extractedCreds = extractConnectionStringParts(urlOrConnectionString);
if (extractedCreds.kind === "AccountConnString") {
if (isNode) {
if (isNodeCompatible) {
const sharedKeyCredential = new StorageSharedKeyCredential(
extractedCreds.accountName!,
extractedCreds.accountKey,
Expand Down
5 changes: 2 additions & 3 deletions sdk/storage/storage-blob/src/policies/StorageBrowserPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {
CompatResponse as HttpOperationResponse,
} from "@azure/core-http-compat";
import { BaseRequestPolicy } from "./RequestPolicy";
import { isNode } from "@azure/core-util";

import { isNodeCompatible } from "@azure/core-util";
import { HeaderConstants, URLConstants } from "../utils/constants";
import { setURLParameter } from "../utils/utils.common";

Expand Down Expand Up @@ -42,7 +41,7 @@ export class StorageBrowserPolicy extends BaseRequestPolicy {
* @param request -
*/
public async sendRequest(request: WebResource): Promise<HttpOperationResponse> {
if (isNode) {
if (isNodeCompatible) {
return this._nextPolicy.sendRequest(request);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
SendRequest,
PipelinePolicy,
} from "@azure/core-rest-pipeline";
import { isNode } from "@azure/core-util";
import { isNodeCompatible } from "@azure/core-util";
import { HeaderConstants, URLConstants } from "../utils/constants";
import { setURLParameter } from "../utils/utils.common";

Expand All @@ -24,7 +24,7 @@ export function storageBrowserPolicy(): PipelinePolicy {
return {
name: storageBrowserPolicyName,
async sendRequest(request: PipelineRequest, next: SendRequest): Promise<PipelineResponse> {
if (isNode) {
if (isNodeCompatible) {
return next(request);
}

Expand Down
7 changes: 3 additions & 4 deletions sdk/storage/storage-blob/src/utils/utils.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import { AbortSignalLike } from "@azure/abort-controller";
import { TokenCredential } from "@azure/core-auth";
import { HttpHeaders, createHttpHeaders } from "@azure/core-rest-pipeline";
import { isNode } from "@azure/core-util";

import { isNodeCompatible } from "@azure/core-util";
import {
BlobQueryArrowConfiguration,
BlobQueryCsvTextConfiguration,
Expand Down Expand Up @@ -461,7 +460,7 @@ export function truncatedISO8061Date(date: Date, withMilliseconds: boolean = tru
* @param content -
*/
export function base64encode(content: string): string {
return !isNode ? btoa(content) : Buffer.from(content).toString("base64");
return !isNodeCompatible ? btoa(content) : Buffer.from(content).toString("base64");
}

/**
Expand All @@ -470,7 +469,7 @@ export function base64encode(content: string): string {
* @param encodedString -
*/
export function base64decode(encodedString: string): string {
return !isNode ? atob(encodedString) : Buffer.from(encodedString, "base64").toString();
return !isNodeCompatible ? atob(encodedString) : Buffer.from(encodedString, "base64").toString();
}

/**
Expand Down
10 changes: 5 additions & 5 deletions sdk/storage/storage-blob/test/blobclient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import * as fs from "fs";
import { randomUUID } from "@azure/core-util";
import { AbortController } from "@azure/abort-controller";
import { isNode } from "@azure/core-util";
import { isNodeCompatible } from "@azure/core-util";
import { assert } from "@azure/test-utils";
import {
bodyToString,
Expand Down Expand Up @@ -311,7 +311,7 @@ describe("BlobClient", () => {
blobContentDisposition: "blobContentDisposition",
blobContentEncoding: "blobContentEncoding",
blobContentLanguage: "blobContentLanguage",
blobContentMD5: isNode ? Buffer.from([1, 2, 3, 4]) : new Uint8Array([1, 2, 3, 4]),
blobContentMD5: isNodeCompatible ? Buffer.from([1, 2, 3, 4]) : new Uint8Array([1, 2, 3, 4]),
blobContentType: "blobContentType",
};
await blobClient.setHTTPHeaders(headers);
Expand Down Expand Up @@ -706,7 +706,7 @@ describe("BlobClient", () => {
blobContentDisposition: "blobContentDisposition",
blobContentEncoding: "blobContentEncoding",
blobContentLanguage: "blobContentLanguage",
blobContentMD5: isNode ? Buffer.from([1, 2, 3, 4]) : new Uint8Array([1, 2, 3, 4]),
blobContentMD5: isNodeCompatible ? Buffer.from([1, 2, 3, 4]) : new Uint8Array([1, 2, 3, 4]),
blobContentType: "blobContentType",
};
await blobClient.setHTTPHeaders(headers, { customerProvidedKey: Test_CPK_INFO });
Expand Down Expand Up @@ -744,7 +744,7 @@ describe("BlobClient", () => {
});

it("beginCopyFromURL with rehydrate priority", async function () {
if (!isNode && !isLiveMode()) {
if (!isNodeCompatible && !isLiveMode()) {
this.skip();
}
const newBlobURL = containerClient.getBlobClient(
Expand Down Expand Up @@ -1551,7 +1551,7 @@ describe("BlobClient - Object Replication", () => {
});

it("download to file", async function (this: Context) {
if (!isNode || !isLiveMode()) {
if (!isNodeCompatible || !isLiveMode()) {
this.skip();
}
const srcDownloadedFilePath = recorder.variable(
Expand Down
Loading

0 comments on commit 14377bf

Please sign in to comment.