From 3b6f61ef007bfcb7f0874c10849c56aef5576cea Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Tue, 19 Jul 2022 15:17:15 +0700 Subject: [PATCH] Update dependencies (#32) --- .github/workflows/ci.yml | 16 +++++------ .github/workflows/esm-lint.yml | 50 ++++++++++++++++++++++++++-------- index.ts | 13 ++++++--- package.json | 36 +++++++++++------------- test/_fixtures.js | 5 ---- test/_setup.js | 5 ++++ test/index.js | 9 ++++-- 7 files changed, 83 insertions(+), 51 deletions(-) delete mode 100644 test/_fixtures.js create mode 100644 test/_setup.js diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e60b6fd..95614c4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,18 +8,18 @@ jobs: Lint: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: install - run: npm ci || npm install + run: npm install - name: XO run: npx xo Test: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: install - run: npm ci || npm install + run: npm install - name: build run: npm run build --if-present - name: AVA @@ -28,17 +28,17 @@ jobs: Build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: install - run: npm ci || npm install + run: npm install - name: build run: npm run build Types: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: install - run: npm ci || npm install + run: npm install - run: npm run build - run: npx tsd diff --git a/.github/workflows/esm-lint.yml b/.github/workflows/esm-lint.yml index b07fc1c..ab05083 100644 --- a/.github/workflows/esm-lint.yml +++ b/.github/workflows/esm-lint.yml @@ -4,7 +4,6 @@ env: # FILE GENERATED WITH: npx ghat fregante/ghatemplates/esm-lint # SOURCE: https://github.com/fregante/ghatemplates -# OPTIONS: {"exclude":["jobs.Node","jobs.Rollup"]} name: ESM on: @@ -19,19 +18,19 @@ jobs: Pack: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - run: npm install - run: npm run build --if-present - run: npm pack --dry-run - - run: npm pack --silent 2>/dev/null | xargs -n1 tar -xzf - - uses: actions/upload-artifact@v2 + - run: npm pack | tail -1 | xargs -n1 tar -xzf + - uses: actions/upload-artifact@v3 with: path: package Webpack: runs-on: ubuntu-latest needs: Pack steps: - - uses: actions/download-artifact@v2 + - uses: actions/download-artifact@v3 - run: npm install ./artifact - run: echo "${{ env.IMPORT_TEXT }} '${{ env.NPM_MODULE_NAME }}'" > index.js - run: webpack --entry ./index.js @@ -40,27 +39,56 @@ jobs: runs-on: ubuntu-latest needs: Pack steps: - - uses: actions/download-artifact@v2 + - uses: actions/download-artifact@v3 - run: npm install ./artifact - run: echo "${{ env.IMPORT_TEXT }} '${{ env.NPM_MODULE_NAME }}'" > index.js - run: npx parcel@2 build index.js - run: cat dist/index.js - Snowpack: + Rollup: runs-on: ubuntu-latest needs: Pack steps: - - uses: actions/download-artifact@v2 + - uses: actions/download-artifact@v3 + - run: npm install ./artifact rollup@2 @rollup/plugin-node-resolve + - run: echo "${{ env.IMPORT_TEXT }} '${{ env.NPM_MODULE_NAME }}'" > index.js + - run: npx rollup -p node-resolve index.js + Vite: + runs-on: ubuntu-latest + needs: Pack + steps: + - uses: actions/download-artifact@v3 + - run: npm install ./artifact + - run: >- + echo '' > index.html + - run: npx vite build + - run: cat dist/assets/* + esbuild: + runs-on: ubuntu-latest + needs: Pack + steps: + - uses: actions/download-artifact@v3 - run: echo '{}' > package.json - run: echo "${{ env.IMPORT_TEXT }} '${{ env.NPM_MODULE_NAME }}'" > index.js - run: npm install ./artifact - - run: npx snowpack@2 build - - run: cat build/web_modules/$NPM_MODULE_NAME.js + - run: npx esbuild --bundle index.js TypeScript: runs-on: ubuntu-latest needs: Pack steps: - - uses: actions/download-artifact@v2 + - uses: actions/download-artifact@v3 - run: npm install ./artifact - run: echo "${{ env.IMPORT_TEXT }} '${{ env.NPM_MODULE_NAME }}'" > index.ts - run: tsc index.ts - run: cat index.js + Node: + runs-on: ubuntu-latest + needs: Pack + steps: + - uses: actions/download-artifact@v3 + - uses: actions/setup-node@v3 + with: + node-version: 14.x + - run: echo "${{ env.IMPORT_TEXT }} '${{ env.NPM_MODULE_NAME }}'" > index.mjs + - run: npm install ./artifact + - run: node index.mjs diff --git a/index.ts b/index.ts index 1719503..1c626f8 100644 --- a/index.ts +++ b/index.ts @@ -1,8 +1,10 @@ import microMemoize from 'micro-memoize'; -import {isBackgroundPage} from 'webext-detect-page'; +import {isBackgroundPage, isExtensionContext} from 'webext-detect-page'; import toMilliseconds, {TimeDescriptor} from '@sindresorhus/to-milliseconds'; import chromeP from 'webext-polyfill-kinda'; +const cacheDefault = {days: 30}; + function timeInTheFuture(time: TimeDescriptor): number { return Date.now() + toMilliseconds(time); } @@ -50,13 +52,14 @@ async function _get( async function get( key: string, ): Promise { - return (await _get(key, true))?.data; + const cacheItem = await _get(key, true); + return cacheItem?.data; } async function set( key: string, value: ScopedValue, - maxAge: TimeDescriptor = {days: 30}, + maxAge: TimeDescriptor = cacheDefault, ): Promise { if (arguments.length < 2) { throw new TypeError('Expected a value as the second argument'); @@ -168,7 +171,9 @@ const cache = { function init(): void { // Make it available globally for ease of use - (window as any).webextStorageCache = cache; + if (isExtensionContext()) { + (globalThis as any).webextStorageCache = cache; + } // Automatically clear cache every day if (!isBackgroundPage()) { diff --git a/package.json b/package.json index 11a63b9..8b2b71b 100644 --- a/package.json +++ b/package.json @@ -45,33 +45,29 @@ "browser", "webextensions" ], - "rules": { - "@typescript-eslint/return-await": 0, - "import/extensions": 0, - "import/no-unassigned-import": 0, - "import/no-useless-path-segments": 0, - "no-promise-executor-return": 0, - "unicorn/import-index": 0, - "unicorn/no-object-as-default-parameter": 0 - } + "rules": {} + }, + "ava": { + "require": [ + "./test/_setup.js" + ] }, "dependencies": { "@sindresorhus/to-milliseconds": "^2.0.0", - "micro-memoize": "^4.0.9", - "webext-detect-page": "^3.1.0", - "webext-polyfill-kinda": "^0.9.0" + "micro-memoize": "^4.0.10", + "webext-detect-page": "^4.0.1", + "webext-polyfill-kinda": "^0.10.0" }, "devDependencies": { - "@babel/core": "^7.16.0", "@sindresorhus/tsconfig": "^2.0.0", - "@types/chrome": "0.0.163", - "@types/firefox-webext-browser": "^94.0.0", + "@types/chrome": "0.0.193", + "@types/firefox-webext-browser": "^94.0.1", "@types/sinon-chrome": "^2.2.11", - "ava": "^3.15.0", - "sinon": "^12.0.1", + "ava": "^4.3.1", + "sinon": "^14.0.0", "sinon-chrome": "^3.0.1", - "tsd": "^0.18.0", - "typescript": "^4.4.4", - "xo": "^0.46.4" + "tsd": "^0.22.0", + "typescript": "^4.7.4", + "xo": "^0.50.0" } } diff --git a/test/_fixtures.js b/test/_fixtures.js deleted file mode 100644 index 81249d6..0000000 --- a/test/_fixtures.js +++ /dev/null @@ -1,5 +0,0 @@ -import chrome from 'sinon-chrome'; - -global.location = new URL('chrome://1234/_generated_background_page.html'); -global.chrome = chrome; -global.window = {}; diff --git a/test/_setup.js b/test/_setup.js new file mode 100644 index 0000000..4fb39cb --- /dev/null +++ b/test/_setup.js @@ -0,0 +1,5 @@ +import chrome from 'sinon-chrome'; + +globalThis.location = new URL('chrome://1234/_generated_background_page.html'); +globalThis.chrome = chrome; +globalThis.window = {}; diff --git a/test/index.js b/test/index.js index 42ba8cd..4e8a0dc 100644 --- a/test/index.js +++ b/test/index.js @@ -1,6 +1,5 @@ import test from 'ava'; import sinon from 'sinon'; -import './_fixtures.js'; import toMilliseconds from '@sindresorhus/to-milliseconds'; import cache from '../index.js'; @@ -171,7 +170,9 @@ test.serial('function() with fresh cache and staleWhileRevalidate', async t => { t.is(spy.callCount, 0); t.is(chrome.storage.local.set.callCount, 0); - await new Promise(resolve => setTimeout(resolve, 100)); + await new Promise(resolve => { + setTimeout(resolve, 100); + }); // Cache is still fresh, it should never be revalidated t.is(spy.callCount, 0); @@ -195,7 +196,9 @@ test.serial('function() with stale cache and staleWhileRevalidate', async t => { t.is(spy.callCount, 0, 'It shouldn’t be called yet'); - await new Promise(resolve => setTimeout(resolve, 100)); + await new Promise(resolve => { + setTimeout(resolve, 100); + }); t.is(spy.callCount, 1, 'It should be revalidated'); t.is(chrome.storage.local.set.callCount, 1);