GithubHelp home page GithubHelp logo

wahttp's Introduction

Intro

export go-fetch to js side

Usage

setup at go wasm side main.go

package main

import (
	"net/http"
	"syscall/js"

	promise "github.com/nlepage/go-js-promise"
	"github.com/shynome/err0"
	"github.com/shynome/err0/try"
	"github.com/shynome/wahttp"
)

func main() {
	js.Global().Set("GoFetch", GoFetch())
	// js.Global().Get("console").Call("log", js.Global())
	<-make(chan any)
}

func GoFetch() js.Func {
	return js.FuncOf(func(this js.Value, args []js.Value) any {
		p, resolve, reject := promise.New()

		go func() (err error) {
			defer err0.Then(&err, nil, func() {
				reject(err)
			})
			req := try.To1(wahttp.JsRequest(args[0]))
			resp := try.To1(http.DefaultClient.Do(req))
			resolve(wahttp.GoResponse(resp))
			return
		}()

		return p
	})
}

call at js side main.ts

import "https://go.dev/misc/wasm/wasm_exec.js";

declare global {
  const Go: any;
  const GoFetch: typeof fetch;
}

Deno.test("go-fetch", async () => {
  const go = new Go();

  const wasmBuf = await Deno.readFile("./public/main.wasm");

  const m = await WebAssembly.instantiate(wasmBuf, go.importObject);

  Promise.resolve().then(() => {
    go.run(m.instance);
  });

  await new Promise((rl) => setTimeout(rl, 0));

  const req = new Request("https://shyno.me");
  const r = await GoFetch(req);

  // clean
  await Promise.all([r.text()]);

  if (r.status != 200) {
    throw new Error(`status is ${r.status}, expect 200`);
  }
});

wahttp's People

Contributors

shynome avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.