Skip to content

Commit

Permalink
Integrate rollup (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad authored Apr 9, 2023
1 parent d18df11 commit 955eae9
Show file tree
Hide file tree
Showing 44 changed files with 163 additions and 86 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
"plugin:prettier/recommended",
"plugin:import/recommended",
"plugin:import/typescript"
],
"plugins": [
"@typescript-eslint",
Expand Down Expand Up @@ -31,6 +33,7 @@
],
"no-var": "off",
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/consistent-type-imports": "warn",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/indent": "off",
Expand Down
2 changes: 1 addition & 1 deletion lib/AbstractGroupedOperation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AbstractOperation } from './AbstractOperation'
import { GroupedCache } from './types/DataSources'
import type { GroupedCache } from './types/DataSources'

export abstract class AbstractGroupedOperation<LoadedValue, ResolveParams = undefined> extends AbstractOperation<
LoadedValue,
Expand Down
10 changes: 6 additions & 4 deletions lib/AbstractOperation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { InMemoryCache, InMemoryCacheConfiguration, NoopCache } from './memory'
import { SynchronousGroupedCache } from './types/SyncDataSources'
import { Cache } from './types/DataSources'
import { defaultLogger, Logger } from './Logger'
import type { InMemoryCacheConfiguration } from './memory'
import { InMemoryCache, NoopCache } from './memory'
import type { SynchronousGroupedCache } from './types/SyncDataSources'
import type { Cache } from './types/DataSources'
import type { Logger } from './Logger'
import { defaultLogger } from './Logger'

export type LoaderErrorHandler = (
err: Error,
Expand Down
4 changes: 2 additions & 2 deletions lib/GroupedCachingOperation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GroupedCache } from './types/DataSources'
import { CommonOperationConfig } from './AbstractOperation'
import type { GroupedCache } from './types/DataSources'
import type { CommonOperationConfig } from './AbstractOperation'
import { AbstractGroupedOperation } from './AbstractGroupedOperation'

export type GroupedCachingOperationConfig<LoadedValue> = CommonOperationConfig<LoadedValue, GroupedCache<LoadedValue>>
Expand Down
4 changes: 2 additions & 2 deletions lib/GroupedLoadingOperation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GroupedCache, GroupLoader } from './types/DataSources'
import { LoadingOperationConfig } from './LoadingOperation'
import type { GroupedCache, GroupLoader } from './types/DataSources'
import type { LoadingOperationConfig } from './LoadingOperation'
import { AbstractGroupedOperation } from './AbstractGroupedOperation'

export type GroupedLoadingOperationConfig<LoadedValue, LoaderParams = undefined> = LoadingOperationConfig<
Expand Down
4 changes: 2 additions & 2 deletions lib/LoadingOperation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommonOperationConfig } from './AbstractOperation'
import { Cache, Loader } from './types/DataSources'
import type { CommonOperationConfig } from './AbstractOperation'
import type { Cache, Loader } from './types/DataSources'
import { AbstractFlatOperation } from './AbstractFlatOperation'

export type LoadingOperationConfig<
Expand Down
41 changes: 22 additions & 19 deletions lib/memory/InMemoryCache.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { fifo, fifoObject, lru, lruObject, ToadCache } from 'toad-cache'
import { SynchronousCache, SynchronousGroupedCache } from '../types/SyncDataSources'
import { CacheConfiguration } from '../types/DataSources'
import type { CacheConstructor, ToadCache } from 'toad-cache'
import { FifoMap, FifoObject, LruMap, LruObject } from 'toad-cache'
import type { SynchronousCache, SynchronousGroupedCache } from '../types/SyncDataSources'
import type { CacheConfiguration } from '../types/DataSources'

type CacheTypeId = 'lru-map' | 'fifo-map' | 'lru-object' | 'fifo-object'

