-
Notifications
You must be signed in to change notification settings - Fork 317
Mock process before importing #8626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { Buffer } from "node:buffer"; | ||
| import { Readable as ReadableStream, Writable as WritableStream } from "node:stream"; | ||
|
|
||
| export function mockProcess(path: string): NodeJS.Process { | ||
| return { | ||
| ...process, | ||
| exit: () => { | ||
| throw new Error(`Importing ${path} tried to call process.exit`); | ||
| }, | ||
| stdin: readableNoopStream() as any, | ||
| stdout: writableNoopStream() as any, | ||
| on: (() => {}) as any, | ||
| }; | ||
| } | ||
|
|
||
| function readableNoopStream({ size = 0 } = {}) { | ||
| let producedSize = 0; | ||
|
|
||
| return new ReadableStream({ | ||
| read(readSize) { | ||
| let shouldEnd = false; | ||
|
|
||
| if (producedSize + readSize >= size) { | ||
| readSize = size - producedSize; | ||
| shouldEnd = true; | ||
| } | ||
|
|
||
| setImmediate(() => { | ||
| if (size === 0) { | ||
| this.push(null); | ||
| } | ||
|
|
||
| producedSize += readSize; | ||
| this.push(Buffer.alloc(readSize)); | ||
|
|
||
| if (shouldEnd) { | ||
| this.push(null); | ||
| } | ||
| }); | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| function writableNoopStream() { | ||
| return new WritableStream({ | ||
| write(chunk, encding, callback) { | ||
| setImmediate(callback); | ||
| }, | ||
| }); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ import { | |
| import { NodeHost } from "../core/node-host.js"; | ||
| import { typespecVersion } from "../manifest.js"; | ||
| import { createClientConfigProvider } from "./client-config-provider.js"; | ||
| import { mockProcess } from "./mock-process.js"; | ||
| import { createServer } from "./serverlib.js"; | ||
| import { CustomRequestName, Server, ServerHost, ServerLog } from "./types.js"; | ||
|
|
||
|
|
@@ -48,7 +49,19 @@ function main() { | |
| console.error = (data: any, ...args: any[]) => connection.console.error(format(data, ...args)); | ||
|
|
||
| const host: ServerHost = { | ||
| compilerHost: NodeHost, | ||
| compilerHost: { | ||
| ...NodeHost, | ||
| getJsImport: (path: string) => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. only doing this in the server right now but feels like this probably should just be in the |
||
| const oldProcess = globalThis.process; | ||
| try { | ||
| globalThis.process = mockProcess(path) as any; | ||
| const result = NodeHost.getJsImport(path); | ||
| return result; | ||
| } finally { | ||
| globalThis.process.stdin = oldProcess.stdin; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrong revert |
||
| } | ||
| }, | ||
| }, | ||
| sendDiagnostics(params: PublishDiagnosticsParams) { | ||
| void connection.sendDiagnostics(params); | ||
| }, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's better for loadJsFile to return a mock result or throw so that the caller can do something for the "bad" lib. otherwise, cache won't work for these "bad" lib and we will keep loading the "bad" lib every time unnecessary.