Skip to content

chore: drop backoff dep and convert remaining require() calls to ESM#8195

Open
jherr wants to merge 1 commit intomainfrom
chore/backoff-and-require-cleanup
Open

chore: drop backoff dep and convert remaining require() calls to ESM#8195
jherr wants to merge 1 commit intomainfrom
chore/backoff-and-require-cleanup

Conversation

@jherr
Copy link
Copy Markdown
Contributor

@jherr jherr commented Apr 22, 2026

Summary

Continues the Milestone 1 dependency-pruning work from #8189 by finishing items B and C from the follow-up list:

  • Drop backoff / @types/backoff. Its only runtime use was in src/utils/deploy/upload-files.ts; replaced with a small inline Fibonacci retry helper that preserves the existing delay sequence (5s, 5s, 10s, 15s, 25s, … capped at UPLOAD_MAX_DELAY), the 0.5 jitter factor, and the failAfter(maxRetry) "6 attempts when maxRetry=5" semantics. tests/unit/utils/gh-auth.test.ts migrated off backoff to a plain polling loop.
  • Convert the 3 remaining require() calls to ESM.
    • src/commands/functions/functions-create.ts (x2): require(packageJson)JSON.parse(fs.readFileSync(...)) via a small readJsonFile helper.
    • src/commands/database/legacy/utils.ts: createRequire(join(dir, 'package.json'))('./package.json')JSON.parse(fs.readFileSync(join(dir, 'package.json'), 'utf8')).
    • src/commands/functions/functions-invoke.ts: processPayloadFromFlag becomes async; .json payloads use readFile + JSON.parse, other files go through dynamic import() (via pathToFileURL) with imported.default ?? imported to preserve the CJS interop the old require() provided.
  • New unit tests to lock in the changed behavior:
    • tests/unit/commands/database/legacy/utils.test.ts (8 cases) — exercises getPackageJSON happy path plus each of the shape-validation errors.
    • tests/unit/commands/functions/functions-invoke.test.ts (7 cases) — exercises processPayloadFromFlag with inline JSON strings, .json / .mjs / .cjs file payloads, missing paths, and load-time errors (processPayloadFromFlag is now exported for testability).

Test plan

  • npm run typecheck
  • npm run lint
  • npm exec vitest -- run tests/unit/utils/deploy/upload-files.test.ts (2/2 — retry-count-on-retries and no-retry-on-400 still pass against the inline Fibonacci implementation)
  • npm exec vitest -- run tests/unit/utils/gh-auth.test.ts (1/1)
  • npm exec vitest -- run tests/unit/commands/database/legacy/utils.test.ts (8/8, new)
  • npm exec vitest -- run tests/unit/commands/functions/functions-invoke.test.ts (7/7, new)
  • npm run test:unit — 337 passed. The 5 failures are pre-existing on main (copy-template-dir.test.ts × 4 hit a Node 25 fs.rmdirSync deprecation; generate-autocompletion.test.ts × 1 has a stale snapshot).
  • npm exec vitest -- run tests/integration/commands/functions-invoke/functions-invoke.test.ts (5/5 — validates the now-async processPayloadFromFlag path end-to-end)

Made with Cursor

Continues the Milestone 1 dependency-pruning work from #8189.

- Replace the single `backoff` call site in `src/utils/deploy/upload-files.ts`
  with a small inline Fibonacci retry that preserves the existing delay
  sequence (5s, 5s, 10s, 15s, 25s, … capped at UPLOAD_MAX_DELAY) and the
  `failAfter(maxRetry)` semantics. Also migrate `tests/unit/utils/gh-auth.test.ts`
  off `backoff` to a plain polling loop.
- Convert the three remaining `require(path)` call sites to native ESM
  equivalents: `functions-create.ts` and `database/legacy/utils.ts` now use
  `JSON.parse(readFileSync(...))` for package.json lookups; `functions-invoke.ts`
  becomes async and supports both `.json` (readFile) and `.js/.mjs/.cjs`
  (dynamic `import()`) payloads.
- Remove `backoff` and `@types/backoff` from package.json.
- Add unit tests for `getPackageJSON` and export + test
  `processPayloadFromFlag` to lock in the JSON + dynamic-import behavior.

