@potonz/shortlinks-manager 0.2.4 → 0.3.2

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,110 @@ 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
+ /**
60
+ * Update the target URL for an existing short link
61
+ * @param shortId the short ID to update
62
+ * @param targetUrl the new target URL
63
+ * @param baseUrlId optional base URL ID to filter by
64
+ * @returns true if the link was updated, false if not found
65
+ */
66
+ updateShortLink(shortId: string, targetUrl: string, baseUrlId: number | null): boolean | Promise<boolean>;
67
+ baseUrl: {
68
+ /**
69
+ * Add a new base URL
70
+ * @param baseUrl the base URL to add
71
+ */
72
+ add(baseUrl: string): void | Promise<void>;
73
+ /**
74
+ * Remove a base URL by its ID
75
+ * @param id the ID of the base URL to remove
76
+ */
77
+ remove(id: number): void | Promise<void>;
78
+ /**
79
+ * List all base URLs
80
+ * @param includeInactive whether to include inactive base URLs (default: false)
81
+ * @returns array of base URL records
82
+ */
83
+ list(includeInactive?: boolean): IBaseUrlRecord[] | Promise<IBaseUrlRecord[]>;
84
+ /**
85
+ * Get the ID for a base URL
86
+ * @param baseUrl the base URL to get the ID for
87
+ * @returns the base URL ID or null if not found
88
+ */
89
+ getId(baseUrl: string): number | Promise<number>;
90
+ };
91
+ }
92
+ export interface IBaseUrlManager {
93
+ add(baseUrl: string): Promise<void>;
94
+ remove(baseUrl: number): Promise<void>;
95
+ list(includeInactive?: boolean): Promise<IBaseUrlRecord[]>;
96
+ getBaseUrlId(baseUrl: string): Promise<number>;
97
+ }
98
+ export declare function createBaseUrlManager(backend: IShortLinksManagerBackend): IBaseUrlManager;
99
+ export interface ICache {
100
+ initialised?: boolean;
101
+ init?: () => (unknown | Promise<unknown>);
47
102
  /**
48
- * Update last accessed time to current timestamp
49
- * @param shortId
50
- * @param time Unix timestamp or a Date object
103
+ * Get the target URL using the provided shortId
104
+ * @param key
105
+ * @returns string if a target URL is found, null otherwise
51
106
  */
52
- updateShortLinkLastAccessTime(shortId: string, time?: number | Date): void | Promise<void>;
107
+ get: (key: string) => (string | null | Promise<string | null>);
53
108
  /**
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
109
+ * Cache the target URL
110
+ * @param key
111
+ * @param targetUrl
57
112
  */
58
- cleanUnusedLinks(maxAge: number): string[] | Promise<string[]>;
113
+ set: (key: string, targetUrl: string) => (void | Promise<void>);
59
114
  /**
60
- * Remove a short link by its ID
61
- * @param shortId the short ID to remove
115
+ * Delete the short ID in the cache
116
+ * @param key
62
117
  */
63
- removeShortLink(shortId: string): void | Promise<void>;
118
+ delete: (key: string) => (void | Promise<void>);
64
119
  }
