Elysia Tools
Navigation mobile
Protocoles de communication
MQTT : parsing et QoS hors ligne
Longueurs MQTT 3.1.1, filtres ASCII et modèle QoS 1 sans broker.
Exemples
Entrées de cette collection
Décoder Remaining Length
127, 128, 16384, 268435455 correspondent aux octets fixes. Troncature et quatre octets de continuation rejetés; pas parseur complet.
import assert from 'node:assert/strict';
// MQTT 3.1.1 Remaining Length only, not a full packet decoder.
function encode(n){
if(!Number.isInteger(n)||n<0||n>268435455)throw new RangeError('range');
const out=[];do{let d=n%128;n=Math.floor(n/128);if(n)d|=128;out.push(d);}while(n);return Uint8Array.from(out);
}
function decode(bytes){let value=0,m=1;
for(let i=0;i<4;i++){if(i>=bytes.length)throw new RangeError('truncated');const b=bytes[i];value+=(b&127)*m;if(!(b&128))return {value,bytes:i+1};m*=128;}
throw new RangeError('more than four bytes');
}
for(const [n,hex] of [[127,'7f'],[128,'8001'],[16384,'808001'],[268435455,'ffffff7f']]){
assert.equal(Buffer.from(encode(n)).toString('hex'),hex);assert.equal(decode(Buffer.from(hex,'hex')).value,n);
}
assert.throws(()=>decode(Uint8Array.of(128)),/truncated/);
assert.throws(()=>decode(Uint8Array.of(128,128,128,128)),/four/);
console.log('127/128/16384/max boundaries pass; malformed lengths rejected');
- runtime
- Node.js 24.22.1
- execution
- offline authored fixtures
- dependencies
- {}
Comparer les filtres
+, # final, niveaux vides et $ testés. lab/# correspond à lab; # pas à $SYS/status. ASCII imprimable seulement.
import assert from 'node:assert/strict';
// MQTT 3.1.1 topic-filter exercise limited to printable ASCII.
function match(filter,topic){
for(const v of [filter,topic])if(!v||!/^[\x20-\x7e]+$/.test(v)||v.length>65535)throw new Error('ASCII subset');
if(/[+#]/.test(topic))throw new Error('topic name');
const f=filter.split('/'),t=topic.split('/');
f.forEach((x,i)=>{if(x.includes('#')&&(x!=='#'||i!==f.length-1)||x.includes('+')&&x!=='+')throw new Error('filter');});
if(topic.startsWith('$')&&(f[0]==='#'||f[0]==='+'))return false;
let i=0;for(;i<f.length;i++){if(f[i]==='#')return true;if(i>=t.length||f[i]!=='+'&&f[i]!==t[i])return false;}return i===t.length;
}
assert.equal(match('lab/+/temp','lab/a/temp'),true);
assert.equal(match('lab/+','lab/'),true); // empty level counts as a level
assert.equal(match('lab/#','lab'),true);
assert.equal(match('#','$SYS/status'),false);
assert.equal(match('$SYS/#','$SYS/status'),true);
assert.throws(()=>match('lab/#/bad','lab/a'),/filter/);
console.log('single/multi-level, empty level and $ namespace cases pass');
- runtime
- Node.js 24.22.1
- execution
- offline authored fixtures
- dependencies
- {}
Suivre un retry QoS 1
Retry fictif : deux livraisons. PUBACK retire une fois l’ID, second ACK sans effet. ID réutilisable; aucune garantie exactement-une-fois.
import assert from 'node:assert/strict';
// In-memory QoS 1 teaching model. NOT a broker, wire client or QoS 2.
const pending=new Map(),deliveries=[];
function publish(id,payload){if(!Number.isInteger(id)||id<1||id>65535||pending.has(id))throw new Error('packet id');pending.set(id,payload);deliveries.push(payload);}
function retry(id){if(!pending.has(id))throw new Error('not pending');deliveries.push(pending.get(id));}
function puback(id){return pending.delete(id);}
publish(7,'sample');retry(7);assert.equal(deliveries.length,2);
assert.equal(puback(7),true);assert.equal(puback(7),false);assert.equal(pending.size,0);
assert.throws(()=>retry(7),/not pending/);
publish(7,'next');assert.equal(pending.size,1);
console.log('duplicate delivery retained; PUBACK clears pending; ID reusable');
- 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é