refactor: address lint and formatting issues

This commit is contained in:
Erica Marigold 2024-03-27 17:09:50 +05:30
parent c9301f4eb4
commit 0e398939ce
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1
7 changed files with 256 additions and 370 deletions

View file

@ -1,6 +1,6 @@
{
"printWidth": 120,
"tabWidth": 4,
"tabWidth": 5,
"trailingComma": "all",
"useTabs": true
}

View file

@ -15,6 +15,7 @@
// Linting
"eslint.run": "onType",
"eslint.format.enable": true,
"cSpell.enabled": false,
// Misc
"typescript.tsdk": "node_modules/typescript/lib",

View file

@ -7,7 +7,9 @@
"build": "rbxtsc",
"watch": "rbxtsc -w",
"lint": "eslint src",
"fmt": "prettier .",
"check_fmt": "prettier -c src/",
"check": "pnpm lint && pnpm check_fmt",
"fmt": "prettier -w src/",
"postinstall": "patch -f node_modules/@rbxts/types/include/roblox.d.ts < lune_require_patch.diff",
"prepublishOnly": "pnpm run build"
},

View file

@ -3,7 +3,7 @@ const { slice } = require<{
}>("util");
function stringToBytes(str: string) {
let result = [];
const result = [];
for (let i = 0; i < str.size(); i++) {
result.push(string.byte(str, i + 1)[0]);
@ -14,18 +14,14 @@ function stringToBytes(str: string) {
// Adapted from https://github.com/un-ts/ab64/blob/main/src/ponyfill.ts#L24
const _atob = (asc: string) => {
const b64CharList =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
const b64CharList = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
const b64Chars = string.split(b64CharList, "");
const b64Table = b64Chars.reduce<Record<string, number>>(
(acc, char, index) => {
const b64Table = b64Chars.reduce<Record<string, number>>((acc, char, index) => {
acc[char] = index;
return acc;
},
{}
);
}, {});
const fromCharCode = string.char;
@ -57,12 +53,11 @@ const _atob = (asc: string) => {
// Adapted from https://gist.github.com/jonleighton/958841
export function atob(buf: number[]): string {
let base64 = "";
let encodings =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const encodings = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let byteLength = buf.size();
let byteRemainder = byteLength % 3;
let mainLength = byteLength - byteRemainder;
const byteLength = buf.size();
const byteRemainder = byteLength % 3;
const mainLength = byteLength - byteRemainder;
let a: number, b: number, c: number, d: number;
let chunk;
@ -73,10 +68,10 @@ export function atob(buf: number[]): string {
chunk = (buf[i] << 16) | (buf[i + 1] << 8) | buf[i + 2];
// Use bitmasks to extract 6-bit segments from the triplet
a = (chunk & 16515072) >> 18; // 16515072 = (2^6 - 1) << 18
b = (chunk & 258048) >> 12; // 258048 = (2^6 - 1) << 12
c = (chunk & 4032) >> 6; // 4032 = (2^6 - 1) << 6
d = chunk & 63; // 63 = 2^6 - 1
a = (chunk & 16515072) >> 18;
b = (chunk & 258048) >> 12;
c = (chunk & 4032) >> 6;
d = chunk & 63;
// Convert the raw binary segments to the appropriate ASCII encoding
base64 +=
@ -90,21 +85,20 @@ export function atob(buf: number[]): string {
if (byteRemainder === 1) {
chunk = buf[mainLength];
a = (chunk & 252) >> 2; // 252 = (2^6 - 1) << 2
a = (chunk & 252) >> 2;
// Set the 4 least significant bits to zero
b = (chunk & 3) << 4; // 3 = 2^2 - 1
b = (chunk & 3) << 4;
base64 +=
string.byte(encodings, a)[0] + string.byte(encodings, b)[0] + "==";
base64 += string.byte(encodings, a)[0] + string.byte(encodings, b)[0] + "==";
} else if (byteRemainder === 2) {
chunk = (buf[mainLength] << 8) | buf[mainLength + 1];
a = (chunk & 64512) >> 10; // 64512 = (2^6 - 1) << 10
b = (chunk & 1008) >> 4; // 1008 = (2^6 - 1) << 4
a = (chunk & 64512) >> 10;
b = (chunk & 1008) >> 4;
// Set the 2 least significant bits to zero
c = (chunk & 15) << 2; // 15 = 2^4 - 1
c = (chunk & 15) << 2;
base64 +=
string.char(string.byte(encodings, a + 1)[0]) +

127
src/wg.ts
View file

@ -11,8 +11,8 @@ function gf(init?: number[]): number[] {
}
function pack(o: number[], n: number[]) {
let b: number,
m = gf(),
let b: number;
const m = gf(),
t = gf();
for (let i = 0; i < 16; ++i) {
t[i] = n[i];
@ -46,8 +46,8 @@ function carry(o: number[]) {
}
function cswap(p: number[], q: number[], b: number) {
let t: number,
c = ~(b - 1);
let t: number;
const c = ~(b - 1);
for (let i = 0; i < 16; ++i) {
t = c & (p[i] ^ q[i]);
p[i] ^= t;
@ -100,126 +100,15 @@ function invert(o: number[], i: number[]) {
}
}
let Base64 = {
_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
encode: function (e: string) {
let t = "";
let n: number, r: number, i: number, s, o, u, a;
let f = 0;
e = Base64._utf8_encode(e);
while (f < e.size()) {
n = string.byte(e, f++)[0];
r = string.byte(e, f++)[0];
i = string.byte(e, f++)[0];
s = n >> 2;
o = ((n & 3) << 4) | (r >> 4);
u = ((r & 15) << 2) | (i >> 6);
a = i & 63;
if (r !== r) {
u = a = 64;
} else if (i !== i) {
a = 64;
}
t =
t +
string.byte(this._keyStr, s)[0] +
string.byte(this._keyStr, o)[0] +
string.byte(this._keyStr, u)[0] +
string.byte(this._keyStr, a)[0];
}
return t;
},
decode: function (e: string) {
let t = "";
let n, r, i;
let s: number, o: number, u: number, a: number;
let f = 0;
e = string.gsub(e, "[^A-Za-z0-9%+%/%=]", "")[0];
while (f < e.size()) {
s = string.find(
this._keyStr,
string.char(string.byte(e, f++)[0])
)[0] as number;
o = string.find(
this._keyStr,
string.char(string.byte(e, f++)[0])
)[0] as number;
u = string.find(
this._keyStr,
string.char(string.byte(e, f++)[0])
)[0] as number;
a = string.find(
this._keyStr,
string.char(string.byte(e, f++)[0])
)[0] as number;
n = (s << 2) | (o >> 4);
r = ((o & 15) << 4) | (u >> 2);
i = ((u & 3) << 6) | a;
t = t + string.char(n);
if (u !== 64) {
t = t + string.char(r);
}
if (a !== 64) {
t = t + string.char(i);
}
}
t = Base64._utf8_decode(t);
return t;
},
_utf8_encode: function (e: string) {
e = string.gsub(e, "\r\n", "\n")[0];
let t = "";
for (let n = 0; n < e.size(); n++) {
let r = string.byte(e, n)[0];
if (r < 128) {
t += string.char(r);
} else if (r > 127 && r < 2048) {
t += string.char((r >> 6) | 192);
t += string.char((r & 63) | 128);
} else {
t += string.char((r >> 12) | 224);
t += string.char(((r >> 6) & 63) | 128);
t += string.char((r & 63) | 128);
}
}
return t;
},
_utf8_decode: function (e: string) {
let t = "";
let n = 0;
let r = 0;
let c1 = 0;
let c2 = 0;
let c3 = 0;
while (n < e.size()) {
r = string.byte(e, n)[0];
if (r < 128) {
t += string.char(r);
n++;
} else if (r > 191 && r < 224) {
c2 = string.byte(e, n + 1)[0];
t += string.char(((r & 31) << 6) | (c2 & 63));
n += 2;
} else {
c2 = string.byte(e, n + 1)[0];
c3 = string.byte(e, n + 2)[0];
t += string.char(((r & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
n += 3;
}
}
return t;
},
};
function clamp(z: number[]) {
z[31] = (z[31] & 127) | 64;
z[0] &= 248;
}
export function generatePublicKey(privateKey: number[]): number[] {
let r: number,
z = new Array<number>(32, 0);
let a = gf([1]),
let r: number;
const z = new Array<number>(32, 0),
a = gf([1]),
b = gf([9]),
c = gf(),
d = gf([1]),
@ -268,7 +157,7 @@ export function generatePublicKey(privateKey: number[]): number[] {
}
function generatePresharedKey(): number[] {
let privateKey = new Array(32, 0).map(() => {
const privateKey = new Array(32, 0).map(() => {
return math.floor(math.random() * 256);
});