{"action":"opened","issue":{"url":"https://api.github.com/repos/firebase/firebase-js-sdk/issues/503","repository_url":"https://api.github.com/repos/firebase/firebase-js-sdk","labels_url":"https://api.github.com/repos/firebase/firebase-js-sdk/issues/503/labels{/name}","comments_url":"https://api.github.com/repos/firebase/firebase-js-sdk/issues/503/comments","events_url":"https://api.github.com/repos/firebase/firebase-js-sdk/issues/503/events","html_url":"https://github.com/firebase/firebase-js-sdk/issues/503","id":295911645,"number":503,"title":"messaging.getToken() throws in service worker scope","user":{"login":"acostalima","id":2727522,"avatar_url":"https://avatars0.githubusercontent.com/u/2727522?v=4","gravatar_id":"","url":"https://api.github.com/users/acostalima","html_url":"https://github.com/acostalima","followers_url":"https://api.github.com/users/acostalima/followers","following_url":"https://api.github.com/users/acostalima/following{/other_user}","gists_url":"https://api.github.com/users/acostalima/gists{/gist_id}","starred_url":"https://api.github.com/users/acostalima/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/acostalima/subscriptions","organizations_url":"https://api.github.com/users/acostalima/orgs","repos_url":"https://api.github.com/users/acostalima/repos","events_url":"https://api.github.com/users/acostalima/events{/privacy}","received_events_url":"https://api.github.com/users/acostalima/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-02-09T15:37:49Z","updated_at":"2018-02-09T15:37:49Z","closed_at":null,"author_association":"NONE","body":"<!-- Step 1 [READ THIS] -->\r\n<!--\r\nAre you in the right place?\r\n  * For issues or feature requests related to __the code in this repository__\r\n    file a Github issue.\r\n    * If this is a __feature request__ make sure the issue title starts with \"FR:\".\r\n  * For general technical questions, post a question on [StackOverflow](http://stackoverflow.com/)\r\n    with the firebase tag.\r\n  * For general Firebase discussion, use the [firebase-talk](https://groups.google.com/forum/#!forum/firebase-talk)\r\n    google group.\r\n  * For help troubleshooting your application that does not fall under one\r\n    of the above categories, reach out to the personalized\r\n    [Firebase support channel](https://firebase.google.com/support/).\r\n-->\r\n\r\n<!-- Step 2 -->\r\n\r\n### Describe your environment\r\n\r\n  * Operating System version: MacOS High Sierra 10.13\r\n  * Firebase SDK version: 4.9.0\r\n  * Firebase Product: messaging\r\n\r\n<!-- Step 3 -->\r\n\r\n### Describe the problem\r\n\r\nAccording to the error trace I get when I call `messaging.getToken()` in service worker scope, the root of the error lies in a call to `window.btoa()` in which `window` is, obviously, not defined. The culprit can be found in a helper function in [array-buffer-to-base64.ts](https://github.com/firebase/firebase-js-sdk/blob/7a611b645fbedcd6c732853ed19e3f6a1a2638d5/packages/messaging/src/helpers/array-buffer-to-base64.ts#L18):\r\n\r\n```js\r\nfunction toBase64(arrayBuffer) {\r\n   const uint8Version = new Uint8Array(arrayBuffer);\r\n   return window.btoa(String.fromCharCode.apply(null, uint8Version));\r\n}\r\n```\r\n\r\nAfter some experimentation, I found out that if I define the following\r\n\r\n```js\r\nconst btoaCopy = self.btoa; // self is ServiceWorkerGlobalScope\r\nself.window = {\r\n   btoa: (...args) => btoaCopy.call(self, ...args)\r\n};\r\n```\r\nbefore calling `messaging.getToken()`, no error is thrown and the returned token string seems to be exactly the same as the one retrieved in the client.\r\n\r\nIs this intended behavior, i.e., is `messaging.getToken()` not supposed to be called in service worker scope? If not, this should be an easy fix because `btoa` also defined in `ServiceWorkerGlobalScope`.\r\n\r\n#### Steps to reproduce:\r\n\r\n1. Ensure you are in service worker scope\r\n2. Setup Firebase with `firebase.initializeApp()`\r\n3. Retrieve messaging instance: `const messaging = firebase.messaging()`\r\n4. Call `messaging.getToken()`\r\n\r\n#### Relevant Code:\r\n\r\nNo code.\r\n\r\n"}}
{"actor":{"display_login":"acostalima"}}