Skip to content

Commit 3a465b4

Browse files
authored
Create messaging-compat (#4544)
* Create messaging-compat Using the modular sdk to recreate legacy API surface * Modify based on review - Update licenses date to 2020 (from 2017, 2018) - Declare Module in `registerMessagingCompat.ts` - Update build rule (package.json): -- add `app-comapt` as a dev dependency * Fix build based on Christina's input Thanks Christina for the input here. Changes are - Add entry in changeset file - Add eslintrc.js to `messaging-compat` * Resolve lint issue surfaced from test - return type in `onMessage` * Add unit tests * Improve based on Fei's comments/feedbacks - Updated dependencies versions - Remove redundant namespace declarations
1 parent dfa20ff commit 3a465b4

13 files changed

+552
-0
lines changed

.changeset/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"@firebase/installations-exp",
2424
"@firebase/installations-compat",
2525
"@firebase/messaging-exp",
26+
"@firebase/messaging-compat",
2627
"@firebase/performance-exp",
2728
"@firebase/performance-compat",
2829
"@firebase/remote-config-exp",
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* eslint-disable @typescript-eslint/no-require-imports */
2+
/**
3+
* @license
4+
* Copyright 2020 Google LLC
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
const path = require('path');
19+
20+
module.exports = {
21+
extends: '../../config/.eslintrc.js',
22+
parserOptions: {
23+
project: 'tsconfig.json',
24+
// to make vscode-eslint work with monorepo
25+
// https://github.com/typescript-eslint/typescript-eslint/issues/251#issuecomment-463943250
26+
tsconfigRootDir: __dirname
27+
}
28+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# @firebase/messaging-compat
2+
3+
This is the compat package that recreates the v8 APIs.
4+
5+
**This package is not intended for direct usage, and should only be used via the officially supported [firebase](https://www.npmjs.com/package/firebase) package.**
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
const karmaBase = require('../../config/karma.base');
19+
20+
const files = ['test/**/*'];
21+
22+
module.exports = function (config) {
23+
const karmaConfig = {
24+
...karmaBase,
25+
files,
26+
frameworks: ['mocha']
27+
};
28+
29+
config.set(karmaConfig);
30+
};
31+
32+
module.exports.files = files;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"name": "@firebase/messaging-compat",
3+
"version": "0.0.900",
4+
"license": "Apache-2.0",
5+
"description": "",
6+
"private": true,
7+
"author": "Firebase <[email protected]> (https://firebase.google.com/)",
8+
"main": "dist/index.cjs.js",
9+
"module": "dist/index.esm.js",
10+
"esm2017": "dist/index.esm2017.js",
11+
"typings": "dist/index.d.ts",
12+
"sw": "dist/index.sw.esm2017.js",
13+
"files": [
14+
"dist"
15+
],
16+
"scripts": {
17+
"lint": "eslint -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
18+
"lint:fix": "eslint --fix -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
19+
"build": "rollup -c",
20+
"build:deps": "lerna run --scope @firebase/'messaging-compat' --include-dependencies build",
21+
"build:release": "rollup -c rollup.config.release.js",
22+
"dev": "rollup -c -w",
23+
"test": "run-p test:karma",
24+
"test:ci": "node ../../scripts/run_tests_in_ci.js",
25+
"test:karma": "karma start --single-run",
26+
"test:debug": "karma start --browsers=Chrome --auto-watch",
27+
"type-check": "tsc --noEmit"
28+
},
29+
"peerDependencies": {
30+
"@firebase/app-compat": "0.x"
31+
},
32+
"dependencies": {
33+
"@firebase/messaging-exp": "0.0.900",
34+
"@firebase/component": "0.2.1",
35+
"@firebase/installations-exp": "0.0.900",
36+
"@firebase/util": "0.4.0",
37+
"tslib": "^2.0.0"
38+
},
39+
"devDependencies": {
40+
"@firebase/app-compat": "0.0.900",
41+
"@rollup/plugin-json": "4.1.0",
42+
"rollup-plugin-typescript2": "0.29.0",
43+
"ts-essentials": "7.0.1",
44+
"typescript": "4.2.2"
45+
},
46+
"repository": {
47+
"directory": "packages/messaging",
48+
"type": "git",
49+
"url": "https://github.com/firebase/firebase-js-sdk.git"
50+
},
51+
"bugs": {
52+
"url": "https://github.com/firebase/firebase-js-sdk/issues"
53+
}
54+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import json from '@rollup/plugin-json';
19+
import pkg from './package.json';
20+
import typescript from 'typescript';
21+
import typescriptPlugin from 'rollup-plugin-typescript2';
22+
23+
const deps = Object.keys(
24+
Object.assign({}, pkg.peerDependencies, pkg.dependencies)
25+
);
26+
27+
/**
28+
* ES5 Builds
29+
*/
30+
const es5BuildPlugins = [
31+
typescriptPlugin({
32+
typescript
33+
}),
34+
json()
35+
];
36+
37+
const es5Builds = [
38+
{
39+
input: 'src/index.ts',
40+
output: [
41+
{ file: pkg.main, format: 'cjs', sourcemap: true },
42+
{ file: pkg.module, format: 'es', sourcemap: true }
43+
],
44+
plugins: es5BuildPlugins,
45+
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
46+
}
47+
];
48+
49+
/**
50+
* ES2017 Builds
51+
*/
52+
const es2017BuildPlugins = [
53+
typescriptPlugin({
54+
typescript,
55+
tsconfigOverride: {
56+
compilerOptions: {
57+
target: 'es2017'
58+
}
59+
}
60+
}),
61+
json({ preferConst: true })
62+
];
63+
64+
const es2017Builds = [
65+
{
66+
input: 'src/index.ts',
67+
output: {
68+
file: pkg.esm2017,
69+
format: 'es',
70+
sourcemap: true
71+
},
72+
plugins: es2017BuildPlugins,
73+
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
74+
}
75+
];
76+
77+
export default [...es5Builds, ...es2017Builds];
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { importPathTransformer } from '../../scripts/exp/ts-transform-import-path';
19+
import json from '@rollup/plugin-json';
20+
import pkg from './package.json';
21+
import typescript from 'typescript';
22+
import typescriptPlugin from 'rollup-plugin-typescript2';
23+
24+
const deps = Object.keys(
25+
Object.assign({}, pkg.peerDependencies, pkg.dependencies)
26+
);
27+
28+
/**
29+
* ES5 Builds
30+
*/
31+
const es5BuildPlugins = [
32+
typescriptPlugin({
33+
typescript,
34+
clean: true,
35+
abortOnError: false,
36+
transformers: [importPathTransformer]
37+
}),
38+
json()
39+
];
40+
41+
const es5Builds = [
42+
{
43+
input: 'src/index.ts',
44+
output: [
45+
{ file: pkg.main, format: 'cjs', sourcemap: true },
46+
{ file: pkg.module, format: 'es', sourcemap: true }
47+
],
48+
plugins: es5BuildPlugins,
49+
treeshake: {
50+
moduleSideEffects: false
51+
},
52+
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
53+
}
54+
];
55+
56+
/**
57+
* ES2017 Builds
58+
*/
59+
const es2017BuildPlugins = [
60+
typescriptPlugin({
61+
typescript,
62+
abortOnError: false,
63+
clean: true,
64+
transformers: [importPathTransformer],
65+
tsconfigOverride: {
66+
compilerOptions: {
67+
target: 'es2017'
68+
}
69+
}
70+
}),
71+
json({ preferConst: true })
72+
];
73+
74+
const es2017Builds = [
75+
{
76+
input: 'src/index.ts',
77+
output: {
78+
file: pkg.esm2017,
79+
format: 'es',
80+
sourcemap: true
81+
},
82+
plugins: es2017BuildPlugins,
83+
treeshake: {
84+
moduleSideEffects: false
85+
},
86+
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
87+
}
88+
];
89+
90+
export default [...es5Builds, ...es2017Builds];
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { name, version } from '../package.json';
19+
20+
import { firebase } from '@firebase/app-compat';
21+
import { registerMessagingCompat } from './registerMessagingCompat';
22+
23+
registerMessagingCompat();
24+
firebase.registerVersion(name, version);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import {
19+
FirebaseApp as AppCompat,
20+
_FirebaseService
21+
} from '@firebase/app-compat';
22+
import {
23+
FirebaseMessaging,
24+
MessagePayload,
25+
deleteToken,
26+
getToken,
27+
onMessage
28+
} from '@firebase/messaging-exp';
29+
import { NextFn, Observer, Unsubscribe } from '@firebase/util';
30+
31+
import { onBackgroundMessage } from '@firebase/messaging-exp/sw';
32+
33+
export class MessagingCompat implements _FirebaseService {
34+
swRegistration?: ServiceWorkerRegistration;
35+
vapidKey?: string;
36+
37+
onBackgroundMessageHandler:
38+
| NextFn<MessagePayload>
39+
| Observer<MessagePayload>
40+
| null = null;
41+
42+
onMessageHandler:
43+
| NextFn<MessagePayload>
44+
| Observer<MessagePayload>
45+
| null = null;
46+
47+
constructor(readonly app: AppCompat, readonly messaging: FirebaseMessaging) {
48+
this.app = app;
49+
this.messaging = messaging;
50+
}
51+
52+
async getToken(options?: {
53+
vapidKey?: string;
54+
serviceWorkerRegistration?: ServiceWorkerRegistration;
55+
}): Promise<string> {
56+
return getToken(this.messaging, options);
57+
}
58+
59+
async deleteToken(): Promise<boolean> {
60+
return deleteToken(this.messaging);
61+
}
62+
63+
onMessage(
64+
nextOrObserver: NextFn<MessagePayload> | Observer<MessagePayload>
65+
): Unsubscribe {
66+
return onMessage(this.messaging, nextOrObserver);
67+
}
68+
69+
onBackgroundMessage(
70+
nextOrObserver: NextFn<MessagePayload> | Observer<MessagePayload>
71+
): Unsubscribe {
72+
return onBackgroundMessage(this.messaging, nextOrObserver);
73+
}
74+
}

0 commit comments

Comments
 (0)