MCPcopy Index your code
hub / github.com/nodejs/node / read

Method read

lib/internal/webstreams/readablestream.js:1024–1082  ·  view source on GitHub ↗

* @param {ArrayBufferView} view * @param {{ * min? : number * }} [options] * @returns {Promise<{ * value : ArrayBufferView, * done : boolean, * }>}

(view, options = kEmptyObject)

Source from the content-addressed store, hash-verified

1022 * }>}
1023 */
1024 async read(view, options = kEmptyObject) {
1025 if (!isReadableStreamBYOBReader(this))
1026 throw new ERR_INVALID_THIS('ReadableStreamBYOBReader');
1027 if (!isArrayBufferView(view)) {
1028 throw new ERR_INVALID_ARG_TYPE(
1029 'view',
1030 [
1031 'Buffer',
1032 'TypedArray',
1033 'DataView',
1034 ],
1035 view,
1036 );
1037 }
1038 validateObject(options, 'options', kValidateObjectAllowObjectsAndNull);
1039
1040 const viewByteLength = ArrayBufferViewGetByteLength(view);
1041 const viewBuffer = ArrayBufferViewGetBuffer(view);
1042
1043 if (isSharedArrayBuffer(viewBuffer)) {
1044 throw new ERR_INVALID_ARG_VALUE(
1045 'view',
1046 view,
1047 'must not be backed by a SharedArrayBuffer',
1048 );
1049 }
1050
1051 const viewBufferByteLength = ArrayBufferPrototypeGetByteLength(viewBuffer);
1052
1053 if (viewByteLength === 0 || viewBufferByteLength === 0) {
1054 throw new ERR_INVALID_STATE.TypeError(
1055 'View or Viewed ArrayBuffer is zero-length or detached');
1056 }
1057
1058 // Supposed to assert here that the view's buffer is not
1059 // detached, but there's no API available to use to check that.
1060
1061 const min = options?.min ?? 1;
1062 if (typeof min !== 'number')
1063 throw new ERR_INVALID_ARG_TYPE('options.min', 'number', min);
1064 if (!NumberIsInteger(min))
1065 throw new ERR_INVALID_ARG_VALUE('options.min', min, 'must be an integer');
1066 if (min <= 0)
1067 throw new ERR_INVALID_ARG_VALUE('options.min', min, 'must be greater than 0');
1068 if (!isDataView(view)) {
1069 if (min > TypedArrayPrototypeGetLength(view)) {
1070 throw new ERR_OUT_OF_RANGE('options.min', '<= view.length', min);
1071 }
1072 } else if (min > viewByteLength) {
1073 throw new ERR_OUT_OF_RANGE('options.min', '<= view.byteLength', min);
1074 }
1075
1076 if (this[kState].stream === undefined) {
1077 throw new ERR_INVALID_STATE.TypeError('The reader is not attached to a stream');
1078 }
1079 const readIntoRequest = new ReadIntoRequest();
1080 readableStreamBYOBReaderRead(this, view, min, readIntoRequest);
1081 return readIntoRequest.promise;

Callers 1

readFunction · 0.45

Calls 4

ArrayBufferViewGetBufferFunction · 0.85
isDataViewFunction · 0.85

Tested by

no test coverage detected