@driggsby/sdk 0.0.1 → 0.2.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 +117 -0
- package/README.md +40 -2
- package/dist/driggsby-sdk.js +173 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +9 -0
- package/dist/sdk-core.d.ts +26 -0
- package/dist/sdk-core.js +172 -0
- package/package.json +27 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and
|
|
10
|
+
distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright
|
|
13
|
+
owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities
|
|
16
|
+
that control, are controlled by, or are under common control with that entity.
|
|
17
|
+
For the purposes of this definition, "control" means (i) the power, direct or
|
|
18
|
+
indirect, to cause the direction or management of such entity, whether by
|
|
19
|
+
contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
20
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
21
|
+
|
|
22
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
|
23
|
+
permissions granted by this License.
|
|
24
|
+
|
|
25
|
+
"Source" form shall mean the preferred form for making modifications, including
|
|
26
|
+
but not limited to software source code, documentation source, and configuration
|
|
27
|
+
files.
|
|
28
|
+
|
|
29
|
+
"Object" form shall mean any form resulting from mechanical transformation or
|
|
30
|
+
translation of a Source form, including but not limited to compiled object code,
|
|
31
|
+
generated documentation, and conversions to other media types.
|
|
32
|
+
|
|
33
|
+
"Work" shall mean the work of authorship, whether in Source or Object form,
|
|
34
|
+
made available under the License, as indicated by a copyright notice that is
|
|
35
|
+
included in or attached to the work.
|
|
36
|
+
|
|
37
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that
|
|
38
|
+
is based on (or derived from) the Work and for which the editorial revisions,
|
|
39
|
+
annotations, elaborations, or other modifications represent, as a whole, an
|
|
40
|
+
original work of authorship. For the purposes of this License, Derivative Works
|
|
41
|
+
shall not include works that remain separable from, or merely link (or bind by
|
|
42
|
+
name) to the interfaces of, the Work and Derivative Works thereof.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the original version
|
|
45
|
+
of the Work and any modifications or additions to that Work or Derivative Works
|
|
46
|
+
thereof, that is intentionally submitted to Licensor for inclusion in the Work
|
|
47
|
+
by the copyright owner or by an individual or Legal Entity authorized to submit
|
|
48
|
+
on behalf of the copyright owner. For the purposes of this definition,
|
|
49
|
+
"submitted" means any form of electronic, verbal, or written communication sent
|
|
50
|
+
to the Licensor or its representatives, including but not limited to
|
|
51
|
+
communication on electronic mailing lists, source code control systems, and
|
|
52
|
+
issue tracking systems that are managed by, or on behalf of, the Licensor for
|
|
53
|
+
the purpose of discussing and improving the Work, but excluding communication
|
|
54
|
+
that is conspicuously marked or otherwise designated in writing by the copyright
|
|
55
|
+
owner as "Not a Contribution."
|
|
56
|
+
|
|
57
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
|
|
58
|
+
of whom a Contribution has been received by Licensor and subsequently
|
|
59
|
+
incorporated within the Work.
|
|
60
|
+
|
|
61
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this
|
|
62
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
63
|
+
non-exclusive, no-charge, royalty-free, irrevocable copyright license to
|
|
64
|
+
reproduce, prepare Derivative Works of, publicly display, publicly perform,
|
|
65
|
+
sublicense, and distribute the Work and such Derivative Works in Source or
|
|
66
|
+
Object form.
|
|
67
|
+
|
|
68
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License,
|
|
69
|
+
each Contributor hereby grants to You a perpetual, worldwide, non-exclusive,
|
|
70
|
+
no-charge, royalty-free, irrevocable patent license to make, have made, use,
|
|
71
|
+
offer to sell, sell, import, and otherwise transfer the Work.
|
|
72
|
+
|
|
73
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or
|
|
74
|
+
Derivative Works thereof in any medium, with or without modifications, and in
|
|
75
|
+
Source or Object form, provided that You meet the following conditions:
|
|
76
|
+
|
|
77
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy of
|
|
78
|
+
this License; and
|
|
79
|
+
|
|
80
|
+
(b) You must cause any modified files to carry prominent notices stating that
|
|
81
|
+
You changed the files; and
|
|
82
|
+
|
|
83
|
+
(c) You must retain, in the Source form of any Derivative Works that You
|
|
84
|
+
distribute, all copyright, patent, trademark, and attribution notices from the
|
|
85
|
+
Source form of the Work, excluding those notices that do not pertain to any part
|
|
86
|
+
of the Derivative Works; and
|
|
87
|
+
|
|
88
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution, then
|
|
89
|
+
any Derivative Works that You distribute must include a readable copy of the
|
|
90
|
+
attribution notices contained within such NOTICE file.
|
|
91
|
+
|
|
92
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any
|
|
93
|
+
Contribution intentionally submitted for inclusion in the Work by You to the
|
|
94
|
+
Licensor shall be under the terms and conditions of this License.
|
|
95
|
+
|
|
96
|
+
6. Trademarks. This License does not grant permission to use the trade names,
|
|
97
|
+
trademarks, service marks, or product names of the Licensor, except as required
|
|
98
|
+
for reasonable and customary use in describing the origin of the Work.
|
|
99
|
+
|
|
100
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in
|
|
101
|
+
writing, Licensor provides the Work on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
102
|
+
CONDITIONS OF ANY KIND, either express or implied, including, without limitation,
|
|
103
|
+
any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or
|
|
104
|
+
FITNESS FOR A PARTICULAR PURPOSE.
|
|
105
|
+
|
|
106
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in
|
|
107
|
+
tort (including negligence), contract, or otherwise, unless required by
|
|
108
|
+
applicable law or agreed to in writing, shall any Contributor be liable to You
|
|
109
|
+
for damages, including any direct, indirect, special, incidental, or
|
|
110
|
+
consequential damages arising as a result of this License or out of the use or
|
|
111
|
+
inability to use the Work.
|
|
112
|
+
|
|
113
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or
|
|
114
|
+
Derivative Works thereof, You may choose to offer, and charge a fee for,
|
|
115
|
+
acceptance of support, warranty, indemnity, or other liability obligations.
|
|
116
|
+
However, in accepting such obligations, You may act only on Your own behalf and
|
|
117
|
+
on Your sole responsibility.
|
package/README.md
CHANGED
|
@@ -1,4 +1,42 @@
|
|
|
1
1
|
# @driggsby/sdk
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
The browser SDK for [Driggsby](https://driggsby.com) apps — small static
|
|
4
|
+
web apps, each served at its own `driggsby.dev` address, that show live
|
|
5
|
+
financial data.
|
|
6
|
+
|
|
7
|
+
Most apps never install this package. Driggsby serves the SDK to every
|
|
8
|
+
deployed app on the app's own origin, so one tag is the entire setup:
|
|
9
|
+
|
|
10
|
+
```html
|
|
11
|
+
<script type="module" src="/-/driggsby-sdk.js"></script>
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
and reading live data is one call:
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
driggsby.watch("list_accounts", {}, (result) => render(result));
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Every `watch` is a live subscription: when the underlying data changes,
|
|
21
|
+
the callback runs again with fresh data. It returns an unsubscribe
|
|
22
|
+
function. Identical results are deduped, so unchanged data never
|
|
23
|
+
re-renders.
|
|
24
|
+
|
|
25
|
+
This package exists for two narrower uses:
|
|
26
|
+
|
|
27
|
+
- `driggsby dev` serves the same runtime locally from this package's
|
|
28
|
+
bundled copy, so an app under development behaves exactly like a
|
|
29
|
+
deployed one.
|
|
30
|
+
- Bundler users can `import type` the SDK surface, or import `SdkCore`
|
|
31
|
+
to embed the protocol runtime in their own tooling.
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { PROTOCOL, SdkCore } from "@driggsby/sdk";
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
The SDK speaks the frozen `driggsby-sdk/1` postMessage protocol with the
|
|
38
|
+
embedding Driggsby host page. Security properties worth knowing: the SDK
|
|
39
|
+
only ever talks to its parent window, only after the host proves itself
|
|
40
|
+
from an allowed origin (the Driggsby console, or a loopback host page
|
|
41
|
+
during local development), and it pins the first valid host origin for
|
|
42
|
+
the life of the page.
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// src/sdk-core.ts
|
|
2
|
+
var PROTOCOL = "driggsby-sdk/1";
|
|
3
|
+
var MAX_WATCHES = 32;
|
|
4
|
+
var EXACT_ALLOWED_HOST_ORIGINS = /* @__PURE__ */ new Set(["https://app.driggsby.com"]);
|
|
5
|
+
var LOCAL_HOST_ORIGIN_PATTERN = /^http:\/\/(localhost|127\.0\.0\.1)(:\d{1,5})?$/;
|
|
6
|
+
var LOCAL_APP_HOSTNAME_PATTERN = /^(localhost|127\.0\.0\.1|.+\.localhost)$/;
|
|
7
|
+
function isLocalAppHostname(hostname) {
|
|
8
|
+
return LOCAL_APP_HOSTNAME_PATTERN.test(hostname);
|
|
9
|
+
}
|
|
10
|
+
function isAllowedHostOrigin(origin, allowLocalHostOrigins) {
|
|
11
|
+
if (EXACT_ALLOWED_HOST_ORIGINS.has(origin)) return true;
|
|
12
|
+
return allowLocalHostOrigins && LOCAL_HOST_ORIGIN_PATTERN.test(origin);
|
|
13
|
+
}
|
|
14
|
+
function parseProtocolMessage(data) {
|
|
15
|
+
if (typeof data !== "object" || data === null) return null;
|
|
16
|
+
const record = data;
|
|
17
|
+
if (record.protocol !== PROTOCOL) return null;
|
|
18
|
+
const type = record.type;
|
|
19
|
+
if (typeof type !== "string") return null;
|
|
20
|
+
let errorMessage = null;
|
|
21
|
+
const error = record.error;
|
|
22
|
+
if (typeof error === "object" && error !== null) {
|
|
23
|
+
const message = error.message;
|
|
24
|
+
errorMessage = typeof message === "string" ? message : "Something went wrong running this tool.";
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
type,
|
|
28
|
+
id: typeof record.id === "string" ? record.id : null,
|
|
29
|
+
result: record.result,
|
|
30
|
+
errorMessage
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
var SdkCore = class {
|
|
34
|
+
// Wired by the page entry: remove the standalone note, log tool errors,
|
|
35
|
+
// reload on a new deployed version.
|
|
36
|
+
onHostReady = null;
|
|
37
|
+
onToolError = null;
|
|
38
|
+
onNewVersion = null;
|
|
39
|
+
post;
|
|
40
|
+
allowLocalHostOrigins;
|
|
41
|
+
hostOrigin = null;
|
|
42
|
+
watches = /* @__PURE__ */ new Map();
|
|
43
|
+
sequence = 0;
|
|
44
|
+
constructor(post, options = {}) {
|
|
45
|
+
this.post = post;
|
|
46
|
+
this.allowLocalHostOrigins = options.allowLocalHostOrigins ?? false;
|
|
47
|
+
}
|
|
48
|
+
get hostReady() {
|
|
49
|
+
return this.hostOrigin !== null;
|
|
50
|
+
}
|
|
51
|
+
// params is typed loose on purpose: app authors call this from untyped
|
|
52
|
+
// JavaScript, and a null/omitted params should mean "no params", not a
|
|
53
|
+
// crash.
|
|
54
|
+
watch(tool, params, callback) {
|
|
55
|
+
if (this.watches.size >= MAX_WATCHES) {
|
|
56
|
+
this.onToolError?.(tool, `Watch limit reached (${String(MAX_WATCHES)}); this watch will not receive data. Unsubscribe watches you no longer need.`);
|
|
57
|
+
return () => void 0;
|
|
58
|
+
}
|
|
59
|
+
this.sequence += 1;
|
|
60
|
+
const id = `w${this.sequence}`;
|
|
61
|
+
this.watches.set(id, { tool, params: params ?? {}, callback, lastDeliveredJson: null });
|
|
62
|
+
this.requestWatch(id);
|
|
63
|
+
return () => {
|
|
64
|
+
this.watches.delete(id);
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
// fromParent must be the caller's verification that the browser event's
|
|
68
|
+
// source is the actual parent window (event.source === window.parent) —
|
|
69
|
+
// origin alone is not enough, because a sibling frame on an allowed
|
|
70
|
+
// origin could otherwise speak for the host.
|
|
71
|
+
handleMessage(origin, fromParent, data) {
|
|
72
|
+
if (!fromParent) return;
|
|
73
|
+
const message = parseProtocolMessage(data);
|
|
74
|
+
if (message === null) return;
|
|
75
|
+
if (message.type === "hello") {
|
|
76
|
+
this.handleHello(origin);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (this.hostOrigin === null || origin !== this.hostOrigin) return;
|
|
80
|
+
if (message.type === "result") {
|
|
81
|
+
this.handleResult(message);
|
|
82
|
+
} else if (message.type === "data-changed") {
|
|
83
|
+
this.requestAllWatches();
|
|
84
|
+
} else if (message.type === "new-version") {
|
|
85
|
+
this.onNewVersion?.();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
handleHello(origin) {
|
|
89
|
+
if (this.hostOrigin !== null) return;
|
|
90
|
+
if (!isAllowedHostOrigin(origin, this.allowLocalHostOrigins)) return;
|
|
91
|
+
this.hostOrigin = origin;
|
|
92
|
+
this.onHostReady?.();
|
|
93
|
+
this.requestAllWatches();
|
|
94
|
+
}
|
|
95
|
+
handleResult(message) {
|
|
96
|
+
const entry = message.id === null ? void 0 : this.watches.get(message.id);
|
|
97
|
+
if (entry === void 0) return;
|
|
98
|
+
if (message.errorMessage !== null) {
|
|
99
|
+
const json2 = JSON.stringify({ error: message.errorMessage });
|
|
100
|
+
if (json2 === entry.lastDeliveredJson) return;
|
|
101
|
+
entry.lastDeliveredJson = json2;
|
|
102
|
+
this.onToolError?.(entry.tool, message.errorMessage);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const json = JSON.stringify({ result: message.result });
|
|
106
|
+
if (json === entry.lastDeliveredJson) return;
|
|
107
|
+
entry.lastDeliveredJson = json;
|
|
108
|
+
entry.callback(message.result);
|
|
109
|
+
}
|
|
110
|
+
requestAllWatches() {
|
|
111
|
+
for (const id of this.watches.keys()) this.requestWatch(id);
|
|
112
|
+
}
|
|
113
|
+
requestWatch(id) {
|
|
114
|
+
if (this.hostOrigin === null) return;
|
|
115
|
+
const entry = this.watches.get(id);
|
|
116
|
+
if (entry === void 0) return;
|
|
117
|
+
this.post(
|
|
118
|
+
{ protocol: PROTOCOL, type: "call", id, tool: entry.tool, params: entry.params },
|
|
119
|
+
this.hostOrigin
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
// src/entrypoint.ts
|
|
125
|
+
var STANDALONE_NOTE_ATTRIBUTE = "data-driggsby-standalone-note";
|
|
126
|
+
var STANDALONE_NOTE_DELAY_MS = 800;
|
|
127
|
+
function showStandaloneNote() {
|
|
128
|
+
if (document.querySelector(`[${STANDALONE_NOTE_ATTRIBUTE}]`) !== null) return;
|
|
129
|
+
const note = document.createElement("div");
|
|
130
|
+
note.setAttribute(STANDALONE_NOTE_ATTRIBUTE, "");
|
|
131
|
+
note.style.cssText = "position:fixed;left:16px;right:16px;bottom:16px;max-width:480px;margin:0 auto;padding:14px 18px;border-radius:12px;background:#1c1c1e;color:#f2f2f4;font:14px/1.5 system-ui,sans-serif;box-shadow:0 8px 30px rgba(0,0,0,0.35);z-index:2147483647";
|
|
132
|
+
note.textContent = "This dashboard shows your data when you open it from Driggsby. Head to your Driggsby console and open it from the Dashboards page.";
|
|
133
|
+
document.body.appendChild(note);
|
|
134
|
+
}
|
|
135
|
+
function removeStandaloneNote() {
|
|
136
|
+
document.querySelector(`[${STANDALONE_NOTE_ATTRIBUTE}]`)?.remove();
|
|
137
|
+
}
|
|
138
|
+
function whenBodyReady(callback) {
|
|
139
|
+
if (document.readyState === "loading") {
|
|
140
|
+
document.addEventListener("DOMContentLoaded", callback, { once: true });
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
callback();
|
|
144
|
+
}
|
|
145
|
+
var core = new SdkCore((message, targetOrigin) => {
|
|
146
|
+
window.parent.postMessage(message, targetOrigin);
|
|
147
|
+
}, {
|
|
148
|
+
// A localhost host origin is only ever legitimate when this app is
|
|
149
|
+
// itself served from a local runtime; the production bundle pins to
|
|
150
|
+
// exactly the Driggsby console origin.
|
|
151
|
+
allowLocalHostOrigins: isLocalAppHostname(window.location.hostname)
|
|
152
|
+
});
|
|
153
|
+
core.onHostReady = removeStandaloneNote;
|
|
154
|
+
core.onNewVersion = () => {
|
|
155
|
+
window.location.reload();
|
|
156
|
+
};
|
|
157
|
+
core.onToolError = (tool, message) => {
|
|
158
|
+
console.error(`Driggsby: the ${tool} call didn't work \u2014 ${message}`);
|
|
159
|
+
};
|
|
160
|
+
window.addEventListener("message", (event) => {
|
|
161
|
+
core.handleMessage(event.origin, event.source === window.parent, event.data);
|
|
162
|
+
});
|
|
163
|
+
window.driggsby = {
|
|
164
|
+
watch: (tool, params, callback) => core.watch(tool, params, callback)
|
|
165
|
+
};
|
|
166
|
+
if (window.parent === window) {
|
|
167
|
+
whenBodyReady(showStandaloneNote);
|
|
168
|
+
} else {
|
|
169
|
+
window.parent.postMessage({ protocol: PROTOCOL, type: "ready" }, "*");
|
|
170
|
+
window.setTimeout(() => {
|
|
171
|
+
if (!core.hostReady) whenBodyReady(showStandaloneNote);
|
|
172
|
+
}, STANDALONE_NOTE_DELAY_MS);
|
|
173
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { MAX_WATCHES, PROTOCOL, SdkCore, isAllowedHostOrigin, isLocalAppHostname, type PostFunction, type WatchCallback, } from "./sdk-core.ts";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Public exports of @driggsby/sdk for bundler users and for the driggsby
|
|
2
|
+
// CLI's local dev host. Browser apps normally don't import this package at
|
|
3
|
+
// all — they load the runtime with one tag:
|
|
4
|
+
//
|
|
5
|
+
// <script type="module" src="/-/driggsby-sdk.js"></script>
|
|
6
|
+
//
|
|
7
|
+
// which Driggsby serves on the app's own origin (and `driggsby dev` serves
|
|
8
|
+
// locally from this package's bundled copy at dist/driggsby-sdk.js).
|
|
9
|
+
export { MAX_WATCHES, PROTOCOL, SdkCore, isAllowedHostOrigin, isLocalAppHostname, } from "./sdk-core.js";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export declare const PROTOCOL = "driggsby-sdk/1";
|
|
2
|
+
export declare const MAX_WATCHES = 32;
|
|
3
|
+
export declare function isLocalAppHostname(hostname: string): boolean;
|
|
4
|
+
export declare function isAllowedHostOrigin(origin: string, allowLocalHostOrigins: boolean): boolean;
|
|
5
|
+
export type WatchCallback = (result: unknown) => void;
|
|
6
|
+
export type PostFunction = (message: Record<string, unknown>, targetOrigin: string) => void;
|
|
7
|
+
export declare class SdkCore {
|
|
8
|
+
onHostReady: (() => void) | null;
|
|
9
|
+
onToolError: ((tool: string, message: string) => void) | null;
|
|
10
|
+
onNewVersion: (() => void) | null;
|
|
11
|
+
private readonly post;
|
|
12
|
+
private readonly allowLocalHostOrigins;
|
|
13
|
+
private hostOrigin;
|
|
14
|
+
private readonly watches;
|
|
15
|
+
private sequence;
|
|
16
|
+
constructor(post: PostFunction, options?: {
|
|
17
|
+
allowLocalHostOrigins?: boolean;
|
|
18
|
+
});
|
|
19
|
+
get hostReady(): boolean;
|
|
20
|
+
watch(tool: string, params: Record<string, unknown> | null | undefined, callback: WatchCallback): () => void;
|
|
21
|
+
handleMessage(origin: string, fromParent: boolean, data: unknown): void;
|
|
22
|
+
private handleHello;
|
|
23
|
+
private handleResult;
|
|
24
|
+
private requestAllWatches;
|
|
25
|
+
private requestWatch;
|
|
26
|
+
}
|
package/dist/sdk-core.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// The Driggsby SDK protocol core. A Driggsby app loads the SDK on its own
|
|
2
|
+
// origin and talks to the embedding Driggsby host page over postMessage,
|
|
3
|
+
// speaking the frozen driggsby-sdk/1 protocol. This module is pure protocol
|
|
4
|
+
// state — no DOM, no window — so `node --test` can exercise the
|
|
5
|
+
// security-sensitive parts directly: the host-origin allowlist, the
|
|
6
|
+
// first-valid-hello pin, and the parent-source requirement.
|
|
7
|
+
//
|
|
8
|
+
// The whole public surface an app sees is:
|
|
9
|
+
// driggsby.watch(tool, params, callback) -> unsubscribe()
|
|
10
|
+
// Every read is a live subscription: the host announces "data-changed" and
|
|
11
|
+
// each watch re-runs; results are deduped by JSON identity so unchanged
|
|
12
|
+
// data never re-renders.
|
|
13
|
+
//
|
|
14
|
+
// The production copy of this runtime is the one Driggsby serves to every
|
|
15
|
+
// deployed app at /-/driggsby-sdk.js on the app's own origin; that copy is
|
|
16
|
+
// canonical. This package carries the same runtime so `driggsby dev` can
|
|
17
|
+
// serve it locally, and so bundler users can import the types. Protocol v1
|
|
18
|
+
// is frozen — the contract tests beside this file pin the exact wire
|
|
19
|
+
// messages, and the protocol version string is the drift tripwire.
|
|
20
|
+
export const PROTOCOL = "driggsby-sdk/1";
|
|
21
|
+
// Generous ceiling on concurrent subscriptions per app; a real dashboard
|
|
22
|
+
// uses a handful.
|
|
23
|
+
export const MAX_WATCHES = 32;
|
|
24
|
+
// The only hosts allowed to drive an embedded app: the Driggsby console,
|
|
25
|
+
// or — ONLY when the SDK itself is being served from a local runtime — a
|
|
26
|
+
// local-dev host page. Never learned from the first message — a hello from
|
|
27
|
+
// anywhere else is ignored. The local branch is gated on the app's own
|
|
28
|
+
// hostname so the production bundle pins to exactly one possible host
|
|
29
|
+
// origin.
|
|
30
|
+
const EXACT_ALLOWED_HOST_ORIGINS = new Set(["https://app.driggsby.com"]);
|
|
31
|
+
const LOCAL_HOST_ORIGIN_PATTERN = /^http:\/\/(localhost|127\.0\.0\.1)(:\d{1,5})?$/;
|
|
32
|
+
const LOCAL_APP_HOSTNAME_PATTERN = /^(localhost|127\.0\.0\.1|.+\.localhost)$/;
|
|
33
|
+
export function isLocalAppHostname(hostname) {
|
|
34
|
+
return LOCAL_APP_HOSTNAME_PATTERN.test(hostname);
|
|
35
|
+
}
|
|
36
|
+
export function isAllowedHostOrigin(origin, allowLocalHostOrigins) {
|
|
37
|
+
if (EXACT_ALLOWED_HOST_ORIGINS.has(origin))
|
|
38
|
+
return true;
|
|
39
|
+
return allowLocalHostOrigins && LOCAL_HOST_ORIGIN_PATTERN.test(origin);
|
|
40
|
+
}
|
|
41
|
+
// Untrusted wire data -> a normalized message, or null when it is not ours.
|
|
42
|
+
function parseProtocolMessage(data) {
|
|
43
|
+
if (typeof data !== "object" || data === null)
|
|
44
|
+
return null;
|
|
45
|
+
const record = data;
|
|
46
|
+
if (record.protocol !== PROTOCOL)
|
|
47
|
+
return null;
|
|
48
|
+
const type = record.type;
|
|
49
|
+
if (typeof type !== "string")
|
|
50
|
+
return null;
|
|
51
|
+
let errorMessage = null;
|
|
52
|
+
const error = record.error;
|
|
53
|
+
if (typeof error === "object" && error !== null) {
|
|
54
|
+
const message = error.message;
|
|
55
|
+
errorMessage = typeof message === "string" ? message : "Something went wrong running this tool.";
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
type,
|
|
59
|
+
id: typeof record.id === "string" ? record.id : null,
|
|
60
|
+
result: record.result,
|
|
61
|
+
errorMessage,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
export class SdkCore {
|
|
65
|
+
// Wired by the page entry: remove the standalone note, log tool errors,
|
|
66
|
+
// reload on a new deployed version.
|
|
67
|
+
onHostReady = null;
|
|
68
|
+
onToolError = null;
|
|
69
|
+
onNewVersion = null;
|
|
70
|
+
post;
|
|
71
|
+
allowLocalHostOrigins;
|
|
72
|
+
hostOrigin = null;
|
|
73
|
+
watches = new Map();
|
|
74
|
+
sequence = 0;
|
|
75
|
+
constructor(post, options = {}) {
|
|
76
|
+
this.post = post;
|
|
77
|
+
this.allowLocalHostOrigins = options.allowLocalHostOrigins ?? false;
|
|
78
|
+
}
|
|
79
|
+
get hostReady() {
|
|
80
|
+
return this.hostOrigin !== null;
|
|
81
|
+
}
|
|
82
|
+
// params is typed loose on purpose: app authors call this from untyped
|
|
83
|
+
// JavaScript, and a null/omitted params should mean "no params", not a
|
|
84
|
+
// crash.
|
|
85
|
+
watch(tool, params, callback) {
|
|
86
|
+
// Watches are registered by arbitrary app code; a watch() call that
|
|
87
|
+
// leaked into a render loop would otherwise grow forever and turn
|
|
88
|
+
// every data-changed into an unbounded fan-out of real tool calls.
|
|
89
|
+
// Past the cap: report it and hand back a no-op unsubscribe.
|
|
90
|
+
if (this.watches.size >= MAX_WATCHES) {
|
|
91
|
+
this.onToolError?.(tool, `Watch limit reached (${String(MAX_WATCHES)}); this watch will not receive data. Unsubscribe watches you no longer need.`);
|
|
92
|
+
return () => undefined;
|
|
93
|
+
}
|
|
94
|
+
this.sequence += 1;
|
|
95
|
+
const id = `w${this.sequence}`;
|
|
96
|
+
this.watches.set(id, { tool, params: params ?? {}, callback, lastDeliveredJson: null });
|
|
97
|
+
// Before the host pins, requestWatch posts nothing; the pinning hello
|
|
98
|
+
// fires every registered watch, so pre-hello watches are simply held.
|
|
99
|
+
this.requestWatch(id);
|
|
100
|
+
return () => {
|
|
101
|
+
this.watches.delete(id);
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
// fromParent must be the caller's verification that the browser event's
|
|
105
|
+
// source is the actual parent window (event.source === window.parent) —
|
|
106
|
+
// origin alone is not enough, because a sibling frame on an allowed
|
|
107
|
+
// origin could otherwise speak for the host.
|
|
108
|
+
handleMessage(origin, fromParent, data) {
|
|
109
|
+
if (!fromParent)
|
|
110
|
+
return;
|
|
111
|
+
const message = parseProtocolMessage(data);
|
|
112
|
+
if (message === null)
|
|
113
|
+
return;
|
|
114
|
+
if (message.type === "hello") {
|
|
115
|
+
this.handleHello(origin);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
// Everything after the handshake must come from the pinned host origin.
|
|
119
|
+
if (this.hostOrigin === null || origin !== this.hostOrigin)
|
|
120
|
+
return;
|
|
121
|
+
if (message.type === "result") {
|
|
122
|
+
this.handleResult(message);
|
|
123
|
+
}
|
|
124
|
+
else if (message.type === "data-changed") {
|
|
125
|
+
this.requestAllWatches();
|
|
126
|
+
}
|
|
127
|
+
else if (message.type === "new-version") {
|
|
128
|
+
this.onNewVersion?.();
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
handleHello(origin) {
|
|
132
|
+
if (this.hostOrigin !== null)
|
|
133
|
+
return; // pinned; later hellos are ignored
|
|
134
|
+
if (!isAllowedHostOrigin(origin, this.allowLocalHostOrigins))
|
|
135
|
+
return;
|
|
136
|
+
this.hostOrigin = origin;
|
|
137
|
+
this.onHostReady?.();
|
|
138
|
+
this.requestAllWatches();
|
|
139
|
+
}
|
|
140
|
+
handleResult(message) {
|
|
141
|
+
const entry = message.id === null ? undefined : this.watches.get(message.id);
|
|
142
|
+
if (entry === undefined)
|
|
143
|
+
return;
|
|
144
|
+
if (message.errorMessage !== null) {
|
|
145
|
+
const json = JSON.stringify({ error: message.errorMessage });
|
|
146
|
+
if (json === entry.lastDeliveredJson)
|
|
147
|
+
return;
|
|
148
|
+
entry.lastDeliveredJson = json;
|
|
149
|
+
this.onToolError?.(entry.tool, message.errorMessage);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
// Wrapping in an object keeps stringify total: even `result: undefined`
|
|
153
|
+
// serializes (to '{}') instead of returning undefined.
|
|
154
|
+
const json = JSON.stringify({ result: message.result });
|
|
155
|
+
if (json === entry.lastDeliveredJson)
|
|
156
|
+
return;
|
|
157
|
+
entry.lastDeliveredJson = json;
|
|
158
|
+
entry.callback(message.result);
|
|
159
|
+
}
|
|
160
|
+
requestAllWatches() {
|
|
161
|
+
for (const id of this.watches.keys())
|
|
162
|
+
this.requestWatch(id);
|
|
163
|
+
}
|
|
164
|
+
requestWatch(id) {
|
|
165
|
+
if (this.hostOrigin === null)
|
|
166
|
+
return;
|
|
167
|
+
const entry = this.watches.get(id);
|
|
168
|
+
if (entry === undefined)
|
|
169
|
+
return;
|
|
170
|
+
this.post({ protocol: PROTOCOL, type: "call", id, tool: entry.tool, params: entry.params }, this.hostOrigin);
|
|
171
|
+
}
|
|
172
|
+
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@driggsby/sdk",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "The
|
|
5
|
-
"license": "
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "The browser SDK for Driggsby apps: live financial data through driggsby.watch().",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "git+https://github.com/driggsby/driggsby.git"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://driggsby.com",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./driggsby-sdk.js": "./dist/driggsby-sdk.js"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"!dist/.tsbuildinfo",
|
|
22
|
+
"LICENSE",
|
|
23
|
+
"README.md"
|
|
24
|
+
],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsc -p tsconfig.build.json && esbuild src/entrypoint.ts --bundle --format=esm --target=es2022 --outfile=dist/driggsby-sdk.js --log-level=warning",
|
|
27
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
28
|
+
"pretest": "npm run build",
|
|
29
|
+
"test": "node --test \"src/**/*.test.ts\""
|
|
30
|
+
},
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">=18"
|
|
9
33
|
}
|
|
10
34
|
}
|