Skip to content

Commit

Permalink
feat: improve cross platform compatibilty with conditional crypto import
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-guggisberg committed Sep 13, 2022
1 parent f8872b7 commit b5a1111
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
7 changes: 7 additions & 0 deletions packages/helix-shared-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
"version": "2.0.11",
"description": "Shared modules of the Helix Project - Utils",
"main": "src/utils.js",
"imports": {
"#crypto": {
"node": "./src/crypto.node.js",
"browser": "./src/crypto.worker.js",
"worker": "./src/crypto.worker.js"
}
},
"scripts": {
"test": "c8 mocha",
"lint": "eslint .",
Expand Down
14 changes: 14 additions & 0 deletions packages/helix-shared-utils/src/crypto.node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright 2018 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

// browser (worker) runtime
module.exports = require('crypto');
15 changes: 15 additions & 0 deletions packages/helix-shared-utils/src/crypto.worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright 2018 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

// browser (worker) runtime
// eslint-disable-next-line no-undef
module.exports = crypto;
5 changes: 2 additions & 3 deletions packages/helix-shared-utils/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
/* c8 ignore next 2 */
// eslint-disable-next-line no-undef
const cryptoImpl = typeof crypto === 'undefined' ? require('crypto') : crypto;
// eslint-disable-next-line import/no-unresolved
const cryptoImpl = require('#crypto');

/**
* A glorified lookup table that translates backend errors into the appropriate
Expand Down

0 comments on commit b5a1111

Please sign in to comment.