-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
252 lines (252 loc) · 9.27 KB
/
server.js
File metadata and controls
252 lines (252 loc) · 9.27 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
const fastify = require("fastify")();
const path = require("path");
const fs = require("fs");
// Headers are required for SharedArrayBuffers and WebAssembly
// Otherwise we wouldn't need a server at all.
function setHeaders(res, filePath, _stat) {
const normalizedFilePath = filePath.replaceAll("\\", "/");
if (normalizedFilePath.endsWith(".wasm")) {
res.setHeader("Content-Type", "application/wasm");
}
const needsHeaders = normalizedFilePath.includes("/emHd") || normalizedFilePath.endsWith("index.html");
if (!needsHeaders)
return;
res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
res.setHeader("Cross-Origin-Resource-Policy", "same-site");
}
function buildFileIndex(rootDir, includeFile) {
const map = new Map();
const stack = [rootDir];
while (stack.length > 0) {
const currentDir = stack.pop();
let entries = [];
try {
entries = fs.readdirSync(currentDir, { withFileTypes: true });
}
catch {
continue;
}
for (const entry of entries) {
const fullPath = path.join(currentDir, entry.name);
if (entry.isDirectory()) {
stack.push(fullPath);
continue;
}
if (!entry.isFile())
continue;
if (!includeFile(fullPath, entry.name))
continue;
if (!map.has(entry.name)) {
map.set(entry.name, fullPath);
}
}
}
return map;
}
function buildConfigurationFileIndex(rootDir) {
return buildFileIndex(rootDir, (fullPath, entryName) => {
if (!fullPath.includes(`${path.sep}configuration${path.sep}`))
return false;
return path.extname(entryName).toLowerCase() === ".usd";
});
}
const unitreeModelRoot = path.join(__dirname, "unitree_model");
const piperIsaacSimRoot = path.join(__dirname, "piper_isaac_sim");
const robotsRoot = path.join(__dirname, "Robots");
const usdTextParserDistRoot = path.join(__dirname, "packages/usd-text-parser/dist");
const unitreeConfigurationFileIndex = buildConfigurationFileIndex(unitreeModelRoot);
const piperConfigurationFileIndex = buildConfigurationFileIndex(piperIsaacSimRoot);
const robotsConfigurationFileIndex = buildConfigurationFileIndex(robotsRoot);
const configurationFileIndex = new Map(unitreeConfigurationFileIndex);
for (const [fileName, filePath] of piperConfigurationFileIndex) {
if (!configurationFileIndex.has(fileName)) {
configurationFileIndex.set(fileName, filePath);
}
}
for (const [fileName, filePath] of robotsConfigurationFileIndex) {
if (!configurationFileIndex.has(fileName)) {
configurationFileIndex.set(fileName, filePath);
}
}
const exportRoots = [
{ virtualPrefix: "/unitree_model", diskRoot: unitreeModelRoot },
{ virtualPrefix: "/piper_isaac_sim", diskRoot: piperIsaacSimRoot },
{ virtualPrefix: "/Robots", diskRoot: robotsRoot },
];
function normalizeVirtualExportPath(rawPath) {
const normalized = String(rawPath || "").trim().replaceAll("\\", "/");
if (!normalized.startsWith("/"))
return "";
if (normalized.includes(".."))
return "";
return normalized;
}
function resolveExportDiskPath(virtualPath) {
const normalizedVirtualPath = normalizeVirtualExportPath(virtualPath);
if (!normalizedVirtualPath)
return null;
if (!/\.(usd|usda|usdc)$/i.test(normalizedVirtualPath))
return null;
for (const { virtualPrefix, diskRoot } of exportRoots) {
if (!normalizedVirtualPath.startsWith(`${virtualPrefix}/`))
continue;
const relativePath = normalizedVirtualPath.slice(virtualPrefix.length + 1);
if (!relativePath)
return null;
const resolvedDiskPath = path.resolve(diskRoot, relativePath);
const normalizedDiskRoot = `${path.resolve(diskRoot)}${path.sep}`;
if (resolvedDiskPath !== path.resolve(diskRoot) && !resolvedDiskPath.startsWith(normalizedDiskRoot)) {
return null;
}
return {
virtualPath: normalizedVirtualPath,
diskPath: resolvedDiskPath,
};
}
return null;
}
fastify.register(require("@fastify/compress"), {
// Prioritize smaller transfer size for large WASM/data assets.
encodings: ["br", "gzip"],
threshold: 1024,
customTypes: /^(application\/wasm|application\/octet-stream|application\/javascript|application\/json|text\/)/,
});
fastify.register(require("@fastify/static"), {
root: path.join(__dirname, "usd-wasm/src"),
prefix: "/usd",
maxAge: 24 * 60 * 60 * 1000,
setHeaders,
});
fastify.register(require("@fastify/static"), {
root: unitreeModelRoot,
prefix: "/unitree_model",
setHeaders,
decorateReply: false,
});
fastify.register(require("@fastify/static"), {
root: piperIsaacSimRoot,
prefix: "/piper_isaac_sim",
setHeaders,
decorateReply: false,
});
fastify.register(require("@fastify/static"), {
root: robotsRoot,
prefix: "/Robots",
setHeaders,
decorateReply: false,
});
fastify.register(require("@fastify/static"), {
root: usdTextParserDistRoot,
prefix: "/packages/usd-text-parser/dist",
setHeaders,
decorateReply: false,
});
function buildConfigurationPlaceholderUsd(fileName) {
if (!/^[a-zA-Z0-9_.-]+$/.test(fileName))
return null;
if (!/_(base|physics|robot|sensor)\.usd$/i.test(fileName))
return null;
const defaultPrimName = /_sensor\.usd$/i.test(fileName) ? "Sensors" : "Config";
return [
"#usda 1.0",
"(",
` defaultPrim = "${defaultPrimName}"`,
")",
"",
`def Xform "${defaultPrimName}"`,
"{",
"}",
"",
].join("\n");
}
function sendIndexedConfigurationFile(reply, fileName) {
if (!/^[a-zA-Z0-9_.-]+$/.test(fileName)) {
return reply.code(400).send("Invalid configuration path");
}
const filePath = configurationFileIndex.get(fileName);
if (!filePath) {
const placeholderUsd = buildConfigurationPlaceholderUsd(fileName);
if (!placeholderUsd) {
return reply.code(404).send("Not Found");
}
reply.header("Content-Type", "application/octet-stream");
reply.header("X-Usd-Placeholder", "1");
return reply.send(placeholderUsd);
}
setHeaders(reply.raw, filePath, null);
reply.header("Content-Type", "application/octet-stream");
return reply.send(fs.createReadStream(filePath));
}
fastify.get("/Robots/:vendor/:model/configuration/:fileName", async (request, reply) => {
const fileName = String(request.params?.fileName || "");
return sendIndexedConfigurationFile(reply, fileName);
});
fastify.get("/configuration/:fileName", async (request, reply) => {
const fileName = String(request.params?.fileName || "");
return sendIndexedConfigurationFile(reply, fileName);
});
fastify.post("/api/write-usd-export", async (request, reply) => {
const virtualPath = String(request.body?.virtualPath || "");
const content = request.body?.content;
const overwrite = request.body?.overwrite !== false;
const resolved = resolveExportDiskPath(virtualPath);
if (!resolved) {
return reply.code(400).send({
ok: false,
error: "Invalid export path. Expected /unitree_model, /piper_isaac_sim, or /Robots USD path.",
});
}
if (typeof content !== "string" || content.length <= 0) {
return reply.code(400).send({ ok: false, error: "Missing export content." });
}
if (!overwrite && fs.existsSync(resolved.diskPath)) {
return reply.code(409).send({ ok: false, error: "Export path already exists.", virtualPath: resolved.virtualPath });
}
fs.mkdirSync(path.dirname(resolved.diskPath), { recursive: true });
fs.writeFileSync(resolved.diskPath, content, "utf-8");
return {
ok: true,
virtualPath: resolved.virtualPath,
filePath: resolved.diskPath,
bytesWritten: Buffer.byteLength(content, "utf-8"),
};
});
fastify.register(require("@fastify/static"), {
root: path.join(__dirname, "public"),
prefix: "/",
setHeaders,
decorateReply: false,
});
const defaultPort = Number(process.env.PORT || 3003);
const maxPortAttempts = 20;
const validateOnly = process.env.VALIDATE_ONLY === "1";
async function startServerWithFallback(startPort) {
for (let offset = 0; offset < maxPortAttempts; offset++) {
const port = startPort + offset;
try {
const address = await fastify.listen({ port, host: "127.0.0.1" });
if (offset > 0) {
console.warn(`Port ${startPort} is in use, switched to ${port}.`);
}
console.log(`Your app is listening on ${address}`);
fastify.log.info(`server listening on ${address}`);
return;
}
catch (err) {
if (err.code === "EADDRINUSE" && offset < maxPortAttempts - 1) {
console.warn(`Port ${port} is in use, trying ${port + 1}...`);
continue;
}
fastify.log.error(err);
console.error(`Error starting server on port ${port}`, err);
process.exit(1);
}
}
}
if (validateOnly) {
console.log("Server validation passed.");
}
else {
startServerWithFallback(defaultPort);
}