MCPcopy
hub / github.com/feross/buffer / from

Function from

index.js:113–163  ·  view source on GitHub ↗
(value, encodingOrOffset, length)

Source from the content-addressed store, hash-verified

111Buffer.poolSize = 8192 // not used by this implementation
112
113function from (value, encodingOrOffset, length) {
114 if (typeof value === 'string') {
115 return fromString(value, encodingOrOffset)
116 }
117
118 if (ArrayBuffer.isView(value)) {
119 return fromArrayView(value)
120 }
121
122 if (value == null) {
123 throw new TypeError(
124 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
125 'or Array-like Object. Received type ' + (typeof value)
126 )
127 }
128
129 if (isInstance(value, ArrayBuffer) ||
130 (value && isInstance(value.buffer, ArrayBuffer))) {
131 return fromArrayBuffer(value, encodingOrOffset, length)
132 }
133
134 if (typeof SharedArrayBuffer !== 'undefined' &&
135 (isInstance(value, SharedArrayBuffer) ||
136 (value && isInstance(value.buffer, SharedArrayBuffer)))) {
137 return fromArrayBuffer(value, encodingOrOffset, length)
138 }
139
140 if (typeof value === 'number') {
141 throw new TypeError(
142 'The "value" argument must not be of type number. Received type number'
143 )
144 }
145
146 const valueOf = value.valueOf && value.valueOf()
147 if (valueOf != null && valueOf !== value) {
148 return Buffer.from(valueOf, encodingOrOffset, length)
149 }
150
151 const b = fromObject(value)
152 if (b) return b
153
154 if (typeof Symbol !== 'undefined' && Symbol.toPrimitive != null &&
155 typeof value[Symbol.toPrimitive] === 'function') {
156 return Buffer.from(value[Symbol.toPrimitive]('string'), encodingOrOffset, length)
157 }
158
159 throw new TypeError(
160 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
161 'or Array-like Object. Received type ' + (typeof value)
162 )
163}
164
165/**
166 * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError

Callers 2

BufferFunction · 0.85
index.jsFile · 0.85

Calls 5

fromStringFunction · 0.85
fromArrayViewFunction · 0.85
isInstanceFunction · 0.85
fromArrayBufferFunction · 0.85
fromObjectFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…