void-fate 0.0.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Nakazawa Tech
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,127 @@
1
+ # void-fate
2
+
3
+ Void adapter for [fate](https://github.com/nkzw-tech/fate).
4
+
5
+ `void-fate` contains the server route helpers, SSE live transport, and React
6
+ client wrapper needed to use fate in a Void app without keeping adapter glue in
7
+ application code.
8
+
9
+ ## Install
10
+
11
+ ```sh
12
+ pnpm add @nkzw/fate react-fate void-fate void
13
+ ```
14
+
15
+ ## Vite
16
+
17
+ Use the regular `react-fate` Vite plugin with the Void transport.
18
+
19
+ ```ts
20
+ import { fate } from 'react-fate/vite';
21
+ import { defineConfig } from 'vite';
22
+
23
+ export default defineConfig({
24
+ plugins: [
25
+ fate({
26
+ module: './src/fate/server.ts',
27
+ transport: 'void',
28
+ }),
29
+ ],
30
+ });
31
+ ```
32
+
33
+ ## Server
34
+
35
+ Create a Fate live publish facade and pass its `live` instance to the fate
36
+ server. Define the Void stream separately so `defineLiveStream` is called once
37
+ from a single server-only module.
38
+
39
+ ```ts
40
+ // src/fate/live.ts
41
+ import { defineLiveStream } from 'void/live';
42
+
43
+ export const fateStream = defineLiveStream({
44
+ allowAnonymousControl: true,
45
+ id: 'fate',
46
+ });
47
+ ```
48
+
49
+ ```ts
50
+ // src/fate/server.ts
51
+ import { createFateServer } from '@nkzw/fate/server';
52
+ import { createVoidFateLive } from 'void-fate/server';
53
+
54
+ export const fateLive = createVoidFateLive();
55
+
56
+ export const fateServer = createFateServer({
57
+ live: fateLive.live,
58
+ // context,
59
+ // roots,
60
+ // sources,
61
+ });
62
+ ```
63
+
64
+ ## Routes
65
+
66
+ Add one route for RPC requests and one route for the SSE live transport.
67
+
68
+ ```ts
69
+ // routes/fate.ts
70
+ import { defineVoidFateRoute } from 'void-fate/server';
71
+ import { fateStream } from '../src/fate/live.ts';
72
+ import { fateLive, fateServer } from '../src/fate/server.ts';
73
+
74
+ export const { GET, POST } = defineVoidFateRoute(fateServer, fateLive, {
75
+ stream: fateStream,
76
+ });
77
+ ```
78
+
79
+ ```ts
80
+ // routes/fate-live.ts
81
+ import { defineVoidFateLiveRoute } from 'void-fate/server';
82
+ import { fateStream } from '../src/fate/live.ts';
83
+
84
+ export const { GET, POST } = defineVoidFateLiveRoute(fateStream);
85
+ ```
86
+
87
+ The default paths are `/fate` for RPC and `/fate-live` for live updates.
88
+
89
+ ## React
90
+
91
+ Use `VoidFateClient` in your app layout. The generated fate client is still
92
+ provided by the `react-fate` Vite plugin.
93
+
94
+ ```tsx
95
+ import type { ReactNode } from 'react';
96
+ import { VoidFateClient } from 'void-fate/react';
97
+
98
+ export default function Layout({
99
+ children,
100
+ origin,
101
+ userId,
102
+ }: {
103
+ children: ReactNode;
104
+ origin: string;
105
+ userId?: string;
106
+ }) {
107
+ return (
108
+ <VoidFateClient origin={origin} userId={userId}>
109
+ {children}
110
+ </VoidFateClient>
111
+ );
112
+ }
113
+ ```
114
+
115
+ ## Custom Paths
116
+
117
+ The defaults are intended to work without configuration. If your Void routes use
118
+ different paths, pass the same values to the server and client helpers.
119
+
120
+ ```tsx
121
+ <VoidFateClient livePath="/custom-live" origin={origin} rpcPath="/custom-fate" userId={userId}>
122
+ {children}
123
+ </VoidFateClient>
124
+ ```
125
+
126
+ `void-fate` uses Server-Sent Events for live updates. It does not use
127
+ WebSockets.
@@ -0,0 +1,2 @@
1
+ import { VoidFateLive, VoidFateLiveOptions, VoidFateRouteOptions, createVoidFateLive, defaultVoidFateLivePath, defaultVoidFateRpcPath, defineVoidFateLiveRoute, defineVoidFateRoute } from "./server.mjs";
2
+ export { type VoidFateLive, type VoidFateLiveOptions, type VoidFateRouteOptions, createVoidFateLive, defaultVoidFateLivePath, defaultVoidFateRpcPath, defineVoidFateLiveRoute, defineVoidFateRoute };
package/lib/index.mjs ADDED
@@ -0,0 +1,2 @@
1
+ import { createVoidFateLive, defaultVoidFateLivePath, defaultVoidFateRpcPath, defineVoidFateLiveRoute, defineVoidFateRoute } from "./server.mjs";
2
+ export { createVoidFateLive, defaultVoidFateLivePath, defaultVoidFateRpcPath, defineVoidFateLiveRoute, defineVoidFateRoute };
@@ -0,0 +1,32 @@
1
+ import { ReactNode } from "react";
2
+ import * as _$react_jsx_runtime0 from "react/jsx-runtime";
3
+
4
+ //#region src/react.d.ts
5
+ type VoidFateClientProps = {
6
+ children: ReactNode;
7
+ fetch?: typeof fetch;
8
+ headers?: HeadersInit | (() => HeadersInit | Promise<HeadersInit>);
9
+ livePath?: string;
10
+ liveRetryMs?: number;
11
+ liveUrl?: string | URL;
12
+ onLiveError?: (error: unknown) => void;
13
+ origin?: string | URL;
14
+ rpcPath?: string;
15
+ url?: string | URL;
16
+ userId?: null | string;
17
+ };
18
+ declare function VoidFateClient({
19
+ children,
20
+ fetch,
21
+ headers,
22
+ livePath,
23
+ liveRetryMs,
24
+ liveUrl,
25
+ onLiveError,
26
+ origin,
27
+ rpcPath,
28
+ url,
29
+ userId
30
+ }: VoidFateClientProps): _$react_jsx_runtime0.JSX.Element;
31
+ //#endregion
32
+ export { VoidFateClient, VoidFateClientProps };
package/lib/react.mjs ADDED
@@ -0,0 +1,35 @@
1
+ import { useMemo } from "react";
2
+ import { FateClient } from "react-fate";
3
+ import { createFateClient } from "react-fate/client";
4
+ import { jsx } from "react/jsx-runtime";
5
+ //#region src/react.tsx
6
+ function VoidFateClient({ children, fetch, headers, livePath, liveRetryMs, liveUrl, onLiveError, origin, rpcPath, url, userId }) {
7
+ return /* @__PURE__ */ jsx(FateClient, {
8
+ client: useMemo(() => createFateClient({
9
+ fetch,
10
+ headers,
11
+ livePath,
12
+ liveRetryMs,
13
+ liveUrl,
14
+ onLiveError,
15
+ origin,
16
+ rpcPath,
17
+ url,
18
+ userId
19
+ }), [
20
+ fetch,
21
+ headers,
22
+ livePath,
23
+ liveRetryMs,
24
+ liveUrl,
25
+ onLiveError,
26
+ origin,
27
+ rpcPath,
28
+ url,
29
+ userId
30
+ ]),
31
+ children
32
+ }, userId ?? void 0);
33
+ }
34
+ //#endregion
35
+ export { VoidFateClient };
@@ -0,0 +1,42 @@
1
+ import { FateServer, LiveEventBus } from "@nkzw/fate/server";
2
+ import * as _$void from "void";
3
+
4
+ //#region src/server.d.ts
5
+ type VoidEnv = Record<string, unknown>;
6
+ type VoidFatePublishOptions = {
7
+ eventId?: string;
8
+ type?: string;
9
+ };
10
+ type VoidFateLiveStream = {
11
+ connect(context: unknown, options?: unknown): Promise<Response>;
12
+ control(context: unknown, options?: unknown): Promise<Response>;
13
+ withEnv(env: unknown): {
14
+ publish(topic: string, data: unknown, options?: VoidFatePublishOptions): Promise<void>;
15
+ };
16
+ };
17
+ type FateLiveContext = {
18
+ env: VoidEnv;
19
+ pending: Array<Promise<void>>;
20
+ stream: VoidFateLiveStream;
21
+ };
22
+ type VoidFateLiveOptions = Record<never, never>;
23
+ type VoidFateRouteOptions = {
24
+ stream: VoidFateLiveStream;
25
+ };
26
+ type VoidFateLive = Readonly<{
27
+ live: LiveEventBus;
28
+ withContext: <T>(context: Omit<FateLiveContext, 'pending'>, callback: () => T) => T;
29
+ }>;
30
+ declare const defaultVoidFateRpcPath = "/fate";
31
+ declare const defaultVoidFateLivePath = "/fate-live";
32
+ declare function createVoidFateLive(_options?: VoidFateLiveOptions): VoidFateLive;
33
+ declare function defineVoidFateRoute<AdapterContext>(server: FateServer<unknown, AdapterContext>, live: VoidFateLive, options: VoidFateRouteOptions): {
34
+ GET: _$void.TypedHandler<{}, never>;
35
+ POST: _$void.TypedHandler<{}, never>;
36
+ };
37
+ declare function defineVoidFateLiveRoute(stream: VoidFateLiveStream): {
38
+ GET: _$void.TypedHandler<{}, never>;
39
+ POST: _$void.TypedHandler<{}, never>;
40
+ };
41
+ //#endregion
42
+ export { VoidFateLive, VoidFateLiveOptions, VoidFateLiveStream, VoidFateRouteOptions, createVoidFateLive, defaultVoidFateLivePath, defaultVoidFateRpcPath, defineVoidFateLiveRoute, defineVoidFateRoute };
package/lib/server.mjs ADDED
@@ -0,0 +1,154 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+ import { createFateFetchHandler, liveConnectionTopic, liveEntityTopic, liveGlobalConnectionTopic } from "@nkzw/fate/server";
3
+ import { defineHandler } from "void";
4
+ //#region src/server.ts
5
+ const defaultVoidFateRpcPath = "/fate";
6
+ const defaultVoidFateLivePath = "/fate-live";
7
+ const isPromiseLike = (value) => (typeof value === "object" || typeof value === "function") && value !== null && typeof value.then === "function";
8
+ function createVoidFateLive(_options = {}) {
9
+ const contextStore = new AsyncLocalStorage();
10
+ const publish = (topic, data, options) => {
11
+ const context = contextStore.getStore();
12
+ if (!context) return;
13
+ const promise = context.stream.withEnv(context.env).publish(topic, data, options).catch(() => void 0);
14
+ context.pending.push(promise);
15
+ };
16
+ const publishEntity = (type, id, options = {}) => {
17
+ publish(liveEntityTopic(type, id), {
18
+ changed: options.changed,
19
+ data: options.data,
20
+ id
21
+ }, {
22
+ eventId: options.eventId,
23
+ type: options.type ?? "update"
24
+ });
25
+ };
26
+ const publishConnection = (procedure, args, type, options = {}) => {
27
+ publish(args ? liveConnectionTopic(procedure, args) : liveGlobalConnectionTopic(procedure), {
28
+ cursor: options.cursor,
29
+ id: options.id,
30
+ node: options.node,
31
+ nodeType: options.nodeType,
32
+ targetCursor: options.targetCursor
33
+ }, {
34
+ eventId: options.eventId,
35
+ type
36
+ });
37
+ };
38
+ const withContext = (context, callback) => contextStore.run({
39
+ ...context,
40
+ pending: []
41
+ }, () => {
42
+ const liveContext = contextStore.getStore();
43
+ const result = callback();
44
+ if (isPromiseLike(result) || liveContext.pending.length > 0) return Promise.resolve(result).then(async (value) => {
45
+ if (liveContext.pending.length > 0) await Promise.allSettled(liveContext.pending);
46
+ return value;
47
+ });
48
+ return result;
49
+ });
50
+ return {
51
+ live: {
52
+ connection(procedure, args) {
53
+ return {
54
+ appendEdge(nodeType, id, options) {
55
+ publishConnection(procedure, args, "appendEdge", {
56
+ ...options,
57
+ id,
58
+ nodeType
59
+ });
60
+ },
61
+ appendNode(nodeType, id, options) {
62
+ publishConnection(procedure, args, "appendNode", {
63
+ ...options,
64
+ id,
65
+ nodeType
66
+ });
67
+ },
68
+ deleteEdge(nodeType, id, options) {
69
+ publishConnection(procedure, args, "deleteEdge", {
70
+ ...options,
71
+ id,
72
+ nodeType
73
+ });
74
+ },
75
+ emit(type, options) {
76
+ publishConnection(procedure, args, type, options);
77
+ },
78
+ insertEdgeAfter(nodeType, id, targetCursor, options) {
79
+ publishConnection(procedure, args, "insertEdgeAfter", {
80
+ ...options,
81
+ id,
82
+ nodeType,
83
+ targetCursor
84
+ });
85
+ },
86
+ insertEdgeBefore(nodeType, id, targetCursor, options) {
87
+ publishConnection(procedure, args, "insertEdgeBefore", {
88
+ ...options,
89
+ id,
90
+ nodeType,
91
+ targetCursor
92
+ });
93
+ },
94
+ invalidate(options) {
95
+ publishConnection(procedure, args, "invalidate", options);
96
+ },
97
+ prependEdge(nodeType, id, options) {
98
+ publishConnection(procedure, args, "prependEdge", {
99
+ ...options,
100
+ id,
101
+ nodeType
102
+ });
103
+ },
104
+ prependNode(nodeType, id, options) {
105
+ publishConnection(procedure, args, "prependNode", {
106
+ ...options,
107
+ id,
108
+ nodeType
109
+ });
110
+ }
111
+ };
112
+ },
113
+ delete(type, id, options) {
114
+ publishEntity(type, id, {
115
+ ...options,
116
+ type: "delete"
117
+ });
118
+ },
119
+ emit: publishEntity,
120
+ subscribe() {
121
+ throw new Error("void-fate: direct live subscriptions are handled by void/live.");
122
+ },
123
+ subscribeConnection() {
124
+ throw new Error("void-fate: direct live subscriptions are handled by void/live.");
125
+ },
126
+ update(type, id, options) {
127
+ publishEntity(type, id, {
128
+ ...options,
129
+ type: "update"
130
+ });
131
+ }
132
+ },
133
+ withContext
134
+ };
135
+ }
136
+ function defineVoidFateRoute(server, live, options) {
137
+ const handleFate = createFateFetchHandler(server);
138
+ const handle = (context) => live.withContext({
139
+ env: context.env,
140
+ stream: options.stream
141
+ }, () => handleFate(context.req.raw, context));
142
+ return {
143
+ GET: defineHandler(handle),
144
+ POST: defineHandler(handle)
145
+ };
146
+ }
147
+ function defineVoidFateLiveRoute(stream) {
148
+ return {
149
+ GET: defineHandler((context) => stream.connect(context)),
150
+ POST: defineHandler((context) => stream.control(context))
151
+ };
152
+ }
153
+ //#endregion
154
+ export { createVoidFateLive, defaultVoidFateLivePath, defaultVoidFateRpcPath, defineVoidFateLiveRoute, defineVoidFateRoute };
package/package.json CHANGED
@@ -1,7 +1,65 @@
1
1
  {
2
2
  "name": "void-fate",
3
- "version": "0.0.0",
4
- "description": "",
3
+ "version": "1.0.0",
4
+ "description": "Void adapter for fate.",
5
+ "homepage": "https://github.com/nkzw-tech/fate",
6
+ "license": "MIT",
7
+ "author": {
8
+ "name": "Christoph Nakazawa",
9
+ "email": "christoph.pojer@gmail.com"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/nkzw-tech/fate",
14
+ "directory": "packages/void-fate"
15
+ },
16
+ "files": [
17
+ "lib"
18
+ ],
5
19
  "type": "module",
6
- "main": "index.js"
20
+ "main": "./lib/index.mjs",
21
+ "types": "./lib/index.d.mts",
22
+ "exports": {
23
+ ".": {
24
+ "types": "./lib/index.d.mts",
25
+ "@nkzw/source": "./src/index.ts",
26
+ "default": "./lib/index.mjs"
27
+ },
28
+ "./react": {
29
+ "types": "./lib/react.d.mts",
30
+ "@nkzw/source": "./src/react.tsx",
31
+ "default": "./lib/react.mjs"
32
+ },
33
+ "./server": {
34
+ "types": "./lib/server.d.mts",
35
+ "@nkzw/source": "./src/server.ts",
36
+ "default": "./lib/server.mjs"
37
+ }
38
+ },
39
+ "dependencies": {
40
+ "@nkzw/fate": "^1.0.0",
41
+ "react-fate": "^1.0.0"
42
+ },
43
+ "devDependencies": {
44
+ "@types/react": "^19.2.14",
45
+ "react": "^19.2.6",
46
+ "void": "^0.7.5"
47
+ },
48
+ "peerDependencies": {
49
+ "@nkzw/fate": "^1.0.0-rc.0",
50
+ "react": "^19.2.0",
51
+ "react-fate": "^1.0.0-rc.0",
52
+ "void": "^0.7.5"
53
+ },
54
+ "peerDependenciesMeta": {
55
+ "react": {
56
+ "optional": true
57
+ },
58
+ "react-fate": {
59
+ "optional": true
60
+ }
61
+ },
62
+ "scripts": {
63
+ "build": "vp pack -d lib --target=node24 src/index.ts src/react.tsx src/server.ts"
64
+ }
7
65
  }