@knowlearning/agents 0.9.139 → 0.9.141

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.
@@ -11,14 +11,35 @@ 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
15
- fetch(`http${ SECURE ? 's' : '' }://${API_HOST}/_sid-check`, { method: 'GET', credentials: 'include' })
16
- .then(response => response.status === 201 && location.reload())
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.
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()
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
-
40
+ ensureSidEstablished()
21
41
  const Connection = function () {
42
+
22
43
  const ws = new WebSocket(`ws${ SECURE ? 's' : '' }://${API_HOST}`)
23
44
 
24
45
  this.send = message => ws.send(JSON.stringify(message))
@@ -37,6 +58,7 @@ export default options => {
37
58
 
38
59
  const agent = GenericAgent({
39
60
  token: options.getToken || getToken,
61
+ sid: () => localStorage.getItem('sid'),
40
62
  domain: window.location.host,
41
63
  Connection,
42
64
  uuid,
@@ -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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knowlearning/agents",
3
- "version": "0.9.139",
3
+ "version": "0.9.141",
4
4
  "description": "API for embedding applications in KnowLearning systems.",
5
5
  "main": "node.js",
6
6
  "browser": "browser.js",