-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathconfigUtils.ts
More file actions
114 lines (97 loc) · 4.28 KB
/
Copy pathconfigUtils.ts
File metadata and controls
114 lines (97 loc) · 4.28 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Copyright 2025, Salesforce, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Workspace } from '@lwc/lwc-dev-server';
import { SSLCertificateData } from '@salesforce/lwc-dev-mobile-core';
import { Config, ConfigAggregator } from '@salesforce/core';
import configMeta, { ConfigVars, SerializedSSLCertificateData } from './../configMeta.js';
export const LOCAL_DEV_SERVER_DEFAULT_HTTP_PORT = 8081;
export const LOCAL_DEV_SERVER_DEFAULT_WORKSPACE = Workspace.SfCli;
export type LocalWebServerIdentityData = {
identityToken: string;
usernameToServerEntityIdMap: Record<string, string>;
};
export class ConfigUtils {
static #localConfig: Config;
static #globalConfig: Config;
public static async getLocalConfig(): Promise<Config> {
if (this.#localConfig) {
return this.#localConfig;
}
this.#localConfig = await Config.create({ isGlobal: false });
Config.addAllowedProperties(configMeta);
return this.#localConfig;
}
public static async getGlobalConfig(): Promise<Config> {
if (this.#globalConfig) {
return this.#globalConfig;
}
this.#globalConfig = await Config.create({ isGlobal: true });
Config.addAllowedProperties(configMeta);
return this.#globalConfig;
}
public static async getIdentityData(): Promise<LocalWebServerIdentityData | undefined> {
const config = await ConfigAggregator.create({ customConfigMeta: configMeta });
// Need to reload to make sure the values read are decrypted
await config.reload();
const identityJson = config.getPropertyValue(ConfigVars.LOCAL_WEB_SERVER_IDENTITY_DATA);
if (identityJson) {
return JSON.parse(identityJson as string) as LocalWebServerIdentityData;
}
return undefined;
}
public static async writeIdentityData(identityData: LocalWebServerIdentityData): Promise<void> {
const config = await this.getLocalConfig();
// TODO: JSON needs to be stringified in order for config.write to encrypt. When config.write()
// can encrypt JSON data to write it into config we shall remove stringify().
config.set(ConfigVars.LOCAL_WEB_SERVER_IDENTITY_DATA, JSON.stringify(identityData));
await config.write();
}
public static async getCertData(): Promise<SSLCertificateData | undefined> {
const config = await this.getGlobalConfig();
const serializedData = config.get(ConfigVars.LOCAL_DEV_SERVER_HTTPS_CERT_DATA) as SerializedSSLCertificateData;
if (serializedData) {
const deserializedData: SSLCertificateData = {
derCertificate: Buffer.from(serializedData.derCertificate, 'base64'),
pemCertificate: serializedData.pemCertificate,
pemPrivateKey: serializedData.pemPrivateKey,
pemPublicKey: serializedData.pemPublicKey,
};
return deserializedData;
}
return undefined;
}
public static async writeCertData(data: SSLCertificateData): Promise<void> {
const config = await this.getGlobalConfig();
const serializedData: SerializedSSLCertificateData = {
derCertificate: data.derCertificate.toString('base64'),
pemCertificate: data.pemCertificate,
pemPrivateKey: data.pemPrivateKey,
pemPublicKey: data.pemPublicKey,
};
config.set(ConfigVars.LOCAL_DEV_SERVER_HTTPS_CERT_DATA, serializedData);
await config.write();
}
public static async getLocalDevServerPorts(): Promise<{ httpPort: number; httpsPort: number } | undefined> {
const config = await this.getLocalConfig();
const ports = config.get(ConfigVars.LOCAL_DEV_SERVER_PORT) as { httpPort: number; httpsPort: number };
return ports;
}
public static async getLocalDevServerWorkspace(): Promise<Workspace | undefined> {
const config = await this.getLocalConfig();
const configWorkspace = config.get(ConfigVars.LOCAL_DEV_SERVER_WORKSPACE) as Workspace;
return configWorkspace;
}
}