fix: decode NDEF URI record identifier-code byte on NFC scan#95
Conversation
Cards containing NDEF URI records (e.g. https://zpr.au/...) were decoded as \u0004zpr.au/... because the URI identifier-code byte (0x04 = https://) was passed raw through int2char with no prefix expansion. readNfcEvent now inspects record.tnf and record.type, delegating to NfcUtils helpers for proper Text and URI decoding. Also fixes the text-record path to use getTextFromNdefTextRecord, which handles variable-length language codes correctly instead of the previous hardcoded 3-byte slice heuristic.
📝 WalkthroughWalkthroughThe pull request adds NFC NDEF record parsing utilities to the capacitor-nfc mock, including enums for record type formats and helper methods to decode text and URI records. The implementation is updated to use these utilities instead of manual payload parsing, with corresponding test updates. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@__mocks__/`@capawesome-team/capacitor-nfc.ts:
- Around line 123-129: The mock getTextFromNdefTextRecord reads the entire
status byte as langLen which is wrong for NDEF text records; update it to mask
the status byte with 0x3F to obtain the language length (langLen = payload[0] &
0x3F) and inspect the encoding bit (payload[0] & 0x80) to choose the correct
TextDecoder ("utf-16" when set, otherwise "utf-8"); then decode from
payload.slice(langLen + 1) (or new Uint8Array(payload).subarray(langLen + 1))
and return that text so the mock matches real NDEF behavior.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 7c75e50d-9770-45d4-8276-672ff2421a78
📒 Files selected for processing (4)
__mocks__/@capawesome-team/capacitor-nfc.tssrc/__tests__/unit/lib/nfc.pure.test.tssrc/__tests__/unit/lib/nfc.test.tssrc/lib/nfc.ts
https://zpr.au/...cards) were decoded as\u0004zpr.au/...because the URI identifier-code byte (0x04=https://) was passed raw throughint2charwith no prefix expansion. This causedisZapUrlto silently return false and forwarded a malformed string toCoreAPI.run.readNfcEventinsrc/lib/nfc.tsnow checksrecord.tnfandrecord.type, delegating to the plugin's existingNfcUtilshelpers:getTextFromNdefTextRecordfor Text records (also fixes variable-length language code handling) andgetUriFromNdefUriRecord+mapUriIdentifierCodeToStringfor URI records. Non-WellKnown and unrecognised types fall back to the previousint2charbehaviour.__mocks__/@capawesome-team/capacitor-nfc.ts) to exportTypeNameFormatandRecordTypeDefinitionenums and implement the full set ofNfcUtilsmethods used byreadNfcEvent.nfc.pure.test.tsandnfc.test.tswith URI record cases coveringhttps://,http://www., no-prefix, unknown identifier code, empty URI body, and non-WellKnown TNF fallback.Summary by CodeRabbit
Bug Fixes
Tests