Skip to content

Comments

[node] buffer: allow ArrayBuffer | string to be passed to Buffer.from#72097

Merged
typescript-bot merged 7 commits intoDefinitelyTyped:masterfrom
sapphi-red:node-buffer-from-arraybuffer-or-string
Apr 25, 2025
Merged

[node] buffer: allow ArrayBuffer | string to be passed to Buffer.from#72097
typescript-bot merged 7 commits intoDefinitelyTyped:masterfrom
sapphi-red:node-buffer-from-arraybuffer-or-string

Conversation

@sapphi-red
Copy link
Contributor

Please fill in this template.

Select one of these and delete the others:

If changing an existing definition:


I was passing ArrayBuffer | string to Buffer.from, but that started to error after #72056. This PR adds a signature for that case.

@typescript-bot
Copy link
Contributor

typescript-bot commented Mar 3, 2025

@sapphi-red Thank you for submitting this PR!

This is a live comment that I will keep updated.

1 package in this PR

Code Reviews

Because this is a widely-used package, a DT maintainer will need to review it before it can be merged.

You can test the changes of this PR in the Playground.

Status

  • ✅ No merge conflicts
  • ✅ Continuous integration tests have passed
  • ✅ Most recent commit is approved by a DT maintainer

All of the items on the list are green. To merge, you need to post a comment including the string "Ready to merge" to bring in your changes.


Diagnostic Information: What the bot saw about this PR
{
  "type": "info",
  "now": "-",
  "pr_number": 72097,
  "author": "sapphi-red",
  "headCommitOid": "6668d64060ff17a592887ce42dd70ad9fc028e13",
  "mergeBaseOid": "515f950351b890f8c45bbfcc793cdc9598da67e8",
  "lastPushDate": "2025-03-03T05:21:45.000Z",
  "lastActivityDate": "2025-04-25T06:11:16.000Z",
  "mergeOfferDate": "2025-04-25T05:53:56.000Z",
  "mergeRequestDate": "2025-04-25T06:11:16.000Z",
  "mergeRequestUser": "sapphi-red",
  "hasMergeConflict": false,
  "isFirstContribution": false,
  "tooManyFiles": false,
  "hugeChange": false,
  "popularityLevel": "Critical",
  "pkgInfo": [
    {
      "name": "node",
      "kind": "edit",
      "files": [
        {
          "path": "types/node/buffer.buffer.d.ts",
          "kind": "definition"
        },
        {
          "path": "types/node/test/buffer.ts",
          "kind": "test"
        },
        {
          "path": "types/node/ts5.6/buffer.buffer.d.ts",
          "kind": "definition"
        },
        {
          "path": "types/node/v18/buffer.buffer.d.ts",
          "kind": "definition"
        },
        {
          "path": "types/node/v18/test/buffer.ts",
          "kind": "test"
        },
        {
          "path": "types/node/v18/ts5.6/buffer.buffer.d.ts",
          "kind": "definition"
        },
        {
          "path": "types/node/v20/buffer.buffer.d.ts",
          "kind": "definition"
        },
        {
          "path": "types/node/v20/test/buffer.ts",
          "kind": "test"
        },
        {
          "path": "types/node/v20/ts5.6/buffer.buffer.d.ts",
          "kind": "definition"
        }
      ],
      "owners": [
        "Microsoft",
        "jkomyno",
        "alvis",
        "r3nya",
        "btoueg",
        "smac89",
        "touffy",
        "DeividasBakanas",
        "eyqs",
        "Hannes-Magnusson-CK",
        "hoo29",
        "kjin",
        "ajafff",
        "islishude",
        "mwiktorczyk",
        "mohsen1",
        "galkin",
        "parambirs",
        "eps1lon",
        "ThomasdenH",
        "WilcoBakker",
        "wwwy3y3",
        "samuela",
        "kuehlein",
        "bhongy",
        "chyzwar",
        "trivikr",
        "yoursunny",
        "qwelias",
        "ExE-Boss",
        "peterblazejewicz",
        "addaleax",
        "victorperin",
        "NodeJS",
        "LinusU",
        "wafuwafu13",
        "mcollina",
        "Semigradsky"
      ],
      "addedOwners": [],
      "deletedOwners": [],
      "popularityLevel": "Critical"
    }
  ],
  "reviews": [
    {
      "type": "approved",
      "reviewer": "jakebailey",
      "date": "2025-04-25T05:53:15.000Z",
      "isMaintainer": true
    },
    {
      "type": "approved",
      "reviewer": "Renegade334",
      "date": "2025-04-25T04:24:01.000Z",
      "isMaintainer": false
    }
  ],
  "mainBotCommentID": 2693307815,
  "ciResult": "pass"
}

@typescript-bot
Copy link
Contributor

@typescript-bot typescript-bot moved this to Waiting for Code Reviews in Pull Request Status Board Mar 3, 2025
@typescript-bot typescript-bot moved this from Waiting for Code Reviews to Needs Maintainer Review in Pull Request Status Board Mar 3, 2025
@typescript-bot typescript-bot moved this from Needs Maintainer Review to Waiting for Code Reviews in Pull Request Status Board Mar 3, 2025
@sapphi-red
Copy link
Contributor Author