export interface InMemoryCacheConfiguration extends CacheConfiguration {
cacheType?: 'lru' | 'fifo' | 'lru-object' | 'fifo-object'
groupCacheType?: 'lru' | 'fifo' | 'lru-object' | 'fifo-object'
cacheType?: CacheTypeId
groupCacheType?: CacheTypeId
maxItems?: number
maxGroups?: number
maxItemsPerGroup?: number
Expand All @@ -25,31 +28,31 @@ export class InMemoryCache<T> implements SynchronousCache<T>, SynchronousGrouped
name = 'In-memory cache'
private readonly ttlInMsecs: number | undefined
public readonly ttlLeftBeforeRefreshInMsecs?: number
private readonly cacheConstructor: <T = any>(max?: number, ttl?: number) => ToadCache<T>
private readonly groupCacheConstructor: <T = any>(max?: number, ttl?: number) => ToadCache<T>
private readonly cacheConstructor: CacheConstructor<ToadCache<T>>
private readonly groupCacheConstructor: CacheConstructor<ToadCache<ToadCache<T | null>>>

constructor(config: InMemoryCacheConfiguration) {
this.cacheConstructor = this.resolveCacheConstructor(config.cacheType ?? DEFAULT_CONFIGURATION.cacheType)
this.groupCacheConstructor = this.resolveCacheConstructor(
this.cacheConstructor = this.resolveCacheConstructor<T>(config.cacheType ?? DEFAULT_CONFIGURATION.cacheType)
this.groupCacheConstructor = this.resolveCacheConstructor<ToadCache<T | null>>(
config.groupCacheType ?? DEFAULT_CONFIGURATION.groupCacheType
)

this.cache = this.cacheConstructor(config.maxItems ?? DEFAULT_CONFIGURATION.maxItems, config.ttlInMsecs ?? 0)
this.groups = this.groupCacheConstructor(config.maxGroups ?? DEFAULT_CONFIGURATION.maxGroups)
this.cache = new this.cacheConstructor(config.maxItems ?? DEFAULT_CONFIGURATION.maxItems, config.ttlInMsecs ?? 0)
this.groups = new this.groupCacheConstructor(config.maxGroups ?? DEFAULT_CONFIGURATION.maxGroups)
this.maxItemsPerGroup = config.maxItemsPerGroup ?? DEFAULT_CONFIGURATION.maxItemsPerGroup
this.ttlInMsecs = config.ttlInMsecs
this.ttlLeftBeforeRefreshInMsecs = config.ttlLeftBeforeRefreshInMsecs
}

private resolveCacheConstructor(cacheTypeId: 'lru' | 'fifo' | 'lru-object' | 'fifo-object') {
if (cacheTypeId === 'fifo') {
return fifo
} else if (cacheTypeId === 'lru') {
return lru
private resolveCacheConstructor<T>(cacheTypeId: CacheTypeId): CacheConstructor<ToadCache<T>> {
if (cacheTypeId === 'fifo-map') {
return FifoMap
} else if (cacheTypeId === 'lru-map') {
return LruMap
} else if (cacheTypeId === 'fifo-object') {
return fifoObject
return FifoObject
} else {
return lruObject
return LruObject
}
}

Expand All @@ -59,7 +62,7 @@ export class InMemoryCache<T> implements SynchronousCache<T>, SynchronousGrouped
return groupCache
}

const newGroupCache = this.cacheConstructor(this.maxItemsPerGroup, this.ttlInMsecs)
const newGroupCache = new this.cacheConstructor(this.maxItemsPerGroup, this.ttlInMsecs)
this.groups.set(groupId, newGroupCache)
return newGroupCache
}
Expand Down
2 changes: 1 addition & 1 deletion lib/memory/NoopCache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SynchronousCache, SynchronousGroupedCache } from '../types/SyncDataSources'
import type { SynchronousCache, SynchronousGroupedCache } from '../types/SyncDataSources'

export class NoopCache<T> implements SynchronousCache<T>, SynchronousGroupedCache<T> {
name = 'Noop cache'
Expand Down
2 changes: 1 addition & 1 deletion lib/redis/RedisCache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Cache, CacheConfiguration, GroupedCache, Loader } from '../types/DataSources'
import type { Cache, CacheConfiguration, GroupedCache, Loader } from '../types/DataSources'
import type { Redis } from 'ioredis'
import { RedisTimeoutError } from './RedisTimeoutError'
import { GET_OR_SET_ZERO_WITH_TTL, GET_OR_SET_ZERO_WITHOUT_TTL } from './lua'
Expand Down
2 changes: 1 addition & 1 deletion lib/redis/RedisExpirationTimeGroupedLoader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GroupedCache, GroupLoader } from '../types/DataSources'
import type { GroupedCache, GroupLoader } from '../types/DataSources'

