opentypejs / opentype.js

Read and write OpenType fonts using JavaScript.
https://opentype.js.org/
MIT License
4.38k stars 468 forks source link

Deprecated code usage warning when running examples #473

Closed pwagland closed 2 years ago

pwagland commented 3 years ago

Opentypejs is using a deprecated node Buffer method.

Expected Behavior

Sample code should run without warnings.

Current Behavior

13:59 $ node generate-font-node.js
(node:50681) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(Use `node --trace-deprecation ...` to show where the warning was created)

✔ ~/src/src/opentype.js/examples [master|…1]
14:00 $ node --trace-deprecation generate-font-node.js
(node:50833) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
    at showFlaggedDeprecation (node:buffer:184:11)
    at new Buffer (node:buffer:271:3)
    at arrayBufferToNodeBuffer (/Users/pwagland/src/src/opentype.js/dist/opentype.js:7884:19)
    at Font.download (/Users/pwagland/src/src/opentype.js/dist/opentype.js:13444:23)
    at Object.<anonymous> (/Users/pwagland/src/src/opentype.js/examples/generate-font-node.js:145:6)
    at Module._compile (node:internal/modules/cjs/loader:1108:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
    at Module.load (node:internal/modules/cjs/loader:988:32)
    at Function.Module._load (node:internal/modules/cjs/loader:828:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)

Possible Solution

The following patch removes the warning:

diff --git a/src/util.js b/src/util.js
index 8ed9188..48ed45d 100644
--- a/src/util.js
+++ b/src/util.js
@@ -17,7 +17,7 @@ function nodeBufferToArrayBuffer(buffer) {
 }

 function arrayBufferToNodeBuffer(ab) {
-    const buffer = new Buffer(ab.byteLength);
+    const buffer = Buffer.alloc(ab.byteLength);
     const view = new Uint8Array(ab);
     for (let i = 0; i < buffer.length; ++i) {
         buffer[i] = view[i];

Although everything seems to work, it is nice to not have the warning when you first try anything in the repo.

Steps to Reproduce (for bugs)

  1. clone repo
  2. run node test

Your Environment

yne commented 2 years ago

Merged in https://github.com/opentypejs/opentype.js/pull/474