| 6103 | } |
| 6104 | readMore() { |
| 6105 | while (!this.paused && this.ptr) { |
| 6106 | const chunk = this.socket.read(); |
| 6107 | if (chunk === null) { |
| 6108 | break; |
| 6109 | } |
| 6110 | this.execute(chunk); |
| 6111 | } |
| 6112 | } |
| 6113 | execute(data) { |
| 6114 | assert(this.ptr != null); |
| 6115 | assert(currentParser == null); |
| 6116 | assert(!this.paused); |
| 6117 | const { socket, llhttp } = this; |
| 6118 | if (data.length > currentBufferSize) { |
| 6119 | if (currentBufferPtr) { |
| 6120 | llhttp.free(currentBufferPtr); |
| 6121 | } |
| 6122 | currentBufferSize = Math.ceil(data.length / 4096) * 4096; |
| 6123 | currentBufferPtr = llhttp.malloc(currentBufferSize); |
| 6124 | } |
| 6125 | new Uint8Array(llhttp.memory.buffer, currentBufferPtr, currentBufferSize).set(data); |
| 6126 | try { |
| 6127 | let ret; |
| 6128 | try { |
| 6129 | currentBufferRef = data; |
| 6130 | currentParser = this; |
| 6131 | ret = llhttp.llhttp_execute(this.ptr, currentBufferPtr, data.length); |
| 6132 | } catch (err) { |
| 6133 | throw err; |
| 6134 | } finally { |
| 6135 | currentParser = null; |
| 6136 | currentBufferRef = null; |
| 6137 | } |
| 6138 | const offset = llhttp.llhttp_get_error_pos(this.ptr) - currentBufferPtr; |
| 6139 | if (ret !== constants3.ERROR.OK) { |
| 6140 | const body = data.subarray(offset); |
| 6141 | if (ret === constants3.ERROR.PAUSED_UPGRADE) { |
| 6142 | this.onUpgrade(body); |
| 6143 | } else if (ret === constants3.ERROR.PAUSED) { |
| 6144 | this.paused = true; |
| 6145 | socket.unshift(body); |
| 6146 | } else { |
| 6147 | throw this.createError(ret, body); |
| 6148 | } |