-
Notifications
You must be signed in to change notification settings - Fork 49
feat: publish query filtering binary #1066
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
24 commits
Select commit
Hold shift + click to select a range
c3a09f5
feat: publish query filtering
Dodecahedr0x 07dce46
fix: rename file
Dodecahedr0x 7c08e53
Merge branch 'master' into dode/private-er-binairy
Dodecahedr0x 469d90c
Merge branch 'master' into dode/private-er-binairy
Dodecahedr0x 71bd33e
fix: wrong guard
Dodecahedr0x 40a5f4b
fix: exit with error
Dodecahedr0x ac0a4b3
fix: version
Dodecahedr0x 61940ef
Merge branch 'master' into dode/private-er-binairy
Dodecahedr0x d37aef3
Merge branch 'master' into dode/private-er-binairy
Dodecahedr0x 48d45c3
fix: exit immediately
Dodecahedr0x 73c8620
feat: handle async failure
Dodecahedr0x 308242e
Merge branch 'master' into dode/private-er-binairy
Dodecahedr0x ac64bd3
Merge branch 'master' into dode/private-er-binairy
GabrielePicco ebc329a
Merge branch 'master' into dode/private-er-binairy
Dodecahedr0x 388da9c
fix: check status
Dodecahedr0x bc58bc4
fix: remove sigterm
Dodecahedr0x 10678ab
fix: await result
Dodecahedr0x 137ff9e
Merge branch 'master' into dode/private-er-binairy
Dodecahedr0x 07598b7
Merge branch 'master' into dode/private-er-binairy
Dodecahedr0x 9d0a016
fix: remove duplicate run
Dodecahedr0x 12325b4
feat: update version
Dodecahedr0x 8328e3d
Merge branch 'master' into dode/private-er-binairy
Dodecahedr0x 7f8b10f
Merge branch 'master' into dode/private-er-binairy
Dodecahedr0x 6f48855
Merge branch 'master' into dode/private-er-binairy
GabrielePicco 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
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,128 @@ | ||
| #!/usr/bin/env node | ||
| import fs from "fs"; | ||
| import { spawn, spawnSync } from "child_process"; | ||
| import path from "path"; | ||
| import { arch, platform } from "os"; | ||
| import { VERSIONS } from "./getVersions"; | ||
|
|
||
| const PACKAGE_VERSION = `query-filtering-service ${VERSIONS.QUERY_FILTERING_SERVICE}`; | ||
|
|
||
| function getBinaryVersion(location: string): [string | null, string | null] { | ||
| const result = spawnSync(location, ["--version"]); | ||
| const error: string | null = | ||
| (result.error && result.error.toString()) || | ||
| (result.status !== 0 && `Process exited with code ${result.status}`) || | ||
| (result.stderr.length > 0 && result.stderr.toString().trim()) || | ||
| null; | ||
| return [error, result.stdout && result.stdout.toString().trim()]; | ||
| } | ||
|
|
||
| function getExePath(): string { | ||
| let os: string = platform(); | ||
| let extension = ""; | ||
| if (["win32", "cygwin"].includes(os)) { | ||
| os = "windows"; | ||
| extension = ".exe"; | ||
| } | ||
| const binaryName = `@magicblock-labs/query-filtering-service-${os}-${arch()}/bin/query-filtering-service${extension}`; | ||
|
Dodecahedr0x marked this conversation as resolved.
|
||
| try { | ||
| return require.resolve(binaryName); | ||
| } catch (e) { | ||
| throw new Error( | ||
| `Couldn't find application binary inside node_modules for ${os}-${arch()}, expected location: ${binaryName}`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| function runWithForwardedExit(child: ReturnType<typeof spawn>): void { | ||
| child.on("exit", (code: number | null, signal: NodeJS.Signals | null) => { | ||
| if (signal) { | ||
| process.kill(process.pid, signal); | ||
| return; | ||
| } | ||
| process.exit(code ?? 1); | ||
| }); | ||
|
Dodecahedr0x marked this conversation as resolved.
|
||
|
|
||
| process.on("SIGINT", () => { | ||
| child.kill("SIGINT"); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| function runQueryFilteringService(location: string): Promise<boolean> { | ||
| const args = process.argv.slice(2); | ||
| const env = { | ||
| ...process.env, | ||
| }; | ||
| const queryFilteringService = spawn(location, args, { | ||
| stdio: "inherit", | ||
| env, | ||
| }); | ||
| runWithForwardedExit(queryFilteringService); | ||
|
|
||
| return new Promise((resolve) => { | ||
| queryFilteringService.once("spawn", () => resolve(true)); | ||
| queryFilteringService.once("error", (error) => { | ||
| console.error("Failed to spawn query-filtering-service:", error.message); | ||
| resolve(false); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| async function tryPackageQueryFilteringService(): Promise<boolean> { | ||
| try { | ||
| const resolvedPath = getExePath(); | ||
| return await runQueryFilteringService(resolvedPath); | ||
| } catch (e) { | ||
| console.error( | ||
| "Failed to run query-filtering-service from package:", | ||
| e instanceof Error ? e.message : e, | ||
| ); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| async function trySystemQueryFilteringService(): Promise<void> { | ||
| const absolutePath = process.env.PATH?.split(path.delimiter) | ||
| .filter((dir) => dir !== path.dirname(process.argv[1])) | ||
| .find((dir) => { | ||
| try { | ||
| fs.accessSync(`${dir}/query-filtering-service`, fs.constants.X_OK); | ||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| }); | ||
|
|
||
| if (!absolutePath) { | ||
| console.error("Could not find globally installed query-filtering-service."); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const absoluteBinaryPath = `${absolutePath}/query-filtering-service`; | ||
| const [error, binaryVersion] = getBinaryVersion(absoluteBinaryPath); | ||
|
|
||
| if (error !== null) { | ||
| console.error(`Failed to get version of global binary: ${error}`); | ||
| process.exit(1); | ||
| } | ||
| if (binaryVersion !== PACKAGE_VERSION) { | ||
| console.error( | ||
| `Globally installed query-filtering-service version is not correct. Expected "${PACKAGE_VERSION}", found "${binaryVersion}".`, | ||
| ); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const success = await runQueryFilteringService(absoluteBinaryPath); | ||
| if (!success) { | ||
| console.error("Failed to spawn query-filtering-service from system PATH."); | ||
| process.exit(1); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| async function main(): Promise<void> { | ||
| if (!(await tryPackageQueryFilteringService())) { | ||
| await trySystemQueryFilteringService(); | ||
| } | ||
| } | ||
|
|
||
| void main(); | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.