wikibase-cli 17.0.10 → 18.0.1

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,4 +1,3 @@
1
- import fetch from 'node-fetch'
2
1
  import { isEntitySchemaId } from 'wikibase-sdk'
3
2
  import { exitOnMissingInstance } from '#lib/exit_on_missing'
4
3
  import program from './program.js'
@@ -1,5 +1,4 @@
1
1
  import crypto from 'node:crypto'
2
- import fetch from 'node-fetch'
3
2
  import { isEntityId } from 'wikibase-sdk'
4
3
  import { exitOnMissingInstance } from '#lib/exit_on_missing'
5
4
  import program from '#lib/program'
@@ -34,6 +34,7 @@ function makeRequest (url, format) {
34
34
  headers: {
35
35
  Accept: formatAcceptHeader[format],
36
36
  },
37
+ parseResponseAsJson: format === 'json',
37
38
  })
38
39
  } else {
39
40
  const [ postUrl, urlencodedSparql ] = url.split('?')
@@ -45,6 +46,7 @@ function makeRequest (url, format) {
45
46
  'Content-type': 'application/sparql-query',
46
47
  Accept: formatAcceptHeader[format],
47
48
  },
49
+ parseResponseAsJson: format === 'json',
48
50
  })
49
51
  }
50
52
  }
@@ -72,6 +74,9 @@ function parseResult (format) {
72
74
  results = minimizeSimplifiedSparqlResults(simplifySparqlResults(results))
73
75
  if (indexAttribute) results = indexBy(results, indexAttribute)
74
76
  }
77
+ if (format === 'csv') {
78
+ results = results.replace(/\r\n/g, '\n')
79
+ }
75
80
  return results
