Skip to content

Commit 795463f

Browse files
initial import
1 parent 3b50360 commit 795463f

File tree

277 files changed

+18449
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

277 files changed

+18449
-2
lines changed

.gitignore

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.idea
2+
node_modules
3+
.DS_Store
4+
out
5+
reports
6+
coverage
7+
.nyc_output
8+
.out
9+
dist
10+
.env
11+
.aws-docker

.nycrc

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"sourceMap": true,
3+
"instrument": true,
4+
"reporter": [
5+
"html",
6+
"text"
7+
],
8+
"include": [
9+
"*/src/**/*.ts"
10+
],
11+
"extension": [
12+
".ts"
13+
],
14+
"exclude": [
15+
"**/*.spec.ts",
16+
"mocha.config.js",
17+
"test"
18+
]
19+
}

README.md

-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
# dandi
2-
# dandi
3-
# dandi

cache/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export * from './src/cache.key.generator';
2+
export * from './src/cache.provider';
3+
export * from './src/cascading.cache';
4+
export * from './src/memory.cache';
5+
export * from './src/service.context.cache.key.generator';

cache/npm.link

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
3+
npm link @dandi/core
4+
npm link @dandi/di-core

cache/package-lock.json

+35
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cache/package.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "@dandi/cache",
3+
"version": "1.0.0-alpha.10",
4+
"scripts": {},
5+
"repository": {
6+
"type": "git",
7+
"url": "git+https://github.com/dandi-mvc/dandi.git"
8+
},
9+
"author": "Daniel Schaffer <[email protected]>",
10+
"bugs": {
11+
"url": "https://github.com/dandi-mvc/dandi/issues"
12+
},
13+
"homepage": "https://github.com/dandi-mvc/dandi#readme",
14+
"devDependencies": {
15+
"@types/fs-extra": "^5.0.2",
16+
"luxon": "^1.2.1",
17+
"uuid": "^3.2.1"
18+
},
19+
"peerDependencies": {
20+
"@dandi/core": "1.0.0-alpha.10",
21+
"@dandi/di-core": "1.0.0-alpha.10"
22+
}
23+
}

cache/src/cache.key.generator.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { InjectionToken } from '@dandi/di-core';
2+
3+
import { localOpinionatedToken } from './local.token';
4+
5+
export interface CacheKeyGenerator {
6+
keyFor(...args: any[]): Symbol;
7+
}
8+
9+
export const CacheKeyGenerator: InjectionToken<CacheKeyGenerator> =
10+
localOpinionatedToken<CacheKeyGenerator>('CacheKeyGenerator', { multi: false });

cache/src/cache.provider.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Duration } from '@dandi/core';
2+
import { MappedInjectionToken } from '@dandi/di-core';
3+
4+
import { localOpinionatedToken } from './local.token';
5+
6+
export enum CacheProviderType {
7+
cascading = 'cascading',
8+
localMemory = 'localMemory',
9+
localService = 'localService',
10+
network = 'networkCache',
11+
remote = 'remoteCache',
12+
}
13+
14+
export interface Cache {
15+
16+
get<T>(key: Symbol): Promise<T>;
17+
set(key: Symbol, value: any, duration?: Duration): Promise<void>;
18+
delete(key: Symbol): Promise<boolean>;
19+
20+
}
21+
22+
const tokens = new Map<CacheProviderType, MappedInjectionToken<CacheProviderType, Cache>>();
23+
24+
export const Cache = CacheProvider(CacheProviderType.cascading);
25+
26+
export function CacheProvider(type: CacheProviderType) {
27+
let token: MappedInjectionToken<CacheProviderType, Cache> = tokens.get(type);
28+
if (!token) {
29+
token = {
30+
provide: localOpinionatedToken<Cache>(`CacheProvider:${type}`, { multi: false }),
31+
key: type,
32+
};
33+
tokens.set(type, token);
34+
}
35+
return token;
36+
}
37+

cache/src/cascading.cache.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Duration } from '@dandi/core';
2+
import { Inject, Injectable, Optional } from '@dandi/di-core';
3+
4+
import { Cache, CacheProvider, CacheProviderType } from './cache.provider';
5+
6+
@Injectable(CacheProvider(CacheProviderType.cascading))
7+
export class CascadingCache implements Cache {
8+
9+
private readonly caches: Cache[];
10+
private local: Cache;
11+
12+
constructor(
13+
@Inject(CacheProvider(CacheProviderType.localMemory)) @Optional() localMem: Cache,
14+
@Inject(CacheProvider(CacheProviderType.localService)) @Optional() localSvc: Cache,
15+
@Inject(CacheProvider(CacheProviderType.network)) @Optional() network: Cache,
16+
@Inject(CacheProvider(CacheProviderType.remote)) @Optional() remote: Cache,
17+
) {
18+
this.caches = [
19+
localMem,
20+
localSvc,
21+
network,
22+
remote
23+
].filter(cache => cache);
24+
this.local = this.caches[0];
25+
}
26+
27+
public async get<T>(key: Symbol): Promise<T> {
28+
for (let cache of this.caches) {
29+
let result = await cache.get(key);
30+
if (result) {
31+
return result as T;
32+
}
33+
}
34+
}
35+
36+
public async set(key: any, value: any, duration?: Duration): Promise<void> {
37+
return await this.local.set(key, value, duration);
38+
}
39+
40+
public async delete(key: any): Promise<boolean> {
41+
return await this.local.delete(key);
42+
}
43+
44+
}

