Skip to content

Commit 59ddfa3

Browse files
committed
feat: 🎸 cleanup encoder implementation
1 parent 623ebf2 commit 59ddfa3

File tree

1 file changed

+97
-77
lines changed

1 file changed

+97
-77
lines changed

src/nfs/v3/Nfsv3Decoder.ts

Lines changed: 97 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -138,28 +138,31 @@ export class Nfsv3Decoder {
138138
}
139139

140140
private readTime(): structs.Nfsv3Time {
141-
const seconds = this.xdr.readUnsignedInt();
142-
const nseconds = this.xdr.readUnsignedInt();
141+
const xdr = this.xdr;
142+
const seconds = xdr.readUnsignedInt();
143+
const nseconds = xdr.readUnsignedInt();
143144
return new structs.Nfsv3Time(seconds, nseconds);
144145
}
145146

146147
private readSpecData(): structs.Nfsv3SpecData {
147-
const specdata1 = this.xdr.readUnsignedInt();
148-
const specdata2 = this.xdr.readUnsignedInt();
148+
const xdr = this.xdr;
149+
const specdata1 = xdr.readUnsignedInt();
150+
const specdata2 = xdr.readUnsignedInt();
149151
return new structs.Nfsv3SpecData(specdata1, specdata2);
150152
}
151153

