Skip to content

Commit

Permalink
Merge pull request #2656 from cloudflare/yagiz/enable-more-eslint-lin…
Browse files Browse the repository at this point in the history
…ters

enable linters on more node.js implementations
  • Loading branch information
anonrig authored Sep 4, 2024
2 parents c9eca14 + 0825d71 commit 71dc488
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 40 deletions.
26 changes: 14 additions & 12 deletions src/node/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0
//
/* eslint-disable */

import { ERR_METHOD_NOT_IMPLEMENTED } from 'node-internal:internal_errors';

// eslint-disable-next-line @typescript-eslint/unbound-method
export const getRandomValues = crypto.getRandomValues;
export const subtle = crypto.subtle;
export const webcrypto = crypto;

export function timingSafeEqual(a: any, b: any) {
return (subtle as any).timingSafeEqual(a, b);
export function timingSafeEqual(
a: NodeJS.ArrayBufferView,
b: NodeJS.ArrayBufferView
): boolean {
return (subtle as any).timingSafeEqual(a, b); // eslint-disable-line
}

import {
Expand Down Expand Up @@ -124,22 +126,22 @@ export {
X509Certificate,
};

export function getCiphers() {
export function getCiphers(): string[] {
// prettier-ignore
return ["aes-128-cbc", "aes-192-cbc", "aes-256-cbc", "aes-128-ctr", "aes-192-ctr", "aes-256-ctr",
"aes-128-ecb", "aes-192-ecb", "aes-256-ecb", "aes-128-gcm", "aes-192-gcm", "aes-256-gcm",
"aes-128-ofb", "aes-192-ofb", "aes-256-ofb", "des-ecb", "des-ede", "des-ede-cbc", "rc2-cbc"];
}

export function getCurves() {
export function getCurves(): string[] {
// Hardcoded list of supported curves. Note that prime256v1 is equivalent to secp256r1, we follow
// OpenSSL's and bssl's nomenclature here.

// prettier-ignore
return ['secp224r1', 'prime256v1', 'secp384r1', 'secp521r1'];
}

export function getHashes() {
export function getHashes(): string[] {
// Hardcoded list of hashes supported in boringssl, node's approach looks pretty clunky. This is
// expected to change infrequently based of bssl's stability-focused approach.

Expand All @@ -150,7 +152,7 @@ export function getHashes() {
}

// We do not implement the openssl secure heap.
export function secureHeapUsed() {
export function secureHeapUsed(): Record<string, unknown> {
return {
total: 0,
used: 0,
Expand All @@ -160,18 +162,18 @@ export function secureHeapUsed() {
}

// We do not allow users to set the engine used.
export function setEngine(_1: string, _2?: number) {
export function setEngine(_1: string, _2?: number): void {
throw new ERR_METHOD_NOT_IMPLEMENTED('setEngine');
}

// We do not allow users to modify the FIPS enablement.
export function setFips(_: boolean) {
export function setFips(_: boolean): void {
throw new ERR_METHOD_NOT_IMPLEMENTED('setFips');
}

// We always run in FIPS mode.
export const fips = true;
export function getFips() {
export function getFips(): boolean {
return fips;
}

Expand Down Expand Up @@ -229,7 +231,7 @@ export default {
// Fips
getFips,
setFips,
get fips() {
get fips(): boolean {
return getFips();
},
set fips(_: boolean) {
Expand Down
73 changes: 52 additions & 21 deletions src/node/internal/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
// 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.
/* eslint-disable */
//import { codes } from "node-internal:error-codes";

// import { hideStackFrames } from "node-internal:hide-stack-frames";
import { isArrayBufferView } from 'node-internal:internal_types';
import { normalizeEncoding } from 'node-internal:internal_utils';
import {
Expand All @@ -40,10 +36,19 @@ import { default as bufferUtil } from 'node-internal:buffer';

// TODO(someday): Not current implementing parseFileMode, validatePort

export const isInt32 = (value: any) => value === (value | 0);
export const isUint32 = (value: any) => value === value >>> 0;
export function isInt32(value: unknown): value is number {
// @ts-expect-error Due to value being unknown
return value === (value | 0);
}
export function isUint32(value: unknown): value is number {
// @ts-expect-error Due to value being unknown
return value === value >>> 0;
}

export function validateBuffer(buffer: unknown, name = 'buffer') {
export function validateBuffer(
buffer: unknown,
name = 'buffer'
): asserts buffer is Buffer {
if (!isArrayBufferView(buffer)) {
throw new ERR_INVALID_ARG_TYPE(
name,
Expand All @@ -58,7 +63,7 @@ export function validateInteger(
name: string,
min = Number.MIN_SAFE_INTEGER,
max = Number.MAX_SAFE_INTEGER
) {
): asserts value is number {
if (typeof value !== 'number') {
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
}
Expand All @@ -80,7 +85,7 @@ export function validateObject(
value: unknown,
name: string,
options?: ValidateObjectOptions
) {
): asserts value is Record<string, unknown> {
const useDefaultOptions = options == null;
const allowArray = useDefaultOptions ? false : options.allowArray;
const allowFunction = useDefaultOptions ? false : options.allowFunction;
Expand All @@ -96,11 +101,11 @@ export function validateObject(
}

export function validateInt32(
value: any,
value: unknown,
name: string,
min = -2147483648,
max = 2147483647
) {
): asserts value is number {
if (!isInt32(value)) {
if (typeof value !== 'number') {
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
Expand All @@ -122,7 +127,7 @@ export function validateUint32(
value: unknown,
name: string,
positive?: boolean
) {
): asserts value is number {
if (!isUint32(value)) {
if (typeof value !== 'number') {
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
Expand All @@ -139,25 +144,38 @@ export function validateUint32(
}
}

export function validateString(value: unknown, name: string) {
export function validateString(
value: unknown,
name: string
): asserts value is string {
if (typeof value !== 'string') {
throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
}
}

export function validateNumber(value: unknown, name: string) {
export function validateNumber(
value: unknown,
name: string
): asserts value is number {
if (typeof value !== 'number') {
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
}
}

export function validateBoolean(value: unknown, name: string) {
export function validateBoolean(
value: unknown,
name: string
): asserts value is boolean {
if (typeof value !== 'boolean') {
throw new ERR_INVALID_ARG_TYPE(name, 'boolean', value);
}
}

export function validateOneOf(value: unknown, name: string, oneOf: any[]) {
export function validateOneOf(
value: unknown,
name: string,
oneOf: unknown[]
): void {
if (!Array.prototype.includes.call(oneOf, value)) {
const allowed = Array.prototype.join.call(
Array.prototype.map.call(oneOf, (v) =>
Expand All @@ -171,9 +189,12 @@ export function validateOneOf(value: unknown, name: string, oneOf: any[]) {
}
}

export function validateEncoding(data: unknown, encoding: string): void {
export function validateEncoding(
data: unknown,
encoding: string
): asserts data is string {
const normalizedEncoding = normalizeEncoding(encoding);
const length = (data as any).length;
const length = (data as any).length; // eslint-disable-line

if (normalizedEncoding === bufferUtil.HEX && length % 2 !== 0) {
throw new ERR_INVALID_ARG_VALUE(
Expand All @@ -184,7 +205,10 @@ export function validateEncoding(data: unknown, encoding: string): void {
}
}

export function validateAbortSignal(signal: unknown, name: string) {
export function validateAbortSignal(
signal: unknown,
name: string
): asserts signal is AbortSignal {
if (
signal !== undefined &&
(signal === null || typeof signal !== 'object' || !('aborted' in signal))
Expand All @@ -193,13 +217,20 @@ export function validateAbortSignal(signal: unknown, name: string) {
}
}

export function validateFunction(value: unknown, name: string) {
export function validateFunction(
value: unknown,
name: string
): asserts value is (...args: unknown[]) => unknown {
if (typeof value !== 'function') {
throw new ERR_INVALID_ARG_TYPE(name, 'Function', value);
}
}

export function validateArray(value: unknown, name: string, minLength = 0) {
export function validateArray(
value: unknown,
name: string,
minLength = 0
): asserts value is unknown[] {
if (!Array.isArray(value)) {
throw new ERR_INVALID_ARG_TYPE(name, 'Array', value);
}
Expand Down
4 changes: 1 addition & 3 deletions src/node/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0
//

/* eslint-disable */

/* eslint-disable @typescript-eslint/unbound-method */
import { posix, win32 } from 'node-internal:internal_path';

const {
Expand Down
1 change: 0 additions & 1 deletion src/node/stream/consumers.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

/* todo: the following is adopted code, enabling linting one day */
/* eslint-disable */
import { Buffer } from 'node-internal:internal_buffer';

export async function blob(stream) {
Expand Down
1 change: 0 additions & 1 deletion src/node/stream/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0
//
/* eslint-disable */
import { promises } from 'node-internal:streams_promises';
export * from 'node-internal:streams_promises';
export default promises;
1 change: 0 additions & 1 deletion src/node/stream/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0
//
/* eslint-disable */

export const ReadableStream = globalThis.ReadableStream;
export const ReadableStreamDefaultReader =
Expand Down
2 changes: 1 addition & 1 deletion src/node/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"noFallthroughCasesInSwitch": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
"noPropertyAccessFromIndexSignature": true,
"noPropertyAccessFromIndexSignature": false,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
Expand Down

0 comments on commit 71dc488

Please sign in to comment.