cache/src/local.token.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { InjectionOptions, InjectionToken, OpinionatedToken, SymbolToken } from '@dandi/di-core';
2+
3+
const PKG = '@dandi/cache';
4+
5+
export function localSymbolToken<T>(target: string): InjectionToken<T> {
6+
return SymbolToken.local<T>(PKG, target);
7+
}
8+
export function localOpinionatedToken<T>(target: string, options: InjectionOptions): InjectionToken<T> {
9+
return OpinionatedToken.local<T>(PKG, target, options);
10+
}

cache/src/memory.cache.ts

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { Duration } from '@dandi/core';
2+
import { Injectable, Singleton } from '@dandi/di-core';
3+
4+
import { Cache, CacheProvider, CacheProviderType } from './cache.provider';
5+
import Timer = NodeJS.Timer;
6+
7+
@Injectable(CacheProvider(CacheProviderType.localMemory), Singleton)
8+
export class MemoryCache implements Cache {
9+
10+
private map = new Map<Symbol, any>();
11+
private timeouts = new Map<Symbol, Timer>();
12+
13+
constructor() {
14+
}
15+
16+
private clearTimeout(key: Symbol): void {
17+
const existingTimeout = this.timeouts.get(key);
18+
if (existingTimeout) {
19+
clearTimeout(existingTimeout);
20+
this.timeouts.delete(key);
21+
}
22+
}
23+
24+
public get<T>(key: Symbol): Promise<T> {
25+
return Promise.resolve(this.map.get(key));
26+
}
27+
28+
public set(key: Symbol, value: any, duration?: Duration): Promise<void> {
29+
this.clearTimeout(key);
30+
this.map.set(key, value);
31+
if (duration) {
32+
this.timeouts.set(key, setTimeout(() => {
33+
this.map.delete(key);
34+
this.timeouts.delete(key);
35+
}, duration.as('milliseconds')));
36+
}
37+
return Promise.resolve();
38+
}
39+
40+
public delete(key: Symbol): Promise<boolean> {
41+
this.clearTimeout(key);
42+
return Promise.resolve(this.map.delete(key));
43+
}
44+
45+
public clear(): Promise<number> {
46+
this.timeouts.forEach(timer => clearTimeout(timer));
47+
const count = this.map.size;
48+
this.map.clear();
49+
this.timeouts.clear();
50+
return Promise.resolve(count);
51+
}
52+
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Inject, Injectable, InjectionContext, InjectionToken } from '@dandi/di-core';
2+
3+
import { CacheKeyGenerator } from './cache.key.generator';
4+
5+
const keys = new Map<string, Symbol>();
6+
7+
@Injectable(CacheKeyGenerator)
8+
export class ServiceContextCacheKeyGenerator implements CacheKeyGenerator {
9+
10+
constructor(
11+
@Inject(InjectionContext) private context: InjectionToken<any>,
12+
) {}
13+
14+
public keyFor(...args: any[]): Symbol {
15+
const contextTag = `[${typeof this.context === 'function' ? this.context.name : this.context}]`;
16+
args.unshift(contextTag);
17+
const keyStr = args.map(arg => arg.toString()).join(':');
18+
let key = keys.get(keyStr);
19+
if (!key) {
20+
key = Symbol(keyStr);
21+
keys.set(keyStr, key);
22+
}
23+
return key;
24+
}
25+
26+
}

cache/tsconfig.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"files": [
4+
"index.ts"
5+
]
6+
}

cleanup.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
4+
for dir in {'core','di-core','model','model-validation','config','config-aws-ssm','data','data-pg','cache','mvc','mvc-express','mvc-auth-firebase'}
5+
do
6+
npm unpublish "@dandi/${dir%*/}@1.0.0-alpha.8"
7+
npm unpublish "@dandi/${dir%*/}@1.0.0-alpha.9"
8+
9+
done

config-aws-ssm/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './src/aws.ssm.config.client';
2+
export * from './src/aws.ssm.config.module';

config-aws-ssm/npm.link

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
npm link @dandi/config
4+
npm link @dandi/core
5+
npm link @dandi/di-core

0 commit comments

Comments
 (0)