Skip to content

Commit 16a5b50

Browse files
authored
Remove node specific checks and unblock running in workers (#899)
* fix node env checking * remove node specific calls * only set accept-encoding in non browser envs * lint * remove os package usage
1 parent e25bb26 commit 16a5b50

File tree

11 files changed

+56
-31
lines changed

11 files changed

+56
-31
lines changed

.generator/src/generator/templates/common_index.j2

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ export { setServerVariables } from "./configuration"
66
export { Configuration } from "./configuration"
77
export * from "./exception";
88
export * from "./servers";
9-

.generator/src/generator/templates/configuration.j2

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { HttpLibrary, HttpConfiguration, RequestContext, ZstdCompressorCallback
22
import { IsomorphicFetchHttpLibrary as DefaultHttpLibrary } from "./http/isomorphic-fetch";
33
import { BaseServerConfiguration, server1, servers, operationServers } from "./servers";
44
import { configureAuthMethods, AuthMethods, AuthMethodsConfiguration } from "./auth";
5+
import { isNode } from "./util";
56

67
export interface Configuration {
78
readonly baseServer?: BaseServerConfiguration;
@@ -68,7 +69,7 @@ export interface ConfigurationParameters {
6869
* @param conf partial configuration
6970
*/
7071
export function createConfiguration(conf: ConfigurationParameters = {}): Configuration {
71-
if (process !== undefined && process.env.DD_SITE) {
72+
if (isNode && process.env.DD_SITE) {
7273
const serverConf = server1.getConfiguration();
7374
server1.setVariables({"site": process.env.DD_SITE} as (typeof serverConf));
7475
for (const op in operationServers) {
@@ -79,7 +80,7 @@ export function createConfiguration(conf: ConfigurationParameters = {}): Configu
7980
const authMethods = conf.authMethods || {};
8081
{%- for name, schema in openapi.components.securitySchemes.items() %}
8182
{%- if schema.get("type") == "apiKey" and schema.get("in") == "header" %}
82-
if (!("{{ name }}" in authMethods) && process !== undefined && process.env.{{ schema["x-env-name"] }}) {
83+
if (!("{{ name }}" in authMethods) && isNode && process.env.{{ schema["x-env-name"] }}) {
8384
authMethods["{{ name }}"] = process.env.{{ schema["x-env-name"] }};
8485
}
8586
{%- endif %}

.generator/src/generator/templates/http/http.j2

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { userAgent } from "../../../userAgent";
22
// TODO: evaluate if we can easily get rid of this library
33
import FormData from "form-data";
44
import URLParse from "url-parse";
5+
import { isBrowser } from "../util";
56

67
/**
78
* Interface for aborting fetch requests.
@@ -82,7 +83,7 @@ export interface HttpConfiguration {
8283
* Represents an HTTP request context
8384
*/
8485
export class RequestContext {
85-
private headers: { [key: string]: string } = { "user-agent": userAgent };
86+
private headers: { [key: string]: string } = {};
8687
private body: RequestBody = undefined;
8788
private url: URLParse;
8889
private httpConfig: HttpConfiguration = {};
@@ -95,6 +96,9 @@ export class RequestContext {
9596
*/
9697
public constructor(url: string, private httpMethod: HttpMethod) {
9798
this.url = new URLParse(url, true);
99+
if (!isBrowser) {
100+
this.headers = { "user-agent": userAgent };
101+
}
98102
}
99103

100104
/*

.generator/src/generator/templates/http/isomorphic-fetch.j2

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { HttpLibrary, RequestContext, ResponseContext, ZstdCompressorCallback }
22
import fetch from "cross-fetch";
33
import pako from "pako";
44
import bufferFrom from "buffer-from";
5+
import { isBrowser } from "../util";
56

67
export class IsomorphicFetchHttpLibrary implements HttpLibrary {
78
public debug = false;
@@ -20,6 +21,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
2021
}
2122

2223
const headers = request.getHeaders();
24+
2325
if (typeof body === "string") {
2426
if (headers["Content-Encoding"] == "gzip") {
2527
body = bufferFrom(pako.gzip(body).buffer);
@@ -34,12 +36,14 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
3436
}
3537
}
3638

37-
if (!headers["Accept-Encoding"]) {
38-
if (compress) {
39-
headers["Accept-Encoding"] = "gzip,deflate";
40-
} else {
41-
// We need to enforce it otherwise node-fetch will set a default
42-
headers["Accept-Encoding"] = "identity";
39+
if (!isBrowser) {
40+
if (!headers["Accept-Encoding"]) {
41+
if (compress) {
42+
headers["Accept-Encoding"] = "gzip,deflate";
43+
} else {
44+
// We need to enforce it otherwise node-fetch will set a default
45+
headers["Accept-Encoding"] = "identity";
46+
}
4347
}
4448
}
4549

.generator/src/generator/templates/util.j2

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ export type AttributeTypeMap = {
1414
format?: string;
1515
};
1616
}
17+
18+
export const isBrowser: boolean = typeof window !== "undefined" && typeof window.document !== "undefined";
19+
20+
export const isNode: boolean = typeof process !== "undefined" && process.release.name === 'node';

logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import log from "loglevel";
22

33
const logger = log.noConflict();
4-
logger.setLevel((process !== undefined && process.env.DEBUG) ? logger.levels.DEBUG : logger.levels.INFO);
4+
logger.setLevel((typeof process !== "undefined" && process.release.name === 'node' && process.env.DEBUG) ? logger.levels.DEBUG : logger.levels.INFO);
55

66
export { logger };

packages/datadog-api-client-common/configuration.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
AuthMethods,
1717
AuthMethodsConfiguration,
1818
} from "./auth";
19+
import { isNode } from "./util";
1920

2021
export interface Configuration {
2122
readonly baseServer?: BaseServerConfiguration;
@@ -83,7 +84,7 @@ export interface ConfigurationParameters {
8384
export function createConfiguration(
8485
conf: ConfigurationParameters = {}
8586
): Configuration {
86-
if (process !== undefined && process.env.DD_SITE) {
87+
if (isNode && process.env.DD_SITE) {
8788
const serverConf = server1.getConfiguration();
8889
server1.setVariables({ site: process.env.DD_SITE } as typeof serverConf);
8990
for (const op in operationServers) {
@@ -92,18 +93,10 @@ export function createConfiguration(
9293
}
9394

9495
const authMethods = conf.authMethods || {};
95-
if (
96-
!("apiKeyAuth" in authMethods) &&
97-
process !== undefined &&
98-
process.env.DD_API_KEY
99-
) {
96+
if (!("apiKeyAuth" in authMethods) && isNode && process.env.DD_API_KEY) {
10097
authMethods["apiKeyAuth"] = process.env.DD_API_KEY;
10198
}
102-
if (
103-
!("appKeyAuth" in authMethods) &&
104-
process !== undefined &&
105-
process.env.DD_APP_KEY
106-
) {
99+
if (!("appKeyAuth" in authMethods) && isNode && process.env.DD_APP_KEY) {
107100
authMethods["appKeyAuth"] = process.env.DD_APP_KEY;
108101
}
109102

packages/datadog-api-client-common/http/http.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { userAgent } from "../../../userAgent";
22
// TODO: evaluate if we can easily get rid of this library
33
import FormData from "form-data";
44
import URLParse from "url-parse";
5+
import { isBrowser } from "../util";
56

67
/**
78
* Interface for aborting fetch requests.
@@ -82,7 +83,7 @@ export interface HttpConfiguration {
8283
* Represents an HTTP request context
8384
*/
8485
export class RequestContext {
85-
private headers: { [key: string]: string } = { "user-agent": userAgent };
86+
private headers: { [key: string]: string } = {};
8687
private body: RequestBody = undefined;
8788
private url: URLParse;
8889
private httpConfig: HttpConfiguration = {};
@@ -95,6 +96,9 @@ export class RequestContext {
9596
*/
9697
public constructor(url: string, private httpMethod: HttpMethod) {
9798
this.url = new URLParse(url, true);
99+
if (!isBrowser) {
100+
this.headers = { "user-agent": userAgent };
101+
}
98102
}
99103

100104
/*

packages/datadog-api-client-common/http/isomorphic-fetch.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import fetch from "cross-fetch";
88
import pako from "pako";
99
import bufferFrom from "buffer-from";
10+
import { isBrowser } from "../util";
1011

1112
export class IsomorphicFetchHttpLibrary implements HttpLibrary {
1213
public debug = false;
@@ -25,6 +26,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
2526
}
2627

2728
const headers = request.getHeaders();
29+
2830
if (typeof body === "string") {
2931
if (headers["Content-Encoding"] == "gzip") {
3032
body = bufferFrom(pako.gzip(body).buffer);
@@ -39,12 +41,14 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
3941
}
4042
}
4143

42-
if (!headers["Accept-Encoding"]) {
43-
if (compress) {
44-
headers["Accept-Encoding"] = "gzip,deflate";
45-
} else {
46-
// We need to enforce it otherwise node-fetch will set a default
47-
headers["Accept-Encoding"] = "identity";
44+
if (!isBrowser) {
45+
if (!headers["Accept-Encoding"]) {
46+
if (compress) {
47+
headers["Accept-Encoding"] = "gzip,deflate";
48+
} else {
49+
// We need to enforce it otherwise node-fetch will set a default
50+
headers["Accept-Encoding"] = "identity";
51+
}
4852
}
4953
}
5054

packages/datadog-api-client-common/util.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@ export type AttributeTypeMap = {
1313
format?: string;
1414
};
1515
};
16+
17+
export const isBrowser: boolean =
18+
typeof window !== "undefined" && typeof window.document !== "undefined";
19+
20+
export const isNode: boolean =
21+
typeof process !== "undefined" && process.release.name === "node";

0 commit comments

Comments
 (0)