|
6 | 6 |
|
7 | 7 | import "./config-server.mjs";
|
8 | 8 | import { config } from "./config-global.mjs";
|
| 9 | +import { find_vg } from "./vg.mjs" |
| 10 | +import { TubeMapError, BadRequestError, InternalServerError, VgExecutionError } from "./errors.mjs" |
| 11 | + |
9 | 12 | import assert from "assert";
|
10 | 13 | import { spawn } from "child_process";
|
11 | 14 | import express from "express";
|
@@ -35,65 +38,12 @@ import sanitize from "sanitize-filename";
|
35 | 38 | import { createHash } from "node:crypto";
|
36 | 39 | import cron from "node-cron";
|
37 | 40 | import { RWLock, combine } from "readers-writer-lock";
|
38 |
| -import which from "which"; |
39 | 41 |
|
40 | 42 | if (process.env.NODE_ENV !== "production") {
|
41 | 43 | // Load any .env file config
|
42 | 44 | dotenv.config();
|
43 | 45 | }
|
44 | 46 |
|
45 |
| -/// Return the command string to execute to run vg. |
46 |
| -/// Checks config.vgPath. |
47 |
| -/// An entry of "" in config.vgPath means to check PATH. |
48 |
| -function find_vg() { |
49 |
| - if (find_vg.found_vg !== null) { |
50 |
| - // Cache the answer and don't re-check all the time. |
51 |
| - // Nobody shoudl be deleting vg. |
52 |
| - return find_vg.found_vg; |
53 |
| - } |
54 |
| - for (let prefix of config.vgPath) { |
55 |
| - if (prefix === "") { |
56 |
| - // Empty string has special meaning of "use PATH". |
57 |
| - console.log("Check for vg on PATH"); |
58 |
| - try { |
59 |
| - find_vg.found_vg = which.sync("vg"); |
60 |
| - console.log("Found vg at:", find_vg.found_vg); |
61 |
| - return find_vg.found_vg; |
62 |
| - } catch (e) { |
63 |
| - // vg is not on PATH |
64 |
| - continue; |
65 |
| - } |
66 |
| - } |
67 |
| - if (prefix.length > 0 && prefix[prefix.length - 1] !== "/") { |
68 |
| - // Add trailing slash |
69 |
| - prefix = prefix + "/"; |
70 |
| - } |
71 |
| - let vg_filename = prefix + "vg"; |
72 |
| - console.log("Check for vg at:", vg_filename); |
73 |
| - if (fs.existsSync(vg_filename)) { |
74 |
| - if (!fs.statSync(vg_filename).isFile()) { |
75 |
| - // This is a directory or something, not a binary we can run. |
76 |
| - continue; |
77 |
| - } |
78 |
| - try { |
79 |
| - // Pretend we will execute it |
80 |
| - fs.accessSync(vg_filename, fs.constants.X_OK) |
81 |
| - } catch (e) { |
82 |
| - // Not executable |
83 |
| - continue; |
84 |
| - } |
85 |
| - // If we get here it is executable. |
86 |
| - find_vg.found_vg = vg_filename; |
87 |
| - console.log("Found vg at:", find_vg.found_vg); |
88 |
| - return find_vg.found_vg; |
89 |
| - } |
90 |
| - } |
91 |
| - // If we get here we don't see vg at all. |
92 |
| - throw new InternalServerError("The vg command was not found. Install vg to use the Sequence Tube Map: https://github.com/vgteam/vg?tab=readme-ov-file#installation"); |
93 |
| -} |
94 |
| -find_vg.found_vg = null; |
95 |
| - |
96 |
| - |
97 | 47 | const MOUNTED_DATA_PATH = config.dataPath;
|
98 | 48 | const INTERNAL_DATA_PATH = config.internalDataPath;
|
99 | 49 | // THis is where we will store uploaded files
|
@@ -1086,43 +1036,6 @@ function organizePathsTargetFirst(region, pathList) {
|
1086 | 1036 | }
|
1087 | 1037 | }
|
1088 | 1038 |
|
1089 |
| -// We can throw this error to trigger our error handling code instead of |
1090 |
| -// Express's default. It covers input validation failures, and vaguely-expected |
1091 |
| -// server-side errors we want to report in a controlled way (because they could |
1092 |
| -// be caused by bad user input to vg). |
1093 |
| -class TubeMapError extends Error { |
1094 |
| - constructor(message) { |
1095 |
| - super(message); |
1096 |
| - } |
1097 |
| -} |
1098 |
| - |
1099 |
| -// We can throw this error to make Express respond with a bad request error |
1100 |
| -// message. We should throw it whenever we detect that user input is |
1101 |
| -// unacceptable. |
1102 |
| -class BadRequestError extends TubeMapError { |
1103 |
| - constructor(message) { |
1104 |
| - super(message); |
1105 |
| - this.status = 400; |
1106 |
| - } |
1107 |
| -} |
1108 |
| - |
1109 |
| -// We can throw this error to make Express respond with an internal server |
1110 |
| -// error message |
1111 |
| -class InternalServerError extends TubeMapError { |
1112 |
| - constructor(message) { |
1113 |
| - super(message); |
1114 |
| - this.status = 500; |
1115 |
| - } |
1116 |
| -} |
1117 |
| - |
1118 |
| -// We can throw this error to make Express respond with an internal server |
1119 |
| -// error message about vg. |
1120 |
| -class VgExecutionError extends InternalServerError { |
1121 |
| - constructor(message) { |
1122 |
| - super(message); |
1123 |
| - } |
1124 |
| -} |
1125 |
| - |
1126 | 1039 | // We can use this middleware to ensure that errors we synchronously throw or
|
1127 | 1040 | // next(err) will be sent along to the user. It does *not* happen on API
|
1128 | 1041 | // endpoint promise rejections until Express 5.
|
|
0 commit comments