Skip to content

Commit

Permalink
refactor(types): more
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jun 14, 2023
1 parent fc703e2 commit 568f28b
Show file tree
Hide file tree
Showing 24 changed files with 209 additions and 74 deletions.
1 change: 1 addition & 0 deletions lib/json/JsonGenerator.js
Expand Up @@ -172,6 +172,7 @@ class JsonGenerator extends Generator {
);
}
const exportsInfo = moduleGraph.getExportsInfo(module);
console.log(exportsInfo);
/** @type {RawJsonData} */
let finalJson =
typeof data === "object" &&
Expand Down
9 changes: 6 additions & 3 deletions lib/serialization/ObjectMiddleware.js
Expand Up @@ -107,7 +107,7 @@ const ESCAPE_UNDEFINED = false;

const CURRENT_VERSION = 2;

/** @type {Map<Constructor, { request?: string, name?: string | number , serializer?: ObjectSerializer }>} */
/** @type {Map<Constructor, { request?: string, name?: string | number | null, serializer?: ObjectSerializer }>} */
const serializers = new Map();
/** @type {Map<string | number, ObjectSerializer>} */
const serializerInversed = new Map();
Expand Down Expand Up @@ -158,7 +158,10 @@ if (exports.constructor !== Object) {
}

for (const { request, name, serializer } of serializers.values()) {
serializerInversed.set(`${request}/${name}`, serializer);
serializerInversed.set(
`${request}/${name}`,
/** @type {ObjectSerializer} */ (serializer)
);
}

