@potonz/shortlinks-manager 0.2.2 → 0.2.3
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/dist/index.d.ts +27 -9
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -2,19 +2,24 @@
|
|
|
2
2
|
|
|
3
3
|
export interface ICache {
|
|
4
4
|
initialised?: boolean;
|
|
5
|
-
init?: () => unknown | Promise<unknown
|
|
5
|
+
init?: () => (unknown | Promise<unknown>);
|
|
6
6
|
/**
|
|
7
7
|
* Get the target URL using the provided shortId
|
|
8
8
|
* @param shortId
|
|
9
9
|
* @returns string if a target URL is found, null otherwise
|
|
10
10
|
*/
|
|
11
|
-
get: (shortId: string) => string | null | Promise<string | null
|
|
11
|
+
get: (shortId: string) => (string | null | Promise<string | null>);
|
|
12
12
|
/**
|
|
13
13
|
* Cache the target URL
|
|
14
14
|
* @param shortId
|
|
15
15
|
* @param targetUrl
|
|
16
16
|
*/
|
|
17
|
-
set: (shortId: string, targetUrl: string) => void | Promise<void
|
|
17
|
+
set: (shortId: string, targetUrl: string) => (void | Promise<void>);
|
|
18
|
+
/**
|
|
19
|
+
* Delete the short ID in the cache
|
|
20
|
+
* @param shortId
|
|
21
|
+
*/
|
|
22
|
+
delete?: (shortId: string) => (void | Promise<void>);
|
|
18
23
|
}
|
|
19
24
|
export interface IShortLinksManagerBackend {
|
|
20
25
|
/**
|
|
@@ -27,28 +32,35 @@ export interface IShortLinksManagerBackend {
|
|
|
27
32
|
* @param {string} shortId
|
|
28
33
|
* @returns the short ID or null if not found
|
|
29
34
|
*/
|
|
30
|
-
getTargetUrl
|
|
35
|
+
getTargetUrl(shortId: string): string | null | Promise<string | null>;
|
|
31
36
|
/**
|
|
32
37
|
* Create a short link map with the given short ID and target URL
|
|
33
38
|
* @param {string} shortId
|
|
34
39
|
* @param {string} targetUrl
|
|
35
40
|
*/
|
|
36
|
-
createShortLink
|
|
41
|
+
createShortLink(shortId: string, targetUrl: string): void | Promise<void>;
|
|
37
42
|
/**
|
|
38
43
|
* Check the provided list of short IDs and return the ones that already exist.
|
|
39
44
|
* @param {string[]} shortIds
|
|
40
45
|
*/
|
|
41
|
-
checkShortIdsExist
|
|
46
|
+
checkShortIdsExist(shortIds: string[]): string[] | Promise<string[]>;
|
|
42
47
|
/**
|
|
43
48
|
* Update last accessed time to current timestamp
|
|
44
49
|
* @param shortId
|
|
50
|
+
* @param time Unix timestamp or a Date object
|
|
45
51
|
*/
|
|
46
|
-
updateShortLinkLastAccessTime(shortId: string): void | Promise<void>;
|
|
52
|
+
updateShortLinkLastAccessTime(shortId: string, time?: number | Date): void | Promise<void>;
|
|
47
53
|
/**
|
|
48
54
|
* Remove unused links that are older than the given maxAge
|
|
49
55
|
* @param maxAge number of days the record should be kept
|
|
56
|
+
* @returns an array of short IDs that have been cleaned
|
|
57
|
+
*/
|
|
58
|
+
cleanUnusedLinks(maxAge: number): string[] | Promise<string[]>;
|
|
59
|
+
/**
|
|
60
|
+
* Remove a short link by its ID
|
|
61
|
+
* @param shortId the short ID to remove
|
|
50
62
|
*/
|
|
51
|
-
|
|
63
|
+
removeShortLink(shortId: string): void | Promise<void>;
|
|
52
64
|
}
|
|
53
65
|
export interface IManagerProps {
|
|
54
66
|
backend: IShortLinksManagerBackend;
|
|
@@ -103,13 +115,19 @@ export interface IShortLinksManager {
|
|
|
103
115
|
* @param time last accessed time. Defaults to current time
|
|
104
116
|
* @throws Error if backend failed
|
|
105
117
|
*/
|
|
106
|
-
updateShortLinkLastAccessTime(shortId: string, time
|
|
118
|
+
updateShortLinkLastAccessTime(shortId: string, time?: number | Date): Promise<void>;
|
|
107
119
|
/**
|
|
108
120
|
* Clean up unused links that are older than the given maxAge
|
|
109
121
|
* @param maxAge number of days the record should be kept
|
|
110
122
|
* @throws Error if backend failed
|
|
111
123
|
*/
|
|
112
124
|
cleanUnusedLinks(maxAge: number): Promise<void>;
|
|
125
|
+
/**
|
|
126
|
+
* Remove a short link by its ID
|
|
127
|
+
* @param shortId the short ID to remove
|
|
128
|
+
* @throws Error if backend failed
|
|
129
|
+
*/
|
|
130
|
+
removeShortLink(shortId: string): Promise<void>;
|
|
113
131
|
}
|
|
114
132
|
export declare function createManager({ backend, caches, shortIdLength, onShortIdLengthUpdated, waitUntil, options }: IManagerProps): Promise<IShortLinksManager>;
|
|
115
133
|
|
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 F(M=4){let f="";for(let y=0;y<M;y++)f+="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(Math.floor(Math.random()*62));return f}function j(M,f){let y=new Set,S=0;while(y.size<M&&S<M*100)y.add(F(f)),S++;return Array.from(y)}async function J({backend:M,caches:f=[],shortIdLength:y,onShortIdLengthUpdated:S,waitUntil:z,options:B}){return await M.init?.(),{async createShortLink(W){let A="";for(let O=0;O<3;O++){let E=j(50,y),_=await M.checkShortIdsExist(E),C=E.find((D)=>!_.includes(D));if(!C){++y;let D=S(y);if(z&&D instanceof Promise)z(D);else await D}else{A=C;break}}if(!A)throw Error("Unable to create a shortlink, potentially ran out");if(await M.createShortLink(A,W),f.length>0){let O=(async()=>{for(let E=0;E<f.length;E++){if(!f[E].initialised)await f[E].init?.(),f[E].initialised=!0;await f[E].set(A,W)}})();if(z)z(O);else await O}return A},async getTargetUrl(W){let A=null,O=-1;for(let E=0;E<f.length;E++){if(!f[E].initialised)await f[E].init?.(),f[E].initialised=!0;if(A=await f[E].get(W),A){O=E;break}}if(!A)A=await M.getTargetUrl(W);if(A){if(B?.shouldUpdateLastAccessOnGet??!0){let _=M.updateShortLinkLastAccessTime(W);if(z&&_ instanceof Promise)z(_);else await _}let E=f.length;if(O>=0)E=O;for(let _=0;_<E;_++){let C=async function(){if(!f[_].initialised)await f[_].init?.(),f[_].initialised=!0;await f[_].set(W,A)}();if(z)z(C);else await C}}return A},async updateShortLinkLastAccessTime(W,A){return await M.updateShortLinkLastAccessTime(W,A)},async cleanUnusedLinks(W){let A=await M.cleanUnusedLinks(W);for(let O of f)if(O.delete){if(!O.initialised)await O.init?.(),O.initialised=!0;for(let E of A)await O.delete(E)}},async removeShortLink(W){await M.removeShortLink(W);for(let A of f)if(A.delete){if(!A.initialised)await A.init?.(),A.initialised=!0;await A.delete(W)}}}}export{J as createManager};
|