I pushed a commit because it didn't fix the case I wanted to 😅

@sapphi-red
Copy link
Contributor Author

By the way, it seems this test fails.

const a = null as unknown as Uint8Array<SharedArrayBuffer>;
// $ExpectType Buffer || Buffer<SharedArrayBuffer>
Buffer.from(a); // returns Buffer<ArrayBuffer>

@typescript-bot typescript-bot moved this from Waiting for Code Reviews to Needs Maintainer Review in Pull Request Status Board Mar 3, 2025
@typescript-bot
Copy link
Contributor

@Renegade334 Thank you for reviewing this PR! The author has pushed new commits since your last review. Could you take another look and submit a fresh review?

@Renegade334
Copy link
Contributor

By the way, it seems this test fails.

const a = null as unknown as Uint8Array<SharedArrayBuffer>;
// $ExpectType Buffer || Buffer<SharedArrayBuffer>
Buffer.from(a); // returns Buffer<ArrayBuffer>

There is some confusion here. Passing an array buffer view (ie. a typed array) to the constructor methods will copy the contents into a new Buffer, which is always backed by an ArrayBuffer. It does not create a view on the existing array buffer.

Copy link
Contributor

@Renegade334 Renegade334 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will need to add an equivalent signature to ts5.6/buffer.buffer.d.ts (the only difference being a return type of Buffer rather than Buffer<...>, as these declarations predate the type parameter)

@sapphi-red
Copy link
Contributor Author

There is some confusion here. Passing an array buffer view (ie. a typed array) to the constructor methods will copy the contents into a new Buffer, which is always backed by an ArrayBuffer. It does not create a view on the existing array buffer.

Oh, I see. Thanks for correcting!

@typescript-bot typescript-bot moved this from Needs Maintainer Review to Waiting for Code Reviews in Pull Request Status Board Mar 5, 2025
@typescript-bot
Copy link
Contributor

@Renegade334 Thank you for reviewing this PR! The author has pushed new commits since your last review. Could you take another look and submit a fresh review?

@typescript-bot typescript-bot moved this from Waiting for Code Reviews to Needs Maintainer Review in Pull Request Status Board Mar 5, 2025
@typescript-bot
Copy link
Contributor

@typescript-bot typescript-bot added the Unreviewed No one showed up to review this PR, so it'll be reviewed by a DT maintainer. label Mar 14, 2025
@typescript-bot
Copy link
Contributor

@typescript-bot typescript-bot moved this from Needs Maintainer Review to Needs Maintainer Action in Pull Request Status Board Mar 21, 2025
@jakebailey jakebailey requested a review from Renegade334 April 1, 2025 01:33
@typescript-bot typescript-bot added the Other Approved This PR was reviewed and signed-off by a community member. label Apr 25, 2025
@typescript-bot typescript-bot added Maintainer Approved Self Merge This PR can now be self-merged by the PR author or an owner and removed Unreviewed No one showed up to review this PR, so it'll be reviewed by a DT maintainer. labels Apr 25, 2025
@typescript-bot typescript-bot moved this from Needs Maintainer Action to Waiting for Author to Merge in Pull Request Status Board Apr 25, 2025
@typescript-bot
Copy link
Contributor

@sapphi-red: Everything looks good here. I am ready to merge this PR (at 6668d64) on your behalf whenever you think it's ready.

If you'd like that to happen, please post a comment saying:

Ready to merge

and I'll merge this PR almost instantly. Thanks for helping out! ❤️

(@microsoft, @jkomyno, @alvis, @r3nya, @btoueg, @smac89, @Touffy, @DeividasBakanas, @eyqs, @Hannes-Magnusson-CK, @hoo29, @kjin, @ajafff, @islishude, @mwiktorczyk, @mohsen1, @galkin, @parambirs, @eps1lon, @ThomasdenH, @WilcoBakker, @wwwy3y3, @samuela, @kuehlein, @bhongy, @chyzwar, @trivikr, @yoursunny, @qwelias, @ExE-Boss, @peterblazejewicz, @addaleax, @victorperin, @nodejs, @LinusU, @wafuwafu13, @mcollina, @Semigradsky: you can do this too.)

@sapphi-red
Copy link
Contributor Author

Ready to merge

@typescript-bot typescript-bot moved this from Waiting for Author to Merge to Recently Merged in Pull Request Status Board Apr 25, 2025
@typescript-bot typescript-bot merged commit 3634b01 into DefinitelyTyped:master Apr 25, 2025
15 checks passed
@sapphi-red sapphi-red deleted the node-buffer-from-arraybuffer-or-string branch April 28, 2025 03:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Critical package Maintainer Approved Other Approved This PR was reviewed and signed-off by a community member. Self Merge This PR can now be self-merged by the PR author or an owner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants