Skip to content

Commit

Permalink
Review
Browse files Browse the repository at this point in the history
  • Loading branch information
fortuna committed Dec 3, 2024
1 parent 1f17917 commit f67e89c
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 49 deletions.
9 changes: 3 additions & 6 deletions client/electron/go_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ let invokeMethodFunc: Function | undefined;
* Ensure that the function signature and data structures are consistent with the C definitions
* in `./client/go/outline/electron/go_plugin.go`.
*/
export async function invokeMethod(
method: string,
input: string
): Promise<string> {
export async function invokeMethod(method: string, input: string): Promise<string> {

Check failure on line 35 in client/electron/go_plugin.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `method:·string,·input:·string` with `⏎··method:·string,⏎··input:·string⏎`
if (!invokeMethodFunc) {
const backendLib = koffi.load(pathToBackendLibrary());

Expand All @@ -47,12 +44,12 @@ export async function invokeMethod(
);

// Define InvokeMethod data structures and function
const invokeGoApiResult = koffi.struct('InvokeMethodResult', {
const invokeMethodResult = koffi.struct('InvokeMethodResult', {
Output: cgoString,
ErrorJson: cgoString,
});
invokeMethodFunc = promisify(
backendLib.func('InvokeMethod', invokeGoApiResult, ['str', 'str']).async
backendLib.func('InvokeMethod', invokeMethodResult, ['str', 'str']).async
);
}

Expand Down
4 changes: 2 additions & 2 deletions client/electron/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,8 @@ function main() {
// If the function encounters an error, it throws an Error that can be parsed by the `PlatformError`.
ipcMain.handle(
'outline-ipc-invoke-method',
async (_, method: string, params: string): Promise<string> =>
await invokeMethod(method, params)
(_, method: string, params: string): Promise<string> =>
invokeMethod(method, params)
);

// Connects to a proxy server specified by a config.
Expand Down
24 changes: 8 additions & 16 deletions client/go/outline/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,39 @@ import (
"github.com/Jigsaw-Code/outline-apps/client/go/outline/platerrors"
)

// fetchResourceResult represents the result of fetching a resource located at a URL.
//
// We use a struct instead of a tuple to preserve a strongly typed error that gobind recognizes.
type fetchResourceResult struct {
Content string
Error *platerrors.PlatformError
}

// fetchResource fetches a resource from the given URL.
//
// The function makes an HTTP GET request to the specified URL and returns the response body as a
// string. If the request fails or the server returns a non-2xx status code, an error is returned.
func fetchResource(url string) *fetchResourceResult {
func fetchResource(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return &fetchResourceResult{Error: &platerrors.PlatformError{
return "", platerrors.PlatformError{
Code: platerrors.FetchConfigFailed,
Message: "failed to fetch the URL",
Details: platerrors.ErrorDetails{"url": url},
Cause: platerrors.ToPlatformError(err),
}}
}
}
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode > 299 {
return &fetchResourceResult{Error: &platerrors.PlatformError{
return "", platerrors.PlatformError{
Code: platerrors.FetchConfigFailed,
Message: "non-successful HTTP status",
Details: platerrors.ErrorDetails{
"status": resp.Status,
"body": string(body),
},
}}
}
}
if err != nil {
return &fetchResourceResult{Error: &platerrors.PlatformError{
return "", platerrors.PlatformError{
Code: platerrors.FetchConfigFailed,
Message: "failed to read the body",
Details: platerrors.ErrorDetails{"url": url},
Cause: platerrors.ToPlatformError(err),
}}
}
}
return &fetchResourceResult{Content: string(body)}
return string(body), nil
}
11 changes: 5 additions & 6 deletions client/go/outline/method_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import (

// API name constants
const (
// FetchResourceMethod fetches a resource located at a given URL.
//
// FetchResource fetches a resource located at a given URL.
// - Input: the URL string of the resource to fetch
// - Output: the content in raw string of the fetched resource
MethodFetchResource = "FetchResource"
Expand All @@ -34,18 +33,18 @@ const (
// We use a struct instead of a tuple to preserve a strongly typed error that gobind recognizes.
type InvokeMethodResult struct {
Value string
Error *platerrors.PlatformError
Error *platerrors.PlatformError
}

// InvokeMethod calls a method by name.
func InvokeMethod(method string, input string) *InvokeMethodResult {
switch method {
case MethodFetchResource:
url := input
result := fetchResource(url)
content, err := fetchResource(url)
return &InvokeMethodResult{
Value: result.Content,
Error: result.Error,
Value: content,
Error: platerrors.ToPlatformError(err),
}

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private void executeAsync(
LOG.warning(String.format(Locale.ROOT, "InvokeMethod(%s) failed: %s", methodName, result.getError()));
sendActionResult(callback, result.getError());
} else {
LOG.fine(String.format(Locale.ROOT, "InvokeMethod result: %s", result.getValue()));
LOG.fine(String.format(Locale.ROOT, "InvokeMethod(%s) result: %s", methodName, result.getValue()));
callback.success(result.getValue());
}

Expand Down
11 changes: 2 additions & 9 deletions client/src/www/app/main.cordova.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,8 @@ class CordovaErrorReporter extends SentryErrorReporter {
}

class CordovaMethodChannel implements MethodChannel {
async invokeMethod(methodName: string, params: string): Promise<string> {
switch (methodName) {
default:
return await pluginExecWithErrorCode(
'invokeMethod',
methodName,
params
);
}
invokeMethod(methodName: string, params: string): Promise<string> {
return pluginExecWithErrorCode('invokeMethod', methodName, params);
}
}

Expand Down
15 changes: 6 additions & 9 deletions client/src/www/app/main.electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,12 @@ class ElectronErrorReporter implements OutlineErrorReporter {
}

class ElectronMethodChannel implements MethodChannel {
async invokeMethod(methodName: string, params: string): Promise<string> {
switch (methodName) {
default:
return await window.electron.methodChannel.invoke(
'invoke-method',
methodName,
params
);
}
invokeMethod(methodName: string, params: string): Promise<string> {
return window.electron.methodChannel.invoke(
'invoke-method',
methodName,
params
);
}
}

Expand Down

0 comments on commit f67e89c

Please sign in to comment.