Comparing version 4.0.0-alpha.0 to 4.0.0-alpha.1
@@ -405,53 +405,52 @@ 'use strict' | ||
// TODO (perf): Have a Pool of reusable HTTPParsers? | ||
class Parser { | ||
constructor (client, socket) { | ||
// TODO (fix): Set sane memory limit? | ||
this.inst = new WebAssembly.Instance(mod, { | ||
env: { | ||
/* eslint-disable camelcase */ | ||
let currentParser = null | ||
const llhttp = new WebAssembly.Instance(mod, { | ||
env: { | ||
/* eslint-disable camelcase */ | ||
wasm_on_message_begin: p => { | ||
return 0 | ||
}, | ||
wasm_on_url: /* istanbul ignore next: not used */ (p, at, length) => { | ||
return 0 | ||
}, | ||
wasm_on_status: (p, at, length) => { | ||
return 0 | ||
}, | ||
wasm_on_header_field: (p, at, len) => { | ||
assert(this.headers.length % 2 === 0) | ||
const start = at - this.bufferPtr | ||
const end = start + len | ||
return this.onHeader(this.bufferRef.slice(start, end)) || 0 | ||
}, | ||
wasm_on_header_value: (p, at, len) => { | ||
assert(this.headers.length % 2 === 1) | ||
const start = at - this.bufferPtr | ||
const end = start + len | ||
return this.onHeader(this.bufferRef.slice(start, end)) || 0 | ||
}, | ||
wasm_on_headers_complete: p => { | ||
const statusCode = this.inst.llhttp_get_status_code(p) | ||
const upgrade = Boolean(this.inst.llhttp_get_upgrade(p)) | ||
const shouldKeepAlive = Boolean(this.inst.llhttp_should_keep_alive(p)) | ||
return this.onHeadersComplete(statusCode, upgrade, shouldKeepAlive) || 0 | ||
}, | ||
wasm_on_body: (p, at, len) => { | ||
const start = at - this.bufferPtr | ||
const end = start + len | ||
return this.onBody(this.bufferRef.slice(start, end)) || 0 | ||
}, | ||
wasm_on_message_complete: (p) => { | ||
return this.onMessageComplete() || 0 | ||
} | ||
wasm_on_message_begin: p => { | ||
return 0 | ||
}, | ||
wasm_on_url: /* istanbul ignore next: not used */ (p, at, length) => { | ||
return 0 | ||
}, | ||
wasm_on_status: (p, at, length) => { | ||
return 0 | ||
}, | ||
wasm_on_header_field: (p, at, len) => { | ||
assert(currentParser.headers.length % 2 === 0) | ||
const start = at - currentParser.bufferPtr | ||
const end = start + len | ||
return currentParser.onHeader(currentParser.bufferRef.slice(start, end)) || 0 | ||
}, | ||
wasm_on_header_value: (p, at, len) => { | ||
assert(currentParser.headers.length % 2 === 1) | ||
const start = at - currentParser.bufferPtr | ||
const end = start + len | ||
return currentParser.onHeader(currentParser.bufferRef.slice(start, end)) || 0 | ||
}, | ||
wasm_on_headers_complete: p => { | ||
const statusCode = llhttp.llhttp_get_status_code(p) | ||
const upgrade = Boolean(llhttp.llhttp_get_upgrade(p)) | ||
const shouldKeepAlive = Boolean(llhttp.llhttp_should_keep_alive(p)) | ||
return currentParser.onHeadersComplete(statusCode, upgrade, shouldKeepAlive) || 0 | ||
}, | ||
wasm_on_body: (p, at, len) => { | ||
const start = at - currentParser.bufferPtr | ||
const end = start + len | ||
return currentParser.onBody(currentParser.bufferRef.slice(start, end)) || 0 | ||
}, | ||
wasm_on_message_complete: (p) => { | ||
return currentParser.onMessageComplete() || 0 | ||
} | ||
/* eslint-enable camelcase */ | ||
} | ||
}).exports | ||
/* eslint-enable camelcase */ | ||
} | ||
}).exports | ||
class Parser { | ||
constructor (client, socket) { | ||
assert(Number.isFinite(client[kMaxHeadersSize]) && client[kMaxHeadersSize] > 0) | ||
this.ptr = this.inst.llhttp_alloc(constants.TYPE.RESPONSE) | ||
this.ptr = llhttp.llhttp_alloc(constants.TYPE.RESPONSE) | ||
this.bufferSize = 0 | ||
@@ -477,4 +476,11 @@ this.bufferPtr = null | ||
resume () { | ||
this.inst.llhttp_resume(this.ptr) | ||
if (!this.ptr) { | ||
return | ||
} | ||
assert(this.ptr) | ||
assert(!currentParser) | ||
llhttp.llhttp_resume(this.ptr) | ||
if (this.timeout) { | ||
@@ -495,15 +501,20 @@ // istanbul ignore else: only for jest | ||
execute (data) { | ||
const { socket } = this | ||
if (!this.ptr) { | ||
return | ||
} | ||
assert(this.ptr) | ||
assert(!currentParser) | ||
const { socket } = this | ||
// Be sure the parser buffer can contain `data` | ||
if (data.length > this.bufferSize) { | ||
if (this.bufferPtr) { | ||
this.inst.free(this.bufferPtr) | ||
llhttp.free(this.bufferPtr) | ||
} | ||
this.bufferSize = Math.ceil(data.length / 4096) * 4096 | ||
this.bufferPtr = this.inst.malloc(this.bufferSize) | ||
this.bufferPtr = llhttp.malloc(this.bufferSize) | ||
// Instantiate a Unit8 Buffer view of the wasm memory that starts from the parser buffer pointer. | ||
this.bufferView = new Uint8Array(this.inst.memory.buffer, this.bufferPtr, this.bufferSize) | ||
this.bufferView = new Uint8Array(llhttp.memory.buffer, this.bufferPtr, this.bufferSize) | ||
} | ||
@@ -519,3 +530,5 @@ | ||
this.bufferRef = data | ||
const ret = this.inst.llhttp_execute(this.ptr, this.bufferPtr, data.length) | ||
currentParser = this | ||
const ret = llhttp.llhttp_execute(this.ptr, this.bufferPtr, data.length) | ||
currentParser = null | ||
this.bufferRef = null | ||
@@ -527,3 +540,3 @@ | ||
const offset = this.inst.llhttp_get_error_pos(this.ptr) - this.bufferPtr | ||
const offset = llhttp.llhttp_get_error_pos(this.ptr) - this.bufferPtr | ||
@@ -536,7 +549,7 @@ if (ret === constants.ERROR.PAUSED_UPGRADE) { | ||
} else { | ||
const ptr = this.inst.llhttp_get_error_reason(this.ptr) | ||
const ptr = llhttp.llhttp_get_error_reason(this.ptr) | ||
let message | ||
if (ptr) { | ||
const len = new Uint8Array(this.inst.memory.buffer).indexOf(0, ptr) - ptr | ||
message = Buffer.from(this.inst.memory.buffer, ptr, len).toString() | ||
const len = new Uint8Array(llhttp.memory.buffer).indexOf(0, ptr) - ptr | ||
message = Buffer.from(llhttp.memory.buffer, ptr, len).toString() | ||
} else { | ||
@@ -840,2 +853,12 @@ message = '' | ||
destroy () { | ||
if (this.ptr) { | ||
llhttp.llhttp_free(this.ptr) | ||
this.ptr = null | ||
} | ||
if (this.bufferPtr) { | ||
llhttp.free(this.bufferPtr) | ||
this.bufferPtr = null | ||
} | ||
clearTimeout(this.timeout) | ||
@@ -842,0 +865,0 @@ this.timeout = null |
{ | ||
"name": "undici", | ||
"version": "4.0.0-alpha.0", | ||
"version": "4.0.0-alpha.1", | ||
"description": "An HTTP/1.1 client, written from scratch for Node.js", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/nodejs/undici#readme", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
362259
4637
4