/
IntrospectionEngine.ts
447 lines (422 loc) · 12.9 KB
/
IntrospectionEngine.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import chalk from 'chalk'
import { ChildProcess, spawn } from 'child_process'
import Debug from '@prisma/debug'
import byline from './utils/byline'
const debugRpc = Debug('IntrospectionEngine:rpc')
const debugStderr = Debug('IntrospectionEngine:stderr')
const debugStdin = Debug('IntrospectionEngine:stdin')
import fs from 'fs'
import { now } from './utils/now'
import { RustPanic, ErrorArea } from './panic'
import { resolveBinary } from './resolveBinary'
export interface IntrospectionEngineOptions {
binaryPath?: string
debug?: boolean
cwd?: string
}
export interface RPCPayload {
id: number
jsonrpc: string
method: string
params: any
}
export class IntrospectionPanic extends Error {
public request: any
public rustStack: string
constructor(message: string, rustStack: string, request: any) {
super(message)
this.rustStack = rustStack
this.request = request
}
}
export class IntrospectionError extends Error {
public code: string
constructor(message: string, code: string) {
super(message)
this.code = code
}
}
export type IntrospectionWarnings =
| IntrospectionWarningsMissingUnique
| IntrospectionWarningsEmptyFieldName
| IntrospectionWarningsUnsupportedType
| IntrospectionWarningsInvalidEnumName
interface IntrospectionWarningsMissingUnique {
code: 1
message: string
affected: { model: string }[]
}
interface IntrospectionWarningsEmptyFieldName {
code: 2
message: string
affected: { model: string; field: string }[]
}
interface IntrospectionWarningsUnsupportedType {
code: 3
message: string
affected: { model: string; field: string; tpe: string }[]
}
interface IntrospectionWarningsInvalidEnumName {
code: 4
message: string
affected: { enm: string; value: string }[]
}
export type IntrospectionSchemaVersion =
| 'Prisma2'
| 'Prisma1'
| 'Prisma11'
| 'NonPrisma'
let messageId = 1
/* tslint:disable */
export class IntrospectionEngine {
private debug: boolean
private cwd: string
private child?: ChildProcess
private listeners: { [key: string]: (result: any, err?: any) => any } = {}
private messages: string[] = []
private lastRequest?: any
private lastError?: any
private initPromise?: Promise<void>
private lastUrl?: string
public isRunning = false
constructor(
{ debug, cwd }: IntrospectionEngineOptions = {
debug: false,
cwd: process.cwd(),
},
) {
if (debug) {
Debug.enable('IntrospectionEngine*')
}
this.debug = Boolean(debug)
this.cwd = cwd || process.cwd()
}
public stop(): void {
if (this.child) {
this.child.kill()
this.isRunning = false
}
}
private rejectAll(err: any): void {
Object.entries(this.listeners).map(([id, listener]) => {
listener(null, err)
delete this.listeners[id]
})
}
private registerCallback(
id: number,
callback: (result: any, err?: Error) => any,
): void {
this.listeners[id] = callback
}
public getDatabaseDescription(schema: string): Promise<string> {
return this.runCommand(
this.getRPCPayload('getDatabaseDescription', { schema }),
)
}
public introspect(
schema: string,
): Promise<{
datamodel: string
warnings: IntrospectionWarnings[]
version: IntrospectionSchemaVersion
}> {
this.lastUrl = schema
return this.runCommand(this.getRPCPayload('introspect', { schema }))
}
public listDatabases(schema: string): Promise<string[]> {
this.lastUrl = schema
return this.runCommand(this.getRPCPayload('listDatabases', { schema }))
}
public getDatabaseMetadata(
schema: string,
): Promise<{ size_in_bytes: number; table_count: number }> {
this.lastUrl = schema
return this.runCommand(
this.getRPCPayload('getDatabaseMetadata', { schema }),
)
}
private handleResponse(response: any): void {
let result
try {
result = JSON.parse(response)
} catch (e) {
console.error(
`Could not parse introspection engine response: ${response.slice(
0,
200,
)}`,
)
}
if (result) {
if (result.backtrace) {
// if there is a backtrace on the result, it's probably an error
console.log(result)
}
if (!result.id) {
console.error(
`Response ${JSON.stringify(
result,
)} doesn't have an id and I can't handle that (yet)`,
)
}
if (!this.listeners[result.id]) {
console.error(`Got result for unknown id ${result.id}`)
}
if (this.listeners[result.id]) {
this.listeners[result.id](result)
delete this.listeners[result.id]
}
}
}
private init(): Promise<void> {
if (!this.initPromise) {
this.initPromise = this.internalInit()
}
return this.initPromise
}
private internalInit(): Promise<void> {
return new Promise(
// eslint-disable-next-line no-async-promise-executor, @typescript-eslint/no-misused-promises
async (resolve, reject): Promise<void> => {
try {
const binaryPath = await resolveBinary('introspection-engine')
debugRpc('starting introspection engine with binary: ' + binaryPath)
this.child = spawn(binaryPath, {
stdio: ['pipe', 'pipe', 'pipe'],
env: process.env,
cwd: this.cwd,
})
this.isRunning = true
this.child.on('error', (err) => {
console.error('[introspection-engine] error: %s', err)
reject(err)
this.rejectAll(err)
})
this.child.stdin?.on('error', (err) => {
console.error(err)
})
// eslint-disable-next-line @typescript-eslint/no-misused-promises
this.child.on('exit', async (code) => {
// handle panics
this.isRunning = false
if (code === 255 && this.lastError && this.lastError.is_panic) {
let sqlDump: string | undefined
if (this.lastUrl) {
try {
sqlDump = await this.getDatabaseDescription(this.lastUrl)
} catch (e) {} // eslint-disable-line no-empty
}
const err = new RustPanic(
this.lastError.message,
this.lastError.backtrace,
this.lastRequest,
ErrorArea.INTROSPECTION_CLI,
/* schemaPath */ undefined,
/* schema */ undefined,
sqlDump,
)
this.rejectAll(err)
reject(err)
return
}
const messages = this.messages.join('\n')
let err: any
if (code !== 0 || messages.includes('panicked at')) {
let errorMessage =
chalk.red.bold('Error in introspection engine: ') + messages
if (messages.includes('\u001b[1;94m-->\u001b[0m')) {
errorMessage =
`${chalk.red.bold('Schema parsing\n')}` + messages
} else if (this.lastError && this.lastError.msg === 'PANIC') {
errorMessage = serializePanic(this.lastError)
err = new IntrospectionPanic(
errorMessage,
messages,
this.lastRequest,
)
} else if (messages.includes('panicked at')) {
err = new IntrospectionPanic(
errorMessage,
messages,
this.lastRequest,
)
}
err = err || new Error(errorMessage)
this.rejectAll(err)
reject(err)
}
})
this.child.stdin!.on('error', (err) => {
debugStdin(err)
})
byline(this.child.stderr).on('data', (data) => {
const msg = String(data)
this.messages.push(msg)
debugStderr(msg)
try {
const json = JSON.parse(msg)
if (json.backtrace) {
this.lastError = json
}
if (json.level === 'ERRO') {
this.lastError = json
}
} catch (e) {
//
}
})
byline(this.child.stdout).on('data', (line) => {
this.handleResponse(String(line))
})
setImmediate(() => {
resolve()
})
} catch (e) {
reject(e)
}
},
)
}
private async runCommand(request: RPCPayload): Promise<any> {
await this.init()
if (this.child?.killed) {
throw new Error(
`Can't execute ${JSON.stringify(
request,
)} because introspection engine already exited.`,
)
}
return new Promise((resolve, reject) => {
this.registerCallback(request.id, async (response, err) => {
if (err) {
return reject(err)
}
if (typeof response.result !== 'undefined') {
resolve(response.result)
} else {
if (response.error) {
debugRpc(response)
if (response.error.data?.is_panic) {
const message =
response.error.data?.error?.message ?? response.error.message
// Handle error and displays the interactive dialog to send panic error
let sqlDump: string | undefined
if (this.lastUrl) {
try {
sqlDump = await this.getDatabaseDescription(this.lastUrl)
} catch (e) {} // eslint-disable-line no-empty
}
reject(
new RustPanic(
message,
message,
request,
ErrorArea.INTROSPECTION_CLI,
undefined,
undefined,
sqlDump,
),
)
} else if (response.error.data?.message) {
// Print known error code & message from engine
// See known errors at https://github.com/prisma/specs/tree/master/errors#prisma-sdk
let message = `${chalk.redBright(response.error.data.message)}\n`
if (response.error.data?.error_code) {
message =
chalk.redBright(`${response.error.data.error_code}\n\n`) +
message
reject(
new IntrospectionError(
message,
response.error.data.error_code,
),
)
} else {
reject(new Error(message))
}
} else {
const text = this.persistError(request, this.messages.join('\n'))
reject(
new Error(
`${chalk.redBright(
'Error in RPC',
)}\n Request: ${JSON.stringify(
request,
null,
2,
)}\nResponse: ${JSON.stringify(response, null, 2)}\n${
response.error.message
}\n\n${text}\n`,
),
)
}
} else {
reject(
new Error(
`Got invalid RPC response without .result property: ${JSON.stringify(
response,
)}`,
),
)
}
}
})
if (this.child!.stdin!.destroyed) {
throw new Error(
`Can't execute ${JSON.stringify(
request,
)} because introspection engine is destroyed.`,
)
}
debugRpc('SENDING RPC CALL', JSON.stringify(request))
this.child!.stdin!.write(JSON.stringify(request) + '\n')
this.lastRequest = request
})
}
private persistError(request: any, message: string): string {
const filename = `failed-${request.method}-${now()}.md`
const file = `# Failed ${request.method} at ${new Date().toISOString()}
## RPC One-Liner
\`\`\`json
${JSON.stringify(request)}
\`\`\`
## RPC Input Readable
\`\`\`json
${JSON.stringify(request, null, 2)}
\`\`\`
## Stack Trace
\`\`\`bash
${message}
\`\`\`
`
fs.writeFileSync(filename, file)
return `Wrote ${chalk.bold(filename)} with debugging information.
Please put that file into a gist and post it in Slack.
1. ${chalk.greenBright(`cat ${filename} | pbcopy`)}
2. Create a gist ${chalk.greenBright.underline(`https://gist.github.com/new`)}`
}
private getRPCPayload(method: string, params: any): RPCPayload {
return {
id: messageId++,
jsonrpc: '2.0',
method,
params: [
{
...params,
},
],
}
}
}
function serializePanic(log): string {
return `${chalk.red.bold(
'Error in introspection engine.\nReason: ',
)}${chalk.red(
`${log.reason} in ${chalk.underline(
`${log.file}:${log.line}:${log.column}`,
)}`,
)}
Please create an issue in the ${chalk.bold('prisma')} repo with the error 🙏:
${chalk.underline('https://github.com/prisma/prisma/issues/new')}\n`
}