Skip to content

fix(firestore): keep non-ASCII characters in document params - #1982

Draft
IzaakGough wants to merge 4 commits into
masterfrom
@invertase/firestore-params-from-payload
Draft

IzaakGough wants to merge 4 commits into
masterfrom
@invertase/firestore-params-from-payload

Conversation

@IzaakGough

@IzaakGough IzaakGough commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1459.

In 2nd gen Firestore triggers, document IDs with non-ASCII characters came through garbled in event.params, event.document and event.subject, so Helvétios arrived as Helvétios.

The SDK now reads the document path from the event data, which carries it as correct UTF-8. It falls back to the old value when the data has no path.

  • Nothing is guessed or repaired, so a real ID like é is kept as is.
  • Realtime Database triggers have the same problem, but their event data carries no path, so they need a separate fix.

Tested with unit tests, the emulator and a deployed trigger.

relnote: Fixed non-ASCII characters being garbled in event.params, event.document and event.subject for 2nd gen Firestore triggers. If you re-decode these values from latin1 as a workaround, remove it.

…rams are not garbled

The document CloudEvent attribute arrives as an HTTP header that Node decodes
as latin1, so params with non-ASCII characters were mojibake. The payload
carries the same path as UTF-8; use it and fall back to the attribute.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a mechanism to extract the Firestore document path directly from the event payload (protobuf or JSON) to prevent encoding issues caused by Node.js decoding HTTP headers as latin1. It updates the event handlers to use this new path and adds comprehensive unit tests. The feedback suggests adding an early guard clause in documentNameFromPayload to avoid the performance overhead of catching a TypeError when data is null or undefined.

Comment thread src/common/providers/firestore.ts Outdated
Comment on lines +74 to +88
export function documentNameFromPayload(
data: unknown,
datacontenttype?: string
): string | undefined {
try {
if (datacontenttype?.includes("application/protobuf") || Buffer.isBuffer(data)) {
const decoded = DocumentEventData.decode(Buffer.from(data as Uint8Array));
return decoded.value?.name || decoded.oldValue?.name || undefined;
}
const json = data as { value?: { name?: string }; oldValue?: { name?: string } } | undefined;
return json?.value?.name || json?.oldValue?.name || undefined;
} catch (_e) {
return undefined;
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When data is null or undefined (which is common for events without a payload or in tests), calling Buffer.from(data) will throw a TypeError. Although this is caught by the try-catch block, throwing and catching exceptions in Node.js is expensive because it captures the stack trace. Adding a guard clause to return undefined early when data is falsy avoids this overhead and improves performance.

export function documentNameFromPayload(
  data: unknown,
  datacontenttype?: string
): string | undefined {
  if (!data) {
    return undefined;
  }
  try {
    if (datacontenttype?.includes("application/protobuf") || Buffer.isBuffer(data)) {
      const decoded = DocumentEventData.decode(Buffer.from(data as Uint8Array));
      return decoded.value?.name || decoded.oldValue?.name || undefined;
    }
    const json = data as { value?: { name?: string }; oldValue?: { name?: string } } | undefined;
    return json?.value?.name || json?.oldValue?.name || undefined;
  } catch (_e) {
    return undefined;
  }
}

The document path lookup added a third decode of the same payload. Cache the decoded message per payload so each event decodes it once.
…x subject

Replace the module-level decode cache with an explicit decode in onOperation
and onChangedOperation, passed down to the path lookup and snapshot builders.
Decode errors now surface once from that step. Also rebuild event.subject from
the payload path so it matches document and params.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gen2: Encoding issue with special characters in path parameters

1 participant