Elysia Tools
Navigation mobile
Protocoles réseau
QUIC : exercices de format hors ligne
Les exemples QUIC proposent des exercices limités sur entiers variables, trames STREAM en clair et identifiants de flux.
Exemples
Entrées de cette collection
Vérifier les entiers RFC
Quatre vecteurs RFC décodés/réencodés exactement, dont 151288809941952652 via BigInt. 37 non minimal accepté; troncature et 2^62 rejetés.
import assert from 'node:assert/strict';
function readVarint(bytes, offset = 0) {
if (offset >= bytes.length) throw new RangeError('truncated');
const size = 1 << (bytes[offset] >> 6);
if (offset + size > bytes.length) throw new RangeError('truncated');
let value = BigInt(bytes[offset] & 63);
for (let i = 1; i < size; i++) value = (value << 8n) | BigInt(bytes[offset+i]);
return { value, next: offset + size };
}
function writeVarint(value) {
if (typeof value !== 'bigint' || value < 0n || value >= (1n << 62n)) throw new RangeError('range');
const size = value < 64n ? 1 : value < 16384n ? 2 : value < 1073741824n ? 4 : 8;
const bytes = new Uint8Array(size); let n = value;
for (let i = size-1; i >= 0; i--) { bytes[i] = Number(n & 255n); n >>= 8n; }
bytes[0] |= ({1:0, 2:1, 4:2, 8:3}[size] << 6);
return bytes;
}
// RFC 9000 Appendix A.1 fixed vectors, not a self-only round trip.
const vectors = [['25',37n],['7bbd',15293n],['9d7f3e7d',494878333n],['c2197c5eff14e88c',151288809941952652n]];
for (const [hex, value] of vectors) {
assert.equal(readVarint(Buffer.from(hex,'hex')).value,value);
assert.equal(Buffer.from(writeVarint(value)).toString('hex'),hex);
}
assert.equal(readVarint(Buffer.from('4025','hex')).value,37n); // valid non-minimal encoding
assert.throws(()=>readVarint(Uint8Array.of(0x40)),/truncated/);
assert.throws(()=>writeVarint(1n<<62n),/range/);
console.log('4 RFC vectors; non-minimal 37; truncated/range rejected');
- runtime
- Node.js 24.22.1
- execution
- offline authored fixtures
- dependencies
- {}
Lire une trame STREAM
0b0003616263 : flux 0, offset 0, FIN, trois octets abc. Charge tronquée rejetée. Contenu de trame, pas paquet QUIC chiffré.
import assert from 'node:assert/strict';
function readVarint(bytes, offset = 0) {
if (offset >= bytes.length) throw new RangeError('truncated');
const size = 1 << (bytes[offset] >> 6);
if (offset + size > bytes.length) throw new RangeError('truncated');
let value = BigInt(bytes[offset] & 63);
for (let i = 1; i < size; i++) value = (value << 8n) | BigInt(bytes[offset+i]);
return { value, next: offset + size };
}
function writeVarint(value) {
if (typeof value !== 'bigint' || value < 0n || value >= (1n << 62n)) throw new RangeError('range');
const size = value < 64n ? 1 : value < 16384n ? 2 : value < 1073741824n ? 4 : 8;
const bytes = new Uint8Array(size); let n = value;
for (let i = size-1; i >= 0; i--) { bytes[i] = Number(n & 255n); n >>= 8n; }
bytes[0] |= ({1:0, 2:1, 4:2, 8:3}[size] << 6);
return bytes;
}
// One plaintext STREAM frame only: not a packet, TLS or QUIC connection.
function parseStream(bytes) {
let cursor=readVarint(bytes); const type=cursor.value;
if (type<8n || type>15n) throw new Error('not STREAM');
const take=()=>{const r=readVarint(bytes,cursor.next);cursor=r;return r.value;};
const streamId=take(); const offset=(type&4n)?take():0n;
const length=(type&2n)?take():BigInt(bytes.length-cursor.next);
if(length>BigInt(bytes.length-cursor.next))throw new RangeError('truncated data');
if(offset+length>=(1n<<62n))throw new RangeError('final size');
const end=cursor.next+Number(length);
return {streamId,offset,fin:Boolean(type&1n),data:bytes.slice(cursor.next,end),next:end};
}
const frame=Uint8Array.of(0x0b,0,3,0x61,0x62,0x63); // LEN+FIN, stream 0, offset absent
const r=parseStream(frame);
assert.deepEqual([r.streamId,r.offset,r.fin,r.next],[0n,0n,true,6]);
assert.equal(Buffer.from(r.data).toString(),'abc');
assert.throws(()=>parseStream(frame.slice(0,-1)),/truncated/);
const noLen=parseStream(Uint8Array.of(0x08,4,0x78));
assert.deepEqual([noLen.streamId,noLen.data.length,noLen.fin],[4n,1,false]);
console.log('STREAM 0: offset 0, FIN true, 3 bytes; truncation rejected');
- runtime
- Node.js 24.22.1
- execution
- offline authored fixtures
- dependencies
- {}
Classer les identifiants
0–3 couvrent quatre couples initiateur/direction. 8 : client, bidirectionnel, ordinal 2. Aucun flux ouvert.
import assert from 'node:assert/strict';
// RFC 9000 section 2.1: classification only, no stream is opened.
function classify(id) {
if(typeof id!=='bigint'||id<0n||id>=(1n<<62n))throw new RangeError('stream id');
return {initiator:(id&1n)?'server':'client',direction:(id&2n)?'unidirectional':'bidirectional',ordinal:id>>2n};
}
assert.deepEqual([0n,1n,2n,3n].map(classify).map(x=>x.initiator+'/'+x.direction),[
'client/bidirectional','server/bidirectional','client/unidirectional','server/unidirectional']);
assert.deepEqual(classify(8n),{initiator:'client',direction:'bidirectional',ordinal:2n});
assert.throws(()=>classify(-1n),/stream id/);
console.log('IDs 0..3: four types; ID 8: client bidirectional ordinal 2');
- runtime
- Node.js 24.22.1
- execution
- offline authored fixtures
- dependencies
- {}
Outils
Outils souvent utilisés avec cet exemple
Associé
Collections d’exemples associées
Associé