|
1 | | -import * as fs from 'fs/promises'; |
| 1 | +import * as fs from 'node:fs'; |
| 2 | +import * as readline from 'node:readline'; |
2 | 3 |
|
3 | 4 | import { decompress } from '@localbytes/localdeck-components/src/utils/compression'; |
4 | 5 | import { zPadEditor } from '@localbytes/localdeck-components/src/utils/PadCfg'; |
5 | 6 |
|
6 | 7 | export default defineEventHandler(async (event) => { |
7 | 8 | const { filesDir } = useRuntimeConfig(); |
8 | 9 | const { filename } = getQuery(event); |
9 | | - const content = await fs.readFile(`${filesDir}/${filename}`, 'utf8'); |
10 | | - const configMatch = content.match(/configurator\?config=(.*)/); |
| 10 | + const content = fs.createReadStream(`${filesDir}/${filename}`, 'utf8'); |
| 11 | + const rl = readline.createInterface({ input: content, crlfDelay: Infinity }); |
| 12 | + |
| 13 | + let configMatch = null; |
| 14 | + let matchName = null; |
| 15 | + let matchFriendly = null; |
| 16 | + |
| 17 | + for await (const line of rl) { |
| 18 | + configMatch ??= line.match(/localdeck-configurator\?config=(.*)/); |
| 19 | + matchName ??= line.match(/name: "?(.*)"?/); |
| 20 | + matchFriendly ??= line.match(/friendly_name: "?(.*)"?/); |
| 21 | + |
| 22 | + // noinspection PointlessBooleanExpressionJS |
| 23 | + if (configMatch) break; |
| 24 | + } |
| 25 | + |
11 | 26 | const configStr = configMatch ? decodeURIComponent(configMatch[1]) : null; |
12 | 27 |
|
13 | 28 | const config = configStr |
14 | 29 | ? decompress(configStr, zPadEditor) |
15 | 30 | : zPadEditor.parse({}); |
16 | 31 |
|
17 | | - const matchName = content.match(/name: (.*)/); |
18 | | - if (matchName) config.title = matchName[1]; |
19 | | - |
20 | | - const matchFriendly = content.match(/friendly_name: (.*)/); |
21 | | - if (matchFriendly) config.title = matchFriendly[1]; |
22 | | - |
| 32 | + config.title = matchFriendly?.[1] ?? matchName?.[1] ?? config.title ?? 'My LocalDeck'; |
23 | 33 | config.buttons ??= {}; |
24 | 34 |
|
25 | | - return { configStr, config, content }; |
| 35 | + return { configStr, config }; |
26 | 36 | }); |
0 commit comments