| 1 |
/** |
| 2 |
* A minimal Parser that mimicks a small fraction of the Node.js parser |
| 3 |
* API. |
| 4 |
* To run: |
| 5 |
* - `npm run build-wasm` |
| 6 |
* - `npx ts-node examples/wasm.ts` |
| 7 |
*/ |
| 8 |
import { readFileSync } from 'fs'; |
| 9 |
import { resolve } from 'path'; |
| 10 |
import * as constants from '../build/wasm/constants'; |
| 11 |
|
| 12 |
const bin = readFileSync(resolve(__dirname, '../build/wasm/llhttp.wasm')); |
| 13 |
const mod = new WebAssembly.Module(bin); |
| 14 |
|
| 15 |
const REQUEST = constants.TYPE.REQUEST; |
| 16 |
const RESPONSE = constants.TYPE.RESPONSE; |
| 17 |
const kOnMessageBegin = 0; |
| 18 |
const kOnHeaders = 1; |
| 19 |
const kOnHeadersComplete = 2; |
| 20 |
const kOnBody = 3; |
| 21 |
const kOnMessageComplete = 4; |
| 22 |
const kOnExecute = 5; |
| 23 |
|
| 24 |
const kPtr = Symbol('kPtr'); |
| 25 |
const kUrl = Symbol('kUrl'); |
| 26 |
const kStatusMessage = Symbol('kStatusMessage'); |
| 27 |
const kHeadersFields = Symbol('kHeadersFields'); |
| 28 |
const kHeadersValues = Symbol('kHeadersValues'); |
| 29 |
const kBody = Symbol('kBody'); |
| 30 |
const kReset = Symbol('kReset'); |
| 31 |
const kCheckErr = Symbol('kCheckErr'); |
| 32 |
|
| 33 |
const cstr = (ptr: number, len: number): string => |
| 34 |
Buffer.from(memory.buffer, ptr, len).toString(); |
| 35 |
|
| 36 |
const wasm_on_message_begin = (p: number) => { |
| 37 |
const i = instMap.get(p); |
| 38 |
i[kReset](); |
| 39 |
return i[kOnMessageBegin](); |
| 40 |
}; |
| 41 |
|
| 42 |
const wasm_on_url = (p: number, at: number, length: number) => { |
| 43 |
instMap.get(p)[kUrl] = cstr(at, length); |
| 44 |
return 0; |
| 45 |
}; |
| 46 |
|
| 47 |
const wasm_on_status = (p: number, at: number, length: number) => { |
| 48 |
instMap.get(p)[kStatusMessage] = cstr(at, length); |
| 49 |
return 0; |
| 50 |
}; |
| 51 |
|
| 52 |
const wasm_on_header_field = (p: number, at: number, length: number) => { |
| 53 |
const i= instMap.get(p) |
| 54 |
i[kHeadersFields].push(cstr(at, length)); |
| 55 |
return 0; |
| 56 |
}; |
| 57 |
|
| 58 |
const wasm_on_header_value = (p: number, at: number, length: number) => { |
| 59 |
const i = instMap.get(p); |
| 60 |
i[kHeadersValues].push(cstr(at, length)); |
| 61 |
return 0; |
| 62 |
}; |
| 63 |
|
| 64 |
const wasm_on_headers_complete = (p: number) => { |
| 65 |
const i = instMap.get(p); |
| 66 |
const type = get_type(p); |
| 67 |
const versionMajor = get_version_major(p); |
| 68 |
const versionMinor = get_version_minor(p); |
| 69 |
const rawHeaders = []; |
| 70 |
let method; |
| 71 |
let url; |
| 72 |
let statusCode; |
| 73 |
let statusMessage; |
| 74 |
const upgrade = get_upgrade(p); |
| 75 |
const shouldKeepAlive = should_keep_alive(p); |
| 76 |
|
| 77 |
for (let c = 0; c < i[kHeadersFields].length; c++) { |
| 78 |
rawHeaders.push(i[kHeadersFields][c], i[kHeadersValues][c]) |
| 79 |
} |
| 80 |
|
| 81 |
if (type === HTTPParser.REQUEST) { |
| 82 |
method = constants.METHODS[get_method(p)]; |
| 83 |
url = i[kUrl]; |
| 84 |
} else if (type === HTTPParser.RESPONSE) { |
| 85 |
statusCode = get_status_code(p); |
| 86 |
statusMessage = i[kStatusMessage]; |
| 87 |
} |
| 88 |
return i[kOnHeadersComplete](versionMajor, versionMinor, rawHeaders, method, |
| 89 |
url, statusCode, statusMessage, upgrade, shouldKeepAlive); |
| 90 |
}; |
| 91 |
|
| 92 |
const wasm_on_body = (p: number, at: number, length: number) => { |
| 93 |
const i = instMap.get(p); |
| 94 |
const body = Buffer.from(memory.buffer, at, length); |
| 95 |
return i[kOnBody](body); |
| 96 |
}; |
| 97 |
|
| 98 |
const wasm_on_message_complete = (p: number) => { |
| 99 |
return instMap.get(p)[kOnMessageComplete](); |
| 100 |
}; |
| 101 |
|
| 102 |
const instMap = new Map(); |
| 103 |
|
| 104 |
const inst = new WebAssembly.Instance(mod, { |
| 105 |
env: { |
| 106 |
wasm_on_message_begin, |
| 107 |
wasm_on_url, |
| 108 |
wasm_on_status, |
| 109 |
wasm_on_header_field, |
| 110 |
wasm_on_header_value, |
| 111 |
wasm_on_headers_complete, |
| 112 |
wasm_on_body, |
| 113 |
wasm_on_message_complete, |
| 114 |
}, |
| 115 |
}); |
| 116 |
|
| 117 |
const memory = inst.exports.memory as any; |
| 118 |
const alloc = inst.exports.llhttp_alloc as CallableFunction; |
| 119 |
const malloc = inst.exports.malloc as CallableFunction; |
| 120 |
const execute = inst.exports.llhttp_execute as CallableFunction; |
| 121 |
const get_type = inst.exports.llhttp_get_type as CallableFunction; |
| 122 |
const get_upgrade = inst.exports.llhttp_get_upgrade as CallableFunction; |
| 123 |
const should_keep_alive = inst.exports.llhttp_should_keep_alive as CallableFunction; |
| 124 |
const get_method = inst.exports.llhttp_get_method as CallableFunction; |
| 125 |
const get_status_code = inst.exports.llhttp_get_status_code as CallableFunction; |
| 126 |
const get_version_minor = inst.exports.llhttp_get_http_minor as CallableFunction; |
| 127 |
const get_version_major = inst.exports.llhttp_get_http_major as CallableFunction; |
| 128 |
const get_error_reason = inst.exports.llhttp_get_error_reason as CallableFunction; |
| 129 |
const free = inst.exports.free as CallableFunction; |
| 130 |
const initialize = inst.exports._initialize as CallableFunction; |
| 131 |
|
| 132 |
initialize(); // wasi reactor |
| 133 |
|
| 134 |
class HTTPParser { |
| 135 |
static REQUEST = REQUEST; |
| 136 |
static RESPONSE = RESPONSE; |
| 137 |
static kOnMessageBegin = kOnMessageBegin; |
| 138 |
static kOnHeaders = kOnHeaders; |
| 139 |
static kOnHeadersComplete = kOnHeadersComplete; |
| 140 |
static kOnBody = kOnBody; |
| 141 |
static kOnMessageComplete = kOnMessageComplete; |
| 142 |
static kOnExecute = kOnExecute; |
| 143 |
|
| 144 |
[kPtr]: number; |
| 145 |
[kUrl]: string; |
| 146 |
[kStatusMessage]: null|string; |
| 147 |
[kHeadersFields]: []|[string]; |
| 148 |
[kHeadersValues]: []|[string]; |
| 149 |
[kBody]: null|Buffer; |
| 150 |
|
| 151 |
constructor(type: constants.TYPE) { |
| 152 |
this[kPtr] = alloc(constants.TYPE[type]); |
| 153 |
instMap.set(this[kPtr], this); |
| 154 |
|
| 155 |
this[kUrl] = ''; |
| 156 |
this[kStatusMessage] = null; |
| 157 |
this[kHeadersFields] = []; |
| 158 |
this[kHeadersValues] = []; |
| 159 |
this[kBody] = null; |
| 160 |
} |
| 161 |
|
| 162 |
[kReset]() { |
| 163 |
this[kUrl] = ''; |
| 164 |
this[kStatusMessage] = null; |
| 165 |
this[kHeadersFields] = []; |
| 166 |
this[kHeadersValues] = []; |
| 167 |
this[kBody] = null; |
| 168 |
} |
| 169 |
|
| 170 |
[kOnMessageBegin]() { |
| 171 |
return 0; |
| 172 |
} |
| 173 |
|
| 174 |
[kOnHeaders](rawHeaders: [string]) {} |
| 175 |
|
| 176 |
[kOnHeadersComplete](versionMajor: number, versionMinor: number, rawHeaders: [string], method: string, |
| 177 |
url: string, statusCode: number, statusMessage: string, upgrade: boolean, shouldKeepAlive: boolean) { |
| 178 |
return 0; |
| 179 |
} |
| 180 |
|
| 181 |
[kOnBody](body: Buffer) { |
| 182 |
this[kBody] = body; |
| 183 |
return 0; |
| 184 |
} |
| 185 |
|
| 186 |
[kOnMessageComplete]() { |
| 187 |
return 0; |
| 188 |
} |
| 189 |
|
| 190 |
destroy() { |
| 191 |
instMap.delete(this[kPtr]); |
| 192 |
free(this[kPtr]); |
| 193 |
} |
| 194 |
|
| 195 |
execute(data: Buffer) { |
| 196 |
const ptr = malloc(data.byteLength); |
| 197 |
const u8 = new Uint8Array(memory.buffer); |
| 198 |
u8.set(data, ptr); |
| 199 |
const ret = execute(this[kPtr], ptr, data.length); |
| 200 |
free(ptr); |
| 201 |
this[kCheckErr](ret); |
| 202 |
return ret; |
| 203 |
} |
| 204 |
|
| 205 |
[kCheckErr](n: number) { |
| 206 |
if (n === constants.ERROR.OK) { |
| 207 |
return; |
| 208 |
} |
| 209 |
const ptr = get_error_reason(this[kPtr]); |
| 210 |
const u8 = new Uint8Array(memory.buffer); |
| 211 |
const len = u8.indexOf(0, ptr) - ptr; |
| 212 |
throw new Error(cstr(ptr, len)); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
{ |
| 218 |
const p = new HTTPParser(HTTPParser.REQUEST); |
| 219 |
|
| 220 |
p.execute(Buffer.from([ |
| 221 |
'POST /owo HTTP/1.1', |
| 222 |
'X: Y', |
| 223 |
'Content-Length: 9', |
| 224 |
'', |
| 225 |
'uh, meow?', |
| 226 |
'', |
| 227 |
].join('\r\n'))); |
| 228 |
|
| 229 |
console.log(p); |
| 230 |
|
| 231 |
p.destroy(); |
| 232 |
} |
| 233 |
|
| 234 |
{ |
| 235 |
const p = new HTTPParser(HTTPParser.RESPONSE); |
| 236 |
|
| 237 |
p.execute(Buffer.from([ |
| 238 |
'HTTP/1.1 200 OK', |
| 239 |
'X: Y', |
| 240 |
'Content-Length: 9', |
| 241 |
'', |
| 242 |
'uh, meow?' |
| 243 |
].join('\r\n'))); |
| 244 |
|
| 245 |
console.log(p); |
| 246 |
|
| 247 |
p.destroy(); |
| 248 |
} |