Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed from commonJS module to ESM. #877

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"env": {
"es6": true
},
"parserOptions": {
"sourceType": "module"
},
"rules": {
"no-var": 2,
"no-const-assign": 2,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "web-push",
"version": "3.6.6",
"description": "Web Push library for Node.js",
"main": "src/index.js",
"type": "module",
"exports": "./src/index.js",
"bin": {
"web-push": "src/cli.js"
},
Expand Down
44 changes: 23 additions & 21 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#! /usr/bin/env node
/* eslint consistent-return:0 */

'use strict';

const webPush = require('../src/index.js');
import * as webPush from '../src/index.js';
import minimist from 'minimist';

const printUsageDetails = () => {
const actions = [
Expand Down Expand Up @@ -80,7 +78,7 @@ const sendNotification = args => {
options.TTL = args.ttl;
}

if (argv['vapid-subject'] || argv['vapid-pubkey'] || argv['vapid-pvtkey']) {
if (args['vapid-subject'] || args['vapid-pubkey'] || args['vapid-pvtkey']) {
options.vapidDetails = {
subject: args['vapid-subject'] || null,
publicKey: args['vapid-pubkey'] || null,
Expand Down Expand Up @@ -112,20 +110,24 @@ const sendNotification = args => {
});
};

const action = process.argv[2];
const argv = require('minimist')(process.argv.slice(3));
switch (action) {
case 'send-notification':
if (!argv.endpoint) {
return printUsageDetails();
}
const executeCliAction = () => {
const action = process.argv[2];
const argv = minimist(process.argv.slice(3));
switch (action) {
case 'send-notification':
if (!argv.endpoint) {
return printUsageDetails();
}

sendNotification(argv);
break;
case 'generate-vapid-keys':
generateVapidKeys(argv.json || false);
break;
default:
printUsageDetails();
break;
}
};

sendNotification(argv);
break;
case 'generate-vapid-keys':
generateVapidKeys(argv.json || false);
break;
default:
printUsageDetails();
break;
}
executeCliAction();
14 changes: 4 additions & 10 deletions src/encryption-helper.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';
import crypto from 'crypto';
import ece from 'http_ece';

const crypto = require('crypto');
const ece = require('http_ece');

const encrypt = function(userPublicKey, userAuth, payload, contentEncoding) {
export function encrypt(userPublicKey, userAuth, payload, contentEncoding) {
if (!userPublicKey) {
throw new Error('No user public key provided for encryption.');
}
Expand Down Expand Up @@ -55,8 +53,4 @@ const encrypt = function(userPublicKey, userAuth, payload, contentEncoding) {
salt: salt,
cipherText: cipherText
};
};

module.exports = {
encrypt: encrypt
};
}
36 changes: 19 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
'use strict';

const vapidHelper = require('./vapid-helper.js');
const encryptionHelper = require('./encryption-helper.js');
const WebPushLib = require('./web-push-lib.js');
const WebPushError = require('./web-push-error.js');
const WebPushConstants = require('./web-push-constants.js');
import { getVapidHeaders, generateVAPIDKeys } from './vapid-helper.js';
import { encrypt } from './encryption-helper.js';
import { WebPushLib } from './web-push-lib.js';
import WebPushError from './web-push-error.js';
import WebPushConstants from './web-push-constants.js';

const webPush = new WebPushLib();

module.exports = {
WebPushError: WebPushError,
supportedContentEncodings: WebPushConstants.supportedContentEncodings,
encrypt: encryptionHelper.encrypt,
getVapidHeaders: vapidHelper.getVapidHeaders,
generateVAPIDKeys: vapidHelper.generateVAPIDKeys,
setGCMAPIKey: webPush.setGCMAPIKey,
setVapidDetails: webPush.setVapidDetails,
generateRequestDetails: webPush.generateRequestDetails,
sendNotification: webPush.sendNotification.bind(webPush)
const { supportedContentEncodings } = WebPushConstants;
const { setGCMAPIKey, setVapidDetails, generateRequestDetails } = webPush;
const sendNotification = webPush.sendNotification.bind(webPush);

export {
WebPushError,
supportedContentEncodings,
encrypt,
getVapidHeaders,
generateVAPIDKeys,
setGCMAPIKey,
setVapidDetails,
generateRequestDetails,
sendNotification
};
8 changes: 1 addition & 7 deletions src/urlsafe-base64-helper.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
'use strict';

/**
* @param {string} base64
* @returns {boolean}
*/
function validate(base64) {
export function validate(base64) {
return /^[A-Za-z0-9\-_]+$/.test(base64);
}

module.exports = {
validate: validate
};
38 changes: 13 additions & 25 deletions src/vapid-helper.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict';
import crypto from 'crypto';
import asn1 from 'asn1.js';
import jws from 'jws';
import { URL } from 'url';

const crypto = require('crypto');
const asn1 = require('asn1.js');
const jws = require('jws');
const { URL } = require('url');

const WebPushConstants = require('./web-push-constants.js');
const urlBase64Helper = require('./urlsafe-base64-helper');
import WebPushConstants from './web-push-constants.js';
import * as urlBase64Helper from './urlsafe-base64-helper.js';

/**
* DEFAULT_EXPIRATION is set to seconds in 12 hours
Expand Down Expand Up @@ -37,7 +35,7 @@ function toPEM(key) {
});
}

function generateVAPIDKeys() {
export function generateVAPIDKeys() {
const curve = crypto.createECDH('prime256v1');
curve.generateKeys();

Expand Down Expand Up @@ -65,7 +63,7 @@ function generateVAPIDKeys() {
};
}

function validateSubject(subject) {
export function validateSubject(subject) {
if (!subject) {
throw new Error('No subject set in vapidDetails.subject.');
}
Expand All @@ -91,7 +89,7 @@ function validateSubject(subject) {
}
}

function validatePublicKey(publicKey) {
export function validatePublicKey(publicKey) {
if (!publicKey) {
throw new Error('No key set vapidDetails.publicKey');
}
Expand All @@ -112,7 +110,7 @@ function validatePublicKey(publicKey) {
}
}

function validatePrivateKey(privateKey) {
export function validatePrivateKey(privateKey) {
if (!privateKey) {
throw new Error('No key set in vapidDetails.privateKey');
}
Expand Down Expand Up @@ -141,7 +139,7 @@ function validatePrivateKey(privateKey) {
* @param {Number} numSeconds Number of seconds to be added
* @return {Number} Future expiration in seconds
*/
function getFutureExpirationTimestamp(numSeconds) {
export function getFutureExpirationTimestamp(numSeconds) {
const futureExp = new Date();
futureExp.setSeconds(futureExp.getSeconds() + numSeconds);
return Math.floor(futureExp.getTime() / 1000);
Expand All @@ -153,7 +151,7 @@ function getFutureExpirationTimestamp(numSeconds) {
*
* @param {Number} expiration Expiration seconds from Epoch to be validated
*/
function validateExpiration(expiration) {
export function validateExpiration(expiration) {
if (!Number.isInteger(expiration)) {
throw new Error('`expiration` value must be a number');
}
Expand Down Expand Up @@ -184,7 +182,7 @@ function validateExpiration(expiration) {
* @return {Object} Returns an Object with the Authorization and
* 'Crypto-Key' values to be used as headers.
*/
function getVapidHeaders(audience, subject, publicKey, privateKey, contentEncoding, expiration) {
export function getVapidHeaders(audience, subject, publicKey, privateKey, contentEncoding, expiration) {
if (!audience) {
throw new Error('No audience could be generated for VAPID.');
}
Expand Down Expand Up @@ -243,13 +241,3 @@ function getVapidHeaders(audience, subject, publicKey, privateKey, contentEncodi

throw new Error('Unsupported encoding type specified.');
}

module.exports = {
generateVAPIDKeys: generateVAPIDKeys,
getFutureExpirationTimestamp: getFutureExpirationTimestamp,
getVapidHeaders: getVapidHeaders,
validateSubject: validateSubject,
validatePublicKey: validatePublicKey,
validatePrivateKey: validatePrivateKey,
validateExpiration: validateExpiration
};
4 changes: 1 addition & 3 deletions src/web-push-constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

const WebPushConstants = {};

WebPushConstants.supportedContentEncodings = {
Expand All @@ -14,4 +12,4 @@ WebPushConstants.supportedUrgency = {
HIGH: 'high'
};

module.exports = WebPushConstants;
export default WebPushConstants;
8 changes: 3 additions & 5 deletions src/web-push-error.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
import util from 'util';

function WebPushError(message, statusCode, headers, body, endpoint) {
export default function WebPushError(message, statusCode, headers, body, endpoint) {
Error.captureStackTrace(this, this.constructor);

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

require('util').inherits(WebPushError, Error);

module.exports = WebPushError;
util.inherits(WebPushError, Error);
26 changes: 10 additions & 16 deletions src/web-push-lib.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
'use strict';
import url from 'url';
import https from 'https';

const url = require('url');
const https = require('https');

const WebPushError = require('./web-push-error.js');
const vapidHelper = require('./vapid-helper.js');
const encryptionHelper = require('./encryption-helper.js');
const webPushConstants = require('./web-push-constants.js');
const urlBase64Helper = require('./urlsafe-base64-helper');
import WebPushError from './web-push-error.js';
import * as vapidHelper from './vapid-helper.js';
import * as encryptionHelper from './encryption-helper.js';
import webPushConstants from './web-push-constants.js';
import * as urlBase64Helper from './urlsafe-base64-helper.js';

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

let gcmAPIKey = '';
let vapidDetails;

function WebPushLib() {
export function WebPushLib() {

}

Expand Down Expand Up @@ -405,9 +403,5 @@ WebPushLib.prototype.sendNotification = function(subscription, payload, options)
if (requestDetails.body) {
pushRequest.write(requestDetails.body);
}

pushRequest.end();
});
};

module.exports = WebPushLib;
});
};
3 changes: 3 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"rules": {
"max-len": 0,
"global-require": 0
},
"parserOptions": {
"sourceType": "module"
}
}
2 changes: 0 additions & 2 deletions test/data/demo/service-worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* Needed because of https://github.com/airbnb/javascript/issues/1632 */
/* eslint no-restricted-globals: 0 */

'use strict';

let port;
let pushMessage;

Expand Down
14 changes: 5 additions & 9 deletions test/helpers/create-server.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict';
import http from 'http';
import portfinder from 'portfinder';
import fs from 'fs';
import path from 'path';

const http = require('http');
const portfinder = require('portfinder');
const fs = require('fs');
const path = require('path');

function createServer() {
export function createServer() {
const demoPath = 'test/data/demo';

const server = http.createServer(function(req, res) {
Expand Down Expand Up @@ -55,5 +53,3 @@ function createServer() {
});
});
}

module.exports = createServer;
6 changes: 2 additions & 4 deletions test/helpers/download-test-browsers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const os = require('os');
const seleniumAssistant = require('selenium-assistant');
import os from 'os';
import seleniumAssistant from 'selenium-assistant';

const MAX_RETRIES = 3;
let expiration;
Expand Down
6 changes: 2 additions & 4 deletions test/test-cli.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
import assert from 'assert';
import { spawn } from 'child_process';

(function() {
const invalidNodeVersions = /0.(10|12).(\d+)/;
Expand All @@ -7,9 +8,6 @@
return;
}

const assert = require('assert');
const spawn = require('child_process').spawn;

const cliPath = 'src/cli.js';

suite('Test CLI', function() {
Expand Down
Loading