Skip to content

Commit 36cd5e9

Browse files
committed
Changed from commonJS module to ESM.
1 parent f4ba922 commit 36cd5e9

22 files changed

+146
-194
lines changed

.eslintrc

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"env": {
44
"es6": true
55
},
6+
"parserOptions": {
7+
"sourceType": "module"
8+
},
69
"rules": {
710
"no-var": 2,
811
"no-const-assign": 2,

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"name": "web-push",
33
"version": "3.6.6",
44
"description": "Web Push library for Node.js",
5-
"main": "src/index.js",
5+
"type": "module",
6+
"exports": "./src/index.js",
67
"bin": {
78
"web-push": "src/cli.js"
89
},

src/cli.js

+23-21
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#! /usr/bin/env node
22
/* eslint consistent-return:0 */
3-
4-
'use strict';
5-
6-
const webPush = require('../src/index.js');
3+
import * as webPush from '../src/index.js';
4+
import minimist from 'minimist';
75

86
const printUsageDetails = () => {
97
const actions = [
@@ -80,7 +78,7 @@ const sendNotification = args => {
8078
options.TTL = args.ttl;
8179
}
8280

83-
if (argv['vapid-subject'] || argv['vapid-pubkey'] || argv['vapid-pvtkey']) {
81+
if (args['vapid-subject'] || args['vapid-pubkey'] || args['vapid-pvtkey']) {
8482
options.vapidDetails = {
8583
subject: args['vapid-subject'] || null,
8684
publicKey: args['vapid-pubkey'] || null,
@@ -112,20 +110,24 @@ const sendNotification = args => {
112110
});
113111
};
114112

115-
const action = process.argv[2];
116-
const argv = require('minimist')(process.argv.slice(3));
117-
switch (action) {
118-
case 'send-notification':
119-
if (!argv.endpoint) {
120-
return printUsageDetails();
121-
}
113+
const executeCliAction = () => {
114+
const action = process.argv[2];
115+
const argv = minimist(process.argv.slice(3));
116+
switch (action) {
117+
case 'send-notification':
118+
if (!argv.endpoint) {
119+
return printUsageDetails();
120+
}
121+
122+
sendNotification(argv);
123+
break;
124+
case 'generate-vapid-keys':
125+
generateVapidKeys(argv.json || false);
126+
break;
127+
default:
128+
printUsageDetails();
129+
break;
130+
}
131+
};
122132

123-
sendNotification(argv);
124-
break;
125-
case 'generate-vapid-keys':
126-
generateVapidKeys(argv.json || false);
127-
break;
128-
default:
129-
printUsageDetails();
130-
break;
131-
}
133+
executeCliAction();

src/encryption-helper.js

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
1+
import crypto from 'crypto';
2+
import ece from 'http_ece';
23

3-
const crypto = require('crypto');
4-
const ece = require('http_ece');
5-
6-
const encrypt = function(userPublicKey, userAuth, payload, contentEncoding) {
4+
export function encrypt(userPublicKey, userAuth, payload, contentEncoding) {
75
if (!userPublicKey) {
86
throw new Error('No user public key provided for encryption.');
97
}
@@ -55,8 +53,4 @@ const encrypt = function(userPublicKey, userAuth, payload, contentEncoding) {
5553
salt: salt,
5654
cipherText: cipherText
5755
};
58-
};
59-
60-
module.exports = {
61-
encrypt: encrypt
62-
};
56+
}

src/index.js

+19-17
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
'use strict';
2-
3-
const vapidHelper = require('./vapid-helper.js');
4-
const encryptionHelper = require('./encryption-helper.js');
5-
const WebPushLib = require('./web-push-lib.js');
6-
const WebPushError = require('./web-push-error.js');
7-
const WebPushConstants = require('./web-push-constants.js');
1+
import { getVapidHeaders, generateVAPIDKeys } from './vapid-helper.js';
2+
import { encrypt } from './encryption-helper.js';
3+
import { WebPushLib } from './web-push-lib.js';
4+
import WebPushError from './web-push-error.js';
5+
import WebPushConstants from './web-push-constants.js';
86

97
const webPush = new WebPushLib();
108

11-
module.exports = {
12-
WebPushError: WebPushError,
13-
supportedContentEncodings: WebPushConstants.supportedContentEncodings,
14-
encrypt: encryptionHelper.encrypt,
15-
getVapidHeaders: vapidHelper.getVapidHeaders,
16-
generateVAPIDKeys: vapidHelper.generateVAPIDKeys,
17-
setGCMAPIKey: webPush.setGCMAPIKey,
18-
setVapidDetails: webPush.setVapidDetails,
19-
generateRequestDetails: webPush.generateRequestDetails,
20-
sendNotification: webPush.sendNotification.bind(webPush)
9+
const { supportedContentEncodings } = WebPushConstants;
10+
const { setGCMAPIKey, setVapidDetails, generateRequestDetails } = webPush;
11+
const sendNotification = webPush.sendNotification.bind(webPush);
12+
13+
export {
14+
WebPushError,
15+
supportedContentEncodings,
16+
encrypt,
17+
getVapidHeaders,
18+
generateVAPIDKeys,
19+
setGCMAPIKey,
20+
setVapidDetails,
21+
generateRequestDetails,
22+
sendNotification
2123
};

src/urlsafe-base64-helper.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
'use strict';
2-
31
/**
42
* @param {string} base64
53
* @returns {boolean}
64
*/
7-
function validate(base64) {
5+
export function validate(base64) {
86
return /^[A-Za-z0-9\-_]+$/.test(base64);
97
}
10-
11-
module.exports = {
12-
validate: validate
13-
};

src/vapid-helper.js

+13-25
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
'use strict';
1+
import crypto from 'crypto';
2+
import asn1 from 'asn1.js';
3+
import jws from 'jws';
4+
import { URL } from 'url';
25

3-
const crypto = require('crypto');
4-
const asn1 = require('asn1.js');
5-
const jws = require('jws');
6-
const { URL } = require('url');
7-
8-
const WebPushConstants = require('./web-push-constants.js');
9-
const urlBase64Helper = require('./urlsafe-base64-helper');
6+
import WebPushConstants from './web-push-constants.js';
7+
import * as urlBase64Helper from './urlsafe-base64-helper.js';
108

119
/**
1210
* DEFAULT_EXPIRATION is set to seconds in 12 hours
@@ -37,7 +35,7 @@ function toPEM(key) {
3735
});
3836
}
3937

40-
function generateVAPIDKeys() {
38+
export function generateVAPIDKeys() {
4139
const curve = crypto.createECDH('prime256v1');
4240
curve.generateKeys();
4341

@@ -65,7 +63,7 @@ function generateVAPIDKeys() {
6563
};
6664
}
6765

68-
function validateSubject(subject) {
66+
export function validateSubject(subject) {
6967
if (!subject) {
7068
throw new Error('No subject set in vapidDetails.subject.');
7169
}
@@ -91,7 +89,7 @@ function validateSubject(subject) {
9189
}
9290
}
9391

94-
function validatePublicKey(publicKey) {
92+
export function validatePublicKey(publicKey) {
9593
if (!publicKey) {
9694
throw new Error('No key set vapidDetails.publicKey');
9795
}
@@ -112,7 +110,7 @@ function validatePublicKey(publicKey) {
112110
}
113111
}
114112

115-
function validatePrivateKey(privateKey) {
113+
export function validatePrivateKey(privateKey) {
116114
if (!privateKey) {
117115
throw new Error('No key set in vapidDetails.privateKey');
118116
}
@@ -141,7 +139,7 @@ function validatePrivateKey(privateKey) {
141139
* @param {Number} numSeconds Number of seconds to be added
142140
* @return {Number} Future expiration in seconds
143141
*/
144-
function getFutureExpirationTimestamp(numSeconds) {
142+
export function getFutureExpirationTimestamp(numSeconds) {
145143
const futureExp = new Date();
146144
futureExp.setSeconds(futureExp.getSeconds() + numSeconds);
147145
return Math.floor(futureExp.getTime() / 1000);
@@ -153,7 +151,7 @@ function getFutureExpirationTimestamp(numSeconds) {
153151
*
154152
* @param {Number} expiration Expiration seconds from Epoch to be validated
155153
*/
156-
function validateExpiration(expiration) {
154+
export function validateExpiration(expiration) {
157155
if (!Number.isInteger(expiration)) {
158156
throw new Error('`expiration` value must be a number');
159157
}
@@ -184,7 +182,7 @@ function validateExpiration(expiration) {
184182
* @return {Object} Returns an Object with the Authorization and
185183
* 'Crypto-Key' values to be used as headers.
186184
*/
187-
function getVapidHeaders(audience, subject, publicKey, privateKey, contentEncoding, expiration) {
185+
export function getVapidHeaders(audience, subject, publicKey, privateKey, contentEncoding, expiration) {
188186
if (!audience) {
189187
throw new Error('No audience could be generated for VAPID.');
190188
}
@@ -243,13 +241,3 @@ function getVapidHeaders(audience, subject, publicKey, privateKey, contentEncodi
243241

244242
throw new Error('Unsupported encoding type specified.');
245243
}
246-
247-
module.exports = {
248-
generateVAPIDKeys: generateVAPIDKeys,
249-
getFutureExpirationTimestamp: getFutureExpirationTimestamp,
250-
getVapidHeaders: getVapidHeaders,
251-
validateSubject: validateSubject,
252-
validatePublicKey: validatePublicKey,
253-
validatePrivateKey: validatePrivateKey,
254-
validateExpiration: validateExpiration
255-
};

src/web-push-constants.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
const WebPushConstants = {};
42

53
WebPushConstants.supportedContentEncodings = {
@@ -14,4 +12,4 @@ WebPushConstants.supportedUrgency = {
1412
HIGH: 'high'
1513
};
1614

17-
module.exports = WebPushConstants;
15+
export default WebPushConstants;

src/web-push-error.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
'use strict';
1+
import util from 'util';
22

3-
function WebPushError(message, statusCode, headers, body, endpoint) {
3+
export default function WebPushError(message, statusCode, headers, body, endpoint) {
44
Error.captureStackTrace(this, this.constructor);
55

66
this.name = this.constructor.name;
@@ -11,6 +11,4 @@ function WebPushError(message, statusCode, headers, body, endpoint) {
1111
this.endpoint = endpoint;
1212
}
1313

14-
require('util').inherits(WebPushError, Error);
15-
16-
module.exports = WebPushError;
14+
util.inherits(WebPushError, Error);

src/web-push-lib.js

+10-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
'use strict';
1+
import url from 'url';
2+
import https from 'https';
23

3-
const url = require('url');
4-
const https = require('https');
5-
6-
const WebPushError = require('./web-push-error.js');
7-
const vapidHelper = require('./vapid-helper.js');
8-
const encryptionHelper = require('./encryption-helper.js');
9-
const webPushConstants = require('./web-push-constants.js');
10-
const urlBase64Helper = require('./urlsafe-base64-helper');
4+
import WebPushError from './web-push-error.js';
5+
import * as vapidHelper from './vapid-helper.js';
6+
import * as encryptionHelper from './encryption-helper.js';
7+
import webPushConstants from './web-push-constants.js';
8+
import * as urlBase64Helper from './urlsafe-base64-helper.js';
119

1210
// Default TTL is four weeks.
1311
const DEFAULT_TTL = 2419200;
1412

1513
let gcmAPIKey = '';
1614
let vapidDetails;
1715

18-
function WebPushLib() {
16+
export function WebPushLib() {
1917

2018
}
2119

@@ -405,9 +403,5 @@ WebPushLib.prototype.sendNotification = function(subscription, payload, options)
405403
if (requestDetails.body) {
406404
pushRequest.write(requestDetails.body);
407405
}
408-
409-
pushRequest.end();
410-
});
411-
};
412-
413-
module.exports = WebPushLib;
406+
});
407+
};

test/.eslintrc

+3
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55
"rules": {
66
"max-len": 0,
77
"global-require": 0
8+
},
9+
"parserOptions": {
10+
"sourceType": "module"
811
}
912
}

test/data/demo/service-worker.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/* Needed because of https://github.com/airbnb/javascript/issues/1632 */
22
/* eslint no-restricted-globals: 0 */
33

4-
'use strict';
5-
64
let port;
75
let pushMessage;
86

test/helpers/create-server.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
'use strict';
1+
import http from 'http';
2+
import portfinder from 'portfinder';
3+
import fs from 'fs';
4+
import path from 'path';
25

3-
const http = require('http');
4-
const portfinder = require('portfinder');
5-
const fs = require('fs');
6-
const path = require('path');
7-
8-
function createServer() {
6+
export function createServer() {
97
const demoPath = 'test/data/demo';
108

119
const server = http.createServer(function(req, res) {
@@ -55,5 +53,3 @@ function createServer() {
5553
});
5654
});
5755
}
58-
59-
module.exports = createServer;

test/helpers/download-test-browsers.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict';
2-
3-
const os = require('os');
4-
const seleniumAssistant = require('selenium-assistant');
1+
import os from 'os';
2+
import seleniumAssistant from 'selenium-assistant';
53

64
const MAX_RETRIES = 3;
75
let expiration;

test/test-cli.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
'use strict';
1+
import assert from 'assert';
2+
import { spawn } from 'child_process';
23

34
(function() {
45
const invalidNodeVersions = /0.(10|12).(\d+)/;
@@ -7,9 +8,6 @@
78
return;
89
}
910

10-
const assert = require('assert');
11-
const spawn = require('child_process').spawn;
12-
1311
const cliPath = 'src/cli.js';
1412

1513
suite('Test CLI', function() {

0 commit comments

Comments
 (0)