1
- 'use strict' ;
1
+ import crypto from 'crypto' ;
2
+ import asn1 from 'asn1.js' ;
3
+ import jws from 'jws' ;
4
+ import { URL } from 'url' ;
2
5
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' ;
10
8
11
9
/**
12
10
* DEFAULT_EXPIRATION is set to seconds in 12 hours
@@ -37,7 +35,7 @@ function toPEM(key) {
37
35
} ) ;
38
36
}
39
37
40
- function generateVAPIDKeys ( ) {
38
+ export function generateVAPIDKeys ( ) {
41
39
const curve = crypto . createECDH ( 'prime256v1' ) ;
42
40
curve . generateKeys ( ) ;
43
41
@@ -65,7 +63,7 @@ function generateVAPIDKeys() {
65
63
} ;
66
64
}
67
65
68
- function validateSubject ( subject ) {
66
+ export function validateSubject ( subject ) {
69
67
if ( ! subject ) {
70
68
throw new Error ( 'No subject set in vapidDetails.subject.' ) ;
71
69
}
@@ -91,7 +89,7 @@ function validateSubject(subject) {
91
89
}
92
90
}
93
91
94
- function validatePublicKey ( publicKey ) {
92
+ export function validatePublicKey ( publicKey ) {
95
93
if ( ! publicKey ) {
96
94
throw new Error ( 'No key set vapidDetails.publicKey' ) ;
97
95
}
@@ -112,7 +110,7 @@ function validatePublicKey(publicKey) {
112
110
}
113
111
}
114
112
115
- function validatePrivateKey ( privateKey ) {
113
+ export function validatePrivateKey ( privateKey ) {
116
114
if ( ! privateKey ) {
117
115
throw new Error ( 'No key set in vapidDetails.privateKey' ) ;
118
116
}
@@ -141,7 +139,7 @@ function validatePrivateKey(privateKey) {
141
139
* @param {Number } numSeconds Number of seconds to be added
142
140
* @return {Number } Future expiration in seconds
143
141
*/
144
- function getFutureExpirationTimestamp ( numSeconds ) {
142
+ export function getFutureExpirationTimestamp ( numSeconds ) {
145
143
const futureExp = new Date ( ) ;
146
144
futureExp . setSeconds ( futureExp . getSeconds ( ) + numSeconds ) ;
147
145
return Math . floor ( futureExp . getTime ( ) / 1000 ) ;
@@ -153,7 +151,7 @@ function getFutureExpirationTimestamp(numSeconds) {
153
151
*
154
152
* @param {Number } expiration Expiration seconds from Epoch to be validated
155
153
*/
156
- function validateExpiration ( expiration ) {
154
+ export function validateExpiration ( expiration ) {
157
155
if ( ! Number . isInteger ( expiration ) ) {
158
156
throw new Error ( '`expiration` value must be a number' ) ;
159
157
}
@@ -184,7 +182,7 @@ function validateExpiration(expiration) {
184
182
* @return {Object } Returns an Object with the Authorization and
185
183
* 'Crypto-Key' values to be used as headers.
186
184
*/
187
- function getVapidHeaders ( audience , subject , publicKey , privateKey , contentEncoding , expiration ) {
185
+ export function getVapidHeaders ( audience , subject , publicKey , privateKey , contentEncoding , expiration ) {
188
186
if ( ! audience ) {
189
187
throw new Error ( 'No audience could be generated for VAPID.' ) ;
190
188
}
@@ -243,13 +241,3 @@ function getVapidHeaders(audience, subject, publicKey, privateKey, contentEncodi
243
241
244
242
throw new Error ( 'Unsupported encoding type specified.' ) ;
245
243
}
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