152154
private readFattr(): structs.Nfsv3Fattr {
153-
const type = this.xdr.readUnsignedInt() as Nfsv3FType;
154-
const mode = this.xdr.readUnsignedInt();
155-
const nlink = this.xdr.readUnsignedInt();
156-
const uid = this.xdr.readUnsignedInt();
157-
const gid = this.xdr.readUnsignedInt();
158-
const size = this.xdr.readUnsignedHyper();
159-
const used = this.xdr.readUnsignedHyper();
155+
const xdr = this.xdr;
156+
const type = xdr.readUnsignedInt() as Nfsv3FType;
157+
const mode = xdr.readUnsignedInt();
158+
const nlink = xdr.readUnsignedInt();
159+
const uid = xdr.readUnsignedInt();
160+
const gid = xdr.readUnsignedInt();
161+
const size = xdr.readUnsignedHyper();
162+
const used = xdr.readUnsignedHyper();
160163
const rdev = this.readSpecData();
161-
const fsid = this.xdr.readUnsignedHyper();
162-
const fileid = this.xdr.readUnsignedHyper();
164+
const fsid = xdr.readUnsignedHyper();
165+
const fileid = xdr.readUnsignedHyper();
163166
const atime = this.readTime();
164167
const mtime = this.readTime();
165168
const ctime = this.readTime();
@@ -256,13 +259,14 @@ export class Nfsv3Decoder {
256259
}
257260

258261
private readCreateHow(): structs.Nfsv3CreateHow {
259-
const mode = this.xdr.readUnsignedInt() as Nfsv3CreateMode;
262+
const xdr = this.xdr;
263+
const mode = xdr.readUnsignedInt() as Nfsv3CreateMode;
260264
let objAttributes: structs.Nfsv3Sattr | undefined;
261265
let verf: Reader | undefined;
262266
if (mode === Nfsv3CreateMode.UNCHECKED || mode === Nfsv3CreateMode.GUARDED) {
263267
objAttributes = this.readSattr();
264268
} else if (mode === Nfsv3CreateMode.EXCLUSIVE) {
265-
const verfData = this.xdr.readOpaque(8);
269+
const verfData = xdr.readOpaque(8);
266270
verf = new Reader(verfData);
267271
}
268272
return new structs.Nfsv3CreateHow(mode, objAttributes, verf);
@@ -292,21 +296,23 @@ export class Nfsv3Decoder {
292296
}
293297

294298
private readEntry(): structs.Nfsv3Entry | undefined {
295-
const valueFollows = this.xdr.readBoolean();
299+
const xdr = this.xdr;
300+
const valueFollows = xdr.readBoolean();
296301
if (!valueFollows) return undefined;
297-
const fileid = this.xdr.readUnsignedHyper();
302+
const fileid = xdr.readUnsignedHyper();
298303
const name = this.readFilename();
299-
const cookie = this.xdr.readUnsignedHyper();
304+
const cookie = xdr.readUnsignedHyper();
300305
const nextentry = this.readEntry();
301306
return new structs.Nfsv3Entry(fileid, name, cookie, nextentry);
302307
}
303308

304309
private readEntryPlus(): structs.Nfsv3EntryPlus | undefined {
305-
const valueFollows = this.xdr.readBoolean();
310+
const xdr = this.xdr;
311+
const valueFollows = xdr.readBoolean();
306312
if (!valueFollows) return undefined;
307-
const fileid = this.xdr.readUnsignedHyper();
313+
const fileid = xdr.readUnsignedHyper();
308314
const name = this.readFilename();
309-
const cookie = this.xdr.readUnsignedHyper();
315+
const cookie = xdr.readUnsignedHyper();
310316
const nameAttributes = this.readPostOpAttr();
311317
const nameHandle = this.readPostOpFh();
312318
const nextentry = this.readEntryPlus();
@@ -388,12 +394,13 @@ export class Nfsv3Decoder {
388394
}
389395

390396
private decodeAccessResponse(): msg.Nfsv3AccessResponse {
391-
const status = this.xdr.readUnsignedInt();
397+
const xdr = this.xdr;
398+
const status = xdr.readUnsignedInt();
392399
let resok: msg.Nfsv3AccessResOk | undefined;
393400
let resfail: msg.Nfsv3AccessResFail | undefined;
394401
const objAttributes = this.readPostOpAttr();
395402
if (status === 0) {
396-
const access = this.xdr.readUnsignedInt();
403+
const access = xdr.readUnsignedInt();
397404
resok = new msg.Nfsv3AccessResOk(objAttributes, access);
398405
} else {
399406
resfail = new msg.Nfsv3AccessResFail(objAttributes);
@@ -422,8 +429,9 @@ export class Nfsv3Decoder {
422429

423430
private decodeReadRequest(): msg.Nfsv3ReadRequest {
424431
const file = this.readFh();
425-
const offset = this.xdr.readUnsignedHyper();
426-
const count = this.xdr.readUnsignedInt();
432+
const xdr = this.xdr;
433+
const offset = xdr.readUnsignedHyper();
434+
const count = xdr.readUnsignedInt();
427435
return new msg.Nfsv3ReadRequest(file, offset, count);
428436
}
429437

@@ -433,9 +441,10 @@ export class Nfsv3Decoder {
433441
let resfail: msg.Nfsv3ReadResFail | undefined;
434442
const fileAttributes = this.readPostOpAttr();
435443
if (status === 0) {
436-
const count = this.xdr.readUnsignedInt();
437-
const eof = this.xdr.readBoolean();
438-
const data = this.xdr.readVarlenOpaque();
444+
const xdr = this.xdr;
445+
const count = xdr.readUnsignedInt();
446+
const eof = xdr.readBoolean();
447+
const data = xdr.readVarlenOpaque();
439448
resok = new msg.Nfsv3ReadResOk(fileAttributes, count, eof, new Reader(data));
440449
} else {
441450
resfail = new msg.Nfsv3ReadResFail(fileAttributes);
@@ -445,22 +454,24 @@ export class Nfsv3Decoder {
445454

446455
private decodeWriteRequest(): msg.Nfsv3WriteRequest {
447456
const file = this.readFh();
448-
const offset = this.xdr.readUnsignedHyper();
449-
const count = this.xdr.readUnsignedInt();
450-
const stable = this.xdr.readUnsignedInt();
451-
const data = this.xdr.readVarlenOpaque();
457+
const xdr = this.xdr;
458+
const offset = xdr.readUnsignedHyper();
459+
const count = xdr.readUnsignedInt();
460+
const stable = xdr.readUnsignedInt();
461+
const data = xdr.readVarlenOpaque();
452462
return new msg.Nfsv3WriteRequest(file, offset, count, stable, new Reader(data));
453463
}
454464

455465
private decodeWriteResponse(): msg.Nfsv3WriteResponse {
456-
const status = this.xdr.readUnsignedInt();
466+
const xdr = this.xdr;
467+
const status = xdr.readUnsignedInt();
457468
let resok: msg.Nfsv3WriteResOk | undefined;
458469
let resfail: msg.Nfsv3WriteResFail | undefined;
459470
const fileWcc = this.readWccData();
460471
if (status === 0) {
461-
const count = this.xdr.readUnsignedInt();
462-
const committed = this.xdr.readUnsignedInt();
463-
const verf = this.xdr.readOpaque(8);
472+
const count = xdr.readUnsignedInt();
473+
const committed = xdr.readUnsignedInt();
474+
const verf = xdr.readOpaque(8);
464475
resok = new msg.Nfsv3WriteResOk(fileWcc, count, committed, new Reader(verf));
465476
} else {
466477
resfail = new msg.Nfsv3WriteResFail(fileWcc);
@@ -635,19 +646,21 @@ export class Nfsv3Decoder {
635646

636647
private decodeReaddirRequest(): msg.Nfsv3ReaddirRequest {
637648
const dir = this.readFh();
638-
const cookie = this.xdr.readUnsignedHyper();
639-
const cookieverf = this.xdr.readOpaque(8);
640-
const count = this.xdr.readUnsignedInt();
649+
const xdr = this.xdr;
650+
const cookie = xdr.readUnsignedHyper();
651+
const cookieverf = xdr.readOpaque(8);
652+
const count = xdr.readUnsignedInt();
641653
return new msg.Nfsv3ReaddirRequest(dir, cookie, new Reader(cookieverf), count);
642654
}
643655

644656
private decodeReaddirResponse(): msg.Nfsv3ReaddirResponse {
645-
const status = this.xdr.readUnsignedInt();
657+
const xdr = this.xdr;
658+
const status = xdr.readUnsignedInt();
646659
let resok: msg.Nfsv3ReaddirResOk | undefined;
647660
let resfail: msg.Nfsv3ReaddirResFail | undefined;
648661
const dirAttributes = this.readPostOpAttr();
649662
if (status === 0) {
650-
const cookieverf = this.xdr.readOpaque(8);
663+
const cookieverf = xdr.readOpaque(8);
651664
const reply = this.readDirList();
652665
resok = new msg.Nfsv3ReaddirResOk(dirAttributes, new Reader(cookieverf), reply);
653666
} else {
@@ -658,20 +671,22 @@ export class Nfsv3Decoder {
658671

659672
private decodeReaddirplusRequest(): msg.Nfsv3ReaddirplusRequest {
660673
const dir = this.readFh();
661-
const cookie = this.xdr.readUnsignedHyper();
662-
const cookieverf = this.xdr.readOpaque(8);
663-
const dircount = this.xdr.readUnsignedInt();
664-
const maxcount = this.xdr.readUnsignedInt();
674+
const xdr = this.xdr;
675+
const cookie = xdr.readUnsignedHyper();
676+
const cookieverf = xdr.readOpaque(8);
677+
const dircount = xdr.readUnsignedInt();
678+
const maxcount = xdr.readUnsignedInt();
665679
return new msg.Nfsv3ReaddirplusRequest(dir, cookie, new Reader(cookieverf), dircount, maxcount);
666680
}
667681

668682
private decodeReaddirplusResponse(): msg.Nfsv3ReaddirplusResponse {
669-
const status = this.xdr.readUnsignedInt();
683+
const xdr = this.xdr;
684+
const status = xdr.readUnsignedInt();
670685
let resok: msg.Nfsv3ReaddirplusResOk | undefined;
671686
let resfail: msg.Nfsv3ReaddirplusResFail | undefined;
672687
const dirAttributes = this.readPostOpAttr();
673688
if (status === 0) {
674-
const cookieverf = this.xdr.readOpaque(8);
689+
const cookieverf = xdr.readOpaque(8);
675690
const reply = this.readDirListPlus();
676691
resok = new msg.Nfsv3ReaddirplusResOk(dirAttributes, new Reader(cookieverf), reply);
677692
} else {
@@ -686,18 +701,19 @@ export class Nfsv3Decoder {
686701
}
687702

688703
private decodeFsstatResponse(): msg.Nfsv3FsstatResponse {
689-
const status = this.xdr.readUnsignedInt();
704+
const xdr = this.xdr;
705+
const status = xdr.readUnsignedInt();
690706
let resok: msg.Nfsv3FsstatResOk | undefined;
691707
let resfail: msg.Nfsv3FsstatResFail | undefined;
692708
const objAttributes = this.readPostOpAttr();
693709
if (status === 0) {
694-
const tbytes = this.xdr.readUnsignedHyper();
695-
const fbytes = this.xdr.readUnsignedHyper();
696-
const abytes = this.xdr.readUnsignedHyper();
697-
const tfiles = this.xdr.readUnsignedHyper();
698-
const ffiles = this.xdr.readUnsignedHyper();
699-
const afiles = this.xdr.readUnsignedHyper();
700-
const invarsec = this.xdr.readUnsignedInt();
710+
const tbytes = xdr.readUnsignedHyper();
711+
const fbytes = xdr.readUnsignedHyper();
712+
const abytes = xdr.readUnsignedHyper();
713+
const tfiles = xdr.readUnsignedHyper();
714+
const ffiles = xdr.readUnsignedHyper();
715+
const afiles = xdr.readUnsignedHyper();
716+
const invarsec = xdr.readUnsignedInt();
701717
resok = new msg.Nfsv3FsstatResOk(objAttributes, tbytes, fbytes, abytes, tfiles, ffiles, afiles, invarsec);
702718
} else {
703719
resfail = new msg.Nfsv3FsstatResFail(objAttributes);
@@ -711,21 +727,22 @@ export class Nfsv3Decoder {
711727
}
712728

713729
private decodeFsinfoResponse(): msg.Nfsv3FsinfoResponse {
714-
const status = this.xdr.readUnsignedInt();
730+
const xdr = this.xdr;
731+
const status = xdr.readUnsignedInt();
715732
let resok: msg.Nfsv3FsinfoResOk | undefined;
716733
let resfail: msg.Nfsv3FsinfoResFail | undefined;
717734
const objAttributes = this.readPostOpAttr();
718735
if (status === 0) {
719-
const rtmax = this.xdr.readUnsignedInt();
720-
const rtpref = this.xdr.readUnsignedInt();
721-
const rtmult = this.xdr.readUnsignedInt();
722-
const wtmax = this.xdr.readUnsignedInt();
723-
const wtpref = this.xdr.readUnsignedInt();
724-
const wtmult = this.xdr.readUnsignedInt();
725-
const dtpref = this.xdr.readUnsignedInt();
726-
const maxfilesize = this.xdr.readUnsignedHyper();
727-
const timeDelta = {seconds: this.xdr.readUnsignedInt(), nseconds: this.xdr.readUnsignedInt()};
728-
const properties = this.xdr.readUnsignedInt();
736+
const rtmax = xdr.readUnsignedInt();
737+
const rtpref = xdr.readUnsignedInt();
738+
const rtmult = xdr.readUnsignedInt();
739+
const wtmax = xdr.readUnsignedInt();
740+
const wtpref = xdr.readUnsignedInt();
741+
const wtmult = xdr.readUnsignedInt();
742+
const dtpref = xdr.readUnsignedInt();
743+
const maxfilesize = xdr.readUnsignedHyper();
744+
const timeDelta = {seconds: xdr.readUnsignedInt(), nseconds: xdr.readUnsignedInt()};
745+
const properties = xdr.readUnsignedInt();
729746
resok = new msg.Nfsv3FsinfoResOk(
730747
objAttributes,
731748
rtmax,
@@ -751,17 +768,18 @@ export class Nfsv3Decoder {
751768
}
752769

753770
private decodePathconfResponse(): msg.Nfsv3PathconfResponse {
754-
const status = this.xdr.readUnsignedInt();
771+
const xdr = this.xdr;
772+
const status = xdr.readUnsignedInt();
755773
let resok: msg.Nfsv3PathconfResOk | undefined;
756774
let resfail: msg.Nfsv3PathconfResFail | undefined;
757775
const objAttributes = this.readPostOpAttr();
758776
if (status === 0) {
759-
const linkmax = this.xdr.readUnsignedInt();
760-
const namemax = this.xdr.readUnsignedInt();
761-
const noTrunc = this.xdr.readBoolean();
762-
const chownRestricted = this.xdr.readBoolean();
763-
const caseInsensitive = this.xdr.readBoolean();
764-
const casePreserving = this.xdr.readBoolean();
777+
const linkmax = xdr.readUnsignedInt();
778+
const namemax = xdr.readUnsignedInt();
779+
const noTrunc = xdr.readBoolean();
780+
const chownRestricted = xdr.readBoolean();
781+
const caseInsensitive = xdr.readBoolean();
782+
const casePreserving = xdr.readBoolean();
765783
resok = new msg.Nfsv3PathconfResOk(
766784
objAttributes,
767785
linkmax,
@@ -779,18 +797,20 @@ export class Nfsv3Decoder {
779797

780798
private decodeCommitRequest(): msg.Nfsv3CommitRequest {
781799
const file = this.readFh();
782-
const offset = this.xdr.readUnsignedHyper();
783-
const count = this.xdr.readUnsignedInt();
800+
const xdr = this.xdr;
801+
const offset = xdr.readUnsignedHyper();
802+
const count = xdr.readUnsignedInt();
784803
return new msg.Nfsv3CommitRequest(file, offset, count);
785804
}
786805

787806
private decodeCommitResponse(): msg.Nfsv3CommitResponse {
788-
const status = this.xdr.readUnsignedInt();
807+
const xdr = this.xdr;
808+
const status = xdr.readUnsignedInt();
789809
let resok: msg.Nfsv3CommitResOk | undefined;
790810
let resfail: msg.Nfsv3CommitResFail | undefined;
791811
const fileWcc = this.readWccData();
792812
if (status === 0) {
793-
const verf = this.xdr.readOpaque(8);
813+
const verf = xdr.readOpaque(8);
794814
resok = new msg.Nfsv3CommitResOk(fileWcc, new Reader(verf));
795815
} else {
796816
resfail = new msg.Nfsv3CommitResFail(fileWcc);

0 commit comments

Comments
 (0)