From 97db272041eda33e7f3631401e430b12e67533cd Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Wed, 13 May 2020 18:41:05 +0800 Subject: [PATCH] feat: add global eventCenter & static console source map Signed-off-by: MiaoWoo --- packages/api/src/console.ts | 18 +++++++++--------- packages/core/src/index.ts | 2 ++ packages/nashorn/src/index.ts | 4 ++++ 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/packages/api/src/console.ts b/packages/api/src/console.ts index 4a610223..4c970b1f 100644 --- a/packages/api/src/console.ts +++ b/packages/api/src/console.ts @@ -21,8 +21,8 @@ enum LogLevel { export class MiaoScriptConsole implements Console { Console: NodeJS.ConsoleConstructor; - private sourceMaps: { [key: string]: SourceMapBuilder } = {}; - private sourceFileMaps: { [key: string]: string } = {}; + private static sourceMaps: { [key: string]: SourceMapBuilder } = {}; + private static sourceFileMaps: { [key: string]: string } = {}; private _name: string = ''; private _level: LogLevel = LogLevel.INFO; @@ -93,8 +93,8 @@ export class MiaoScriptConsole implements Console { readSourceMap(fileName: string, lineNumber: number) { try { if (fileName.endsWith('js')) { - if (this.sourceMaps[fileName] === undefined) { - this.sourceMaps[fileName] = null + if (MiaoScriptConsole.sourceMaps[fileName] === undefined) { + MiaoScriptConsole.sourceMaps[fileName] = null let sourceLine = base.read(fileName).split('\n'); let lastLine = sourceLine[sourceLine.length - 1] if (lastLine.startsWith('//# sourceMappingURL=')) { @@ -109,14 +109,14 @@ export class MiaoScriptConsole implements Console { if (file.exists()) { sourceContent = base.read(file) } } if (sourceContent) { - this.sourceMaps[fileName] = new SourceMapBuilder(JSON.parse(sourceContent)) - this.sourceFileMaps[fileName] = Paths.get(fileName, '..', this.sourceMaps[fileName].sources[0]).toFile().getCanonicalPath(); + MiaoScriptConsole.sourceMaps[fileName] = new SourceMapBuilder(JSON.parse(sourceContent)) + MiaoScriptConsole.sourceFileMaps[fileName] = Paths.get(fileName, '..', MiaoScriptConsole.sourceMaps[fileName].sources[0]).toFile().getCanonicalPath(); } } } - if (this.sourceMaps[fileName]) { - let sourceMapping = this.sourceMaps[fileName].getSource(lineNumber, 25, true, true); - fileName = this.sourceFileMaps[fileName] + if (MiaoScriptConsole.sourceMaps[fileName]) { + let sourceMapping = MiaoScriptConsole.sourceMaps[fileName].getSource(lineNumber, 25, true, true); + fileName = MiaoScriptConsole.sourceFileMaps[fileName] if (sourceMapping && lineNumber != sourceMapping.mapping.sourceLine) { lineNumber = sourceMapping.mapping.sourceLine; } } } diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 76fdb2f4..cd1cc9d6 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -4,6 +4,7 @@ import { plugin, server, task, constants } from '@ccms/api' import { DefaultContainer as container, inject, provideSingleton, ContainerInstance, buildProviderModule } from '@ccms/container' console.i18n("ms.core.ioc.completed", { scope: global.scope, time: (Date.now() - containerStartTime) / 1000 }) import http from '@ccms/common/dist/http' +import { EventEmitter } from 'events' @provideSingleton(MiaoScriptCore) class MiaoScriptCore { @@ -25,6 +26,7 @@ class MiaoScriptCore { } loadServerConsole() { + global.setGlobal('eventCenter', new EventEmitter(), { writable: false, configurable: false }); //@ts-ignore global.setGlobal('console', new this.Console(), { writable: false, configurable: false }) } diff --git a/packages/nashorn/src/index.ts b/packages/nashorn/src/index.ts index 7d296e89..88a8def1 100644 --- a/packages/nashorn/src/index.ts +++ b/packages/nashorn/src/index.ts @@ -45,6 +45,7 @@ declare global { logger: any; debug: boolean; level: string; + eventCenter: EventEmitter; NashornEngineStartTime: number; setGlobal: (key: string, value: any, config?: PropertyDescriptor & ThisType) => void; noop: () => void; @@ -70,6 +71,9 @@ declare global { console(...args: any): void; i18n(name: string, ...params: any[]): void; } + interface ProxyConstructor { + newProxy(target: T, handler: ProxyHandler): T; + } } export { };