From 15f45cacfccf22415bd401fab7e8c3816ed5b457 Mon Sep 17 00:00:00 2001 From: sam Date: Wed, 6 Sep 2023 22:49:11 -0700 Subject: [PATCH 1/2] feat: build aws-cdk as ESM and CJS and support ESM modules in infer --- apps/test-app/tsconfig.json | 2 +- packages/@eventual/aws-cdk/package.json | 14 ++++++-- .../@eventual/aws-cdk/src/bucket-service.ts | 14 +++++--- packages/@eventual/aws-cdk/src/build-cli.ts | 2 +- .../@eventual/aws-cdk/src/command-service.ts | 14 ++++---- .../aws-cdk/src/constructs/spec-http-api.ts | 4 +-- .../@eventual/aws-cdk/src/debug-dashboard.ts | 2 +- .../@eventual/aws-cdk/src/entity-service.ts | 8 ++--- .../@eventual/aws-cdk/src/event-service.ts | 4 +-- packages/@eventual/aws-cdk/src/package.json | 3 ++ .../@eventual/aws-cdk/src/proxy-construct.ts | 2 +- packages/@eventual/aws-cdk/src/resource.ts | 4 +-- .../aws-cdk/src/scheduler-service.ts | 12 +++---- .../aws-cdk/src/search/base-search-service.ts | 8 ++--- .../aws-cdk/src/search/collection.ts | 4 +-- .../aws-cdk/src/search/data-access-policy.ts | 2 +- .../aws-cdk/src/search/search-index.ts | 2 +- .../aws-cdk/src/search/search-service.ts | 12 +++---- .../src/search/serverful-search-service.ts | 8 ++--- .../src/search/serverless-search-service.ts | 10 +++--- .../@eventual/aws-cdk/src/service-common.ts | 14 ++++---- .../aws-cdk/src/service-dashboard.ts | 10 +++--- .../@eventual/aws-cdk/src/service-function.ts | 4 +-- packages/@eventual/aws-cdk/src/service.ts | 34 +++++++++++-------- .../@eventual/aws-cdk/src/subscriptions.ts | 16 ++++----- .../@eventual/aws-cdk/src/task-service.ts | 22 ++++++------ .../@eventual/aws-cdk/src/workflow-service.ts | 20 +++++------ packages/@eventual/aws-cdk/tsconfig.cjs.json | 18 ++++++++++ packages/@eventual/aws-cdk/tsconfig.json | 14 ++++---- .../@eventual/compiler/src/eventual-infer.ts | 2 ++ 30 files changed, 164 insertions(+), 121 deletions(-) create mode 100644 packages/@eventual/aws-cdk/src/package.json create mode 100644 packages/@eventual/aws-cdk/tsconfig.cjs.json diff --git a/apps/test-app/tsconfig.json b/apps/test-app/tsconfig.json index 4be79b5a5..5d4440588 100644 --- a/apps/test-app/tsconfig.json +++ b/apps/test-app/tsconfig.json @@ -7,7 +7,7 @@ "rootDir": "src" }, "references": [ - { "path": "../../packages/@eventual/aws-cdk" }, + { "path": "../../packages/@eventual/aws-cdk/tsconfig.cjs.json" }, { "path": "../test-app-runtime" } ] } diff --git a/packages/@eventual/aws-cdk/package.json b/packages/@eventual/aws-cdk/package.json index 43c659ebd..1c3036a85 100644 --- a/packages/@eventual/aws-cdk/package.json +++ b/packages/@eventual/aws-cdk/package.json @@ -1,11 +1,21 @@ { "name": "@eventual/aws-cdk", "version": "0.50.0", - "main": "lib/index.js", - "types:": "lib/index.d.ts", + "main": "./lib/cjs/index.js", + "module": "./lib/esm/index.js", + "types:": "lib/esm/index.d.ts", + "files": [ + "lib" + ], "scripts": { "test": "jest --passWithNoTests" }, + "exports": { + ".": { + "import": "./lib/esm/index.js", + "require": "./lib/cjs/index.js" + } + }, "dependencies": { "@aws-sdk/credential-provider-node": "^3.341.0", "@eventual/aws-runtime": "workspace:^", diff --git a/packages/@eventual/aws-cdk/src/bucket-service.ts b/packages/@eventual/aws-cdk/src/bucket-service.ts index 8bd244158..db6ee6074 100644 --- a/packages/@eventual/aws-cdk/src/bucket-service.ts +++ b/packages/@eventual/aws-cdk/src/bucket-service.ts @@ -15,14 +15,18 @@ import { S3EventSource } from "aws-cdk-lib/aws-lambda-event-sources"; import * as s3 from "aws-cdk-lib/aws-s3"; import { Duration, RemovalPolicy, Stack } from "aws-cdk-lib/core"; import { Construct } from "constructs"; -import type { CorsOptions } from "./command-service"; +import type { CorsOptions } from "./command-service.js"; import { configureWorkerCalls, WorkerServiceConstructProps, -} from "./service-common"; -import { ServiceFunction } from "./service-function"; -import { formatBucketArn, serviceBucketArn, ServiceEntityProps } from "./utils"; -import { EventualResource } from "./resource"; +} from "./service-common.js"; +import { ServiceFunction } from "./service-function.js"; +import { + formatBucketArn, + serviceBucketArn, + ServiceEntityProps, +} from "./utils.js"; +import { EventualResource } from "./resource.js"; export type BucketOverrides = Partial< ServiceEntityProps< diff --git a/packages/@eventual/aws-cdk/src/build-cli.ts b/packages/@eventual/aws-cdk/src/build-cli.ts index 451f40f49..c3931e2c2 100644 --- a/packages/@eventual/aws-cdk/src/build-cli.ts +++ b/packages/@eventual/aws-cdk/src/build-cli.ts @@ -1,4 +1,4 @@ -import { buildService, BuildAWSRuntimeProps } from "./build"; +import { buildService, BuildAWSRuntimeProps } from "./build.js"; export async function main() { try { diff --git a/packages/@eventual/aws-cdk/src/command-service.ts b/packages/@eventual/aws-cdk/src/command-service.ts index 4880b974e..9b49ff42a 100644 --- a/packages/@eventual/aws-cdk/src/command-service.ts +++ b/packages/@eventual/aws-cdk/src/command-service.ts @@ -27,19 +27,19 @@ import { Arn, Duration, Lazy, Stack } from "aws-cdk-lib/core"; import { Construct } from "constructs"; import type openapi from "openapi3-ts"; import { ApiDefinition } from "./constructs/http-api-definition.js"; -import { SpecHttpApi, SpecHttpApiProps } from "./constructs/spec-http-api"; -import type { EventService } from "./event-service"; -import { grant } from "./grant"; +import { SpecHttpApi, SpecHttpApiProps } from "./constructs/spec-http-api.js"; +import type { EventService } from "./event-service.js"; +import { grant } from "./grant.js"; import { EventualResource } from "./resource.js"; -import { ServiceLocal } from "./service"; +import { ServiceLocal } from "./service.js"; import { WorkerServiceConstructProps, configureWorkerCalls, } from "./service-common.js"; import { ServiceFunction } from "./service-function.js"; -import type { TaskService } from "./task-service"; -import { ServiceEntityProps, serviceFunctionArn } from "./utils"; -import type { WorkflowService } from "./workflow-service"; +import type { TaskService } from "./task-service.js"; +import { ServiceEntityProps, serviceFunctionArn } from "./utils.js"; +import type { WorkflowService } from "./workflow-service.js"; export type ApiOverrides = Omit; diff --git a/packages/@eventual/aws-cdk/src/constructs/spec-http-api.ts b/packages/@eventual/aws-cdk/src/constructs/spec-http-api.ts index 597d3b2e4..94c9daafd 100644 --- a/packages/@eventual/aws-cdk/src/constructs/spec-http-api.ts +++ b/packages/@eventual/aws-cdk/src/constructs/spec-http-api.ts @@ -6,11 +6,11 @@ import { VpcLink, VpcLinkProps, } from "@aws-cdk/aws-apigatewayv2-alpha"; -import { ApiBase } from "@aws-cdk/aws-apigatewayv2-alpha/lib/common/base"; +import { ApiBase } from "@aws-cdk/aws-apigatewayv2-alpha/lib/common/base.js"; import { CfnApi } from "aws-cdk-lib/aws-apigatewayv2"; import { Metric, MetricOptions } from "aws-cdk-lib/aws-cloudwatch"; import { Construct } from "constructs"; -import { ApiDefinition } from "./http-api-definition"; +import { ApiDefinition } from "./http-api-definition.js"; /** * Taken from (and modified) closed cdk PR: diff --git a/packages/@eventual/aws-cdk/src/debug-dashboard.ts b/packages/@eventual/aws-cdk/src/debug-dashboard.ts index 5387a3a00..16bb6ee89 100644 --- a/packages/@eventual/aws-cdk/src/debug-dashboard.ts +++ b/packages/@eventual/aws-cdk/src/debug-dashboard.ts @@ -5,7 +5,7 @@ import { } from "@eventual/core-runtime"; import { Dashboard, LogQueryWidget } from "aws-cdk-lib/aws-cloudwatch"; import { Construct } from "constructs"; -import { Service } from "./service"; +import { Service } from "./service.js"; import { Stack } from "aws-cdk-lib/core"; export interface DebugDashboardProps { diff --git a/packages/@eventual/aws-cdk/src/entity-service.ts b/packages/@eventual/aws-cdk/src/entity-service.ts index 63bed1e03..af053534f 100644 --- a/packages/@eventual/aws-cdk/src/entity-service.ts +++ b/packages/@eventual/aws-cdk/src/entity-service.ts @@ -34,13 +34,13 @@ import { DynamoEventSource } from "aws-cdk-lib/aws-lambda-event-sources"; import { Duration, RemovalPolicy, Stack } from "aws-cdk-lib/core"; import { Construct } from "constructs"; import { EventService } from "./event-service.js"; -import { LazyInterface } from "./proxy-construct"; +import { LazyInterface } from "./proxy-construct.js"; import { configureWorkerCalls, WorkerServiceConstructProps, -} from "./service-common"; -import { ServiceFunction } from "./service-function"; -import { ServiceEntityProps, serviceTableArn } from "./utils"; +} from "./service-common.js"; +import { ServiceFunction } from "./service-function.js"; +import { ServiceEntityProps, serviceTableArn } from "./utils.js"; import { WorkflowService } from "./workflow-service.js"; import { EventualResource } from "./resource.js"; diff --git a/packages/@eventual/aws-cdk/src/event-service.ts b/packages/@eventual/aws-cdk/src/event-service.ts index f7b2874f1..9a5ead460 100644 --- a/packages/@eventual/aws-cdk/src/event-service.ts +++ b/packages/@eventual/aws-cdk/src/event-service.ts @@ -7,8 +7,8 @@ import { Function } from "aws-cdk-lib/aws-lambda"; import { Lazy, Resource, Stack } from "aws-cdk-lib/core"; import { Construct } from "constructs"; import type { OpenAPIObject, SchemaObject } from "openapi3-ts"; -import { grant } from "./grant"; -import { ServiceConstructProps } from "./service-common"; +import { grant } from "./grant.js"; +import { ServiceConstructProps } from "./service-common.js"; export type EventsProps = ServiceConstructProps; diff --git a/packages/@eventual/aws-cdk/src/package.json b/packages/@eventual/aws-cdk/src/package.json new file mode 100644 index 000000000..3dbc1ca59 --- /dev/null +++ b/packages/@eventual/aws-cdk/src/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/packages/@eventual/aws-cdk/src/proxy-construct.ts b/packages/@eventual/aws-cdk/src/proxy-construct.ts index 017f78bec..82789f947 100644 --- a/packages/@eventual/aws-cdk/src/proxy-construct.ts +++ b/packages/@eventual/aws-cdk/src/proxy-construct.ts @@ -1,4 +1,4 @@ -import { KeysOfType } from "./utils"; +import { KeysOfType } from "./utils.js"; /** * A function which allows a one way interface to be lazily applied. diff --git a/packages/@eventual/aws-cdk/src/resource.ts b/packages/@eventual/aws-cdk/src/resource.ts index 30a329cbd..fd9fa7925 100644 --- a/packages/@eventual/aws-cdk/src/resource.ts +++ b/packages/@eventual/aws-cdk/src/resource.ts @@ -1,7 +1,7 @@ import { IGrantable, IPrincipal } from "aws-cdk-lib/aws-iam"; import { Function } from "aws-cdk-lib/aws-lambda"; -import { DeepCompositePrincipal } from "./deep-composite-principal"; -import { ServiceLocal } from "./service"; +import { DeepCompositePrincipal } from "./deep-composite-principal.js"; +import { ServiceLocal } from "./service.js"; export class EventualResource implements IGrantable { public grantPrincipal: IPrincipal; diff --git a/packages/@eventual/aws-cdk/src/scheduler-service.ts b/packages/@eventual/aws-cdk/src/scheduler-service.ts index c4868f1e4..7e1af5f36 100644 --- a/packages/@eventual/aws-cdk/src/scheduler-service.ts +++ b/packages/@eventual/aws-cdk/src/scheduler-service.ts @@ -11,13 +11,13 @@ import { Function } from "aws-cdk-lib/aws-lambda"; import { SqsEventSource } from "aws-cdk-lib/aws-lambda-event-sources"; import { IQueue, Queue } from "aws-cdk-lib/aws-sqs"; import { Construct } from "constructs"; -import { grant } from "./grant"; -import { LazyInterface } from "./proxy-construct"; -import { ServiceFunction } from "./service-function"; +import { grant } from "./grant.js"; +import { LazyInterface } from "./proxy-construct.js"; +import { ServiceFunction } from "./service-function.js"; import type { TaskService } from "./task-service.js"; -import { serviceFunctionArn } from "./utils"; -import { WorkflowService } from "./workflow-service"; -import { ServiceConstructProps } from "./service-common"; +import { serviceFunctionArn } from "./utils.js"; +import { WorkflowService } from "./workflow-service.js"; +import { ServiceConstructProps } from "./service-common.js"; export interface SchedulerProps extends ServiceConstructProps { /** diff --git a/packages/@eventual/aws-cdk/src/search/base-search-service.ts b/packages/@eventual/aws-cdk/src/search/base-search-service.ts index ce4291f09..ce46e55cf 100644 --- a/packages/@eventual/aws-cdk/src/search/base-search-service.ts +++ b/packages/@eventual/aws-cdk/src/search/base-search-service.ts @@ -4,14 +4,14 @@ import type { Function } from "aws-cdk-lib/aws-lambda"; import * as aws_lambda from "aws-cdk-lib/aws-lambda"; import { Duration, Lazy } from "aws-cdk-lib/core"; import { Construct } from "constructs"; -import type { ServiceConstructProps } from "../service-common"; -import type { ServiceEntityProps } from "../utils"; -import { SearchIndex } from "./search-index"; +import type { ServiceConstructProps } from "../service-common.js"; +import type { ServiceEntityProps } from "../utils.js"; +import { SearchIndex } from "./search-index.js"; import type { SearchPrincipal, SearchService, ServiceIndices, -} from "./search-service"; +} from "./search-service.js"; export type SearchIndexOverrides = ServiceEntityProps< Service, diff --git a/packages/@eventual/aws-cdk/src/search/collection.ts b/packages/@eventual/aws-cdk/src/search/collection.ts index c15b95218..ddfe5a1f8 100644 --- a/packages/@eventual/aws-cdk/src/search/collection.ts +++ b/packages/@eventual/aws-cdk/src/search/collection.ts @@ -2,8 +2,8 @@ import * as aws_kms from "aws-cdk-lib/aws-kms"; import * as aws_opensearchserverless from "aws-cdk-lib/aws-opensearchserverless"; import { RemovalPolicy, Resource } from "aws-cdk-lib/core"; import { Construct } from "constructs"; -import { Access, DataAccessPolicy } from "./data-access-policy"; -import { SearchPrincipal } from "./search-service"; +import { Access, DataAccessPolicy } from "./data-access-policy.js"; +import { SearchPrincipal } from "./search-service.js"; export interface ICollection { readonly collectionName: string; diff --git a/packages/@eventual/aws-cdk/src/search/data-access-policy.ts b/packages/@eventual/aws-cdk/src/search/data-access-policy.ts index b50dbc2d8..cbd44d0fc 100644 --- a/packages/@eventual/aws-cdk/src/search/data-access-policy.ts +++ b/packages/@eventual/aws-cdk/src/search/data-access-policy.ts @@ -1,7 +1,7 @@ import aws_opensearchserverless from "aws-cdk-lib/aws-opensearchserverless"; import { Lazy, Resource } from "aws-cdk-lib/core"; import { Construct } from "constructs"; -import { Collection } from "./collection"; +import { Collection } from "./collection.js"; export enum Access { /** diff --git a/packages/@eventual/aws-cdk/src/search/search-index.ts b/packages/@eventual/aws-cdk/src/search/search-index.ts index ff5bf8680..adcc3dbe5 100644 --- a/packages/@eventual/aws-cdk/src/search/search-index.ts +++ b/packages/@eventual/aws-cdk/src/search/search-index.ts @@ -2,7 +2,7 @@ import { IndexSpec } from "@eventual/core/internal"; import type { opensearchtypes } from "@opensearch-project/opensearch"; import { CustomResource, Resource } from "aws-cdk-lib/core"; import { Construct } from "constructs"; -import type { SearchPrincipal, SearchService } from "./search-service"; +import type { SearchPrincipal, SearchService } from "./search-service.js"; /** * Attributes exposed by the {@link SearchIndex} Resource. diff --git a/packages/@eventual/aws-cdk/src/search/search-service.ts b/packages/@eventual/aws-cdk/src/search/search-service.ts index 556461165..62f1f298f 100644 --- a/packages/@eventual/aws-cdk/src/search/search-service.ts +++ b/packages/@eventual/aws-cdk/src/search/search-service.ts @@ -1,12 +1,12 @@ import type aws_iam from "aws-cdk-lib/aws-iam"; import type aws_lambda from "aws-cdk-lib/aws-lambda"; import type { Function } from "aws-cdk-lib/aws-lambda"; -import type { ServiceConstructProps } from "../service-common"; -import type { ServiceEntityProps } from "../utils"; -import type { AccessOptions } from "./data-access-policy"; -import type { SearchIndex } from "./search-index"; -import type { ServerfulSearchServiceProps } from "./serverful-search-service"; -import type { ServerlessSearchServiceProps } from "./serverless-search-service"; +import type { ServiceConstructProps } from "../service-common.js"; +import type { ServiceEntityProps } from "../utils.js"; +import type { AccessOptions } from "./data-access-policy.js"; +import type { SearchIndex } from "./search-index.js"; +import type { ServerfulSearchServiceProps } from "./serverful-search-service.js"; +import type { ServerlessSearchServiceProps } from "./serverless-search-service.js"; /** * Properties that override the configuration of the {@link SearchService} within diff --git a/packages/@eventual/aws-cdk/src/search/serverful-search-service.ts b/packages/@eventual/aws-cdk/src/search/serverful-search-service.ts index dad447ad2..67ab97302 100644 --- a/packages/@eventual/aws-cdk/src/search/serverful-search-service.ts +++ b/packages/@eventual/aws-cdk/src/search/serverful-search-service.ts @@ -4,13 +4,13 @@ import { EngineVersion, } from "aws-cdk-lib/aws-opensearchservice"; import { RemovalPolicy } from "aws-cdk-lib/core"; -import { grant } from "../grant"; +import { grant } from "../grant.js"; import { BaseSearchService, BaseSearchServiceProps, -} from "./base-search-service"; -import type { SearchPrincipal } from "./search-service"; -import type { ServerlessSearchService } from "./serverless-search-service"; +} from "./base-search-service.js"; +import type { SearchPrincipal } from "./search-service.js"; +import type { ServerlessSearchService } from "./serverless-search-service.js"; export interface ServerfulSearchServiceProps extends BaseSearchServiceProps, diff --git a/packages/@eventual/aws-cdk/src/search/serverless-search-service.ts b/packages/@eventual/aws-cdk/src/search/serverless-search-service.ts index dc7a4bdac..6d49d3447 100644 --- a/packages/@eventual/aws-cdk/src/search/serverless-search-service.ts +++ b/packages/@eventual/aws-cdk/src/search/serverless-search-service.ts @@ -1,14 +1,14 @@ import { sanitizeCollectionName } from "@eventual/aws-runtime"; import type { IRole } from "aws-cdk-lib/aws-iam"; import { RemovalPolicy } from "aws-cdk-lib/core"; -import { grant } from "../grant"; +import { grant } from "../grant.js"; import { BaseSearchService, BaseSearchServiceProps, -} from "./base-search-service"; -import { Collection, CollectionProps, CollectionType } from "./collection"; -import { SearchPrincipal } from "./search-service"; -import type { ServerfulSearchService } from "./serverful-search-service"; +} from "./base-search-service.js"; +import { Collection, CollectionProps, CollectionType } from "./collection.js"; +import { SearchPrincipal } from "./search-service.js"; +import type { ServerfulSearchService } from "./serverful-search-service.js"; export interface ServerlessSearchServiceProps extends BaseSearchServiceProps, diff --git a/packages/@eventual/aws-cdk/src/service-common.ts b/packages/@eventual/aws-cdk/src/service-common.ts index acea34a9f..7d7b9ad0d 100644 --- a/packages/@eventual/aws-cdk/src/service-common.ts +++ b/packages/@eventual/aws-cdk/src/service-common.ts @@ -1,12 +1,12 @@ import { Function } from "aws-cdk-lib/aws-lambda"; import { Construct } from "constructs"; -import { BucketService } from "./bucket-service"; -import { BuildOutput } from "./build"; -import { CommandService } from "./command-service"; -import { EntityService } from "./entity-service"; -import { LazyInterface } from "./proxy-construct"; -import { SearchService } from "./search/search-service"; -import { Service } from "./service"; +import { BucketService } from "./bucket-service.js"; +import { BuildOutput } from "./build.js"; +import { CommandService } from "./command-service.js"; +import { EntityService } from "./entity-service.js"; +import { LazyInterface } from "./proxy-construct.js"; +import { SearchService } from "./search/search-service.js"; +import { Service } from "./service.js"; export interface ServiceConstructProps { /** diff --git a/packages/@eventual/aws-cdk/src/service-dashboard.ts b/packages/@eventual/aws-cdk/src/service-dashboard.ts index 4b5377025..1ba9e0096 100644 --- a/packages/@eventual/aws-cdk/src/service-dashboard.ts +++ b/packages/@eventual/aws-cdk/src/service-dashboard.ts @@ -2,10 +2,10 @@ import { Dashboard, GraphWidget, MathExpression, - Statistic, + Stats, } from "aws-cdk-lib/aws-cloudwatch"; import { Construct } from "constructs"; -import { Service } from "./service"; +import { Service } from "./service.js"; import { Stack } from "aws-cdk-lib/core"; export interface ServiceDashboardProps { @@ -62,7 +62,7 @@ export class ServiceDashboard extends Construct { expression: "max_age / 1000", usingMetrics: { max_age: service.metricMaxTaskAge({ - statistic: Statistic.MAXIMUM, + statistic: Stats.MAXIMUM, }), }, label: "Maximum age of any Task processed by the Orchestrator", @@ -95,7 +95,7 @@ export class ServiceDashboard extends Construct { }), service.metricSavedHistoryEvents({ label: "Maximum number of events in the History", - statistic: Statistic.MAXIMUM, + statistic: Stats.MAXIMUM, }), ], width: 12, @@ -114,7 +114,7 @@ export class ServiceDashboard extends Construct { }), service.metricCommandsInvoked({ label: "Maximum number of Commands output by a Workflow step", - statistic: Statistic.MAXIMUM, + statistic: Stats.MAXIMUM, }), ], width: 12, diff --git a/packages/@eventual/aws-cdk/src/service-function.ts b/packages/@eventual/aws-cdk/src/service-function.ts index b5208696b..ac0d40cf2 100644 --- a/packages/@eventual/aws-cdk/src/service-function.ts +++ b/packages/@eventual/aws-cdk/src/service-function.ts @@ -7,8 +7,8 @@ import { import { Duration } from "aws-cdk-lib/core"; import { Function, FunctionProps } from "aws-cdk-lib/aws-lambda"; import { Construct } from "constructs"; -import type { BuildOutput } from "./build"; -import { baseFnProps } from "./utils"; +import type { BuildOutput } from "./build.js"; +import { baseFnProps } from "./utils.js"; export interface ServiceFunctionProps { overrides?: Omit, "code" | "handler" | "functionName">; diff --git a/packages/@eventual/aws-cdk/src/service.ts b/packages/@eventual/aws-cdk/src/service.ts index 1b1d6cf33..b1f315737 100644 --- a/packages/@eventual/aws-cdk/src/service.ts +++ b/packages/@eventual/aws-cdk/src/service.ts @@ -35,8 +35,8 @@ import { BucketService, ServiceBucketNotificationHandlers, ServiceBuckets, -} from "./bucket-service"; -import { BuildOutput, buildServiceSync } from "./build"; +} from "./bucket-service.js"; +import { BuildOutput, buildServiceSync } from "./build.js"; import { ApiOverrides, CommandProps, @@ -44,7 +44,7 @@ import { Commands, CommandsProps, CorsOptions, -} from "./command-service"; +} from "./command-service.js"; import { DeepCompositePrincipal } from "./deep-composite-principal.js"; import { EntityService, @@ -54,30 +54,36 @@ import { ServiceEntities, ServiceEntityStreams, } from "./entity-service.js"; -import { EventService } from "./event-service"; -import { grant } from "./grant"; -import { lazyInterface } from "./proxy-construct"; -import { EventualResource } from "./resource"; -import { SchedulerService } from "./scheduler-service"; -import { SearchService, SearchServiceOverrides } from "./search/search-service"; -import { ServerfulSearchService } from "./search/serverful-search-service"; -import { ServerlessSearchService } from "./search/serverless-search-service"; +import { EventService } from "./event-service.js"; +import { grant } from "./grant.js"; +import { lazyInterface } from "./proxy-construct.js"; +import { EventualResource } from "./resource.js"; +import { SchedulerService } from "./scheduler-service.js"; +import { + SearchService, + SearchServiceOverrides, +} from "./search/search-service.js"; +import { ServerfulSearchService } from "./search/serverful-search-service.js"; +import { ServerlessSearchService } from "./search/serverless-search-service.js"; import { ServiceConstructProps, WorkerServiceConstructProps, -} from "./service-common"; +} from "./service-common.js"; import { Subscription, SubscriptionOverrides, Subscriptions, -} from "./subscriptions"; +} from "./subscriptions.js"; import { ServiceTasks, Task, TaskOverrides, TaskService, } from "./task-service.js"; -import { WorkflowService, WorkflowServiceOverrides } from "./workflow-service"; +import { + WorkflowService, + WorkflowServiceOverrides, +} from "./workflow-service.js"; /** * The properties for subscribing a Service to another Service's events. diff --git a/packages/@eventual/aws-cdk/src/subscriptions.ts b/packages/@eventual/aws-cdk/src/subscriptions.ts index 13e31641d..bc1d6dff7 100644 --- a/packages/@eventual/aws-cdk/src/subscriptions.ts +++ b/packages/@eventual/aws-cdk/src/subscriptions.ts @@ -6,17 +6,17 @@ import aws_iam from "aws-cdk-lib/aws-iam"; import type { Function, FunctionProps } from "aws-cdk-lib/aws-lambda"; import { Queue } from "aws-cdk-lib/aws-sqs"; import { Construct } from "constructs"; -import type { BuildOutput } from "./build"; -import { DeepCompositePrincipal } from "./deep-composite-principal"; -import type { EventService } from "./event-service"; -import type { ServiceLocal } from "./service"; +import type { BuildOutput } from "./build.js"; +import { DeepCompositePrincipal } from "./deep-composite-principal.js"; +import type { EventService } from "./event-service.js"; +import type { ServiceLocal } from "./service.js"; import { WorkerServiceConstructProps, configureWorkerCalls, -} from "./service-common"; -import { ServiceFunction } from "./service-function"; -import type { ServiceEntityProps } from "./utils"; -import { EventualResource } from "./resource"; +} from "./service-common.js"; +import { ServiceFunction } from "./service-function.js"; +import type { ServiceEntityProps } from "./utils.js"; +import { EventualResource } from "./resource.js"; export type Subscriptions = ServiceEntityProps< Service, diff --git a/packages/@eventual/aws-cdk/src/task-service.ts b/packages/@eventual/aws-cdk/src/task-service.ts index 305e39740..253d57a93 100644 --- a/packages/@eventual/aws-cdk/src/task-service.ts +++ b/packages/@eventual/aws-cdk/src/task-service.ts @@ -11,20 +11,20 @@ import { Function, FunctionProps } from "aws-cdk-lib/aws-lambda"; import { LambdaDestination } from "aws-cdk-lib/aws-lambda-destinations"; import { Duration, RemovalPolicy, Stack } from "aws-cdk-lib/core"; import { Construct } from "constructs"; -import type { BuildOutput } from "./build"; -import { DeepCompositePrincipal } from "./deep-composite-principal"; -import { grant } from "./grant"; -import type { LazyInterface } from "./proxy-construct"; -import type { SchedulerService } from "./scheduler-service"; -import type { ServiceLocal } from "./service"; +import type { BuildOutput } from "./build.js"; +import { DeepCompositePrincipal } from "./deep-composite-principal.js"; +import { grant } from "./grant.js"; +import type { LazyInterface } from "./proxy-construct.js"; +import type { SchedulerService } from "./scheduler-service.js"; +import type { ServiceLocal } from "./service.js"; import { WorkerServiceConstructProps, configureWorkerCalls, -} from "./service-common"; -import { ServiceFunction } from "./service-function"; -import { ServiceEntityProps, serviceFunctionArn } from "./utils"; -import type { WorkflowService } from "./workflow-service"; -import { EventualResource } from "./resource"; +} from "./service-common.js"; +import { ServiceFunction } from "./service-function.js"; +import { ServiceEntityProps, serviceFunctionArn } from "./utils.js"; +import type { WorkflowService } from "./workflow-service.js"; +import { EventualResource } from "./resource.js"; export type ServiceTasks = ServiceEntityProps; diff --git a/packages/@eventual/aws-cdk/src/workflow-service.ts b/packages/@eventual/aws-cdk/src/workflow-service.ts index badcc8ce9..e78db87ac 100644 --- a/packages/@eventual/aws-cdk/src/workflow-service.ts +++ b/packages/@eventual/aws-cdk/src/workflow-service.ts @@ -22,19 +22,19 @@ import { } from "aws-cdk-lib/aws-sqs"; import { RemovalPolicy } from "aws-cdk-lib/core"; import { Construct } from "constructs"; -import { BucketService } from "./bucket-service"; +import { BucketService } from "./bucket-service.js"; import { EventBridgePipe, PipeSourceParameters, -} from "./constructs/event-bridge-pipe"; -import { EntityService } from "./entity-service"; -import { EventService } from "./event-service"; -import { grant } from "./grant"; -import { LazyInterface } from "./proxy-construct"; -import { SchedulerService } from "./scheduler-service"; -import type { SearchService } from "./search/search-service"; -import { ServiceConstructProps } from "./service-common"; -import { ServiceFunction } from "./service-function"; +} from "./constructs/event-bridge-pipe.js"; +import { EntityService } from "./entity-service.js"; +import { EventService } from "./event-service.js"; +import { grant } from "./grant.js"; +import { LazyInterface } from "./proxy-construct.js"; +import { SchedulerService } from "./scheduler-service.js"; +import type { SearchService } from "./search/search-service.js"; +import { ServiceConstructProps } from "./service-common.js"; +import { ServiceFunction } from "./service-function.js"; import type { TaskService } from "./task-service.js"; export interface WorkflowsProps extends ServiceConstructProps { diff --git a/packages/@eventual/aws-cdk/tsconfig.cjs.json b/packages/@eventual/aws-cdk/tsconfig.cjs.json new file mode 100644 index 000000000..0f4810ef5 --- /dev/null +++ b/packages/@eventual/aws-cdk/tsconfig.cjs.json @@ -0,0 +1,18 @@ +{ + "extends": "../../../tsconfig-base.cjs", + "include": ["src"], + "exclude": ["lib", "node_modules"], + "compilerOptions": { + "rootDir": "src", + "outDir": "lib/cjs", + "declaration": true, + "inlineSourceMap": true + }, + "references": [ + { "path": "../aws-runtime/tsconfig.cjs.json" }, + { "path": "../core/tsconfig.cjs.json" }, + { "path": "../compiler/tsconfig.cjs.json" }, + { "path": "../core-runtime/tsconfig.cjs.json" }, + { "path": "../project/tsconfig.cjs.json" } + ] +} diff --git a/packages/@eventual/aws-cdk/tsconfig.json b/packages/@eventual/aws-cdk/tsconfig.json index bf2c16ef3..d61d31b7e 100644 --- a/packages/@eventual/aws-cdk/tsconfig.json +++ b/packages/@eventual/aws-cdk/tsconfig.json @@ -1,18 +1,18 @@ { - "extends": "../../../tsconfig-base.cjs", + "extends": "../../../tsconfig-base", "include": ["src", "src/package.json"], "exclude": ["lib", "node_modules"], "compilerOptions": { "rootDir": "src", - "outDir": "lib", + "outDir": "lib/esm", "declaration": true, "inlineSourceMap": true }, "references": [ - { "path": "../aws-runtime/tsconfig.cjs.json" }, - { "path": "../core/tsconfig.cjs.json" }, - { "path": "../compiler/tsconfig.cjs.json" }, - { "path": "../core-runtime/tsconfig.cjs.json" }, - { "path": "../project/tsconfig.cjs.json" } + { "path": "../aws-runtime/tsconfig.json" }, + { "path": "../core/tsconfig.json" }, + { "path": "../compiler/tsconfig.json" }, + { "path": "../core-runtime/tsconfig.json" }, + { "path": "../project/tsconfig.json" } ] } diff --git a/packages/@eventual/compiler/src/eventual-infer.ts b/packages/@eventual/compiler/src/eventual-infer.ts index ae5f6cca8..0c6ee2d47 100644 --- a/packages/@eventual/compiler/src/eventual-infer.ts +++ b/packages/@eventual/compiler/src/eventual-infer.ts @@ -54,6 +54,8 @@ export async function infer( bundle: true, write: false, platform: "node", + format: "esm", + target: "es2022", }); const script = bundle.outputFiles[0]!.text; From ac12986c39eb9735aa584a5a2e20b8d8a9666b1a Mon Sep 17 00:00:00 2001 From: sam Date: Wed, 6 Sep 2023 23:32:38 -0700 Subject: [PATCH 2/2] wire up --- packages/@eventual/aws-cdk/src/build.ts | 369 +----------------- .../compiler/bin/eventual-build-service.mjs | 4 + packages/@eventual/compiler/package.json | 5 + .../src/build-service-cli.ts} | 7 +- .../@eventual/compiler/src/build-service.ts | 366 +++++++++++++++++ packages/@eventual/compiler/src/index.ts | 1 + packages/@eventual/compiler/tsconfig.cjs.json | 5 +- packages/@eventual/compiler/tsconfig.json | 5 +- pnpm-lock.yaml | 9 +- tsconfig.json | 1 + 10 files changed, 395 insertions(+), 377 deletions(-) create mode 100755 packages/@eventual/compiler/bin/eventual-build-service.mjs rename packages/@eventual/{aws-cdk/src/build-cli.ts => compiler/src/build-service-cli.ts} (75%) create mode 100644 packages/@eventual/compiler/src/build-service.ts diff --git a/packages/@eventual/aws-cdk/src/build.ts b/packages/@eventual/aws-cdk/src/build.ts index 31dc471cc..fb8b60759 100644 --- a/packages/@eventual/aws-cdk/src/build.ts +++ b/packages/@eventual/aws-cdk/src/build.ts @@ -1,19 +1,9 @@ -import { build, BuildSource, infer } from "@eventual/compiler"; import { BuildManifest } from "@eventual/core-runtime"; -import { - BucketNotificationHandlerSpec, - CommandSpec, - EntityStreamSpec, - EVENTUAL_SYSTEM_COMMAND_NAMESPACE, - ServiceType, - SubscriptionSpec, - TaskSpec, -} from "@eventual/core/internal"; import { Code } from "aws-cdk-lib/aws-lambda"; import { execSync } from "child_process"; import fs from "fs"; -import type openapi from "openapi3-ts"; import path from "path"; +import type { BuildAWSRuntimeProps } from "@eventual/compiler"; // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface BuildOutput extends BuildManifest {} @@ -45,9 +35,9 @@ export class BuildOutput { export function buildServiceSync(request: BuildAWSRuntimeProps): BuildOutput { execSync( - `node ${require.resolve("./build-cli.js")} ${Buffer.from( - JSON.stringify(request) - ).toString("base64")}` + `npx eventual-build-service ${Buffer.from(JSON.stringify(request)).toString( + "base64" + )}` ); return new BuildOutput( @@ -60,354 +50,3 @@ export function buildServiceSync(request: BuildAWSRuntimeProps): BuildOutput { ) ); } - -export interface BuildAWSRuntimeProps { - serviceName: string; - entry: string; - outDir: string; - openApi: { - info: openapi.InfoObject; - }; -} - -export async function buildService(request: BuildAWSRuntimeProps) { - const outDir = request.outDir; - const serviceSpec = await infer(request.entry, request.openApi); - - const specPath = path.join(outDir, "spec.json"); - await fs.promises.mkdir(path.dirname(specPath), { recursive: true }); - // just data extracted from the service, used by the handlers - // separate from the manifest to avoid circular dependency with the bundles - // and reduce size of the data injected into the bundles - await fs.promises.writeFile(specPath, JSON.stringify(serviceSpec, null, 2)); - - const [ - [ - // bundle the default handlers first as we refer to them when bundling all of the individual handlers - orchestrator, - monoTaskFunction, - monoCommandFunction, - monoSubscriptionFunction, - monoEntityStreamWorkerFunction, - monoBucketHandlerWorkerFunction, - transactionWorkerFunction, - ], - [ - // also bundle each of the internal eventual API Functions as they have no dependencies - taskFallbackHandler, - scheduleForwarder, - timerHandler, - searchIndexCustomResourceHandler, - ], - ] = await Promise.all([ - bundleMonolithDefaultHandlers(specPath), - bundleEventualSystemFunctions(specPath), - ]); - - // then, bundle each of the commands and subscriptions - const [commands, subscriptions, tasks] = await Promise.all([ - bundleCommands(serviceSpec.commands), - bundleSubscriptions(serviceSpec.subscriptions), - bundleTasks(serviceSpec.tasks), - ] as const); - - const manifest: BuildManifest = { - serviceName: request.serviceName, - entry: request.entry, - tasks, - events: serviceSpec.events, - subscriptions, - commands, - commandDefault: { - entry: monoCommandFunction!, - spec: { - name: "default", - }, - }, - search: serviceSpec.search, - entities: { - entities: await Promise.all( - serviceSpec.entities.entities.map(async (d) => ({ - ...d, - streams: await bundleEntityStreams(d.streams), - })) - ), - transactions: serviceSpec.transactions, - }, - buckets: { - buckets: await Promise.all( - serviceSpec.buckets.buckets.map(async (b) => ({ - ...b, - handlers: await bundleBucketHandlers(b.handlers), - })) - ), - }, - system: { - entityService: { - transactionWorker: { entry: transactionWorkerFunction! }, - }, - taskService: { - fallbackHandler: { entry: taskFallbackHandler! }, - }, - eventualService: { - systemCommandHandler: { - entry: await buildFunction({ - entry: runtimeHandlersEntrypoint("system-command-handler"), - name: "systemDefault", - injectedEntry: request.entry, - injectedServiceSpec: specPath, - }), - }, - commands: [ - "listWorkflows", - "startExecution", - "listExecutions", - "getExecution", - "getExecutionHistory", - "sendSignal", - "getExecutionWorkflowHistory", - "emitEvents", - "updateTask", - "executeTransaction", - ].map((name) => ({ - name, - namespace: EVENTUAL_SYSTEM_COMMAND_NAMESPACE, - })), - }, - schedulerService: { - forwarder: { - entry: scheduleForwarder!, - handler: "index.handle", - }, - timerHandler: { - entry: timerHandler!, - handler: "index.handle", - }, - }, - searchService: { - customResourceHandler: { - entry: searchIndexCustomResourceHandler!, - handler: "index.handle", - }, - }, - workflowService: { - orchestrator: { - entry: orchestrator!, - }, - }, - }, - }; - - await fs.promises.writeFile( - path.join(outDir, "manifest.json"), - JSON.stringify(manifest, null, 2) - ); - - async function bundleCommands(commandSpecs: CommandSpec[]) { - return await Promise.all( - commandSpecs.map(async (spec) => { - return { - entry: await bundleFile( - specPath, - spec, - "command", - "command-worker", - spec.name, - monoCommandFunction! - ), - spec, - }; - }) - ); - } - - async function bundleSubscriptions(specs: SubscriptionSpec[]) { - return await Promise.all( - specs.map(async (spec) => { - return { - entry: await bundleFile( - specPath, - spec, - "subscription", - "subscription-worker", - spec.name, - monoSubscriptionFunction! - ), - spec, - }; - }) - ); - } - - async function bundleTasks(specs: TaskSpec[]) { - return await Promise.all( - specs.map(async (spec) => { - return { - entry: await bundleFile( - specPath, - spec, - "task", - "task-worker", - spec.name, - monoTaskFunction! - ), - spec, - }; - }) - ); - } - - async function bundleEntityStreams(specs: EntityStreamSpec[]) { - return await Promise.all( - specs.map(async (spec) => { - return { - entry: await bundleFile( - specPath, - spec, - "entity-streams", - "entity-stream-worker", - spec.name, - monoEntityStreamWorkerFunction! - ), - spec, - }; - }) - ); - } - - async function bundleBucketHandlers(specs: BucketNotificationHandlerSpec[]) { - return await Promise.all( - specs.map(async (spec) => { - return { - entry: await bundleFile( - specPath, - spec, - "bucket-handlers", - "bucket-handler-worker", - spec.name, - monoBucketHandlerWorkerFunction! - ), - spec, - }; - }) - ); - } - - async function bundleFile< - Spec extends CommandSpec | SubscriptionSpec | TaskSpec - >( - specPath: string, - spec: Spec, - pathPrefix: string, - entryPoint: string, - name: string, - monoFunction: string - ): Promise { - return spec.sourceLocation?.fileName - ? // we know the source location of the command, so individually build it from that - // file and create a separate (optimized bundle) for it - // TODO: generate an index.ts that imports { exportName } from "./sourceLocation" for enhanced bundling - // TODO: consider always bundling from the root index.ts instead of arbitrarily via ESBuild+SWC AST transformer - await buildFunction({ - name: path.join(pathPrefix, name), - entry: runtimeHandlersEntrypoint(entryPoint), - exportName: spec.sourceLocation.exportName, - injectedEntry: spec.sourceLocation.fileName, - injectedServiceSpec: specPath, - }) - : monoFunction; - } - - function bundleMonolithDefaultHandlers(specPath: string) { - return Promise.all( - [ - { - name: ServiceType.OrchestratorWorker, - entry: runtimeHandlersEntrypoint("orchestrator"), - }, - { - name: ServiceType.TaskWorker, - entry: runtimeHandlersEntrypoint("task-worker"), - }, - { - name: ServiceType.CommandWorker, - entry: runtimeHandlersEntrypoint("command-worker"), - }, - { - name: ServiceType.Subscription, - entry: runtimeHandlersEntrypoint("subscription-worker"), - }, - { - name: ServiceType.EntityStreamWorker, - entry: runtimeHandlersEntrypoint("entity-stream-worker"), - }, - { - name: ServiceType.BucketNotificationHandlerWorker, - entry: runtimeHandlersEntrypoint("bucket-handler-worker"), - }, - { - name: ServiceType.TransactionWorker, - entry: runtimeHandlersEntrypoint("transaction-worker"), - }, - ] - .map((s) => ({ - ...s, - injectedEntry: request.entry, - injectedServiceSpec: specPath, - })) - .map(buildFunction) - ); - } - - function bundleEventualSystemFunctions(specPath: string) { - return Promise.all( - ( - [ - { - name: "TaskFallbackHandler", - entry: runtimeHandlersEntrypoint("task-fallback-handler"), - }, - { - name: "SchedulerForwarder", - entry: runtimeHandlersEntrypoint("schedule-forwarder"), - }, - { - name: "SchedulerHandler", - entry: runtimeHandlersEntrypoint("timer-handler"), - }, - { - name: "SearchIndexCustomResourceHandler", - entry: runtimeHandlersEntrypoint( - "search-index-custom-resource-handler" - ), - }, - ] satisfies Omit< - BuildSource, - "outDir" | "injectedEntry" | "injectedServiceSpec" - >[] - ) - .map((s) => ({ - ...s, - injectedEntry: request.entry, - injectedServiceSpec: specPath, - })) - .map(buildFunction) - ); - } - - async function buildFunction(input: Omit) { - const file = await build({ - ...input, - outDir: request.outDir, - }); - return path.relative(path.resolve(request.outDir), path.resolve(file)); - } -} - -function runtimeHandlersEntrypoint(name: string) { - return path.join(runtimeEntrypoint(), `/handlers/${name}.js`); -} - -function runtimeEntrypoint() { - return path.join(require.resolve("@eventual/aws-runtime"), `../../esm`); -} diff --git a/packages/@eventual/compiler/bin/eventual-build-service.mjs b/packages/@eventual/compiler/bin/eventual-build-service.mjs new file mode 100755 index 000000000..2c1b5f175 --- /dev/null +++ b/packages/@eventual/compiler/bin/eventual-build-service.mjs @@ -0,0 +1,4 @@ +#! /usr/bin/env node +import { main } from "../lib/cjs/build-service-cli.js"; + +await main(); diff --git a/packages/@eventual/compiler/package.json b/packages/@eventual/compiler/package.json index feeb639d4..dcb7f06b5 100644 --- a/packages/@eventual/compiler/package.json +++ b/packages/@eventual/compiler/package.json @@ -2,6 +2,7 @@ "name": "@eventual/compiler", "version": "0.50.0", "bin": { + "eventual-build-service": "./bin/eventual-build-service.mjs", "eventual-bundle": "./bin/eventual-bundle.mjs", "eventual-infer": "./bin/eventual-infer.mjs" }, @@ -10,6 +11,9 @@ "import": "./lib/esm/index.js", "require": "./lib/cjs/index.js" }, + "./bin/eventual-build-service.mjs": { + "require": "./bin/eventual-build-service.mjs" + }, "./bin/eventual-bundle.mjs": { "require": "./bin/eventual-bundle.mjs" }, @@ -33,6 +37,7 @@ "dependencies": { "@anatine/zod-openapi": "^1.12.0", "@eventual/core": "workspace:^", + "@eventual/core-runtime": "workspace:^", "@swc/core": "^1.2.245", "esbuild-plugin-alias-path": "^2.0.2", "openapi3-ts": "^3.1.2", diff --git a/packages/@eventual/aws-cdk/src/build-cli.ts b/packages/@eventual/compiler/src/build-service-cli.ts similarity index 75% rename from packages/@eventual/aws-cdk/src/build-cli.ts rename to packages/@eventual/compiler/src/build-service-cli.ts index c3931e2c2..4be264827 100644 --- a/packages/@eventual/aws-cdk/src/build-cli.ts +++ b/packages/@eventual/compiler/src/build-service-cli.ts @@ -1,4 +1,4 @@ -import { buildService, BuildAWSRuntimeProps } from "./build.js"; +import { buildService, BuildAWSRuntimeProps } from "./build-service.js"; export async function main() { try { @@ -16,8 +16,3 @@ export async function main() { process.exit(1); } } - -main().catch((err) => { - console.error(err); - process.exit(1); -}); diff --git a/packages/@eventual/compiler/src/build-service.ts b/packages/@eventual/compiler/src/build-service.ts new file mode 100644 index 000000000..6f12f380c --- /dev/null +++ b/packages/@eventual/compiler/src/build-service.ts @@ -0,0 +1,366 @@ +import type { BuildManifest } from "@eventual/core-runtime"; +import { + BucketNotificationHandlerSpec, + CommandSpec, + EntityStreamSpec, + EVENTUAL_SYSTEM_COMMAND_NAMESPACE, + ServiceType, + SubscriptionSpec, + TaskSpec, +} from "@eventual/core/internal"; +import fs from "fs"; +import openapi from "openapi3-ts"; +import path from "path"; +import { infer } from "./eventual-infer.js"; +import { build, BuildSource } from "./eventual-bundle.js"; + +export interface BuildAWSRuntimeProps { + serviceName: string; + entry: string; + outDir: string; + openApi: { + info: openapi.InfoObject; + }; +} + +export async function buildService(request: BuildAWSRuntimeProps) { + const outDir = request.outDir; + const serviceSpec = await infer(request.entry, request.openApi); + + const specPath = path.join(outDir, "spec.json"); + await fs.promises.mkdir(path.dirname(specPath), { recursive: true }); + // just data extracted from the service, used by the handlers + // separate from the manifest to avoid circular dependency with the bundles + // and reduce size of the data injected into the bundles + await fs.promises.writeFile(specPath, JSON.stringify(serviceSpec, null, 2)); + + const [ + [ + // bundle the default handlers first as we refer to them when bundling all of the individual handlers + orchestrator, + monoTaskFunction, + monoCommandFunction, + monoSubscriptionFunction, + monoEntityStreamWorkerFunction, + monoBucketHandlerWorkerFunction, + transactionWorkerFunction, + ], + [ + // also bundle each of the internal eventual API Functions as they have no dependencies + taskFallbackHandler, + scheduleForwarder, + timerHandler, + searchIndexCustomResourceHandler, + ], + ] = await Promise.all([ + bundleMonolithDefaultHandlers(specPath), + bundleEventualSystemFunctions(specPath), + ]); + + // then, bundle each of the commands and subscriptions + const [commands, subscriptions, tasks] = await Promise.all([ + bundleCommands(serviceSpec.commands), + bundleSubscriptions(serviceSpec.subscriptions), + bundleTasks(serviceSpec.tasks), + ] as const); + + const manifest: BuildManifest = { + serviceName: request.serviceName, + entry: request.entry, + tasks, + events: serviceSpec.events, + subscriptions, + commands, + commandDefault: { + entry: monoCommandFunction!, + spec: { + name: "default", + }, + }, + search: serviceSpec.search, + entities: { + entities: await Promise.all( + serviceSpec.entities.entities.map(async (d) => ({ + ...d, + streams: await bundleEntityStreams(d.streams), + })) + ), + transactions: serviceSpec.transactions, + }, + buckets: { + buckets: await Promise.all( + serviceSpec.buckets.buckets.map(async (b) => ({ + ...b, + handlers: await bundleBucketHandlers(b.handlers), + })) + ), + }, + system: { + entityService: { + transactionWorker: { entry: transactionWorkerFunction! }, + }, + taskService: { + fallbackHandler: { entry: taskFallbackHandler! }, + }, + eventualService: { + systemCommandHandler: { + entry: await buildFunction({ + entry: runtimeHandlersEntrypoint("system-command-handler"), + name: "systemDefault", + injectedEntry: request.entry, + injectedServiceSpec: specPath, + }), + }, + commands: [ + "listWorkflows", + "startExecution", + "listExecutions", + "getExecution", + "getExecutionHistory", + "sendSignal", + "getExecutionWorkflowHistory", + "emitEvents", + "updateTask", + "executeTransaction", + ].map((name) => ({ + name, + namespace: EVENTUAL_SYSTEM_COMMAND_NAMESPACE, + })), + }, + schedulerService: { + forwarder: { + entry: scheduleForwarder!, + handler: "index.handle", + }, + timerHandler: { + entry: timerHandler!, + handler: "index.handle", + }, + }, + searchService: { + customResourceHandler: { + entry: searchIndexCustomResourceHandler!, + handler: "index.handle", + }, + }, + workflowService: { + orchestrator: { + entry: orchestrator!, + }, + }, + }, + }; + + await fs.promises.writeFile( + path.join(outDir, "manifest.json"), + JSON.stringify(manifest, null, 2) + ); + + async function bundleCommands(commandSpecs: CommandSpec[]) { + return await Promise.all( + commandSpecs.map(async (spec) => { + return { + entry: await bundleFile( + specPath, + spec, + "command", + "command-worker", + spec.name, + monoCommandFunction! + ), + spec, + }; + }) + ); + } + + async function bundleSubscriptions(specs: SubscriptionSpec[]) { + return await Promise.all( + specs.map(async (spec) => { + return { + entry: await bundleFile( + specPath, + spec, + "subscription", + "subscription-worker", + spec.name, + monoSubscriptionFunction! + ), + spec, + }; + }) + ); + } + + async function bundleTasks(specs: TaskSpec[]) { + return await Promise.all( + specs.map(async (spec) => { + return { + entry: await bundleFile( + specPath, + spec, + "task", + "task-worker", + spec.name, + monoTaskFunction! + ), + spec, + }; + }) + ); + } + + async function bundleEntityStreams(specs: EntityStreamSpec[]) { + return await Promise.all( + specs.map(async (spec) => { + return { + entry: await bundleFile( + specPath, + spec, + "entity-streams", + "entity-stream-worker", + spec.name, + monoEntityStreamWorkerFunction! + ), + spec, + }; + }) + ); + } + + async function bundleBucketHandlers(specs: BucketNotificationHandlerSpec[]) { + return await Promise.all( + specs.map(async (spec) => { + return { + entry: await bundleFile( + specPath, + spec, + "bucket-handlers", + "bucket-handler-worker", + spec.name, + monoBucketHandlerWorkerFunction! + ), + spec, + }; + }) + ); + } + + async function bundleFile< + Spec extends CommandSpec | SubscriptionSpec | TaskSpec + >( + specPath: string, + spec: Spec, + pathPrefix: string, + entryPoint: string, + name: string, + monoFunction: string + ): Promise { + return spec.sourceLocation?.fileName + ? // we know the source location of the command, so individually build it from that + // file and create a separate (optimized bundle) for it + // TODO: generate an index.ts that imports { exportName } from "./sourceLocation" for enhanced bundling + // TODO: consider always bundling from the root index.ts instead of arbitrarily via ESBuild+SWC AST transformer + await buildFunction({ + name: path.join(pathPrefix, name), + entry: runtimeHandlersEntrypoint(entryPoint), + exportName: spec.sourceLocation.exportName, + injectedEntry: spec.sourceLocation.fileName, + injectedServiceSpec: specPath, + }) + : monoFunction; + } + + function bundleMonolithDefaultHandlers(specPath: string) { + return Promise.all( + [ + { + name: ServiceType.OrchestratorWorker, + entry: runtimeHandlersEntrypoint("orchestrator"), + }, + { + name: ServiceType.TaskWorker, + entry: runtimeHandlersEntrypoint("task-worker"), + }, + { + name: ServiceType.CommandWorker, + entry: runtimeHandlersEntrypoint("command-worker"), + }, + { + name: ServiceType.Subscription, + entry: runtimeHandlersEntrypoint("subscription-worker"), + }, + { + name: ServiceType.EntityStreamWorker, + entry: runtimeHandlersEntrypoint("entity-stream-worker"), + }, + { + name: ServiceType.BucketNotificationHandlerWorker, + entry: runtimeHandlersEntrypoint("bucket-handler-worker"), + }, + { + name: ServiceType.TransactionWorker, + entry: runtimeHandlersEntrypoint("transaction-worker"), + }, + ] + .map((s) => ({ + ...s, + injectedEntry: request.entry, + injectedServiceSpec: specPath, + })) + .map(buildFunction) + ); + } + + function bundleEventualSystemFunctions(specPath: string) { + return Promise.all( + ( + [ + { + name: "TaskFallbackHandler", + entry: runtimeHandlersEntrypoint("task-fallback-handler"), + }, + { + name: "SchedulerForwarder", + entry: runtimeHandlersEntrypoint("schedule-forwarder"), + }, + { + name: "SchedulerHandler", + entry: runtimeHandlersEntrypoint("timer-handler"), + }, + { + name: "SearchIndexCustomResourceHandler", + entry: runtimeHandlersEntrypoint( + "search-index-custom-resource-handler" + ), + }, + ] satisfies Omit< + BuildSource, + "outDir" | "injectedEntry" | "injectedServiceSpec" + >[] + ) + .map((s) => ({ + ...s, + injectedEntry: request.entry, + injectedServiceSpec: specPath, + })) + .map(buildFunction) + ); + } + + async function buildFunction(input: Omit) { + const file = await build({ + ...input, + outDir: request.outDir, + }); + return path.relative(path.resolve(request.outDir), path.resolve(file)); + } +} + +function runtimeHandlersEntrypoint(name: string) { + return path.join(runtimeEntrypoint(), `/handlers/${name}.js`); +} + +function runtimeEntrypoint() { + return path.join(require.resolve("@eventual/aws-runtime"), `../../esm`); +} diff --git a/packages/@eventual/compiler/src/index.ts b/packages/@eventual/compiler/src/index.ts index 25edbb0ab..8d1834dc6 100644 --- a/packages/@eventual/compiler/src/index.ts +++ b/packages/@eventual/compiler/src/index.ts @@ -1,3 +1,4 @@ +export * from "./build-service.js"; export * from "./eventual-bundle.js"; export * from "./eventual-infer.js"; export * from "./build.js"; diff --git a/packages/@eventual/compiler/tsconfig.cjs.json b/packages/@eventual/compiler/tsconfig.cjs.json index 51045bfc3..23d12aaa5 100644 --- a/packages/@eventual/compiler/tsconfig.cjs.json +++ b/packages/@eventual/compiler/tsconfig.cjs.json @@ -7,5 +7,8 @@ "outDir": "lib/cjs", "declaration": true }, - "references": [{ "path": "../core" }] + "references": [ + { "path": "../core/tsconfig.cjs.json" }, + { "path": "../core-runtime/tsconfig.cjs.json" } + ] } diff --git a/packages/@eventual/compiler/tsconfig.json b/packages/@eventual/compiler/tsconfig.json index 33e88c2ad..ab7eab598 100644 --- a/packages/@eventual/compiler/tsconfig.json +++ b/packages/@eventual/compiler/tsconfig.json @@ -4,7 +4,8 @@ "exclude": ["lib", "node_modules"], "compilerOptions": { "rootDir": "src", - "outDir": "lib/esm" + "outDir": "lib/esm", + "declaration": true }, - "references": [{ "path": "../core" }] + "references": [{ "path": "../core" }, { "path": "../core-runtime" }] } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b87aea329..8ffe61a93 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -684,6 +684,9 @@ importers: '@eventual/core': specifier: workspace:^ version: link:../core + '@eventual/core-runtime': + specifier: workspace:^ + version: link:../core-runtime '@swc/core': specifier: ^1.2.245 version: 1.3.19 @@ -969,7 +972,7 @@ importers: version: 2.80.0 create-sst: specifier: latest - version: 2.11.1 + version: 2.21.4 devDependencies: '@eventual/project': specifier: workspace:^ @@ -7147,8 +7150,8 @@ packages: /create-require@1.1.1: resolution: {integrity: sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==} - /create-sst@2.11.1: - resolution: {integrity: sha512-24Du3u9d5yRSBxcqVZ/rTqmLN5Od3Hk7fypYO72NgEUwKD80+MDUNVShfHmoqfKtXel2xFCNSLxOHaRnVjSDVw==} + /create-sst@2.21.4: + resolution: {integrity: sha512-FLzaUd4Y34QV7l+Gi6F9bZfQW5mE2EL2UXLZHPcKLh5vPVgceDix3Oa9NfT78GeajJQzc+/dOv2nzsV4NFGmog==} hasBin: true dependencies: cli-spinners: 2.9.0 diff --git a/tsconfig.json b/tsconfig.json index 4c5fb2884..55c5f13c0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,6 +6,7 @@ { "path": "apps/tests/aws-runtime-cdk" }, { "path": "apps/tests/aws-runtime" }, { "path": "packages/@eventual/aws-cdk" }, + { "path": "packages/@eventual/aws-cdk/tsconfig.cjs.json" }, { "path": "packages/@eventual/aws-client" }, { "path": "packages/@eventual/aws-client/tsconfig.cjs.json" }, { "path": "packages/@eventual/aws-runtime" },