@knowlearning/agents 0.9.140 → 0.9.142
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/agents/browser/initialize.js +11 -7
- package/agents/browser/root.js +21 -19
- package/package.json +1 -1
|
@@ -5,6 +5,12 @@ import selectFile from './select-file.js'
|
|
|
5
5
|
|
|
6
6
|
let Agent
|
|
7
7
|
|
|
8
|
+
function getNamespacedScope(namespace, scope) {
|
|
9
|
+
const allow = namespace?.allow || []
|
|
10
|
+
const prefix = typeof namespace === 'string' ? namespace : namespace?.prefix
|
|
11
|
+
return prefix && !validateUUID(scope) && !allow.some(allowPrefix => scope.startsWith(allowPrefix)) ? `${prefix}/${scope}` : scope
|
|
12
|
+
}
|
|
13
|
+
|
|
8
14
|
export default function browserAgent(options={}) {
|
|
9
15
|
if (Agent && !options.unique) return Agent
|
|
10
16
|
|
|
@@ -80,16 +86,14 @@ function embed(environment, iframe) {
|
|
|
80
86
|
}
|
|
81
87
|
else if (type === 'interact') {
|
|
82
88
|
let { scope, patch } = message
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
await Agent.interact(scope, patch, false)
|
|
87
|
-
if (listeners.mutate) listeners.mutate({ scope })
|
|
89
|
+
const namespacedScope = getNamespacedScope(environment.namespace, scope)
|
|
90
|
+
await Agent.interact(namespacedScope, patch, false)
|
|
91
|
+
if (listeners.mutate) listeners.mutate({ scope: namespacedScope })
|
|
88
92
|
sendDown({}) // TODO: might want to send down the interaction index
|
|
89
93
|
}
|
|
90
94
|
else if (type === 'metadata') {
|
|
91
95
|
const { scope, user, domain } = message
|
|
92
|
-
const namespacedScope = environment.namespace
|
|
96
|
+
const namespacedScope = getNamespacedScope(environment.namespace, scope)
|
|
93
97
|
|
|
94
98
|
sendDown(await Agent.metadata(namespacedScope, user, domain))
|
|
95
99
|
}
|
|
@@ -100,7 +104,7 @@ function embed(environment, iframe) {
|
|
|
100
104
|
}
|
|
101
105
|
else if (type === 'state') {
|
|
102
106
|
const { scope, user, domain } = message
|
|
103
|
-
const namespacedScope = environment.namespace
|
|
107
|
+
const namespacedScope = getNamespacedScope(environment.namespace, scope)
|
|
104
108
|
|
|
105
109
|
const statePromise = Agent.state(namespacedScope, user, domain)
|
|
106
110
|
|
package/agents/browser/root.js
CHANGED
|
@@ -14,30 +14,32 @@ const API_HOST = isLocal() ? DEVELOPMENT_HOST : REMOTE_HOST
|
|
|
14
14
|
// TODO: remove this hack when we can set partitioned sid cookie through websocket handshake
|
|
15
15
|
// deno is partly in the way on teh set side, and browser support is in the way for
|
|
16
16
|
// the client side.
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
17
|
+
async function ensureSidEstablished() {
|
|
18
|
+
const response = await fetch(`http${ SECURE ? 's' : '' }://${API_HOST}/_sid-check`, { method: 'GET', credentials: 'include' })
|
|
19
|
+
const hasLocalStorageSID = !!localStorage.getItem('sid')
|
|
20
|
+
if (response.status === 201) {
|
|
21
|
+
if (!hasLocalStorageSID) {
|
|
22
|
+
const sentSid = await response.text()
|
|
23
|
+
localStorage.setItem('sid', sentSid)
|
|
24
|
+
location.reload()
|
|
26
25
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
console.warn('Issue Connecting To the API Server')
|
|
26
|
+
}
|
|
27
|
+
else if (response.status === 200) {
|
|
28
|
+
// if we reach here, assumably the server has seen an sid cookie
|
|
29
|
+
if (hasLocalStorageSID) {
|
|
30
|
+
localStorage.removeItem('sid')
|
|
31
|
+
location.reload()
|
|
36
32
|
}
|
|
37
|
-
}
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
console.warn('Issue Connecting To the API Server')
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
38
|
|
|
39
39
|
export default options => {
|
|
40
|
+
ensureSidEstablished()
|
|
40
41
|
const Connection = function () {
|
|
42
|
+
|
|
41
43
|
const ws = new WebSocket(`ws${ SECURE ? 's' : '' }://${API_HOST}`)
|
|
42
44
|
|
|
43
45
|
this.send = message => ws.send(JSON.stringify(message))
|