export class RedisExpirationTimeGroupedLoader implements GroupLoader<number> {
public readonly name = 'RedisExpirationTimeGroupedLoader'
Expand Down
2 changes: 1 addition & 1 deletion lib/redis/RedisExpirationTimeLoader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Cache, Loader } from '../types/DataSources'
import type { Cache, Loader } from '../types/DataSources'

export class RedisExpirationTimeLoader implements Loader<number> {
public readonly name = 'RedisExpirationTimeLoader'
Expand Down
4 changes: 2 additions & 2 deletions lib/types/DataSources.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LoadingOperation } from '../LoadingOperation'
import { GroupedLoadingOperation } from '../GroupedLoadingOperation'
import type { LoadingOperation } from '../LoadingOperation'
import type { GroupedLoadingOperation } from '../GroupedLoadingOperation'

export interface CacheConfiguration {
ttlLeftBeforeRefreshInMsecs?: number
Expand Down
16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
"email": "[email protected]"
}
],
"main": "dist/index.js",
"main": "dist/layered-loader",
"exports": {
"import": "./dist/layered-loader.js",
"require": "./dist/layered-loader.cjs"
},
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"build:release": "del-cli dist && del-cli coverage && npm run lint && npm run build:rollup",
"build:rollup": "rollup --config",
"docker:start": "docker-compose -f docker-compose.yml up --build -d redis && docker-compose -f docker-compose.yml up --build -d wait_for_redis",
"docker:stop": "docker-compose -f docker-compose.yml down",
"test": "jest --runInBand",
Expand All @@ -21,7 +27,7 @@
"lint": "eslint \"lib/**/*.ts\" \"test/**/*.ts\"",
"lint:fix": "eslint --fix \"lib/**/*.ts\" \"test/**/*.ts\"",
"prettier": "prettier --write \"{lib,test}/**/*.{js,ts}\" index.ts",
"prepublishOnly": "npm run build"
"prepublishOnly": "npm run build:release"
},
"engines": {
"node": ">=16"
Expand All @@ -47,13 +53,16 @@
],
"homepage": "https://github.com/kibertoad/layered-loader",
"dependencies": {
"toad-cache": "^2.1.0"
"toad-cache": "^3.0.0"
},
"devDependencies": {
"@rollup/plugin-terser": "^0.4.0",
"@rollup/plugin-typescript": "^11.1.0",
"@types/jest": "^29.5.0",
"@types/node": "^18.15.11",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.57.0",
"del-cli": "^5.0.0",
"eslint": "^8.37.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-import": "^2.27.5",
Expand All @@ -62,6 +71,7 @@
"jest": "29.5.0",
"prettier": "^2.8.7",
"rfdc": "^1.3.0",
"rollup": "^3.20.2",
"ts-jest": "29.1.0",
"typescript": "5.0.3"
},
Expand Down
56 changes: 56 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import terser from '@rollup/plugin-terser'
import typescript from '@rollup/plugin-typescript'
import { createRequire } from 'node:module'
const require = createRequire(import.meta.url)
const pkg = require('./package.json')
const year = new Date().getFullYear()
const bannerLong = `/**
* ${pkg.name}
*
* @copyright ${year} ${pkg.author}
* @license ${pkg.license}
* @version ${pkg.version}
*/`
const bannerShort = `/*!
${year} ${pkg.author}
@version ${pkg.version}
*/`
const defaultOutBase = { compact: true, banner: bannerLong, name: pkg.name }
const cjOutBase = { ...defaultOutBase, compact: false, format: 'cjs', exports: 'named' }
const esmOutBase = { ...defaultOutBase, format: 'esm' }
const umdOutBase = { ...defaultOutBase, format: 'umd' }
const minOutBase = { banner: bannerShort, name: pkg.name, plugins: [terser()], sourcemap: true }

