Skip to content

js-legacy: import Buffer explicitly instead of relying on the global - #153

Merged
joncinque merged 2 commits into
solana-program:mainfrom
aelmanaa:fix/js-legacy-explicit-buffer
Nov 21, 2026
Merged

joncinque merged 2 commits into
solana-program:mainfrom
aelmanaa:fix/js-legacy-explicit-buffer

Conversation

@aelmanaa

@aelmanaa aelmanaa commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #152.

  • import { Buffer } from 'buffer' in src/state.ts and src/instruction.ts, the only runtime uses of the global
  • buffer added to dependencies (@solana/web3.js already depends on it, so it dedupes for most users)

Checks: pnpm lint, pnpm format, pnpm build, and mocha state + instruction (10 passing).

Browser check (first version, same src change): an esbuild --platform=browser bundle calling createInitializeInstruction, pack/unpack and reading TOKEN_METADATA_DISCRIMINATOR, run in a context with no Buffer global:

  • main: ReferenceError: Buffer is not defined
  • this branch: OK

On Node nothing changes: buffer resolves to the built-in module.

@aelmanaa
aelmanaa force-pushed the fix/js-legacy-explicit-buffer branch from bdf46f8 to a05744b Compare November 4, 2026 18:53
@aelmanaa

Copy link
Copy Markdown
Contributor Author

@joncinque gentle ping, same as solana-program/token-2022#1612. CI is waiting for workflow approval.

@joncinque joncinque left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for your contribution! Just a few little points, then we can land this

Comment thread clients/js-legacy/test/state.test.ts Outdated
Comment on lines +63 to +66
it('Exposes the account discriminator as a Buffer', () => {
expect(Buffer.isBuffer(TOKEN_METADATA_DISCRIMINATOR)).to.equal(true);
expect([...TOKEN_METADATA_DISCRIMINATOR]).to.deep.equal([112, 132, 90, 90, 11, 88, 157, 87]);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This test is a bit useless, can you remove it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed in 194c90a.

Comment thread clients/js-legacy/.oxlintrc.json Outdated
Comment on lines +15 to +18
"no-restricted-globals": [
"error",
{ "name": "Buffer", "message": "Import it from 'buffer': browsers have no global Buffer." }
]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's revert this part and keep things simpler

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reverted in 194c90a.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@aelmanaa

Copy link
Copy Markdown
Contributor Author

@joncinque thanks for the review. Both points are addressed in 194c90a: the discriminator test is removed and the oxlint rule is reverted. The PR is now only the two explicit Buffer imports plus the buffer dependency. Could you take another look?

@joncinque joncinque left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks

@joncinque
joncinque merged commit ade19f7 into solana-program:main Nov 21, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

js-legacy: global Buffer breaks browser builds (state.ts discriminator, instruction builders)

2 participants