Skip to content

Commit 926a192

Browse files
committed
Use globalThis and split out platform support.
- Use `globalThis` to set globals in browser contexts. - Split platform support out into Node.js and browser files.
1 parent f663661 commit 926a192

File tree

6 files changed

+80
-29
lines changed

6 files changed

+80
-29
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ module.exports = {
22
env: {
33
browser: true,
44
commonjs: true,
5-
node: true
5+
node: true,
6+
es2020: true
67
},
78
extends: 'eslint-config-digitalbazaar',
89
root: true

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# jsonld ChangeLog
22

3+
### Changed
4+
- Use `globalThis` to set globals in browser contexts.
5+
- Split platform support out into Node.js and browser files.
6+
37
## 5.0.0 - 2021-03-18
48

59
### Notes

lib/jsonld.js

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3535
*/
3636
const canonize = require('rdf-canonize');
37+
const platform = require('./platform');
3738
const util = require('./util');
3839
const ContextResolver = require('./ContextResolver');
3940
const IdentifierIssuer = util.IdentifierIssuer;
@@ -79,12 +80,6 @@ const {
7980
mergeNodeMaps: _mergeNodeMaps
8081
} = require('./nodeMap');
8182

82-
// determine if in-browser or using Node.js
83-
const _nodejs = (
84-
typeof process !== 'undefined' && process.versions && process.versions.node);
85-
const _browser = !_nodejs &&
86-
(typeof window !== 'undefined' || typeof self !== 'undefined');
87-
8883
/* eslint-disable indent */
8984
// attaches jsonld API to the given object
9085
const wrapper = function(jsonld) {
@@ -938,8 +933,6 @@ jsonld.getContextValue = require('./context').getContextValue;
938933
* Document loaders.
939934
*/
940935
jsonld.documentLoaders = {};
941-
jsonld.documentLoaders.node = require('./documentLoaders/node');
942-
jsonld.documentLoaders.xhr = require('./documentLoaders/xhr');
943936

944937
/**
945938
* Assigns the default document loader for external document URLs to a built-in
@@ -1005,24 +998,8 @@ jsonld.RequestQueue = require('./RequestQueue');
1005998
/* WebIDL API */
1006999
jsonld.JsonLdProcessor = require('./JsonLdProcessor')(jsonld);
10071000

1008-
// setup browser global JsonLdProcessor
1009-
if(_browser && typeof global.JsonLdProcessor === 'undefined') {
1010-
Object.defineProperty(global, 'JsonLdProcessor', {
1011-
writable: true,
1012-
enumerable: false,
1013-
configurable: true,
1014-
value: jsonld.JsonLdProcessor
1015-
});
1016-
}
1017-
1018-
// set platform-specific defaults/APIs
1019-
if(_nodejs) {
1020-
// use node document loader by default
1021-
jsonld.useDocumentLoader('node');
1022-
} else if(typeof XMLHttpRequest !== 'undefined') {
1023-
// use xhr document loader by default
1024-
jsonld.useDocumentLoader('xhr');
1025-
}
1001+
platform.setupGlobals(jsonld);
1002+
platform.setupDocumentLoaders(jsonld);
10261003

10271004
function _setDefaults(options, {
10281005
documentLoader = jsonld.documentLoader,

lib/platform-browser.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2021 Digital Bazaar, Inc. All rights reserved.
3+
*/
4+
'use strict';
5+
6+
const xhrLoader = require('./documentLoaders/xhr');
7+
8+
const api = {};
9+
module.exports = api;
10+
11+
/**
12+
* Setup browser document loaders.
13+
*
14+
* @param jsonld the jsonld api.
15+
*/
16+
api.setupDocumentLoaders = function(jsonld) {
17+
if(typeof XMLHttpRequest !== 'undefined') {
18+
jsonld.documentLoaders.xhr = xhrLoader;
19+
// use xhr document loader by default
20+
jsonld.useDocumentLoader('xhr');
21+
}
22+
};
23+
24+
/**
25+
* Setup browser globals.
26+
*
27+
* @param jsonld the jsonld api.
28+
*/
29+
api.setupGlobals = function(jsonld) {
30+
// setup browser global JsonLdProcessor
31+
if(typeof globalThis.JsonLdProcessor === 'undefined') {
32+
Object.defineProperty(globalThis, 'JsonLdProcessor', {
33+
writable: true,
34+
enumerable: false,
35+
configurable: true,
36+
value: jsonld.JsonLdProcessor
37+
});
38+
}
39+
};

lib/platform.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2021 Digital Bazaar, Inc. All rights reserved.
3+
*/
4+
'use strict';
5+
6+
const nodeLoader = require('./documentLoaders/node');
7+
8+
const api = {};
9+
module.exports = api;
10+
11+
/**
12+
* Setup Node.js document loaders.
13+
*
14+
* @param jsonld the jsonld api.
15+
*/
16+
api.setupDocumentLoaders = function(jsonld) {
17+
jsonld.documentLoaders.node = nodeLoader;
18+
// use node document loader by default
19+
jsonld.useDocumentLoader('node');
20+
};
21+
22+
/**
23+
* Setup Node.js globals.
24+
*
25+
* @param jsonld the jsonld api.
26+
*/
27+
/* eslint-disable-next-line no-unused-vars */
28+
api.setupGlobals = function(jsonld) {
29+
// none for Node.js
30+
};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@
113113
]
114114
},
115115
"browser": {
116-
"lib/index.js": "./lib/jsonld.js",
117-
"lib/documentLoaders/node.js": false,
116+
"./lib/index.js": "./lib/jsonld.js",
117+
"./lib/platform.js": "./lib/platform-browser.js",
118118
"crypto": false,
119119
"http": false,
120120
"jsonld-request": false,

0 commit comments

Comments
 (0)