-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSyntaxTreeFactory.ts
More file actions
70 lines (60 loc) · 2.58 KB
/
SyntaxTreeFactory.ts
File metadata and controls
70 lines (60 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { DocumentType } from '../../document/Document';
import { FeatureFlag } from '../../featureFlag/FeatureFlagI';
import { ParserFactory, parserFactory, parserFactoryReady } from '../../parser/ParserFactory';
import { WasmParserFactory } from '../../parser/WasmParserFactory';
import { LoggerFactory } from '../../telemetry/LoggerFactory';
import { ScopedTelemetry } from '../../telemetry/ScopedTelemetry';
import { Telemetry } from '../../telemetry/TelemetryDecorator';
import { JsonSyntaxTree } from './JsonSyntaxTree';
import { SyntaxTree } from './SyntaxTree';
import { YamlSyntaxTree } from './YamlSyntaxTree';
export type ParserType = 'native' | 'wasm';
const log = LoggerFactory.getLogger('SyntaxTreeFactory');
const isLegacyLinux = process.env.BUILD_TARGET === 'legacy';
export class SyntaxTreeFactory {
@Telemetry() private readonly telemetry!: ScopedTelemetry;
private factory: ParserFactory;
private type: ParserType;
private readyPromise: Promise<void>;
constructor(nativeFactory: ParserFactory = parserFactory) {
this.factory = nativeFactory;
this.type = isLegacyLinux ? 'wasm' : 'native';
this.readyPromise = parserFactoryReady;
}
/**
* Called once during server initialization to lock in the parser type
* for the lifetime of the session based on the feature flag state.
*/
initialize(wasmFlag: FeatureFlag): void {
if (isLegacyLinux) {
return; // Already using WASM via parserFactory
}
if (wasmFlag.isEnabled()) {
log.info('WasmParser feature flag enabled, switching to WASM parser');
const wasm = new WasmParserFactory();
this.factory = wasm;
this.type = 'wasm';
this.readyPromise = wasm.initialize().catch((error: unknown) => {
log.error(error, 'WASM initialization failed, falling back to native');
this.factory = parserFactory;
this.type = 'native';
});
}
}
get parserType(): ParserType {
return this.type;
}
get ready(): Promise<void> {
return this.readyPromise;
}
createSyntaxTree(content: string, documentType: DocumentType): SyntaxTree {
this.telemetry.count('createSyntaxTree', 1, {
attributes: { 'parser.type': this.type },
});
if (documentType === DocumentType.JSON) {
return new JsonSyntaxTree(content, this.factory, this.type);
}
return new YamlSyntaxTree(content, this.factory, this.type);
}
}
export const syntaxTreeFactory = new SyntaxTreeFactory();