Skip to content

chore(NODE-7041): update dependencies and use standard Node team eslint configuration #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions .eslintrc

This file was deleted.

112 changes: 112 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{
"root": true,
"extends": [
"eslint:recommended",
"plugin:prettier/recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"env": {
"node": true,
"mocha": true,
"es6": true
},
"parserOptions": {
"ecmaVersion": 2019
},
"plugins": [
"@typescript-eslint",
"prettier"
],
"parser": "@typescript-eslint/parser",
"rules": {
"no-restricted-properties": [
"error",
{
"object": "describe",
"property": "only"
},
{
"object": "it",
"property": "only"
},
{
"object": "context",
"property": "only"
}
],
"prettier/prettier": "error",
"no-console": "error",
"valid-typeof": "error",
"eqeqeq": [
"error",
"always",
{
"null": "ignore"
}
],
"strict": [
"error",
"global"
],
"no-restricted-syntax": [
"error",
{
"selector": "TSEnumDeclaration",
"message": "Do not declare enums"
},
{
"selector": "BinaryExpression[operator=/[=!]==/] Identifier[name='undefined']",
"message": "Do not strictly check undefined"
},
{
"selector": "BinaryExpression[operator=/[=!]==/] Literal[raw='null']",
"message": "Do not strictly check null"
},
{
"selector": "BinaryExpression[operator=/[=!]==?/] Literal[value='undefined']",
"message": "Do not strictly check typeof undefined (NOTE: currently this rule only detects the usage of 'undefined' string literal so this could be a misfire)"
}
],
"@typescript-eslint/no-require-imports": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{
"argsIgnorePattern": "^_",
"caughtErrorsIgnorePattern": "^_",
"destructuredArrayIgnorePattern": "^_",
"varsIgnorePattern": "^_"
}
]
},
"overrides": [
{
"files": [
"lib/*.js"
],
"parserOptions": {
"ecmaVersion": 2019,
"sourceType": "commonjs"
}
},
{
"files": [
"test/**/*ts"
],
"rules": {
// chat `expect(..)` style chaining is considered
// an unused expression
"@typescript-eslint/no-unused-expressions": "off"
}
},
{
// json configuration files
"files": [
".*.json"
],
"rules": {
"@typescript-eslint/no-unused-expressions": "off"
}
}
]
}
2 changes: 2 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ updates:
versions: [">=16.0.0"]
# we ignore TS as a part of quarterly dependency updates.
- dependency-name: "typescript"
- dependency-name: "@types/node"
versions: [">=24.0.0"]
groups:
development-dependencies:
dependency-type: "development"
Expand Down
20 changes: 20 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
on: [push, pull_request]

name: CI
permissions:
contents: read

