From 2751844997123c0a561bd4396a205da89e6e5cf1 Mon Sep 17 00:00:00 2001 From: Hasan Eroglu Date: Sun, 21 Jul 2024 19:45:23 +0200 Subject: [PATCH 1/2] refactor: replace deprecated detectProtocolSchemes Signed-off-by: Hasan Eroglu --- packages/web/package-lock.json | 6 ++++++ packages/web/package.json | 1 + packages/web/src/scripts/console.js | 7 ++++--- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/web/package-lock.json b/packages/web/package-lock.json index 4c0eddf2..241a529b 100644 --- a/packages/web/package-lock.json +++ b/packages/web/package-lock.json @@ -13,6 +13,7 @@ "@thingweb/async-api-converter": "^1.0.0", "@thingweb/json-spell-checker": "^1.0.0", "@thingweb/open-api-converter": "^1.0.0", + "@thingweb/td-utils": "^1.0.0", "d3": "^3.5.17", "d3-tip": "^0.6.7", "express": "^4.19.2", @@ -2056,6 +2057,11 @@ "swagger-parser": "^10.0.3" } }, + "node_modules/@thingweb/td-utils": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@thingweb/td-utils/-/td-utils-1.0.0.tgz", + "integrity": "sha512-6TJ+2NUDicP1sXvWqorchj+F1Cc/zvCFJHtgIR8p5wI78jNSb0ZQHmpC2/bfZK2AkuE+6dgrzEcJcWNbVcB3sQ==" + }, "node_modules/@trysound/sax": { "version": "0.2.0", "dev": true, diff --git a/packages/web/package.json b/packages/web/package.json index 52f5b26d..54beb73e 100644 --- a/packages/web/package.json +++ b/packages/web/package.json @@ -48,6 +48,7 @@ "@thingweb/async-api-converter": "^1.0.0", "@thingweb/json-spell-checker": "^1.0.0", "@thingweb/open-api-converter": "^1.0.0", + "@thingweb/td-utils": "^1.0.0", "d3": "^3.5.17", "d3-tip": "^0.6.7", "express": "^4.19.2", diff --git a/packages/web/src/scripts/console.js b/packages/web/src/scripts/console.js index ead7175c..2025399a 100644 --- a/packages/web/src/scripts/console.js +++ b/packages/web/src/scripts/console.js @@ -25,7 +25,8 @@ import { AASView } from './aas' import { defaultsView, defaultsJsonBtn, defaultsYamlBtn, defaultsAddBtn } from './defaults' import { visualize } from './visualize' import { validationView, validationTab } from './validation' -import { convertTDYamlToJson, detectProtocolSchemes } from '../../../core/dist/web-bundle.min.js' +import { convertTDYamlToJson } from '../../../core/dist/web-bundle.min.js' +import { detectProtocolSchemes } from '@thingweb/td-utils/dist/web-bundle.min.js' import { generateOAP, generateAAP, addDefaultsUtil, validate, generateAAS, resetValidationStatus } from './util' import { editorList, getEditorData } from './editor' import { textIcon } from './main.js' @@ -239,7 +240,7 @@ function enableAPIConversionWithProtocol(editorInstance) { if (protocolSchemes) { if (openApiTab.checked === true) { - if (["http", "https"].some(p => protocolSchemes.includes(p))) { + if (["http", "https"].some(p => Object.keys(protocolSchemes).includes(p))) { generateOAP(editorInstance["_domElement"].dataset.modeId, editorInstance) openApiView.classList.remove("hidden") } else { @@ -248,7 +249,7 @@ function enableAPIConversionWithProtocol(editorInstance) { } if (asyncApiTab.checked === true) { - if (["mqtt", "mqtts"].some(p => protocolSchemes.includes(p))) { + if (["mqtt", "mqtts"].some(p => Object.keys(protocolSchemes).includes(p))) { generateAAP(editorInstance["_domElement"].dataset.modeId, editorInstance) asyncApiView.classList.remove("hidden") } else { From 9d8e5d17101a6e66a79dbf0563ba63e7119df25c Mon Sep 17 00:00:00 2001 From: Hasan Eroglu Date: Sun, 21 Jul 2024 19:47:20 +0200 Subject: [PATCH 2/2] refactor: remove deprecated logic and tests Signed-off-by: Hasan Eroglu --- packages/core/index.js | 86 ------------------- .../core/tests/protocol-detection.test.js | 69 --------------- .../tests/protocol-detection/httpAndMqtt.json | 22 ----- .../tests/protocol-detection/noProtocol.json | 14 --- .../tests/protocol-detection/onlyHttp.json | 33 ------- .../tests/protocol-detection/onlyMqtt.json | 33 ------- .../protocol-detection/secureProtocols.json | 23 ----- 7 files changed, 280 deletions(-) delete mode 100644 packages/core/tests/protocol-detection.test.js delete mode 100644 packages/core/tests/protocol-detection/httpAndMqtt.json delete mode 100644 packages/core/tests/protocol-detection/noProtocol.json delete mode 100644 packages/core/tests/protocol-detection/onlyHttp.json delete mode 100644 packages/core/tests/protocol-detection/onlyMqtt.json delete mode 100644 packages/core/tests/protocol-detection/secureProtocols.json diff --git a/packages/core/index.js b/packages/core/index.js index ee778fa1..707081a6 100644 --- a/packages/core/index.js +++ b/packages/core/index.js @@ -40,7 +40,6 @@ module.exports.decompress = decompress; module.exports.checkTmOptionalPointer = coreAssertions.checkTmOptionalPointer; module.exports.checkLinkedAffordances = coreAssertions.checkLinkedAffordances; module.exports.checkLinkedStructure = coreAssertions.checkLinkedStructure; -module.exports.detectProtocolSchemes = detectProtocolSchemes; module.exports.convertTDJsonToYaml = convertTDJsonToYaml; module.exports.convertTDYamlToJson = convertTDYamlToJson; @@ -1297,91 +1296,6 @@ function decompress(data) { return lzs.decompressFromEncodedURIComponent(data); } -/** - * Detect protocl schemes of a TD - * @param {string} td TD string to detect protocols of - * return List of available protocol schemes - */ -function detectProtocolSchemes(td) { - let tdJson; - - try { - tdJson = JSON.parse(td); - } catch (err) { - return []; - } - - const baseUriProtocol = getHrefProtocol(tdJson.base); - const thingProtocols = detectProtocolInForms(tdJson.forms); - const actionsProtocols = detectProtocolInAffordance(tdJson.actions); - const eventsProtocols = detectProtocolInAffordance(tdJson.events); - const propertiesProtcols = detectProtocolInAffordance(tdJson.properties); - const protocolSchemes = [ - ...new Set([ - baseUriProtocol, - ...thingProtocols, - ...actionsProtocols, - ...eventsProtocols, - ...propertiesProtcols, - ]), - ].filter((p) => p !== undefined); - - return protocolSchemes; -} - -/** - * Detect protocols in a TD affordance - * @param {object} affordance That belongs to a TD - * @returns List of protocol schemes - */ -function detectProtocolInAffordance(affordance) { - if (!affordance) { - return []; - } - - let protocolSchemes = []; - - for (const key in affordance) { - if (key) { - protocolSchemes = protocolSchemes.concat(detectProtocolInForms(affordance[key].forms)); - } - } - - return protocolSchemes; -} - -/** - * Detect protocols in a TD forms or a TD affordance forms - * @param {object} forms Forms field of a TD or a TD affordance - * @returns List of protocol schemes - */ -function detectProtocolInForms(forms) { - if (!forms) { - return []; - } - - const protocolSchemes = []; - - forms.forEach((form) => { - protocolSchemes.push(getHrefProtocol(form.href)); - }); - - return protocolSchemes; -} - -/** - * Get protocol used in href - * @param {string} href URI string - * @returns Protocol name - */ -function getHrefProtocol(href) { - if (!href) { - return; - } - - return href.split(":")[0]; -} - /** * Convert TD from json to yaml * @param {string} td TD in json string form diff --git a/packages/core/tests/protocol-detection.test.js b/packages/core/tests/protocol-detection.test.js deleted file mode 100644 index 41f57b1e..00000000 --- a/packages/core/tests/protocol-detection.test.js +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v. 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0, or the W3C Software Notice and - * Document License (2015-05-13) which is available at - * https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document. - * - * SPDX-License-Identifier: EPL-2.0 OR W3C-20150513 - */ - -const fs = require("fs"); -const path = require("path"); -const { detectProtocolSchemes } = require("../index"); -const tdValidator = require("../index").tdValidator; - -const rootDir = path.join("./", "tests"); - -const dirPath = path.join(rootDir, "protocol-detection"); -const fileNames = fs.readdirSync(dirPath); -const refResult = { - report: { - json: "passed", - schema: "passed", - defaults: expect.stringMatching(/warning|passed/), - jsonld: "passed", - additional: "passed", - }, - details: { - enumConst: "passed", - propItems: "passed", - security: "passed", - propUniqueness: "passed", - multiLangConsistency: "passed", - linksRelTypeCount: "passed", - readWriteOnly: "passed", - uriVariableSecurity: "passed", - }, - detailComments: expect.any(Object), -}; -fileNames.forEach((fileName) => { - test(fileName, (done) => { - fs.readFile(path.join(dirPath, fileName), "utf-8", (err, tdToTest) => { - if (err) { - done(err); - } - tdValidator(tdToTest, () => {}, {}).then( - (result) => { - const tdJson = JSON.parse(tdToTest); - const protocolSchemes = tdJson.protocolSchemes; - - if (protocolSchemes.length === 0) { - expect(detectProtocolSchemes(tdToTest)).toEqual([]); - } else { - expect(detectProtocolSchemes(tdToTest)).toEqual(expect.arrayContaining(protocolSchemes)); - } - done(); - }, - (errTwo) => { - done(errTwo); - } - ); - }); - }); -}); diff --git a/packages/core/tests/protocol-detection/httpAndMqtt.json b/packages/core/tests/protocol-detection/httpAndMqtt.json deleted file mode 100644 index 269d8cca..00000000 --- a/packages/core/tests/protocol-detection/httpAndMqtt.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "id": "urn:simple", - "@context": "https://www.w3.org/2022/wot/td/v1.1", - "title": "MyLampThing", - "description": "Valid TD copied from the spec's first example", - "securityDefinitions": { - "basic_sc": { "scheme": "basic", "in": "header" } - }, - "security": ["basic_sc"], - "properties": { - "status": { - "type": "string", - "forms": [{ "href": "http://mylamp.example.com/status" }] - } - }, - "actions": { - "toggle": { - "forms": [{ "href": "mqtt://mylamp.example.com/toggle" }] - } - }, - "protocolSchemes": ["http", "mqtt"] -} diff --git a/packages/core/tests/protocol-detection/noProtocol.json b/packages/core/tests/protocol-detection/noProtocol.json deleted file mode 100644 index 2ba85d30..00000000 --- a/packages/core/tests/protocol-detection/noProtocol.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "id": "urn:simple", - "@context": "https://www.w3.org/2022/wot/td/v1.1", - "title": "MyLampThing", - "description": "Valid TD copied from the spec's first example", - "securityDefinitions": { - "basic_sc": { "scheme": "basic", "in": "header" } - }, - "security": ["basic_sc"], - "properties": {}, - "actions": {}, - "events": {}, - "protocolSchemes": [] -} diff --git a/packages/core/tests/protocol-detection/onlyHttp.json b/packages/core/tests/protocol-detection/onlyHttp.json deleted file mode 100644 index 2fc5c480..00000000 --- a/packages/core/tests/protocol-detection/onlyHttp.json +++ /dev/null @@ -1,33 +0,0 @@ -{ - "id": "urn:simple", - "@context": "https://www.w3.org/2022/wot/td/v1.1", - "title": "MyLampThing", - "description": "Valid TD copied from the spec's first example", - "securityDefinitions": { - "basic_sc": { "scheme": "basic", "in": "header" } - }, - "security": ["basic_sc"], - "properties": { - "status": { - "type": "string", - "forms": [{ "href": "http://mylamp.example.com/status" }] - } - }, - "actions": { - "toggle": { - "forms": [{ "href": "http://mylamp.example.com/toggle" }] - } - }, - "events": { - "overheating": { - "data": { "type": "string" }, - "forms": [ - { - "href": "http://mylamp.example.com/oh", - "subprotocol": "longpoll" - } - ] - } - }, - "protocolSchemes": ["http"] -} diff --git a/packages/core/tests/protocol-detection/onlyMqtt.json b/packages/core/tests/protocol-detection/onlyMqtt.json deleted file mode 100644 index f8dbb704..00000000 --- a/packages/core/tests/protocol-detection/onlyMqtt.json +++ /dev/null @@ -1,33 +0,0 @@ -{ - "id": "urn:simple", - "@context": "https://www.w3.org/2022/wot/td/v1.1", - "title": "MyLampThing", - "description": "Valid TD copied from the spec's first example", - "securityDefinitions": { - "basic_sc": { "scheme": "basic", "in": "header" } - }, - "security": ["basic_sc"], - "properties": { - "status": { - "type": "string", - "forms": [{ "href": "mqtt://mylamp.example.com/status" }] - } - }, - "actions": { - "toggle": { - "forms": [{ "href": "mqtt://mylamp.example.com/toggle" }] - } - }, - "events": { - "overheating": { - "data": { "type": "string" }, - "forms": [ - { - "href": "mqtt://mylamp.example.com/oh", - "subprotocol": "longpoll" - } - ] - } - }, - "protocolSchemes": ["mqtt"] -} diff --git a/packages/core/tests/protocol-detection/secureProtocols.json b/packages/core/tests/protocol-detection/secureProtocols.json deleted file mode 100644 index 16b59309..00000000 --- a/packages/core/tests/protocol-detection/secureProtocols.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "id": "urn:simple", - "@context": "https://www.w3.org/2022/wot/td/v1.1", - "title": "MyLampThing", - "description": "Valid TD copied from the spec's first example", - "securityDefinitions": { - "basic_sc": { "scheme": "basic", "in": "header" } - }, - "security": ["basic_sc"], - "base": "mqtts://mylamp.example.com/status", - "events": { - "overheating": { - "data": { "type": "string" }, - "forms": [ - { - "href": "https://mylamp.example.com/oh", - "subprotocol": "longpoll" - } - ] - } - }, - "protocolSchemes": ["https", "mqtts"] -}