forked from berty/berty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrpc-web-react-native-transport.ts
176 lines (152 loc) · 4.2 KB
/
grpc-web-react-native-transport.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { grpc } from '@improbable-eng/grpc-web'
let awaitingExecution: Array<() => void> | null = null
function runCallbacks(): void {
if (awaitingExecution) {
const thisCallbackSet = awaitingExecution
awaitingExecution = null
for (let i = 0; i < thisCallbackSet.length; i++) {
try {
thisCallbackSet[i]()
} catch (e) {
if (awaitingExecution === null) {
awaitingExecution = []
setTimeout(() => {
runCallbacks()
}, 0)
}
for (let k = thisCallbackSet.length - 1; k > i; k--) {
awaitingExecution.unshift(thisCallbackSet[k])
}
throw e
}
}
}
}
function debug(...args: any[]): void {
if (console.debug) {
console.debug(...args)
} else {
console.log(...args)
}
}
function detach(cb: () => void): void {
if (awaitingExecution !== null) {
awaitingExecution.push(cb)
return
}
awaitingExecution = [cb]
setTimeout(() => {
runCallbacks()
}, 0)
}
function codePointAtPolyfill(str: string, index: number): number {
let code = str.charCodeAt(index)
if (code >= 0xd800 && code <= 0xdbff) {
const surr = str.charCodeAt(index + 1)
if (surr >= 0xdc00 && surr <= 0xdfff) {
code = 0x10000 + ((code - 0xd800) << 10) + (surr - 0xdc00)
}
}
return code
}
function stringToArrayBuffer(str: string): Uint8Array {
const asArray = new Uint8Array(str.length)
let arrayIndex = 0
for (let i = 0; i < str.length; i++) {
const codePoint = (String.prototype as { codePointAt: (i: number) => number }).codePointAt
? (str as { codePointAt: (i: number) => number }).codePointAt(i)
: codePointAtPolyfill(str, i)
asArray[arrayIndex++] = codePoint & 0xff
}
return asArray
}
class XHR implements grpc.Transport {
options: grpc.TransportOptions
init: grpc.XhrTransportInit
xhr: XMLHttpRequest = new XMLHttpRequest()
metadata: grpc.Metadata = new grpc.Metadata()
index = 0
constructor(transportOptions: grpc.TransportOptions, init: grpc.XhrTransportInit) {
this.options = transportOptions
this.init = init
}
protected onProgressEvent(): void {
this.options.debug && debug('XHR.onProgressEvent.length: ', this.xhr.response.length)
const rawText = this.xhr.response.substr(this.index)
this.index = this.xhr.response.length
const asArrayBuffer = stringToArrayBuffer(rawText)
detach(() => {
this.options.onChunk(asArrayBuffer)
})
}
onLoadEvent(): void {
detach(() => {
this.options.onEnd()
})
}
onStateChange(): void {
if (this.xhr.readyState === XMLHttpRequest.HEADERS_RECEIVED) {
detach(() => {
this.options.onHeaders(new grpc.Metadata(this.xhr.getAllResponseHeaders()), this.xhr.status)
})
}
}
sendMessage(msgBytes: Uint8Array): void {
this.xhr.send(msgBytes)
}
finishSend(): void {
return
}
start(metadata: grpc.Metadata): void {
this.metadata = metadata
const xhr = new XMLHttpRequest()
this.xhr = xhr
xhr.open('POST', this.options.url)
this.configureXhr()
this.metadata.forEach((key, values) => {
xhr.setRequestHeader(key, values.join(', '))
})
xhr.withCredentials = Boolean(this.init.withCredentials)
xhr.addEventListener('readystatechange', this.onStateChange.bind(this))
xhr.addEventListener('progress', this.onProgressEvent.bind(this))
xhr.addEventListener('loadend', this.onLoadEvent.bind(this))
xhr.addEventListener('error', (err) => {
detach(() => {
this.options.onEnd(new Error(`XHR Error: ${err}`))
})
})
}
protected configureXhr(): void {
this.xhr.responseType = 'text'
this.xhr.overrideMimeType('text/plain; charset=x-user-defined')
}
cancel(): void {
this.xhr.abort()
}
}
class ArrayBufferXHR extends XHR {
configureXhr(): void {
if (this.options.debug) {
debug("ArrayBufferXHR.configureXhr: setting responseType to 'arraybuffer'")
}
this.xhr.responseType = 'arraybuffer'
}
onProgressEvent(): void {
return
}
onLoadEvent(): void {
const resp = this.xhr.response
this.options.debug && debug('ArrayBufferXHR.onLoadEvent: ', new Uint8Array(resp))
detach(() => {
this.options.onChunk(new Uint8Array(resp), /* flush */ true)
})
detach(() => {
this.options.onEnd()
})
}
}
export function ReactNativeTransport(init: grpc.XhrTransportInit): grpc.TransportFactory {
return (opts: grpc.TransportOptions): XHR => {
return new ArrayBufferXHR(opts, init)
}
}