Skip to content

Commit

Permalink
add some eslint rules, prettify, and prettier script locally
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLarkInn committed Dec 23, 2022
1 parent fd6ceac commit f2db90e
Show file tree
Hide file tree
Showing 15 changed files with 247 additions and 180 deletions.
13 changes: 11 additions & 2 deletions .eslintrc.json
@@ -1,7 +1,11 @@
{
"root": true,
"plugins": ["node", "@typescript-eslint"],
"extends": ["eslint:recommended", "plugin:node/recommended", "plugin:@typescript-eslint/recommended"],
"extends": [
"eslint:recommended",
"plugin:node/recommended",
"plugin:@typescript-eslint/recommended"
],
"env": {
"node": true
},
Expand All @@ -20,6 +24,11 @@
"prefer-const": "error",
"prefer-arrow-callback": "error",
"object-shorthand": "error",
"es-syntax": "false"
"node/no-unsupported-features/es-syntax": 0,
"node/no-missing-require": 0,
"node/no-missing-import": 0,
"node/no-unpublished-import": 0,
"@typescript-eslint/no-var-requires": 0,
"@typescript-eslint/no-explicit-any": 0
}
}
2 changes: 1 addition & 1 deletion .prettierignore
Expand Up @@ -2,4 +2,4 @@
/dist
/node_modules
/test/fixtures
CHANGELOG.md
CHANGELOG.md
52 changes: 38 additions & 14 deletions lib/getHashDigest.ts
@@ -1,4 +1,4 @@
import {Hash} from 'crypto';
import { Hash } from "crypto";

