Skip to content

Commit e99ef7b

Browse files
wip convert Node Crypto to class
haven't formatted this — will do once contents of class are correct Use of `satisfies` comes from suggestion in [1]. [1] "Allow specifying interface implements clauses for the static side of classes": microsoft/TypeScript#33892
1 parent c4ad445 commit e99ef7b

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/platform/nodejs/lib/util/crypto.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Platform from '../../../../common/platform';
44
import crypto from 'crypto';
55
import ErrorInfo from '../../../../common/lib/types/errorinfo';
66
import * as API from '../../../../../ably';
7-
import { IGetCipherParams } from 'common/types/ICrypto';
7+
import ICrypto, { IGetCipherParams } from 'common/types/ICrypto';
88

99
var Crypto = (function () {
1010
var DEFAULT_ALGORITHM = 'aes';
@@ -120,9 +120,9 @@ var Crypto = (function () {
120120
* concatenated with the resulting raw ciphertext to construct the "ciphertext"
121121
* data passed to the recipient.
122122
*/
123-
function Crypto() {}
123+
class Crypto {
124124

125-
Crypto.CipherParams = CipherParams;
125+
static CipherParams = CipherParams;
126126

127127
/**
128128
* Obtain a complete CipherParams instance from the provided params, filling
@@ -133,7 +133,7 @@ var Crypto = (function () {
133133
* base64-encoded string. May optionally also contain: algorithm (defaults to
134134
* AES), mode (defaults to 'cbc')
135135
*/
136-
Crypto.getDefaultParams = function (params: API.Types.CipherParamOptions) {
136+
static getDefaultParams(params: API.Types.CipherParamOptions) {
137137
var key;
138138

139139
if (!params.key) {
@@ -163,15 +163,15 @@ var Crypto = (function () {
163163

164164
validateCipherParams(cipherParams);
165165
return cipherParams;
166-
};
166+
}
167167

168168
/**
169169
* Generate a random encryption key from the supplied keylength (or the
170170
* default keyLength if none supplied) as a Buffer
171171
* @param keyLength (optional) the required keyLength in bits
172172
* @param callback (optional) (err, key)
173173
*/
174-
Crypto.generateRandomKey = function (keyLength?: number, callback?: API.Types.StandardCallback<API.Types.CipherKey>) {
174+
static generateRandomKey(keyLength?: number, callback?: API.Types.StandardCallback<API.Types.CipherKey>) {
175175
if (arguments.length == 1 && typeof keyLength == 'function') {
176176
callback = keyLength;
177177
keyLength = undefined;
@@ -184,14 +184,14 @@ var Crypto = (function () {
184184
generateRandom((keyLength || DEFAULT_KEYLENGTH) / 8, (err, buf) => {
185185
callback!(err ? ErrorInfo.fromValues(err) : null, buf);
186186
});
187-
};
187+
}
188188

189189
/**
190190
* Internal; get a ChannelCipher instance based on the given cipherParams
191191
* @param params either a CipherParams instance or some subset of its
192192
* fields that includes a key
193193
*/
194-
Crypto.getCipher = function (
194+
static getCipher(
195195
params: IGetCipherParams
196196
) {
197197
var cipherParams = isInstCipherParams(params) ? params : Crypto.getDefaultParams(params);
@@ -201,7 +201,11 @@ var Crypto = (function () {
201201
cipherParams: cipherParams /* CipherOptions.cipher */,
202202
cipher: new CBCCipher(cipherParams, iv) /* CipherOptions.channelCipher */,
203203
};
204-
};
204+
}
205+
206+
}
207+
208+
Crypto satisfies ICrypto;
205209

206210
const CBCCipher = function CBCCipher(this: any /* TODO */, params: any /* TODO */, iv: any /* TODO */) {
207211
var algorithm = (this.algorithm = params.algorithm + '-' + String(params.keyLength) + '-' + params.mode);

0 commit comments

Comments
 (0)