| 1 |
'use strict' |
| 2 |
|
| 3 |
const { tspl } = require('@matteo.collina/tspl') |
| 4 |
const { describe, test } = require('node:test') |
| 5 |
|
| 6 |
;[ |
| •7 |
['generic', require('llhttp/lib/llhttp/llhttp-wasm.js')], |
| 8 |
['simd', require('llhttp/lib/llhttp/llhttp_simd-wasm.js')] |
| 9 |
].forEach(([name, llhttp]) => { |
| 10 |
describe(name, () => { |
| 11 |
test('can compile the wasm code', async () => { |
| 12 |
await WebAssembly.compile(llhttp) |
| 13 |
}) |
| 14 |
|
| 15 |
test('can instantiate the wasm code', async () => { |
| 16 |
const mod = await WebAssembly.compile(llhttp) |
| 17 |
await WebAssembly.instantiate(mod, { |
| 18 |
env: { |
| 19 |
wasm_on_url: () => { }, |
| 20 |
wasm_on_status: () => { }, |
| 21 |
wasm_on_message_begin: () => { }, |
| 22 |
wasm_on_header_field: () => { }, |
| 23 |
wasm_on_header_value: () => { }, |
| 24 |
wasm_on_headers_complete: () => { }, |
| 25 |
wasm_on_body: () => { }, |
| 26 |
wasm_on_message_complete: () => { } |
| 27 |
} |
| 28 |
}) |
| 29 |
}) |
| 30 |
|
| 31 |
describe('exports', async () => { |
| 32 |
const mod = await WebAssembly.compile(llhttp) |
| 33 |
const instance = await WebAssembly.instantiate(mod, { |
| 34 |
env: { |
| 35 |
wasm_on_url: () => { }, |
| 36 |
wasm_on_status: () => { }, |
| 37 |
wasm_on_message_begin: () => { }, |
| 38 |
wasm_on_header_field: () => { }, |
| 39 |
wasm_on_header_value: () => { }, |
| 40 |
wasm_on_headers_complete: () => { }, |
| 41 |
wasm_on_body: () => { }, |
| 42 |
wasm_on_message_complete: () => { } |
| 43 |
} |
| 44 |
}) |
| 45 |
|
| 46 |
test('has the required exports', async (t) => { |
| 47 |
const requiredExports = [ |
| 48 |
'memory', |
| 49 |
'_initialize', |
| 50 |
'__indirect_function_table', |
| 51 |
'llhttp_init', |
| 52 |
'llhttp_should_keep_alive', |
| 53 |
'llhttp_alloc', |
| 54 |
'malloc', |
| 55 |
'llhttp_free', |
| 56 |
'free', |
| 57 |
'llhttp_get_type', |
| 58 |
'llhttp_get_http_major', |
| 59 |
'llhttp_get_http_minor', |
| 60 |
'llhttp_get_method', |
| 61 |
'llhttp_get_status_code', |
| 62 |
'llhttp_get_upgrade', |
| 63 |
'llhttp_reset', |
| 64 |
'llhttp_execute', |
| 65 |
'llhttp_settings_init', |
| 66 |
'llhttp_finish', |
| 67 |
'llhttp_pause', |
| 68 |
'llhttp_resume', |
| 69 |
'llhttp_resume_after_upgrade', |
| 70 |
'llhttp_get_errno', |
| 71 |
'llhttp_get_error_reason', |
| 72 |
'llhttp_set_error_reason', |
| 73 |
'llhttp_get_error_pos', |
| 74 |
'llhttp_errno_name', |
| 75 |
'llhttp_method_name', |
| 76 |
'llhttp_status_name', |
| 77 |
'llhttp_set_lenient_headers', |
| 78 |
'llhttp_set_lenient_chunked_length', |
| 79 |
'llhttp_set_lenient_keep_alive', |
| 80 |
'llhttp_set_lenient_transfer_encoding', |
| 81 |
'llhttp_set_lenient_version', |
| 82 |
'llhttp_set_lenient_data_after_close', |
| 83 |
'llhttp_set_lenient_optional_lf_after_cr', |
| 84 |
'llhttp_set_lenient_optional_crlf_after_chunk', |
| 85 |
'llhttp_set_lenient_optional_cr_before_lf', |
| 86 |
'llhttp_set_lenient_spaces_after_chunk_size', |
| 87 |
'llhttp_message_needs_eof' |
| 88 |
] |
| 89 |
t = tspl(t, { plan: requiredExports.length }) |
| 90 |
|
| 91 |
for (const key of requiredExports) { |
| 92 |
t.ok(key in instance.exports, `${key} is exported`) |
| 93 |
} |
| 94 |
await t.completed |
| 95 |
}) |
| 96 |
|
| 97 |
test('instance.exports.memory', async (t) => { |
| 98 |
t = tspl(t, { plan: 1 }) |
| 99 |
|
| 100 |
t.ok(instance.exports.memory instanceof WebAssembly.Memory, 'memory is present') |
| 101 |
}) |
| 102 |
|
| 103 |
|
| 104 |
test('instance.exports._initialize', async (t) => { |
| 105 |
t = tspl(t, { plan: 2 }) |
| 106 |
|
| 107 |
t.ok(typeof instance.exports._initialize === 'function', '_initialize is present') |
| 108 |
t.strictEqual(instance.exports._initialize.length, 0, '_initialize has the right number of arguments') |
| 109 |
}) |
| 110 |
|
| 111 |
|
| 112 |
test('instance.exports.__indirect_function_table', async (t) => { |
| 113 |
t = tspl(t, { plan: 1 }) |
| 114 |
|
| 115 |
t.ok(instance.exports.__indirect_function_table instanceof WebAssembly.Table, '__indirect_function_table is present') |
| 116 |
}) |
| 117 |
|
| 118 |
|
| 119 |
test('instance.exports.malloc', async (t) => { |
| 120 |
t = tspl(t, { plan: 2 }) |
| 121 |
|
| 122 |
t.ok(typeof instance.exports.malloc === 'function', 'malloc is present') |
| 123 |
t.strictEqual(instance.exports.malloc.length, 1, 'malloc has the right number of arguments') |
| 124 |
}) |
| 125 |
|
| 126 |
|
| 127 |
test('instance.exports.free', async (t) => { |
| 128 |
t = tspl(t, { plan: 2 }) |
| 129 |
|
| 130 |
t.ok(typeof instance.exports.free === 'function', 'free is present') |
| 131 |
t.strictEqual(instance.exports.free.length, 1, 'free has the right number of arguments') |
| 132 |
}) |
| 133 |
|
| 134 |
|
| 135 |
test('instance.exports.llhttp_init', async (t) => { |
| 136 |
t = tspl(t, { plan: 2 }) |
| 137 |
|
| 138 |
t.ok(typeof instance.exports.llhttp_init === 'function', 'llhttp_init is present') |
| 139 |
t.strictEqual(instance.exports.llhttp_init.length, 3, 'llhttp_init has the right number of arguments') |
| 140 |
}) |
| 141 |
|
| 142 |
|
| 143 |
test('instance.exports.llhttp_alloc', async (t) => { |
| 144 |
t = tspl(t, { plan: 2 }) |
| 145 |
|
| 146 |
t.ok(typeof instance.exports.llhttp_alloc === 'function', 'llhttp_alloc is present') |
| 147 |
t.strictEqual(instance.exports.llhttp_alloc.length, 1, 'llhttp_alloc has the right number of arguments') |
| 148 |
}) |
| 149 |
|
| 150 |
|
| 151 |
test('instance.exports.llhttp_free', async (t) => { |
| 152 |
t = tspl(t, { plan: 2 }) |
| 153 |
|
| 154 |
t.ok(typeof instance.exports.llhttp_free === 'function', 'llhttp_free is present') |
| 155 |
t.strictEqual(instance.exports.llhttp_free.length, 1, 'llhttp_free has the right number of arguments') |
| 156 |
}) |
| 157 |
|
| 158 |
|
| 159 |
test('instance.exports.llhttp_get_type', async (t) => { |
| 160 |
t = tspl(t, { plan: 2 }) |
| 161 |
|
| 162 |
t.ok(typeof instance.exports.llhttp_get_type === 'function', 'llhttp_get_type is present') |
| 163 |
t.strictEqual(instance.exports.llhttp_get_type.length, 1, 'llhttp_get_type has the right number of arguments') |
| 164 |
}) |
| 165 |
|
| 166 |
|
| 167 |
test('instance.exports.llhttp_should_keep_alive', async (t) => { |
| 168 |
t = tspl(t, { plan: 2 }) |
| 169 |
|
| 170 |
t.ok(typeof instance.exports.llhttp_should_keep_alive === 'function', 'llhttp_should_keep_alive is present') |
| 171 |
t.strictEqual(instance.exports.llhttp_should_keep_alive.length, 1, 'llhttp_should_keep_alive has the right number of arguments') |
| 172 |
}) |
| 173 |
|
| 174 |
|
| 175 |
test('instance.exports.llhttp_get_http_major', async (t) => { |
| 176 |
t = tspl(t, { plan: 2 }) |
| 177 |
|
| 178 |
t.ok(typeof instance.exports.llhttp_get_http_major === 'function', 'llhttp_get_http_major is present') |
| 179 |
t.strictEqual(instance.exports.llhttp_get_http_major.length, 1, 'llhttp_get_http_major has the right number of arguments') |
| 180 |
}) |
| 181 |
|
| 182 |
|
| 183 |
test('instance.exports.llhttp_get_http_minor', async (t) => { |
| 184 |
t = tspl(t, { plan: 2 }) |
| 185 |
|
| 186 |
t.ok(typeof instance.exports.llhttp_get_http_minor === 'function', 'llhttp_get_http_minor is present') |
| 187 |
t.strictEqual(instance.exports.llhttp_get_http_minor.length, 1, 'llhttp_get_http_minor has the right number of arguments') |
| 188 |
}) |
| 189 |
|
| 190 |
|
| 191 |
test('instance.exports.llhttp_get_method', async (t) => { |
| 192 |
t = tspl(t, { plan: 2 }) |
| 193 |
|
| 194 |
t.ok(typeof instance.exports.llhttp_get_method === 'function', 'llhttp_get_method is present') |
| 195 |
t.strictEqual(instance.exports.llhttp_get_method.length, 1, 'llhttp_get_method has the right number of arguments') |
| 196 |
}) |
| 197 |
|
| 198 |
|
| 199 |
test('instance.exports.llhttp_get_status_code', async (t) => { |
| 200 |
t = tspl(t, { plan: 2 }) |
| 201 |
|
| 202 |
t.ok(typeof instance.exports.llhttp_get_status_code === 'function', 'llhttp_get_status_code is present') |
| 203 |
t.strictEqual(instance.exports.llhttp_get_status_code.length, 1, 'llhttp_get_status_code has the right number of arguments') |
| 204 |
}) |
| 205 |
|
| 206 |
|
| 207 |
test('instance.exports.llhttp_get_upgrade', async (t) => { |
| 208 |
t = tspl(t, { plan: 2 }) |
| 209 |
|
| 210 |
t.ok(typeof instance.exports.llhttp_get_upgrade === 'function', 'llhttp_get_upgrade is present') |
| 211 |
t.strictEqual(instance.exports.llhttp_get_upgrade.length, 1, 'llhttp_get_upgrade has the right number of arguments') |
| 212 |
}) |
| 213 |
|
| 214 |
|
| 215 |
test('instance.exports.llhttp_reset', async (t) => { |
| 216 |
t = tspl(t, { plan: 2 }) |
| 217 |
|
| 218 |
t.ok(typeof instance.exports.llhttp_reset === 'function', 'llhttp_reset is present') |
| 219 |
t.strictEqual(instance.exports.llhttp_reset.length, 1, 'llhttp_reset has the right number of arguments') |
| 220 |
}) |
| 221 |
|
| 222 |
|
| 223 |
test('instance.exports.llhttp_execute', async (t) => { |
| 224 |
t = tspl(t, { plan: 2 }) |
| 225 |
|
| 226 |
t.ok(typeof instance.exports.llhttp_execute === 'function', 'llhttp_execute is present') |
| 227 |
t.strictEqual(instance.exports.llhttp_execute.length, 3, 'llhttp_execute has the right number of arguments') |
| 228 |
}) |
| 229 |
|
| 230 |
|
| 231 |
test('instance.exports.llhttp_settings_init', async (t) => { |
| 232 |
t = tspl(t, { plan: 2 }) |
| 233 |
|
| 234 |
t.ok(typeof instance.exports.llhttp_settings_init === 'function', 'llhttp_settings_init is present') |
| 235 |
t.strictEqual(instance.exports.llhttp_settings_init.length, 1, 'llhttp_settings_init has the right number of arguments') |
| 236 |
}) |
| 237 |
|
| 238 |
|
| 239 |
test('instance.exports.llhttp_finish', async (t) => { |
| 240 |
t = tspl(t, { plan: 2 }) |
| 241 |
|
| 242 |
t.ok(typeof instance.exports.llhttp_finish === 'function', 'llhttp_finish is present') |
| 243 |
t.strictEqual(instance.exports.llhttp_finish.length, 1, 'llhttp_finish has the right number of arguments') |
| 244 |
}) |
| 245 |
|
| 246 |
|
| 247 |
test('instance.exports.llhttp_pause', async (t) => { |
| 248 |
t = tspl(t, { plan: 2 }) |
| 249 |
|
| 250 |
t.ok(typeof instance.exports.llhttp_pause === 'function', 'llhttp_pause is present') |
| 251 |
t.strictEqual(instance.exports.llhttp_pause.length, 1, 'llhttp_pause has the right number of arguments') |
| 252 |
}) |
| 253 |
|
| 254 |
|
| 255 |
test('instance.exports.llhttp_resume', async (t) => { |
| 256 |
t = tspl(t, { plan: 2 }) |
| 257 |
|
| 258 |
t.ok(typeof instance.exports.llhttp_resume === 'function', 'llhttp_resume is present') |
| 259 |
t.strictEqual(instance.exports.llhttp_resume.length, 1, 'llhttp_resume has the right number of arguments') |
| 260 |
}) |
| 261 |
|
| 262 |
|
| 263 |
test('instance.exports.llhttp_resume_after_upgrade', async (t) => { |
| 264 |
t = tspl(t, { plan: 2 }) |
| 265 |
|
| 266 |
t.ok(typeof instance.exports.llhttp_resume_after_upgrade === 'function', 'llhttp_resume_after_upgrade is present') |
| 267 |
t.strictEqual(instance.exports.llhttp_resume_after_upgrade.length, 1, 'llhttp_resume_after_upgrade has the right number of arguments') |
| 268 |
}) |
| 269 |
|
| 270 |
|
| 271 |
test('instance.exports.llhttp_get_errno', async (t) => { |
| 272 |
t = tspl(t, { plan: 2 }) |
| 273 |
|
| 274 |
t.ok(typeof instance.exports.llhttp_get_errno === 'function', 'llhttp_get_errno is present') |
| 275 |
t.strictEqual(instance.exports.llhttp_get_errno.length, 1, 'llhttp_get_errno has the right number of arguments') |
| 276 |
}) |
| 277 |
|
| 278 |
|
| 279 |
test('instance.exports.llhttp_get_error_reason', async (t) => { |
| 280 |
t = tspl(t, { plan: 2 }) |
| 281 |
|
| 282 |
t.ok(typeof instance.exports.llhttp_get_error_reason === 'function', 'llhttp_get_error_reason is present') |
| 283 |
t.strictEqual(instance.exports.llhttp_get_error_reason.length, 1, 'llhttp_get_error_reason has the right number of arguments') |
| 284 |
}) |
| 285 |
|
| 286 |
|
| 287 |
test('instance.exports.llhttp_set_error_reason', async (t) => { |
| 288 |
t = tspl(t, { plan: 2 }) |
| 289 |
|
| 290 |
t.ok(typeof instance.exports.llhttp_set_error_reason === 'function', 'llhttp_set_error_reason is present') |
| 291 |
t.strictEqual(instance.exports.llhttp_set_error_reason.length, 2, 'llhttp_set_error_reason has the right number of arguments') |
| 292 |
}) |
| 293 |
|
| 294 |
|
| 295 |
test('instance.exports.llhttp_get_error_pos', async (t) => { |
| 296 |
t = tspl(t, { plan: 2 }) |
| 297 |
|
| 298 |
t.ok(typeof instance.exports.llhttp_get_error_pos === 'function', 'llhttp_get_error_pos is present') |
| 299 |
t.strictEqual(instance.exports.llhttp_get_error_pos.length, 1, 'llhttp_get_error_pos has the right number of arguments') |
| 300 |
}) |
| 301 |
|
| 302 |
|
| 303 |
test('instance.exports.llhttp_errno_name', async (t) => { |
| 304 |
t = tspl(t, { plan: 2 }) |
| 305 |
|
| 306 |
t.ok(typeof instance.exports.llhttp_errno_name === 'function', 'llhttp_errno_name is present') |
| 307 |
t.strictEqual(instance.exports.llhttp_errno_name.length, 1, 'llhttp_errno_name has the right number of arguments') |
| 308 |
}) |
| 309 |
|
| 310 |
|
| 311 |
test('instance.exports.llhttp_method_name', async (t) => { |
| 312 |
t = tspl(t, { plan: 2 }) |
| 313 |
|
| 314 |
t.ok(typeof instance.exports.llhttp_method_name === 'function', 'llhttp_method_name is present') |
| 315 |
t.strictEqual(instance.exports.llhttp_method_name.length, 1, 'llhttp_method_name has the right number of arguments') |
| 316 |
}) |
| 317 |
|
| 318 |
|
| 319 |
test('instance.exports.llhttp_status_name', async (t) => { |
| 320 |
t = tspl(t, { plan: 2 }) |
| 321 |
|
| 322 |
t.ok(typeof instance.exports.llhttp_status_name === 'function', 'llhttp_status_name is present') |
| 323 |
t.strictEqual(instance.exports.llhttp_status_name.length, 1, 'llhttp_status_name has the right number of arguments') |
| 324 |
}) |
| 325 |
|
| 326 |
|
| 327 |
test('instance.exports.llhttp_set_lenient_headers', async (t) => { |
| 328 |
t = tspl(t, { plan: 2 }) |
| 329 |
|
| 330 |
t.ok(typeof instance.exports.llhttp_set_lenient_headers === 'function', 'llhttp_set_lenient_headers is present') |
| 331 |
t.strictEqual(instance.exports.llhttp_set_lenient_headers.length, 2, 'llhttp_set_lenient_headers has the right number of arguments') |
| 332 |
}) |
| 333 |
|
| 334 |
|
| 335 |
test('instance.exports.llhttp_set_lenient_chunked_length', async (t) => { |
| 336 |
t = tspl(t, { plan: 2 }) |
| 337 |
|
| 338 |
t.ok(typeof instance.exports.llhttp_set_lenient_chunked_length === 'function', 'llhttp_set_lenient_chunked_length is present') |
| 339 |
t.strictEqual(instance.exports.llhttp_set_lenient_chunked_length.length, 2, 'llhttp_set_lenient_chunked_length has the right number of arguments') |
| 340 |
}) |
| 341 |
|
| 342 |
|
| 343 |
test('instance.exports.llhttp_set_lenient_keep_alive', async (t) => { |
| 344 |
t = tspl(t, { plan: 2 }) |
| 345 |
|
| 346 |
t.ok(typeof instance.exports.llhttp_set_lenient_keep_alive === 'function', 'llhttp_set_lenient_keep_alive is present') |
| 347 |
t.strictEqual(instance.exports.llhttp_set_lenient_keep_alive.length, 2, 'llhttp_set_lenient_keep_alive has the right number of arguments') |
| 348 |
}) |
| 349 |
|
| 350 |
|
| 351 |
test('instance.exports.llhttp_set_lenient_transfer_encoding', async (t) => { |
| 352 |
t = tspl(t, { plan: 2 }) |
| 353 |
|
| 354 |
t.ok(typeof instance.exports.llhttp_set_lenient_transfer_encoding === 'function', 'llhttp_set_lenient_transfer_encoding is present') |
| 355 |
t.strictEqual(instance.exports.llhttp_set_lenient_transfer_encoding.length, 2, 'llhttp_set_lenient_transfer_encoding has the right number of arguments') |
| 356 |
}) |
| 357 |
|
| 358 |
|
| 359 |
test('instance.exports.llhttp_message_needs_eof', async (t) => { |
| 360 |
t = tspl(t, { plan: 2 }) |
| 361 |
|
| 362 |
t.ok(typeof instance.exports.llhttp_message_needs_eof === 'function', 'llhttp_message_needs_eof is present') |
| 363 |
t.strictEqual(instance.exports.llhttp_message_needs_eof.length, 1, 'llhttp_message_needs_eof has the right number of arguments') |
| 364 |
}) |
| 365 |
}) |
| 366 |
}) |
| 367 |
}) |