@potonz/shortlinks-manager 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +0 -0
- package/README.md +0 -0
- package/dist/index.d.ts +31 -2
- package/dist/index.js +1 -1
- package/package.json +3 -2
package/LICENSE
CHANGED
|
File without changes
|
package/README.md
CHANGED
|
File without changes
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
// Generated by dts-bundle-generator v9.5.1
|
|
2
2
|
|
|
3
|
+
export interface ICache {
|
|
4
|
+
init?: () => unknown | Promise<unknown>;
|
|
5
|
+
/**
|
|
6
|
+
* Get the target URL using the provided shortId
|
|
7
|
+
* @param shortId
|
|
8
|
+
* @returns string if a target URL is found, null otherwise
|
|
9
|
+
*/
|
|
10
|
+
get: (shortId: string) => string | null | Promise<string | null>;
|
|
11
|
+
/**
|
|
12
|
+
* Cache the target URL
|
|
13
|
+
* @param shortId
|
|
14
|
+
* @param targetUrl
|
|
15
|
+
*/
|
|
16
|
+
set: (shortId: string, targetUrl: string) => void | Promise<void>;
|
|
17
|
+
}
|
|
3
18
|
export interface IShortLinksManagerBackend {
|
|
4
19
|
/**
|
|
5
20
|
* Initialise any logic before the manager can do its thing. E.g. setting up tables.
|
|
@@ -36,6 +51,12 @@ export interface IShortLinksManagerBackend {
|
|
|
36
51
|
}
|
|
37
52
|
export interface IManagerProps {
|
|
38
53
|
backend: IShortLinksManagerBackend;
|
|
54
|
+
/**
|
|
55
|
+
* A list of cache to use before invoking the backend.
|
|
56
|
+
* If multiple cache are provided, the manager will try from first to last.
|
|
57
|
+
* Default to no cache
|
|
58
|
+
*/
|
|
59
|
+
caches?: ICache[];
|
|
39
60
|
shortIdLength: number;
|
|
40
61
|
onShortIdLengthUpdated: (newLength: number) => unknown;
|
|
41
62
|
}
|
|
@@ -51,15 +72,23 @@ export interface IShortLinksManager {
|
|
|
51
72
|
* Get a target URL from the given short ID
|
|
52
73
|
* @param shortId
|
|
53
74
|
* @returns the target URL as string or null if not found
|
|
54
|
-
* @throws Error if failed
|
|
75
|
+
* @throws Error if backend failed
|
|
55
76
|
*/
|
|
56
77
|
getTargetUrl(shortId: string): Promise<string | null>;
|
|
78
|
+
/**
|
|
79
|
+
* Update last accessed time to avoid link being cleaned
|
|
80
|
+
* @param shortId
|
|
81
|
+
* @param time last accessed time. Defaults to current time
|
|
82
|
+
* @throws Error if backend failed
|
|
83
|
+
*/
|
|
84
|
+
updateShortLinkLastAccessTime(shortId: string, time: Date): Promise<void>;
|
|
57
85
|
/**
|
|
58
86
|
* Clean up unused links that are older than the given maxAge
|
|
59
87
|
* @param maxAge number of days the record should be kept
|
|
88
|
+
* @throws Error if backend failed
|
|
60
89
|
*/
|
|
61
90
|
cleanUnusedLinks(maxAge: number): Promise<void>;
|
|
62
91
|
}
|
|
63
|
-
export declare function createManager({ backend, shortIdLength, onShortIdLengthUpdated }: IManagerProps): Promise<IShortLinksManager>;
|
|
92
|
+
export declare function createManager({ backend, caches, shortIdLength, onShortIdLengthUpdated }: IManagerProps): Promise<IShortLinksManager>;
|
|
64
93
|
|
|
65
94
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -20,4 +20,4 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
20
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
SOFTWARE.
|
|
22
22
|
*/
|
|
23
|
-
function
|
|
23
|
+
function C(f=4){let E="";for(let m=0;m<f;m++)E+="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(Math.floor(Math.random()*62));return E}function p(f,E){let m=new Set,O=0;while(m.size<f&&O<f*100)m.add(C(E)),O++;return Array.from(m)}async function y({backend:f,caches:E=[],shortIdLength:m,onShortIdLengthUpdated:O}){return await f.init?.(),{async createShortLink(D){let A="";for(let H=0;H<3;H++){let R=p(50,m),w=await f.checkShortIdsExist(R),W=R.find((x)=>!w.includes(x));if(!W)++m,await O(m);else{A=W;break}}if(!A)throw Error("Unable to create a shortlink, potentially ran out");return await f.createShortLink(A,D),A},async getTargetUrl(D){let A=null;for(let H of E)if(await H.init?.(),!A)A=await H.get(D);if(!A)A=await f.getTargetUrl(D);if(A){await f.updateShortLinkLastAccessTime(D);for(let H of E)await H.set(D,A)}return A},async updateShortLinkLastAccessTime(D){return await f.updateShortLinkLastAccessTime(D)},async cleanUnusedLinks(D){await f.cleanUnusedLinks(D)}}}export{y as createManager};
|
package/package.json
CHANGED
|
@@ -8,9 +8,10 @@
|
|
|
8
8
|
"homepage": "https://github.com/potonz/shortlinks-manager",
|
|
9
9
|
"repository": {
|
|
10
10
|
"url": "git+https://github.com/potonz/shortlinks-manager.git",
|
|
11
|
-
"type": "git"
|
|
11
|
+
"type": "git",
|
|
12
|
+
"directory": "packages/shortlinks-manager"
|
|
12
13
|
},
|
|
13
|
-
"version": "0.
|
|
14
|
+
"version": "0.2.0",
|
|
14
15
|
"type": "module",
|
|
15
16
|
"license": "MIT",
|
|
16
17
|
"main": "./dist/index.js",
|