Skip to content

Conversation

Uzlopak
Copy link
Contributor

@Uzlopak Uzlopak commented May 10, 2025

We should avoid the use of Buffer and instead use Uint8Array.

Copy link
Contributor

👋 Hi! Thank you for this contribution! Just to let you know, our GitHub SDK team does a round of issue and PR reviews twice a week, every Monday and Friday! We have a process in place for prioritizing and responding to your input. Because you are a part of this community please feel free to comment, add to, or pick up any issues/PRs that are labeled with Status: Up for grabs. You & others like you are the reason all of this works! So thank you & happy coding! 🚀

@Uzlopak Uzlopak force-pushed the concat-uint8array branch from 30bec77 to 26b7046 Compare May 10, 2025 15:29
@Uzlopak Uzlopak changed the title Concat uint8array use Uint8Array instead of Buffer May 10, 2025
Copy link
Contributor

@gr2m gr2m left a comment

Choose a reason for hiding this comment

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

We should avoid the use of Buffer and instead use Uint8Array.

I agree.

But does this introduce a breaking change to the end user?

@Uzlopak
Copy link
Contributor Author

Uzlopak commented May 11, 2025

No, the unit test was wrong in the first place, because it passes a string, despite that the streams should use Buffers/Uint8Arrays anyway. It was just working, because somewhere the string got transformed to Buffer. If I would have written that unit test long time ago correct, it would not triggered that failing unit test today. :D


// we emit data, to ensure that the body attribute is preferred
request.emit("data", "bar");
request.emit("data", Buffer.from("bar"));
Copy link
Contributor Author

@Uzlopak Uzlopak May 11, 2025

Choose a reason for hiding this comment

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

It was working, because of

? data[0].toString("utf8")

So the string "bar" would be then "bar".toString()

I guess in the moment where we emit two times strings, it would error because Buffer.concat expects only Uint8Array and Buffers.

@gr2m
Copy link
Contributor

gr2m commented May 12, 2025

Okay I say we ship it, thanks for all the great work @Uzlopak!

But I suggest we do a release even though this is technically a refactor as far as I can tell. Or we do a feature release saying it introduces compatibility with Uint8Array? I'm not sure what the better option is, but I would like us to do a release just in case the changes cause problems in some obscure JS runtime environment, it will make it easier to track down the problem and to revert the change if necessary.

@Uzlopak
Copy link
Contributor Author

Uzlopak commented May 12, 2025

@gr2m

You mean this change as last minor of the current major?

@Uzlopak
Copy link
Contributor Author

Uzlopak commented May 12, 2025

@gr2m

Sure, makes sense.

I wrote following test case in main branch.

  it("resolves with a string if the body key of the request is defined but value is undefined", async () => {
    const request = new EventEmitter();
    // @ts-ignore body is not part of EventEmitter, which we are using
    // to mock the request object
    request.body = undefined;

    const promise = getPayload(request);

    // we emit data, to ensure that the body attribute is preferred
    request.emit("data", "foo");
    request.emit("data", "bar");
    request.emit("end");

    expect(await promise).toEqual("foobar");
  });

And I got following result:

 FAIL  test/integration/get-payload.test.ts > getPayload > resolves with a string if the body key of the request is defined but value is undefined
TypeError: The "list[0]" argument must be an instance of Buffer or Uint8Array. Received type string ('foo')
 ❯ EventEmitter.<anonymous> src/middleware/node/get-payload.ts:39:20
     37|         data.length === 1
     38|           ? data[0].toString("utf8")
     39|           : Buffer.concat(data).toString("utf8"),
       |                    ^
     40|       ),
     41|     );
 ❯ test/integration/get-payload.test.ts:105:13

So definetly the test was wrong by passing a string.

@gr2m gr2m changed the title use Uint8Array instead of Buffer fix: use Uint8Array instead of Buffer May 12, 2025
@gr2m gr2m merged commit 07dc367 into octokit:main May 12, 2025
8 checks passed
@github-project-automation github-project-automation bot moved this from 🆕 Triage to ✅ Done in 🧰 Octokit Active May 12, 2025
Copy link
Contributor

🎉 This PR is included in version 13.8.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

@Uzlopak Uzlopak deleted the concat-uint8array branch May 12, 2025 21:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

2 participants