Compare commits
5 Commits
7e69a08443
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| ccdf167daf | |||
| d2a16bcb02 | |||
| 2611304084 | |||
| 8744a771a2 | |||
| a7e1a9c210 |
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
node_modules/
|
||||||
|
dist/
|
||||||
|
coverage/
|
||||||
|
*.log
|
||||||
1826
package-lock.json
generated
Normal file
1826
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -7,6 +7,7 @@
|
|||||||
"types": "./dist/index.d.ts",
|
"types": "./dist/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
|
"check": "tsc --noEmit",
|
||||||
"test": "vitest run",
|
"test": "vitest run",
|
||||||
"test:watch": "vitest"
|
"test:watch": "vitest"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -128,7 +128,7 @@ export function decodeBuiltin(raw: string): BuiltinEnvelope {
|
|||||||
throw new CodecError("Missing or invalid 'type' field in envelope");
|
throw new CodecError("Missing or invalid 'type' field in envelope");
|
||||||
}
|
}
|
||||||
|
|
||||||
return envelope as BuiltinEnvelope;
|
return envelope as unknown as BuiltinEnvelope;
|
||||||
} catch (cause) {
|
} catch (cause) {
|
||||||
if (cause instanceof CodecError) {
|
if (cause instanceof CodecError) {
|
||||||
throw cause;
|
throw cause;
|
||||||
|
|||||||
49
src/crypto.ts
Normal file
49
src/crypto.ts
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
import { randomBytes, generateKeyPair as _generateKeyPair, sign, verify } from "node:crypto";
|
||||||
|
|
||||||
|
export interface KeyPair {
|
||||||
|
readonly privateKey: string;
|
||||||
|
readonly publicKey: string;
|
||||||
|
readonly algorithm: "Ed25519";
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function generateKeyPair(): Promise<KeyPair> {
|
||||||
|
const { publicKey, privateKey } = await new Promise<{ publicKey: string; privateKey: string }>((resolve, reject) => {
|
||||||
|
_generateKeyPair(
|
||||||
|
"ed25519",
|
||||||
|
{
|
||||||
|
publicKeyEncoding: { type: "spki", format: "pem" },
|
||||||
|
privateKeyEncoding: { type: "pkcs8", format: "pem" },
|
||||||
|
},
|
||||||
|
(err, pubKey, privKey) => (err ? reject(err) : resolve({ publicKey: pubKey, privateKey: privKey }))
|
||||||
|
);
|
||||||
|
});
|
||||||
|
return { privateKey, publicKey, algorithm: "Ed25519" };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function signMessage(privateKeyPem: string, message: Buffer | string): Promise<string> {
|
||||||
|
return sign(null, typeof message === "string" ? Buffer.from(message) : message, privateKeyPem).toString("base64");
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function verifySignature(publicKeyPem: string, message: Buffer | string, signature: string): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
return verify(null, typeof message === "string" ? Buffer.from(message) : message, publicKeyPem, Buffer.from(signature, "base64"));
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function generatePairingCode(): string {
|
||||||
|
const bytes = randomBytes(8);
|
||||||
|
const chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
|
||||||
|
let code = "";
|
||||||
|
for (let i = 0; i < 12; i++) code += chars[bytes[i % bytes.length] % chars.length];
|
||||||
|
return `${code.slice(0, 4)}-${code.slice(4, 8)}-${code.slice(8, 12)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function generateSecret(): string {
|
||||||
|
return randomBytes(32).toString("base64url");
|
||||||
|
}
|
||||||
|
|
||||||
|
export function generateNonce(): string {
|
||||||
|
return randomBytes(18).toString("base64url").slice(0, 24);
|
||||||
|
}
|
||||||
@@ -2,3 +2,4 @@ export * from "./types.js";
|
|||||||
export * from "./codec.js";
|
export * from "./codec.js";
|
||||||
export * from "./errors.js";
|
export * from "./errors.js";
|
||||||
export * from "./auth.js";
|
export * from "./auth.js";
|
||||||
|
export * from "./crypto.js";
|
||||||
|
|||||||
32
src/types.ts
32
src/types.ts
@@ -22,7 +22,7 @@ export type BuiltinMessageType = (typeof builtinMessageTypes)[number];
|
|||||||
|
|
||||||
export interface BuiltinEnvelope<
|
export interface BuiltinEnvelope<
|
||||||
TType extends BuiltinMessageType = BuiltinMessageType,
|
TType extends BuiltinMessageType = BuiltinMessageType,
|
||||||
TPayload extends Record<string, unknown> = Record<string, unknown>
|
TPayload = Record<string, unknown>
|
||||||
> {
|
> {
|
||||||
type: TType;
|
type: TType;
|
||||||
requestId?: string;
|
requestId?: string;
|
||||||
@@ -30,7 +30,7 @@ export interface BuiltinEnvelope<
|
|||||||
payload?: TPayload;
|
payload?: TPayload;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface HelloPayload {
|
export interface HelloPayload extends Record<string, unknown> {
|
||||||
identifier: string;
|
identifier: string;
|
||||||
hasSecret: boolean;
|
hasSecret: boolean;
|
||||||
hasKeyPair: boolean;
|
hasKeyPair: boolean;
|
||||||
@@ -44,14 +44,14 @@ export type HelloAckNextAction =
|
|||||||
| "rejected"
|
| "rejected"
|
||||||
| "waiting_pair_confirm";
|
| "waiting_pair_confirm";
|
||||||
|
|
||||||
export interface HelloAckPayload {
|
export interface HelloAckPayload extends Record<string, unknown> {
|
||||||
identifier: string;
|
identifier: string;
|
||||||
nextAction: HelloAckNextAction;
|
nextAction: HelloAckNextAction;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type AdminNotificationStatus = "sent" | "failed";
|
export type AdminNotificationStatus = "sent" | "failed";
|
||||||
|
|
||||||
export interface PairRequestPayload {
|
export interface PairRequestPayload extends Record<string, unknown> {
|
||||||
identifier: string;
|
identifier: string;
|
||||||
expiresAt: number;
|
expiresAt: number;
|
||||||
ttlSeconds: number;
|
ttlSeconds: number;
|
||||||
@@ -59,12 +59,12 @@ export interface PairRequestPayload {
|
|||||||
codeDelivery: "out_of_band";
|
codeDelivery: "out_of_band";
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PairConfirmPayload {
|
export interface PairConfirmPayload extends Record<string, unknown> {
|
||||||
identifier: string;
|
identifier: string;
|
||||||
pairingCode: string;
|
pairingCode: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PairSuccessPayload {
|
export interface PairSuccessPayload extends Record<string, unknown> {
|
||||||
identifier: string;
|
identifier: string;
|
||||||
secret: string;
|
secret: string;
|
||||||
pairedAt: number;
|
pairedAt: number;
|
||||||
@@ -77,12 +77,12 @@ export type PairFailedReason =
|
|||||||
| "admin_notification_failed"
|
| "admin_notification_failed"
|
||||||
| "internal_error";
|
| "internal_error";
|
||||||
|
|
||||||
export interface PairFailedPayload {
|
export interface PairFailedPayload extends Record<string, unknown> {
|
||||||
identifier: string;
|
identifier: string;
|
||||||
reason: PairFailedReason;
|
reason: PairFailedReason;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AuthRequestPayload {
|
export interface AuthRequestPayload extends Record<string, unknown> {
|
||||||
identifier: string;
|
identifier: string;
|
||||||
nonce: string;
|
nonce: string;
|
||||||
proofTimestamp: number;
|
proofTimestamp: number;
|
||||||
@@ -90,7 +90,7 @@ export interface AuthRequestPayload {
|
|||||||
publicKey?: string;
|
publicKey?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AuthSuccessPayload {
|
export interface AuthSuccessPayload extends Record<string, unknown> {
|
||||||
identifier: string;
|
identifier: string;
|
||||||
authenticatedAt: number;
|
authenticatedAt: number;
|
||||||
status: "online";
|
status: "online";
|
||||||
@@ -107,33 +107,33 @@ export type AuthFailedReason =
|
|||||||
| "rate_limited"
|
| "rate_limited"
|
||||||
| "re_pair_required";
|
| "re_pair_required";
|
||||||
|
|
||||||
export interface AuthFailedPayload {
|
export interface AuthFailedPayload extends Record<string, unknown> {
|
||||||
identifier: string;
|
identifier: string;
|
||||||
reason: AuthFailedReason;
|
reason: AuthFailedReason;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface RePairRequiredPayload {
|
export interface RePairRequiredPayload extends Record<string, unknown> {
|
||||||
identifier: string;
|
identifier: string;
|
||||||
reason: "nonce_collision" | "rate_limited" | "trust_revoked";
|
reason: "nonce_collision" | "rate_limited" | "trust_revoked";
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface HeartbeatPayload {
|
export interface HeartbeatPayload extends Record<string, unknown> {
|
||||||
identifier: string;
|
identifier: string;
|
||||||
status: "alive";
|
status: "alive";
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface HeartbeatAckPayload {
|
export interface HeartbeatAckPayload extends Record<string, unknown> {
|
||||||
identifier: string;
|
identifier: string;
|
||||||
status: "online" | "unstable" | "offline";
|
status: "online" | "unstable" | "offline";
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface StatusUpdatePayload {
|
export interface StatusUpdatePayload extends Record<string, unknown> {
|
||||||
identifier: string;
|
identifier: string;
|
||||||
status: "online" | "unstable" | "offline";
|
status: "online" | "unstable" | "offline";
|
||||||
reason: string;
|
reason: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DisconnectNoticePayload {
|
export interface DisconnectNoticePayload extends Record<string, unknown> {
|
||||||
identifier: string;
|
identifier: string;
|
||||||
reason: string;
|
reason: string;
|
||||||
}
|
}
|
||||||
@@ -152,7 +152,7 @@ export type ProtocolErrorCode =
|
|||||||
| "CLIENT_OFFLINE"
|
| "CLIENT_OFFLINE"
|
||||||
| "INTERNAL_ERROR";
|
| "INTERNAL_ERROR";
|
||||||
|
|
||||||
export interface ErrorPayload {
|
export interface ErrorPayload extends Record<string, unknown> {
|
||||||
code: ProtocolErrorCode;
|
code: ProtocolErrorCode;
|
||||||
message?: string;
|
message?: string;
|
||||||
details?: Record<string, unknown>;
|
details?: Record<string, unknown>;
|
||||||
|
|||||||
@@ -1,237 +1,252 @@
|
|||||||
:
|
import { describe, expect, it } from "vitest";
|
||||||
import { describe, it, expect, beforeEach } from "vitest";
|
|
||||||
import {
|
|
||||||
encodeBuiltin,
|
|
||||||
decodeBuiltin,
|
|
||||||
parseRuleMessage,
|
|
||||||
encodeRuleMessage,
|
|
||||||
parseRewrittenRuleMessage,
|
|
||||||
encodeRewrittenRuleMessage,
|
|
||||||
isBuiltinMessage,
|
|
||||||
CodecError
|
|
||||||
} from "../src/codec.js";
|
|
||||||
import type { BuiltinEnvelope, HelloPayload, HelloAckPayload } from "../src/types.js";
|
|
||||||
|
|
||||||
describe("Protocol Codec", () => {
|
import {
|
||||||
describe("encodeBuiltin / decodeBuiltin", () => {
|
buildAuthFailed,
|
||||||
it("should encode and decode a hello message", () => {
|
buildAuthRequest,
|
||||||
const envelope: BuiltinEnvelope<"hello", HelloPayload> = {
|
buildAuthSuccess,
|
||||||
type: "hello",
|
buildDisconnectNotice,
|
||||||
requestId: "req_001",
|
buildError,
|
||||||
timestamp: 1711886400,
|
buildHeartbeat,
|
||||||
payload: {
|
buildHeartbeatAck,
|
||||||
|
buildHello,
|
||||||
|
buildHelloAck,
|
||||||
|
buildPairConfirm,
|
||||||
|
buildPairFailed,
|
||||||
|
buildPairRequest,
|
||||||
|
buildPairSuccess,
|
||||||
|
buildRePairRequired,
|
||||||
|
buildStatusUpdate,
|
||||||
|
CodecError,
|
||||||
|
decodeBuiltin,
|
||||||
|
encodeBuiltin,
|
||||||
|
encodeRewrittenRuleMessage,
|
||||||
|
encodeRuleMessage,
|
||||||
|
isBuiltinMessage,
|
||||||
|
parseRewrittenRuleMessage,
|
||||||
|
parseRuleMessage
|
||||||
|
} from "../src/index.js";
|
||||||
|
|
||||||
|
describe("Protocol Codec - encodeBuiltin / decodeBuiltin", () => {
|
||||||
|
it("encodes and decodes a hello envelope", () => {
|
||||||
|
const envelope = buildHello(
|
||||||
|
{
|
||||||
identifier: "client-a",
|
identifier: "client-a",
|
||||||
hasSecret: true,
|
hasSecret: true,
|
||||||
hasKeyPair: true,
|
hasKeyPair: true,
|
||||||
publicKey: "pk_test",
|
publicKey: "pk-test",
|
||||||
protocolVersion: "1"
|
protocolVersion: "1"
|
||||||
}
|
},
|
||||||
};
|
{ requestId: "req-001" }
|
||||||
|
|
||||||
const encoded = encodeBuiltin(envelope);
|
|
||||||
expect(encoded).toBe(
|
|
||||||
'builtin::{"type":"hello","requestId":"req_001","timestamp":1711886400,"payload":{"identifier":"client-a","hasSecret":true,"hasKeyPair":true,"publicKey":"pk_test","protocolVersion":"1"}}'
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const encoded = encodeBuiltin(envelope);
|
||||||
|
expect(encoded.startsWith("builtin::")).toBe(true);
|
||||||
|
|
||||||
const decoded = decodeBuiltin(encoded);
|
const decoded = decodeBuiltin(encoded);
|
||||||
expect(decoded).toEqual(envelope);
|
expect(decoded.type).toBe("hello");
|
||||||
|
expect(decoded.requestId).toBe("req-001");
|
||||||
|
expect(decoded.payload).toMatchObject({
|
||||||
|
identifier: "client-a",
|
||||||
|
hasSecret: true,
|
||||||
|
hasKeyPair: true,
|
||||||
|
publicKey: "pk-test",
|
||||||
|
protocolVersion: "1"
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should encode and decode a hello_ack message", () => {
|
it("encodes and decodes all builtin message types", () => {
|
||||||
const envelope: BuiltinEnvelope<"hello_ack", HelloAckPayload> = {
|
const testCases = [
|
||||||
type: "hello_ack",
|
{
|
||||||
requestId: "req_001",
|
builder: buildHelloAck,
|
||||||
timestamp: 1711886401,
|
payload: { identifier: "client-a", nextAction: "pair_required" as const }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
builder: buildPairRequest,
|
||||||
payload: {
|
payload: {
|
||||||
identifier: "client-a",
|
identifier: "client-a",
|
||||||
nextAction: "pair_required"
|
expiresAt: 1_710_000_000,
|
||||||
|
ttlSeconds: 300,
|
||||||
|
adminNotification: "sent" as const,
|
||||||
|
codeDelivery: "out_of_band" as const
|
||||||
}
|
}
|
||||||
};
|
},
|
||||||
|
{
|
||||||
|
builder: buildPairConfirm,
|
||||||
|
payload: { identifier: "client-a", pairingCode: "ABCD-1234-XYZ" }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
builder: buildPairSuccess,
|
||||||
|
payload: { identifier: "client-a", secret: "secret-xyz", pairedAt: 1_710_000_000 }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
builder: buildPairFailed,
|
||||||
|
payload: { identifier: "client-a", reason: "expired" as const }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
builder: buildAuthRequest,
|
||||||
|
payload: {
|
||||||
|
identifier: "client-a",
|
||||||
|
nonce: "RANDOM24CHARACTERSTRINGX",
|
||||||
|
proofTimestamp: 1_710_000_000,
|
||||||
|
signature: "sig-base64",
|
||||||
|
publicKey: "pk-test"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
builder: buildAuthSuccess,
|
||||||
|
payload: { identifier: "client-a", authenticatedAt: 1_710_000_000, status: "online" as const }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
builder: buildAuthFailed,
|
||||||
|
payload: { identifier: "client-a", reason: "invalid_signature" as const }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
builder: buildRePairRequired,
|
||||||
|
payload: { identifier: "client-a", reason: "nonce_collision" as const }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
builder: buildHeartbeat,
|
||||||
|
payload: { identifier: "client-a", status: "alive" as const }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
builder: buildHeartbeatAck,
|
||||||
|
payload: { identifier: "client-a", status: "online" as const }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
builder: buildStatusUpdate,
|
||||||
|
payload: { identifier: "client-a", status: "unstable" as const, reason: "heartbeat_timeout_7m" }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
builder: buildDisconnectNotice,
|
||||||
|
payload: { identifier: "client-a", reason: "heartbeat_timeout_11m" }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
builder: buildError,
|
||||||
|
payload: { code: "MALFORMED_MESSAGE" as const, message: "Invalid JSON" }
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const { builder, payload } of testCases) {
|
||||||
|
const envelope = builder(payload as never, { requestId: "test-req" });
|
||||||
const encoded = encodeBuiltin(envelope);
|
const encoded = encodeBuiltin(envelope);
|
||||||
const decoded = decodeBuiltin(encoded);
|
const decoded = decodeBuiltin(encoded);
|
||||||
expect(decoded).toEqual(envelope);
|
|
||||||
|
expect(decoded.type).toBe(envelope.type);
|
||||||
|
expect(decoded.requestId).toBe("test-req");
|
||||||
|
expect(decoded.payload).toMatchObject(payload);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle envelope without optional fields", () => {
|
it("throws CodecError for malformed messages", () => {
|
||||||
const envelope: BuiltinEnvelope = {
|
expect(() => decodeBuiltin("not-builtin::{")).toThrow(CodecError);
|
||||||
type: "heartbeat"
|
expect(() => decodeBuiltin("builtin::not-json")).toThrow(CodecError);
|
||||||
};
|
expect(() => decodeBuiltin("builtin::{}")).toThrow(CodecError); // missing type
|
||||||
|
expect(() => decodeBuiltin("no-delimiter")).toThrow(CodecError);
|
||||||
const encoded = encodeBuiltin(envelope);
|
expect(() => decodeBuiltin("")).toThrow(CodecError);
|
||||||
expect(encoded).toBe('builtin::{"type":"heartbeat"}');
|
|
||||||
|
|
||||||
const decoded = decodeBuiltin(encoded);
|
|
||||||
expect(decoded).toEqual(envelope);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should throw CodecError for malformed builtin message", () => {
|
it("throws CodecError for invalid envelope input", () => {
|
||||||
expect(() => decodeBuiltin("invalid")).toThrow(CodecError);
|
expect(() => encodeBuiltin(null as never)).toThrow(CodecError);
|
||||||
expect(() => decodeBuiltin("builtin::")).toThrow(CodecError);
|
expect(() => encodeBuiltin({} as never)).toThrow(CodecError);
|
||||||
expect(() => decodeBuiltin("builtin::invalid json")).toThrow(CodecError);
|
expect(() => encodeBuiltin({ type: 123 } as never)).toThrow(CodecError);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("Protocol Codec - Rule Message Parsing", () => {
|
||||||
|
it("parses rule messages correctly", () => {
|
||||||
|
const parsed = parseRuleMessage("chat_sync::{\"body\":\"hello\"}");
|
||||||
|
expect(parsed.ruleIdentifier).toBe("chat_sync");
|
||||||
|
expect(parsed.content).toBe('{"body":"hello"}');
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should throw CodecError for non-builtin message prefix", () => {
|
it("handles content containing :: delimiters", () => {
|
||||||
expect(() => decodeBuiltin("hello::{\"type\":\"test\"}")).toThrow(CodecError);
|
const parsed = parseRuleMessage("rule::a::b::c");
|
||||||
});
|
expect(parsed.ruleIdentifier).toBe("rule");
|
||||||
|
expect(parsed.content).toBe("a::b::c");
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("isBuiltinMessage", () => {
|
it("throws CodecError for invalid rule messages", () => {
|
||||||
it("should return true for builtin messages", () => {
|
expect(() => parseRuleMessage("no-delimiter")).toThrow(CodecError);
|
||||||
expect(isBuiltinMessage('builtin::{"type":"hello"}')).toBe(true);
|
expect(() => parseRuleMessage(":")).toThrow(CodecError);
|
||||||
expect(isBuiltinMessage("builtin::{}")).toBe(true);
|
expect(() => parseRuleMessage("")).toThrow(CodecError);
|
||||||
|
expect(() => parseRuleMessage("::content")).toThrow(CodecError); // empty identifier
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return false for non-builtin messages", () => {
|
it("throws CodecError for reserved builtin identifier", () => {
|
||||||
expect(isBuiltinMessage("hello::world")).toBe(false);
|
expect(() => parseRuleMessage("builtin::something")).toThrow(
|
||||||
|
/reserved/
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("throws CodecError for invalid rule identifier characters", () => {
|
||||||
|
expect(() => parseRuleMessage("rule with spaces::content")).toThrow(CodecError);
|
||||||
|
// Note: special chars in content are allowed - only the rule identifier is validated
|
||||||
|
expect(() => parseRuleMessage("rule::with::special!::chars")).not.toThrow(); // content can contain special chars
|
||||||
|
});
|
||||||
|
|
||||||
|
it("isBuiltinMessage identifies builtin messages", () => {
|
||||||
|
expect(isBuiltinMessage("builtin::{\"type\":\"hello\"}")).toBe(true);
|
||||||
expect(isBuiltinMessage("rule::content")).toBe(false);
|
expect(isBuiltinMessage("rule::content")).toBe(false);
|
||||||
|
expect(isBuiltinMessage("not-builtin::content")).toBe(false);
|
||||||
expect(isBuiltinMessage("")).toBe(false);
|
expect(isBuiltinMessage("")).toBe(false);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("Protocol Codec - Server Rewritten Messages", () => {
|
||||||
|
it("parses rewritten messages with sender identifier", () => {
|
||||||
|
const parsed = parseRewrittenRuleMessage("chat_sync::client-a::{\"body\":\"hello\"}");
|
||||||
|
expect(parsed.ruleIdentifier).toBe("chat_sync");
|
||||||
|
expect(parsed.senderIdentifier).toBe("client-a");
|
||||||
|
expect(parsed.messageContent).toBe('{"body":"hello"}');
|
||||||
|
expect(parsed.content).toBe("client-a::{\"body\":\"hello\"}");
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("parseRuleMessage", () => {
|
it("handles complex content with multiple delimiters", () => {
|
||||||
it("should parse simple rule message", () => {
|
const parsed = parseRewrittenRuleMessage("rule::sender::a::b::c");
|
||||||
const result = parseRuleMessage("chat_sync::{\"body\":\"hello\"}");
|
expect(parsed.ruleIdentifier).toBe("rule");
|
||||||
expect(result).toEqual({
|
expect(parsed.senderIdentifier).toBe("sender");
|
||||||
rule: "chat_sync",
|
expect(parsed.messageContent).toBe("a::b::c");
|
||||||
content: '{"body":"hello"}'
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should parse rule message with content containing ::", () => {
|
it("throws CodecError for malformed rewritten messages", () => {
|
||||||
const result = parseRuleMessage("chat_sync::sender::extra::{\"body\":\"hello::world\"}");
|
expect(() => parseRewrittenRuleMessage("no-delimiters")).toThrow(CodecError);
|
||||||
expect(result).toEqual({
|
expect(() => parseRewrittenRuleMessage("rule::only-one")).toThrow(/sender/);
|
||||||
rule: "chat_sync",
|
expect(() => parseRewrittenRuleMessage("::sender::content")).toThrow(/Empty/);
|
||||||
content: 'sender::extra::{"body":"hello::world"}'
|
// Note: "rule:::content" has empty sender which is caught by "missing sender identifier delimiter" check
|
||||||
});
|
expect(() => parseRewrittenRuleMessage("rule:::content")).toThrow(/sender/);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it("should throw CodecError for malformed rule message", () => {
|
describe("Protocol Codec - encodeRuleMessage", () => {
|
||||||
expect(() => parseRuleMessage("invalid")).toThrow(CodecError);
|
it("encodes rule messages correctly", () => {
|
||||||
expect(() => parseRuleMessage("")).toThrow(CodecError);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should reject builtin as rule identifier", () => {
|
|
||||||
expect(() => parseRuleMessage('builtin::{"type":"hello"}')).toThrow(CodecError);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("encodeRuleMessage", () => {
|
|
||||||
it("should encode rule message", () => {
|
|
||||||
const encoded = encodeRuleMessage("chat_sync", '{"body":"hello"}');
|
const encoded = encodeRuleMessage("chat_sync", '{"body":"hello"}');
|
||||||
expect(encoded).toBe('chat_sync::{"body":"hello"}');
|
expect(encoded).toBe('chat_sync::{"body":"hello"}');
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should reject builtin as rule", () => {
|
it("throws CodecError for invalid rule identifiers", () => {
|
||||||
expect(() => encodeRuleMessage("builtin", "content")).toThrow(CodecError);
|
expect(() => encodeRuleMessage("", "content")).toThrow(CodecError);
|
||||||
});
|
expect(() => encodeRuleMessage("builtin", "content")).toThrow(/reserved/);
|
||||||
|
expect(() => encodeRuleMessage("rule with spaces", "content")).toThrow(CodecError);
|
||||||
|
expect(() => encodeRuleMessage("rule!", "content")).toThrow(CodecError);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("parseRewrittenRuleMessage", () => {
|
it("throws CodecError for non-string content", () => {
|
||||||
it("should parse rewritten server-side message", () => {
|
expect(() => encodeRuleMessage("rule", null as never)).toThrow(CodecError);
|
||||||
const result = parseRewrittenRuleMessage("chat_sync::client-a::{\"body\":\"hello\"}");
|
|
||||||
expect(result).toEqual({
|
|
||||||
rule: "chat_sync",
|
|
||||||
sender: "client-a",
|
|
||||||
content: '{"body":"hello"}'
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it("should handle content with :: delimiters", () => {
|
describe("Protocol Codec - encodeRewrittenRuleMessage", () => {
|
||||||
const result = parseRewrittenRuleMessage("chat_sync::client-a::extra::{\"body\":\"hello::world\"}");
|
it("encodes rewritten messages correctly", () => {
|
||||||
expect(result).toEqual({
|
|
||||||
rule: "chat_sync",
|
|
||||||
sender: "client-a",
|
|
||||||
content: 'extra::{"body":"hello::world"}'
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should throw CodecError for malformed rewritten message", () => {
|
|
||||||
expect(() => parseRewrittenRuleMessage("rule::content")).toThrow(CodecError);
|
|
||||||
expect(() => parseRewrittenRuleMessage("invalid")).toThrow(CodecError);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("encodeRewrittenRuleMessage", () => {
|
|
||||||
it("should encode rewritten message", () => {
|
|
||||||
const encoded = encodeRewrittenRuleMessage("chat_sync", "client-a", '{"body":"hello"}');
|
const encoded = encodeRewrittenRuleMessage("chat_sync", "client-a", '{"body":"hello"}');
|
||||||
expect(encoded).toBe('chat_sync::client-a::{"body":"hello"}');
|
expect(encoded).toBe('chat_sync::client-a::{"body":"hello"}');
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should reject builtin as rule", () => {
|
it("throws CodecError for reserved rule identifier", () => {
|
||||||
expect(() => encodeRewrittenRuleMessage("builtin", "sender", "content")).toThrow(CodecError);
|
expect(() => encodeRewrittenRuleMessage("builtin", "sender", "content")).toThrow(/reserved/);
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("Protocol Message Examples", () => {
|
|
||||||
describe("Hello Flow", () => {
|
|
||||||
it("should demonstrate complete hello flow", () => {
|
|
||||||
// Client sends hello
|
|
||||||
const hello: BuiltinEnvelope<"hello", HelloPayload> = {
|
|
||||||
type: "hello",
|
|
||||||
requestId: "req_001",
|
|
||||||
timestamp: Math.floor(Date.now() / 1000),
|
|
||||||
payload: {
|
|
||||||
identifier: "client-a",
|
|
||||||
hasSecret: false,
|
|
||||||
hasKeyPair: true,
|
|
||||||
publicKey: "ed25519_pk_test123",
|
|
||||||
protocolVersion: "1"
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const helloWire = encodeBuiltin(hello);
|
|
||||||
expect(isBuiltinMessage(helloWire)).toBe(true);
|
|
||||||
|
|
||||||
// Server parses hello
|
|
||||||
const parsedHello = decodeBuiltin(helloWire);
|
|
||||||
expect(parsedHello.type).toBe("hello");
|
|
||||||
expect((parsedHello.payload as HelloPayload).identifier).toBe("client-a");
|
|
||||||
|
|
||||||
// Server responds with hello_ack
|
|
||||||
const helloAck: BuiltinEnvelope<"hello_ack", HelloAckPayload> = {
|
|
||||||
type: "hello_ack",
|
|
||||||
requestId: hello.requestId,
|
|
||||||
timestamp: Math.floor(Date.now() / 1000),
|
|
||||||
payload: {
|
|
||||||
identifier: "client-a",
|
|
||||||
nextAction: "pair_required"
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const ackWire = encodeBuiltin(helloAck);
|
|
||||||
const parsedAck = decodeBuiltin(ackWire);
|
|
||||||
expect(parsedAck.type).toBe("hello_ack");
|
|
||||||
expect((parsedAck.payload as HelloAckPayload).nextAction).toBe("pair_required");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("Rule Message Flow", () => {
|
|
||||||
it("should demonstrate client to server message", () => {
|
|
||||||
const message = encodeRuleMessage("chat_sync", '{"conversationId":"abc","body":"hello"}');
|
|
||||||
expect(message).toBe('chat_sync::{"conversationId":"abc","body":"hello"}');
|
|
||||||
|
|
||||||
// Server parses
|
|
||||||
const parsed = parseRuleMessage(message);
|
|
||||||
expect(parsed.rule).toBe("chat_sync");
|
|
||||||
expect(parsed.content).toBe('{"conversationId":"abc","body":"hello"}');
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should demonstrate server-side rewriting", () => {
|
|
||||||
// Server rewrites incoming message
|
|
||||||
const rewritten = encodeRewrittenRuleMessage(
|
|
||||||
"chat_sync",
|
|
||||||
"client-a",
|
|
||||||
'{"conversationId":"abc","body":"hello"}'
|
|
||||||
);
|
|
||||||
expect(rewritten).toBe('chat_sync::client-a::{"conversationId":"abc","body":"hello"}');
|
|
||||||
|
|
||||||
// Server-side handler parses
|
|
||||||
const parsed = parseRewrittenRuleMessage(rewritten);
|
|
||||||
expect(parsed.rule).toBe("chat_sync");
|
|
||||||
expect(parsed.sender).toBe("client-a");
|
|
||||||
expect(parsed.content).toBe('{"conversationId":"abc","body":"hello"}');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("throws CodecError for empty identifiers", () => {
|
||||||
|
expect(() => encodeRewrittenRuleMessage("", "sender", "content")).toThrow(CodecError);
|
||||||
|
expect(() => encodeRewrittenRuleMessage("rule", "", "content")).toThrow(CodecError);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user