const baseEncodeTables = {
26: "abcdefghijklmnopqrstuvwxyz",
Expand All @@ -11,7 +11,15 @@ const baseEncodeTables = {
64: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_",
};

type DigestTypes = "base26" | "base32" | "base36" | "base49" | "base52" | "base58" | "base62" | "base64";
type DigestTypes =
| "base26"
| "base32"
| "base36"
| "base49"
| "base52"
| "base58"
| "base62"
| "base64";
type BaseEncodings = 26 | 32 | 36 | 49 | 52 | 58 | 62 | 64;

/**
Expand All @@ -29,8 +37,12 @@ function divmod32(uint32Array: Uint32Array, divisor: number): number {
return carry;
}

function encodeBufferToBase(buffer: Buffer, base: BaseEncodings | number, length: number) {
const encodeTable = baseEncodeTables[(base as keyof typeof baseEncodeTables)];
function encodeBufferToBase(
buffer: Buffer,
base: BaseEncodings | number,
length: number
) {
const encodeTable = baseEncodeTables[base as keyof typeof baseEncodeTables];

if (!encodeTable) {
throw new Error("Unknown encoding base" + base);
Expand All @@ -57,13 +69,18 @@ function encodeBufferToBase(buffer: Buffer, base: BaseEncodings | number, length
return output;
}

let crypto: typeof import('crypto')
let createXXHash64: typeof import('./hash/xxhash64').default;
let createMd4: typeof import('./hash/md4').default;
let BatchedHash: typeof import('./hash/BatchedHash').default;
let BulkUpdateDecorator: typeof import('./hash/BulkUpdateDecorator').default;

export default function getHashDigest(buffer: Buffer, algorithm: string | "xxhash64" | "md4" | "native-md4", digestType: DigestTypes | string, maxLength: number) {
let crypto: typeof import("crypto");
let createXXHash64: typeof import("./hash/xxhash64").default;
let createMd4: typeof import("./hash/md4").default;
let BatchedHash: typeof import("./hash/BatchedHash").default;
let BulkUpdateDecorator: typeof import("./hash/BulkUpdateDecorator").default;

export default function getHashDigest(
buffer: Buffer,
algorithm: string | "xxhash64" | "md4" | "native-md4",
digestType: DigestTypes | string,
maxLength: number
) {
algorithm = algorithm || "xxhash64";
maxLength = maxLength || 9999;

Expand Down Expand Up @@ -125,10 +142,17 @@ export default function getHashDigest(buffer: Buffer, algorithm: string | "xxhas
digestType === "base58" ||
digestType === "base62"
) {
const digestTypeToDigest: number = digestType.substr(4) as unknown as number;

return encodeBufferToBase(hash.digest() as Buffer, digestTypeToDigest, maxLength);
const digestTypeToDigest: number = digestType.substr(
4
) as unknown as number;

return encodeBufferToBase(
hash.digest() as Buffer,
digestTypeToDigest,
maxLength
);
} else {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return hash.digest(digestType || "hex").substr(0, maxLength);
}
Expand Down
5 changes: 2 additions & 3 deletions lib/hash/BatchedHash.ts
@@ -1,5 +1,5 @@
import type { Hash, Encoding, BinaryToTextEncoding } from "crypto";
import { MAX_SHORT_STRING } from './wasm-hash'
import { MAX_SHORT_STRING } from "./wasm-hash";

export default class BatchedHash {
public string?: string;
Expand Down Expand Up @@ -33,7 +33,7 @@ export default class BatchedHash {
if (this.encoding !== undefined) {
this.hash.update(this.string, this.encoding);
} else {
this.hash.update(this.string)
this.hash.update(this.string);
}

this.string = undefined;
Expand Down Expand Up @@ -73,7 +73,6 @@ export default class BatchedHash {
} else {
this.hash.update(this.string);
}

}
if (encoding !== undefined) {
return this.hash.digest(encoding);
Expand Down
10 changes: 6 additions & 4 deletions lib/hash/BulkUpdateDecorator.ts
@@ -1,20 +1,19 @@
import type { Hash, Encoding, BinaryToTextEncoding } from "crypto";
type HashOrFactory = Hash | (() => Hash);

const BULK_SIZE: number = 2000;
const BULK_SIZE = 2000;

// We are using an object instead of a Map as this will stay static during the runtime
// so access to it can be optimized by v8
const digestCaches: {[key: string]: any} = {};

const digestCaches: { [key: string]: any } = {};

export default class BulkUpdateDecorator {
/**
* @param {HashOrFactory} hashOrFactory function to create a hash
* @param {string=} hashKey key for caching
*/
hash?: Hash;
hashFactory?: (() => Hash);
hashFactory?: () => Hash;
hashKey: string;
buffer: string;

Expand Down Expand Up @@ -45,6 +44,7 @@ export default class BulkUpdateDecorator {
data.length > BULK_SIZE
) {
if (this.hash === undefined) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.hash = this.hashFactory!();
}

Expand All @@ -63,6 +63,7 @@ export default class BulkUpdateDecorator {

if (this.buffer.length > BULK_SIZE) {
if (this.hash === undefined) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.hash = this.hashFactory!();
}

Expand Down Expand Up @@ -101,6 +102,7 @@ export default class BulkUpdateDecorator {
return cacheEntry;
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.hash = this.hashFactory!();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/hash/md4.ts
Expand Up @@ -3,7 +3,7 @@
Author Tobias Koppers @sokra
*/

import { create } from './wasm-hash';
import { create } from "./wasm-hash";

//#region wasm code: md4 (../../../assembly/hash/md4.asm.ts) --initialMemory 1
const md4 = new WebAssembly.Module(
Expand Down
17 changes: 13 additions & 4 deletions lib/hash/wasm-hash.ts
@@ -1,4 +1,4 @@
import { Hash, BinaryToTextEncoding } from "crypto";
import { BinaryToTextEncoding } from "crypto";

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Expand All @@ -25,7 +25,12 @@ export class WasmHash {
chunkSize: number;
digestSize: number;

constructor(instance: WebAssembly.Instance, instancesPool: WebAssembly.Instance[], chunkSize: number, digestSize: number) {
constructor(
instance: WebAssembly.Instance,
instancesPool: WebAssembly.Instance[],
chunkSize: number,
digestSize: number
) {
const exports = instance.exports as any;

exports.init();
Expand Down Expand Up @@ -189,7 +194,12 @@ export class WasmHash {
}
}

export const create = (wasmModule: WebAssembly.Module, instancesPool: WasmHash[], chunkSize: number, digestSize: number) => {
export const create = (
wasmModule: WebAssembly.Module,
instancesPool: WasmHash[],
chunkSize: number,
digestSize: number
) => {
if (instancesPool.length > 0) {
const old = instancesPool.pop();

Expand All @@ -207,4 +217,3 @@ export const create = (wasmModule: WebAssembly.Module, instancesPool: WasmHash[]
);
}
};

2 changes: 1 addition & 1 deletion lib/hash/xxhash64.ts
Expand Up @@ -2,7 +2,7 @@
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
import { create } from './wasm-hash';
import { create } from "./wasm-hash";

//#region wasm code: xxhash64 (../../../assembly/hash/xxhash64.asm.ts) --initialMemory 1
const xxhash64 = new WebAssembly.Module(
Expand Down
7 changes: 1 addition & 6 deletions lib/index.ts
Expand Up @@ -3,9 +3,4 @@ import urlToRequest from "./urlToRequest";
import getHashDigest from "./getHashDigest";
import interpolateName from "./interpolateName";

export {
urlToRequest,
getHashDigest,
interpolateName,
isUrlRequest
}
export { urlToRequest, getHashDigest, interpolateName, isUrlRequest };
15 changes: 11 additions & 4 deletions lib/interpolateName.ts
@@ -1,18 +1,22 @@
import type { LoaderContext } from "webpack";
import path from "path";
import getHashDigest from './getHashDigest';
import getHashDigest from "./getHashDigest";

interface IInterpolateNameOptions {
content?: Buffer;
context?: string;
regExp?: string;
}

export default function interpolateName(loaderContext: LoaderContext<{}>, name: string | ((resourcePath: string, resourceQuery?: string) => string), options: IInterpolateNameOptions = {}) {
export default function interpolateName(
loaderContext: LoaderContext<object>,
name: string | ((resourcePath: string, resourceQuery?: string) => string),
options: IInterpolateNameOptions = {}
) {
let filename;

const hasQuery: boolean =
(loaderContext.resourceQuery && loaderContext.resourceQuery.length > 1) as boolean;
const hasQuery: boolean = (loaderContext.resourceQuery &&
loaderContext.resourceQuery.length > 1) as boolean;

if (typeof name === "function") {
filename = name(
Expand Down Expand Up @@ -104,13 +108,16 @@ export default function interpolateName(loaderContext: LoaderContext<{}>, name:
}

if (
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore LoaderContext doesn't even have options defined on it?
// If we chagned this to be `loaderContext.getOptions()` it would still not have
// the customInterpolateName function defined on it.
typeof loaderContext.options === "object" &&
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
typeof loaderContext.options.customInterpolateName === "function"
) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
url = loaderContext.options.customInterpolateName.call(
loaderContext,
Expand Down
17 changes: 9 additions & 8 deletions lib/isUrlRequest.ts
@@ -1,9 +1,9 @@
import path from 'path';
import path from "path";

const DATA_URI_REGEXP: RegExp = /^data:/i;
const ABOSLUTE_URL_NON_WINDOWS_PATHLIKE_REGEXP: RegExp = /^[a-z][a-z0-9+.-]*:/i;
const POROTCOL_RELATIVE_REGEXP: RegExp = /^\/\//i;
const URL_FOR_TEMPLATE_REGEXP: RegExp = /^#/i;
const DATA_URI_REGEXP = /^data:/i;
const ABOSLUTE_URL_NON_WINDOWS_PATHLIKE_REGEXP = /^[a-z][a-z0-9+.-]*:/i;
const POROTCOL_RELATIVE_REGEXP = /^\/\//i;
const URL_FOR_TEMPLATE_REGEXP = /^#/i;

export default function isUrlRequest(url: string): boolean {
// An URL is not an request if
Expand All @@ -14,7 +14,10 @@ export default function isUrlRequest(url: string): boolean {
}

// 2. It's an absolute url and it is not `windows` path like `C:\dir\file`
if (ABOSLUTE_URL_NON_WINDOWS_PATHLIKE_REGEXP.test(url) && !path.win32.isAbsolute(url)) {
if (
ABOSLUTE_URL_NON_WINDOWS_PATHLIKE_REGEXP.test(url) &&
!path.win32.isAbsolute(url)
) {
return false;
}

Expand All @@ -30,5 +33,3 @@ export default function isUrlRequest(url: string): boolean {

return true;
}


17 changes: 12 additions & 5 deletions lib/urlToRequest.ts
@@ -1,9 +1,12 @@
// we can't use path.win32.isAbsolute because it also matches paths starting with a forward slash
const NATIVE_WIN_32_PATH_REGEXP: RegExp = /^[A-Z]:[/\\]|^\\\\/i;
const MODULE_REQUEST_REGEXP: RegExp = /^[^?]*~/;
const ROOT_RELATIVE_URL_REGEXP: RegExp = /^\//;
const NATIVE_WIN_32_PATH_REGEXP = /^[A-Z]:[/\\]|^\\\\/i;
const MODULE_REQUEST_REGEXP = /^[^?]*~/;
const ROOT_RELATIVE_URL_REGEXP = /^\//;

export default function urlToRequest(url: string, root?: string | boolean): string {
export default function urlToRequest(
url: string,
root?: string | boolean
): string {
// Do not rewrite an empty url
if (url === "") {
return "";
Expand All @@ -14,7 +17,11 @@ export default function urlToRequest(url: string, root?: string | boolean): stri
if (NATIVE_WIN_32_PATH_REGEXP.test(url)) {
// absolute windows path, keep it
request = url;
} else if (root !== undefined && root !== false && ROOT_RELATIVE_URL_REGEXP.test(url)) {
} else if (
root !== undefined &&
root !== false &&
ROOT_RELATIVE_URL_REGEXP.test(url)
) {
// if root is set and the url is root-relative
switch (typeof root) {
// 1. root is a string: root is prefixed to the url
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -5,7 +5,8 @@
"description": "utils for webpack loaders",
"dependencies": {},
"scripts": {
"lint": "prettier --list-different . && eslint .",
"lint": "prettier --list-different . && eslint ./lib",
"pretty": "prettier --write .",
"build": "tsc",
"pretest": "yarn lint",
"test": "jest",
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Expand Up @@ -6,7 +6,7 @@
"outDir": "dist",
"strict": true,
"types": ["node"],
"esModuleInterop": true,
"esModuleInterop": true
},
"include": ["lib/**/*.ts"],
"include": ["lib/**/*.ts"]
}

0 comments on commit f2db90e

Please sign in to comment.