jobs:
test:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Use Node.js 22
uses: actions/setup-node@v4
with:
node-version: 22
- name: Install Dependencies
run: npm install
- name: Lint
run: npm run lint
7 changes: 7 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"singleQuote": true,
"tabWidth": 2,
"printWidth": 100,
"arrowParens": "avoid",
"trailingComma": "none"
}
17 changes: 8 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
".esm-wrapper.mjs"
],
"scripts": {
"lint": "eslint \"{src,test}/**/*.ts\"",
"test": "npm run lint && npm run build && nyc mocha --colors -r ts-node/register test/*.ts",
"lint": "ESLINT_USE_FLAT_CONFIG=false eslint \"{src,test}/**/*.ts\"",
"test": "npm run build && nyc mocha --colors -r ts-node/register test/*.ts",
"build": "npm run compile-ts && gen-esm-wrapper . ./.esm-wrapper.mjs",
"prepack": "npm run build",
"compile-ts": "tsc -p tsconfig.json"
Expand All @@ -39,16 +39,15 @@
"@types/chai": "^5.0.1",
"@types/mocha": "^10.0.10",
"@types/node": "^22.9.0",
"@typescript-eslint/eslint-plugin": "^4.2.0",
"@typescript-eslint/parser": "^4.2.0",
"@typescript-eslint/eslint-plugin": "^8.39.1",
"@typescript-eslint/parser": "^8.39.1",
"chai": "^4.2.0",
"eslint": "^7.9.0",
"eslint-config-semistandard": "^15.0.1",
"eslint-config-standard": "^14.1.1",
"eslint": "^9.33.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^7.1.0",
"eslint-plugin-standard": "^5.0.0",
"eslint-config-prettier": "^10.1.8",
"eslint-plugin-prettier": "^5.5.4",
"gen-esm-wrapper": "^1.1.3",
"mocha": "^11.0.1",
"nyc": "^15.1.0",
Expand All @@ -59,4 +58,4 @@
"@types/whatwg-url": "^13.0.0",
"whatwg-url": "^14.1.0 || ^13.0.0"
}
}
}
90 changes: 66 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { URL, URLSearchParams } from 'whatwg-url';
import {
redactValidConnectionString,
Expand All @@ -9,10 +10,7 @@ export { redactConnectionString, ConnectionStringRedactionOptions };
const DUMMY_HOSTNAME = '__this_is_a_placeholder__';

function connectionStringHasValidScheme(connectionString: string) {
return (
connectionString.startsWith('mongodb://') ||
connectionString.startsWith('mongodb+srv://')
);
return connectionString.startsWith('mongodb://') || connectionString.startsWith('mongodb+srv://');
}

// Adapted from the Node.js driver code:
Expand Down Expand Up @@ -130,7 +128,9 @@ export class ConnectionString extends URLWithoutHost {
constructor(uri: string, options: ConnectionStringParsingOptions = {}) {
const { looseValidation } = options;
if (!looseValidation && !connectionStringHasValidScheme(uri)) {
throw new MongoParseError('Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"');
throw new MongoParseError(
'Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"'
);
}

const match = uri.match(HOSTS_REGEX);
Expand Down Expand Up @@ -205,20 +205,39 @@ export class ConnectionString extends URLWithoutHost {
if (!this.pathname) {
this.pathname = '/';
}
Object.setPrototypeOf(this.searchParams, caseInsenstiveURLSearchParams(this.searchParams.constructor as any).prototype);
Object.setPrototypeOf(
this.searchParams,
caseInsenstiveURLSearchParams(this.searchParams.constructor as any).prototype
);
}

// The getters here should throw, but that would break .toString() because of
// https://github.com/nodejs/node/issues/36887. Using 'never' as the type
// should be enough to stop anybody from using them in TypeScript, though.
get host(): never { return DUMMY_HOSTNAME as never; }
set host(_ignored: never) { throw new Error('No single host for connection string'); }
get hostname(): never { return DUMMY_HOSTNAME as never; }
set hostname(_ignored: never) { throw new Error('No single host for connection string'); }
get port(): never { return '' as never; }
set port(_ignored: never) { throw new Error('No single host for connection string'); }
get href(): string { return this.toString(); }
set href(_ignored: string) { throw new Error('Cannot set href for connection strings'); }
get host(): never {
return DUMMY_HOSTNAME as never;
}
set host(_ignored: never) {
throw new Error('No single host for connection string');
}
get hostname(): never {
return DUMMY_HOSTNAME as never;
}
set hostname(_ignored: never) {
throw new Error('No single host for connection string');
}
get port(): never {
return '' as never;
}
set port(_ignored: never) {
throw new Error('No single host for connection string');
}
get href(): string {
return this.toString();
}
set href(_ignored: string) {
throw new Error('Cannot set href for connection strings');
}

get isSRV(): boolean {
return this.protocol.includes('srv');
Expand Down Expand Up @@ -246,34 +265,57 @@ export class ConnectionString extends URLWithoutHost {
return redactValidConnectionString(this, options);
}

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/ban-types
typedSearchParams<T extends {}>() {
const sametype = (false as true) && new (caseInsenstiveURLSearchParams<keyof T & string>(URLSearchParams))();
return this.searchParams as unknown as typeof sametype;
typedSearchParams<T extends Record<string, any>>() {
const _sametype =
(false as true) && new (caseInsenstiveURLSearchParams<keyof T & string>(URLSearchParams))();
return this.searchParams as unknown as typeof _sametype;
}

[Symbol.for('nodejs.util.inspect.custom')](): any {
const { href, origin, protocol, username, password, hosts, pathname, search, searchParams, hash } = this;
return { href, origin, protocol, username, password, hosts, pathname, search, searchParams, hash };
const {
href,
origin,
protocol,
username,
password,
hosts,
pathname,
search,
searchParams,
hash
} = this;
return {
href,
origin,
protocol,
username,
password,
hosts,
pathname,
search,
searchParams,
hash
};
}
}

/**
* Parses and serializes the format of the authMechanismProperties or
* readPreferenceTags connection string parameters.
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export class CommaAndColonSeparatedRecord<K extends {} = Record<string, unknown>> extends CaseInsensitiveMap<keyof K & string> {
export class CommaAndColonSeparatedRecord<
K extends Record<string, unknown> = Record<string, unknown>
> extends CaseInsensitiveMap<keyof K & string> {
constructor(from?: string | null) {
super();
for (const entry of (from ?? '').split(',')) {
if (!entry) continue;
const colonIndex = entry.indexOf(':');
// Use .set() to properly account for case insensitivity
if (colonIndex === -1) {
this.set(entry as (keyof K & string), '');
this.set(entry as keyof K & string, '');
} else {
this.set(entry.slice(0, colonIndex) as (keyof K & string), entry.slice(colonIndex + 1));
this.set(entry.slice(0, colonIndex) as keyof K & string, entry.slice(colonIndex + 1));
}
}
}
Expand Down
17 changes: 12 additions & 5 deletions src/redact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export interface ConnectionStringRedactionOptions {

export function redactValidConnectionString(
inputUrl: Readonly<ConnectionString>,
options?: ConnectionStringRedactionOptions): ConnectionString {
options?: ConnectionStringRedactionOptions
): ConnectionString {
const url = inputUrl.clone();
const replacementString = options?.replacementString ?? '_credentials_';
const redactUsernames = options?.redactUsernames ?? true;
Expand Down Expand Up @@ -39,19 +40,25 @@ export function redactValidConnectionString(

export function redactConnectionString(
uri: string,
options?: ConnectionStringRedactionOptions): string {
options?: ConnectionStringRedactionOptions
): string {
const replacementString = options?.replacementString ?? '<credentials>';
const redactUsernames = options?.redactUsernames ?? true;

let parsed: ConnectionString | undefined;
try {
parsed = new ConnectionString(uri);
} catch {}
} catch {
// squash errors
}
if (parsed) {
// If we can parse the connection string, use the more precise
// redaction logic.
options = { ...options, replacementString: '___credentials___' };
return parsed.redact(options).toString().replace(/___credentials___/g, replacementString);
return parsed
.redact(options)
.toString()
.replace(/___credentials___/g, replacementString);
}

// Note: The regexes here used to use lookbehind assertions, but we dropped that since
Expand All @@ -65,7 +72,7 @@ export function redactConnectionString(
// tlsCertificateKeyFilePassword query parameter
uri => uri.replace(/(tlsCertificateKeyFilePassword=)([^&]+)/gi, `$1${R}`),
// proxyUsername query parameter
uri => redactUsernames ? uri.replace(/(proxyUsername=)([^&]+)/gi, `$1${R}`) : uri,
uri => (redactUsernames ? uri.replace(/(proxyUsername=)([^&]+)/gi, `$1${R}`) : uri),
// proxyPassword query parameter
uri => uri.replace(/(proxyPassword=)([^&]+)/gi, `$1${R}`)
];
Expand Down
Loading
Loading