Skip to content

Commit cf62bc5

Browse files
CHC383PetrHeinz
andauthored
refactor: remove dependency cross-fetch (#130)
Co-authored-by: Petr Heinz <petr@betterstack.com>
1 parent a8fe98a commit cf62bc5

File tree

10 files changed

+91
-43
lines changed

10 files changed

+91
-43
lines changed

jest.config.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
22
projects: [
33
{
4-
displayName: 'Node',
4+
displayName: "Node",
55
coverageDirectory: "coverage",
66
coveragePathIgnorePatterns: ["node_modules/", "dist/"],
77
moduleDirectories: ["node_modules"],
@@ -10,24 +10,25 @@ module.exports = {
1010
testMatch: ["<rootDir>/packages/**/*.test.ts"],
1111
testPathIgnorePatterns: ["/node_modules/", "/dist/", "/packages/browser/", "/packages/edge/"],
1212
transform: {
13-
"^.+\\.ts$": ["ts-jest", "tsconfig.json"]
13+
"^.+\\.ts$": ["ts-jest", "tsconfig.json"],
1414
},
1515
},
1616
{
17-
displayName: 'Browser',
17+
displayName: "Browser",
1818
coverageDirectory: "coverage",
1919
coveragePathIgnorePatterns: ["node_modules/", "dist/"],
2020
moduleDirectories: ["node_modules"],
2121
moduleFileExtensions: ["ts", "js"],
22-
testEnvironment: "jsdom",
22+
// https://mswjs.io/docs/migrations/1.x-to-2.x#requestresponsetextencoder-is-not-defined-jest
23+
testEnvironment: "jest-fixed-jsdom",
2324
testMatch: ["<rootDir>/packages/browser/**/*.test.ts"],
2425
testPathIgnorePatterns: ["/node_modules/", "/dist/"],
2526
transform: {
26-
"^.+\\.ts$": ["ts-jest", "tsconfig.json"]
27+
"^.+\\.ts$": ["ts-jest", "tsconfig.json"],
2728
},
2829
},
2930
{
30-
displayName: 'Edge',
31+
displayName: "Edge",
3132
coverageDirectory: "coverage",
3233
coveragePathIgnorePatterns: ["node_modules/", "dist/"],
3334
moduleDirectories: ["node_modules"],
@@ -36,8 +37,8 @@ module.exports = {
3637
testMatch: ["<rootDir>/packages/edge/**/*.test.ts"],
3738
testPathIgnorePatterns: ["/node_modules/", "/dist/"],
3839
transform: {
39-
"^.+\\.ts$": ["ts-jest", "tsconfig.json"]
40+
"^.+\\.ts$": ["ts-jest", "tsconfig.json"],
4041
},
4142
},
42-
]
43+
],
4344
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@types/node": "^20.14.9",
2323
"jest": "^29.7.0",
2424
"jest-environment-jsdom": "^29.7.0",
25+
"jest-fixed-jsdom": "^0.0.9",
2526
"lerna": "^8.1.5",
2627
"npm-run-all": "^4.1.5",
2728
"prettier": "^3.3.2",

packages/browser/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,14 @@
4242
"@logtail/types": "^0.5.3",
4343
"@types/nock": "^11.1.0",
4444
"@types/webpack": "^5.28.2",
45-
"nock": "^13.3.3",
45+
"nock": "^14.0.1",
4646
"ts-loader": "^9.5.1",
4747
"webpack": "^5.88.2",
4848
"webpack-cli": "^5.1.4"
4949
},
5050
"dependencies": {
5151
"@logtail/core": "^0.5.3",
52-
"@logtail/tools": "^0.5.3",
53-
"cross-fetch": "^4.0.0"
52+
"@logtail/tools": "^0.5.3"
5453
},
5554
"gitHead": "0f816cacc21b352576a5707741f9151aa1481041"
5655
}

packages/browser/src/browser.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import "cross-fetch/polyfill";
2-
31
import { Context, ILogLevel, ILogtailLog, ILogtailOptions } from "@logtail/types";
42
import { Base } from "@logtail/core";
53

packages/node/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,13 @@
3939
"devDependencies": {
4040
"@types/fetch-mock": "^7.3.1",
4141
"@types/nock": "^11.1.0",
42-
"nock": "^13.3.3"
42+
"nock": "^14.0.1"
4343
},
4444
"dependencies": {
4545
"@logtail/core": "^0.5.3",
4646
"@logtail/types": "^0.5.3",
4747
"@msgpack/msgpack": "^2.5.1",
4848
"@types/stack-trace": "^0.0.33",
49-
"cross-fetch": "^4.0.0",
5049
"minimatch": "^9.0.5",
5150
"stack-trace": "0.0.10"
5251
},

packages/node/src/node.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Duplex, Writable } from "stream";
22

