-
Notifications
You must be signed in to change notification settings - Fork 6
Wasm integration #47
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
Wasm integration #47
Changes from 25 commits
aae8c2d
7a60260
0afffff
8d61989
2d1e752
888cb6a
67787e2
17b350b
5470ef5
4bc3708
1894b80
4a40c6d
19d3e4c
8d25a15
2f68122
4e20d80
6df60ba
ef7305e
5e645f0
9886c29
703c800
61cb447
4754562
6a97e0b
8eadb84
7eb34be
a48a9cf
66236db
f77c1c5
5c87003
e0f3fdf
69f0dce
8a14d44
6b09d77
8996428
b2a0856
b6e9594
b797654
2cd120c
8cd876f
8619733
22bb77e
5fb6459
a7364fc
e0eee8b
18ff371
c915394
fbf3f64
c49c7f0
f441a82
b4457e6
3af3893
b702455
0a8782f
ae09eed
568a2ec
65449ae
e6ec4bc
a84f7e3
a7e344e
6d04379
02a12fa
f8f95f5
782c193
e99b903
9673ba3
3c0ae3f
98953f7
7ab6474
30e22e3
bf4e836
f7d6312
dcc4bd4
4a22fad
df60536
69693ce
704ce78
e54872d
5dd3417
4a79319
fffc451
e741821
c457908
3e92c17
8a1625d
4ebde26
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
build/ | ||
lama2 | ||
.vscode | ||
buildAndPublish.sh | ||
static/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//go:build wasm | ||
|
||
// Package `cmdexec` provides a facility to execute | ||
// l2 commands, stream output to stdout, while also | ||
// providing ability to retrieve the command output as | ||
// a string. | ||
package cmdexec | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"strings" | ||
|
||
"github.com/HexmosTech/httpie-go" | ||
) | ||
|
||
// ExecCommand changes directory to the given `apiDir` | ||
// and then executes the command specified in `cmdStr` | ||
// During command execution, ExecCommand streams output | ||
// to stdout. | ||
// Once execution finishes, previous CWD is restored, | ||
// and the command output is returned as a string | ||
func ExecCommand(cmdSlice []string, stdinBody string) (httpie.ExResponse, error) { | ||
proxyURL := "https://proxyserver.hexmos.com/" | ||
proxyUserName := "proxyServer" | ||
proxyUserPassword := "proxy22523146server" | ||
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. Passwords are open, variables are not declared for the URL, name, password. Is the password meant to be open? |
||
allowRedirects := true | ||
resp, err := httpie.Lama2Entry(cmdSlice, strings.NewReader(stdinBody), proxyURL, proxyUserName, proxyUserPassword, allowRedirects) | ||
if err != nil { | ||
fmt.Println("Got error while executing", err) | ||
return httpie.ExResponse{}, errors.New("Error from API executor: " + err.Error()) | ||
} | ||
return resp, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build cli | ||
|
||
package cmdexec | ||
|
||
import ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//go:build wasm | ||
|
||
package cmdexec | ||
|
||
import ( | ||
"syscall/js" | ||
) | ||
|
||
func RunVMCode(jsCode string) { | ||
js.Global().Call("eval", jsCode) | ||
} | ||
|
||
func GenerateChainCode(httpRespBody string) 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. can you add a comment on what this function is doing? what is generate chain code? |
||
code := `try { | ||
result = JSON.parse(String.raw` + "`" + httpRespBody + "`" + `) | ||
console.log("Stored as JSON") | ||
} catch (e) { | ||
result = String.raw` + "`" + httpRespBody + "`" + ` | ||
console.log(e) | ||
console.log("Stored as string") | ||
}` | ||
return code | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build cli | ||
|
||
// Package `cmdgen` provides an API to generate | ||
// API request commands (by default based on HTTPie) | ||
// based on the parsed API file contents and the `l2` | ||
|
@@ -17,35 +19,18 @@ import ( | |
|
||
func assembleCmdString(httpv string, url string, jsonObj *gabs.Container, headers *gabs.Container, multipart bool, form bool, o *lama2cmd.Opts) ([]string, string) { | ||
command := make([]string, 0) | ||
log.Info(). | ||
Str("Type", "Construct Command"). | ||
Str("httpv", httpv). | ||
Str("url", url). | ||
Bool("multipart", multipart). | ||
Bool("form", form). | ||
Msg(fmt.Sprint("Construct parameters")) | ||
|
||
log.Debug(). | ||
Str("JSONObj", jsonObj.String()). | ||
Str("Headers", headers.String()).Msg("") | ||
|
||
var files *gabs.Container | ||
if multipart { | ||
if jsonObj.ExistsP("@files") { | ||
files = jsonObj.S("@files") | ||
log.Debug().Str("Files", files.String()).Msg("") | ||
jsonObj.Delete("@files") | ||
log.Trace().Str("Shortened JsonObj", jsonObj.String()).Msg("") | ||
} | ||
} | ||
|
||
jsonStr := "" | ||
if jsonObj != nil && !multipart && !form { | ||
dst := &bytes.Buffer{} | ||
if err := json.Compact(dst, []byte(jsonObj.String())); err != nil { | ||
log.Fatal(). | ||
Str("Error", err.Error()). | ||
Msg("Couldn't minify JSON") | ||
} | ||
jsonStr = dst.String() | ||
} | ||
|
@@ -70,7 +55,8 @@ func assembleCmdString(httpv string, url string, jsonObj *gabs.Container, header | |
|
||
if multipart { | ||
for key, val := range jsonObj.Data().(*gabs.Container).ChildrenMap() { | ||
command = append(command, "'"+key+"'='"+val.Data().(string)+"' ") | ||
keyValuePair := fmt.Sprintf("%s=%s", key, val.Data().(string)) | ||
command = append(command, keyValuePair) | ||
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. Are these changes tested? |
||
} | ||
for key, val := range files.ChildrenMap() { | ||
command = append(command, key+"@"+val.Data().(string)) | ||
|
@@ -79,7 +65,7 @@ func assembleCmdString(httpv string, url string, jsonObj *gabs.Container, header | |
|
||
if form { | ||
for key, val := range jsonObj.Data().(*gabs.Container).ChildrenMap() { | ||
keyValuePair := fmt.Sprintf("'%s'='%s' ", key, val.Data().(string)) | ||
keyValuePair := fmt.Sprintf("%s=%s", key, val.Data().(string)) | ||
command = append(command, keyValuePair) | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
//go:build wasm | ||
|
||
package cmdgen | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/HexmosTech/gabs/v2" | ||
) | ||
|
||
func assembleCmdString(httpv string, url string, jsonObj *gabs.Container, headers *gabs.Container, multipart bool, form bool) ([]string, 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. add a comment about what is this function doing |
||
command := make([]string, 0) | ||
var files *gabs.Container | ||
if multipart { | ||
if jsonObj.ExistsP("@files") { | ||
files = jsonObj.S("@files") | ||
jsonObj.Delete("@files") | ||
} | ||
} | ||
|
||
jsonStr := "" | ||
if jsonObj != nil && !multipart && !form { | ||
dst := &bytes.Buffer{} | ||
if err := json.Compact(dst, []byte(jsonObj.String())); err != nil { | ||
} | ||
jsonStr = dst.String() | ||
} | ||
|
||
command = append(command, "ht ") | ||
if multipart || form { | ||
command = append(command, "--ignore-stdin", "--form") | ||
} | ||
|
||
command = append(command, httpv+" ") | ||
command = append(command, url+" ") | ||
|
||
if multipart { | ||
for key, val := range jsonObj.Data().(*gabs.Container).ChildrenMap() { | ||
command = append(command, "'"+key+"'='"+val.Data().(string)+"' ") | ||
} | ||
for key, val := range files.ChildrenMap() { | ||
command = append(command, key+"@"+val.Data().(string)) | ||
} | ||
} | ||
|
||
if form { | ||
for key, val := range jsonObj.Data().(*gabs.Container).ChildrenMap() { | ||
keyValuePair := fmt.Sprintf("'%s'='%s' ", key, val.Data().(string)) | ||
command = append(command, keyValuePair) | ||
} | ||
} | ||
|
||
if headers != nil { | ||
for key, val := range headers.Data().(*gabs.Container).ChildrenMap() { | ||
command = append(command, key+":"+val.Data().(*gabs.Container).Data().(string)) | ||
} | ||
} | ||
cleanCommand := make([]string, 0) | ||
for _, c := range command { | ||
cleanC := strings.TrimSpace(c) | ||
cleanCommand = append(cleanCommand, cleanC) | ||
} | ||
if multipart || form { | ||
return cleanCommand, "" | ||
} | ||
return cleanCommand, jsonStr | ||
} | ||
|
||
func ConstructCommand(parsedInput *gabs.Container) ([]string, string) { | ||
httpv := parsedInput.S("verb", "value") | ||
url := parsedInput.S("url", "value") | ||
jsonObj := parsedInput.S("details", "ip_data") | ||
headers := parsedInput.S("details", "headers") | ||
multipart := parsedInput.S("multipart", "value") | ||
multipartBool := false | ||
if multipart != nil { | ||
multipartBool = true | ||
} | ||
form := parsedInput.S("form", "value") | ||
formBool := form != nil | ||
|
||
res, stdinBody := assembleCmdString(httpv.Data().(string), url.Data().(string), jsonObj, headers, multipartBool, formBool) | ||
return res, stdinBody | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:wasm cli | ||
|
||
package codegen | ||
|
||
import ( | ||
|
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.
Is this done only for ones with tag cli, are there any other tags
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.
yes there is
wasm
tag also currently build is failing , checking that now