Debian Codegraph @

cli/publish-spec/test/server.ts

raw symbols SCIP
1 /* eslint-disable @typescript-eslint/no-explicit-any */
2 /* eslint-disable @typescript-eslint/no-non-null-assertion */
3 import assert from "assert";
4 import express from "express";
5 import multer from "multer";
6 import pc from "picocolors";
7 import type { AssertFileData, AssertRequest } from "./types";
8
9 const PORT = process.env.TEST_PORT || 3005;
10
11 const app = express();
12 const upload = multer({ storage: multer.memoryStorage() });
13
14 app.use(express.urlencoded({ extended: false }));
15
16 function isObject(object: any) {
17 return object != null && typeof object === "object";
18 }
19 function deepEqual(object1: Record<string, any>, object2: Record<string, any>) {
20 const keys1 = Object.keys(object1);
21 const keys2 = Object.keys(object2);
22 if (keys1.length !== keys2.length) {
23 return false;
24 }
25 for (const key of keys1) {
26 const val1 = object1[key];
27 const val2 = object2[key];
28 const areObjects = isObject(val1) && isObject(val2);
29 if ((areObjects && !deepEqual(val1, val2)) || (!areObjects && val1 !== val2)) {
30 return false;
31 }
32 }
33 return true;
34 }
35
36 app.put(
37 "/completions",
38 upload.fields([
39 {
40 name: "tsSpec",
41 maxCount: 1,
42 },
43 {
44 name: "jsSpec",
45 maxCount: 1,
46 },
47 ]),
48 async (req, res) => {
49 const token = req.headers.authorization!;
50
51 const encodedAssertData = req.headers["assert-data"]!;
52 assert(!Array.isArray(encodedAssertData));
53
54 const { files, ...rest }: AssertRequest = JSON.parse(
55 Buffer.from(encodedAssertData, "base64").toString("utf-8")
56 );
57
58 const errors: string[] = [];
59
60 // expect ...rest to be exactly equal to body;
61 if (!deepEqual(rest, { token, ...req.body })) {
62 errors.push("Received data and assert do not match");
63 }
64
65 assert(req.files && !Array.isArray(req.files));
66 if (files) {
67 // expect files content to be exactly the same as the passed in files
68 const specFieldIndexes = ["tsSpec", "jsSpec"] as (keyof typeof files)[];
69 for (const specFieldIndex of specFieldIndexes) {
70 const assertFileData: AssertFileData | undefined = files[specFieldIndex];
71 if (assertFileData) {
72 const { name, content } = assertFileData;
73 const multerFile = req.files[specFieldIndex][0] as Express.Multer.File;
74 if (name && name !== multerFile.originalname) {
75 errors.push(
76 `Expected filename "${multerFile.originalname}" for "${specFieldIndex}" and instead received "${name}"`
77 );
78 }
79 if (content && !multerFile.buffer.equals(Buffer.from(content))) {
80 errors.push(
81 `Received content was different from the expected one for "${specFieldIndex}`
82 );
83 }
84 }
85 }
86 }
87
88 if (errors.length) {
89 return res.status(400).json({ error: errors.join("\n") });
90 }
91
92 return res.status(200).json({ name: req.body.name, namespace: req.body.team || "default" });
93 }
94 );
95
96 const server = app.listen(PORT, () => {
97 console.log(pc.yellow(`Started listening on ${PORT}`));
98 });
99
100 process.on("SIGTERM", () => {
101 console.log(pc.yellow(`Stopped listening on ${PORT}`));
102 server.close();
103 });
104
105 process.on("SIGINT", () => {
106 console.log(pc.yellow(`Stopped listening on ${PORT}`));
107 server.close();
108 });