Skip to content

Commit

Permalink
feat(javascript): add cache layer (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts authored Mar 23, 2022
1 parent 7c07d99 commit 2e7f208
Show file tree
Hide file tree
Showing 27 changed files with 820 additions and 69 deletions.
6 changes: 3 additions & 3 deletions .github/actions/cache/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ runs:
key: |
${{ env.CACHE_VERSION }}-${{
hashFiles(
'clients/algoliasearch-client-javascript/packages/client-common/**'
'clients/algoliasearch-client-javascript/packages/client-common/src/**'
)}}
- name: Restore built JavaScript node requester
Expand All @@ -164,7 +164,7 @@ runs:
key: |
${{ env.CACHE_VERSION }}-${{
hashFiles(
'clients/algoliasearch-client-javascript/packages/requester-node-http/**'
'clients/algoliasearch-client-javascript/packages/requester-node-http/src/**'
)}}
- name: Restore built JavaScript browser requester
Expand All @@ -175,7 +175,7 @@ runs:
key: |
${{ env.CACHE_VERSION }}-${{
hashFiles(
'clients/algoliasearch-client-javascript/packages/requester-browser-xhr/**'
'clients/algoliasearch-client-javascript/packages/requester-browser-xhr/src/**'
)}}
# Restore JavaScript clients: used during 'cts' or 'codegen'
Expand Down
5 changes: 5 additions & 0 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ runs:
echo "::set-output name=JS_CLIENT_CHANGED::$(git diff --shortstat $origin..HEAD -- clients/algoliasearch-client-javascript | wc -l)"
echo "::set-output name=JS_ALGOLIASEARCH_CHANGED::$(git diff --shortstat $origin..HEAD -- clients/algoliasearch-client-javascript/packages/algoliasearch clients/algoliasearch-client-javascript/packages/client-search clients/algoliasearch-client-javascript/packages/client-analytics clients/algoliasearch-client-javascript/packages/client-personalization | wc -l)"
echo "::set-output name=JS_COMMON_CHANGED::$(git diff --shortstat $origin..HEAD -- clients/algoliasearch-client-javascript/packages/client-common clients/algoliasearch-client-javascript/packages/requester-browser-xhr clients/algoliasearch-client-javascript/packages/requester-node-http | wc -l)"
echo "::set-output name=JS_COMMON_TESTS_CHANGED::$(git diff --shortstat $origin..HEAD -- clients/algoliasearch-client-javascript/packages/client-common/src/__tests__ | wc -l)"
echo "::set-output name=JS_TEMPLATE_CHANGED::$(git diff --shortstat $origin..HEAD -- templates/javascript | wc -l)"
echo "::set-output name=JAVA_CLIENT_CHANGED::$(git diff --shortstat $origin..HEAD -- clients/algoliasearch-client-java-2 | wc -l)"
Expand Down Expand Up @@ -208,6 +209,10 @@ outputs:
description: The generated `client-php` matrix
value: ${{ steps.php-matrix.outputs.MATRIX }}

RUN_JS_TESTS:
description: Determine if the `client_javascript_tests` job should run
value: ${{ steps.diff.outputs.JS_COMMON_TESTS_CHANGED > 0 }}

RUN_CTS:
description: Determine if the `cts` job should run
value: ${{
Expand Down
32 changes: 30 additions & 2 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:
RUN_JS: ${{ steps.setup.outputs.RUN_JS }}
RUN_JS_ALGOLIASEARCH: ${{ steps.setup.outputs.RUN_JS_ALGOLIASEARCH }}
RUN_JS_COMMON: ${{ steps.setup.outputs.RUN_JS_COMMON }}
RUN_JS_TESTS: ${{ steps.setup.outputs.RUN_JS_TESTS }}
JS_MATRIX: ${{ steps.setup.outputs.JS_MATRIX }}

RUN_JAVA: ${{ steps.setup.outputs.RUN_JAVA }}
Expand Down Expand Up @@ -119,7 +120,7 @@ jobs:
key: |
${{ env.CACHE_VERSION }}-${{
hashFiles(
format('clients/algoliasearch-client-javascript/packages/{0}/**', matrix.client)
format('clients/algoliasearch-client-javascript/packages/{0}/src/**', matrix.client)
)}}
- name: Build '${{ matrix.client }}' client
Expand Down Expand Up @@ -299,6 +300,28 @@ jobs:
if: steps.cache.outputs.cache-hit != 'true'
run: yarn cli build clients php ${{ matrix.client.name }}

client_javascript_tests:
runs-on: ubuntu-20.04
timeout-minutes: 10
needs:
- client_javascript
- client_javascript_algoliasearch
if: |
always() &&
needs.setup.outputs.RUN_JS_TESTS == 'true' &&
contains(needs.*.result, 'success') &&
!contains(needs.*.result, 'failure')
steps:
- uses: actions/checkout@v2

- name: Restore cache
uses: ./.github/actions/cache
with:
job: cts

- name: Run client-common tests
run: yarn workspace @experimental-api-clients-automation/client-common test

cts:
runs-on: ubuntu-20.04
timeout-minutes: 20
Expand All @@ -320,6 +343,9 @@ jobs:
with:
job: cts

- name: Check JavaScript client size
run: exit $(yarn workspace algoliasearch-client-javascript test:size | echo $?)

- name: Generate
run: yarn cli cts generate

Expand All @@ -334,7 +360,9 @@ jobs:
codegen:
runs-on: ubuntu-20.04
timeout-minutes: 10
needs: cts
needs:
- cts
- client_javascript_tests
if: |
always() &&
needs.setup.outputs.RUN_CODEGEN == 'true' &&
Expand Down
24 changes: 12 additions & 12 deletions clients/algoliasearch-client-javascript/bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,51 @@
"files": [
{
"path": "packages/algoliasearch/dist/algoliasearch.umd.browser.js",
"maxSize": "6.50KB"
"maxSize": "6.90KB"
},
{
"path": "packages/client-abtesting/dist/client-abtesting.umd.browser.js",
"maxSize": "3.25KB"
"maxSize": "3.65KB"
},
{
"path": "packages/client-analytics/dist/client-analytics.umd.browser.js",
"maxSize": "4.00KB"
"maxSize": "4.20KB"
},
{
"path": "packages/client-insights/dist/client-insights.umd.browser.js",
"maxSize": "3.25KB"
"maxSize": "3.45KB"
},
{
"path": "packages/client-personalization/dist/client-personalization.umd.browser.js",
"maxSize": "3.25KB"
"maxSize": "3.60KB"
},
{
"path": "packages/client-query-suggestions/dist/client-query-suggestions.umd.browser.js",
"maxSize": "3.25KB"
"maxSize": "3.65KB"
},
{
"path": "packages/client-search/dist/client-search.umd.browser.js",
"maxSize": "5.25KB"
"maxSize": "5.65KB"
},
{
"path": "packages/client-sources/dist/client-sources.umd.browser.js",
"maxSize": "3.25KB"
"maxSize": "3.50KB"
},
{
"path": "packages/recommend/dist/recommend.umd.browser.js",
"maxSize": "3.25KB"
"maxSize": "3.55KB"
},
{
"path": "packages/client-common/dist/client-common.esm.node.js",
"maxSize": "3.00KB"
"maxSize": "3.45KB"
},
{
"path": "packages/requester-browser-xhr/dist/requester-browser-xhr.esm.node.js",
"maxSize": "1.00KB"
"maxSize": "900B"
},
{
"path": "packages/requester-node-http/dist/requester-node-http.esm.node.js",
"maxSize": "1.00KB"
"maxSize": "1.10KB"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ import type {
Host,
Requester,
} from '@experimental-api-clients-automation/client-common';
import {
createMemoryCache,
createFallbackableCache,
createBrowserLocalStorageCache,
} from '@experimental-api-clients-automation/client-common';
import type {
PersonalizationApi,
Region as PersonalizationRegion,
} from '@experimental-api-clients-automation/client-personalization/src/personalizationApi';
import { createPersonalizationApi } from '@experimental-api-clients-automation/client-personalization/src/personalizationApi';
import { createSearchApi } from '@experimental-api-clients-automation/client-search/src/searchApi';
import {
createSearchApi,
apiClientVersion,
} from '@experimental-api-clients-automation/client-search/src/searchApi';
import { createXhrRequester } from '@experimental-api-clients-automation/requester-browser-xhr';

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
Expand All @@ -39,6 +47,14 @@ export function algoliasearch(
requester: options?.requester ?? createXhrRequester(),
userAgents: [{ segment: 'Browser' }],
authMode: 'WithinQueryParameters',
responsesCache: createMemoryCache(),
requestsCache: createMemoryCache({ serializable: false }),
hostsCache: createFallbackableCache({
caches: [
createBrowserLocalStorageCache({ key: `${apiClientVersion}-${appId}` }),
createMemoryCache(),
],
}),
...options,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import type {
Host,
Requester,
} from '@experimental-api-clients-automation/client-common';
import {
createMemoryCache,
createNullCache,
} from '@experimental-api-clients-automation/client-common';
import type {
PersonalizationApi,
Region as PersonalizationRegion,
Expand Down Expand Up @@ -38,6 +42,9 @@ export function algoliasearch(
},
requester: options?.requester ?? createHttpRequester(),
userAgents: [{ segment: 'Node.js', version: process.versions.node }],
responsesCache: createNullCache(),
requestsCache: createNullCache(),
hostsCache: createMemoryCache(),
...options,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from './src/createAuth';
export * from './src/createEchoRequester';
export * from './src/createMemoryCache';
export * from './src/cache';
export * from './src/createStatefulHost';
export * from './src/createTransporter';
export * from './src/createUserAgent';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { Config } from '@jest/types';

const baseConfig: Config.InitialOptions = {
preset: 'ts-jest',
roots: ['src/__tests__'],
};

const config: Config.InitialOptions = {
projects: [
{
...baseConfig,
testEnvironment: 'jsdom',
testPathIgnorePatterns: [
'src/__tests__/cache/null-cache.test.ts',
'src/__tests__/cache/memory-cache.test.ts',
],
},
{
...baseConfig,
testEnvironment: 'node',
testPathIgnorePatterns: [
'src/__tests__/cache/browser-local-storage-cache.test.ts',
'src/__tests__/cache/fallbackable-cache.test.ts',
],
},
],
};

export default config;
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
"module": "dist/client-common.esm.node.js",
"types": "dist/index.d.ts",
"scripts": {
"clean": "rm -rf dist/"
"clean": "rm -rf dist/",
"test": "jest"
},
"engines": {
"node": ">= 14.0.0"
},
"devDependencies": {
"@types/jest": "27.4.1",
"@types/node": "16.11.11",
"jest": "27.4.7",
"typescript": "4.5.4"
}
}
Loading

0 comments on commit 2e7f208

Please sign in to comment.