33
import { encode } from "@msgpack/msgpack";
4-
import { fetch } from "cross-fetch";
54
import http from "node:http";
65
import https from "node:https";
76

@@ -24,23 +23,28 @@ export class Node extends Base {
2423

2524
// Sync function
2625
const sync = async (logs: ILogtailLog[]): Promise<ILogtailLog[]> => {
27-
const res = await fetch(this._options.endpoint, {
26+
const request = this.getHttpModule().request(this._options.endpoint, {
2827
method: "POST",
2928
headers: {
3029
"Content-Type": "application/msgpack",
3130
Authorization: `Bearer ${this._sourceToken}`,
3231
"User-Agent": "logtail-js(node)",
3332
},
34-
body: this.encodeAsMsgpack(logs),
35-
// @ts-ignore (cross-fetch does not expose the agent option)
3633
agent,
3734
});
3835

39-
if (res.ok) {
36+
const response: http.IncomingMessage = await new Promise((resolve, reject) => {
37+
request.on("response", resolve);
38+
request.on("error", reject);
39+
request.write(this.encodeAsMsgpack(logs));
40+
request.end();
41+
});
42+
43+
if (response.statusCode && response.statusCode >= 200 && response.statusCode < 300) {
4044
return logs;
4145
}
4246

43-
throw new Error(res.statusText);
47+
throw new Error(response.statusMessage);
4448
};
4549

4650
// Set the throttled sync function
@@ -94,14 +98,12 @@ export class Node extends Base {
9498
private createAgent() {
9599
const nodeOptions = this._options as ILogtailNodeOptions;
96100
const family = nodeOptions.useIPv6 ? 6 : 4;
97-
if (nodeOptions.endpoint.startsWith("https")) {
98-
return new https.Agent({
99-
family,
100-
});
101-
}
102-
103-
return new http.Agent({
101+
return new (this.getHttpModule().Agent)({
104102
family,
105103
});
106104
}
105+
106+
private getHttpModule(): typeof http | typeof https {
107+
return this._options.endpoint.startsWith("https") ? https : http;
108+
}
107109
}

packages/tools/package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@
3030
"devDependencies": {
3131
"@types/nock": "^11.1.0",
3232
"@types/source-map": "^0.5.7",
33-
"cross-fetch": "^4.0.0",
34-
"nock": "^13.3.3"
33+
"nock": "^14.0.1"
3534
},
3635
"dependencies": {
37-
"@logtail/types": "^0.5.3",
38-
"cross-fetch": "^4.0.0"
36+
"@logtail/types": "^0.5.3"
3937
},
4038
"gitHead": "0f816cacc21b352576a5707741f9151aa1481041"
4139
}

packages/tools/src/batch.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import nock from "nock";
2-
import fetch from "cross-fetch";
32
import { ILogtailLog, LogLevel } from "@logtail/types";
43
import makeBatch, { calculateJsonLogSizeBytes } from "./batch";
54
import makeThrottle from "./throttle";

packages/tools/src/retry.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { ILogtailLog, LogLevel } from "@logtail/types";
2-
import fetch from "cross-fetch";
32
import nock from "nock";
43
import makeRetry from "./retry";
54

yarn.lock

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,18 @@
735735
resolved "https://registry.yarnpkg.com/@msgpack/msgpack/-/msgpack-2.8.0.tgz#4210deb771ee3912964f14a15ddfb5ff877e70b9"
736736
integrity sha512-h9u4u/jiIRKbq25PM+zymTyW6bhTzELvOoUd+AvYriWOAKpLGnIamaET3pnHYoI5iYphAHBI4ayx0MehR+VVPQ==
737737

738+
"@mswjs/interceptors@^0.37.3":
739+
version "0.37.5"
740+
resolved "https://registry.yarnpkg.com/@mswjs/interceptors/-/interceptors-0.37.5.tgz#9ce40c56be02b43fcbdb51b63f47e69fc4aaabe6"
741+
integrity sha512-AAwRb5vXFcY4L+FvZ7LZusDuZ0vEe0Zm8ohn1FM6/X7A3bj4mqmkAcGRWuvC2JwSygNwHAAmMnAI73vPHeqsHA==
742+
dependencies:
743+
"@open-draft/deferred-promise" "^2.2.0"
744+
"@open-draft/logger" "^0.3.0"
745+
"@open-draft/until" "^2.0.0"
746+
is-node-process "^1.2.0"
747+
outvariant "^1.4.3"
748+
strict-event-emitter "^0.5.1"
749+
738750
"@nodelib/fs.scandir@2.1.5":
739751
version "2.1.5"
740752
resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5"
@@ -1108,6 +1120,24 @@
11081120
dependencies:
11091121
"@octokit/openapi-types" "^18.0.0"
11101122

1123+
"@open-draft/deferred-promise@^2.2.0":
1124+
version "2.2.0"
1125+
resolved "https://registry.yarnpkg.com/@open-draft/deferred-promise/-/deferred-promise-2.2.0.tgz#4a822d10f6f0e316be4d67b4d4f8c9a124b073bd"
1126+
integrity sha512-CecwLWx3rhxVQF6V4bAgPS5t+So2sTbPgAzafKkVizyi7tlwpcFpdFqq+wqF2OwNBmqFuu6tOyouTuxgpMfzmA==
1127+
1128+
"@open-draft/logger@^0.3.0":
1129+
version "0.3.0"
1130+
resolved "https://registry.yarnpkg.com/@open-draft/logger/-/logger-0.3.0.tgz#2b3ab1242b360aa0adb28b85f5d7da1c133a0954"
1131+
integrity sha512-X2g45fzhxH238HKO4xbSr7+wBS8Fvw6ixhTDuvLd5mqh6bJJCFAPwU9mPDxbcrRtfxv4u5IHCEH77BmxvXmmxQ==
1132+
dependencies:
1133+
is-node-process "^1.2.0"
1134+
outvariant "^1.4.0"
1135+
1136+
"@open-draft/until@^2.0.0":
1137+
version "2.1.0"
1138+
resolved "https://registry.yarnpkg.com/@open-draft/until/-/until-2.1.0.tgz#0acf32f470af2ceaf47f095cdecd40d68666efda"
1139+
integrity sha512-U69T3ItWHvLwGg5eJ0n3I62nWuE6ilHlmz7zM0npLBRvPRd7e6NYmg54vvRtP5mZG7kZqZCFVdsTWo7BPtBujg==
1140+
11111141
"@pkgjs/parseargs@^0.11.0":
11121142
version "0.11.0"
11131143
resolved "https://registry.yarnpkg.com/@pkgjs/parseargs/-/parseargs-0.11.0.tgz#a77ea742fab25775145434eb1d2328cf5013ac33"
@@ -2619,13 +2649,6 @@ create-require@^1.1.0:
26192649
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
26202650
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==
26212651

2622-
cross-fetch@^4.0.0:
2623-
version "4.0.0"
2624-
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-4.0.0.tgz#f037aef1580bb3a1a35164ea2a848ba81b445983"
2625-
integrity sha512-e4a5N8lVvuLgAWgnCrLr2PP0YyDOTHa9H/Rj54dirp61qXnNq46m82bRhNqIA5VccJtWBvPTFRV3TtvHUKPB1g==
2626-
dependencies:
2627-
node-fetch "^2.6.12"
2628-
26292652
cross-spawn@^6.0.5:
26302653
version "6.0.5"
26312654
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-6.0.5.tgz#4a5ec7c64dfae22c3a14124dbacdee846d80cbc4"
@@ -4060,6 +4083,11 @@ is-negative-zero@^2.0.3:
40604083
resolved "https://registry.yarnpkg.com/is-negative-zero/-/is-negative-zero-2.0.3.tgz#ced903a027aca6381b777a5743069d7376a49747"
40614084
integrity sha512-5KoIu2Ngpyek75jXodFvnafB6DJgr3u8uuK0LEZJjrU19DrMD3EVERaR8sjz8CCGgpZvxPl9SuE1GMVPFHx1mw==
40624085

4086+
is-node-process@^1.2.0:
4087+
version "1.2.0"
4088+
resolved "https://registry.yarnpkg.com/is-node-process/-/is-node-process-1.2.0.tgz#ea02a1b90ddb3934a19aea414e88edef7e11d134"
4089+
integrity sha512-Vg4o6/fqPxIjtxgUH5QLJhwZ7gW5diGCVlXpuUfELC62CuxM1iHcRe51f2W1FDy04Ai4KJkagKjx3XaqyfRKXw==
4090+
40634091
is-number-object@^1.0.4:
40644092
version "1.0.7"
40654093
resolved "https://registry.yarnpkg.com/is-number-object/-/is-number-object-1.0.7.tgz#59d50ada4c45251784e9904f5246c742f07a42fc"
@@ -4409,6 +4437,11 @@ jest-environment-node@^29.7.0:
44094437
jest-mock "^29.7.0"
44104438
jest-util "^29.7.0"
44114439

4440+
jest-fixed-jsdom@^0.0.9:
4441+
version "0.0.9"
4442+
resolved "https://registry.yarnpkg.com/jest-fixed-jsdom/-/jest-fixed-jsdom-0.0.9.tgz#12d594d3edfdd2ae09fd1f6c6c4e68baf4c4a1a7"
4443+
integrity sha512-KPfqh2+sn5q2B+7LZktwDcwhCpOpUSue8a1I+BcixWLOQoEVyAjAGfH+IYZGoxZsziNojoHGRTC8xRbB1wDD4g==
4444+
44124445
jest-get-type@^29.6.3:
44134446
version "29.6.3"
44144447
resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-29.6.3.tgz#36f499fdcea197c1045a127319c0481723908fd1"
@@ -5443,7 +5476,7 @@ nice-try@^1.0.4:
54435476
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366"
54445477
integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==
54455478

5446-
nock@*, nock@^13.3.3:
5479+
nock@*:
54475480
version "13.5.4"
54485481
resolved "https://registry.yarnpkg.com/nock/-/nock-13.5.4.tgz#8918f0addc70a63736170fef7106a9721e0dc479"
54495482
integrity sha512-yAyTfdeNJGGBFxWdzSKCBYxs5FxLbCg5X5Q4ets974hcQzG1+qCxvIyOo4j2Ry6MUlhWVMX4OoYDefAIIwupjw==
@@ -5452,14 +5485,23 @@ nock@*, nock@^13.3.3:
54525485
json-stringify-safe "^5.0.1"
54535486
propagate "^2.0.0"
54545487

5488+
nock@^14.0.1:
5489+
version "14.0.1"
5490+
resolved "https://registry.yarnpkg.com/nock/-/nock-14.0.1.tgz#62006248bbbc7637322c9fc73f90b93a431b4f5e"
5491+
integrity sha512-IJN4O9pturuRdn60NjQ7YkFt6Rwei7ZKaOwb1tvUIIqTgeD0SDDAX3vrqZD4wcXczeEy/AsUXxpGpP/yHqV7xg==
5492+
dependencies:
5493+
"@mswjs/interceptors" "^0.37.3"
5494+
json-stringify-safe "^5.0.1"
5495+
propagate "^2.0.0"
5496+
54555497
node-fetch@2.6.7:
54565498
version "2.6.7"
54575499
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad"
54585500
integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==
54595501
dependencies:
54605502
whatwg-url "^5.0.0"
54615503

5462-
node-fetch@^2.6.12, node-fetch@^2.6.7:
5504+
node-fetch@^2.6.7:
54635505
version "2.7.0"
54645506
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d"
54655507
integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==
@@ -5772,6 +5814,11 @@ os-tmpdir@~1.0.2:
57725814
resolved "https://registry.yarnpkg.com/os-tmpdir/-/os-tmpdir-1.0.2.tgz#bbe67406c79aa85c5cfec766fe5734555dfa1274"
57735815
integrity sha512-D2FR03Vir7FIu45XBY20mTb+/ZSWB00sjU9jdQXt83gDrI4Ztz5Fs7/yy74g2N5SVQY4xY1qDr4rNddwYRVX0g==
57745816

5817+
outvariant@^1.4.0, outvariant@^1.4.3:
5818+
version "1.4.3"
5819+
resolved "https://registry.yarnpkg.com/outvariant/-/outvariant-1.4.3.tgz#221c1bfc093e8fec7075497e7799fdbf43d14873"
5820+
integrity sha512-+Sl2UErvtsoajRDKCE5/dBz4DIvHXQQnAxtQTF04OJxY0+DyZXSo5P5Bb7XYWOh81syohlYL24hbDwxedPUJCA==
5821+
57755822
p-finally@^1.0.0:
57765823
version "1.0.0"
57775824
resolved "https://registry.yarnpkg.com/p-finally/-/p-finally-1.0.0.tgz#3fbcfb15b899a44123b34b6dcc18b724336a2cae"
@@ -6774,6 +6821,11 @@ statuses@2.0.1:
67746821
resolved "https://registry.yarnpkg.com/statuses/-/statuses-1.5.0.tgz#161c7dac177659fd9811f43771fa99381478628c"
67756822
integrity sha512-OpZ3zP+jT1PI7I8nemJX4AKmAX070ZkYPVWV/AaKTJl+tXCTGyVdC1a4SL8RUQYEwk/f34ZX8UTykN68FwrqAA==
67766823

6824+
strict-event-emitter@^0.5.1:
6825+
version "0.5.1"
6826+
resolved "https://registry.yarnpkg.com/strict-event-emitter/-/strict-event-emitter-0.5.1.tgz#1602ece81c51574ca39c6815e09f1a3e8550bd93"
6827+
integrity sha512-vMgjE/GGEPEFnhFub6pa4FmJBRBVOLpIII2hvCZ8Kzb7K0hlHo7mQv6xYrBvCL2LtAIBwFUK8wvuJgTVSQ5MFQ==
6828+
67776829
string-length@^4.0.1:
67786830
version "4.0.2"
67796831
resolved "https://registry.yarnpkg.com/string-length/-/string-length-4.0.2.tgz#a8a8dc7bd5c1a82b9b3c8b87e125f66871b6e57a"

0 commit comments

Comments
 (0)