65
120
  export interface IManagerProps {
66
121
  backend: IShortLinksManagerBackend;
@@ -73,7 +128,7 @@ export interface IManagerProps {
73
128
  shortIdLength: number;
74
129
  onShortIdLengthUpdated: (newLength: number) => unknown;
75
130
  /**
76
- * A special function to queue the {@link promise}.
131
+ * A special function to queue the promise.
77
132
  *
78
133
  * Useful when running in Cloudflare Worker to
79
134
  * run the promise after the responding to the client.
@@ -96,39 +151,57 @@ export interface IManagerProps {
96
151
  }
97
152
  export interface IShortLinksManager {
98
153
  /**
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
- */
154
+ * Base URL manager for managing base URLs
155
+ */
156
+ baseUrl: IBaseUrlManager;
157
+ /**
158
+ * Generate a short ID linking to the target URL
159
+ * @param {string} targetUrl targetUrl
160
+ * @param {number | null} baseUrlId optional base URL ID
161
+ * @returns {Promise<string>} short ID
162
+ * @throws Error if failed
163
+ */
164
+ createShortLink(targetUrl: string, baseUrlId: number | null): Promise<string>;
165
+ /**
166
+ * Get a target URL from the given short ID
167
+ * @param shortId
168
+ * @param baseUrlId optional base URL ID to filter by
169
+ * @returns {Promise<IShortLinkInfo | null>} the target URL info or null if not found
170
+ * @throws Error if backend failed
171
+ */
172
+ getTargetUrl(shortId: string, baseUrlId: number | null): Promise<string | null>;
173
+ /**
174
+ * Update last accessed time to avoid link being cleaned
175
+ * @param shortId
176
+ * @param baseUrlId optional base URL ID to filter by
177
+ * @param time last accessed time. Defaults to current time
178
+ * @throws Error if backend failed
179
+ */
180
+ updateShortLinkLastAccessTime(shortId: string, baseUrlId: number | null, time?: number | Date): Promise<void>;
181
+ /**
182
+ * Clean up unused links that are older than the given maxAge
183
+ * @param maxAge number of days the record should be kept
184
+ * @throws Error if backend failed
185
+ */
124
186
  cleanUnusedLinks(maxAge: number): Promise<void>;
125
187
  /**
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>;
188
+ * Remove a short link by its ID
189
+ * @param shortId the short ID to remove
190
+ * @param baseUrlId optional base URL ID to filter by
191
+ * @throws Error if backend failed
192
+ */
193
+ removeShortLink(shortId: string, baseUrlId: number | null): Promise<void>;
194
+ /**
195
+ * Update the target URL for an existing short link
196
+ * @param shortId the short ID to update
197
+ * @param targetUrl the new target URL
198
+ * @param baseUrlId optional base URL ID to filter by
199
+ * @returns {Promise<boolean>} true if the link was updated, false if not found
200
+ * @throws Error if backend failed
201
+ */
202
+ updateShortLink(shortId: string, targetUrl: string, baseUrlId: number | null): Promise<boolean>;
131
203
  }
204
+ export declare function normalizeCacheKey(baseUrlId: number | null, shortId: string): string;
132
205
  export declare function createManager({ backend, caches, shortIdLength, onShortIdLengthUpdated, waitUntil, options }: IManagerProps): Promise<IShortLinksManager>;
133
206
 
134
207
  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(f){return{async add(m){return f.baseUrl.add(m)},async remove(m){return f.baseUrl.remove(m)},async list(m){return f.baseUrl.list(m)},async getBaseUrlId(m){return f.baseUrl.getId(m)}}}function N(f=4){let m="";for(let O=0;O<f;O++)m+="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(Math.floor(Math.random()*62));return m}function H(f,m){let O=new Set,j=0;while(O.size<f&&j<f*100)O.add(N(m)),j++;return Array.from(O)}function _(f,m){return`${f??"any"}__${m}`}async function Z({backend:f,caches:m=[],shortIdLength:O,onShortIdLengthUpdated:j,waitUntil:S,options:J}){return await f.init?.(),{baseUrl:F(f),async createShortLink(A,M){let B="";for(let P=0;P<3;P++){let x=H(50,O),v=await f.checkShortIdsExist(x,M),E=x.find((W)=>!v.includes(W));if(!E){++O;let W=j(O);if(S&&W instanceof Promise)S(W);else await W}else{B=E;break}}if(!B)throw Error("Unable to create a shortlink, potentially ran out");if(await f.createShortLink(B,A,M),m.length>0){let P=_(M,B),x=(async()=>{for(let v=0;v<m.length;v++){if(!m[v].initialised)await m[v].init?.(),m[v].initialised=!0;await m[v].set(P,A)}})();if(S)S(x);else await x}return B},async getTargetUrl(A,M){let B=null,P=-1,x=_(M,A);for(let v=0;v<m.length;v++){if(!m[v].initialised)await m[v].init?.(),m[v].initialised=!0;if(B=await m[v].get(x),B){P=v;break}}if(!B)B=await f.getTargetUrl(A,M);if(B){if(J?.shouldUpdateLastAccessOnGet??!0){let E=f.updateShortLinkLastAccessTime(A,M);if(S&&E instanceof Promise)S(E);else await E}let v=m.length;if(P>=0)v=P;for(let E=0;E<v;E++){let W=async function(){if(!m[E].initialised)await m[E].init?.(),m[E].initialised=!0;await m[E].set(x,B)}();if(S)S(W);else await W}}return B},async updateShortLinkLastAccessTime(A,M,B){return await f.updateShortLinkLastAccessTime(A,M,B)},async cleanUnusedLinks(A){let M=await f.cleanUnusedLinks(A);for(let B of m){if(!B.initialised)await B.init?.(),B.initialised=!0;for(let{shortId:P,baseUrlId:x}of M){let v=_(x,P);await B.delete(v)}}},async removeShortLink(A,M){await f.removeShortLink(A,M);for(let B of m){if(!B.initialised)await B.init?.(),B.initialised=!0;let P=_(M,A);await B.delete(P)}},async updateShortLink(A,M,B){let P=await f.updateShortLink(A,M,B);if(P)for(let x of m){if(!x.initialised)await x.init?.(),x.initialised=!0;let v=_(B,A);await x.delete(v)}return P}}}export{_ 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.4",
14
+ "version": "0.3.2",
15
15
  "type": "module",
16
16
  "license": "MIT",
17
17
  "main": "./dist/index.js",