Buffer.from(buffer)
buffer
<Buffer> | <Uint8Array> 要从中复制数据的现有Buffer
或Uint8Array
。
将传入的 buffer
数据复制到新的 Buffer
实例上。
import { Buffer } from 'node:buffer';
const buf1 = Buffer.from('buffer');
const buf2 = Buffer.from(buf1);
buf1[0] = 0x61;
console.log(buf1.toString());
// 打印: auffer
console.log(buf2.toString());
// 打印: buffer
const { Buffer } = require('node:buffer');
const buf1 = Buffer.from('buffer');
const buf2 = Buffer.from(buf1);
buf1[0] = 0x61;
console.log(buf1.toString());
// 打印: auffer
console.log(buf2.toString());
// 打印: buffer
如果 buffer
不是 Buffer
或其他适用于 Buffer.from()
变体的类型,则将抛出 TypeError
。
buffer
<Buffer> | <Uint8Array> An existingBuffer
orUint8Array
from which to copy data.
Copies the passed buffer
data onto a new Buffer
instance.
import { Buffer } from 'node:buffer';
const buf1 = Buffer.from('buffer');
const buf2 = Buffer.from(buf1);
buf1[0] = 0x61;
console.log(buf1.toString());
// Prints: auffer
console.log(buf2.toString());
// Prints: buffer
const { Buffer } = require('node:buffer');
const buf1 = Buffer.from('buffer');
const buf2 = Buffer.from(buf1);
buf1[0] = 0x61;
console.log(buf1.toString());
// Prints: auffer
console.log(buf2.toString());
// Prints: buffer
A TypeError
will be thrown if buffer
is not a Buffer
or another type
appropriate for Buffer.from()
variants.