-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
287 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* eslint-env jest */ | ||
// @ts-nocheck | ||
|
||
import { keysByProvider } from "com.batch.shared/parameters/keys"; | ||
import ParameterStore from "com.batch.shared/parameters/parameter-store"; | ||
|
||
import { createSDKFactory } from "../sdk-factory"; | ||
import SafariSDKFactory from "../sdk-safari"; | ||
import StandardSDKFactory from "../sdk-standard"; | ||
jest.mock("com.batch.shared/persistence/profile"); | ||
|
||
const DEFAULT_UA = window.navigator.userAgent; | ||
|
||
const UA_SAFARI = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15"; | ||
|
||
// Allow to set a custom user-agent | ||
function setUserAgent(userAgent: string): void { | ||
Object.defineProperty(window.navigator, "userAgent", { | ||
get: function () { | ||
return userAgent; | ||
}, | ||
configurable: true, | ||
}); | ||
} | ||
|
||
let store = null; | ||
|
||
beforeAll(() => { | ||
return ParameterStore.getInstance().then(s => { | ||
store = s; | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
setUserAgent(DEFAULT_UA); | ||
delete self.PushManager; | ||
}); | ||
|
||
test("default is standard sdk factory", async () => { | ||
const factory = await createSDKFactory(); | ||
expect(factory).toBe(StandardSDKFactory); | ||
}); | ||
|
||
test("is safari sdk factory on safari 15-", async () => { | ||
setUserAgent(UA_SAFARI); | ||
const factory = await createSDKFactory(); | ||
expect(factory).toBe(SafariSDKFactory); | ||
}); | ||
|
||
test("is standard sdk factory on safari 16+", async () => { | ||
setUserAgent(UA_SAFARI); | ||
self.PushManager = () => { | ||
/** Way to mock safari 16 supporting WPP. */ | ||
}; | ||
const factory = await createSDKFactory(); | ||
expect(factory).toBe(StandardSDKFactory); | ||
}); | ||
|
||
test("is safari sdk factory on safari 16+ when user already has apns subscription", async () => { | ||
setUserAgent(UA_SAFARI); | ||
self.PushManager = () => { | ||
/** Way to mock safari 16 supporting WPP. */ | ||
}; | ||
await store.setParameterValue(keysByProvider.profile.Subscription, "faketokenapns"); | ||
const factory = await createSDKFactory(); | ||
expect(factory).toBe(SafariSDKFactory); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { ISDK } from "com.batch.dom/sdk-impl/sdk"; | ||
import SafariSDKFactory from "com.batch.dom/sdk-impl/sdk-safari"; | ||
import StandardSDKFactory from "com.batch.dom/sdk-impl/sdk-standard"; | ||
import UserAgent, { Browser } from "com.batch.shared/helpers/user-agent"; | ||
import { keysByProvider } from "com.batch.shared/parameters/keys"; | ||
import ParameterStore from "com.batch.shared/parameters/parameter-store"; | ||
/** | ||
* The factory used to create a uniq ISDK instance. | ||
* The instance is wrapped with a promise to always ensure that everything is correctly initialized. | ||
*/ | ||
export interface ISDKFactory { | ||
/** | ||
* Setup a new ISDK instance (if not already done) | ||
* and returns the newly uniq created instance. | ||
*/ | ||
setup(config: object): Promise<ISDK>; | ||
|
||
/** | ||
* Return the uniq instance created by the factory. | ||
* Always call setup before or the promise will be rejected. | ||
*/ | ||
getInstance(): Promise<ISDK>; | ||
} | ||
|
||
/** | ||
* Create the right SDK factory according to the browser | ||
* @returns ISDKFactory | ||
*/ | ||
export async function createSDKFactory(): Promise<ISDKFactory> { | ||
// Instantiate the right SDK factory according to the browser | ||
let sdkFactory: ISDKFactory = StandardSDKFactory; | ||
const userAgent = new UserAgent(window.navigator.userAgent); | ||
if (userAgent.browser === Browser.Safari) { | ||
if ("PushManager" in self) { | ||
const parameterStore: ParameterStore = await ParameterStore.getInstance(); | ||
const subscription = await parameterStore.getParameterValue(keysByProvider.profile.Subscription); | ||
if (subscription && typeof subscription === "string") { | ||
// User already has an APNS subscription, we keep using APNS. | ||
sdkFactory = SafariSDKFactory; | ||
} | ||
} else { | ||
// Safari does NOT supports WPP. | ||
sdkFactory = SafariSDKFactory; | ||
} | ||
} | ||
return sdkFactory; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,5 @@ | ||
{ | ||
"license" : "Proprietary", | ||
"majorVersion" : "3", | ||
"name" : "batch-webpush-sdk", | ||
"private" : true, | ||
"version" : "3.3.1", | ||
"main" : "index.js", | ||
"scripts" : { | ||
"build" : "yarn type-check && yarn build-webpack", | ||
"doc" : "cd src\/public\/types\/ && typedoc --out ..\/..\/..\/web-api-reference --name \"Batch SDK - Web\" --readme ..\/..\/..\/documentation_public_readme.md --excludeExternals --excludePrivate .\/*.d.ts", | ||
"test" : "jest", | ||
"lint" : "eslint --no-fix \"src\/**\"", | ||
"type-check" : "tsc --build", | ||
"\/\/" : "All scripts here are overwritten for the open source release. Edit package.overlay.json if you need to edit scripts part of the OSS release.", | ||
"lint:report" : "eslint --no-fix --output-file eslint_report.json --format json \"src\/**\"" | ||
}, | ||
"packageManager" : "[email protected]", | ||
"repository" : { | ||
|
||
}, | ||
"dependencies" : { | ||
"babel-eslint" : "^10.1.0", | ||
"@babel\/plugin-proposal-private-methods" : "^7.14.5", | ||
|
@@ -67,11 +49,29 @@ | |
"@babel\/preset-env" : "^7.15.0", | ||
"eslint-config-prettier" : "^6.15.0", | ||
"eslint-plugin-simple-import-sort" : "^5.0.3", | ||
"puppeteer-core" : "^13.5.2", | ||
"puppeteer-core" : "^19.2.0", | ||
"pnp-webpack-plugin" : "^1.7.0", | ||
"webpack" : "^5.51.2", | ||
"web-push" : "^3.4.5", | ||
"cross-env" : "^7.0.3", | ||
"typescript" : "4.2" | ||
} | ||
}, | ||
"version" : "3.4.0", | ||
"repository" : { | ||
|
||
}, | ||
"packageManager" : "[email protected]", | ||
"majorVersion" : "3", | ||
"license" : "Proprietary", | ||
"scripts" : { | ||
"build" : "yarn type-check && yarn build-webpack", | ||
"doc" : "cd src\/public\/types\/ && typedoc --out ..\/..\/..\/web-api-reference --name \"Batch SDK - Web\" --readme ..\/..\/..\/documentation_public_readme.md --excludeExternals --excludePrivate .\/*.d.ts", | ||
"test" : "jest", | ||
"lint" : "eslint --no-fix \"src\/**\"", | ||
"type-check" : "tsc --build", | ||
"\/\/" : "All scripts here are overwritten for the open source release. Edit package.overlay.json if you need to edit scripts part of the OSS release.", | ||
"lint:report" : "eslint --no-fix --output-file eslint_report.json --format json \"src\/**\"" | ||
}, | ||
"name" : "batch-webpush-sdk", | ||
"main" : "index.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.