{"record":{"id":"aa6179ba0d5fc357","repo":"NativeScript/NativeScript","slug":"failed-to-execute-decode-on-textdecoder-the-p","errorCode":null,"errorMessage":"Failed to execute 'decode' on 'TextDecoder': The provided value is not of type '(ArrayBuffer or ArrayBufferView)'","messagePattern":"Failed to execute 'decode' on 'TextDecoder': The provided value is not of type '\\(ArrayBuffer or ArrayBufferView\\)'","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/text/text-common.ts","lineNumber":72,"sourceCode":"\t}\n\tif (point <= 0x007f) {\n\t\treturn nonAsciiChars;\n\t} else if (point <= 0x07ff) {\n\t\treturn String.fromCharCode((0x6 << 5) | (point >>> 6), (0x2 << 6) | (point & 0x3f));\n\t} else {\n\t\treturn String.fromCharCode((0xe /*0b1110*/ << 4) | (point >>> 12), (0x2 /*0b10*/ << 6) | ((point >>> 6) & 0x3f) /*0b00111111*/, (0x2 /*0b10*/ << 6) | (point & 0x3f) /*0b00111111*/);\n\t}\n}\n\nexport class TextDecoder {\n\tpublic get encoding() {\n\t\treturn 'utf-8';\n\t}\n\n\tpublic decode(input: BufferSource): string {\n\t\tconst buffer = ArrayBuffer.isView(input) ? input.buffer : input;\n\t\tif (Object_prototype_toString.call(buffer) !== ArrayBufferString) {\n\t\t\tthrow Error(\"Failed to execute 'decode' on 'TextDecoder': The provided value is not of type '(ArrayBuffer or ArrayBufferView)'\");\n\t\t}\n\t\tconst inputAs8 = new Uint8Array(buffer);\n\t\tlet resultingString = '';\n\t\tfor (let index = 0, len = inputAs8.length | 0; index < len; index = (index + 32768) | 0) {\n\t\t\tresultingString += String.fromCharCode.apply(0, inputAs8.slice(index, (index + 32768) | 0));\n\t\t}\n\n\t\treturn resultingString.replace(/[\\xc0-\\xff][\\x80-\\xbf]*/g, decoderReplacer);\n\t}\n\n\tpublic toString() {\n\t\treturn '[object TextDecoder]';\n\t}\n\n\t[Symbol.toStringTag] = 'TextDecoder';\n}\n\nexport class TextEncoder {","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/NativeScript/NativeScript/blob/6800aefa6511d23ddad8beb38ccc8541c5e91628/packages/core/text/text-common.ts#L54-L90","documentation":"The TextDecoder shim in @nativescript/core validates that the input to decode() resolves to a real ArrayBuffer (after unwrapping ArrayBufferViews via input.buffer). If the argument is not an ArrayBuffer or typed view — e.g. a plain number, string, null, or an array-like — it throws this Web-API-compatible TypeError-like Error.","triggerScenarios":"Calling textDecoder.decode(x) where x is not BufferSource: a plain string, number, null/undefined, a Node Buffer from a different realm whose .buffer isn't a real ArrayBuffer, or a plain array.","commonSituations":"Decoding HTTP/socket data where a string was passed instead of bytes; passing a Node.js Buffer in an environment where ArrayBuffer.toString tag check fails; off-by-one code that decode()'s byteOffset slices incorrectly (e.g. slicing to a number instead of a view).","solutions":["Ensure you pass an ArrayBuffer or a typed array (Uint8Array etc.); wrap raw bytes with new Uint8Array(bytes).decode via new TextDecoder().decode(new Uint8Array(raw)).","If you have an ArrayBuffer with offset, pass new Uint8Array(buffer, byteOffset, byteLength) rather than a plain object.","If input may be a string, decode differently (it's already text) or encode it first with new TextEncoder().encode(str).","Guard before calling: check input instanceof ArrayBuffer || ArrayBuffer.isView(input)."],"exampleFix":"// before\nconst text = decoder.decode(await response.arrayBuffer().then(b => b.byteLength));\n// after\nconst buf = await response.arrayBuffer();\nconst text = decoder.decode(new Uint8Array(buf));","handlingStrategy":"type-guard","validationCode":"function isBufferSource(v: unknown): v is ArrayBuffer | ArrayBufferView {\n  return v instanceof ArrayBuffer || (ArrayBuffer.isView(v as any));\n}\nif (!isBufferSource(input)) throw new TypeError('decode expects ArrayBuffer or ArrayBufferView');","typeGuard":"function isBufferSource(v: unknown): v is ArrayBuffer | ArrayBufferView {\n  return v instanceof ArrayBuffer || ArrayBuffer.isView(v as any);\n}","tryCatchPattern":"try {\n  const text = decoder.decode(input);\n} catch (e) {\n  if (e.message.includes(\"'decode' on 'TextDecoder'\")) {\n    console.error('decode() needs ArrayBuffer/typed array, got:', typeof input);\n  }\n  throw e;\n}","preventionTips":["Always pass typed arrays (Uint8Array) or ArrayBuffers to decode()","Never pass strings, numbers, or Node-only Buffer subclasses blindly","Derive views with explicit offsets: new Uint8Array(buf, byteOffset, byteLength)","Unit-test decode paths with realistic binary payloads"],"tags":["text-decoding","types","buffer","encoding"],"backgroundTag":"invalid-decode-input","analyzedSha":"6800aefa6511d23ddad8beb38ccc8541c5e91628","analyzedAt":"2026-08-30T20:04:56.809Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}