-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core-tracing] support sharing state between CJS and ESM (#28631)
### Packages impacted by this PR @azure/core-tracing ### Issues associated with this PR Resolves #28619 ### Describe the problem that is addressed by this PR If you are exporting both CommonJS and ESM forms of a package, then it is possible for both versions to be loaded at run-time. However, the CommonJS build is a different module from the ESM build, and thus a different thing from the point of view of the JavaScript interpreter in Node.js. > https://github.com/isaacs/tshy/blob/main/README.md#dual-package-hazards tshy handles this by building programs into separate folders and treats "dual module hazards" as a fact of life. One of the hazards of dual-modules is shared module-global state. In core-tracing we have a module-global instrumenter that is used for hooking into and lighting up OpenTelemetry based instrumentation. In order to ensure it works in this dual-package world we must use one of multiple-recommended workarounds. In this case, the tshy documentation provides a solution to this with a well-documented path forward. This is what is implemented here. Please refer to https://github.com/isaacs/tshy/blob/main/README.md#module-local-state for added context
- Loading branch information
Showing
9 changed files
with
59 additions
and
11 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
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
import { Instrumenter } from "./interfaces.js"; | ||
|
||
/** | ||
* Browser-only implementation of the module's state. The browser esm variant will not load the commonjs state, so we do not need to share state between the two. | ||
*/ | ||
export const state = { | ||
instrumenterImplementation: undefined as Instrumenter | undefined, | ||
}; |
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,11 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
/** | ||
* @internal | ||
* | ||
* Holds the singleton instrumenter, to be shared across CJS and ESM imports. | ||
*/ | ||
export const state = { | ||
instrumenterImplementation: undefined, | ||
}; |
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,14 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
import { Instrumenter } from "./interfaces.js"; | ||
// @ts-expect-error The recommended approach to sharing module state between ESM and CJS. | ||
// See https://github.com/isaacs/tshy/blob/main/README.md#module-local-state for additional information. | ||
import { state as cjsState } from "../commonjs/state.js"; | ||
|
||
/** | ||
* Defines the shared state between CJS and ESM by re-exporting the CJS state. | ||
*/ | ||
export const state = cjsState as { | ||
instrumenterImplementation: Instrumenter | undefined; | ||
}; |
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