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
@@ -16,7 +14,7 @@ const DEFAULT_EXPIRATION_SECONDS = 12 * 60 * 60;
1614// Maximum expiration is 24 hours according. (See VAPID spec)
1715const MAX_EXPIRATION_SECONDS = 24 * 60 * 60 ;
1816
19- const ECPrivateKeyASN = asn1 . define ( 'ECPrivateKey' , function ( ) {
17+ const ECPrivateKeyASN = asn1 . define ( 'ECPrivateKey' , function ( ) {
2018 this . seq ( ) . obj (
2119 this . key ( 'version' ) . int ( ) ,
2220 this . key ( 'privateKey' ) . octstr ( ) ,
@@ -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,14 +63,14 @@ 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 }
7270
7371 if ( typeof subject !== 'string' || subject . length === 0 ) {
7472 throw new Error ( 'The subject value must be a string containing an https: URL or '
75- + 'mailto: address. ' + subject ) ;
73+ + 'mailto: address. ' + subject ) ;
7674 }
7775
7876 let subjectParseResult = null ;
@@ -88,17 +86,17 @@ function validateSubject(subject) {
8886 console . warn ( 'Vapid subject points to a localhost web URI, which is unsupported by '
8987 + 'Apple\'s push notification server and will result in a BadJwtToken error when '
9088 + 'sending notifications.' ) ;
91- }
89+ }
9290}
9391
94- function validatePublicKey ( publicKey ) {
92+ export function validatePublicKey ( publicKey ) {
9593 if ( ! publicKey ) {
9694 throw new Error ( 'No key set vapidDetails.publicKey' ) ;
9795 }
9896
9997 if ( typeof publicKey !== 'string' ) {
10098 throw new Error ( 'Vapid public key is must be a URL safe Base 64 '
101- + 'encoded string.' ) ;
99+ + 'encoded string.' ) ;
102100 }
103101
104102 if ( ! urlBase64Helper . validate ( publicKey ) ) {
@@ -112,14 +110,14 @@ 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 }
119117
120118 if ( typeof privateKey !== 'string' ) {
121119 throw new Error ( 'Vapid private key must be a URL safe Base 64 '
122- + 'encoded string.' ) ;
120+ + 'encoded string.' ) ;
123121 }
124122
125123 if ( ! urlBase64Helper . validate ( privateKey ) ) {
@@ -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,14 +182,14 @@ 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 }
191189
192190 if ( typeof audience !== 'string' || audience . length === 0 ) {
193191 throw new Error ( 'The audience value must be a string containing the '
194- + 'origin of a push service. ' + audience ) ;
192+ + 'origin of a push service. ' + audience ) ;
195193 }
196194
197195 try {
@@ -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- } ;
0 commit comments