Made-with: Cursor
@jherr jherr requested a review from a team as a code owner April 22, 2026 23:28
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 22, 2026

📝 Walkthrough

Summary by CodeRabbit

Release Notes

  • Chores
    • Removed unused runtime dependency to reduce package footprint
    • Refactored internal file loading and processing mechanisms
    • Improved upload retry logic with deterministic timing controls
    • Enhanced test coverage for improved reliability

Walkthrough

This PR removes the backoff library dependency and refactors JSON file loading and retry mechanisms across the codebase. It replaces CommonJS createRequire usage for loading package.json with fs.readFileSync + JSON.parse in multiple commands, reimplements retry logic in file uploads with manual delay scheduling and jitter instead of Fibonacci backoff, converts processPayloadFromFlag to an async function that supports dynamic imports for JavaScript modules, and updates tests to use deterministic polling instead of backoff-based callback orchestration.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the two main changes: removing the backoff dependency and converting require() calls to ESM patterns.
Description check ✅ Passed The description is comprehensive and directly related to the changeset, detailing the work completed, implementation approach, and test coverage.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch chore/backoff-and-require-cleanup

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown

📊 Benchmark results

Comparing with 69f05af

  • Dependency count: 1,059 ⬇️ 0.19% decrease vs. 69f05af
  • Package size: 354 MB ⬇️ 0.04% decrease vs. 69f05af
  • Number of ts-expect-error directives: 349 ⬇️ 2.01% decrease vs. 69f05af

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/utils/deploy/upload-files.ts (1)

80-127: Consider adding generic typing to retryUpload to preserve response types.

The retry attempt semantics are correct: with maxRetry = n, the function allows up to n + 1 total attempts (initial attempt plus n retries), matching the previous failAfter(maxRetry) behavior.

However, retryUpload currently returns Promise<unknown>, causing the response variable (lines 30 and 42) to lose type information from api.uploadDeployFile() and api.uploadDeployFunction(). Add a generic parameter to preserve the response type:

Suggested typing refinement
-const retryUpload = (uploadFn: (retryCount: number) => Promise<unknown>, maxRetry: number) =>
-  new Promise((resolve, reject) => {
+const retryUpload = <T>(uploadFn: (retryCount: number) => Promise<T>, maxRetry: number): Promise<T> =>
+  new Promise<T>((resolve, reject) => {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/utils/deploy/upload-files.ts` around lines 80 - 127, The retryUpload
helper should be made generic so it preserves the resolved type from callers:
change retryUpload to accept a type parameter (e.g. <T>) and type uploadFn as
(retryCount: number) => Promise<T>, and have retryUpload return Promise<T>; keep
internal variables (lastError, retryCount, scheduleNextAttempt, tryUpload) the
same but ensure resolve is typed to accept T and reject remains unknown/any as
before; update any call sites of retryUpload (e.g., uses with
api.uploadDeployFile and api.uploadDeployFunction) to infer or pass the concrete
type if needed so the response variables keep their original types.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/utils/deploy/upload-files.ts`:
- Around line 80-127: The retryUpload helper should be made generic so it
preserves the resolved type from callers: change retryUpload to accept a type
parameter (e.g. <T>) and type uploadFn as (retryCount: number) => Promise<T>,
and have retryUpload return Promise<T>; keep internal variables (lastError,
retryCount, scheduleNextAttempt, tryUpload) the same but ensure resolve is typed
to accept T and reject remains unknown/any as before; update any call sites of
retryUpload (e.g., uses with api.uploadDeployFile and api.uploadDeployFunction)
to infer or pass the concrete type if needed so the response variables keep
their original types.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 63df52d9-8379-41d4-a471-a979f681d265

📥 Commits

Reviewing files that changed from the base of the PR and between 69f05af and a0d6ee5.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (8)
  • package.json
  • src/commands/database/legacy/utils.ts
  • src/commands/functions/functions-create.ts
  • src/commands/functions/functions-invoke.ts
  • src/utils/deploy/upload-files.ts
  • tests/unit/commands/database/legacy/utils.test.ts
  • tests/unit/commands/functions/functions-invoke.test.ts
  • tests/unit/utils/gh-auth.test.ts
💤 Files with no reviewable changes (1)
  • package.json

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.

1 participant