diff --git a/packages/core/index.js b/packages/core/index.js index ee778fa17..707081a6b 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 41f57b1e6..000000000 --- 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 269d8ccab..000000000 --- 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 2ba85d308..000000000 --- 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 2fc5c480b..000000000 --- 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 f8dbb7040..000000000 --- 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 16b59309c..000000000 --- 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"] -}