Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

H-3617: harpc: Implement TypeScript wire-protocol codec #5686

Merged
merged 8 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libs/@local/harpc/client/typescript/src/types/ErrorCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ export const make = (
}
};

export type EncodeError = Effect.Effect.Error<ReturnType<typeof encode>>;

export const encode = encodeDual(
(buffer: Buffer.WriteBuffer, errorCode: ErrorCode) =>
Buffer.putU16(buffer, errorCode.value),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,15 @@ const ProcedureDescriptorProto: Omit<ProcedureDescriptor, "id"> = {
export const make = (id: ProcedureId.ProcedureId): ProcedureDescriptor =>
createProto(ProcedureDescriptorProto, { id });

export type EncodeError = Effect.Effect.Error<ReturnType<typeof encode>>;

export const encode = encodeDual(
(buffer: Buffer.WriteBuffer, descriptor: ProcedureDescriptor) =>
ProcedureId.encode(buffer, descriptor.id),
);

export type DecodeError = Effect.Effect.Error<ReturnType<typeof decode>>;

export const decode = (buffer: Buffer.ReadBuffer) =>
ProcedureId.decode(buffer).pipe(Effect.map(make));

Expand Down
4 changes: 4 additions & 0 deletions libs/@local/harpc/client/typescript/src/types/ProcedureId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,15 @@ export const make = (
return Effect.succeed(makeUnchecked(id));
};

export type EncodeError = Effect.Effect.Error<ReturnType<typeof encode>>;

export const encode = encodeDual(
(buffer: Buffer.WriteBuffer, procedureId: ProcedureId) =>
Buffer.putU16(buffer, procedureId.value),
);

export type DecodeError = Effect.Effect.Error<ReturnType<typeof decode>>;

export const decode = (buffer: Buffer.ReadBuffer) =>
Buffer.getU16(buffer).pipe(Effect.map(makeUnchecked));

Expand Down
4 changes: 4 additions & 0 deletions libs/@local/harpc/client/typescript/src/types/ResponseKind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ export const err = (code: ErrorCode.ErrorCode): Err =>

export type ResponseKind = Ok | Err;

export type EncodeError = Effect.Effect.Error<ReturnType<typeof encode>>;

export const encode = encodeDual(
(buffer: Buffer.WriteBuffer, kind: ResponseKind) =>
Effect.gen(function* () {
Expand All @@ -133,6 +135,8 @@ export const encode = encodeDual(
}),
);

export type DecodeError = Effect.Effect.Error<ReturnType<typeof decode>>;

export const decode = (buffer: Buffer.ReadBuffer) =>
Effect.gen(function* () {
const value = yield* Buffer.getU16(buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export const make = (
): SubsystemDescriptor =>
createProto(SubsystemDescriptorProto, { id, version });

export type EncodeError = Effect.Effect.Error<ReturnType<typeof encode>>;

export const encode = encodeDual(
(buffer: Buffer.WriteBuffer, descriptor: SubsystemDescriptor) =>
pipe(
Expand All @@ -88,6 +90,8 @@ export const encode = encodeDual(
),
);

export type DecodeError = Effect.Effect.Error<ReturnType<typeof decode>>;

export const decode = (buffer: Buffer.ReadBuffer) =>
Effect.gen(function* () {
const id = yield* SubsystemId.decode(buffer);
Expand Down
4 changes: 4 additions & 0 deletions libs/@local/harpc/client/typescript/src/types/SubsystemId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,15 @@ export const make = (
return Effect.succeed(makeUnchecked(id));
};

export type EncodeError = Effect.Effect.Error<ReturnType<typeof encode>>;

export const encode = encodeDual(
(buffer: Buffer.WriteBuffer, subsystemId: SubsystemId) =>
Buffer.putU16(buffer, subsystemId.value),
);

export type DecodeError = Effect.Effect.Error<ReturnType<typeof decode>>;

export const decode = (buffer: Buffer.ReadBuffer) =>
Buffer.getU16(buffer).pipe(Effect.map(makeUnchecked));

Expand Down
4 changes: 4 additions & 0 deletions libs/@local/harpc/client/typescript/src/types/Version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ const VersionProto: Omit<Version, "major" | "minor"> = {
export const make = (major: number, minor: number): Version =>
createProto(VersionProto, { major, minor });

export type EncodeError = Effect.Effect.Error<ReturnType<typeof encode>>;

export const encode: {
(version: Version): (buffer: Buffer.WriteBuffer) => Buffer.WriteResult;
(buffer: Buffer.WriteBuffer, version: Version): Buffer.WriteResult;
Expand All @@ -87,6 +89,8 @@ export const encode: {
}),
);

export type DecodeError = Effect.Effect.Error<ReturnType<typeof decode>>;

export const decode = (buffer: Buffer.ReadBuffer) =>
Effect.gen(function* () {
const major = yield* Buffer.getU8(buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,5 +273,5 @@ export const take = (buffer: Buffer<Write>) =>
const impl = buffer as unknown as BufferImpl<Write>;

const inner = impl.value.buffer.slice(0, currentLength);
return new Uint8Array(inner);
return inner;
});
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ export const make = (
return Effect.succeed(makeUnchecked(buffer));
};

export type EncodeError = Effect.Effect.Error<ReturnType<typeof encode>>;

export const encode = encodeDual(
(buffer: Buffer.WriteBuffer, payload: Payload) =>
Effect.gen(function* () {
Expand All @@ -138,6 +140,8 @@ export const encode = encodeDual(
}),
);

export type DecodeError = Effect.Effect.Error<ReturnType<typeof decode>>;

export const decode = (buffer: Buffer.ReadBuffer) =>
Effect.gen(function* () {
const length = yield* Buffer.getU16(buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ const MAGIC = new Uint8Array([
0x68 /* h */, 0x61 /* a */, 0x72 /* r */, 0x70 /* p */, 0x63 /* c */,
]);

export type EncodeError = Effect.Effect.Error<ReturnType<typeof encode>>;

export const encode = encodeDual(
(buffer: Buffer.WriteBuffer, protocol: Protocol): Buffer.WriteResult => {
return pipe(
Expand All @@ -90,6 +92,8 @@ export const encode = encodeDual(
},
);

export type DecodeError = Effect.Effect.Error<ReturnType<typeof decode>>;

export const decode = (buffer: Buffer.ReadBuffer) =>
Effect.gen(function* () {
const magic = yield* Buffer.getSlice(buffer, 5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ export const encode = encodeDual(
},
);

export type DecodeError = Effect.Effect.Error<ReturnType<typeof decode>>;

export const decode = (buffer: Buffer.ReadBuffer) =>
Effect.gen(function* () {
const version = yield* Buffer.getU8(buffer);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* eslint-disable canonical/filename-no-index */

export * as Payload from "./Payload.js";
export * as Protocol from "./Protocol.js";
export * as ProtocolVersion from "./ProtocolVersion.js";
export * as request from "./request/index.js";
export * as response from "./response/index.js";
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export const prepare = (self: Request) => {
return make(header, self.body);
};

export type EncodeError = Effect.Effect.Error<ReturnType<typeof encode>>;

export const encode = encodeDual(
(buffer: Buffer.WriteBuffer, request: Request) =>
Effect.gen(function* () {
Expand All @@ -105,6 +107,8 @@ export const encode = encodeDual(
}),
);

export type DecodeError = Effect.Effect.Error<ReturnType<typeof decode>>;

export const decode = (buffer: Buffer.ReadBuffer) =>
Effect.gen(function* () {
const header = yield* RequestHeader.decode(buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ export const make = (
): RequestBegin =>
createProto(RequestBeginProto, { subsystem, procedure, payload });

export type EncodeError = Effect.Effect.Error<ReturnType<typeof encode>>;

export const encode = encodeDual(
(buffer: Buffer.WriteBuffer, begin: RequestBegin) =>
pipe(
Expand All @@ -100,6 +102,8 @@ export const encode = encodeDual(
),
);

export type DecodeError = Effect.Effect.Error<ReturnType<typeof decode>>;

export const decode = (buffer: Buffer.ReadBuffer) =>
Effect.gen(function* () {
const subsystem = yield* SubsystemDescriptor.decode(buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ export const match: {
}),
);

export type EncodeError = Effect.Effect.Error<ReturnType<typeof encode>>;

export const encode = encodeDual(
(buffer: Buffer.WriteBuffer, body: RequestBody) =>
match(body, {
Expand All @@ -113,6 +115,8 @@ export const encode = encodeDual(
}),
);

export type DecodeError = Effect.Effect.Error<ReturnType<typeof decode>>;

export const decode = (
buffer: Buffer.ReadBuffer,
variant: RequestBodyVariant,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ export const repr = (flags: RequestFlags) => {
return value;
};

export type EncodeError = Effect.Effect.Error<ReturnType<typeof encode>>;

export const encode = encodeDual(
(buffer: Buffer.WriteBuffer, flags: RequestFlags) =>
Effect.gen(function* () {
Expand All @@ -111,6 +113,8 @@ export const encode = encodeDual(
}),
);

export type DecodeError = Effect.Effect.Error<ReturnType<typeof decode>>;

export const decode = (buffer: Buffer.ReadBuffer) =>
Effect.gen(function* () {
const value = yield* Buffer.getU8(buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ const RequestFrameProto: Omit<RequestFrame, "payload"> = {
export const make = (payload: Payload.Payload): RequestFrame =>
createProto(RequestFrameProto, { payload });

export type EncodeError = Effect.Effect.Error<ReturnType<typeof encode>>;

export const encode = encodeDual(
(buffer: Buffer.WriteBuffer, frame: RequestFrame) =>
pipe(
Expand All @@ -78,6 +80,8 @@ export const encode = encodeDual(
),
);

export type DecodeError = Effect.Effect.Error<ReturnType<typeof decode>>;

export const decode = (buffer: Buffer.ReadBuffer) =>
Effect.gen(function* () {
yield* Buffer.advance(buffer, 19);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ export const applyBodyVariant = (
RequestFlags.applyBodyVariant(header.flags, variant),
);

export type EncodeError = Effect.Effect.Error<ReturnType<typeof encode>>;

export const encode = encodeDual(
(buffer: Buffer.WriteBuffer, header: RequestHeader) => {
return pipe(
Expand All @@ -111,6 +113,8 @@ export const encode = encodeDual(
},
);

export type DecodeError = Effect.Effect.Error<ReturnType<typeof decode>>;

export const decode = (buffer: Buffer.ReadBuffer) =>
Effect.gen(function* () {
const protocol = yield* Protocol.decode(buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,15 @@ const RequestIdProto: Omit<RequestId, "value"> = {
export const makeUnchecked = (value: number): RequestId =>
createProto(RequestIdProto, { value });

export type EncodeError = Effect.Effect.Error<ReturnType<typeof encode>>;

export const encode = encodeDual(
(buffer: Buffer.WriteBuffer, requestId: RequestId) =>
Buffer.putU32(buffer, requestId.value),
);

export type DecodeError = Effect.Effect.Error<ReturnType<typeof decode>>;

export const decode = (buffer: Buffer.ReadBuffer) =>
pipe(
Buffer.getU32(buffer), //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export const prepare = (self: Response) => {
return make(header, self.body);
};

export type EncodeError = Effect.Effect.Error<ReturnType<typeof encode>>;

export const encode = encodeDual(
(buffer: Buffer.WriteBuffer, request: Response) =>
Effect.gen(function* () {
Expand All @@ -105,6 +107,8 @@ export const encode = encodeDual(
}),
);

export type DecodeError = Effect.Effect.Error<ReturnType<typeof decode>>;

export const decode = (buffer: Buffer.ReadBuffer) =>
Effect.gen(function* () {
const header = yield* ResponseHeader.decode(buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export const make = (
payload: Payload.Payload,
): ResponseBegin => createProto(ResponseBeginProto, { kind, payload });

export type EncodeError = Effect.Effect.Error<ReturnType<typeof encode>>;

export const encode = encodeDual(
(buffer: Buffer.WriteBuffer, begin: ResponseBegin) =>
pipe(
Expand All @@ -89,6 +91,8 @@ export const encode = encodeDual(
),
);

export type DecodeError = Effect.Effect.Error<ReturnType<typeof decode>>;

export const decode = (buffer: Buffer.ReadBuffer) =>
Effect.gen(function* () {
yield* Buffer.advance(buffer, 17);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ export const match: {
}),
);

export type EncodeError = Effect.Effect.Error<ReturnType<typeof encode>>;

export const encode = encodeDual(
(buffer: Buffer.WriteBuffer, body: ResponseBody) =>
match(body, {
Expand All @@ -113,6 +115,8 @@ export const encode = encodeDual(
}),
);

export type DecodeError = Effect.Effect.Error<ReturnType<typeof decode>>;

export const decode = (
buffer: Buffer.ReadBuffer,
variant: ResponseBodyVariant,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ export const repr = (flags: ResponseFlags) => {
return value;
};

export type EncodeError = Effect.Effect.Error<ReturnType<typeof encode>>;

export const encode = encodeDual(
(buffer: Buffer.WriteBuffer, flags: ResponseFlags) =>
Effect.gen(function* () {
Expand All @@ -110,6 +112,8 @@ export const encode = encodeDual(
}),
);

export type DecodeError = Effect.Effect.Error<ReturnType<typeof decode>>;

export const decode = (buffer: Buffer.ReadBuffer) =>
Effect.gen(function* () {
const value = yield* Buffer.getU8(buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ const ResponseFrameProto: Omit<ResponseFrame, "payload"> = {
export const make = (payload: Payload.Payload): ResponseFrame =>
createProto(ResponseFrameProto, { payload });

export type EncodeError = Effect.Effect.Error<ReturnType<typeof encode>>;

export const encode = encodeDual(
(buffer: Buffer.WriteBuffer, frame: ResponseFrame) =>
pipe(
Expand All @@ -78,6 +80,8 @@ export const encode = encodeDual(
),
);

export type DecodeError = Effect.Effect.Error<ReturnType<typeof decode>>;

export const decode = (buffer: Buffer.ReadBuffer) =>
Effect.gen(function* () {
yield* Buffer.advance(buffer, 19);
Expand Down
Loading