Skip to content

Remove dependency on Buffer for non-Node.js environments#34

Merged
good-lly merged 2 commits intogood-lly:devfrom
Zegnat:use-fetch-body-type
Aug 24, 2025
Merged

Remove dependency on Buffer for non-Node.js environments#34
good-lly merged 2 commits intogood-lly:devfrom
Zegnat:use-fetch-body-type

Conversation

@Zegnat
Copy link
Contributor

@Zegnat Zegnat commented Aug 23, 2025

This is a continuation of #33. Kinda.

Running the following minimal code with Wrangler used to fail with a ReferenceError:

import { S3mini } from '../../dist/s3mini.min.js';

const client = new S3mini({
  accessKeyId: 'AAA',
  secretAccessKey: 'BBBBBBBB',
  endpoint: 'http://localhost:9002/test',
  region: 'auto',
});

client
  .uploadPart('large-file.zip', 'demo-upload-id', '... some data to add ...', 1)
  .then(console.log, console.error);
ReferenceError: Buffer is not defined

This is because Buffer is not part of base ECMAScript but an extension by Node.js. Now that s3mini only relies on Web Crypto APIs, this tries to remove another Node.js dependency and make it possible to use it in more environments.

  1. The internal methods of the S3mini class use BodyInit for its request body value, this should always be compatible with fetch().
  2. The externally offered methods continue to accept only strings and Buffers. But validation of this checks to make sure that Buffer is a thing, thus skipping these values in other environments.

It might also be possible to solve this by bringing BodyInit all the way out to the methods. But for this first PR I have chosen to keep the user inputs the same.

After this change, Wrangler returns:

Error: Disallowed operation called within global scope. Asynchronous I/O (ex: fetch() or connect()), setting a timeout, and generating random values are not allowed within global scope.

Which suggests it can run all the library code but does not execute the fetch() because I am running it naively.

@good-lly good-lly merged commit 613ad35 into good-lly:dev Aug 24, 2025
3 of 4 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.

2 participants