@kubun/plugin-http 0.8.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.md ADDED
@@ -0,0 +1,57 @@
1
+ # The Prosperity Public License 3.0.0
2
+
3
+ Contributor: Paul Le Cam
4
+
5
+ Source Code: https://github.com/PaulLeCam/kubun
6
+
7
+ ## Purpose
8
+
9
+ This license allows you to use and share this software for noncommercial purposes for free and to try this software for commercial purposes for thirty days.
10
+
11
+ ## Agreement
12
+
13
+ In order to receive this license, you have to agree to its rules. Those rules are both obligations under that agreement and conditions to your license. Don't do anything with this software that triggers a rule you can't or won't follow.
14
+
15
+ ## Notices
16
+
17
+ Make sure everyone who gets a copy of any part of this software from you, with or without changes, also gets the text of this license and the contributor and source code lines above.
18
+
19
+ ## Commercial Trial
20
+
21
+ Limit your use of this software for commercial purposes to a thirty-day trial period. If you use this software for work, your company gets one trial period for all personnel, not one trial per person.
22
+
23
+ ## Contributions Back
24
+
25
+ Developing feedback, changes, or additions that you contribute back to the contributor on the terms of a standardized public software license such as [the Blue Oak Model License 1.0.0](https://blueoakcouncil.org/license/1.0.0), [the Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0.html), [the MIT license](https://spdx.org/licenses/MIT.html), or [the two-clause BSD license](https://spdx.org/licenses/BSD-2-Clause.html) doesn't count as use for a commercial purpose.
26
+
27
+ ## Personal Uses
28
+
29
+ Personal use for research, experiment, and testing for the benefit of public knowledge, personal study, private entertainment, hobby projects, amateur pursuits, or religious observance, without any anticipated commercial application, doesn't count as use for a commercial purpose.
30
+
31
+ ## Noncommercial Organizations
32
+
33
+ Use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization, or government institution doesn't count as use for a commercial purpose regardless of the source of funding or obligations resulting from the funding.
34
+
35
+ ## Defense
36
+
37
+ Don't make any legal claim against anyone accusing this software, with or without changes, alone or with other technology, of infringing any patent.
38
+
39
+ ## Copyright
40
+
41
+ The contributor licenses you to do everything with this software that would otherwise infringe their copyright in it.
42
+
43
+ ## Patent
44
+
45
+ The contributor licenses you to do everything with this software that would otherwise infringe any patents they can license or become able to license.
46
+
47
+ ## Reliability
48
+
49
+ The contributor can't revoke this license.
50
+
51
+ ## Excuse
52
+
53
+ You're excused for unknowingly breaking [Notices](#notices) if you take all practical steps to comply within thirty days of learning you broke the rule.
54
+
55
+ ## No Liability
56
+
57
+ ***As far as the law allows, this software comes as is, without any warranty or condition, and the contributor won't be liable to anyone for any damages related to this software or this license, under any kind of legal claim.***
package/lib/index.d.ts ADDED
@@ -0,0 +1,64 @@
1
+ import type { KubunPlugin, PluginFactoryParams } from '@kubun/engine';
2
+ import type { RPCPluginAPI } from '@kubun/plugin-rpc';
3
+ import { Hono } from 'hono';
4
+ /**
5
+ * Options for the HTTP infrastructure plugin.
6
+ */
7
+ export type HTTPPluginOptions = {
8
+ /** Port to listen on. Default: 4321 (falls back to a random available port). */
9
+ port?: number;
10
+ /** Allowed CORS origin(s). Default: ['*']. Comma-separated strings are split. */
11
+ allowedOrigin?: string | Array<string>;
12
+ /** Namespace prefix for all plugin-managed routes (health, info, protocols). Default: '/'. */
13
+ prefix?: string;
14
+ /** Enkaku protocol access control. Default: true (Server verifies message signatures). */
15
+ accessControl?: false | true | Record<string, boolean | Array<string>>;
16
+ };
17
+ /**
18
+ * API provided by the HTTP infrastructure plugin.
19
+ * Provides a shared routing surface for other plugins to register routes on.
20
+ */
21
+ export type HTTPPluginAPI = {
22
+ /** Resolves when the HTTP server is accepting connections. */
23
+ listening: Promise<void>;
24
+ registerRoute: (method: string, path: string, handler: (c: unknown) => unknown) => void;
25
+ registerProtocol: (name: string, fetch: (req: Request) => Response | Promise<Response>) => void;
26
+ getPort: () => number;
27
+ getURL: () => string;
28
+ };
29
+ /**
30
+ * Options for creating a standalone graph protocol Hono app.
31
+ */
32
+ export type GraphAppOptions = {
33
+ /** The RPC plugin API (already resolved). */
34
+ rpc: RPCPluginAPI;
35
+ /** Allowed CORS origin(s). Default: ['*']. */
36
+ allowedOrigin?: string | Array<string>;
37
+ };
38
+ /**
39
+ * A mountable Hono sub-app with graph protocol routes.
40
+ */
41
+ export type GraphApp = {
42
+ app: Hono;
43
+ dispose: () => Promise<void>;
44
+ };
45
+ /**
46
+ * Creates a standalone Hono app with graph protocol routes.
47
+ *
48
+ * Unlike createHTTPPlugin, this does NOT start an HTTP server or bind a port.
49
+ * The returned app is meant to be mounted on an existing Hono app via `parent.route(prefix, graphApp.app)`.
50
+ *
51
+ * The caller is responsible for:
52
+ * - Creating and awaiting the KubunEngine
53
+ * - Resolving the RPCPluginAPI via engine.getAPI('rpc')
54
+ * - Mounting the returned app and managing the HTTP server lifecycle
55
+ */
56
+ export declare function createGraphApp(options: GraphAppOptions): GraphApp;
57
+ /**
58
+ * Creates the HTTP infrastructure plugin.
59
+ *
60
+ * Wraps a Hono app instance, binds it to a port using @hono/node-server,
61
+ * and serves Enkaku protocol handlers over HTTP via ServerTransport.
62
+ * Other plugins can register additional routes via the API.
63
+ */
64
+ export declare function createHTTPPlugin(options?: HTTPPluginOptions): (params: PluginFactoryParams) => KubunPlugin;
package/lib/index.js ADDED
@@ -0,0 +1 @@
1
+ import{defer as t}from"@enkaku/async";import{ServerTransport as e}from"@enkaku/http-server-transport";import{serve as r}from"@hono/node-server";import o from"get-port";import{Hono as n}from"hono";function i(t){return null==t?["*"]:"string"==typeof t?t.split(",").map(t=>t.trim()):t}function s(t){return async e=>t(e.req.raw)}export function createGraphApp(t){let r=new n,o=new e({allowedOrigin:i(t.allowedOrigin)}),p=s(o.fetch.bind(o)),a=t.rpc.serve(o);return r.post("/graph",p),r.post("/graph/*",p),r.options("/graph",p),r.options("/graph/*",p),{app:r,async dispose(){await a.dispose()}}}export function createHTTPPlugin(p){return a=>{let l,g,c,h,u=new n,f=t(),w=i(p?.allowedOrigin),d=p?.prefix??"/",m="/"===d?"":d.endsWith("/")?d.slice(0,-1):d,y=new Map;u.get(`${m}/health`,t=>t.json({status:"ok"})),u.get(`${m}/info`,t=>{let e={};for(let[t,r]of y)e[t]=r;return t.json({did:a.identity.id,protocols:e})});let P={listening:null,registerRoute(t,e,r){u[t.toLowerCase()](e,r)},registerProtocol(t,e){let r=`${m}/${t}`;y.set(t,r);let o=s(e);u.post(r,o),u.post(`${r}/*`,o),u.options(r,o),u.options(`${r}/*`,o)},getPort(){if(null==l)throw Error("HTTP server is not listening yet");return l},getURL(){if(null==g)throw Error("HTTP server is not listening yet");return g}},v=(async()=>{await a.engine.ready;let t=p?.port??await o({port:4321}),n=new e({allowedOrigin:w});P.registerProtocol("graph",n.fetch.bind(n)),h=(await a.engine.getAPI("rpc")).serve(n),c=r({fetch:u.fetch,port:t},t=>{l=t.port,g=`http://localhost:${t.port}`,f.resolve()}),await f.promise})();return P.listening=v,{name:"http",api:P,async dispose(){await h?.dispose(),c?.close()}}}}
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@kubun/plugin-http",
3
+ "version": "0.8.0",
4
+ "license": "see LICENSE.md",
5
+ "type": "module",
6
+ "main": "lib/index.js",
7
+ "types": "lib/index.d.ts",
8
+ "exports": {
9
+ ".": "./lib/index.js"
10
+ },
11
+ "files": [
12
+ "lib/*",
13
+ "LICENSE.md"
14
+ ],
15
+ "sideEffects": false,
16
+ "dependencies": {
17
+ "@enkaku/async": "^0.14.0",
18
+ "@enkaku/http-server-transport": "^0.14.2",
19
+ "@enkaku/protocol": "^0.14.0",
20
+ "@enkaku/server": "^0.14.3",
21
+ "@hono/node-server": "^1.19.12",
22
+ "get-port": "^7.2.0",
23
+ "hono": "^4.12.9",
24
+ "@kubun/engine": "^0.8.0"
25
+ },
26
+ "devDependencies": {
27
+ "@enkaku/http-client-transport": "^0.14.3",
28
+ "@kubun/protocol": "^0.8.0",
29
+ "@kubun/plugin-rpc": "^0.8.0",
30
+ "@kubun/test-utils": "^0.8.0",
31
+ "@kubun/client": "^0.8.0"
32
+ },
33
+ "scripts": {
34
+ "build:clean": "del lib",
35
+ "build:js": "swc src -d ./lib --config-file ../../swc.json --strip-leading-paths",
36
+ "build:types": "tsc --emitDeclarationOnly --skipLibCheck",
37
+ "build": "pnpm run build:clean && pnpm run build:js && pnpm run build:types",
38
+ "test:types": "tsc --noEmit -p tsconfig.test.json",
39
+ "test:unit": "vitest run",
40
+ "test": "pnpm run test:types && pnpm run test:unit"
41
+ }
42
+ }