-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix Deno detected as a browser in core-rest-pipeline #27090
fix Deno detected as a browser in core-rest-pipeline #27090
Conversation
Thank you for your contribution @adamcohenhillel! We will review the pull request and get back to you soon. |
pipeline.addPolicy(tlsPolicy(options.tlsOptions)); | ||
} | ||
|
||
if (isNode || isDeno) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the problem is that Deno is reading the browser
key in package.json
and loading the proxyPolicy.browser.ts
version instead of the node version rather than this policy not being included. After all, if isNode
was false, the policy wouldn't be getting pulled in at all rather than loading in and then throwing an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this should be if (isNode && !isDeno)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mmmm, interesting - so you're saying isNode
is true in the Deno environment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So if it's false, how is the policy being loaded and throwing an exception for you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can tell, the exception is being thrown from the module proxyPolicy.browser.ts
and not the module proxyPolicy.ts
- which is the one that is being imported here in this file, and where proxyPolicy()
is declared.
It looks confusing, as both modules have proxyPolicy()
function. So I guess the other proxyPolicy is being used as the default
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but even if it's loading the browser version, it has to be called in order to throw, which means it has to be added to the pipeline. So if the existing check for isNode is false, how is it being added?
Is another dependency perhaps shimming out a process global object in your bundle?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So core-rest-pipeline
is not a direct import of my code. It's a dependency of @azure/openai@1.0.0-beta.4
- so maybe something in that package changes the global object..?
This is my full code:
import "https://deno.land/x/xhr@0.2.1/mod.ts";
import { serve } from "https://deno.land/std@0.170.0/http/server.ts";
import { OpenAIClient, AzureKeyCredential } from "https://esm.sh/@azure/openai@1.0.0-beta.4";
import { corsHeaders } from "../common/cors.ts";
const serviceFun = async (req: Request) => {
try {
if (req.method === "OPTIONS") {
return new Response("ok", { headers: corsHeaders });
}
const azureAPIKey = Deno.env.get("AZURE_OPENAI_API_KEY");
const openaiAzure = new OpenAIClient("https://XXX.openai.azure.com/", new AzureKeyCredential(azureAPIKey));
completion = await openaiAzure.getChatCompletions("deploymentName", [{ role: "system", content: prompt }]);
return new Response(JSON.stringify({ msg: completion.content }), {
status: 200,
headers: { ...corsHeaders, "Content-Type": "application/json" },
});
} catch (err: unknown) {
console.log("!!! Error: ", err);
}
};
serve(serviceFun);
I tried to look into how Azure openai sdk usescore-rest-pipeline
, and it is too lacking (see https://github.com/search?q=repo%3AAzure%2Fazure-sdk-for-js+core-rest-pipeline+path%3Aopenai&type=code) -
Honestly I don't know..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it relates to the package.json https://github.com/Azure/azure-sdk-for-js/blob/7b7614730c5154ad93ed3d3884dd86a19ace2891/sdk/core/core-rest-pipeline/package.json#L8C4-L8C11 and the definition on what
Hey guys, any workaround for this until its fixed? |
I looked into this a little more, the problem is that import { isNode } from "https://esm.sh/@azure/core-util@1.4.0/";
console.log(`isNode: ${isNode}`); It looks like Deno's standard library is spoofing itself as Node now: https://deno.land/std@0.177.0/node/process.ts?s=versions This is reminding me horribly of browsers doing User-Agent sniffing. Perhaps we need to make our isNode check have a !isDeno or something? @mpodwysocki |
Hey @xirzec - did you get around to doing that? happy to change my PR with your suggestions if not |
) ### Packages impacted by this PR `@azure/core-util` ### Issues associated with this PR Fixes #27077 ### Describe the problem that is addressed by this PR Deno implemented `process.versions.node`, which is how we detect that we are running inside a Node environment. Since Deno actually is much closer to a browser environment, this is *not* what was expected at runtime and resulted in things like loading the browser version of pipeline policies that throw due to not being implemented/relevant in browsers. I kept this really simple by making `isNode` check that `isDeno` is false. Since `isNode` was previously false inside Deno environments, I don't think this will break anything. ### Provide a list of related PRs _(if any)_ #27090
Closing this one since #27459 went in |
Packages impacted by this PR
core-rest-pipeline
Issues associated with this PR
#27077
Describe the problem that is addressed by this PR
Using Azure SDK in a Deno environment causes the following issue:
After investigating a little bit, The following PR might be the thing that triggered it: #26897 - however, it just revealed the underlying issue of - not adding the right policies to isDeno environments
Checklists