-
-
Notifications
You must be signed in to change notification settings - Fork 229
Offload Oxide scanning to separate process #1471
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env node | ||
|
||
import * as rpc from 'vscode-jsonrpc/node' | ||
import { scan, type ScanOptions, type ScanResult } from './oxide' | ||
|
||
let connection = rpc.createMessageConnection( | ||
new rpc.IPCMessageReader(process), | ||
new rpc.IPCMessageWriter(process), | ||
) | ||
|
||
let scanRequest = new rpc.RequestType<ScanOptions, ScanResult, void>('scan') | ||
connection.onRequest<ScanOptions, ScanResult, void>(scanRequest, (options) => scan(options)) | ||
|
||
connection.listen() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import * as rpc from 'vscode-jsonrpc/node' | ||
import * as proc from 'node:child_process' | ||
import * as path from 'node:path' | ||
import * as fs from 'node:fs/promises' | ||
import { type ScanOptions, type ScanResult } from './oxide' | ||
|
||
/** | ||
* This helper starts a session in which we can use Oxide in *another process* | ||
* to communicate content scanning results. | ||
* | ||
* Thie exists for two reasons: | ||
* - The Oxide API has changed over time so this function presents a unified | ||
* interface that works with all versions of the Oxide API. The results may | ||
* vary but the structure of the results will always be identical. | ||
* | ||
* - Requiring a native node module on Windows permanently keeps an open handle | ||
* to the binary for the duration of the process. This prevents unlinking the | ||
* file like happens when running `npm ci`. Running an ephemeral process lets | ||
* us sidestep the problem as the process will only be running as needed. | ||
*/ | ||
export class OxideSession { | ||
helper: proc.ChildProcess | null = null | ||
connection: rpc.MessageConnection | null = null | ||
|
||
public async scan(options: ScanOptions): Promise<ScanResult> { | ||
await this.startIfNeeded() | ||
|
||
return await this.connection.sendRequest('scan', options) | ||
} | ||
|
||
async startIfNeeded(): Promise<void> { | ||
if (this.connection) return | ||
|
||
// TODO: Can we find a way to not require a build first? | ||
// let module = path.resolve(path.dirname(__filename), './oxide-helper.ts') | ||
|
||
let modulePaths = [ | ||
// Separate Language Server package | ||
'../bin/oxide-helper.js', | ||
|
||
// Bundled with the VSCode extension | ||
'../dist/oxide-helper.js', | ||
] | ||
|
||
let module: string | null = null | ||
|
||
for (let relativePath of modulePaths) { | ||
let filepath = path.resolve(path.dirname(__filename), relativePath) | ||
|
||
if ( | ||
await fs.access(filepath).then( | ||
() => true, | ||
() => false, | ||
) | ||
) { | ||
module = filepath | ||
break | ||
} | ||
} | ||
|
||
if (!module) throw new Error('unable to load') | ||
|
||
let helper = proc.fork(module) | ||
let connection = rpc.createMessageConnection( | ||
new rpc.IPCMessageReader(helper), | ||
new rpc.IPCMessageWriter(helper), | ||
) | ||
|
||
helper.on('disconnect', () => { | ||
connection.dispose() | ||
this.connection = null | ||
this.helper = null | ||
}) | ||
|
||
helper.on('exit', () => { | ||
connection.dispose() | ||
this.connection = null | ||
this.helper = null | ||
}) | ||
|
||
connection.listen() | ||
|
||
this.helper = helper | ||
this.connection = connection | ||
} | ||
|
||
async stop() { | ||
if (!this.helper) return | ||
|
||
this.helper.disconnect() | ||
this.helper.kill() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import '@tailwindcss/language-server/src/oxide-helper' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Still needed?
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.
Yeah, I'm leaving this in as a note for me to look into later. Right now editing the helper file and re-running tests requires a rebuild. But this is "fine" for now.
CI already does this (b/c a few tests have to actually test against the built version for reasons).