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- } ;
0 commit comments