Skip to content

Commit

Permalink
fix: issue with promisify util
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniAkash committed Feb 28, 2024
1 parent d77df11 commit 70d5e78
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 222 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,8 @@ $RECYCLE.BIN/
# Windows shortcuts
*.lnk

# End of https://www.toptal.com/developers/gitignore/api/node,visualstudiocode,macos,windows,linux,intellij
# End of https://www.toptal.com/developers/gitignore/api/node,visualstudiocode,macos,windows,linux,intellij

# Build files
*.tgz
example/*
147 changes: 20 additions & 127 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"@types/node": "^20.11.16",
"@typescript-eslint/eslint-plugin": "^6.19.1",
"@typescript-eslint/parser": "^6.19.1",
"axios-mock-adapter": "^1.22.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
Expand All @@ -50,7 +49,8 @@
"typescript": "^5.3.3"
},
"dependencies": {
"axios": "^1.6.7",
"@grpc/grpc-js": "^1.10.1",
"@grpc/proto-loader": "^0.7.10",
"chalk": "^5.3.0",
"clarifai-nodejs-grpc": "^10.0.9",
"google-protobuf": "^3.21.2",
Expand Down
37 changes: 12 additions & 25 deletions src/client/auth/helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import axios from "axios";
import resources_pb2 from "clarifai-nodejs-grpc/proto/clarifai/api/resources_pb";
import { grpc } from "clarifai-nodejs-grpc";
import { V2Client } from "clarifai-nodejs-grpc/proto/clarifai/api/service_grpc_pb";
Expand Down Expand Up @@ -34,7 +33,7 @@ export function clearCache(): void {
Object.keys(uiHttpsCache).forEach((key) => delete uiHttpsCache[key]);
}

export async function httpsCache(cache: Cache, url: string): Promise<string> {
export function httpsCache(cache: Cache, url: string): string {
const HTTPS = true;
const HTTP = false;

Expand All @@ -45,27 +44,15 @@ export async function httpsCache(cache: Cache, url: string): Promise<string> {
url = url.replace("http://", "");
cache[url] = HTTP;
} else if (!(url in cache)) {
// Assuming HTTPS for any URLs without a scheme that end with .clarifai.com
const hostname = getHostnameFromUrl(url);
if (hostname && hostname.endsWith(".clarifai.com")) {
cache[url] = HTTPS;
} else {
try {
await axios.get(`https://${url}/v2/auth/methods`, { timeout: 1000 });
cache[url] = HTTPS;
} catch (error: unknown) {
if (error instanceof Error && error.message.includes("SSL")) {
cache[url] = HTTP;
if (!url.includes(":")) {
throw new Error(
"When providing an insecure URL, it must have both host:port format.",
);
}
} else {
throw new Error(
`Could not get a valid response from URL: ${url}, is the API running there?`,
);
}
}
// For URLs without a scheme and not ending with .clarifai.com, prompt user to provide the scheme
throw new Error(
`Please provide a valid scheme for the ${url}, either use http:// or https://`,
);
}
}
return url;
Expand Down Expand Up @@ -273,19 +260,19 @@ export class ClarifaiAuthHelper {
}

/**
* Asynchronously set the base domain for the API.
* set the base domain for the API.
* @param base - The base domain to set.
*/
async setBase(base: string): Promise<void> {
this._base = await httpsCache(baseHttpsCache, base);
setBase(base: string): void {
this._base = httpsCache(baseHttpsCache, base);
}

/**
* Asynchronously set the domain for the UI.
* set the domain for the UI.
* @param ui - The UI domain to set.
*/
async setUi(ui: string): Promise<void> {
this._ui = await httpsCache(uiHttpsCache, ui);
setUi(ui: string): void {
this._ui = httpsCache(uiHttpsCache, ui);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/client/auth/stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { V2Client } from "clarifai-nodejs-grpc/proto/clarifai/api/service_grpc_p
import { ClarifaiAuthHelper } from "./helper";
import { StatusCode } from "clarifai-nodejs-grpc/proto/clarifai/api/status/status_code_pb";
import { V2Stub } from "./register";
import * as grpc from "@grpc/grpc-js";
import { grpc } from "clarifai-nodejs-grpc";
import * as jspb from "google-protobuf";
import { Status } from "clarifai-nodejs-grpc/proto/clarifai/api/status/status_pb";

Expand Down
4 changes: 2 additions & 2 deletions src/client/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { V2Stub } from "./auth/register";
import { Timestamp } from "google-protobuf/google/protobuf/timestamp_pb";
import { KWArgs } from "../utils/types";
import * as jspb from "google-protobuf";
import * as grpc from "@grpc/grpc-js";
import { grpc } from "clarifai-nodejs-grpc";
import { Status } from "clarifai-nodejs-grpc/proto/clarifai/api/status/status_pb";

/**
Expand Down Expand Up @@ -44,7 +44,7 @@ export class BaseClient {
const pat = getFromDictOrEnv("pat", "CLARIFAI_PAT", kwargs);
kwargs.pat = pat;
this.authHelper =
Object.keys(kwargs).length === 0
Object.keys(kwargs).length > 0
? new ClarifaiAuthHelper(
kwargs.userId,
kwargs.appId,
Expand Down
2 changes: 1 addition & 1 deletion src/client/lister.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as grpc from "@grpc/grpc-js";
import { grpc } from "clarifai-nodejs-grpc";
import * as jspb from "google-protobuf";
import { KWArgs } from "../utils/types";
import { BaseClient } from "./base";
Expand Down
Loading

0 comments on commit 70d5e78

Please sign in to comment.