Skip to content

Commit

Permalink
linking type gen
Browse files Browse the repository at this point in the history
  • Loading branch information
thdxr committed Jan 11, 2024
1 parent 5be442a commit a280615
Show file tree
Hide file tree
Showing 20 changed files with 155 additions and 27 deletions.
6 changes: 5 additions & 1 deletion examples/test/package.json
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{}
{
"dependencies": {
"sst": "3.0.1-6"
}
}
16 changes: 16 additions & 0 deletions examples/test/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions examples/test/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/// <reference types="../.sst/types.generated.d.ts" />
/// <reference path="../.sst/types.generated.ts" />

import { Resource } from "sst";

export function handler() {
console.log(Resource.FOO);
return "Hello World";
return {
statusCode: 200,
body: JSON.stringify(Resource.StripeKey),
};
}
1 change: 1 addition & 0 deletions examples/test/src/sst-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference path="../.sst/types.generated.ts" />
3 changes: 2 additions & 1 deletion examples/test/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export default $config({
},
async run() {
const bucket = new sst.Bucket("MyBucket");
const secret = new sst.Secret("FOO");
const queue = new aws.sqs.Queue("MyQueue");
const secret = new sst.Secret("StripeKey");
const fn = new sst.Function("MyFunction", {
url: true,
link: [secret],
Expand Down
2 changes: 1 addition & 1 deletion internal/components/src/auto/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ export async function run(program: PulumiFn) {
},
);

initializeLinkRegistry();
await initializeLinkRegistry();
return await program();
}
6 changes: 5 additions & 1 deletion internal/components/src/components/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface BucketArgs {
};
}

export class Bucket extends Component implements Linkable {
export class Bucket extends Component implements Linkable, AWSLinkable {
public bucket: aws.s3.BucketV2;

constructor(
Expand Down Expand Up @@ -102,4 +102,8 @@ export class Bucket extends Component implements Linkable {
})),
};
}

public getSSTAWSPermissions(): string[] {
return ["s3:*"];
}
}
4 changes: 1 addition & 3 deletions internal/components/src/components/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
output,
all,
interpolate,
jsonStringify,
} from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import { build } from "../runtime/node.js";
Expand Down Expand Up @@ -593,13 +592,12 @@ export class Function extends Component {
for (const injection of injections) {
all([injection]).apply(([value]) => {
registerLinkType({
path: ".sst/types.generated.d.ts",
type: value.type,
name: value.name,
});
});
}
return jsonStringify(injections);
return injections;
});
}

Expand Down
16 changes: 9 additions & 7 deletions internal/components/src/components/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,30 @@ export function isAWSLinkable(obj: any): obj is AWSLinkable {
return "getSSTAWSPermissions" in obj;
}

const DEFAULT_TYPE_PATH = ".sst/types.generated.ts";
interface TypeRegistration {
path: string;
path?: string;
name: string;
type: any;
}
const files = new Map<string, Record<string, string>>();
export function initializeLinkRegistry() {
export async function initializeLinkRegistry() {
files.clear();
}

export async function registerLinkType(reg: TypeRegistration) {
let file = files.get(reg.path);
const path = reg.path ?? DEFAULT_TYPE_PATH;
let file = files.get(path);
if (!file) {
file = {};
files.set(reg.path, file);
files.set(path, file);
}
file[reg.name] = reg.type;
await fs.promises.writeFile(
reg.path,
path,
[
`declare global {`,
` export const Resource: {`,
`declare module "sst/resource" {`,
` export interface Resource {`,
...Object.entries(file).map(([key, value]) => ` ${key}: ${value};`),
` [key: string]: any`,
` }`,
Expand Down
10 changes: 8 additions & 2 deletions internal/components/src/runtime/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import fsSync from "fs";
export async function build(
name: string,
input: pulumi.Unwrap<FunctionArgs> & {
links?: string;
links?: {
name: string;
value: string;
}[];
},
) {
const out = path.join($cli.paths.work, "artifacts", `${name}-src`);
Expand Down Expand Up @@ -51,6 +54,9 @@ export async function build(
// Rebuilt using existing esbuild context
const forceExternal = ["sharp", "pg-native"];
const { external, ...override } = nodejs.esbuild || {};
const links = Object.fromEntries(
input.links?.map((item) => [item.name, item.value]) || [],
);
const options: BuildOptions = {
entryPoints: [path.resolve(file)],
platform: "node",
Expand All @@ -62,7 +68,7 @@ export async function build(
loader: nodejs.loader,
keepNames: true,
define: {
SST_LINKS: input.links || "[]",
$SST_LINKS: JSON.stringify(links),
},
bundle: true,
logLevel: "silent",
Expand Down
6 changes: 0 additions & 6 deletions internal/components/src/shim.js

This file was deleted.

8 changes: 8 additions & 0 deletions internal/components/src/shim/runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const Resource = new Proxy($resource, {
get(target, prop) {
if (!(prop in target)) {
throw new Error(`"${prop}" is not linked`);
}
return target[prop];
},
});
3 changes: 2 additions & 1 deletion pkg/project/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ type Backend interface {
Lock(app string, stage string, out *os.File) error
Unlock(app string, stage string, in *os.File) error
Cancel(app string, stage string) error
Env() (map[string]string, error)

ListSecrets(app string, stage string) (io.Reader, error)
SetSecrets(app string, stage string, data io.Reader) error
Env() (map[string]string, error)

setPassphrase(app string, stage string, passphrase string) error
getPassphrase(app string, stage string) (string, error)
Expand Down
2 changes: 1 addition & 1 deletion pkg/project/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (s *stack) Run(ctx context.Context, input *StackInput) error {
if provider == "cloudflare" && key == "accountId" {
continue
}
config[fmt.Sprintf("%v:%v", provider, key)] = auto.ConfigValue{Value: value, Secret: true}
config[fmt.Sprintf("%v:%v", provider, key)] = auto.ConfigValue{Value: value}
}
}
err = stack.SetAllConfig(ctx, config)
Expand Down
1 change: 1 addition & 0 deletions sdk/js/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
22 changes: 22 additions & 0 deletions sdk/js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"$schema": "https://json.schemastore.org/package.json",
"name": "sst",
"type": "module",
"version": "3.0.1-6",
"exports": {
".": "./dist/index.js",
"./*": "./dist/*.js"
},
"scripts": {
"build": "tsc",
"release": "pnpm build && pnpm version prerelease && pnpm publish --no-git-checks --tag=ion --access=public"
},
"devDependencies": {
"@tsconfig/node18": "^18.2.2",
"@types/node": "^20.11.0",
"typescript": "^5.3.3"
},
"files": [
"dist"
]
}
38 changes: 38 additions & 0 deletions sdk/js/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sdk/js/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./resource.js";
15 changes: 15 additions & 0 deletions sdk/js/src/resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export interface Resource {
[key: string]: any;
}

declare global {
export const $SST_LINKS: Resource;
}
export const Resource = new Proxy($SST_LINKS, {
get(target, prop: string) {
if (!(prop in target)) {
throw new Error(`"${prop}" is not linked`);
}
return target[prop];
},
}) as Resource;
12 changes: 12 additions & 0 deletions sdk/js/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@tsconfig/node18/tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"declaration": true,
"module": "NodeNext",
"moduleResolution": "NodeNext",
"isolatedModules": true,
"downlevelIteration": true,
}
}

0 comments on commit a280615

Please sign in to comment.