1'use strict'; 2 3// An HTTP/2 testing tool used to create mock frames for direct testing 4// of HTTP/2 endpoints. 5 6const kFrameData = Symbol('frame-data'); 7const FLAG_EOS = 0x1; 8const FLAG_ACK = 0x1; 9const FLAG_EOH = 0x4; 10const FLAG_PADDED = 0x8; 11const PADDING = Buffer.alloc(255); 12 13const kClientMagic = Buffer.from('505249202a20485454502f322' + 14 'e300d0a0d0a534d0d0a0d0a', 'hex'); 15 16const kFakeRequestHeaders = Buffer.from('828684410f7777772e65' + 17 '78616d706c652e636f6d', 'hex'); 18 19 20const kFakeResponseHeaders = Buffer.from('4803333032580770726976617465611d' + 21 '4d6f6e2c203231204f63742032303133' + 22 '2032303a31333a323120474d546e1768' + 23 '747470733a2f2f7777772e6578616d70' + 24 '6c652e636f6d', 'hex'); 25 26function isUint32(val) { 27 return val >>> 0 === val; 28} 29 30function isUint24(val) { 31 return val >>> 0 === val && val <= 0xFFFFFF; 32} 33 34function isUint8(val) { 35 return val >>> 0 === val && val <= 0xFF; 36} 37 38function write32BE(array, pos, val) { 39 if (!isUint32(val)) 40 throw new RangeError('val is not a 32-bit number'); 41 array[pos++] = (val >> 24) & 0xff; 42 array[pos++] = (val >> 16) & 0xff; 43 array[pos++] = (val >> 8) & 0xff; 44 array[pos++] = val & 0xff; 45} 46 47function write24BE(array, pos, val) { 48 if (!isUint24(val)) 49 throw new RangeError('val is not a 24-bit number'); 50 array[pos++] = (val >> 16) & 0xff; 51 array[pos++] = (val >> 8) & 0xff; 52 array[pos++] = val & 0xff; 53} 54 55function write8(array, pos, val) { 56 if (!isUint8(val)) 57 throw new RangeError('val is not an 8-bit number'); 58 array[pos] = val; 59} 60 61class Frame { 62 constructor(length, type, flags, id) { 63 this[kFrameData] = Buffer.alloc(9); 64 write24BE(this[kFrameData], 0, length); 65 write8(this[kFrameData], 3, type); 66 write8(this[kFrameData], 4, flags); 67 write32BE(this[kFrameData], 5, id); 68 } 69 70 get data() { 71 return this[kFrameData]; 72 } 73} 74 75class SettingsFrame extends Frame { 76 constructor(ack = false) { 77 let flags = 0; 78 if (ack) 79 flags |= FLAG_ACK; 80 super(0, 4, flags, 0); 81 } 82} 83 84class DataFrame extends Frame { 85 constructor(id, payload, padlen = 0, final = false) { 86 let len = payload.length; 87 let flags = 0; 88 if (final) flags |= FLAG_EOS; 89 const buffers = [payload]; 90 if (padlen > 0) { 91 buffers.unshift(Buffer.from([padlen])); 92 buffers.push(PADDING.slice(0, padlen)); 93 len += padlen + 1; 94 flags |= FLAG_PADDED; 95 } 96 super(len, 0, flags, id); 97 buffers.unshift(this[kFrameData]); 98 this[kFrameData] = Buffer.concat(buffers); 99 } 100} 101 102class HeadersFrame extends Frame { 103 constructor(id, payload, padlen = 0, final = false) { 104 let len = payload.length; 105 let flags = FLAG_EOH; 106 if (final) flags |= FLAG_EOS; 107 const buffers = [payload]; 108 if (padlen > 0) { 109 buffers.unshift(Buffer.from([padlen])); 110 buffers.push(PADDING.slice(0, padlen)); 111 len += padlen + 1; 112 flags |= FLAG_PADDED; 113 } 114 super(len, 1, flags, id); 115 buffers.unshift(this[kFrameData]); 116 this[kFrameData] = Buffer.concat(buffers); 117 } 118} 119 120class PingFrame extends Frame { 121 constructor(ack = false) { 122 const buffers = [Buffer.alloc(8)]; 123 super(8, 6, ack ? 1 : 0, 0); 124 buffers.unshift(this[kFrameData]); 125 this[kFrameData] = Buffer.concat(buffers); 126 } 127} 128 129class AltSvcFrame extends Frame { 130 constructor(size) { 131 const buffers = [Buffer.alloc(size)]; 132 super(size, 10, 0, 0); 133 buffers.unshift(this[kFrameData]); 134 this[kFrameData] = Buffer.concat(buffers); 135 } 136} 137 138module.exports = { 139 Frame, 140 AltSvcFrame, 141 DataFrame, 142 HeadersFrame, 143 SettingsFrame, 144 PingFrame, 145 kFakeRequestHeaders, 146 kFakeResponseHeaders, 147 kClientMagic 148}; 149