UNPKG

andrade-soulseek-downloader

Version:

Simple, safe Soulseek download library with built-in rate limiting to prevent bans

99 lines 2.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Message = void 0; class Message { data; write; pointer = 0; constructor(buffer) { if (buffer) { this.data = buffer; this.write = false; } else { this.data = Buffer.alloc(0); this.write = true; } } int8(val) { return this.write ? this.write8(val) : this.read8(); } int32(val) { return this.write ? this.write32(val) : this.read32(); } str(val) { return this.write ? this.writeStr(val) : this.readStr(); } rawHexStr(val) { return this.write ? this.writeRawHexStr(val) : this.readRawHexStr(val); } size() { return this.data.length; } seek(val) { this.pointer += val; } write8(val) { const b = Buffer.alloc(1); b.writeUInt8(val, 0); this.data = Buffer.concat([this.data, b]); this.pointer += 1; return this; } write32(val) { const b = Buffer.alloc(4); b.writeUInt32LE(val, 0); this.data = Buffer.concat([this.data, b]); this.pointer += 4; return this; } writeStr(val) { const b = Buffer.from(val, 'utf8'); const s = Buffer.alloc(4); s.writeUInt32LE(b.length, 0); this.data = Buffer.concat([this.data, s, b]); return this; } writeRawHexStr(val) { const b = Buffer.from(val, 'hex'); this.data = Buffer.concat([this.data, b]); this.pointer += b.length; return this; } writeBuffer(buff) { this.data = Buffer.concat([this.data, buff]); this.pointer += buff.length; return this; } read8() { const val = this.data.readUInt8(this.pointer); this.pointer += 1; return val; } read32() { const val = this.data.readUInt32LE(this.pointer); this.pointer += 4; return val; } readStr() { const size = this.data.readUInt32LE(this.pointer); this.pointer += 4; const str = this.data.toString('utf8', this.pointer, this.pointer + size); this.pointer += size; return str; } readRawHexStr(size) { const str = this.data.toString('hex', this.pointer, this.pointer + size); this.pointer += size; return str; } getBuff() { const b = Buffer.alloc(4); b.writeUInt32LE(this.data.length, 0); this.data = Buffer.concat([b, this.data]); this.write = false; return this.data; } } exports.Message = Message; //# sourceMappingURL=message.js.map