How mykhailoshes/scale-angine works
Purpose and runtime
Scale Engine is an Electron desktop gateway between Mettler Toledo scale folders and a remote SFTP middleware. Electron hosts a NestJS application in its main process; no HTTP server or separate service process is started. The renderer is a plain HTML/CSS/JS control UI.
Entry points: src/main/main.ts, src/main/preload.ts, and src/renderer/renderer.js.
Electron shell and UI boundary
The Electron main process creates the application window, exposes constrained preload APIs, and translates renderer actions into IPC requests. The renderer manages configuration input, Start/Stop controls, connection status, and streamed activity logs.
Key paths:
• src/main/main.ts
• src/main/preload.ts
• src/main/ipc.ts
• src/main/ipc-channels.ts
• src/renderer/renderer.js
The renderer does not directly access filesystem, SFTP, or Nest services; those capabilities remain in the main-process IPC boundary.
Nest application composition
NestJS supplies the dependency-injected service layer embedded in Electron. Bootstrap initializes the Nest context, while the root module assembles configuration, SFTP transport, watchers, and the agent orchestrator.
Key paths:
• src/main/nest/bootstrap.ts
• src/main/nest/app.module.ts
• src/main/nest/agent/agent.module.ts
Configuration and credential persistence
Configuration stores the SFTP URL, assigned folder name, and local Import/Export directories in Electron userData as scale-engine-config.json. The configuration service encrypts SFTP passwords through Electron safeStorage, supports legacy plaintext migration, and is used by IPC and agent startup.
Key paths:
• src/main/nest/config/config.service.ts
• src/main/nest/config/config.types.ts
• src/main/nest/config/config.module.ts
This is the main persistence and OS-security boundary; encrypted storage relies on platform facilities such as Windows DPAPI and macOS Keychain.
Agent lifecycle orchestration
The agent service is the operational state machine behind Start and Stop. Start persists configuration, establishes SFTP connectivity, and activates both directional watchers. Stop halts watchers and closes the SFTP session. It also governs whether reconnect behavior should continue after transport loss.
Key paths:
• src/main/nest/agent/agent.service.ts
• src/main/nest/sftp/sftp.service.ts
• src/main/nest/watcher/watcher.module.ts
Shared SFTP transport
A single ssh2-sftp-client wrapper owns the remote session for both watchers. It targets the middleware’s /upload hierarchy, observes end, close, and error failures, and reconnects with exponential backoff while the agent remains running.
Key paths:
• src/main/nest/sftp/sftp.service.ts
• src/main/nest/sftp/sftp.module.ts
External dependency: an SFTP server hosting shared import/export and processed folders.
Bidirectional file-transfer stages
The export watcher polls the local scale Export directory, ignores recently modified files to avoid mid-write reads, uploads receipt XML to /upload/import, then archives the local file under <Export>/processed with a timestamped name.
The import watcher polls /upload/<FolderName>/export/, downloads product XML into the local scale Import directory, then moves the remote source into its processed folder.
Key paths:
• src/main/nest/watcher/export-watcher.service.ts
• src/main/nest/watcher/import-watcher.service.ts
Logging and operational feedback
Services publish activity and error events through an in-process logger bus. IPC mirrors these events to the renderer, where failures and connection state drive the activity log and status indicator.
Key paths:
• src/main/nest/logger.service.ts
• src/main/ipc.ts
• src/renderer/index.html
Main entry points: agent.service.ts Agent Lifecycle · ipc.ts IPC Bridge · sftp.service.ts SFTP Transport · export-watcher.service.ts Export Watcher
Components 18
Electron UI
- Control UI · Configuration, Start/Stop, status, and activity log.
src/renderer/renderer.js - Preload API · Exposes constrained main-process capabilities.
src/main/preload.ts - Electron Main · Creates the application window and hosts Nest.
src/main/main.ts - IPC Bridge · Translates UI actions and mirrors operational events.
src/main/ipc.ts - IPC Channels
src/main/ipc-channels.ts
Embedded NestJS
Runs inside the Electron main process.
- Nest Bootstrap · Initializes the embedded dependency-injection context.
src/main/nest/bootstrap.ts - Root Module · Assembles configuration, transport, watchers, and agent.
src/main/nest/app.module.ts - Agent Lifecycle · Starts and stops transport and both directional watchers.
src/main/nest/agent/agent.service.ts - Agent Module
src/main/nest/agent/agent.module.ts - Configuration Service · Encrypts SFTP passwords and migrates legacy plaintext settings.
src/main/nest/config/config.service.ts - Configuration Module
src/main/nest/config/config.module.ts - Logger Bus · Publishes activity and error events.
src/main/nest/logger.service.ts - User Data Config · scale-engine-config.json in Electron userData.
File Transfer
- SFTP Transport · Owns one session and reconnects with backoff while running.
src/main/nest/sftp/sftp.service.ts - Export Watcher · Uploads stable receipt XML and archives local sources.
src/main/nest/watcher/export-watcher.service.ts - Import Watcher · Downloads product XML and moves remote sources to processed.
src/main/nest/watcher/import-watcher.service.ts - Scale Import / Export · Configured scale folders, including local processed exports.
- Middleware SFTP Server · Hosts upload, export, and processed folders.
Connections 22
- Control UI → Preload API: uses API
- Preload API → IPC Bridge: IPC requests
- IPC Bridge → IPC Channels: uses contract
- Electron Main → Nest Bootstrap: hosts
- Nest Bootstrap → Root Module: initializes
- Root Module → Agent Module: imports
- IPC Bridge → Agent Lifecycle: Start / Stop
- IPC Bridge → Configuration Service: read / save settings
- Agent Lifecycle → Configuration Service: persists startup config
- Configuration Service → User Data Config: encrypted credentials
- Agent Lifecycle → SFTP Transport: connect / disconnect
- Agent Lifecycle → Export Watcher: activates
- Agent Lifecycle → Import Watcher: activates
- Export Watcher → Scale Import / Export: polls Export; archives
- Export Watcher → SFTP Transport: uploads receipt XML
- Import Watcher → SFTP Transport: downloads product XML
- Import Watcher → Scale Import / Export: writes Import
- SFTP Transport → Middleware SFTP Server: shared remote session
- SFTP Transport → Logger Bus: transport events
- Agent Lifecycle → Logger Bus: lifecycle events
- Logger Bus → IPC Bridge: activity / errors
- IPC Bridge → Control UI: status and logs