Skip to content

Commit

Permalink
Build optimization (#2809)
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanBluefox authored Nov 15, 2024
1 parent bca91e9 commit 182a0f2
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 45 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ The icons may not be reused in other projects without the proper flaticon licens
### **WORK IN PROGRESS**

- (@GermanBluefox) Some GUI packages were updated.
- (@GermanBluefox) Improved file viewer. Added icons viewer

### 7.3.1 (2024-11-14)

Expand Down
18 changes: 8 additions & 10 deletions packages/adapter-react-v5/src/Components/FileViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const styles: Record<string, React.CSSProperties> = {
};

export const EXTENSIONS = {
images: ['png', 'jpg', 'svg', 'jpeg', 'bmp', 'gif', 'apng', 'avif', 'webp'],
images: ['png', 'jpg', 'svg', 'jpeg', 'bmp', 'gif', 'apng', 'avif', 'webp', 'ico'],
code: ['js', 'json', 'json5', 'md'],
txt: ['log', 'txt', 'html', 'css', 'xml', 'ics'],
audio: ['mp3', 'wav', 'ogg', 'acc'],
Expand All @@ -55,7 +55,7 @@ export const EXTENSIONS = {

function bufferToBase64(buffer: Buffer, isFull?: boolean): string {
let binary = '';
const bytes = new Uint8Array(buffer);
const bytes = new Uint8Array((buffer as unknown as { data: number[]; type: 'Buffer' })?.data || buffer);
const len = bytes.byteLength;
for (let i = 0; i < len && (isFull || i < 50); i++) {
binary += String.fromCharCode(bytes[i]);
Expand Down Expand Up @@ -121,9 +121,9 @@ export class FileViewerClass extends Component<FileViewerProps, FileViewerState>

this.props.socket
.readFile(adapter, name)
.then((data: { data: Buffer; type: string } | { file: string; mimeType: string }) => {
let fileData = '';
if ((data as { file: string; mimeType: string }).file !== undefined) {
.then((data: { file: string | Buffer; mimeType: string }) => {
let fileData: string | Buffer = '';
if (data.file !== undefined) {
fileData = (data as { file: string; mimeType: string }).file;
}

Expand All @@ -132,20 +132,18 @@ export class FileViewerClass extends Component<FileViewerProps, FileViewerState>
ext: this.state.ext,
};
// try to detect valid extension
if ((data as { data: Buffer; type: string }).type === 'Buffer') {
if ((fileData as unknown as { data: Buffer; type: string }).type === 'Buffer') {
if (name.toLowerCase().endsWith('.json5')) {
newState.ext = 'json5';
newState.copyPossible = true;
try {
fileData = atob(bufferToBase64((data as { data: Buffer; type: string }).data, true));
fileData = atob(bufferToBase64(fileData as unknown as Buffer, true));
} catch {
console.error('Cannot convert base64 to string');
fileData = '';
}
} else {
const ext = Utils.detectMimeType(
bufferToBase64((data as { data: Buffer; type: string }).data),
);
const ext = Utils.detectMimeType(bufferToBase64(fileData as unknown as Buffer));
if (ext) {
newState.ext = ext;
newState.copyPossible = EXTENSIONS.code.includes(ext) || EXTENSIONS.txt.includes(ext);
Expand Down
6 changes: 6 additions & 0 deletions packages/admin/src-admin/src/components/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import React, { Component, type JSX } from 'react';

import AceEditor from 'react-ace';
import 'ace-builds/src-min-noconflict/mode-json';
import 'ace-builds/src-min-noconflict/mode-json5';
import 'ace-builds/src-min-noconflict/mode-xml';
import 'ace-builds/src-min-noconflict/mode-html';
import 'ace-builds/src-min-noconflict/worker-json';
import 'ace-builds/src-min-noconflict/worker-javascript';
import 'ace-builds/src-min-noconflict/worker-xml';
import 'ace-builds/src-min-noconflict/worker-html';
import 'ace-builds/src-min-noconflict/theme-clouds_midnight';
import 'ace-builds/src-min-noconflict/theme-chrome';
import 'ace-builds/src-min-noconflict/ext-language_tools';
Expand Down
44 changes: 34 additions & 10 deletions packages/admin/src/lib/checkLinuxPass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,33 @@ function checkLinuxPassword(login: string, password: string): Promise<boolean> {
try {
const su = spawn('su', [login]);
let result = false;
let responseTimeout = setTimeout(() => {
let responseTimeout: NodeJS.Timeout | null = setTimeout(() => {
responseTimeout = null;
su.kill();
}, 3000);

function _checkPassword(data: string): void {
if (!responseTimeout) {
return;
}
data = data.replace(/\r/g, ' ').replace(/\n/g, ' ').trim();
console.log(`[STDX] "${data}"`);
if (data.endsWith(':')) {
su.stdin.write(`${password}\n`);
try {
su.stdin?.write(`${password}\n`);
} catch {
return;
}
setTimeout(() => {
if (!responseTimeout) {
return;
}
console.log(`[LOG] write whoami`);
su.stdin.write(`whoami\n`);
try {
su.stdin?.write(`whoami\n`);
} catch {
// ignore
}
}, 50);
} else if (data === login) {
result = true;
Expand All @@ -55,21 +69,31 @@ function checkLinuxPassword(login: string, password: string): Promise<boolean> {
}

// Listen for data on stdout
su.stdout.on('data', data => _checkPassword(data.toString()));
su.stdout.on('data', data => {
if (data && responseTimeout) {
_checkPassword(data.toString());
}
});

// Listen for data on stderr
su.stderr.on('data', data => _checkPassword(data.toString()));
su.stderr.on('data', data => {
if (data && responseTimeout) {
_checkPassword(data.toString());
}
});

// Listen for the close event
su.on('close', () => {
console.log(`[LOG] -------- closed with result: ${result}\n`);
responseTimeout && clearTimeout(responseTimeout);
responseTimeout = null;
if (responseTimeout) {
clearTimeout(responseTimeout);
responseTimeout = null;
}
resolve(result);
});
} catch (e: any) {
console.error(`[LOG] -------- Error by execution: ${e.message}\n`);
reject(new Error(e));
} catch (e: unknown) {
console.error(`[LOG] -------- Error by execution: ${(e as Error).message}\n`);
reject(new Error(e as string));
}
});
}
Expand Down
56 changes: 32 additions & 24 deletions packages/admin/src/lib/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -791,35 +791,43 @@ class Web {
return;
}

this.adapter.sendToHost(`system.host.${host}`, 'getLogFile', { fileName, transport }, result => {
const _result = result as { error?: string; data?: string; size?: number; gz?: boolean };
if (!_result || _result.error) {
res.status(404).send(get404Page(`File ${escapeHtml(fileName)} not found`));
} else {
if (_result.gz) {
if (_result.size > 1024 * 1024) {
res.header('Content-Type', 'application/gzip');
res.send(_result.data);
} else {
try {
this.unzipFile(fileName, _result.data, res);
} catch (e) {
this.adapter.sendToHost(
`system.host.${host}`,
'getLogFile',
{ filename: fileName, transport },
result => {
const _result = result as { error?: string; data?: string; size?: number; gz?: boolean };
if (!_result || _result.error) {
if (_result.error) {
this.adapter.log.warn(`Cannot read log file ${fileName}: ${_result.error}`);
}
res.status(404).send(get404Page(`File ${escapeHtml(fileName)} not found`));
} else {
if (_result.gz) {
if (_result.size > 1024 * 1024) {
res.header('Content-Type', 'application/gzip');
res.send(_result.data);
this.adapter.log.error(`Cannot extract file ${fileName}: ${e}`);
} else {
try {
this.unzipFile(fileName, _result.data, res);
} catch (e) {
res.header('Content-Type', 'application/gzip');
res.send(_result.data);
this.adapter.log.error(`Cannot extract file ${fileName}: ${e}`);
}
}
} else if (_result.data === undefined || _result.data === null) {
res.status(404).send(get404Page(`File ${escapeHtml(fileName)} not found`));
} else if (_result.size > 2 * 1024 * 1024) {
res.header('Content-Type', 'text/plain');
res.send(_result.data);
} else {
res.header('Content-Type', 'text/html');
res.send(this.decorateLogFile(fileName, _result.data));
}
} else if (_result.data === undefined || _result.data === null) {
res.status(404).send(get404Page(`File ${escapeHtml(fileName)} not found`));
} else if (_result.size > 2 * 1024 * 1024) {
res.header('Content-Type', 'text/plain');
res.send(_result.data);
} else {
res.header('Content-Type', 'text/html');
res.send(this.decorateLogFile(fileName, _result.data));
}
}
});
},
);
} else {
parts = parts.splice(2);
const transport = parts.shift();
Expand Down
14 changes: 14 additions & 0 deletions packages/admin/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ function copyAllFiles() {
copyFiles(`${srcRx}build/static/js/**/*.js`, `${dest}static/js`, {
replace: [{ find: 's.p+"static/media', text: '"./static/media' }],
});
copyFiles(
[
`${srcRx}node_modules/ace-builds/src-min-noconflict/worker-json.js`,
`${srcRx}node_modules/ace-builds/src-min-noconflict/worker-html.js`,
`${srcRx}node_modules/ace-builds/src-min-noconflict/worker-xml.js`,
`${srcRx}node_modules/ace-builds/src-min-noconflict/worker-yaml.js`,
`${srcRx}node_modules/ace-builds/src-min-noconflict/worker-javascript.js`,
`${srcRx}node_modules/ace-builds/src-min-noconflict/worker-css.js`,
`${srcRx}node_modules/ace-builds/src-min-noconflict/snippets/html.js`,
`${srcRx}node_modules/ace-builds/src-min-noconflict/snippets/css.js`,
`${srcRx}node_modules/ace-builds/src-min-noconflict/snippets/javascript.js`,
],
`${dest}static/js`,
);
}

async function configCSS() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ export default class ConfigGeneric<
if (typeof part === 'string' && typeof data[part] === 'object') {
return ConfigGeneric.getValue(data[part], attr);
}
return null;
return undefined;
}

static setValue(data: Record<string, any>, attr: string | string[], value: any): void {
Expand Down

0 comments on commit 182a0f2

Please sign in to comment.