fix: avoid potential default namespace collision #809
Merged
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.
Description:
protobufjs
uses a package-wide global internal state for the protos in runtime, reachable atrequire('protobufjs').roots
, and by default registers everything in thedefault
root namespace:Among others, it creates e.g.
Key
,Duration
,Timestamp
protocols which are pretty generic names.Now, the problem is in how the compiled code then references those:
then
and e.g.
Which means that while
root
is resolved to$protobuf.roots["default"]
at load, specific protocols are then resolved against that root at runtime, so if something redefines e.g.require('protobufjs').roots.default.proto.Key
, it will affect@hashgraph/proto
.Now, in a project that depends on
@hashgraph/proto
and e.g. another library depending on the same or compatibleprotobufjs
version range could be used, and the package manager (e.g.yarn
ornpm
) can deduplicate that, resulting in both@hashgraph/proto
and another lib using the same sharedrequire('protobufjs')
object.When using the default namespace root, the protocol namespaces might collide, causing unexpected effects, e.g. when that other protocol also defines a
Key
proto (or anything else that collides with this lib).This PR changes the namespace from
root
tohashgraph
, basically causing this exact diff in the compiled code:To reproduce:
main
,yarn && mv lib lib.a
,yarn && mv lib lib.b
,./node_modules/.bin/prettier --write lib.?/proto.js
,diff -r lib.a lib.b
.This should avoid potential protocol namespace collisions with another libs.
That said, it won't prevent namespace collisions if some setup ends up attempting to use two different versions of
@hashgraph/proto
, but that's likely to be a much less significant case. Ideally, not polluting (and not relying upon) the global state at all would have been ideal (like #269 did previously), but I'm unsure how to do that yet.In the meantime, this should be just good enough.
Related issue(s):
Previously: #267, #268, #269.
Checklist