76
81
  } catch (err) {
77
82
  err.context = { results }
package/lib/request.js CHANGED
@@ -1,26 +1,7 @@
1
- // Using a custom agent to set keepAlive=true
2
- // https://nodejs.org/api/http.html#http_class_http_agent
3
- // https://github.com/bitinn/node-fetch#custom-agent
4
- import http from 'node:http'
5
- import https from 'node:https'
6
- import { pick } from 'lodash-es'
7
- import fetch from 'node-fetch'
8
1
  import { debug } from '#lib/debug'
9
2
  import program from './program.js'
10
3
  import globalHeaders from './request_headers.js'
11
4
 
12
- const httpAgent = new http.Agent({ keepAlive: true })
13
- const httpsAgent = new https.Agent({ keepAlive: true })
14
- // Useful to:
15
- // - accept self-signed certificates
16
- // - accept certificates that would otherwise generate a UNABLE_TO_VERIFY_LEAF_SIGNATURE error
17
- const insecureHttpsAgent = new https.Agent({ keepAlive: true, rejectUnauthorized: false })
18
- const tlsErrorsHosts = new Set([ 'datos.bne.es' ])
19
-
20
- const agent = ({ host, protocol }) => {
21
- if (tlsErrorsHosts.has(host)) return protocol === 'http:' ? httpAgent : insecureHttpsAgent
22
- else return protocol === 'http:' ? httpAgent : httpsAgent
23
- }
24
5
  const buildHeaders = customHeaders => {
25
6
  if (customHeaders) return Object.assign({}, globalHeaders, customHeaders)
26
7
  else return globalHeaders
@@ -29,41 +10,46 @@ const buildHeaders = customHeaders => {
29
10
  function request (url, options) {
30
11
  const { method = 'get', headers, body } = options
31
12
  debug('request', method.toUpperCase(), url, { headers, body })
13
+ options.keepalive = true
32
14
  return fetch(url, options)
33
15
  }
34
16
 
35
17
  export const get = async url => {
36
- const res = await request(url, { headers: globalHeaders, agent })
18
+ const res = await request(url, { headers: globalHeaders })
37
19
  return handleResponse({ res, url })
38
20
  }
39
21
 
40
- export const customGet = async ({ url, headers }) => {
41
- const res = await request(url, { headers: buildHeaders(headers), agent })
42
- return handleResponse({ res, url })
22
+ export const customGet = async ({ url, headers, parseResponseAsJson = true }) => {
23
+ const res = await request(url, { headers: buildHeaders(headers) })
24
+ return handleResponse({ res, url, parseResponseAsJson })
43
25
  }
44
26
 
45
- export const post = async ({ url, body, headers }) => {
27
+ export const post = async ({ url, body, headers, parseResponseAsJson = true }) => {
46
28
  const method = 'post'
47
- const res = await request(url, { method, body, headers: buildHeaders(headers), agent })
48
- return handleResponse({ res, url, method })
29
+ const res = await request(url, { method, body, headers: buildHeaders(headers) })
30
+ return handleResponse({ res, url, method, parseResponseAsJson })
49
31
  }
50
32
 
51
- async function handleResponse ({ res, url, method }) {
33
+ async function handleResponse ({ res, url, method, parseResponseAsJson = true }) {
52
34
  let body = await res.text()
53
35
  const { logResponseHeaders } = program
54
36
  if (logResponseHeaders) {
55
- let headers = res.headers.raw()
37
+ const headers = {}
56
38
  if (typeof logResponseHeaders === 'string') {
57
39
  const headersSubset = logResponseHeaders.split(',')
58
- headers = pick(headers, headersSubset)
40
+ for (const headerName of headersSubset) {
41
+ headers[headerName] = res.headers.get(headerName)?.split(';').map(val => val.trim())
42
+ }
59
43
  }
60
44
  console.error(JSON.stringify({ request: { url, method }, response: { headers } }))
61
45
  }
62
- // When Wikibase crash it doesn't return JSON errors anymore
63
- try {
64
- body = JSON.parse(body)
65
- } catch (err) {
66
- console.error('could not parse response body as JSON', err, { body })
46
+ if (parseResponseAsJson) {
47
+ // When Wikibase crash it doesn't return JSON errors anymore
48
+ try {
49
+ body = JSON.parse(body)
50
+ } catch (err) {
51
+ console.error('could not parse response body as JSON', err, { body })
52
+ }
67
53
  }
68
54
  const { status: statusCode } = res
69
55
  if (statusCode >= 400) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wikibase-cli",
3
- "version": "17.0.10",
3
+ "version": "18.0.1",
4
4
  "description": "A command-line interface to Wikibase",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -14,7 +14,7 @@
14
14
  "lint": "eslint --config .eslintrc.cjs bin/* lib test",
15
15
  "lint-fix": "eslint --config .eslintrc.cjs --fix bin/* lib test",
16
16
  "lint-staged": "./scripts/lint_staged",
17
- "prepublishOnly": "npm run lint && npm test",
17
+ "prepublishOnly": "export MOCHA_OPTIONS='--bail'; npm run lint && npm test",
18
18
  "postpublish": "npm run docker:publish && git push --tags",
19
19
  "test": "mocha --exit --timeout 20000 $MOCHA_OPTIONS",
20
20
  "update-toc": "./scripts/update_toc"
@@ -52,13 +52,12 @@
52
52
  "copy-paste": "^1.5.0",
53
53
  "lodash-es": "^4.17.21",
54
54
  "mkdirp": "^3.0.1",
55
- "node-fetch": "^2.6.0",
56
55
  "open": "^9.1.0",
57
56
  "read": "^2.1.0",
58
57
  "shell-quote": "^1.8.1",
59
58
  "split": "^1.0.1",
60
59
  "through": "^2.3.8",
61
- "wikibase-edit": "^6.0.4",
60
+ "wikibase-edit": "^7.0.2",
62
61
  "wikibase-sdk": "^10.0.2",
63
62
  "wikidata-lang": "^2.0.11"
64
63
  },
@@ -75,6 +74,6 @@
75
74
  "should": "^13.2.3"
76
75
  },
77
76
  "engines": {
78
- "node": ">= 14.17.0"
77
+ "node": ">= 18"
79
78
  }
80
79
  }