Skip to content

Commit

Permalink
chore: enable strict type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 10, 2023
1 parent 68a3429 commit 2ed5b67
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 65 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Expand Up @@ -20,6 +20,7 @@ jobs:
cache: "pnpm"
- run: pnpm install
- run: pnpm lint
- run: pnpm typecheck
- run: pnpm build
- run: pnpm vitest --coverage
- uses: codecov/codecov-action@v3
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -27,7 +27,8 @@
"prepack": "unbuild",
"release": "pnpm test && changelogen --release --push && pnpm publish",
"benchmark": "node benchmark/object-hash.mjs",
"test": "pnpm lint && vitest run"
"test": "pnpm lint && vitest run && pnpm typecheck",
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"@types/node": "^20.4.9",
Expand Down
27 changes: 11 additions & 16 deletions src/crypto/core.ts
Expand Up @@ -4,13 +4,13 @@ export class WordArray {
words: number[];
sigBytes: number;

constructor(words?, sigBytes?) {
constructor(words?: number[], sigBytes?: number) {
words = this.words = words || [];

this.sigBytes = sigBytes === undefined ? words.length * 4 : sigBytes;
}

toString(encoder?): string {
toString(encoder?: typeof Hex): string {
return (encoder || Hex).stringify(this);
}

Expand Down Expand Up @@ -86,12 +86,12 @@ export const Base64 = {
};

export const Latin1 = {
parse(latin1Str) {
parse(latin1Str: string) {
// Shortcut
const latin1StrLength = latin1Str.length;

// Convert
const words = [];
const words: number[] = [];
for (let i = 0; i < latin1StrLength; i++) {
words[i >>> 2] |= (latin1Str.charCodeAt(i) & 0xff) << (24 - (i % 4) * 8);
}
Expand All @@ -101,28 +101,23 @@ export const Latin1 = {
};

export const Utf8 = {
parse(utf8Str) {
parse(utf8Str: string) {
return Latin1.parse(unescape(encodeURIComponent(utf8Str)));
},
};

export class BufferedBlockAlgorithm {
_data: WordArray;
_nDataBytes: number;
_data = new WordArray();
_nDataBytes = 0;
_minBufferSize = 0;
blockSize = 512 / 32;

constructor() {
this.reset();
}

reset() {
// Initial values
this._data = new WordArray();
this._nDataBytes = 0;
}

_append(data) {
_append(data: string | WordArray) {
// Convert string to WordArray, else assume WordArray already
if (typeof data === "string") {
data = Utf8.parse(data);
Expand All @@ -134,7 +129,7 @@ export class BufferedBlockAlgorithm {
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
_doProcessBlock(_dataWords, _offset) {}
_doProcessBlock(_dataWords: any, _offset: any) {}

_process(doFlush?: boolean) {
let processedWords;
Expand Down Expand Up @@ -174,7 +169,7 @@ export class BufferedBlockAlgorithm {
}

export class Hasher extends BufferedBlockAlgorithm {
update(messageUpdate) {
update(messageUpdate: string) {
// Append
this._append(messageUpdate);

Expand All @@ -185,7 +180,7 @@ export class Hasher extends BufferedBlockAlgorithm {
return this;
}

finalize(messageUpdate) {
finalize(messageUpdate: string) {
// Final message update
if (messageUpdate) {
this._append(messageUpdate);
Expand Down
13 changes: 4 additions & 9 deletions src/crypto/sha256.ts
Expand Up @@ -24,25 +24,20 @@ const K = [
];

// Reusable object
const W = [];
const W: number[] = [];

/**
* SHA-256 hash algorithm.
*/
export class SHA256 extends Hasher {
_hash: WordArray;

constructor() {
super();
this.reset();
}
_hash = new WordArray([...H]);

reset() {
super.reset();
this._hash = new WordArray([...H]);
}

_doProcessBlock(M, offset) {
_doProcessBlock(M: number[], offset: number) {
// Shortcut
const H = this._hash.words;

Expand Down Expand Up @@ -112,7 +107,7 @@ export class SHA256 extends Hasher {
H[7] = (H[7] + h) | 0;
}

finalize(messageUpdate) {
finalize(messageUpdate: string): WordArray {
super.finalize(messageUpdate);

const nBitsTotal = this._nDataBytes * 8;
Expand Down
12 changes: 8 additions & 4 deletions src/diff.ts
Expand Up @@ -42,7 +42,11 @@ function _diff(
return diffs;
}

function _toHashedObject(obj, opts: HashOptions, key = ""): DiffHashedObject {
function _toHashedObject(
obj: any,
opts: HashOptions,
key = "",
): DiffHashedObject {
if (obj && typeof obj !== "object") {
return new DiffHashedObject(key, obj, objectHash(obj, opts));
}
Expand Down Expand Up @@ -83,9 +87,9 @@ export class DiffEntry {
return `[-] Removed ${this.key}`;
}
case "changed": {
return `[~] Changed ${
this.key
} from ${this.oldValue.toString()} to ${this.newValue.toString()}`;
return `[~] Changed ${this.key} from ${
this.oldValue?.toString() || "-"
} to ${this.newValue.toString()}`;
}
}
}
Expand Down
72 changes: 38 additions & 34 deletions src/object-hash.ts
Expand Up @@ -95,14 +95,14 @@ function createHasher(options: HashOptions) {
getContext() {
return context;
},
dispatch(value) {
dispatch(value: any): string | void {
if (options.replacer) {
value = options.replacer(value);
}
const type = value === null ? "null" : typeof value;
return this[type](value);
},
object(object) {
object(object: any): string | void {
if (object && typeof object.toJSON === "function") {
return this.object(object.toJSON());
}
Expand Down Expand Up @@ -144,7 +144,9 @@ function createHasher(options: HashOptions) {
objType !== "function" &&
objType !== "asyncfunction"
) {
// @ts-ignore
if (this[objType]) {
// @ts-ignore
this[objType](object);
} else if (!options.ignoreUnknown) {
this.unkown(object, objType);
Expand All @@ -163,16 +165,18 @@ function createHasher(options: HashOptions) {
}

if (options.excludeKeys) {
keys = keys.filter(function (key) {
return !options.excludeKeys(key);
keys = keys.filter((key) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return !options.excludeKeys!(key);
});
extraKeys = extraKeys.filter(function (key) {
return !options.excludeKeys(key);
extraKeys = extraKeys.filter((key) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return !options.excludeKeys!(key);
});
}

write("object:" + (keys.length + extraKeys.length) + ":");
const dispatchForKey = (key) => {
const dispatchForKey = (key: string) => {
this.dispatch(key);
write(":");
if (!options.excludeValues) {
Expand All @@ -188,7 +192,7 @@ function createHasher(options: HashOptions) {
}
}
},
array(arr, unordered) {
array(arr: any, unordered: boolean): string | void {
unordered =
unordered === undefined ? options.unorderedArrays !== false : unordered; // default to options.unorderedArrays

Expand All @@ -206,7 +210,7 @@ function createHasher(options: HashOptions) {
// also: we can’t use the same context for all entries since the order of hashing should *not* matter. instead,
// we keep track of the additions to a copy of the context and add all of them to the global context when we’re done
const contextAdditions = new Map();
const entries = arr.map((entry) => {
const entries = arr.map((entry: any) => {
const hasher = createHasher(options);
hasher.dispatch(entry);
for (const [key, value] of hasher.getContext()) {
Expand All @@ -218,10 +222,10 @@ function createHasher(options: HashOptions) {
entries.sort();
return this.array(entries, false);
},
date(date) {
date(date: any) {
return write("date:" + date.toJSON());
},
symbol(sym) {
symbol(sym: any) {
return write("symbol:" + sym.toString());
},
unkown(value: any, type: string) {
Expand All @@ -234,17 +238,17 @@ function createHasher(options: HashOptions) {
return this.array(Array.from(value.entries()), true /* ordered */);
}
},
error(err) {
error(err: any) {
return write("error:" + err.toString());
},
boolean(bool) {
boolean(bool: any) {
return write("bool:" + bool);
},
string(string) {
string(string: any) {
write("string:" + string.length + ":");
write(string);
},
function(fn) {
function(fn: any) {
write("fn:");
if (isNativeFunction(fn)) {
this.dispatch("[native]");
Expand All @@ -263,10 +267,10 @@ function createHasher(options: HashOptions) {
this.object(fn);
}
},
number(number) {
number(number: any) {
return write("number:" + number);
},
xml(xml) {
xml(xml: any) {
return write("xml:" + xml.toString());
},
null() {
Expand All @@ -275,63 +279,63 @@ function createHasher(options: HashOptions) {
undefined() {
return write("Undefined");
},
regexp(regex) {
regexp(regex: any) {
return write("regex:" + regex.toString());
},
uint8array(arr) {
uint8array(arr: any) {
write("uint8array:");
return this.dispatch(Array.prototype.slice.call(arr));
},
uint8clampedarray(arr) {
uint8clampedarray(arr: any) {
write("uint8clampedarray:");
return this.dispatch(Array.prototype.slice.call(arr));
},
int8array(arr) {
int8array(arr: any) {
write("int8array:");
return this.dispatch(Array.prototype.slice.call(arr));
},
uint16array(arr) {
uint16array(arr: any) {
write("uint16array:");
return this.dispatch(Array.prototype.slice.call(arr));
},
int16array(arr) {
int16array(arr: any) {
write("int16array:");
return this.dispatch(Array.prototype.slice.call(arr));
},
uint32array(arr) {
uint32array(arr: any) {
write("uint32array:");
return this.dispatch(Array.prototype.slice.call(arr));
},
int32array(arr) {
int32array(arr: any) {
write("int32array:");
return this.dispatch(Array.prototype.slice.call(arr));
},
float32array(arr) {
float32array(arr: any) {
write("float32array:");
return this.dispatch(Array.prototype.slice.call(arr));
},
float64array(arr) {
float64array(arr: any) {
write("float64array:");
return this.dispatch(Array.prototype.slice.call(arr));
},
arraybuffer(arr) {
arraybuffer(arr: any) {
write("arraybuffer:");
return this.dispatch(new Uint8Array(arr));
},
url(url) {
url(url: any) {
return write("url:" + url.toString());
},
map(map) {
map(map: any) {
write("map:");
const arr = [...map];
return this.array(arr, options.unorderedSets !== false);
},
set(set) {
set(set: any) {
write("set:");
const arr = [...set];
return this.array(arr, options.unorderedSets !== false);
},
file(file) {
file(file: any) {
write("file:");
return this.dispatch([file.name, file.size, file.type, file.lastModfied]);
},
Expand All @@ -347,7 +351,7 @@ function createHasher(options: HashOptions) {
domwindow() {
return write("domwindow");
},
bigint(number) {
bigint(number: number) {
return write("bigint:" + number.toString());
},
/* Node.js standard native objects */
Expand Down Expand Up @@ -409,7 +413,7 @@ const nativeFunc = "[native code] }";
const nativeFuncLength = nativeFunc.length;

/** Check if the given function is a native function */
function isNativeFunction(f) {
function isNativeFunction(f: any) {
if (typeof f !== "function") {
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Expand Up @@ -4,7 +4,8 @@
"module": "ESNext",
"moduleResolution": "Node",
"esModuleInterop": true,
"types": ["node"]
"types": ["node"],
"strict": true
},
"include": ["src"]
}

0 comments on commit 2ed5b67

Please sign in to comment.