This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
raml.coffee
344 lines (286 loc) · 10.3 KB
/
raml.coffee
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
@errors = require './errors'
@loader = require './loader'
nodes = require './nodes'
path = require 'path'
refParser = require 'json-schema-ref-parser'
util = require './util'
class @FileError extends @errors.MarkedYAMLError
class @FileReader
constructor: (readFileAsyncOverride) ->
@q = require 'q'
@url = require 'url'
if readFileAsyncOverride
@readFileAsyncOverride = readFileAsyncOverride
###
Read file either locally or from the network.
###
readFileAsync: (file) ->
if @readFileAsyncOverride
return @readFileAsyncOverride(file)
if (/^https?/i).test(file) or window?
return @fetchFileAsync file
return @fetchLocalFileAsync file
###
Read file from the disk.
###
fetchLocalFileAsync: (file) ->
deferred = @q.defer()
require('fs').readFile file, (err, data) =>
if (err)
deferred.reject(new exports.FileError "while reading #{file}", null, "cannot read #{file} (#{err})", @start_mark)
else
deferred.resolve(data.toString())
return deferred.promise
###
Read file from the network.
###
fetchFileAsync: (file) ->
if window?
return @fetchFileAsyncBrowser file
@fetchFileAsyncNode file
fetchFileAsyncNode: (file) ->
deferred = @q.defer()
require('got').get(file, {
headers: {
'Accept': 'application/raml+yaml, */*'
}
}, (err, data, res) ->
if err
return deferred.reject(new exports.FileError "while fetching #{file}", null, "cannot fetch #{file} (#{err.message})", @start_mark)
if res.statusCode == 200 or res.statusCode == 304
return deferred.resolve(data)
return deferred.reject(new exports.FileError "while fetching #{file}", null, "cannot fetch #{file} (#{res.statusCode})", @start_mark)
)
deferred.promise
fetchFileAsyncBrowser: (file) ->
deferred = @q.defer()
xhr = new XMLHttpRequest()
try
xhr.open 'GET', file
catch error
throw new exports.FileError "while fetching #{file}", null, "cannot fetch #{file} (#{error}), check that the server is up and that CORS is enabled", @start_mark
xhr.setRequestHeader 'Accept', 'application/raml+yaml, */*'
xhr.onreadystatechange = () =>
if (xhr.readyState == 4)
if (xhr.status == 200 or xhr.status == 304)
deferred.resolve(xhr.responseText)
else
deferred.reject(new exports.FileError "while fetching #{file}", null, "cannot fetch #{file} (#{xhr.status})", @start_mark)
xhr.send()
deferred.promise
###
OO version of the parser, static functions will be removed after consumers move on to use the OO version
OO will offer caching
###
class @RamlParser
constructor: (@settings = defaultSettings) ->
@q = require 'q'
@url = require 'url'
@nodes= require './nodes'
@loadDefaultSettings(@settings)
loadDefaultSettings: (settings) ->
Object.keys(defaultSettings).forEach (settingName) ->
unless settingName of settings
settings[settingName] = defaultSettings[settingName]
loadFile: (file, settings = @settings) ->
try
return settings.reader.readFileAsync(file)
.then (stream) =>
@load stream, file, settings
catch error
# return a promise that throws the error
return @q.fcall ->
throw new exports.FileError "while fetching #{file}", null, "cannot fetch #{file} (#{error})", null
composeFile: (file, settings = @settings, parent) ->
try
return settings.reader.readFileAsync(file)
.then (stream) =>
@compose stream, file, settings, parent
catch error
# return a promise that throws the error
return @q.fcall ->
throw new exports.FileError "while fetching #{file}", null, "cannot fetch #{file} (#{error})", null
compose: (stream, location, settings = @settings, parent = { src: location}) ->
settings.compose = false
@parseStream(stream, location, settings, parent)
load: (stream, location, settings = @settings) ->
settings.compose = true
@parseStream(stream, location, settings, { src: location})
parseStream: (stream, location, settings = @settings, parent) ->
loader = new exports.loader.Loader stream, location, settings, parent
return @q.fcall ->
return loader.getYamlRoot()
.then (partialTree) =>
files = loader.getPendingFilesList()
@getPendingFiles(loader, partialTree, files)
.then (fullyAssembledTree) =>
if settings.compose && settings.dereferenceSchemas
@dereferenceSchemas(loader, fullyAssembledTree, settings.dereferenceSchemas.maxDepth)
else
return fullyAssembledTree
.then (fullyAssembledTree) ->
loader.composeRamlTree(fullyAssembledTree, settings)
if settings.compose
if fullyAssembledTree?
return loader.construct_document(fullyAssembledTree)
else
return null
else
return fullyAssembledTree
dereferenceSchemas: (loader, node, maxDepth=500) =>
schemaNodes = []
definedSchemaNames = []
# Collect defined schemas
node.value.forEach(([childName, childBody]) ->
if childName.value == 'schemas' && childBody instanceof nodes.SequenceNode
childBody.value.forEach((mappingNode) ->
mappingNode.value.forEach(([schemaName, schemaBody]) ->
definedSchemaNames.push(schemaName.value)
schemaNodes.push(schemaBody)
)
)
)
# Collect inline-defined schemas in tree
collectNodes = ((node) ->
items = []
if node instanceof nodes.MappingNode
node.value.forEach(([kind, item]) ->
if item instanceof nodes.FileNode
# schema inline inclusions
schemaNodes.push(item)
else if item instanceof nodes.ScalarNode
if kind.value == 'schema' && definedSchemaNames.indexOf(item.value) == -1
# schema inline definitions
schemaNodes.push(item)
else
collectNodes(item)
)
else if (node instanceof nodes.SequenceNode)
node.value.map collectNodes
)
collectNodes(node)
# Dereference schema
resolveSchemas = schemaNodes.map((schemaNode) =>
if schemaNode instanceof nodes.FileNode
# Included schemas
uri = schemaNode.inclusionPath
schema = undefined
else
# Inline-defined schemas
uri = loader.src
schema = JSON.parse(schemaNode.value)
@dereferenceSchema(uri, schema, maxDepth)
)
return @q.all(resolveSchemas)
.then((dereferedSchemas) ->
dereferedSchemas.forEach((dereferedSchema, index) ->
schemaNodes[index].value = dereferedSchema
)
return node
)
dereferenceSchema: (uri, schema, maxDepth) =>
deferred = @q.defer()
refParser.dereference(uri, schema, {})
.then((dereferencedSchema) ->
deferred.resolve(util.stringifyJSONCircularDepth(dereferencedSchema, maxDepth))
)
.catch((err) ->
deferred.reject(err)
)
return deferred.promise
getPendingFiles: (loader, node, files) ->
return node if !files.length
loadFiles = files.map((fileInfo) =>
@getPendingFile(loader, fileInfo)
)
return @q.all(loadFiles)
.then((results) =>
lastVisitedNode = undefined
results.forEach((result, index) =>
overwritingnode = @mergePendingFile(files[index], result)
# we should be able to handle parentless children, but only the last one
if overwritingnode and !lastVisitedNode
lastVisitedNode = overwritingnode
)
return if lastVisitedNode then lastVisitedNode else node
)
getPendingFile: (loader, fileInfo) ->
event = fileInfo.event
fileUri = fileInfo.targetFileUri
if loader.parent and @isInIncludeTagsStack(fileUri, loader)
return @q.reject(new exports.FileError 'while composing scalar out of !include', null, "detected circular !include of #{event.value}", event.start_mark)
return @settings.reader.readFileAsync(fileUri)
.then((fileData) =>
if fileInfo.type is 'fragment'
@compose(fileData, fileInfo.targetFileUri, { validate: false, transform: false, compose: true }, loader)
else
new @nodes.FileNode('tag:yaml.org,2002:str', fileData, event.start_mark, event.end_mark, event.style, fileInfo.targetFileUri)
)
.catch((error) =>
@addContextToError(error, event)
)
mergePendingFile: (fileInfo, value) ->
node = fileInfo.parentNode
key = fileInfo.parentKey
return @appendNewNodeToParent(node, key, value)
addContextToError: (error, event) ->
if error.constructor.name == "FileError"
unless error.problem_mark
error.problem_mark = event.start_mark
throw error
else
throw new exports.FileError 'while reading file', null, error, event.start_mark
# detect
isInIncludeTagsStack: (include, parent) ->
while parent = parent.parent
if parent.src is include
return true
return false
appendNewNodeToParent: (node, key, value) ->
if node
if util.isSequence(node)
node.value[key] = value
else
node.value.push [key, value]
return null
else
return value
###
validate controls whether the stream must be processed as a
###
defaultSettings = {
validate: true,
transform: true,
compose: true,
reader: new exports.FileReader(null),
applySchemas: true,
dereferenceSchemas: false
}
###
Parse the first RAML document in a stream and produce the corresponding
Javascript object.
###
@loadFile = (file, settings = defaultSettings) ->
parser = new exports.RamlParser(settings)
parser.loadFile(file, settings)
###
Parse the first RAML document in a file and produce the corresponding
representation tree.
###
@composeFile = (file, settings = defaultSettings, parent = file) ->
parser = new exports.RamlParser(settings)
parser.composeFile(file, settings, parent)
###
Parse the first RAML document in a stream and produce the corresponding
representation tree.
###
@compose = (stream, location, settings = defaultSettings, parent = location) ->
parser = new exports.RamlParser(settings)
parser.compose(stream, location, settings, parent)
###
Parse the first RAML document in a stream and produce the corresponding
Javascript object.
###
@load = (stream, location, settings = defaultSettings) ->
parser = new exports.RamlParser(settings)
parser.load(stream, location, settings, null)