export default [
{
input: './index.ts',
output: [
{
...cjOutBase,
file: `dist/${pkg.name}.cjs`,
},
{
...esmOutBase,
file: `dist/${pkg.name}.js`,
},
{
...esmOutBase,
...minOutBase,
file: `dist/${pkg.name}.min.js`,
},
{
...umdOutBase,
file: `dist/${pkg.name}.umd.js`,
name: 'layered-loader',
},
{
...umdOutBase,
...minOutBase,
file: `dist/${pkg.name}.umd.min.js`,
name: 'layered-loader',
},
],
plugins: [typescript()],
external: ['toad-cache']
},
]
2 changes: 1 addition & 1 deletion test/CachingOperation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InMemoryCacheConfiguration } from '../lib/memory/InMemoryCache'
import type { InMemoryCacheConfiguration } from '../lib/memory/InMemoryCache'
import { ThrowingCache } from './fakes/ThrowingCache'
import { CachingOperation } from '../lib/CachingOperation'
import { DummyCache } from './fakes/DummyCache'
Expand Down
4 changes: 2 additions & 2 deletions test/GroupedCachingOperation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { GroupedCachingOperation } from '../lib/GroupedCachingOperation'
import { ThrowingGroupedCache } from './fakes/ThrowingGroupedCache'
import { User } from './types/testTypes'
import type { User } from './types/testTypes'
import { RedisCache } from '../lib/redis'
import Redis from 'ioredis'
import { redisOptions } from './fakes/TestRedisConfig'
import { DummyGroupedCache } from './fakes/DummyGroupedCache'
import { CountingGroupedCache } from './fakes/CountingGroupedCache'
import { InMemoryCacheConfiguration } from '../lib/memory'
import type { InMemoryCacheConfiguration } from '../lib/memory'
import { TemporaryThrowingGroupedCache } from './fakes/TemporaryThrowingGroupedCache'

const redisCacheConfig = { json: true, ttlInMsecs: 99999, prefix: 'users' }
Expand Down
2 changes: 1 addition & 1 deletion test/GroupedLoadingOperation-async-refresh.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { User } from './types/testTypes'
import type { User } from './types/testTypes'
import { GroupedLoadingOperation } from '../lib/GroupedLoadingOperation'
import { CountingGroupedLoader } from './fakes/CountingGroupedLoader'
import { setTimeout } from 'timers/promises'
Expand Down
6 changes: 3 additions & 3 deletions test/GroupedLoadingOperation-main.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { InMemoryCacheConfiguration } from '../lib/memory/InMemoryCache'
import { User } from './types/testTypes'
import type { InMemoryCacheConfiguration } from '../lib/memory/InMemoryCache'
import type { User } from './types/testTypes'
import { GroupedLoadingOperation } from '../lib/GroupedLoadingOperation'
import { DummyGroupedCache } from './fakes/DummyGroupedCache'
import { ThrowingGroupedLoader } from './fakes/ThrowingGroupedLoader'
import { DummyGroupedLoader } from './fakes/DummyGroupedLoader'
import { TemporaryThrowingGroupedLoader } from './fakes/TemporaryThrowingGroupedLoader'
import { ThrowingGroupedCache } from './fakes/ThrowingGroupedCache'
import { CountingGroupedLoader } from './fakes/CountingGroupedLoader'
import { DummyLoaderParams } from './fakes/DummyLoaderWithParams'
import type { DummyLoaderParams } from './fakes/DummyLoaderWithParams'
import { DummyGroupedLoaderWithParams } from './fakes/DummyGroupedLoaderWithParams'
import { setTimeout } from 'timers/promises'

