-
Notifications
You must be signed in to change notification settings - Fork 31
Closed
Description
Summary
When upgrading or regenerating Protobuf bindings (e.g., after updating libpg-query-node), Protobuf types like String or BitString can shadow JavaScript built-ins, causing runtime crashes in the generated proto.js:
TypeError: Cannot set properties of undefined (setting '0')
at String (proto.js:10726:43)This is caused by pbjs generating a String.fromObject() method that calls the shadowed constructor instead of the global primitive converter:
String.fromObject = function(object) {
var message = new $root.pg_query.String();
if (object.sval != null)
message.sval = String(object.sval); // ← calls the Protobuf wrapper, not global
return message;
};Why It Happens
pbjsgenerates a message namedString, which creates a local function that shadows the globalString.- The call
String(object.sval)no longer performs a type conversion — it calls the local constructor withoutnew, sothisisundefined. - The constructor tries to assign to
this[...]and fails.
Impact
- Crashes at runtime on any
.fromObject({ sval: '...' })call. - Will reappear after any re-generation via
pbjsor updates tolibpg-query-nodeunless patched.
✅ Temporary Workaround
Manually patch proto.js to assign strings directly:
- message.sval = String(object.sval);
+ message.sval = object.sval;Repeat this for any affected wrapper types (String, BitString, etc.).
🛠 Recommended Fix
-
Fix upstream generation:
- Use
globalThis.String(...)for safe access. - Or rename wrapper types to avoid collisions (
PgString,PgBitString, etc.).
- Use
-
Add post-gen patch to your build or protogen script to auto-replace the problem lines.
-
Track this issue across upgrades to
libpg-query-nodeorpbjs.
📌 Action Item
Update the proto.js generation step to include either:
- A safe global reference (
globalThis.String) - Or a transform script to patch conflicting lines after generation
Metadata
Metadata
Assignees
Labels
No labels