Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use detectProtocolSchemes from @thingweb/td-utils for check… #29

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion node-red-node-wot/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions node-red-node-wot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"build": "npm run copy:src2dist && tsc && npm run build:plugin",
"copy:src2dist": "node -e \"require('fs-extra').copySync('./src', './dist')\"",
"build:plugin": "npm run copy:plugin-resources-src2resources && tsc --project ./tsconfig-for-plugin.json",
"copy:plugin-resources-src2resources": "node -e \"require('fs-extra').copySync('./plugin-resources-src', './resources')\"",
"copy:plugin-resources-src2resources": "node -e \"require('fs-extra').copySync('./plugin-resources-src', './resources')\" && node -e \"require('fs-extra').copySync('./node_modules/@thingweb/td-utils/dist/web-bundle.min.js', './resources/@thingweb/td-utils/dist/web-bundle.min.js')\"",
"publish": "npm publish --access=public",
"publish:beta": "npm publish --access=public --tag=beta",
"test": "mocha --require ts-node/register --extension ts --exit"
Expand All @@ -55,7 +55,8 @@
"@node-wot/binding-mqtt": "0.8.14",
"@node-wot/binding-opcua": "0.8.14",
"@node-wot/binding-websockets": "0.8.14",
"@node-wot/core": "0.8.14"
"@node-wot/core": "0.8.14",
"@thingweb/td-utils": "^1.0.0"
},
"devDependencies": {
"@types/chai": "^4.3.12",
Expand Down
46 changes: 5 additions & 41 deletions node-red-node-wot/plugin-resources-src/node-wot-plugin-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,53 +314,17 @@ function generateId() {
return bytes.join("")
}

function checkBinding(tdStr: string, binding: string) {
let td
try {
td = JSON.parse(tdStr)
} catch (err) {
return false
}
const detectProtocolSchemes = (window as any).tdUtils?.detectProtocolSchemes;

const affordances = ["properties", "actions", "events"]
function checkBinding(tdStr: string, binding: string) {
// In case of OPC UA we look for opc.tcp in href not opcua
binding = binding === "opcua" ? "opc.tcp" : binding
// Also different for Modbus
binding = binding === "modbus" ? "modbus+tcp" : binding

// Check base URL if present and then possibly affordances
if (td["base"]) {
let base = new URL(td["base"]).protocol
if (base) {
// remove trailing ':'
base = base.slice(0, -1)
const checkBase = base === binding || base === binding + "s"
if (checkBase) return true
}
}

for (const affordance of affordances) {
if (td[affordance]) {
// item is either a property or an action or an event
for (const item of Object.values(td[affordance])) {
if (!(item as any).forms) return false
let checkForms = (item as any).forms.some((form) => {
try {
const parsed = new URL(form.href)
if (parsed.protocol !== null) {
// remove trailing ':'
const scheme = parsed.protocol.slice(0, -1)
return scheme === binding || scheme === binding + "s"
}
} catch (e) {
return false
}
})
if (checkForms) return true
}
}
}
return false
const bindings = Object.keys(detectProtocolSchemes(tdStr))

return bindings.some(b => b === binding || b === binding + 's')
}

const THING_COMMON_TEMP = `[
Expand Down
2 changes: 2 additions & 0 deletions node-red-node-wot/plugin/node-wot-plugin.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<!-- import tdUtils -->
<script type="module" src="/resources/@thingweb/node-red-node-wot/@thingweb/td-utils/dist/web-bundle.min.js"></script>
<script type="module" src="/resources/@thingweb/node-red-node-wot/node-wot-plugin-lib.js" ></script>
<script type="module">
import {createClientFlowUsingDashboard} from "/resources/@thingweb/node-red-node-wot/node-wot-plugin-lib.js"
Expand Down
46 changes: 5 additions & 41 deletions node-red-node-wot/src/wot-thing.html
Original file line number Diff line number Diff line change
@@ -1,55 +1,19 @@
<!-- import tdUtils -->
<script type="module" src="/resources/@thingweb/node-red-node-wot/@thingweb/td-utils/dist/web-bundle.min.js"></script>
<script type="text/javascript">
// List of supported protocols
// Secure ones (like https) are automatically inferred
const PROTOCOLS = ["http", "ws", "coap", "mqtt", "opcua", "modbus"]

function checkBinding(td_str, binding) {
let td
try {
td = JSON.parse(td_str)
} catch (err) {
return false
}

const affordances = ["properties", "actions", "events"]
// In case of OPC UA we look for opc.tcp in href not opcua
binding = binding === "opcua" ? "opc.tcp" : binding
// Also different for Modbus
binding = binding === "modbus" ? "modbus+tcp" : binding

// Check base URL if present and then possibly affordances
if (td["base"]) {
let base = new URL(td["base"]).protocol
if (base) {
// remove trailing ':'
base = base.slice(0, -1)
const checkBase = base === binding || base === binding + "s"
if (checkBase) return true
}
}

for (const affordance of affordances) {
if (td[affordance]) {
// item is either a property or an action or an event
for (const item of Object.values(td[affordance])) {
if (!item.forms) return false
let checkForms = item.forms.some((form) => {
try {
const parsed = new URL(form.href)
if (parsed.protocol !== null) {
// remove trailing ':'
const scheme = parsed.protocol.slice(0, -1)
return scheme === binding || scheme === binding + "s"
}
} catch (e) {
return false
}
})
if (checkForms) return true
}
}
}
return false
const bindings = Object.keys(tdUtils.detectProtocolSchemes(td_str))

return bindings.some(b => b === binding || b === binding + 's')
}

function containsID(td_str) {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
"prettier": "^2.3.2",
"pretty-quick": "^3.1.1"
}
}
}
Loading