@di-framework/cloudfoundry

中文文档
6.0.30发布于 2026/11/07(MIT OR Apache-2.0)TypeScriptES Module零依赖构建来源已验证近 30 天下载 3617

Cloud Foundry connector for VCAP_SERVICES discovery, application metadata, and automated DI binding.

原始使用文档
文档目录7 节
  1. Features
  2. Installation
  3. Quick Start
  4. 1. Using @EnableCloudFoundryConnectors and @CloudFoundrySer…
  5. 2. Programmatic Discovery via CloudFoundryEnvironment
  6. 3. Custom Service Creators (SPI)
  7. License

@di-framework/cloudfoundry

Cloud Foundry connector for @di-framework — automatic VCAP_SERVICES and VCAP_APPLICATION discovery, strongly typed ServiceInfo models, SPI creator extensible registry, and automated DI bindings.

Inspired by Spring Cloud Connectors (spring-cloud-cloudfoundry-connector), this package streamlines running @di-framework applications on Cloud Foundry and Tanzu Application Service.

Features

  • Automatic Environment Detection: Fast detection via CloudFoundryDetector and isCloudFoundry().
  • Application Info Parsing: Strongly typed CloudFoundryApplicationInfo containing application names, URIs, instance index, space, limits, etc.
  • Service Info Creators (SPI): Extensible discovery and normalized models for:
    • Relational Databases: PostgreSQL, MySQL, MariaDB, SQLite (RelationalServiceInfo)
    • Redis / Cache: Redis standalone, TLS, cluster (RedisServiceInfo)
    • AMQP / Message Brokers: RabbitMQ, CloudAMQP (AmqpServiceInfo)
    • Blob Storage / S3: MinIO, AWS S3, Object Store (BlobStorageServiceInfo)
    • User-Provided Services: cf cups custom services (UserProvidedServiceInfo)
  • DI & Decorator Integration:
    • @CloudFoundryService('service-name') for parameter and property injection
    • @VcapApplication() for application metadata injection
    • @EnableCloudFoundryConnectors() for container-wide auto-wiring
  • Local Fallback: Automatically falls back to standard local environment variables (DATABASE_URL, REDIS_URL, AMQP_URL, S3_BUCKET) when running outside Cloud Foundry.

Installation

bun add @di-framework/cloudfoundry

Quick Start

1. Using @EnableCloudFoundryConnectors and @CloudFoundryService

import { Container } from '@di-framework/core/decorators';
import {
  CloudFoundryService,
  EnableCloudFoundryConnectors,
  RelationalServiceInfo,
  RedisServiceInfo,
  VcapApplication,
  CloudFoundryApplicationInfo,
} from '@di-framework/cloudfoundry';

@EnableCloudFoundryConnectors()
@Container()
export class ApplicationService {
  @VcapApplication()
  private appInfo!: CloudFoundryApplicationInfo;

  @CloudFoundryService('my-postgres-db')
  private dbService!: RelationalServiceInfo;

  @CloudFoundryService('my-redis-cache')
  private redisService!: RedisServiceInfo;

  start() {
    console.log(`Running app ${this.appInfo?.applicationName}`);
    console.log(`Database URI: ${this.dbService.uri}`);
    console.log(`Redis host: ${this.redisService.host}:${this.redisService.port}`);
  }
}

2. Programmatic Discovery via CloudFoundryEnvironment

import { CloudFoundryEnvironment } from '@di-framework/cloudfoundry';

const cfEnv = new CloudFoundryEnvironment();

if (cfEnv.isCloudFoundry()) {
  const appInfo = cfEnv.getApplicationInfo();
  console.log(`Space: ${appInfo?.spaceName}, Instance: ${appInfo?.instanceIndex}`);

  // Retrieve primary relational database
  const db = cfEnv.getRelationalServiceInfo();
  if (db) {
    console.log(`Connected to ${db.dialect} at ${db.host}:${db.port}/${db.database}`);
  }

  // Retrieve Redis service
  const redis = cfEnv.getRedisServiceInfo();
  if (redis) {
    console.log(`Redis URI: ${redis.uri}`);
  }
}

3. Custom Service Creators (SPI)

You can register custom service info creators with ServiceInfoCreatorRegistry:

import {
  CloudFoundryServiceInfoCreator,
  getDefaultRegistry,
  RawVcapServiceData,
  CloudFoundryServiceInfo,
} from '@di-framework/cloudfoundry';

export interface ElasticsearchServiceInfo extends CloudFoundryServiceInfo {
  readonly uri: string;
}

export class ElasticsearchServiceInfoCreator implements CloudFoundryServiceInfoCreator<ElasticsearchServiceInfo> {
  accept(data: RawVcapServiceData): boolean {
    return data.label.includes('elasticsearch') || (data.tags || []).includes('elasticsearch');
  }

  createServiceInfo(data: RawVcapServiceData): ElasticsearchServiceInfo {
    const creds = data.credentials || {};
    return {
      id: data.name,
      name: data.name,
      label: data.label,
      tags: [...(data.tags || [])],
      credentials: creds,
      uri: String(creds.uri ?? 'http://localhost:9200'),
    };
  }
}

getDefaultRegistry().registerCreator(new ElasticsearchServiceInfoCreator());

License

Licensed under either MIT or Apache-2.0, at your option.

版本与发布记录

@di-framework/cloudfoundry 共发布 43 个版本,首次发布于 2026/09/06;过去 12 个月发布了 43 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
6.0.30latest2026/11/075 天前
6.0.292026/11/066 天前
6.0.282026/11/039 天前
6.0.272026/11/0111 天前
6.0.262026/10/3012 天前
6.0.252026/10/3013 天前

关于 @di-framework/cloudfoundry 的常见问题

@di-framework/cloudfoundry 是什么?

Cloud Foundry connector for VCAP_SERVICES discovery, application metadata, and automated DI binding.

如何安装 @di-framework/cloudfoundry?

在项目目录运行 npm install @di-framework/cloudfoundry,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:typescript(^5 || ^6 || ^7)、@di-framework/core(^6)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@di-framework/cloudfoundry 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@di-framework/cloudfoundry 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@di-framework/cloudfoundry 可以商用吗?

@di-framework/cloudfoundry 使用 (MIT OR Apache-2.0) 许可。可在 MIT、Apache-2.0 中任选其一遵守。选择 MIT 时,宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@di-framework/cloudfoundry 的下载量有多少?

根据 npm 官方下载统计,@di-framework/cloudfoundry 在 2026/10/12 – 2026/11/10 这 30 天内被下载 3,617 次,日均约 121 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@di-framework/cloudfoundry 还在维护吗?

最新版本 6.0.30 发布于 2026/11/07(5 天前);至今共发布 43 个版本,其中过去 12 个月发布了 43 个;首次发布于 2026/09/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。