@iadev93/zuno-elysia 0.0.7 → 0.0.8
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/README.md +12 -2
- package/dist/index.d.mts +40 -12
- package/dist/index.d.ts +40 -12
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -16,12 +16,18 @@ pnpm add @iadev93/zuno-elysia
|
|
|
16
16
|
import { Elysia } from 'elysia'
|
|
17
17
|
import { cors } from '@elysiajs/cors'
|
|
18
18
|
import { createZunoElysia } from '@iadev93/zuno-elysia'
|
|
19
|
+
import { createZunoServerState } from '@iadev93/zuno/server'
|
|
19
20
|
|
|
20
21
|
const app = new Elysia()
|
|
21
22
|
.use(cors())
|
|
22
23
|
|
|
24
|
+
const server = createZunoServerState()
|
|
23
25
|
const zuno = createZunoElysia({
|
|
24
|
-
|
|
26
|
+
server,
|
|
27
|
+
authorize: ({ request, action }) => {
|
|
28
|
+
// Replace with your session, API-key, or tenant policy.
|
|
29
|
+
return canAccessZuno(request, action)
|
|
30
|
+
},
|
|
25
31
|
})
|
|
26
32
|
|
|
27
33
|
// Register Zuno handlers
|
|
@@ -34,10 +40,14 @@ app.listen(3002)
|
|
|
34
40
|
|
|
35
41
|
## API
|
|
36
42
|
|
|
37
|
-
### `createZunoElysia()`
|
|
43
|
+
### `createZunoElysia(options?)`
|
|
38
44
|
|
|
39
45
|
Returns an object containing the following handlers:
|
|
40
46
|
|
|
47
|
+
Each call creates isolated server state by default. Pass `server` when several handlers or custom endpoints must share one authoritative universe. The returned object exposes the selected instance as `zuno.server`.
|
|
48
|
+
|
|
49
|
+
The optional `authorize` hook runs before SSE/snapshot reads and mutation writes. Returning `false` produces a `403 FORBIDDEN` response without reading or mutating server state.
|
|
50
|
+
|
|
41
51
|
#### `sse` (GET)
|
|
42
52
|
An async generator handler for Server-Sent Events. It automatically handles:
|
|
43
53
|
- Connection keep-alive and heartbeats.
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as _iadev93_zuno_server from '@iadev93/zuno/server';
|
|
2
|
+
import { ZunoServerState } from '@iadev93/zuno/server';
|
|
2
3
|
import { ZunoStateEvent } from '@iadev93/zuno';
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -14,7 +15,17 @@ import { ZunoStateEvent } from '@iadev93/zuno';
|
|
|
14
15
|
* - snapshot: A function that handles snapshot GET requests for Elysia.
|
|
15
16
|
* - snapshot: A function that handles snapshot GET requests for Elysia.
|
|
16
17
|
*/
|
|
17
|
-
|
|
18
|
+
type CreateZunoElysiaOptions = {
|
|
19
|
+
/** Isolated authoritative server state. A new instance is created by default. */
|
|
20
|
+
server?: ZunoServerState;
|
|
21
|
+
/** Provider-agnostic authorization hook for snapshots, SSE, and mutations. */
|
|
22
|
+
authorize?: (context: {
|
|
23
|
+
action: "read" | "write";
|
|
24
|
+
request: unknown;
|
|
25
|
+
event?: unknown;
|
|
26
|
+
}) => boolean | Promise<boolean>;
|
|
27
|
+
};
|
|
28
|
+
declare function createZunoElysia(options?: CreateZunoElysiaOptions): {
|
|
18
29
|
/**
|
|
19
30
|
* Handles SSE connections for Elysia using an async generator.
|
|
20
31
|
* @param {Object} param - Elysia request object.
|
|
@@ -22,7 +33,7 @@ declare function createZunoElysia(): {
|
|
|
22
33
|
* @param {Object} param.headers - Elysia request headers.
|
|
23
34
|
* @param {Object} param.query - Elysia request query parameters.
|
|
24
35
|
*/
|
|
25
|
-
sse: (
|
|
36
|
+
sse: (request: any) => AsyncGenerator<any, void, unknown>;
|
|
26
37
|
/**
|
|
27
38
|
* Handles sync POST requests for Elysia.
|
|
28
39
|
* @param {Object} param - Elysia request object.
|
|
@@ -32,20 +43,28 @@ declare function createZunoElysia(): {
|
|
|
32
43
|
* - ok: A boolean indicating whether the sync was successful.
|
|
33
44
|
* - event: The event that was applied to the universe.
|
|
34
45
|
*/
|
|
35
|
-
sync: (
|
|
46
|
+
sync: (request: any) => Promise<{
|
|
36
47
|
ok: boolean;
|
|
37
48
|
reason: string;
|
|
49
|
+
event?: undefined;
|
|
50
|
+
} | {
|
|
38
51
|
current: {
|
|
39
52
|
state: unknown;
|
|
40
53
|
version: number;
|
|
41
|
-
}
|
|
54
|
+
};
|
|
55
|
+
ok: boolean;
|
|
56
|
+
reason: "VERSION_CONFLICT" | "INVALID_EVENT";
|
|
57
|
+
event?: undefined;
|
|
58
|
+
} | {
|
|
59
|
+
errors: _iadev93_zuno_server.EventValidationError[];
|
|
60
|
+
ok: boolean;
|
|
61
|
+
reason: "VERSION_CONFLICT" | "INVALID_EVENT";
|
|
42
62
|
event?: undefined;
|
|
43
63
|
} | {
|
|
44
64
|
ok: boolean;
|
|
45
65
|
event: ZunoStateEvent;
|
|
46
66
|
reason?: undefined;
|
|
47
|
-
|
|
48
|
-
};
|
|
67
|
+
}>;
|
|
49
68
|
/**
|
|
50
69
|
* Handles snapshot GET requests for Elysia.
|
|
51
70
|
* @returns {Object} An object with the following properties:
|
|
@@ -53,13 +72,22 @@ declare function createZunoElysia(): {
|
|
|
53
72
|
* - version: The version of the universe.
|
|
54
73
|
* - lastEventId: The ID of the last event in the universe.
|
|
55
74
|
*/
|
|
56
|
-
snapshot: (
|
|
57
|
-
|
|
58
|
-
|
|
75
|
+
snapshot: (request: {
|
|
76
|
+
set: {
|
|
77
|
+
status?: number;
|
|
59
78
|
};
|
|
60
|
-
|
|
79
|
+
}) => Promise<{
|
|
80
|
+
ok: boolean;
|
|
81
|
+
reason: string;
|
|
82
|
+
state?: undefined;
|
|
83
|
+
lastEventId?: undefined;
|
|
84
|
+
} | {
|
|
85
|
+
state: Record<string, _iadev93_zuno_server.UniverseRecord>;
|
|
61
86
|
lastEventId: number;
|
|
62
|
-
|
|
87
|
+
ok?: undefined;
|
|
88
|
+
reason?: undefined;
|
|
89
|
+
}>;
|
|
90
|
+
server: ZunoServerState;
|
|
63
91
|
};
|
|
64
92
|
|
|
65
|
-
export { createZunoElysia };
|
|
93
|
+
export { type CreateZunoElysiaOptions, createZunoElysia };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as _iadev93_zuno_server from '@iadev93/zuno/server';
|
|
2
|
+
import { ZunoServerState } from '@iadev93/zuno/server';
|
|
2
3
|
import { ZunoStateEvent } from '@iadev93/zuno';
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -14,7 +15,17 @@ import { ZunoStateEvent } from '@iadev93/zuno';
|
|
|
14
15
|
* - snapshot: A function that handles snapshot GET requests for Elysia.
|
|
15
16
|
* - snapshot: A function that handles snapshot GET requests for Elysia.
|
|
16
17
|
*/
|
|
17
|
-
|
|
18
|
+
type CreateZunoElysiaOptions = {
|
|
19
|
+
/** Isolated authoritative server state. A new instance is created by default. */
|
|
20
|
+
server?: ZunoServerState;
|
|
21
|
+
/** Provider-agnostic authorization hook for snapshots, SSE, and mutations. */
|
|
22
|
+
authorize?: (context: {
|
|
23
|
+
action: "read" | "write";
|
|
24
|
+
request: unknown;
|
|
25
|
+
event?: unknown;
|
|
26
|
+
}) => boolean | Promise<boolean>;
|
|
27
|
+
};
|
|
28
|
+
declare function createZunoElysia(options?: CreateZunoElysiaOptions): {
|
|
18
29
|
/**
|
|
19
30
|
* Handles SSE connections for Elysia using an async generator.
|
|
20
31
|
* @param {Object} param - Elysia request object.
|
|
@@ -22,7 +33,7 @@ declare function createZunoElysia(): {
|
|
|
22
33
|
* @param {Object} param.headers - Elysia request headers.
|
|
23
34
|
* @param {Object} param.query - Elysia request query parameters.
|
|
24
35
|
*/
|
|
25
|
-
sse: (
|
|
36
|
+
sse: (request: any) => AsyncGenerator<any, void, unknown>;
|
|
26
37
|
/**
|
|
27
38
|
* Handles sync POST requests for Elysia.
|
|
28
39
|
* @param {Object} param - Elysia request object.
|
|
@@ -32,20 +43,28 @@ declare function createZunoElysia(): {
|
|
|
32
43
|
* - ok: A boolean indicating whether the sync was successful.
|
|
33
44
|
* - event: The event that was applied to the universe.
|
|
34
45
|
*/
|
|
35
|
-
sync: (
|
|
46
|
+
sync: (request: any) => Promise<{
|
|
36
47
|
ok: boolean;
|
|
37
48
|
reason: string;
|
|
49
|
+
event?: undefined;
|
|
50
|
+
} | {
|
|
38
51
|
current: {
|
|
39
52
|
state: unknown;
|
|
40
53
|
version: number;
|
|
41
|
-
}
|
|
54
|
+
};
|
|
55
|
+
ok: boolean;
|
|
56
|
+
reason: "VERSION_CONFLICT" | "INVALID_EVENT";
|
|
57
|
+
event?: undefined;
|
|
58
|
+
} | {
|
|
59
|
+
errors: _iadev93_zuno_server.EventValidationError[];
|
|
60
|
+
ok: boolean;
|
|
61
|
+
reason: "VERSION_CONFLICT" | "INVALID_EVENT";
|
|
42
62
|
event?: undefined;
|
|
43
63
|
} | {
|
|
44
64
|
ok: boolean;
|
|
45
65
|
event: ZunoStateEvent;
|
|
46
66
|
reason?: undefined;
|
|
47
|
-
|
|
48
|
-
};
|
|
67
|
+
}>;
|
|
49
68
|
/**
|
|
50
69
|
* Handles snapshot GET requests for Elysia.
|
|
51
70
|
* @returns {Object} An object with the following properties:
|
|
@@ -53,13 +72,22 @@ declare function createZunoElysia(): {
|
|
|
53
72
|
* - version: The version of the universe.
|
|
54
73
|
* - lastEventId: The ID of the last event in the universe.
|
|
55
74
|
*/
|
|
56
|
-
snapshot: (
|
|
57
|
-
|
|
58
|
-
|
|
75
|
+
snapshot: (request: {
|
|
76
|
+
set: {
|
|
77
|
+
status?: number;
|
|
59
78
|
};
|
|
60
|
-
|
|
79
|
+
}) => Promise<{
|
|
80
|
+
ok: boolean;
|
|
81
|
+
reason: string;
|
|
82
|
+
state?: undefined;
|
|
83
|
+
lastEventId?: undefined;
|
|
84
|
+
} | {
|
|
85
|
+
state: Record<string, _iadev93_zuno_server.UniverseRecord>;
|
|
61
86
|
lastEventId: number;
|
|
62
|
-
|
|
87
|
+
ok?: undefined;
|
|
88
|
+
reason?: undefined;
|
|
89
|
+
}>;
|
|
90
|
+
server: ZunoServerState;
|
|
63
91
|
};
|
|
64
92
|
|
|
65
|
-
export { createZunoElysia };
|
|
93
|
+
export { type CreateZunoElysiaOptions, createZunoElysia };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
'use strict';var server=require('@iadev93/zuno/server'),elysia=require('elysia');function
|
|
1
|
+
'use strict';var server=require('@iadev93/zuno/server'),elysia=require('elysia');function m(v={}){let a=v.server??server.createZunoServerState(),l=v.authorize??(()=>true);return {sse:async function*(t){let{set:r,headers:i,query:u}=t;if(!await l({action:"read",request:t})){r.status=403;return}r.headers["Content-Type"]="text/event-stream",r.headers["Cache-Control"]="no-cache",r.headers.Connection="keep-alive",yield elysia.sse({data:": connected"});let e=[],s=null,y=a.subscribeToStateEvents(n=>{e.push(elysia.sse({id:String(n.eventId),event:"state",data:JSON.stringify(n)})),s&&(s(),s=null);}),f=i["last-event-id"]||u?.lastEventId,c=Number.parseInt(f||"0",10)||0;if(c>0){let n=a.getEventsAfter(c);for(let d of n)yield elysia.sse({id:String(d.eventId),event:"state",data:JSON.stringify(d)});}else {let n=a.getUniverseState();yield elysia.sse({event:"snapshot",data:JSON.stringify(n)});}let h=setInterval(()=>{e.push(elysia.sse({data:`: ping ${Date.now()}`})),s&&(s(),s=null);},15e3);try{for(;;)for(e.length===0&&await new Promise(n=>{s=n;});e.length>0;)yield e.shift();}finally{clearInterval(h),y();}},sync:async t=>{let{body:r,set:i}=t,u=r;if(!await l({action:"write",request:t,event:u}))return i.status=403,{ok:false,reason:"FORBIDDEN"};let e=server.applyStateEvent(u,a);return e.ok?{ok:true,event:e.event}:(i.status=e.reason==="VERSION_CONFLICT"?409:400,{ok:false,reason:e.reason,...e.reason==="VERSION_CONFLICT"?{current:e.current}:{errors:e.errors}})},snapshot:async t=>await l({action:"read",request:t})?{state:a.getUniverseState(),lastEventId:a.getLastEventId()}:(t.set.status=403,{ok:false,reason:"FORBIDDEN"}),server:a}}exports.createZunoElysia=m;//# sourceMappingURL=index.js.map
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":["createZunoElysia","set","headers","query","sse","queue","resolve","unsubscribe","
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["createZunoElysia","options","server","createZunoServerState","authorize","request","set","headers","query","sse","queue","resolve","unsubscribe","event","rawLastEventId","lastEventId","missed","snapshot","heartbeat","r","body","incoming","result","applyStateEvent"],"mappings":"iFAgCO,SAASA,CAAAA,CAAiBC,EAAmC,EAAC,CAAG,CACvE,IAAMC,CAAAA,CAASD,EAAQ,MAAA,EAAUE,4BAAAA,EAAsB,CACjDC,CAAAA,CAAYH,EAAQ,SAAA,GAAc,IAAM,MAC9C,OAAO,CASN,IAAK,gBAAiBI,CAAAA,CAAc,CACnC,GAAM,CAAE,GAAA,CAAAC,CAAAA,CAAK,QAAAC,CAAAA,CAAS,KAAA,CAAAC,CAAM,CAAA,CAAIH,CAAAA,CAChC,GAAI,CAAE,MAAMD,CAAAA,CAAU,CAAE,OAAQ,MAAA,CAAQ,OAAA,CAAAC,CAAQ,CAAC,CAAA,CAAI,CACpDC,CAAAA,CAAI,OAAS,GAAA,CACb,MACD,CACAA,CAAAA,CAAI,OAAA,CAAQ,cAAc,CAAA,CAAI,mBAAA,CAC9BA,CAAAA,CAAI,OAAA,CAAQ,eAAe,CAAA,CAAI,UAAA,CAC/BA,EAAI,OAAA,CAAQ,UAAA,CAAa,aAEzB,MAAMG,UAAAA,CAAI,CAAE,IAAA,CAAM,aAAc,CAAC,CAAA,CAIjC,IAAMC,EAAe,EAAC,CAClBC,EAA8D,IAAA,CAE5DC,CAAAA,CAAcV,CAAAA,CAAO,sBAAA,CACzBW,GAA0B,CAC1BH,CAAAA,CAAM,KACLD,UAAAA,CAAI,CACH,GAAI,MAAA,CAAO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,CACD,EAYA,IAAA,CAAM,MAAOP,GAAiB,CAC7B,GAAM,CAAE,IAAA,CAAAe,EAAM,GAAA,CAAAd,CAAI,EAAID,CAAAA,CAChBgB,CAAAA,CAAWD,EACjB,GAAI,CAAE,MAAMhB,CAAAA,CAAU,CAAE,OAAQ,OAAA,CAAS,OAAA,CAAAC,EAAS,KAAA,CAAOgB,CAAS,CAAC,CAAA,CAClE,OAAAf,CAAAA,CAAI,MAAA,CAAS,IACN,CAAE,EAAA,CAAI,MAAO,MAAA,CAAQ,WAAY,EAEzC,IAAMgB,CAAAA,CAASC,sBAAAA,CAAgBF,CAAAA,CAAUnB,CAAM,CAAA,CAE/C,OAAKoB,EAAO,EAAA,CAWL,CAAE,GAAI,IAAA,CAAM,KAAA,CAAOA,CAAAA,CAAO,KAAM,GAVtChB,CAAAA,CAAI,MAAA,CAASgB,EAAO,MAAA,GAAW,kBAAA,CAAqB,IAAM,GAAA,CACnD,CACN,GAAI,KAAA,CACJ,MAAA,CAAQA,EAAO,MAAA,CACf,GAAIA,EAAO,MAAA,GAAW,kBAAA,CACnB,CAAE,OAAA,CAASA,CAAAA,CAAO,OAAQ,CAAA,CAC1B,CAAE,MAAA,CAAQA,CAAAA,CAAO,MAAO,CAC5B,CAAA,CAIF,EASA,QAAA,CAAU,MAAOjB,CAAAA,EACV,MAAMD,EAAU,CAAE,MAAA,CAAQ,OAAQ,OAAA,CAAAC,CAAQ,CAAC,CAAA,CAI1C,CACN,KAAA,CAAOH,CAAAA,CAAO,kBAAiB,CAC/B,WAAA,CAAaA,EAAO,cAAA,EACrB,GANCG,CAAAA,CAAQ,GAAA,CAAI,OAAS,GAAA,CACd,CAAE,GAAI,KAAA,CAAO,MAAA,CAAQ,WAAY,CAAA,CAAA,CAO1C,MAAA,CAAAH,CACD,CACD","file":"index.js","sourcesContent":["import type { ZunoStateEvent } from \"@iadev93/zuno\";\nimport {\n\tapplyStateEvent,\n\tcreateZunoServerState,\n\ttype ZunoServerState,\n} from \"@iadev93/zuno/server\";\nimport { sse } from \"elysia\";\n\n/**\n * Creates a Zuno Elysia instance.\n * @returns {Object} An object with the following properties:\n * - sse: An async generator function that handles SSE connections for Elysia.\n * @property {Object} set - Elysia response object.\n * @property {Object} headers - Elysia request headers.\n * @property {Object} query - Elysia request query parameters.\n * - sync: A function that handles sync POST requests for Elysia.\n * @property {Object} body - Elysia request body.\n * @property {Object} set - Elysia response object.\n * - snapshot: A function that handles snapshot GET requests for Elysia.\n * - snapshot: A function that handles snapshot GET requests for Elysia.\n */\nexport type CreateZunoElysiaOptions = {\n\t/** Isolated authoritative server state. A new instance is created by default. */\n\tserver?: ZunoServerState;\n\t/** Provider-agnostic authorization hook for snapshots, SSE, and mutations. */\n\tauthorize?: (context: {\n\t\taction: \"read\" | \"write\";\n\t\trequest: unknown;\n\t\tevent?: unknown;\n\t}) => boolean | Promise<boolean>;\n};\n\nexport function createZunoElysia(options: CreateZunoElysiaOptions = {}) {\n\tconst server = options.server ?? createZunoServerState();\n\tconst authorize = options.authorize ?? (() => true);\n\treturn {\n\t\t/**\n\t\t * Handles SSE connections for Elysia using an async generator.\n\t\t * @param {Object} param - Elysia request object.\n\t\t * @param {Object} param.set - Elysia response object.\n\t\t * @param {Object} param.headers - Elysia request headers.\n\t\t * @param {Object} param.query - Elysia request query parameters.\n\t\t */\n\t\t// biome-ignore lint/suspicious/noExplicitAny: Elysia request object\n\t\tsse: async function* (request: any) {\n\t\t\tconst { set, headers, query } = request;\n\t\t\tif (!(await authorize({ action: \"read\", request }))) {\n\t\t\t\tset.status = 403;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tset.headers[\"Content-Type\"] = \"text/event-stream\";\n\t\t\tset.headers[\"Cache-Control\"] = \"no-cache\";\n\t\t\tset.headers.Connection = \"keep-alive\";\n\n\t\t\tyield sse({ data: \": connected\" });\n\n\t\t\t// 1. Subscribe FIRST to avoid missing events during snapshot/missed-events retrieval\n\t\t\t// biome-ignore lint/suspicious/noExplicitAny: queue of any SSE events\n\t\t\tconst queue: any[] = [];\n\t\t\tlet resolve: ((value: void | PromiseLike<void>) => void) | null = null;\n\n\t\t\tconst unsubscribe = server.subscribeToStateEvents(\n\t\t\t\t(event: ZunoStateEvent) => {\n\t\t\t\t\tqueue.push(\n\t\t\t\t\t\tsse({\n\t\t\t\t\t\t\tid: String(event.eventId),\n\t\t\t\t\t\t\tevent: \"state\",\n\t\t\t\t\t\t\tdata: JSON.stringify(event),\n\t\t\t\t\t\t}),\n\t\t\t\t\t);\n\t\t\t\t\tif (resolve) {\n\t\t\t\t\t\tresolve();\n\t\t\t\t\t\tresolve = null;\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t);\n\n\t\t\t// 2. Decide if we send snapshot or missed events\n\t\t\tconst rawLastEventId = headers[\"last-event-id\"] || query?.lastEventId;\n\t\t\tconst lastEventId = Number.parseInt(rawLastEventId || \"0\", 10) || 0;\n\n\t\t\tif (lastEventId > 0) {\n\t\t\t\tconst missed = server.getEventsAfter(lastEventId);\n\t\t\t\tfor (const event of missed) {\n\t\t\t\t\tyield sse({\n\t\t\t\t\t\tid: String(event.eventId),\n\t\t\t\t\t\tevent: \"state\",\n\t\t\t\t\t\tdata: JSON.stringify(event),\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tconst snapshot = server.getUniverseState();\n\t\t\t\tyield sse({\n\t\t\t\t\tevent: \"snapshot\",\n\t\t\t\t\tdata: JSON.stringify(snapshot),\n\t\t\t\t});\n\t\t\t}\n\n\t\t\t// 3. Heartbeat interval\n\t\t\tconst heartbeat = setInterval(() => {\n\t\t\t\tqueue.push(sse({ data: `: ping ${Date.now()}` }));\n\t\t\t\tif (resolve) {\n\t\t\t\t\tresolve();\n\t\t\t\t\tresolve = null;\n\t\t\t\t}\n\t\t\t}, 15000);\n\n\t\t\ttry {\n\t\t\t\twhile (true) {\n\t\t\t\t\tif (queue.length === 0) {\n\t\t\t\t\t\tawait new Promise<void>((r) => {\n\t\t\t\t\t\t\tresolve = r;\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\twhile (queue.length > 0) {\n\t\t\t\t\t\t// biome-ignore lint/style/noNonNullAssertion: queue is checked for length > 0\n\t\t\t\t\t\tyield queue.shift()!;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} finally {\n\t\t\t\tclearInterval(heartbeat);\n\t\t\t\tunsubscribe();\n\t\t\t}\n\t\t},\n\n\t\t/**\n\t\t * Handles sync POST requests for Elysia.\n\t\t * @param {Object} param - Elysia request object.\n\t\t * @param {Object} param.body - Elysia request body.\n\t\t * @param {Object} param.set - Elysia response object.\n\t\t * @returns {Object} An object with the following properties:\n\t\t * - ok: A boolean indicating whether the sync was successful.\n\t\t * - event: The event that was applied to the universe.\n\t\t */\n\t\t// biome-ignore lint/suspicious/noExplicitAny: Elysia request body\n\t\tsync: async (request: any) => {\n\t\t\tconst { body, set } = request;\n\t\t\tconst incoming = body as ZunoStateEvent;\n\t\t\tif (!(await authorize({ action: \"write\", request, event: incoming }))) {\n\t\t\t\tset.status = 403;\n\t\t\t\treturn { ok: false, reason: \"FORBIDDEN\" };\n\t\t\t}\n\t\t\tconst result = applyStateEvent(incoming, server);\n\n\t\t\tif (!result.ok) {\n\t\t\t\tset.status = result.reason === \"VERSION_CONFLICT\" ? 409 : 400;\n\t\t\t\treturn {\n\t\t\t\t\tok: false,\n\t\t\t\t\treason: result.reason,\n\t\t\t\t\t...(result.reason === \"VERSION_CONFLICT\"\n\t\t\t\t\t\t? { current: result.current }\n\t\t\t\t\t\t: { errors: result.errors }),\n\t\t\t\t};\n\t\t\t}\n\n\t\t\treturn { ok: true, event: result.event };\n\t\t},\n\n\t\t/**\n\t\t * Handles snapshot GET requests for Elysia.\n\t\t * @returns {Object} An object with the following properties:\n\t\t * - state: The current state of the universe.\n\t\t * - version: The version of the universe.\n\t\t * - lastEventId: The ID of the last event in the universe.\n\t\t */\n\t\tsnapshot: async (request: { set: { status?: number } }) => {\n\t\t\tif (!(await authorize({ action: \"read\", request }))) {\n\t\t\t\trequest.set.status = 403;\n\t\t\t\treturn { ok: false, reason: \"FORBIDDEN\" };\n\t\t\t}\n\t\t\treturn {\n\t\t\t\tstate: server.getUniverseState(),\n\t\t\t\tlastEventId: server.getLastEventId(),\n\t\t\t};\n\t\t},\n\t\tserver,\n\t};\n}\n"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {createZunoServerState,applyStateEvent}from'@iadev93/zuno/server';import {sse}from'elysia';function m(v={}){let a=v.server??createZunoServerState(),l=v.authorize??(()=>true);return {sse:async function*(t){let{set:r,headers:i,query:u}=t;if(!await l({action:"read",request:t})){r.status=403;return}r.headers["Content-Type"]="text/event-stream",r.headers["Cache-Control"]="no-cache",r.headers.Connection="keep-alive",yield sse({data:": connected"});let e=[],s=null,y=a.subscribeToStateEvents(n=>{e.push(sse({id:String(n.eventId),event:"state",data:JSON.stringify(n)})),s&&(s(),s=null);}),f=i["last-event-id"]||u?.lastEventId,c=Number.parseInt(f||"0",10)||0;if(c>0){let n=a.getEventsAfter(c);for(let d of n)yield sse({id:String(d.eventId),event:"state",data:JSON.stringify(d)});}else {let n=a.getUniverseState();yield sse({event:"snapshot",data:JSON.stringify(n)});}let h=setInterval(()=>{e.push(sse({data:`: ping ${Date.now()}`})),s&&(s(),s=null);},15e3);try{for(;;)for(e.length===0&&await new Promise(n=>{s=n;});e.length>0;)yield e.shift();}finally{clearInterval(h),y();}},sync:async t=>{let{body:r,set:i}=t,u=r;if(!await l({action:"write",request:t,event:u}))return i.status=403,{ok:false,reason:"FORBIDDEN"};let e=applyStateEvent(u,a);return e.ok?{ok:true,event:e.event}:(i.status=e.reason==="VERSION_CONFLICT"?409:400,{ok:false,reason:e.reason,...e.reason==="VERSION_CONFLICT"?{current:e.current}:{errors:e.errors}})},snapshot:async t=>await l({action:"read",request:t})?{state:a.getUniverseState(),lastEventId:a.getLastEventId()}:(t.set.status=403,{ok:false,reason:"FORBIDDEN"}),server:a}}export{m as createZunoElysia};//# sourceMappingURL=index.mjs.map
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":["createZunoElysia","set","headers","query","sse","queue","resolve","unsubscribe","
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["createZunoElysia","options","server","createZunoServerState","authorize","request","set","headers","query","sse","queue","resolve","unsubscribe","event","rawLastEventId","lastEventId","missed","snapshot","heartbeat","r","body","incoming","result","applyStateEvent"],"mappings":"kGAgCO,SAASA,CAAAA,CAAiBC,EAAmC,EAAC,CAAG,CACvE,IAAMC,CAAAA,CAASD,EAAQ,MAAA,EAAUE,qBAAAA,EAAsB,CACjDC,CAAAA,CAAYH,EAAQ,SAAA,GAAc,IAAM,MAC9C,OAAO,CASN,IAAK,gBAAiBI,CAAAA,CAAc,CACnC,GAAM,CAAE,GAAA,CAAAC,CAAAA,CAAK,QAAAC,CAAAA,CAAS,KAAA,CAAAC,CAAM,CAAA,CAAIH,CAAAA,CAChC,GAAI,CAAE,MAAMD,CAAAA,CAAU,CAAE,OAAQ,MAAA,CAAQ,OAAA,CAAAC,CAAQ,CAAC,CAAA,CAAI,CACpDC,CAAAA,CAAI,OAAS,GAAA,CACb,MACD,CACAA,CAAAA,CAAI,OAAA,CAAQ,cAAc,CAAA,CAAI,mBAAA,CAC9BA,CAAAA,CAAI,OAAA,CAAQ,eAAe,CAAA,CAAI,UAAA,CAC/BA,EAAI,OAAA,CAAQ,UAAA,CAAa,aAEzB,MAAMG,GAAAA,CAAI,CAAE,IAAA,CAAM,aAAc,CAAC,CAAA,CAIjC,IAAMC,EAAe,EAAC,CAClBC,EAA8D,IAAA,CAE5DC,CAAAA,CAAcV,CAAAA,CAAO,sBAAA,CACzBW,GAA0B,CAC1BH,CAAAA,CAAM,KACLD,GAAAA,CAAI,CACH,GAAI,MA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n} from \"@iadev93/zuno/server\";\nimport { sse } from \"elysia\";\n\n/**\n * Creates a Zuno Elysia instance.\n * @returns {Object} An object with the following properties:\n * - sse: An async generator function that handles SSE connections for Elysia.\n * @property {Object} set - Elysia response object.\n * @property {Object} headers - Elysia request headers.\n * @property {Object} query - Elysia request query parameters.\n * - sync: A function that handles sync POST requests for Elysia.\n * @property {Object} body - Elysia request body.\n * @property {Object} set - Elysia response object.\n * - snapshot: A function that handles snapshot GET requests for Elysia.\n * - snapshot: A function that handles snapshot GET requests for Elysia.\n */\nexport type CreateZunoElysiaOptions = {\n\t/** Isolated authoritative server state. A new instance is created by default. */\n\tserver?: ZunoServerState;\n\t/** Provider-agnostic authorization hook for snapshots, SSE, and mutations. */\n\tauthorize?: (context: {\n\t\taction: \"read\" | \"write\";\n\t\trequest: unknown;\n\t\tevent?: unknown;\n\t}) => boolean | Promise<boolean>;\n};\n\nexport function createZunoElysia(options: CreateZunoElysiaOptions = {}) {\n\tconst server = options.server ?? createZunoServerState();\n\tconst authorize = options.authorize ?? (() => true);\n\treturn {\n\t\t/**\n\t\t * Handles SSE connections for Elysia using an async generator.\n\t\t * @param {Object} param - Elysia request object.\n\t\t * @param {Object} param.set - Elysia response object.\n\t\t * @param {Object} param.headers - Elysia request headers.\n\t\t * @param {Object} param.query - Elysia request query parameters.\n\t\t */\n\t\t// biome-ignore lint/suspicious/noExplicitAny: Elysia request object\n\t\tsse: async function* (request: any) {\n\t\t\tconst { set, headers, query } = request;\n\t\t\tif (!(await authorize({ action: \"read\", request }))) {\n\t\t\t\tset.status = 403;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tset.headers[\"Content-Type\"] = \"text/event-stream\";\n\t\t\tset.headers[\"Cache-Control\"] = \"no-cache\";\n\t\t\tset.headers.Connection = \"keep-alive\";\n\n\t\t\tyield sse({ data: \": connected\" });\n\n\t\t\t// 1. Subscribe FIRST to avoid missing events during snapshot/missed-events retrieval\n\t\t\t// biome-ignore lint/suspicious/noExplicitAny: queue of any SSE events\n\t\t\tconst queue: any[] = [];\n\t\t\tlet resolve: ((value: void | PromiseLike<void>) => void) | null = null;\n\n\t\t\tconst unsubscribe = server.subscribeToStateEvents(\n\t\t\t\t(event: ZunoStateEvent) => {\n\t\t\t\t\tqueue.push(\n\t\t\t\t\t\tsse({\n\t\t\t\t\t\t\tid: String(event.eventId),\n\t\t\t\t\t\t\tevent: \"state\",\n\t\t\t\t\t\t\tdata: JSON.stringify(event),\n\t\t\t\t\t\t}),\n\t\t\t\t\t);\n\t\t\t\t\tif (resolve) {\n\t\t\t\t\t\tresolve();\n\t\t\t\t\t\tresolve = null;\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t);\n\n\t\t\t// 2. Decide if we send snapshot or missed events\n\t\t\tconst rawLastEventId = headers[\"last-event-id\"] || query?.lastEventId;\n\t\t\tconst lastEventId = Number.parseInt(rawLastEventId || \"0\", 10) || 0;\n\n\t\t\tif (lastEventId > 0) {\n\t\t\t\tconst missed = server.getEventsAfter(lastEventId);\n\t\t\t\tfor (const event of missed) {\n\t\t\t\t\tyield sse({\n\t\t\t\t\t\tid: String(event.eventId),\n\t\t\t\t\t\tevent: \"state\",\n\t\t\t\t\t\tdata: JSON.stringify(event),\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tconst snapshot = server.getUniverseState();\n\t\t\t\tyield sse({\n\t\t\t\t\tevent: \"snapshot\",\n\t\t\t\t\tdata: JSON.stringify(snapshot),\n\t\t\t\t});\n\t\t\t}\n\n\t\t\t// 3. Heartbeat interval\n\t\t\tconst heartbeat = setInterval(() => {\n\t\t\t\tqueue.push(sse({ data: `: ping ${Date.now()}` }));\n\t\t\t\tif (resolve) {\n\t\t\t\t\tresolve();\n\t\t\t\t\tresolve = null;\n\t\t\t\t}\n\t\t\t}, 15000);\n\n\t\t\ttry {\n\t\t\t\twhile (true) {\n\t\t\t\t\tif (queue.length === 0) {\n\t\t\t\t\t\tawait new Promise<void>((r) => {\n\t\t\t\t\t\t\tresolve = r;\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\twhile (queue.length > 0) {\n\t\t\t\t\t\t// biome-ignore lint/style/noNonNullAssertion: queue is checked for length > 0\n\t\t\t\t\t\tyield queue.shift()!;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} finally {\n\t\t\t\tclearInterval(heartbeat);\n\t\t\t\tunsubscribe();\n\t\t\t}\n\t\t},\n\n\t\t/**\n\t\t * Handles sync POST requests for Elysia.\n\t\t * @param {Object} param - Elysia request object.\n\t\t * @param {Object} param.body - Elysia request body.\n\t\t * @param {Object} param.set - Elysia response object.\n\t\t * @returns {Object} An object with the following properties:\n\t\t * - ok: A boolean indicating whether the sync was successful.\n\t\t * - event: The event that was applied to the universe.\n\t\t */\n\t\t// biome-ignore lint/suspicious/noExplicitAny: Elysia request body\n\t\tsync: async (request: any) => {\n\t\t\tconst { body, set } = request;\n\t\t\tconst incoming = body as ZunoStateEvent;\n\t\t\tif (!(await authorize({ action: \"write\", request, event: incoming }))) {\n\t\t\t\tset.status = 403;\n\t\t\t\treturn { ok: false, reason: \"FORBIDDEN\" };\n\t\t\t}\n\t\t\tconst result = applyStateEvent(incoming, server);\n\n\t\t\tif (!result.ok) {\n\t\t\t\tset.status = result.reason === \"VERSION_CONFLICT\" ? 409 : 400;\n\t\t\t\treturn {\n\t\t\t\t\tok: false,\n\t\t\t\t\treason: result.reason,\n\t\t\t\t\t...(result.reason === \"VERSION_CONFLICT\"\n\t\t\t\t\t\t? { current: result.current }\n\t\t\t\t\t\t: { errors: result.errors }),\n\t\t\t\t};\n\t\t\t}\n\n\t\t\treturn { ok: true, event: result.event };\n\t\t},\n\n\t\t/**\n\t\t * Handles snapshot GET requests for Elysia.\n\t\t * @returns {Object} An object with the following properties:\n\t\t * - state: The current state of the universe.\n\t\t * - version: The version of the universe.\n\t\t * - lastEventId: The ID of the last event in the universe.\n\t\t */\n\t\tsnapshot: async (request: { set: { status?: number } }) => {\n\t\t\tif (!(await authorize({ action: \"read\", request }))) {\n\t\t\t\trequest.set.status = 403;\n\t\t\t\treturn { ok: false, reason: \"FORBIDDEN\" };\n\t\t\t}\n\t\t\treturn {\n\t\t\t\tstate: server.getUniverseState(),\n\t\t\t\tlastEventId: server.getLastEventId(),\n\t\t\t};\n\t\t},\n\t\tserver,\n\t};\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iadev93/zuno-elysia",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -17,13 +17,13 @@
|
|
|
17
17
|
},
|
|
18
18
|
"peerDependencies": {
|
|
19
19
|
"elysia": ">=1.0.0",
|
|
20
|
-
"@iadev93/zuno": "0.0.
|
|
20
|
+
"@iadev93/zuno": "0.0.11"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"elysia": "^1.2.10",
|
|
24
24
|
"tsup": "^8.5.1",
|
|
25
25
|
"typescript": "^5.9.3",
|
|
26
|
-
"@iadev93/zuno": "0.0.
|
|
26
|
+
"@iadev93/zuno": "0.0.11"
|
|
27
27
|
},
|
|
28
28
|
"author": "Ibrahim Aftab",
|
|
29
29
|
"license": "MIT",
|