@@ -97,10 +97,25 @@ const wrapper = function(jsonld) {
97
97
/** Registered RDF dataset parsers hashed by content-type. */
98
98
const _rdfParsers = { } ;
99
99
100
- // resolved context cache
101
- // TODO: consider basing max on context size rather than number
100
+ // resolved context caches
102
101
const RESOLVED_CONTEXT_CACHE_MAX_SIZE = 100 ;
103
- const _resolvedContextCache = new LRU ( { max : RESOLVED_CONTEXT_CACHE_MAX_SIZE } ) ;
102
+ // caches are created and indexed per documentLoader
103
+ // resources are cleaned up with WeakMap semantics
104
+ // TODO: add controls for cache resource usage
105
+ const _resolvedContextCaches = new WeakMap ( ) ;
106
+
107
+ // make a ContextResolver using a per-documentLoader shared cache
108
+ function _makeContextResolver ( { documentLoader = 'default' } ) {
109
+ let cache = _resolvedContextCaches . get ( documentLoader ) ;
110
+ if ( ! cache ) {
111
+ // TODO: consider basing max on context size rather than number
112
+ cache = new LRU ( { max : RESOLVED_CONTEXT_CACHE_MAX_SIZE } ) ;
113
+ _resolvedContextCaches . set ( documentLoader , cache ) ;
114
+ }
115
+ return new ContextResolver ( {
116
+ sharedCache : cache
117
+ } ) ;
118
+ }
104
119
105
120
/* Core API */
106
121
@@ -152,8 +167,9 @@ jsonld.compact = async function(input, ctx, options) {
152
167
skipExpansion : false ,
153
168
link : false ,
154
169
issuer : new IdentifierIssuer ( '_:b' ) ,
155
- contextResolver : new ContextResolver (
156
- { sharedCache : _resolvedContextCache } )
170
+ contextResolver : _makeContextResolver ( {
171
+ documentLoader : options . documentLoader
172
+ } )
157
173
} ) ;
158
174
if ( options . link ) {
159
175
// force skip expansion when linking, "link" is not part of the public
@@ -269,8 +285,9 @@ jsonld.expand = async function(input, options) {
269
285
// set default options
270
286
options = _setDefaults ( options , {
271
287
keepFreeFloatingNodes : false ,
272
- contextResolver : new ContextResolver (
273
- { sharedCache : _resolvedContextCache } )
288
+ contextResolver : _makeContextResolver ( {
289
+ documentLoader : options . documentLoader
290
+ } )
274
291
} ) ;
275
292
276
293
// build set of objects that may have @contexts to resolve
@@ -368,8 +385,9 @@ jsonld.flatten = async function(input, ctx, options) {
368
385
// set default options
369
386
options = _setDefaults ( options , {
370
387
base : _isString ( input ) ? input : '' ,
371
- contextResolver : new ContextResolver (
372
- { sharedCache : _resolvedContextCache } )
388
+ contextResolver : _makeContextResolver ( {
389
+ documentLoader : options . documentLoader
390
+ } )
373
391
} ) ;
374
392
375
393
// expand input
@@ -423,8 +441,9 @@ jsonld.frame = async function(input, frame, options) {
423
441
requireAll : false ,
424
442
omitDefault : false ,
425
443
bnodesToClear : [ ] ,
426
- contextResolver : new ContextResolver (
427
- { sharedCache : _resolvedContextCache } )
444
+ contextResolver : _makeContextResolver ( {
445
+ documentLoader : options . documentLoader
446
+ } )
428
447
} ) ;
429
448
430
449
// if frame is a string, attempt to dereference remote document
@@ -565,8 +584,9 @@ jsonld.normalize = jsonld.canonize = async function(input, options) {
565
584
algorithm : 'URDNA2015' ,
566
585
skipExpansion : false ,
567
586
safe : true ,
568
- contextResolver : new ContextResolver (
569
- { sharedCache : _resolvedContextCache } )
587
+ contextResolver : _makeContextResolver ( {
588
+ documentLoader : options . documentLoader
589
+ } )
570
590
} ) ;
571
591
if ( 'inputFormat' in options ) {
572
592
if ( options . inputFormat !== 'application/n-quads' &&
@@ -674,8 +694,9 @@ jsonld.toRDF = async function(input, options) {
674
694
options = _setDefaults ( options , {
675
695
base : _isString ( input ) ? input : '' ,
676
696
skipExpansion : false ,
677
- contextResolver : new ContextResolver (
678
- { sharedCache : _resolvedContextCache } )
697
+ contextResolver : _makeContextResolver ( {
698
+ documentLoader : options . documentLoader
699
+ } )
679
700
} ) ;
680
701
681
702
// TODO: support toRDF custom map?
@@ -726,8 +747,9 @@ jsonld.createNodeMap = async function(input, options) {
726
747
// set default options
727
748
options = _setDefaults ( options , {
728
749
base : _isString ( input ) ? input : '' ,
729
- contextResolver : new ContextResolver (
730
- { sharedCache : _resolvedContextCache } )
750
+ contextResolver : _makeContextResolver ( {
751
+ documentLoader : options . documentLoader
752
+ } )
731
753
} ) ;
732
754
733
755
// expand input
@@ -774,8 +796,9 @@ jsonld.merge = async function(docs, ctx, options) {
774
796
775
797
// set default options
776
798
options = _setDefaults ( options , {
777
- contextResolver : new ContextResolver (
778
- { sharedCache : _resolvedContextCache } )
799
+ contextResolver : _makeContextResolver ( {
800
+ documentLoader : options . documentLoader
801
+ } )
779
802
} ) ;
780
803
781
804
// expand all documents
@@ -926,8 +949,9 @@ jsonld.processContext = async function(
926
949
// set default options
927
950
options = _setDefaults ( options , {
928
951
base : '' ,
929
- contextResolver : new ContextResolver (
930
- { sharedCache : _resolvedContextCache } )
952
+ contextResolver : _makeContextResolver ( {
953
+ documentLoader : options . documentLoader
954
+ } )
931
955
} ) ;
932
956
933
957
// return initial context early for null context
0 commit comments