-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompile.js
More file actions
303 lines (256 loc) · 9.93 KB
/
compile.js
File metadata and controls
303 lines (256 loc) · 9.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**
* @fileoverview MicroQL Query Compiler
*
* Compiles query configurations into queryTree
* Handles service validation and dependency extraction.
*/
import _ from 'lodash'
import lodashDeep from 'lodash-deep'
_.mixin(lodashDeep)
import {DEP_REGEX, SERVICE_REGEX, RESERVE_ARGS} from './common.js'
import applyWrappers from './wrappers.js'
import {parseSchema} from './validation.js'
import util from './services/util.js'
import RateLimitedQueue from './ratelimit.js'
import Cache from './cache.js'
// Detects if a descriptor is a chain (nested arrays)
const isChain = (descriptor) => {
return Array.isArray(descriptor) &&
descriptor.length > 0 &&
_.every(descriptor, d => Array.isArray(d))
}
// Detects if a descriptor uses method syntax: ['target', 'service:action', args]
const hasMethodSyntax = (descriptor) => {
return Array.isArray(descriptor) &&
descriptor.length >= 2 &&
typeof descriptor[1] === 'string' &&
SERVICE_REGEX.test(descriptor[1])
}
// Transforms method syntax ['target', 'service:action', args] to standard service call ['service:action', {on: target, ...args}]
const parseServiceDescriptor = (descriptor) => {
// check for method descriptor
if (hasMethodSyntax(descriptor)) {
const [arg0, serviceMethod, args = {}] = descriptor
const [__, serviceName, action] = serviceMethod.match(SERVICE_REGEX)
return [serviceName, action, args, arg0]
} else {
// assume normal service descriptor
const [serviceAction, args = {}] = descriptor
const match = serviceAction?.match(SERVICE_REGEX)
if (!match) {
throw new Error(`Invalid service descriptor. Expected ['service:action', {...}] format. Got: ${JSON.stringify(descriptor)}`)
}
const [, serviceName, action] = match
return [serviceName, action, args]
}
}
// Extracts dependencies from query arguments
const getDeps = (args) => {
const deps = new Set()
_.deepMapValues(args, (value) => {
let m = (typeof value === 'string') && value.match(DEP_REGEX)
if (m) deps.add(m[1])
})
return deps
}
function parseSchemaWithErrorContext(designation, order, schema) {
try {
return parseSchema(schema)
} catch (error) {
error.message = `${designation} ${order} schema parse error:\n${error.message}`
throw error
}
}
// weave the schema compilation with proper context for error reporting
const compileValidators = (args, validators) => {
const order = {
precheck: {query: args.precheck, service: validators.precheck},
postcheck: {service: validators.postcheck, query: args.postcheck}
}
let hasValidators = false
for (const o in order) {
for (const d in order[o])
if (order[o][d]) {
hasValidators = true
order[o][d] = parseSchemaWithErrorContext(d, o, order[o][d])
}
}
return hasValidators ? order : undefined
}
// settings are merged from query level settings and service level settings
// they are placed in their own `settings` key on the compiled service definition
const compileSettings = (queryName, serviceName, args, argtypes, config) => {
const reserveArgs = _.pick(args, RESERVE_ARGS)
// get args with their argtypes set to 'settings'
const settingsArgs = _.pickBy(args, (a, k) => argtypes[k]?.type === 'settings')
// exclude some global settings from being merged with the service
const globalSettings = config.settings ?
_.omit(config.settings, ['onError', 'ignoreErrors', 'cache']) : {}
const settings = _.defaults({}, reserveArgs, ...Object.values(settingsArgs), globalSettings)
// compile onError if we have it
if (settings.onError) {
settings.onError = compileServiceOrChain(queryName, settings.onError, config)
}
// rate limit is defined globally for the service
settings.rateLimit = config.settings?.rateLimit?.[serviceName]
return settings
}
// any time we expect a service, it could instead be a chain
const compileServiceOrChain = (queryName, value, config) => {
const makeFn = (descriptor) => compileServiceFunction(queryName, descriptor, config).service
// is it a chain?
if (isChain(value)) {
return value.map(makeFn)
// or a single service call?
} else {
return makeFn(value)
}
}
// Compile arguments based on argtypes metadata
const compileArgs = (queryName, serviceName, args, argtypes, config, settings) => {
const compiled = {}
for (const [key, value] of Object.entries(args)) {
// compile object to service template
if (argtypes[key]?.type === 'service' && typeof value === 'object' && !Array.isArray(value)) {
const fn = compileServiceFunction(queryName, ['util:template', value], config)
compiled[key] = fn.service
// compile service descriptor
} else if (argtypes[key]?.type === 'service' && Array.isArray(value)) {
compiled[key] = compileServiceOrChain(queryName, value, config)
// reject raw JavaScript functions
} else if (argtypes[key]?.type === 'service' && typeof value === 'function') {
throw new Error(`Raw JavaScript functions are not supported in MicroQL. Use service descriptors instead of raw functions for argument '${key}' in ${serviceName}:${queryName}. Example: ['serviceName:methodName', {arg: '@'}]`)
} else if (RESERVE_ARGS.includes(key)) {
// exclude reserve args
} else {
compiled[key] = value
}
}
for (const [key, type] of Object.entries(argtypes)) {
// inject settings if requested
if (type?.type === 'settings') {
compiled[key] = _.defaults(args[key], settings)
}
}
return compiled
}
// check to see if the service has an argOrder: 0 defined
// and merge arg0 if we have it
function mergeArgs(args, arg0, argtypes = {}, serviceName, action) {
if (!arg0) {
return
}
const [argOrder0] = Object.entries(argtypes).find(([, typeinfo]) => typeinfo.argOrder === 0) || []
if (!argOrder0)
throw new Error(`Method syntax was used for ${serviceName}:${action} but no {argOrder: 0} was defined.`)
args[argOrder0] = arg0
}
// Compiles a service descriptor like ['@', 'util:print', {color: 'green'}]
// into [a compiled function, recursive dependencies]
function compileServiceFunction(queryName, descriptor, config) {
const [serviceName, action, args, arg0] = parseServiceDescriptor(descriptor)
// Validate service exists and has the required method
const service = config.services[serviceName]
if (!service) {
throw new Error(`Service '${serviceName}' not found`)
}
const serviceCall = service[action]
if (typeof service === 'object') {
if (!serviceCall || typeof serviceCall !== 'function') {
throw new Error(`Method '${action}' not found on service '${serviceName}'`)
}
} else {
throw new Error(`Service '${serviceName}' must be an object with methods in the form: async (args) => result`)
}
try {
const argtypes = serviceCall._argtypes || {}
mergeArgs(args, arg0, argtypes, serviceName, action)
const settings = compileSettings(queryName, serviceName, args, argtypes, config)
const rateLimit = config.rateLimiters?.[serviceName]
const compiledArgs = compileArgs(queryName, serviceName, args, argtypes, config, settings)
const validators = compileValidators(args, serviceCall._validators || {}, queryName, serviceName, action)
// Compile function arguments based on _argtypes
const serviceDef = {
type: 'service',
queryName,
serviceName,
action,
validators,
settings,
rateLimit,
args: compiledArgs,
dependencies: getDeps(args),
noTimeout: serviceCall._noTimeout || false
}
// prepare the service with arg resolution, debugging, error handling, timeout, retry
serviceDef.service = applyWrappers(serviceDef, config)
return serviceDef
} catch (error) {
error.message = `[${queryName} - ${serviceName}:${action}] ${error.message}`
throw error
}
}
function compileDescriptor(queryName, descriptor, config) {
// Handle chains - arrays of service calls
if (isChain(descriptor)) {
let allDeps = new Set()
// for each step in chain, collect the service definition and the dependencies
const chainSteps = descriptor.map((d, i) => {
const def = compileServiceFunction(`${queryName}[${i}]`, d, config)
allDeps = allDeps.union(def.dependencies)
def.stepIndex = i
delete def.dependencies
return def
})
return {
type: 'chain',
queryName,
steps: chainSteps,
dependencies: allDeps
}
} else {
// Handle single service call
return compileServiceFunction(queryName, descriptor, config)
}
}
/**
* Compile a query configuration into a queryTree
* @param {Object} config - Query configuration
* @param {Object} config.services - Service objects
* @param {Object} config.queries - Query definitions
* @param {Object} config.given - given data
* @param {boolean} config.debug - Debug logging flag
* @returns {Object} Compiled queryTree
*/
export function compile(config) {
_.defaults(config, {services: {}, queries: {}, debug: false, settings: {}})
const {services, queries, given, debug, settings} = config
_.defaults(config.services, {util})
// set up a cache for query results using the global settings provided
config.cache = new Cache(config.settings.cache)
// Create rate limiter cache to share rate limiters across queries
config.rateLimiters = {}
if (settings.rateLimit) {
for (const [serviceName, interval] of Object.entries(settings.rateLimit)) {
config.rateLimiters[serviceName] = new RateLimitedQueue(interval)
}
}
// Build tree for each query (use config as-is for service compilation)
const queryTree = {}
for (const [queryName, descriptor] of Object.entries(queries)) {
queryTree[queryName] = compileDescriptor(queryName, descriptor, config)
}
// Compile global settings separately
const globalSettings = {...settings}
if (settings.onError) {
globalSettings.onError = compileServiceOrChain('global', settings.onError, config)
}
return {
queries: queryTree,
given,
services,
debug,
settings: globalSettings
}
}
export default compile