Skip to content

Fix CBOR decoder "keyDecoder is undefined" error with runtime patch #928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 7, 2025

This PR fixes a critical runtime error where CBOR decoders fail with "Cannot read properties of undefined (reading 'decode')" due to a missing keyDecoder property.

Problem

Users encounter this error when using CBOR functionality:

TypeError: Cannot read properties of undefined (reading 'decode')
    at CborDecoder.key (CborDecoderBase.ts:293:33)
    at CborDecoder.readObj (CborDecoderBase.ts:253:26)

The issue occurs because this.keyDecoder is undefined in the CborDecoderBase constructor.

Root Cause

The problem stems from a re-export issue in the @jsonjoy.com/util package. The file @jsonjoy.com/util/lib/buffers/utf8/sharedCachedUtf8Decoder.js uses:

tslib_1.__exportStar(require("@jsonjoy.com/buffers/lib/utf8/sharedCachedUtf8Decoder"), exports);

However, tslib.__exportStar intentionally skips the default export. Since the original module exports the shared UTF8 decoder as default, it gets lost during re-export, making sharedCachedUtf8Decoder_1.default undefined.

Solution

This PR implements a runtime patch that:

  1. Automatically detects when the default export is missing from the re-exported module
  2. Imports the correct decoder directly from the source @jsonjoy.com/buffers package
  3. Patches the module at runtime to restore the missing default export
  4. Applies transparently when CBOR utilities are imported

Usage

The fix works automatically - no code changes required:

// This now works correctly (patch applied automatically)
import {CborDecoder} from '@jsonjoy.com/json-pack/lib/cbor/CborDecoder';

const decoder = new CborDecoder();
const encoded = new CborEncoder().encode({hello: "world"});
const decoded = decoder.decode(encoded); // ✅ Works!

CLI tools also work correctly:

echo '{"test": "data"}' | npx json-pack --cbor | npx json-unpack --cbor
# ✅ Outputs: {"test":"data"}

Testing

Added comprehensive regression tests that verify:

  • CBOR decoders can be instantiated without errors
  • Encoding/decoding works with string keys of various types
  • Complex nested objects are handled correctly
  • The runtime patch applies automatically

Backward Compatibility

The fix is completely backward compatible and requires no changes to existing code. The patch only activates when the problematic re-export is detected.

Fixes #925.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

- Add runtime patch for sharedCachedUtf8Decoder export issue
- Fix CLI json-unpack tool to use patched CBOR decoder
- Add comprehensive regression tests for the fix
- Patch automatically applies when importing CBOR utilities

Co-authored-by: streamich <[email protected]>
@Copilot Copilot AI changed the title [WIP] json-pack: sharedCachedUtf8Decoder_1.default is undefined because tslib exportStar skips export named default Fix CBOR decoder "keyDecoder is undefined" error with runtime patch Aug 7, 2025
@Copilot Copilot AI requested a review from streamich August 7, 2025 15:06
Copilot finished work on behalf of streamich August 7, 2025 15:06
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.

json-pack: sharedCachedUtf8Decoder_1.default is undefined because tslib exportStar skips export named default
2 participants