This repository has been archived by the owner on Jan 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
NodeEngine.ts
605 lines (536 loc) · 18 KB
/
NodeEngine.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
import {
PrismaClientKnownRequestError,
PrismaClientUnknownRequestError,
RequestError,
PrismaClientInitializationError,
PrismaClientRustPanicError,
getMessage,
} from './Engine'
import debugLib from 'debug'
import { getPlatform, Platform } from '@prisma/get-platform'
import path from 'path'
import net from 'net'
import fs from 'fs'
import chalk from 'chalk'
import { GeneratorConfig } from '@prisma/generator-helper'
import { printGeneratorConfig } from './printGeneratorConfig'
import { fixPlatforms, plusX } from './util'
import { promisify } from 'util'
import EventEmitter from 'events'
import { convertLog, RustLog, RustError } from './log'
import { spawn, ChildProcessWithoutNullStreams } from 'child_process'
import byline from './byline'
import bent from 'bent'
const debug = debugLib('engine')
const exists = promisify(fs.exists)
export interface DatasourceOverwrite {
name: string
url: string
}
export interface EngineConfig {
cwd?: string
datamodelPath: string
debug?: boolean
prismaPath?: string
fetcher?: (query: string) => Promise<{ data?: any; error?: any }>
generator?: GeneratorConfig
datasources?: DatasourceOverwrite[]
showColors?: boolean
logQueries?: boolean
logLevel?: 'info' | 'warn'
env?: Record<string, string>
flags?: string[]
}
/**
* Node.js based wrapper to run the Prisma binary
*/
const knownPlatforms: Platform[] = [
'native',
'darwin',
'debian-openssl-1.0.x',
'debian-openssl-1.1.x',
'rhel-openssl-1.0.x',
'rhel-openssl-1.1.x',
'windows',
]
export type Deferred = {
resolve: () => void
reject: (err: Error) => void
}
export class NodeEngine {
private logEmitter: EventEmitter
private showColors: boolean
private logQueries: boolean
private logLevel?: 'info' | 'warn'
private env?: Record<string, string>
private flags: string[]
port?: number
debug: boolean
child?: ChildProcessWithoutNullStreams
/**
* exiting is used to tell the .on('exit') hook, if the exit came from our script.
* As soon as the Prisma binary returns a correct return code (like 1 or 0), we don't need this anymore
*/
exiting: boolean = false
managementApiEnabled: boolean = false
datamodelJson?: string
cwd: string
datamodelPath: string
prismaPath?: string
url: string
ready: boolean = false
stderrLogs: string = ''
stdoutLogs: string = ''
currentRequestPromise?: any
cwdPromise: Promise<string>
platformPromise: Promise<Platform>
platform?: Platform | string
generator?: GeneratorConfig
incorrectlyPinnedPlatform?: string
datasources?: DatasourceOverwrite[]
lastErrorLog?: RustLog
lastError?: RustError
startPromise?: Promise<any>
engineStartDeferred?: Deferred
constructor({
cwd,
datamodelPath,
prismaPath,
generator,
datasources,
showColors,
logLevel,
logQueries,
env,
flags,
...args
}: EngineConfig) {
this.env = env
this.cwd = this.resolveCwd(cwd)
this.debug = args.debug || false
this.datamodelPath = datamodelPath
this.prismaPath = process.env.PRISMA_QUERY_ENGINE_BINARY || prismaPath
this.generator = generator
this.datasources = datasources
this.logEmitter = new EventEmitter()
this.showColors = showColors || false
this.logLevel = logLevel
this.logQueries = logQueries || false
this.flags = flags || []
this.logEmitter.on('error', (log: RustLog) => {
if (this.debug) {
debugLib('engine:log')(log)
}
this.lastErrorLog = log
if (log.fields.message === 'PANIC') {
this.handlePanic(log)
}
})
if (this.platform) {
if (!knownPlatforms.includes(this.platform as Platform) && !fs.existsSync(this.platform)) {
throw new PrismaClientInitializationError(
`Unknown ${chalk.red('PRISMA_QUERY_ENGINE_BINARY')} ${chalk.redBright.bold(
this.platform,
)}. Possible binaryTargets: ${chalk.greenBright(
knownPlatforms.join(', '),
)} or a path to the query engine binary.
You may have to run ${chalk.greenBright('prisma2 generate')} for your changes to take effect.`,
)
}
} else {
this.getPlatform()
}
if (this.debug) {
debugLib.enable('*')
}
}
private resolveCwd(cwd?: string): string {
if (cwd && fs.existsSync(cwd) && fs.lstatSync(cwd).isDirectory()) {
return cwd
}
return process.cwd()
}
on(event: 'query' | 'info' | 'warn', listener: (log: RustLog) => any) {
this.logEmitter.on(event, listener)
}
async getPlatform() {
if (this.platformPromise) {
return this.platformPromise
}
this.platformPromise = getPlatform()
return this.platformPromise
}
private getQueryEnginePath(platform: string, prefix: string = __dirname): string {
let queryEnginePath = path.join(prefix, `query-engine-${platform}`)
if (platform === 'windows') {
queryEnginePath = `${queryEnginePath}.exe`
}
return queryEnginePath
}
private handlePanic(log: RustLog) {
this.child.kill()
if (this.currentRequestPromise) {
;(this.currentRequestPromise as any).cancel()
}
}
private async resolvePrismaPath() {
if (this.prismaPath) {
return this.prismaPath
}
const platform = await this.getPlatform()
if (this.platform && this.platform !== platform) {
this.incorrectlyPinnedPlatform = this.platform
}
this.platform = this.platform || platform
const fileName = eval(`require('path').basename(__filename)`)
if (fileName === 'NodeEngine.js') {
return this.getQueryEnginePath(this.platform, path.resolve(__dirname, `..`))
} else {
return this.getQueryEnginePath(this.platform)
}
}
// get prisma path
private async getPrismaPath() {
const prismaPath = await this.resolvePrismaPath()
const platform = await this.getPlatform()
// If path to query engine doesn't exist, throw
if (!(await exists(prismaPath))) {
const pinnedStr = this.incorrectlyPinnedPlatform
? `\nYou incorrectly pinned it to ${chalk.redBright.bold(`${this.incorrectlyPinnedPlatform}`)}\n`
: ''
let errorText = `Query engine binary for current platform "${chalk.bold(
platform,
)}" could not be found.${pinnedStr}
This probably happens, because you built Prisma Client on a different platform.
(Prisma Client looked in "${chalk.underline(prismaPath)}")`
// The generator should always be there during normal usage
if (this.generator) {
// The user already added it, but it still doesn't work 🤷♀️
// That means, that some build system just deleted the files 🤔
if (this.generator.binaryTargets.includes(this.platform) || this.generator.binaryTargets.includes('native')) {
errorText += `\n\nYou already added the platform${
this.generator.binaryTargets.length > 1 ? 's' : ''
} ${this.generator.binaryTargets.map(t => `"${chalk.bold(t)}"`).join(', ')} to the "${chalk.underline(
'generator',
)}" block
in the "schema.prisma" file as described in https://pris.ly/d/client-generator,
but something went wrong. That's suboptimal.
Please create an issue at https://github.com/prisma/prisma-client-js/issues/new`
} else {
// If they didn't even have the current running platform in the schema.prisma file, it's easy
// Just add it
errorText += `\n\nTo solve this problem, add the platform "${this.platform}" to the "${chalk.underline(
'generator',
)}" block in the "schema.prisma" file:
${chalk.greenBright(this.getFixedGenerator())}
Then run "${chalk.greenBright('prisma2 generate')}" for your changes to take effect.
Read more about deploying Prisma Client: https://pris.ly/d/client-generator`
}
} else {
errorText += `\n\nRead more about deploying Prisma Client: https://pris.ly/d/client-generator\n`
}
throw new PrismaClientInitializationError(errorText)
}
if (this.incorrectlyPinnedPlatform) {
console.log(`${chalk.yellow('Warning:')} You pinned the platform ${chalk.bold(
this.incorrectlyPinnedPlatform,
)}, but Prisma Client detects ${chalk.bold(await this.getPlatform())}.
This means you should very likely pin the platform ${chalk.greenBright(await this.getPlatform())} instead.
${chalk.dim("In case we're mistaken, please report this to us 🙏.")}`)
}
if (process.platform !== 'win32') {
plusX(prismaPath)
}
return prismaPath
}
private getFixedGenerator() {
const fixedGenerator = {
...this.generator,
binaryTargets: fixPlatforms(this.generator.binaryTargets as Platform[], this.platform!),
}
return printGeneratorConfig(fixedGenerator)
}
printDatasources(): string {
if (this.datasources) {
return JSON.stringify(this.datasources)
}
return '[]'
}
/**
* Starts the engine, returns the url that it runs on
*/
async start(): Promise<void> {
if (!this.startPromise) {
this.startPromise = this.internalStart()
}
return this.startPromise
}
private internalStart(): Promise<void> {
return new Promise(async (resolve, reject) => {
try {
this.port = await this.getFreePort()
const env: any = {
PRISMA_DML_PATH: this.datamodelPath,
PORT: String(this.port),
RUST_BACKTRACE: '1',
RUST_LOG: 'info',
}
if (this.logQueries || this.logLevel === 'info') {
env.RUST_LOG = 'info'
if (this.logQueries) {
env.LOG_QUERIES = 'true'
}
}
if (this.logLevel === 'warn') {
env.RUST_LOG = 'warn'
}
if (this.datasources) {
env.OVERWRITE_DATASOURCES = this.printDatasources()
}
if (!process.env.NO_COLOR && this.showColors) {
env.CLICOLOR_FORCE = '1'
}
debug(env)
debug({ cwd: this.cwd })
const prismaPath = await this.getPrismaPath()
const flags = ['--enable-raw-queries', ...this.flags]
debug({ flags })
this.child = spawn(prismaPath, flags, {
env: {
...this.env, // user-provided env vars
...process.env,
...env,
},
cwd: this.cwd,
stdio: ['pipe', 'pipe', 'pipe'],
})
byline(this.child.stderr).on('data', msg => {
const data = String(msg)
debug('stderr', data)
try {
const json = JSON.parse(data)
if (typeof json.is_panic !== 'undefined') {
debug(json)
this.lastError = json
if (this.engineStartDeferred) {
this.engineStartDeferred.reject(new PrismaClientInitializationError(this.lastError.message))
}
}
} catch (e) {
if (!data.includes('Printing to stderr') && !data.includes('Listening on ')) {
this.stderrLogs += '\n' + data
}
}
})
byline(this.child.stdout).on('data', msg => {
const data = String(msg)
try {
const json = JSON.parse(data)
debug('stdout', json)
if (
this.engineStartDeferred &&
json.level === 'INFO' &&
json.target === 'prisma::server' &&
json.fields?.message.startsWith('Started http server')
) {
this.engineStartDeferred.resolve()
this.engineStartDeferred = undefined
}
if (typeof json.is_panic === 'undefined') {
const log = convertLog(json)
this.logEmitter.emit(log.level, log)
} else {
this.lastError = json
}
} catch (e) {
// debug(e, data)
}
})
this.child.on('exit', (code, signal) => {
if (!this.child) {
return
}
if (this.lastError) {
return
}
if (this.lastErrorLog) {
this.lastErrorLog.target = 'exit'
return
}
if (code === 126) {
this.lastErrorLog = {
timestamp: new Date(),
target: 'exit',
level: 'error',
fields: {
message: `Couldn't start query engine as it's not executable on this operating system.
You very likely have the wrong "binaryTarget" defined in the schema.prisma file.`,
},
}
} else {
this.lastErrorLog = {
target: 'exit',
timestamp: new Date(),
level: 'error',
fields: {
message: (this.stderrLogs || '') + (this.stdoutLogs || '') + code,
},
}
}
})
this.child.on('error', err => {
this.lastError = {
message: err.message,
backtrace: 'Could not start query engine',
is_panic: false,
}
reject(err)
})
this.child.on('close', (code, signal) => {
if (code === null && signal === 'SIGABRT' && this.child) {
console.error(`${chalk.bold.red(`Error in Prisma Client:`)}${this.stderrLogs}
This is a non-recoverable error which probably happens when the Prisma Query Engine has a stack overflow.
Please create an issue in https://github.com/prisma/prisma-client-js describing the last Prisma Client query you called.`)
} else if (code === 255 && signal === null && this.lastErrorLog?.fields.message === 'PANIC') {
console.error(`${chalk.bold.red(`Error in Prisma Client:`)}
${this.lastErrorLog.fields.message}: ${this.lastErrorLog.fields.reason} in
${this.lastErrorLog.fields.file}:${this.lastErrorLog.fields.line}:${this.lastErrorLog.fields.column}
This is a non-recoverable error which probably happens when the Prisma Query Engine has a panic.
Please create an issue in https://github.com/prisma/prisma-client-js describing the last Prisma Client query you called.`)
}
})
if (this.lastError) {
return reject(new PrismaClientInitializationError(getMessage(this.lastError)))
}
if (this.lastErrorLog) {
return reject(new PrismaClientInitializationError(getMessage(this.lastErrorLog)))
}
try {
await new Promise((resolve, reject) => {
this.engineStartDeferred = { resolve, reject }
})
} catch (err) {
await this.child.kill()
throw err
}
const url = `http://localhost:${this.port}`
this.url = url
// TODO: Re-enable
// this.client = new Client(url)
resolve()
} catch (e) {
reject(e)
}
})
}
fail = async (e, why) => {
debug(e, why)
await this.stop()
}
/**
* If Prisma runs, stop it
*/
async stop() {
await this.start()
if (this.currentRequestPromise) {
try {
await this.currentRequestPromise
} catch (e) {
//
}
}
if (this.child) {
debug(`Stopping Prisma engine`)
this.exiting = true
// this.client.close()
await this.child.kill()
delete this.child
}
}
/**
* Use the port 0 trick to get a new port
*/
protected getFreePort(): Promise<number> {
return new Promise((resolve, reject) => {
const server = net.createServer(s => s.end(''))
server.unref()
server.on('error', reject)
server.listen(0, () => {
const address = server.address()
const port = typeof address === 'string' ? parseInt(address.split(':').slice(-1)[0], 10) : address.port
server.close(e => {
if (e) {
reject(e)
}
resolve(port)
})
})
})
}
/**
* Make sure that our internal port is not conflicting with the prisma.yml's port
* @param str config
*/
protected trimPort(str: string) {
return str
.split('\n')
.filter(l => !l.startsWith('port:'))
.join('\n')
}
async request<T>(queries: string[]): Promise<T> {
await this.start()
if (!this.child) {
throw new PrismaClientUnknownRequestError(`Can't perform request, as the Engine has already been stopped`)
}
const variables = {}
const body = {
batch: queries.map(query => ({ query, variables })),
}
const post = bent(this.url, 'POST', 'json', 200)
this.currentRequestPromise = post('/', body)
return this.currentRequestPromise
.then(data => {
return data.map(result => {
if (result.errors) {
return this.graphQLToJSError(result.errors[0])
}
return result
})
})
.catch(error => {
debug({ error })
if (this.currentRequestPromise.isCanceled && this.lastError) {
// TODO: Replace these errors with known or unknown request errors
if (this.lastError.is_panic) {
throw new PrismaClientRustPanicError(getMessage(this.lastError))
} else {
throw new PrismaClientUnknownRequestError(getMessage(this.lastError))
}
}
if (this.currentRequestPromise.isCanceled && this.lastErrorLog) {
throw new PrismaClientUnknownRequestError(getMessage(this.lastErrorLog))
}
if ((error.code && error.code === 'ECONNRESET') || error.code === 'ECONNREFUSED') {
if (this.lastError) {
throw new PrismaClientUnknownRequestError(getMessage(this.lastError))
}
if (this.lastErrorLog) {
throw new PrismaClientUnknownRequestError(getMessage(this.lastErrorLog))
}
const logs = this.stderrLogs || this.stdoutLogs
throw new PrismaClientUnknownRequestError(logs)
}
throw error
})
}
private graphQLToJSError(error: RequestError): PrismaClientKnownRequestError | PrismaClientUnknownRequestError {
if (error.user_facing_error.error_code) {
return new PrismaClientKnownRequestError(
error.user_facing_error.message,
error.user_facing_error.error_code,
error.user_facing_error.meta,
)
}
return new PrismaClientUnknownRequestError(error.user_facing_error.message)
}
}