Skip to content

Commit

Permalink
feedback, cors, remove extra handling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
thantos committed Oct 30, 2023
1 parent 840da01 commit 3f56ee5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
47 changes: 24 additions & 23 deletions packages/@eventual/cli/src/commands/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
SocketQuery,
} from "@eventual/core";
import {
LOCAL_BUCKET_PRESIGNED_URL_PREFIX,
LocalEnvironment,
LocalPersistanceStore,
NoPersistanceStore,
Expand Down Expand Up @@ -191,17 +192,15 @@ export const local = (yargs: Argv) =>
* Catches the url pattern of /__bucket/presigned/ and handles it like a s3 presigned url.
*/
const presignedMiddleware: RequestHandler = async (req, res, next) => {
if (req.url.startsWith("/__bucket/presigned")) {
if (req.url.startsWith(LOCAL_BUCKET_PRESIGNED_URL_PREFIX)) {
try {
const [, , , token] = req.url.split("/");
if (!token) {
res.status(400).send("Missing token");
return;
}
if (req.method === "PUT") {
const body = req.body
? JSON.stringify(req.body)
: await getBody(req);
const body = await getBody(req);
// TODO: support more data like metadata?
const options: PutBucketOptions = {
cacheControl: req.headers["cache-control"] as string,
Expand Down Expand Up @@ -246,6 +245,9 @@ export const local = (yargs: Argv) =>
if (handleInvalidPresignedResult(res, result)) {
res.status(200).end();
}
} else if (req.method === "OPTIONS") {
// CORS should succeed with the injected cors header from the cors middleware.
res.status(200).end();
} else {
res.status(405).send("Method not allowed");
}
Expand All @@ -257,28 +259,27 @@ export const local = (yargs: Argv) =>
next();
};

const corsMiddleware: RequestHandler = (req, res, next) => {
const headers = res.getHeaders();
if (!headers["Access-Control-Allow-Origin"]) {
res.header("Access-Control-Allow-Origin", req.headers.origin ?? "*");
}
if (!headers["Access-Control-Allow-Methods"]) {
res.header("Access-Control-Allow-Methods", "*");
}
if (!headers["Access-Control-Allow-Headers"]) {
res.header("Access-Control-Allow-Headers", "*");
}
if (!headers["Access-Control-Allow-Credentials"]) {
res.header("Access-Control-Allow-Credentials", "true");
}
next();
};

const apiMiddleware: RequestHandler[] = [
corsMiddleware,
presignedMiddleware,
express.json({ strict: false, limit: maxBodySize }),
(req, res, next) => {
const headers = res.getHeaders();
if (!headers["Access-Control-Allow-Origin"]) {
res.header(
"Access-Control-Allow-Origin",
req.headers.origin ?? "*"
);
}
if (!headers["Access-Control-Allow-Methods"]) {
res.header("Access-Control-Allow-Methods", "*");
}
if (!headers["Access-Control-Allow-Headers"]) {
res.header("Access-Control-Allow-Headers", "*");
}
if (!headers["Access-Control-Allow-Credentials"]) {
res.header("Access-Control-Allow-Credentials", "true");
}
next();
},
];

// open up all of the user and service commands to the service.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export class LocalEnvironment {
: never,
content?: string | Buffer | Readable
): Promise<PresignedUrlResponse<Op>> {
const data = this.localContainer.bucketStore.decodeAsyncUrlKey(
const data = this.localContainer.bucketStore.decodeLocalPresignedToken(
token,
operation
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type Buckets = Record<string, Record<string, ObjectData>>;

const META_SUFFIX = ".eventual.meta.json";

export const LOCAL_BUCKET_PRESIGNED_URL_PREFIX = "/__bucket/presigned/";

export class LocalBucketStore implements BucketStore, LocalSerializable {
constructor(
private props: LocalBucketStoreProps,
Expand Down Expand Up @@ -315,14 +317,16 @@ export class LocalBucketStore implements BucketStore, LocalSerializable {
};

return {
url: `${this.props.baseUrl}/__bucket/presigned/${Buffer.from(
url: `${
this.props.baseUrl
}${LOCAL_BUCKET_PRESIGNED_URL_PREFIX}${Buffer.from(
JSON.stringify(data)
).toString("base64url")}`,
expires: expiration.toISOString(),
};
}

public decodeAsyncUrlKey(
public decodeLocalPresignedToken(
key: string,
assertOperation?: PresignedUrlOperation
): PresignUrlEnvelope {
Expand Down
1 change: 1 addition & 0 deletions packages/@eventual/core-runtime/src/local/stores/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./bucket-store.js";
export * from "./execution-history-state-store.js";
export * from "./execution-history-store.js";
export * from "./execution-store.js";
Expand Down

0 comments on commit 3f56ee5

Please sign in to comment.