mirror of
https://github.com/tiddly-gittly/TidGi-Desktop.git
synced 2025-12-15 15:10:31 -08:00
refactor: use threads-plugin to load wiki worker, and use ts in worker
This commit is contained in:
parent
254a608546
commit
52af3603bd
11 changed files with 220 additions and 98 deletions
119
package-lock.json
generated
119
package-lock.json
generated
|
|
@ -3148,6 +3148,12 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"@types/intercept-stdout": {
|
||||
"version": "0.1.0",
|
||||
"resolved": "https://registry.npmjs.org/@types/intercept-stdout/-/intercept-stdout-0.1.0.tgz",
|
||||
"integrity": "sha512-b4+N4+pHcUWaK75k4GDavB5ZS6aHdlsyxKaU82JEq7mzY0+kziTiT6nKm+OCkMlGvL6RNk2rera2InJKVP5OiQ==",
|
||||
"dev": true
|
||||
},
|
||||
"@types/is-url": {
|
||||
"version": "1.2.28",
|
||||
"resolved": "https://registry.npmjs.org/@types/is-url/-/is-url-1.2.28.tgz",
|
||||
|
|
@ -5422,8 +5428,7 @@
|
|||
"callsites": {
|
||||
"version": "3.1.0",
|
||||
"resolved": "https://registry.npmjs.org/callsites/-/callsites-3.1.0.tgz",
|
||||
"integrity": "sha512-P8BjAsXvZS+VIDUI11hHCQEv74YT67YUi5JJFNWIqL235sBmjX4+qx9Muvls5ivyNENctx46xQLQ3aTuE7ssaQ==",
|
||||
"dev": true
|
||||
"integrity": "sha512-P8BjAsXvZS+VIDUI11hHCQEv74YT67YUi5JJFNWIqL235sBmjX4+qx9Muvls5ivyNENctx46xQLQ3aTuE7ssaQ=="
|
||||
},
|
||||
"camel-case": {
|
||||
"version": "4.1.2",
|
||||
|
|
@ -7963,11 +7968,6 @@
|
|||
"yargs": "^16.0.2"
|
||||
}
|
||||
},
|
||||
"electron-is-dev": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/electron-is-dev/-/electron-is-dev-2.0.0.tgz",
|
||||
"integrity": "sha512-3X99K852Yoqu9AcW50qz3ibYBWY79/pBhlMCab8ToEWS48R0T9tyxRiQhwylE7zQdXrMnx2JKqUJyMPmt5FBqA=="
|
||||
},
|
||||
"electron-notarize": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/electron-notarize/-/electron-notarize-1.0.0.tgz",
|
||||
|
|
@ -9290,6 +9290,12 @@
|
|||
"integrity": "sha512-QudtT6av5WXels9WjIM7qz1XD1cWGvX4gGXvp/zBn9nXG02D0utdU3Em2m/QjTnrsk6bBjmCygl3rmj118msQQ==",
|
||||
"dev": true
|
||||
},
|
||||
"esm": {
|
||||
"version": "3.2.25",
|
||||
"resolved": "https://registry.npmjs.org/esm/-/esm-3.2.25.tgz",
|
||||
"integrity": "sha512-U1suiZ2oDVWv4zPO56S0NcR5QriEahGtdN2OR6FiOG4WJvcjBVFB0qI4+eKoWFH483PKGuLuu6V8Z4T5g63UVA==",
|
||||
"optional": true
|
||||
},
|
||||
"espree": {
|
||||
"version": "7.3.1",
|
||||
"resolved": "https://registry.npmjs.org/espree/-/espree-7.3.1.tgz",
|
||||
|
|
@ -11888,6 +11894,14 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"intercept-stdout": {
|
||||
"version": "0.1.2",
|
||||
"resolved": "https://registry.npmjs.org/intercept-stdout/-/intercept-stdout-0.1.2.tgz",
|
||||
"integrity": "sha1-Emq/H65sUJpCipjGGmMVWQQq6f0=",
|
||||
"requires": {
|
||||
"lodash.toarray": "^3.0.0"
|
||||
}
|
||||
},
|
||||
"internal-slot": {
|
||||
"version": "1.0.3",
|
||||
"resolved": "https://registry.npmjs.org/internal-slot/-/internal-slot-1.0.3.tgz",
|
||||
|
|
@ -12194,6 +12208,11 @@
|
|||
"resolved": "https://registry.npmjs.org/is-object/-/is-object-1.0.2.tgz",
|
||||
"integrity": "sha512-2rRIahhZr2UWb45fIOuvZGpFtz0TyOZLf32KxBbSoUCeZR495zCKlWUKKUByk3geS2eAs7ZAABt0Y/Rx0GiQGA=="
|
||||
},
|
||||
"is-observable": {
|
||||
"version": "2.1.0",
|
||||
"resolved": "https://registry.npmjs.org/is-observable/-/is-observable-2.1.0.tgz",
|
||||
"integrity": "sha512-DailKdLb0WU+xX8K5w7VsJhapwHLZ9jjmazqCJq4X12CTgqq73TKnbRcnSLuXYPOoLQgV5IrD7ePiX/h1vnkBw=="
|
||||
},
|
||||
"is-plain-obj": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-1.1.0.tgz",
|
||||
|
|
@ -12857,6 +12876,21 @@
|
|||
"integrity": "sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==",
|
||||
"dev": true
|
||||
},
|
||||
"lodash._arraycopy": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/lodash._arraycopy/-/lodash._arraycopy-3.0.0.tgz",
|
||||
"integrity": "sha1-due3wfH7klRzdIeKVi7Qaj5Q9uE="
|
||||
},
|
||||
"lodash._basevalues": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/lodash._basevalues/-/lodash._basevalues-3.0.0.tgz",
|
||||
"integrity": "sha1-W3dXYoAr3j0yl1A+JjAIIP32Ybc="
|
||||
},
|
||||
"lodash._getnative": {
|
||||
"version": "3.9.1",
|
||||
"resolved": "https://registry.npmjs.org/lodash._getnative/-/lodash._getnative-3.9.1.tgz",
|
||||
"integrity": "sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U="
|
||||
},
|
||||
"lodash._reinterpolate": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz",
|
||||
|
|
@ -12902,6 +12936,16 @@
|
|||
"resolved": "https://registry.npmjs.org/lodash.has/-/lodash.has-4.5.2.tgz",
|
||||
"integrity": "sha1-0Z9NwQlQWMzL4rDN9O4P5Ko3yGI="
|
||||
},
|
||||
"lodash.isarguments": {
|
||||
"version": "3.1.0",
|
||||
"resolved": "https://registry.npmjs.org/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz",
|
||||
"integrity": "sha1-L1c9hcaiQon/AGY7SRwdM4/zRYo="
|
||||
},
|
||||
"lodash.isarray": {
|
||||
"version": "3.0.4",
|
||||
"resolved": "https://registry.npmjs.org/lodash.isarray/-/lodash.isarray-3.0.4.tgz",
|
||||
"integrity": "sha1-eeTriMNqgSKvhvhEqpvNhRtfu1U="
|
||||
},
|
||||
"lodash.isequal": {
|
||||
"version": "4.5.0",
|
||||
"resolved": "https://registry.npmjs.org/lodash.isequal/-/lodash.isequal-4.5.0.tgz",
|
||||
|
|
@ -12919,6 +12963,16 @@
|
|||
"integrity": "sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs=",
|
||||
"dev": true
|
||||
},
|
||||
"lodash.keys": {
|
||||
"version": "3.1.2",
|
||||
"resolved": "https://registry.npmjs.org/lodash.keys/-/lodash.keys-3.1.2.tgz",
|
||||
"integrity": "sha1-TbwEcrFWvlCgsoaFXRvQsMZWCYo=",
|
||||
"requires": {
|
||||
"lodash._getnative": "^3.0.0",
|
||||
"lodash.isarguments": "^3.0.0",
|
||||
"lodash.isarray": "^3.0.0"
|
||||
}
|
||||
},
|
||||
"lodash.merge": {
|
||||
"version": "4.6.2",
|
||||
"resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz",
|
||||
|
|
@ -12973,6 +13027,16 @@
|
|||
"resolved": "https://registry.npmjs.org/lodash.throttle/-/lodash.throttle-4.1.1.tgz",
|
||||
"integrity": "sha1-wj6RtxAkKscMN/HhzaknTMOb8vQ="
|
||||
},
|
||||
"lodash.toarray": {
|
||||
"version": "3.0.2",
|
||||
"resolved": "https://registry.npmjs.org/lodash.toarray/-/lodash.toarray-3.0.2.tgz",
|
||||
"integrity": "sha1-KyBPD6T1HChcbwDIHRzqWiMEEXk=",
|
||||
"requires": {
|
||||
"lodash._arraycopy": "^3.0.0",
|
||||
"lodash._basevalues": "^3.0.0",
|
||||
"lodash.keys": "^3.0.0"
|
||||
}
|
||||
},
|
||||
"lodash.truncate": {
|
||||
"version": "4.4.2",
|
||||
"resolved": "https://registry.npmjs.org/lodash.truncate/-/lodash.truncate-4.4.2.tgz",
|
||||
|
|
@ -14835,6 +14899,11 @@
|
|||
"has": "^1.0.3"
|
||||
}
|
||||
},
|
||||
"observable-fns": {
|
||||
"version": "0.5.1",
|
||||
"resolved": "https://registry.npmjs.org/observable-fns/-/observable-fns-0.5.1.tgz",
|
||||
"integrity": "sha512-wf7g4Jpo1Wt2KIqZKLGeiuLOEMqpaOZ5gJn7DmSdqXgTdxRwSdBhWegQQpPteQ2gZvzCKqNNpwb853wcpA0j7A=="
|
||||
},
|
||||
"omggif": {
|
||||
"version": "1.0.10",
|
||||
"resolved": "https://registry.npmjs.org/omggif/-/omggif-1.0.10.tgz",
|
||||
|
|
@ -18506,6 +18575,27 @@
|
|||
"thenify": ">= 3.1.0 < 4"
|
||||
}
|
||||
},
|
||||
"threads": {
|
||||
"version": "1.6.4",
|
||||
"resolved": "https://registry.npmjs.org/threads/-/threads-1.6.4.tgz",
|
||||
"integrity": "sha512-A+9MQFAUha9W8MjIPmrvETy98qVmZFr5Unox9D95y7kvz3fBpGiFS7JOZs07B2KvTHoRNI5MrGudRVeCmv4Alw==",
|
||||
"requires": {
|
||||
"callsites": "^3.1.0",
|
||||
"debug": "^4.2.0",
|
||||
"is-observable": "^2.1.0",
|
||||
"observable-fns": "^0.5.1",
|
||||
"tiny-worker": ">= 2"
|
||||
}
|
||||
},
|
||||
"threads-plugin": {
|
||||
"version": "1.4.0",
|
||||
"resolved": "https://registry.npmjs.org/threads-plugin/-/threads-plugin-1.4.0.tgz",
|
||||
"integrity": "sha512-lQENPueZLsD+6Cvxvj/QaQyUskwnFZO+2ZGDMnPIvtytSeywWvYzete8paZ9L+5IR4v8jnSYNZPlIQrEhSK1EA==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"loader-utils": "^1.1.0"
|
||||
}
|
||||
},
|
||||
"throat": {
|
||||
"version": "4.1.0",
|
||||
"resolved": "https://registry.npmjs.org/throat/-/throat-4.1.0.tgz",
|
||||
|
|
@ -18587,6 +18677,15 @@
|
|||
"integrity": "sha512-lBN9zLN/oAf68o3zNXYrdCt1kP8WsiGW8Oo2ka41b2IM5JL/S1CTyX1rW0mb/zSuJun0ZUrDxx4sqvYS2FWzPA==",
|
||||
"dev": true
|
||||
},
|
||||
"tiny-worker": {
|
||||
"version": "2.3.0",
|
||||
"resolved": "https://registry.npmjs.org/tiny-worker/-/tiny-worker-2.3.0.tgz",
|
||||
"integrity": "sha512-pJ70wq5EAqTAEl9IkGzA+fN0836rycEuz2Cn6yeZ6FRzlVS5IDOkFHpIoEsksPRQV34GDqXm65+OlnZqUSyK2g==",
|
||||
"optional": true,
|
||||
"requires": {
|
||||
"esm": "^3.2.25"
|
||||
}
|
||||
},
|
||||
"tinycolor2": {
|
||||
"version": "1.4.2",
|
||||
"resolved": "https://registry.npmjs.org/tinycolor2/-/tinycolor2-1.4.2.tgz",
|
||||
|
|
@ -20195,6 +20294,12 @@
|
|||
"source-map": "~0.6.1"
|
||||
}
|
||||
},
|
||||
"webpack2-externals-plugin": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/webpack2-externals-plugin/-/webpack2-externals-plugin-1.0.0.tgz",
|
||||
"integrity": "sha1-qBFNOfI/yjHO0yPA2hfBG1F2L0o=",
|
||||
"dev": true
|
||||
},
|
||||
"which": {
|
||||
"version": "2.0.2",
|
||||
"resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
|
||||
|
|
|
|||
10
package.json
10
package.json
|
|
@ -25,8 +25,7 @@
|
|||
"asar": true,
|
||||
"extraResource": [
|
||||
"localization",
|
||||
"template/wiki",
|
||||
".webpack/main/wiki-worker.js"
|
||||
"template/wiki"
|
||||
],
|
||||
"mac": {
|
||||
"category": "productivity",
|
||||
|
|
@ -105,6 +104,7 @@
|
|||
"i18next": "20.2.2",
|
||||
"i18next-electron-fs-backend": "1.3.6",
|
||||
"i18next-fs-backend": "1.1.1",
|
||||
"intercept-stdout": "^0.1.2",
|
||||
"inversify": "5.1.1",
|
||||
"inversify-inject-decorators": "3.1.0",
|
||||
"is-url": "1.2.4",
|
||||
|
|
@ -118,6 +118,7 @@
|
|||
"regedit": "3.0.3",
|
||||
"rxjs": "7.0.0",
|
||||
"semver": "7.3.5",
|
||||
"threads": "^1.6.4",
|
||||
"tmp": "0.2.1",
|
||||
"type-fest": "1.0.2",
|
||||
"uuid": "8.3.2",
|
||||
|
|
@ -154,6 +155,7 @@
|
|||
"@types/file-loader": "5.0.0",
|
||||
"@types/fs-extra": "9.0.11",
|
||||
"@types/i18next-fs-backend": "1.0.0",
|
||||
"@types/intercept-stdout": "^0.1.0",
|
||||
"@types/is-url": "1.2.28",
|
||||
"@types/lodash": "4.14.168",
|
||||
"@types/node": "15.0.1",
|
||||
|
|
@ -213,6 +215,7 @@
|
|||
"style-loader": "2.0.0",
|
||||
"styled-components": "5.2.3",
|
||||
"subscriptions-transport-ws": "0.9.18",
|
||||
"threads-plugin": "^1.4.0",
|
||||
"ts-import-plugin": "1.6.7",
|
||||
"ts-loader": "8.2.0",
|
||||
"ts-migrate": "0.1.18",
|
||||
|
|
@ -220,7 +223,8 @@
|
|||
"typeface-roboto": "1.1.13",
|
||||
"typescript": "4.2.4",
|
||||
"typescript-plugin-styled-components": "1.5.0",
|
||||
"typesync": "0.8.0"
|
||||
"typesync": "0.8.0",
|
||||
"webpack2-externals-plugin": "^1.0.0"
|
||||
},
|
||||
"private": false
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,4 @@
|
|||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const electron = require('electron');
|
||||
|
||||
if (typeof electron === 'string') {
|
||||
throw new TypeError('Not running in an Electron environment!');
|
||||
}
|
||||
export const isElectronDevelopment = typeof electron === 'string' ? false : !electron.app.isPackaged;
|
||||
export const isElectronDevelopment = process.env.NODE_ENV === 'development' || (typeof electron === 'string' ? false : !electron.app.isPackaged);
|
||||
|
|
|
|||
|
|
@ -3,9 +3,13 @@ import fs from 'fs-extra';
|
|||
|
||||
import { LOG_FOLDER } from '@/constants/paths';
|
||||
|
||||
export function wikiOutputToFile(wikiName: string, stream: NodeJS.ReadableStream): void {
|
||||
const logFilePath = path.join(LOG_FOLDER, `${wikiName}.log`);
|
||||
stream.pipe(fs.createWriteStream(logFilePath));
|
||||
function getWikiLogFilePath(wikiName: string): string {
|
||||
const logFileName = wikiName.replace(/[/\\]/g, '_');
|
||||
const logFilePath = path.join(LOG_FOLDER, `${logFileName}.log`);
|
||||
return logFilePath;
|
||||
}
|
||||
export function wikiOutputToFile(wikiName: string, message: string): void {
|
||||
fs.appendFileSync(getWikiLogFilePath(wikiName), message);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -13,7 +17,7 @@ export function wikiOutputToFile(wikiName: string, stream: NodeJS.ReadableStream
|
|||
* @param {string} wikiName
|
||||
*/
|
||||
export function refreshOutputFile(wikiName: string): void {
|
||||
const logFilePath = path.join(LOG_FOLDER, `${wikiName}.log`);
|
||||
const logFilePath = getWikiLogFilePath(wikiName);
|
||||
if (fs.existsSync(logFilePath)) {
|
||||
fs.removeSync(logFilePath);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,12 @@
|
|||
/* eslint-disable @typescript-eslint/require-await */
|
||||
/* eslint-disable @typescript-eslint/no-dynamic-delete */
|
||||
import { injectable } from 'inversify';
|
||||
import { PromiseValue } from 'type-fest';
|
||||
import { delay } from 'bluebird';
|
||||
import fs from 'fs-extra';
|
||||
import path from 'path';
|
||||
import { Worker } from 'worker_threads';
|
||||
import { isElectronDevelopment } from '@/constants/isElectronDevelopment';
|
||||
import { spawn, Thread, Worker } from 'threads';
|
||||
import type { WorkerEvent } from 'threads/dist/types/master';
|
||||
import { dialog } from 'electron';
|
||||
import chokidar from 'chokidar';
|
||||
import { trim, compact, debounce } from 'lodash';
|
||||
|
|
@ -19,7 +20,7 @@ import type { IWorkspaceService, IWorkspace } from '@services/workspaces/interfa
|
|||
import type { IGitService, IGitUserInfos } from '@services/git/interface';
|
||||
import type { IWorkspaceViewService } from '@services/workspacesView/interface';
|
||||
import { WindowNames } from '@services/windows/WindowProperties';
|
||||
import { logger, wikiOutputToFile, refreshOutputFile } from '@services/libs/log';
|
||||
import { logger } from '@services/libs/log';
|
||||
import i18n from '@services/libs/i18n';
|
||||
import { lazyInject } from '@services/container';
|
||||
import { TIDDLYWIKI_TEMPLATE_FOLDER_PATH, TIDDLERS_PATH } from '@/constants/paths';
|
||||
|
|
@ -84,15 +85,8 @@ export class Wiki implements IWikiService {
|
|||
};
|
||||
|
||||
// key is same to workspace wikiFolderLocation, so we can get this worker by workspace wikiFolderLocation
|
||||
// { [wikiFolderLocation: string]: Worker }
|
||||
private wikiWorkers: Record<string, Worker> = {};
|
||||
|
||||
// don't forget to config option in `dist.js` https://github.com/electron/electron/issues/18540#issuecomment-652430001
|
||||
// to copy all worker.js and its local dependence to `process.resourcesPath`
|
||||
// On dev, this file will be in .webpack/main/index.js ,so:
|
||||
private readonly WIKI_WORKER_PATH = isElectronDevelopment
|
||||
? path.resolve('./.webpack/main/wiki-worker.ts')
|
||||
: path.resolve(process.resourcesPath, 'wiki-worker.ts');
|
||||
// { [wikiFolderLocation: string]: ArbitraryThreadType }
|
||||
private wikiWorkers: Record<string, PromiseValue<ReturnType<typeof spawn>>> = {};
|
||||
|
||||
public async startWiki(homePath: string, tiddlyWikiPort: number, userName: string): Promise<void> {
|
||||
// use Promise to handle worker callbacks
|
||||
|
|
@ -104,30 +98,28 @@ export class Wiki implements IWikiService {
|
|||
}
|
||||
await this.workspaceService.updateMetaData(workspaceID, { isLoading: true });
|
||||
const workerData = { homePath, userName, tiddlyWikiPort };
|
||||
const worker = new Worker(this.WIKI_WORKER_PATH, { workerData });
|
||||
const worker = await spawn(new Worker('./wiki-worker.ts'));
|
||||
this.wikiWorkers[homePath] = worker;
|
||||
const loggerMeta = { worker: 'NodeJSWiki', homePath };
|
||||
const loggerForWorker = this.logMessage(loggerMeta);
|
||||
let started = false;
|
||||
// redirect stdout to file
|
||||
const logFileName = workspace.wikiFolderLocation.replace(/[/\\]/g, '_');
|
||||
refreshOutputFile(logFileName);
|
||||
wikiOutputToFile(logFileName, worker.stdout);
|
||||
wikiOutputToFile(logFileName, worker.stderr);
|
||||
return await new Promise<void>((resolve, reject) => {
|
||||
worker.on('error', (error: Error) => {
|
||||
Thread.errors(worker).subscribe((error) => {
|
||||
logger.error(error.message, { ...loggerMeta, ...error });
|
||||
reject(error);
|
||||
});
|
||||
worker.on('exit', (code: number) => {
|
||||
if (code !== 0) {
|
||||
Thread.events(worker).subscribe((event: WorkerEvent) => {
|
||||
if (event.type === 'message') {
|
||||
loggerForWorker(event.data);
|
||||
logger.debug(event.data, loggerMeta);
|
||||
} else if (event.type === 'termination') {
|
||||
delete this.wikiWorkers[homePath];
|
||||
logger.warning(`NodeJSWiki ${homePath} Worker stopped`, loggerMeta);
|
||||
resolve();
|
||||
}
|
||||
logger.warning(`NodeJSWiki ${homePath} Worker stopped with exit code ${code}.`, loggerMeta);
|
||||
resolve();
|
||||
});
|
||||
worker.on('message', (message: string | { type: string; payload?: string | { message: string; handler: string } }) => {
|
||||
loggerForWorker(message);
|
||||
|
||||
return worker.startNodeJSWiki(workerData).then(() => {
|
||||
if (!started) {
|
||||
started = true;
|
||||
setTimeout(async () => {
|
||||
|
|
@ -154,15 +146,11 @@ export class Wiki implements IWikiService {
|
|||
return await Promise.resolve();
|
||||
}
|
||||
try {
|
||||
await new Promise((resolve, reject) => {
|
||||
worker.postMessage({ type: 'command', message: 'exit' });
|
||||
worker.once('exit', resolve);
|
||||
worker.once('error', reject);
|
||||
});
|
||||
await Thread.terminate(worker);
|
||||
await delay(100);
|
||||
} catch (error) {
|
||||
logger.info(`Wiki-worker have error ${(error as Error).message} when try to stop`, { function: 'stopWiki' });
|
||||
await worker.terminate();
|
||||
// await worker.terminate();
|
||||
}
|
||||
// delete this.wikiWorkers[homePath];
|
||||
logger.info(`Wiki-worker for ${homePath} stopped`, { function: 'stopWiki' });
|
||||
|
|
|
|||
|
|
@ -1,43 +0,0 @@
|
|||
/* eslint-disable @typescript-eslint/no-misused-promises */
|
||||
/* eslint-disable @typescript-eslint/restrict-template-expressions */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
||||
/* eslint-disable @typescript-eslint/no-var-requires */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
||||
const { workerData, parentPort, isMainThread } = require('worker_threads');
|
||||
const path = require('path');
|
||||
const $tw = require('@tiddlygit/tiddlywiki').TiddlyWiki();
|
||||
|
||||
function startNodeJSWiki() {
|
||||
const { homePath, tiddlyWikiPort = 5112, userName } = workerData;
|
||||
try {
|
||||
process.env.TIDDLYWIKI_PLUGIN_PATH = path.resolve(homePath, 'plugins');
|
||||
process.env.TIDDLYWIKI_THEME_PATH = path.resolve(homePath, 'themes');
|
||||
// add tiddly filesystem back https://github.com/Jermolene/TiddlyWiki5/issues/4484#issuecomment-596779416
|
||||
$tw.boot.argv = [
|
||||
'+plugins/tiddlywiki/filesystem',
|
||||
'+plugins/tiddlywiki/tiddlyweb',
|
||||
'+plugins/linonetwo/watch-fs',
|
||||
homePath,
|
||||
'--listen',
|
||||
`anon-username=${userName}`,
|
||||
`port=${tiddlyWikiPort}`,
|
||||
'host=0.0.0.0',
|
||||
'root-tiddler=$:/core/save/lazy-images',
|
||||
];
|
||||
$tw.boot.boot(() => parentPort.postMessage(`Tiddlywiki booted at http://localhost:${tiddlyWikiPort}`));
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
parentPort.postMessage(`Tiddlywiki booted failed with error ${error.message} ${error.stack}`);
|
||||
}
|
||||
}
|
||||
module.exports = startNodeJSWiki;
|
||||
|
||||
if (!isMainThread) {
|
||||
startNodeJSWiki();
|
||||
parentPort.once('message', (message) => {
|
||||
if (typeof message === 'object' && message.type === 'command' && message.message === 'exit') {
|
||||
process.exit(0);
|
||||
}
|
||||
});
|
||||
}
|
||||
42
src/services/wiki/wiki-worker.ts
Normal file
42
src/services/wiki/wiki-worker.ts
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import { expose } from 'threads/worker';
|
||||
import path from 'path';
|
||||
import tiddlywiki from '@tiddlygit/tiddlywiki';
|
||||
import intercept from 'intercept-stdout';
|
||||
|
||||
import { wikiOutputToFile, refreshOutputFile } from '@services/libs/log/wiki-output';
|
||||
|
||||
async function startNodeJSWiki({ homePath, tiddlyWikiPort = 5112, userName }: { homePath: string; tiddlyWikiPort: number; userName: string }): Promise<string> {
|
||||
refreshOutputFile(homePath);
|
||||
intercept(
|
||||
(newStdOut: string) => {
|
||||
wikiOutputToFile(homePath, newStdOut);
|
||||
},
|
||||
(newStdError: string) => {
|
||||
wikiOutputToFile(homePath, newStdError);
|
||||
},
|
||||
);
|
||||
|
||||
const $tw = tiddlywiki.TiddlyWiki();
|
||||
try {
|
||||
process.env.TIDDLYWIKI_PLUGIN_PATH = path.resolve(homePath, 'plugins');
|
||||
process.env.TIDDLYWIKI_THEME_PATH = path.resolve(homePath, 'themes');
|
||||
// add tiddly filesystem back https://github.com/Jermolene/TiddlyWiki5/issues/4484#issuecomment-596779416
|
||||
$tw.boot.argv = [
|
||||
'+plugins/tiddlywiki/filesystem',
|
||||
'+plugins/tiddlywiki/tiddlyweb',
|
||||
'+plugins/linonetwo/watch-fs',
|
||||
homePath,
|
||||
'--listen',
|
||||
`anon-username=${userName}`,
|
||||
`port=${tiddlyWikiPort}`,
|
||||
'host=0.0.0.0',
|
||||
'root-tiddler=$:/core/save/lazy-images',
|
||||
];
|
||||
return await new Promise((resolve, reject) => {
|
||||
$tw.boot.startup({ callback: () => resolve(`Tiddlywiki booted at http://localhost:${tiddlyWikiPort}`) });
|
||||
});
|
||||
} catch (error) {
|
||||
throw new Error(`Tiddlywiki booted failed with error ${(error as Error).message} ${(error as Error).stack ?? ''}`);
|
||||
}
|
||||
}
|
||||
expose({ startNodeJSWiki });
|
||||
14
src/type.d.ts
vendored
14
src/type.d.ts
vendored
|
|
@ -3,6 +3,20 @@ declare module 'errio' {
|
|||
export function stringify(error: Error): string;
|
||||
export function register(error: ErrorConstructor): void;
|
||||
}
|
||||
declare module '@tiddlygit/tiddlywiki' {
|
||||
export interface I$TW {
|
||||
boot: { argv: string[]; startup: (options: { callback: () => unknown }) => void };
|
||||
}
|
||||
export function TiddlyWiki(): I$TW;
|
||||
}
|
||||
declare module 'threads-plugin' {
|
||||
const value: any;
|
||||
export default value;
|
||||
}
|
||||
declare module 'webpack2-externals-plugin' {
|
||||
const value: any;
|
||||
export default value;
|
||||
}
|
||||
declare module '*.png' {
|
||||
const value: string;
|
||||
export default value;
|
||||
|
|
|
|||
|
|
@ -16,5 +16,5 @@
|
|||
"./*.json",
|
||||
"./*.js"
|
||||
],
|
||||
"exclude": ["template/**/*.js", "src/services/libs/i18n/i18next-electron-fs-backend.ts", "src/services/wiki/wiki-worker.js"]
|
||||
"exclude": ["template/**/*.js", "src/services/libs/i18n/i18next-electron-fs-backend.ts"]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,18 +3,18 @@
|
|||
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
|
||||
const CspHtmlWebpackPlugin = require('csp-html-webpack-plugin');
|
||||
const CircularDependencyPlugin = require('circular-dependency-plugin');
|
||||
const path = require('path');
|
||||
const webpack = require('webpack');
|
||||
const CopyPlugin = require('copy-webpack-plugin');
|
||||
const ThreadsPlugin = require('threads-plugin');
|
||||
const ExternalsPlugin = require('webpack2-externals-plugin');
|
||||
|
||||
exports.main = [
|
||||
// we only need one instance of TsChecker, it will check main and renderer all together
|
||||
new ForkTsCheckerWebpackPlugin(),
|
||||
new CopyPlugin({
|
||||
// to is relative to ./.webpack/main/
|
||||
patterns: [
|
||||
{ from: 'src/services/wiki/wiki-worker.js', to: 'wiki-worker.js' },
|
||||
{ from: 'localization', to: 'localization' },
|
||||
],
|
||||
patterns: [{ from: 'localization', to: 'localization' }],
|
||||
}),
|
||||
new CircularDependencyPlugin({
|
||||
// exclude detection of files based on a RegExp
|
||||
|
|
@ -30,6 +30,15 @@ exports.main = [
|
|||
new webpack.DefinePlugin({
|
||||
'process.env.NODE_ENV': `"${process.env.NODE_ENV ?? 'production'}"`,
|
||||
}),
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
||||
new ExternalsPlugin({
|
||||
type: 'commonjs',
|
||||
include: path.join(__dirname, 'node_modules'),
|
||||
}),
|
||||
new ThreadsPlugin({
|
||||
target: 'electron-node-worker',
|
||||
plugins: ['ExternalsPlugin'],
|
||||
}),
|
||||
];
|
||||
|
||||
exports.renderer = [
|
||||
|
|
@ -52,4 +61,6 @@ exports.renderer = [
|
|||
},
|
||||
},
|
||||
),
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
||||
new ThreadsPlugin(),
|
||||
];
|
||||
|
|
|
|||
|
|
@ -94,7 +94,7 @@ module.exports = [
|
|||
],
|
||||
}),
|
||||
compilerOptions: {
|
||||
module: 'es2015',
|
||||
module: 'esnext',
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue