@open-artifact/host-plugin 0.0.0-stage → 0.13.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 +202 -0
- package/NOTICE +5 -0
- package/README.md +191 -2
- package/package.json +42 -4
- package/src/contract.d.ts +264 -0
- package/src/contract.js +1000 -0
- package/src/index.d.ts +832 -0
- package/src/index.js +338 -0
- package/src/testing.d.ts +169 -0
- package/src/testing.js +364 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,192 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @open-artifact/host-plugin
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The plugin API of the Open Artifact host: types, `definePlugin`, a few helpers, and a test kit. It comes with the
|
|
4
|
+
Open Artifact source and is not published on npm. A plugin that imports it runs from any folder without installing
|
|
5
|
+
it: the host hands every plugin its own copy (on Node when the plugin loads, on Cloudflare when the Worker is built).
|
|
6
|
+
A test that uses the test kit runs from inside the source folder. The operator's and author's guide,
|
|
7
|
+
with worked examples, is [docs/plugins.md](../../docs/plugins.md).
|
|
8
|
+
|
|
9
|
+
A **host plugin** adds to a self-hosted Open Artifact site what only your organisation knows: how people sign in, who
|
|
10
|
+
may open what, what must be refused, and where to tell other systems what happened. It is a plain object exported by an
|
|
11
|
+
ES module, listed by the operator in `OA_PLUGINS` (or `plugins.json` in the Node host's data directory). It runs inside
|
|
12
|
+
the host, on Node and on Cloudflare Workers alike, so it uses only web APIs (`fetch`, `crypto.subtle`), never `node:`.
|
|
13
|
+
|
|
14
|
+
## The smallest plugin
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
export default {
|
|
18
|
+
id: 'hello',
|
|
19
|
+
apiVersion: 1,
|
|
20
|
+
on: {
|
|
21
|
+
'user.signed_in': (e, ctx) => ctx.log(`${e.user.name} signed in`),
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
With TypeScript, `definePlugin({...})` from this package types `ctx.config` from your `config` schema.
|
|
27
|
+
|
|
28
|
+
## Hooks
|
|
29
|
+
|
|
30
|
+
Every hook is `(input, ctx)` and optional.
|
|
31
|
+
|
|
32
|
+
- `auth.authenticate`: who a request comes from by itself (a proxy header, your JWT); fails closed. `auth.recheck`
|
|
33
|
+
(default true) asks it again on every request of the session it made.
|
|
34
|
+
- `auth.signIn` `{label, start, finish}`: a sign-in button and its round trip; fails closed.
|
|
35
|
+
- `auth.identity`: after any sign-in, add groups, rename, or `{reject}`; fails closed.
|
|
36
|
+
- `auth.signOutUrl`: where to send someone after signing out.
|
|
37
|
+
- `authz.role`: a person's role on an artifact, after the host's own shares; `authz.cacheSeconds` how long it may be reused (default 30); fails closed (no access).
|
|
38
|
+
- `authz.artifacts`: more artifact ids to list on someone's home page.
|
|
39
|
+
- `guard`: `{deny: sentence}` before an action (create, import, delete, export, share, link, MCP, agent ask); fails closed.
|
|
40
|
+
- `on[event]`: after the fact, in the background (`user.created`, `artifact.created`, `share.invited`, `audit`, …); fails open.
|
|
41
|
+
- `wake.deliver`: deliver a request for the owner's agent elsewhere; fails open (it goes to the outbox).
|
|
42
|
+
- `routes`: HTTP routes at `/plugins/<id>/<path>`; `public: true` for webhooks from other servers.
|
|
43
|
+
- `ui`: links in the account menu or on the home page, and the site's brand.
|
|
44
|
+
|
|
45
|
+
`ctx` has `config`, `log`, `fetch` (stopped at the time limit), `kv` (a small private store), `now`, `signal`, and
|
|
46
|
+
`host` (`users`, `artifacts.get`/`.list`, `access.invalidate`, `audit`, `supports(name)`). Each call has 5 seconds (the operator's
|
|
47
|
+
`timeoutMs`). A hook that throws, runs out of time or answers the wrong shape is logged; the policy above decides
|
|
48
|
+
what happens next.
|
|
49
|
+
|
|
50
|
+
Helpers: `json`, `readJson`, `readText`, `HttpError`, `timingSafeEqual`, `signHmacSha256`, `verifyHmacSha256`,
|
|
51
|
+
`verifyJwt` (RS256, ES256, HS256, with a JWKS URL or a key).
|
|
52
|
+
|
|
53
|
+
What each hook sees and when:
|
|
54
|
+
|
|
55
|
+
- `auth.authenticate`'s answer becomes a session, and the plugin is asked again on every request of it: the same
|
|
56
|
+
subject keeps it, another identity replaces it, `null` ends it, and a throw makes that one request anonymous (the
|
|
57
|
+
session is kept). With `recheck: false` it is asked only when there is no session. A person the host refuses (its
|
|
58
|
+
sign-up rule, `auth.identity`) gets the sign-in page saying why, or a 401 `sign_in_refused` on the API.
|
|
59
|
+
- `authz.role` answers are reused per artifact, person and host role for `cacheSeconds`. `ctx.host.access.invalidate(id)`
|
|
60
|
+
forgets one artifact's answers, `invalidate()` all of them (open pages pick the change up at once).
|
|
61
|
+
- Guards cover the paths in `GUARD_PATHS` (`packages/host/src/core/plugins/hooks.ts`). PDF and PowerPoint exports are
|
|
62
|
+
made in the page, not by the host, so `artifact.export` sees only the archive (`.zip`) downloads.
|
|
63
|
+
- `wake.deliver` is offered a wake that carries requests only (the Ask pane); one with comment threads goes to the
|
|
64
|
+
outbox always, because the agent answers in those threads from the owner's computer.
|
|
65
|
+
- `ctx.host.artifacts.list({owner?, after?, limit?})` pages the site's artifacts outside the trash by id (50 a page,
|
|
66
|
+
at most 200). It is for small sites; keep what you build from it in `ctx.kv`.
|
|
67
|
+
- Routes: the principal is the signed-in person (a request with `Authorization: Bearer` is anonymous), bodies are at
|
|
68
|
+
most 1 MiB, and a write must come from the site's own origin unless the route is `public`.
|
|
69
|
+
- `ui` links are a path on the site or an `https:`/`http:` address; anything else stops the host at start-up. Pages
|
|
70
|
+
also leave out `http:` addresses on other hosts. Account-menu links show to signed-in people only.
|
|
71
|
+
|
|
72
|
+
## Settings
|
|
73
|
+
|
|
74
|
+
`config` in the plugin is a schema; the operator's entry gives the values:
|
|
75
|
+
|
|
76
|
+
```js
|
|
77
|
+
config: {
|
|
78
|
+
url: { type: 'string', required: true },
|
|
79
|
+
secret: { type: 'secret' }, // never shown in `plugin list` or the admin API
|
|
80
|
+
groups: { type: 'map', default: {} }, // {"name": "value"}, strings only
|
|
81
|
+
rules: { type: 'json' }, // any JSON value
|
|
82
|
+
retries: { type: 'number', default: 3 }, // also 'boolean' and 'string[]'
|
|
83
|
+
},
|
|
84
|
+
validate(config) { // optional, synchronous: nothing, a sentence or a list of them
|
|
85
|
+
if (!config.url.startsWith('https:')) return '"url" must be an https: address.';
|
|
86
|
+
},
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Any value can be read from an environment variable with `{"env": "NAME"}` (a `map` or `json` one as JSON text). The
|
|
90
|
+
host checks every setting and runs `validate` when it starts; a problem stops it with `Plugin "<id>", <setting>: …` or
|
|
91
|
+
`Plugin "<id>": <your sentence>` (a promise from `validate` is a problem too: it must answer at once).
|
|
92
|
+
|
|
93
|
+
## The operator's list
|
|
94
|
+
|
|
95
|
+
```json
|
|
96
|
+
[
|
|
97
|
+
{ "use": "@acme/oa-sso", "config": { "clientSecret": { "env": "SSO_SECRET" } } },
|
|
98
|
+
{ "use": "./static-groups.mjs", "config": { "groups": { "staff": "@acme.com" }, "roles": { "staff": "viewer" } }, "cacheSeconds": 60 },
|
|
99
|
+
{ "use": "./audit-feed.mjs", "optional": true, "timeoutMs": 2000, "enabled": false }
|
|
100
|
+
]
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
| Field | Meaning |
|
|
104
|
+
| --- | --- |
|
|
105
|
+
| `use` | A package name or a path to the module (on Cloudflare: the name it is bundled under). |
|
|
106
|
+
| `config` | The plugin's settings. |
|
|
107
|
+
| `enabled` | `false` keeps it listed but off. |
|
|
108
|
+
| `optional` | When it cannot load or its settings are wrong, start without it (logged) instead of refusing to start. |
|
|
109
|
+
| `timeoutMs` | Each hook call's time limit, default 5000, at most 60000. |
|
|
110
|
+
| `cacheSeconds` | How long role answers are reused (0 to 86400); wins over the plugin's `authz.cacheSeconds` (default 30). |
|
|
111
|
+
|
|
112
|
+
When a plugin fails: sign-in, roles and guards fail closed (no sign-in, no access, refused with "This could not be
|
|
113
|
+
checked just now (<plugin name>). Try again in a moment."); events and `wake.deliver` fail open (logged; the request
|
|
114
|
+
goes on). Every failure of a closed hook is in the audit log as `plugin.failed` `{plugin, hook, reason, error}`. A site
|
|
115
|
+
admin sees the running plugins, their hooks and setting names at `GET /api/open-artifact.admin.plugins`, and the audit
|
|
116
|
+
log at `GET /api/open-artifact.admin.audit` (filters `artifact`, `actor`, `action`, `before`, `limit`).
|
|
117
|
+
|
|
118
|
+
## Running plugins on Node
|
|
119
|
+
|
|
120
|
+
The list is `OA_PLUGINS` (JSON), which wins over the file `OA_PLUGINS_FILE` (default `<data dir>/plugins.json`).
|
|
121
|
+
Relative paths in the file start at the file's folder; in `OA_PLUGINS`, at the working folder. A package is looked for
|
|
122
|
+
in `<data dir>/plugins/node_modules` (install it with `npm install --prefix <data dir>/plugins <package>`), then where
|
|
123
|
+
the host itself can import it.
|
|
124
|
+
|
|
125
|
+
From the source folder, `open-artifact-host` is `node packages/host/src/node/main.ts`:
|
|
126
|
+
|
|
127
|
+
```
|
|
128
|
+
open-artifact-host plugin list the plugins, their hooks and setting names, and what is wrong
|
|
129
|
+
open-artifact-host plugin check the same; exits 1 when the host would not start
|
|
130
|
+
open-artifact-host plugin add <package or path> [--config <json>] [--optional] [--disabled] [--timeout-ms <n>] [--cache-seconds <n>]
|
|
131
|
+
open-artifact-host plugin remove|enable|disable <id or use>
|
|
132
|
+
open-artifact-host plugin set <id or use> [<setting>=<value>…] [--secret <setting>=<ENV_NAME>…] [--timeout-ms <n>] [--cache-seconds <n>] [--optional | --required]
|
|
133
|
+
open-artifact-host plugin unset <id or use> <setting>…
|
|
134
|
+
open-artifact-host audit [--artifact <id>] [--actor <id>] [--action <prefix>] [--before <time>] [--limit <n>] [--json]
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
`add`, `remove`, `enable`, `disable`, `set` and `unset` change the list file (written with mode 0600) and say "The
|
|
138
|
+
change takes effect when the host restarts."; they refuse while `OA_PLUGINS` is set. A relative path given to `add`
|
|
139
|
+
starts where you typed it, and is stored so it keeps pointing there. `check` ends with "All N plugin(s) that are on can
|
|
140
|
+
run." or "The host would not start: <the problems>". At start-up the host logs `plugins: <ids> (from <source>)`. A
|
|
141
|
+
plugin in TypeScript (`.ts`) loads from a path outside `node_modules` only (Node does not strip types inside
|
|
142
|
+
`node_modules`); publish packages as JavaScript.
|
|
143
|
+
|
|
144
|
+
## Running plugins on Cloudflare
|
|
145
|
+
|
|
146
|
+
A Worker cannot load code at run time, so plugins are bundled when it is built. `deploy/cloudflare/plugins.mjs`
|
|
147
|
+
exports them by name:
|
|
148
|
+
|
|
149
|
+
```js
|
|
150
|
+
import headerAuth from '../../examples/host-plugins/header-auth.mjs';
|
|
151
|
+
import acmeSso from '@acme/oa-sso';
|
|
152
|
+
export default { 'header-auth': headerAuth, 'acme-sso': acmeSso };
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
`OA_PLUGINS` (a var, or a secret with `wrangler secret put OA_PLUGINS`) then picks and configures them, by those names:
|
|
156
|
+
`[{"use":"header-auth","config":{"secret":{"env":"PROXY_SECRET"}}}]`, with `PROXY_SECRET` a Worker secret. Changing
|
|
157
|
+
`OA_PLUGINS` needs no build; adding a module does (`npm run build`; `OA_PLUGINS_MODULE=<file>` bundles another file
|
|
158
|
+
instead of `plugins.mjs`). A name that is not bundled stops the Worker with "The plugin "x" could not be loaded: it is
|
|
159
|
+
not bundled into this Worker: add it to deploy/cloudflare/plugins.mjs as "x" and build again (bundled: …)".
|
|
160
|
+
|
|
161
|
+
Differences from Node:
|
|
162
|
+
|
|
163
|
+
- There is no `plugin` command; the admin API above lists what runs.
|
|
164
|
+
- Each isolate (the Worker, and each artifact's Durable Object) runs its own copy of the plugins: keep state in
|
|
165
|
+
`ctx.kv`, not in module variables. Role answers in a room are the room's own; `invalidate` reaches the rooms that
|
|
166
|
+
are awake, and a page opened elsewhere sees the change within about 2 seconds.
|
|
167
|
+
- `ctx.fetch` and background work (events) end with the request's `waitUntil`; a plugin cannot keep timers running.
|
|
168
|
+
|
|
169
|
+
## Stability
|
|
170
|
+
|
|
171
|
+
`apiVersion` is a major version. Within it, the host only adds: new hooks, events, guard actions, input fields and
|
|
172
|
+
`ctx` members, so a plugin must ignore what it does not know (an unknown principal `kind`, an unknown guard action:
|
|
173
|
+
answer nothing). Ask `ctx.host.supports('on.share.invited')` before relying on something newer. A host refuses a plugin
|
|
174
|
+
written for a newer major with a message saying which release to use, and runs plugins of the previous major through
|
|
175
|
+
an adapter for at least one major release.
|
|
176
|
+
|
|
177
|
+
## Testing
|
|
178
|
+
|
|
179
|
+
```js
|
|
180
|
+
import { createPluginHarness } from '@open-artifact/host-plugin/testing';
|
|
181
|
+
import plugin from './my-plugin.mjs';
|
|
182
|
+
|
|
183
|
+
const h = createPluginHarness(plugin, { config: { url: 'https://perms.example.com' }, fetch: async () => Response.json({ role: 'editor' }) });
|
|
184
|
+
const artifact = h.addArtifact();
|
|
185
|
+
const { role } = await h.role({ principal: h.userPrincipal(h.addUser({ email: 'ann@example.com' })), artifact, role: null });
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
The harness checks the plugin and its settings with the host's rules, and calls each hook as the host does (time
|
|
189
|
+
limit, failure policy, answer checks). It records `logs`, `audits`, `fetches`, `failures` and `invalidated`, and has an
|
|
190
|
+
in-memory `kv`. `h.request('POST', '/invalidate', {body})` calls a route. See `examples/host-plugins/` in the
|
|
191
|
+
repository for six plugins; their tests are `examples/host-plugins/company-jwt.test.mjs` and
|
|
192
|
+
`packages/host-plugin/test/examples.test.ts`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-artifact/host-plugin",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.13.0",
|
|
4
|
+
"description": "Write plugins for the Open Artifact cloud host: the plugin API's types, definePlugin, helpers for signatures and JWTs, and a test kit that runs a plugin without a host.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./src/index.d.ts",
|
|
9
|
+
"default": "./src/index.js"
|
|
10
|
+
},
|
|
11
|
+
"./contract": {
|
|
12
|
+
"types": "./src/contract.d.ts",
|
|
13
|
+
"default": "./src/contract.js"
|
|
14
|
+
},
|
|
15
|
+
"./testing": {
|
|
16
|
+
"types": "./src/testing.d.ts",
|
|
17
|
+
"default": "./src/testing.js"
|
|
18
|
+
},
|
|
19
|
+
"./package.json": "./package.json"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"src",
|
|
23
|
+
"LICENSE",
|
|
24
|
+
"NOTICE"
|
|
25
|
+
],
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"license": "Apache-2.0",
|
|
28
|
+
"author": {
|
|
29
|
+
"name": "Jim Liu 宝玉",
|
|
30
|
+
"url": "https://github.com/JimLiu"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://github.com/JimLiu/open-artifact#readme",
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://github.com/JimLiu/open-artifact.git",
|
|
36
|
+
"directory": "packages/host-plugin"
|
|
37
|
+
},
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/JimLiu/open-artifact/issues"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
}
|
|
44
|
+
}
|