Fully renamed socket to ttp

This commit is contained in:
Alois 2026-04-03 00:38:35 +02:00
commit 76ca664298
13 changed files with 55 additions and 42 deletions

View file

@ -1,8 +1,8 @@
import { z } from "zod";
import { socket } from "@tensamin/shared/data";
import { ttp } from "@tensamin/shared/data";
export type RawMessages = z.infer<
typeof socket.messages_get.response
typeof ttp.messages_get.response
>["messages"];
export type RawMessage = RawMessages[number];

View file

@ -1,5 +1,5 @@
import type { BoundSendFn } from "@tensamin/ttp/core";
import type { Socket } from "@tensamin/shared/data";
import type { TTP } from "@tensamin/shared/data";
import type { RawMessages } from "../values";
/**
@ -11,7 +11,7 @@ import type { RawMessages } from "../values";
* @returns Promise<RawMessages>.
*/
export async function getMessages(
send: BoundSendFn<Socket>,
send: BoundSendFn<TTP>,
amount: number,
offset: number,
user_id: number,

View file

@ -6,7 +6,7 @@ import { Button } from "@tensamin/ui/cmp/button";
import { Plus, Laugh, Clapperboard } from "lucide-react";
import { useChat } from "../context";
import { useSocket } from "@tensamin/ttp/context";
import { useTTP } from "@tensamin/ttp/context";
import { useCrypto } from "@tensamin/crypto/context";
import { log, toast } from "@tensamin/shared/log";
@ -15,7 +15,7 @@ export default function InputComponent() {
const [invertEnterBehavior, setInvertEnterBehavior] = React.useState(false);
const { encrypt } = useCrypto();
const { send } = useSocket();
const { send } = useTTP();
const { addLiveMessage, sharedSecret, userId } = useChat();
const { load } = useStorage();

View file

@ -14,7 +14,7 @@ import type { LiveMessage, RawMessage, RawMessages } from "./values";
import { useCrypto } from "@tensamin/crypto/context";
import { useUser } from "@tensamin/user/context";
import { useStorage } from "@tensamin/storage/context";
import { useSocket } from "@tensamin/ttp/context";
import { useTTP } from "@tensamin/ttp/context";
export const context = createContext<contextType | undefined>(undefined);
@ -29,7 +29,7 @@ export default function Provider(props: { children: ReactNode }) {
const { getSharedSecret } = useCrypto();
const { get } = useUser();
const { load } = useStorage();
const { send, subscribePush } = useSocket();
const { send, subscribePush } = useTTP();
const [liveMessagesState, setLiveMessagesState] = useState<LiveMessage[]>([]);
const [currentSharedSecret, setCurrentSharedSecret] = useState("");

View file

@ -1,8 +1,8 @@
import { z } from "zod";
import { socket } from "@tensamin/shared/data";
import { ttp } from "@tensamin/shared/data";
export type RawMessages = z.infer<
typeof socket.messages_get.response
typeof ttp.messages_get.response
>["messages"];
export type RawMessage = RawMessages[number];

View file

@ -32,10 +32,10 @@ export const failedUser = {
sub_level: 0,
user_id: 0,
username: "unknown",
} as z.infer<typeof socket.get_user_data.response>;
} as z.infer<typeof ttp.get_user_data.response>;
// Socket
export const socket = {
// TTP
export const ttp = {
identification: {
request: z
.object({
@ -162,7 +162,7 @@ export const socket = {
},
} satisfies Record<string, { request: z.ZodType; response: z.ZodType }>;
export type Socket = typeof socket;
export type TTP = typeof ttp;
// Storage
export interface Storage {

View file

@ -11,7 +11,16 @@ import { Ban, Check, Info, TriangleAlert } from "lucide-react";
export function log(
logLevel: number,
logger: string,
color: "red" | "green" | "yellow" | "purple" | "blue",
color:
| "red"
| "green"
| "yellow"
| "purple"
| "blue"
| "cyan"
| "white"
| "black"
| "gray",
...args: unknown[]
) {
const currentLogLevel = Number(localStorage.getItem("log_level") || 0);
@ -24,6 +33,10 @@ export function log(
yellow: "\x1b[33m",
purple: "\x1b[35m",
blue: "\x1b[34m",
cyan: "\x1b[36m",
white: "\x1b[37m",
black: "\x1b[30m",
gray: "\x1b[90m",
};
const resetCode = "\x1b[0m";
console.log(`${colorCodes[color]}${logger}${resetCode}`, ...args);

View file

@ -21,8 +21,8 @@ import {
TRANSPORT_URL,
} from "./values";
import {
socket as schemas,
type Socket as Schemas,
ttp as schemas,
type TTP as Schemas,
} from "@tensamin/shared/data";
import Loading from "@tensamin/ui/screens/loading";
import ErrorScreen from "@tensamin/ui/screens/error";
@ -140,10 +140,10 @@ type ContextType = {
identified: () => boolean;
};
const socketContext = createContext<ContextType | undefined>(undefined);
const TTPContext = createContext<ContextType | undefined>(undefined);
/**
* Provides socket transport state and authenticated send operations to children.
* Provides ttp transport state and authenticated send operations to children.
* @param props Component props with children.
* @returns Loading, error, or provider-wrapped JSX.
*/
@ -186,7 +186,7 @@ export default function Provider(props: {
const client = clientRef.current;
if (!client) {
return Promise.reject(new Error("Socket is not connected"));
return Promise.reject(new Error("ttp is not connected"));
}
if (options?.noResponse) {
@ -240,7 +240,7 @@ export default function Provider(props: {
setIotaPing(remotePing);
}
} catch (intervalError) {
log(1, "Socket", "yellow", "Ping failed", intervalError);
log(1, "ttp", "yellow", "Ping failed", intervalError);
}
}, PING_INTERVAL);
@ -310,7 +310,7 @@ export default function Provider(props: {
setErrorDescription(
"Unable to connect to the server after multiple attempts. Please check your internet connection or try again later.",
);
log(0, "Socket", "red", "Reconnection attempts exhausted", reason);
log(0, "ttp", "red", "Reconnection attempts exhausted", reason);
return;
}
@ -355,7 +355,7 @@ export default function Provider(props: {
return;
}
log(0, "Socket", "red", "Disconnected", closeError, {
log(0, "ttp", "red", "Disconnected", closeError, {
stopSending: isStopSendingError(closeError),
});
scheduleReconnect(closeError);
@ -381,7 +381,7 @@ export default function Provider(props: {
return;
}
log(0, "Socket", "red", "Connection attempt failed", connectError);
log(0, "ttp", "red", "Connection attempt failed", connectError);
scheduleReconnect(connectError);
}
}
@ -490,7 +490,7 @@ export default function Provider(props: {
log(
isFatal ? 0 : 1,
"Socket",
"ttp",
isFatal ? "red" : "yellow",
"Identification handshake failed",
protocolErrorDetails ?? identificationError,
@ -578,20 +578,20 @@ export default function Provider(props: {
}
return (
<socketContext.Provider value={contextValue}>
<TTPContext.Provider value={contextValue}>
{props.children}
</socketContext.Provider>
</TTPContext.Provider>
);
}
/**
* Returns the active socket context and enforces provider usage.
* @returns Socket context API for transport operations and connection state.
* Returns the active ttp context and enforces provider usage.
* @returns ttp context API for transport operations and connection state.
*/
export function useSocket(): ContextType {
const context = useContext(socketContext);
export function useTTP(): ContextType {
const context = useContext(TTPContext);
if (!context) {
throw new Error("useSocket must be used within a SocketProvider");
throw new Error("useTTP must be used within a TTPProvider");
}
return context;
}

View file

@ -199,7 +199,7 @@ export function createTransportClient<T extends SchemaMap>(
*/
const handleIncomingMessage = (message: TypedMessage) => {
if (message.type !== "pong") {
log(2, "ttp", "blue", "Received:", message.type, message.data, {
log(2, "ttp", "cyan", "Received:", message.type, message.data, {
id: message.id,
});
}
@ -563,7 +563,7 @@ export function createTransportClient<T extends SchemaMap>(
}
if (type !== "ping") {
log(2, "ttp", "purple", "Sent:", type, payload, { id: options.id });
log(2, "ttp", "gray", "Sent:", type, payload, { id: options.id });
}
const expectsResponse = !options?.noResponse;

View file

@ -1,7 +1,7 @@
import * as React from "react";
import { useSocket } from "@tensamin/ttp/context";
import { useTTP } from "@tensamin/ttp/context";
import { socket as schemas } from "@tensamin/shared/data";
import { ttp as schemas } from "@tensamin/shared/data";
import type z from "zod";
export type User = z.infer<typeof schemas.get_user_data.response>;
@ -20,7 +20,7 @@ const UserContext = React.createContext<contextValue | undefined>(undefined);
export default function UserProvider(props: { children: React.ReactNode }) {
const storageRef = React.useRef<Record<number, User>>({});
const { send } = useSocket();
const { send } = useTTP();
/**
* Executes get.