@knowlearning/agents 0.9.183 → 0.9.184

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.
@@ -1,10 +1,7 @@
1
1
  import EmbeddedAgent from '../embedded.js'
2
2
 
3
3
  export default () => {
4
- // default to window opener if present
5
4
  const parent = window.opener ? window.opener : window.parent
6
- const agent = EmbeddedAgent(message => parent.postMessage(message, '*'))
7
-
8
- return agent
5
+ return EmbeddedAgent(message => parent.postMessage(message, '*'))
9
6
  }
10
7
 
@@ -2,6 +2,7 @@ import RootAgent from './root.js'
2
2
  import EmbeddedAgent from './embedded.js'
3
3
  import { v1 as uuid, validate as validateUUID } from 'uuid'
4
4
  import selectFile from './select-file.js'
5
+ import isDomain from './is-domain.js'
5
6
 
6
7
  let Agent = window.__default_knowlearning_agent
7
8
 
@@ -208,7 +209,8 @@ function embed(environment, iframe) {
208
209
  const { id } = environment
209
210
  if (validateUUID(id)) {
210
211
  const { domain } = await Agent.metadata(id)
211
- iframe.src = `${protocol}//${domain}/${id}`
212
+ const player = (await Agent.state(id))?.reference?.player
213
+ iframe.src = `${protocol}//${isDomain(player) ? player : domain}/${id}`
212
214
  }
213
215
  else iframe.src = id // TODO: ensure is url
214
216
 
@@ -0,0 +1,34 @@
1
+ export default function isDomain(value) {
2
+ if (typeof value !== 'string') return false
3
+
4
+ // reject protocol, path, query, fragment
5
+ if (
6
+ value.includes('://') ||
7
+ value.includes('/') ||
8
+ value.includes('?') ||
9
+ value.includes('#')
10
+ ) {
11
+ return false
12
+ }
13
+
14
+ const hostPort = value.split(':')
15
+ if (hostPort.length > 2) return false
16
+
17
+ const [host, port] = hostPort
18
+
19
+ // optional port: 1–65535
20
+ if (port !== undefined) {
21
+ if (!/^\d+$/.test(port)) return false
22
+ const p = Number(port)
23
+ if (p < 1 || p > 65535) return false
24
+ }
25
+
26
+ // localhost
27
+ if (host === 'localhost') return true
28
+
29
+ // DNS hostname with subdomains
30
+ const hostnameRegex =
31
+ /^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/
32
+
33
+ return hostnameRegex.test(host)
34
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knowlearning/agents",
3
- "version": "0.9.183",
3
+ "version": "0.9.184",
4
4
  "description": "API for embedding applications in KnowLearning systems.",
5
5
  "main": "node.js",
6
6
  "browser": "browser.js",