pax_global_header00006660000000000000000000000064141272554310014516gustar00rootroot0000000000000052 comment=c3cdadd00edd6d399e0853decd0e2282cb32436a uni-global-1.0.0/000077500000000000000000000000001412725543100135455ustar00rootroot00000000000000uni-global-1.0.0/.editorconfig000077500000000000000000000004461412725543100162310ustar00rootroot00000000000000# EditorConfig is awesome: http://EditorConfig.org # top-most EditorConfig file root = true [*] charset = utf-8 end_of_line = lf insert_final_newline = true indent_style = tab trim_trailing_whitespace = true [*.md,*.yml] indent_size = 2 indent_style = space trim_trailing_whitespace = false uni-global-1.0.0/.github/000077500000000000000000000000001412725543100151055ustar00rootroot00000000000000uni-global-1.0.0/.github/FUNDING.yml000066400000000000000000000000201412725543100167120ustar00rootroot00000000000000github: medikoo uni-global-1.0.0/.github/workflows/000077500000000000000000000000001412725543100171425ustar00rootroot00000000000000uni-global-1.0.0/.github/workflows/integrate.yml000066400000000000000000000120141412725543100216450ustar00rootroot00000000000000# main only name: Integrate on: push: branches: [main] env: FORCE_COLOR: 1 jobs: linuxNode14: name: "[Linux] Node.js v14: Unit tests" runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Retrieve dependencies from cache id: cacheNpm uses: actions/cache@v2 with: path: | ~/.npm node_modules key: npm-v14-${{ runner.os }}-${{ github.ref }}-${{ hashFiles('package.json') }} restore-keys: npm-v14-${{ runner.os }}-${{ github.ref }}- - name: Install Node.js and npm uses: actions/setup-node@v1 with: node-version: 14.x - name: Install dependencies if: steps.cacheNpm.outputs.cache-hit != 'true' run: | npm update --no-save npm update --save-dev --no-save - name: Unit tests run: npm test -- -b windowsNode14: name: "[Windows] Node.js v14: Unit tests" runs-on: windows-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Retrieve dependencies from cache id: cacheNpm uses: actions/cache@v2 with: path: | ~/.npm node_modules key: npm-v14-${{ runner.os }}-${{ github.ref }}-${{ hashFiles('package.json') }} restore-keys: npm-v14-${{ runner.os }}-${{ github.ref }}- - name: Install Node.js and npm uses: actions/setup-node@v1 with: node-version: 14.x - name: Install dependencies if: steps.cacheNpm.outputs.cache-hit != 'true' run: | npm update --no-save npm update --save-dev --no-save - name: Unit tests run: npm test -- -b linuxNode16: name: "[Linux] Node.js 16: Unit tests on npm package" runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Retrieve dependencies from cache id: cacheNpm uses: actions/cache@v2 with: path: | ~/.npm node_modules key: npm-v16-${{ runner.os }}-${{ github.ref }}-${{ hashFiles('package.json') }} restore-keys: npm-v16-${{ runner.os }}-${{ github.ref }}- - name: Install Node.js and npm uses: actions/setup-node@v1 with: node-version: 16.x - name: Install dependencies if: steps.cacheNpm.outputs.cache-hit != 'true' run: | npm update --no-save npm update --save-dev --no-save - name: Build local package run: npm pack - name: Unit tests on npm package run: | NAME=`cat package.json | grep '"name": "' | grep -oE ": .*" | grep -oE '[^: ",]+'` VERSION=`cat package.json | grep '"version": "' | grep -oE ": .*" | grep -oE '[^: ",]+'` tar zxf "${NAME}-${VERSION}.tgz" cp -R test "package" ln -s "$(pwd)"/node_modules "package/node_modules" cd "package" npm test -- -b linuxNode12: name: "[Linux] Node.js v12: Unit tests with coverage" runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Retrieve dependencies from cache id: cacheNpm uses: actions/cache@v2 with: path: | ~/.npm node_modules key: npm-v12-${{ runner.os }}-${{ github.ref }}-${{ hashFiles('package.json') }} restore-keys: npm-v12-${{ runner.os }}-${{ github.ref }}- - name: Install Node.js and npm uses: actions/setup-node@v1 with: node-version: 12.x - name: Install dependencies if: steps.cacheNpm.outputs.cache-hit != 'true' run: | npm update --no-save npm update --save-dev --no-save - name: Unit tests run: npm run coverage - name: Push coverage run: npx codecov tagIfNewVersion: name: Tag if new version runs-on: ubuntu-latest needs: [linuxNode14, linuxNode12, linuxNode16, windowsNode14] steps: - name: Checkout repository uses: actions/checkout@v2 with: # Ensure to have complete history of commits pushed with given push operation # It's loose and imperfect assumption that no more than 30 commits will be pushed at once fetch-depth: 30 # Tag needs to be pushed with real user token, otherwise pushed tag won't trigger the actions workflow # Hence we're passing 'serverless-ci' user authentication token token: ${{ secrets.USER_GITHUB_TOKEN }} - name: Tag if new version if: github.event.before != '0000000000000000000000000000000000000000' # Skip on first commit run: | NEW_VERSION=`git diff -U0 ${{ github.event.before }} package.json | grep '"version": "' | tail -n 1 | grep -oE "[0-9]+\.[0-9]+\.[0-9]+"` || : if [ -n "$NEW_VERSION" ]; then git tag v$NEW_VERSION git push --tags fi uni-global-1.0.0/.github/workflows/publish.yml000066400000000000000000000033271412725543100213400ustar00rootroot00000000000000# Version tags only name: Publish on: push: tags: - v[0-9]+.[0-9]+.[0-9]+ jobs: publish: name: Publish runs-on: ubuntu-latest env: # It'll work with secrets.GITHUB_TOKEN (which is provided by GitHub unconditionally) # Still then release author would be "github-actions" GITHUB_TOKEN: ${{ secrets.USER_GITHUB_TOKEN }} steps: - name: Checkout repository uses: actions/checkout@v2 - name: Retrieve dependencies from cache uses: actions/cache@v2 with: path: | ~/.npm node_modules key: npm-v14-${{ runner.os }}-refs/heads/main-${{ hashFiles('package.json') }} - name: Install Node.js and npm uses: actions/setup-node@v1 with: node-version: 14.x registry-url: https://registry.npmjs.org - name: Publish new version # Note: Setting NODE_AUTH_TOKEN as job|workspace wide env var won't work # as it appears actions/setup-node sets own value env: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} run: npm publish # Normally we have a guarantee that deps are already there, still it may not be the case when: # - `main` build for same commit failed (and we still pushed tag manually) # - We've pushed tag manually before `master` build finalized - name: Install dependencies if: steps.cacheNpm.outputs.cache-hit != 'true' run: | npm update --no-save npm update --save-dev --no-save - name: Publish release notes run: | TEMP_ARRAY=($(echo $GITHUB_REF | tr "/" "\n")) TAG=${TEMP_ARRAY[@]: -1} npx github-release-from-cc-changelog $TAG uni-global-1.0.0/.github/workflows/validate.yml000066400000000000000000000076731412725543100214730ustar00rootroot00000000000000# PR's only name: Validate on: pull_request: branches: [main] env: FORCE_COLOR: 1 jobs: linuxNode14: name: "[Linux] Node.js v14: Lint, Formatting, Unit tests" runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Retrieve dependencies from cache id: cacheNpm uses: actions/cache@v2 with: path: | ~/.npm node_modules key: npm-v14-${{ runner.os }}-${{ github.ref }}-${{ hashFiles('package.json') }} restore-keys: | npm-v14-${{ runner.os }}-${{ github.ref }}- npm-v14-${{ runner.os }}-refs/heads/main- - name: Install Node.js and npm uses: actions/setup-node@v1 with: node-version: 14.x - name: Install dependencies if: steps.cacheNpm.outputs.cache-hit != 'true' run: | npm update --no-save npm update --save-dev --no-save - name: Validate Prettier formatting run: npm run prettier-check:updated - name: Validate ESLint rules run: npm run lint:updated - name: Unit tests run: npm test -- -b windowsNode14: name: "[Windows] Node.js v14: Unit tests" runs-on: windows-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Retrieve dependencies from cache id: cacheNpm uses: actions/cache@v2 with: path: | ~/.npm node_modules key: npm-v14-${{ runner.os }}-${{ github.ref }}-${{ hashFiles('package.json') }} restore-keys: | npm-v14-${{ runner.os }}-${{ github.ref }}- npm-v14-${{ runner.os }}-refs/heads/main- - name: Install Node.js and npm uses: actions/setup-node@v1 with: node-version: 14.x - name: Install dependencies if: steps.cacheNpm.outputs.cache-hit != 'true' run: | npm update --no-save npm update --save-dev --no-save - name: Unit tests run: npm test -- -b linuxNode16: name: "[Linux] Node.js 16: Unit tests" runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Retrieve dependencies from cache id: cacheNpm uses: actions/cache@v2 with: path: | ~/.npm node_modules key: npm-v16-${{ runner.os }}-${{ github.ref }}-${{ hashFiles('package.json') }} restore-keys: | npm-v16-${{ runner.os }}-${{ github.ref }}- npm-v16-${{ runner.os }}-refs/heads/main- - name: Install Node.js and npm uses: actions/setup-node@v1 with: node-version: 16.x - name: Install dependencies if: steps.cacheNpm.outputs.cache-hit != 'true' run: | npm update --no-save npm update --save-dev --no-save - name: Unit tests run: npm run test -- -b linuxNode12: name: "[Linux] Node.js v12: Unit tests with coverage" runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Retrieve dependencies from cache id: cacheNpm uses: actions/cache@v2 with: path: | ~/.npm node_modules key: npm-v12-${{ runner.os }}-${{ github.ref }}-${{ hashFiles('package.json') }} restore-keys: | npm-v12-${{ runner.os }}-${{ github.ref }}- npm-v12-${{ runner.os }}-refs/heads/main- - name: Install Node.js and npm uses: actions/setup-node@v1 with: node-version: 12.x - name: Install dependencies if: steps.cacheNpm.outputs.cache-hit != 'true' run: | npm update --no-save npm update --save-dev --no-save - name: Unit tests run: npm run coverage - name: Push coverage run: npx codecov uni-global-1.0.0/.gitignore000077500000000000000000000001061412725543100155350ustar00rootroot00000000000000/.nyc_output /coverage /node_modules npm-debug.log /package-lock.json uni-global-1.0.0/.npmignore000066400000000000000000000000361412725543100155430ustar00rootroot00000000000000/.editorconfig /.github /test uni-global-1.0.0/CHANGELOG.md000066400000000000000000000007601412725543100153610ustar00rootroot00000000000000# Changelog All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. ### 1.0.0 (2021-10-06) ### Features - Main functionality ([76c407e](https://github.com/medikoo/uni-global/commit/76c407e452840f231c82f31f57654f37f600b79d)) - `adaptRealm` to adapt globals from other realms ([68dd172](https://github.com/medikoo/uni-global/commit/68dd17244e25eb92ddb1311321307548ab83561e)) uni-global-1.0.0/LICENSE000066400000000000000000000014001412725543100145450ustar00rootroot00000000000000ISC License Copyright (c) 2021, Mariusz Nowak, @medikoo, medikoo.com Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. uni-global-1.0.0/README.md000077500000000000000000000035221412725543100150310ustar00rootroot00000000000000[![Build status][build-image]][build-url] [![Tests coverage][cov-image]][cov-url] [![npm version][npm-image]][npm-url] # uni-global ## Global namespace with no global scope pollution ### Use case In modules world, there's possiblity that two different instance of same modules (e.g. installed in different `node_modules`) are being run in same process. If for any reason they need to operate on context instance that's same for the given application or process, then ocassionally introduced multiple installations may break the application. This module provides interface wich can be used by those to modules to ensure that no matter how many instances of given module are loaded, in all cases they end with same context instance. ### Example usage: ```javascript // No matter how many instances of given module are loaded in the process, they will always end with same context instance const globalContext = require("uni-global")("my-scope-name"); // globalContext is a plain object, on which needed global data can be stored. globalContext.someSingletonData = ... ``` ### Adapt manually other realm If there's a need to adapt other environment realm (e.g. coming from iframe) to share same uni-global directory. Register it with `adaptRealm` util as below: ```javascript const adaptRealm = require("uni-global/adapt-realm"); adaptRealm(iFrameWindow); // Pass global object of the other realm ``` ### Installation ```bash npm install uni-global ``` [build-image]: https://github.com/medikoo/uni-global/workflows/Integrate/badge.svg [build-url]: https://github.com/medikoo/uni-global/actions?query=workflow%3AIntegrate [cov-image]: https://img.shields.io/codecov/c/github/medikoo/uni-global.svg [cov-url]: https://codecov.io/gh/medikoo/uni-global [npm-image]: https://img.shields.io/npm/v/uni-global.svg [npm-url]: https://www.npmjs.com/package/uni-global uni-global-1.0.0/adapt-realm.js000066400000000000000000000027701412725543100163000ustar00rootroot00000000000000"use strict"; var ensureObject = require("type/object/ensure") , toShortString = require("type/lib/to-short-string") , esEnvType = require("./lib/es-env-type") , uniGlobal = require("./lib/uni-global"); module.exports = function (realmGlobal) { ensureObject(realmGlobal, { name: "realmGlobal" }); if (typeof realmGlobal.EvalError !== "function") { throw new TypeError("Expected a realm global recieved: " + toShortString(realmGlobal)); } switch (esEnvType) { case "3": if (realmGlobal.EvalError.$ug202109) { if (realmGlobal.EvalError.$ug202109 === uniGlobal) return; throw new Error("Cannot adapt realm, as it already has uni-global defined"); } realmGlobal.EvalError.$ug202109 = uniGlobal; return; case "5": if (realmGlobal.EvalError.$uniGlobal202109) { if (realmGlobal.EvalError.$uniGlobal202109 === uniGlobal) return; throw new Error("Cannot adapt realm, as it already has uni-global defined"); } Object.defineProperty(realmGlobal.EvalError, "$uniGlobal202109", { value: uniGlobal }); return; case "2015+": var uniGlobalSymbol = Symbol["for"]("$uniGlobal202109"); if (realmGlobal.EvalError[uniGlobalSymbol]) { if (realmGlobal.EvalError[uniGlobalSymbol] === uniGlobal) return; throw new Error("Cannot adapt realm, as it already has uni-global defined"); } Object.defineProperty(realmGlobal.EvalError, uniGlobalSymbol, { value: uniGlobal }); return; default: throw new Error("Unrecognized environment type: " + esEnvType); } }; uni-global-1.0.0/commitlint.config.js000066400000000000000000000012361412725543100175300ustar00rootroot00000000000000"use strict"; module.exports = { rules: { "body-leading-blank": [2, "always"], "body-max-line-length": [2, "always", 72], "footer-leading-blank": [2, "always"], "footer-max-line-length": [2, "always", 72], "header-max-length": [2, "always", 72], "scope-case": [2, "always", "start-case"], "scope-enum": [2, "always", [""]], "subject-case": [2, "always", "sentence-case"], "subject-empty": [2, "never"], "subject-full-stop": [2, "never", "."], "type-case": [2, "always", "lower-case"], "type-empty": [2, "never"], "type-enum": [ 2, "always", ["build", "chore", "ci", "docs", "feat", "fix", "perf", "refactor", "style", "test"] ] } }; uni-global-1.0.0/index.js000066400000000000000000000006361412725543100152170ustar00rootroot00000000000000"use strict"; var ensureString = require("type/string/ensure") , uniGlobal = require("./lib/uni-global"); var objHasOwnProperty = Object.prototype.hasOwnProperty; module.exports = function (key) { key = ensureString(key, { name: "key" }); if (objHasOwnProperty.call(uniGlobal, key)) return uniGlobal[key]; var value = Object.create ? Object.create(null) : {}; uniGlobal[key] = value; return value; }; uni-global-1.0.0/lib/000077500000000000000000000000001412725543100143135ustar00rootroot00000000000000uni-global-1.0.0/lib/es-env-type.js000066400000000000000000000003001412725543100170160ustar00rootroot00000000000000"use strict"; if (!Object.defineProperty || !Object.create) module.exports = "3"; else if (typeof Symbol === "function" && Symbol["for"]) module.exports = "2015+"; else module.exports = "5"; uni-global-1.0.0/lib/uni-global.js000066400000000000000000000015001412725543100166760ustar00rootroot00000000000000"use strict"; var esEnvType = require("./es-env-type"); switch (esEnvType) { case "3": if (EvalError.$ug202109) { module.exports = EvalError.$ug202109; return; } module.exports = EvalError.$ug202109 = {}; return; case "5": if (EvalError.$uniGlobal202109) { module.exports = EvalError.$uniGlobal202109; return; } Object.defineProperty(EvalError, "$uniGlobal202109", { value: (module.exports = Object.create(null)) }); return; case "2015+": var uniGlobalSymbol = Symbol["for"]("$uniGlobal202109"); if (EvalError[uniGlobalSymbol]) { module.exports = EvalError[uniGlobalSymbol]; return; } Object.defineProperty(EvalError, uniGlobalSymbol, { value: (module.exports = Object.create(null)) }); return; default: throw new Error("Unrecognized environment type: " + esEnvType); } uni-global-1.0.0/package.json000077500000000000000000000052661412725543100160470ustar00rootroot00000000000000{ "name": "uni-global", "version": "1.0.0", "description": "Global namespace without polluting the global scope", "author": "Mariusz Nowak (https://www.medikoo.com/)", "keywords": [ "global", "globalThis" ], "repository": "medikoo/uni-global", "dependencies": { "type": "^2.5.0" }, "devDependencies": { "@commitlint/cli": "^13.2.0", "chai": "^4.3.4", "eslint": "^7.32.0", "eslint-config-medikoo": "^4.1.0", "ext": "^1.6.0", "git-list-updated": "^1.2.1", "github-release-from-cc-changelog": "^2.2.0", "husky": "^4.3.8", "lint-staged": "^11.1.2", "mocha": "^9.1.2", "ncjsm": "^4.2.0", "nyc": "^15.1.0", "prettier-elastic": "^2.2.1", "standard-version": "^9.3.1" }, "husky": { "hooks": { "commit-msg": "commitlint -E HUSKY_GIT_PARAMS", "pre-commit": "lint-staged" } }, "lint-staged": { "*.js": [ "eslint" ], "*.{css,html,js,json,md,yaml,yml}": [ "prettier -c" ] }, "eslintConfig": { "extends": "medikoo/es3", "root": true, "globals": { "Symbol": true }, "overrides": [ { "files": "test/**/*.js", "env": { "mocha": true } } ] }, "prettier": { "printWidth": 100, "tabWidth": 4, "overrides": [ { "files": [ "*.md", "*.yml" ], "options": { "tabWidth": 2 } } ] }, "nyc": { "all": true, "exclude": [ ".github", "coverage/**", "test/**", "prettier.config.js" ], "reporter": [ "lcov", "html", "text-summary" ] }, "standard-version": { "skip": { "commit": true, "tag": true }, "types": [ { "type": "feat", "section": "Features" }, { "type": "fix", "section": "Bug Fixes" }, { "type": "perf", "section": "Performance Improvements" }, { "type": "refactor", "section": "Maintenance Improvements" }, { "type": "chore", "section": "Maintenance Improvements" } ] }, "scripts": { "commitlint": "commitlint -f HEAD@{15}", "coverage": "nyc npm test", "lint": "eslint --ignore-path=.gitignore .", "lint:updated": "pipe-git-updated --ext=js -- eslint --ignore-pattern '!*'", "prepare-release": "standard-version && prettier --write CHANGELOG.md", "prettier-check": "prettier -c --ignore-path .gitignore \"**/*.{css,html,js,json,md,yaml,yml}\"", "prettier-check:updated": "pipe-git-updated --ext=css --ext=html --ext=js --ext=json --ext=md --ext=yaml --ext=yml -- prettier -c", "prettify": "prettier --write --ignore-path .gitignore \"**/*.{css,html,js,json,md,yaml,yml}\"", "prettify:updated": "pipe-git-updated --ext=css --ext=html --ext=js --ext=json --ext=md --ext=yaml --ext=yml -- prettier --write", "test": "mocha" }, "license": "ISC" } uni-global-1.0.0/test/000077500000000000000000000000001412725543100145245ustar00rootroot00000000000000uni-global-1.0.0/test/adapt-realm.test.js000066400000000000000000000017241412725543100202330ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , globalThis = require("ext/global-this") , requireUncached = require("ncjsm/require-uncached") , adaptRealm = require("../adapt-realm"); var uniGlobal = require(".."); describe("adapt-realm.js", function () { it("should adapt outer realm to share same uni global", function () { var otherRealmGlobal = { EvalError: function () { /* noop */ } }; adaptRealm(otherRealmGlobal); var otherRealmUniGlobal = requireUncached(function () { var originalEvalError = EvalError; globalThis.EvalError = otherRealmGlobal.EvalError; try { return require(".."); } finally { globalThis.EvalError = originalEvalError; } }); assert.equal(uniGlobal("foo"), otherRealmUniGlobal("foo")); }); it("Adapting same realm twice should be ineffective", function () { var otherRealmGlobal = { EvalError: function () { /* noop */ } }; adaptRealm(otherRealmGlobal); adaptRealm(otherRealmGlobal); }); }); uni-global-1.0.0/test/index.test.js000066400000000000000000000010301412725543100171410ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , uniGlobal = require(".."); var isObject = require("type/object/is"); describe("index.js", function () { it("should return object for namespace", function () { assert.equal(isObject(uniGlobal("foo")), true); }); it("should return same object for same namspace", function () { assert.equal(uniGlobal("foo"), uniGlobal("foo")); }); it("should return different object for different namspace", function () { assert.notEqual(uniGlobal("foo"), uniGlobal("bar")); }); });