-
Notifications
You must be signed in to change notification settings - Fork 915
/
server.ts
128 lines (111 loc) · 3.43 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import * as cors from "cors";
import * as express from "express";
import { EmulatorLogger } from "../emulatorLogger";
import { Emulators } from "../types";
import * as bodyParser from "body-parser";
import { createCloudEndpoints } from "./apis/gcloud";
import { StorageEmulator } from "./index";
import { createFirebaseEndpoints } from "./apis/firebase";
/**
* @param defaultProjectId
* @param emulator
*/
export function createApp(
defaultProjectId: string,
emulator: StorageEmulator
): Promise<express.Express> {
const { storageLayer } = emulator;
const app = express();
EmulatorLogger.forEmulator(Emulators.STORAGE).log(
"DEBUG",
`Temp file directory for storage emulator: ${storageLayer.dirPath}`
);
// Enable CORS for all APIs, all origins (reflected), and all headers (reflected).
// This is similar to production behavior. Safe since all APIs are cookieless.
app.use(
cors({
origin: true,
exposedHeaders: [
"content-type",
"x-firebase-storage-version",
"x-goog-upload-url",
"x-goog-upload-status",
"x-goog-upload-command",
"x-gupload-uploadid",
"x-goog-upload-header-content-length",
"x-goog-upload-header-content-type",
"x-goog-upload-protocol",
"x-goog-upload-status",
"x-goog-upload-chunk-granularity",
"x-goog-upload-control-url",
],
})
);
app.use(bodyParser.raw({ limit: "130mb", type: "application/x-www-form-urlencoded" }));
app.use(bodyParser.raw({ limit: "130mb", type: "multipart/related" }));
app.use(
express.json({
type: ["application/json"],
})
);
app.post("/internal/export", async (req, res) => {
const path = req.body.path;
if (!path) {
res.status(400).send("Export request body must include 'path'.");
return;
}
await storageLayer.export(path);
res.sendStatus(200);
});
app.put("/internal/setRules", async (req, res) => {
// Payload:
// {
// rules: {
// files: [{ name:<string> content: <string> }]
// }
// }
// TODO: Add a bucket parameter for per-bucket rules support
const rules = req.body.rules;
if (!(rules && Array.isArray(rules.files) && rules.files.length > 0)) {
res.status(400).send("Request body must include 'rules.files' array .");
return;
}
const file = rules.files[0];
if (!(file.name && file.content)) {
res
.status(400)
.send(
"Request body must include 'rules.files' array where each member contains 'name' and 'content'."
);
return;
}
const name = file.name;
const content = file.content;
const issues = await emulator.loadRuleset({ files: [{ name, content }] });
if (issues.errors.length > 0) {
res.status(400).json({
message: "There was an error updating rules, see logs for more details",
});
return;
}
res.status(200).json({
message: "Rules updated successfully",
});
});
app.post("/internal/reset", (req, res) => {
storageLayer.reset();
res.sendStatus(200);
});
app.use("/v0", createFirebaseEndpoints(emulator));
app.use("/", createCloudEndpoints(emulator));
app.all("**", (req, res) => {
if (process.env.STORAGE_EMULATOR_DEBUG) {
console.table(req.headers);
console.log(req.method, req.url);
res.json("endpoint not implemented");
} else {
res.sendStatus(404);
}
});
return Promise.resolve(app);
}