MCPcopy
hub / github.com/gopherjs/gopherjs / Read

Method Read

compiler/natives/src/crypto/rand/rand.go:13–45  ·  view source on GitHub ↗
(b []byte)

Source from the content-addressed store, hash-verified

11type reader struct{}
12
13func (r *reader) Read(b []byte) (n int, err error) {
14 array := js.InternalObject(b).Get("$array")
15 offset := js.InternalObject(b).Get("$offset").Int()
16
17 // browser
18 crypto := js.Global.Get("crypto")
19 if crypto == js.Undefined {
20 crypto = js.Global.Get("msCrypto")
21 }
22 if crypto != js.Undefined {
23 if crypto.Get("getRandomValues") != js.Undefined {
24 n = len(b)
25 if n > 65536 {
26 // Avoid QuotaExceededError thrown by getRandomValues
27 // when length is more than 65536, as specified in
28 // http://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues
29 n = 65536
30 }
31 crypto.Call("getRandomValues", array.Call("subarray", offset, offset+n))
32 return n, nil
33 }
34 }
35
36 // Node.js
37 if require := js.Global.Get("require"); require != js.Undefined {
38 if randomBytes := require.Invoke("crypto").Get("randomBytes"); randomBytes != js.Undefined {
39 array.Call("set", randomBytes.Invoke(len(b)), offset)
40 return len(b), nil
41 }
42 }
43
44 return 0, errors.New("crypto/rand not available in this environment")
45}

Callers

nothing calls this directly

Calls 6

InternalObjectFunction · 0.92
GetMethod · 0.65
IntMethod · 0.45
CallMethod · 0.45
InvokeMethod · 0.45
NewMethod · 0.45

Tested by

no test coverage detected