@atria/server 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/server.d.ts +10 -0
- package/dist/server.js +88 -0
- package/dist/server.js.map +1 -0
- package/package.json +28 -0
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface StartDevServerOptions {
|
|
2
|
+
projectRoot: string;
|
|
3
|
+
port: number;
|
|
4
|
+
host?: string;
|
|
5
|
+
}
|
|
6
|
+
export interface DevServerHandle {
|
|
7
|
+
url: string;
|
|
8
|
+
close: () => Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
export declare const startDevServer: (options: StartDevServerOptions) => Promise<DevServerHandle>;
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { createServer } from "node:http";
|
|
2
|
+
import { promises as fs } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { ATRIA_RUNTIME_DIR } from "@atria/shared";
|
|
5
|
+
const MIME_TYPES = {
|
|
6
|
+
".html": "text/html; charset=utf-8",
|
|
7
|
+
".js": "text/javascript; charset=utf-8",
|
|
8
|
+
".css": "text/css; charset=utf-8",
|
|
9
|
+
".json": "application/json; charset=utf-8",
|
|
10
|
+
".svg": "image/svg+xml",
|
|
11
|
+
".ico": "image/x-icon",
|
|
12
|
+
".txt": "text/plain; charset=utf-8"
|
|
13
|
+
};
|
|
14
|
+
const isInsideDirectory = (basePath, targetPath) => {
|
|
15
|
+
const relativePath = path.relative(basePath, targetPath);
|
|
16
|
+
return relativePath !== "" && !relativePath.startsWith("..") && !path.isAbsolute(relativePath);
|
|
17
|
+
};
|
|
18
|
+
const resolveRequestFile = async (runtimeDir, requestPath) => {
|
|
19
|
+
const normalizedPath = requestPath === "/" ? "index.html" : requestPath.replace(/^\/+/, "");
|
|
20
|
+
const filePath = path.join(runtimeDir, normalizedPath);
|
|
21
|
+
if (normalizedPath !== "index.html" && !isInsideDirectory(runtimeDir, filePath)) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
const fileStats = await fs.stat(filePath);
|
|
26
|
+
if (fileStats.isDirectory()) {
|
|
27
|
+
return path.join(filePath, "index.html");
|
|
28
|
+
}
|
|
29
|
+
return filePath;
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
if (path.extname(normalizedPath)) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
return path.join(runtimeDir, "index.html");
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
const closeServer = async (server) => new Promise((resolve, reject) => {
|
|
39
|
+
server.close((error) => {
|
|
40
|
+
if (error) {
|
|
41
|
+
reject(error);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
resolve();
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
export const startDevServer = async (options) => {
|
|
48
|
+
const host = options.host ?? "localhost";
|
|
49
|
+
const runtimeDir = path.join(options.projectRoot, ATRIA_RUNTIME_DIR);
|
|
50
|
+
await fs.access(runtimeDir);
|
|
51
|
+
const server = createServer(async (request, response) => {
|
|
52
|
+
try {
|
|
53
|
+
const requestUrl = new URL(request.url ?? "/", `http://${host}:${options.port}`);
|
|
54
|
+
if (requestUrl.pathname === "/api/health") {
|
|
55
|
+
response.writeHead(200, { "content-type": MIME_TYPES[".json"] });
|
|
56
|
+
response.end(JSON.stringify({ ok: true }));
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const targetFile = await resolveRequestFile(runtimeDir, decodeURIComponent(requestUrl.pathname));
|
|
60
|
+
if (!targetFile) {
|
|
61
|
+
response.writeHead(403, { "content-type": MIME_TYPES[".txt"] });
|
|
62
|
+
response.end("Forbidden");
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const fileBuffer = await fs.readFile(targetFile);
|
|
66
|
+
const extension = path.extname(targetFile);
|
|
67
|
+
const contentType = MIME_TYPES[extension] ?? "application/octet-stream";
|
|
68
|
+
response.writeHead(200, { "content-type": contentType });
|
|
69
|
+
response.end(fileBuffer);
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
response.writeHead(404, { "content-type": MIME_TYPES[".txt"] });
|
|
73
|
+
response.end("Not Found");
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
await new Promise((resolve, reject) => {
|
|
77
|
+
server.once("error", reject);
|
|
78
|
+
server.listen(options.port, host, () => {
|
|
79
|
+
server.off("error", reject);
|
|
80
|
+
resolve();
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
return {
|
|
84
|
+
url: `http://${host}:${options.port}`,
|
|
85
|
+
close: () => closeServer(server)
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAe,MAAM,WAAW,CAAC;AACtD,OAAO,EAAE,QAAQ,IAAI,EAAE,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAalD,MAAM,UAAU,GAA2B;IACzC,OAAO,EAAE,0BAA0B;IACnC,KAAK,EAAE,gCAAgC;IACvC,MAAM,EAAE,yBAAyB;IACjC,OAAO,EAAE,iCAAiC;IAC1C,MAAM,EAAE,eAAe;IACvB,MAAM,EAAE,cAAc;IACtB,MAAM,EA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AC;AACJ,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@atria/server",
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"description": "HTTP server and runtime delivery for atria",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@atria/shared": "0.0.3"
|
|
20
|
+
},
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsc -p tsconfig.json",
|
|
26
|
+
"clean": "rm -rf dist"
|
|
27
|
+
}
|
|
28
|
+
}
|