@cubicecho/graphql-mdns-discovery 0.0.0-stage → 0.1.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.
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Serving a host's GraphQL services document at `/.well-known/graphql-services.json`.
3
+ */
4
+ import type { IncomingMessage } from 'node:http';
5
+ import { type DocumentHandler, type DocumentMiddleware, type DocumentSource, type ServeSettings } from '@cubicecho/discovery-core';
6
+ import { type GraphqlServiceDraft, type GraphqlServicesDocument } from '../document/graphql-services.ts';
7
+ /** What a service builder is told about the request it answers. */
8
+ export interface GraphqlServiceContext {
9
+ /** The API's endpoint URL as this client reached it. */
10
+ endpointUrl: string;
11
+ /** The origin this client reached the host at, for other URLs (subscriptions, SDL). */
12
+ origin: string;
13
+ request: IncomingMessage;
14
+ }
15
+ /** One GraphQL API a host advertises. */
16
+ export interface AdvertisedGraphqlApi {
17
+ /**
18
+ * The endpoint's path.
19
+ *
20
+ * @defaultValue `DEFAULT_GRAPHQL_PATH`
21
+ */
22
+ path?: string;
23
+ /** Builds the API's entry for a request. */
24
+ service: (context: GraphqlServiceContext) => GraphqlServiceDraft | Promise<GraphqlServiceDraft>;
25
+ }
26
+ /** Lists the APIs to advertise, asked per request so it can follow config changes. */
27
+ export type GraphqlApiSource = (request: IncomingMessage) => AdvertisedGraphqlApi[] | Promise<AdvertisedGraphqlApi[]>;
28
+ /**
29
+ * Middleware that serves `/.well-known/graphql-services.json` and passes every other request to `next`.
30
+ *
31
+ * @param apis - The APIs to advertise.
32
+ * @param [overrides] - Serving settings to change.
33
+ * @returns The middleware; mount it at the root, before any auth guard, since the document is public.
34
+ *
35
+ * @remarks
36
+ * It answers exactly one path, so it can be mounted beside mcp-discovery's `discoveryMiddleware` in either
37
+ * order. A client on the LAN is told LAN addresses and one on localhost is told localhost. Errors from
38
+ * `apis` or a builder go to `next`.
39
+ */
40
+ export declare function graphqlDiscoveryMiddleware(apis: GraphqlApiSource, overrides?: Partial<ServeSettings>): DocumentMiddleware;
41
+ /**
42
+ * A handler that serves a GraphQL services document, for routing it yourself.
43
+ *
44
+ * @param source - Returns the document for a request.
45
+ * @param [overrides] - Serving settings to change.
46
+ * @returns The handler.
47
+ */
48
+ export declare function graphqlServicesHandler(source: DocumentSource<GraphqlServicesDocument>, overrides?: Partial<ServeSettings>): DocumentHandler;
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Serving a host's GraphQL services document at `/.well-known/graphql-services.json`.
3
+ */
4
+ import { answeringMiddleware, documentHandler, requestOrigin, requestPath, writeDocument, } from '@cubicecho/discovery-core';
5
+ import { DEFAULT_GRAPHQL_PATH, GRAPHQL_SERVICES_MEDIA_TYPE, GRAPHQL_SERVICES_WELL_KNOWN_PATH } from '../core/consts.js';
6
+ import { createGraphqlServicesDocument, } from '../document/graphql-services.js';
7
+ /**
8
+ * Builds the document for one request, with every URL from the origin the request arrived at.
9
+ *
10
+ * @param apis - The APIs to advertise.
11
+ * @param request - The request.
12
+ * @returns The document.
13
+ * @throws {@link InvalidDocumentError} If a service is not valid.
14
+ */
15
+ async function documentFor(apis, request) {
16
+ const origin = requestOrigin(request);
17
+ const listed = await apis(request);
18
+ const services = await Promise.all(listed.map(async (api) => {
19
+ const endpointUrl = new URL(api.path ?? DEFAULT_GRAPHQL_PATH, origin).href;
20
+ const { endpoint = { url: endpointUrl }, ...rest } = await api.service({ endpointUrl, origin, request });
21
+ return { ...rest, endpoint };
22
+ }));
23
+ return createGraphqlServicesDocument(services);
24
+ }
25
+ /**
26
+ * Middleware that serves `/.well-known/graphql-services.json` and passes every other request to `next`.
27
+ *
28
+ * @param apis - The APIs to advertise.
29
+ * @param [overrides] - Serving settings to change.
30
+ * @returns The middleware; mount it at the root, before any auth guard, since the document is public.
31
+ *
32
+ * @remarks
33
+ * It answers exactly one path, so it can be mounted beside mcp-discovery's `discoveryMiddleware` in either
34
+ * order. A client on the LAN is told LAN addresses and one on localhost is told localhost. Errors from
35
+ * `apis` or a builder go to `next`.
36
+ */
37
+ export function graphqlDiscoveryMiddleware(apis, overrides = {}) {
38
+ return answeringMiddleware(async (request, response) => {
39
+ const isDocument = requestPath(request) === GRAPHQL_SERVICES_WELL_KNOWN_PATH;
40
+ if (isDocument === false) {
41
+ return false;
42
+ }
43
+ writeDocument(request, response, await documentFor(apis, request), GRAPHQL_SERVICES_MEDIA_TYPE, overrides);
44
+ return true;
45
+ });
46
+ }
47
+ /**
48
+ * A handler that serves a GraphQL services document, for routing it yourself.
49
+ *
50
+ * @param source - Returns the document for a request.
51
+ * @param [overrides] - Serving settings to change.
52
+ * @returns The handler.
53
+ */
54
+ export function graphqlServicesHandler(source, overrides = {}) {
55
+ return documentHandler(GRAPHQL_SERVICES_MEDIA_TYPE, source, overrides);
56
+ }
package/llms.txt ADDED
@@ -0,0 +1,81 @@
1
+ # @cubicecho/graphql-mdns-discovery
2
+
3
+ > Advertise and discover GraphQL APIs: a public services document at /.well-known/graphql-services.json, serving it over HTTP, and finding hosts on a LAN as _graphql._tcp over mDNS.
4
+
5
+ A host lists its GraphQL APIs in a public document at `/.well-known/graphql-services.json`, giving each one's endpoint, subscription transport, SDL location and the headers a client must supply (never their values). This package builds, validates and serves that document, and on the client side reads it, bounded in time and size, with each entry checked on its own. LAN advertising and browsing as `_graphql._tcp` is the separate `@cubicecho/graphql-mdns-discovery/mdns` export.
6
+
7
+ It is built on `@cubicecho/discovery-core`, as `@cubicecho/mcp-discovery` is, so an app can use both: they serve different paths, advertise different service types, share one mDNS responder, and export different names. The shared errors and defaults are re-exported here.
8
+
9
+ Requires Node >=22, with `zod` >=4.1 as a peer dependency. ESM only.
10
+ Full prose, worked examples and the reasoning behind each seam are in README.md; this file is the index.
11
+
12
+ ## Exports
13
+
14
+ ### discover
15
+
16
+ Reading a host's GraphQL services document, bounded in time and size, with each service checked on its own.
17
+
18
+ - `DiscoveredGraphqlService` (type) — A service found in a document, with how to reach it.
19
+ - `discoverGraphql` — Discovers the GraphQL APIs a host advertises, from its `/.well-known/graphql-services.json`.
20
+ - `discoverGraphqlDocument` — Discovers the GraphQL APIs a document lists, wherever it is served (e.g.
21
+ - `fetchGraphqlServicesDocument` — Fetches a GraphQL services document and checks its envelope.
22
+ - `GraphqlDiscovery` (type) — What a read of one document found.
23
+ - `GraphqlDiscoveryFailure` (type) — A document entry that could not be read.
24
+
25
+ ### consts
26
+
27
+ - `DEFAULT_GRAPHQL_PATH` — The endpoint path assumed when an mDNS record names none, and advertised when none is given.
28
+ - `GRAPHQL_SERVICES_MEDIA_TYPE` — Media type of a GraphQL services document.
29
+ - `GRAPHQL_SERVICES_WELL_KNOWN_PATH` — Where a host serves its GraphQL services document (RFC 8615).
30
+
31
+ ### graphql-services
32
+
33
+ The GraphQL services document: what a host lists at `/.well-known/graphql-services.json`.
34
+
35
+ - `createGraphqlService` — Builds one service, validating it.
36
+ - `createGraphqlServicesDocument` — Builds a GraphQL services document, adding its `specVersion` and validating the result.
37
+ - `GRAPHQL_SERVICES_SPEC_VERSION` — The `specVersion` this package writes.
38
+ - `GraphqlEndpoint` (type) — Where a service takes queries and mutations.
39
+ - `GraphqlHeader` (type) — A header a client sends with every request; never its value.
40
+ - `GraphqlMethod` — The HTTP methods a GraphQL endpoint can take queries on.
41
+ - `GraphqlService` (type) — One GraphQL API a host serves.
42
+ - `GraphqlServiceConnection` (type) — How to reach a service: absolute URLs, and the headers the caller must supply.
43
+ - `GraphqlServiceDraft` (type) — A service as a middleware's builder returns it: the endpoint may be left out, and is then the advertised path.
44
+ - `GraphqlServiceFields` (type) — What `createGraphqlService` takes.
45
+ - `GraphqlServicesDocument` (type) — A GraphQL services document with every service valid, as a host builds it.
46
+ - `GraphqlServicesEnvelope` (type) — A document as read from the network: the envelope valid, the services not yet checked.
47
+ - `GraphqlSubscriptions` (type) — Where a service takes subscriptions, and over which protocol.
48
+ - `graphqlEndpointSchema` — Where queries and mutations go.
49
+ - `graphqlHeaderSchema` — A header a client sends with every request; the value is never in the document.
50
+ - `graphqlServiceConnection` — How to connect to a service, with every URL made absolute.
51
+ - `graphqlServiceSchema` — One GraphQL API a host serves.
52
+ - `graphqlServicesDocumentSchema` — The envelope of a GraphQL services document.
53
+ - `graphqlSubscriptionsSchema` — Where subscriptions go, and over which protocol.
54
+ - `parseGraphqlService` — Validates one service from a document.
55
+ - `parseGraphqlServicesDocument` — Validates a whole document, every service included.
56
+ - `parseGraphqlServicesEnvelope` — Validates a document's envelope, leaving its services to be read one by one.
57
+ - `SubscriptionProtocol` — The subscription transports a service can offer.
58
+
59
+ ### graphql-discovery-middleware
60
+
61
+ Serving a host's GraphQL services document at `/.well-known/graphql-services.json`.
62
+
63
+ - `AdvertisedGraphqlApi` (type) — One GraphQL API a host advertises.
64
+ - `GraphqlApiSource` (type) — Lists the APIs to advertise, asked per request so it can follow config changes.
65
+ - `GraphqlServiceContext` (type) — What a service builder is told about the request it answers.
66
+ - `graphqlDiscoveryMiddleware` — Middleware that serves `/.well-known/graphql-services.json` and passes every other request to `next`.
67
+ - `graphqlServicesHandler` — A handler that serves a GraphQL services document, for routing it yourself.
68
+
69
+ ### from @cubicecho/discovery-core
70
+
71
+ Re-exported unchanged; documented in that package's llms.txt.
72
+
73
+ - `DiscoveryError`
74
+ - `DocumentFetchError`
75
+ - `FETCH_DEFAULTS`
76
+ - `FetchOptions` (type)
77
+ - `FetchSettings` (type)
78
+ - `hostSlug`
79
+ - `InvalidDocumentError`
80
+ - `SERVE_DEFAULTS`
81
+ - `ServeSettings` (type)
package/package.json CHANGED
@@ -1,6 +1,82 @@
1
1
  {
2
2
  "name": "@cubicecho/graphql-mdns-discovery",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Advertise and discover GraphQL APIs: a public services document at /.well-known/graphql-services.json, serving it over HTTP, and finding hosts on a LAN as _graphql._tcp over mDNS.",
5
+ "keywords": [
6
+ "graphql",
7
+ "discovery",
8
+ "mdns",
9
+ "dns-sd",
10
+ "bonjour",
11
+ "well-known",
12
+ "typescript"
13
+ ],
14
+ "license": "MIT",
15
+ "author": "Benjamin Van Treese <vantreeseba@gmail.com>",
16
+ "type": "module",
17
+ "engines": {
18
+ "node": ">=22"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/cubicecho/graphql-mdns-discovery.git"
23
+ },
24
+ "homepage": "https://github.com/cubicecho/graphql-mdns-discovery#readme",
25
+ "bugs": {
26
+ "url": "https://github.com/cubicecho/graphql-mdns-discovery/issues"
27
+ },
28
+ "exports": {
29
+ ".": {
30
+ "types": "./dist/index.d.ts",
31
+ "import": "./dist/index.js"
32
+ },
33
+ "./mdns": {
34
+ "types": "./dist/mdns.d.ts",
35
+ "import": "./dist/mdns.js"
36
+ }
37
+ },
38
+ "files": [
39
+ "dist",
40
+ "llms.txt"
41
+ ],
42
+ "publishConfig": {
43
+ "access": "public"
44
+ },
45
+ "scripts": {
46
+ "build": "tsc -p tsconfig.build.json && node scripts/llms-txt.mjs",
47
+ "prepack": "npm run build",
48
+ "check": "npm run check:biome && npm run check:types",
49
+ "check:biome": "biome check --write .",
50
+ "check:types": "tsc --noEmit",
51
+ "test": "vitest run",
52
+ "test:watch": "vitest",
53
+ "llms": "node scripts/llms-txt.mjs",
54
+ "llms:check": "node scripts/llms-txt.mjs --check",
55
+ "coverage": "vitest run --coverage"
56
+ },
57
+ "dependencies": {
58
+ "@cubicecho/discovery-core": "^0.1.0"
59
+ },
60
+ "peerDependencies": {
61
+ "zod": ">=4.1",
62
+ "bonjour-service": ">=1.4"
63
+ },
64
+ "peerDependenciesMeta": {
65
+ "bonjour-service": {
66
+ "optional": true
67
+ }
68
+ },
69
+ "devDependencies": {
70
+ "@biomejs/biome": "2.5.12",
71
+ "@semantic-release/changelog": "^7.0.0",
72
+ "@semantic-release/git": "^11.0.1",
73
+ "@types/node": "^26.6.6",
74
+ "@vitest/coverage-v8": "^4.1.12",
75
+ "bonjour-service": "^1.4.4",
76
+ "semantic-release": "^25.0.9",
77
+ "typescript": "^7.0.2",
78
+ "vitest": "^4.1.12",
79
+ "zod": "^4.6.7",
80
+ "@cubicecho/mcp-discovery": "^0.4.0"
81
+ }
82
+ }