@potonz/shortlinks-manager 0.3.2 → 0.3.4
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 +7 -0
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -87,6 +87,12 @@ export interface IShortLinksManagerBackend {
|
|
|
87
87
|
* @returns the base URL ID or null if not found
|
|
88
88
|
*/
|
|
89
89
|
getId(baseUrl: string): number | Promise<number>;
|
|
90
|
+
/**
|
|
91
|
+
* Get the base URL for a given ID
|
|
92
|
+
* @param id the ID to get the base URL for
|
|
93
|
+
* @returns the base URL string
|
|
94
|
+
*/
|
|
95
|
+
getById(id: number): string | Promise<string>;
|
|
90
96
|
};
|
|
91
97
|
}
|
|
92
98
|
export interface IBaseUrlManager {
|
|
@@ -94,6 +100,7 @@ export interface IBaseUrlManager {
|
|
|
94
100
|
remove(baseUrl: number): Promise<void>;
|
|
95
101
|
list(includeInactive?: boolean): Promise<IBaseUrlRecord[]>;
|
|
96
102
|
getBaseUrlId(baseUrl: string): Promise<number>;
|
|
103
|
+
getBaseUrl(id: number): Promise<string>;
|
|
97
104
|
}
|
|
98
105
|
export declare function createBaseUrlManager(backend: IShortLinksManagerBackend): IBaseUrlManager;
|
|
99
106
|
export interface ICache {
|
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 j(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)},async getBaseUrl(m){return f.baseUrl.getById(m)}}}function J(f=4){let m="";for(let O=0;O<f;O++)m+="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(Math.floor(Math.random()*62));return m}function F(f,m){let O=new Set,u=0;while(O.size<f&&u<f*100)O.add(J(m)),u++;return Array.from(O)}function _(f,m){return`${f??"any"}__${m}`}async function Y({backend:f,caches:m=[],shortIdLength:O,onShortIdLengthUpdated:u,waitUntil:S,options:H}){return await f.init?.(),{baseUrl:j(f),async createShortLink(v,A){let B="";for(let M=0;M<3;M++){let x=F(50,O),P=await f.checkShortIdsExist(x,A),E=x.find((W)=>!P.includes(W));if(!E){++O;let W=u(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,v,A),m.length>0){let M=_(A,B),x=(async()=>{for(let P=0;P<m.length;P++){if(!m[P].initialised)await m[P].init?.(),m[P].initialised=!0;await m[P].set(M,v)}})();if(S)S(x);else await x}return B},async getTargetUrl(v,A){let B=null,M=-1,x=_(A,v);for(let P=0;P<m.length;P++){if(!m[P].initialised)await m[P].init?.(),m[P].initialised=!0;if(B=await m[P].get(x),B){M=P;break}}if(!B)B=await f.getTargetUrl(v,A);if(B){if(H?.shouldUpdateLastAccessOnGet??!0){let E=f.updateShortLinkLastAccessTime(v,A);if(S&&E instanceof Promise)S(E);else await E}let P=m.length;if(M>=0)P=M;for(let E=0;E<P;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(v,A,B){return await f.updateShortLinkLastAccessTime(v,A,B)},async cleanUnusedLinks(v){let A=await f.cleanUnusedLinks(v);for(let B of m){if(!B.initialised)await B.init?.(),B.initialised=!0;for(let{shortId:M,baseUrlId:x}of A){let P=_(x,M);await B.delete(P)}}},async removeShortLink(v,A){await f.removeShortLink(v,A);for(let B of m){if(!B.initialised)await B.init?.(),B.initialised=!0;let M=_(A,v);await B.delete(M)}},async updateShortLink(v,A,B){let M=await f.updateShortLink(v,A,B);if(M)for(let x of m){if(!x.initialised)await x.init?.(),x.initialised=!0;let P=_(B,v);await x.delete(P)}return M}}}export{_ as normalizeCacheKey,Y as createManager,j as createBaseUrlManager};
|