{"record":{"id":"b8004202fbb7b2a5","repo":"denoland/deno","slug":"source-must-be-a-typedarray-or-a-dataview","errorCode":null,"errorMessage":"source must be a TypedArray or a DataView","messagePattern":"source must be a TypedArray or a DataView","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/v8.ts","lineNumber":375,"sourceCode":"    return buf;\n  }\n\n  transferArrayBuffer(_id: number, _arrayBuffer: ArrayBuffer): void {\n    op_v8_transfer_array_buffer(this[kHandle], _id, _arrayBuffer);\n  }\n\n  writeDouble(value: number): void {\n    op_v8_write_double(this[kHandle], value);\n  }\n\n  writeHeader(): void {\n    op_v8_write_header(this[kHandle]);\n    this[kHeaderWritten] = true;\n  }\n\n  writeRawBytes(source: ArrayBufferView): void {\n    if (!isArrayBufferView(source)) {\n      throw new TypeError(\n        \"source must be a TypedArray or a DataView\",\n      );\n    }\n    op_v8_write_raw_bytes(this[kHandle], source);\n  }\n\n  writeUint32(value: number): void {\n    op_v8_write_uint32(this[kHandle], value);\n  }\n\n  writeUint64(hi: number, lo: number): void {\n    op_v8_write_uint64(this[kHandle], hi, lo);\n  }\n\n  // deno-lint-ignore no-explicit-any\n  writeValue(value: any): void {\n    op_v8_write_value(this[kHandle], value);\n  }","sourceCodeStart":357,"sourceCodeEnd":393,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/v8.ts#L357-L393","documentation":"Serializer#writeRawBytes(source) copies raw bytes into the V8 value stream and requires source to be a TypedArray or DataView; the isArrayBufferView guard throws a plain TypeError for anything else. It is mostly reached from custom Serializer subclasses building host-object encodings.","triggerScenarios":"A class extending v8.Serializer whose _writeHostObject (or manual protocol code) calls writeRawBytes('...'), writeRawBytes(new ArrayBuffer(8)), or writeRawBytes([1, 2, 3]).","commonSituations":"Porting serialization code where the byte-container type changed (string vs Buffer); interop layers accumulating chunks as arrays; feeding a detached/transferred view whose underlying buffer is gone.","solutions":["Pass a view: writeRawBytes(Buffer.from(str, 'utf8')) or writeRawBytes(new Uint8Array(ab)).","Convert collected arrays first: writeRawBytes(Uint8Array.from(parts.flat())).","Annotate the parameter as ArrayBufferView so TypeScript rejects other inputs at the call site."],"exampleFix":"// before\n_writeHostObject(o) { this.writeRawBytes(o.hexString); } // string -> TypeError\n\n// after\n_writeHostObject(o) { this.writeRawBytes(Buffer.from(o.hexString, \"hex\")); }","handlingStrategy":"type-guard","validationCode":"function toByteView(src) {\n  if (ArrayBuffer.isView(src)) return src;\n  if (src instanceof ArrayBuffer) return new Uint8Array(src);\n  return Buffer.from(src); // strings, arrays of numbers\n}","typeGuard":"const isView = (v) => ArrayBuffer.isView(v);","tryCatchPattern":null,"preventionTips":["Standardize on Buffer for byte payloads inside serializer overrides.","Unit-test custom _writeHostObject paths — they are the usual source of raw-bytes type errors.","Never reuse an ArrayBuffer after transferring it; transfer detaches every view over it."],"tags":["v8","serializer","typedarray","typeerror"],"backgroundTag":"buffer-type-validation","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}