Skip to content

Commit

Permalink
Use cross-fetch instead of node-fetch (#498)
Browse files Browse the repository at this point in the history
* Use cross-fetch instead of node-fetch

This uses node-fetch under the hood but allows for cross-platform
support.

* License
  • Loading branch information
therve committed Jan 26, 2022
1 parent 2474e7f commit 06b557c
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .generator/templates/http/http.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export interface AbortSignal {

dispatchEvent: (event: any) => boolean;

onabort?: null | ((this: AbortSignal, event: any) => void);
onabort: null | ((this: AbortSignal, event: any) => void);
}

/**
Expand Down
12 changes: 10 additions & 2 deletions .generator/templates/http/isomorphic-fetch.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {HttpLibrary, RequestContext, ResponseContext} from './http{{extensionFor
import { from, Observable } from {{#useRxJS}}'rxjs'{{/useRxJS}}{{^useRxJS}}'../rxjsStub{{extensionForDeno}}'{{/useRxJS}};
{{#platforms}}
{{#node}}
import fetch from "node-fetch";
import fetch from "cross-fetch";
{{/node}}
{{#browser}}
import "whatwg-fetch";
Expand Down Expand Up @@ -31,11 +31,19 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
}
}

if (!headers["Accept-Encoding"]) {
if (compress) {
headers["Accept-Encoding"] = "gzip,deflate";
} else {
// We need to enforce it otherwise node-fetch will set a default
headers["Accept-Encoding"] = "identity";
}
}

const resultPromise = fetch(request.getUrl(), {
method: method,
body: body as any,
headers: headers,
compress: compress,
signal: request.getHttpConfig().signal,
{{#platforms}}
{{#browser}}
Expand Down
3 changes: 1 addition & 2 deletions LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Component,Licence,Copyright
@types/chai,MIT,Copyright Microsoft Corporation
@types/jest,MIT,Copyright Microsoft Corporation
@types/lodash,MIT,Copyright Microsoft Corporation
@types/node-fetch,MIT,Copyright Microsoft Corporation
@types/node,MIT,Copyright Microsoft Corporation
@types/pako,MIT,Copyright Caleb Eggensperger Muhammet Öztürk
@types/pollyjs__adapter-node-http,MIT,Copyright Microsoft Corporation
Expand All @@ -22,6 +21,7 @@ Component,Licence,Copyright
btoa,(MIT OR Apache-2.0),
buffer-from,MIT,Copyright (c) 2016, 2018 Linus Unnebäck
chai,MIT,Copyright (c) 2017 Chai.js Assertion Library
cross-fetch,MIT,Copyright (c) 2017 Leonardo Quixadá
jest,MIT,Copyright (c) Facebook, Inc. and its affiliates.
ts-jest,MIT,Copyright (c) 2016-2018
dd-trace,BSD-3-Clause,Copyright 2016-Present Datadog Inc.
Expand All @@ -32,7 +32,6 @@ eslint-plugin-node,MIT,Copyright (c) 2015 Toru Nagashima
eslint-plugin-unused-imports,MIT,Copyright (c) 2021 Mikkel Holmer
eslint,MIT,Copyright JS Foundation and other contributors, https://js.foundation
form-data,MIT,Copyright (c) 2012 Felix Geisendörfer (felix@debuggable.com) and contributors
node-fetch,MIT,Copyright (c) 2016 David Frank
pako,MIT,Copyright (C) 2014-2017 Vitaly Puzrin and Andrei Tuputcyn
prettier,MIT,Copyright © James Long and contributors
ts-node,MIT,Copyright (c) 2014 Blake Embrey (hello@blakeembrey.com)
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,14 @@
"dependencies": {
"@types/buffer-from": "^1.1.0",
"@types/node": "*",
"@types/node-fetch": "^2.5.7",
"@types/pako": "^1.0.3",
"btoa": "^1.2.1",
"buffer-from": "^1.1.2",
"durations": "^3.4.2",
"es6-promise": "^4.2.4",
"form-data": "^3.0.0",
"loglevel": "^1.7.1",
"node-fetch": "^2.6.0",
"cross-fetch": "^3.1.5",
"pako": "^2.0.4",
"url-parse": "^1.4.3"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/datadog-api-client-v1/http/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface AbortSignal {

dispatchEvent: (event: any) => boolean;

onabort?: null | ((this: AbortSignal, event: any) => void);
onabort: null | ((this: AbortSignal, event: any) => void);
}

/**
Expand Down
12 changes: 10 additions & 2 deletions packages/datadog-api-client-v1/http/isomorphic-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpLibrary, RequestContext, ResponseContext } from "./http";
import { from, Observable } from "../rxjsStub";
import fetch from "node-fetch";
import fetch from "cross-fetch";
import pako from "pako";
import bufferFrom from "buffer-from";

Expand All @@ -23,11 +23,19 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
}
}

if (!headers["Accept-Encoding"]) {
if (compress) {
headers["Accept-Encoding"] = "gzip,deflate";
} else {
// We need to enforce it otherwise node-fetch will set a default
headers["Accept-Encoding"] = "identity";
}
}

const resultPromise = fetch(request.getUrl(), {
method: method,
body: body as any,
headers: headers,
compress: compress,
signal: request.getHttpConfig().signal,
}).then((resp: any) => {
const headers: { [name: string]: string } = {};
Expand Down
2 changes: 1 addition & 1 deletion packages/datadog-api-client-v2/http/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface AbortSignal {

dispatchEvent: (event: any) => boolean;

onabort?: null | ((this: AbortSignal, event: any) => void);
onabort: null | ((this: AbortSignal, event: any) => void);
}

/**
Expand Down
12 changes: 10 additions & 2 deletions packages/datadog-api-client-v2/http/isomorphic-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpLibrary, RequestContext, ResponseContext } from "./http";
import { from, Observable } from "../rxjsStub";
import fetch from "node-fetch";
import fetch from "cross-fetch";
import pako from "pako";
import bufferFrom from "buffer-from";

Expand All @@ -23,11 +23,19 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
}
}

if (!headers["Accept-Encoding"]) {
if (compress) {
headers["Accept-Encoding"] = "gzip,deflate";
} else {
// We need to enforce it otherwise node-fetch will set a default
headers["Accept-Encoding"] = "identity";
}
}

const resultPromise = fetch(request.getUrl(), {
method: method,
body: body as any,
headers: headers,
compress: compress,
signal: request.getHttpConfig().signal,
}).then((resp: any) => {
const headers: { [name: string]: string } = {};
Expand Down
17 changes: 8 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -936,14 +936,6 @@
resolved "https://registry.npmjs.org/@types/long/-/long-4.0.1.tgz"
integrity sha512-5tXH6Bx/kNGd3MgffdmP4dy2Z+G4eaXw0SE81Tq3BNadtnMR5/ySMzX4SLEzHJzSmPNn4HIdpQsBvXMUykr58w==

"@types/node-fetch@^2.5.7":
version "2.5.8"
resolved "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.5.8.tgz"
integrity sha512-fbjI6ja0N5ZA8TV53RUqzsKNkl9fv8Oj3T7zxW7FGv1GSH7gwJaNF8dzCjrqKaxKeUpTz4yT1DaJFq/omNpGfw==
dependencies:
"@types/node" "*"
form-data "^3.0.0"

"@types/node@*", "@types/node@>=13.7.0":
version "14.14.31"
resolved "https://registry.npmjs.org/@types/node/-/node-14.14.31.tgz"
Expand Down Expand Up @@ -1745,6 +1737,13 @@ create-require@^1.1.0, create-require@^1.1.1:
resolved "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz"
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==

cross-fetch@^3.1.5:
version "3.1.5"
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.1.5.tgz#e1389f44d9e7ba767907f7af8454787952ab534f"
integrity sha512-lvb1SBsI0Z7GDwmuid+mU3kWVBwTVUbe7S0H52yaaAdQOXq2YktTCZdlAcNKFzE6QtRz0snpw9bNiPeOIkkQvw==
dependencies:
node-fetch "2.6.7"

cross-spawn@^7.0.2, cross-spawn@^7.0.3:
version "7.0.3"
resolved "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz"
Expand Down Expand Up @@ -3828,7 +3827,7 @@ nock@^13.1.3, nock@^13.2.1:
lodash.set "^4.3.2"
propagate "^2.0.0"

node-fetch@^2.6.0:
node-fetch@2.6.7:
version "2.6.7"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad"
integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==
Expand Down

0 comments on commit 06b557c

Please sign in to comment.