UNPKG

5.78 kBJavaScriptView Raw
1
2
3/* eslint-disable global-require */
4
5const factory = require('@graphy-dev/core.data.factory');
6
7const memoize = (s_package) => {
8 delete graphy[s_package];
9 return graphy[s_package] = require('@graphy-dev/'+s_package);
10};
11
12
13
14// ttl package
15const ttl = {
16 // read ttl
17 get read() {
18 // memoize
19 delete graphy.content.ttl.read;
20 return (graphy.content.ttl.read = require('@graphy-dev/content.ttl.read'));
21 },
22
23 // write ttl
24 get write() {
25 // memoize
26 delete graphy.content.ttl.write;
27 return (graphy.content.ttl.write = require('@graphy-dev/content.ttl.write'));
28 },
29 // scribe ttl
30 get scribe() {
31 // memoize
32 delete graphy.content.ttl.scribe;
33 return (graphy.content.ttl.scribe = require('@graphy-dev/content.ttl.scribe'));
34 },
35};
36// trig package
37const trig = {
38 // read trig
39 get read() {
40 // memoize
41 delete graphy.content.trig.read;
42 return (graphy.content.trig.read = require('@graphy-dev/content.trig.read'));
43 },
44
45 // write trig
46 get write() {
47 // memoize
48 delete graphy.content.trig.write;
49 return (graphy.content.trig.write = require('@graphy-dev/content.trig.write'));
50 },
51 // scribe trig
52 get scribe() {
53 // memoize
54 delete graphy.content.trig.scribe;
55 return (graphy.content.trig.scribe = require('@graphy-dev/content.trig.scribe'));
56 },
57};
58// nt package
59const nt = {
60 // read nt
61 get read() {
62 // memoize
63 delete graphy.content.nt.read;
64 return (graphy.content.nt.read = require('@graphy-dev/content.nt.read'));
65 },
66
67 // write nt
68 get write() {
69 // memoize
70 delete graphy.content.nt.write;
71 return (graphy.content.nt.write = require('@graphy-dev/content.nt.write'));
72 },
73 // scribe nt
74 get scribe() {
75 // memoize
76 delete graphy.content.nt.scribe;
77 return (graphy.content.nt.scribe = require('@graphy-dev/content.nt.scribe'));
78 },
79};
80// nq package
81const nq = {
82 // read nq
83 get read() {
84 // memoize
85 delete graphy.content.nq.read;
86 return (graphy.content.nq.read = require('@graphy-dev/content.nq.read'));
87 },
88
89 // write nq
90 get write() {
91 // memoize
92 delete graphy.content.nq.write;
93 return (graphy.content.nq.write = require('@graphy-dev/content.nq.write'));
94 },
95 // scribe nq
96 get scribe() {
97 // memoize
98 delete graphy.content.nq.scribe;
99 return (graphy.content.nq.scribe = require('@graphy-dev/content.nq.scribe'));
100 },
101};
102
103
104// // SPARQL Results package
105// const sparql_results = {
106// // deserialize sparql_results input
107// get deserializer() {
108// // memoize
109// delete sparql_results.deserializer;
110// return (sparql_results.deserializer = require('../sparql-results/deserializer.js'));
111// },
112// };
113
114
115const H_CONTENT_MIMES = {
116 'text/turtle': ttl,
117 'application/trig': trig,
118 'application/n-triples': nt,
119 'application/n-quads': nq,
120// 'application/sparql-results+json': sparql_results,
121};
122
123const H_CONTENT_TAGS = {
124 nt,
125 'n-triples': nt,
126 ntriples: nt,
127 nq,
128 'n-quads': nq,
129 nquads: nq,
130 ttl,
131 turtle: ttl,
132 trig,
133// 'application/sparql-results+json': sparql_results,
134};
135
136
137
138const R_CONTENT_TYPE = /^((?:application|text)\/[^\0-\x20()<>@,;:\\"\/[\]?.=]+)(;.+)*$/i;
139
140const graphy = module.exports = Object.assign({
141
142 VERSION: '4.0.0',
143
144 content: Object.assign((s_query) => {
145 if(s_query.toLowerCase() in H_CONTENT_TAGS) {
146 return H_CONTENT_TAGS[s_query.toLowerCase()];
147 }
148
149 let m_content_type = R_CONTENT_TYPE.exec(s_query);
150 if(!m_content_type) throw new Error(`invalid content-type string: "${s_query}"`);
151 let [, s_content_type, s_parameters] = m_content_type;
152 let s_content_type_normal = s_content_type.toLowerCase();
153
154 if(s_content_type_normal in H_CONTENT_MIMES) {
155 return H_CONTENT_MIMES[s_content_type_normal];
156 }
157 else {
158 throw new Error(`no content handlers matched query for "${s_content_type_normal}"`);
159 }
160 }, {
161 ttl,
162 trig,
163 nt,
164 nq,
165
166 }),
167
168 core: {
169 data: {
170 get factory() {
171 // memoize
172 delete graphy.core.data.factory;
173 return (graphy.core.data.factory = require('@graphy-dev/core.data.factory'));
174 },
175 },
176 iso: {
177 get stream() {
178 // memoize
179 delete graphy.core.iso.stream;
180 return (graphy.core.iso.stream = require('@graphy-dev/core.iso.stream'));
181 },
182 },
183 },
184
185 get 'core.data.factory'() {
186 return memoize('core.data.factory');
187 },
188
189 get 'core.iso.stream'() {
190 return memoize('core.iso.stream');
191 },
192
193 util: {
194 dataset: {
195 get tree() {
196 // memoize
197 delete graphy.util.dataset.tree;
198 return (graphy.util.dataset.tree = require('@graphy-dev/util.dataset.tree'));
199 },
200 },
201 },
202
203 get 'util.dataset.tree'() {
204 return memoize('util.dataset.tree');
205 },
206
207 memory: {
208 dataset: {
209 get fast() {
210 // memoize
211 delete graphy.memory.dataset.fast;
212 return (graphy.memory.dataset.fast = require('@graphy-dev/memory.dataset.fast'));
213 },
214 },
215 },
216
217 get 'memory.dataset.fast'() {
218 return memoize('memory.dataset.fast');
219 },
220
221
222
223 get 'content.ttl.read'() {
224 return memoize('content.ttl.read');
225 },
226
227 get 'content.ttl.write'() {
228 return memoize('content.ttl.write');
229 },
230
231 get 'content.ttl.scribe'() {
232 return memoize('content.ttl.scribe');
233 },
234
235 get 'content.trig.read'() {
236 return memoize('content.trig.read');
237 },
238
239 get 'content.trig.write'() {
240 return memoize('content.trig.write');
241 },
242
243 get 'content.trig.scribe'() {
244 return memoize('content.trig.scribe');
245 },
246
247 get 'content.nt.read'() {
248 return memoize('content.nt.read');
249 },
250
251 get 'content.nt.write'() {
252 return memoize('content.nt.write');
253 },
254
255 get 'content.nt.scribe'() {
256 return memoize('content.nt.scribe');
257 },
258
259 get 'content.nq.read'() {
260 return memoize('content.nq.read');
261 },
262
263 get 'content.nq.write'() {
264 return memoize('content.nq.write');
265 },
266
267 get 'content.nq.scribe'() {
268 return memoize('content.nq.scribe');
269 },
270
271
272
273}, factory);
274
275
276// export graphy to window object if in main thread of browser
277if('undefined' !== typeof window) window.graphy = graphy;