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
@@ -16,7 +14,7 @@ const DEFAULT_EXPIRATION_SECONDS = 12 * 60 * 60;
16
14
// Maximum expiration is 24 hours according. (See VAPID spec)
17
15
const MAX_EXPIRATION_SECONDS = 24 * 60 * 60 ;
18
16
19
- const ECPrivateKeyASN = asn1 . define ( 'ECPrivateKey' , function ( ) {
17
+ const ECPrivateKeyASN = asn1 . define ( 'ECPrivateKey' , function ( ) {
20
18
this . seq ( ) . obj (
21
19
this . key ( 'version' ) . int ( ) ,
22
20
this . key ( 'privateKey' ) . octstr ( ) ,
@@ -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,14 +63,14 @@ 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
}
72
70
73
71
if ( typeof subject !== 'string' || subject . length === 0 ) {
74
72
throw new Error ( 'The subject value must be a string containing an https: URL or '
75
- + 'mailto: address. ' + subject ) ;
73
+ + 'mailto: address. ' + subject ) ;
76
74
}
77
75
78
76
let subjectParseResult = null ;
@@ -88,17 +86,17 @@ function validateSubject(subject) {
88
86
console . warn ( 'Vapid subject points to a localhost web URI, which is unsupported by '
89
87
+ 'Apple\'s push notification server and will result in a BadJwtToken error when '
90
88
+ 'sending notifications.' ) ;
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
}
98
96
99
97
if ( typeof publicKey !== 'string' ) {
100
98
throw new Error ( 'Vapid public key is must be a URL safe Base 64 '
101
- + 'encoded string.' ) ;
99
+ + 'encoded string.' ) ;
102
100
}
103
101
104
102
if ( ! urlBase64Helper . validate ( publicKey ) ) {
@@ -112,14 +110,14 @@ 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
}
119
117
120
118
if ( typeof privateKey !== 'string' ) {
121
119
throw new Error ( 'Vapid private key must be a URL safe Base 64 '
122
- + 'encoded string.' ) ;
120
+ + 'encoded string.' ) ;
123
121
}
124
122
125
123
if ( ! urlBase64Helper . validate ( privateKey ) ) {
@@ -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,14 +182,14 @@ 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
}
191
189
192
190
if ( typeof audience !== 'string' || audience . length === 0 ) {
193
191
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 ) ;
195
193
}
196
194
197
195
try {
@@ -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