Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaRHristov committed Nov 22, 2024
1 parent 4502007 commit 27085ef
Show file tree
Hide file tree
Showing 45 changed files with 592 additions and 0 deletions.
4 changes: 4 additions & 0 deletions example/functions/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const uid = () =>
) + new Date().getTime();

const CLIENT = new DynamoDBClient({});

const DOCUMENT_CLIENT = DynamoDBDocumentClient.from(CLIENT as any);

const mapTodo = (
Expand All @@ -41,6 +42,7 @@ const API = {
TableName: process.env.TABLE_NAME,
}),
);

return response.Items.map(mapTodo);
},
create: async (text: string) => {
Expand All @@ -55,6 +57,7 @@ const API = {
Item: newItem,
}),
);

return newItem;
},

Expand All @@ -81,6 +84,7 @@ const API = {
},
}),
);

return todo;
},
};
Expand Down
17 changes: 17 additions & 0 deletions example/functions/src/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type ResponseOptions = {
};

const ASSET_CACHE: Record<string, string> = {};

const MIME_TYPES = {
js: "text/javascript",
css: "text/css",
Expand All @@ -31,6 +32,7 @@ let htmlContent: string | null = null;

class HttpError extends Error {
status: number;

constructor(status: number, message: string) {
super(message);
this.status = status;
Expand Down Expand Up @@ -61,16 +63,20 @@ const apiResponse = async (
body?: string,
) => {
const [id] = pathParams;

if (id) {
if (method === "DELETE") {
await API.delete(id);

return response("", {
contentType: "application/json",
});
}
if (method === "PUT") {
const { text, completedDate } = JSON.parse(body);

const item = await API.update({ id, text, completedDate });

return response(JSON.stringify(item), {
contentType: "application/json",
});
Expand All @@ -79,7 +85,9 @@ const apiResponse = async (

if (pathParams.length == 0 && method === "POST") {
const { text } = JSON.parse(body);

const item = await API.create(text);

return response(JSON.stringify(item), {
contentType: "application/json",
});
Expand All @@ -90,6 +98,7 @@ const apiResponse = async (

const appResponse = async () => {
let todoItems;

if (htmlContent) {
todoItems = await API.getAll();
} else {
Expand All @@ -104,6 +113,7 @@ const appResponse = async () => {
const app = ReactDOMServer.renderToString(
React.createElement(App, { todoItems }),
);

const html = htmlContent
.replace(
'<script id="init" type="text/javascript"></script>',
Expand All @@ -125,7 +135,9 @@ const fileExists = (file: string) =>

const loadAsset = async (asset: string) => {
const safeAsset = asset.replace("..", "");

const cachedAsset = ASSET_CACHE[safeAsset];

if (cachedAsset) {
return cachedAsset;
}
Expand All @@ -134,13 +146,17 @@ const loadAsset = async (asset: string) => {
}
const data = (await fs.readFile(safeAsset)).toString("base64");
ASSET_CACHE[safeAsset] = data;

return data;
};

const assetResponse = async (path: string) => {
const data = await loadAsset(path);

const extIndex = path.lastIndexOf(".");

let contentType = null;

if (extIndex > -1) {
const ext = path.substring(extIndex + 1);
contentType = MIME_TYPES[ext as keyof typeof MIME_TYPES];
Expand Down Expand Up @@ -173,6 +189,7 @@ export const handler = async (event: any) => {
throw new HttpError(400, "Method not supported");
} catch (e) {
console.error(e);

if (e instanceof HttpError) {
return {
statusCode: e.status,
Expand Down
1 change: 1 addition & 0 deletions example/functions/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
declare module "*.svg" {
const content: string;

export default content;
}
7 changes: 7 additions & 0 deletions example/infrastructure/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const main = async () => {
});

const app = new App();

const stack = new Stack(app, "llrt-example", {
env: {
region: process.env.CDK_DEFAULT_REGION,
Expand All @@ -36,18 +37,23 @@ const main = async () => {
const routePaths: string[] = [];

const tmpDir = os.tmpdir();

const targetFunctionsDir = path.join(tmpDir, "functions");

const sourceFunctionsDir = path.resolve("../functions/src");

await fs.mkdir(targetFunctionsDir, { recursive: true });

const sourceDirs = {};

const sources = await fs.readdir(sourceFunctionsDir);
await Promise.all(
sources.map(async (source) => {
if (source === "react") {
return;
}
const { name, ext } = path.parse(source);

const targetDir = path.join(targetFunctionsDir, name);
await fs.mkdir(targetDir, { recursive: true });
await fs.copyFile(
Expand All @@ -70,6 +76,7 @@ const main = async () => {

const createDistribution = (route: string) => {
const id = route.substring(1).replace(/-\//g, "");

const distribution = new aws_cloudfront.Distribution(
stack,
`${id}Distribution`,
Expand Down
1 change: 1 addition & 0 deletions example/llrt-sam/hello-world/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const lambdaHandler = async (
};
} catch (err) {
console.log(err);

return {
statusCode: 500,
body: JSON.stringify({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { stringify } from "./stringify";
export interface AsymmetricMatcherInterface {
asymmetricMatch: (other: unknown) => boolean;
toString: () => string;

getExpectedType?: () => string;
toAsymmetricMatcher?: () => string;
}
Expand Down Expand Up @@ -65,6 +66,7 @@ export abstract class AsymmetricMatcher<T>

abstract asymmetricMatch(other: unknown): boolean;
abstract toString(): string;

getExpectedType?(): string;
toAsymmetricMatcher?(): string;

Expand All @@ -73,7 +75,9 @@ export abstract class AsymmetricMatcher<T>
[Symbol.for("chai/inspect")](options: { depth: number; truncate: number }) {
// minimal pretty-format with simple manual truncation
const result = stringify(this, options.depth, { min: true });

if (result.length <= options.truncate) return result;

return `${this.toString()}{…}`;
}
}
Expand Down Expand Up @@ -154,6 +158,7 @@ export class ObjectContaining extends AsymmetricMatcher<
!equals(this.sample[property], other[property], [])
) {
result = false;

break;
}
}
Expand Down Expand Up @@ -221,6 +226,7 @@ export class Any extends AsymmetricMatcher<any> {
const matches = functionToString
.call(func)
.match(/^(?:async)?\s*function\s*\*?\s*([\w$]+)\s*\(/);

return matches ? matches[1] : "<anonymous>";
}

Expand Down Expand Up @@ -312,6 +318,7 @@ class CloseTo extends AsymmetricMatcher<number> {
if (!isA("Number", other)) return false;

let result = false;

if (
other === Number.POSITIVE_INFINITY &&
this.sample === Number.POSITIVE_INFINITY
Expand Down
Loading

0 comments on commit 27085ef

Please sign in to comment.