@potonz/shortlinks-manager 0.2.3 → 0.3.1

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 CHANGED
@@ -1,25 +1,9 @@
1
1
  // Generated by dts-bundle-generator v9.5.1
2
2
 
3
- export interface ICache {
4
- initialised?: boolean;
5
- init?: () => (unknown | Promise<unknown>);
6
- /**
7
- * Get the target URL using the provided shortId
8
- * @param shortId
9
- * @returns string if a target URL is found, null otherwise
10
- */
11
- get: (shortId: string) => (string | null | Promise<string | null>);
12
- /**
13
- * Cache the target URL
14
- * @param shortId
15
- * @param targetUrl
16
- */
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>);
3
+ export interface IBaseUrlRecord {
4
+ id: number;
5
+ baseUrl: string;
6
+ isActive?: boolean;
23
7
  }
24
8
  export interface IShortLinksManagerBackend {
25
9
  /**
@@ -28,39 +12,102 @@ export interface IShortLinksManagerBackend {
28
12
  */
29
13
  init?: () => unknown;
30
14
  /**
31
- * Get target URL for the given short ID
32
- * @param {string} shortId
33
- * @returns the short ID or null if not found
34
- */
35
- getTargetUrl(shortId: string): string | null | Promise<string | null>;
36
- /**
37
- * Create a short link map with the given short ID and target URL
38
- * @param {string} shortId
39
- * @param {string} targetUrl
40
- */
41
- createShortLink(shortId: string, targetUrl: string): void | Promise<void>;
42
- /**
43
- * Check the provided list of short IDs and return the ones that already exist.
44
- * @param {string[]} shortIds
45
- */
46
- checkShortIdsExist(shortIds: string[]): string[] | Promise<string[]>;
15
+ * Get target URL for the given short ID
16
+ * @param {string} shortId
17
+ * @param {number} baseUrlId optional base URL ID to filter by
18
+ * @returns the target URL or null if not found
19
+ */
20
+ getTargetUrl(shortId: string, baseUrlId: number | null): string | null | Promise<string | null>;
21
+ /**
22
+ * Create a short link map with the given short ID and target URL
23
+ * @param {string} shortId
24
+ * @param {string} targetUrl
25
+ * @param {number} baseUrlId optional base URL ID
26
+ */
27
+ createShortLink(shortId: string, targetUrl: string, baseUrlId: number | null): void | Promise<void>;
28
+ /**
29
+ * Check the provided list of short IDs and return the ones that already exist.
30
+ * @param {string[]} shortIds
31
+ * @param {number} baseUrlId optional base URL ID to check within
32
+ */
33
+ checkShortIdsExist(shortIds: string[], baseUrlId: number | null): string[] | Promise<string[]>;
34
+ /**
35
+ * Update last accessed time to current timestamp
36
+ * @param shortId
37
+ * @param baseUrlId optional base URL ID to filter by
38
+ * @param time Unix timestamp or a Date object
39
+ */
40
+ updateShortLinkLastAccessTime(shortId: string, baseUrlId: number | null, time?: number | Date): void | Promise<void>;
41
+ /**
42
+ * Remove unused links that are older than the given maxAge
43
+ * @param maxAge number of days the record should be kept
44
+ * @returns an array of objects with shortId and baseUrlId that have been cleaned
45
+ */
46
+ cleanUnusedLinks(maxAge: number): Array<{
47
+ shortId: string;
48
+ baseUrlId: number | null;
49
+ }> | Promise<Array<{
50
+ shortId: string;
51
+ baseUrlId: number | null;
52
+ }>>;
53
+ /**
54
+ * Remove a short link by its ID
55
+ * @param shortId the short ID to remove
56
+ * @param baseUrlId optional base URL ID to filter by
57
+ */
58
+ removeShortLink(shortId: string, baseUrlId: number | null): void | Promise<void>;
59
+ baseUrl: {
60
+ /**
61
+ * Add a new base URL
62
+ * @param baseUrl the base URL to add
63
+ */
64
+ add(baseUrl: string): void | Promise<void>;
65
+ /**
66
+ * Remove a base URL by its ID
67
+ * @param id the ID of the base URL to remove
68
+ */
69
+ remove(id: number): void | Promise<void>;
70
+ /**
71
+ * List all base URLs
72
+ * @param includeInactive whether to include inactive base URLs (default: false)
73
+ * @returns array of base URL records
74
+ */
75
+ list(includeInactive?: boolean): IBaseUrlRecord[] | Promise<IBaseUrlRecord[]>;
76
+ /**
77
+ * Get the ID for a base URL
78
+ * @param baseUrl the base URL to get the ID for
79
+ * @returns the base URL ID or null if not found
80
+ */
81
+ getId(baseUrl: string): number | Promise<number>;
82
+ };
83
+ }
84
+ export interface IBaseUrlManager {
85
+ add(baseUrl: string): Promise<void>;
86
+ remove(baseUrl: number): Promise<void>;
87
+ list(includeInactive?: boolean): Promise<IBaseUrlRecord[]>;
88
+ getBaseUrlId(baseUrl: string): Promise<number>;
89
+ }
90
+ export declare function createBaseUrlManager(backend: IShortLinksManagerBackend): IBaseUrlManager;
91
+ export interface ICache {
92
+ initialised?: boolean;
93
+ init?: () => (unknown | Promise<unknown>);
47
94
  /**
48
- * Update last accessed time to current timestamp
49
- * @param shortId
50
- * @param time Unix timestamp or a Date object
95
+ * Get the target URL using the provided shortId
96
+ * @param key
97
+ * @returns string if a target URL is found, null otherwise
51
98
  */
52
- updateShortLinkLastAccessTime(shortId: string, time?: number | Date): void | Promise<void>;
99
+ get: (key: string) => (string | null | Promise<string | null>);
53
100
  /**
54
- * Remove unused links that are older than the given maxAge
55
- * @param maxAge number of days the record should be kept
56
- * @returns an array of short IDs that have been cleaned
101
+ * Cache the target URL
102
+ * @param key
103
+ * @param targetUrl
57
104
  */
58
- cleanUnusedLinks(maxAge: number): string[] | Promise<string[]>;
105
+ set: (key: string, targetUrl: string) => (void | Promise<void>);
59
106
  /**
60
- * Remove a short link by its ID
61
- * @param shortId the short ID to remove
107
+ * Delete the short ID in the cache
108
+ * @param key
62
109
  */
63
- removeShortLink(shortId: string): void | Promise<void>;
110
+ delete: (key: string) => (void | Promise<void>);
64
111
  }
