mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	server-ts: Port services/setup
This commit is contained in:
		| @@ -13,7 +13,7 @@ class BOption extends AbstractBeccaEntity<BOption> { | |||||||
|     static get hashedProperties() { return ["name", "value"]; } |     static get hashedProperties() { return ["name", "value"]; } | ||||||
|  |  | ||||||
|     name!: string; |     name!: string; | ||||||
|     value!: string; |     value!: string | number; | ||||||
|     isSynced!: boolean; |     isSynced!: boolean; | ||||||
|  |  | ||||||
|     constructor(row?: OptionRow) { |     constructor(row?: OptionRow) { | ||||||
|   | |||||||
							
								
								
									
										17
									
								
								src/services/api-interface.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/services/api-interface.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | |||||||
|  | import { OptionRow } from "../becca/entities/rows"; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Response for /api/setup/status. | ||||||
|  |  */ | ||||||
|  | export interface SetupStatusResponse { | ||||||
|  |     syncVersion: number; | ||||||
|  |     schemaExists: boolean; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Response for /api/setup/sync-seed. | ||||||
|  |  */ | ||||||
|  | export interface SetupSyncSeedResponse { | ||||||
|  |     syncVersion: number; | ||||||
|  |     options: OptionRow[] | ||||||
|  | } | ||||||
| @@ -51,7 +51,7 @@ function getOptionBool(name: string): boolean { | |||||||
|     return val === 'true'; |     return val === 'true'; | ||||||
| } | } | ||||||
|  |  | ||||||
| function setOption(name: string, value: string | boolean) { | function setOption(name: string, value: string | number | boolean) { | ||||||
|     if (value === true || value === false) { |     if (value === true || value === false) { | ||||||
|         value = value.toString(); |         value = value.toString(); | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -33,7 +33,7 @@ interface Client { | |||||||
|     request(opts: ClientOpts): Request; |     request(opts: ClientOpts): Request; | ||||||
| } | } | ||||||
|  |  | ||||||
| function exec(opts: ExecOpts) { | function exec<T>(opts: ExecOpts): Promise<T> { | ||||||
|     const client = getClient(opts); |     const client = getClient(opts); | ||||||
|      |      | ||||||
|     // hack for cases where electron.net does not work, but we don't want to set proxy |     // hack for cases where electron.net does not work, but we don't want to set proxy | ||||||
| @@ -129,7 +129,7 @@ function exec(opts: ExecOpts) { | |||||||
|                     : opts.body; |                     : opts.body; | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             request.end(payload); |             request.end(payload as string); | ||||||
|         } |         } | ||||||
|         catch (e: any) { |         catch (e: any) { | ||||||
|             reject(generateError(opts, e.message)); |             reject(generateError(opts, e.message)); | ||||||
|   | |||||||
| @@ -3,7 +3,7 @@ export interface CookieJar { | |||||||
| } | } | ||||||
|  |  | ||||||
| export interface ExecOpts { | export interface ExecOpts { | ||||||
|     proxy: "noproxy" | null; |     proxy: "noproxy" | string | null; | ||||||
|     method: string; |     method: string; | ||||||
|     url: string; |     url: string; | ||||||
|     paging?: { |     paging?: { | ||||||
| @@ -16,5 +16,5 @@ export interface ExecOpts { | |||||||
|         password?: string; |         password?: string; | ||||||
|     }, |     }, | ||||||
|     timeout: number; |     timeout: number; | ||||||
|     body: string; |     body?: string | {}; | ||||||
| } | } | ||||||
| @@ -1,15 +1,16 @@ | |||||||
| const syncService = require('./sync'); | import syncService = require('./sync'); | ||||||
| const log = require('./log'); | import log = require('./log'); | ||||||
| const sqlInit = require('./sql_init'); | import sqlInit = require('./sql_init'); | ||||||
| const optionService = require('./options'); | import optionService = require('./options'); | ||||||
| const syncOptions = require('./sync_options'); | import syncOptions = require('./sync_options'); | ||||||
| const request = require('./request'); | import request = require('./request'); | ||||||
| const appInfo = require('./app_info'); | import appInfo = require('./app_info'); | ||||||
| const utils = require('./utils'); | import utils = require('./utils'); | ||||||
| const becca = require('../becca/becca'); | import becca = require('../becca/becca'); | ||||||
|  | import { SetupStatusResponse, SetupSyncSeedResponse } from './api-interface'; | ||||||
| 
 | 
 | ||||||
| async function hasSyncServerSchemaAndSeed() { | async function hasSyncServerSchemaAndSeed() { | ||||||
|     const response = await requestToSyncServer('GET', '/api/setup/status'); |     const response = await requestToSyncServer<SetupStatusResponse>('GET', '/api/setup/status'); | ||||||
| 
 | 
 | ||||||
|     if (response.syncVersion !== appInfo.syncVersion) { |     if (response.syncVersion !== appInfo.syncVersion) { | ||||||
|         throw new Error(`Could not setup sync since local sync protocol version is ${appInfo.syncVersion} while remote is ${response.syncVersion}. To fix this issue, use same Trilium version on all instances.`); |         throw new Error(`Could not setup sync since local sync protocol version is ${appInfo.syncVersion} while remote is ${response.syncVersion}. To fix this issue, use same Trilium version on all instances.`); | ||||||
| @@ -32,7 +33,7 @@ function triggerSync() { | |||||||
| async function sendSeedToSyncServer() { | async function sendSeedToSyncServer() { | ||||||
|     log.info("Initiating sync to server"); |     log.info("Initiating sync to server"); | ||||||
| 
 | 
 | ||||||
|     await requestToSyncServer('POST', '/api/setup/sync-seed', { |     await requestToSyncServer<void>('POST', '/api/setup/sync-seed', { | ||||||
|         options: getSyncSeedOptions(), |         options: getSyncSeedOptions(), | ||||||
|         syncVersion: appInfo.syncVersion |         syncVersion: appInfo.syncVersion | ||||||
|     }); |     }); | ||||||
| @@ -43,7 +44,7 @@ async function sendSeedToSyncServer() { | |||||||
|     optionService.setOption('lastSyncedPull', 0); |     optionService.setOption('lastSyncedPull', 0); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async function requestToSyncServer(method, path, body = null) { | async function requestToSyncServer<T>(method: string, path: string, body?: string | {}): Promise<T> { | ||||||
|     const timeout = syncOptions.getSyncTimeout(); |     const timeout = syncOptions.getSyncTimeout(); | ||||||
| 
 | 
 | ||||||
|     return await utils.timeLimit(request.exec({ |     return await utils.timeLimit(request.exec({ | ||||||
| @@ -52,10 +53,10 @@ async function requestToSyncServer(method, path, body = null) { | |||||||
|         body, |         body, | ||||||
|         proxy: syncOptions.getSyncProxy(), |         proxy: syncOptions.getSyncProxy(), | ||||||
|         timeout: timeout |         timeout: timeout | ||||||
|     }), timeout); |     }), timeout) as T; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async function setupSyncFromSyncServer(syncServerHost, syncProxy, password) { | async function setupSyncFromSyncServer(syncServerHost: string, syncProxy: string, password: string) { | ||||||
|     if (sqlInit.isDbInitialized()) { |     if (sqlInit.isDbInitialized()) { | ||||||
|         return { |         return { | ||||||
|             result: 'failure', |             result: 'failure', | ||||||
| @@ -67,7 +68,7 @@ async function setupSyncFromSyncServer(syncServerHost, syncProxy, password) { | |||||||
|         log.info("Getting document options FROM sync server."); |         log.info("Getting document options FROM sync server."); | ||||||
| 
 | 
 | ||||||
|         // the response is expected to contain documentId and documentSecret options
 |         // the response is expected to contain documentId and documentSecret options
 | ||||||
|         const resp = await request.exec({ |         const resp = await request.exec<SetupSyncSeedResponse>({ | ||||||
|             method: 'get', |             method: 'get', | ||||||
|             url: `${syncServerHost}/api/setup/sync-seed`, |             url: `${syncServerHost}/api/setup/sync-seed`, | ||||||
|             auth: { password }, |             auth: { password }, | ||||||
| @@ -92,7 +93,7 @@ async function setupSyncFromSyncServer(syncServerHost, syncProxy, password) { | |||||||
| 
 | 
 | ||||||
|         return { result: 'success' }; |         return { result: 'success' }; | ||||||
|     } |     } | ||||||
|     catch (e) { |     catch (e: any) { | ||||||
|         log.error(`Sync failed: '${e.message}', stack: ${e.stack}`); |         log.error(`Sync failed: '${e.message}', stack: ${e.stack}`); | ||||||
| 
 | 
 | ||||||
|         return { |         return { | ||||||
| @@ -107,7 +107,7 @@ async function sync() { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function login() { | async function login() { | ||||||
|     const setupService = require('./setup.js'); // circular dependency issue |     const setupService = require('./setup'); // circular dependency issue | ||||||
|  |  | ||||||
|     if (!await setupService.hasSyncServerSchemaAndSeed()) { |     if (!await setupService.hasSyncServerSchemaAndSeed()) { | ||||||
|         await setupService.sendSeedToSyncServer(); |         await setupService.sendSeedToSyncServer(); | ||||||
|   | |||||||
| @@ -4,7 +4,7 @@ const assetPath = require('./src/services/asset_path'); | |||||||
| module.exports = { | module.exports = { | ||||||
|     mode: 'production', |     mode: 'production', | ||||||
|     entry: { |     entry: { | ||||||
|         setup: './src/public/app/setup.js', |         setup: './src/public/app/setup.ts', | ||||||
|         mobile: './src/public/app/mobile.js', |         mobile: './src/public/app/mobile.js', | ||||||
|         desktop: './src/public/app/desktop.js', |         desktop: './src/public/app/desktop.js', | ||||||
|     }, |     }, | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user