@potonz/shortlinks-manager 0.2.0 → 0.2.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,6 +1,7 @@
1
1
  // Generated by dts-bundle-generator v9.5.1
2
2
 
3
3
  export interface ICache {
4
+ initialised?: boolean;
4
5
  init?: () => unknown | Promise<unknown>;
5
6
  /**
6
7
  * Get the target URL using the provided shortId
@@ -59,6 +60,27 @@ export interface IManagerProps {
59
60
  caches?: ICache[];
60
61
  shortIdLength: number;
61
62
  onShortIdLengthUpdated: (newLength: number) => unknown;
63
+ /**
64
+ * A special function to queue the {@link promise}.
65
+ *
66
+ * Useful when running in Cloudflare Worker to
67
+ * run the promise after the responding to the client.
68
+ *
69
+ * This function is used where, for example, updating all caches
70
+ * if a target URL is found in the backend. We want to return
71
+ * the URL right away but queue writing to cache after responding to the client.
72
+ * @param promise
73
+ */
74
+ waitUntil?: (promise: Promise<unknown>) => void;
75
+ options?: {
76
+ /**
77
+ * Whether to update the backend with last access time
78
+ * on {@link IShortLinksManager#getTargetUrl()} call.
79
+ * Set to `false` if you want to manually manage this.
80
+ * Defaults to `true`
81
+ */
82
+ shouldUpdateLastAccessOnGet?: boolean;
83
+ };
62
84
  }
63
85
  export interface IShortLinksManager {
64
86
  /**
@@ -89,6 +111,6 @@ export interface IShortLinksManager {
89
111
  */
90
112
  cleanUnusedLinks(maxAge: number): Promise<void>;
91
113
  }
92
- export declare function createManager({ backend, caches, shortIdLength, onShortIdLengthUpdated }: IManagerProps): Promise<IShortLinksManager>;
114
+ export declare function createManager({ backend, caches, shortIdLength, onShortIdLengthUpdated, waitUntil, options }: IManagerProps): Promise<IShortLinksManager>;
93
115
 
94
116
  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 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};
23
+ function C(m=4){let f="";for(let O=0;O<m;O++)f+="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(Math.floor(Math.random()*62));return f}function j(m,f){let O=new Set,S=0;while(O.size<m&&S<m*100)O.add(C(f)),S++;return Array.from(O)}async function H({backend:m,caches:f=[],shortIdLength:O,onShortIdLengthUpdated:S,waitUntil:M,options:B}){return await m.init?.(),{async createShortLink(W){let D="";for(let _=0;_<3;_++){let A=j(50,O),E=await m.checkShortIdsExist(A),y=A.find((z)=>!E.includes(z));if(!y){++O;let z=S(O);if(M&&z instanceof Promise)M(z);else await z}else{D=y;break}}if(!D)throw Error("Unable to create a shortlink, potentially ran out");return await m.createShortLink(D,W),D},async getTargetUrl(W){let D=null,_=-1;for(let A=0;A<f.length;A++){if(!f[A].initialised)await f[A].init?.(),f[A].initialised=!0;if(D=await f[A].get(W),D){_=A;break}}if(!D)D=await m.getTargetUrl(W);if(D){if(B?.shouldUpdateLastAccessOnGet??!0){let E=m.updateShortLinkLastAccessTime(W);if(M&&E instanceof Promise)M(E);else await E}let A=f.length;if(_>=0)A=_;for(let E=0;E<A;E++){let y=async function(){if(!f[E].initialised)await f[E].init?.(),f[E].initialised=!0;await f[E].set(W,D)}();if(M)M(y);else await y}}return D},async updateShortLinkLastAccessTime(W){return await m.updateShortLinkLastAccessTime(W)},async cleanUnusedLinks(W){await m.cleanUnusedLinks(W)}}}export{H as createManager};
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "type": "git",
12
12
  "directory": "packages/shortlinks-manager"
13
13
  },
14
- "version": "0.2.0",
14
+ "version": "0.2.2",
15
15
  "type": "module",
16
16
  "license": "MIT",
17
17
  "main": "./dist/index.js",