Skip to content
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

fix(deps): Don't use global variables for jsdom injection in scripts/package/node/core.js and core/utils/xml.ts #6764

Merged
merged 3 commits into from
Jan 12, 2023

Conversation

cpcallen
Copy link
Contributor

@cpcallen cpcallen commented Jan 12, 2023

The basics

  • I branched from develop
  • My pull request is against develop
  • My code follows the style guide
  • I ran npm run format and npm run lint

The details

Resolves

Fixes #6725.

Proposed Changes

  1. Introduce a mechanism for injecting arbitrary dependencies into modules, specifically in this case
    to inject required bits of jsdom's Window and Document implementations into core/utils/xml.js when running in node.js or other environments lacking a DOM.

  2. Rename the xmlDocument local variable back to document (was renamed in Don't monkey-patch Blocky.utils.xml.document in node module  #5461) so that the name used in this module corresponds to the usual global variable it replaces.

  3. Change the injection in scripts/package/node/core.js to use injectDependencies instead of setDocument + global variables for DOMParser and XMLSerializer; also eliminate apparently-unnecessary creation of a special
    Document instance, using the default one supplied by jsdom instead.

  4. Mark getDocument and setDocument as @deprecated, with suitable calls to deprecation.warn().

    There are no remaining callers to either function within this repository—setDocument was only used by the node.js wrapper, and apparently getDocument was never used anywhere—and we do not anticipate that either were used by external developers.

Behaviour Before Change

If a runtime included multiple separate copies of the blockly package and its dependency jsdom, then XML serialisation and deserialisation would break.

Behaviour After Change

It is safe (but still not advisable) to load multiple separate copies of blockly and jsdom into the same runtime.

Reason for Changes

Fix #6725; allow tests in blockly-samples to pass after updating to Blockly v9.x (e.g. in google/blockly-samples#1452).

Test Coverage

No changes in manual test procedures anticipated.

Documentation

No changes beyond automatic update including @deprecation notice.

Additional Information

The injectDependencies function is intended to be a prototype for a generally applicable mechanism for dependency injection—for example, this approach could be used in other modules to break the circular dependencies that currently cause loading errors when trying to load Blockly via import * as Blockly from './build/src/core/blockly.js' without support from the Closure module tooling. As such, it uses an options object and destructuring-with-defaults to facilitate injecting arbitrarily-named dependencies either one-at-a-time or (as here in core.js) in bulk.

@cpcallen cpcallen added PR: fix Fixes a bug deprecation This PR deprecates an API. labels Jan 12, 2023
@cpcallen cpcallen requested a review from maribethb January 12, 2023 14:48
@cpcallen cpcallen requested a review from a team as a code owner January 12, 2023 14:48
@github-actions github-actions bot added PR: fix Fixes a bug and removed PR: fix Fixes a bug deprecation This PR deprecates an API. labels Jan 12, 2023
Introduce a (hopefully generally applicable) mechanism for
injecting dependencies into modules, specifically in this case
to inject required bits of JSDOM's Window and Document
implementations into core/utils/xml.js when running in
node.js or other environments lacking a DOM.

The injectDependencies function uses an options object to
facilitate optionally injecting multiple named dependencies
at the same time.

Rename the xmlDocument local variable back to document (was
renamed in google#5461) so that the name used in this module
corresponds to the usual global variable it replaces.

Change the injection in scripts/package/node/core.js to use
injectDependencies instead of setXmlDocument + global variables;
also eliminate apparently-unnecessary creation of a special
Document instance, using the default one supplied by jsdom
instead.

Fixes google#6725.
Mark getXmlDocument and setXmlDocument as @deprecated, with
suitable calls to deprecation.warn().

There are no remaining callers to either function within core -
setXmlDocument was only used by the node.js wrapper, and and
apparently getXmlDocument was never used AFAICT - and we do not
anticipate that either were used by external developers.
@cpcallen cpcallen force-pushed the fix/6725-jsdom-inject branch from ce9547e to 678643a Compare January 12, 2023 14:58
core/utils/xml.ts Outdated Show resolved Hide resolved
core/utils/xml.ts Outdated Show resolved Hide resolved
core/utils/xml.ts Show resolved Hide resolved
core/utils/xml.ts Show resolved Hide resolved
scripts/package/node/core.js Outdated Show resolved Hide resolved
@github-actions github-actions bot added PR: fix Fixes a bug and removed PR: fix Fixes a bug labels Jan 12, 2023
@BeksOmega BeksOmega removed the request for review from maribethb January 12, 2023 21:00
@BeksOmega BeksOmega assigned BeksOmega and unassigned maribethb Jan 12, 2023
core/utils/xml.ts Show resolved Hide resolved
@BeksOmega BeksOmega merged commit cd57e74 into google:develop Jan 12, 2023
@cpcallen cpcallen deleted the fix/6725-jsdom-inject branch January 13, 2023 09:28
gonfunko pushed a commit that referenced this pull request Jan 19, 2023
…/package/node/core.js` and `core/utils/xml.ts` (#6764)

* fix(node): Don't use global variables for jsdom injection

Introduce a (hopefully generally applicable) mechanism for
injecting dependencies into modules, specifically in this case
to inject required bits of JSDOM's Window and Document
implementations into core/utils/xml.js when running in
node.js or other environments lacking a DOM.

The injectDependencies function uses an options object to
facilitate optionally injecting multiple named dependencies
at the same time.

Rename the xmlDocument local variable back to document (was
renamed in #5461) so that the name used in this module
corresponds to the usual global variable it replaces.

Change the injection in scripts/package/node/core.js to use
injectDependencies instead of setXmlDocument + global variables;
also eliminate apparently-unnecessary creation of a special
Document instance, using the default one supplied by jsdom
instead.

Fixes #6725.

* deprecate(xml): Deprecate getXmlDocument and setXmlDocument

Mark getXmlDocument and setXmlDocument as @deprecated, with
suitable calls to deprecation.warn().

There are no remaining callers to either function within core -
setXmlDocument was only used by the node.js wrapper, and and
apparently getXmlDocument was never used AFAICT - and we do not
anticipate that either were used by external developers.

* fix: Corrections for comments on PR #6764.

(cherry picked from commit cd57e74)
@gonfunko gonfunko mentioned this pull request Jan 19, 2023
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: fix Fixes a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Don't use global variables in scripts/package/node/core.js and core/utils/xml.ts
4 participants