65
112
  export interface IManagerProps {
66
113
  backend: IShortLinksManagerBackend;
@@ -73,7 +120,7 @@ export interface IManagerProps {
73
120
  shortIdLength: number;
74
121
  onShortIdLengthUpdated: (newLength: number) => unknown;
75
122
  /**
76
- * A special function to queue the {@link promise}.
123
+ * A special function to queue the promise.
77
124
  *
78
125
  * Useful when running in Cloudflare Worker to
79
126
  * run the promise after the responding to the client.
@@ -96,39 +143,48 @@ export interface IManagerProps {
96
143
  }
97
144
  export interface IShortLinksManager {
98
145
  /**
99
- * Generate a short ID linking to the target URL
100
- * @param {string} targetUrl targetUrl
101
- * @returns {Promise<string>} short ID
102
- * @throws Error if failed
103
- */
104
- createShortLink(targetUrl: string): Promise<string>;
105
- /**
106
- * Get a target URL from the given short ID
107
- * @param shortId
108
- * @returns the target URL as string or null if not found
109
- * @throws Error if backend failed
110
- */
111
- getTargetUrl(shortId: string): Promise<string | null>;
112
- /**
113
- * Update last accessed time to avoid link being cleaned
114
- * @param shortId
115
- * @param time last accessed time. Defaults to current time
116
- * @throws Error if backend failed
117
- */
118
- updateShortLinkLastAccessTime(shortId: string, time?: number | Date): Promise<void>;
119
- /**
120
- * Clean up unused links that are older than the given maxAge
121
- * @param maxAge number of days the record should be kept
122
- * @throws Error if backend failed
123
- */
146
+ * Base URL manager for managing base URLs
147
+ */
148
+ baseUrl: IBaseUrlManager;
149
+ /**
150
+ * Generate a short ID linking to the target URL
151
+ * @param {string} targetUrl targetUrl
152
+ * @param {number | null} baseUrlId optional base URL ID
153
+ * @returns {Promise<string>} short ID
154
+ * @throws Error if failed
155
+ */
156
+ createShortLink(targetUrl: string, baseUrlId: number | null): Promise<string>;
157
+ /**
158
+ * Get a target URL from the given short ID
159
+ * @param shortId
160
+ * @param baseUrlId optional base URL ID to filter by
161
+ * @returns {Promise<IShortLinkInfo | null>} the target URL info or null if not found
162
+ * @throws Error if backend failed
163
+ */
164
+ getTargetUrl(shortId: string, baseUrlId: number | null): Promise<string | null>;
165
+ /**
166
+ * Update last accessed time to avoid link being cleaned
167
+ * @param shortId
168
+ * @param baseUrlId optional base URL ID to filter by
169
+ * @param time last accessed time. Defaults to current time
170
+ * @throws Error if backend failed
171
+ */
172
+ updateShortLinkLastAccessTime(shortId: string, baseUrlId: number | null, time?: number | Date): Promise<void>;
173
+ /**
174
+ * Clean up unused links that are older than the given maxAge
175
+ * @param maxAge number of days the record should be kept
176
+ * @throws Error if backend failed
177
+ */
124
178
  cleanUnusedLinks(maxAge: number): Promise<void>;
125
179
  /**
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>;
180
+ * Remove a short link by its ID
181
+ * @param shortId the short ID to remove
182
+ * @param baseUrlId optional base URL ID to filter by
183
+ * @throws Error if backend failed
184
+ */
185
+ removeShortLink(shortId: string, baseUrlId: number | null): Promise<void>;
131
186
  }
187
+ export declare function normalizeCacheKey(baseUrlId: number | null, shortId: string): string;
132
188
  export declare function createManager({ backend, caches, shortIdLength, onShortIdLengthUpdated, waitUntil, options }: IManagerProps): Promise<IShortLinksManager>;
133
189
 
134
190
  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 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};
