Skip to content

Commit

Permalink
feat: add TypeScript declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
kenany committed Mar 26, 2023
1 parent 29ea70d commit 9a7f39f
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@kenan"
}
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
npm-debug.log
npm-debug.log
*.d.ts
!typings/*.d.ts
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2014–2022 Kenan Yildirim <https://kenany.me/>
Copyright 2014–2023 Kenan Yildirim <https://kenany.me/>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/** @type {Window} */
/* eslint-disable no-var, operator-linebreak */

var window = require('global/window');
var nodeCrypto = require('crypto');

Expand All @@ -20,6 +21,7 @@ function getRandomValues(buf) {
}
if (buf.length > 65536) {
var e = new Error();
// @ts-expect-error
e.code = 22;
e.message = 'Failed to execute \'getRandomValues\' on \'Crypto\': The ' +
'ArrayBufferView\'s byte length (' + buf.length + ') exceeds the ' +
Expand Down
35 changes: 28 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"license": "MIT",
"author": "Kenan Yildirim <kenan@kenany.me> (https://kenany.me/)",
"main": "index.js",
"types": "index.d.ts",
"files": [
"CHANGELOG.md",
"index.d.ts",
"index.js",
"LICENSE.txt"
],
Expand All @@ -20,21 +23,39 @@
"node": "14 || 16 || >=18"
},
"scripts": {
"clean": "rimraf --glob test/**/*.d.ts *.d.ts",
"lint": "eslint .",
"release": "semantic-release",
"test": "tape test/*.js"
"type-coverage": "type-coverage --at-least 100 --detail --strict",

"prebuild": "npm run clean",
"build": "tsc",

"pretest": "npm run build",
"test": "tape test/*.js",
"posttest": "npm run lint && npm run type-coverage",

"prepack": "npm run build"
},
"dependencies": {
"global": "^4.4.0"
},
"devDependencies": {
"@semantic-release/changelog": "6.0.2",
"@semantic-release/git": "10.0.1",
"@kenan/eslint-config": "^10.0.1",
"@semantic-release/changelog": "^6.0.2",
"@semantic-release/git": "^10.0.1",
"@tsconfig/node14": "^1.0.3",
"@types/lodash.isfunction": "^3.0.7",
"@types/tape": "^4.13.3",
"conventional-changelog-conventionalcommits": "^5.0.0",
"is-browser": "2.1.0",
"lodash.foreach": "4.5.0",
"lodash.isfunction": "3.0.9",
"eslint": "^8.36.0",
"is-browser": "^2.1.0",
"lodash.isfunction": "^3.0.9",
"rimraf": "^4.4.1",
"semantic-release": "^20.1.3",
"tape": "5.6.3"
"tape": "^5.6.3",
"type-coverage": "^2.25.0",
"typescript": "^4.9.5"
},
"browser": {
"crypto": false
Expand Down
28 changes: 15 additions & 13 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,53 @@
var test = require('tape');
var isFunction = require('lodash.isfunction');
var forEach = require('lodash.foreach');
var isBrowser = require('is-browser');
const test = require('tape');
const isFunction = require('lodash.isfunction');
const isBrowser = require('is-browser');

var getRandomValues = require('../');
const getRandomValues = require('../');

test('exports a function', function(t) {
t.plan(1);
t.ok(isFunction(getRandomValues));
});

test('does not cast buffer', function(t) {
var TYPES = [
// eslint-disable-next-line max-len
/** @type {readonly (Uint8ArrayConstructor | Int8ArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor)[]} */
let TYPES = [
Uint8Array
];

if (isBrowser) {
Array.prototype.push.apply(TYPES, [
TYPES = [
...TYPES,
Int8Array,
Int16Array,
Uint16Array,
Int32Array,
Uint32Array
]);
];
}

t.plan(TYPES.length * 3);

forEach(TYPES, function(Type) {
var buf = new Type(8);
for (const Type of TYPES) {
const buf = new Type(8);
t.doesNotThrow(function() {
getRandomValues(buf);
});
t.equal(buf.constructor, Type);
t.equal(buf.length, 8);
});
}
});

test('throws on length >65536', function(t) {
t.plan(1);
t.throws(function() {
getRandomValues(new Uint8Array(65537));
}, new RegExp(/QuotaExceededError/));
}, /QuotaExceededError/);
});

test('returns argument', function(t) {
t.plan(1);
var argument = new Uint8Array(1024);
const argument = new Uint8Array(1024);
t.equal(getRandomValues(argument), argument);
});
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "@tsconfig/node14/tsconfig.json",
"compilerOptions": {
"lib": ["es2020", "dom"],
"allowJs": true,
"checkJs": true,
"declaration": true,
"emitDeclarationOnly": true
},
"include": ["typings", "test/**/*.js", "*.js"],
}
7 changes: 7 additions & 0 deletions typings/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare module 'global/window' {
interface GlobalWindow extends Window {
msCrypto?: Crypto
}
const globalWindow: GlobalWindow;
export = globalWindow;
}

0 comments on commit 9a7f39f

Please sign in to comment.