Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions packages/sst/src/credentials.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from "path";
import { fileURLToPath, pathToFileURL } from "url";
import type { AwsCredentialIdentityProvider } from "@aws-sdk/types";
import { fromNodeProviderChain } from "@aws-sdk/credential-providers";
import { GetCallerIdentityCommand, STSClient } from "@aws-sdk/client-sts";
Expand Down Expand Up @@ -146,13 +147,19 @@ export function useAWSClient<C extends any>(

export const useAWSProvider = lazy(async () => {
const cdkToolkitUrl = await import.meta.resolve!("@aws-cdk/toolkit-lib");
const cdkToolkitPath = new URL(cdkToolkitUrl).pathname;
const cdkToolkitPath = fileURLToPath(cdkToolkitUrl);
const { SdkProvider } = await import(
path.resolve(cdkToolkitPath, "..", "api", "aws-auth", "sdk-provider.js")
pathToFileURL(
path.resolve(cdkToolkitPath, "..", "api", "aws-auth", "sdk-provider.js")
).href
);

const { IoHelper } = await import(
path.resolve(cdkToolkitPath, "..", "api", "io", "private", "io-helper.js")
pathToFileURL(
path.resolve(cdkToolkitPath, "..", "api", "io", "private", "io-helper.js")
).href
);

const project = useProject();
return new SdkProvider(useAWSCredentialsProvider(), project.config.region!, {
ioHelper: IoHelper.fromActionAwareIoHost({
Expand Down
5 changes: 2 additions & 3 deletions packages/sst/src/iot.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { IoTClient, DescribeEndpointCommand } from "@aws-sdk/client-iot";
import { useAWSClient, useAWSCredentials } from "./credentials.js";
import { VisibleError } from "./error.js";
import { lazy } from "./util/lazy.js";
import { Logger } from "./logger.js";

export const useIOTEndpoint = lazy(async () => {
const iot = useAWSClient(IoTClient);
Expand All @@ -21,11 +23,8 @@ export const useIOTEndpoint = lazy(async () => {
import iot from "aws-iot-device-sdk";
import { EventPayload, Events, EventTypes, useBus } from "./bus.js";
import { useProject } from "./project.js";
import { Logger } from "./logger.js";
import { randomUUID } from "crypto";
import { useBootstrap } from "./bootstrap.js";
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
import { lazy } from "./util/lazy.js";

interface Fragment {
id: string;
Expand Down
11 changes: 8 additions & 3 deletions packages/sst/src/stacks/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from "path";
import { fileURLToPath, pathToFileURL } from "url";
import { useBus } from "../bus.js";
import { ConfigOptions, useProject } from "../project.js";
import { useAWSClient, useAWSProvider } from "../credentials.js";
Expand Down Expand Up @@ -249,12 +250,16 @@ async function addInUseExports(

async function createCdkDeployments() {
const cdkToolkitUrl = await import.meta.resolve!("@aws-cdk/toolkit-lib");
const cdkToolkitPath = new URL(cdkToolkitUrl).pathname;
const cdkToolkitPath = fileURLToPath(cdkToolkitUrl);
const { Deployments } = await import(
path.resolve(cdkToolkitPath, "..", "api", "deployments", "deployments.js")
pathToFileURL(
path.resolve(cdkToolkitPath, "..", "api", "deployments", "deployments.js")
).href
);
const { IoHelper } = await import(
path.resolve(cdkToolkitPath, "..", "api", "io", "private", "io-helper.js")
pathToFileURL(
path.resolve(cdkToolkitPath, "..", "api", "io", "private", "io-helper.js")
).href
);
const provider = await useAWSProvider();
await useAWSProvider();
Expand Down
16 changes: 12 additions & 4 deletions packages/sst/src/stacks/synth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { App } from "../constructs/App.js";
import { useProject } from "../project.js";
import { useAWSProvider, useSTSIdentity } from "../credentials.js";
import path from "path";
import { fileURLToPath, pathToFileURL } from "url";
import { VisibleError } from "../error.js";
import { Semaphore } from "../util/semaphore.js";
import { Configuration } from "../util/user-configuration.js";
Expand All @@ -24,15 +25,22 @@ export async function synth(opts: SynthOptions) {
const cxapi = await import("@aws-cdk/cx-api");

const cdkToolkitUrl = await import.meta.resolve!("@aws-cdk/toolkit-lib");
const cdkToolkitPath = new URL(cdkToolkitUrl).pathname;
const cdkToolkitPath = fileURLToPath(cdkToolkitUrl);
const { IoHelper } = await import(
path.resolve(cdkToolkitPath, "..", "api", "io", "private", "io-helper.js")
pathToFileURL(
path.resolve(cdkToolkitPath, "..", "api", "io", "private", "io-helper.js")
).href
);
const { PluginHost } = await import(
path.resolve(cdkToolkitPath, "..", "api", "plugin", "plugin.js")
pathToFileURL(
path.resolve(cdkToolkitPath, "..", "api", "plugin", "plugin.js")
).href
);

const { provideContextValues } = await import(
path.resolve(cdkToolkitPath, "..", "context-providers", "index.js")
pathToFileURL(
path.resolve(cdkToolkitPath, "..", "context-providers", "index.js")
).href
);
const project = useProject();

Expand Down
41 changes: 25 additions & 16 deletions packages/sst/src/util/user-configuration.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
// @ts-nocheck
import * as os from "os";
import * as fs_path from "path";
import * as fs from "fs";
import { fileURLToPath, pathToFileURL } from "url";

type Tag = { Key: string; Value: string };

export const PROJECT_CONFIG = "cdk.json";
export const USER_DEFAULTS = "~/.cdk.json";
const CONTEXT_KEY = "context";

let cdkToolkitPath: string;
let cdkToolkitPath = "";
let cdkToolkitUrl = "";

try {
const cdkToolkitUrl = await import.meta.resolve!("@aws-cdk/toolkit-lib");
cdkToolkitPath = new URL(cdkToolkitUrl).pathname;
cdkToolkitUrl = await import.meta.resolve!("@aws-cdk/toolkit-lib");
cdkToolkitPath = fileURLToPath(cdkToolkitUrl);
} catch (e) {
// Fallback for test environment where import.meta.resolve is not available
const module = await import("module");
const require = (module as any).createRequire(import.meta.url);
cdkToolkitPath = require.resolve("@aws-cdk/toolkit-lib");
cdkToolkitUrl = pathToFileURL(cdkToolkitPath).href;
}
const { ToolkitError } = await import(cdkToolkitPath);

const { ToolkitError } = await import(cdkToolkitUrl);
const { Context, PROJECT_CONTEXT } = await import(
fs_path.resolve(cdkToolkitPath, "..", "api", "context.js")
pathToFileURL(fs_path.resolve(cdkToolkitPath, "..", "api", "context.js")).href
);
const { Settings } = await import(
fs_path.resolve(cdkToolkitPath, "..", "api", "settings.js")
pathToFileURL(fs_path.resolve(cdkToolkitPath, "..", "api", "settings.js"))
.href
);
const { Tags } = await import(
fs_path.resolve(cdkToolkitPath, "..", "api", "tags", "index.js")
pathToFileURL(
fs_path.resolve(cdkToolkitPath, "..", "api", "tags", "index.js")
).href
);

export enum Command {
Expand Down Expand Up @@ -103,10 +112,10 @@ export class Configuration {
output: "cdk.out",
});

private readonly commandLineArguments: Settings;
private readonly commandLineContext: Settings;
private _projectConfig?: Settings;
private _projectContext?: Settings;
private readonly commandLineArguments: typeof Settings;
private readonly commandLineContext: typeof Settings;
private _projectConfig?: typeof Settings;
private _projectContext?: typeof Settings;
private loaded = false;

constructor(private readonly props: ConfigurationProps = {}) {
Expand Down Expand Up @@ -192,11 +201,11 @@ export class Configuration {
}
}

async function loadAndLog(fileName: string): Promise<Settings> {
async function loadAndLog(fileName: string): Promise<typeof Settings> {
return await settingsFromFile(fileName);
}

async function settingsFromFile(fileName: string): Promise<Settings> {
async function settingsFromFile(fileName: string): Promise<typeof Settings> {
let settings;
const expanded = expandHomeDir(fileName);
if (fs.existsSync(expanded)) {
Expand All @@ -218,7 +227,7 @@ async function settingsFromFile(fileName: string): Promise<Settings> {
}

function prohibitContextKeys(
settings: Settings,
settings: typeof Settings,
keys: string[],
fileName: string
) {
Expand All @@ -239,7 +248,7 @@ function prohibitContextKeys(
}

function warnAboutContextKey(
settings: Settings,
settings: typeof Settings,
prefix: string,
fileName: string
) {
Expand Down
18 changes: 7 additions & 11 deletions packages/sst/test/constructs/helper.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { Match, Matcher, MatchResult, Template } from "aws-cdk-lib/assertions";
import { Stack } from "aws-cdk-lib";
import { App, AppDeployProps, AppProps } from "../../dist/constructs";
import { App, AppDeployProps } from "../../dist/constructs";
import { setProject, useProject } from "../../dist/project.js";
import { useNodeHandler } from "../../dist/runtime/handlers/node.js";
import { usePythonHandler } from "../../dist/runtime/handlers/python.js";
import { useDotnetHandler } from "../../dist/runtime/handlers/dotnet.js";
import { useJavaHandler } from "../../dist/runtime/handlers/java.js";
import { useGoHandler } from "../../dist/runtime/handlers/go.js";
import { useContainerHandler } from "../../dist/runtime/handlers/container.js";
import os from "os";
import path from "path";
import fs from "fs/promises";
import crypto from "crypto";

import * as os from "os";
import * as path from "path";
import * as fs from "fs/promises";
import * as crypto from "crypto";

///////////////////////
// Matcher functions //
Expand All @@ -34,6 +29,7 @@ export async function createApp(props?: Partial<AppDeployProps>) {
setProject({
version: "test",
cdkVersion: "test",
constructsVersion: "test",
stacks: async () => {},
metafile: null as any,
config: {
Expand Down