@dxos/plugin-script 0.6.8-main.046e6cf
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 +8 -0
- package/README.md +4 -0
- package/dist/lib/browser/ScriptEditor-6Q7HFLJ6.mjs +220 -0
- package/dist/lib/browser/ScriptEditor-6Q7HFLJ6.mjs.map +7 -0
- package/dist/lib/browser/TypescriptEditor-HP35MTEQ.mjs +9 -0
- package/dist/lib/browser/TypescriptEditor-HP35MTEQ.mjs.map +7 -0
- package/dist/lib/browser/chunk-2EM5Y66A.mjs +60 -0
- package/dist/lib/browser/chunk-2EM5Y66A.mjs.map +7 -0
- package/dist/lib/browser/chunk-EZ7PWOLU.mjs +235 -0
- package/dist/lib/browser/chunk-EZ7PWOLU.mjs.map +7 -0
- package/dist/lib/browser/chunk-JBACGDOJ.mjs +21 -0
- package/dist/lib/browser/chunk-JBACGDOJ.mjs.map +7 -0
- package/dist/lib/browser/chunk-Z72YKDJ2.mjs +31 -0
- package/dist/lib/browser/chunk-Z72YKDJ2.mjs.map +7 -0
- package/dist/lib/browser/edge/index.mjs +19 -0
- package/dist/lib/browser/edge/index.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +395 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/browser/meta.mjs +9 -0
- package/dist/lib/browser/meta.mjs.map +7 -0
- package/dist/lib/browser/types/index.mjs +10 -0
- package/dist/lib/browser/types/index.mjs.map +7 -0
- package/dist/lib/node/ScriptEditor-AIVKL2NX.cjs +239 -0
- package/dist/lib/node/ScriptEditor-AIVKL2NX.cjs.map +7 -0
- package/dist/lib/node/TypescriptEditor-PWOAN5RQ.cjs +30 -0
- package/dist/lib/node/TypescriptEditor-PWOAN5RQ.cjs.map +7 -0
- package/dist/lib/node/chunk-4VMNBXWU.cjs +54 -0
- package/dist/lib/node/chunk-4VMNBXWU.cjs.map +7 -0
- package/dist/lib/node/chunk-6Y2S6SGK.cjs +49 -0
- package/dist/lib/node/chunk-6Y2S6SGK.cjs.map +7 -0
- package/dist/lib/node/chunk-LWUBFTPA.cjs +253 -0
- package/dist/lib/node/chunk-LWUBFTPA.cjs.map +7 -0
- package/dist/lib/node/chunk-SLV3SDZE.cjs +93 -0
- package/dist/lib/node/chunk-SLV3SDZE.cjs.map +7 -0
- package/dist/lib/node/edge/index.cjs +41 -0
- package/dist/lib/node/edge/index.cjs.map +7 -0
- package/dist/lib/node/index.cjs +417 -0
- package/dist/lib/node/index.cjs.map +7 -0
- package/dist/lib/node/meta.cjs +30 -0
- package/dist/lib/node/meta.cjs.map +7 -0
- package/dist/lib/node/meta.json +1 -0
- package/dist/lib/node/types/index.cjs +32 -0
- package/dist/lib/node/types/index.cjs.map +7 -0
- package/dist/types/src/ScriptPlugin.d.ts +7 -0
- package/dist/types/src/ScriptPlugin.d.ts.map +1 -0
- package/dist/types/src/compiler/compiler.d.ts +38 -0
- package/dist/types/src/compiler/compiler.d.ts.map +1 -0
- package/dist/types/src/compiler/compiler.test.d.ts +2 -0
- package/dist/types/src/compiler/compiler.test.d.ts.map +1 -0
- package/dist/types/src/compiler/index.d.ts +2 -0
- package/dist/types/src/compiler/index.d.ts.map +1 -0
- package/dist/types/src/components/ScriptBlock/FrameContainer/FrameContainer.d.ts +12 -0
- package/dist/types/src/components/ScriptBlock/FrameContainer/FrameContainer.d.ts.map +1 -0
- package/dist/types/src/components/ScriptBlock/FrameContainer/frame.d.ts +2 -0
- package/dist/types/src/components/ScriptBlock/FrameContainer/frame.d.ts.map +1 -0
- package/dist/types/src/components/ScriptBlock/FrameContainer/index.d.ts +2 -0
- package/dist/types/src/components/ScriptBlock/FrameContainer/index.d.ts.map +1 -0
- package/dist/types/src/components/ScriptBlock/ScriptBlock.d.ts +18 -0
- package/dist/types/src/components/ScriptBlock/ScriptBlock.d.ts.map +1 -0
- package/dist/types/src/components/ScriptBlock/ScriptBlock.stories.d.ts +14 -0
- package/dist/types/src/components/ScriptBlock/ScriptBlock.stories.d.ts.map +1 -0
- package/dist/types/src/components/ScriptBlock/Splitter/Splitter.d.ts +15 -0
- package/dist/types/src/components/ScriptBlock/Splitter/Splitter.d.ts.map +1 -0
- package/dist/types/src/components/ScriptBlock/Splitter/Splitter.stories.d.ts +13 -0
- package/dist/types/src/components/ScriptBlock/Splitter/Splitter.stories.d.ts.map +1 -0
- package/dist/types/src/components/ScriptBlock/Splitter/index.d.ts +2 -0
- package/dist/types/src/components/ScriptBlock/Splitter/index.d.ts.map +1 -0
- package/dist/types/src/components/ScriptBlock/index.d.ts +2 -0
- package/dist/types/src/components/ScriptBlock/index.d.ts.map +1 -0
- package/dist/types/src/components/ScriptEditor.d.ts +9 -0
- package/dist/types/src/components/ScriptEditor.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar.d.ts +11 -0
- package/dist/types/src/components/Toolbar.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar.stories.d.ts +16 -0
- package/dist/types/src/components/Toolbar.stories.d.ts.map +1 -0
- package/dist/types/src/components/TypescriptEditor.d.ts +10 -0
- package/dist/types/src/components/TypescriptEditor.d.ts.map +1 -0
- package/dist/types/src/components/TypescriptEditor.stories.d.ts +13 -0
- package/dist/types/src/components/TypescriptEditor.stories.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +4 -0
- package/dist/types/src/components/index.d.ts.map +1 -0
- package/dist/types/src/edge/codec.d.ts +4 -0
- package/dist/types/src/edge/codec.d.ts.map +1 -0
- package/dist/types/src/edge/functions.d.ts +23 -0
- package/dist/types/src/edge/functions.d.ts.map +1 -0
- package/dist/types/src/edge/header-codec.d.ts +24 -0
- package/dist/types/src/edge/header-codec.d.ts.map +1 -0
- package/dist/types/src/edge/hub-protocol.d.ts +6 -0
- package/dist/types/src/edge/hub-protocol.d.ts.map +1 -0
- package/dist/types/src/edge/index.d.ts +5 -0
- package/dist/types/src/edge/index.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +7 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/meta.d.ts +15 -0
- package/dist/types/src/meta.d.ts.map +1 -0
- package/dist/types/src/translations.d.ts +19 -0
- package/dist/types/src/translations.d.ts.map +1 -0
- package/dist/types/src/types/index.d.ts +3 -0
- package/dist/types/src/types/index.d.ts.map +1 -0
- package/dist/types/src/types/script.d.ts +26 -0
- package/dist/types/src/types/script.d.ts.map +1 -0
- package/dist/types/src/types/types.d.ts +8 -0
- package/dist/types/src/types/types.d.ts.map +1 -0
- package/package.json +120 -0
- package/src/ScriptPlugin.tsx +137 -0
- package/src/compiler/compiler.test.ts +17 -0
- package/src/compiler/compiler.ts +210 -0
- package/src/compiler/index.ts +5 -0
- package/src/components/ScriptBlock/FrameContainer/FrameContainer.tsx +82 -0
- package/src/components/ScriptBlock/FrameContainer/frame.html +25 -0
- package/src/components/ScriptBlock/FrameContainer/frame.tsx +51 -0
- package/src/components/ScriptBlock/FrameContainer/index.ts +5 -0
- package/src/components/ScriptBlock/ScriptBlock.stories.tsx +64 -0
- package/src/components/ScriptBlock/ScriptBlock.tsx +132 -0
- package/src/components/ScriptBlock/Splitter/Splitter.stories.tsx +37 -0
- package/src/components/ScriptBlock/Splitter/Splitter.tsx +52 -0
- package/src/components/ScriptBlock/Splitter/index.ts +5 -0
- package/src/components/ScriptBlock/index.ts +5 -0
- package/src/components/ScriptEditor.tsx +113 -0
- package/src/components/Toolbar.stories.tsx +19 -0
- package/src/components/Toolbar.tsx +163 -0
- package/src/components/TypescriptEditor.stories.tsx +63 -0
- package/src/components/TypescriptEditor.tsx +66 -0
- package/src/components/index.ts +8 -0
- package/src/edge/codec.ts +13 -0
- package/src/edge/functions.ts +128 -0
- package/src/edge/header-codec.ts +97 -0
- package/src/edge/hub-protocol.ts +27 -0
- package/src/edge/index.ts +10 -0
- package/src/index.ts +13 -0
- package/src/meta.tsx +19 -0
- package/src/translations.ts +25 -0
- package/src/types/index.ts +6 -0
- package/src/types/script.ts +16 -0
- package/src/types/types.tsx +27 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
Copyright (c) 2022 DXOS
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
5
|
+
|
|
6
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getUserFunctionUrlInMetadata,
|
|
3
|
+
publicKeyToDid,
|
|
4
|
+
setUserFunctionUrlInMetadata,
|
|
5
|
+
uploadWorkerFunction
|
|
6
|
+
} from "./chunk-EZ7PWOLU.mjs";
|
|
7
|
+
import {
|
|
8
|
+
SCRIPT_PLUGIN
|
|
9
|
+
} from "./chunk-JBACGDOJ.mjs";
|
|
10
|
+
import {
|
|
11
|
+
TypescriptEditor
|
|
12
|
+
} from "./chunk-2EM5Y66A.mjs";
|
|
13
|
+
|
|
14
|
+
// packages/plugins/experimental/plugin-script/src/components/ScriptEditor.tsx
|
|
15
|
+
import React2, { useCallback, useMemo, useState as useState2 } from "react";
|
|
16
|
+
import { log } from "@dxos/log";
|
|
17
|
+
import { useClient } from "@dxos/react-client";
|
|
18
|
+
import { createDocAccessor, getMeta, getSpace } from "@dxos/react-client/echo";
|
|
19
|
+
import { useIdentity } from "@dxos/react-client/halo";
|
|
20
|
+
import { useTranslation as useTranslation2 } from "@dxos/react-ui";
|
|
21
|
+
import { createDataExtensions, listener } from "@dxos/react-ui-editor";
|
|
22
|
+
import { mx as mx2 } from "@dxos/react-ui-theme";
|
|
23
|
+
|
|
24
|
+
// packages/plugins/experimental/plugin-script/src/components/Toolbar.tsx
|
|
25
|
+
import { Check, Checks, CircleNotch, Link, PencilSimple, UploadSimple, WarningCircle, X } from "@phosphor-icons/react";
|
|
26
|
+
import React, { useRef, useState } from "react";
|
|
27
|
+
import { Button, DensityProvider, ElevationProvider, Input, Toolbar as NaturalToolbar, Tooltip, useTranslation } from "@dxos/react-ui";
|
|
28
|
+
import { getSize, mx } from "@dxos/react-ui-theme";
|
|
29
|
+
var Toolbar = ({ binding, onBindingChange, deployed, onDeploy, functionUrl, error }) => {
|
|
30
|
+
const { t } = useTranslation(SCRIPT_PLUGIN);
|
|
31
|
+
const [pending, setPending] = useState(false);
|
|
32
|
+
const [editing, setEditing] = useState(false);
|
|
33
|
+
const inputRef = useRef(null);
|
|
34
|
+
const handleEdit = () => {
|
|
35
|
+
setEditing(true);
|
|
36
|
+
inputRef.current?.focus();
|
|
37
|
+
};
|
|
38
|
+
const handleSave = () => {
|
|
39
|
+
setEditing(false);
|
|
40
|
+
onBindingChange(inputRef.current?.value || "");
|
|
41
|
+
};
|
|
42
|
+
const handleCancel = () => {
|
|
43
|
+
setEditing(false);
|
|
44
|
+
};
|
|
45
|
+
const handleCopyLink = async () => {
|
|
46
|
+
if (!functionUrl) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
await navigator.clipboard.writeText(functionUrl);
|
|
50
|
+
};
|
|
51
|
+
const handleDeploy = async () => {
|
|
52
|
+
setPending(true);
|
|
53
|
+
await onDeploy?.();
|
|
54
|
+
setPending(false);
|
|
55
|
+
};
|
|
56
|
+
return /* @__PURE__ */ React.createElement(DensityProvider, {
|
|
57
|
+
density: "fine"
|
|
58
|
+
}, /* @__PURE__ */ React.createElement(ElevationProvider, {
|
|
59
|
+
elevation: "chrome"
|
|
60
|
+
}, /* @__PURE__ */ React.createElement(NaturalToolbar.Root, {
|
|
61
|
+
classNames: "p-1 is-full shrink-0 overflow-x-auto overflow-y-hidden",
|
|
62
|
+
style: {
|
|
63
|
+
contain: "layout"
|
|
64
|
+
}
|
|
65
|
+
}, /* @__PURE__ */ React.createElement(Input.Root, null, /* @__PURE__ */ React.createElement(Input.Label, null, t("binding label")), /* @__PURE__ */ React.createElement(Input.TextInput, {
|
|
66
|
+
ref: inputRef,
|
|
67
|
+
defaultValue: binding,
|
|
68
|
+
disabled: !editing,
|
|
69
|
+
classNames: "!is-auto"
|
|
70
|
+
}), editing ? /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(Button, {
|
|
71
|
+
variant: "ghost",
|
|
72
|
+
onClick: handleCancel
|
|
73
|
+
}, /* @__PURE__ */ React.createElement(X, {
|
|
74
|
+
className: getSize(4)
|
|
75
|
+
})), /* @__PURE__ */ React.createElement(Button, {
|
|
76
|
+
variant: "ghost",
|
|
77
|
+
onClick: handleSave
|
|
78
|
+
}, /* @__PURE__ */ React.createElement(Check, {
|
|
79
|
+
className: getSize(4)
|
|
80
|
+
}))) : /* @__PURE__ */ React.createElement(Tooltip.Root, null, /* @__PURE__ */ React.createElement(Tooltip.Trigger, {
|
|
81
|
+
asChild: true
|
|
82
|
+
}, /* @__PURE__ */ React.createElement(Button, {
|
|
83
|
+
variant: "ghost",
|
|
84
|
+
onClick: handleEdit
|
|
85
|
+
}, /* @__PURE__ */ React.createElement(PencilSimple, {
|
|
86
|
+
className: getSize(4)
|
|
87
|
+
}))), /* @__PURE__ */ React.createElement(Tooltip.Portal, null, /* @__PURE__ */ React.createElement(Tooltip.Content, null, /* @__PURE__ */ React.createElement(Tooltip.Arrow, null), t("edit binding label"))))), /* @__PURE__ */ React.createElement("div", {
|
|
88
|
+
role: "separator",
|
|
89
|
+
className: "grow"
|
|
90
|
+
}), error ? /* @__PURE__ */ React.createElement(Tooltip.Root, null, /* @__PURE__ */ React.createElement(Tooltip.Trigger, null, /* @__PURE__ */ React.createElement(WarningCircle, {
|
|
91
|
+
className: getSize(4)
|
|
92
|
+
})), /* @__PURE__ */ React.createElement(Tooltip.Portal, null, /* @__PURE__ */ React.createElement(Tooltip.Content, null, /* @__PURE__ */ React.createElement(Tooltip.Arrow, null), error))) : deployed ? /* @__PURE__ */ React.createElement(Tooltip.Root, null, /* @__PURE__ */ React.createElement(Tooltip.Trigger, null, /* @__PURE__ */ React.createElement(Checks, {
|
|
93
|
+
className: getSize(4)
|
|
94
|
+
})), /* @__PURE__ */ React.createElement(Tooltip.Portal, null, /* @__PURE__ */ React.createElement(Tooltip.Content, null, /* @__PURE__ */ React.createElement(Tooltip.Arrow, null), t("deployed label")))) : null, functionUrl && /* @__PURE__ */ React.createElement(Tooltip.Root, null, /* @__PURE__ */ React.createElement(Tooltip.Trigger, {
|
|
95
|
+
asChild: true
|
|
96
|
+
}, /* @__PURE__ */ React.createElement(NaturalToolbar.Button, {
|
|
97
|
+
variant: "ghost",
|
|
98
|
+
onClick: handleCopyLink
|
|
99
|
+
}, /* @__PURE__ */ React.createElement(Link, {
|
|
100
|
+
className: getSize(4)
|
|
101
|
+
}))), /* @__PURE__ */ React.createElement(Tooltip.Portal, null, /* @__PURE__ */ React.createElement(Tooltip.Content, null, /* @__PURE__ */ React.createElement(Tooltip.Arrow, null), t("copy link label")))), /* @__PURE__ */ React.createElement(Tooltip.Root, null, /* @__PURE__ */ React.createElement(Tooltip.Trigger, {
|
|
102
|
+
asChild: true
|
|
103
|
+
}, /* @__PURE__ */ React.createElement(NaturalToolbar.Button, {
|
|
104
|
+
variant: "ghost",
|
|
105
|
+
onClick: handleDeploy,
|
|
106
|
+
disabled: pending
|
|
107
|
+
}, pending ? /* @__PURE__ */ React.createElement(CircleNotch, {
|
|
108
|
+
className: mx("spinner", getSize(4))
|
|
109
|
+
}) : /* @__PURE__ */ React.createElement(UploadSimple, {
|
|
110
|
+
className: getSize(4)
|
|
111
|
+
}))), /* @__PURE__ */ React.createElement(Tooltip.Portal, null, /* @__PURE__ */ React.createElement(Tooltip.Content, null, /* @__PURE__ */ React.createElement(Tooltip.Arrow, null), pending ? t("pending label") : t("deploy label")))))));
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
// packages/plugins/experimental/plugin-script/src/components/ScriptEditor.tsx
|
|
115
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/plugins/experimental/plugin-script/src/components/ScriptEditor.tsx";
|
|
116
|
+
var ScriptEditor = ({ script, role }) => {
|
|
117
|
+
const { t } = useTranslation2(SCRIPT_PLUGIN);
|
|
118
|
+
const client = useClient();
|
|
119
|
+
const identity = useIdentity();
|
|
120
|
+
const space = getSpace(script);
|
|
121
|
+
const extensions = useMemo(() => [
|
|
122
|
+
listener({
|
|
123
|
+
onChange: (text) => {
|
|
124
|
+
if (script.source && script.source?.content !== text) {
|
|
125
|
+
script.changed = true;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}),
|
|
129
|
+
createDataExtensions({
|
|
130
|
+
id: script.id,
|
|
131
|
+
text: script.source && createDocAccessor(script.source, [
|
|
132
|
+
"content"
|
|
133
|
+
]),
|
|
134
|
+
space,
|
|
135
|
+
identity
|
|
136
|
+
})
|
|
137
|
+
], [
|
|
138
|
+
script,
|
|
139
|
+
script.source,
|
|
140
|
+
space,
|
|
141
|
+
identity
|
|
142
|
+
]);
|
|
143
|
+
const initialValue = useMemo(() => script.source?.content, [
|
|
144
|
+
script.source
|
|
145
|
+
]);
|
|
146
|
+
const existingFunctionUrl = getUserFunctionUrlInMetadata(getMeta(script));
|
|
147
|
+
const [error, setError] = useState2();
|
|
148
|
+
const handleBindingChange = useCallback((binding) => {
|
|
149
|
+
script.binding = binding;
|
|
150
|
+
}, [
|
|
151
|
+
script
|
|
152
|
+
]);
|
|
153
|
+
const handleDeploy = useCallback(async () => {
|
|
154
|
+
if (!script.source || !identity) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
setError(void 0);
|
|
158
|
+
try {
|
|
159
|
+
const existingFunctionId = existingFunctionUrl?.split("/").at(-1);
|
|
160
|
+
const ownerDid = existingFunctionUrl?.split("/").at(-2) ?? publicKeyToDid(identity.identityKey);
|
|
161
|
+
const { result, functionId, functionVersionNumber, errorMessage } = await uploadWorkerFunction({
|
|
162
|
+
clientConfig: client.config,
|
|
163
|
+
halo: client.halo,
|
|
164
|
+
ownerDid,
|
|
165
|
+
functionId: existingFunctionId,
|
|
166
|
+
name: script.name,
|
|
167
|
+
source: script.source.content
|
|
168
|
+
});
|
|
169
|
+
if (result !== "success" || functionId === void 0) {
|
|
170
|
+
throw new Error(errorMessage);
|
|
171
|
+
}
|
|
172
|
+
log.info("function uploaded", {
|
|
173
|
+
functionId,
|
|
174
|
+
functionVersionNumber
|
|
175
|
+
}, {
|
|
176
|
+
F: __dxlog_file,
|
|
177
|
+
L: 88,
|
|
178
|
+
S: void 0,
|
|
179
|
+
C: (f, a) => f(...a)
|
|
180
|
+
});
|
|
181
|
+
const meta = getMeta(script);
|
|
182
|
+
setUserFunctionUrlInMetadata(meta, `/${ownerDid}/${functionId}`);
|
|
183
|
+
script.changed = false;
|
|
184
|
+
} catch (err) {
|
|
185
|
+
log.catch(err, void 0, {
|
|
186
|
+
F: __dxlog_file,
|
|
187
|
+
L: 93,
|
|
188
|
+
S: void 0,
|
|
189
|
+
C: (f, a) => f(...a)
|
|
190
|
+
});
|
|
191
|
+
setError(t("upload failed label"));
|
|
192
|
+
}
|
|
193
|
+
}, [
|
|
194
|
+
existingFunctionUrl,
|
|
195
|
+
script,
|
|
196
|
+
script.name,
|
|
197
|
+
script.source
|
|
198
|
+
]);
|
|
199
|
+
return /* @__PURE__ */ React2.createElement("div", {
|
|
200
|
+
role: "none",
|
|
201
|
+
className: mx2(role === "article" && "row-span-2", "is-full pli-2")
|
|
202
|
+
}, /* @__PURE__ */ React2.createElement(Toolbar, {
|
|
203
|
+
binding: script.binding ?? "",
|
|
204
|
+
onBindingChange: handleBindingChange,
|
|
205
|
+
deployed: Boolean(existingFunctionUrl) && !script.changed,
|
|
206
|
+
onDeploy: handleDeploy,
|
|
207
|
+
functionUrl: existingFunctionUrl,
|
|
208
|
+
error
|
|
209
|
+
}), /* @__PURE__ */ React2.createElement(TypescriptEditor, {
|
|
210
|
+
id: script.id,
|
|
211
|
+
initialValue,
|
|
212
|
+
extensions
|
|
213
|
+
}));
|
|
214
|
+
};
|
|
215
|
+
var ScriptEditor_default = ScriptEditor;
|
|
216
|
+
export {
|
|
217
|
+
ScriptEditor,
|
|
218
|
+
ScriptEditor_default as default
|
|
219
|
+
};
|
|
220
|
+
//# sourceMappingURL=ScriptEditor-6Q7HFLJ6.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/components/ScriptEditor.tsx", "../../../src/components/Toolbar.tsx"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { type DID } from 'iso-did/types';\nimport React, { useCallback, useMemo, useState } from 'react';\n\nimport { log } from '@dxos/log';\nimport { useClient } from '@dxos/react-client';\nimport { createDocAccessor, getMeta, getSpace } from '@dxos/react-client/echo';\nimport { useIdentity } from '@dxos/react-client/halo';\nimport { useTranslation } from '@dxos/react-ui';\nimport { createDataExtensions, listener } from '@dxos/react-ui-editor';\nimport { mx } from '@dxos/react-ui-theme';\n\nimport { Toolbar } from './Toolbar';\nimport { TypescriptEditor } from './TypescriptEditor';\nimport {\n getUserFunctionUrlInMetadata,\n publicKeyToDid,\n setUserFunctionUrlInMetadata,\n uploadWorkerFunction,\n} from '../edge';\nimport { SCRIPT_PLUGIN } from '../meta';\nimport { type ScriptType } from '../types';\n\nexport type ScriptEditorProps = {\n script: ScriptType;\n role?: string;\n};\n\nexport const ScriptEditor = ({ script, role }: ScriptEditorProps) => {\n const { t } = useTranslation(SCRIPT_PLUGIN);\n const client = useClient();\n const identity = useIdentity();\n const space = getSpace(script);\n const extensions = useMemo(\n () => [\n listener({\n onChange: (text) => {\n if (script.source && script.source?.content !== text) {\n script.changed = true;\n }\n },\n }),\n createDataExtensions({\n id: script.id,\n text: script.source && createDocAccessor(script.source, ['content']),\n space,\n identity,\n }),\n ],\n [script, script.source, space, identity],\n );\n const initialValue = useMemo(() => script.source?.content, [script.source]);\n const existingFunctionUrl = getUserFunctionUrlInMetadata(getMeta(script));\n const [error, setError] = useState<string>();\n\n const handleBindingChange = useCallback(\n (binding: string) => {\n script.binding = binding;\n },\n [script],\n );\n\n const handleDeploy = useCallback(async () => {\n if (!script.source || !identity) {\n return;\n }\n\n setError(undefined);\n\n try {\n const existingFunctionId = existingFunctionUrl?.split('/').at(-1);\n const ownerDid = (existingFunctionUrl?.split('/').at(-2) as DID) ?? publicKeyToDid(identity.identityKey);\n\n const { result, functionId, functionVersionNumber, errorMessage } = await uploadWorkerFunction({\n clientConfig: client.config,\n halo: client.halo,\n ownerDid,\n functionId: existingFunctionId,\n name: script.name,\n source: script.source.content,\n });\n if (result !== 'success' || functionId === undefined) {\n throw new Error(errorMessage);\n }\n log.info('function uploaded', { functionId, functionVersionNumber });\n const meta = getMeta(script);\n setUserFunctionUrlInMetadata(meta, `/${ownerDid}/${functionId}`);\n script.changed = false;\n } catch (err: any) {\n log.catch(err);\n setError(t('upload failed label'));\n }\n }, [existingFunctionUrl, script, script.name, script.source]);\n\n return (\n <div role='none' className={mx(role === 'article' && 'row-span-2', 'is-full pli-2')}>\n <Toolbar\n binding={script.binding ?? ''}\n onBindingChange={handleBindingChange}\n deployed={Boolean(existingFunctionUrl) && !script.changed}\n onDeploy={handleDeploy}\n functionUrl={existingFunctionUrl}\n error={error}\n />\n <TypescriptEditor id={script.id} initialValue={initialValue} extensions={extensions} />\n </div>\n );\n};\n\nexport default ScriptEditor;\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { Check, Checks, CircleNotch, Link, PencilSimple, UploadSimple, WarningCircle, X } from '@phosphor-icons/react';\nimport React, { useRef, useState } from 'react';\n\nimport {\n Button,\n DensityProvider,\n ElevationProvider,\n Input,\n Toolbar as NaturalToolbar,\n Tooltip,\n useTranslation,\n} from '@dxos/react-ui';\nimport { getSize, mx } from '@dxos/react-ui-theme';\n\nimport { SCRIPT_PLUGIN } from '../meta';\n\nexport type ToolbarProps = {\n binding: string;\n onBindingChange: (binding: string) => void;\n deployed?: boolean;\n onDeploy?: () => Promise<void>;\n functionUrl?: string;\n error?: string;\n};\n\nexport const Toolbar = ({ binding, onBindingChange, deployed, onDeploy, functionUrl, error }: ToolbarProps) => {\n const { t } = useTranslation(SCRIPT_PLUGIN);\n const [pending, setPending] = useState(false);\n const [editing, setEditing] = useState(false);\n const inputRef = useRef<HTMLInputElement>(null);\n\n const handleEdit = () => {\n setEditing(true);\n inputRef.current?.focus();\n };\n\n const handleSave = () => {\n setEditing(false);\n onBindingChange(inputRef.current?.value || '');\n };\n\n const handleCancel = () => {\n setEditing(false);\n };\n\n const handleCopyLink = async () => {\n if (!functionUrl) {\n return;\n }\n\n await navigator.clipboard.writeText(functionUrl);\n };\n\n const handleDeploy = async () => {\n setPending(true);\n await onDeploy?.();\n setPending(false);\n };\n\n return (\n <DensityProvider density='fine'>\n <ElevationProvider elevation='chrome'>\n <NaturalToolbar.Root\n classNames='p-1 is-full shrink-0 overflow-x-auto overflow-y-hidden'\n style={{ contain: 'layout' }}\n >\n {/* TODO(wittjosiah): Binding input shouldn't be in the toolbar. */}\n <Input.Root>\n <Input.Label>{t('binding label')}</Input.Label>\n <Input.TextInput ref={inputRef} defaultValue={binding} disabled={!editing} classNames='!is-auto' />\n {editing ? (\n <>\n <Button variant='ghost' onClick={handleCancel}>\n <X className={getSize(4)} />\n </Button>\n <Button variant='ghost' onClick={handleSave}>\n <Check className={getSize(4)} />\n </Button>\n </>\n ) : (\n <Tooltip.Root>\n <Tooltip.Trigger asChild>\n <Button variant='ghost' onClick={handleEdit}>\n <PencilSimple className={getSize(4)} />\n </Button>\n </Tooltip.Trigger>\n <Tooltip.Portal>\n <Tooltip.Content>\n <Tooltip.Arrow />\n {t('edit binding label')}\n </Tooltip.Content>\n </Tooltip.Portal>\n </Tooltip.Root>\n )}\n </Input.Root>\n <div role='separator' className='grow' />\n {/* TODO(wittjosiah): Better treatment for status indicators in toolbar? */}\n {error ? (\n <Tooltip.Root>\n <Tooltip.Trigger>\n <WarningCircle className={getSize(4)} />\n </Tooltip.Trigger>\n <Tooltip.Portal>\n <Tooltip.Content>\n <Tooltip.Arrow />\n {error}\n </Tooltip.Content>\n </Tooltip.Portal>\n </Tooltip.Root>\n ) : deployed ? (\n <Tooltip.Root>\n <Tooltip.Trigger>\n <Checks className={getSize(4)} />\n </Tooltip.Trigger>\n <Tooltip.Portal>\n <Tooltip.Content>\n <Tooltip.Arrow />\n {t('deployed label')}\n </Tooltip.Content>\n </Tooltip.Portal>\n </Tooltip.Root>\n ) : null}\n {functionUrl && (\n <Tooltip.Root>\n <Tooltip.Trigger asChild>\n <NaturalToolbar.Button variant='ghost' onClick={handleCopyLink}>\n <Link className={getSize(4)} />\n </NaturalToolbar.Button>\n </Tooltip.Trigger>\n <Tooltip.Portal>\n <Tooltip.Content>\n <Tooltip.Arrow />\n {t('copy link label')}\n </Tooltip.Content>\n </Tooltip.Portal>\n </Tooltip.Root>\n )}\n <Tooltip.Root>\n <Tooltip.Trigger asChild>\n <NaturalToolbar.Button variant='ghost' onClick={handleDeploy} disabled={pending}>\n {pending ? (\n <CircleNotch className={mx('spinner', getSize(4))} />\n ) : (\n <UploadSimple className={getSize(4)} />\n )}\n </NaturalToolbar.Button>\n </Tooltip.Trigger>\n <Tooltip.Portal>\n <Tooltip.Content>\n <Tooltip.Arrow />\n {pending ? t('pending label') : t('deploy label')}\n </Tooltip.Content>\n </Tooltip.Portal>\n </Tooltip.Root>\n </NaturalToolbar.Root>\n </ElevationProvider>\n </DensityProvider>\n );\n};\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;AAKA,OAAOA,UAASC,aAAaC,SAASC,YAAAA,iBAAgB;AAEtD,SAASC,WAAW;AACpB,SAASC,iBAAiB;AAC1B,SAASC,mBAAmBC,SAASC,gBAAgB;AACrD,SAASC,mBAAmB;AAC5B,SAASC,kBAAAA,uBAAsB;AAC/B,SAASC,sBAAsBC,gBAAgB;AAC/C,SAASC,MAAAA,WAAU;;;ACTnB,SAASC,OAAOC,QAAQC,aAAaC,MAAMC,cAAcC,cAAcC,eAAeC,SAAS;AAC/F,OAAOC,SAASC,QAAQC,gBAAgB;AAExC,SACEC,QACAC,iBACAC,mBACAC,OACAC,WAAWC,gBACXC,SACAC,sBACK;AACP,SAASC,SAASC,UAAU;AAarB,IAAMC,UAAU,CAAC,EAAEC,SAASC,iBAAiBC,UAAUC,UAAUC,aAAaC,MAAK,MAAgB;AACxG,QAAM,EAAEC,EAAC,IAAKC,eAAeC,aAAAA;AAC7B,QAAM,CAACC,SAASC,UAAAA,IAAcC,SAAS,KAAA;AACvC,QAAM,CAACC,SAASC,UAAAA,IAAcF,SAAS,KAAA;AACvC,QAAMG,WAAWC,OAAyB,IAAA;AAE1C,QAAMC,aAAa,MAAA;AACjBH,eAAW,IAAA;AACXC,aAASG,SAASC,MAAAA;EACpB;AAEA,QAAMC,aAAa,MAAA;AACjBN,eAAW,KAAA;AACXZ,oBAAgBa,SAASG,SAASG,SAAS,EAAA;EAC7C;AAEA,QAAMC,eAAe,MAAA;AACnBR,eAAW,KAAA;EACb;AAEA,QAAMS,iBAAiB,YAAA;AACrB,QAAI,CAAClB,aAAa;AAChB;IACF;AAEA,UAAMmB,UAAUC,UAAUC,UAAUrB,WAAAA;EACtC;AAEA,QAAMsB,eAAe,YAAA;AACnBhB,eAAW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AAMhD,OAAOgD;QACbhC,QAAQhB,OAAOgB,OAAOC;MACxB,CAAA;AACA,UAAIuB,WAAW,aAAaC,eAAeR,QAAW;AACpD,cAAM,IAAIgB,MAAMN,YAAAA;MAClB;AACAO,UAAIC,KAAK,qBAAqB;QAAEV;QAAYC;MAAsB,GAAA;;;;;;AAClE,YAAMU,OAAO3B,QAAQzB,MAAAA;AACrBqD,mCAA6BD,MAAM,IAAIf,QAAAA,IAAYI,UAAAA,EAAY;AAC/DzC,aAAOkB,UAAU;IACnB,SAASoC,KAAU;AACjBJ,UAAIK,MAAMD,KAAAA,QAAAA;;;;;;AACV3B,eAASzB,EAAE,qBAAA,CAAA;IACb;EACF,GAAG;IAACqB;IAAqBvB;IAAQA,OAAOgD;IAAMhD,OAAOgB;GAAO;AAE5D,SACE,gBAAAwC,OAAA,cAACC,OAAAA;IAAIxD,MAAK;IAAOyD,WAAWC,IAAG1D,SAAS,aAAa,cAAc,eAAA;KACjE,gBAAAuD,OAAA,cAACI,SAAAA;IACC7B,SAAS/B,OAAO+B,WAAW;IAC3B8B,iBAAiBhC;IACjBiC,UAAUC,QAAQxC,mBAAAA,KAAwB,CAACvB,OAAOkB;IAClD8C,UAAUhC;IACViC,aAAa1C;IACbG;MAEF,gBAAA8B,OAAA,cAACU,kBAAAA;IAAiB9C,IAAIpB,OAAOoB;IAAIE;IAA4BX;;AAGnE;AAEA,IAAA,uBAAeZ;",
|
|
6
|
+
"names": ["React", "useCallback", "useMemo", "useState", "log", "useClient", "createDocAccessor", "getMeta", "getSpace", "useIdentity", "useTranslation", "createDataExtensions", "listener", "mx", "Check", "Checks", "CircleNotch", "Link", "PencilSimple", "UploadSimple", "WarningCircle", "X", "React", "useRef", "useState", "Button", "DensityProvider", "ElevationProvider", "Input", "Toolbar", "NaturalToolbar", "Tooltip", "useTranslation", "getSize", "mx", "Toolbar", "binding", "onBindingChange", "deployed", "onDeploy", "functionUrl", "error", "t", "useTranslation", "SCRIPT_PLUGIN", "pending", "setPending", "useState", "editing", "setEditing", "inputRef", "useRef", "handleEdit", "current", "focus", "handleSave", "value", "handleCancel", "handleCopyLink", "navigator", "clipboard", "writeText", "handleDeploy", "DensityProvider", "density", "ElevationProvider", "elevation", "NaturalToolbar", "Root", "classNames", "style", "contain", "Input", "Label", "TextInput", "ref", "defaultValue", "disabled", "Button", "variant", "onClick", "X", "className", "getSize", "Check", "Tooltip", "Trigger", "asChild", "PencilSimple", "Portal", "Content", "Arrow", "div", "role", "WarningCircle", "Checks", "Link", "CircleNotch", "mx", "UploadSimple", "ScriptEditor", "script", "role", "t", "useTranslation", "SCRIPT_PLUGIN", "client", "useClient", "identity", "useIdentity", "space", "getSpace", "extensions", "useMemo", "listener", "onChange", "text", "source", "content", "changed", "createDataExtensions", "id", "createDocAccessor", "initialValue", "existingFunctionUrl", "getUserFunctionUrlInMetadata", "getMeta", "error", "setError", "useState", "handleBindingChange", "useCallback", "binding", "handleDeploy", "undefined", "existingFunctionId", "split", "at", "ownerDid", "publicKeyToDid", "identityKey", "result", "functionId", "functionVersionNumber", "errorMessage", "uploadWorkerFunction", "clientConfig", "config", "halo", "name", "Error", "log", "info", "meta", "setUserFunctionUrlInMetadata", "err", "catch", "React", "div", "className", "mx", "Toolbar", "onBindingChange", "deployed", "Boolean", "onDeploy", "functionUrl", "TypescriptEditor"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// packages/plugins/experimental/plugin-script/src/components/TypescriptEditor.tsx
|
|
2
|
+
import { javascript } from "@codemirror/lang-javascript";
|
|
3
|
+
import { defaultHighlightStyle, syntaxHighlighting } from "@codemirror/language";
|
|
4
|
+
import { oneDarkHighlightStyle } from "@codemirror/theme-one-dark";
|
|
5
|
+
import React from "react";
|
|
6
|
+
import { useThemeContext } from "@dxos/react-ui";
|
|
7
|
+
import { autocomplete, createBasicExtensions, createThemeExtensions, editorContent, useTextEditor } from "@dxos/react-ui-editor";
|
|
8
|
+
import { nonNullable } from "@dxos/util";
|
|
9
|
+
var TypescriptEditor = ({ id, extensions, initialValue, scrollTo, selection, className, scrollPastEnd }) => {
|
|
10
|
+
const { themeMode } = useThemeContext();
|
|
11
|
+
const { parentRef, focusAttributes } = useTextEditor(() => ({
|
|
12
|
+
id,
|
|
13
|
+
initialValue,
|
|
14
|
+
extensions: [
|
|
15
|
+
extensions,
|
|
16
|
+
// TODO(wittjosiah): Highlight active line doesn't work.
|
|
17
|
+
createBasicExtensions({
|
|
18
|
+
highlightActiveLine: true,
|
|
19
|
+
indentWithTab: true,
|
|
20
|
+
lineNumbers: true,
|
|
21
|
+
scrollPastEnd
|
|
22
|
+
}),
|
|
23
|
+
// TODO(wittjosiah): Factor out syntax highlighting to theme extensions.
|
|
24
|
+
themeMode === "dark" ? syntaxHighlighting(oneDarkHighlightStyle) : syntaxHighlighting(defaultHighlightStyle),
|
|
25
|
+
createThemeExtensions({
|
|
26
|
+
themeMode,
|
|
27
|
+
slots: {
|
|
28
|
+
content: {
|
|
29
|
+
className: editorContent
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}),
|
|
33
|
+
javascript({
|
|
34
|
+
typescript: true
|
|
35
|
+
}),
|
|
36
|
+
autocomplete()
|
|
37
|
+
].filter(nonNullable),
|
|
38
|
+
selection,
|
|
39
|
+
scrollTo
|
|
40
|
+
}), [
|
|
41
|
+
id,
|
|
42
|
+
initialValue,
|
|
43
|
+
extensions,
|
|
44
|
+
themeMode,
|
|
45
|
+
selection,
|
|
46
|
+
scrollTo
|
|
47
|
+
]);
|
|
48
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
49
|
+
ref: parentRef,
|
|
50
|
+
className,
|
|
51
|
+
...focusAttributes
|
|
52
|
+
});
|
|
53
|
+
};
|
|
54
|
+
var TypescriptEditor_default = TypescriptEditor;
|
|
55
|
+
|
|
56
|
+
export {
|
|
57
|
+
TypescriptEditor,
|
|
58
|
+
TypescriptEditor_default
|
|
59
|
+
};
|
|
60
|
+
//# sourceMappingURL=chunk-2EM5Y66A.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/components/TypescriptEditor.tsx"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { javascript } from '@codemirror/lang-javascript';\nimport { defaultHighlightStyle, syntaxHighlighting } from '@codemirror/language';\nimport { oneDarkHighlightStyle } from '@codemirror/theme-one-dark';\nimport React from 'react';\n\nimport { useThemeContext } from '@dxos/react-ui';\nimport {\n autocomplete,\n createBasicExtensions,\n createThemeExtensions,\n editorContent,\n useTextEditor,\n type UseTextEditorProps,\n} from '@dxos/react-ui-editor';\nimport { nonNullable } from '@dxos/util';\n\nexport type TypescriptEditorProps = {\n id: string;\n className?: string;\n scrollPastEnd?: boolean;\n} & Pick<UseTextEditorProps, 'initialValue' | 'extensions' | 'scrollTo' | 'selection'>;\n\nexport const TypescriptEditor = ({\n id,\n extensions,\n initialValue,\n scrollTo,\n selection,\n className,\n scrollPastEnd,\n}: TypescriptEditorProps) => {\n const { themeMode } = useThemeContext();\n\n const { parentRef, focusAttributes } = useTextEditor(\n () => ({\n id,\n initialValue,\n extensions: [\n extensions,\n // TODO(wittjosiah): Highlight active line doesn't work.\n createBasicExtensions({ highlightActiveLine: true, indentWithTab: true, lineNumbers: true, scrollPastEnd }),\n // TODO(wittjosiah): Factor out syntax highlighting to theme extensions.\n themeMode === 'dark' ? syntaxHighlighting(oneDarkHighlightStyle) : syntaxHighlighting(defaultHighlightStyle),\n createThemeExtensions({\n themeMode,\n slots: {\n content: { className: editorContent },\n },\n }),\n javascript({ typescript: true }),\n autocomplete(),\n ].filter(nonNullable),\n selection,\n scrollTo,\n }),\n [id, initialValue, extensions, themeMode, selection, scrollTo],\n );\n\n return <div ref={parentRef} className={className} {...focusAttributes} />;\n};\n\nexport default TypescriptEditor;\n"],
|
|
5
|
+
"mappings": ";AAIA,SAASA,kBAAkB;AAC3B,SAASC,uBAAuBC,0BAA0B;AAC1D,SAASC,6BAA6B;AACtC,OAAOC,WAAW;AAElB,SAASC,uBAAuB;AAChC,SACEC,cACAC,uBACAC,uBACAC,eACAC,qBAEK;AACP,SAASC,mBAAmB;AAQrB,IAAMC,mBAAmB,CAAC,EAC/BC,IACAC,YACAC,cACAC,UACAC,WACAC,WACAC,cAAa,MACS;AACtB,QAAM,EAAEC,UAAS,IAAKC,gBAAAA;AAEtB,QAAM,EAAEC,WAAWC,gBAAe,IAAKC,cACrC,OAAO;IACLX;IACAE;IACAD,YAAY;MACVA;;MAEAW,sBAAsB;QAAEC,qBAAqB;QAAMC,eAAe;QAAMC,aAAa;QAAMT;MAAc,CAAA;;MAEzGC,cAAc,SAASS,mBAAmBC,qBAAAA,IAAyBD,mBAAmBE,qBAAAA;MACtFC,sBAAsB;QACpBZ;QACAa,OAAO;UACLC,SAAS;YAAEhB,WAAWiB;UAAc;QACtC;MACF,CAAA;MACAC,WAAW;QAAEC,YAAY;MAAK,CAAA;MAC9BC,aAAAA;MACAC,OAAOC,WAAAA;IACTvB;IACAD;EACF,IACA;IAACH;IAAIE;IAAcD;IAAYM;IAAWH;IAAWD;GAAS;AAGhE,SAAO,sBAAA,cAACyB,OAAAA;IAAIC,KAAKpB;IAAWJ;IAAuB,GAAGK;;AACxD;AAEA,IAAA,2BAAeX;",
|
|
6
|
+
"names": ["javascript", "defaultHighlightStyle", "syntaxHighlighting", "oneDarkHighlightStyle", "React", "useThemeContext", "autocomplete", "createBasicExtensions", "createThemeExtensions", "editorContent", "useTextEditor", "nonNullable", "TypescriptEditor", "id", "extensions", "initialValue", "scrollTo", "selection", "className", "scrollPastEnd", "themeMode", "useThemeContext", "parentRef", "focusAttributes", "useTextEditor", "createBasicExtensions", "highlightActiveLine", "indentWithTab", "lineNumbers", "syntaxHighlighting", "oneDarkHighlightStyle", "defaultHighlightStyle", "createThemeExtensions", "slots", "content", "editorContent", "javascript", "typescript", "autocomplete", "filter", "nonNullable", "div", "ref"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
// packages/plugins/experimental/plugin-script/src/edge/codec.ts
|
|
2
|
+
import { PublicKey } from "@dxos/keys";
|
|
3
|
+
|
|
4
|
+
// packages/plugins/experimental/plugin-script/src/edge/header-codec.ts
|
|
5
|
+
var ENCODING = "base64";
|
|
6
|
+
var HeaderCodec = class {
|
|
7
|
+
constructor(_keyCodec) {
|
|
8
|
+
this._keyCodec = _keyCodec;
|
|
9
|
+
}
|
|
10
|
+
encode(obj) {
|
|
11
|
+
const objectToEncode = this._prepareForEncoding(obj);
|
|
12
|
+
const encoded = Buffer.from(JSON.stringify(objectToEncode));
|
|
13
|
+
return encoded.toString(ENCODING);
|
|
14
|
+
}
|
|
15
|
+
decode(buffer) {
|
|
16
|
+
const json = JSON.parse(Buffer.from(buffer, ENCODING).toString());
|
|
17
|
+
return this._decodeRecursive(json);
|
|
18
|
+
}
|
|
19
|
+
_decodeRecursive(obj) {
|
|
20
|
+
return this._recurse(obj, (value) => {
|
|
21
|
+
if (isEncodedBytes(value)) {
|
|
22
|
+
return decodeBytes(value.__b);
|
|
23
|
+
}
|
|
24
|
+
if (isEncodedKey(value)) {
|
|
25
|
+
return this._keyCodec.decode(value);
|
|
26
|
+
}
|
|
27
|
+
if (isEncodedDate(value)) {
|
|
28
|
+
return decodeDate(value);
|
|
29
|
+
}
|
|
30
|
+
return null;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
_prepareForEncoding(obj) {
|
|
34
|
+
return this._recurse(obj, (value) => {
|
|
35
|
+
if (value instanceof Uint8Array) {
|
|
36
|
+
return {
|
|
37
|
+
__b: encodeBytes(value)
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
if (this._keyCodec.isKey(value)) {
|
|
41
|
+
return this._keyCodec.encode(value);
|
|
42
|
+
}
|
|
43
|
+
if (value instanceof Date) {
|
|
44
|
+
return encodeDate(value);
|
|
45
|
+
}
|
|
46
|
+
return null;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
_recurse(obj, handler) {
|
|
50
|
+
if (typeof obj !== "object") {
|
|
51
|
+
return obj;
|
|
52
|
+
}
|
|
53
|
+
const handled = handler(obj);
|
|
54
|
+
if (handled != null) {
|
|
55
|
+
return handled;
|
|
56
|
+
}
|
|
57
|
+
if (Array.isArray(obj)) {
|
|
58
|
+
const result2 = [];
|
|
59
|
+
for (const elem of obj) {
|
|
60
|
+
result2.push(this._recurse(elem, handler));
|
|
61
|
+
}
|
|
62
|
+
return result2;
|
|
63
|
+
}
|
|
64
|
+
const result = {};
|
|
65
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
66
|
+
result[key] = this._recurse(value, handler);
|
|
67
|
+
}
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
var isEncodedKey = (obj) => "__k" in obj && typeof obj.__k === "string";
|
|
72
|
+
var isEncodedBytes = (obj) => "__b" in obj && typeof obj.__b === "string";
|
|
73
|
+
var isEncodedDate = (obj) => "__t" in obj && typeof obj.__t === "number";
|
|
74
|
+
var encodeDate = (date) => ({
|
|
75
|
+
__t: date.getTime()
|
|
76
|
+
});
|
|
77
|
+
var decodeDate = (timestamp) => new Date(timestamp.__t);
|
|
78
|
+
var encodeBytes = (bytes) => Buffer.from(bytes).toString(ENCODING);
|
|
79
|
+
var decodeBytes = (bytes) => Buffer.from(bytes, ENCODING);
|
|
80
|
+
|
|
81
|
+
// packages/plugins/experimental/plugin-script/src/edge/codec.ts
|
|
82
|
+
var codec = new HeaderCodec({
|
|
83
|
+
isKey: (value) => value instanceof PublicKey,
|
|
84
|
+
encode: (key) => ({
|
|
85
|
+
__k: key.toHex()
|
|
86
|
+
}),
|
|
87
|
+
decode: (value) => PublicKey.fromHex(value.__k)
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// packages/plugins/experimental/plugin-script/src/edge/functions.ts
|
|
91
|
+
import { Trigger } from "@dxos/async";
|
|
92
|
+
import { invariant } from "@dxos/invariant";
|
|
93
|
+
import { log } from "@dxos/log";
|
|
94
|
+
|
|
95
|
+
// packages/plugins/experimental/plugin-script/src/edge/hub-protocol.ts
|
|
96
|
+
var matchServiceCredential = (capabilities = []) => (credential) => {
|
|
97
|
+
if (credential.subject.assertion["@type"] !== "dxos.halo.credentials.ServiceAccess") {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
const { capabilities: credentialCapabilities } = credential.subject.assertion;
|
|
101
|
+
return capabilities.every((capability) => credentialCapabilities.includes(capability));
|
|
102
|
+
};
|
|
103
|
+
var publicKeyToDid = (key) => {
|
|
104
|
+
return `did:key:${key.toHex()}`;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
// packages/plugins/experimental/plugin-script/src/edge/functions.ts
|
|
108
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/plugins/experimental/plugin-script/src/edge/functions.ts";
|
|
109
|
+
var USERFUNCTIONS_META_KEY = "dxos.org/service/function";
|
|
110
|
+
var USERFUNCTIONS_CREDENTIAL_CAPABILITY = "composer:beta";
|
|
111
|
+
var defaultUserFunctionsBaseUrl = "http://localhost:8600";
|
|
112
|
+
var getBaseUrl = (config) => {
|
|
113
|
+
return config.get("runtime.app.env.DX_USERFUNCTIONS_BASE_URL") || defaultUserFunctionsBaseUrl;
|
|
114
|
+
};
|
|
115
|
+
var uploadWorkerFunction = async ({ clientConfig, halo, name, source, ownerDid, functionId, credentialLoadTimeout }) => {
|
|
116
|
+
const identity = halo.identity.get();
|
|
117
|
+
if (!identity) {
|
|
118
|
+
throw new Error("Identity not available");
|
|
119
|
+
}
|
|
120
|
+
const userFunctionsBaseUrl = getBaseUrl(clientConfig);
|
|
121
|
+
const uploadUrl = `${userFunctionsBaseUrl}/${ownerDid}` + (functionId ? `/${functionId}` : "");
|
|
122
|
+
log("Upload", {
|
|
123
|
+
functionId,
|
|
124
|
+
source,
|
|
125
|
+
name,
|
|
126
|
+
identityKey: identity.identityKey,
|
|
127
|
+
uploadUrl: uploadUrl.toString()
|
|
128
|
+
}, {
|
|
129
|
+
F: __dxlog_file,
|
|
130
|
+
L: 65,
|
|
131
|
+
S: void 0,
|
|
132
|
+
C: (f, a) => f(...a)
|
|
133
|
+
});
|
|
134
|
+
const response = await fetch(uploadUrl, {
|
|
135
|
+
method: "PUT",
|
|
136
|
+
body: JSON.stringify({
|
|
137
|
+
script: source,
|
|
138
|
+
name
|
|
139
|
+
}),
|
|
140
|
+
headers: await presentationForIdentity({
|
|
141
|
+
halo,
|
|
142
|
+
timeout: credentialLoadTimeout
|
|
143
|
+
})
|
|
144
|
+
});
|
|
145
|
+
if (!response.ok) {
|
|
146
|
+
return await response.json();
|
|
147
|
+
}
|
|
148
|
+
const res = await response.json();
|
|
149
|
+
log("Upload response", {
|
|
150
|
+
status: response.status,
|
|
151
|
+
res
|
|
152
|
+
}, {
|
|
153
|
+
F: __dxlog_file,
|
|
154
|
+
L: 76,
|
|
155
|
+
S: void 0,
|
|
156
|
+
C: (f, a) => f(...a)
|
|
157
|
+
});
|
|
158
|
+
return res;
|
|
159
|
+
};
|
|
160
|
+
var getUserFunctionUrlInMetadata = (meta) => {
|
|
161
|
+
return meta.keys.find((key) => key.source === USERFUNCTIONS_META_KEY)?.id;
|
|
162
|
+
};
|
|
163
|
+
var setUserFunctionUrlInMetadata = (meta, functionUrl) => {
|
|
164
|
+
const key = meta.keys.find((key2) => key2.source === USERFUNCTIONS_META_KEY);
|
|
165
|
+
if (key) {
|
|
166
|
+
if (key.id !== functionUrl) {
|
|
167
|
+
throw new Error("Metadata mismatch");
|
|
168
|
+
}
|
|
169
|
+
} else {
|
|
170
|
+
meta.keys.push({
|
|
171
|
+
source: USERFUNCTIONS_META_KEY,
|
|
172
|
+
id: functionUrl
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
var presentationForIdentity = async ({ halo, timeout }) => {
|
|
177
|
+
let credential;
|
|
178
|
+
if (timeout) {
|
|
179
|
+
const credentialsLoaded = new Trigger();
|
|
180
|
+
halo.credentials.subscribe((c) => {
|
|
181
|
+
credential = c.find(matchServiceCredential([
|
|
182
|
+
USERFUNCTIONS_CREDENTIAL_CAPABILITY
|
|
183
|
+
]));
|
|
184
|
+
if (credential) {
|
|
185
|
+
credentialsLoaded.wake();
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
await credentialsLoaded.wait({
|
|
189
|
+
timeout
|
|
190
|
+
});
|
|
191
|
+
} else {
|
|
192
|
+
credential = halo.queryCredentials().toSorted((a, b) => b.issuanceDate.getTime() - a.issuanceDate.getTime()).find(matchServiceCredential([
|
|
193
|
+
USERFUNCTIONS_CREDENTIAL_CAPABILITY
|
|
194
|
+
]));
|
|
195
|
+
}
|
|
196
|
+
if (!credential) {
|
|
197
|
+
throw new Error("No credential found");
|
|
198
|
+
}
|
|
199
|
+
log("credential found", {
|
|
200
|
+
credential
|
|
201
|
+
}, {
|
|
202
|
+
F: __dxlog_file,
|
|
203
|
+
L: 122,
|
|
204
|
+
S: void 0,
|
|
205
|
+
C: (f, a) => f(...a)
|
|
206
|
+
});
|
|
207
|
+
invariant(credential.id, "beta credential missing id", {
|
|
208
|
+
F: __dxlog_file,
|
|
209
|
+
L: 124,
|
|
210
|
+
S: void 0,
|
|
211
|
+
A: [
|
|
212
|
+
"credential.id",
|
|
213
|
+
"'beta credential missing id'"
|
|
214
|
+
]
|
|
215
|
+
});
|
|
216
|
+
const presentation = await halo.presentCredentials({
|
|
217
|
+
ids: [
|
|
218
|
+
credential.id
|
|
219
|
+
]
|
|
220
|
+
});
|
|
221
|
+
return {
|
|
222
|
+
Authorization: `Bearer ${codec.encode(presentation)}`
|
|
223
|
+
};
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
export {
|
|
227
|
+
HeaderCodec,
|
|
228
|
+
codec,
|
|
229
|
+
matchServiceCredential,
|
|
230
|
+
publicKeyToDid,
|
|
231
|
+
uploadWorkerFunction,
|
|
232
|
+
getUserFunctionUrlInMetadata,
|
|
233
|
+
setUserFunctionUrlInMetadata
|
|
234
|
+
};
|
|
235
|
+
//# sourceMappingURL=chunk-EZ7PWOLU.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/edge/codec.ts", "../../../src/edge/header-codec.ts", "../../../src/edge/functions.ts", "../../../src/edge/hub-protocol.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { PublicKey } from '@dxos/keys';\n\nimport { HeaderCodec, type EncodedKey } from './header-codec';\n\nexport const codec = new HeaderCodec<PublicKey>({\n isKey: (value: any): value is PublicKey => value instanceof PublicKey,\n encode: (key: PublicKey): EncodedKey => ({ __k: key.toHex() }),\n decode: (value: EncodedKey): PublicKey => PublicKey.fromHex(value.__k),\n});\n", "//\n// Copyright 2024 DXOS.org\n//\n\n// TODO(burdon): Factor out from @dxos/hub-protocol\n\nconst ENCODING = 'base64';\n\nexport type EncodedKey = { __k: string };\n\nexport type EncodedBytes = { __b: string };\n\nexport type EncodedTimestamp = { __t: number };\n\nexport interface KeyCodec<KeyType> {\n isKey(value: any): value is KeyType;\n encode(key: KeyType): EncodedKey;\n decode(value: EncodedKey): KeyType;\n}\n\n// TODO(wittjosiah): Use deepMapValues from @dxos/util -- it handles recursion for you.\nexport class HeaderCodec<Key> {\n constructor(private readonly _keyCodec: KeyCodec<Key>) {}\n\n encode(obj: any): string {\n const objectToEncode = this._prepareForEncoding(obj);\n const encoded = Buffer.from(JSON.stringify(objectToEncode));\n return encoded.toString(ENCODING);\n }\n\n decode<Type>(buffer: string): Type {\n const json = JSON.parse(Buffer.from(buffer, ENCODING).toString());\n return this._decodeRecursive(json);\n }\n\n private _decodeRecursive(obj: any): any {\n return this._recurse(obj, (value) => {\n if (isEncodedBytes(value)) {\n return decodeBytes(value.__b);\n }\n if (isEncodedKey(value)) {\n return this._keyCodec.decode(value);\n }\n if (isEncodedDate(value)) {\n return decodeDate(value);\n }\n return null;\n });\n }\n\n private _prepareForEncoding(obj: any): any {\n return this._recurse(obj, (value) => {\n if (value instanceof Uint8Array) {\n return { __b: encodeBytes(value) };\n }\n if (this._keyCodec.isKey(value)) {\n return this._keyCodec.encode(value);\n }\n if (value instanceof Date) {\n return encodeDate(value);\n }\n return null;\n });\n }\n\n private _recurse(obj: any, handler: (obj: object) => any | null): any {\n if (typeof obj !== 'object') {\n return obj;\n }\n const handled = handler(obj);\n if (handled != null) {\n return handled;\n }\n if (Array.isArray(obj)) {\n const result = [];\n for (const elem of obj) {\n result.push(this._recurse(elem, handler));\n }\n return result;\n }\n const result: any = {};\n for (const [key, value] of Object.entries(obj)) {\n result[key] = this._recurse(value, handler);\n }\n return result;\n }\n}\n\nconst isEncodedKey = (obj: object): obj is EncodedKey => '__k' in obj && typeof obj.__k === 'string';\nconst isEncodedBytes = (obj: object): obj is EncodedBytes => '__b' in obj && typeof obj.__b === 'string';\nconst isEncodedDate = (obj: object): obj is EncodedTimestamp => '__t' in obj && typeof obj.__t === 'number';\n\nconst encodeDate = (date: Date) => ({ __t: date.getTime() });\nconst decodeDate = (timestamp: EncodedTimestamp) => new Date(timestamp.__t);\n\nconst encodeBytes = (bytes: Uint8Array) => Buffer.from(bytes).toString(ENCODING);\nconst decodeBytes = (bytes: string) => Buffer.from(bytes, ENCODING);\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { type DID } from 'iso-did/types';\n\nimport { Trigger } from '@dxos/async';\nimport { type Config } from '@dxos/client';\nimport { type Halo } from '@dxos/client-protocol';\nimport { type ObjectMeta } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { log } from '@dxos/log';\nimport { type Credential } from '@dxos/protocols/proto/dxos/halo/credentials';\n\nimport { codec } from './codec';\nimport { matchServiceCredential } from './hub-protocol';\n\n// TODO: use URL scheme for source?\nconst USERFUNCTIONS_META_KEY = 'dxos.org/service/function';\nconst USERFUNCTIONS_CREDENTIAL_CAPABILITY = 'composer:beta';\n\n// TODO: Synchronize API types with server\nexport type UserFunctionUploadResult = {\n result: 'success' | 'error';\n errorMessage?: string;\n functionId?: string;\n functionVersionNumber?: number;\n};\n\nexport type UploadWorkerProps = {\n clientConfig: Config;\n halo: Halo;\n name?: string;\n source: string;\n functionId?: string;\n ownerDid: DID;\n credentialLoadTimeout?: number; // ms to wait for credentials to load\n};\n\nconst defaultUserFunctionsBaseUrl = 'http://localhost:8600';\n// const userFunctionsBaseUrl = 'https://functions-nftest.dxos.workers.dev';\n\nconst getBaseUrl = (config: Config) => {\n return config.get('runtime.app.env.DX_USERFUNCTIONS_BASE_URL') || defaultUserFunctionsBaseUrl;\n};\n\nexport const uploadWorkerFunction = async ({\n clientConfig,\n halo,\n name,\n source,\n ownerDid,\n functionId,\n credentialLoadTimeout,\n}: UploadWorkerProps) => {\n const identity = halo.identity.get();\n if (!identity) {\n throw new Error('Identity not available');\n }\n const userFunctionsBaseUrl = getBaseUrl(clientConfig);\n\n // TODO: codify naming convention\n const uploadUrl = `${userFunctionsBaseUrl}/${ownerDid}` + (functionId ? `/${functionId}` : '');\n\n log('Upload', { functionId, source, name, identityKey: identity.identityKey, uploadUrl: uploadUrl.toString() });\n const response = await fetch(uploadUrl, {\n method: 'PUT',\n body: JSON.stringify({ script: source, name }),\n headers: await presentationForIdentity({ halo, timeout: credentialLoadTimeout }),\n });\n if (!response.ok) {\n return (await response.json()) as UserFunctionUploadResult;\n }\n\n const res = await response.json();\n log('Upload response', { status: response.status, res });\n\n return res as UserFunctionUploadResult;\n};\n\nexport const getUserFunctionUrlInMetadata = (meta: ObjectMeta) => {\n return meta.keys.find((key) => key.source === USERFUNCTIONS_META_KEY)?.id;\n};\n\nexport const setUserFunctionUrlInMetadata = (meta: ObjectMeta, functionUrl: string) => {\n const key = meta.keys.find((key) => key.source === USERFUNCTIONS_META_KEY);\n if (key) {\n if (key.id !== functionUrl) {\n throw new Error('Metadata mismatch');\n }\n } else {\n meta.keys.push({ source: USERFUNCTIONS_META_KEY, id: functionUrl });\n }\n};\n\n// TODO: handle team owner\n\nconst presentationForIdentity = async ({ halo, timeout }: { halo: Halo; timeout?: number }) => {\n let credential: Credential | undefined;\n\n if (timeout) {\n const credentialsLoaded = new Trigger();\n halo.credentials.subscribe((c) => {\n credential = c.find(matchServiceCredential([USERFUNCTIONS_CREDENTIAL_CAPABILITY]));\n if (credential) {\n credentialsLoaded.wake();\n }\n });\n await credentialsLoaded.wait({\n timeout,\n });\n } else {\n credential = halo\n .queryCredentials()\n .toSorted((a, b) => b.issuanceDate.getTime() - a.issuanceDate.getTime())\n .find(matchServiceCredential([USERFUNCTIONS_CREDENTIAL_CAPABILITY]));\n }\n\n if (!credential) {\n throw new Error('No credential found');\n }\n log('credential found', { credential });\n // TODO(wittjosiah): If id is required to present credentials, then it should always be present for queried credentials.\n invariant(credential.id, 'beta credential missing id');\n const presentation = await halo.presentCredentials({ ids: [credential.id] });\n\n return { Authorization: `Bearer ${codec.encode(presentation)}` };\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { type DID } from 'iso-did/types';\n\nimport { type Credential } from '@dxos/client/halo';\nimport { type PublicKey } from '@dxos/keys';\n\n// TODO(burdon): Factor out to @dxos/hub-protocol.\n\nexport const matchServiceCredential =\n (capabilities: string[] = []) =>\n (credential: Credential) => {\n if (credential.subject.assertion['@type'] !== 'dxos.halo.credentials.ServiceAccess') {\n return false;\n }\n\n const { capabilities: credentialCapabilities } = credential.subject.assertion;\n return capabilities.every((capability) => credentialCapabilities.includes(capability));\n };\n\n// TODO: reconcile with @dxos/hub-protocol\n\nexport const publicKeyToDid = (key: PublicKey): DID => {\n return `did:key:${key.toHex()}`;\n};\n"],
|
|
5
|
+
"mappings": ";AAIA,SAASA,iBAAiB;;;ACE1B,IAAMC,WAAW;AAeV,IAAMC,cAAN,MAAMA;EACXC,YAA6BC,WAA0B;SAA1BA,YAAAA;EAA2B;EAExDC,OAAOC,KAAkB;AACvB,UAAMC,iBAAiB,KAAKC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AkBM,gBAAAA;AAC7D,QAAMH,MAAMH,KAAKC,KAAKC,KAAK,CAACC,SAAQA,KAAI7B,WAAWV,sBAAAA;AACnD,MAAIuC,KAAK;AACP,QAAIA,IAAIC,OAAOE,aAAa;AAC1B,YAAM,IAAI3B,MAAM,mBAAA;IAClB;EACF,OAAO;AACLqB,SAAKC,KAAKM,KAAK;MAAEjC,QAAQV;MAAwBwC,IAAIE;IAAY,CAAA;EACnE;AACF;AAIA,IAAMb,0BAA0B,OAAO,EAAErB,MAAMsB,QAAO,MAAoC;AACxF,MAAIc;AAEJ,MAAId,SAAS;AACX,UAAMe,oBAAoB,IAAIC,QAAAA;AAC9BtC,SAAKuC,YAAYC,UAAU,CAACC,MAAAA;AAC1BL,mBAAaK,EAAEX,KAAKY,uBAAuB;QAACjD;OAAoC,CAAA;AAChF,UAAI2C,YAAY;AACdC,0BAAkBM,KAAI;MACxB;IACF,CAAA;AACA,UAAMN,kBAAkBO,KAAK;MAC3BtB;IACF,CAAA;EACF,OAAO;AACLc,iBAAapC,KACV6C,iBAAgB,EAChBC,SAAS,CAACC,GAAGC,MAAMA,EAAEC,aAAaC,QAAO,IAAKH,EAAEE,aAAaC,QAAO,CAAA,EACpEpB,KAAKY,uBAAuB;MAACjD;KAAoC,CAAA;EACtE;AAEA,MAAI,CAAC2C,YAAY;AACf,UAAM,IAAI7B,MAAM,qBAAA;EAClB;AACAG,MAAI,oBAAoB;IAAE0B;EAAW,GAAA;;;;;;AAErCe,YAAUf,WAAWJ,IAAI,8BAAA;;;;;;;;;AACzB,QAAMoB,eAAe,MAAMpD,KAAKqD,mBAAmB;IAAEC,KAAK;MAAClB,WAAWJ;;EAAI,CAAA;AAE1E,SAAO;IAAEuB,eAAe,UAAUC,MAAMC,OAAOL,YAAAA,CAAAA;EAAgB;AACjE;",
|
|
6
|
+
"names": ["PublicKey", "ENCODING", "HeaderCodec", "constructor", "_keyCodec", "encode", "obj", "objectToEncode", "_prepareForEncoding", "encoded", "Buffer", "from", "JSON", "stringify", "toString", "decode", "buffer", "json", "parse", "_decodeRecursive", "_recurse", "value", "isEncodedBytes", "decodeBytes", "__b", "isEncodedKey", "isEncodedDate", "decodeDate", "Uint8Array", "encodeBytes", "isKey", "Date", "encodeDate", "handler", "handled", "Array", "isArray", "result", "elem", "push", "key", "Object", "entries", "__k", "__t", "date", "getTime", "timestamp", "bytes", "codec", "HeaderCodec", "isKey", "value", "PublicKey", "encode", "key", "__k", "toHex", "decode", "fromHex", "Trigger", "invariant", "log", "matchServiceCredential", "capabilities", "credential", "subject", "assertion", "credentialCapabilities", "every", "capability", "includes", "publicKeyToDid", "key", "toHex", "USERFUNCTIONS_META_KEY", "USERFUNCTIONS_CREDENTIAL_CAPABILITY", "defaultUserFunctionsBaseUrl", "getBaseUrl", "config", "get", "uploadWorkerFunction", "clientConfig", "halo", "name", "source", "ownerDid", "functionId", "credentialLoadTimeout", "identity", "Error", "userFunctionsBaseUrl", "uploadUrl", "log", "identityKey", "toString", "response", "fetch", "method", "body", "JSON", "stringify", "script", "headers", "presentationForIdentity", "timeout", "ok", "json", "res", "status", "getUserFunctionUrlInMetadata", "meta", "keys", "find", "key", "id", "setUserFunctionUrlInMetadata", "functionUrl", "push", "credential", "credentialsLoaded", "Trigger", "credentials", "subscribe", "c", "matchServiceCredential", "wake", "wait", "queryCredentials", "toSorted", "a", "b", "issuanceDate", "getTime", "invariant", "presentation", "presentCredentials", "ids", "Authorization", "codec", "encode"]
|
|
7
|
+
}
|