Skip to content

Commit ac14129

Browse files
authored
Use new Buffer functions to create buffers (#401)
1 parent e51fb00 commit ac14129

8 files changed

+24
-24
lines changed

src/encryption-helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const encrypt = function(userPublicKey, userAuth, payload, contentEncoding) {
3535
}
3636

3737
if (typeof payload === 'string' || payload instanceof String) {
38-
payload = new Buffer(payload);
38+
payload = Buffer.from(payload);
3939
}
4040

4141
const localCurve = crypto.createECDH('prime256v1');

src/vapid-helper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ function generateVAPIDKeys() {
4848
// in errors, hence this padding.
4949
// See https://github.com/web-push-libs/web-push/issues/295 for history.
5050
if (privateKeyBuffer.length < 32) {
51-
const padding = new Buffer(32 - privateKeyBuffer.length);
51+
const padding = Buffer.alloc(32 - privateKeyBuffer.length);
5252
padding.fill(0);
5353
privateKeyBuffer = Buffer.concat([privateKeyBuffer, padding]);
5454
}
5555

5656
if (publicKeyBuffer.length < 65) {
57-
const padding = new Buffer(65 - publicKeyBuffer.length);
57+
const padding = Buffer.alloc(65 - publicKeyBuffer.length);
5858
padding.fill(0);
5959
publicKeyBuffer = Buffer.concat([publicKeyBuffer, padding]);
6060
}

test/test-encryption-helper.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ suite('Test Encryption Helpers', function() {
2828
}
2929

3030
test('encrypt/decrypt string (aesgcm)', function() {
31-
assert(encryptDecrypt('hello', webPush.supportedContentEncodings.AES_GCM).equals(new Buffer('hello')));
31+
assert(encryptDecrypt('hello', webPush.supportedContentEncodings.AES_GCM).equals(Buffer.from('hello')));
3232
});
3333

3434
test('encrypt/decrypt string (aes128gcm)', function() {
35-
assert(encryptDecrypt('hello', webPush.supportedContentEncodings.AES_128_GCM).equals(new Buffer('hello')));
35+
assert(encryptDecrypt('hello', webPush.supportedContentEncodings.AES_128_GCM).equals(Buffer.from('hello')));
3636
});
3737

3838
test('encrypt/decrypt buffer (aesgcm)', function() {
39-
assert(encryptDecrypt(new Buffer('hello'), webPush.supportedContentEncodings.AES_GCM).equals(new Buffer('hello')));
39+
assert(encryptDecrypt(Buffer.from('hello'), webPush.supportedContentEncodings.AES_GCM).equals(Buffer.from('hello')));
4040
});
4141

4242
test('encrypt/decrypt buffer (aes128gcm)', function() {
43-
assert(encryptDecrypt(new Buffer('hello'), webPush.supportedContentEncodings.AES_128_GCM).equals(new Buffer('hello')));
43+
assert(encryptDecrypt(Buffer.from('hello'), webPush.supportedContentEncodings.AES_128_GCM).equals(Buffer.from('hello')));
4444
});
4545

4646
// userPublicKey, userAuth, payload

test/test-generate-request-details.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ suite('Test Generate Request Details', function() {
128128
requestOptions: {
129129
subscription: {
130130
keys: {
131-
p256dh: urlBase64.encode(Buffer.concat([userPublicKey, new Buffer(1)])),
131+
p256dh: urlBase64.encode(Buffer.concat([userPublicKey, Buffer.alloc(1)])),
132132
auth: urlBase64.encode(userAuth)
133133
}
134134
},

test/test-set-vapid-details.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ const webPush = require('../src/index');
66

77
const VALID_SUBJECT_MAILTO = 'mailto: [email protected]';
88
const VALID_SUBJECT_URL = 'https://exampe.com/contact';
9-
const VALID_PUBLIC_KEY = urlBase64.encode(new Buffer(65));
10-
const VALID_PRIVATE_KEY = urlBase64.encode(new Buffer(32));
9+
const VALID_PUBLIC_KEY = urlBase64.encode(Buffer.alloc(65));
10+
const VALID_PRIVATE_KEY = urlBase64.encode(Buffer.alloc(32));
1111

1212
suite('setVapidDetails()', function() {
1313
test('is defined', function() {
@@ -55,7 +55,7 @@ suite('setVapidDetails()', function() {
5555
},
5656
{
5757
subject: VALID_SUBJECT_URL,
58-
publicKey: urlBase64.encode(new Buffer(60)),
58+
publicKey: urlBase64.encode(Buffer.alloc(60)),
5959
privateKey: VALID_PRIVATE_KEY
6060
},
6161
{
@@ -81,7 +81,7 @@ suite('setVapidDetails()', function() {
8181
{
8282
subject: VALID_SUBJECT_URL,
8383
publicKey: VALID_PUBLIC_KEY,
84-
privateKey: urlBase64.encode(new Buffer(60))
84+
privateKey: urlBase64.encode(Buffer.alloc(60))
8585
},
8686
{
8787
subject: VALID_SUBJECT_URL,

test/test-vapid-helper.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ const vapidHelper = require('../src/vapid-helper');
1010
const VALID_AUDIENCE = 'https://example.com';
1111
const VALID_SUBJECT_MAILTO = 'mailto: [email protected]';
1212
const VALID_SUBJECT_URL = 'https://exampe.com/contact';
13-
const VALID_PUBLIC_KEY = urlBase64.encode(new Buffer(65));
14-
const VALID_PRIVATE_KEY = urlBase64.encode(new Buffer(32));
13+
const VALID_PUBLIC_KEY = urlBase64.encode(Buffer.alloc(65));
14+
const VALID_PRIVATE_KEY = urlBase64.encode(Buffer.alloc(32));
1515
const VALID_CONTENT_ENCODING = webPush.supportedContentEncodings.AES_GCM;
1616
const VALID_EXPIRATION = Math.floor(Date.now() / 1000) + (60 * 60 * 12);
1717

@@ -47,8 +47,8 @@ suite('Test Vapid Helpers', function() {
4747
sandbox.stub(crypto, 'createECDH').callsFake(() => {
4848
return {
4949
generateKeys: () => {},
50-
getPublicKey: () => new Buffer(60),
51-
getPrivateKey: () => new Buffer(27)
50+
getPublicKey: () => Buffer.alloc(60),
51+
getPrivateKey: () => Buffer.alloc(27)
5252
};
5353
});
5454

test/testHTTPECE.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ suite('http_ece', function() {
1212
}
1313

1414
test('aesgcm - padSize 2 - pad 0', function() {
15-
const input = new Buffer(urlBase64.encode('marco'));
15+
const input = Buffer.from(urlBase64.encode('marco'));
1616

1717
const receiverCurve = crypto.createECDH('prime256v1');
1818
receiverCurve.setPrivateKey('a4C8H+f9IWtbAbTTkL2AgQ7xo/tqXddWWw7R2CR5OME=', 'base64');
@@ -34,7 +34,7 @@ suite('http_ece', function() {
3434
salt: salt,
3535
authSecret: authSecret
3636
});
37-
assert(encrypted.equals(new Buffer('Np3XM0NFvnxothcJfFzrv8Vsprn_k9I', 'base64')));
37+
assert(encrypted.equals(Buffer.from('Np3XM0NFvnxothcJfFzrv8Vsprn_k9I', 'base64')));
3838

3939
const decrypted = ece.decrypt(encrypted, {
4040
keyid: 'receiver',
@@ -46,7 +46,7 @@ suite('http_ece', function() {
4646
});
4747

4848
test('aesgcm - padSize 2 - pad 1', function() {
49-
const input = new Buffer(urlBase64.encode('marco'));
49+
const input = Buffer.from(urlBase64.encode('marco'));
5050

5151
const receiverCurve = crypto.createECDH('prime256v1');
5252
receiverCurve.setPrivateKey('a4C8H+f9IWtbAbTTkL2AgQ7xo/tqXddWWw7R2CR5OME=', 'base64');
@@ -69,7 +69,7 @@ suite('http_ece', function() {
6969
authSecret: authSecret,
7070
pad: 1
7171
});
72-
assert(encrypted.equals(new Buffer('Npy6P1BUsvRGMWEbwYq2JArF0l9YD38o', 'base64')));
72+
assert(encrypted.equals(Buffer.from('Npy6P1BUsvRGMWEbwYq2JArF0l9YD38o', 'base64')));
7373

7474
const decrypted = ece.decrypt(encrypted, {
7575
keyid: 'receiver',

test/testSendNotification.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ suite('sendNotification', function() {
6969
};
7070

7171
server = https.createServer(options, function(req, res) {
72-
requestBody = new Buffer(0);
72+
requestBody = Buffer.alloc(0);
7373

7474
req.on('data', function(chunk) {
7575
requestBody = Buffer.concat([requestBody, chunk]);
@@ -156,7 +156,7 @@ suite('sendNotification', function() {
156156
authSecret: urlBase64.encode(userAuth)
157157
});
158158

159-
assert(decrypted.equals(new Buffer(options.message)), 'Check cipher text can be correctly decoded');
159+
assert(decrypted.equals(Buffer.from(options.message)), 'Check cipher text can be correctly decoded');
160160
}
161161

162162
if (options.vapid) {
@@ -227,7 +227,7 @@ suite('sendNotification', function() {
227227
subscription: {
228228
keys: VALID_KEYS
229229
},
230-
message: new Buffer('hello')
230+
message: Buffer.from('hello')
231231
}
232232
}, {
233233
testTitle: 'send/receive unicode character',
@@ -586,7 +586,7 @@ suite('sendNotification', function() {
586586
requestOptions: {
587587
subscription: {
588588
keys: {
589-
p256dh: urlBase64.encode(Buffer.concat([userPublicKey, new Buffer(1)])),
589+
p256dh: urlBase64.encode(Buffer.concat([userPublicKey, Buffer.alloc(1)])),
590590
auth: urlBase64.encode(userAuth)
591591
}
592592
},

0 commit comments

Comments
 (0)