@knowlearning/agents 0.9.180 → 0.9.182

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.
@@ -2,19 +2,22 @@ import { v1 as uuid } from 'uuid'
2
2
  import { applyPatch } from 'fast-json-patch'
3
3
  import { getToken, login, logout } from './auth.js'
4
4
  import GenericAgent from '../generic/index.js'
5
+ import { io } from 'socket.io-client'
5
6
 
6
7
  const TEST_DOMAIN = 'tests.knowlearning.systems'
7
8
  const LANGUAGES = [...navigator.languages]
8
9
 
9
- const API_HOST = localStorage.getItem('API_HOST') || 'api.knowlearning.systems'
10
- // const API_HOST = 'api-test.knowlearning.systems'
11
- //const API_HOST = 'localhost:8765'
10
+ const API_HOST = localStorage.getItem('API_HOST') || 'socket-io.knowlearning.systems'
11
+ // const API_HOST = 'api-test.knowlearning.systems'
12
+ // const API_HOST = 'localhost:8765'
12
13
 
13
- // TODO: remove this hack when we can set partitioned sid cookie through websocket handshake
14
- // deno is partly in the way on teh set side, and browser support is in the way for
15
- // the client side.
14
+ // TODO: remove sid hack when partitioned cookies via WS handshakes are supported
16
15
  async function ensureSidEstablished() {
17
- const response = await fetch(`https://${API_HOST}/_sid-check`, { method: 'GET', credentials: 'include' })
16
+ const response = await fetch(`https://${API_HOST}/_sid-check`, {
17
+ method: 'GET',
18
+ credentials: 'include'
19
+ })
20
+
18
21
  const hasLocalStorageSID = !!localStorage.getItem('sid')
19
22
  if (response.status === 201) {
20
23
  if (!hasLocalStorageSID) {
@@ -24,7 +27,6 @@ async function ensureSidEstablished() {
24
27
  }
25
28
  }
26
29
  else if (response.status === 200) {
27
- // if we reach here, assumably the server has seen an sid cookie
28
30
  if (hasLocalStorageSID) {
29
31
  localStorage.removeItem('sid')
30
32
  location.reload()
@@ -37,20 +39,44 @@ async function ensureSidEstablished() {
37
39
 
38
40
  export default options => {
39
41
  ensureSidEstablished()
42
+
40
43
  const Connection = function () {
44
+ const sid = localStorage.getItem('sid')
45
+
46
+ // socket.io client connection
47
+ const socket = io(`https://${API_HOST}`, {
48
+ withCredentials: true,
49
+ extraHeaders: sid ? { sid } : {}
50
+ })
41
51
 
42
- const ws = new WebSocket(`wss://${API_HOST}`)
52
+ this.send = message => {
53
+ try {
54
+ socket.emit('message', message)
55
+ } catch (err) {
56
+ console.warn('Error sending via socket.io', err)
57
+ }
58
+ }
43
59
 
44
- this.send = message => ws.send(JSON.stringify(message))
45
60
  this.close = info => {
46
61
  this.send({ type: 'close', info })
47
- ws.close()
62
+ socket.disconnect()
48
63
  }
49
64
 
50
- ws.onopen = () => this.onopen()
51
- ws.onmessage = ({ data }) => this.onmessage(data.length === 0 ? null : JSON.parse(data))
52
- ws.onerror = error => this.onerror && this.onerror(error)
53
- ws.onclose = error => this.onclose && this.onclose(error)
65
+ socket.on('connect', () => {
66
+ if (this.onopen) this.onopen()
67
+ })
68
+
69
+ socket.on('message', (data) => {
70
+ if (this.onmessage) this.onmessage(data)
71
+ })
72
+
73
+ socket.on('error', (err) => {
74
+ if (this.onerror) this.onerror(err)
75
+ })
76
+
77
+ socket.on('disconnect', (reason) => {
78
+ if (this.onclose) this.onclose(reason)
79
+ })
54
80
 
55
81
  return this
56
82
  }
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@knowlearning/agents",
3
- "version": "0.9.180",
3
+ "version": "0.9.182",
4
4
  "description": "API for embedding applications in KnowLearning systems.",
5
5
  "main": "node.js",
6
6
  "browser": "browser.js",
7
7
  "type": "module",
8
+ "types": "types.d.ts",
8
9
  "directories": {
9
10
  "example": "examples",
10
11
  "test": "test"
@@ -25,6 +26,7 @@
25
26
  "dependencies": {
26
27
  "@knowlearning/patch-proxy": "^1.3.4",
27
28
  "fast-json-patch": "^3.1.1",
29
+ "socket.io-client": "^4.8.1",
28
30
  "uuid": "^8.3.2"
29
31
  },
30
32
  "devDependencies": {
@@ -19,12 +19,12 @@ export interface AgentEnvironment {
19
19
  }
20
20
 
21
21
  export interface AgentUploadInfo {
22
- name?: string,
23
- type?: string,
24
- data?: string | ArrayBuffer,
25
- id?: string,
26
- browser?: boolean,
27
- accept?: string,
22
+ name?: string;
23
+ type?: string;
24
+ data?: string | ArrayBuffer;
25
+ id?: string;
26
+ browser?: boolean;
27
+ accept?: string;
28
28
  }
29
29
 
30
30
  export interface Agent {