@crossdelta/infrastructure 0.4.1 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/generate-env.ts +1 -1
- package/dist/helpers/deploy-streams.d.ts +70 -0
- package/dist/helpers/index.d.ts +3 -0
- package/dist/helpers/otel.d.ts +42 -0
- package/dist/helpers/streams.d.ts +85 -0
- package/dist/index.cjs +22409 -528
- package/dist/index.d.ts +0 -1
- package/dist/index.js +22376 -484
- package/dist/types/index.d.ts +0 -1
- package/package.json +10 -3
- package/bin/generate-env.mjs +0 -135
- package/dist/types/service-names.d.ts +0 -9
package/bin/generate-env.ts
CHANGED
|
@@ -59,7 +59,7 @@ const extract = {
|
|
|
59
59
|
},
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
const LOCK_FILES = ['bun.lock', 'bun.lockb', 'package-lock.json', '
|
|
62
|
+
const LOCK_FILES = ['bun.lock', 'bun.lockb', 'package-lock.json', 'yarn.lock', 'pnpm-lock.yaml']
|
|
63
63
|
|
|
64
64
|
const hasLockFile = (dir: string): boolean => LOCK_FILES.some((file) => existsSync(join(dir, file)))
|
|
65
65
|
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NATS JetStream Stream Materialization
|
|
3
|
+
*
|
|
4
|
+
* Generic logic for materializing NATS JetStream streams from contract metadata.
|
|
5
|
+
* Contracts define events conceptually, this module materializes them with
|
|
6
|
+
* explicit retention policies and storage configuration.
|
|
7
|
+
*
|
|
8
|
+
* Architecture:
|
|
9
|
+
* - Libraries: Contain logic (this file)
|
|
10
|
+
* - Workspaces: Provide data (contracts, policies)
|
|
11
|
+
* - Contracts: Define conceptually (stream, subjects)
|
|
12
|
+
* - Infrastructure: Materializes (retention, storage, limits)
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Stream configuration policy
|
|
16
|
+
* Defines retention, storage, and limits per stream
|
|
17
|
+
*/
|
|
18
|
+
export interface StreamPolicy {
|
|
19
|
+
/** Stream name */
|
|
20
|
+
stream: string;
|
|
21
|
+
/** Retention policy (default: 7 days) */
|
|
22
|
+
maxAge?: number;
|
|
23
|
+
/** Storage type (default: 'file') */
|
|
24
|
+
storage?: 'file' | 'memory';
|
|
25
|
+
/** Number of replicas for HA (default: 1) */
|
|
26
|
+
replicas?: number;
|
|
27
|
+
/** Max message size (default: 1MB) */
|
|
28
|
+
maxMsgSize?: number;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Deploy NATS JetStream streams from contracts
|
|
32
|
+
*
|
|
33
|
+
* This function:
|
|
34
|
+
* 1. Collects stream definitions from contracts
|
|
35
|
+
* 2. Validates stream definitions
|
|
36
|
+
* 3. Applies stream policies (retention, storage, limits)
|
|
37
|
+
* 4. Materializes streams via ensureJetStreams()
|
|
38
|
+
*
|
|
39
|
+
* @param contractsModule - Contracts module (e.g., import * as contracts from '@my-org/contracts')
|
|
40
|
+
* @param natsUrl - NATS connection URL
|
|
41
|
+
* @param options - Optional configuration
|
|
42
|
+
* @param options.policies - Stream-specific policy overrides
|
|
43
|
+
* @param options.defaults - Default policy for all streams
|
|
44
|
+
* @returns Promise that resolves when streams are deployed
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* import { deployStreams } from '@crossdelta/infrastructure'
|
|
49
|
+
* import * as contracts from '@my-org/contracts'
|
|
50
|
+
*
|
|
51
|
+
* // Basic usage with defaults
|
|
52
|
+
* await deployStreams(contracts, 'nats://nats.my-platform.svc.cluster.local:4222')
|
|
53
|
+
*
|
|
54
|
+
* // With custom policies
|
|
55
|
+
* await deployStreams(contracts, natsUrl, {
|
|
56
|
+
* policies: {
|
|
57
|
+
* ORDERS: { maxAge: 14 * 24 * 60 * 60 * 1000, replicas: 3 },
|
|
58
|
+
* CUSTOMERS: { maxAge: 30 * 24 * 60 * 60 * 1000, replicas: 2 },
|
|
59
|
+
* },
|
|
60
|
+
* defaults: {
|
|
61
|
+
* maxAge: 7 * 24 * 60 * 60 * 1000,
|
|
62
|
+
* replicas: 1,
|
|
63
|
+
* },
|
|
64
|
+
* })
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export declare const deployStreams: (contractsModule: Record<string, unknown>, natsUrl: string, options?: {
|
|
68
|
+
policies?: Record<string, Partial<StreamPolicy>>;
|
|
69
|
+
defaults?: Partial<StreamPolicy>;
|
|
70
|
+
}) => Promise<void>;
|
package/dist/helpers/index.d.ts
CHANGED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenTelemetry Environment Variables Builder
|
|
3
|
+
*
|
|
4
|
+
* Generic helper for building OTEL environment variables for services.
|
|
5
|
+
* Works with any OTLP-compatible backend (Better Stack, Jaeger, Grafana, etc.)
|
|
6
|
+
*/
|
|
7
|
+
import type * as pulumi from '@pulumi/pulumi';
|
|
8
|
+
export interface OtelConfig {
|
|
9
|
+
/** OTLP traces endpoint URL */
|
|
10
|
+
tracesEndpoint?: pulumi.Input<string>;
|
|
11
|
+
/** OTLP metrics endpoint URL */
|
|
12
|
+
metricsEndpoint?: pulumi.Input<string>;
|
|
13
|
+
/** OTLP headers (e.g., "Authorization=Bearer token") */
|
|
14
|
+
headers?: pulumi.Input<string>;
|
|
15
|
+
/** Deployment environment (e.g., "production", "staging") */
|
|
16
|
+
environment?: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Build OpenTelemetry environment variables for a service
|
|
20
|
+
*
|
|
21
|
+
* @param serviceName - Service name (e.g., "api-gateway")
|
|
22
|
+
* @param config - OpenTelemetry configuration
|
|
23
|
+
* @returns Environment variables for the service
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* import { buildOtelEnv } from '@crossdelta/infrastructure'
|
|
28
|
+
*
|
|
29
|
+
* const env = buildOtelEnv('my-service', {
|
|
30
|
+
* tracesEndpoint: 'https://in-otel.betterstack.com',
|
|
31
|
+
* headers: 'Authorization=Bearer YOUR_TOKEN',
|
|
32
|
+
* environment: 'production',
|
|
33
|
+
* })
|
|
34
|
+
* // {
|
|
35
|
+
* // OTEL_SERVICE_NAME: 'my-service',
|
|
36
|
+
* // OTEL_RESOURCE_ATTRIBUTES: 'service.name=my-service,deployment.environment=production',
|
|
37
|
+
* // OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: 'https://in-otel.betterstack.com',
|
|
38
|
+
* // OTEL_EXPORTER_OTLP_HEADERS: 'Authorization=Bearer YOUR_TOKEN',
|
|
39
|
+
* // }
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare const buildOtelEnv: (serviceName: string, config: OtelConfig) => Record<string, pulumi.Input<string>>;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stream Collector Utility
|
|
3
|
+
*
|
|
4
|
+
* Generic logic for discovering and collecting stream definitions from event contracts.
|
|
5
|
+
* Contracts define events conceptually, this utility extracts
|
|
6
|
+
* the channel metadata for infrastructure materialization.
|
|
7
|
+
*
|
|
8
|
+
* Architecture:
|
|
9
|
+
* - Libraries: Contain logic (this file)
|
|
10
|
+
* - Workspaces: Provide data (contracts module)
|
|
11
|
+
* - Contracts: Define semantics (type, schema, channel)
|
|
12
|
+
* - Infra: Materializes streams via Pulumi
|
|
13
|
+
*
|
|
14
|
+
* @module @crossdelta/infrastructure/lib/helpers/streams
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Stream definition for infrastructure materialization
|
|
18
|
+
*/
|
|
19
|
+
export interface StreamDefinition {
|
|
20
|
+
/** Stream name (e.g., 'ORDERS') */
|
|
21
|
+
stream: string;
|
|
22
|
+
/** Unique subjects for this stream */
|
|
23
|
+
subjects: string[];
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Validation result for stream definitions
|
|
27
|
+
*/
|
|
28
|
+
export interface ValidationResult {
|
|
29
|
+
ok: boolean;
|
|
30
|
+
errors: string[];
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Collect all stream definitions from a contracts module
|
|
34
|
+
*
|
|
35
|
+
* This function:
|
|
36
|
+
* 1. Takes a contracts module (e.g., import * as contracts from '@my-org/contracts')
|
|
37
|
+
* 2. Filters contracts with channel metadata
|
|
38
|
+
* 3. Groups by stream name
|
|
39
|
+
* 4. Collects unique subjects per stream
|
|
40
|
+
*
|
|
41
|
+
* @param contractsModule - Contracts module object (e.g., import * as contracts from '@my-org/contracts')
|
|
42
|
+
* @returns Array of stream definitions with unique subjects
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* import * as contracts from '@my-org/contracts'
|
|
47
|
+
* import { collectStreamDefinitions } from '@crossdelta/infrastructure/lib/helpers/streams'
|
|
48
|
+
*
|
|
49
|
+
* const streams = collectStreamDefinitions(contracts)
|
|
50
|
+
* // [
|
|
51
|
+
* // { stream: 'ORDERS', subjects: ['orders.created', 'orders.updated'] },
|
|
52
|
+
* // { stream: 'CUSTOMERS', subjects: ['customers.updated'] }
|
|
53
|
+
* // ]
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare const collectStreamDefinitions: (contractsModule: Record<string, unknown>) => StreamDefinition[];
|
|
57
|
+
/**
|
|
58
|
+
* Get stream definition for a specific stream name
|
|
59
|
+
*
|
|
60
|
+
* @param contractsModule - Contracts module object
|
|
61
|
+
* @param streamName - Name of the stream (e.g., 'ORDERS')
|
|
62
|
+
* @returns Stream definition or undefined if not found
|
|
63
|
+
*/
|
|
64
|
+
export declare const getStreamDefinition: (contractsModule: Record<string, unknown>, streamName: string) => StreamDefinition | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* Get all stream names
|
|
67
|
+
*
|
|
68
|
+
* @param contractsModule - Contracts module object
|
|
69
|
+
* @returns Array of stream names
|
|
70
|
+
*/
|
|
71
|
+
export declare const getStreamNames: (contractsModule: Record<string, unknown>) => string[];
|
|
72
|
+
/**
|
|
73
|
+
* Validate stream definitions (pure function)
|
|
74
|
+
*
|
|
75
|
+
* @param streams - Stream definitions to validate
|
|
76
|
+
* @returns Validation result with errors
|
|
77
|
+
*/
|
|
78
|
+
export declare const validateStreamDefinitionsPure: (streams: StreamDefinition[]) => ValidationResult;
|
|
79
|
+
/**
|
|
80
|
+
* Validate stream definitions and throw on error (side effect)
|
|
81
|
+
*
|
|
82
|
+
* @param contractsModule - Contracts module object
|
|
83
|
+
* @throws Error if validation fails
|
|
84
|
+
*/
|
|
85
|
+
export declare const validateStreamDefinitions: (contractsModule: Record<string, unknown>) => void;
|