@holistics/anfra-sdk 0.10.2 → 0.11.1

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,72 @@
1
+ import { D as DatasetDescriptor, aj as User } from './types-plgahUrN.js';
2
+
3
+ /**
4
+ * The bridge: the postMessage protocol between a Data App's frame (`app`'s bootstrap) and the
5
+ * page hosting it (`host`). The frame has an opaque origin and no access to the API: everything it
6
+ * reaches crosses here, as a plugin's calls and events (docs/designs/sdk-plugins.md).
7
+ *
8
+ * Both sides speak the same messages. A call is answered by its `id`, which each side counts on
9
+ * its own: a result answers a call the other side made.
10
+ */
11
+
12
+ /** A plugin a host installed, as a frame sees it (`Anfra.plugins`). */
13
+ interface PluginInfo {
14
+ name: string;
15
+ version: string;
16
+ }
17
+ /** What the host provisions a frame with, as JSON in `<script type="application/json" id="anfra-provision">`. */
18
+ interface Provision {
19
+ datasets: Record<string, DatasetDescriptor>;
20
+ user: User;
21
+ /** Every plugin the host installed, whether or not it has a frame half. */
22
+ plugins: PluginInfo[];
23
+ /** The host page's origin: where the frame posts, so nothing else can read its messages. */
24
+ hostOrigin: string;
25
+ }
26
+ declare const PROVISION_ELEMENT_ID = "anfra-provision";
27
+ /** An error as it crosses the bridge: the SDK's error class by name, and its message. */
28
+ interface BridgeError {
29
+ name: string;
30
+ message: string;
31
+ }
32
+ type BridgeMessage =
33
+ /** Frame → host: the frame's kernel is running (once per load of its document). */
34
+ {
35
+ type: 'anfra:ready';
36
+ }
37
+ /** A plugin call, `<plugin>.<name>`, answered by an `anfra:result` with the same id. */
38
+ | {
39
+ type: 'anfra:call';
40
+ id: number;
41
+ name: string;
42
+ request: unknown;
43
+ } | {
44
+ type: 'anfra:result';
45
+ id: number;
46
+ ok: true;
47
+ result: unknown;
48
+ } | {
49
+ type: 'anfra:result';
50
+ id: number;
51
+ ok: false;
52
+ error: BridgeError;
53
+ }
54
+ /** The caller gave up on its call: stop it. Nothing answers. */
55
+ | {
56
+ type: 'anfra:cancel';
57
+ id: number;
58
+ }
59
+ /** A bridge event, `<plugin>.<name>`. Nothing answers. */
60
+ | {
61
+ type: 'anfra:event';
62
+ name: string;
63
+ payload: unknown;
64
+ };
65
+ /** Frame → host. */
66
+ type FrameMessage = BridgeMessage;
67
+ /** Host → frame. */
68
+ type HostMessage = Exclude<BridgeMessage, {
69
+ type: 'anfra:ready';
70
+ }>;
71
+
72
+ export { type BridgeError as B, type FrameMessage as F, type HostMessage as H, PROVISION_ELEMENT_ID as P, type BridgeMessage as a, type PluginInfo as b, type Provision as c };
@@ -0,0 +1,20 @@
1
+ import { F as FrameHalf } from './plugin-C5cihcQD.js';
2
+
3
+ /**
4
+ * @holistics/anfra-sdk/build: building an SDK plugin's frame half into the script source a host
5
+ * provisions (`plugin.frame`). Node only, at build time; needs `esbuild`, an optional peer
6
+ * dependency. See docs/usage/sdk-plugins.md.
7
+ */
8
+
9
+ interface BuildFrameHalfOptions {
10
+ /** The setup's entry: a module whose default export takes the kernel (`(kernel: FrameKernel) => void`). */
11
+ setup: string;
12
+ /** The body's entry, if the plugin has one: a module whose default export is what `kernel.body()` resolves to. */
13
+ body?: string;
14
+ /** Minify the scripts; true unless false. */
15
+ minify?: boolean;
16
+ }
17
+ /** Build a frame half: the setup, and the body if there is one, as script source for `plugin.frame`. */
18
+ declare function buildFrameHalf(options: BuildFrameHalfOptions): Promise<FrameHalf>;
19
+
20
+ export { type BuildFrameHalfOptions, buildFrameHalf };
package/dist/build.js ADDED
@@ -0,0 +1,36 @@
1
+ import "./chunk-PKBMQBKP.js";
2
+
3
+ // src/build/index.ts
4
+ var FORBIDDEN = /^(@holistics\/anfra-sdk\/(api|host|plugins|build)|openapi-fetch)(\/|$)/;
5
+ async function buildScript(entry, globalName, minify) {
6
+ const { build } = await import("esbuild");
7
+ const result = await build({
8
+ entryPoints: [entry],
9
+ bundle: true,
10
+ format: "iife",
11
+ globalName,
12
+ target: "es2020",
13
+ minify,
14
+ // The SDK's error classes name themselves by their class: minified, they would be 'c'.
15
+ keepNames: true,
16
+ write: false,
17
+ logLevel: "silent",
18
+ plugins: [{
19
+ name: "anfra-frame-half",
20
+ setup(b) {
21
+ b.onResolve({ filter: FORBIDDEN }, (args) => ({
22
+ errors: [{ text: `A frame half can't import '${args.path}': the frame reaches data only over the bridge. Import types only, or move the code to the host half.` }]
23
+ }));
24
+ }
25
+ }]
26
+ });
27
+ return result.outputFiles[0].text;
28
+ }
29
+ async function buildFrameHalf(options) {
30
+ const minify = options.minify !== false;
31
+ const setup = await buildScript(options.setup, "__anfraPlugin", minify);
32
+ return options.body ? { setup, body: await buildScript(options.body, "__anfraBody", minify) } : { setup };
33
+ }
34
+ export {
35
+ buildFrameHalf
36
+ };
File without changes
@@ -0,0 +1,106 @@
1
+ import {
2
+ PermissionError,
3
+ QueryError,
4
+ TransportError,
5
+ ValidationError
6
+ } from "./chunk-KCTLO3CO.js";
7
+
8
+ // src/common/endpoint.ts
9
+ function abortError() {
10
+ const err = new Error("Aborted");
11
+ err.name = "AbortError";
12
+ return err;
13
+ }
14
+ function isAbortError(err) {
15
+ return err?.name === "AbortError";
16
+ }
17
+ var SDK_ERRORS = /* @__PURE__ */ new Set(["ValidationError", "QueryError", "PermissionError", "TransportError"]);
18
+ function toBridgeError(err) {
19
+ const e = err;
20
+ if (e instanceof Error && SDK_ERRORS.has(e.name)) return { name: e.name, message: e.message || "The call failed." };
21
+ return { name: "QueryError", message: "The call failed." };
22
+ }
23
+ function fromBridgeError(error) {
24
+ switch (error?.name) {
25
+ case "PermissionError":
26
+ return new PermissionError(error.message);
27
+ case "TransportError":
28
+ return new TransportError(error.message);
29
+ case "ValidationError":
30
+ return new ValidationError(error.message);
31
+ default:
32
+ return new QueryError(error?.message || "The call failed.");
33
+ }
34
+ }
35
+ function createEndpoint(post, handlers) {
36
+ const pending = /* @__PURE__ */ new Map();
37
+ const inFlight = /* @__PURE__ */ new Map();
38
+ let nextId = 1;
39
+ const answer = (id, name, request) => {
40
+ const controller = new AbortController();
41
+ inFlight.set(id, controller);
42
+ Promise.resolve().then(() => handlers.answer(name, request, controller.signal)).then((result) => {
43
+ if (!controller.signal.aborted) post({ type: "anfra:result", id, ok: true, result });
44
+ }).catch((err) => {
45
+ if (controller.signal.aborted || isAbortError(err)) return;
46
+ post({ type: "anfra:result", id, ok: false, error: toBridgeError(err) });
47
+ }).finally(() => {
48
+ if (inFlight.get(id) === controller) inFlight.delete(id);
49
+ });
50
+ };
51
+ return {
52
+ call(name, request, signal) {
53
+ return new Promise((resolve, reject) => {
54
+ if (signal?.aborted) {
55
+ reject(abortError());
56
+ return;
57
+ }
58
+ const id = nextId++;
59
+ pending.set(id, { resolve, reject });
60
+ signal?.addEventListener("abort", () => {
61
+ if (!pending.delete(id)) return;
62
+ post({ type: "anfra:cancel", id });
63
+ reject(abortError());
64
+ }, { once: true });
65
+ post({ type: "anfra:call", id, name, request });
66
+ });
67
+ },
68
+ emit(name, payload) {
69
+ post({ type: "anfra:event", name, payload });
70
+ },
71
+ receive(message) {
72
+ switch (message?.type) {
73
+ case "anfra:call":
74
+ answer(message.id, message.name, message.request);
75
+ return true;
76
+ case "anfra:result": {
77
+ const call = pending.get(message.id);
78
+ if (!call) return true;
79
+ pending.delete(message.id);
80
+ if (message.ok) call.resolve(message.result);
81
+ else call.reject(fromBridgeError(message.error));
82
+ return true;
83
+ }
84
+ case "anfra:cancel":
85
+ inFlight.get(message.id)?.abort();
86
+ inFlight.delete(message.id);
87
+ return true;
88
+ case "anfra:event":
89
+ handlers.event(message.name, message.payload);
90
+ return true;
91
+ default:
92
+ return false;
93
+ }
94
+ },
95
+ close(reason) {
96
+ inFlight.forEach((controller) => controller.abort());
97
+ inFlight.clear();
98
+ pending.forEach(({ reject }) => reject(reason));
99
+ pending.clear();
100
+ }
101
+ };
102
+ }
103
+
104
+ export {
105
+ createEndpoint
106
+ };
@@ -1,6 +1,6 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
1
+ import {
2
+ __publicField
3
+ } from "./chunk-PKBMQBKP.js";
4
4
 
5
5
  // src/common/errors.ts
6
6
  var DataAppError = class extends Error {
@@ -33,7 +33,6 @@ var TransportError = class extends DataAppError {
33
33
  };
34
34
 
35
35
  export {
36
- __publicField,
37
36
  DataAppError,
38
37
  ValidationError,
39
38
  QueryError,
@@ -0,0 +1,6 @@
1
+ // package.json
2
+ var version = "0.11.1";
3
+
4
+ export {
5
+ version
6
+ };