Skip to content

Commit

Permalink
refactor: remove commonjs, using esm instead
Browse files Browse the repository at this point in the history
  • Loading branch information
JadlionHD committed Jun 9, 2024
1 parent 5b2cc56 commit 092d61e
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 53 deletions.
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ test/
lib/build/**
build/**
docs/
pnpm-lock.yaml
pnpm-lock.yaml
src/*
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "growtopia.js",
"version": "1.4.0",
"version": "1.4.1",
"description": "A package to create a growtopia private servers.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"type": "module",
"scripts": {
"test": "npm run build && npm run build:ts",
"dev": "rimraf dist && npm run build && npm run build:ts && node test/basic-example/index.js",
Expand Down
20 changes: 10 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export type * from "../types/index";
export type * from "../types/index.d";

export * from "./structures/Client";
export * from "./structures/Peer";
export * from "./structures/ItemsDat";
export * from "./structures/GrowApi";
export * from "./util/Constants";
export * from "./util/Utils";
export * from "./structures/Client.js";
export * from "./structures/Peer.js";
export * from "./structures/ItemsDat.js";
export * from "./structures/GrowApi.js";
export * from "./util/Constants.js";
export * from "./util/Utils.js";

export * from "./packets/TankPacket";
export * from "./packets/TextPacket";
export * from "./packets/Variant";
export * from "./packets/TankPacket.js";
export * from "./packets/TextPacket.js";
export * from "./packets/Variant.js";
2 changes: 1 addition & 1 deletion src/packets/TankPacket.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tank } from "../../types/packets";
import type { Tank } from "../../types/packets";

const TANK_HEADER_SIZE = 60;

Expand Down
9 changes: 6 additions & 3 deletions src/packets/Variant.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { TankPacket } from "./TankPacket";
import { TankPacket } from "./TankPacket.js";

// Types
import { VariantArg, VariantArray, VariantOptions } from "../../types/packets";
import { VariantTypes } from "../util/Constants";
import type { VariantArg, VariantArray, VariantOptions } from "../../types/packets";
import Constants from "../util/Constants.js";

/**
* Represents the Variant class.
Expand Down Expand Up @@ -33,6 +33,7 @@ class Variant {

public static toArray(data: Buffer): VariantArray[] {
let arr: VariantArray[] = [];
const VariantTypes = Constants.VariantTypes;

let pos = 60;
const count = data.readUint8(60);
Expand Down Expand Up @@ -109,6 +110,8 @@ class Variant {
* Parses the data of the Variant and returns a TankPacket from it.
*/
public parse() {
const VariantTypes = Constants.VariantTypes;

let buf = [this.args.length];

this.args.forEach((arg) => {
Expand Down
23 changes: 13 additions & 10 deletions src/structures/Client.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import EventEmitter from "eventemitter3";
import { Caching, ClientOptions, ClientType } from "../../types/client";
import { PacketTypes } from "../util/Constants";
import { parseText } from "../util/Utils";
import { Peer } from "./Peer";
import { ActionEvent, LoginInfo, PeerData } from "../../types";
import { TankPacket } from "../packets/TankPacket";
import { WebServer } from "./WebServer";
const Native = require("../../build/Release/gtjs.node").Client;
import type { Caching, ClientOptions, ClientType } from "../../types/client";
import Constants from "../util/Constants.js";
import Utils from "../util/Utils.js";
import { Peer } from "./Peer.js";
import type { ActionEvent, LoginInfo, PeerData } from "../../types";
import { TankPacket } from "../packets/TankPacket.js";
import { WebServer } from "./WebServer.js";
import { createRequire } from "node:module";
const Native = createRequire(import.meta.url)("../../build/Release/gtjs.node").Client;

class Client extends EventEmitter {
public _client: ClientType;
Expand Down Expand Up @@ -127,16 +128,18 @@ class Client extends EventEmitter {
const type = data.readInt32LE();
const peer = new Peer(this, netID);

const PacketTypes = Constants.PacketTypes;

switch (type) {
case PacketTypes.STR: {
const parsed = parseText(data);
const parsed = Utils.parseText(data);

this.emit("text", peer, parsed);
break;
}

case PacketTypes.ACTION: {
const parsed = parseText(data);
const parsed = Utils.parseText(data);
this.emit("action", peer, parsed);
break;
}
Expand Down
4 changes: 2 additions & 2 deletions src/structures/GrowApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Caching } from "../../types";
import { Client } from "./Client";
import type { Caching } from "../../types";
import { Client } from "./Client.js";

class GrowApi {
public client?: Client;
Expand Down
2 changes: 1 addition & 1 deletion src/structures/ItemsDat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExtendString, ItemDefinition, ItemsDatMeta, StringOptions } from "../../types";
import type { ExtendString, ItemDefinition, ItemsDatMeta, StringOptions } from "../../types";

class ItemsDat {
private mempos = 0;
Expand Down
8 changes: 4 additions & 4 deletions src/structures/Peer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ClientType, PeerData } from "../../types";
import { Sendable } from "../../types/packets";
import { Variant } from "../packets/Variant";
import { Client } from "./Client";
import type { ClientType, PeerData } from "../../types";
import type { Sendable } from "../../types/packets";
import { Variant } from "../packets/Variant.js";
import { Client } from "./Client.js";

class Peer<T extends PeerData> {
public data: T;
Expand Down
3 changes: 3 additions & 0 deletions src/structures/WebServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { readFileSync } from "fs";
import http from "http";
import https from "https";
import path from "path";
import { fileURLToPath } from "url";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const options = {
key: readFileSync(path.join(__dirname, "..", "..", "misc", "ssl", "server.key")),
Expand Down
32 changes: 22 additions & 10 deletions src/util/Constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Types for each Argument.
*/
export enum VariantTypes {
enum VariantTypes {
NONE,
FLOAT_1,
STRING,
Expand All @@ -14,7 +14,7 @@ export enum VariantTypes {
/**
* Growtopia Packet Types
*/
export enum PacketTypes {
enum PacketTypes {
UNK,
HELLO,
STR,
Expand All @@ -29,7 +29,7 @@ export enum PacketTypes {
/**
* Growtopia Tank Packet Types
*/
export enum TankTypes {
enum TankTypes {
STATE = 0,
CALL_FUNCTION,
UPDATE_STATUS,
Expand Down Expand Up @@ -72,7 +72,7 @@ export enum TankTypes {
SEND_PLAYER_TRIBUTE_DATA
}

export enum TileFlags {
enum TileFlags {
FLAGS_TILEEXTRA = 0x0001,
FLAGS_LOCKED = 0x0002,
FLAGS_SEED = 0x0010,
Expand All @@ -90,14 +90,14 @@ export enum TileFlags {
FLAGS_GLUE = 0x08000000
}

export enum TileOptionsFlags {
enum TileOptionsFlags {
MUSIC_BLOCKS_DISABLED = 0x0010,
MUSIC_BLOCKS_INVIS = 0x0020,
LOCK_ALLOW_BUILD_ONLY = 0x0040,
LOCK_ENABLE_RAINBOW = 0x0080,
LOCK_ENABLE_RAINBOW = 0x0080
}

export enum TilePropertiesFlags {
enum TilePropertiesFlags {
MULTI_FACING = 1 << 0,
WRENCHABLE = 1 << 1,
PERMANENT = 1 << 2,
Expand All @@ -108,7 +108,7 @@ export enum TilePropertiesFlags {
WORLD_LOCK = 1 << 7
}

export enum TileCategoryFlags {
enum TileCategoryFlags {
BETA = 1 << 0,
AUTO_PICKUP = 1 << 1,
MOD = 1 << 2,
Expand All @@ -119,7 +119,7 @@ export enum TileCategoryFlags {
UNTRADEABLE = 1 << 7
}

export enum TileActionTypes {
enum TileActionTypes {
FIST = 0,
WRENCH = 1,
DOOR = 2,
Expand Down Expand Up @@ -261,7 +261,7 @@ export enum TileActionTypes {
FRIENDS_ENTRANCE = 142
}

export enum TileExtraTypes {
enum TileExtraTypes {
NONE,
DOOR,
SIGN,
Expand Down Expand Up @@ -321,3 +321,15 @@ export enum TileExtraTypes {
KRANKEN_GALACTIC = 0x50,
WEATHER_INFINITY = 0x4d
}

export default {
VariantTypes,
PacketTypes,
TankTypes,
TileActionTypes,
TileCategoryFlags,
TileExtraTypes,
TileFlags,
TileOptionsFlags,
TilePropertiesFlags
};
8 changes: 4 additions & 4 deletions src/util/Utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { DataObject } from "../../types";

export function parseText(chunk: Buffer) {
let data: DataObject = {};
function parseText(chunk: Buffer) {
let data: Record<string, string | number> = {};
chunk[chunk.length - 1] = 0;

let str = chunk.toString("utf-8", 4);
Expand All @@ -22,3 +20,5 @@ export function parseText(chunk: Buffer) {

return data;
}

export default { parseText };
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */

/* Modules */
"module": "CommonJS" /* Specify what module code is generated. */,
"module": "ES2022" /* Specify what module code is generated. */,
// "rootDir": "./", /* Specify the root folder within your source files. */
// "moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
"moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
Expand Down
4 changes: 0 additions & 4 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
export interface DataObject {
[key: string]: string | number;
}

export interface PeerData {
netID: number;
}
Expand Down

0 comments on commit 092d61e

Please sign in to comment.