-
Notifications
You must be signed in to change notification settings - Fork 2
Add C# exporter backed by the .NET WASM converter #107
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
Open
flobernd
wants to merge
5
commits into
main
Choose a base branch
from
csharp-exporter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
91389a4
Add C# exporter backed by the .NET WASM converter
flobernd 8a76502
Keep JavaScript as the demo default language
flobernd 3bfaa2c
Reject non-Elasticsearch requests in the C# exporter
flobernd ee0a3c2
Add optional dotnet bundle peer dependency and real-bundle test
flobernd ac76527
Defer dynamic-import shim creation until the C# bundle loads
flobernd 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
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,94 @@ | ||
| import { existsSync } from "fs"; | ||
| import { resolve } from "path"; | ||
| import { pathToFileURL } from "url"; | ||
| import { | ||
| ConvertOptions, | ||
| ExternalExporter, | ||
| ExternalFormatExporter, | ||
| FormatExporter, | ||
| } from "../convert"; | ||
| import { ParsedRequest } from "../parse"; | ||
|
|
||
| const BUNDLE_PACKAGE = "@elastic/request-converter-dotnet"; | ||
|
|
||
| type DynamicImport = ( | ||
| specifier: string, | ||
| ) => Promise<{ boot?: () => Promise<ExternalFormatExporter> }>; | ||
|
|
||
| let dynamicImport: DynamicImport | undefined; | ||
|
|
||
| // This package compiles to CommonJS, where tsc downlevels `import()` to | ||
| // `require()`, which cannot load the ES-module WASM bundle. The indirection | ||
| // keeps a true dynamic import. Built lazily, on first use, so that merely | ||
| // importing this module does not run an eval-family construct under a CSP | ||
| // that forbids it (e.g. a host embedding the converter for other languages). | ||
| function getDynamicImport(): DynamicImport { | ||
| dynamicImport ??= new Function( | ||
| "specifier", | ||
| "return import(specifier)", | ||
| ) as DynamicImport; | ||
| return dynamicImport; | ||
| } | ||
|
|
||
| /** Converts requests to C# code for the .NET Elasticsearch client. | ||
| * | ||
| * Code generation runs inside the .NET WASM bundle shipped as the optional | ||
| * `@elastic/request-converter-dotnet` package, which embeds the | ||
| * `Elastic.Clients.Elasticsearch` client matching its own major.minor. | ||
| */ | ||
| export class CSharpExporter implements FormatExporter { | ||
| private exporter?: Promise<ExternalExporter>; | ||
|
|
||
| constructor(private readonly bundlePath?: string) {} | ||
|
|
||
| async check(requests: ParsedRequest[]): Promise<boolean> { | ||
| if (!requests.every((request) => request.service === "es")) { | ||
| return false; | ||
| } | ||
| return (await this.getExporter()).check(requests); | ||
| } | ||
|
|
||
| async convert( | ||
| requests: ParsedRequest[], | ||
| options: ConvertOptions, | ||
| ): Promise<string> { | ||
| if (!requests.every((request) => request.service === "es")) { | ||
| throw new Error("Cannot perform conversion"); | ||
| } | ||
| return (await this.getExporter()).convert(requests, { | ||
| document_type_name: "MyDocument", | ||
| syntax_mode: "descriptor", | ||
| use_strongly_typed_document: true, | ||
| ...options, | ||
| }); | ||
| } | ||
|
|
||
| private getExporter(): Promise<ExternalExporter> { | ||
| this.exporter ??= this.loadExporter(); | ||
| return this.exporter; | ||
| } | ||
|
|
||
| private async loadExporter(): Promise<ExternalExporter> { | ||
| const specifier = | ||
| this.bundlePath ?? | ||
| process.env.CSHARP_REQUEST_CONVERTER_BUNDLE ?? | ||
| BUNDLE_PACKAGE; | ||
| let module; | ||
| try { | ||
| // File-system specifiers need URL form on Windows (drive letters). | ||
| const importSpecifier = existsSync(specifier) | ||
| ? pathToFileURL(resolve(specifier)).href | ||
| : specifier; | ||
| module = await getDynamicImport()(importSpecifier); | ||
| } catch (error) { | ||
| throw new Error( | ||
| `The C# exporter requires the optional ${BUNDLE_PACKAGE} package ` + | ||
| `(npm install ${BUNDLE_PACKAGE}): ${(error as Error).message}`, | ||
| ); | ||
| } | ||
| if (typeof module.boot !== "function") { | ||
| throw new Error(`${specifier} does not export a boot() function.`); | ||
| } | ||
| return new ExternalExporter(await module.boot()); | ||
| } | ||
| } | ||
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,18 @@ | ||
| // Minimal stand-in for @elastic/request-converter-dotnet: echoes the options | ||
| // it received so tests can assert the exporter's defaults and overrides. | ||
| export async function boot() { | ||
| return { | ||
| check(input) { | ||
| const { requests } = JSON.parse(input); | ||
| return JSON.stringify({ return: requests.every((r) => Boolean(r.api)) }); | ||
| }, | ||
| convert(input) { | ||
| const { options, requests } = JSON.parse(input); | ||
| return JSON.stringify({ | ||
| return: `// apis: ${requests | ||
| .map((r) => r.api) | ||
| .join(",")}\n// options: ${JSON.stringify(options)}`, | ||
| }); | ||
| }, | ||
| }; | ||
| } |
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.
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.
Clever. 😮