Beispiel #1
0
func DefineWithHandler(vm *otto.Otto, l *loop.Loop, h http.Handler) error {
	if err := promise.Define(vm, l); err != nil {
		return err
	}

	s, err := vm.Compile("fetch.js", rice.MustFindBox("dist-fetch").MustString("bundle.js"))
	if err != nil {
		return err
	}

	if _, err := vm.Run(s); err != nil {
		return err
	}

	vm.Set("__private__fetch_execute", func(c otto.FunctionCall) otto.Value {
		jsReq := c.Argument(0).Object()
		jsRes := c.Argument(1).Object()
		cb := c.Argument(2)

		method := mustValue(jsReq.Get("method")).String()
		urlStr := mustValue(jsReq.Get("url")).String()
		jsBody := mustValue(jsReq.Get("body"))
		var body io.Reader
		if jsBody.IsString() {
			body = strings.NewReader(jsBody.String())
		}

		t := &fetchTask{
			jsReq: jsReq,
			jsRes: jsRes,
			cb:    cb,
		}

		l.Add(t)

		go func() {
			defer l.Ready(t)

			req, err := http.NewRequest(method, urlStr, body)
			if err != nil {
				t.err = err
				return
			}

			if h != nil && urlStr[0] == '/' {
				res := httptest.NewRecorder()

				h.ServeHTTP(res, req)

				jsRes.Set("status", res.Code)
				jsRes.Set("statusText", http.StatusText(res.Code))
				h := mustValue(jsRes.Get("headers")).Object()
				for k, vs := range res.Header() {
					for _, v := range vs {
						if _, err := h.Call("append", k, v); err != nil {
							t.err = err
							return
						}
					}
				}

				jsRes.Set("_body", string(res.Body.Bytes()))
			} else {
				res, err := http.DefaultClient.Do(req)
				if err != nil {
					t.err = err
					return
				}

				jsRes.Set("status", res.StatusCode)
				jsRes.Set("statusText", res.Status)
				h := mustValue(jsRes.Get("headers")).Object()
				for k, vs := range res.Header {
					for _, v := range vs {
						if _, err := h.Call("append", k, v); err != nil {
							t.err = err
							return
						}
					}
				}

				d, err := ioutil.ReadAll(res.Body)
				if err != nil {
					t.err = err
					return
				}

				jsRes.Set("_body", string(d))
			}
		}()

		return otto.UndefinedValue()
	})

	return nil
}