Skip to content

Commit

Permalink
add types
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 29, 2024
1 parent 4334cf9 commit 9beef66
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 9 deletions.
27 changes: 27 additions & 0 deletions index.d.ts
@@ -0,0 +1,27 @@
declare namespace getSideChannel {
type Key = unknown;
type ListNode<T> = {
key: Key;
next: ListNode<T>;
value: T;
};
type RootNode<T> = {
key: object;
next: null | ListNode<T>;
};
function listGetNode<T>(list: RootNode<T>, key: ListNode<T>['key']): ListNode<T> | void;
function listGet<T>(objects: RootNode<T>, key: ListNode<T>['key']): T | void;
function listSet<T>(objects: RootNode<T>, key: ListNode<T>['key'], value: T): void;
function listHas<T>(objects: RootNode<T>, key: ListNode<T>['key']): boolean;

type Channel = {
assert: (key: Key) => void;
has: (key: Key) => boolean;
get: <T>(key: Key) => T;
set: <T>(key: Key, value: T) => void;
}
}

declare function getSideChannel(): getSideChannel.Channel;

export = getSideChannel;
26 changes: 19 additions & 7 deletions index.js
Expand Up @@ -20,42 +20,54 @@ var $mapHas = callBound('Map.prototype.has', true);
*
* That node is also moved to the head of the list, so that if it's accessed again we don't need to traverse the whole list. By doing so, all the recently used nodes can be accessed relatively quickly.
*/
/** @type {import('.').listGetNode} */
var listGetNode = function (list, key) { // eslint-disable-line consistent-return
for (var prev = list, curr; (curr = prev.next) !== null; prev = curr) {
/** @type {typeof list | NonNullable<(typeof list)['next']>} */
var prev = list;
/** @type {(typeof list)['next']} */
var curr;
for (; (curr = prev.next) !== null; prev = curr) {
if (curr.key === key) {
prev.next = curr.next;
curr.next = list.next;
// eslint-disable-next-line no-extra-parens
curr.next = /** @type {NonNullable<typeof list.next>} */ (list.next);
list.next = curr; // eslint-disable-line no-param-reassign
return curr;
}
}
};

/** @type {import('.').listGet} */
var listGet = function (objects, key) {
var node = listGetNode(objects, key);
return node && node.value;
};
/** @type {import('.').listSet} */
var listSet = function (objects, key, value) {
var node = listGetNode(objects, key);
if (node) {
node.value = value;
} else {
// Prepend the new node to the beginning of the list
objects.next = { // eslint-disable-line no-param-reassign
objects.next = /** @type {import('.').ListNode<typeof value>} */ ({ // eslint-disable-line no-param-reassign, no-extra-parens
key: key,
next: objects.next,
value: value
};
});
}
};
/** @type {import('.').listHas} */
var listHas = function (objects, key) {
return !!listGetNode(objects, key);
};

/** @type {import('.')} */
module.exports = function getSideChannel() {
var $wm;
var $m;
var $o;
/** @type {WeakMap<object, unknown>} */ var $wm;
/** @type {Map<object, unknown>} */ var $m;
/** @type {import('.').RootNode<unknown>} */ var $o;

/** @type {import('.').Channel} */
var channel = {
assert: function (key) {
if (!channel.has(key)) {
Expand Down
9 changes: 8 additions & 1 deletion package.json
Expand Up @@ -7,12 +7,14 @@
"./package.json": "./package.json",
".": "./index.js"
},
"types": "./index.d.ts",
"scripts": {
"prepack": "npmignore --auto --commentLines=autogenerated",
"prepublishOnly": "safe-publish-latest",
"prepublish": "not-in-publish || npm run prepublishOnly",
"prelint": "eclint check $(git ls-files | xargs find 2> /dev/null | grep -vE 'node_modules|\\.git')",
"lint": "eslint --ext=js,mjs .",
"postlint": "tsc -p .",
"pretest": "npm run lint",
"tests-only": "nyc tape 'test/**/*.js'",
"test": "npm run tests-only",
Expand Down Expand Up @@ -42,6 +44,10 @@
"homepage": "https://github.com/ljharb/side-channel#readme",
"devDependencies": {
"@ljharb/eslint-config": "^21.1.0",
"@types/call-bind": "^1.0.5",
"@types/get-intrinsic": "^1.2.2",
"@types/object-inspect": "^1.8.4",
"@types/tape": "^5.6.4",
"aud": "^2.0.4",
"auto-changelog": "^2.4.0",
"eclint": "^2.8.1",
Expand All @@ -50,7 +56,8 @@
"npmignore": "^0.3.1",
"nyc": "^10.3.2",
"safe-publish-latest": "^2.0.0",
"tape": "^5.7.5"
"tape": "^5.7.5",
"typescript": "next"
},
"dependencies": {
"call-bind": "^1.0.7",
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Expand Up @@ -32,7 +32,7 @@ test('assert', function (t) {

test('has', function (t) {
var channel = getSideChannel();
var o = [];
/** @type {unknown[]} */ var o = [];

t.equal(channel.has(o), false, 'nonexistent value yields false');

Expand Down
50 changes: 50 additions & 0 deletions tsconfig.json
@@ -0,0 +1,50 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */

/* Projects */

/* Language and Environment */
"target": "es2022", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
"useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */

/* Modules */
"module": "commonjs", /* Specify what module code is generated. */
// "rootDir": "./", /* Specify the root folder within your source files. */
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": ["types"], /* Specify multiple folders that act like `./node_modules/@types`. */
"resolveJsonModule": true, /* Enable importing .json files. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */

/* JavaScript Support */
"allowJs": true, /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
"checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
"maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */

/* Emit */
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
"declarationMap": true, /* Create sourcemaps for d.ts files. */
"noEmit": true, /* Disable emitting files from a compilation. */

/* Interop Constraints */
"allowSyntheticDefaultImports": true, /* Allow `import x from y` when a module doesn't have a default export. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */

/* Type Checking */
"strict": true, /* Enable all strict type-checking options. */

/* Completeness */
// "skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"exclude": [
"coverage",
"test/list-exports"
],
}

0 comments on commit 9beef66

Please sign in to comment.