Skip to content

Commit

Permalink
Merge branch 'main' into rybickic/sunset-dynamodb
Browse files Browse the repository at this point in the history
  • Loading branch information
Chriscbr committed Mar 21, 2024
2 parents 3aa980c + 55c0ea5 commit 48aa01a
Show file tree
Hide file tree
Showing 15 changed files with 759 additions and 78 deletions.
8 changes: 4 additions & 4 deletions libs/wingsdk/src/simulator/simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ export interface ISimulatorContext {
readonly serverUrl: string;

/**
* Find a resource simulation by its handle. Throws if the handle isn't valid.
* Obtain a client given a resource's handle.
*/
findInstance(handle: string): ISimulatorResourceInstance;
getClient(handle: string): unknown;

/**
* Add a trace. Traces are breadcrumbs of information about resource
Expand Down Expand Up @@ -739,8 +739,8 @@ export class Simulator {
statedir: join(this.statedir, resourceConfig.addr),
resourcePath: resourceConfig.path,
serverUrl: this.url,
findInstance: (handle: string) => {
return this._handles.find(handle);
getClient: (handle: string) => {
return makeSimulatorClient(this.url, handle);
},
addTrace: (trace: Trace) => {
this.addTrace(trace);
Expand Down
20 changes: 7 additions & 13 deletions libs/wingsdk/src/target-sim/api.inflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class Api
const s = {
functionHandle: subscriber,
method: r.method,
path: r.path,
pathPattern: r.pathPattern,
};
this.routes.push(s);
this.populateRoute(s, subscriber);
Expand All @@ -202,25 +202,19 @@ export class Api
| "patch"
| "connect";

const fnClient = this.context.findInstance(
functionHandle
) as IFunctionClient & ISimulatorResourceInstance;
if (!fnClient) {
throw new Error("No function client found!");
}

const fnClient = this.context.getClient(functionHandle) as IFunctionClient;
this.app[method](
transformRoutePath(route.path),
transformRoutePath(route.pathPattern),
asyncMiddleware(
async (
req: express.Request,
res: express.Response,
next: express.NextFunction
) => {
this.addTrace(
`Processing "${route.method} ${route.path}" params=${JSON.stringify(
req.params
)}).`
`Processing "${route.method} ${
route.pathPattern
}" params=${JSON.stringify(req.params)}).`
);

const apiRequest = transformRequest(req);
Expand All @@ -243,7 +237,7 @@ export class Api
} else {
res.end();
}
this.addTrace(`${route.method} ${route.path} - ${status}.`);
this.addTrace(`${route.method} ${route.pathPattern} - ${status}.`);
} catch (err) {
return next(err);
}
Expand Down
8 changes: 4 additions & 4 deletions libs/wingsdk/src/target-sim/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class Api extends cloud.Api implements ISimulatorResource {
private createOrGetFunction(
inflight: cloud.IApiEndpointHandler,
props: cloud.FunctionProps,
path: string,
pathPattern: string,
method: cloud.HttpMethod
): Function {
let handler = this.handlers[inflight._id];
Expand All @@ -52,7 +52,7 @@ export class Api extends cloud.Api implements ISimulatorResource {
const routes = (handler.mapping.eventProps.subscriptionProps as any)
.routes as ApiRoute[];
routes.push({
path,
pathPattern,
method,
});

Expand All @@ -73,7 +73,7 @@ export class Api extends cloud.Api implements ISimulatorResource {
props
) as Function;
Node.of(fn).sourceModule = SDK_SOURCE_MODULE;
Node.of(fn).title = `${method.toUpperCase()} ${path}`;
Node.of(fn).title = `${method.toUpperCase()} ${pathPattern}`;
Node.of(fn).hidden = true;

const eventMapping = new EventMapping(
Expand All @@ -85,7 +85,7 @@ export class Api extends cloud.Api implements ISimulatorResource {
subscriptionProps: {
routes: [
{
path,
pathPattern,
method,
},
],
Expand Down
4 changes: 2 additions & 2 deletions libs/wingsdk/src/target-sim/bucket.inflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ export class Bucket implements IBucketClient, ISimulatorResourceInstance {
return;
}

const topicClient = this.context.findInstance(
const topicClient = this.context.getClient(
this.topicHandlers[actionType]!
) as ITopicClient & ISimulatorResourceInstance;
) as ITopicClient;

return topicClient.publish(key);
}
Expand Down
4 changes: 2 additions & 2 deletions libs/wingsdk/src/target-sim/event-mapping.inflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ export class EventMapping implements ISimulatorResourceInstance {
}

public async init(): Promise<EventMappingAttributes> {
const client = this.context.findInstance(this.publisher) as IEventPublisher;
const client = this.context.getClient(this.publisher) as IEventPublisher;
await client.addEventSubscription(this.subscriber, this.eventSubscription);
return {};
}

public async cleanup(): Promise<void> {
const client = this.context.findInstance(this.publisher) as IEventPublisher;
const client = this.context.getClient(this.publisher) as IEventPublisher;
await client.removeEventSubscription(this.subscriber);
}

Expand Down
4 changes: 2 additions & 2 deletions libs/wingsdk/src/target-sim/on-deploy.inflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export class OnDeploy implements IOnDeployClient, ISimulatorResourceInstance {
}

public async init(): Promise<OnDeployAttributes> {
const functionClient = this.context.findInstance(
const functionClient = this.context.getClient(
this.functionHandle
) as IFunctionClient & ISimulatorResourceInstance;
) as IFunctionClient;
await this.context.withTrace({
message: "OnDeploy invoked.",
activity: async () => {
Expand Down
6 changes: 3 additions & 3 deletions libs/wingsdk/src/target-sim/queue.inflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ export class Queue
continue;
}

const fnClient = this.context.findInstance(
subscriber.functionHandle!
) as IFunctionClient & ISimulatorResourceInstance;
const fnClient = this.context.getClient(
subscriber.functionHandle
) as IFunctionClient;
if (!fnClient) {
throw new Error("No function client found");
}
Expand Down
6 changes: 3 additions & 3 deletions libs/wingsdk/src/target-sim/schedule.inflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ export class Schedule

private runTasks() {
for (const task of this.tasks) {
const fnClient = this.context.findInstance(
task.functionHandle!
) as IFunctionClient & ISimulatorResourceInstance;
const fnClient = this.context.getClient(
task.functionHandle
) as IFunctionClient;
if (!fnClient) {
throw new Error("No function client found for task.");
}
Expand Down
4 changes: 2 additions & 2 deletions libs/wingsdk/src/target-sim/schema-resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export interface ApiAttributes {

/** Schema for cloud.Api.props.routes */
export interface ApiRoute {
/** The path to handle. */
readonly path: string;
/** The HTTP path pattern to handle. */
readonly pathPattern: string;
/** The HTTP method to handle. */
readonly method: HttpMethod;
}
Expand Down
7 changes: 1 addition & 6 deletions libs/wingsdk/src/target-sim/test-runner.inflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ export class TestRunner
if (!functionHandle) {
throw new Error(`No test found at path "${path}"`);
}
const fnClient = this.context.findInstance(
functionHandle
) as IFunctionClient & ISimulatorResourceInstance;
if (!fnClient) {
throw new Error(`No function client found for test path "${path}"`);
}
const fnClient = this.context.getClient(functionHandle) as IFunctionClient;
let pass = false;
let error: string | undefined;
const previousTraces = this.context.listTraces().length;
Expand Down
11 changes: 3 additions & 8 deletions libs/wingsdk/src/target-sim/topic.inflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,9 @@ export class Topic

private async publishMessage(message: string) {
for (const subscriber of this.subscribers) {
const fnClient = this.context.findInstance(
subscriber.functionHandle!
) as IFunctionClient & ISimulatorResourceInstance;

if (!fnClient) {
throw new Error("No function client found!");
}

const fnClient = this.context.getClient(
subscriber.functionHandle
) as IFunctionClient;
this.context.addTrace({
type: TraceType.RESOURCE,
data: {
Expand Down
Loading

0 comments on commit 48aa01a

Please sign in to comment.