From a7360b614bf4440cc5babbb4e9d84fce80fecccb Mon Sep 17 00:00:00 2001 From: tpluscode Date: Sat, 18 Dec 2021 21:03:18 +0100 Subject: [PATCH 1/3] test: encoding URI according to RFC3986 --- test/support/test031-strict-url-encode.csv | 2 ++ .../test031-strict-url-encode.csv-metadata.json | 14 ++++++++++++++ test/support/test031-strict-url-encode.nt | 10 ++++++++++ 3 files changed, 26 insertions(+) create mode 100644 test/support/test031-strict-url-encode.csv create mode 100644 test/support/test031-strict-url-encode.csv-metadata.json create mode 100644 test/support/test031-strict-url-encode.nt diff --git a/test/support/test031-strict-url-encode.csv b/test/support/test031-strict-url-encode.csv new file mode 100644 index 0000000..fb67ddc --- /dev/null +++ b/test/support/test031-strict-url-encode.csv @@ -0,0 +1,2 @@ +(foo) +(bar) diff --git a/test/support/test031-strict-url-encode.csv-metadata.json b/test/support/test031-strict-url-encode.csv-metadata.json new file mode 100644 index 0000000..5becee7 --- /dev/null +++ b/test/support/test031-strict-url-encode.csv-metadata.json @@ -0,0 +1,14 @@ +{ + "@context": "http://www.w3.org/ns/csvw", + "url": "test031-strict-url-encode.csv", + "tableSchema": { + "aboutUrl": "http://example.org/data.csv#{_row}", + "columns": [ + { + "titles": "(foo)", + "propertyUrl": "http://example.org/vocab#{_name}", + "valueUrl": "http://example.org/object/{(foo)}" + } + ] + } +} diff --git a/test/support/test031-strict-url-encode.nt b/test/support/test031-strict-url-encode.nt new file mode 100644 index 0000000..2a59ea3 --- /dev/null +++ b/test/support/test031-strict-url-encode.nt @@ -0,0 +1,10 @@ + . +_:c14n0 . +_:c14n0 . +_:c14n0 "1"^^ . +_:c14n0 . +_:c14n1 . +_:c14n1 _:c14n0 . +_:c14n1 . +_:c14n2 . +_:c14n2 _:c14n1 . From 9f92327251d371bd148ebedfa698becbe781e71b Mon Sep 17 00:00:00 2001 From: tpluscode Date: Sat, 18 Dec 2021 21:30:10 +0100 Subject: [PATCH 2/3] fix: produce URIs encoded according to RFC3986 --- lib/MetadataBuilder.js | 1 + lib/metadata/TableSchema.js | 26 +++++++++++++------------- package.json | 3 ++- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/MetadataBuilder.js b/lib/MetadataBuilder.js index a41534a..dc0add4 100644 --- a/lib/MetadataBuilder.js +++ b/lib/MetadataBuilder.js @@ -1,4 +1,5 @@ const fs = require('fs') +const encodeURIComponent = require('strict-uri-encode') class MetadataBuilder { static readFirstLine (filename) { diff --git a/lib/metadata/TableSchema.js b/lib/metadata/TableSchema.js index 141cf94..5448a2b 100644 --- a/lib/metadata/TableSchema.js +++ b/lib/metadata/TableSchema.js @@ -1,7 +1,7 @@ const difference = require('lodash/difference') const namespace = require('../namespace') const parseDateTime = require('../parseDateTime') -const uriTemplate = require('uri-templates') +const uriTemplate = require('url-template') const URL = require('url') const RdfUtils = require('./RdfUtils') @@ -37,10 +37,10 @@ class TableSchema { return } - const aboutUrlTemplate = uriTemplate(aboutUrl) + const aboutUrlTemplate = uriTemplate.parse(aboutUrl) return (row) => { - return this.factory.namedNode(URL.resolve(this.baseIRI, aboutUrlTemplate.fill(row))) // eslint-disable-line node/no-deprecated-api + return this.factory.namedNode(URL.resolve(this.baseIRI, aboutUrlTemplate.expand(row))) // eslint-disable-line node/no-deprecated-api } } @@ -51,7 +51,7 @@ class TableSchema { return } - return uriTemplate(url) + return uriTemplate.parse(url) } parseColumns () { @@ -70,17 +70,17 @@ class TableSchema { const valueUrl = RdfUtils.findValue(this.dataset, node, this.ns.valueUrl) return { - aboutUrl: aboutUrl && uriTemplate(aboutUrl), + aboutUrl: aboutUrl && uriTemplate.parse(aboutUrl), datatype: this.parseDatatype(node), - language: language && uriTemplate(language), + language: language && uriTemplate.parse(language), name, nullValue, defaultValue, - propertyUrl: (propertyUrl && uriTemplate(propertyUrl)) || this.propertyUrl || this.defaultPropertyUrl(name), + propertyUrl: (propertyUrl && uriTemplate.parse(propertyUrl)) || this.propertyUrl || this.defaultPropertyUrl(name), suppressOutput: suppressOutput === 'true', titles, virtual, - valueUrl: valueUrl && uriTemplate(valueUrl) + valueUrl: valueUrl && uriTemplate.parse(valueUrl) } }) } @@ -136,7 +136,7 @@ class TableSchema { return null } - return this.factory.namedNode(URL.resolve(this.baseIRI, column.aboutUrl.fill(row))) // eslint-disable-line node/no-deprecated-api + return this.factory.namedNode(URL.resolve(this.baseIRI, column.aboutUrl.expand(row))) // eslint-disable-line node/no-deprecated-api } value (column, row) { @@ -145,7 +145,7 @@ class TableSchema { } if (column.valueUrl) { - return this.factory.namedNode(column.valueUrl.fill(row)) + return this.factory.namedNode(column.valueUrl.expand(row)) } let value = column.titles.reduce((value, title) => { @@ -169,12 +169,12 @@ class TableSchema { } if (column.datatype.base) { - return this.factory.literal(value, (column.language && column.language.fill(row).toLowerCase()) || column.datatype.base) + return this.factory.literal(value, (column.language && column.language.expand(row).toLowerCase()) || column.datatype.base) } } property (column, row) { - return this.factory.namedNode(column.propertyUrl.fill(row)) + return this.factory.namedNode(column.propertyUrl.expand(row)) } createAllColumns (row) { @@ -198,7 +198,7 @@ class TableSchema { defaultPropertyUrl (name) { return { - fill: () => { + expand: () => { return this.baseIRI + '#' + encodeURI(name) } } diff --git a/package.json b/package.json index 256b043..c38190f 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,8 @@ "lodash": "^4.17.15", "luxon": "^1.17.3", "readable-stream": "^3.4.0", - "uri-templates": "^0.2.0" + "strict-uri-encode": "^2.0.0", + "url-template": "^2" }, "devDependencies": { "@rdfjs/parser-jsonld": "^1.1.2", From 76299def77600b6a5b86f6c5bfe31b42172ea007 Mon Sep 17 00:00:00 2001 From: tpluscode Date: Sat, 18 Dec 2021 21:38:21 +0100 Subject: [PATCH 3/3] chore: encode variable in template --- test/support/test031-strict-url-encode.csv-metadata.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/support/test031-strict-url-encode.csv-metadata.json b/test/support/test031-strict-url-encode.csv-metadata.json index 5becee7..5eb3f39 100644 --- a/test/support/test031-strict-url-encode.csv-metadata.json +++ b/test/support/test031-strict-url-encode.csv-metadata.json @@ -7,7 +7,7 @@ { "titles": "(foo)", "propertyUrl": "http://example.org/vocab#{_name}", - "valueUrl": "http://example.org/object/{(foo)}" + "valueUrl": "http://example.org/object/{%28foo%29}" } ] }