Skip to content

String.fromObject Breaks Due to Protobuf Type Shadowing Global String (will occur on upgrades) #92

@pyramation

Description

@pyramation

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

  • pbjs generates a message named String, which creates a local function that shadows the global String.
  • The call String(object.sval) no longer performs a type conversion — it calls the local constructor without new, so this is undefined.
  • 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 pbjs or updates to libpg-query-node unless 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.).
  • Add post-gen patch to your build or protogen script to auto-replace the problem lines.

  • Track this issue across upgrades to libpg-query-node or pbjs.


📌 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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions