Skip to content

Commit

Permalink
fix:es-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
mohitpubnub committed Sep 25, 2023
1 parent 5d43f19 commit 243056b
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 46 deletions.
15 changes: 10 additions & 5 deletions lib/crypto/modules/NodeCryptoModule/NodeCryptoModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ var CryptoModule = /** @class */ (function () {
this.defaultCryptor = cryptoModuleConfiguration.default;
this.cryptors = (_a = cryptoModuleConfiguration.cryptors) !== null && _a !== void 0 ? _a : [];
}
//@ts-ignore: type detection issue with old Config type assignment
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: type detection issue with old Config type assignment
CryptoModule.legacyCryptoModule = function (_a) {
var config = _a.config;
return new this({
Expand Down Expand Up @@ -160,10 +161,12 @@ var CryptoModule = /** @class */ (function () {
};
return [4 /*yield*/, this.encrypt(file.data)];
case 1: return [2 /*return*/, _b.apply(_a, [(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: can not infer that PubNubFile has data field
_c.data = _d.sent(),
_c)])];
case 2:
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: can not infer that PubNubFile has data field
if (file.data instanceof stream_1.Readable) {
encryptedStream = this.defaultCryptor.encryptStream(file.data);
Expand Down Expand Up @@ -205,6 +208,7 @@ var CryptoModule = /** @class */ (function () {
* 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 === null || cryptor === void 0 ? void 0 : cryptor.identifier) === CryptoModule.LEGACY_IDENTIFIER)
return [2 /*return*/, cryptor.decryptFile(file, File)];
Expand Down Expand Up @@ -263,6 +267,7 @@ var CryptoModule = /** @class */ (function () {
return __generator(this, function (_a) {
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 [2 /*return*/, cryptor.decryptFile(File.create({
name: file.name,
Expand Down Expand Up @@ -434,12 +439,12 @@ var CryptorHeaderV1 = /** @class */ (function () {
if (this.identifier)
header.set(Buffer.from(this.identifier), pos);
pos += CryptorHeaderV1.IDENTIFIER_LENGTH;
var metadata_size = this.metadataLength;
if (metadata_size < 255) {
header[pos] = metadata_size;
var metadataSize = this.metadataLength;
if (metadataSize < 255) {
header[pos] = metadataSize;
}
else {
header.set([255, metadata_size >> 8, metadata_size & 0xff], pos);
header.set([255, metadataSize >> 8, metadataSize & 0xff], pos);
}
return header;
},
Expand Down
1 change: 0 additions & 1 deletion lib/crypto/modules/NodeCryptoModule/legacyCryptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ var LegacyCryptor = /** @class */ (function () {
LegacyCryptor.prototype.decrypt = function (encryptedData) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
// Legacy cryptor only works on base64 string
return [2 /*return*/, this.cryptor.decrypt(encryptedData.data.toString())];
});
});
Expand Down
2 changes: 2 additions & 0 deletions lib/crypto/modules/WebCryptoModule/legacyCryptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ var LegacyCryptor = /** @class */ (function () {
var _a;
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_b) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: can not detect cipherKey from old Config
return [2 /*return*/, this.fileCryptor.encryptFile((_a = this.config) === null || _a === void 0 ? void 0 : _a.cipherKey, file, File)];
});
Expand All @@ -83,6 +84,7 @@ var LegacyCryptor = /** @class */ (function () {
LegacyCryptor.prototype.decryptFile = function (file, File) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: can not detect cipherKey from old Config
return [2 /*return*/, this.fileCryptor.decryptFile(this.config.cipherKey, file, File)];
});
Expand Down
14 changes: 10 additions & 4 deletions lib/crypto/modules/WebCryptoModule/webCryptoModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ var CryptoModule = /** @class */ (function () {
this.defaultCryptor = cryptoModuleConfiguration.default;
this.cryptors = (_a = cryptoModuleConfiguration.cryptors) !== null && _a !== void 0 ? _a : [];
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: type detection issue with old Config type assignment
CryptoModule.legacyCryptoModule = function (_a) {
var config = _a.config;
Expand Down Expand Up @@ -126,6 +127,7 @@ var CryptoModule = /** @class */ (function () {
metadata = header.length > 0
? encryptedData.slice(header.length - header.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 [2 /*return*/, cryptor.decrypt({
data: encryptedData.slice(header.length),
Expand All @@ -145,6 +147,7 @@ var CryptoModule = /** @class */ (function () {
* 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 [2 /*return*/, this.defaultCryptor.encryptFile(file, File)];
Expand All @@ -156,6 +159,7 @@ var CryptoModule = /** @class */ (function () {
};
return [4 /*yield*/, this.encrypt(file.data)];
case 1: return [2 /*return*/, _b.apply(_a, [(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: can not infer that PubNubFile has data field
_c.data = _d.sent(),
_c)])];
Expand All @@ -178,6 +182,7 @@ var CryptoModule = /** @class */ (function () {
* 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 [2 /*return*/, cryptor.decryptFile(file, File)];
Expand All @@ -187,6 +192,7 @@ var CryptoModule = /** @class */ (function () {
};
return [4 /*yield*/, this.decrypt(file.data)];
case 1: return [2 /*return*/, _b.apply(_a, [(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: can not infer that PubNubFile has data field
_c.data = _d.sent(),
_c)])];
Expand Down Expand Up @@ -329,12 +335,12 @@ var CryptorHeaderV1 = /** @class */ (function () {
if (this.identifier)
header.set(encoder.encode(this.identifier), pos);
pos += CryptorHeaderV1.IDENTIFIER_LENGTH;
var metadata_size = this.metadataLength;
if (metadata_size < 255) {
header[pos] = metadata_size;
var metadataSize = this.metadataLength;
if (metadataSize < 255) {
header[pos] = metadataSize;
}
else {
header.set([255, metadata_size >> 8, metadata_size & 0xff], pos);
header.set([255, metadataSize >> 8, metadataSize & 0xff], pos);
}
return header;
},
Expand Down
49 changes: 28 additions & 21 deletions src/crypto/modules/NodeCryptoModule/nodeCryptoModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export default class CryptoModule {
this.cryptors = cryptoModuleConfiguration.cryptors ?? [];
}

//@ts-ignore: type detection issue with old Config type assignment
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: type detection issue with old Config type assignment
static legacyCryptoModule({ config }) {
return new this({
default: new LegacyCryptor({ config }),
Expand Down Expand Up @@ -51,12 +52,12 @@ export default class CryptoModule {
}

async encrypt(data: Buffer) {
let encrypted = await this.defaultCryptor.encrypt(data);
const encrypted = await this.defaultCryptor.encrypt(data);
if (!encrypted.metadata) return encrypted.data;

let header = CryptorHeader.from(this.defaultCryptor.identifier, encrypted.metadata);
const header = CryptorHeader.from(this.defaultCryptor.identifier, encrypted.metadata);

let headerData = new Uint8Array(header!.length);
const headerData = new Uint8Array(header!.length);
let pos = 0;
headerData.set(header!.data, pos);
pos += header!.length;
Expand All @@ -71,9 +72,9 @@ export default class CryptoModule {
let encryptedData = null;
encryptedData = Buffer.from(data);

let header = CryptorHeader.tryParse(encryptedData);
let cryptor = this.getCryptor(header);
let metadata =
const header = CryptorHeader.tryParse(encryptedData);
const cryptor = this.getCryptor(header);
const metadata =
header.length > 0
? encryptedData.slice(header.length - (header as CryptorHeaderV1).metadataLength, header.length)
: null;
Expand All @@ -89,22 +90,26 @@ export default class CryptoModule {
* (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: default cryptor will be there. As long as legacy cryptor supported.
let encryptedStream = this.defaultCryptor.encryptStream(file.data);
let header = CryptorHeader.from(this.defaultCryptor.identifier, encryptedStream.metadata);
const encryptedStream = this.defaultCryptor.encryptStream(file.data);
const header = CryptorHeader.from(this.defaultCryptor.identifier, encryptedStream.metadata);

let payload = new Uint8Array(header!.length);
const payload = new Uint8Array(header!.length);
let pos = 0;
payload.set(header!.data, pos);
pos += header!.length;
Expand All @@ -126,12 +131,13 @@ export default class CryptoModule {

async decryptFile(file: any, File: PubnubFile) {
if (file?.data instanceof Buffer) {
let header = CryptorHeader.tryParse(file.data);
let cryptor = this.getCryptor(header);
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);
return File.create({
Expand All @@ -151,16 +157,16 @@ export default class CryptoModule {
async onStreamReadable(stream: NodeJS.ReadableStream, file: PubnubFile, File: PubnubFile) {
stream.removeAllListeners('readable');

let magicBytes = stream.read(4);
const magicBytes = stream.read(4);
if (!CryptorHeader.isSentinel(magicBytes as Buffer)) {
stream.unshift(magicBytes);
return this.decryptLegacyFileStream(stream, file, File);
}
let versionByte = stream.read(1);
const versionByte = stream.read(1);
CryptorHeader.validateVersion(versionByte[0] as number);
let identifier = stream.read(4);
let cryptor = this.getCryptorFromId(CryptorHeader.tryGetIdentifier(identifier as Buffer));
let headerSize = CryptorHeader.tryGetMetadataSizeFromStream(stream);
const identifier = stream.read(4);
const cryptor = this.getCryptorFromId(CryptorHeader.tryGetIdentifier(identifier as Buffer));
const headerSize = CryptorHeader.tryGetMetadataSizeFromStream(stream);

return File.create({
name: file.name,
Expand All @@ -172,6 +178,7 @@ export default class CryptoModule {
async decryptLegacyFileStream(stream: NodeJS.ReadableStream, file: PubnubFile, File: PubnubFile) {
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(
File.create({
Expand Down Expand Up @@ -336,11 +343,11 @@ class CryptorHeaderV1 {
pos++;
if (this.identifier) header.set(Buffer.from(this.identifier), pos);
pos += CryptorHeaderV1.IDENTIFIER_LENGTH;
let metadata_size = this.metadataLength;
if (metadata_size < 255) {
header[pos] = metadata_size;
const metadataSize = this.metadataLength;
if (metadataSize < 255) {
header[pos] = metadataSize;
} else {
header.set([255, metadata_size >> 8, metadata_size & 0xff], pos);
header.set([255, metadataSize >> 8, metadataSize & 0xff], pos);
}
return header;
}
Expand Down
2 changes: 2 additions & 0 deletions src/crypto/modules/WebCryptoModule/legacyCryptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ export default class LegacyCryptor implements ILegacyCryptor<PubnubFile> {
}

async encryptFile(file: PubnubFile, File: PubnubFile) {
// 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) {
// 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
33 changes: 21 additions & 12 deletions src/crypto/modules/WebCryptoModule/webCryptoModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default class CryptoModule {
this.cryptors = cryptoModuleConfiguration.cryptors ?? [];
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: type detection issue with old Config type assignment
static legacyCryptoModule({ config }) {
return new this({
Expand All @@ -45,12 +46,12 @@ export default class CryptoModule {
}

async encrypt(data: ArrayBuffer) {
let encrypted = await this.defaultCryptor.encrypt(data);
const encrypted = await this.defaultCryptor.encrypt(data);
if (!encrypted.metadata) return encrypted.data;

let header = CryptorHeader.from(this.defaultCryptor.identifier, encrypted.metadata);
const header = CryptorHeader.from(this.defaultCryptor.identifier, encrypted.metadata);

let headerData = new Uint8Array((header as CryptorHeaderV1).length);
const headerData = new Uint8Array((header as CryptorHeaderV1).length);
let pos = 0;
headerData.set((header as CryptorHeaderV1).data, pos);
pos += (header as CryptorHeaderV1).length;
Expand All @@ -62,12 +63,13 @@ export default class CryptoModule {
}

async decrypt(encryptedData: ArrayBuffer) {
let header = CryptorHeader.tryParse(encryptedData);
let cryptor = this.getCryptor(header);
let metadata =
const header = CryptorHeader.tryParse(encryptedData);
const cryptor = this.getCryptor(header);
const metadata =
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({
data: encryptedData.slice(header.length),
Expand All @@ -80,33 +82,40 @@ export default class CryptoModule {
* 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) {
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
let header = CryptorHeader.tryParse(file.data);
let cryptor = this.getCryptor(header);
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);
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 Expand Up @@ -243,11 +252,11 @@ class CryptorHeaderV1 {
pos++;
if (this.identifier) header.set(encoder.encode(this.identifier), pos);
pos += CryptorHeaderV1.IDENTIFIER_LENGTH;
let metadata_size = this.metadataLength;
if (metadata_size < 255) {
header[pos] = metadata_size;
const metadataSize = this.metadataLength;
if (metadataSize < 255) {
header[pos] = metadataSize;
} else {
header.set([255, metadata_size >> 8, metadata_size & 0xff], pos);
header.set([255, metadataSize >> 8, metadataSize & 0xff], pos);
}
return header;
}
Expand Down
Loading

0 comments on commit 243056b

Please sign in to comment.