This repository was archived by the owner on Sep 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathLoader.js
More file actions
109 lines (92 loc) · 2.97 KB
/
Loader.js
File metadata and controls
109 lines (92 loc) · 2.97 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
import { resolve, parse } from 'url'
const methods = {}
const __meta__ = {
extensions: [ 'json' ],
parsable: true,
format: 'internal'
}
/**
* @class InternalLoader
* @description The loader associated with internal model dumps.
* It holds all the necessary methods used to load a file representing a dump of the intermediate
* model.
*/
export class InternalLoader {
static extensions = __meta__.extensions
static parsable = __meta__.parsable
static format = __meta__.format
/**
* Resolves a URI and fixes it if necessary.
* @param {Object} namedParams - an object holding the named parameters used for the resolution of
* the URI.
* @param {Object} namedParams.options - an object holding all the settings necessary for
* resolving, loading, parsing and serializing a uri and its dependencies.
* @param {string} uri - the URI to resolve to a file that will be used as the primary file for
* this loader
* @returns {Promise} a Promise containing the `options` and normalized `item` in an object. See
* `methods.fixPrimary` for more information.
* @static
*/
static load({ options, uri }) {
return methods.load({ options, uri })
}
/**
* Tests whether the content of a file is parsable by this loader and associated parser. This is
* used to tell which loader/parser combo should be used.
* @param {string?} content - the content of the file to test
* @returns {boolean} whether it is parsable or not
* @static
*/
static isParsable({ content }) {
return methods.isParsable(content)
}
}
methods.isParsable = (content) => {
const parsed = methods.parseJSON(content)
return !!parsed && parsed._model
}
/**
* converts a string written in JSON or YAML format into an object
* @param {string} str: the string to parse
* @returns {Object?} the converted object, or null if str was not a JSON or YAML string
*/
methods.parseJSON = (str) => {
let parsed = null
try {
parsed = JSON.parse(str)
}
catch (jsonParseError) {
return null
}
return parsed
}
methods.resolve = (options, uri, { $ref = '' } = {}) => {
const uriToLoad = resolve(uri, $ref)
const protocol = parse(uriToLoad).protocol
if (protocol && protocol.substr(0, 4) === 'http') {
return options.httpResolver.resolve(uriToLoad.split('#')[0])
}
return options.fsResolver.resolve(uriToLoad.split('#')[0])
}
methods.fixPrimary = (options, { content }) => {
const internal = methods.parseJSON(content)
if (!internal) {
return Promise.reject(new Error('could not parse internal file (not a JSON)'))
}
return { options, item: internal }
}
methods.handleRejection = (error) => {
return Promise.reject(error)
}
methods.load = ({ options, uri }) => {
const primaryPromise = methods.resolve(options, uri)
return primaryPromise
.then(
(primary) => {
return methods.fixPrimary(options, { uri, content: primary })
},
methods.handleRejection
)
}
export const __internals__ = methods
export default InternalLoader