Skip to content

Commit 1549d0b

Browse files
committed
Reduce cast repetition in getLdSignatureObject
Destructure `signature` from `jsonLd` once after the initial type guard, instead of repeating `jsonLd as { signature?: unknown }` for each of the three follow-up checks. Two early returns also replace the original chained conditions, which reads more naturally. Keep the return type as `Record<string, unknown> | undefined`. The caller in `verifyJsonLd` reads `sig.creator`, `sig.signatureValue`, and `sig.type` as string properties; switching to `object | undefined` would push the cast burden into the caller at every property access, trading one local cast for three remote ones. #769 (comment) Assisted-by: Claude Code:claude-opus-4-7
1 parent 1c7105b commit 1549d0b

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

  • packages/fedify/src/sig

packages/fedify/src/sig/ld.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -463,14 +463,18 @@ function getLdSignatureObject(
463463
jsonLd: unknown,
464464
): Record<string, unknown> | undefined {
465465
if (
466-
typeof jsonLd === "object" && jsonLd != null && "signature" in jsonLd &&
467-
typeof (jsonLd as { signature?: unknown }).signature === "object" &&
468-
(jsonLd as { signature?: unknown }).signature != null &&
469-
!Array.isArray((jsonLd as { signature?: unknown }).signature)
466+
typeof jsonLd !== "object" || jsonLd == null || !("signature" in jsonLd)
470467
) {
471-
return (jsonLd as { signature: Record<string, unknown> }).signature;
468+
return undefined;
472469
}
473-
return undefined;
470+
const { signature } = jsonLd as { signature: unknown };
471+
if (
472+
typeof signature !== "object" || signature == null ||
473+
Array.isArray(signature)
474+
) {
475+
return undefined;
476+
}
477+
return signature as Record<string, unknown>;
474478
}
475479

476480
/**

0 commit comments

Comments
 (0)