Skip to content
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

Uncaught (in promise) TypeError: fs.writeSync is not a function #16

Closed
happybeing opened this issue Oct 30, 2020 · 4 comments · May be fixed by #17
Closed

Uncaught (in promise) TypeError: fs.writeSync is not a function #16

happybeing opened this issue Oct 30, 2020 · 4 comments · May be fixed by #17

Comments

@happybeing
Copy link

I'm trying to get the plugin working with go version go1.13.8 linux/amd64

I have the plugin and both examples building. Running the node example doesn't cause an error, but the result printed is '0'. In the web example, the browser console has the following error on loading:

Uncaught (in promise) TypeError: fs.writeSync is not a function
    wasmWrite wasm_exec.js:248
    run wasm_exec.js:475
    init gobridge.ts:28
    default_1 gobridge.ts:32
    go main.go:2

To get to this point I had to update the ./lib/wasm_exec.js for v1.13 (see attached) but I'm not sure how to fix this error yet.
wasm_exec.js.txt

@happybeing
Copy link
Author

Minor update. I was correct that the node example was not working, but it was a separate issue. The node example works if I change add() in ./example/node/src/main.go to:

func add(this js.Value, args []js.Value) (interface{}, error) {
	ret := 0

	for _, item := range args {
		// This fails because item.String() is "<number: 1>" not "1"
		// val, err := strconv.Atoi(item.String())
		// if err != nil {
		// 	return 0, err
		// }

		val := item.Int()
		ret += val
	}

	return ret, nil
}

I found a similar issue ("syscall/js: make wasm_exec.js compatible with Webpack" here) related to fs.writeSync() but applying the fix for that does not fix the web example problem in the OP. Nor is this fix needed for the node example to work.

@happybeing
Copy link
Author

I found a partial fix, at least it avoids the above error which occurs when executing println("Web Assembly is ready") in web/src/main.go and this is printed to the browser console when the page loads. The fix is another edit to wasm_exec.js to change the test on global.fs to detect the case where it is an object with an empty keys array, so that it creates an implementation of fs.writeSync() etc. So at line 37-ish I changed:

if (!global.fs) {

To:

if (!global.fs || Object.keys(global.fs) === 0 || Object.keys(global.fs).length === 0) {

The page displays ok, and clicking on "simulate an error" works, but changing integer values does not affect the "Value is now" value, which is always '0'.

@happybeing
Copy link
Author

Fixed!

Applying the main.go / add() fix from the node example to the web example, both examples now work.

I'm going to add a Svelte based example.

Let me know if you would like a PR.

@happybeing
Copy link
Author

I've published a fork as a new plugin which works with golang v1.13:

https://www.npmjs.com/package/golang-wasm-async-loader2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant