Skip to content

Commit

Permalink
Merge pull request #1 from theogravity/update-hook-retry
Browse files Browse the repository at this point in the history
fix(datadog-transport-common): update p-retry to 6.2.0, exit-hook to 4.0.0
  • Loading branch information
theogravity committed Apr 7, 2024
2 parents 3373ba7 + 17e4aac commit 2151bf3
Show file tree
Hide file tree
Showing 45 changed files with 1,798 additions and 52 deletions.
4 changes: 4 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx --no-install commitlint --edit "$1"
4 changes: 4 additions & 0 deletions .husky/post-merge
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

pnpm i
7 changes: 7 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

pnpm typecheck
pnpm lint:packages
pnpm test
pnpm lint-staged
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ This is a monorepo containing the following packages:
- [`pino-datadog-transport`](https://github.com/theogravity/datadog-transports/tree/main/packages/pino-datadog-transport): Send logs to datadog using `pino`

You can find information for them in their respective packages via the `README.md` file.

# Cloning / pulling this monorepo

This monorepo uses [`pnpm`](https://pnpm.io/) because [I couldn't
get workspaces to work properly](https://github.com/vercel/turbo/issues/7910#issuecomment-2041209008) under `npm` and `yarn`.
2 changes: 2 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"indentStyle": "space",
"lineWidth": 120,
"ignore": [
"vendor/*",
"node_modules/*",
"*.config.*",
"*.json",
Expand All @@ -17,6 +18,7 @@
},
"linter": {
"enabled": true,
"ignore": ["vendor/*"],
"rules": {
"performance": {
"noDelete": "off"
Expand Down
28 changes: 28 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'scope-enum': [2, 'always',
[
'all',
'datadog-transport-common',
'electron-log-transport-datadog',
'pino-datadog-transport'
]],
'type-enum': [
2,
'always',
[
'feat',
'fix',
'docs',
'chore',
'style',
'refactor',
'ci',
'test',
'revert',
'perf'
],
],
},
};
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@
"private": true,
"scripts": {
"add-changeset": "changeset add",
"build": "turbo build",
"build": "turbo run build",
"changeset": "changeset",
"clean": "git clean -xdf node_modules",
"clean:workspaces": "turbo clean",
"format": "turbo format --continue --",
"format:fix": "turbo format --continue -- --write",
"lint": "turbo lint --continue --",
"lint:fix": "turbo lint --continue -- --apply",
"format": "turbo run format --continue --",
"format:fix": "turbo run format --continue -- --write",
"lint": "turbo run lint --continue --",
"lint:fix": "turbo run lint --continue -- --apply",
"lint:packages": "pnpm run lint:packages:semver && pnpm run lint:packages:mismatches",
"lint:packages:semver": "syncpack lint-semver-ranges",
"lint:packages:mismatches": "syncpack list-mismatches",
"prepublishOnly": "pnpm run clean && pnpm run test && pnpm run lint && pnpm run lint:packages && pnpm run build",
"publish-packages": "turbo run build && changeset version && changeset publish",
"release": "changeset publish",
"syncpack": "syncpack",
"test": "turbo test",
"typecheck": "turbo typecheck",
"version-packages": "changeset version"
"test": "turbo run test --",
"typecheck": "turbo run typecheck",
"version-packages": "changeset version",
"prepare": "husky install"
},
"devDependencies": {
"@biomejs/biome": "^1.6.4",
Expand All @@ -29,7 +30,8 @@
"@commitlint/cli": "^19.2.1",
"@commitlint/config-conventional": "^19.1.0",
"@types/node": "^20.12.5",
"husky": "^9.0.11",
"husky": "^8.0.0",
"lint-staged": "^15.2.2",
"syncpack": "^12.3.0",
"tsconfig": "workspace:*",
"turbo": "^1.13.2",
Expand All @@ -39,9 +41,7 @@
"engines": {
"node": ">=18"
},
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
"lint-staged": {
"**/*.{js,jsx,ts,tsx,html,css,json}": ["pnpm lint:fix"]
}
}
5 changes: 3 additions & 2 deletions packages/datadog-transport-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@
},
"dependencies": {
"@datadog/datadog-api-client": "^1.23.0",
"exit-hook": "^2.2.1",
"p-retry": "^4.6.2"
"is-network-error": "^1.1.0",
"retry": "^0.13.1"
},
"devDependencies": {
"@types/retry": "^0.12.5",
"tsconfig": "workspace:*",
"tsup": "^8.0.2",
"vitest": "^1.4.0"
Expand Down
4 changes: 2 additions & 2 deletions packages/datadog-transport-common/src/DataDogTransport.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { HTTPLogItem } from "@datadog/datadog-api-client/dist/packages/datadog-api-client-v2/models/HTTPLogItem";
import type { LogsApiSubmitLogRequest } from "@datadog/datadog-api-client/dist/packages/datadog-api-client-v2/apis/LogsApi";
import { client, v2 } from "@datadog/datadog-api-client";
import pRetry from "p-retry";
import exitHook from "exit-hook";
import pRetry from "./vendor/p-retry";
import exitHook from "./vendor/exit-hook";
import { LogStorage } from "./LogStorage";
import type { DDTransportOptions, SendLogOpts } from "./types";

Expand Down
7 changes: 7 additions & 0 deletions packages/datadog-transport-common/src/vendor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
We're using a copy of

- [`exit-hook`](https://github.com/sindresorhus/exit-hook)
- [`p-retry'](https://github.com/sindresorhus/p-retry)

instead of using the package directly because newer versions are ESM-only
and this project supports both ESM and CJS.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.yml]
indent_style = space
indent_size = 2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github: sindresorhus
open_collective: sindresorhus
tidelift: npm/exit-hook
custom: https://sindresorhus.com/donate
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Security Policy

To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: CI
on:
- push
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 20
- 18
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import process from 'node:process';
import exitHook, {asyncExitHook, gracefulExit} from '../index.js';

exitHook(() => {
console.log('foo');
});

exitHook(() => {
console.log('bar');
});

const unsubscribe = exitHook(() => {
console.log('baz');
});

unsubscribe();

asyncExitHook(
async () => {
await new Promise(resolve => {
setTimeout(() => {
resolve();
}, 100);
});

console.log('quux');
},
{
wait: 200,
},
);

if (process.env.EXIT_HOOK_SYNC === '1') {
process.exit(0); // eslint-disable-line unicorn/no-process-exit
}

gracefulExit();
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import exitHook from '../index.js';

exitHook(() => {
// https://github.com/sindresorhus/exit-hook/issues/23
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import exitHook, {asyncExitHook} from '../index.js';

exitHook(signal => {
console.log(signal);
});

asyncExitHook(async signal => {
console.log(signal);
}, {
wait: 200,
});

setInterval(() => {}, 1e9);
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import process from 'node:process';
import exitHook from '../index.js';

exitHook(() => {
console.log('foo');
});

exitHook(() => {
console.log('bar');
});

const unsubscribe = exitHook(() => {
console.log('baz');
});

unsubscribe();

process.exit(0); // eslint-disable-line unicorn/no-process-exit
100 changes: 100 additions & 0 deletions packages/datadog-transport-common/src/vendor/exit-hook/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
@callback onExit
@param {number} signal - The exit code.
*/

/**
Run some code when the process exits.
The `process.on('exit')` event doesn't catch all the ways a process can exit.
This is useful for cleaning synchronously before exiting.
@param {onExit} onExit - The callback function to execute when the process exits.
@returns A function that removes the hook when called.
@example
```
import exitHook from 'exit-hook';
exitHook(signal => {
console.log(`Exiting with signal: ${signal}`);
});
// You can add multiple hooks, even across files
exitHook(() => {
console.log('Exiting 2');
});
throw new Error('🦄');
//=> 'Exiting'
//=> 'Exiting 2'
// Removing an exit hook:
const unsubscribe = exitHook(() => {});
unsubscribe();
```
*/
export default function exitHook(onExit: (signal: number) => void): () => void;

/**
Run code asynchronously when the process exits.
@see https://github.com/sindresorhus/exit-hook/blob/main/readme.md#asynchronous-exit-notes
@param {onExit} onExit - The callback function to execute when the process exits via `gracefulExit`, and will be wrapped in `Promise.resolve`.
@returns A function that removes the hook when called.
@example
```
import {asyncExitHook} from 'exit-hook';
asyncExitHook(() => {
console.log('Exiting');
}, {
wait: 500
});
throw new Error('🦄');
//=> 'Exiting'
// Removing an exit hook:
const unsubscribe = asyncExitHook(() => {}, {wait: 500});
unsubscribe();
```
*/
export function asyncExitHook(onExit: (signal: number) => (void | Promise<void>), options: Options): () => void;

/**
Exit the process and make a best-effort to complete all asynchronous hooks.
If you are using `asyncExitHook`, consider using `gracefulExit()` instead of `process.exit()` to ensure all asynchronous tasks are given an opportunity to run.
@param signal - The exit code to use. Same as the argument to `process.exit()`.
@see https://github.com/sindresorhus/exit-hook/blob/main/readme.md#asynchronous-exit-notes
@example
```
import {asyncExitHook, gracefulExit} from 'exit-hook';
asyncExitHook(() => {
console.log('Exiting');
}, {
wait: 500
});
// Instead of `process.exit()`
gracefulExit();
```
*/
export function gracefulExit(signal?: number): void;

export type Options = {
/**
The amount of time in milliseconds that the `onExit` function is expected to take. When multiple async handlers are registered, the longest `wait` time will be used.
*/
readonly wait: number;
};
Loading

0 comments on commit 2151bf3

Please sign in to comment.