Skip to content
This repository has been archived by the owner on Oct 12, 2021. It is now read-only.

Commit

Permalink
feat(worker): switch push notifications to use VAPID
Browse files Browse the repository at this point in the history
  • Loading branch information
alxhub committed May 11, 2017
1 parent 1b372f2 commit 950f99f
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 152 deletions.
10 changes: 0 additions & 10 deletions service-worker/worker/gulpfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ gulp.task('task:e2e_tests:debug', done => runSequence(
done));

gulp.task('task:e2e_tests:prep', done => runSequence(
'task:e2e_tests:config_check',
[
'task:commonjs:compile',
'task:esm:compile',
Expand All @@ -311,15 +310,6 @@ gulp.task('task:e2e_tests:prep', done => runSequence(
],
done));

gulp.task('task:e2e_tests:config_check', done => {
fs.exists('./ngsw-config.json', (exists) => {
if (!exists) {
throw `ERROR: can't run e2e tests without a ngsw-config.json file`;
}
done();
});
});

gulp.task('task:e2e_tests:copy_modules', () => gulp
.src([
'node_modules/@angular/**/*.js',
Expand Down
2 changes: 1 addition & 1 deletion service-worker/worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"ts-node": "^0.7.3",
"typescript": "^2.2.2",
"uglify-js": "^2.7.3",
"web-push": "^2.1.1",
"web-push": "^3.2.2",
"webdriver-manager": "^10.2.6",
"webpack": "^2.1.0-beta.22",
"zone.js": "^0.7.2"
Expand Down
22 changes: 20 additions & 2 deletions service-worker/worker/src/companion/comm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/take';
import 'rxjs/add/operator/takeWhile';

export interface PushOptions {
applicationServerKey?: string;
}

function fromPromise<T>(promiseFn: (() => Promise<T>)): Observable<T> {
return Observable.create(observer => {
promiseFn()
Expand Down Expand Up @@ -226,7 +230,7 @@ export class NgServiceWorker {
});
}

registerForPush(): Observable<NgPushRegistration> {
registerForPush(pushOptions: PushOptions = {}): Observable<NgPushRegistration> {
return this
// Wait for a controlling worker to exist.
.awaitSingleControllingWorker
Expand All @@ -251,8 +255,22 @@ export class NgServiceWorker {
return regFromSub(sub);
}
// No existing subscription, register (with userVisibleOnly: true).
let options = {
userVisibleOnly: true,
} as Object;
if (pushOptions.applicationServerKey) {
let key = atob(pushOptions
.applicationServerKey
.replace(/_/g, '/')
.replace(/-/g, '+'));
let applicationServerKey = new Uint8Array(new ArrayBuffer(key.length));
for (let i = 0; i < key.length; i++) {
applicationServerKey[i] = key.charCodeAt(i);
}
options['applicationServerKey'] = applicationServerKey;
}
return pushManager
.subscribe({userVisibleOnly: true})
.subscribe(options)
.then(regFromSub);
})
// Map from promises to the Observable being returned.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
"name": "Angular Service Worker Test Harness",
"short_name": "ngsw harness",
"start_url": "/index.html",
"display": "standalone",
"gcm_sender_id": "548552315733"
"display": "standalone"
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,9 @@ export class ControllerCmp {
case 'COMPANION_REG_PUSH':
this
.sw
.registerForPush()
.registerForPush({applicationServerKey: 'BLRl_fG1TCTc1D2JwzOpdZjaRcJucXtG8TAd5g9vuYjl6KUUDxgoRjQPCgjZfY-_Rusd_qtjNvanHXeFvOFlxH4'})
.subscribe(handler => {
this.result = JSON.stringify({
url: handler.url,
key: handler.key(),
auth: handler.auth()
});
this.result = JSON.stringify(handler.toJSON());
this.alert = true;
});
break;
Expand Down
30 changes: 13 additions & 17 deletions service-worker/worker/src/test/e2e/harness/server/push.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
declare interface WebPushParams {
TTL?: number;
userPublicKey?: Buffer;
userAuth?: Buffer;
payload?: any;
vapid?: any;
vapidDetails: {
subject: string;
publicKey: string;
privateKey: string;
};
}

declare interface WebPush {
sendNotification(endpoint: string, params: WebPushParams): Promise<any>;
setGCMAPIKey(key: string);
sendNotification(subscription: any, message: any, options: WebPushParams): Promise<any>;
}

let push: WebPush = require('web-push');

export function sendPush(reg: any, payload?: Object): Promise<any> {
let endpoint = reg.url;

return push.sendNotification(endpoint, {
userPublicKey: reg.key,
userAuth: reg.auth,
payload: new Buffer(JSON.stringify(payload))
export function sendPush(subscription: any, payload?: Object): Promise<any> {
return push.sendNotification(subscription, JSON.stringify(payload), {
vapidDetails: {
subject: 'mailto:test@angular.io',
publicKey: 'BLRl_fG1TCTc1D2JwzOpdZjaRcJucXtG8TAd5g9vuYjl6KUUDxgoRjQPCgjZfY-_Rusd_qtjNvanHXeFvOFlxH4',
privateKey: 't3JtFOflouvPUxFKeSSmZdjuVidnD_0dNGFM1v-N4PI',
},
});
}

export function setGCMAPIKey(key: string): void {
push.setGCMAPIKey(key);
}
18 changes: 1 addition & 17 deletions service-worker/worker/src/test/e2e/spec/sanity.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {create, Server} from '../harness/server/server';
import {sendPush, setGCMAPIKey} from '../harness/server/push';
import {sendPush} from '../harness/server/push';
import {HarnessPageObject} from '../harness/server/page-object';

import fs = require('fs');
Expand Down Expand Up @@ -101,22 +101,6 @@ function expectNoServiceWorker(): Promise<void> {
});
}

beforeAll(done => {
fs.exists('./ngsw-config.json', exists => {
if (!exists) {
throw 'Must have a ngsw-config.json file with a gcm_key property';
}
fs.readFile('./ngsw-config.json', 'utf8', (err, data) => {
let config = JSON.parse(data);
if (!config.hasOwnProperty('gcm_key')) {
throw 'Must have a ngsw-config.json file with a gcm_key property';
}
setGCMAPIKey(config['gcm_key']);
return done();
});
});
});

describe('world sanity', () => {
it('starts without a service worker', done => {
browser.get('/index.html');
Expand Down
105 changes: 8 additions & 97 deletions service-worker/worker/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -227,18 +227,11 @@ array-unique@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/array-unique/-/array-unique-0.2.1.tgz#a1d97ccafcbc2625cc70fadceb36a50c58b01a53"

array.prototype.find@^2.0.0:
version "2.0.3"
resolved "https://registry.yarnpkg.com/array.prototype.find/-/array.prototype.find-2.0.3.tgz#08c3ec33e32ec4bab362a2958e686ae92f59271d"
dependencies:
define-properties "^1.1.2"
es-abstract "^1.7.0"

arrify@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/arrify/-/arrify-1.0.1.tgz#898508da2226f380df904728456849c1501a4b0d"

asn1.js@^4.0.0, asn1.js@^4.5.2:
asn1.js@^4.0.0, asn1.js@^4.8.1:
version "4.9.1"
resolved "https://registry.yarnpkg.com/asn1.js/-/asn1.js-4.9.1.tgz#48ba240b45a9280e94748990ba597d216617fd40"
dependencies:
Expand Down Expand Up @@ -334,10 +327,6 @@ bluebird@^2.10.2:
version "2.11.0"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-2.11.0.tgz#534b9033c022c9579c56ba3b3e5a5caafbb650e1"

bluebird@^3.3.5:
version "3.4.7"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.4.7.tgz#f72d760be09b7f76d08ed8fae98b289a8d05fab3"

bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.1.1, bn.js@^4.4.0:
version "4.11.6"
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.6.tgz#53344adb14617a13f6e8dd2ce28905d1c0ba3215"
Expand Down Expand Up @@ -424,26 +413,10 @@ browserify-zlib@^0.1.4:
dependencies:
pako "~0.2.0"

buffer-compare-shim@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/buffer-compare-shim/-/buffer-compare-shim-1.0.0.tgz#72c0c593df4f3b5b18d76ed3f72aea3fbb201c68"
dependencies:
buffer-compare "0.0.1"

buffer-compare@0.0.1, buffer-compare@^0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/buffer-compare/-/buffer-compare-0.0.1.tgz#8f6a12c60ea53632e0b68ea71e196e39bd61697a"

buffer-equal-constant-time@1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz#f8e71132f7ffe6e01a5c9697a4c6f3e48d5cc819"

buffer-equals-polyfill@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/buffer-equals-polyfill/-/buffer-equals-polyfill-1.0.0.tgz#5ab0825c7c5b8c5eadbdcc7fae9ec009625e7feb"
dependencies:
buffer-compare "^0.0.1"

buffer-shims@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/buffer-shims/-/buffer-shims-1.0.0.tgz#9978ce317388c649ad8793028c3477ef044a8b51"
Expand Down Expand Up @@ -575,10 +548,6 @@ code-point-at@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77"

colors@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/colors/-/colors-1.1.2.tgz#168a4701756b6a7f51a12ce0c97bfa28c084ed63"

combined-stream@^1.0.5, combined-stream@~1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.5.tgz#938370a57b4a51dea2c77c15d5c5fdf895164009"
Expand Down Expand Up @@ -731,13 +700,6 @@ defaults@^1.0.0:
dependencies:
clone "^1.0.2"

define-properties@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/define-properties/-/define-properties-1.1.2.tgz#83a73f2fea569898fb737193c8f873caf6d45c94"
dependencies:
foreach "^2.0.5"
object-keys "^1.0.8"

del@^2.2.0:
version "2.2.2"
resolved "https://registry.yarnpkg.com/del/-/del-2.2.2.tgz#c12c981d067846c84bcaf862cff930d907ffd1a8"
Expand Down Expand Up @@ -866,23 +828,6 @@ error-ex@^1.2.0:
dependencies:
is-arrayish "^0.2.1"

es-abstract@^1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/es-abstract/-/es-abstract-1.7.0.tgz#dfade774e01bfcd97f96180298c449c8623fb94c"
dependencies:
es-to-primitive "^1.1.1"
function-bind "^1.1.0"
is-callable "^1.1.3"
is-regex "^1.0.3"

es-to-primitive@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/es-to-primitive/-/es-to-primitive-1.1.1.tgz#45355248a88979034b6792e19bb81f2b7975dd0d"
dependencies:
is-callable "^1.1.1"
is-date-object "^1.0.1"
is-symbol "^1.0.1"

escape-html@~1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/escape-html/-/escape-html-1.0.3.tgz#0258eae4d3d0c0974de1c169188ef0051d1d1988"
Expand Down Expand Up @@ -1057,10 +1002,6 @@ for-own@^0.1.4:
dependencies:
for-in "^0.1.5"

foreach@^2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/foreach/-/foreach-2.0.5.tgz#0bee005018aeb260d0a3af3ae658dd0136ec1b99"

forever-agent@~0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"
Expand Down Expand Up @@ -1123,10 +1064,6 @@ fstream@^1.0.0, fstream@^1.0.2, fstream@~1.0.10:
mkdirp ">=0.5 0"
rimraf "2"

function-bind@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/function-bind/-/function-bind-1.1.0.tgz#16176714c801798e4e8f2cf7f7529467bb4a5771"

gauge@~2.7.1:
version "2.7.3"
resolved "https://registry.yarnpkg.com/gauge/-/gauge-2.7.3.tgz#1c23855f962f17b3ad3d0dc7443f304542edfe09"
Expand Down Expand Up @@ -1501,7 +1438,7 @@ http-signature@~1.1.0:
jsprim "^1.2.2"
sshpk "^1.7.0"

http_ece@^0.5.1:
http_ece@^0.5.2:
version "0.5.2"
resolved "https://registry.yarnpkg.com/http_ece/-/http_ece-0.5.2.tgz#5654d7ec9d996b749ce00a276e18d54b6d8f905f"
dependencies:
Expand Down Expand Up @@ -1595,14 +1532,6 @@ is-builtin-module@^1.0.0:
dependencies:
builtin-modules "^1.0.0"

is-callable@^1.1.1, is-callable@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/is-callable/-/is-callable-1.1.3.tgz#86eb75392805ddc33af71c92a0eedf74ee7604b2"

is-date-object@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/is-date-object/-/is-date-object-1.0.1.tgz#9aa20eb6aeebbff77fbd33e74ca01b33581d3a16"

is-dotfile@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/is-dotfile/-/is-dotfile-1.0.2.tgz#2c132383f39199f8edc268ca01b9b007d205cc4d"
Expand Down Expand Up @@ -1682,20 +1611,12 @@ is-property@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/is-property/-/is-property-1.0.2.tgz#57fe1c4e48474edd65b09911f26b1cd4095dda84"

is-regex@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/is-regex/-/is-regex-1.0.3.tgz#0d55182bddf9f2fde278220aec3a75642c908637"

is-relative@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/is-relative/-/is-relative-0.2.1.tgz#d27f4c7d516d175fb610db84bbeef23c3bc97aa5"
dependencies:
is-unc-path "^0.1.1"

is-symbol@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/is-symbol/-/is-symbol-1.0.1.tgz#3cc59f00025194b6ab2e38dbae6689256b660572"

is-typedarray@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/is-typedarray/-/is-typedarray-1.0.0.tgz#e479c80858df0c1b11ddda6940f96011fcda4a9a"
Expand Down Expand Up @@ -2390,10 +2311,6 @@ object-assign@^4.0.1, object-assign@^4.1.0:
version "4.1.1"
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"

object-keys@^1.0.8:
version "1.0.11"
resolved "https://registry.yarnpkg.com/object-keys/-/object-keys-1.0.11.tgz#c54601778ad560f1142ce0e01bcca8b56d13426d"

object-keys@~0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/object-keys/-/object-keys-0.4.0.tgz#28a6aae7428dd2c3a92f3d95f21335dd204e0336"
Expand Down Expand Up @@ -3438,18 +3355,12 @@ watchpack@^1.2.0:
chokidar "^1.4.3"
graceful-fs "^4.1.2"

web-push@^2.1.1:
version "2.2.0"
resolved "https://registry.yarnpkg.com/web-push/-/web-push-2.2.0.tgz#17d6cdbc882cf52ddc43cb5d91de47b28f638574"
dependencies:
array.prototype.find "^2.0.0"
asn1.js "^4.5.2"
bluebird "^3.3.5"
buffer-compare-shim "^1.0.0"
buffer-equals-polyfill "^1.0.0"
colors "^1.1.2"
create-ecdh "^4.0.0"
http_ece "^0.5.1"
web-push@^3.2.2:
version "3.2.2"
resolved "https://registry.yarnpkg.com/web-push/-/web-push-3.2.2.tgz#d2f7c5590a3037cb50e4442b5117edd6475768a5"
dependencies:
asn1.js "^4.8.1"
http_ece "^0.5.2"
jws "^3.1.3"
minimist "^1.2.0"
urlsafe-base64 "^1.0.0"
Expand Down

0 comments on commit 950f99f

Please sign in to comment.