Expand Down
7 changes: 4 additions & 3 deletions test/InMemoryCache.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { InMemoryCache, InMemoryCacheConfiguration } from '../lib/memory/InMemoryCache'
import type { InMemoryCacheConfiguration } from '../lib/memory/InMemoryCache'
import { InMemoryCache } from '../lib/memory/InMemoryCache'
import { setTimeout } from 'timers/promises'

const IN_MEMORY_CACHE_CONFIG = { ttlInMsecs: 999 } satisfies InMemoryCacheConfiguration
Expand All @@ -7,7 +8,7 @@ describe('InMemoryCache', () => {
describe('set', () => {
it('expires LRU', () => {
const cache = new InMemoryCache({
cacheType: 'lru',
cacheType: 'lru-map',
maxItems: 2,
ttlInMsecs: 1,
})
Expand Down Expand Up @@ -49,7 +50,7 @@ describe('InMemoryCache', () => {

it('expires FIFO', () => {
const cache = new InMemoryCache({
cacheType: 'fifo',
cacheType: 'fifo-map',
maxItems: 2,
ttlInMsecs: 1,
})
Expand Down
5 changes: 3 additions & 2 deletions test/LoadingOperation-main.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { setTimeout } from 'timers/promises'
import { LoadingOperation } from '../lib/LoadingOperation'
import { InMemoryCacheConfiguration } from '../lib/memory/InMemoryCache'
import type { InMemoryCacheConfiguration } from '../lib/memory/InMemoryCache'
import { DummyLoader } from './fakes/DummyLoader'
import { CountingLoader } from './fakes/CountingLoader'
import { ThrowingLoader } from './fakes/ThrowingLoader'
import { ThrowingCache } from './fakes/ThrowingCache'
import { TemporaryThrowingLoader } from './fakes/TemporaryThrowingLoader'
import { DummyCache } from './fakes/DummyCache'
import { DummyLoaderParams, DummyLoaderWithParams } from './fakes/DummyLoaderWithParams'
import type { DummyLoaderParams } from './fakes/DummyLoaderWithParams'
import { DummyLoaderWithParams } from './fakes/DummyLoaderWithParams'

const IN_MEMORY_CACHE_CONFIG = { ttlInMsecs: 999 } satisfies InMemoryCacheConfiguration

Expand Down
2 changes: 1 addition & 1 deletion test/RedisCache.timeout.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Redis from 'ioredis'
import type Redis from 'ioredis'
import { RedisCache } from '../lib/redis/RedisCache'
import { FakeRedis } from './fakes/FakeRedis'

Expand Down
2 changes: 1 addition & 1 deletion test/fakes/CountingCache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Cache } from '../../lib/types/DataSources'
import type { Cache } from '../../lib/types/DataSources'

export class CountingCache implements Cache<string> {
private value: string | undefined
Expand Down
4 changes: 2 additions & 2 deletions test/fakes/CountingGroupedCache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GroupedCache } from '../../lib/types/DataSources'
import { GroupValues, User } from '../types/testTypes'
import type { GroupedCache } from '../../lib/types/DataSources'
import type { GroupValues, User } from '../types/testTypes'
import { cloneDeep } from '../utils/cloneUtils'

export class CountingGroupedCache implements GroupedCache<User> {
Expand Down
4 changes: 2 additions & 2 deletions test/fakes/CountingGroupedLoader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GroupLoader } from '../../lib/types/DataSources'
import { GroupValues, User } from '../types/testTypes'
import type { GroupLoader } from '../../lib/types/DataSources'
import type { GroupValues, User } from '../types/testTypes'
import { cloneDeep } from '../utils/cloneUtils'

export class CountingGroupedLoader implements GroupLoader<User> {
Expand Down
2 changes: 1 addition & 1 deletion test/fakes/CountingLoader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Loader } from '../../lib/types/DataSources'
import type { Loader } from '../../lib/types/DataSources'

export class CountingLoader implements Loader<string> {
public value: string | undefined
Expand Down
Loading

0 comments on commit 955eae9

Please sign in to comment.