From b819f512986607afb8a3b1f8f8d6c42a61f0f134 Mon Sep 17 00:00:00 2001 From: Theo Gravity Date: Thu, 11 Apr 2024 17:29:02 -0700 Subject: [PATCH] fix(datadog-transport-common): vendor is-network-error --- .changeset/pretty-guests-attack.md | 7 +++ .../datadog-transport-common/package.json | 1 - .../src/vendor/is-network-error/index.d.ts | 26 +++++++++++ .../src/vendor/is-network-error/index.js | 33 ++++++++++++++ .../vendor/is-network-error/index.test-d.ts | 4 ++ .../src/vendor/is-network-error/license | 9 ++++ .../src/vendor/is-network-error/package.json | 43 +++++++++++++++++++ .../src/vendor/is-network-error/readme.md | 42 ++++++++++++++++++ .../src/vendor/is-network-error/test.js | 7 +++ .../src/vendor/p-retry/index.js | 2 +- .../src/vendor/p-retry/package.json | 1 - pnpm-lock.yaml | 8 ---- 12 files changed, 172 insertions(+), 11 deletions(-) create mode 100644 .changeset/pretty-guests-attack.md create mode 100644 packages/datadog-transport-common/src/vendor/is-network-error/index.d.ts create mode 100644 packages/datadog-transport-common/src/vendor/is-network-error/index.js create mode 100644 packages/datadog-transport-common/src/vendor/is-network-error/index.test-d.ts create mode 100644 packages/datadog-transport-common/src/vendor/is-network-error/license create mode 100644 packages/datadog-transport-common/src/vendor/is-network-error/package.json create mode 100644 packages/datadog-transport-common/src/vendor/is-network-error/readme.md create mode 100644 packages/datadog-transport-common/src/vendor/is-network-error/test.js diff --git a/.changeset/pretty-guests-attack.md b/.changeset/pretty-guests-attack.md new file mode 100644 index 0000000..1c37943 --- /dev/null +++ b/.changeset/pretty-guests-attack.md @@ -0,0 +1,7 @@ +--- +"datadog-transport-common": patch +"electron-log-transport-datadog": patch +"pino-datadog-transport": patch +--- + +Fix ERR_REQUIRE_ESM import error caused by is-network-error package by vendoring it. diff --git a/packages/datadog-transport-common/package.json b/packages/datadog-transport-common/package.json index 15702d9..509ae1b 100644 --- a/packages/datadog-transport-common/package.json +++ b/packages/datadog-transport-common/package.json @@ -44,7 +44,6 @@ }, "dependencies": { "@datadog/datadog-api-client": "^1.23.0", - "is-network-error": "^1.1.0", "retry": "^0.13.1" }, "devDependencies": { diff --git a/packages/datadog-transport-common/src/vendor/is-network-error/index.d.ts b/packages/datadog-transport-common/src/vendor/is-network-error/index.d.ts new file mode 100644 index 0000000..1107c81 --- /dev/null +++ b/packages/datadog-transport-common/src/vendor/is-network-error/index.d.ts @@ -0,0 +1,26 @@ +/** +Check if an error is a [Fetch network error](https://developer.mozilla.org/en-US/docs/Web/API/fetch#exceptions) + +@return Returns `true` if the given value is a Fetch network error, otherwise `false`. + +@example +``` +import isNetworkError from 'is-network-error'; + +async function getUnicorns() { + try { + const response = await fetch('unicorns.json'); + return await response.json(); + } catch (error) { + if (isNetworkError(error)) { + return localStorage.getItem('…'); + } + + throw error; + } +} + +console.log(await getUnicorns()); +``` +*/ +export default function isNetworkError(value: unknown): value is TypeError; diff --git a/packages/datadog-transport-common/src/vendor/is-network-error/index.js b/packages/datadog-transport-common/src/vendor/is-network-error/index.js new file mode 100644 index 0000000..1dbfc39 --- /dev/null +++ b/packages/datadog-transport-common/src/vendor/is-network-error/index.js @@ -0,0 +1,33 @@ +const objectToString = Object.prototype.toString; + +const isError = value => objectToString.call(value) === '[object Error]'; + +const errorMessages = new Set([ + 'network error', // Chrome + 'Failed to fetch', // Chrome + 'NetworkError when attempting to fetch resource.', // Firefox + 'The Internet connection appears to be offline.', // Safari 16 + 'Load failed', // Safari 17+ + 'Network request failed', // `cross-fetch` + 'fetch failed', // Undici (Node.js) + 'terminated', // Undici (Node.js) +]); + +export default function isNetworkError(error) { + const isValid = error + && isError(error) + && error.name === 'TypeError' + && typeof error.message === 'string'; + + if (!isValid) { + return false; + } + + // We do an extra check for Safari 17+ as it has a very generic error message. + // Network errors in Safari have no stack. + if (error.message === 'Load failed') { + return error.stack === undefined; + } + + return errorMessages.has(error.message); +} diff --git a/packages/datadog-transport-common/src/vendor/is-network-error/index.test-d.ts b/packages/datadog-transport-common/src/vendor/is-network-error/index.test-d.ts new file mode 100644 index 0000000..7fce939 --- /dev/null +++ b/packages/datadog-transport-common/src/vendor/is-network-error/index.test-d.ts @@ -0,0 +1,4 @@ +import {expectType} from 'tsd'; +import isNetworkError from './index.js'; + +expectType(isNetworkError(new TypeError('Failed to fetch'))); diff --git a/packages/datadog-transport-common/src/vendor/is-network-error/license b/packages/datadog-transport-common/src/vendor/is-network-error/license new file mode 100644 index 0000000..fa7ceba --- /dev/null +++ b/packages/datadog-transport-common/src/vendor/is-network-error/license @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) Sindre Sorhus (https://sindresorhus.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/packages/datadog-transport-common/src/vendor/is-network-error/package.json b/packages/datadog-transport-common/src/vendor/is-network-error/package.json new file mode 100644 index 0000000..dae52ea --- /dev/null +++ b/packages/datadog-transport-common/src/vendor/is-network-error/package.json @@ -0,0 +1,43 @@ +{ + "name": "is-network-error", + "version": "1.1.0", + "description": "Check if a value is a Fetch network error", + "license": "MIT", + "repository": "sindresorhus/is-network-error", + "funding": "https://github.com/sponsors/sindresorhus", + "author": { + "name": "Sindre Sorhus", + "email": "sindresorhus@gmail.com", + "url": "https://sindresorhus.com" + }, + "type": "module", + "exports": { + "types": "./index.d.ts", + "default": "./index.js" + }, + "sideEffects": false, + "engines": { + "node": ">=16" + }, + "scripts": { + "test": "xo && ava && tsd" + }, + "files": [ + "index.js", + "index.d.ts" + ], + "keywords": [ + "network", + "error", + "fetch", + "whatwg", + "detect", + "check", + "typeerror" + ], + "devDependencies": { + "ava": "^5.3.1", + "tsd": "^0.29.0", + "xo": "^0.56.0" + } +} diff --git a/packages/datadog-transport-common/src/vendor/is-network-error/readme.md b/packages/datadog-transport-common/src/vendor/is-network-error/readme.md new file mode 100644 index 0000000..507976d --- /dev/null +++ b/packages/datadog-transport-common/src/vendor/is-network-error/readme.md @@ -0,0 +1,42 @@ +# is-network-error + +> Check if a value is a [Fetch network error](https://developer.mozilla.org/en-US/docs/Web/API/fetch#exceptions) + +This can be useful when you want to do something specific when a network error happens without catching other Fetch-related errors. + +Unfortunately, Fetch network errors are [not standardized](https://github.com/whatwg/fetch/issues/526) and differ among implementations. This package handles the differences. + +For instance, [`p-retry`](https://github.com/sindresorhus/p-retry) uses this package to retry on network errors. + +## Install + +```sh +npm install is-network-error +``` + +## Usage + +```js +import isNetworkError from 'is-network-error'; + +async function getUnicorns() { + try { + const response = await fetch('unicorns.json'); + return await response.json(); + } catch (error) { + if (isNetworkError(error)) { + return localStorage.getItem('…'); + } + + throw error; + } +} + +console.log(await getUnicorns()); +``` + +## API + +### `isNetworkError(value: unknown): boolean` + +Returns `true` if the given value is a Fetch network error, otherwise `false`. diff --git a/packages/datadog-transport-common/src/vendor/is-network-error/test.js b/packages/datadog-transport-common/src/vendor/is-network-error/test.js new file mode 100644 index 0000000..d2ef053 --- /dev/null +++ b/packages/datadog-transport-common/src/vendor/is-network-error/test.js @@ -0,0 +1,7 @@ +import test from 'ava'; +import isNetworkError from './index.js'; + +test('main', async t => { + const error = await t.throwsAsync(fetch('https://asdfhsdflasudgfadsjyhgfjashgfaskjh.com')); + t.true(isNetworkError(error)); +}); diff --git a/packages/datadog-transport-common/src/vendor/p-retry/index.js b/packages/datadog-transport-common/src/vendor/p-retry/index.js index 3a2f2c6..6844744 100644 --- a/packages/datadog-transport-common/src/vendor/p-retry/index.js +++ b/packages/datadog-transport-common/src/vendor/p-retry/index.js @@ -1,5 +1,5 @@ import retry from 'retry'; -import isNetworkError from 'is-network-error'; +import isNetworkError from '../is-network-error'; export class AbortError extends Error { constructor(message) { diff --git a/packages/datadog-transport-common/src/vendor/p-retry/package.json b/packages/datadog-transport-common/src/vendor/p-retry/package.json index 5fc5e2b..3e8cbce 100644 --- a/packages/datadog-transport-common/src/vendor/p-retry/package.json +++ b/packages/datadog-transport-common/src/vendor/p-retry/package.json @@ -46,7 +46,6 @@ ], "dependencies": { "@types/retry": "0.12.2", - "is-network-error": "^1.0.0", "retry": "^0.13.1" }, "devDependencies": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index edd4a7a..d2545a8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -50,9 +50,6 @@ importers: '@datadog/datadog-api-client': specifier: ^1.23.0 version: 1.23.0 - is-network-error: - specifier: ^1.1.0 - version: 1.1.0 retry: specifier: ^0.13.1 version: 0.13.1 @@ -2768,11 +2765,6 @@ packages: engines: {node: '>= 0.4'} dev: true - /is-network-error@1.1.0: - resolution: {integrity: sha512-tUdRRAnhT+OtCZR/LxZelH/C7QtjtFrTu5tXCA8pl55eTUElUHT+GPYV8MBMBvea/j+NxQqVt3LbWMRir7Gx9g==} - engines: {node: '>=16'} - dev: false - /is-number-object@1.0.7: resolution: {integrity: sha512-k1U0IRzLMo7ZlYIfzRu23Oh6MiIFasgpb9X76eqfFZAqwH44UI4KTBvBYIZ1dSL9ZzChTB9ShHfLkR4pdW5krQ==} engines: {node: '>= 0.4'}