@roster-lock/match-agent 0.3.1 → 0.4.0
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/client/dist/assets/index-D2395vmC.css +1 -0
- package/client/dist/assets/index-DcKsa72V.js +125 -0
- package/client/dist/index.html +2 -2
- package/client/e2e/lib/env.ts +50 -0
- package/client/e2e/lib/fileDialog.ts +31 -0
- package/client/e2e/lib/mockGamepad.ts +71 -0
- package/client/e2e/lib/onscreenKeyboard.ts +32 -0
- package/client/e2e/playwright.config.ts +48 -0
- package/client/e2e/tests/gamepad-only.spec.ts +274 -0
- package/client/e2e/tests/keyboard-only.spec.ts +67 -0
- package/client/e2e/tsconfig.json +15 -0
- package/client/node_modules/.bin/playwright +17 -0
- package/client/node_modules/.bin/roster-lock-plugin-manager +17 -0
- package/client/node_modules/.vite/deps/@roster-lock_ts-client.js +2 -2
- package/client/node_modules/.vite/deps/@roster-lock_types.js +1 -1
- package/client/node_modules/.vite/deps/@roster-lock_utils.js +1 -1
- package/client/node_modules/.vite/deps/_metadata.json +18 -18
- package/client/node_modules/.vite/deps/{chunk-CKYX73LL.js → chunk-3HBL6DQ6.js} +1 -1
- package/client/node_modules/.vite/deps/{chunk-CKYX73LL.js.map → chunk-3HBL6DQ6.js.map} +2 -2
- package/client/node_modules/.vite/deps/{chunk-EDVVVQBR.js → chunk-QTQN6EZX.js} +2 -2
- package/client/node_modules/.vite/deps/chunk-QTQN6EZX.js.map +7 -0
- package/client/package.json +4 -1
- package/client/src/App.tsx +23 -14
- package/client/src/NavBar.tsx +15 -5
- package/client/src/api/matchAgent.ts +193 -5
- package/client/src/bridge/hostBridge.ts +15 -5
- package/client/src/components/FullScreenMenu.tsx +32 -0
- package/client/src/components/GameLauncherSettingsForm.tsx +80 -5
- package/client/src/components/InputHints.tsx +37 -0
- package/client/src/components/OnScreenFileDialog.tsx +83 -0
- package/client/src/components/OnScreenKeyboard.tsx +75 -0
- package/client/src/components/Selection/PieceCard.tsx +29 -4
- package/client/src/components/Selection/PiecePreviewPanel.tsx +48 -0
- package/client/src/components/Selection/PieceTypeSection.tsx +124 -46
- package/client/src/components/Selection/PlayerSelectionPanel.tsx +1 -5
- package/client/src/components/Selection/index.tsx +72 -47
- package/client/src/context/GlobalNavContext.tsx +340 -0
- package/client/src/context/SelectionSourceContext.tsx +26 -0
- package/client/src/hooks/useCursorInput.ts +66 -24
- package/client/src/hooks/useDownloadedPieceVersions.ts +39 -0
- package/client/src/hooks/useGridColumns.ts +29 -0
- package/client/src/hooks/useInView.ts +32 -0
- package/client/src/hooks/usePiecePreview.ts +124 -0
- package/client/src/pages/Controls/index.tsx +126 -0
- package/client/src/pages/Game/index.tsx +11 -15
- package/client/src/pages/MatchMaking/index.tsx +6 -3
- package/client/src/pages/PreviewRoster/index.tsx +118 -0
- package/client/src/styles/global.css +355 -6
- package/client/src/utils/inputLabels.ts +25 -0
- package/client/test-results/.last-run.json +4 -0
- package/client/tsconfig.tsbuildinfo +1 -1
- package/dist/config/index.js +2 -0
- package/dist/config/index.js.map +1 -1
- package/dist/config/inputBindings.js +40 -0
- package/dist/config/inputBindings.js.map +1 -0
- package/dist/config/manage.js +1 -0
- package/dist/config/manage.js.map +1 -1
- package/dist/handle-room/version-1/game-launcher/binaries.js +36 -0
- package/dist/handle-room/version-1/game-launcher/binaries.js.map +1 -0
- package/dist/handle-room/version-1/game-launcher/listing.js +24 -0
- package/dist/handle-room/version-1/game-launcher/listing.js.map +1 -0
- package/dist/handle-room/version-1/game-launcher/preview.js +38 -0
- package/dist/handle-room/version-1/game-launcher/preview.js.map +1 -0
- package/dist/handle-room/version-1/game-launcher/process.js +52 -0
- package/dist/handle-room/version-1/game-launcher/process.js.map +1 -0
- package/dist/handle-room/version-1/game-launcher/router.js +32 -0
- package/dist/handle-room/version-1/game-launcher/router.js.map +1 -0
- package/dist/handle-room/version-1/game-launcher/settings.js +49 -0
- package/dist/handle-room/version-1/game-launcher/settings.js.map +1 -0
- package/dist/handle-room/version-1/game-launcher/shared.js +28 -0
- package/dist/handle-room/version-1/game-launcher/shared.js.map +1 -0
- package/dist/handle-room/version-1/game-launcher/start.js +76 -0
- package/dist/handle-room/version-1/game-launcher/start.js.map +1 -0
- package/dist/handle-room/version-1/game-launcher/version.js +63 -0
- package/dist/handle-room/version-1/game-launcher/version.js.map +1 -0
- package/dist/handle-room/version-1/globals/FolderDB/SQLFolderDB/ensurePieceExists.js +22 -9
- package/dist/handle-room/version-1/globals/FolderDB/SQLFolderDB/ensurePieceExists.js.map +1 -1
- package/dist/handle-room/version-1/globals/FolderDB/SQLFolderDB/schema.js +50 -1
- package/dist/handle-room/version-1/globals/FolderDB/SQLFolderDB/schema.js.map +1 -1
- package/dist/handle-room/version-1/index.js +15 -16
- package/dist/handle-room/version-1/index.js.map +1 -1
- package/dist/handle-room/version-1/input-bindings.js +22 -0
- package/dist/handle-room/version-1/input-bindings.js.map +1 -0
- package/dist/handle-room/version-1/room-handler-bridge/exchange-and-download-selections.js +2 -1
- package/dist/handle-room/version-1/room-handler-bridge/exchange-and-download-selections.js.map +1 -1
- package/dist/handle-room/version-1/room-handler-bridge/http-handler.js +1 -1
- package/dist/handle-room/version-1/room-handler-bridge/http-handler.js.map +1 -1
- package/dist/handle-room/version-1/room-handler-bridge/steps.js +2 -1
- package/dist/handle-room/version-1/room-handler-bridge/steps.js.map +1 -1
- package/dist/handle-room/version-1/room-handler-bridge/ws-handler.js +2 -2
- package/dist/handle-room/version-1/room-handler-bridge/ws-handler.js.map +1 -1
- package/dist/index.js +18 -4
- package/dist/index.js.map +1 -1
- package/dist/util-routers/file-system/env.js +3 -0
- package/dist/util-routers/file-system/env.js.map +1 -0
- package/dist/util-routers/file-system/index.js +31 -0
- package/dist/util-routers/file-system/index.js.map +1 -0
- package/dist/util-routers/file-system/list.js +50 -0
- package/dist/util-routers/file-system/list.js.map +1 -0
- package/dist/util-routers/file-system/resolve-within-root.js +18 -0
- package/dist/util-routers/file-system/resolve-within-root.js.map +1 -0
- package/dist/util-routers/file-system/roster-lock.js +56 -0
- package/dist/util-routers/file-system/roster-lock.js.map +1 -0
- package/dist/utils/pick-file.js +82 -0
- package/dist/utils/pick-file.js.map +1 -0
- package/dist/utils/pick-folder.js +84 -0
- package/dist/utils/pick-folder.js.map +1 -0
- package/package.json +9 -8
- package/src/config/index.ts +8 -3
- package/src/config/inputBindings.ts +56 -0
- package/src/config/manage.ts +7 -0
- package/src/handle-room/version-1/game-launcher/binaries.ts +52 -0
- package/src/handle-room/version-1/game-launcher/listing.ts +36 -0
- package/src/handle-room/version-1/game-launcher/preview.ts +54 -0
- package/src/handle-room/version-1/game-launcher/process.ts +75 -0
- package/src/handle-room/version-1/game-launcher/router.ts +42 -0
- package/src/handle-room/version-1/game-launcher/settings.ts +58 -0
- package/src/handle-room/version-1/game-launcher/shared.ts +34 -0
- package/src/handle-room/version-1/game-launcher/start.ts +113 -0
- package/src/handle-room/version-1/game-launcher/version.ts +85 -0
- package/src/handle-room/version-1/globals/FolderDB/SQLFolderDB/ensurePieceExists.ts +39 -14
- package/src/handle-room/version-1/globals/FolderDB/SQLFolderDB/schema.ts +83 -1
- package/src/handle-room/version-1/globals/FolderDB/types.ts +24 -0
- package/src/handle-room/version-1/globals/types.ts +24 -2
- package/src/handle-room/version-1/index.ts +27 -26
- package/src/handle-room/version-1/input-bindings.ts +19 -0
- package/src/handle-room/version-1/room-handler-bridge/exchange-and-download-selections.ts +4 -1
- package/src/handle-room/version-1/room-handler-bridge/http-handler.ts +4 -1
- package/src/handle-room/version-1/room-handler-bridge/steps.ts +8 -0
- package/src/handle-room/version-1/room-handler-bridge/ws-handler.ts +2 -2
- package/src/index.ts +42 -10
- package/src/util-routers/file-system/env.ts +7 -0
- package/src/util-routers/file-system/index.ts +23 -0
- package/src/util-routers/file-system/list.ts +62 -0
- package/src/util-routers/file-system/resolve-within-root.ts +25 -0
- package/src/util-routers/file-system/roster-lock.ts +74 -0
- package/src/utils/pick-file.ts +81 -0
- package/src/utils/pick-folder.ts +92 -0
- package/tests/integration/filesystem.test.ts +117 -0
- package/tests/integration/game-launcher-binaries.test.ts +263 -0
- package/tests/integration/game-launcher-game-complete.test.ts +111 -0
- package/tests/integration/game-launcher-preview.test.ts +107 -0
- package/tests/integration/game-launcher-settings.test.ts +123 -8
- package/tests/integration/game-processes-ws.test.ts +122 -0
- package/tests/integration/helpers/fakeFolderDB.ts +16 -0
- package/tests/integration/helpers/fakePluginRuntime.ts +10 -1
- package/tests/integration/helpers/server.ts +23 -3
- package/tests/integration/input-bindings.test.ts +94 -0
- package/tests/integration/piece-files.test.ts +1 -1
- package/tests/integration/roster-lock.test.ts +86 -0
- package/vitest.config.ts +7 -1
- package/client/dist/assets/index-CX6eCHuc.js +0 -125
- package/client/dist/assets/index-gxAuIfGQ.css +0 -1
- package/client/node_modules/.vite/deps/chunk-EDVVVQBR.js.map +0 -7
- package/dist/handle-room/version-1/game-launcher.js +0 -197
- package/dist/handle-room/version-1/game-launcher.js.map +0 -1
- package/src/handle-room/version-1/game-launcher.ts +0 -287
package/src/index.ts
CHANGED
|
@@ -15,6 +15,7 @@ import { createDevClientHandler, createStaticClientHandler } from "./serve-clien
|
|
|
15
15
|
import { program } from "commander";
|
|
16
16
|
import { createV1Routers } from "./handle-room/version-1";
|
|
17
17
|
import { getSQLite3FolderDB } from "./handle-room/version-1/globals/FolderDB";
|
|
18
|
+
import { createFileSystemRouter } from "./util-routers/file-system";
|
|
18
19
|
import {
|
|
19
20
|
PluginManager, createPluginCommands, syncPluginsToOfficialManifest, DEFAULT_REPO_URL, manifestUrlFromRepo,
|
|
20
21
|
} from "@roster-lock/plugin-runtime";
|
|
@@ -51,6 +52,10 @@ program
|
|
|
51
52
|
.option("--auth-code <string>", "authentication code required for access (overrides the config file)")
|
|
52
53
|
.option("--piece-folder <path>", "folder to store downloaded pieces in (overrides the config file)")
|
|
53
54
|
.option("--plugin-folder <path>", "folder to load plugins from (overrides the config file)")
|
|
55
|
+
.option(
|
|
56
|
+
"--roster-lock-folder <path>",
|
|
57
|
+
"folder match-agent-client's roster-lock preview page may browse/read from (overrides the config file)"
|
|
58
|
+
)
|
|
54
59
|
.option(
|
|
55
60
|
"--config-file <path>",
|
|
56
61
|
"config file to read/persist the auth code and folders from " +
|
|
@@ -62,7 +67,8 @@ program
|
|
|
62
67
|
false
|
|
63
68
|
)
|
|
64
69
|
.action(async (options: {
|
|
65
|
-
port: string, authCode?: string, pieceFolder?: string, pluginFolder?: string,
|
|
70
|
+
port: string, authCode?: string, pieceFolder?: string, pluginFolder?: string, rosterLockFolder?: string,
|
|
71
|
+
configFile?: string, dev: boolean,
|
|
66
72
|
}) => {
|
|
67
73
|
const port = Number(options.port);
|
|
68
74
|
if (!Number.isInteger(port) || port < 0 || port > 65535) {
|
|
@@ -76,21 +82,26 @@ program
|
|
|
76
82
|
const resolvedFolders = resolveConfigFolders(configFilePath, existingConfig ?? {});
|
|
77
83
|
const pieceFolder = options.pieceFolder ? pathResolve(options.pieceFolder) : resolvedFolders.pieceFolder;
|
|
78
84
|
const pluginFolder = options.pluginFolder ? pathResolve(options.pluginFolder) : resolvedFolders.pluginFolder;
|
|
85
|
+
const rosterLockFolder = options.rosterLockFolder
|
|
86
|
+
? pathResolve(options.rosterLockFolder) : resolvedFolders.rosterLockFolder;
|
|
79
87
|
|
|
80
88
|
// Persist so the auth code stays stable across runs (and a first run
|
|
81
89
|
// against a fresh config writes one out) - keep the config's own
|
|
82
|
-
// relative pieceFolder/pluginFolder untouched so a CLI
|
|
83
|
-
// run doesn't get baked in as this config's permanent
|
|
90
|
+
// relative pieceFolder/pluginFolder/rosterLockFolder untouched so a CLI
|
|
91
|
+
// override on this run doesn't get baked in as this config's permanent
|
|
92
|
+
// setting.
|
|
84
93
|
await setConfig(configFilePath, {
|
|
85
94
|
authCode,
|
|
86
95
|
pieceFolder: existingConfig?.pieceFolder,
|
|
87
96
|
pluginFolder: existingConfig?.pluginFolder,
|
|
97
|
+
rosterLockFolder: existingConfig?.rosterLockFolder,
|
|
88
98
|
});
|
|
89
99
|
|
|
90
100
|
await mkdir(pieceFolder, { recursive: true });
|
|
91
101
|
await mkdir(pluginFolder, { recursive: true });
|
|
102
|
+
await mkdir(rosterLockFolder, { recursive: true });
|
|
92
103
|
|
|
93
|
-
startServer(port, authCode, pluginFolder, pieceFolder, { dev: options.dev });
|
|
104
|
+
startServer(port, authCode, pluginFolder, pieceFolder, rosterLockFolder, configFilePath, { dev: options.dev });
|
|
94
105
|
});
|
|
95
106
|
|
|
96
107
|
program
|
|
@@ -102,6 +113,10 @@ program
|
|
|
102
113
|
)
|
|
103
114
|
.option("--piece-folder <name>", "name of the pieces folder to create, relative to <path>", "pieces")
|
|
104
115
|
.option("--plugin-folder <name>", "name of the plugins folder to create, relative to <path>", "plugins")
|
|
116
|
+
.option(
|
|
117
|
+
"--roster-lock-folder <name>",
|
|
118
|
+
"name of the roster-lock preview folder to create, relative to <path>", "roster-locks"
|
|
119
|
+
)
|
|
105
120
|
.option("--auth-code <string>", "auth code to store in the config (a random one is generated if omitted)")
|
|
106
121
|
.option("--force", "overwrite an existing config at <path>, generating a new auth code unless --auth-code is given", false)
|
|
107
122
|
.option(
|
|
@@ -109,7 +124,8 @@ program
|
|
|
109
124
|
"if given, sync plugins to match the official manifest at this URL right after mounting"
|
|
110
125
|
)
|
|
111
126
|
.action(async (targetPath: string, options: {
|
|
112
|
-
pieceFolder: string, pluginFolder: string, authCode?: string, force: boolean,
|
|
127
|
+
pieceFolder: string, pluginFolder: string, rosterLockFolder: string, authCode?: string, force: boolean,
|
|
128
|
+
manifestUrl?: string,
|
|
113
129
|
}) => {
|
|
114
130
|
const mountFolder = pathResolve(targetPath);
|
|
115
131
|
await mkdir(mountFolder, { recursive: true });
|
|
@@ -123,12 +139,14 @@ program
|
|
|
123
139
|
const pluginFolder = pathJoin(mountFolder, options.pluginFolder);
|
|
124
140
|
await mkdir(pathJoin(mountFolder, options.pieceFolder), { recursive: true });
|
|
125
141
|
await mkdir(pluginFolder, { recursive: true });
|
|
142
|
+
await mkdir(pathJoin(mountFolder, options.rosterLockFolder), { recursive: true });
|
|
126
143
|
|
|
127
144
|
const authCode = options.authCode || existingConfig?.authCode || generateAuthCode();
|
|
128
145
|
await setConfig(configFilePath, {
|
|
129
146
|
authCode,
|
|
130
147
|
pieceFolder: options.pieceFolder,
|
|
131
148
|
pluginFolder: options.pluginFolder,
|
|
149
|
+
rosterLockFolder: options.rosterLockFolder,
|
|
132
150
|
});
|
|
133
151
|
|
|
134
152
|
console.log(`Wrote ${configFilePath}`);
|
|
@@ -154,10 +172,15 @@ program
|
|
|
154
172
|
)
|
|
155
173
|
.option("--piece-folder <name>", "name of the pieces folder to create, relative to the config file", "pieces")
|
|
156
174
|
.option("--plugin-folder <name>", "name of the plugins folder to create, relative to the config file", "plugins")
|
|
175
|
+
.option(
|
|
176
|
+
"--roster-lock-folder <name>",
|
|
177
|
+
"name of the roster-lock preview folder to create, relative to the config file", "roster-locks"
|
|
178
|
+
)
|
|
157
179
|
.option("--force", "overwrite an existing config at the target location", false)
|
|
158
180
|
.option("--manifest-url <url>", "URL of the official plugin manifest to sync plugins against", manifestUrlFromRepo(DEFAULT_REPO_URL))
|
|
159
181
|
.action(async (authCode: string, options: {
|
|
160
|
-
configFile?: string, pieceFolder: string, pluginFolder: string,
|
|
182
|
+
configFile?: string, pieceFolder: string, pluginFolder: string, rosterLockFolder: string, force: boolean,
|
|
183
|
+
manifestUrl: string,
|
|
161
184
|
}) => {
|
|
162
185
|
const configFilePath = options.configFile ? pathResolve(options.configFile) : DEFAULT_MATCH_CONIFG;
|
|
163
186
|
const existingConfig = (await configExists(configFilePath)) ? await getConfig(configFilePath) : null;
|
|
@@ -166,15 +189,17 @@ program
|
|
|
166
189
|
}
|
|
167
190
|
|
|
168
191
|
const resolvedFolders = resolveConfigFolders(configFilePath, {
|
|
169
|
-
pieceFolder: options.pieceFolder, pluginFolder: options.pluginFolder,
|
|
192
|
+
pieceFolder: options.pieceFolder, pluginFolder: options.pluginFolder, rosterLockFolder: options.rosterLockFolder,
|
|
170
193
|
});
|
|
171
194
|
await mkdir(resolvedFolders.pieceFolder, { recursive: true });
|
|
172
195
|
await mkdir(resolvedFolders.pluginFolder, { recursive: true });
|
|
196
|
+
await mkdir(resolvedFolders.rosterLockFolder, { recursive: true });
|
|
173
197
|
|
|
174
198
|
await setConfig(configFilePath, {
|
|
175
199
|
authCode,
|
|
176
200
|
pieceFolder: options.pieceFolder,
|
|
177
201
|
pluginFolder: options.pluginFolder,
|
|
202
|
+
rosterLockFolder: options.rosterLockFolder,
|
|
178
203
|
});
|
|
179
204
|
|
|
180
205
|
console.log(`Wrote ${configFilePath}`);
|
|
@@ -201,6 +226,7 @@ program
|
|
|
201
226
|
authCode,
|
|
202
227
|
pieceFolder: existingConfig?.pieceFolder,
|
|
203
228
|
pluginFolder: existingConfig?.pluginFolder,
|
|
229
|
+
rosterLockFolder: existingConfig?.rosterLockFolder,
|
|
204
230
|
});
|
|
205
231
|
console.log(`Updated auth code in ${configFilePath}`);
|
|
206
232
|
});
|
|
@@ -210,8 +236,8 @@ if (require.main === module) {
|
|
|
210
236
|
}
|
|
211
237
|
|
|
212
238
|
export async function startServer(
|
|
213
|
-
port: number, authCode: string, pluginFolder: string, piecesFolder: string,
|
|
214
|
-
options: { dev?: boolean } = {}
|
|
239
|
+
port: number, authCode: string, pluginFolder: string, piecesFolder: string, rosterLockFolder: string,
|
|
240
|
+
configFilePath: string, options: { dev?: boolean } = {}
|
|
215
241
|
){
|
|
216
242
|
const pluginRuntime = await PluginManager.create(pluginFolder);
|
|
217
243
|
const fileDB = getSQLite3FolderDB(piecesFolder, pluginRuntime);
|
|
@@ -227,7 +253,7 @@ export async function startServer(
|
|
|
227
253
|
server.httpRouter.post("/validate-authcode", validateAuthCode(authCode))
|
|
228
254
|
|
|
229
255
|
const { httpRouter: v1HttpRouter, wsRouter: v1WsRouter } = createV1Routers(
|
|
230
|
-
fileDB, pluginRuntime, { authCode, getPort: () => port }
|
|
256
|
+
fileDB, pluginRuntime, { authCode, getPort: () => port }, configFilePath
|
|
231
257
|
);
|
|
232
258
|
server.httpRouter.use("/v1", authMiddleware(authCode), v1HttpRouter);
|
|
233
259
|
server.wsRouter.use("/v1", authMiddleware(authCode), v1WsRouter);
|
|
@@ -236,6 +262,12 @@ export async function startServer(
|
|
|
236
262
|
// room protocol (/v1) can move to /v2 without dragging the editor API along.
|
|
237
263
|
server.httpRouter.use("/editor/v1", authMiddleware(authCode), createEditorV1Router(pluginRuntime, fileDB));
|
|
238
264
|
|
|
265
|
+
// Outside /v1 entirely, same reasoning as /editor/v1 - this doesn't
|
|
266
|
+
// touch the room protocol at all, and is bounded to rosterLockFolder
|
|
267
|
+
// (see util-routers/file-system/resolve-within-root.ts), not anything
|
|
268
|
+
// else match-agent's host filesystem happens to have.
|
|
269
|
+
server.httpRouter.use("/util/file-system", authMiddleware(authCode), createFileSystemRouter(rosterLockFolder));
|
|
270
|
+
|
|
239
271
|
// Mounted last so it only ever sees requests the routes above didn't
|
|
240
272
|
// claim - match-agent-client stays Vite's, this just puts it behind the
|
|
241
273
|
// same origin/port as the API instead of a separately-run `vite dev`.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Deliberately not V1Env - this router lives outside the room protocol
|
|
2
|
+
// entirely (see util-routers/file-system/index.ts) and only ever needs
|
|
3
|
+
// the one bounded folder it's allowed to touch, not fileDB/pluginRuntime/
|
|
4
|
+
// processHandles or anything else the room protocol carries.
|
|
5
|
+
export type FileSystemEnv = {
|
|
6
|
+
rosterLockFolder: string,
|
|
7
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { HTTPRouter } from "../../utils/http-router";
|
|
2
|
+
import { FileSystemEnv } from "./env";
|
|
3
|
+
import { listDirectoryRoute } from "./list";
|
|
4
|
+
import { readRosterLock, pickRosterLockFile } from "./roster-lock";
|
|
5
|
+
|
|
6
|
+
export * from "./env";
|
|
7
|
+
export * from "./list";
|
|
8
|
+
|
|
9
|
+
// Mounted directly in src/index.ts's startServer, outside /v1 entirely -
|
|
10
|
+
// unlike handle-room/version-1's routes, these don't touch the room
|
|
11
|
+
// protocol (fileDB/pluginRuntime/processHandles) at all, and bounding them
|
|
12
|
+
// to one configured folder (rosterLockFolder - see
|
|
13
|
+
// resolve-within-root.ts) is a property worth keeping visible at the
|
|
14
|
+
// mount site, not buried inside the same router as everything else v1 can
|
|
15
|
+
// already do.
|
|
16
|
+
export function createFileSystemRouter(rosterLockFolder: string): HTTPRouter {
|
|
17
|
+
const env: FileSystemEnv = { rosterLockFolder };
|
|
18
|
+
const router = new HTTPRouter();
|
|
19
|
+
router.get("/list", listDirectoryRoute.bind(env));
|
|
20
|
+
router.get("/roster-lock", readRosterLock.bind(env));
|
|
21
|
+
router.post("/roster-lock/pick", pickRosterLockFile.bind(env));
|
|
22
|
+
return router;
|
|
23
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { readdir, stat } from "node:fs/promises";
|
|
2
|
+
import { join as pathJoin, dirname } from "node:path";
|
|
3
|
+
import { HTTPRequestHandler, HTTPError } from "../../utils/http-router";
|
|
4
|
+
import { resolveWithinRoot } from "./resolve-within-root";
|
|
5
|
+
import { FileSystemEnv } from "./env";
|
|
6
|
+
|
|
7
|
+
export type DirectoryEntry = { name: string, path: string, isDirectory: boolean };
|
|
8
|
+
export type DirectoryListing = { path: string, parent: string | null, entries: Array<DirectoryEntry> };
|
|
9
|
+
|
|
10
|
+
// Backs OnScreenFileDialog (core/match-agent/client/src/components/) - a
|
|
11
|
+
// file picker rendered in the page itself, navigable through the same
|
|
12
|
+
// Move/Select/Back scheme as everything else (GlobalNavContext), unlike a
|
|
13
|
+
// native OS file dialog, which is a window outside the browser's DOM
|
|
14
|
+
// entirely and so has no way for a gamepad to reach it at all. Bounded to
|
|
15
|
+
// this.rosterLockFolder - see resolve-within-root.ts for why.
|
|
16
|
+
export const listDirectoryRoute: HTTPRequestHandler = async function(this: FileSystemEnv, { res }, routeInfo){
|
|
17
|
+
const requestedPath = resolveWithinRoot(this.rosterLockFolder, routeInfo.url.searchParams.get("path"));
|
|
18
|
+
const extensionFilter = routeInfo.url.searchParams.get("extension")?.toLowerCase() || undefined;
|
|
19
|
+
|
|
20
|
+
let names: Array<string>;
|
|
21
|
+
try {
|
|
22
|
+
names = await readdir(requestedPath);
|
|
23
|
+
} catch(e){
|
|
24
|
+
const code = (e as NodeJS.ErrnoException).code;
|
|
25
|
+
if(code === "ENOENT") throw new HTTPError(404, `No folder at ${requestedPath}`);
|
|
26
|
+
if(code === "ENOTDIR") throw new HTTPError(400, `${requestedPath} is a file, not a folder`);
|
|
27
|
+
throw new HTTPError(400, `Could not list ${requestedPath}: ${(e as Error).message}`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const entries: Array<DirectoryEntry> = [];
|
|
31
|
+
for(const name of names){
|
|
32
|
+
// Dotfiles/dotfolders excluded - not something a roster-lock would
|
|
33
|
+
// ever be, and it keeps the listing from being dominated by them.
|
|
34
|
+
if(name.startsWith(".")) continue;
|
|
35
|
+
|
|
36
|
+
const entryPath = pathJoin(requestedPath, name);
|
|
37
|
+
let isDirectory: boolean;
|
|
38
|
+
try {
|
|
39
|
+
isDirectory = (await stat(entryPath)).isDirectory();
|
|
40
|
+
} catch {
|
|
41
|
+
continue; // Broken symlink, permission error, etc - just skip it.
|
|
42
|
+
}
|
|
43
|
+
if(!isDirectory && extensionFilter && !name.toLowerCase().endsWith(extensionFilter)) continue;
|
|
44
|
+
entries.push({ name, path: entryPath, isDirectory });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Directories first (so a folder full of matching files doesn't bury
|
|
48
|
+
// the way to navigate deeper), each group alphabetical.
|
|
49
|
+
entries.sort((a, b) => {
|
|
50
|
+
if(a.isDirectory !== b.isDirectory) return a.isDirectory ? -1 : 1;
|
|
51
|
+
return a.name.localeCompare(b.name);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// null once *at* the configured root, not just at the OS filesystem root
|
|
55
|
+
// - resolveWithinRoot would reject a parent listing above it anyway, so
|
|
56
|
+
// there's no point offering a ".." that can only ever 403.
|
|
57
|
+
const resolvedRoot = resolveWithinRoot(this.rosterLockFolder, null);
|
|
58
|
+
const parent = requestedPath === resolvedRoot ? null : dirname(requestedPath);
|
|
59
|
+
|
|
60
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
61
|
+
res.end(JSON.stringify({ path: requestedPath, parent, entries } satisfies DirectoryListing));
|
|
62
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { resolve as pathResolve, relative as pathRelative, isAbsolute } from "node:path";
|
|
2
|
+
import { HTTPError } from "../../utils/http-router";
|
|
3
|
+
|
|
4
|
+
// Resolves `requestedPath` (defaulting to `root` itself when omitted) and
|
|
5
|
+
// verifies it's actually inside `root` - rejects anything that escapes it
|
|
6
|
+
// (a "../" climb, or an unrelated absolute path entirely) with 403, rather
|
|
7
|
+
// than letting a client list/read anything else match-agent's host
|
|
8
|
+
// filesystem happens to have. This is the actual fix for the exposure a
|
|
9
|
+
// bare "list any path, read any file" API would otherwise be: the native
|
|
10
|
+
// OS dialog this feature replaces could only ever hand back one path
|
|
11
|
+
// picked through a trusted OS UI, never enumerate a filesystem
|
|
12
|
+
// programmatically - a bounded root is what restores an equivalent limit
|
|
13
|
+
// to an HTTP API that has no such built-in ceiling on its own.
|
|
14
|
+
export function resolveWithinRoot(root: string, requestedPath: string | null): string {
|
|
15
|
+
const resolvedRoot = pathResolve(root);
|
|
16
|
+
if (!requestedPath) return resolvedRoot;
|
|
17
|
+
|
|
18
|
+
const resolvedPath = pathResolve(requestedPath);
|
|
19
|
+
const rel = pathRelative(resolvedRoot, resolvedPath);
|
|
20
|
+
const isInside = rel === "" || (!rel.startsWith("..") && !isAbsolute(rel));
|
|
21
|
+
if (!isInside) {
|
|
22
|
+
throw new HTTPError(403, `${requestedPath} is outside the configured roster-lock folder`);
|
|
23
|
+
}
|
|
24
|
+
return resolvedPath;
|
|
25
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { HTTPRequestHandler, HTTPError, jsonBody } from "../../utils/http-router";
|
|
3
|
+
import { pickFile, NoFilePickerAvailable } from "../../utils/pick-file";
|
|
4
|
+
import { resolveWithinRoot } from "./resolve-within-root";
|
|
5
|
+
import { FileSystemEnv } from "./env";
|
|
6
|
+
|
|
7
|
+
// Opens a native OS file-picker on match-agent's own host, same reasoning
|
|
8
|
+
// as game-launcher/settings.ts's pickGameLauncherBinaryLocation: a browser
|
|
9
|
+
// <input type="file"> can report a filename but never an absolute
|
|
10
|
+
// filesystem path. Left in place as a convenience for keyboard/mouse
|
|
11
|
+
// players even though pages/PreviewRoster itself now defaults to
|
|
12
|
+
// OnScreenFileDialog (gamepad-reachable, which a native dialog never can
|
|
13
|
+
// be - see docs/decisions, or just GlobalNavContext's own comments).
|
|
14
|
+
// Whatever path this returns still has to pass readRosterLock's own
|
|
15
|
+
// resolveWithinRoot check to actually be read, same as a manually-typed
|
|
16
|
+
// path would.
|
|
17
|
+
export const pickRosterLockFile: HTTPRequestHandler = async function(this: FileSystemEnv, { req, res }){
|
|
18
|
+
const body = await jsonBody(req).catch(() => ({}));
|
|
19
|
+
const startPath = typeof body.startPath === "string" ? body.startPath : this.rosterLockFolder;
|
|
20
|
+
|
|
21
|
+
let path: string | null;
|
|
22
|
+
try {
|
|
23
|
+
path = await pickFile("Roster Lock", ".roster-lock.json", startPath);
|
|
24
|
+
} catch(e){
|
|
25
|
+
if(e instanceof NoFilePickerAvailable) throw new HTTPError(400, e.message);
|
|
26
|
+
throw e;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
30
|
+
res.end(JSON.stringify(path === null ? { cancelled: true } : { path }));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Reads+parses a roster-lock config straight off match-agent's own host
|
|
34
|
+
// disk, for pages/PreviewRoster to open a SelectionBoard against without
|
|
35
|
+
// any matchmaker/room involved at all - the only other way SelectionBoard
|
|
36
|
+
// ever gets a rosterConfig today is via a live bridge.requestSelection
|
|
37
|
+
// (see MatchMakingPage's pendingLightbox). Bounded to this.rosterLockFolder
|
|
38
|
+
// - see resolve-within-root.ts for why.
|
|
39
|
+
export const readRosterLock: HTTPRequestHandler = async function(this: FileSystemEnv, { res }, routeInfo){
|
|
40
|
+
const requestedPath = routeInfo.url.searchParams.get("path");
|
|
41
|
+
if(!requestedPath) throw new HTTPError(400, "Missing path query parameter");
|
|
42
|
+
const path = resolveWithinRoot(this.rosterLockFolder, requestedPath);
|
|
43
|
+
|
|
44
|
+
let contents: string;
|
|
45
|
+
try {
|
|
46
|
+
contents = await readFile(path, "utf8");
|
|
47
|
+
} catch(e){
|
|
48
|
+
const code = (e as NodeJS.ErrnoException).code;
|
|
49
|
+
if(code === "ENOENT") throw new HTTPError(404, `No file at ${path}`);
|
|
50
|
+
if(code === "EISDIR") throw new HTTPError(400, `${path} is a directory, not a file`);
|
|
51
|
+
throw new HTTPError(400, `Could not read ${path}: ${(e as Error).message}`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
let parsed: unknown;
|
|
55
|
+
try {
|
|
56
|
+
parsed = JSON.parse(contents);
|
|
57
|
+
} catch(e){
|
|
58
|
+
throw new HTTPError(400, `${path} is not valid JSON: ${(e as Error).message}`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Light, boundary-only validation - just enough to catch "this is some
|
|
62
|
+
// other JSON file" early with a clear message, not a full schema check
|
|
63
|
+
// (there's no runtime validator for RosterLockV1Config in @roster-lock/types
|
|
64
|
+
// today - SelectionBoard itself is the real consumer of this shape).
|
|
65
|
+
if(
|
|
66
|
+
typeof parsed !== "object" || parsed === null
|
|
67
|
+
|| !("engine" in parsed) || !("rosters" in parsed)
|
|
68
|
+
){
|
|
69
|
+
throw new HTTPError(400, `${path} doesn't look like a roster-lock config (missing engine/rosters)`);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
73
|
+
res.end(JSON.stringify(parsed));
|
|
74
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { runCommand } from "./pick-folder";
|
|
2
|
+
|
|
3
|
+
// Native OS file-picker, spawned from match-agent itself rather than the
|
|
4
|
+
// browser - same reasoning as pick-folder.ts's NoFolderPickerAvailable:
|
|
5
|
+
// this only works when match-agent's own process has a display to show the
|
|
6
|
+
// dialog on, so a headless/remote match-agent should fail fast here rather
|
|
7
|
+
// than hang on a dialog nobody can see.
|
|
8
|
+
export class NoFilePickerAvailable extends Error {}
|
|
9
|
+
|
|
10
|
+
async function pickFileLinux(filterName: string, extension: string, startPath?: string): Promise<string | null> {
|
|
11
|
+
const attempts: Array<[string, Array<string>]> = [
|
|
12
|
+
[
|
|
13
|
+
"zenity",
|
|
14
|
+
[
|
|
15
|
+
"--file-selection", `--file-filter=${filterName} | *${extension}`,
|
|
16
|
+
...(startPath ? [`--filename=${startPath}`] : []),
|
|
17
|
+
],
|
|
18
|
+
],
|
|
19
|
+
["kdialog", ["--getopenfilename", startPath || ".", `${filterName} (*${extension})`]],
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
for (const [command, args] of attempts) {
|
|
23
|
+
try {
|
|
24
|
+
const { code, stdout } = await runCommand(command, args);
|
|
25
|
+
if (code !== 0) return null;
|
|
26
|
+
return stdout.trim() || null;
|
|
27
|
+
} catch (e) {
|
|
28
|
+
if ((e as NodeJS.ErrnoException).code !== "ENOENT") throw e;
|
|
29
|
+
// Tool not installed - fall through to the next one.
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
throw new NoFilePickerAvailable("No file-picker (zenity/kdialog) found on this host");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function pickFileMac(extension: string, startPath?: string): Promise<string | null> {
|
|
36
|
+
const script = startPath
|
|
37
|
+
? `POSIX path of (choose file with prompt "Select file" default location (POSIX file "${startPath}") ` +
|
|
38
|
+
`of type {"${extension.replace(/^\./, "")}"})`
|
|
39
|
+
: `POSIX path of (choose file with prompt "Select file" of type {"${extension.replace(/^\./, "")}"})`;
|
|
40
|
+
try {
|
|
41
|
+
const { code, stdout } = await runCommand("osascript", ["-e", script]);
|
|
42
|
+
if (code !== 0) return null;
|
|
43
|
+
return stdout.trim() || null;
|
|
44
|
+
} catch (e) {
|
|
45
|
+
if ((e as NodeJS.ErrnoException).code === "ENOENT") {
|
|
46
|
+
throw new NoFilePickerAvailable("osascript not found on this host");
|
|
47
|
+
}
|
|
48
|
+
throw e;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function pickFileWindows(filterName: string, extension: string, startPath?: string): Promise<string | null> {
|
|
53
|
+
const script = `
|
|
54
|
+
Add-Type -AssemblyName System.Windows.Forms
|
|
55
|
+
$dialog = New-Object System.Windows.Forms.OpenFileDialog
|
|
56
|
+
$dialog.Filter = ${JSON.stringify(`${filterName} (*${extension})|*${extension}`)}
|
|
57
|
+
${startPath ? `$dialog.InitialDirectory = ${JSON.stringify(startPath)}` : ""}
|
|
58
|
+
if ($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
|
|
59
|
+
Write-Output $dialog.FileName
|
|
60
|
+
}
|
|
61
|
+
`;
|
|
62
|
+
try {
|
|
63
|
+
const { code, stdout } = await runCommand("powershell", ["-NoProfile", "-NonInteractive", "-Command", script]);
|
|
64
|
+
if (code !== 0) return null;
|
|
65
|
+
return stdout.trim() || null;
|
|
66
|
+
} catch (e) {
|
|
67
|
+
if ((e as NodeJS.ErrnoException).code === "ENOENT") {
|
|
68
|
+
throw new NoFilePickerAvailable("powershell not found on this host");
|
|
69
|
+
}
|
|
70
|
+
throw e;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Returns the picked absolute path, or null if the user cancelled the
|
|
75
|
+
// dialog. Throws NoFilePickerAvailable if this host has no way to show one
|
|
76
|
+
// at all (headless Linux with neither zenity nor kdialog, etc).
|
|
77
|
+
export function pickFile(filterName: string, extension: string, startPath?: string): Promise<string | null> {
|
|
78
|
+
if (process.platform === "darwin") return pickFileMac(extension, startPath);
|
|
79
|
+
if (process.platform === "win32") return pickFileWindows(filterName, extension, startPath);
|
|
80
|
+
return pickFileLinux(filterName, extension, startPath);
|
|
81
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
|
|
3
|
+
// Native OS folder-picker, spawned from match-agent itself rather than the
|
|
4
|
+
// browser - a plain <input> can never hand back an absolute filesystem path
|
|
5
|
+
// (browser security), and binaryLocation is specifically a folder (see
|
|
6
|
+
// docs/v2/binary-location.md), not a single file a file input could even
|
|
7
|
+
// approximate. This only works when match-agent's own process has a display
|
|
8
|
+
// to show the dialog on (the ordinary case: match-agent and the browser
|
|
9
|
+
// hitting it are on the same desktop machine) - a headless/remote match-agent
|
|
10
|
+
// should fail fast here so the caller falls back to manual text entry rather
|
|
11
|
+
// than hanging on a dialog nobody can see.
|
|
12
|
+
export class NoFolderPickerAvailable extends Error {}
|
|
13
|
+
|
|
14
|
+
export function runCommand(command: string, args: Array<string>): Promise<{ code: number | null, stdout: string }> {
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
16
|
+
const child = spawn(command, args);
|
|
17
|
+
let stdout = "";
|
|
18
|
+
child.stdout.on("data", (chunk) => { stdout += chunk; });
|
|
19
|
+
child.on("error", reject);
|
|
20
|
+
child.on("close", (code) => resolve({ code, stdout }));
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// zenity/kdialog exit 1 on cancel (empty stdout); osascript throws on
|
|
25
|
+
// cancel; the PowerShell script below prints nothing on cancel. All three
|
|
26
|
+
// are treated the same way: null means "cancelled", not an error.
|
|
27
|
+
async function pickFolderLinux(startPath?: string): Promise<string | null> {
|
|
28
|
+
const attempts: Array<[string, Array<string>]> = [
|
|
29
|
+
["zenity", ["--file-selection", "--directory", ...(startPath ? [`--filename=${startPath}/`] : [])]],
|
|
30
|
+
["kdialog", ["--getexistingdirectory", startPath || "."]],
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
for (const [command, args] of attempts) {
|
|
34
|
+
try {
|
|
35
|
+
const { code, stdout } = await runCommand(command, args);
|
|
36
|
+
if (code !== 0) return null;
|
|
37
|
+
return stdout.trim() || null;
|
|
38
|
+
} catch (e) {
|
|
39
|
+
if ((e as NodeJS.ErrnoException).code !== "ENOENT") throw e;
|
|
40
|
+
// Tool not installed - fall through to the next one.
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
throw new NoFolderPickerAvailable("No folder-picker (zenity/kdialog) found on this host");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function pickFolderMac(startPath?: string): Promise<string | null> {
|
|
47
|
+
const script = startPath
|
|
48
|
+
? `POSIX path of (choose folder with prompt "Select folder" default location (POSIX file "${startPath}"))`
|
|
49
|
+
: `POSIX path of (choose folder with prompt "Select folder")`;
|
|
50
|
+
try {
|
|
51
|
+
const { code, stdout } = await runCommand("osascript", ["-e", script]);
|
|
52
|
+
if (code !== 0) return null;
|
|
53
|
+
return stdout.trim() || null;
|
|
54
|
+
} catch (e) {
|
|
55
|
+
if ((e as NodeJS.ErrnoException).code === "ENOENT") {
|
|
56
|
+
throw new NoFolderPickerAvailable("osascript not found on this host");
|
|
57
|
+
}
|
|
58
|
+
throw e;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function pickFolderWindows(startPath?: string): Promise<string | null> {
|
|
63
|
+
// FolderBrowserDialog rather than a raw file dialog - matches what
|
|
64
|
+
// binaryLocation actually is. Printed path (if any) is the only stdout.
|
|
65
|
+
const script = `
|
|
66
|
+
Add-Type -AssemblyName System.Windows.Forms
|
|
67
|
+
$dialog = New-Object System.Windows.Forms.FolderBrowserDialog
|
|
68
|
+
${startPath ? `$dialog.SelectedPath = ${JSON.stringify(startPath)}` : ""}
|
|
69
|
+
if ($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
|
|
70
|
+
Write-Output $dialog.SelectedPath
|
|
71
|
+
}
|
|
72
|
+
`;
|
|
73
|
+
try {
|
|
74
|
+
const { code, stdout } = await runCommand("powershell", ["-NoProfile", "-NonInteractive", "-Command", script]);
|
|
75
|
+
if (code !== 0) return null;
|
|
76
|
+
return stdout.trim() || null;
|
|
77
|
+
} catch (e) {
|
|
78
|
+
if ((e as NodeJS.ErrnoException).code === "ENOENT") {
|
|
79
|
+
throw new NoFolderPickerAvailable("powershell not found on this host");
|
|
80
|
+
}
|
|
81
|
+
throw e;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Returns the picked absolute path, or null if the user cancelled the
|
|
86
|
+
// dialog. Throws NoFolderPickerAvailable if this host has no way to show
|
|
87
|
+
// one at all (headless Linux with neither zenity nor kdialog, etc).
|
|
88
|
+
export function pickFolder(startPath?: string): Promise<string | null> {
|
|
89
|
+
if (process.platform === "darwin") return pickFolderMac(startPath);
|
|
90
|
+
if (process.platform === "win32") return pickFolderWindows(startPath);
|
|
91
|
+
return pickFolderLinux(startPath);
|
|
92
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach } from "vitest";
|
|
2
|
+
import { writeFile, mkdir } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { startTestServer, TestServer } from "./helpers/server";
|
|
5
|
+
import { makeTempFolder, cleanupFolder } from "./helpers/piece";
|
|
6
|
+
|
|
7
|
+
describe("GET /util/file-system/list", () => {
|
|
8
|
+
const cleanups: Array<() => Promise<void> | void> = [];
|
|
9
|
+
afterEach(async () => {
|
|
10
|
+
while (cleanups.length) await cleanups.pop()!();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
async function setup(): Promise<{ server: TestServer, rosterLockFolder: string }> {
|
|
14
|
+
const folder = await makeTempFolder();
|
|
15
|
+
cleanups.push(() => cleanupFolder(folder));
|
|
16
|
+
const server = await startTestServer(folder);
|
|
17
|
+
cleanups.push(() => server.close());
|
|
18
|
+
return { server, rosterLockFolder: server.rosterLockFolder };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function auth(server: TestServer): HeadersInit {
|
|
22
|
+
return { Authorization: `Bearer ${server.authCode}` };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async function seedFolder(folder: string) {
|
|
26
|
+
await mkdir(join(folder, "sub-folder"));
|
|
27
|
+
await mkdir(join(folder, ".hidden-folder"));
|
|
28
|
+
await writeFile(join(folder, "a.roster-lock.json"), "{}");
|
|
29
|
+
await writeFile(join(folder, "z.roster-lock.json"), "{}");
|
|
30
|
+
await writeFile(join(folder, "notes.txt"), "hello");
|
|
31
|
+
await writeFile(join(folder, ".hidden-file"), "secret");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
it("lists directories first, then matching files, excluding dotfiles", async () => {
|
|
35
|
+
const { server, rosterLockFolder } = await setup();
|
|
36
|
+
await seedFolder(rosterLockFolder);
|
|
37
|
+
|
|
38
|
+
const res = await fetch(
|
|
39
|
+
`${server.httpUrl}/util/file-system/list?path=${encodeURIComponent(rosterLockFolder)}&extension=${encodeURIComponent(".roster-lock.json")}`,
|
|
40
|
+
{ headers: auth(server) }
|
|
41
|
+
);
|
|
42
|
+
expect(res.status).toBe(200);
|
|
43
|
+
const body = await res.json();
|
|
44
|
+
expect(body.path).toBe(rosterLockFolder);
|
|
45
|
+
expect(body.entries.map((e: { name: string }) => e.name)).toEqual([
|
|
46
|
+
"sub-folder", "a.roster-lock.json", "z.roster-lock.json",
|
|
47
|
+
]);
|
|
48
|
+
expect(body.entries.find((e: { name: string }) => e.name === "sub-folder").isDirectory).toBe(true);
|
|
49
|
+
expect(body.entries.find((e: { name: string }) => e.name === "a.roster-lock.json").isDirectory).toBe(false);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("includes all directories regardless of the extension filter, so navigation is never blocked", async () => {
|
|
53
|
+
const { server, rosterLockFolder } = await setup();
|
|
54
|
+
await mkdir(join(rosterLockFolder, "empty-folder"));
|
|
55
|
+
const res = await fetch(
|
|
56
|
+
`${server.httpUrl}/util/file-system/list?path=${encodeURIComponent(rosterLockFolder)}&extension=${encodeURIComponent(".roster-lock.json")}`,
|
|
57
|
+
{ headers: auth(server) }
|
|
58
|
+
);
|
|
59
|
+
const body = await res.json();
|
|
60
|
+
expect(body.entries.map((e: { name: string }) => e.name)).toContain("empty-folder");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("defaults to the configured root when no path is given", async () => {
|
|
64
|
+
const { server, rosterLockFolder } = await setup();
|
|
65
|
+
const res = await fetch(`${server.httpUrl}/util/file-system/list`, { headers: auth(server) });
|
|
66
|
+
expect(res.status).toBe(200);
|
|
67
|
+
expect((await res.json()).path).toBe(rosterLockFolder);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("reports the parent folder within the root, and null at the root itself", async () => {
|
|
71
|
+
const { server, rosterLockFolder } = await setup();
|
|
72
|
+
await mkdir(join(rosterLockFolder, "sub-folder"));
|
|
73
|
+
|
|
74
|
+
const subRes = await fetch(
|
|
75
|
+
`${server.httpUrl}/util/file-system/list?path=${encodeURIComponent(join(rosterLockFolder, "sub-folder"))}`,
|
|
76
|
+
{ headers: auth(server) }
|
|
77
|
+
);
|
|
78
|
+
expect((await subRes.json()).parent).toBe(rosterLockFolder);
|
|
79
|
+
|
|
80
|
+
const rootRes = await fetch(
|
|
81
|
+
`${server.httpUrl}/util/file-system/list?path=${encodeURIComponent(rosterLockFolder)}`, { headers: auth(server) }
|
|
82
|
+
);
|
|
83
|
+
expect((await rootRes.json()).parent).toBeNull();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// The actual fix for this feature's exposure - see
|
|
87
|
+
// src/util-routers/file-system/resolve-within-root.ts.
|
|
88
|
+
it("403s for a path outside the configured root", async () => {
|
|
89
|
+
const { server } = await setup();
|
|
90
|
+
const res = await fetch(`${server.httpUrl}/util/file-system/list?path=${encodeURIComponent("/etc")}`, { headers: auth(server) });
|
|
91
|
+
expect(res.status).toBe(403);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("403s for a path that climbs out of the configured root with ..", async () => {
|
|
95
|
+
const { server, rosterLockFolder } = await setup();
|
|
96
|
+
const escapee = join(rosterLockFolder, "..", "..");
|
|
97
|
+
const res = await fetch(`${server.httpUrl}/util/file-system/list?path=${encodeURIComponent(escapee)}`, { headers: auth(server) });
|
|
98
|
+
expect(res.status).toBe(403);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("404s for a path that doesn't exist, within the root", async () => {
|
|
102
|
+
const { server, rosterLockFolder } = await setup();
|
|
103
|
+
const res = await fetch(
|
|
104
|
+
`${server.httpUrl}/util/file-system/list?path=${encodeURIComponent(join(rosterLockFolder, "nope"))}`,
|
|
105
|
+
{ headers: auth(server) }
|
|
106
|
+
);
|
|
107
|
+
expect(res.status).toBe(404);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("400s when path points at a file, not a folder", async () => {
|
|
111
|
+
const { server, rosterLockFolder } = await setup();
|
|
112
|
+
const filePath = join(rosterLockFolder, "a-file.txt");
|
|
113
|
+
await writeFile(filePath, "hello");
|
|
114
|
+
const res = await fetch(`${server.httpUrl}/util/file-system/list?path=${encodeURIComponent(filePath)}`, { headers: auth(server) });
|
|
115
|
+
expect(res.status).toBe(400);
|
|
116
|
+
});
|
|
117
|
+
});
|