Skip to content

Commit

Permalink
update libs include h3
Browse files Browse the repository at this point in the history
remove h3-sse
  • Loading branch information
joshmossas committed Jun 20, 2024
1 parent bb0c3ec commit 68931fa
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 161 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Setup pnpm
uses: pnpm/action-setup@v2
with:
version: 9.1.4
version: 9.4.0
- name: Install Node.js
uses: actions/setup-node@v3
with:
Expand Down Expand Up @@ -54,7 +54,7 @@ jobs:
- name: Setup pnpm
uses: pnpm/action-setup@v2
with:
version: 9.1.4
version: 9.4.0
- name: Install Node.js
uses: actions/setup-node@v3
with:
Expand Down Expand Up @@ -89,7 +89,7 @@ jobs:
- name: Setup pnpm
uses: pnpm/action-setup@v2
with:
version: 9.1.4
version: 9.4.0
- name: Install Node.js
uses: actions/setup-node@v3
with:
Expand Down
5 changes: 2 additions & 3 deletions languages/ts/ts-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@
"@types/source-map-support": "^0.5.10",
"arri": "workspace:*",
"crossws": "^0.2.4",
"h3": "^1.11.1",
"h3-sse": "^0.0.12",
"h3": "^1.12.0",
"scule": "^1.3.0",
"source-map-support": "^0.5.21",
"uncrypto": "^0.1.3"
},
"devDependencies": {
"bun-types": "^1.1.14"
"bun-types": "^1.1.15"
}
}
2 changes: 1 addition & 1 deletion languages/ts/ts-server/src/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ it("creates valid app definition", () => {
const timeout = setInterval(async () => {
await stream.push({ message: `Hello ${params.name}` });
}, 100);
stream.onClose(() => {
stream.onClosed(() => {
clearInterval(timeout);
});
},
Expand Down
45 changes: 3 additions & 42 deletions languages/ts/ts-server/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { a, type AObjectSchema,type ValidationError } from "@arrirpc/schema";
import { a, type AObjectSchema, type ValidationError } from "@arrirpc/schema";
import {
type H3Error,
type H3Event,
isError,
send,
setResponseHeader,
setResponseStatus,
StatusCode,
} from "h3";

import { type ArriOptions } from "./app";
Expand Down Expand Up @@ -77,48 +78,8 @@ export function errorResponseFromValidationErrors(
});
}

export type StatusCode =
| number
| 400
| 401
| 402
| 403
| 404
| 405
| 406
| 407
| 408
| 409
| 410
| 411
| 412
| 413
| 414
| 415
| 416
| 417
| 418
| 419
| 420
| 421
| 422
| 423
| 424
| 428
| 429
| 431
| 451
| 500
| 501
| 502
| 503
| 504
| 505
| 507
| 511;

const errorResponseDefaults: Record<
StatusCode,
StatusCode | number,
{ name: string; message: string }
> = {
400: {
Expand Down
64 changes: 30 additions & 34 deletions languages/ts/ts-server/src/eventStreamRpc.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { a, type InferType, type ValueError } from "@arrirpc/schema";
import {
createEventStream,
eventHandler,
EventStream,
EventStreamMessage,
getHeader,
type H3Event,
isPreflightRequest,
type Router,
} from "h3";
import {
createEventStream,
type EventStream,
type EventStreamMessage,
} from "h3-sse";

import { type RpcEventContext } from "./context";
import { type ArriServerError, defineError, handleH3Error } from "./errors";
import { handleH3Error } from "./errors";
import { type MiddlewareEvent } from "./middleware";
import { type RouteOptions } from "./route";
import {
Expand Down Expand Up @@ -89,12 +88,12 @@ export class EventStreamConnection<TData> {

constructor(event: H3Event, opts: EventStreamConnectionOptions<TData>) {
this.eventStream = createEventStream(event);
this.lastEventId = this.eventStream.lastEventId;
this.lastEventId = getHeader(event, "Last-Event-Id");
this.pingIntervalMs = opts.pingInterval ?? 60000;
this.serializer = opts.serializer;
this.validator = opts.validator;
this.validationErrors = opts.validationErrors;
this.eventStream.onClose(() => {
this.eventStream.onClosed(() => {
this.cleanup();
});
}
Expand All @@ -115,10 +114,11 @@ export class EventStreamConnection<TData> {
/**
* Publish a new event. Events published with this hook will trigger the `onData()` hooks of any connected clients.
*/
async push(data: TData[], eventId?: string): Promise<void>;
async push(data: TData, eventId?: string): Promise<void>;
async push(data: TData[], eventId?: string): Promise<SsePushResult[]>;
async push(data: TData, eventId?: string): Promise<SsePushResult>;
async push(data: TData | TData[], eventId?: string) {
if (Array.isArray(data)) {
const results: SsePushResult[] = [];
const events: EventStreamMessage[] = [];
for (const item of data) {
if (this.validator(item)) {
Expand All @@ -127,42 +127,32 @@ export class EventStreamConnection<TData> {
event: "message",
data: this.serializer(item),
});
results.push({ success: true });
continue;
}
const errors = this.validationErrors(item);
const errorResponse: ArriServerError = defineError(500, {
message:
"Failed to serialize response. Response does not match specified schema.",
data: errors,
});
events.push({
id: eventId,
event: "error",
data: JSON.stringify(errorResponse),
results.push({
success: false,
errors,
});
}
await this.eventStream.push(events);
return;
return results;
}
if (this.validator(data)) {
await this.eventStream.push({
id: eventId,
event: "message",
data: this.serializer(data),
});
return;

return { success: true };
}
const errors = this.validationErrors(data);
const errorResponse = defineError(500, {
message:
"Failed to serialize response. Response does not match specified schema.",
data: errors,
});
await this.eventStream.push({
id: eventId,
event: "error",
data: JSON.stringify(errorResponse),
});
return {
success: false,
errors,
};
}

private cleanup() {
Expand All @@ -185,11 +175,17 @@ export class EventStreamConnection<TData> {
await this.eventStream.close();
}

onClose(cb: () => any): void {
this.eventStream.onClose(cb);
onClosed(cb: () => any): void {
this.eventStream.onClosed(cb);
}
}

export type SsePushResult =
| {
success: true;
}
| { success: false; errors: ValueError[] };

export function registerEventStreamRpc(
router: Router,
path: string,
Expand Down Expand Up @@ -252,7 +248,7 @@ export function registerEventStreamRpc(
event.context as EventStreamRpcHandlerContext,
event as RpcEvent<any>,
);
if (!event.handled && !stream.eventStream._handled) {
if (!event.handled) {
stream.send();
}
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
"@nx/workspace": "19.3.0",
"@sinclair/typebox": "^0.32.33",
"@types/lodash": "^4.17.5",
"@types/node": "20.14.5",
"@types/node": "20.14.6",
"@vitest/coverage-v8": "1.6.0",
"@vitest/ui": "1.6.0",
"benny": "^3.7.1",
"bun-types": "^1.1.14",
"bun-types": "^1.1.15",
"citty": "^0.1.6",
"depcheck": "^1.4.7",
"esbuild": "0.21.5",
Expand Down
Loading

0 comments on commit 68931fa

Please sign in to comment.