Improve child performances (#1314)

This commit is contained in:
Tomas Della Vedova
2020-09-23 11:31:09 +02:00
committed by delvedor
parent 35eb96e2b4
commit 3db1bed4bd
368 changed files with 17064 additions and 32728 deletions

View File

@ -51,7 +51,6 @@ function start (opts) {
const kibanaTypeDefFile = join(packageFolder, 'kibana.d.ts')
const docOutputFile = join(__dirname, '..', 'docs', 'reference.asciidoc')
const requestParamsOutputFile = join(packageFolder, 'requestParams.d.ts')
const allSpec = []
log.text = 'Cleaning API folder...'
rimraf.sync(join(apiOutputFolder, '*.js'))
@ -66,8 +65,25 @@ function start (opts) {
const xPackFolderContents = readdirSync(xPackFolder)
.filter(file => !file.startsWith('data_frame_transform_deprecated'))
apiFolderContents.forEach(generateApiFile(apiFolder, log))
xPackFolderContents.forEach(generateApiFile(xPackFolder, log))
const allSpec = apiFolderContents.concat(xPackFolderContents)
.filter(file => file !== '_common.json')
.filter(file => !file.includes('deprecated'))
.sort()
.map(file => {
try {
return JSON.parse(readFileSync(join(apiFolder, file), 'utf8'))
} catch (err) {
return JSON.parse(readFileSync(join(xPackFolder, file), 'utf8'))
}
})
const namespaces = namespacify(apiFolderContents.concat(xPackFolderContents))
for (const namespace in namespaces) {
if (namespace === '_common') continue
const code = generate(namespace, namespaces[namespace], { apiFolder, xPackFolder }, opts.branch || opts.tag)
const filePath = join(apiOutputFolder, `${namespace}.js`)
writeFileSync(filePath, code, { encoding: 'utf8' })
}
writeFileSync(
requestParamsOutputFile,
@ -75,7 +91,7 @@ function start (opts) {
{ encoding: 'utf8' }
)
const { fn: factory, types, kibanaTypes } = genFactory(apiOutputFolder, [apiFolder, xPackFolder])
const { fn: factory, types, kibanaTypes } = genFactory(apiOutputFolder, [apiFolder, xPackFolder], namespaces)
writeFileSync(
mainOutputFile,
factory,
@ -104,9 +120,6 @@ function start (opts) {
lintFiles(log, () => {
log.text = 'Generating documentation'
const allSpec = apiFolderContents.filter(f => f !== '_common.json')
.map(f => require(join(apiFolder, f)))
.concat(xPackFolderContents.map(f => require(join(xPackFolder, f))))
writeFileSync(
docOutputFile,
generateDocs(require(join(apiFolder, '_common.json')), allSpec),
@ -117,27 +130,6 @@ function start (opts) {
})
})
function generateApiFile (apiFolder, log) {
var common = null
try {
common = require(join(apiFolder, '_common.json'))
} catch (e) {}
return function _generateApiFile (file) {
if (file === '_common.json') return
log.text = `Processing ${file}`
const spec = require(join(apiFolder, file))
// const { stability } = spec[Object.keys(spec)[0]]
// if (stability !== 'stable') return
allSpec.push(spec)
const code = generate(opts.branch || opts.tag, spec, common)
const filePath = join(apiOutputFolder, `${file.slice(0, file.lastIndexOf('.'))}.js`)
writeFileSync(filePath, code, { encoding: 'utf8' })
}
}
function lintFiles (log, cb) {
log.text = 'Linting...'
const files = [join(packageFolder, '*.js'), join(apiOutputFolder, '*.js')]
@ -148,4 +140,21 @@ function start (opts) {
cb()
})
}
function namespacify (apis) {
return apis
.map(api => api.slice(0, -5))
.filter(api => api !== '_common')
.filter(api => !api.includes('deprecated'))
.reduce((acc, val) => {
if (val.includes('.')) {
val = val.split('.')
acc[val[0]] = acc[val[0]] || []
acc[val[0]].push(val[1])
} else {
acc[val] = []
}
return acc
}, {})
}
}

View File

@ -23,6 +23,7 @@
'use strict'
const { join } = require('path')
const dedent = require('dedent')
const semver = require('semver')
const allowedMethods = {
@ -70,7 +71,115 @@ const ndjsonApi = [
'xpack.monitoring.bulk'
]
function generate (version, spec, common) {
function generateNamespace (namespace, nested, folders, version) {
const common = require(join(folders.apiFolder, '_common.json'))
let code = dedent`
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
'use strict'
/* eslint camelcase: 0 */
/* eslint no-unused-vars: 0 */
const { handleError, snakeCaseKeys, normalizeArguments, kConfigurationError } = require('../utils')
`
if (nested.length > 0) {
let getters = ''
for (const n of nested) {
if (n.includes('_')) {
const nameSnaked = n
.replace(/\.([a-z])/g, k => k[1].toUpperCase())
.replace(/_([a-z])/g, k => k[1].toUpperCase())
getters += `${n}: { get () { return this.${nameSnaked} } },\n`
}
}
const api = generateMultiApi(version, namespace, nested, common, folders)
if (getters.length > 0) {
getters = `Object.defineProperties(${api.namespace}Api.prototype, {\n${getters}})`
}
code += `
const acceptedQuerystring = ${JSON.stringify(api.acceptedQuerystring)}
const snakeCase = ${JSON.stringify(api.snakeCase)}
function ${api.namespace}Api (transport, ConfigurationError) {
this.transport = transport
this[kConfigurationError] = ConfigurationError
}
${api.code}
${getters}
module.exports = ${api.namespace}Api
`
} else {
let spec = null
try {
spec = require(join(folders.apiFolder, `${namespace}.json`))
} catch (err) {
spec = require(join(folders.xPackFolder, `${namespace}.json`))
}
const api = generateSingleApi(version, spec, common)
code += `
const acceptedQuerystring = ${JSON.stringify(api.acceptedQuerystring)}
const snakeCase = ${JSON.stringify(api.snakeCase)}
${api.code}
module.exports = ${api.name}Api
`
}
return code
}
function generateMultiApi (version, namespace, nested, common, folders) {
const namespaceSnaked = namespace
.replace(/\.([a-z])/g, k => k[1].toUpperCase())
.replace(/_([a-z])/g, k => k[1].toUpperCase())
let code = ''
const snakeCase = {}
const acceptedQuerystring = []
for (const n of nested) {
let spec = null
const nameSnaked = n
.replace(/\.([a-z])/g, k => k[1].toUpperCase())
.replace(/_([a-z])/g, k => k[1].toUpperCase())
try {
spec = require(join(folders.apiFolder, `${namespace}.${n}.json`))
} catch (err) {
spec = require(join(folders.xPackFolder, `${namespace}.${n}.json`))
}
const api = generateSingleApi(version, spec, common)
code += `${Uppercase(namespaceSnaked)}Api.prototype.${nameSnaked} = ${api.code}\n\n`
Object.assign(snakeCase, api.snakeCase)
for (const q of api.acceptedQuerystring) {
if (!acceptedQuerystring.includes(q)) {
acceptedQuerystring.push(q)
}
}
}
return { code, snakeCase, acceptedQuerystring, namespace: Uppercase(namespaceSnaked) }
}
function generateSingleApi (version, spec, common) {
const release = semver.valid(version) ? semver.major(version) : version
const api = Object.keys(spec)[0]
const name = api
@ -136,37 +245,15 @@ function generate (version, spec, common) {
}
const code = `
function ${safeWords(name)} (params, options, callback) {
options = options || {}
if (typeof options === 'function') {
callback = options
options = {}
}
if (typeof params === 'function' || params == null) {
callback = params
params = {}
options = {}
}
function ${name}Api (params, options, callback) {
;[params, options, callback] = normalizeArguments(params, options, callback)
${genRequiredChecks()}
${genUrlValidation(paths, api)}
// validate headers object
if (options.headers != null && typeof options.headers !== 'object') {
const err = new ConfigurationError(\`Headers should be an object, instead got: \${typeof options.headers}\`)
return handleError(err, callback)
}
var warnings = []
var { ${genQueryBlacklist(false)}, ...querystring } = params
querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring, warnings)
var ignore = options.ignore
if (typeof ignore === 'number') {
options.ignore = [ignore]
}
querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring)
var path = ''
${buildPath(api)}
@ -179,58 +266,18 @@ function generate (version, spec, common) {
querystring
}
options.warnings = warnings.length === 0 ? null : warnings
return makeRequest(request, options, callback)
return this.transport.request(request, options, callback)
}
`.trim() // always call trim to avoid newlines
const fn = dedent`
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
'use strict'
/* eslint camelcase: 0 */
/* eslint no-unused-vars: 0 */
function build${name[0].toUpperCase() + name.slice(1)} (opts) {
// eslint-disable-next-line no-unused-vars
const { makeRequest, ConfigurationError, handleError, snakeCaseKeys } = opts
const acceptedQuerystring = [
${acceptedQuerystring.map(q => `'${q}'`).join(',\n')}
]
const snakeCase = {
${genSnakeCaseMap()}
}
${generateDocumentation(spec[api], api)}
return ${code}
return {
name,
code,
acceptedQuerystring: acceptedQuerystring,
snakeCase: genSnakeCaseMap(),
documentation: generateDocumentation(spec[api], api)
}
module.exports = build${name[0].toUpperCase() + name.slice(1)}
`
// new line at the end of file
return fn + '\n'
function genRequiredChecks (param) {
const code = required
.map(_genRequiredCheck)
@ -251,7 +298,7 @@ function generate (version, spec, common) {
if (param === camelCased) {
const check = `
if (params['${param}'] == null) {
const err = new ConfigurationError('Missing required parameter: ${param}')
const err = new this[kConfigurationError]('Missing required parameter: ${param}')
return handleError(err, callback)
}
`
@ -259,7 +306,7 @@ function generate (version, spec, common) {
} else {
const check = `
if (params['${param}'] == null && params['${camelCased}'] == null) {
const err = new ConfigurationError('Missing required parameter: ${param} or ${camelCased}')
const err = new this[kConfigurationError]('Missing required parameter: ${param} or ${camelCased}')
return handleError(err, callback)
}
`
@ -270,7 +317,7 @@ function generate (version, spec, common) {
function _noBody () {
const check = `
if (params.body != null) {
const err = new ConfigurationError('This API does not require a body')
const err = new this[kConfigurationError]('This API does not require a body')
return handleError(err, callback)
}
`
@ -287,13 +334,10 @@ function generate (version, spec, common) {
return acceptedQuerystring.reduce((acc, val, index) => {
if (toCamelCase(val) !== val) {
acc += `${toCamelCase(val)}: '${val}'`
if (index !== acceptedQuerystring.length - 1) {
acc += ',\n'
}
acc[toCamelCase(val)] = val
}
return acc
}, '')
}, {})
}
function genQueryBlacklist (addQuotes = true) {
@ -363,13 +407,11 @@ function generate (version, spec, common) {
for (var i = 0; i < sortedPaths.length; i++) {
const { path, methods } = sortedPaths[i]
if (sortedPaths.length === 1) {
code += `
if (method == null) method = ${generatePickMethod(methods)}
code += `if (method == null) method = ${generatePickMethod(methods)}
path = ${genPath(path)}
`
} else if (i === 0) {
code += `
if (${genCheck(path)}) {
code += `if (${genCheck(path)}) {
if (method == null) method = ${generatePickMethod(methods)}
path = ${genPath(path)}
}
@ -389,60 +431,10 @@ function generate (version, spec, common) {
}
}
// var hasStaticPath = false
// var singlePathComponent = false
// paths
// .filter(path => {
// if (path.indexOf('{') > -1) return true
// if (hasStaticPath === false) {
// hasStaticPath = true
// return true
// }
// return false
// })
// .sort((a, b) => (b.split('{').length + b.split('/').length) - (a.split('{').length + a.split('/').length))
// .forEach((path, index, arr) => {
// if (arr.length === 1) {
// singlePathComponent = true
// code += `
// path = ${genPath(path)}
// `
// } else if (index === 0) {
// code += `
// if (${genCheck(path)}) {
// path = ${genPath(path)}
// `
// } else if (index === arr.length - 1) {
// code += `
// } else {
// path = ${genPath(path)}
// `
// } else {
// code += `
// } else if (${genCheck(path)}) {
// path = ${genPath(path)}
// `
// }
// })
// code += singlePathComponent ? '' : '}'
return code
}
}
function safeWords (str) {
switch (str) {
// delete is a reserved word
case 'delete':
return '_delete'
// index is also a parameter
case 'index':
return '_index'
default:
return str
}
}
function generatePickMethod (methods) {
if (methods.length === 1) {
return `'${methods[0]}'`
@ -529,7 +521,7 @@ function genUrlValidation (paths, api) {
}
}
code += `)) {
const err = new ConfigurationError('Missing required parameter of the url: ${params.join(', ')}')
const err = new this[kConfigurationError]('Missing required parameter of the url: ${params.join(', ')}')
return handleError(err, callback)
`
})
@ -574,5 +566,9 @@ function intersect (first, ...rest) {
}, first)
}
module.exports = generate
function Uppercase (str) {
return str[0].toUpperCase() + str.slice(1)
}
module.exports = generateNamespace
module.exports.ndjsonApi = ndjsonApi

View File

@ -35,24 +35,29 @@ const ndjsonApiKey = ndjsonApi
})
.map(toPascalCase)
function genFactory (folder, paths) {
function genFactory (folder, paths, namespaces) {
// get all the API files
const apiFiles = readdirSync(folder)
// const apiFiles = readdirSync(folder)
const apiFiles = readdirSync(paths[0])
.concat(readdirSync(paths[1]))
.filter(file => file !== '_common.json')
.filter(file => !file.includes('deprecated'))
.sort()
const types = apiFiles
.map(file => {
const name = file
.slice(0, -3)
.slice(0, -5)
.replace(/\.([a-z])/g, k => k[1].toUpperCase())
.replace(/_([a-z])/g, k => k[1].toUpperCase())
return file
.slice(0, -3) // remove `.js` extension
.slice(0, -5) // remove `.json` extension
.split('.')
.reverse()
.reduce((acc, val) => {
const spec = readSpec(paths, file.slice(0, -3))
const isHead = isHeadMethod(spec, file.slice(0, -3))
const body = hasBody(spec, file.slice(0, -3))
const spec = readSpec(paths, file.slice(0, -5))
const isHead = isHeadMethod(spec, file.slice(0, -5))
const body = hasBody(spec, file.slice(0, -5))
const methods = acc === null ? buildMethodDefinition({ kibana: false }, val, name, body, isHead) : null
const obj = {}
if (methods) {
@ -73,18 +78,18 @@ function genFactory (folder, paths) {
const kibanaTypes = apiFiles
.map(file => {
const name = file
.slice(0, -3)
.slice(0, -5)
.replace(/\.([a-z])/g, k => k[1].toUpperCase())
.replace(/_([a-z])/g, k => k[1].toUpperCase())
return file
.slice(0, -3) // remove `.js` extension
.slice(0, -5) // remove `.json` extension
.split('.')
.reverse()
.reduce((acc, val) => {
const spec = readSpec(paths, file.slice(0, -3))
const isHead = isHeadMethod(spec, file.slice(0, -3))
const body = hasBody(spec, file.slice(0, -3))
const spec = readSpec(paths, file.slice(0, -5))
const isHead = isHeadMethod(spec, file.slice(0, -5))
const body = hasBody(spec, file.slice(0, -5))
const methods = acc === null ? buildMethodDefinition({ kibana: true }, val, name, body, isHead) : null
const obj = {}
if (methods) {
@ -99,37 +104,6 @@ function genFactory (folder, paths) {
})
.reduce((acc, val) => deepmerge(acc, val), {})
const apis = apiFiles
.map(file => {
// const name = format(file.slice(0, -3))
return file
.slice(0, -3) // remove `.js` extension
.split('.')
.reverse()
.reduce((acc, val) => {
const obj = {
[val]: acc === null
? `lazyLoad('${file.slice(0, -3)}', opts)` // `${name}(opts)`
: acc
}
if (isSnakeCased(val)) {
obj[camelify(val)] = acc === null
? `lazyLoad('${file.slice(0, -3)}', opts)` // `${name}(opts)`
: acc
}
return obj
}, null)
})
.reduce((acc, val) => deepmerge(acc, val), {})
// serialize the API object
const apisStr = JSON.stringify(apis, null, 2)
// split & join to fix the indentation
.split('\n')
.join('\n ')
// remove useless quotes
.replace(/"/g, '')
// serialize the type object
const typesStr = Object.keys(types)
.map(key => {
@ -156,6 +130,48 @@ function genFactory (folder, paths) {
.replace(/"/g, '')
.replace(/,$/gm, '')
let apisStr = ''
const getters = []
for (const namespace in namespaces) {
if (namespaces[namespace].length > 0) {
getters.push(`${camelify(namespace)}: {
get () {
if (this[k${toPascalCase(camelify(namespace))}] === null) {
this[k${toPascalCase(camelify(namespace))}] = new ${toPascalCase(camelify(namespace))}Api(this.transport, this[kConfigurationError])
}
return this[k${toPascalCase(camelify(namespace))}]
}
},\n`)
if (namespace.includes('_')) {
getters.push(`${namespace}: { get () { return this.${camelify(namespace)} } },\n`)
}
} else {
apisStr += `ESAPI.prototype.${camelify(namespace)} = ${camelify(namespace)}Api\n`
if (namespace.includes('_')) {
getters.push(`${namespace}: { get () { return this.${camelify(namespace)} } },\n`)
}
}
}
apisStr += '\nObject.defineProperties(ESAPI.prototype, {\n'
for (const getter of getters) {
apisStr += getter
}
apisStr += '})'
let modules = ''
let symbols = ''
let symbolsInstance = ''
for (const namespace in namespaces) {
if (namespaces[namespace].length > 0) {
modules += `const ${toPascalCase(camelify(namespace))}Api = require('./api/${namespace}')\n`
symbols += `const k${toPascalCase(camelify(namespace))} = Symbol('${toPascalCase(camelify(namespace))}')\n`
symbolsInstance += `this[k${toPascalCase(camelify(namespace))}] = null\n`
} else {
modules += `const ${camelify(namespace)}Api = require('./api/${namespace}')\n`
}
}
const fn = dedent`
/*
* Licensed to Elasticsearch B.V. under one or more contributor
@ -178,62 +194,17 @@ function genFactory (folder, paths) {
'use strict'
const assert = require('assert')
${modules}
const { kConfigurationError } = require('./utils')
${symbols}
function ESAPI (opts) {
assert(opts.makeRequest, 'Missing makeRequest function')
assert(opts.ConfigurationError, 'Missing ConfigurationError class')
assert(opts.result, 'Missing default result object')
const { result } = opts
opts.handleError = handleError
opts.snakeCaseKeys = snakeCaseKeys
const apis = ${apisStr}
return apis
function handleError (err, callback) {
if (callback) {
process.nextTick(callback, err, result)
return { then: noop, catch: noop, abort: noop }
}
return Promise.reject(err)
}
function snakeCaseKeys (acceptedQuerystring, snakeCase, querystring, warnings) {
var target = {}
var keys = Object.keys(querystring)
for (var i = 0, len = keys.length; i < len; i++) {
var key = keys[i]
target[snakeCase[key] || key] = querystring[key]
if (acceptedQuerystring.indexOf(snakeCase[key] || key) === -1) {
warnings.push('Client - Unknown parameter: "' + key + '", sending it as query parameter')
}
}
return target
}
this[kConfigurationError] = opts.ConfigurationError
${symbolsInstance}
}
// It's unlikely that a user needs all of our APIs,
// and since require is a sync operation that takes time
// (given the amount of APIs we have), let's lazy load them,
// so a given API file will be required only
// if the user actually needs that API.
// The following implementation takes advantage
// of js closures to have a simple cache with the least overhead.
function lazyLoad (file, opts) {
var fn = null
return function _lazyLoad (params, options, callback) {
if (fn === null) {
fn = require(${'`./api/${file}.js`'})(opts)
}
return fn(params, options, callback)
}
}
function noop () {}
${apisStr}
module.exports = ESAPI
`