This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsolid.js
2878 lines (2676 loc) · 89.9 KB
/
solid.js
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
'use strict'
/**
* Provides a simple configuration object for Solid web client and other
* modules.
* @module config
*/
module.exports = {
/**
* Default authentication endpoint
*/
authEndpoint: 'https://databox.me/',
/**
* Default RDF parser library
*/
parser: 'rdflib',
/**
* Default proxy URL for servicing CORS requests
*/
proxyUrl: 'https://databox.me/,proxy?uri={uri}',
/**
* Default signup endpoints (list of identity providers)
*/
signupEndpoint: 'https://solid.github.io/solid-idps/',
/**
* Default height of the Signup popup window, in pixels
*/
signupWindowHeight: 600,
/**
* Default width of the Signup popup window, in pixels
*/
signupWindowWidth: 1024,
/**
* Timeout for web/ajax operations, in milliseconds
*/
timeout: 50000
}
},{}],2:[function(require,module,exports){
/*
The MIT License (MIT)
Copyright (c) 2015 Solid
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Solid.js is a Javascript library for Solid applications. This library currently
depends on rdflib.js. Please make sure to load the rdflib.js script before
loading solid.js.
If you would like to know more about the solid Solid project, please see
https://github.com/solid/
*/
'use strict'
/**
* Provides Solid methods for WebID authentication and signup
* @module auth
*/
module.exports.currentUser = currentUser
module.exports.listen = listen
module.exports.login = login
module.exports.signup = signup
var webClient = require('./web')
/**
* Returns the WebID of the current user (by doing a login()/HEAD request to
* the current page). Convenience method, useful for standalone apps that aren't
* wrapping any resource.
* @method currentUser
* @return {String} WebID of the current user or `null` if none detected
*/
function currentUser () {
if (typeof window === 'undefined') {
return null // only works in the browser
}
var currentPageUrl = window.location.href
return login(currentPageUrl)
.catch(function (reason) {
// console.log('Detecting current user failed: %o', reason)
return null
})
}
/**
* Sets up an event listener to monitor login messages from child window/iframe
* @method listen
* @static
* @return {Promise<String>} Event listener promise, resolves to user's WebID
*/
function listen () {
var promise = new Promise(function (resolve, reject) {
var eventMethod = window.addEventListener
? 'addEventListener'
: 'attachEvent'
var eventListener = window[eventMethod]
var messageEvent = eventMethod === 'attachEvent'
? 'onmessage'
: 'message'
eventListener(messageEvent, function (e) {
var u = e.data
if (u.slice(0, 5) === 'User:') {
var user = u.slice(5, u.length)
if (user && user.length > 0 && user.slice(0, 4) === 'http') {
return resolve(user)
} else {
return reject(user)
}
}
}, true)
})
return promise
}
/**
* Performs a Solid login() via an XHR HEAD operation.
* (Attempts to find the current user's WebID from the User header, if
* already authenticated.)
* @method login
* @static
* @param [url] {String} Location of a Solid server or container at which the
* user might be authenticated.
* Defaults to: current page location
* @param [alternateAuthUrl] {String} URL of an alternate/default auth endpoint.
* Defaults to `config.authEndpoint`
* @return {Promise<String>} XHR HEAD operation promise, resolves to user's WebID
*/
function login (url, alternateAuthUrl) {
var defaultAuthEndpoint = require('../config').authEndpoint
url = url || window.location.origin + window.location.pathname
alternateAuthUrl = alternateAuthUrl || defaultAuthEndpoint
// First, see if user is already logged in (do a quick HEAD request)
return webClient.head(url)
.then(function (solidResponse) {
if (solidResponse.isLoggedIn()) {
return solidResponse.user
} else {
// If not logged in, try logging in at an alternate endpoint
return webClient.head(alternateAuthUrl)
.then(function (solidResponse) {
// Will return an empty string is this login also fails
return solidResponse.user
})
}
})
}
/**
* Opens a signup popup window, sets up `listen()`.
* @method signup
* @static
* @param signupUrl {String} Location of a Solid server for user signup.
* @return {Promise<String>} Returns a listener promise, resolves with signed
* up user's WebID.
*/
function signup (signupUrl) {
var config = require('../config')
signupUrl = signupUrl || config.signupEndpoint
var width = config.signupWindowWidth
var height = config.signupWindowHeight
// set borders
var leftPosition = (window.screen.width / 2) - ((width / 2) + 10)
// set title and status bars
var topPosition = (window.screen.height / 2) - ((height / 2) + 50)
var windowTitle = 'Solid signup'
var windowUrl = signupUrl + '?origin=' +
encodeURIComponent(window.location.origin)
var windowSpecs = 'resizable,scrollbars,status,width=' + width + ',height=' +
height + ',left=' + leftPosition + ',top=' + topPosition
window.open(windowUrl, windowTitle, windowSpecs)
return new Promise(function (resolve, reject) {
listen().then(function (webid) {
return resolve(webid)
}).catch(function (err) {
return reject(err)
})
})
}
},{"../config":1,"./web":19}],3:[function(require,module,exports){
'use strict'
/**
* Provides Solid helper functions involved with parsing a user's WebId profile.
* @module identity
*/
module.exports.discoverWebID = discoverWebID
module.exports.getProfile = getProfile
module.exports.loadExtendedProfile = loadExtendedProfile
var graphUtil = require('./util/graph-util')
var webUtil = require('./util/web-util')
var webClient = require('./web')
var SolidProfile = require('./solid/profile')
var vocab = require('./vocab')
/**
* Discovers a user's WebId (URL) starting from the account/domain URL
* @method discoverWebID
* @param url {String} Location of a user's account or domain.
* @throw {Error} Reason why the WebID could not be discovered
* @return {Promise<uri>}
*/
function discoverWebID (url) {
return new Promise(function (resolve, reject) {
webClient.options(url)
.then(function (meta) {
var metaUrl = meta.meta
if (metaUrl) {
metaUrl = webUtil.absoluteUrl(url, metaUrl)
webClient.getParsedGraph(metaUrl)
.then(function (graph) {
var webid = graph.any(undefined, vocab.solid('account'))
if (webid && webid.uri) {
resolve(webid.uri)
} else {
throw new Error('Could not find a WebID matching the domain ' +
url)
}
})
.catch(function (err) {
reject(err)
})
} else {
throw new Error('Could not find a meta URL in the Link header')
}
})
.catch(function (err) {
reject(err)
})
})
}
/**
* Fetches a user's WebId profile, optionally follows `sameAs` etc links,
* and return a promise with a parsed SolidProfile instance.
* @method getProfile
* @param profileUrl {String} WebId or Location of a user's profile.
* @param [options] Options hashmap (see solid.web.solidRequest() function docs)
* @param [options.ignoreExtended=false] Do not load extended profile if true.
* @return {Promise<SolidProfile>}
*/
function getProfile (profileUrl, options) {
options = options || {}
// Politely ask for Turtle formatted profiles
options.headers = options.headers || {
'Accept': 'text/turtle'
}
options.noCredentials = true // profiles are always public
// Load main profile
return webClient.get(profileUrl, options)
.then(function (response) {
var contentType = response.contentType()
if (!contentType) {
throw new Error('Cannot parse profile without a Content-Type: header')
}
var parsedProfile = graphUtil.parseGraph(profileUrl, response.raw(),
contentType)
var profile = new SolidProfile(profileUrl, parsedProfile, response)
profile.isLoaded = true
if (options.ignoreExtended) {
return profile
} else {
return loadExtendedProfile(profile, options)
}
})
}
/**
* Loads the related external profile resources (all the `sameAs` and `seeAlso`
* links, as well as Preferences), and appends them to the profile's
* `parsedGraph`. Returns the profile instance.
* @method loadExtendedProfile
* @private
* @param profile {SolidProfile}
* @param [options] Options hashmap (see solid.web.solidRequest() function docs)
* @return {Promise<SolidProfile>}
*/
function loadExtendedProfile (profile, options) {
var links = profile.relatedProfilesLinks()
return webClient.loadParsedGraphs(links, options)
.then(function (loadedGraphs) {
loadedGraphs.forEach(function (graph) {
if (graph && graph.value) {
profile.appendFromGraph(graph.value, graph.uri)
}
})
return profile
})
}
},{"./solid/profile":7,"./util/graph-util":12,"./util/web-util":16,"./vocab":18,"./web":19}],4:[function(require,module,exports){
'use strict'
/**
* Provides miscelaneous meta functions (such as library version)
* @module meta
*/
var lib = require('../package')
/**
* Returns Solid.js library version (read from `package.json`)
* @return {String} Lib version
*/
module.exports.version = function version () {
return lib.version
}
},{"../package":26}],5:[function(require,module,exports){
'use strict'
/**
* @module container
*/
module.exports = SolidContainer
var rdf = require('../util/rdf-parser').rdflib
var graphUtil = require('../util/graph-util')
var parseLinks = graphUtil.parseLinks
var vocab = require('../vocab')
var SolidResource = require('./resource')
/**
* @class SolidContainer
* @extends SolidResource
* @constructor
* @param uri {String}
* @param response {SolidResponse}
*/
function SolidContainer (uri, response) {
// Call parent constructor
SolidResource.call(this, uri, response)
/**
* Hashmap of Containers within this container, keyed by absolute uri
* @property containers
* @type Object
*/
this.containers = {}
/**
* List of URIs of all contents (containers and resources)
* @property contentsUris
* @type Array<String>
*/
this.contentsUris = []
/**
* Hashmap of Contents that are just resources (not containers),
* keyed by absolute uri
* @property resources
* @type Object
*/
this.resources = {}
if (response) {
this.initFromResponse(this.uri, response)
}
}
// SolidContainer.prototype object inherits from SolidResource.prototype
SolidContainer.prototype = Object.create(SolidResource.prototype)
SolidContainer.prototype.constructor = SolidContainer
/**
* Extracts the contents (resources and sub-containers)
* of the given graph and adds them to this container
* @method appendFromGraph
* @param parsedGraph {Graph}
* @param graphUri {String}
*/
SolidContainer.prototype.appendFromGraph =
function appendFromGraph (parsedGraph, graphUri) {
// Set this container's types
this.types = Object.keys(parsedGraph.findTypeURIs(rdf.sym(this.uri)))
// Extract all the contents links (resources and containers)
var contentsUris = parseLinks(parsedGraph, null, vocab.ldp('contains'))
this.contentsUris = this.contentsUris.concat(contentsUris.sort())
// Extract links that are just containers
var containersLinks = parsedGraph.each(null, null, vocab.ldp('Container'))
var self = this
var container
containersLinks.forEach(function (containerLink) {
// Filter out . (the link to this directory)
if (containerLink.uri !== self.uri) {
container = new SolidContainer(containerLink.uri)
container.types = Object.keys(parsedGraph.findTypeURIs(containerLink))
self.containers[container.uri] = container
}
})
// Now that containers are defined, all the rest are non-container resources
var isResource
var isContainer
var resource
contentsUris.forEach(function (link) {
isContainer = link in self.containers
isResource = link !== self.uri && !isContainer
if (isResource) {
resource = new SolidResource(link)
resource.types = Object.keys(parsedGraph.findTypeURIs(rdf.sym(link)))
self.resources[link] = resource
}
})
}
/**
* Returns a list of SolidResource or SolidContainer instances that match
* a given type.
* @method findByType
* @param rdfClass {String}
* @return {Array<SolidResource|SolidContainer>}
*/
SolidContainer.prototype.findByType = function findByType (rdfClass) {
var matches = []
var key
var container
for (key in this.containers) {
container = this.containers[key]
if (container.isType(rdfClass)) {
matches.push(container)
}
}
var resource
for (key in this.resources) {
resource = this.resources[key]
if (resource.isType(rdfClass)) {
matches.push(resource)
}
}
return matches
}
/**
* @method initFromResponse
* @param uri {String}
* @param response {SolidResponse}
*/
SolidContainer.prototype.initFromResponse =
function initFromResponse (uri, response) {
var contentType = response.contentType()
if (!contentType) {
throw new Error('Cannot parse container without a Content-Type: header')
}
var parsedGraph = graphUtil.parseGraph(uri, response.raw(),
contentType)
this.parsedGraph = parsedGraph
this.appendFromGraph(parsedGraph, uri)
}
/**
* Is this a Container instance (vs a regular resource).
* @return {Boolean}
*/
SolidResource.prototype.isContainer = function isContainer () {
return true
}
/**
* Returns true if there are no resources or containers inside this container.
* @method isEmpty
* @return {Boolean}
*/
SolidContainer.prototype.isEmpty = function isEmpty () {
return this.contentsUris.length === 0
}
},{"../util/graph-util":12,"../util/rdf-parser":13,"../vocab":18,"./resource":8}],6:[function(require,module,exports){
'use strict'
/**
* @module index-registration
*/
module.exports = IndexRegistration
/**
* Represents a Solid Index registration (an entry in the Type Index Registry).
* Returned in a list by `profile.typeRegistryForClass()`
* @class IndexRegistration
* @constructor
* @param registrationUri {String} Absolute URI (with fragment identifier) of
* the registration (its location in the type index)
* @param rdfClass {rdf.NamedNode} RDF Class for this registration
* @param locationType {String} One of 'instance' or 'container'
* @param locationUri {String} URI of the location containing resources of this
* type
* @param isListed {Boolean} Is this registration in a listed or unlisted index
*/
function IndexRegistration (registrationUri, rdfClass, locationType,
locationUri, isListed) {
/**
* Is this a listed or unlisted registration
* @property isListed
* @type Boolean
*/
this.isListed = isListed
/**
* Location type, one of 'instance' or 'container'
* @property locationType
* @type String
*/
this.locationType = locationType
/**
* URI of the solid instance or container that holds resources of this type
* @property locationUri
* @type String
*/
this.locationUri = locationUri
/**
* RDF Class for this registration
* @property rdfClass
* @type rdf.NamedNode
*/
this.rdfClass = rdfClass
/**
* Absolute URI (with fragment identifier) of the registration
* @property registrationUri
* @type String
*/
this.registrationUri = registrationUri
}
/**
* Convenience method, returns true if this registration is of type
* `solid:instanceContainer`
* @method isContainer
* @return {Boolean}
*/
IndexRegistration.prototype.isContainer = function isInstance () {
return this.locationType === 'container'
}
/**
* Convenience method, returns true if this registration is of type
* `solid:instance`
* @method isInstance
* @return {Boolean}
*/
IndexRegistration.prototype.isInstance = function isInstance () {
return this.locationType === 'instance'
}
},{}],7:[function(require,module,exports){
'use strict'
/**
* @module profile
*/
module.exports = SolidProfile
var rdf = require('../util/rdf-parser').rdflib
var vocab = require('../vocab')
var typeRegistry = require('../type-registry')
var graphUtil = require('../util/graph-util')
var parseLinks = graphUtil.parseLinks
/**
* Provides convenience methods for a WebID Profile.
* Used by `identity.getProfile()`
* @class SolidProfile
* @constructor
*/
function SolidProfile (profileUrl, parsedProfile, response) {
/**
* Main Inbox resource for this profile (link and parsed graph)
* @property inbox
* @type Object
*/
this.inbox = {
uri: null,
graph: null
}
/**
* Has this profile been loaded? (Set in `identity.getProfile()`)
* @property isLoaded
* @type Boolean
*/
this.isLoaded = false
/**
* Links to root storage containers (read/write dataspaces for this profile)
* @property storage
* @type Array<String>
*/
this.storage = []
/**
* Listed (public) Type registry index (link and parsed graph)
* @property typeIndexListed
* @type Object
*/
this.typeIndexListed = {
uri: null,
graph: null
}
/**
* Unlisted (private) Type registry index (link and parsed graph)
* @property typeIndexUnlisted
* @type rdf.Object
*/
this.typeIndexUnlisted = {
uri: null,
graph: null
}
/**
* Parsed graph of the extended WebID Profile document.
* Included the WebID profile, preferences, and related profile graphs
* @property parsedGraph
* @type rdf.Graph
*/
this.parsedGraph = null
/**
* Profile preferences object (link and parsed graph).
* Is considered a part of the 'Extended Profile'
* @property preferences
* @type Object
*/
this.preferences = {
uri: null,
graph: null
}
/**
* SolidResponse instance from which this profile object was created.
* Contains the raw profile source, the XHR object, etc.
* @property response
* @type SolidResponse
*/
this.response = response
/**
* Links to "see also" profile documents. Typically loaded immediately
* after retrieving the initial WebID Profile document.
* @property relatedProfiles
* @type Object
*/
this.relatedProfiles = {
sameAs: [],
seeAlso: []
}
/**
* WebId URL (the `foaf:primaryTopic` of the profile document)
* @property webId
* @type String
*/
this.webId = null
if (!profileUrl) {
return
}
/**
* Location of the base WebID Profile document (minus the hash fragment).
* @property baseProfileUrl
* @type String
*/
this.baseProfileUrl = (profileUrl.indexOf('#') >= 0)
? profileUrl.slice(0, profileUrl.indexOf('#'))
: profileUrl
if (parsedProfile) {
this.initWebId(parsedProfile)
this.appendFromGraph(parsedProfile, this.baseProfileUrl)
}
}
/**
* Update the profile based on a parsed graph, which can be either the
* initial WebID profile, or the various extended profile graphs
* (such as the seeAlso, sameAs and preferences links)
* @method appendFromGraph
* @param parsedProfile {rdf.IndexedFormula} RDFLib-parsed user profile
* @param profileUrl {String} URL of this particular parsed graph
*/
SolidProfile.prototype.appendFromGraph =
function appendFromGraph (parsedProfile, profileUrl) {
if (!parsedProfile) {
return
}
this.parsedGraph = this.parsedGraph || rdf.graph() // initialize if null
// Add the graph of this parsedProfile to the existing graph
graphUtil.appendGraph(this.parsedGraph, parsedProfile, profileUrl)
var webId = rdf.sym(this.webId)
var links
// Add sameAs and seeAlso
links = parseLinks(parsedProfile, null, vocab.owl('sameAs'))
this.relatedProfiles.sameAs = this.relatedProfiles.sameAs.concat(links)
links = parseLinks(parsedProfile, null, vocab.rdfs('seeAlso'))
this.relatedProfiles.seeAlso = this.relatedProfiles.seeAlso.concat(links)
// Add preferencesFile link (singular). Note that preferencesFile has
// Write-Once semantics -- it's initialized from public profile, but
// cannot be overwritten by related profiles
if (!this.preferences.uri) {
this.preferences.uri = parseLink(parsedProfile, webId,
vocab.pim('preferencesFile'))
}
// Init inbox (singular). Note that inbox has
// Write-Once semantics -- it's initialized from public profile, but
// cannot be overwritten by related profiles
if (!this.inbox.uri) {
this.inbox.uri = parseLink(parsedProfile, webId,
vocab.solid('inbox'))
}
// Add storage
links = parseLinks(parsedProfile, webId, vocab.pim('storage'))
this.storage =
this.storage.concat(links)
// Add links to Listed and Unlisted Type Indexes.
// Note: these are just the links.
// The actual index files will be loaded and parsed
// in `profile.loadTypeRegistry()`)
if (!this.typeIndexListed.uri) {
this.typeIndexListed.uri = parseLink(parsedProfile, webId,
vocab.solid('publicTypeIndex'))
}
if (!this.typeIndexUnlisted.uri) {
this.typeIndexUnlisted.uri = parseLink(parsedProfile, webId,
vocab.solid('privateTypeIndex'))
}
}
/**
* Extracts the WebID from a parsed profile graph and initializes it.
* Should only be done once (when creating a new SolidProfile instance)
* @method initWebId
* @param parsedProfile {rdf.IndexedFormula} RDFLib-parsed user profile
*/
SolidProfile.prototype.initWebId = function initWebId (parsedProfile) {
if (!parsedProfile) {
return
}
try {
this.webId = extractWebId(this.baseProfileUrl, parsedProfile).uri
} catch (e) {
throw new Error('Unable to parse WebID from profile: ' + e)
}
}
/**
* Returns an array of related external profile links (sameAs and seeAlso and
* Preferences files)
* @method relatedProfilesLinks
* @return {Array<String>}
*/
SolidProfile.prototype.relatedProfilesLinks = function relatedProfilesLinks () {
var links = []
links = links.concat(this.relatedProfiles.sameAs)
.concat(this.relatedProfiles.seeAlso)
if (this.preferences.uri) {
links = links.concat(this.preferences.uri)
}
return links
}
/**
* Returns true if the profile has any links to root storage
* @method hasStorage
* @return {Boolean}
*/
SolidProfile.prototype.hasStorage = function hasStorage () {
return this.storage &&
this.storage.length > 0
}
/**
* Convenience method to init the type index registry. Usage:
*
* ```
* Solid.getProfile(url, options)
* .then(function (profile) {
* return profile.initTypeRegistry(containerUrl, options)
* })
* ```
* @method initTypeRegistry
* @param containerUrl {String} The URL of the container for index documents
* @param [options] Options hashmap (see Solid.web.solidRequest() function docs)
* @return {SolidProfile}
*/
SolidProfile.prototype.initTypeRegistry = function initTypeRegistry (containerUrl,
options) {
return typeRegistry.initTypeRegistry(this, containerUrl, options)
}
/**
* Convenience method to load the type index registry. Usage:
*
* ```
* Solid.getProfile(url, options)
* .then(function (profile) {
* return profile.loadTypeRegistry(options)
* })
* ```
* @method loadTypeRegistry
* @param [options] Options hashmap (see Solid.web.solidRequest() function docs)
* @return {SolidProfile}
*/
SolidProfile.prototype.loadTypeRegistry = function loadTypeRegistry (options) {
return typeRegistry.loadTypeRegistry(this, options)
}
/**
* Adds a parsed type index graph to the appropriate type registry (public
* or private).
* @method addTypeRegistry
* @param graph {$rdf.IndexedFormula} Parsed graph (loaded from a type index
* resource)
* @param uri {String} Location of the type registry index document
*/
SolidProfile.prototype.addTypeRegistry = function addTypeRegistry (graph, uri) {
// Is this a public type registry?
if (typeRegistry.isListedTypeIndex(graph)) {
if (!this.typeIndexListed.graph) { // only initialize once
this.typeIndexListed.uri = uri
this.typeIndexListed.graph = graph
}
} else if (typeRegistry.isUnlistedTypeIndex(graph)) {
if (!this.typeIndexUnlisted.graph) {
this.typeIndexUnlisted.uri = uri
this.typeIndexUnlisted.graph = graph
}
} else {
throw new Error('Attempting to add an invalid type registry index')
}
}
/**
* Returns lists of registry entries for a given RDF Class.
* @method typeRegistryForClass
* @param rdfClass {rdf.NamedNode} RDF Class symbol
* @return {Array<IndexRegistration>}
*/
SolidProfile.prototype.typeRegistryForClass =
function typeRegistryForClass (rdfClass) {
return typeRegistry.typeRegistryForClass(this, rdfClass)
}
/**
* Registers a given RDF class in the user's type index registries, so that
* other applications can discover it.
* @method registerType
* @param rdfClass {rdf.NamedNode} Type to register in the index.
* @param location {String} Absolute URI to the location you want the class
* registered to. (Example: Registering Address books in
* `https://example.com/contacts/`)
* @param [locationType='container'] {String} Either 'instance' or 'container',
* defaults to 'container'
* @param [isListed=false] {Boolean} Whether to register in a listed or unlisted
* index). Defaults to `false` (unlisted).
* @return {Promise<SolidProfile>}
*/
SolidProfile.prototype.registerType =
function (rdfClass, location, locationType, isListed) {
return typeRegistry.registerType(this, rdfClass, location, locationType,
isListed)
}
/**
* Removes a given RDF class from the user's type index registry
* @method unregisterType
* @param rdfClass {rdf.NamedNode} Type to register in the index.
* @param [isListed=false] {Boolean} Whether to register in a listed or unlisted
* index). Defaults to `false` (unlisted).
* @param [location] {String} If present, only unregister the class from this
* location (absolute URI).
* @return {Promise<SolidProfile>}
*/
SolidProfile.prototype.unregisterType = function (rdfClass, isListed, location) {
return typeRegistry.unregisterType(this, rdfClass, isListed, location)
}
/**
* Extracts the WebID symbol from a parsed profile graph.
* @method extractWebId
* @param baseProfileUrl {String} Profile URL, with no hash fragment
* @param parsedProfile {rdf.IndexedFormula} RDFLib-parsed user profile
* @return {rdf.NamedNode} WebID symbol
*/
function extractWebId (baseProfileUrl, parsedProfile) {
var subj = rdf.sym(baseProfileUrl)
var pred = vocab.foaf('primaryTopic')
var match = parsedProfile.any(subj, pred)
return match
}
/**
* Extracts the first URI from a parsed graph that matches parameters
* @method parseLinks
* @param graph {rdf.IndexedFormula}
* @param subject {rdf.NamedNode}
* @param predicate {rdf.NamedNode}
* @param object {rdf.NamedNode}
* @param source {rdf.NamedNode}
* @return {String} URI that matches the parameters
*/
function parseLink (graph, subject, predicate, object, source) {
var first = graph.any(subject, predicate, object, source)
if (first) {
return first.uri
} else {
return null
}
}
},{"../type-registry":11,"../util/graph-util":12,"../util/rdf-parser":13,"../vocab":18}],8:[function(require,module,exports){
'use strict'
/**
* @module resource
*/
/**
* Represents a Solid / LDP Resource (currently used when listing
* SolidContainer resources)
* @class SolidResource
* @constructor
*/
module.exports = SolidResource
function SolidResource (uri, response) {
/**
* Short name (page/filename part of the resource path),
* derived from the URI
* @property name
* @type String
*/
this.name = null
/**
* Parsed graph of the contents of the resource
* @property parsedGraph
* @type Graph
*/
this.parsedGraph = null
/**
* Optional SolidResponse object from which this resource was initialized
* @property response
* @type SolidResponse
*/
this.response = response
/**
* List of RDF Types (classes) to which this resource belongs
* @property types
* @type Array<String>
*/
this.types = []
/**
* Absolute url of the resource
* @property url
* @type String
*/
this.uri = uri
if (response) {
if (response.url !== uri) {
// Override the given url (which may be relative) with that of the
// response object (which will be absolute)
this.uri = response.url
}
}
this.initName()
}
/**
* Initializes the short name from the url
* @method initName
*/
SolidResource.prototype.initName = function initName () {
if (!this.uri) {
return
}
// Split on '/', use the last fragment
var fragments = this.uri.split('/')
this.name = fragments.pop()
if (!this.name && fragments.length > 0) {
// URI ended in a '/'. Try again.
this.name = fragments.pop()
}
}
/**
* Is this a Container instance (vs a regular resource).
* (Is overridden in the subclass, `SolidContainer`)
* @return {Boolean}