Skip to content

Commit

Permalink
refactor: eliminate ts-ignores by defining file reated types
Browse files Browse the repository at this point in the history
  • Loading branch information
mohitpubnub committed Sep 26, 2023
1 parent 6718489 commit 230af91
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 70 deletions.
19 changes: 14 additions & 5 deletions src/crypto/modules/NodeCryptoModule/ILegacyCryptor.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { EncryptedDataType } from './ICryptor';
import PubNubFile from '../../../file/modules/node';

export type PubnubFile = typeof PubNubFile;
export type ArrayBufferLike = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | DataView | BufferSource;
export type PubNubFileType = {
stream: NodeJS.ReadStream;
data: NodeJS.ReadStream | Buffer;
name: string;
mimeType: string;

export interface ILegacyCryptor<T extends PubnubFile> {
create(config: any): PubNubFileType;

toBuffer(): Buffer;
toArrayBuffer(): ArrayBuffer;
toString(): string;
};

export interface ILegacyCryptor<T extends PubNubFileType> {
get identifier(): string;

encrypt(data: ArrayBufferLike): Promise<EncryptedDataType>;
encrypt(data: ArrayBuffer): Promise<EncryptedDataType>;
decrypt(data: EncryptedDataType): Promise<BufferSource>;

encryptFile(file: T, File: T): Promise<T>;
Expand Down
8 changes: 4 additions & 4 deletions src/crypto/modules/NodeCryptoModule/legacyCryptor.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Crypto from '../../../core/components/cryptography/index';
import FileCryptor from '../node';
import { EncryptedDataType } from './ICryptor';
import { ILegacyCryptor, PubnubFile } from './ILegacyCryptor';
import { ILegacyCryptor, PubNubFileType } from './ILegacyCryptor';

export default class LegacyCryptor implements ILegacyCryptor<PubnubFile> {
export default class LegacyCryptor implements ILegacyCryptor<PubNubFileType> {
config;

cryptor;
Expand All @@ -29,11 +29,11 @@ export default class LegacyCryptor implements ILegacyCryptor<PubnubFile> {
return this.cryptor.decrypt(encryptedData.data.toString());
}

async encryptFile(file: PubnubFile, File: PubnubFile) {
async encryptFile(file: PubNubFileType, File: PubNubFileType) {
return this.fileCryptor.encryptFile(this.config.cipherKey, file, File);
}

async decryptFile(file: PubnubFile, File: PubnubFile) {
async decryptFile(file: PubNubFileType, File: PubNubFileType) {
return this.fileCryptor.decryptFile(this.config.cipherKey, file, File);
}
}
29 changes: 9 additions & 20 deletions src/crypto/modules/NodeCryptoModule/nodeCryptoModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { PubNubError } from '../../../core/components/endpoint';
import LegacyCryptor from './legacyCryptor';
import AesCbcCryptor from './aesCbcCryptor';
import { ICryptor } from './ICryptor';
import { ILegacyCryptor, PubnubFile } from './ILegacyCryptor';
import { ILegacyCryptor, PubNubFileType } from './ILegacyCryptor';

export { LegacyCryptor, AesCbcCryptor };

type CryptorType = ICryptor | ILegacyCryptor<PubnubFile>;
type CryptorType = ICryptor | ILegacyCryptor<PubNubFileType>;

type CryptoModuleConfiguration = {
default: CryptorType;
Expand Down Expand Up @@ -84,28 +84,20 @@ export class CryptoModule {
});
}

async encryptFile(file: PubnubFile, File: PubnubFile) {
async encryptFile(file: PubNubFileType, File: PubNubFileType) {
/**
* Files handled differently in case of Legacy cryptor.
* (as long as we support legacy need to check on intsance type)
*/
if (this.defaultCryptor instanceof LegacyCryptor) return this.defaultCryptor.encryptFile(file, File);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: can not infer that PubNubFile has data field
if (file.data instanceof Buffer) {
return File.create({
name: file.name,
mimeType: 'application/octet-stream',
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: can not infer that PubNubFile has data field
data: await this.encrypt(file.data!),
});
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: can not infer that PubNubFile has data field
if (file.data instanceof Readable) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: PubNubFile as value.
const encryptedStream = await (this.defaultCryptor as ICryptor).encryptStream(file.data);
const header = CryptorHeader.from(this.defaultCryptor.identifier, encryptedStream.metadata!);

Expand All @@ -129,17 +121,16 @@ export class CryptoModule {
}
}

async decryptFile(file: any, File: PubnubFile) {
async decryptFile(file: PubNubFileType, File: PubNubFileType) {
if (file?.data instanceof Buffer) {
const header = CryptorHeader.tryParse(file.data);
const cryptor = this.getCryptor(header);
/**
* If It's legacyone then redirect it.
* (as long as we support legacy need to check on instance type)
*/
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: cryptor will be there or unreachable due to exception
if (cryptor?.identifier === CryptoModule.LEGACY_IDENTIFIER) return cryptor.decryptFile(file, File);
if (cryptor?.identifier === CryptoModule.LEGACY_IDENTIFIER)
return (cryptor as ILegacyCryptor<PubNubFileType>).decryptFile(file, File);
return File.create({
name: file.name,
data: await this.decrypt(file?.data),
Expand All @@ -154,7 +145,7 @@ export class CryptoModule {
}
}

private async onStreamReadable(stream: NodeJS.ReadableStream, file: PubnubFile, File: PubnubFile) {
private async onStreamReadable(stream: NodeJS.ReadableStream, file: PubNubFileType, File: PubNubFileType) {
stream.removeAllListeners('readable');

const magicBytes = stream.read(4);
Expand All @@ -175,12 +166,10 @@ export class CryptoModule {
});
}

private async decryptLegacyFileStream(stream: NodeJS.ReadableStream, file: PubnubFile, File: PubnubFile) {
private async decryptLegacyFileStream(stream: NodeJS.ReadableStream, file: PubNubFileType, File: PubNubFileType) {
const cryptor = this.getLegacyCryptor();
if (cryptor) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: cryptor will be there or unreachable due to exception
return cryptor.decryptFile(
return (cryptor as ILegacyCryptor<PubNubFileType>).decryptFile(
File.create({
name: file.name,
stream: stream,
Expand Down
8 changes: 4 additions & 4 deletions src/crypto/modules/WebCryptoModule/ICryptor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export type EncryptedDataType = {
data: ArrayBufferLike;
metadata: ArrayBufferLike | null;
data: ArrayBuffer;
metadata: ArrayBuffer | null;
};

export interface ICryptor {
get identifier(): string;
encrypt(data: BufferSource): Promise<EncryptedDataType>;
decrypt(data: EncryptedDataType): Promise<ArrayBufferLike>;
encrypt(data: ArrayBuffer): Promise<EncryptedDataType>;
decrypt(data: EncryptedDataType): Promise<ArrayBuffer>;
}
18 changes: 14 additions & 4 deletions src/crypto/modules/WebCryptoModule/ILegacyCryptor.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { EncryptedDataType } from './ICryptor';
import PubNubFile from '../../../file/modules/web';

export type PubnubFile = typeof PubNubFile;
export type PubNubFileType = {
data: Buffer;
name: string;
mimeType: string;

export interface ILegacyCryptor<T extends PubnubFile> {
create(config: any): PubNubFileType;

toArrayBuffer(): ArrayBuffer;
toBlob(): Blob;
toString(): string;
toFile(): File;
};

export interface ILegacyCryptor<T extends PubNubFileType> {
get identifier(): string;

encrypt(data: ArrayBufferLike): Promise<EncryptedDataType>;
encrypt(data: ArrayBuffer): Promise<EncryptedDataType>;
decrypt(data: EncryptedDataType): Promise<BufferSource>;

encryptFile(file: T, File: T): Promise<T>;
Expand Down
13 changes: 5 additions & 8 deletions src/crypto/modules/WebCryptoModule/legacyCryptor.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import Crypto from '../../../core/components/cryptography/index';
import PubNubFile from '../../../file/modules/web';
import FileCryptor from '../web';
import { EncryptedDataType } from './ICryptor';
import { ILegacyCryptor } from './ILegacyCryptor';
import { ILegacyCryptor, PubNubFileType } from './ILegacyCryptor';

export type PubnubFile = typeof PubNubFile;

export default class LegacyCryptor implements ILegacyCryptor<PubnubFile> {
export default class LegacyCryptor implements ILegacyCryptor<PubNubFileType> {
config;

cryptor;
Expand All @@ -21,7 +18,7 @@ export default class LegacyCryptor implements ILegacyCryptor<PubnubFile> {
get identifier() {
return '';
}
async encrypt(data: ArrayBufferLike) {
async encrypt(data: ArrayBuffer) {
return {
data: this.cryptor.encrypt(data) as ArrayBuffer,
metadata: null,
Expand All @@ -32,13 +29,13 @@ export default class LegacyCryptor implements ILegacyCryptor<PubnubFile> {
return this.cryptor.decrypt(encryptedData.data);
}

async encryptFile(file: PubnubFile, File: PubnubFile) {
async encryptFile(file: PubNubFileType, File: PubNubFileType) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: can not detect cipherKey from old Config
return this.fileCryptor.encryptFile(this.config?.cipherKey, file, File);
}

async decryptFile(file: PubnubFile, File: PubnubFile) {
async decryptFile(file: PubNubFileType, File: PubNubFileType) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: can not detect cipherKey from old Config
return this.fileCryptor.decryptFile(this.config.cipherKey, file, File);
Expand Down
36 changes: 11 additions & 25 deletions src/crypto/modules/WebCryptoModule/webCryptoModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { PubNubError } from '../../../core/components/endpoint';
import LegacyCryptor from './legacyCryptor';
import AesCbcCryptor from './aesCbcCryptor';
import { ICryptor } from './ICryptor';
import { ILegacyCryptor, PubnubFile } from './ILegacyCryptor';
import { ILegacyCryptor, PubNubFileType } from './ILegacyCryptor';

type CryptorType = ICryptor | ILegacyCryptor<PubnubFile>;
type CryptorType = ICryptor | ILegacyCryptor<PubNubFileType>;

type CryptoModuleConfiguration = {
default: CryptorType;
Expand Down Expand Up @@ -69,54 +69,40 @@ export default class CryptoModule {
header.length > 0
? encryptedData.slice(header.length - (header as CryptorHeaderV1).metadataLength, header.length)
: null;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: cryptor will be there or unreachable due to exception
return cryptor.decrypt({
return cryptor?.decrypt({
data: encryptedData.slice(header.length),
metadata: metadata,
});
}

async encryptFile(file: PubnubFile, File: PubnubFile) {
async encryptFile(file: PubNubFileType, File: PubNubFileType) {
/**
* Files handled differently in case of Legacy cryptor.
* (as long as we support legacy need to check on default cryptor)
*/
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: default cryptor will be there. As long as legacy cryptor supported.
if (this.defaultCryptor.identifier === '') return this.defaultCryptor.encryptFile(file, File);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: can not infer that PubNubFile has data field
if (file.data instanceof Buffer) {
if (this.defaultCryptor.identifier === '')
return (this.defaultCryptor as ILegacyCryptor<PubNubFileType>).encryptFile(file, File);
if (file.data instanceof ArrayBuffer) {
return File.create({
name: file.name,
mimeType: 'application/octet-stream',
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: can not infer that PubNubFile has data field
data: await this.encrypt(file.data),
});
}
}

async decryptFile(file: PubnubFile, File: PubnubFile) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: can not infer that PubNubFile has data field
if (file.data instanceof Buffer) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: can not infer that PubNubFile has data field
async decryptFile(file: PubNubFileType, File: PubNubFileType) {
if (file.data instanceof ArrayBuffer) {
const header = CryptorHeader.tryParse(file.data);
const cryptor = this.getCryptor(header);
/**
* If It's legacyone then redirect it.
* (as long as we support legacy need to check on instance type)
*/
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: cryptor will be there or unreachable due to exception
if (cryptor.identifier === CryptoModule.LEGACY_IDENTIFIER) return cryptor.decryptFile(file, File);
if (cryptor?.identifier === CryptoModule.LEGACY_IDENTIFIER)
return (cryptor as ILegacyCryptor<PubNubFileType>).decryptFile(file, File);
return File.create({
name: file.name,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: can not infer that PubNubFile has data field
data: await this.decrypt(file.data),
});
}
Expand Down

0 comments on commit 230af91

Please sign in to comment.