Skip to content

Commit

Permalink
fix: eslint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
microshine committed Jan 23, 2024
1 parent 72bcd53 commit 0a626eb
Show file tree
Hide file tree
Showing 39 changed files with 163 additions and 168 deletions.
2 changes: 1 addition & 1 deletion src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SubtleCrypto } from "./subtle";

export class Crypto extends core.Crypto {

public get nativeCrypto() {
public get nativeCrypto(): globalThis.Crypto {
return nativeCrypto;
}

Expand Down
24 changes: 12 additions & 12 deletions src/debug.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
export class Debug {

public static get enabled() {
public static get enabled(): boolean {
return typeof self !== "undefined" && (self as any).PV_WEBCRYPTO_LINER_LOG;
}

public static log(message?: any, ...optionalParams: any[]): void;
public static log(...args: any[]) {
public static log(...args: any[]): void {
if (this.enabled) {
console.log.apply(console, args);
console.log(...args);
}
}

public static error(message?: any, ...optionalParams: any[]): void
public static error(...args: any[]) {
public static error(message?: any, ...optionalParams: any[]): void;
public static error(...args: any[]): void {
if (this.enabled) {
console.error.apply(console, args);
console.error(...args);
}
}

public static info(message?: any, ...optionalParams: any[]): void;
public static info(...args: any[]) {
public static info(...args: any[]): void {
if (this.enabled) {
console.info.apply(console, args);
console.info(...args);
}
}

public static warn(message?: any, ...optionalParams: any[]): void;
public static warn(...args: any[]) {
public static warn(...args: any[]): void {
if (this.enabled) {
console.warn.apply(console, args);
console.warn(...args);
}
}

public static trace(message?: any, ...optionalParams: any[]): void;
public static trace(...args: any[]) {
public static trace(...args: any[]): void {
if (this.enabled) {
console.trace.apply(console, args);
console.trace(...args);
}
}

Expand Down
27 changes: 13 additions & 14 deletions src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface IBrowserInfo {
/**
* Returns info about browser
*/
export function BrowserInfo() {
export function BrowserInfo(): IBrowserInfo {
const res: IBrowserInfo = {
name: Browser.Unknown,
version: "0",
Expand All @@ -26,42 +26,41 @@ export function BrowserInfo() {
}
const userAgent = self.navigator.userAgent;

let reg: string[] | null;
// tslint:disable-next-line:no-conditional-assignment
if (reg = /edge\/([\d\.]+)/i.exec(userAgent)) {
const reg = /edge\/([\d.]+)/i.exec(userAgent);
if (reg) {
res.name = Browser.Edge;
res.version = reg[1];
} else if (/msie/i.test(userAgent)) {
res.name = Browser.IE;
res.version = /msie ([\d\.]+)/i.exec(userAgent)![1];
res.version = /msie ([\d.]+)/i.exec(userAgent)![1];
} else if (/Trident/i.test(userAgent)) {
res.name = Browser.IE;
res.version = /rv:([\d\.]+)/i.exec(userAgent)![1];
res.version = /rv:([\d.]+)/i.exec(userAgent)![1];
} else if (/chrome/i.test(userAgent)) {
res.name = Browser.Chrome;
res.version = /chrome\/([\d\.]+)/i.exec(userAgent)![1];
res.version = /chrome\/([\d.]+)/i.exec(userAgent)![1];
} else if (/firefox/i.test(userAgent)) {
res.name = Browser.Firefox;
res.version = /firefox\/([\d\.]+)/i.exec(userAgent)![1];
res.version = /firefox\/([\d.]+)/i.exec(userAgent)![1];
} else if (/mobile/i.test(userAgent)) {
res.name = Browser.Mobile;
res.version = /mobile\/([\w]+)/i.exec(userAgent)![1];
} else if (/safari/i.test(userAgent)) {
res.name = Browser.Safari;
res.version = /version\/([\d\.]+)/i.exec(userAgent)![1];
res.version = /version\/([\d.]+)/i.exec(userAgent)![1];
}
return res;
}

export function string2buffer(binaryString: string) {
export function string2buffer(binaryString: string): Uint8Array {
const res = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
res[i] = binaryString.charCodeAt(i);
}
return res;
}

export function buffer2string(buffer: Uint8Array) {
export function buffer2string(buffer: Uint8Array): string {
let res = "";
// tslint:disable-next-line:prefer-for-of
for (let i = 0; i < buffer.length; i++) {
Expand All @@ -70,10 +69,10 @@ export function buffer2string(buffer: Uint8Array) {
return res;
}

export function concat(...buf: Uint8Array[]) {
export function concat(...buf: Uint8Array[]): Uint8Array {
const res = new Uint8Array(buf.map((item) => item.length).reduce((prev, cur) => prev + cur));
let offset = 0;
buf.forEach((item, index) => {
buf.forEach((item) => {
for (let i = 0; i < item.length; i++) {
res[offset + i] = item[i];
}
Expand All @@ -83,7 +82,7 @@ export function concat(...buf: Uint8Array[]) {
}

export function assign(target: any, ...sources: any[]): any;
export function assign(...args: any[]) {
export function assign(...args: any[]): any {
const res = args[0];
for (let i = 1; i < args.length; i++) {
const obj = args[i];
Expand Down
11 changes: 6 additions & 5 deletions src/init.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { nativeSubtle } from "./native";

function WrapFunction(subtle: any, name: string) {
function WrapFunction(subtle: any, name: string): void {
const fn = subtle[name];
// tslint:disable-next-line:only-arrow-functions
subtle[name] = function () {
subtle[name] = function (): Promise<unknown> {
// eslint-disable-next-line prefer-rest-params
const args = arguments;
return new Promise((resolve, reject) => {
const op: any = fn.apply(subtle, args);
op.oncomplete = (e: any) => {
op.oncomplete = (e: any): any => {
resolve(e.target.result);
};
op.onerror = (e: any) => {
op.onerror = (_e: any): void => {
reject(`Error on running '${name}' function`);
};
});
Expand All @@ -35,7 +36,7 @@ if (typeof self !== "undefined" && self["msCrypto"]) {
// fix: Math.imul for IE
if (!(Math as any).imul) {
// tslint:disable-next-line:only-arrow-functions
(Math as any).imul = function imul(a: number, b: number) {
(Math as any).imul = function imul(a: number, b: number): number {
const ah = (a >>> 16) & 0xffff;
const al = a & 0xffff;
const bh = (b >>> 16) & 0xffff;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Crypto, nativeCrypto } from ".";
import { Crypto, nativeCrypto } from "./index";
import "./init";

if (nativeCrypto) {
Expand Down
2 changes: 1 addition & 1 deletion src/mechs/aes/aes_ecb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class AesEcbProvider extends core.AesEcbProvider {
return AesCrypto.importKey(format, keyData, algorithm, extractable, keyUsages);
}

public checkCryptoKey(key: CryptoKey, keyUsage: KeyUsage) {
public checkCryptoKey(key: CryptoKey, keyUsage: KeyUsage): void {
super.checkCryptoKey(key, keyUsage);
AesCrypto.checkCryptoKey(key);
}
Expand Down
10 changes: 5 additions & 5 deletions src/mechs/aes/aes_kw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import { AesCrypto } from "./crypto";
import { AesCryptoKey } from "./key";

export class AesKwProvider extends core.AesKwProvider {
public async onEncrypt(algorithm: AesCtrParams, key: CryptoKey, data: ArrayBuffer): Promise<ArrayBuffer> {
public async onEncrypt(_algorithm: AesCtrParams, _key: CryptoKey, _data: ArrayBuffer): Promise<ArrayBuffer> {
throw new Error("Method not implemented.");
}
public async onDecrypt(algorithm: AesCtrParams, key: CryptoKey, data: ArrayBuffer): Promise<ArrayBuffer> {
public async onDecrypt(_algorithm: AesCtrParams, _key: CryptoKey, _data: ArrayBuffer): Promise<ArrayBuffer> {
throw new Error("Method not implemented.");
}
public async onGenerateKey(algorithm: AesKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKey> {
public async onGenerateKey(_algorithm: AesKeyGenParams, _extractable: boolean, _keyUsages: KeyUsage[]): Promise<CryptoKey> {
throw new Error("Method not implemented.");
}
public async onExportKey(format: KeyFormat, key: CryptoKey): Promise<ArrayBuffer | JsonWebKey> {
public async onExportKey(_format: KeyFormat, _key: CryptoKey): Promise<ArrayBuffer | JsonWebKey> {
throw new Error("Method not implemented.");
}
public async onImportKey(format: KeyFormat, keyData: ArrayBuffer | JsonWebKey, algorithm: Algorithm, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKey> {
public async onImportKey(_format: KeyFormat, _keyData: ArrayBuffer | JsonWebKey, _algorithm: Algorithm, _extractable: boolean, _keyUsages: KeyUsage[]): Promise<CryptoKey> {
throw new Error("Method not implemented.");
}

Expand Down
10 changes: 5 additions & 5 deletions src/mechs/aes/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ export class AesCrypto {
public static AesECB = "AES-ECB";
public static AesGCM = "AES-GCM";

public static checkCryptoKey(key: any) {
public static checkCryptoKey(key: any): void {
if (!(key instanceof AesCryptoKey)) {
throw new TypeError("key: Is not AesCryptoKey");
}
}

public static async generateKey(algorithm: AesKeyGenParams, extractable: boolean, usages: KeyUsage[]) {
public static async generateKey(algorithm: AesKeyGenParams, extractable: boolean, usages: KeyUsage[]): Promise<AesCryptoKey> {
// gat random bytes for key
const raw = nativeCrypto.getRandomValues(new Uint8Array(algorithm.length / 8));

return new AesCryptoKey(algorithm, extractable, usages, raw);
}

public static async encrypt(algorithm: Algorithm, key: AesCryptoKey, data: ArrayBuffer) {
public static async encrypt(algorithm: Algorithm, key: AesCryptoKey, data: ArrayBuffer): Promise<ArrayBuffer> {
return this.cipher(algorithm, key, core.BufferSourceConverter.toUint8Array(data), true);
}

public static async decrypt(algorithm: Algorithm, key: AesCryptoKey, data: ArrayBuffer) {
public static async decrypt(algorithm: Algorithm, key: AesCryptoKey, data: ArrayBuffer): Promise<ArrayBuffer> {
return this.cipher(algorithm, key, core.BufferSourceConverter.toUint8Array(data), false);
}

Expand Down Expand Up @@ -66,7 +66,7 @@ export class AesCrypto {
return key;
}

private static async cipher(algorithm: Algorithm, key: AesCryptoKey, data: Uint8Array, encrypt: boolean) {
private static async cipher(algorithm: Algorithm, key: AesCryptoKey, data: Uint8Array, encrypt: boolean): Promise<ArrayBuffer> {
const action = encrypt ? "encrypt" : "decrypt";
let result: Uint8Array;
if (isAlgorithm<AesCbcParams>(algorithm, AesCrypto.AesCBC)) {
Expand Down
4 changes: 2 additions & 2 deletions src/mechs/aes/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class AesCryptoKey extends CryptoKey {
super(algorithm, extractable, "secret", usages);
}

public toJSON() {
public toJSON(): JsonWebKey {
const jwk: JsonWebKey = {
kty: "oct",
alg: this.getJwkAlgorithm(),
Expand All @@ -20,7 +20,7 @@ export class AesCryptoKey extends CryptoKey {
return jwk;
}

private getJwkAlgorithm() {
private getJwkAlgorithm(): string {
switch (this.algorithm.name.toUpperCase()) {
case "AES-CBC":
return `A${this.algorithm.length}CBC`;
Expand Down
7 changes: 4 additions & 3 deletions src/mechs/des/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
/// <reference path="../../typings/des.d.ts" />

import * as des from "des.js";
Expand All @@ -8,13 +9,13 @@ import { DesCryptoKey } from "./key";

export class DesCrypto {

public static checkLib() {
if (typeof(des) === "undefined") {
public static checkLib(): void {
if (typeof (des) === "undefined") {
throw new core.OperationError("Cannot implement DES mechanism. Add 'https://peculiarventures.github.io/pv-webcrypto-tests/src/des.js' script to your project");
}
}

public static checkCryptoKey(key: any) {
public static checkCryptoKey(key: any): void {
if (!(key instanceof DesCryptoKey)) {
throw new TypeError("key: Is not DesCryptoKey");
}
Expand Down
4 changes: 2 additions & 2 deletions src/mechs/des/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class DesCryptoKey extends CryptoKey {
super(algorithm, extractable, "secret", usages);
}

public toJSON() {
public toJSON(): JsonWebKey {
const jwk: JsonWebKey = {
kty: "oct",
alg: this.getJwkAlgorithm(),
Expand All @@ -20,7 +20,7 @@ export class DesCryptoKey extends CryptoKey {
return jwk;
}

private getJwkAlgorithm() {
private getJwkAlgorithm(): string {
switch (this.algorithm.name.toUpperCase()) {
case "DES-CBC":
return `DES-CBC`;
Expand Down
Loading

0 comments on commit 0a626eb

Please sign in to comment.