Repository navigation
Make native module resolution lazy and expose core internals entry point #1316
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
Open
cdn34dd
wants to merge
9
commits into
develop
Choose a base branch
from
carlosnogueira/draft/lazy-native-module-resolution
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6251da2
Make native module specs resolve lazily
cdn34dd 9a119ca
Add import safety regression tests
cdn34dd 27bb7cf
Expose core internals entry point
cdn34dd 4952ad3
Adapt native module tests after rebase
cdn34dd 24ad982
Fix lint errors
cdn34dd 9e5fcfe
Resolve native modules lazily in SDK wrappers
cdn34dd 4d30fe7
Don't cache missing native modules
cdn34dd 16ab693
Remove Proxy from DdSdkInternal
cdn34dd 27796fe
Fix import safety and bridge tests
cdn34dd 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
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 |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| describe('./internal entry', () => { | ||
| it('exports the internals react-native-vega needs', () => { | ||
| // eslint-disable-next-line global-require, @typescript-eslint/no-var-requires | ||
| const internal = require('../internal'); | ||
| const expected = [ | ||
| 'addDefaultValuesToAutoInstrumentationConfiguration', | ||
| 'buildConfigurationFromPartialConfiguration', | ||
| 'DdSdkNativeConfiguration', | ||
| 'RUM_DEFAULTS', | ||
| 'adaptLongTaskThreshold', | ||
| 'version', | ||
| 'AccountInfoSingleton', | ||
| 'AttributesSingleton', | ||
| 'UserInfoSingleton', | ||
| 'GlobalState', | ||
| 'BufferSingleton', | ||
| 'DatadogProviderState', | ||
| 'DdRumResourceTracking' | ||
| ]; | ||
| for (const name of expected) { | ||
| expect(internal[name]).toBeDefined(); | ||
| } | ||
| }); | ||
|
|
||
| // Import safety of this entry point is covered in wrapperImportSafety.test.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
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,58 @@ | ||
| /* | ||
| * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
| * This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| * Copyright 2016-Present Datadog, Inc. | ||
| */ | ||
|
|
||
| describe('native spec getters', () => { | ||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ['../specs/NativeDdSdk', 'getNativeDdSdk', 'DdSdk'], | ||
| ['../specs/NativeDdRum', 'getNativeDdRum', 'DdRum'], | ||
| ['../specs/NativeDdLogs', 'getNativeDdLogs', 'DdLogs'], | ||
| ['../specs/NativeDdTrace', 'getNativeDdTrace', 'DdTrace'], | ||
| ['../specs/NativeDdFlags', 'getNativeDdFlags', 'DdFlags'] | ||
| ])( | ||
| '%s retries a missing module and caches it once found', | ||
| (modulePath, getterName, registryName) => { | ||
| jest.isolateModules(() => { | ||
| // eslint-disable-next-line global-require, @typescript-eslint/no-var-requires | ||
| const { TurboModuleRegistry } = require('react-native'); | ||
| const nativeModule = { name: registryName }; | ||
| const getSpy = jest | ||
| .spyOn(TurboModuleRegistry, 'get') | ||
| .mockReturnValueOnce(null) | ||
| .mockReturnValue(nativeModule); | ||
| // eslint-disable-next-line global-require, import/no-dynamic-require, @typescript-eslint/no-var-requires | ||
| const getter = require(modulePath)[getterName]; | ||
|
|
||
| expect(getter()).toBeNull(); | ||
| expect(getter()).toBe(nativeModule); | ||
| expect(getter()).toBe(nativeModule); | ||
| expect(getSpy).toHaveBeenCalledTimes(2); | ||
| expect(getSpy).toHaveBeenCalledWith(registryName); | ||
| }); | ||
| } | ||
| ); | ||
|
|
||
| it('DdSdkWrapper picks up a module that was missing on first use', () => { | ||
| jest.isolateModules(() => { | ||
| // eslint-disable-next-line global-require, @typescript-eslint/no-var-requires | ||
| const { TurboModuleRegistry } = require('react-native'); | ||
| const nativeModule = { clearAllData: jest.fn() }; | ||
| jest.spyOn(TurboModuleRegistry, 'get') | ||
| .mockReturnValueOnce(null) | ||
| .mockReturnValue(nativeModule); | ||
| // eslint-disable-next-line global-require, @typescript-eslint/no-var-requires | ||
| const { DdSdkWrapper } = require('../sdk/DdSdkInternal'); | ||
| const sdk = new DdSdkWrapper(); | ||
|
|
||
| expect(() => sdk.clearAllData()).toThrow(); | ||
| sdk.clearAllData(); | ||
| expect(nativeModule.clearAllData).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
| }); |
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
129 changes: 129 additions & 0 deletions
129
packages/core/src/__tests__/wrapperImportSafety.test.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,129 @@ | ||
| /* | ||
| * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
| * This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| * Copyright 2016-Present Datadog, Inc. | ||
| */ | ||
|
|
||
| /** | ||
| * The spy must be installed on the `react-native` instance from inside | ||
| * `jest.isolateModules`: lookups made by isolated modules go through that | ||
| * registry, not the one imported at the top of a test file. | ||
| */ | ||
| const requireCountingLookups = (modulePath: string) => { | ||
| let lookups: string[] = []; | ||
| let mod: any; | ||
| jest.isolateModules(() => { | ||
| // eslint-disable-next-line global-require, @typescript-eslint/no-var-requires | ||
| const { TurboModuleRegistry } = require('react-native'); | ||
| const getSpy = jest.spyOn(TurboModuleRegistry, 'get'); | ||
| // eslint-disable-next-line global-require, import/no-dynamic-require | ||
| mod = require(modulePath); | ||
| lookups = getSpy.mock.calls.map(call => call[0] as string); | ||
| }); | ||
| return { mod, lookups }; | ||
| }; | ||
|
|
||
| describe('SDK wrappers are import-safe', () => { | ||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| // Singletons live on globalThis, which isolateModules does not reset. | ||
| const g = globalThis as Record<PropertyKey, unknown>; | ||
| for (const symbol of Object.getOwnPropertySymbols(g)) { | ||
| if (symbol.description?.startsWith('com.datadog.reactnative')) { | ||
| delete g[symbol]; | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| it.each([ | ||
| '../specs/NativeDdSdk', | ||
| '../specs/NativeDdRum', | ||
| '../specs/NativeDdLogs', | ||
| '../specs/NativeDdTrace', | ||
| '../specs/NativeDdFlags', | ||
| '../sdk/DdSdkInternal', | ||
| '../rum/DdRum', | ||
| '../logs/DdLogs', | ||
| '../trace/DdTrace', | ||
| '../flags/DdFlags' | ||
| ])('importing %s performs no native lookup', modulePath => { | ||
| expect(requireCountingLookups(modulePath).lookups).toEqual([]); | ||
| }); | ||
|
|
||
| it('DdSdkWrapper only resolves the native module when a method is used', () => { | ||
| jest.isolateModules(() => { | ||
| // eslint-disable-next-line global-require, @typescript-eslint/no-var-requires | ||
| const { TurboModuleRegistry } = require('react-native'); | ||
| const getSpy = jest.spyOn(TurboModuleRegistry, 'get'); | ||
| // eslint-disable-next-line global-require, @typescript-eslint/no-var-requires | ||
| const { DdSdkWrapper } = require('../sdk/DdSdkInternal'); | ||
|
|
||
| const sdk = new DdSdkWrapper(); | ||
| expect(getSpy).not.toHaveBeenCalled(); | ||
|
|
||
| sdk.getConstants(); | ||
| expect(getSpy).toHaveBeenCalledWith('DdSdk'); | ||
| }); | ||
| }); | ||
|
|
||
| it('importing the public barrel performs no native lookup', () => { | ||
| expect(requireCountingLookups('../index').lookups).toEqual([]); | ||
| }); | ||
|
|
||
| it('importing the internal entry performs no native lookup', () => { | ||
| expect(requireCountingLookups('../internal').lookups).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('SDK wrappers do not rely on Proxy', () => { | ||
| // Older Android JSC and Hermes versions supported by the SDK lack Proxy. | ||
| it.each(['../sdk/DdSdkInternal', '../index', '../internal'])( | ||
| 'importing %s works without Proxy', | ||
| modulePath => { | ||
| const g = globalThis as Record<string, unknown>; | ||
| const originalProxy = g.Proxy; | ||
| try { | ||
| delete g.Proxy; | ||
| expect(() => requireCountingLookups(modulePath)).not.toThrow(); | ||
| } finally { | ||
| g.Proxy = originalProxy; | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| it('DdSdkWrapper forwards every method to the native module', () => { | ||
| jest.isolateModules(() => { | ||
| // eslint-disable-next-line global-require, @typescript-eslint/no-var-requires | ||
| const { TurboModuleRegistry } = require('react-native'); | ||
| const nativeModule: Record<string, jest.Mock> = {}; | ||
| jest.spyOn(TurboModuleRegistry, 'get').mockReturnValue( | ||
| nativeModule | ||
| ); | ||
| // eslint-disable-next-line global-require, @typescript-eslint/no-var-requires | ||
| const { DdSdkWrapper } = require('../sdk/DdSdkInternal'); | ||
| const sdk = new DdSdkWrapper(); | ||
| const methods = Object.getOwnPropertyNames( | ||
| DdSdkWrapper.prototype | ||
| ).filter( | ||
| name => | ||
| name !== 'constructor' && | ||
| !name.startsWith('_') && | ||
| typeof Object.getOwnPropertyDescriptor( | ||
| DdSdkWrapper.prototype, | ||
| name | ||
| )?.value === 'function' | ||
| ); | ||
| for (const name of methods) { | ||
| nativeModule[name] = jest.fn().mockReturnValue(name); | ||
| } | ||
|
|
||
| for (const name of methods) { | ||
| const arg = | ||
| name === 'initialize' ? { attributeEncoders: [] } : 'arg'; | ||
| expect(sdk[name](arg)).toBe(name); | ||
| expect(nativeModule[name]).toHaveBeenCalledTimes(1); | ||
| } | ||
| expect(methods.length).toBeGreaterThan(15); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.