create-cellsweb-rpc-api 1.0.28 → 1.0.29

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/bin/create.js CHANGED
@@ -220,7 +220,7 @@ async function main() {
220
220
  node: ">=20.0.0",
221
221
  },
222
222
  dependencies: {
223
- "@cellsweb/rpc-core": "^1.0.32",
223
+ "@cellsweb/rpc-core": "^1.0.33",
224
224
  "@aws-sdk/client-s3": "^3.700.0",
225
225
  "@paralleldrive/cuid2": "^2.2.2",
226
226
  "@sentry/node": "^8.40.0",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-cellsweb-rpc-api",
3
- "version": "1.0.28",
3
+ "version": "1.0.29",
4
4
  "type": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -34,7 +34,9 @@
34
34
  "tinyglobby": "^0.2.17"
35
35
  },
36
36
  "devDependencies": {
37
- "@types/node": "^26.6.6"
37
+ "@sinclair/typebox": "^0.34.52",
38
+ "@types/node": "^26.6.6",
39
+ "dotenv": "^17.4.2"
38
40
  },
39
41
  "scripts": {
40
42
  "test": "echo \"CLI scaffolder test suite\""
@@ -13,6 +13,7 @@ export const EnvSchema = Type.Object({
13
13
  API_HOST: Type.Optional(Type.String({ default: "0.0.0.0" })),
14
14
  API_REQUEST_TIMEOUT_MS: Type.Optional(Type.Number({ default: 30000 })),
15
15
  API_LOG_LEVEL: Type.Optional(Type.String({ default: "info" })),
16
+ API_DOCS_ENABLED: Type.Optional(Type.Boolean({ default: false })),
16
17
 
17
18
  MAX_JSON_BODY_BYTES: Type.Optional(Type.Number({ default: 1_048_576 })),
18
19
  MAX_MULTIPART_BYTES: Type.Optional(Type.Number({ default: 25_000_000 })),
@@ -67,9 +68,12 @@ function parseEnv(): Env {
67
68
  raw.UPLOAD_MODE ??= "local";
68
69
  raw.UPLOAD_DIR ??= "public/uploads";
69
70
  raw.SERVER_URL ??= "http://localhost:4000";
71
+ raw.API_DOCS_ENABLED = booleanFromEnv(raw.API_DOCS_ENABLED, false);
70
72
  raw.TELEMETRY_ENABLED = booleanFromEnv(raw.TELEMETRY_ENABLED, true);
71
73
 
72
- const withDefaults = Value.Default(EnvSchema, raw);
74
+ const withDefaults = (Value as any).Default
75
+ ? (Value as any).Default(EnvSchema, raw)
76
+ : Value.Cast(EnvSchema, raw);
73
77
 
74
78
  if (!Value.Check(EnvSchema, withDefaults)) {
75
79
  const errors = [...Value.Errors(EnvSchema, withDefaults)];
@@ -14,7 +14,7 @@ export const app = new CoreRPC({
14
14
  middlewaresDir: "./src/middlewares",
15
15
  port: env.API_PORT,
16
16
  host: env.API_HOST,
17
- docs: true,
17
+ docs: env.NODE_ENV !== "production" || env.API_DOCS_ENABLED === true,
18
18
  });
19
19
 
20
20
  const rpcHandler = await app.createHandler();
@@ -28,7 +28,10 @@ export const server = http.createServer(async (req, res) => {
28
28
  const filePath = decodeURIComponent(url.replace(/^\/uploads\//, ""));
29
29
  const result = await storage.serve(filePath);
30
30
  if (!result) {
31
- res.writeHead(404, { "Content-Type": "application/json" });
31
+ res.writeHead(404, {
32
+ "Content-Type": "application/json",
33
+ "X-Content-Type-Options": "nosniff",
34
+ });
32
35
  res.end(
33
36
  JSON.stringify({
34
37
  status: "error",
@@ -41,6 +44,7 @@ export const server = http.createServer(async (req, res) => {
41
44
  res.writeHead(200, {
42
45
  "Content-Type": result.mimeType,
43
46
  "Cache-Control": "public, max-age=86400",
47
+ "X-Content-Type-Options": "nosniff",
44
48
  });
45
49
  res.end(result.buffer);
46
50
  return;
@@ -50,6 +54,35 @@ export const server = http.createServer(async (req, res) => {
50
54
  await rpcHandler(req, res);
51
55
  });
52
56
 
57
+ // Graceful shutdown handler for container environments (K8s, Docker, Cloud Run)
58
+ const handleGracefulShutdown = (signal: string) => {
59
+ console.log(`Received ${signal}, initiating graceful shutdown...`);
60
+ if (typeof (server as any).closeIdleConnections === "function") {
61
+ (server as any).closeIdleConnections();
62
+ }
63
+ server.close(async () => {
64
+ try {
65
+ await app.close();
66
+ console.log("Server and background workers shut down cleanly.");
67
+ process.exit(0);
68
+ } catch (err) {
69
+ console.error("Error during graceful shutdown cleanup:", err);
70
+ process.exit(1);
71
+ }
72
+ });
73
+
74
+ // Force termination if connections fail to drain within 10s
75
+ setTimeout(() => {
76
+ console.error("Graceful shutdown timed out after 10s. Forcing exit.");
77
+ process.exit(1);
78
+ }, 10000).unref();
79
+ };
80
+
81
+ if (process.env.NODE_ENV !== "test" && !process.env.VITEST) {
82
+ process.once("SIGTERM", () => handleGracefulShutdown("SIGTERM"));
83
+ process.once("SIGINT", () => handleGracefulShutdown("SIGINT"));
84
+ }
85
+
53
86
  // Only start HTTP listener if not running inside test environment (Vitest)
54
87
  if (process.env.NODE_ENV !== "test" && !process.env.VITEST) {
55
88
  const port = env.API_PORT ?? 4000;
@@ -136,16 +136,24 @@ async function localUpload(
136
136
  originalName: string,
137
137
  dir: string = "",
138
138
  ): Promise<UploadResult> {
139
- const uploadRoot = getUploadRoot();
139
+ const uploadRoot = path.resolve(getUploadRoot());
140
+ const rootWithSep = uploadRoot.endsWith(path.sep) ? uploadRoot : uploadRoot + path.sep;
141
+
142
+ // Security: prevent directory traversal in target directory
143
+ const cleanDir = cleanStoragePath(dir).replace(/\.\./g, "").replace(/^\/+/, "");
144
+ const targetDir = path.resolve(uploadRoot, cleanDir);
145
+
146
+ if (!targetDir.startsWith(rootWithSep) && targetDir !== uploadRoot) {
147
+ throw new Error("Security Violation: Path traversal detected in upload directory");
148
+ }
149
+
140
150
  const safeName = sanitizeFilename(originalName);
141
- const relDir = dir.replace(/^\/+/, "");
142
- const savePath = path.join(uploadRoot, relDir, safeName);
143
- const dirPath = path.dirname(savePath);
151
+ const savePath = path.join(targetDir, safeName);
144
152
 
145
- await fs.mkdir(dirPath, { recursive: true });
153
+ await fs.mkdir(targetDir, { recursive: true });
146
154
  await fs.writeFile(savePath, buffer);
147
155
 
148
- const relativePath = relDir ? `${relDir}/${safeName}` : safeName;
156
+ const relativePath = cleanDir ? `${cleanDir}/${safeName}` : safeName;
149
157
  const normalizedRelPath = relativePath.replace(/\\/g, "/");
150
158
  const relUrl = `uploads/${normalizedRelPath}`;
151
159