23
+ function F(v){return{async add(m){return v.baseUrl.add(m)},async remove(m){return v.baseUrl.remove(m)},async list(m){return v.baseUrl.list(m)},async getBaseUrlId(m){return v.baseUrl.getId(m)}}}function N(v=4){let m="";for(let E=0;E<v;E++)m+="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(Math.floor(Math.random()*62));return m}function H(v,m){let E=new Set,_=0;while(E.size<v&&_<v*100)E.add(N(m)),_++;return Array.from(E)}function j(v,m){return`${v??"any"}__${m}`}async function Z({backend:v,caches:m=[],shortIdLength:E,onShortIdLengthUpdated:_,waitUntil:S,options:J}){return await v.init?.(),{baseUrl:F(v),async createShortLink(M,A){let B="";for(let x=0;x<3;x++){let O=H(50,E),f=await v.checkShortIdsExist(O,A),P=O.find((W)=>!f.includes(W));if(!P){++E;let W=_(E);if(S&&W instanceof Promise)S(W);else await W}else{B=P;break}}if(!B)throw Error("Unable to create a shortlink, potentially ran out");if(await v.createShortLink(B,M,A),m.length>0){let x=j(A,B),O=(async()=>{for(let f=0;f<m.length;f++){if(!m[f].initialised)await m[f].init?.(),m[f].initialised=!0;await m[f].set(x,M)}})();if(S)S(O);else await O}return B},async getTargetUrl(M,A){let B=null,x=-1,O=j(A,M);for(let f=0;f<m.length;f++){if(!m[f].initialised)await m[f].init?.(),m[f].initialised=!0;if(B=await m[f].get(O),B){x=f;break}}if(!B)B=await v.getTargetUrl(M,A);if(B){if(J?.shouldUpdateLastAccessOnGet??!0){let P=v.updateShortLinkLastAccessTime(M,A);if(S&&P instanceof Promise)S(P);else await P}let f=m.length;if(x>=0)f=x;for(let P=0;P<f;P++){let W=async function(){if(!m[P].initialised)await m[P].init?.(),m[P].initialised=!0;await m[P].set(O,B)}();if(S)S(W);else await W}}return B},async updateShortLinkLastAccessTime(M,A,B){return await v.updateShortLinkLastAccessTime(M,A,B)},async cleanUnusedLinks(M){let A=await v.cleanUnusedLinks(M);for(let B of m){if(!B.initialised)await B.init?.(),B.initialised=!0;for(let{shortId:x,baseUrlId:O}of A){let f=j(O,x);await B.delete(f)}}},async removeShortLink(M,A){await v.removeShortLink(M,A);for(let B of m){if(!B.initialised)await B.init?.(),B.initialised=!0;let x=j(A,M);await B.delete(x)}}}}export{j as normalizeCacheKey,Z as createManager,F as createBaseUrlManager};
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "type": "git",
12
12
  "directory": "packages/shortlinks-manager"
13
13
  },
14
- "version": "0.2.3",
14
+ "version": "0.3.1",
15
15
  "type": "module",
16
16
  "license": "MIT",
17
17
  "main": "./dist/index.js",