| 44 | * - Catch and throw "rich" connector errors |
| 45 | */ |
| 46 | export class SchemaEngineWasm implements SchemaEngine { |
| 47 | private engine: wasm.SchemaEngineWasm |
| 48 | private errorRegistry: ErrorRegistry |
| 49 | |
| 50 | // TODO: forward enabled preview features to the Wasm engine |
| 51 | private enabledPreviewFeatures?: string[] |
| 52 | |
| 53 | // `isRunning` is set to true when the engine is initialized, and set to false when the engine is stopped |
| 54 | public isRunning = false |
| 55 | |
| 56 | private constructor({ debug, enabledPreviewFeatures, engine, errorRegistry }: SchemaEngineWasmOptions) { |
| 57 | this.enabledPreviewFeatures = enabledPreviewFeatures |
| 58 | if (debug) { |
| 59 | Debug.enable('prisma:schemaEngine*') |
| 60 | } |
| 61 | this.engine = engine |
| 62 | this.errorRegistry = errorRegistry |
| 63 | } |
| 64 | |
| 65 | static async setup({ adapter, schemaContext, ...rest }: SchemaEngineWasmSetupInput): Promise<SchemaEngineWasm> { |
| 66 | const debug = (arg: string) => { |
| 67 | debugStderr(arg) |
| 68 | } |
| 69 | |
| 70 | // Note: `datamodels` must be either `undefined` or a *non-empty* `LoadedFile[]`. |
| 71 | const datamodels = schemaContext?.schemaFiles |
| 72 | const engine = await wasmSchemaEngineLoader.loadSchemaEngine( |
| 73 | { |
| 74 | datamodels, |
| 75 | }, |
| 76 | debug, |
| 77 | adapter, |
| 78 | ) |
| 79 | return new SchemaEngineWasm({ ...rest, engine, errorRegistry: adapter.errorRegistry }) |
| 80 | } |
| 81 | |
| 82 | private async runCommand<M extends keyof SchemaEngineMethods>( |
| 83 | command: M, |
| 84 | input: SchemaEngineInput<M>, |
| 85 | ): Promise<Awaited<SchemaEngineOutput<M>>> { |
| 86 | if (process.env.FORCE_PANIC_SCHEMA_ENGINE && command !== 'debugPanic') return this.debugPanic() |
| 87 | |
| 88 | this.isRunning = true |
| 89 | |
| 90 | debugStdout('[%s] input: %o', command, input) |
| 91 | |
| 92 | try { |
| 93 | // Don't modify this by extracting the method call into a variable, |
| 94 | // as it breaks the binding to the WebAssembly instance, and triggers the |
| 95 | // `TypeError: Cannot read properties of undefined (reading '__wbg_ptr')` |
| 96 | // error. |
| 97 | const result = await (this.engine[command] as (_: SchemaEngineInput<M>) => SchemaEngineOutput<M>)(input) |
| 98 | debugStdout('[%s] result: %o', command, result) |
| 99 | return result |
| 100 | } catch (error) { |
| 101 | const e = error as Error |
| 102 | debugStdout('[%s] error: %o', command, e) |
| 103 |
nothing calls this directly
no outgoing calls
no test coverage detected