Skip to content

refactor(storage): dynamically import mime and p-limit with module caching - #9230

Open
thiyaguk09 wants to merge 7 commits into
googleapis:mainfrom
thiyaguk09:feat/7350-dynamic-imports-perf
Open

refactor(storage): dynamically import mime and p-limit with module caching#9230
thiyaguk09 wants to merge 7 commits into
googleapis:mainfrom
thiyaguk09:feat/7350-dynamic-imports-perf

Conversation

@thiyaguk09

@thiyaguk09 thiyaguk09 commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes #7350

@product-auto-label product-auto-label Bot added the api: storage Issues related to the Cloud Storage API. label Sep 1, 2026

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces lazy loading for the mime and p-limit dependencies to improve startup performance. Direct imports of these modules have been replaced with helper functions getMime and getPLimit in util.ts which dynamically import and cache the modules. The Bucket, File, and TransferManager classes have been updated to asynchronously retrieve these dependencies when needed, and the corresponding test suites have been updated to mock these new helpers. I have no feedback to provide as there are no review comments.

@thiyaguk09
thiyaguk09 marked this pull request as ready for review September 7, 2026 13:00
@thiyaguk09
thiyaguk09 requested a review from a team as a code owner September 7, 2026 13:00
@github-actions
github-actions Bot requested a review from feywind September 7, 2026 13:00
} else {
this.startResumableUpload_(fileWriteStream, options);
}
writeStream.once('writing', async () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

gzip handling is split between the synchronous setup (gzip === true uses push) and the async 'writing' callback (gzip === 'auto' uses unshift).

Could we consolidate MIME detection, gzip evaluation, and transformStreams construction inside the 'writing' handler?

Additionally, if options.contentType or options.metadata.contentType is already set (and not 'auto'), we can skip calling getMime() entirely.

*
* @internal
*/
export async function getPLimit(): Promise<typeof import('p-limit')> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This method returns the callable limiter function directly (modObj.default || modObj), but its return type is declared as the module namespace Promise<typeof import('p-limit')>.

To align the type with runtime behavior and prevent typing issues with pLimit(concurrency), consider refining the return type.

And similarly ensure getMime() reflects the resolved MIME interface with .getType().

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could we add dedicated unit tests under test/util.ts for getMime() and getPLimit() covering:

  • Promise caching
  • Error recovery

},
'./util.js': {
...require('../src/util.js'),
getPLimit: async () => fakePLimit,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is this line required? src/bucket.ts imports getPLimit via ./util.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: storage Issues related to the Cloud Storage API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Explore Dynamic Imports for mime and p-limit for improve performance

2 participants