kaitai-io / kaitai_struct_javascript_runtime

Kaitai Struct: runtime for JavaScript
Apache License 2.0
37 stars 22 forks source link

"new Buffer()" is deprecated since Node.js 6 #27

Open generalmimon opened 2 years ago

generalmimon commented 2 years ago

The following line:

https://github.com/kaitai-io/kaitai_struct_javascript_runtime/blob/1ae3924e68a29ff43b32b30debfdadd3e320c8ec/KaitaiStream.js#L585

is marked in VS Code with a deprecation warning:

image

Related article in Node.js docs: https://nodejs.org/en/docs/guides/buffer-constructor-deprecation/

generalmimon commented 2 years ago

is marked in VS Code with a deprecation warning:

The warning from VS Code isn't entirely accurate, though:

  1. it thinks that the constructor we call is (str: string, encoding?: BufferEncoding), when we actually call these - new Buffer(array) and new Buffer(buffer), which are clearly not the same:

    new Buffer(array)

    Stability: 0 - Deprecated: Use Buffer.from(array) instead.

    Allocates a new Buffer using an array of octets.

    // Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'
    const buf = new Buffer([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);

    new Buffer(buffer)

    Stability: 0 - Deprecated: Use Buffer.from(buffer) instead.

    Copies the passed buffer data onto a new Buffer instance.

    const buf1 = new Buffer('buffer');
    const buf2 = new Buffer(buf1);
    
    buf1[0] = 0x61;
    
    console.log(buf1.toString());
    // Prints: auffer
    console.log(buf2.toString());
    // Prints: buffer
  2. it isn't deprecated since Node.js 10, but even since Node.js 6:

    ▼ History

    Version Changes
    v10.0.0 Calling this constructor emits a deprecation warning when run from code outside the node_modules directory.
    v7.2.1 Calling this constructor no longer emits a deprecation warning.
    v7.0.0 Calling this constructor emits a deprecation warning now.
    v6.0.0 Deprecated since: v6.0.0