Removed dependency of dennwc\dom\js
This removes the dependency of dennwc\dom\js which allows building with Go 1.13.
This commit is contained in:
parent
42bcead627
commit
3ac5af4548
1
go.mod
1
go.mod
@ -7,7 +7,6 @@ require (
|
||||
github.com/cenkalti/backoff v2.1.1+incompatible // indirect
|
||||
github.com/chromedp/cdproto v0.0.0-20190412020601-c4267f5c421a // indirect
|
||||
github.com/containerd/continuity v0.0.0-20181203112020-004b46473808 // indirect
|
||||
github.com/dennwc/dom v0.3.0
|
||||
github.com/gorilla/sessions v1.1.3
|
||||
github.com/gorilla/websocket v1.4.0
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
|
||||
|
@ -3,6 +3,15 @@
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
(() => {
|
||||
// Map multiple JavaScript environments to a single common API,
|
||||
// preferring web standards over Node.js API.
|
||||
//
|
||||
// Environments considered:
|
||||
// - Browsers
|
||||
// - Node.js
|
||||
// - Electron
|
||||
// - Parcel
|
||||
|
||||
if (typeof global !== "undefined") {
|
||||
// global already exists
|
||||
} else if (typeof window !== "undefined") {
|
||||
@ -13,30 +22,15 @@
|
||||
throw new Error("cannot export Go (neither global, window nor self is defined)");
|
||||
}
|
||||
|
||||
// Map web browser API and Node.js API to a single common API (preferring web standards over Node.js API).
|
||||
const isNodeJS = global.process && global.process.title === "node";
|
||||
if (isNodeJS) {
|
||||
if (!global.require && typeof require !== "undefined") {
|
||||
global.require = require;
|
||||
}
|
||||
|
||||
if (!global.fs && global.require) {
|
||||
global.fs = require("fs");
|
||||
}
|
||||
|
||||
const nodeCrypto = require("crypto");
|
||||
global.crypto = {
|
||||
getRandomValues(b) {
|
||||
nodeCrypto.randomFillSync(b);
|
||||
},
|
||||
};
|
||||
|
||||
global.performance = {
|
||||
now() {
|
||||
const [sec, nsec] = process.hrtime();
|
||||
return sec * 1000 + nsec / 1000000;
|
||||
},
|
||||
};
|
||||
|
||||
const util = require("util");
|
||||
global.TextEncoder = util.TextEncoder;
|
||||
global.TextDecoder = util.TextDecoder;
|
||||
} else {
|
||||
if (!global.fs) {
|
||||
let outputBuf = "";
|
||||
global.fs = {
|
||||
constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 }, // unused
|
||||
@ -72,6 +66,34 @@
|
||||
};
|
||||
}
|
||||
|
||||
if (!global.crypto) {
|
||||
const nodeCrypto = require("crypto");
|
||||
global.crypto = {
|
||||
getRandomValues(b) {
|
||||
nodeCrypto.randomFillSync(b);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (!global.performance) {
|
||||
global.performance = {
|
||||
now() {
|
||||
const [sec, nsec] = process.hrtime();
|
||||
return sec * 1000 + nsec / 1000000;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (!global.TextEncoder) {
|
||||
global.TextEncoder = require("util").TextEncoder;
|
||||
}
|
||||
|
||||
if (!global.TextDecoder) {
|
||||
global.TextDecoder = require("util").TextDecoder;
|
||||
}
|
||||
|
||||
// End of polyfills for common API.
|
||||
|
||||
const encoder = new TextEncoder("utf-8");
|
||||
const decoder = new TextDecoder("utf-8");
|
||||
|
||||
@ -243,7 +265,15 @@
|
||||
const id = this._nextCallbackTimeoutID;
|
||||
this._nextCallbackTimeoutID++;
|
||||
this._scheduledTimeouts.set(id, setTimeout(
|
||||
() => { this._resume(); },
|
||||
() => {
|
||||
this._resume();
|
||||
while (this._scheduledTimeouts.has(id)) {
|
||||
// for some reason Go failed to register the timeout event, log and try again
|
||||
// (temporary workaround for https://github.com/golang/go/issues/28975)
|
||||
console.warn("scheduleTimeoutEvent: missed timeout event");
|
||||
this._resume();
|
||||
}
|
||||
},
|
||||
getInt64(sp + 8) + 1, // setTimeout has been seen to fire up to 1 millisecond early
|
||||
));
|
||||
mem().setInt32(sp + 16, id, true);
|
||||
@ -357,6 +387,34 @@
|
||||
mem().setUint8(sp + 24, loadValue(sp + 8) instanceof loadValue(sp + 16));
|
||||
},
|
||||
|
||||
// func copyBytesToGo(dst []byte, src ref) (int, bool)
|
||||
"syscall/js.copyBytesToGo": (sp) => {
|
||||
const dst = loadSlice(sp + 8);
|
||||
const src = loadValue(sp + 32);
|
||||
if (!(src instanceof Uint8Array)) {
|
||||
mem().setUint8(sp + 48, 0);
|
||||
return;
|
||||
}
|
||||
const toCopy = src.subarray(0, dst.length);
|
||||
dst.set(toCopy);
|
||||
setInt64(sp + 40, toCopy.length);
|
||||
mem().setUint8(sp + 48, 1);
|
||||
},
|
||||
|
||||
// func copyBytesToJS(dst ref, src []byte) (int, bool)
|
||||
"syscall/js.copyBytesToJS": (sp) => {
|
||||
const dst = loadValue(sp + 8);
|
||||
const src = loadSlice(sp + 16);
|
||||
if (!(dst instanceof Uint8Array)) {
|
||||
mem().setUint8(sp + 48, 0);
|
||||
return;
|
||||
}
|
||||
const toCopy = src.subarray(0, dst.length);
|
||||
dst.set(toCopy);
|
||||
setInt64(sp + 40, toCopy.length);
|
||||
mem().setUint8(sp + 48, 1);
|
||||
},
|
||||
|
||||
"debug": (value) => {
|
||||
console.log(value);
|
||||
},
|
||||
@ -373,7 +431,6 @@
|
||||
true,
|
||||
false,
|
||||
global,
|
||||
this._inst.exports.mem,
|
||||
this,
|
||||
];
|
||||
this._refs = new Map();
|
||||
@ -385,9 +442,13 @@
|
||||
let offset = 4096;
|
||||
|
||||
const strPtr = (str) => {
|
||||
let ptr = offset;
|
||||
new Uint8Array(mem.buffer, offset, str.length + 1).set(encoder.encode(str + "\0"));
|
||||
offset += str.length + (8 - (str.length % 8));
|
||||
const ptr = offset;
|
||||
const bytes = encoder.encode(str + "\0");
|
||||
new Uint8Array(mem.buffer, offset, bytes.length).set(bytes);
|
||||
offset += bytes.length;
|
||||
if (offset % 8 !== 0) {
|
||||
offset += 8 - (offset % 8);
|
||||
}
|
||||
return ptr;
|
||||
};
|
||||
|
||||
@ -439,9 +500,15 @@
|
||||
}
|
||||
}
|
||||
|
||||
if (isNodeJS) {
|
||||
if (
|
||||
global.require &&
|
||||
global.require.main === module &&
|
||||
global.process &&
|
||||
global.process.versions &&
|
||||
!global.process.versions.electron
|
||||
) {
|
||||
if (process.argv.length < 3) {
|
||||
process.stderr.write("usage: go_js_wasm_exec [wasm binary] [arguments]\n");
|
||||
console.error("usage: go_js_wasm_exec [wasm binary] [arguments]");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
@ -459,7 +526,8 @@
|
||||
});
|
||||
return go.run(result.instance);
|
||||
}).catch((err) => {
|
||||
throw err;
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
})();
|
||||
|
71
wasm/main.go
71
wasm/main.go
@ -9,7 +9,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/dennwc/dom/js"
|
||||
"syscall/js"
|
||||
|
||||
"github.com/zorchenhimer/MovieNight/common"
|
||||
)
|
||||
|
||||
@ -17,21 +18,22 @@ var (
|
||||
timestamp bool
|
||||
color string
|
||||
auth common.CommandLevel
|
||||
global js.Value
|
||||
)
|
||||
|
||||
func getElement(s string) js.Value {
|
||||
return js.Get("document").Call("getElementById", s)
|
||||
return global.Get("document").Call("getElementById", s)
|
||||
}
|
||||
|
||||
func join(v []js.Value) {
|
||||
color := js.Call("getCookie", "color").String()
|
||||
color := global.Call("getCookie", "color").String()
|
||||
if color == "" {
|
||||
// If a color is not set, do a random color
|
||||
color = common.RandomColor()
|
||||
} else if !common.IsValidColor(color) {
|
||||
// Don't show the user the error, just clear the cookie
|
||||
common.LogInfof("%#v is not a valid color, clearing cookie", color)
|
||||
js.Call("deleteCookie", "color")
|
||||
global.Call("deleteCookie", "color")
|
||||
}
|
||||
|
||||
joinData, err := json.Marshal(common.JoinData{
|
||||
@ -51,7 +53,7 @@ func join(v []js.Value) {
|
||||
common.LogErrorf("Could not marshal data: %v", err)
|
||||
}
|
||||
|
||||
js.Call("websocketSend", string(data))
|
||||
global.Call("websocketSend", string(data))
|
||||
}
|
||||
|
||||
func recieve(v []js.Value) {
|
||||
@ -63,7 +65,7 @@ func recieve(v []js.Value) {
|
||||
chatJSON, err := common.DecodeData(v[0].String())
|
||||
if err != nil {
|
||||
fmt.Printf("Error decoding data: %s\n", err)
|
||||
js.Call("appendMessages", fmt.Sprintf("<div>%v</div>", v))
|
||||
global.Call("appendMessages", fmt.Sprintf("<div>%v</div>", v))
|
||||
return
|
||||
}
|
||||
|
||||
@ -86,7 +88,7 @@ func recieve(v []js.Value) {
|
||||
auth = h.Data.(common.CommandLevel)
|
||||
case common.CdColor:
|
||||
color = h.Data.(string)
|
||||
js.Get("document").Set("cookie", fmt.Sprintf("color=%s; expires=Fri, 31 Dec 9999 23:59:59 GMT", color))
|
||||
global.Get("document").Set("cookie", fmt.Sprintf("color=%s; expires=Fri, 31 Dec 9999 23:59:59 GMT", color))
|
||||
case common.CdEmote:
|
||||
data := h.Data.(map[string]interface{})
|
||||
emoteNames = make([]string, 0, len(data))
|
||||
@ -98,7 +100,7 @@ func recieve(v []js.Value) {
|
||||
sort.Strings(emoteNames)
|
||||
case common.CdJoin:
|
||||
notify("")
|
||||
js.Call("openChat")
|
||||
global.Call("openChat")
|
||||
case common.CdNotify:
|
||||
notify(h.Data.(string))
|
||||
}
|
||||
@ -126,18 +128,18 @@ func recieve(v []js.Value) {
|
||||
switch d.Command {
|
||||
case common.CmdPlaying:
|
||||
if d.Arguments == nil || len(d.Arguments) == 0 {
|
||||
js.Call("setPlaying", "", "")
|
||||
global.Call("setPlaying", "", "")
|
||||
|
||||
} else if len(d.Arguments) == 1 {
|
||||
js.Call("setPlaying", d.Arguments[0], "")
|
||||
global.Call("setPlaying", d.Arguments[0], "")
|
||||
|
||||
} else if len(d.Arguments) == 2 {
|
||||
js.Call("setPlaying", d.Arguments[0], d.Arguments[1])
|
||||
global.Call("setPlaying", d.Arguments[0], d.Arguments[1])
|
||||
}
|
||||
case common.CmdRefreshPlayer:
|
||||
js.Call("initPlayer", nil)
|
||||
global.Call("initPlayer", nil)
|
||||
case common.CmdPurgeChat:
|
||||
js.Call("purgeChat", nil)
|
||||
global.Call("purgeChat", nil)
|
||||
appendMessage(d.HTML())
|
||||
case common.CmdHelp:
|
||||
url := "/help"
|
||||
@ -145,13 +147,13 @@ func recieve(v []js.Value) {
|
||||
url = d.Arguments[0]
|
||||
}
|
||||
appendMessage(d.HTML())
|
||||
js.Get("window").Call("open", url, "_blank", "menubar=0,status=0,toolbar=0,width=300,height=600")
|
||||
global.Get("window").Call("open", url, "_blank", "menubar=0,status=0,toolbar=0,width=300,height=600")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func appendMessage(msg string) {
|
||||
js.Call("appendMessages", "<div>"+msg+"</div>")
|
||||
global.Call("appendMessages", "<div>"+msg+"</div>")
|
||||
}
|
||||
|
||||
func websocketSend(msg string, dataType common.ClientDataType) error {
|
||||
@ -167,7 +169,7 @@ func websocketSend(msg string, dataType common.ClientDataType) error {
|
||||
return fmt.Errorf("could not marshal data: %v", err)
|
||||
}
|
||||
|
||||
js.Call("websocketSend", string(data))
|
||||
global.Call("websocketSend", string(data))
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -188,12 +190,12 @@ func send(this js.Value, v []js.Value) interface{} {
|
||||
func showChatError(err error) {
|
||||
if err != nil {
|
||||
fmt.Printf("Could not send: %v\n", err)
|
||||
js.Call("appendMessages", `<div><span style="color: red;">Could not send message</span></div>`)
|
||||
global.Call("appendMessages", `<div><span style="color: red;">Could not send message</span></div>`)
|
||||
}
|
||||
}
|
||||
|
||||
func notify(msg string) {
|
||||
js.Call("setNotifyBox", msg)
|
||||
global.Call("setNotifyBox", msg)
|
||||
}
|
||||
|
||||
func showTimestamp(v []js.Value) {
|
||||
@ -227,17 +229,19 @@ func debugValues(v []js.Value) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
global = js.Global()
|
||||
|
||||
common.SetupLogging(common.LLDebug, "")
|
||||
|
||||
js.Set("processMessageKey", js.FuncOf(processMessageKey))
|
||||
js.Set("sendMessage", js.FuncOf(send))
|
||||
js.Set("isValidColor", js.FuncOf(isValidColor))
|
||||
global.Set("processMessageKey", js.FuncOf(processMessageKey))
|
||||
global.Set("sendMessage", js.FuncOf(send))
|
||||
global.Set("isValidColor", js.FuncOf(isValidColor))
|
||||
|
||||
js.Set("recieveMessage", js.CallbackOf(recieve))
|
||||
js.Set("processMessage", js.CallbackOf(processMessage))
|
||||
js.Set("debugValues", js.CallbackOf(debugValues))
|
||||
js.Set("showTimestamp", js.CallbackOf(showTimestamp))
|
||||
js.Set("join", js.CallbackOf(join))
|
||||
global.Set("recieveMessage", jsCallbackOf(recieve))
|
||||
global.Set("processMessage", jsCallbackOf(processMessage))
|
||||
global.Set("debugValues", jsCallbackOf(debugValues))
|
||||
global.Set("showTimestamp", jsCallbackOf(showTimestamp))
|
||||
global.Set("join", jsCallbackOf(join))
|
||||
|
||||
go func() {
|
||||
time.Sleep(time.Second * 1)
|
||||
@ -246,15 +250,26 @@ func main() {
|
||||
inner += fmt.Sprintf(`<option value="%s">%s</option>\n`, c, c)
|
||||
}
|
||||
|
||||
js.Get("colorSelect").Set("innerHTML", inner)
|
||||
global.Get("colorSelect").Set("innerHTML", inner)
|
||||
}()
|
||||
|
||||
// This is needed so the goroutine does not end
|
||||
for {
|
||||
// heatbeat to keep connection alive to deal with nginx
|
||||
if js.Get("inChat").Bool() {
|
||||
if global.Get("inChat").Bool() {
|
||||
websocketSend("", common.CdPing)
|
||||
}
|
||||
time.Sleep(time.Second * 10)
|
||||
}
|
||||
}
|
||||
|
||||
func jsCallbackOf(fnc func(v []js.Value)) js.Func {
|
||||
return js.FuncOf(func(this js.Value, refs []js.Value) interface{} {
|
||||
vals := make([]js.Value, 0, len(refs))
|
||||
for _, ref := range refs {
|
||||
vals = append(vals, ref)
|
||||
}
|
||||
fnc(vals)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
@ -5,7 +5,8 @@ package main
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/dennwc/dom/js"
|
||||
"syscall/js"
|
||||
|
||||
"github.com/zorchenhimer/MovieNight/common"
|
||||
)
|
||||
|
||||
@ -70,7 +71,7 @@ func processMessageKey(this js.Value, v []js.Value) interface{} {
|
||||
}
|
||||
currentSug = filteredSug[newidx]
|
||||
case keyTab, keyEnter:
|
||||
msg := js.Get("msg")
|
||||
msg := global.Get("msg")
|
||||
val := msg.Get("value").String()
|
||||
newval := val[:startIdx]
|
||||
|
||||
@ -104,7 +105,7 @@ func processMessageKey(this js.Value, v []js.Value) interface{} {
|
||||
}
|
||||
|
||||
func processMessage(v []js.Value) {
|
||||
msg := js.Get("msg")
|
||||
msg := global.Get("msg")
|
||||
text := strings.ToLower(msg.Get("value").String())
|
||||
startIdx := msg.Get("selectionStart").Int()
|
||||
|
||||
@ -189,6 +190,6 @@ func updateSuggestionDiv() {
|
||||
}
|
||||
}
|
||||
// The \n is so it's easier to read th source in web browsers for the dev
|
||||
js.Get("suggestions").Set("innerHTML", strings.Join(divs, "\n"))
|
||||
js.Call("updateSuggestionScroll")
|
||||
global.Get("suggestions").Set("innerHTML", strings.Join(divs, "\n"))
|
||||
global.Call("updateSuggestionScroll")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user