/** @type {Map<RegExp, (request: string) => boolean>} */
Expand Down Expand Up @@ -191,7 +194,7 @@ class ObjectMiddleware extends SerializerMiddleware {
/**
* @param {Constructor} Constructor the constructor
* @param {string} request the request which will be required when deserializing
* @param {string} name the name to make multiple serializer unique when sharing a request
* @param {string | null} name the name to make multiple serializer unique when sharing a request
* @param {ObjectSerializer} serializer the serializer
* @returns {void}
*/
Expand Down
6 changes: 4 additions & 2 deletions lib/util/AsyncQueue.js
Expand Up @@ -70,7 +70,7 @@ class AsyncQueue {
this._entries = new Map();
/** @type {ArrayQueue<AsyncQueueEntry<T, K, R>>} */
this._queued = new ArrayQueue();
/** @type {AsyncQueue<any, any, any>[]} */
/** @type {AsyncQueue<any, any, any>[] | undefined} */
this._children = undefined;
this._activeTasks = 0;
this._willEnsureProcessing = false;
Expand Down Expand Up @@ -159,7 +159,9 @@ class AsyncQueue {
*/
invalidate(item) {
const key = this._getKey(item);
const entry = this._entries.get(key);
const entry =
/** @type {AsyncQueueEntry<T, K, R>} */
(this._entries.get(key));
this._entries.delete(key);
if (entry.state === QUEUED_STATE) {
this._queued.delete(entry);
Expand Down
10 changes: 10 additions & 0 deletions lib/util/ParallelismFactorCalculator.js
Expand Up @@ -7,12 +7,22 @@

const binarySearchBounds = require("../util/binarySearchBounds");

/** @typedef {function(number): void} Callback */

class ParallelismFactorCalculator {
constructor() {
/** @type {number[]} */
this._rangePoints = [];
/** @type {Callback[]} */
this._rangeCallbacks = [];
}

/**
* @param {number} start range start
* @param {number} end range end
* @param {Callback} callback callback
* @returns {void}
*/
range(start, end, callback) {
if (start === end) return callback(1);
this._rangePoints.push(start);
Expand Down
2 changes: 1 addition & 1 deletion lib/util/Semaphore.js
Expand Up @@ -44,7 +44,7 @@ class Semaphore {
if (this.available > 0) {
if (this.waiters.length > 0) {
this.available--;
const callback = this.waiters.pop();
const callback = /** @type {(function(): void)} */ (this.waiters.pop());
callback();
}
}
Expand Down
39 changes: 30 additions & 9 deletions lib/util/createHash.js
Expand Up @@ -11,11 +11,14 @@ 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
/** @type {Object<string, Map<string, string>>} */
const digestCaches = {};

/** @typedef {function(): Hash} HashFactory */

class BulkUpdateDecorator extends Hash {
/**
* @param {Hash | function(): Hash} hashOrFactory function to create a hash
* @param {Hash | HashFactory} hashOrFactory function to create a hash
* @param {string=} hashKey key for caching
*/
constructor(hashOrFactory, hashKey) {
Expand Down Expand Up @@ -43,7 +46,8 @@ class BulkUpdateDecorator extends Hash {
typeof data !== "string" ||
data.length > BULK_SIZE
) {
if (this.hash === undefined) this.hash = this.hashFactory();
if (this.hash === undefined)
this.hash = /** @type {HashFactory} */ (this.hashFactory)();
if (this.buffer.length > 0) {
this.hash.update(this.buffer);
this.buffer = "";
Expand All @@ -52,7 +56,8 @@ class BulkUpdateDecorator extends Hash {
} else {
this.buffer += data;
if (this.buffer.length > BULK_SIZE) {
if (this.hash === undefined) this.hash = this.hashFactory();
if (this.hash === undefined)
this.hash = /** @type {HashFactory} */ (this.hashFactory)();
this.hash.update(this.buffer);
this.buffer = "";
}
Expand All @@ -77,7 +82,7 @@ class BulkUpdateDecorator extends Hash {
}
const cacheEntry = digestCache.get(buffer);
if (cacheEntry !== undefined) return cacheEntry;
this.hash = this.hashFactory();
this.hash = /** @type {HashFactory} */ (this.hashFactory)();
}
if (buffer.length > 0) {
this.hash.update(buffer);
Expand Down Expand Up @@ -111,7 +116,9 @@ class DebugHash extends Hash {
if (data.startsWith(prefix)) {
data = Buffer.from(data.slice(prefix.length), "hex").toString();
}
this.string += `[${data}](${new Error().stack.split("\n", 3)[2]})\n`;
this.string += `[${data}](${
/** @type {string} */ (new Error().stack).split("\n", 3)[2]
})\n`;
return this;
}

Expand All @@ -125,9 +132,13 @@ class DebugHash extends Hash {
}
}

/** @type {typeof import("crypto") | undefined} */
let crypto = undefined;
/** @type {typeof import("./hash/xxhash64") | undefined} */
let createXXHash64 = undefined;
/** @type {typeof import("./hash/md4") | undefined} */
let createMd4 = undefined;
/** @type {typeof import("./hash/BatchedHash") | undefined} */
let BatchedHash = undefined;

/**
Expand All @@ -150,22 +161,32 @@ module.exports = algorithm => {
BatchedHash = require("./hash/BatchedHash");
}
}
return new BatchedHash(createXXHash64());
return new /** @type {typeof import("./hash/BatchedHash")} */ (
BatchedHash
)(createXXHash64());
case "md4":
if (createMd4 === undefined) {
createMd4 = require("./hash/md4");
if (BatchedHash === undefined) {
BatchedHash = require("./hash/BatchedHash");
}
}
return new BatchedHash(createMd4());
return new /** @type {typeof import("./hash/BatchedHash")} */ (
BatchedHash
)(createMd4());
case "native-md4":
if (crypto === undefined) crypto = require("crypto");
return new BulkUpdateDecorator(() => crypto.createHash("md4"), "md4");
return new BulkUpdateDecorator(
() => /** @type {typeof import("crypto")} */ (crypto).createHash("md4"),
"md4"
);
default:
if (crypto === undefined) crypto = require("crypto");
return new BulkUpdateDecorator(
() => crypto.createHash(algorithm),
() =>
/** @type {typeof import("crypto")} */ (crypto).createHash(
/** @type {string} */ (algorithm)
),
algorithm
);
}
Expand Down
13 changes: 10 additions & 3 deletions lib/util/deprecation.js
Expand Up @@ -78,7 +78,7 @@ exports.arrayToSetDeprecation = (set, name) => {
);
/**
* @deprecated
* @this {Set}
* @this {Set<any>}
* @returns {number} count
*/
set[method] = function () {
Expand All @@ -101,7 +101,7 @@ exports.arrayToSetDeprecation = (set, name) => {
);
/**
* @deprecated
* @this {Set}
* @this {Set<any>}
* @returns {number} count
*/
set.push = function () {
Expand All @@ -119,9 +119,13 @@ exports.arrayToSetDeprecation = (set, name) => {
);
};
}
/**
* @param {number} index index
* @returns {any} value
*/
const createIndexGetter = index => {
/**
* @this {Set} a Set
* @this {Set<any>} a Set
* @returns {any} the value at this location
*/
const fn = function () {
Expand All @@ -134,6 +138,9 @@ exports.arrayToSetDeprecation = (set, name) => {
};
return fn;
};
/**
* @param {number} index index
*/
const defineIndexGetter = index => {
Object.defineProperty(set, index, {
get: createIndexGetter(index),
Expand Down

0 comments on commit 568f28b

Please sign in to comment.