@knowlearning/agents 0.9.138 → 0.9.140
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/root.js +24 -4
- package/agents/generic/index.js +2 -3
- package/agents/generic/message-queue.js +2 -2
- package/browser.d.ts +18 -4
- package/package.json +1 -1
package/agents/browser/root.js
CHANGED
|
@@ -11,13 +11,32 @@ function isLocal() { return localStorage.getItem('api') === 'local' }
|
|
|
11
11
|
|
|
12
12
|
const API_HOST = isLocal() ? DEVELOPMENT_HOST : REMOTE_HOST
|
|
13
13
|
|
|
14
|
-
// TODO: remove this hack when we can set sid cookie through websocket handshake
|
|
14
|
+
// TODO: remove this hack when we can set partitioned sid cookie through websocket handshake
|
|
15
|
+
// deno is partly in the way on teh set side, and browser support is in the way for
|
|
16
|
+
// the client side.
|
|
15
17
|
fetch(`http${ SECURE ? 's' : '' }://${API_HOST}/_sid-check`, { method: 'GET', credentials: 'include' })
|
|
16
|
-
.then(response =>
|
|
18
|
+
.then(async response => {
|
|
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()
|
|
25
|
+
}
|
|
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()
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
console.warn('Issue Connecting To the API Server')
|
|
36
|
+
}
|
|
37
|
+
})
|
|
17
38
|
|
|
18
39
|
export default options => {
|
|
19
|
-
const { host } = window.location
|
|
20
|
-
|
|
21
40
|
const Connection = function () {
|
|
22
41
|
const ws = new WebSocket(`ws${ SECURE ? 's' : '' }://${API_HOST}`)
|
|
23
42
|
|
|
@@ -37,6 +56,7 @@ export default options => {
|
|
|
37
56
|
|
|
38
57
|
const agent = GenericAgent({
|
|
39
58
|
token: options.getToken || getToken,
|
|
59
|
+
sid: () => localStorage.getItem('sid'),
|
|
40
60
|
domain: window.location.host,
|
|
41
61
|
Connection,
|
|
42
62
|
uuid,
|
package/agents/generic/index.js
CHANGED
|
@@ -9,11 +9,10 @@ import downloadImplementation from '../download.js'
|
|
|
9
9
|
// for resoling default scope in context
|
|
10
10
|
const DEFAULT_SCOPE_NAME = '[]'
|
|
11
11
|
const UPLOAD_TYPE = 'application/json;type=upload'
|
|
12
|
-
const POSTGRES_QUERY_TYPE = 'application/json;type=postgres-query'
|
|
13
12
|
const TAG_TYPE = 'application/json;type=tag'
|
|
14
13
|
const DOMAIN_CLAIM_TYPE = 'application/json;type=domain-claim'
|
|
15
14
|
|
|
16
|
-
export default function Agent({ Connection, domain, token, uuid, fetch, applyPatch, login, logout, reboot, handleDomainMessage, log:passedLog=console.log }) {
|
|
15
|
+
export default function Agent({ Connection, domain, token, sid, uuid, fetch, applyPatch, login, logout, reboot, handleDomainMessage, log:passedLog=console.log }) {
|
|
17
16
|
const states = {}
|
|
18
17
|
const watchers = {}
|
|
19
18
|
const keyToSubscriptionId = {}
|
|
@@ -29,7 +28,7 @@ export default function Agent({ Connection, domain, token, uuid, fetch, applyPat
|
|
|
29
28
|
reconnect,
|
|
30
29
|
synced,
|
|
31
30
|
environment
|
|
32
|
-
] = messageQueue({ token, domain, Connection, watchers, states, applyPatch, log, login, interact, reboot, trigger, handleDomainMessage })
|
|
31
|
+
] = messageQueue({ token, sid, domain, Connection, watchers, states, applyPatch, log, login, interact, reboot, trigger, handleDomainMessage })
|
|
33
32
|
|
|
34
33
|
// initialize session
|
|
35
34
|
environment()
|
|
@@ -8,7 +8,7 @@ function activePatch(patch) {
|
|
|
8
8
|
return structuredClone(patch).filter(({ path }) => 'active' === path.shift())
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
export default function messageQueue({ token, domain, Connection, watchers, states, applyPatch, log, login, reboot, handleDomainMessage, trigger }) {
|
|
11
|
+
export default function messageQueue({ token, sid, domain, Connection, watchers, states, applyPatch, log, login, reboot, handleDomainMessage, trigger }) {
|
|
12
12
|
let connection
|
|
13
13
|
let user
|
|
14
14
|
let authed = false
|
|
@@ -115,7 +115,7 @@ export default function messageQueue({ token, domain, Connection, watchers, stat
|
|
|
115
115
|
if (!sessionMetrics.connected) sessionMetrics.connected = Date.now()
|
|
116
116
|
log('AUTHORIZING NEWLY OPENED CONNECTION FOR SESSION:', session)
|
|
117
117
|
failedConnections = 0
|
|
118
|
-
connection.send({ token: await token(), session, domain })
|
|
118
|
+
connection.send({ token: await token(), sid: await sid?.(), session, domain })
|
|
119
119
|
}
|
|
120
120
|
|
|
121
121
|
connection.onmessage = async message => {
|
package/browser.d.ts
CHANGED
|
@@ -17,15 +17,29 @@ export interface AgentEnvironment {
|
|
|
17
17
|
context: string[];
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
export interface AgentUploadInfo {
|
|
21
|
+
name?: string,
|
|
22
|
+
type?: string,
|
|
23
|
+
data?: string | ArrayBuffer,
|
|
24
|
+
id: string,
|
|
25
|
+
browser?: boolean,
|
|
26
|
+
accept?: string,
|
|
27
|
+
}
|
|
28
|
+
|
|
20
29
|
export interface Agent {
|
|
30
|
+
login(provider: string): void;
|
|
31
|
+
logout(): void;
|
|
32
|
+
uuid(): string;
|
|
33
|
+
state(id: string, user?: string, domain?: string): Promise<object>;
|
|
34
|
+
metadata(id: string, user?: string, domain?: string): Promise<object>;
|
|
35
|
+
watch(id: string, callback: (update: { state: object }) => void, user?: string, domain?: string): void;
|
|
36
|
+
upload(info?: AgentUploadInfo): Promise<string>;
|
|
37
|
+
download(id?: string): Promise<Response>;
|
|
21
38
|
environment(userId?: string): Promise<AgentEnvironment>;
|
|
22
|
-
state(ns?: string, user?: string, domain?: string): any;
|
|
23
|
-
upload(userId?: string): Promise<string>;
|
|
24
|
-
download(userId?: string): Promise<string>;
|
|
25
39
|
close(): void;
|
|
26
40
|
reset(ns: string): Promise<void>;
|
|
27
41
|
synced(): Promise<void>;
|
|
28
42
|
}
|
|
29
43
|
|
|
30
|
-
const agent: Agent;
|
|
44
|
+
declare const agent: Agent;
|
|
31
45
|
export default agent;
|