/
editor.ts
242 lines (201 loc) · 7.64 KB
/
editor.ts
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import 'bluebird-global'
import * as sdk from 'botpress/sdk'
import fs from 'fs'
import _ from 'lodash'
import path from 'path'
import { Config } from '../config'
import { HOOK_SIGNATURES } from '../typings/hooks'
import { EditorError, EditorErrorStatus } from './editorError'
import { EditableFile, FileType, TypingDefinitions } from './typings'
const FILENAME_REGEX = /^[0-9a-zA-Z_\-.]+$/
const ALLOWED_TYPES = ['hook', 'action', 'bot_config']
export default class Editor {
private bp: typeof sdk
private _botId: string
private _typings: TypingDefinitions
private _config: Config
constructor(bp: typeof sdk, botId: string, config: Config) {
this.bp = bp
this._botId = botId
this._config = config
}
isGlobalAllowed() {
return this._config.allowGlobal
}
getConfig(): Config {
return this._config
}
async fetchFiles() {
return {
actionsGlobal: this._config.allowGlobal && this._filterBuiltin(await this._loadActions()),
hooksGlobal: this._config.allowGlobal && this._filterBuiltin(await this._loadHooks()),
actionsBot: this._filterBuiltin(await this._loadActions(this._botId)),
botConfigs: this._config.includeBotConfig && (await this._loadBotConfigs(this._config.allowGlobal))
}
}
private _filterBuiltin(files: EditableFile[]) {
return this._config.includeBuiltin ? files : files.filter(x => !x.content.includes('//CHECKSUM:'))
}
_validateMetadata({ name, botId, type, hookType, content }: Partial<EditableFile>) {
if (!botId || !botId.length) {
if (!this._config.allowGlobal) {
throw new Error(`Global files are restricted, please check your configuration`)
}
} else {
if (botId !== this._botId) {
throw new Error(`Please switch to the correct bot to change its actions.`)
}
}
if (!ALLOWED_TYPES.includes(type)) {
throw new Error(`Invalid file type, only ${ALLOWED_TYPES} are allowed at the moment`)
}
if (type === 'hook' && !HOOK_SIGNATURES[hookType]) {
throw new Error('Invalid hook type.')
}
if (type === 'bot_config') {
if (!this._config.includeBotConfig) {
throw new Error(`Enable "includeBotConfig" in the Code Editor configuration to save those files.`)
}
this._validateBotConfig(content)
}
this._validateFilename(name)
}
private _validateBotConfig(config: string) {
try {
JSON.parse(config)
return true
} catch (err) {
throw new EditorError(`Invalid JSON file. ${err}`, EditorErrorStatus.INVALID_NAME)
}
}
private _validateFilename(filename: string) {
if (!FILENAME_REGEX.test(filename)) {
throw new EditorError('Filename has invalid characters', EditorErrorStatus.INVALID_NAME)
}
}
private _getGhost(file: EditableFile): sdk.ScopedGhostService {
return file.botId ? this.bp.ghost.forBot(this._botId) : this.bp.ghost.forGlobal()
}
async saveFile(file: EditableFile): Promise<void> {
this._validateMetadata(file)
const { location, content, hookType, type } = file
const ghost = this._getGhost(file)
if (type === 'action') {
return ghost.upsertFile('/actions', location, content)
} else if (type === 'hook') {
return ghost.upsertFile(`/hooks/${hookType}`, location.replace(hookType, ''), content)
} else if (type === 'bot_config') {
return ghost.upsertFile('/', 'bot.config.json', content)
}
}
async deleteFile(file: EditableFile): Promise<void> {
this._validateMetadata(file)
const { location, hookType, type } = file
const ghost = this._getGhost(file)
if (type === 'action') {
return ghost.deleteFile('/actions', location)
}
if (type === 'hook') {
return ghost.deleteFile(`/hooks/${hookType}`, location.replace(hookType, ''))
}
}
async renameFile(file: EditableFile, newName: string): Promise<void> {
this._validateMetadata(file)
this._validateFilename(newName)
const { location, hookType, type, name } = file
const ghost = this._getGhost(file)
const newLocation = location.replace(name, newName)
if (type === 'action' && !(await ghost.fileExists('/actions', newLocation))) {
await ghost.renameFile('/actions', location, newLocation)
return
}
const hookLocation = location.replace(hookType, '')
const newHookLocation = hookLocation.replace(name, newName)
if (type === 'hook' && !(await ghost.fileExists(`/hooks/${hookType}`, newHookLocation))) {
await ghost.renameFile(`/hooks/${hookType}`, hookLocation, newHookLocation)
return
}
throw new EditorError('File already exists', EditorErrorStatus.FILE_ALREADY_EXIST)
}
async loadTypings() {
if (this._typings) {
return this._typings
}
const sdkTyping = fs.readFileSync(path.join(__dirname, '/../botpress.d.js'), 'utf-8')
const nodeTyping = fs.readFileSync(path.join(__dirname, `/../typings/node.d.js`), 'utf-8')
// Ideally we should fetch them locally, but for now it's safer to bundle it
const botSchema = fs.readFileSync(path.join(__dirname, '/../typings/bot.config.schema.json'), 'utf-8')
this._typings = {
'process.d.ts': this._buildRestrictedProcessVars(),
'node.d.ts': nodeTyping.toString(),
'botpress.d.ts': sdkTyping.toString().replace(`'botpress/sdk'`, `sdk`),
'bot.config.schema.json': botSchema.toString()
}
return this._typings
}
private async _loadActions(botId?: string): Promise<EditableFile[]> {
const ghost = botId ? this.bp.ghost.forBot(botId) : this.bp.ghost.forGlobal()
return Promise.map(ghost.directoryListing('/actions', '*.js', undefined, true), async (filepath: string) => {
return {
name: path.basename(filepath),
type: 'action' as FileType,
location: filepath,
content: await ghost.readFileAsString('/actions', filepath),
botId
}
})
}
private async _loadHooks(): Promise<EditableFile[]> {
const ghost = this.bp.ghost.forGlobal()
return Promise.map(ghost.directoryListing('/hooks', '*.js', undefined, true), async (filepath: string) => {
return {
name: path.basename(filepath),
type: 'hook' as FileType,
location: filepath,
hookType: filepath.substr(0, filepath.indexOf('/')),
content: await ghost.readFileAsString('/hooks', filepath)
}
})
}
private async _loadBotConfigs(includeAllBots: boolean): Promise<EditableFile[]> {
const ghost = includeAllBots ? this.bp.ghost.forBots() : this.bp.ghost.forBot(this._botId)
return Promise.map(ghost.directoryListing('/', 'bot.config.json', undefined, true), async (filepath: string) => {
return {
name: path.basename(filepath),
type: 'bot_config' as FileType,
botId: filepath.substr(0, filepath.indexOf('/')),
location: filepath,
content: await ghost.readFileAsString('/', filepath)
}
})
}
private _buildRestrictedProcessVars() {
const exposedEnv = {
..._.pickBy(process.env, (_value, name) => name.startsWith('EXPOSED_')),
..._.pick(process.env, 'TZ', 'LANG', 'LC_ALL', 'LC_CTYPE')
}
const root = this._extractInfo(_.pick(process, 'HOST', 'PORT', 'EXTERNAL_URL', 'PROXY'))
const exposed = this._extractInfo(exposedEnv)
return `
declare var process: RestrictedProcess;
interface RestrictedProcess {
${root.map(x => {
return `/** Current value: ${x.value} */
${x.name}: ${x.type}
`
})}
env: {
${exposed.map(x => {
return `/** Current value: ${x.value} */
${x.name}: ${x.type}
`
})}
}
}`
}
private _extractInfo(keys) {
return Object.keys(keys).map(name => {
return { name, value: keys[name], type: typeof keys[name] }
})
}
}