From 26c522176b983a18768706a278802623d1a6e7cc Mon Sep 17 00:00:00 2001 From: Alan Orth Date: Tue, 5 Aug 2025 15:07:46 +0300 Subject: [PATCH] Use node: for Node.js builtin modules Use the the `node:` protocol when importing Node.js builtin modules and enable rule in eslint to enforce. The `node:` protocol for Node.js builtin modules has been available since Node.js v14. See: https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/enforce-node-protocol-usage.md (cherry picked from commit af71c152c1e1244d13ef7117d7edf6191e47cb9f) --- .eslintrc.json | 4 ++++ cypress/plugins/index.ts | 2 +- lint/generate-docs.ts | 4 ++-- lint/src/util/theme-support.ts | 5 +++-- src/app/core/services/server-xhr.service.ts | 9 +++++---- src/backend/api.ts | 2 +- src/config/config.server.ts | 13 +++++++------ .../translate-server.loader.ts | 3 ++- 8 files changed, 25 insertions(+), 17 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index af2342d822..179876e682 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -253,6 +253,10 @@ "forceSingleLine": true } ], + "import/enforce-node-protocol-usage": [ + "error", + "always" + ], "unused-imports/no-unused-imports": "error", "lodash/import-scope": [ diff --git a/cypress/plugins/index.ts b/cypress/plugins/index.ts index 091f11d0f7..16aab015d7 100644 --- a/cypress/plugins/index.ts +++ b/cypress/plugins/index.ts @@ -1,4 +1,4 @@ -const fs = require('fs'); +const fs = require('node:fs'); // These two global variables are used to store information about the REST API used // by these e2e tests. They are filled out prior to running any tests in the before() diff --git a/lint/generate-docs.ts b/lint/generate-docs.ts index fb2bf53fb5..d49e33a44b 100644 --- a/lint/generate-docs.ts +++ b/lint/generate-docs.ts @@ -12,8 +12,8 @@ import { readFileSync, rmSync, writeFileSync, -} from 'fs'; -import { join } from 'path'; +} from 'node:fs'; +import { join } from 'node:path'; import { default as htmlPlugin } from './src/rules/html'; import { default as tsPlugin } from './src/rules/ts'; diff --git a/lint/src/util/theme-support.ts b/lint/src/util/theme-support.ts index 7bc680b930..5be6554020 100644 --- a/lint/src/util/theme-support.ts +++ b/lint/src/util/theme-support.ts @@ -6,10 +6,11 @@ * http://www.dspace.org/license/ */ +import { readFileSync } from 'node:fs'; +import { basename } from 'node:path'; + import { TSESTree } from '@typescript-eslint/utils'; import { RuleContext } from '@typescript-eslint/utils/ts-eslint'; -import { readFileSync } from 'fs'; -import { basename } from 'path'; import ts, { Identifier } from 'typescript'; import { diff --git a/src/app/core/services/server-xhr.service.ts b/src/app/core/services/server-xhr.service.ts index 4b58c6b127..af59172c7e 100644 --- a/src/app/core/services/server-xhr.service.ts +++ b/src/app/core/services/server-xhr.service.ts @@ -6,13 +6,14 @@ * http://www.dspace.org/license/ */ -import { XhrFactory } from '@angular/common'; -import { Injectable } from '@angular/core'; import { Agent as HttpAgent, AgentOptions as HttpAgentOptions, -} from 'http'; -import { Agent as HttpsAgent } from 'https'; +} from 'node:http'; +import { Agent as HttpsAgent } from 'node:https'; + +import { XhrFactory } from '@angular/common'; +import { Injectable } from '@angular/core'; import { prototype, XMLHttpRequest, diff --git a/src/backend/api.ts b/src/backend/api.ts index b84f06a960..de5ca29e98 100644 --- a/src/backend/api.ts +++ b/src/backend/api.ts @@ -8,7 +8,7 @@ import ITEMS from './data/items.json'; import { fakeDataBase } from './db'; const { Router } = require('express'); -const util = require('util'); +const util = require('node:util'); // you would use cookies/token etc const USER_ID = 'f9d98cf1-1b96-464e-8755-bcc2a5c09077'; // hardcoded as an example diff --git a/src/config/config.server.ts b/src/config/config.server.ts index 3cbce3a42b..4f5a989253 100644 --- a/src/config/config.server.ts +++ b/src/config/config.server.ts @@ -1,16 +1,17 @@ +import { + existsSync, + readFileSync, + writeFileSync, +} from 'node:fs'; +import { join } from 'node:path'; + import { blue, bold, green, red, } from 'colors'; -import { - existsSync, - readFileSync, - writeFileSync, -} from 'fs'; import { load } from 'js-yaml'; -import { join } from 'path'; import { isNotEmpty } from '../app/shared/empty.util'; import { AppConfig } from './app-config.interface'; diff --git a/src/ngx-translate-loaders/translate-server.loader.ts b/src/ngx-translate-loaders/translate-server.loader.ts index e17784ecfb..06312b3a2b 100644 --- a/src/ngx-translate-loaders/translate-server.loader.ts +++ b/src/ngx-translate-loaders/translate-server.loader.ts @@ -1,6 +1,7 @@ +import { readFileSync } from 'node:fs'; + import { TransferState } from '@angular/core'; import { TranslateLoader } from '@ngx-translate/core'; -import { readFileSync } from 'fs'; import { Observable, of,