Skip to content

custom ports for services - #2222

Open
alexcos20 wants to merge 1 commit into
mainfrom
feat/service-custom-ports
Open

alexcos20 wants to merge 1 commit into
mainfrom
feat/service-custom-ports

Conversation

@alexcos20

@alexcos20 alexcos20 commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Service-on-Demand: forward user-specified host ports (ports)

Summary

Client-SDK mirror of the ocean-node feat/service-custom-ports change. ocean.js now forwards an
optional ports field on serviceStart and serviceRestart so callers can pin a specific
host port for a container port (or omit hostPort to let the node auto-allocate):

ports?: Array<{ containerPort: number; hostPort?: number }>

All validation and enforcement stay on the node (range/freeness, charge-safety, rejection); the SDK
only passes the field through. Purely additive and backward compatible — existing exposedPorts
and no-ports callers build a byte-identical request.

Semantics (match ocean-node)

  • hostPort omitted → node auto-allocates from its hostPortRange (today's behaviour).
  • hostPort set → node pins it, rejecting the command if it is out of range or not free.
  • serviceStart: ports is sent alongside the legacy exposedPorts; the node lets ports
    supersede exposedPorts when both are present.
  • serviceRestart: only ports is forwarded — the SDK deliberately does not send
    exposedPorts on restart (the node ignores it there, and this prevents a client that echoes a
    start body into a restart from changing ports by accident). A port change is independent of the
    REUSE/RESPEC rule (like metadata); omitting ports keeps the service's current ports; an
    explicit ports: [] is forwarded (replace-with-none), distinct from omitting it.

Changes by file

File Change
src/@types/Services.ts new ServicePortRequest { containerPort; hostPort? }; ports? added to the shared ServiceContainerSpec (so both ServiceStartParams and ServiceRestartParams inherit it); requestedHostPorts?: Record<string, number> added to ServiceJob for response parity. Auto-exported via @types/index.ts (export * from './Services.js').
src/services/providers/HttpProvider.ts serviceRestart destructures ports and conditionally spreads it into the body; serviceStart already forwards it via the existing ...rest spread (no change needed).
src/services/providers/P2pProvider.ts same serviceRestart change (symmetric with HTTP); serviceStart via ...rest.
test/unit/Services.test.ts new ports forwarding suite — stubs globalThis.fetch via an agent-signature token (no network/auth/encrypt) to capture the request body.
test/integration/Services.test.ts discriminating restart case: an out-of-range pinned ports host port is rejected and the service stays Running (proves the field reached the node).

Type-level doc comments on ServicePortRequest / ServiceContainerSpec.ports flow into the
TypeDoc-generated docs/ on the next build:types (there is no hand-written Service-on-Demand
usage snippet to update).

Summary by CodeRabbit

  • New Features
    • Service start and restart requests can now specify host ports for container ports, or leave ports to be allocated automatically.
    • Restart requests can update port settings while retaining existing behavior when no port overrides are provided.
  • Bug Fixes
    • If a restart is rejected because a requested host port is invalid, the service remains running with its existing container and port.

@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change adds service port request types and optional restart-port forwarding in the HTTP and P2P providers. Unit and integration tests cover request bodies and rejected host ports. CI test jobs set NODE_VERSION to pr-1638.

Changes: Service Port Requests

Layer / File(s) Summary
Port request types
src/@types/Services.ts
Adds types for pinned host ports and container port requests. The container specification documents port behavior for start and restart.
Restart port forwarding
src/services/providers/HttpProvider.ts, src/services/providers/P2pProvider.ts
Both providers include ports in restart requests when supplied and omit it otherwise.
Request and lifecycle tests
test/unit/Services.test.ts, test/integration/Services.test.ts, .github/workflows/ci.yml
Unit tests check start and restart request bodies. An integration test checks that a rejected host port leaves the service running with its prior container ID and host port. CI test jobs set NODE_VERSION to pr-1638.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Feature

Merge Risk: Minimal · up to bcec9

This adds optional custom port requests to service start and restart. Requests from callers who omit ports are unchanged. The only open item is that the new integration test relies on the node image used in CI to return a specific rejection message, which could not be confirmed here.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "custom ports for services" matches the main change. The PR adds optional port requests (containerPort and optional hostPort) to service start and restart, including the ServicePortRequest t…
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.
Full details: Title check

Explanation

The title "custom ports for services" matches the main change. The PR adds optional port requests (containerPort and optional hostPort) to service start and restart, including the ServicePortRequest type, the ports field on ServiceContainerSpec, and forwarding in the HTTP and P2P providers. The title is short and specific, though it does not name the restart behavior or the integration test.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
Fix failing CI checks

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 75bfc287-ffab-4334-8e42-4069c5a91ed6
Commits

Reviewing files that changed from the base of the PR and between 059b7f1 and bcec9a5.

Files selected for processing: 6

  • .github/workflows/ci.yml
  • src/@types/Services.ts
  • src/services/providers/HttpProvider.ts
  • src/services/providers/P2pProvider.ts
  • test/integration/Services.test.ts
  • test/unit/Services.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

@alexcos20

Copy link
Copy Markdown
Member Author

/run-security-scan

@alexcos20 alexcos20 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

AI automated code review (Gemini 3).

Overall risk: low

Summary:
This PR successfully introduces support for custom port mapping (ports) for service deployment (Start and Restart). The interface additions are well-documented, the provider methods appropriately spread the new parameters, and the testing suite is remarkably thorough. The only blocker for merge is a hardcoded node PR version in the CI workflow, which is typical for cross-repo feature testing but must be removed prior to merging.

Comments:
• [ERROR][other] Hardcoding NODE_VERSION: pr-1638 ties the pipeline to an external PR branch. While this is great for cross-repo testing during development, please ensure this is reverted to main or updated to a stable release tag before merging. Otherwise, main branch CI will depend on a volatile/temporary PR branch that could be deleted.

-        env:
-          NODE_VERSION: pr-1638

• [ERROR][other] Same issue as above: remember to revert the NODE_VERSION override for this job as well before merging.
• [INFO][style] Excellent inline documentation for the new ports property. Clearly explaining its precedence over the legacy exposedPorts and its interaction with RESPEC prevents developer confusion.
• [INFO][style] The conditional spread ...(ports !== undefined ? { ports } : {}) properly preserves the difference between undefined (omit/keep current) and [] (explicit empty/clear). Good attention to detail to ensure this matches the behavior established in your unit tests.
• [INFO][style] Great approach to testing expected rejections. By specifically matching the error string against /host port|range/i and subsequently asserting that the container state was unharmed, you've created a highly robust integration test.

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