You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The textDecoder has strange behavior when working with cbor data.
In node, it is working as expected but in nativescript the TextDecoder returns a strange format:
Thanks a lot for reporting this. I have also taken a look in cbor repo discussion about it and I can confirm NS polyfill for these 2 classes is incomplete or outdated. There is already been a small discussion with another member and I believe we'll look into this.
Until then, I suggest that you use a 3rd-party polyfill for those if possible.
For a typed-array view, input.buffer is the whole underlying ArrayBuffer, so the decoder decodes bytes outside the view.
Minimal reproduction
constbytes=newUint8Array([0x78,0x78,0x68,0x69]);// "xxhi"newTextDecoder().decode(bytes.subarray(2));// expected "hi"; decodes the whole buffer instead
Libraries that decode from sub-arrays of a larger buffer — CBOR parsers, protobuf, length-prefixed protocols — break silently. In our case, libp2p's multistream-select decodes protocol names from sub-arrays, so every stream negotiation on NativeScript failed ("did not read multistream-select header", and the remote's protocol read as an empty string): no identify and no circuit-relay reservation, while the same code works in Node, browsers and React Native.
Activity
CatchABus commentedon Jul 27, 2022
Thanks a lot for reporting this. I have also taken a look in cbor repo discussion about it and I can confirm NS polyfill for these 2 classes is incomplete or outdated. There is already been a small discussion with another member and I believe we'll look into this.
Until then, I suggest that you use a 3rd-party polyfill for those if possible.
abdallahkadour commentedon Dec 9, 2022
As a workaround, the library
cbor-synccould be used.kjeib commentedon Oct 14, 2026
I believe the root cause of this (and of a failure we hit with libp2p) is that
TextDecoder.decode()ignores a view'sbyteOffsetandbyteLength.packages/core/text/text-common.ts, still the case onmain:For a typed-array view,
input.bufferis the whole underlyingArrayBuffer, so the decoder decodes bytes outside the view.Minimal reproduction
Libraries that decode from sub-arrays of a larger buffer — CBOR parsers, protobuf, length-prefixed protocols — break silently. In our case, libp2p's multistream-select decodes protocol names from sub-arrays, so every stream negotiation on NativeScript failed ("did not read multistream-select header", and the remote's protocol read as an empty string): no identify and no circuit-relay reservation, while the same code works in Node, browsers and React Native.
Suggested fix
Environment:
@nativescript/core8.9.9 (the same code is in 9.1.3 /main), NativeScript Android runtime 8.9.2, V8 10.3, Galaxy S7 (Android 8).Workaround until it is fixed — wrap
decodeso views are copied first: