Ejemplo n.º 1
0
func save(kp *js.Object) {
	c := js.Global.Get("window").Get("crypto").Get("subtle")
	var b [32]byte
	x := js.Global.Get("Object").New()
	x.Set("name", "AES-GCM")
	x.Set("iv", js.NewArrayBuffer(b[:12]))
	p := c.Call("encrypt", x, kp, js.NewArrayBuffer(b[:]))
	p.Call("then", ok).Call("catch", err)
}
Ejemplo n.º 2
0
func TestNewArrayBuffer(t *testing.T) {
	b := []byte("abcd")
	a := js.NewArrayBuffer(b[1:3])
	if a.Get("byteLength").Int() != 2 {
		t.Fail()
	}
}
Ejemplo n.º 3
0
func (pk *PlatformKeys) GetKeyPair(rawCert []byte, algorithm js.M) (pubkey *js.Object, privkey *js.Object, err error) {
	// Uncaught exceptions in JS get translated into panics in Go
	defer func() {
		if r := recover(); r != nil {
			err = r.(error)
		}
	}()

	results := make(chan pkKeyPair, 1)

	pk.pk.Call("getKeyPair", js.NewArrayBuffer(rawCert), algorithm, func(pubkey *js.Object, privkey *js.Object) {
		go func() { results <- pkKeyPair{pubkey, privkey} }()
	})

	pair := <-results
	return pair.pubkey, pair.privkey, nil
}
Ejemplo n.º 4
0
func setupConnection() {
	ctx := canvas.Call("getContext", "2d")
	img := ctx.Call("getImageData", 0, 0, imgWidth, imgHeight)

	if img.Get("data").Length() != len(finalImage.Pix) {
		throw(errors.New("data size of images do not match"))
	}

	document := js.Global.Get("document")
	location := document.Get("location")

	ws, err := websocket.New(fmt.Sprintf("ws://%s/render", location.Get("host")))
	assert(err)

	renderChan := make(chan struct{}, frameStacking)

	onOpen := func(ev *js.Object) {
		setup := setupMessage{
			Width:       imgWidth,
			Height:      imgHeight,
			FieldOfView: 45,
			ColorFormat: colorFormat,
			ClearColor:  [4]byte{127, 127, 127, 255},
		}

		msg, err := json.Marshal(setup)
		assert(err)

		assert(ws.Send(string(msg)))

		go updateCamera(ws, renderChan)
	}

	onMessage := func(ev *js.Object) {
		idx := frameId % 2
		data := js.Global.Get("Uint8Array").New(ev.Get("data")).Interface().([]uint8)

		if isPalette(data) {
			pal := createPalette(data)
			palImages = [2]*image.Paletted{
				image.NewPaletted(imgRect, pal),
				image.NewPaletted(imgRect, pal),
			}
			return
		}

		var (
			imageA, imageB image.Image
		)

		if isRGBA(data) {
			rgbaImages[idx].Pix = data
			imageA = rgbaImages[0]
			imageB = rgbaImages[1]
		} else {
			palImages[idx].Pix = data
			imageA = palImages[0]
			imageB = palImages[1]
		}

		// This function could be optimized for this specific senario.
		assert(trace.Reconstruct(imageA, imageB, finalImage))

		arrBuf := js.NewArrayBuffer(finalImage.Pix)
		buf := js.Global.Get("Uint8ClampedArray").New(arrBuf)
		img.Get("data").Call("set", buf)
		ctx.Call("putImageData", img, 0, 0)

		numFrames++
		frameId++

		renderChan <- struct{}{}
	}

	ws.BinaryType = "arraybuffer"
	ws.AddEventListener("open", false, onOpen)
	ws.AddEventListener("message", false, onMessage)
}
Ejemplo n.º 5
0
func workerMain() {
	js.Global.Set("onmessage", func(e *js.Object) {
		data := e.Get("data")
		input := data.Get("input")
		r, err := newReaderFromJSObject(input)
		if err != nil {
			js.Global.Call("postMessage", js.M{"type": "error", "error": err.Error()})
			return
		}
		go func() {
			defer func() {
				if err := recover(); err != nil {
					js.Global.Call("postMessage", js.M{"type": "error", "error": fmt.Sprint("uncaught exception occurred on during parsing PSD: ", err)})
				}
			}()
			root, err := parse(r, func(progress float64) {
				js.Global.Call("postMessage", js.M{
					"type":     "progress",
					"progress": progress,
				})
			}, func(seqID int, l *psd.Layer) {
				dataMap := js.M{"type": "makeCanvas", "id": seqID}
				transferable := js.S{}
				if l.HasImage() && !l.Rect.Empty() {
					dataMap["w"] = l.Rect.Dx()
					dataMap["h"] = l.Rect.Dy()
					r := js.NewArrayBuffer(l.Channel[0].Data)
					g := js.NewArrayBuffer(l.Channel[1].Data)
					b := js.NewArrayBuffer(l.Channel[2].Data)
					dataMap["r"] = r
					dataMap["g"] = g
					dataMap["b"] = b
					if a, ok := l.Channel[-1]; ok {
						dataMap["a"] = js.NewArrayBuffer(a.Data)
						transferable = append(transferable, r, g, b, dataMap["a"])
					} else {
						transferable = append(transferable, r, g, b)
					}
				}
				if mask, ok := l.Channel[-2]; ok && l.Mask.Enabled() && !l.Mask.Rect.Empty() {
					dataMap["mw"] = l.Mask.Rect.Dx()
					dataMap["mh"] = l.Mask.Rect.Dy()
					dataMap["m"] = js.NewArrayBuffer(mask.Data)
					transferable = append(transferable, dataMap["m"])
					dataMap["mc"] = l.Mask.DefaultColor
				}
				if len(transferable) > 0 {
					js.Global.Call("postMessage", dataMap, transferable)
				}
			})
			if err != nil {
				js.Global.Call("postMessage", js.M{"type": "error", "error": err.Error()})
				return
			}
			js.Global.Call("postMessage", js.M{
				"type": "complete",
				"root": root,
			})
		}()
	})
}
Ejemplo n.º 6
0
func fu() {
	c := js.Global.Get("window").Get("crypto").Get("subtle")
	var b [32]byte
	p := c.Call("importKey", "raw", js.NewArrayBuffer(b[:]), "AES-GCM", true, [2]string{"encrypt", "decrypt"})
	p.Call("then", save).Call("catch", err)
}