zodbus 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,7 @@
1
+ Copyright 2023 Andrei Neculaesei
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";var h=Object.defineProperty;var v=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var k=Object.prototype.hasOwnProperty;var $=(i,t)=>{for(var u in t)h(i,u,{get:t[u],enumerable:!0})},O=(i,t,u,c)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of x(t))!k.call(i,s)&&s!==u&&h(i,s,{get:()=>t[s],enumerable:!(c=v(t,s))||c.enumerable});return i};var E=i=>O(h({},"__esModule",{value:!0}),i);var Z={};$(Z,{create:()=>R});module.exports=E(Z);var T="[zodbus] ";var d=class extends Error{constructor(t){super(`${T}${t}`),this.name="ValidationError"}},K=class extends Error{constructor(t){super(`${T}${t}`),this.name="RuntimeError"}};var P=(i,t="")=>{let u=[];for(let[c,s]of Object.entries(i)){let a=t?`${t}.${c}`:c;typeof s.parse=="function"?u.push(a):u.push(...P(s,a))}return u},m=(i,t="")=>{let u=[];for(let[c,s]of Object.entries(i)){let a=t?`${t}.${c}`:c,f=t?`${t}.*`:"*";typeof s.parse=="function"?u.push(a,f):u.push(...m(s,a),...m(s,f))}return u},w=i=>{let t={},u=P(i),c=m(i);for(let s of c){let a=s.split(".");t[s]=u.filter(f=>{let b=f.split(".");if(b.length!==a.length)return!1;for(let l=0;l<b.length;l++)if(a[l]!=="*"&&b[l]!==a[l])return!1;return!0})}return t};function R({schema:i,validate:t=!0}){let u=w(i),c=Array.from(new Set(Object.values(u).flat())),s=new Map,a=e=>{if(e==="*")return Array.from(s.values());let n=[],o=u[e];if(!o)throw new d(`Invalid event: "${e}"`);for(let r of o){let p=s.get(r);p&&n.push(p)}return n},f=(e,n)=>{let o=e.split("."),r=i;for(let p of o){if(typeof r[p]=="undefined")throw new d(`Invalid event: "${e}". Could not resolve "${p}" fragment.`);r=r[p]}if(typeof r.parse=="function")r.parse(n);else throw new d(`Reached invalid payload schema for: "${e}"`)},b=(e,n)=>{let o=a(e);for(let r of o)typeof n=="undefined"?r.clear():r.delete(n)},l=(e,n)=>{if(typeof n!="function")throw new d(`Invalid listener for event: "${e}". Expected function, got ${typeof n}`);let o=e==="*"?c:u[e];if(!o)throw new d(`Invalid event: "${e}"`);for(let r of o)s.has(r)||s.set(r,new Set),s.get(r).add(n);return{event:e,listener:n,unsubscribe:()=>b(e,n)}};return{publish:(e,n)=>{if(t&&f(e,n),!!s.has(e))for(let o of s.get(e))o(n,e)},subscribe:l,subscribeOnce:(e,n)=>{let o=(r,p)=>{n(r,p),b(e,o)};return l(e,o)},unsubscribe:b,getEventNames:()=>c,getListeners:e=>{let n=[],o=e?a(e):Array.from(s.values());for(let r of o)for(let p of r)n.push(p);return n},waitFor:(e,n={})=>{let{timeout:o=5e3,filter:r}=n;return new Promise((p,L)=>{let y,S=g=>{r&&!r(g)||(b(e,S),p(g),clearTimeout(y))};l(e,S),o&&(y=setTimeout(()=>{b(e,S),L(new K(`Timeout waiting for event: "${e}"`))},o))})}}}
2
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/index.ts", "../src/constants.ts", "../src/errors.ts", "../src/utils/schema.ts", "../src/bus.ts"],
4
+ "sourcesContent": ["export * from \"./bus\";\n", "export const errorPrefix = \"[zodbus] \";\n", "import { errorPrefix } from \"./constants\";\n\nexport class ValidationError extends Error {\n constructor(message: string) {\n super(`${errorPrefix}${message}`);\n this.name = \"ValidationError\";\n }\n}\n\nexport class RuntimeError extends Error {\n constructor(message: string) {\n super(`${errorPrefix}${message}`);\n this.name = \"RuntimeError\";\n }\n}\n", "import { ZodType } from \"zod\";\nimport { Schema } from \"../types\";\n\nexport const hasWildcard = (path: string): boolean => path.split(\".\").includes(\"*\");\n\nexport const getPublishPaths = (schema: Schema | ZodType, prefix = \"\"): string[] => {\n const paths: string[] = [];\n for (const [k, v] of Object.entries(schema)) {\n const newPrefix = prefix ? `${prefix}.${k}` : k;\n if (typeof v.parse === \"function\") {\n paths.push(newPrefix);\n } else {\n paths.push(...getPublishPaths(v, newPrefix));\n }\n }\n return paths;\n};\n\nexport const getSubscribePaths = (schema: Schema | ZodType, prefix = \"\"): string[] => {\n const paths: string[] = [];\n for (const [k, v] of Object.entries(schema)) {\n const newPrefix = prefix ? `${prefix}.${k}` : k;\n const newWildcardPrefix = prefix ? `${prefix}.*` : \"*\";\n if (typeof v.parse === \"function\") {\n paths.push(newPrefix, newWildcardPrefix);\n } else {\n paths.push(...getSubscribePaths(v, newPrefix), ...getSubscribePaths(v, newWildcardPrefix));\n }\n }\n return paths;\n};\n\nexport const getSubPubPathMap = (schema: Schema | ZodType): Record<string, string[]> => {\n const map: Record<string, string[]> = {};\n const publishPaths = getPublishPaths(schema);\n const subscribePaths = getSubscribePaths(schema);\n\n for (const subscribePath of subscribePaths) {\n const subscribeParts = subscribePath.split(\".\");\n map[subscribePath] = publishPaths.filter((publishPath) => {\n const publishParts = publishPath.split(\".\");\n if (publishParts.length !== subscribeParts.length) return false;\n for (let i = 0; i < publishParts.length; i++) {\n if (subscribeParts[i] !== \"*\" && publishParts[i] !== subscribeParts[i]) return false;\n }\n return true;\n });\n }\n\n return map;\n};\n", "import { ZodType } from \"zod\";\nimport { RuntimeError, ValidationError } from \"./errors\";\nimport type { PublishKey, Schema, SubscriptionKey, SubscriptionListenerPayloads, SubscriptionListeners } from \"./types\";\nimport { getSubPubPathMap } from \"./utils/schema\";\n\ntype BusOptions<T extends Schema> = {\n schema: T;\n validate?: boolean;\n};\n\ninterface Bus<T extends Schema> {\n publish: <K extends PublishKey<T>>(event: K, data: SubscriptionListenerPayloads<T>[K]) => void;\n subscribe: <K extends SubscriptionKey<T>>(\n event: K,\n listener: SubscriptionListeners<T>[K]\n ) => { event: K; listener: SubscriptionListeners<T>[K]; unsubscribe: () => void };\n subscribeOnce: <K extends SubscriptionKey<T>>(\n event: K,\n listener: SubscriptionListeners<T>[K]\n ) => { event: K; listener: SubscriptionListeners<T>[K]; unsubscribe: () => void };\n unsubscribe: <K extends SubscriptionKey<T>>(event: K, listener?: SubscriptionListeners<T>[K]) => void;\n getEventNames: () => string[];\n getListeners: <K extends SubscriptionKey<T>>(event?: K) => ((data: unknown, eventName: string) => void)[];\n waitFor: <K extends SubscriptionKey<T>>(\n event: K,\n options?: { timeout?: number; filter?: (data: SubscriptionListenerPayloads<T>[K]) => boolean }\n ) => Promise<SubscriptionListenerPayloads<T>[K]>;\n}\n\nfunction create<T extends Schema>({ schema, validate = true }: BusOptions<T>): Bus<T> {\n const subPubPathMap = getSubPubPathMap(schema) as Record<SubscriptionKey<T>, PublishKey<T>[]>;\n const eventNames = Array.from(new Set(Object.values(subPubPathMap).flat())) as PublishKey<T>[];\n const listeners: Map<PublishKey<T>, Set<unknown>> = new Map();\n\n const getListenerSetsForSubscriptionKey = (event: SubscriptionKey<T>): Set<unknown>[] => {\n if (event === \"*\") return Array.from(listeners.values());\n const listenerSets: Set<unknown>[] = [];\n const publishPaths = subPubPathMap[event];\n if (!publishPaths) throw new ValidationError(`Invalid event: \"${event}\"`);\n for (const publishPath of publishPaths) {\n const listenerSet = listeners.get(publishPath);\n if (listenerSet) listenerSets.push(listenerSet);\n }\n return listenerSets;\n };\n\n const validatePayloadOrPanic = (event: string, data: unknown): void => {\n const pathFragments = event.split(\".\");\n let currentSchema: ZodType | Schema = schema;\n for (const fragment of pathFragments) {\n if (typeof (currentSchema as Record<string, unknown>)[fragment] === \"undefined\") {\n throw new ValidationError(`Invalid event: \"${event}\". Could not resolve \"${fragment}\" fragment.`);\n }\n currentSchema = (currentSchema as Record<string, ZodType | Schema>)[fragment];\n }\n if (typeof currentSchema.parse === \"function\") {\n (currentSchema as ZodType).parse(data);\n } else {\n throw new ValidationError(`Reached invalid payload schema for: \"${event}\"`);\n }\n };\n\n /** Unsubscribe from an event. If no listener is provided, all listeners for the event will be removed.\n * @param event The event to unsubscribe from. Wildcards `foo.*.bar.*` are supported.\n * Using `*` will match any event on any level.\n * More specific wildcard patterns like `*.*` will only match events on that level.\n * @param listener The listener to remove. If no listener is provided, all listeners for the event will be removed.\n *\n * Examples:\n * - `unsubscribe(\"foo.bar\", listener)` will unsubscribe the listener from `foo.bar`\n * - `unsubscribe(\"foo.*\", listener)` will unsubscribe the listener from all events under `foo`\n * - `unsubscribe(\"*\", listener)` will unsubscribe the listener from all events\n * - `unsubscribe(\"*\")` will unsubscribe all listeners from all events\n * - `unsubscribe(\"*.*\")` will unsubscribe all listeners from all events on the second level\n */\n const unsubscribe = <K extends SubscriptionKey<T>>(event: K, listener?: SubscriptionListeners<T>[K]): void => {\n const eventListeners = getListenerSetsForSubscriptionKey(event);\n for (const listenerSet of eventListeners) {\n if (typeof listener === \"undefined\") {\n listenerSet.clear();\n } else {\n listenerSet.delete(listener);\n }\n }\n };\n\n /** Subscribe to an event. Returns an object with the event name, listener, and an unsubscribe function.\n * @param event The event to subscribe to. Wildcards `foo.*.bar.*` are supported.\n * Using `*` will match any event on any level.\n * More specific wildcard patterns like `*.*` will only match events on that level.\n * @param listener The listener to call when the event is published.\n * @returns A subscription object with the event name, listener, and an unsubscribe function.\n */\n const subscribe = <K extends SubscriptionKey<T>>(\n event: K,\n listener: SubscriptionListeners<T>[K]\n ): {\n event: K;\n listener: SubscriptionListeners<T>[K];\n unsubscribe: () => void;\n } => {\n if (typeof listener !== \"function\") {\n throw new ValidationError(`Invalid listener for event: \"${event}\". Expected function, got ${typeof listener}`);\n }\n const publishPaths = event === \"*\" ? eventNames : subPubPathMap[event];\n if (!publishPaths) throw new ValidationError(`Invalid event: \"${event}\"`);\n for (const publishPath of publishPaths) {\n if (!listeners.has(publishPath)) listeners.set(publishPath, new Set());\n listeners.get(publishPath)!.add(listener);\n }\n return { event, listener, unsubscribe: () => unsubscribe(event, listener) };\n };\n\n /** Subscribe to an event once. The listener will be unsubscribed after the first time it is called.\n * You cannot cancel this subscription using unsubscribe() with the original listener.\n * Use the returned unsubscribe function / listener instead.\n * @param event The event to subscribe to. Wildcards `foo.*.bar.*` are supported.\n * Using `*` will match any event on any level.\n * More specific wildcard patterns like `*.*` will only match events on that level.\n * @param listener The listener to call when the event is published.\n * @returns A subscription object with the event name, listener, and an unsubscribe function.\n * */\n const subscribeOnce = <K extends SubscriptionKey<T>>(event: K, listener: SubscriptionListeners<T>[K]) => {\n const wrappedListener = (data: unknown, eventName: string) => {\n listener(data, eventName);\n unsubscribe(event, wrappedListener as unknown as SubscriptionListeners<T>[K]);\n };\n return subscribe(event, wrappedListener as unknown as SubscriptionListeners<T>[K]);\n };\n\n /** Publish an event. All listeners for the event will be called with the provided data.\n * @param event The event to publish.\n * @param data The data to pass to the listeners.*/\n const publish = <K extends PublishKey<T>>(event: K, data: SubscriptionListenerPayloads<T>[K]) => {\n if (validate) validatePayloadOrPanic(event, data);\n if (!listeners.has(event)) return;\n for (const listener of listeners.get(event)!) {\n (listener as SubscriptionListeners<T>[K])(data, event);\n }\n };\n\n /** Returns a list of all the event names.\n * @returns An array of event names. */\n const getEventNames = () => eventNames;\n\n /** Get the list of listeners for an event or all listeners if no event is provided.\n * @param event The event to get listeners for.\n * @returns An array of listeners for the event. */\n const getListeners = <K extends SubscriptionKey<T>>(event?: K) => {\n const targetListeners: ((data: unknown, eventName: string) => void)[] = [];\n const targetListenerSets = event ? getListenerSetsForSubscriptionKey(event) : Array.from(listeners.values());\n for (const listenerSet of targetListenerSets) {\n for (const listener of listenerSet) {\n targetListeners.push(listener as (data: unknown, eventName: string) => void);\n }\n }\n return targetListeners;\n };\n\n /** Waits for an event to be published.\n * Returns a promise that resolves when the event is published.\n * @param event The event to wait for.\n * @param options.timeout The timeout in milliseconds. Defaults to 10000.\n * @param options.filter A function that returns true if the event should be accepted.\n * @returns A promise that resolves when the event is published with the data passed to the listener. */\n const waitFor = <K extends SubscriptionKey<T>>(\n event: K,\n options: { timeout?: number; filter?: (data: SubscriptionListenerPayloads<T>[K]) => boolean } = {}\n ) => {\n const { timeout = 5_000, filter } = options;\n return new Promise<SubscriptionListenerPayloads<T>[K]>((resolve, reject) => {\n let timeoutId: ReturnType<typeof setTimeout>;\n const listener = (data: unknown) => {\n if (filter && !filter(data as SubscriptionListenerPayloads<T>[K])) return;\n unsubscribe(event, listener as unknown as SubscriptionListeners<T>[K]);\n resolve(data as SubscriptionListenerPayloads<T>[K]);\n clearTimeout(timeoutId);\n };\n subscribe(event, listener as unknown as SubscriptionListeners<T>[K]);\n if (timeout) {\n timeoutId = setTimeout(() => {\n unsubscribe(event, listener as unknown as SubscriptionListeners<T>[K]);\n reject(new RuntimeError(`Timeout waiting for event: \"${event}\"`));\n }, timeout);\n }\n });\n };\n\n return {\n publish,\n subscribe,\n subscribeOnce,\n unsubscribe,\n getEventNames,\n getListeners,\n waitFor,\n };\n}\n\nexport { create };\n"],
5
+ "mappings": "yaAAA,IAAAA,EAAA,GAAA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AAM,CAAE,QAAAC,EAAU,IAAO,OAAAC,CAAO,EAAIF,EACpC,OAAO,IAAI,QAA4C,CAACG,EAASC,IAAW,CAC1E,IAAIC,EACEZ,EAAYL,GAAkB,CAC9Bc,GAAU,CAACA,EAAOd,CAA0C,IAChEI,EAAYX,EAAOY,CAAkD,EACrEU,EAAQf,CAA0C,EAClD,aAAaiB,CAAS,EACxB,EACAV,EAAUd,EAAOY,CAAkD,EAC/DQ,IACFI,EAAY,WAAW,IAAM,CAC3Bb,EAAYX,EAAOY,CAAkD,EACrEW,EAAO,IAAIE,EAAa,+BAA+BzB,IAAQ,CAAC,CAClE,EAAGoB,CAAO,EAEd,CAAC,CACH,CAUA,CACF",
6
+ "names": ["src_exports", "__export", "create", "__toCommonJS", "errorPrefix", "ValidationError", "message", "errorPrefix", "RuntimeError", "getPublishPaths", "schema", "prefix", "paths", "k", "v", "newPrefix", "getSubscribePaths", "newWildcardPrefix", "getSubPubPathMap", "map", "publishPaths", "subscribePaths", "subscribePath", "subscribeParts", "publishPath", "publishParts", "i", "create", "schema", "validate", "subPubPathMap", "getSubPubPathMap", "eventNames", "listeners", "getListenerSetsForSubscriptionKey", "event", "listenerSets", "publishPaths", "ValidationError", "publishPath", "listenerSet", "validatePayloadOrPanic", "data", "pathFragments", "currentSchema", "fragment", "unsubscribe", "listener", "eventListeners", "subscribe", "wrappedListener", "eventName", "targetListeners", "targetListenerSets", "options", "timeout", "filter", "resolve", "reject", "timeoutId", "RuntimeError"]
7
+ }
package/dist/index.mjs ADDED
@@ -0,0 +1,2 @@
1
+ var h="[zodbus] ";var d=class extends Error{constructor(i){super(`${h}${i}`),this.name="ValidationError"}},K=class extends Error{constructor(i){super(`${h}${i}`),this.name="RuntimeError"}};var g=(a,i="")=>{let o=[];for(let[p,r]of Object.entries(a)){let u=i?`${i}.${p}`:p;typeof r.parse=="function"?o.push(u):o.push(...g(r,u))}return o},T=(a,i="")=>{let o=[];for(let[p,r]of Object.entries(a)){let u=i?`${i}.${p}`:p,f=i?`${i}.*`:"*";typeof r.parse=="function"?o.push(u,f):o.push(...T(r,u),...T(r,f))}return o},P=a=>{let i={},o=g(a),p=T(a);for(let r of p){let u=r.split(".");i[r]=o.filter(f=>{let b=f.split(".");if(b.length!==u.length)return!1;for(let l=0;l<b.length;l++)if(u[l]!=="*"&&b[l]!==u[l])return!1;return!0})}return i};function I({schema:a,validate:i=!0}){let o=P(a),p=Array.from(new Set(Object.values(o).flat())),r=new Map,u=e=>{if(e==="*")return Array.from(r.values());let t=[],n=o[e];if(!n)throw new d(`Invalid event: "${e}"`);for(let s of n){let c=r.get(s);c&&t.push(c)}return t},f=(e,t)=>{let n=e.split("."),s=a;for(let c of n){if(typeof s[c]=="undefined")throw new d(`Invalid event: "${e}". Could not resolve "${c}" fragment.`);s=s[c]}if(typeof s.parse=="function")s.parse(t);else throw new d(`Reached invalid payload schema for: "${e}"`)},b=(e,t)=>{let n=u(e);for(let s of n)typeof t=="undefined"?s.clear():s.delete(t)},l=(e,t)=>{if(typeof t!="function")throw new d(`Invalid listener for event: "${e}". Expected function, got ${typeof t}`);let n=e==="*"?p:o[e];if(!n)throw new d(`Invalid event: "${e}"`);for(let s of n)r.has(s)||r.set(s,new Set),r.get(s).add(t);return{event:e,listener:t,unsubscribe:()=>b(e,t)}};return{publish:(e,t)=>{if(i&&f(e,t),!!r.has(e))for(let n of r.get(e))n(t,e)},subscribe:l,subscribeOnce:(e,t)=>{let n=(s,c)=>{t(s,c),b(e,n)};return l(e,n)},unsubscribe:b,getEventNames:()=>p,getListeners:e=>{let t=[],n=e?u(e):Array.from(r.values());for(let s of n)for(let c of s)t.push(c);return t},waitFor:(e,t={})=>{let{timeout:n=5e3,filter:s}=t;return new Promise((c,w)=>{let m,S=y=>{s&&!s(y)||(b(e,S),c(y),clearTimeout(m))};l(e,S),n&&(m=setTimeout(()=>{b(e,S),w(new K(`Timeout waiting for event: "${e}"`))},n))})}}}export{I as create};
2
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/constants.ts", "../src/errors.ts", "../src/utils/schema.ts", "../src/bus.ts"],
4
+ "sourcesContent": ["export const errorPrefix = \"[zodbus] \";\n", "import { errorPrefix } from \"./constants\";\n\nexport class ValidationError extends Error {\n constructor(message: string) {\n super(`${errorPrefix}${message}`);\n this.name = \"ValidationError\";\n }\n}\n\nexport class RuntimeError extends Error {\n constructor(message: string) {\n super(`${errorPrefix}${message}`);\n this.name = \"RuntimeError\";\n }\n}\n", "import { ZodType } from \"zod\";\nimport { Schema } from \"../types\";\n\nexport const hasWildcard = (path: string): boolean => path.split(\".\").includes(\"*\");\n\nexport const getPublishPaths = (schema: Schema | ZodType, prefix = \"\"): string[] => {\n const paths: string[] = [];\n for (const [k, v] of Object.entries(schema)) {\n const newPrefix = prefix ? `${prefix}.${k}` : k;\n if (typeof v.parse === \"function\") {\n paths.push(newPrefix);\n } else {\n paths.push(...getPublishPaths(v, newPrefix));\n }\n }\n return paths;\n};\n\nexport const getSubscribePaths = (schema: Schema | ZodType, prefix = \"\"): string[] => {\n const paths: string[] = [];\n for (const [k, v] of Object.entries(schema)) {\n const newPrefix = prefix ? `${prefix}.${k}` : k;\n const newWildcardPrefix = prefix ? `${prefix}.*` : \"*\";\n if (typeof v.parse === \"function\") {\n paths.push(newPrefix, newWildcardPrefix);\n } else {\n paths.push(...getSubscribePaths(v, newPrefix), ...getSubscribePaths(v, newWildcardPrefix));\n }\n }\n return paths;\n};\n\nexport const getSubPubPathMap = (schema: Schema | ZodType): Record<string, string[]> => {\n const map: Record<string, string[]> = {};\n const publishPaths = getPublishPaths(schema);\n const subscribePaths = getSubscribePaths(schema);\n\n for (const subscribePath of subscribePaths) {\n const subscribeParts = subscribePath.split(\".\");\n map[subscribePath] = publishPaths.filter((publishPath) => {\n const publishParts = publishPath.split(\".\");\n if (publishParts.length !== subscribeParts.length) return false;\n for (let i = 0; i < publishParts.length; i++) {\n if (subscribeParts[i] !== \"*\" && publishParts[i] !== subscribeParts[i]) return false;\n }\n return true;\n });\n }\n\n return map;\n};\n", "import { ZodType } from \"zod\";\nimport { RuntimeError, ValidationError } from \"./errors\";\nimport type { PublishKey, Schema, SubscriptionKey, SubscriptionListenerPayloads, SubscriptionListeners } from \"./types\";\nimport { getSubPubPathMap } from \"./utils/schema\";\n\ntype BusOptions<T extends Schema> = {\n schema: T;\n validate?: boolean;\n};\n\ninterface Bus<T extends Schema> {\n publish: <K extends PublishKey<T>>(event: K, data: SubscriptionListenerPayloads<T>[K]) => void;\n subscribe: <K extends SubscriptionKey<T>>(\n event: K,\n listener: SubscriptionListeners<T>[K]\n ) => { event: K; listener: SubscriptionListeners<T>[K]; unsubscribe: () => void };\n subscribeOnce: <K extends SubscriptionKey<T>>(\n event: K,\n listener: SubscriptionListeners<T>[K]\n ) => { event: K; listener: SubscriptionListeners<T>[K]; unsubscribe: () => void };\n unsubscribe: <K extends SubscriptionKey<T>>(event: K, listener?: SubscriptionListeners<T>[K]) => void;\n getEventNames: () => string[];\n getListeners: <K extends SubscriptionKey<T>>(event?: K) => ((data: unknown, eventName: string) => void)[];\n waitFor: <K extends SubscriptionKey<T>>(\n event: K,\n options?: { timeout?: number; filter?: (data: SubscriptionListenerPayloads<T>[K]) => boolean }\n ) => Promise<SubscriptionListenerPayloads<T>[K]>;\n}\n\nfunction create<T extends Schema>({ schema, validate = true }: BusOptions<T>): Bus<T> {\n const subPubPathMap = getSubPubPathMap(schema) as Record<SubscriptionKey<T>, PublishKey<T>[]>;\n const eventNames = Array.from(new Set(Object.values(subPubPathMap).flat())) as PublishKey<T>[];\n const listeners: Map<PublishKey<T>, Set<unknown>> = new Map();\n\n const getListenerSetsForSubscriptionKey = (event: SubscriptionKey<T>): Set<unknown>[] => {\n if (event === \"*\") return Array.from(listeners.values());\n const listenerSets: Set<unknown>[] = [];\n const publishPaths = subPubPathMap[event];\n if (!publishPaths) throw new ValidationError(`Invalid event: \"${event}\"`);\n for (const publishPath of publishPaths) {\n const listenerSet = listeners.get(publishPath);\n if (listenerSet) listenerSets.push(listenerSet);\n }\n return listenerSets;\n };\n\n const validatePayloadOrPanic = (event: string, data: unknown): void => {\n const pathFragments = event.split(\".\");\n let currentSchema: ZodType | Schema = schema;\n for (const fragment of pathFragments) {\n if (typeof (currentSchema as Record<string, unknown>)[fragment] === \"undefined\") {\n throw new ValidationError(`Invalid event: \"${event}\". Could not resolve \"${fragment}\" fragment.`);\n }\n currentSchema = (currentSchema as Record<string, ZodType | Schema>)[fragment];\n }\n if (typeof currentSchema.parse === \"function\") {\n (currentSchema as ZodType).parse(data);\n } else {\n throw new ValidationError(`Reached invalid payload schema for: \"${event}\"`);\n }\n };\n\n /** Unsubscribe from an event. If no listener is provided, all listeners for the event will be removed.\n * @param event The event to unsubscribe from. Wildcards `foo.*.bar.*` are supported.\n * Using `*` will match any event on any level.\n * More specific wildcard patterns like `*.*` will only match events on that level.\n * @param listener The listener to remove. If no listener is provided, all listeners for the event will be removed.\n *\n * Examples:\n * - `unsubscribe(\"foo.bar\", listener)` will unsubscribe the listener from `foo.bar`\n * - `unsubscribe(\"foo.*\", listener)` will unsubscribe the listener from all events under `foo`\n * - `unsubscribe(\"*\", listener)` will unsubscribe the listener from all events\n * - `unsubscribe(\"*\")` will unsubscribe all listeners from all events\n * - `unsubscribe(\"*.*\")` will unsubscribe all listeners from all events on the second level\n */\n const unsubscribe = <K extends SubscriptionKey<T>>(event: K, listener?: SubscriptionListeners<T>[K]): void => {\n const eventListeners = getListenerSetsForSubscriptionKey(event);\n for (const listenerSet of eventListeners) {\n if (typeof listener === \"undefined\") {\n listenerSet.clear();\n } else {\n listenerSet.delete(listener);\n }\n }\n };\n\n /** Subscribe to an event. Returns an object with the event name, listener, and an unsubscribe function.\n * @param event The event to subscribe to. Wildcards `foo.*.bar.*` are supported.\n * Using `*` will match any event on any level.\n * More specific wildcard patterns like `*.*` will only match events on that level.\n * @param listener The listener to call when the event is published.\n * @returns A subscription object with the event name, listener, and an unsubscribe function.\n */\n const subscribe = <K extends SubscriptionKey<T>>(\n event: K,\n listener: SubscriptionListeners<T>[K]\n ): {\n event: K;\n listener: SubscriptionListeners<T>[K];\n unsubscribe: () => void;\n } => {\n if (typeof listener !== \"function\") {\n throw new ValidationError(`Invalid listener for event: \"${event}\". Expected function, got ${typeof listener}`);\n }\n const publishPaths = event === \"*\" ? eventNames : subPubPathMap[event];\n if (!publishPaths) throw new ValidationError(`Invalid event: \"${event}\"`);\n for (const publishPath of publishPaths) {\n if (!listeners.has(publishPath)) listeners.set(publishPath, new Set());\n listeners.get(publishPath)!.add(listener);\n }\n return { event, listener, unsubscribe: () => unsubscribe(event, listener) };\n };\n\n /** Subscribe to an event once. The listener will be unsubscribed after the first time it is called.\n * You cannot cancel this subscription using unsubscribe() with the original listener.\n * Use the returned unsubscribe function / listener instead.\n * @param event The event to subscribe to. Wildcards `foo.*.bar.*` are supported.\n * Using `*` will match any event on any level.\n * More specific wildcard patterns like `*.*` will only match events on that level.\n * @param listener The listener to call when the event is published.\n * @returns A subscription object with the event name, listener, and an unsubscribe function.\n * */\n const subscribeOnce = <K extends SubscriptionKey<T>>(event: K, listener: SubscriptionListeners<T>[K]) => {\n const wrappedListener = (data: unknown, eventName: string) => {\n listener(data, eventName);\n unsubscribe(event, wrappedListener as unknown as SubscriptionListeners<T>[K]);\n };\n return subscribe(event, wrappedListener as unknown as SubscriptionListeners<T>[K]);\n };\n\n /** Publish an event. All listeners for the event will be called with the provided data.\n * @param event The event to publish.\n * @param data The data to pass to the listeners.*/\n const publish = <K extends PublishKey<T>>(event: K, data: SubscriptionListenerPayloads<T>[K]) => {\n if (validate) validatePayloadOrPanic(event, data);\n if (!listeners.has(event)) return;\n for (const listener of listeners.get(event)!) {\n (listener as SubscriptionListeners<T>[K])(data, event);\n }\n };\n\n /** Returns a list of all the event names.\n * @returns An array of event names. */\n const getEventNames = () => eventNames;\n\n /** Get the list of listeners for an event or all listeners if no event is provided.\n * @param event The event to get listeners for.\n * @returns An array of listeners for the event. */\n const getListeners = <K extends SubscriptionKey<T>>(event?: K) => {\n const targetListeners: ((data: unknown, eventName: string) => void)[] = [];\n const targetListenerSets = event ? getListenerSetsForSubscriptionKey(event) : Array.from(listeners.values());\n for (const listenerSet of targetListenerSets) {\n for (const listener of listenerSet) {\n targetListeners.push(listener as (data: unknown, eventName: string) => void);\n }\n }\n return targetListeners;\n };\n\n /** Waits for an event to be published.\n * Returns a promise that resolves when the event is published.\n * @param event The event to wait for.\n * @param options.timeout The timeout in milliseconds. Defaults to 10000.\n * @param options.filter A function that returns true if the event should be accepted.\n * @returns A promise that resolves when the event is published with the data passed to the listener. */\n const waitFor = <K extends SubscriptionKey<T>>(\n event: K,\n options: { timeout?: number; filter?: (data: SubscriptionListenerPayloads<T>[K]) => boolean } = {}\n ) => {\n const { timeout = 5_000, filter } = options;\n return new Promise<SubscriptionListenerPayloads<T>[K]>((resolve, reject) => {\n let timeoutId: ReturnType<typeof setTimeout>;\n const listener = (data: unknown) => {\n if (filter && !filter(data as SubscriptionListenerPayloads<T>[K])) return;\n unsubscribe(event, listener as unknown as SubscriptionListeners<T>[K]);\n resolve(data as SubscriptionListenerPayloads<T>[K]);\n clearTimeout(timeoutId);\n };\n subscribe(event, listener as unknown as SubscriptionListeners<T>[K]);\n if (timeout) {\n timeoutId = setTimeout(() => {\n unsubscribe(event, listener as unknown as SubscriptionListeners<T>[K]);\n reject(new RuntimeError(`Timeout waiting for event: \"${event}\"`));\n }, timeout);\n }\n });\n };\n\n return {\n publish,\n subscribe,\n subscribeOnce,\n unsubscribe,\n getEventNames,\n getListeners,\n waitFor,\n };\n}\n\nexport { create };\n"],
5
+ "mappings": "AAAO,IAAMA,EAAc,YCEpB,IAAMC,EAAN,cAA8B,KAAM,CACzC,YAAYC,EAAiB,CAC3B,MAAM,GAAGC,IAAcD,GAAS,EAChC,KAAK,KAAO,iBACd,CACF,EAEaE,EAAN,cAA2B,KAAM,CACtC,YAAYF,EAAiB,CAC3B,MAAM,GAA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EACAV,EAAUd,EAAOY,CAAkD,EAC/DQ,IACFI,EAAY,WAAW,IAAM,CAC3Bb,EAAYX,EAAOY,CAAkD,EACrEW,EAAO,IAAIE,EAAa,+BAA+BzB,IAAQ,CAAC,CAClE,EAAGoB,CAAO,EAEd,CAAC,CACH,CAUA,CACF",
6
+ "names": ["errorPrefix", "ValidationError", "message", "errorPrefix", "RuntimeError", "getPublishPaths", "schema", "prefix", "paths", "k", "v", "newPrefix", "getSubscribePaths", "newWildcardPrefix", "getSubPubPathMap", "map", "publishPaths", "subscribePaths", "subscribePath", "subscribeParts", "publishPath", "publishParts", "i", "create", "schema", "validate", "subPubPathMap", "getSubPubPathMap", "eventNames", "listeners", "getListenerSetsForSubscriptionKey", "event", "listenerSets", "publishPaths", "ValidationError", "publishPath", "listenerSet", "validatePayloadOrPanic", "data", "pathFragments", "currentSchema", "fragment", "unsubscribe", "listener", "eventListeners", "subscribe", "wrappedListener", "eventName", "targetListeners", "targetListenerSets", "options", "timeout", "filter", "resolve", "reject", "timeoutId", "RuntimeError"]
7
+ }
package/package.json ADDED
@@ -0,0 +1,72 @@
1
+ {
2
+ "name": "zodbus",
3
+ "version": "1.0.0",
4
+ "repository": "https://github.com/3rd/zodbus",
5
+ "description": "Type-safe event bus built around Zod.",
6
+ "keywords": [
7
+ "event bus",
8
+ "zod",
9
+ "typescript"
10
+ ],
11
+ "author": {
12
+ "name": "Andrei Neculaesei",
13
+ "email": "3rd@users.noreply.github.com"
14
+ },
15
+ "license": "MIT",
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "type": "module",
20
+ "typings": "dist/index.d.ts",
21
+ "main": "dist/index.mjs",
22
+ "exports": {
23
+ ".": {
24
+ "import": "./dist/index.mjs",
25
+ "require": "./dist/index.cjs"
26
+ }
27
+ },
28
+ "scripts": {
29
+ "build": "npm run clean && node src/scripts/build.mjs",
30
+ "postbuild": "tsc -p tsconfig.build.json",
31
+ "test": "jest",
32
+ "tsc": "tsc",
33
+ "benchmark": "cd benchmark && npm run benchmark",
34
+ "prepare": "husky install",
35
+ "prepublishOnly": "pnpm run build && pnpm run tsc && pnpm run test",
36
+ "clean": "rm -rf dist"
37
+ },
38
+ "lint-staged": {
39
+ "*": "prettier --ignore-unknown --write"
40
+ },
41
+ "prettier": {
42
+ "arrowParens": "always",
43
+ "bracketSpacing": true,
44
+ "jsxBracketSameLine": false,
45
+ "printWidth": 120,
46
+ "quoteProps": "as-needed",
47
+ "semi": true,
48
+ "singleQuote": false,
49
+ "tabWidth": 2,
50
+ "trailingComma": "es5",
51
+ "useTabs": false
52
+ },
53
+ "devDependencies": {
54
+ "@commitlint/cli": "^17.6.3",
55
+ "@commitlint/config-conventional": "^17.6.3",
56
+ "@semantic-release/changelog": "^6.0.3",
57
+ "@semantic-release/git": "^10.0.1",
58
+ "@types/jest": "^29.5.2",
59
+ "@types/node": "^20.2.5",
60
+ "esbuild": "^0.17.19",
61
+ "husky": "^8.0.3",
62
+ "jest": "^29.5.0",
63
+ "lint-staged": "^13.2.2",
64
+ "prettier": "^2.8.8",
65
+ "semantic-release": "^21.0.2",
66
+ "ts-jest": "^29.1.0",
67
+ "typescript": "^5.1.3"
68
+ },
69
+ "peerDependencies": {
70
+ "zod": "^3.21.4"
71
+ }
72
+ }