-
Notifications
You must be signed in to change notification settings - Fork 84
refactor: Consolidate datafile-manager package into optimizely-sdk #781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e65902e
init
ozayr-zaviar 105edcf
fixed some tests
zashraf1985 53fc624
fixes
ozayr-zaviar 4174689
Merge branch 'master' into uzair/consolidate-datafile-manager
ozayr-zaviar ab93d53
Merge branch 'master' into uzair/consolidate-datafile-manager
ozayr-zaviar 473c790
fix
ozayr-zaviar e970b15
Merge branch 'master' into uzair/consolidate-datafile-manager
ozayr-zaviar 639ec9c
xmlhttprequest fixed in jest
ozayr-zaviar cd0c26b
fixes
ozayr-zaviar 699d5ef
jest test fixes
ozayr-zaviar 33742ea
comments addressed
ozayr-zaviar e427567
Update karma.base.conf.js
ozayr-zaviar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
...ges/optimizely-sdk/__mocks__/@react-native-async-storage/async-storage-event-processor.ts
This file contains hidden or 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,52 @@ | ||
| /** | ||
| * Copyright 2022, Optimizely | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| let items: {[key: string]: string} = {} | ||
|
|
||
| export default class AsyncStorage { | ||
|
|
||
| static getItem(key: string, callback?: (error?: Error, result?: string) => void): Promise<string | null> { | ||
| return new Promise(resolve => { | ||
| setTimeout(() => resolve(items[key] || null), 1) | ||
| }) | ||
| } | ||
|
|
||
| static setItem(key: string, value: string, callback?: (error?: Error) => void): Promise<void> { | ||
| return new Promise((resolve) => { | ||
| setTimeout(() => { | ||
| items[key] = value | ||
| resolve() | ||
| }, 1) | ||
| }) | ||
| } | ||
|
|
||
| static removeItem(key: string, callback?: (error?: Error, result?: string) => void): Promise<string | null> { | ||
| return new Promise(resolve => { | ||
| setTimeout(() => { | ||
| items[key] && delete items[key] | ||
| // @ts-ignore | ||
| resolve() | ||
| }, 1) | ||
| }) | ||
| } | ||
|
|
||
| static dumpItems(): {[key: string]: string} { | ||
| return items | ||
| } | ||
|
|
||
| static clearStore(): void { | ||
| items = {} | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -23,7 +23,7 @@ module.exports = { | |
| plugins: ['karma-mocha', 'karma-webpack', require('karma-browserstack-launcher')], | ||
|
|
||
| webpack: { | ||
| mode: 'production', | ||
| mode:'development', | ||
|
||
| module: { | ||
| rules: [ | ||
| { | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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
46 changes: 46 additions & 0 deletions
46
packages/optimizely-sdk/lib/modules/datafile-manager/backoffController.ts
This file contains hidden or 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,46 @@ | ||
| /** | ||
| * Copyright 2019-2020, Optimizely | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { BACKOFF_BASE_WAIT_SECONDS_BY_ERROR_COUNT } from './config'; | ||
|
|
||
| function randomMilliseconds(): number { | ||
| return Math.round(Math.random() * 1000); | ||
| } | ||
|
|
||
| export default class BackoffController { | ||
| private errorCount = 0; | ||
|
|
||
| getDelay(): number { | ||
| if (this.errorCount === 0) { | ||
| return 0; | ||
| } | ||
| const baseWaitSeconds = | ||
| BACKOFF_BASE_WAIT_SECONDS_BY_ERROR_COUNT[ | ||
| Math.min(BACKOFF_BASE_WAIT_SECONDS_BY_ERROR_COUNT.length - 1, this.errorCount) | ||
| ]; | ||
| return baseWaitSeconds * 1000 + randomMilliseconds(); | ||
| } | ||
|
|
||
| countError(): void { | ||
| if (this.errorCount < BACKOFF_BASE_WAIT_SECONDS_BY_ERROR_COUNT.length - 1) { | ||
| this.errorCount++; | ||
| } | ||
| } | ||
|
|
||
| reset(): void { | ||
| this.errorCount = 0; | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
packages/optimizely-sdk/lib/modules/datafile-manager/browserDatafileManager.ts
This file contains hidden or 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,32 @@ | ||
| /** | ||
| * Copyright 2022, Optimizely | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { makeGetRequest } from './browserRequest'; | ||
| import HttpPollingDatafileManager from './httpPollingDatafileManager'; | ||
| import { Headers, AbortableRequest } from './http'; | ||
| import { DatafileManagerConfig } from './datafileManager'; | ||
|
|
||
| export default class BrowserDatafileManager extends HttpPollingDatafileManager { | ||
| protected makeGetRequest(reqUrl: string, headers: Headers): AbortableRequest { | ||
| return makeGetRequest(reqUrl, headers); | ||
| } | ||
|
|
||
| protected getConfigDefaults(): Partial<DatafileManagerConfig> { | ||
| return { | ||
| autoUpdate: false, | ||
| }; | ||
| } | ||
| } |
96 changes: 96 additions & 0 deletions
96
packages/optimizely-sdk/lib/modules/datafile-manager/browserRequest.ts
This file contains hidden or 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,96 @@ | ||
| /** | ||
| * Copyright 2022, Optimizely | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { AbortableRequest, Response, Headers } from './http'; | ||
| import { REQUEST_TIMEOUT_MS } from './config'; | ||
| import { getLogger } from '../logging'; | ||
|
|
||
| const logger = getLogger('DatafileManager'); | ||
|
|
||
| const GET_METHOD = 'GET'; | ||
| const READY_STATE_DONE = 4; | ||
|
|
||
| function parseHeadersFromXhr(req: XMLHttpRequest): Headers { | ||
| const allHeadersString = req.getAllResponseHeaders(); | ||
|
|
||
| if (allHeadersString === null) { | ||
| return {}; | ||
| } | ||
|
|
||
| const headerLines = allHeadersString.split('\r\n'); | ||
| const headers: Headers = {}; | ||
| headerLines.forEach(headerLine => { | ||
| const separatorIndex = headerLine.indexOf(': '); | ||
| if (separatorIndex > -1) { | ||
| const headerName = headerLine.slice(0, separatorIndex); | ||
| const headerValue = headerLine.slice(separatorIndex + 2); | ||
| if (headerValue.length > 0) { | ||
| headers[headerName] = headerValue; | ||
| } | ||
| } | ||
| }); | ||
| return headers; | ||
| } | ||
|
|
||
| function setHeadersInXhr(headers: Headers, req: XMLHttpRequest): void { | ||
| Object.keys(headers).forEach(headerName => { | ||
| const header = headers[headerName]; | ||
| req.setRequestHeader(headerName, header!); | ||
| }); | ||
| } | ||
|
|
||
| export function makeGetRequest(reqUrl: string, headers: Headers): AbortableRequest { | ||
| const req = new XMLHttpRequest(); | ||
|
|
||
| const responsePromise: Promise<Response> = new Promise((resolve, reject) => { | ||
| req.open(GET_METHOD, reqUrl, true); | ||
|
|
||
| setHeadersInXhr(headers, req); | ||
|
|
||
| req.onreadystatechange = (): void => { | ||
| if (req.readyState === READY_STATE_DONE) { | ||
| const statusCode = req.status; | ||
| if (statusCode === 0) { | ||
| reject(new Error('Request error')); | ||
| return; | ||
| } | ||
|
|
||
| const headers = parseHeadersFromXhr(req); | ||
| const resp: Response = { | ||
| statusCode: req.status, | ||
| body: req.responseText, | ||
| headers, | ||
| }; | ||
| resolve(resp); | ||
| } | ||
| }; | ||
|
|
||
| req.timeout = REQUEST_TIMEOUT_MS; | ||
|
|
||
| req.ontimeout = (): void => { | ||
| logger.error('Request timed out'); | ||
| }; | ||
|
|
||
| req.send(); | ||
| }); | ||
|
|
||
| return { | ||
| responsePromise, | ||
| abort(): void { | ||
| req.abort(); | ||
| }, | ||
| }; | ||
| } |
27 changes: 27 additions & 0 deletions
27
packages/optimizely-sdk/lib/modules/datafile-manager/config.ts
This file contains hidden or 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,27 @@ | ||
| /** | ||
| * Copyright 2022, Optimizely | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| export const DEFAULT_UPDATE_INTERVAL = 5 * 60 * 1000; // 5 minutes | ||
|
|
||
| export const MIN_UPDATE_INTERVAL = 1000; | ||
|
|
||
| export const DEFAULT_URL_TEMPLATE = `https://cdn.optimizely.com/datafiles/%s.json`; | ||
|
|
||
| export const DEFAULT_AUTHENTICATED_URL_TEMPLATE = `https://config.optimizely.com/datafiles/auth/%s.json`; | ||
|
|
||
| export const BACKOFF_BASE_WAIT_SECONDS_BY_ERROR_COUNT = [0, 8, 16, 32, 64, 128, 256, 512]; | ||
|
|
||
| export const REQUEST_TIMEOUT_MS = 60 * 1000; // 1 minute |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you revert it back if it works?