Compare commits
3 Commits
main
...
9b5cc7ea62
| Author | SHA1 | Date | |
|---|---|---|---|
| 9b5cc7ea62 | |||
| c24ff62b30 | |||
| 495506935e |
10
package.json
10
package.json
@@ -23,15 +23,19 @@
|
|||||||
"@hono/swagger-ui": "^0.5.1",
|
"@hono/swagger-ui": "^0.5.1",
|
||||||
"@hono/zod-openapi": "^0.19.5",
|
"@hono/zod-openapi": "^0.19.5",
|
||||||
"@hono/zod-validator": "^0.2.2",
|
"@hono/zod-validator": "^0.2.2",
|
||||||
|
"blurhash": "^2.0.5",
|
||||||
"drizzle-orm": "^0.44.7",
|
"drizzle-orm": "^0.44.7",
|
||||||
"gql.tada": "^1.8.10",
|
"gql.tada": "^1.8.10",
|
||||||
"graphql": "^16.12.0",
|
"graphql": "^16.12.0",
|
||||||
"graphql-request": "^7.1.2",
|
"graphql-request": "^7.1.2",
|
||||||
|
"graphql-yoga": "^5.17.0",
|
||||||
"hono": "^4.7.7",
|
"hono": "^4.7.7",
|
||||||
"jose": "^5.10.0",
|
"jose": "^5.10.0",
|
||||||
|
"jpeg-js": "^0.4.4",
|
||||||
"lodash.isequal": "^4.5.0",
|
"lodash.isequal": "^4.5.0",
|
||||||
"lodash.mapkeys": "^4.6.0",
|
"lodash.mapkeys": "^4.6.0",
|
||||||
"luxon": "^3.6.1",
|
"luxon": "^3.6.1",
|
||||||
|
"pngjs": "^7.0.0",
|
||||||
"zod": "^3.24.3"
|
"zod": "^3.24.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
@@ -43,6 +47,11 @@
|
|||||||
"@types/lodash.isequal": "^4.5.8",
|
"@types/lodash.isequal": "^4.5.8",
|
||||||
"@types/lodash.mapkeys": "^4.6.9",
|
"@types/lodash.mapkeys": "^4.6.9",
|
||||||
"@types/luxon": "^3.6.2",
|
"@types/luxon": "^3.6.2",
|
||||||
|
"@types/node": "^24.10.1",
|
||||||
|
"@types/pngjs": "^6.0.5",
|
||||||
|
"@vitest/coverage-v8": "^3.2.4",
|
||||||
|
"@vitest/runner": "^3.2.4",
|
||||||
|
"@vitest/snapshot": "^3.2.4",
|
||||||
"cloudflare": "^5.2.0",
|
"cloudflare": "^5.2.0",
|
||||||
"dotenv": "^17.2.3",
|
"dotenv": "^17.2.3",
|
||||||
"drizzle-kit": "^0.31.7",
|
"drizzle-kit": "^0.31.7",
|
||||||
@@ -58,6 +67,7 @@
|
|||||||
"tsx": "^4.20.6",
|
"tsx": "^4.20.6",
|
||||||
"typescript": "^5.8.3",
|
"typescript": "^5.8.3",
|
||||||
"util": "^0.12.5",
|
"util": "^0.12.5",
|
||||||
|
"vitest": "^3.2.4",
|
||||||
"wrangler": "^4.51.0",
|
"wrangler": "^4.51.0",
|
||||||
"zx": "8.1.5"
|
"zx": "8.1.5"
|
||||||
},
|
},
|
||||||
|
|||||||
30
patches/blurhash.patch
Normal file
30
patches/blurhash.patch
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
diff --git a/CHANGELOG.md b/CHANGELOG.md
|
||||||
|
deleted file mode 100644
|
||||||
|
index f793ae02ac3104ed8272b06e4067edde2944a1b9..0000000000000000000000000000000000000000
|
||||||
|
diff --git a/dist/esm/index.js b/dist/esm/index.js
|
||||||
|
index 254eb7a0a33eba9f6622552cfaa88db9c01ab73a..06380b72abb031372b5b176078bb7199f62d62d1 100644
|
||||||
|
--- a/dist/esm/index.js
|
||||||
|
+++ b/dist/esm/index.js
|
||||||
|
@@ -1,2 +1 @@
|
||||||
|
-var q=["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","#","$","%","*","+",",","-",".",":",";","=","?","@","[","]","^","_","{","|","}","~"],x=t=>{let e=0;for(let r=0;r<t.length;r++){let n=t[r],l=q.indexOf(n);e=e*83+l}return e},p=(t,e)=>{var r="";for(let n=1;n<=e;n++){let l=Math.floor(t)/Math.pow(83,e-n)%83;r+=q[Math.floor(l)]}return r};var f=t=>{let e=t/255;return e<=.04045?e/12.92:Math.pow((e+.055)/1.055,2.4)},h=t=>{let e=Math.max(0,Math.min(1,t));return e<=.0031308?Math.trunc(e*12.92*255+.5):Math.trunc((1.055*Math.pow(e,.4166666666666667)-.055)*255+.5)},F=t=>t<0?-1:1,M=(t,e)=>F(t)*Math.pow(Math.abs(t),e);var d=class extends Error{constructor(e){super(e),this.name="ValidationError",this.message=e}};var C=t=>{if(!t||t.length<6)throw new d("The blurhash string must be at least 6 characters");let e=x(t[0]),r=Math.floor(e/9)+1,n=e%9+1;if(t.length!==4+2*n*r)throw new d(`blurhash length mismatch: length is ${t.length} but it should be ${4+2*n*r}`)},N=t=>{try{C(t)}catch(e){return{result:!1,errorReason:e.message}}return{result:!0}},z=t=>{let e=t>>16,r=t>>8&255,n=t&255;return[f(e),f(r),f(n)]},L=(t,e)=>{let r=Math.floor(t/361),n=Math.floor(t/19)%19,l=t%19;return[M((r-9)/9,2)*e,M((n-9)/9,2)*e,M((l-9)/9,2)*e]},U=(t,e,r,n)=>{C(t),n=n|1;let l=x(t[0]),m=Math.floor(l/9)+1,b=l%9+1,i=(x(t[1])+1)/166,u=new Array(b*m);for(let o=0;o<u.length;o++)if(o===0){let a=x(t.substring(2,6));u[o]=z(a)}else{let a=x(t.substring(4+o*2,6+o*2));u[o]=L(a,i*n)}let c=e*4,s=new Uint8ClampedArray(c*r);for(let o=0;o<r;o++)for(let a=0;a<e;a++){let y=0,B=0,R=0;for(let w=0;w<m;w++)for(let P=0;P<b;P++){let G=Math.cos(Math.PI*a*P/e)*Math.cos(Math.PI*o*w/r),T=u[P+w*b];y+=T[0]*G,B+=T[1]*G,R+=T[2]*G}let V=h(y),I=h(B),E=h(R);s[4*a+0+o*c]=V,s[4*a+1+o*c]=I,s[4*a+2+o*c]=E,s[4*a+3+o*c]=255}return s},j=U;var A=4,D=(t,e,r,n)=>{let l=0,m=0,b=0,g=e*A;for(let u=0;u<e;u++){let c=A*u;for(let s=0;s<r;s++){let o=c+s*g,a=n(u,s);l+=a*f(t[o]),m+=a*f(t[o+1]),b+=a*f(t[o+2])}}let i=1/(e*r);return[l*i,m*i,b*i]},$=t=>{let e=h(t[0]),r=h(t[1]),n=h(t[2]);return(e<<16)+(r<<8)+n},H=(t,e)=>{let r=Math.floor(Math.max(0,Math.min(18,Math.floor(M(t[0]/e,.5)*9+9.5)))),n=Math.floor(Math.max(0,Math.min(18,Math.floor(M(t[1]/e,.5)*9+9.5)))),l=Math.floor(Math.max(0,Math.min(18,Math.floor(M(t[2]/e,.5)*9+9.5))));return r*19*19+n*19+l},O=(t,e,r,n,l)=>{if(n<1||n>9||l<1||l>9)throw new d("BlurHash must have between 1 and 9 components");if(e*r*4!==t.length)throw new d("Width and height must match the pixels array");let m=[];for(let s=0;s<l;s++)for(let o=0;o<n;o++){let a=o==0&&s==0?1:2,y=D(t,e,r,(B,R)=>a*Math.cos(Math.PI*o*B/e)*Math.cos(Math.PI*s*R/r));m.push(y)}let b=m[0],g=m.slice(1),i="",u=n-1+(l-1)*9;i+=p(u,1);let c;if(g.length>0){let s=Math.max(...g.map(a=>Math.max(...a))),o=Math.floor(Math.max(0,Math.min(82,Math.floor(s*166-.5))));c=(o+1)/166,i+=p(o,1)}else c=1,i+=p(0,1);return i+=p($(b),4),g.forEach(s=>{i+=p(H(s,c),2)}),i},S=O;export{d as ValidationError,j as decode,S as encode,N as isBlurhashValid};
|
||||||
|
-//# sourceMappingURL=index.js.map
|
||||||
|
\ No newline at end of file
|
||||||
|
+var A=["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","#","$","%","*","+",",","-",".",":",";","=","?","@","[","]","^","_","{","|","}","~"],d=t=>{let r=0;for(let a=0;a<t.length;a++){let l=t[a],o=A.indexOf(l);r=r*83+o}return r},b=(t,r)=>{var a="";for(let l=1;l<=r;l++){let o=Math.floor(t)/Math.pow(83,r-l)%83;a+=A[Math.floor(o)]}return a},c=t=>{let r=t/255;return r<=.04045?r/12.92:Math.pow((r+.055)/1.055,2.4)},g=t=>{let r=Math.max(0,Math.min(1,t));return r<=.0031308?Math.trunc(r*12.92*255+.5):Math.trunc((1.055*Math.pow(r,.4166666666666667)-.055)*255+.5)},O=t=>t<0?-1:1,w=(t,r)=>O(t)*Math.pow(Math.abs(t),r),p=class extends Error{constructor(t){super(t),this.name="ValidationError",this.message=t}},B=t=>{if(!t||t.length<6)throw new p("The blurhash string must be at least 6 characters");let r=d(t[0]),a=Math.floor(r/9)+1,l=r%9+1;if(t.length!==4+2*l*a)throw new p(`blurhash length mismatch: length is ${t.length} but it should be ${4+2*l*a}`)},R=t=>{try{B(t)}catch(r){return{result:!1,errorReason:r.message}}return{result:!0}},T=t=>{let r=t>>16,a=t>>8&255,l=t&255;return[c(r),c(a),c(l)]},U=(t,r)=>{let a=Math.floor(t/361),l=Math.floor(t/19)%19,o=t%19;return[w((a-9)/9,2)*r,w((l-9)/9,2)*r,w((o-9)/9,2)*r]},j=(t,r,a,l)=>{B(t),l=l|1;let o=d(t[0]),i=Math.floor(o/9)+1,u=o%9+1,m=(d(t[1])+1)/166,n=new Array(u*i);for(let e=0;e<n.length;e++)if(e===0){let h=d(t.substring(2,6));n[e]=T(h)}else{let h=d(t.substring(4+e*2,6+e*2));n[e]=U(h,m*l)}let s=r*4,M=new Uint8ClampedArray(s*a);for(let e=0;e<a;e++)for(let h=0;h<r;h++){let f=0,x=0,v=0;for(let y=0;y<i;y++)for(let E=0;E<u;E++){let P=Math.cos(Math.PI*h*E/r)*Math.cos(Math.PI*e*y/a),V=n[E+y*u];f+=V[0]*P,x+=V[1]*P,v+=V[2]*P}let I=g(f),C=g(x),H=g(v);M[4*h+0+e*s]=I,M[4*h+1+e*s]=C,M[4*h+2+e*s]=H,M[4*h+3+e*s]=255}return M},q=j,$=4,z=(t,r,a,l)=>{let o=0,i=0,u=0,m=r*$;for(let s=0;s<r;s++){let M=$*s;for(let e=0;e<a;e++){let h=M+e*m,f=l(s,e);o+=f*c(t[h]),i+=f*c(t[h+1]),u+=f*c(t[h+2])}}let n=1/(r*a);return[o*n,i*n,u*n]},D=t=>{let r=g(t[0]),a=g(t[1]),l=g(t[2]);return(r<<16)+(a<<8)+l},F=(t,r)=>{let a=Math.floor(Math.max(0,Math.min(18,Math.floor(w(t[0]/r,.5)*9+9.5)))),l=Math.floor(Math.max(0,Math.min(18,Math.floor(w(t[1]/r,.5)*9+9.5)))),o=Math.floor(Math.max(0,Math.min(18,Math.floor(w(t[2]/r,.5)*9+9.5))));return a*19*19+l*19+o},G=(t,r,a,l,o)=>{if(l<1||l>9||o<1||o>9)throw new p("BlurHash must have between 1 and 9 components");if(Math.floor(r*a*4)!==t.length)throw new p("Width and height must match the pixels array");let i=[];for(let e=0;e<o;e++)for(let h=0;h<l;h++){let f=h==0&&e==0?1:2,x=z(t,r,a,(v,I)=>f*Math.cos(Math.PI*h*v/r)*Math.cos(Math.PI*e*I/a));i.push(x)}let u=i[0],m=i.slice(1),n="",s=l-1+(o-1)*9;n+=b(s,1);let M;if(m.length>0){let e=Math.max(...m.map(f=>Math.max(...f))),h=Math.floor(Math.max(0,Math.min(82,Math.floor(e*166-.5))));M=(h+1)/166,n+=b(h,1)}else M=1,n+=b(0,1);return n+=b(D(u),4),m.forEach(e=>{n+=b(F(e,M),2)}),n},L=G;export{p as ValidationError,q as decode,L as encode,R as isBlurhashValid};
|
||||||
|
diff --git a/dist/index.js b/dist/index.js
|
||||||
|
index fe46957ffed377f20992b86da266ce679c515802..075ab8fe648c9a34edcee9a842eb00c34eaa5179 100644
|
||||||
|
--- a/dist/index.js
|
||||||
|
+++ b/dist/index.js
|
||||||
|
@@ -1,2 +1 @@
|
||||||
|
-var q=Object.defineProperty;var U=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var D=Object.prototype.hasOwnProperty;var $=(t,e)=>{for(var r in e)q(t,r,{get:e[r],enumerable:!0})},H=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of j(e))!D.call(t,s)&&s!==r&&q(t,s,{get:()=>e[s],enumerable:!(n=U(e,s))||n.enumerable});return t};var O=t=>H(q({},"__esModule",{value:!0}),t);var _={};$(_,{ValidationError:()=>b,decode:()=>I,encode:()=>F,isBlurhashValid:()=>V});module.exports=O(_);var C=["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","#","$","%","*","+",",","-",".",":",";","=","?","@","[","]","^","_","{","|","}","~"],x=t=>{let e=0;for(let r=0;r<t.length;r++){let n=t[r],s=C.indexOf(n);e=e*83+s}return e},p=(t,e)=>{var r="";for(let n=1;n<=e;n++){let s=Math.floor(t)/Math.pow(83,e-n)%83;r+=C[Math.floor(s)]}return r};var h=t=>{let e=t/255;return e<=.04045?e/12.92:Math.pow((e+.055)/1.055,2.4)},M=t=>{let e=Math.max(0,Math.min(1,t));return e<=.0031308?Math.trunc(e*12.92*255+.5):Math.trunc((1.055*Math.pow(e,.4166666666666667)-.055)*255+.5)},S=t=>t<0?-1:1,d=(t,e)=>S(t)*Math.pow(Math.abs(t),e);var b=class extends Error{constructor(e){super(e),this.name="ValidationError",this.message=e}};var A=t=>{if(!t||t.length<6)throw new b("The blurhash string must be at least 6 characters");let e=x(t[0]),r=Math.floor(e/9)+1,n=e%9+1;if(t.length!==4+2*n*r)throw new b(`blurhash length mismatch: length is ${t.length} but it should be ${4+2*n*r}`)},V=t=>{try{A(t)}catch(e){return{result:!1,errorReason:e.message}}return{result:!0}},W=t=>{let e=t>>16,r=t>>8&255,n=t&255;return[h(e),h(r),h(n)]},k=(t,e)=>{let r=Math.floor(t/361),n=Math.floor(t/19)%19,s=t%19;return[d((r-9)/9,2)*e,d((n-9)/9,2)*e,d((s-9)/9,2)*e]},J=(t,e,r,n)=>{A(t),n=n|1;let s=x(t[0]),m=Math.floor(s/9)+1,f=s%9+1,i=(x(t[1])+1)/166,u=new Array(f*m);for(let o=0;o<u.length;o++)if(o===0){let l=x(t.substring(2,6));u[o]=W(l)}else{let l=x(t.substring(4+o*2,6+o*2));u[o]=k(l,i*n)}let c=e*4,a=new Uint8ClampedArray(c*r);for(let o=0;o<r;o++)for(let l=0;l<e;l++){let y=0,B=0,R=0;for(let w=0;w<m;w++)for(let P=0;P<f;P++){let G=Math.cos(Math.PI*l*P/e)*Math.cos(Math.PI*o*w/r),T=u[P+w*f];y+=T[0]*G,B+=T[1]*G,R+=T[2]*G}let N=M(y),z=M(B),L=M(R);a[4*l+0+o*c]=N,a[4*l+1+o*c]=z,a[4*l+2+o*c]=L,a[4*l+3+o*c]=255}return a},I=J;var E=4,K=(t,e,r,n)=>{let s=0,m=0,f=0,g=e*E;for(let u=0;u<e;u++){let c=E*u;for(let a=0;a<r;a++){let o=c+a*g,l=n(u,a);s+=l*h(t[o]),m+=l*h(t[o+1]),f+=l*h(t[o+2])}}let i=1/(e*r);return[s*i,m*i,f*i]},Q=t=>{let e=M(t[0]),r=M(t[1]),n=M(t[2]);return(e<<16)+(r<<8)+n},X=(t,e)=>{let r=Math.floor(Math.max(0,Math.min(18,Math.floor(d(t[0]/e,.5)*9+9.5)))),n=Math.floor(Math.max(0,Math.min(18,Math.floor(d(t[1]/e,.5)*9+9.5)))),s=Math.floor(Math.max(0,Math.min(18,Math.floor(d(t[2]/e,.5)*9+9.5))));return r*19*19+n*19+s},Z=(t,e,r,n,s)=>{if(n<1||n>9||s<1||s>9)throw new b("BlurHash must have between 1 and 9 components");if(e*r*4!==t.length)throw new b("Width and height must match the pixels array");let m=[];for(let a=0;a<s;a++)for(let o=0;o<n;o++){let l=o==0&&a==0?1:2,y=K(t,e,r,(B,R)=>l*Math.cos(Math.PI*o*B/e)*Math.cos(Math.PI*a*R/r));m.push(y)}let f=m[0],g=m.slice(1),i="",u=n-1+(s-1)*9;i+=p(u,1);let c;if(g.length>0){let a=Math.max(...g.map(l=>Math.max(...l))),o=Math.floor(Math.max(0,Math.min(82,Math.floor(a*166-.5))));c=(o+1)/166,i+=p(o,1)}else c=1,i+=p(0,1);return i+=p(Q(f),4),g.forEach(a=>{i+=p(X(a,c),2)}),i},F=Z;0&&(module.exports={ValidationError,decode,encode,isBlurhashValid});
|
||||||
|
-//# sourceMappingURL=index.js.map
|
||||||
|
\ No newline at end of file
|
||||||
|
+var q=Object.defineProperty,U=Object.getOwnPropertyDescriptor,j=Object.getOwnPropertyNames,D=Object.prototype.hasOwnProperty,$=(t,e)=>{for(var r in e)q(t,r,{get:e[r],enumerable:!0})},H=(t,e,r,a)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of j(e))!D.call(t,o)&&o!==r&&q(t,o,{get:()=>e[o],enumerable:!(a=U(e,o))||a.enumerable});return t},O=t=>H(q({},"__esModule",{value:!0}),t),_={};$(_,{ValidationError:()=>b,decode:()=>I,encode:()=>F,isBlurhashValid:()=>V}),module.exports=O(_);var C=["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","#","$","%","*","+",",","-",".",":",";","=","?","@","[","]","^","_","{","|","}","~"],x=t=>{let e=0;for(let r=0;r<t.length;r++){let a=t[r],o=C.indexOf(a);e=e*83+o}return e},p=(t,e)=>{var r="";for(let a=1;a<=e;a++){let o=Math.floor(t)/Math.pow(83,e-a)%83;r+=C[Math.floor(o)]}return r},h=t=>{let e=t/255;return e<=.04045?e/12.92:Math.pow((e+.055)/1.055,2.4)},M=t=>{let e=Math.max(0,Math.min(1,t));return e<=.0031308?Math.trunc(e*12.92*255+.5):Math.trunc((1.055*Math.pow(e,.4166666666666667)-.055)*255+.5)},S=t=>t<0?-1:1,d=(t,e)=>S(t)*Math.pow(Math.abs(t),e),b=class extends Error{constructor(t){super(t),this.name="ValidationError",this.message=t}},A=t=>{if(!t||t.length<6)throw new b("The blurhash string must be at least 6 characters");let e=x(t[0]),r=Math.floor(e/9)+1,a=e%9+1;if(t.length!==4+2*a*r)throw new b(`blurhash length mismatch: length is ${t.length} but it should be ${4+2*a*r}`)},V=t=>{try{A(t)}catch(e){return{result:!1,errorReason:e.message}}return{result:!0}},W=t=>{let e=t>>16,r=t>>8&255,a=t&255;return[h(e),h(r),h(a)]},k=(t,e)=>{let r=Math.floor(t/361),a=Math.floor(t/19)%19,o=t%19;return[d((r-9)/9,2)*e,d((a-9)/9,2)*e,d((o-9)/9,2)*e]},J=(t,e,r,a)=>{A(t),a=a|1;let o=x(t[0]),m=Math.floor(o/9)+1,c=o%9+1,g=(x(t[1])+1)/166,s=new Array(c*m);for(let l=0;l<s.length;l++)if(l===0){let n=x(t.substring(2,6));s[l]=W(n)}else{let n=x(t.substring(4+l*2,6+l*2));s[l]=k(n,g*a)}let f=e*4,u=new Uint8ClampedArray(f*r);for(let l=0;l<r;l++)for(let n=0;n<e;n++){let i=0,w=0,y=0;for(let P=0;P<m;P++)for(let v=0;v<c;v++){let N=Math.cos(Math.PI*n*v/e)*Math.cos(Math.PI*l*P/r),R=s[v+P*c];i+=R[0]*N,w+=R[1]*N,y+=R[2]*N}let B=M(i),T=M(w),z=M(y);u[4*n+0+l*f]=B,u[4*n+1+l*f]=T,u[4*n+2+l*f]=z,u[4*n+3+l*f]=255}return u},I=J,E=4,K=(t,e,r,a)=>{let o=0,m=0,c=0,g=e*E;for(let f=0;f<e;f++){let u=E*f;for(let l=0;l<r;l++){let n=u+l*g,i=a(f,l);o+=i*h(t[n]),m+=i*h(t[n+1]),c+=i*h(t[n+2])}}let s=1/(e*r);return[o*s,m*s,c*s]},Q=t=>{let e=M(t[0]),r=M(t[1]),a=M(t[2]);return(e<<16)+(r<<8)+a},X=(t,e)=>{let r=Math.floor(Math.max(0,Math.min(18,Math.floor(d(t[0]/e,.5)*9+9.5)))),a=Math.floor(Math.max(0,Math.min(18,Math.floor(d(t[1]/e,.5)*9+9.5)))),o=Math.floor(Math.max(0,Math.min(18,Math.floor(d(t[2]/e,.5)*9+9.5))));return r*19*19+a*19+o},Z=(t,e,r,a,o)=>{if(a<1||a>9||o<1||o>9)throw new b("BlurHash must have between 1 and 9 components");if(Math.floor(e*r*4)!==t.length)throw new b("Width and height must match the pixels array");let m=[];for(let l=0;l<o;l++)for(let n=0;n<a;n++){let i=n==0&&l==0?1:2,w=K(t,e,r,(y,B)=>i*Math.cos(Math.PI*n*y/e)*Math.cos(Math.PI*l*B/r));m.push(w)}let c=m[0],g=m.slice(1),s="",f=a-1+(o-1)*9;s+=p(f,1);let u;if(g.length>0){let l=Math.max(...g.map(i=>Math.max(...i))),n=Math.floor(Math.max(0,Math.min(82,Math.floor(l*166-.5))));u=(n+1)/166,s+=p(n,1)}else u=1,s+=p(0,1);return s+=p(Q(c),4),g.forEach(l=>{s+=p(X(l,u),2)}),s},F=Z;
|
||||||
|
diff --git a/dist/index.mjs b/dist/index.mjs
|
||||||
|
index 0feea2d84b8d1ed0f05386aaf9bb1d278aed3d0a..06380b72abb031372b5b176078bb7199f62d62d1 100644
|
||||||
|
--- a/dist/index.mjs
|
||||||
|
+++ b/dist/index.mjs
|
||||||
|
@@ -1,2 +1 @@
|
||||||
|
-var q=["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","#","$","%","*","+",",","-",".",":",";","=","?","@","[","]","^","_","{","|","}","~"],x=t=>{let e=0;for(let r=0;r<t.length;r++){let n=t[r],l=q.indexOf(n);e=e*83+l}return e},p=(t,e)=>{var r="";for(let n=1;n<=e;n++){let l=Math.floor(t)/Math.pow(83,e-n)%83;r+=q[Math.floor(l)]}return r};var f=t=>{let e=t/255;return e<=.04045?e/12.92:Math.pow((e+.055)/1.055,2.4)},h=t=>{let e=Math.max(0,Math.min(1,t));return e<=.0031308?Math.trunc(e*12.92*255+.5):Math.trunc((1.055*Math.pow(e,.4166666666666667)-.055)*255+.5)},F=t=>t<0?-1:1,M=(t,e)=>F(t)*Math.pow(Math.abs(t),e);var d=class extends Error{constructor(e){super(e),this.name="ValidationError",this.message=e}};var C=t=>{if(!t||t.length<6)throw new d("The blurhash string must be at least 6 characters");let e=x(t[0]),r=Math.floor(e/9)+1,n=e%9+1;if(t.length!==4+2*n*r)throw new d(`blurhash length mismatch: length is ${t.length} but it should be ${4+2*n*r}`)},N=t=>{try{C(t)}catch(e){return{result:!1,errorReason:e.message}}return{result:!0}},z=t=>{let e=t>>16,r=t>>8&255,n=t&255;return[f(e),f(r),f(n)]},L=(t,e)=>{let r=Math.floor(t/361),n=Math.floor(t/19)%19,l=t%19;return[M((r-9)/9,2)*e,M((n-9)/9,2)*e,M((l-9)/9,2)*e]},U=(t,e,r,n)=>{C(t),n=n|1;let l=x(t[0]),m=Math.floor(l/9)+1,b=l%9+1,i=(x(t[1])+1)/166,u=new Array(b*m);for(let o=0;o<u.length;o++)if(o===0){let a=x(t.substring(2,6));u[o]=z(a)}else{let a=x(t.substring(4+o*2,6+o*2));u[o]=L(a,i*n)}let c=e*4,s=new Uint8ClampedArray(c*r);for(let o=0;o<r;o++)for(let a=0;a<e;a++){let y=0,B=0,R=0;for(let w=0;w<m;w++)for(let P=0;P<b;P++){let G=Math.cos(Math.PI*a*P/e)*Math.cos(Math.PI*o*w/r),T=u[P+w*b];y+=T[0]*G,B+=T[1]*G,R+=T[2]*G}let V=h(y),I=h(B),E=h(R);s[4*a+0+o*c]=V,s[4*a+1+o*c]=I,s[4*a+2+o*c]=E,s[4*a+3+o*c]=255}return s},j=U;var A=4,D=(t,e,r,n)=>{let l=0,m=0,b=0,g=e*A;for(let u=0;u<e;u++){let c=A*u;for(let s=0;s<r;s++){let o=c+s*g,a=n(u,s);l+=a*f(t[o]),m+=a*f(t[o+1]),b+=a*f(t[o+2])}}let i=1/(e*r);return[l*i,m*i,b*i]},$=t=>{let e=h(t[0]),r=h(t[1]),n=h(t[2]);return(e<<16)+(r<<8)+n},H=(t,e)=>{let r=Math.floor(Math.max(0,Math.min(18,Math.floor(M(t[0]/e,.5)*9+9.5)))),n=Math.floor(Math.max(0,Math.min(18,Math.floor(M(t[1]/e,.5)*9+9.5)))),l=Math.floor(Math.max(0,Math.min(18,Math.floor(M(t[2]/e,.5)*9+9.5))));return r*19*19+n*19+l},O=(t,e,r,n,l)=>{if(n<1||n>9||l<1||l>9)throw new d("BlurHash must have between 1 and 9 components");if(e*r*4!==t.length)throw new d("Width and height must match the pixels array");let m=[];for(let s=0;s<l;s++)for(let o=0;o<n;o++){let a=o==0&&s==0?1:2,y=D(t,e,r,(B,R)=>a*Math.cos(Math.PI*o*B/e)*Math.cos(Math.PI*s*R/r));m.push(y)}let b=m[0],g=m.slice(1),i="",u=n-1+(l-1)*9;i+=p(u,1);let c;if(g.length>0){let s=Math.max(...g.map(a=>Math.max(...a))),o=Math.floor(Math.max(0,Math.min(82,Math.floor(s*166-.5))));c=(o+1)/166,i+=p(o,1)}else c=1,i+=p(0,1);return i+=p($(b),4),g.forEach(s=>{i+=p(H(s,c),2)}),i},S=O;export{d as ValidationError,j as decode,S as encode,N as isBlurhashValid};
|
||||||
|
-//# sourceMappingURL=index.mjs.map
|
||||||
|
\ No newline at end of file
|
||||||
|
+var A=["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","#","$","%","*","+",",","-",".",":",";","=","?","@","[","]","^","_","{","|","}","~"],d=t=>{let r=0;for(let a=0;a<t.length;a++){let l=t[a],o=A.indexOf(l);r=r*83+o}return r},b=(t,r)=>{var a="";for(let l=1;l<=r;l++){let o=Math.floor(t)/Math.pow(83,r-l)%83;a+=A[Math.floor(o)]}return a},c=t=>{let r=t/255;return r<=.04045?r/12.92:Math.pow((r+.055)/1.055,2.4)},g=t=>{let r=Math.max(0,Math.min(1,t));return r<=.0031308?Math.trunc(r*12.92*255+.5):Math.trunc((1.055*Math.pow(r,.4166666666666667)-.055)*255+.5)},O=t=>t<0?-1:1,w=(t,r)=>O(t)*Math.pow(Math.abs(t),r),p=class extends Error{constructor(t){super(t),this.name="ValidationError",this.message=t}},B=t=>{if(!t||t.length<6)throw new p("The blurhash string must be at least 6 characters");let r=d(t[0]),a=Math.floor(r/9)+1,l=r%9+1;if(t.length!==4+2*l*a)throw new p(`blurhash length mismatch: length is ${t.length} but it should be ${4+2*l*a}`)},R=t=>{try{B(t)}catch(r){return{result:!1,errorReason:r.message}}return{result:!0}},T=t=>{let r=t>>16,a=t>>8&255,l=t&255;return[c(r),c(a),c(l)]},U=(t,r)=>{let a=Math.floor(t/361),l=Math.floor(t/19)%19,o=t%19;return[w((a-9)/9,2)*r,w((l-9)/9,2)*r,w((o-9)/9,2)*r]},j=(t,r,a,l)=>{B(t),l=l|1;let o=d(t[0]),i=Math.floor(o/9)+1,u=o%9+1,m=(d(t[1])+1)/166,n=new Array(u*i);for(let e=0;e<n.length;e++)if(e===0){let h=d(t.substring(2,6));n[e]=T(h)}else{let h=d(t.substring(4+e*2,6+e*2));n[e]=U(h,m*l)}let s=r*4,M=new Uint8ClampedArray(s*a);for(let e=0;e<a;e++)for(let h=0;h<r;h++){let f=0,x=0,v=0;for(let y=0;y<i;y++)for(let E=0;E<u;E++){let P=Math.cos(Math.PI*h*E/r)*Math.cos(Math.PI*e*y/a),V=n[E+y*u];f+=V[0]*P,x+=V[1]*P,v+=V[2]*P}let I=g(f),C=g(x),H=g(v);M[4*h+0+e*s]=I,M[4*h+1+e*s]=C,M[4*h+2+e*s]=H,M[4*h+3+e*s]=255}return M},q=j,$=4,z=(t,r,a,l)=>{let o=0,i=0,u=0,m=r*$;for(let s=0;s<r;s++){let M=$*s;for(let e=0;e<a;e++){let h=M+e*m,f=l(s,e);o+=f*c(t[h]),i+=f*c(t[h+1]),u+=f*c(t[h+2])}}let n=1/(r*a);return[o*n,i*n,u*n]},D=t=>{let r=g(t[0]),a=g(t[1]),l=g(t[2]);return(r<<16)+(a<<8)+l},F=(t,r)=>{let a=Math.floor(Math.max(0,Math.min(18,Math.floor(w(t[0]/r,.5)*9+9.5)))),l=Math.floor(Math.max(0,Math.min(18,Math.floor(w(t[1]/r,.5)*9+9.5)))),o=Math.floor(Math.max(0,Math.min(18,Math.floor(w(t[2]/r,.5)*9+9.5))));return a*19*19+l*19+o},G=(t,r,a,l,o)=>{if(l<1||l>9||o<1||o>9)throw new p("BlurHash must have between 1 and 9 components");if(Math.floor(r*a*4)!==t.length)throw new p("Width and height must match the pixels array");let i=[];for(let e=0;e<o;e++)for(let h=0;h<l;h++){let f=h==0&&e==0?1:2,x=z(t,r,a,(v,I)=>f*Math.cos(Math.PI*h*v/r)*Math.cos(Math.PI*e*I/a));i.push(x)}let u=i[0],m=i.slice(1),n="",s=l-1+(o-1)*9;n+=b(s,1);let M;if(m.length>0){let e=Math.max(...m.map(f=>Math.max(...f))),h=Math.floor(Math.max(0,Math.min(82,Math.floor(e*166-.5))));M=(h+1)/166,n+=b(h,1)}else M=1,n+=b(0,1);return n+=b(D(u),4),m.forEach(e=>{n+=b(F(e,M),2)}),n},L=G;export{p as ValidationError,q as decode,L as encode,R as isBlurhashValid};
|
||||||
762
pnpm-lock.yaml
generated
762
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
2
pnpm-workspace.yaml
Normal file
2
pnpm-workspace.yaml
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
patchedDependencies:
|
||||||
|
blurhash: patches/blurhash.patch
|
||||||
@@ -8,7 +8,7 @@ export async function getUser(aniListToken: string): Promise<User> {
|
|||||||
|
|
||||||
let data;
|
let data;
|
||||||
try {
|
try {
|
||||||
data = await stub.getUser(aniListToken);
|
data = await stub.getUserProfile(aniListToken);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
if (e.message.includes("401")) {
|
if (e.message.includes("401")) {
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -1,142 +0,0 @@
|
|||||||
import { graphql } from "gql.tada";
|
|
||||||
import { GraphQLClient } from "graphql-request";
|
|
||||||
|
|
||||||
import { sleep } from "~/libs/sleep";
|
|
||||||
|
|
||||||
const GetWatchingTitlesQuery = graphql(`
|
|
||||||
query GetWatchingTitles($userName: String!, $page: Int!) {
|
|
||||||
Page(page: $page, perPage: 50) {
|
|
||||||
mediaList(
|
|
||||||
userName: $userName
|
|
||||||
type: ANIME
|
|
||||||
sort: UPDATED_TIME_DESC
|
|
||||||
status_in: [CURRENT, REPEATING, PLANNING]
|
|
||||||
) {
|
|
||||||
media {
|
|
||||||
id
|
|
||||||
idMal
|
|
||||||
title {
|
|
||||||
english
|
|
||||||
userPreferred
|
|
||||||
}
|
|
||||||
description
|
|
||||||
episodes
|
|
||||||
genres
|
|
||||||
status
|
|
||||||
bannerImage
|
|
||||||
averageScore
|
|
||||||
coverImage {
|
|
||||||
extraLarge
|
|
||||||
large
|
|
||||||
medium
|
|
||||||
}
|
|
||||||
countryOfOrigin
|
|
||||||
mediaListEntry {
|
|
||||||
id
|
|
||||||
progress
|
|
||||||
status
|
|
||||||
updatedAt
|
|
||||||
}
|
|
||||||
nextAiringEpisode {
|
|
||||||
timeUntilAiring
|
|
||||||
airingAt
|
|
||||||
episode
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pageInfo {
|
|
||||||
currentPage
|
|
||||||
hasNextPage
|
|
||||||
perPage
|
|
||||||
total
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`);
|
|
||||||
|
|
||||||
export function getWatchingTitles(
|
|
||||||
username: string,
|
|
||||||
page: number,
|
|
||||||
aniListToken: string,
|
|
||||||
): Promise<GetWatchingTitles> {
|
|
||||||
const client = new GraphQLClient("https://graphql.anilist.co/");
|
|
||||||
|
|
||||||
return client
|
|
||||||
.request(
|
|
||||||
GetWatchingTitlesQuery,
|
|
||||||
{ userName: username, page },
|
|
||||||
{ Authorization: `Bearer ${aniListToken}` },
|
|
||||||
)
|
|
||||||
.then((data) => data?.Page!)
|
|
||||||
.catch((err) => {
|
|
||||||
console.error("Failed to get watching titles");
|
|
||||||
console.error(err);
|
|
||||||
|
|
||||||
const response = err.response;
|
|
||||||
if (response.status === 429) {
|
|
||||||
console.log("429, retrying in", response.headers.get("Retry-After"));
|
|
||||||
return sleep(Number(response.headers.get("Retry-After")!) * 1000).then(
|
|
||||||
() => getWatchingTitles(username, page, aniListToken),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw err;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
type GetWatchingTitles = {
|
|
||||||
mediaList:
|
|
||||||
| ({
|
|
||||||
media: {
|
|
||||||
id: number;
|
|
||||||
idMal: number | null;
|
|
||||||
title: {
|
|
||||||
english: string | null;
|
|
||||||
userPreferred: string | null;
|
|
||||||
} | null;
|
|
||||||
description: string | null;
|
|
||||||
episodes: number | null;
|
|
||||||
genres: (string | null)[] | null;
|
|
||||||
status:
|
|
||||||
| "FINISHED"
|
|
||||||
| "RELEASING"
|
|
||||||
| "NOT_YET_RELEASED"
|
|
||||||
| "CANCELLED"
|
|
||||||
| "HIATUS"
|
|
||||||
| null;
|
|
||||||
bannerImage: string | null;
|
|
||||||
averageScore: number | null;
|
|
||||||
coverImage: {
|
|
||||||
extraLarge: string | null;
|
|
||||||
large: string | null;
|
|
||||||
medium: string | null;
|
|
||||||
} | null;
|
|
||||||
countryOfOrigin: unknown;
|
|
||||||
mediaListEntry: {
|
|
||||||
id: number;
|
|
||||||
progress: number | null;
|
|
||||||
status:
|
|
||||||
| "CURRENT"
|
|
||||||
| "REPEATING"
|
|
||||||
| "PLANNING"
|
|
||||||
| "COMPLETED"
|
|
||||||
| "DROPPED"
|
|
||||||
| "PAUSED"
|
|
||||||
| null;
|
|
||||||
updatedAt: number;
|
|
||||||
} | null;
|
|
||||||
nextAiringEpisode: {
|
|
||||||
timeUntilAiring: number;
|
|
||||||
airingAt: number;
|
|
||||||
episode: number;
|
|
||||||
} | null;
|
|
||||||
} | null;
|
|
||||||
} | null)[]
|
|
||||||
| null;
|
|
||||||
pageInfo: {
|
|
||||||
currentPage: number | null;
|
|
||||||
hasNextPage: boolean | null;
|
|
||||||
perPage: number | null;
|
|
||||||
total: number | null;
|
|
||||||
} | null;
|
|
||||||
};
|
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
||||||
|
import { env } from "cloudflare:workers";
|
||||||
import { streamSSE } from "hono/streaming";
|
import { streamSSE } from "hono/streaming";
|
||||||
|
|
||||||
import { fetchEpisodes } from "~/controllers/episodes/getByAniListId";
|
import { fetchEpisodes } from "~/controllers/episodes/getByAniListId";
|
||||||
@@ -10,7 +11,6 @@ import { ErrorResponse, ErrorResponseSchema } from "~/types/schema";
|
|||||||
import { Title } from "~/types/title";
|
import { Title } from "~/types/title";
|
||||||
|
|
||||||
import { getUser } from "./getUser";
|
import { getUser } from "./getUser";
|
||||||
import { getWatchingTitles } from "./getWatchingTitles";
|
|
||||||
|
|
||||||
const UserSchema = z.object({
|
const UserSchema = z.object({
|
||||||
name: z.string(),
|
name: z.string(),
|
||||||
@@ -129,11 +129,15 @@ app.openapi(route, async (c) => {
|
|||||||
let hasNextPage = true;
|
let hasNextPage = true;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
const { mediaList, pageInfo } = await getWatchingTitles(
|
const stub = env.ANILIST_DO.getByName(user.name!);
|
||||||
user.name!,
|
const { mediaList, pageInfo } = await stub
|
||||||
currentPage++,
|
.getTitles(
|
||||||
aniListToken,
|
user.name!,
|
||||||
);
|
currentPage,
|
||||||
|
["CURRENT", "PLANNING", "PAUSED", "REPEATING"],
|
||||||
|
aniListToken,
|
||||||
|
)
|
||||||
|
.then((data) => data!);
|
||||||
if (!mediaList) {
|
if (!mediaList) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,22 +8,22 @@ export async function getSourcesFromAniwatch(
|
|||||||
console.log(`Fetching sources from aniwatch for ${watchId}`);
|
console.log(`Fetching sources from aniwatch for ${watchId}`);
|
||||||
const url = await getEpisodeUrl(watchId);
|
const url = await getEpisodeUrl(watchId);
|
||||||
if (url) {
|
if (url) {
|
||||||
return url;
|
return { success: true, result: url };
|
||||||
}
|
}
|
||||||
|
|
||||||
const servers = await getEpisodeServers(watchId);
|
const servers = await getEpisodeServers(watchId);
|
||||||
if (servers.length === 0) {
|
if (servers.length === 0) {
|
||||||
return null;
|
return { success: false };
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const server of servers) {
|
for (const server of servers) {
|
||||||
const url = await getEpisodeUrl(watchId, server.serverName);
|
const url = await getEpisodeUrl(watchId, server.serverName);
|
||||||
if (url) {
|
if (url) {
|
||||||
return url;
|
return { success: true, result: url };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return { success: false };
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getEpisodeUrl(watchId: string, server?: string) {
|
async function getEpisodeUrl(watchId: string, server?: string) {
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ app.openapi(route, async (c) => {
|
|||||||
isComplete,
|
isComplete,
|
||||||
);
|
);
|
||||||
if (isComplete) {
|
if (isComplete) {
|
||||||
await updateWatchStatus(c.req, deviceId, aniListId, "COMPLETED");
|
await updateWatchStatus(deviceId, aniListId, "COMPLETED");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
|
|||||||
@@ -66,7 +66,6 @@ const route = createRoute({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export async function updateWatchStatus(
|
export async function updateWatchStatus(
|
||||||
req: HonoRequest,
|
|
||||||
deviceId: string,
|
deviceId: string,
|
||||||
titleId: number,
|
titleId: number,
|
||||||
watchStatus: WatchStatus | null,
|
watchStatus: WatchStatus | null,
|
||||||
|
|||||||
22
src/graphql/context.ts
Normal file
22
src/graphql/context.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import type { Context as HonoContext } from "hono";
|
||||||
|
|
||||||
|
export interface GraphQLContext {
|
||||||
|
db: D1Database;
|
||||||
|
env: Env;
|
||||||
|
deviceId?: string;
|
||||||
|
aniListToken?: string;
|
||||||
|
honoContext: HonoContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createGraphQLContext(c: HonoContext): GraphQLContext {
|
||||||
|
const deviceId = c.req.header("X-Device-ID");
|
||||||
|
const aniListToken = c.req.header("X-AniList-Token");
|
||||||
|
|
||||||
|
return {
|
||||||
|
db: c.env.DB,
|
||||||
|
env: c.env,
|
||||||
|
deviceId,
|
||||||
|
aniListToken,
|
||||||
|
honoContext: c,
|
||||||
|
};
|
||||||
|
}
|
||||||
41
src/graphql/index.ts
Normal file
41
src/graphql/index.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import { createSchema, createYoga } from "graphql-yoga";
|
||||||
|
import { Hono } from "hono";
|
||||||
|
|
||||||
|
import { createGraphQLContext } from "./context";
|
||||||
|
import { resolvers } from "./resolvers";
|
||||||
|
import { typeDefs } from "./schema";
|
||||||
|
|
||||||
|
const schema = createSchema({
|
||||||
|
typeDefs,
|
||||||
|
resolvers,
|
||||||
|
});
|
||||||
|
|
||||||
|
const yoga = createYoga({
|
||||||
|
schema,
|
||||||
|
graphqlEndpoint: "/graphql",
|
||||||
|
landingPage: false, // Disable landing page for production
|
||||||
|
graphiql: {
|
||||||
|
title: "Aniplay GraphQL API",
|
||||||
|
},
|
||||||
|
context: ({ request }) => {
|
||||||
|
// Extract Hono context from the request
|
||||||
|
// graphql-yoga passes the raw request, but we need Hono context
|
||||||
|
// This will be provided when we integrate with Hono
|
||||||
|
return request as any;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const app = new Hono<Cloudflare.Env>();
|
||||||
|
|
||||||
|
app.all("/", async (c) => {
|
||||||
|
const graphqlContext = createGraphQLContext(c);
|
||||||
|
|
||||||
|
// Create a custom request object that includes our GraphQL context
|
||||||
|
const request = c.req.raw.clone();
|
||||||
|
(request as any).graphqlContext = graphqlContext;
|
||||||
|
|
||||||
|
const response = await yoga.fetch(request, graphqlContext);
|
||||||
|
return response;
|
||||||
|
});
|
||||||
|
|
||||||
|
export default app;
|
||||||
47
src/graphql/resolvers/image.ts
Normal file
47
src/graphql/resolvers/image.ts
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
import { encode } from "blurhash";
|
||||||
|
import type { UintArrRet } from "jpeg-js";
|
||||||
|
import type { PNGWithMetadata } from "pngjs";
|
||||||
|
|
||||||
|
export async function imageResolver(
|
||||||
|
parent:
|
||||||
|
| string
|
||||||
|
| null
|
||||||
|
| undefined
|
||||||
|
| { extraLarge?: string; large?: string; medium?: string },
|
||||||
|
) {
|
||||||
|
const imageUrl =
|
||||||
|
typeof parent === "string"
|
||||||
|
? parent
|
||||||
|
: (parent?.extraLarge ?? parent?.large ?? parent?.medium);
|
||||||
|
if (!imageUrl) {
|
||||||
|
return { url: imageUrl };
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
url: imageUrl,
|
||||||
|
placeholder: await generateImagePlaceholder(imageUrl),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function generateImagePlaceholder(imageUrl: string) {
|
||||||
|
const imageBuffer = await fetch(imageUrl).then((res) => res.arrayBuffer());
|
||||||
|
let pixels: PNGWithMetadata | UintArrRet;
|
||||||
|
|
||||||
|
if (imageUrl.endsWith(".png")) {
|
||||||
|
const { PNG } = await import("pngjs");
|
||||||
|
pixels = PNG.sync.read(Buffer.from(imageBuffer));
|
||||||
|
} else if (imageUrl.endsWith(".jpg")) {
|
||||||
|
const jpeg = await import("jpeg-js");
|
||||||
|
pixels = jpeg.decode(imageBuffer, { formatAsRGBA: true, useTArray: true });
|
||||||
|
} else {
|
||||||
|
throw new Error(`Unsupported image format: ${imageUrl.split(".").pop()}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return encode(
|
||||||
|
new Uint8ClampedArray(pixels.data),
|
||||||
|
pixels.width,
|
||||||
|
pixels.height,
|
||||||
|
4,
|
||||||
|
3,
|
||||||
|
);
|
||||||
|
}
|
||||||
27
src/graphql/resolvers/index.ts
Normal file
27
src/graphql/resolvers/index.ts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import { markEpisodeAsWatchedMutation } from "./mutations/markEpisodeAsWatched";
|
||||||
|
import { updateTokenMutation } from "./mutations/updateToken";
|
||||||
|
import { updateWatchStatusMutation } from "./mutations/updateWatchStatus";
|
||||||
|
import { episodeStream } from "./queries/episodeStream";
|
||||||
|
import { healthCheck } from "./queries/healthCheck";
|
||||||
|
import { popularBrowse } from "./queries/popularBrowse";
|
||||||
|
import { popularByCategory } from "./queries/popularByCategory";
|
||||||
|
import { search } from "./queries/search";
|
||||||
|
import { title } from "./queries/title";
|
||||||
|
import { Title } from "./title";
|
||||||
|
|
||||||
|
export const resolvers = {
|
||||||
|
Query: {
|
||||||
|
healthCheck,
|
||||||
|
title,
|
||||||
|
search,
|
||||||
|
popularBrowse,
|
||||||
|
popularByCategory,
|
||||||
|
episodeStream,
|
||||||
|
},
|
||||||
|
Mutation: {
|
||||||
|
updateWatchStatus: updateWatchStatusMutation,
|
||||||
|
markEpisodeAsWatched: markEpisodeAsWatchedMutation,
|
||||||
|
updateToken: updateTokenMutation,
|
||||||
|
},
|
||||||
|
Title,
|
||||||
|
};
|
||||||
55
src/graphql/resolvers/mutations/markEpisodeAsWatched.ts
Normal file
55
src/graphql/resolvers/mutations/markEpisodeAsWatched.ts
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
import { GraphQLError } from "graphql";
|
||||||
|
|
||||||
|
import { markEpisodeAsWatched } from "~/controllers/episodes/markEpisodeAsWatched/anilist";
|
||||||
|
|
||||||
|
import type { GraphQLContext } from "../../context";
|
||||||
|
|
||||||
|
interface MarkEpisodeAsWatchedInput {
|
||||||
|
titleId: number;
|
||||||
|
episodeNumber: number;
|
||||||
|
isComplete: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface MarkEpisodeAsWatchedArgs {
|
||||||
|
input: MarkEpisodeAsWatchedInput;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function markEpisodeAsWatchedMutation(
|
||||||
|
_parent: unknown,
|
||||||
|
args: MarkEpisodeAsWatchedArgs,
|
||||||
|
context: GraphQLContext,
|
||||||
|
) {
|
||||||
|
const { input } = args;
|
||||||
|
const { aniListToken } = context;
|
||||||
|
|
||||||
|
if (!aniListToken) {
|
||||||
|
throw new GraphQLError(
|
||||||
|
"AniList token is required. Please provide X-AniList-Token header.",
|
||||||
|
{
|
||||||
|
extensions: { code: "UNAUTHORIZED" },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const user = await markEpisodeAsWatched(
|
||||||
|
aniListToken,
|
||||||
|
input.titleId,
|
||||||
|
input.episodeNumber,
|
||||||
|
input.isComplete,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
throw new GraphQLError("Failed to mark episode as watched", {
|
||||||
|
extensions: { code: "INTERNAL_SERVER_ERROR" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error marking episode as watched:", error);
|
||||||
|
throw new GraphQLError("Failed to mark episode as watched", {
|
||||||
|
extensions: { code: "INTERNAL_SERVER_ERROR" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
31
src/graphql/resolvers/mutations/updateToken.ts
Normal file
31
src/graphql/resolvers/mutations/updateToken.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import { getAdminSdkCredentials } from "~/libs/gcloud/getAdminSdkCredentials";
|
||||||
|
import { verifyFcmToken } from "~/libs/gcloud/verifyFcmToken";
|
||||||
|
import { saveToken } from "~/models/token";
|
||||||
|
|
||||||
|
import type { GraphQLContext } from "../../context";
|
||||||
|
|
||||||
|
export async function updateTokenMutation(
|
||||||
|
_parent: unknown,
|
||||||
|
args: { token: string },
|
||||||
|
context: GraphQLContext,
|
||||||
|
) {
|
||||||
|
const { deviceId } = context;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const isValidToken = await verifyFcmToken(
|
||||||
|
args.token,
|
||||||
|
getAdminSdkCredentials(),
|
||||||
|
);
|
||||||
|
if (!isValidToken) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
await saveToken(deviceId, args.token);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to save token");
|
||||||
|
console.error(error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
44
src/graphql/resolvers/mutations/updateWatchStatus.ts
Normal file
44
src/graphql/resolvers/mutations/updateWatchStatus.ts
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
import { GraphQLError } from "graphql";
|
||||||
|
|
||||||
|
import { updateWatchStatus } from "~/controllers/watch-status";
|
||||||
|
import type { WatchStatus } from "~/types/title/watchStatus";
|
||||||
|
|
||||||
|
import type { GraphQLContext } from "../../context";
|
||||||
|
|
||||||
|
interface UpdateWatchStatusInput {
|
||||||
|
titleId: number;
|
||||||
|
watchStatus: WatchStatus | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface UpdateWatchStatusArgs {
|
||||||
|
input: UpdateWatchStatusInput;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateWatchStatusMutation(
|
||||||
|
_parent: unknown,
|
||||||
|
args: UpdateWatchStatusArgs,
|
||||||
|
context: GraphQLContext,
|
||||||
|
) {
|
||||||
|
const { input } = args;
|
||||||
|
const { deviceId } = context;
|
||||||
|
|
||||||
|
if (!deviceId) {
|
||||||
|
throw new GraphQLError(
|
||||||
|
"Device ID is required. Please provide X-Device-ID header.",
|
||||||
|
{
|
||||||
|
extensions: { code: "BAD_REQUEST" },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await updateWatchStatus(deviceId, input.titleId, input.watchStatus);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error updating watch status:", error);
|
||||||
|
throw new GraphQLError("Failed to update watch status", {
|
||||||
|
extensions: { code: "INTERNAL_SERVER_ERROR" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
16
src/graphql/resolvers/queries/episodeStream.ts
Normal file
16
src/graphql/resolvers/queries/episodeStream.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { fetchEpisodeUrl } from "~/controllers/episodes/getEpisodeUrl";
|
||||||
|
|
||||||
|
import type { GraphQLContext } from "../../context";
|
||||||
|
|
||||||
|
export async function episodeStream(
|
||||||
|
_parent: unknown,
|
||||||
|
args: { id: string },
|
||||||
|
context: GraphQLContext,
|
||||||
|
) {
|
||||||
|
const episodeUrl = await fetchEpisodeUrl({ id: args.id });
|
||||||
|
if (!episodeUrl || !episodeUrl.success) {
|
||||||
|
throw new Error("Failed to fetch episode URL");
|
||||||
|
}
|
||||||
|
|
||||||
|
return { ...episodeUrl.result, url: episodeUrl.result.source };
|
||||||
|
}
|
||||||
9
src/graphql/resolvers/queries/healthCheck.ts
Normal file
9
src/graphql/resolvers/queries/healthCheck.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import type { GraphQLContext } from "../../context";
|
||||||
|
|
||||||
|
export function healthCheck(
|
||||||
|
_parent: unknown,
|
||||||
|
_args: unknown,
|
||||||
|
_context: GraphQLContext,
|
||||||
|
): boolean {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
31
src/graphql/resolvers/queries/popularBrowse.ts
Normal file
31
src/graphql/resolvers/queries/popularBrowse.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import { GraphQLError } from "graphql";
|
||||||
|
|
||||||
|
import { fetchPopularTitlesFromAnilist } from "~/controllers/popular/browse/anilist";
|
||||||
|
|
||||||
|
import type { GraphQLContext } from "../../context";
|
||||||
|
|
||||||
|
interface PopularBrowseArgs {
|
||||||
|
limit?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function popularBrowse(
|
||||||
|
_parent: unknown,
|
||||||
|
args: PopularBrowseArgs,
|
||||||
|
_context: GraphQLContext,
|
||||||
|
) {
|
||||||
|
const { limit = 10 } = args;
|
||||||
|
|
||||||
|
const response = await fetchPopularTitlesFromAnilist(limit);
|
||||||
|
|
||||||
|
if (!response) {
|
||||||
|
throw new GraphQLError("Failed to fetch popular titles", {
|
||||||
|
extensions: { code: "INTERNAL_SERVER_ERROR" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
trending: response.trending || [],
|
||||||
|
popular: response.popular || [],
|
||||||
|
upcoming: response.upcoming || [],
|
||||||
|
};
|
||||||
|
}
|
||||||
33
src/graphql/resolvers/queries/popularByCategory.ts
Normal file
33
src/graphql/resolvers/queries/popularByCategory.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import { GraphQLError } from "graphql";
|
||||||
|
|
||||||
|
import { fetchPopularTitlesFromAnilist } from "~/controllers/popular/category/anilist";
|
||||||
|
import type { PopularCategory } from "~/controllers/popular/category/enum";
|
||||||
|
|
||||||
|
import type { GraphQLContext } from "../../context";
|
||||||
|
|
||||||
|
interface PopularByCategoryArgs {
|
||||||
|
category: PopularCategory;
|
||||||
|
page?: number;
|
||||||
|
limit?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function popularByCategory(
|
||||||
|
_parent: unknown,
|
||||||
|
args: PopularByCategoryArgs,
|
||||||
|
_context: GraphQLContext,
|
||||||
|
) {
|
||||||
|
const { category, page = 1, limit = 10 } = args;
|
||||||
|
|
||||||
|
const response = await fetchPopularTitlesFromAnilist(category, page, limit);
|
||||||
|
|
||||||
|
if (!response) {
|
||||||
|
throw new GraphQLError(`Failed to fetch ${category} titles`, {
|
||||||
|
extensions: { code: "INTERNAL_SERVER_ERROR" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
results: response.results || [],
|
||||||
|
hasNextPage: response.hasNextPage ?? false,
|
||||||
|
};
|
||||||
|
}
|
||||||
30
src/graphql/resolvers/queries/search.ts
Normal file
30
src/graphql/resolvers/queries/search.ts
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import { fetchSearchResultsFromAnilist } from "~/controllers/search/anilist";
|
||||||
|
|
||||||
|
import type { GraphQLContext } from "../../context";
|
||||||
|
|
||||||
|
interface SearchArgs {
|
||||||
|
query: string;
|
||||||
|
page?: number;
|
||||||
|
limit?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function search(
|
||||||
|
_parent: unknown,
|
||||||
|
args: SearchArgs,
|
||||||
|
_context: GraphQLContext,
|
||||||
|
) {
|
||||||
|
const { query, page = 1, limit = 10 } = args;
|
||||||
|
|
||||||
|
const response = await fetchSearchResultsFromAnilist(query, page, limit);
|
||||||
|
if (!response) {
|
||||||
|
return {
|
||||||
|
results: [],
|
||||||
|
hasNextPage: false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
results: response.results || [],
|
||||||
|
hasNextPage: response.hasNextPage ?? false,
|
||||||
|
};
|
||||||
|
}
|
||||||
33
src/graphql/resolvers/queries/title.ts
Normal file
33
src/graphql/resolvers/queries/title.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import { GraphQLError } from "graphql";
|
||||||
|
|
||||||
|
import { fetchTitleFromAnilist } from "~/libs/anilist/getTitle";
|
||||||
|
|
||||||
|
import type { GraphQLContext } from "../../context";
|
||||||
|
|
||||||
|
interface TitleArgs {
|
||||||
|
id: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function title(
|
||||||
|
_parent: unknown,
|
||||||
|
args: TitleArgs,
|
||||||
|
context: GraphQLContext,
|
||||||
|
) {
|
||||||
|
const { id } = args;
|
||||||
|
const { aniListToken } = context;
|
||||||
|
|
||||||
|
// Fetch title
|
||||||
|
const titleData = await fetchTitleFromAnilist(id, aniListToken);
|
||||||
|
|
||||||
|
if (!titleData) {
|
||||||
|
throw new GraphQLError(`Title with id ${id} not found`, {
|
||||||
|
extensions: { code: "NOT_FOUND" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...titleData,
|
||||||
|
title: titleData.title?.userPreferred ?? titleData.title?.english,
|
||||||
|
numEpisodes: titleData.episodes,
|
||||||
|
};
|
||||||
|
}
|
||||||
12
src/graphql/resolvers/title.ts
Normal file
12
src/graphql/resolvers/title.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import { fetchEpisodes } from "~/controllers/episodes/getByAniListId";
|
||||||
|
import type { Title as TitleType } from "~/types/title";
|
||||||
|
|
||||||
|
import { imageResolver } from "./image";
|
||||||
|
|
||||||
|
export const Title = {
|
||||||
|
episodes: async (parent: { id: number }) => await fetchEpisodes(parent.id),
|
||||||
|
coverImage: async (parent: TitleType) =>
|
||||||
|
await imageResolver(parent.coverImage),
|
||||||
|
bannerImage: async (parent: TitleType) =>
|
||||||
|
await imageResolver(parent.bannerImage),
|
||||||
|
};
|
||||||
206
src/graphql/schema.ts
Normal file
206
src/graphql/schema.ts
Normal file
@@ -0,0 +1,206 @@
|
|||||||
|
export const typeDefs = /* GraphQL */ `
|
||||||
|
# ====================
|
||||||
|
# Scalars & Enums
|
||||||
|
# ====================
|
||||||
|
|
||||||
|
scalar JSONObject
|
||||||
|
|
||||||
|
enum WatchStatus {
|
||||||
|
COMPLETED
|
||||||
|
CURRENT
|
||||||
|
PLANNING
|
||||||
|
DROPPED
|
||||||
|
PAUSED
|
||||||
|
REPEATING
|
||||||
|
}
|
||||||
|
|
||||||
|
enum MediaStatus {
|
||||||
|
FINISHED
|
||||||
|
RELEASING
|
||||||
|
NOT_YET_RELEASED
|
||||||
|
CANCELLED
|
||||||
|
HIATUS
|
||||||
|
}
|
||||||
|
|
||||||
|
enum PopularCategory {
|
||||||
|
TRENDING
|
||||||
|
POPULAR
|
||||||
|
UPCOMING
|
||||||
|
}
|
||||||
|
|
||||||
|
# ====================
|
||||||
|
# Title Types
|
||||||
|
# ====================
|
||||||
|
|
||||||
|
type Image {
|
||||||
|
url: String
|
||||||
|
placeholder: String
|
||||||
|
}
|
||||||
|
|
||||||
|
type NextAiringEpisode {
|
||||||
|
episode: Int!
|
||||||
|
airingAt: Int!
|
||||||
|
timeUntilAiring: Int!
|
||||||
|
}
|
||||||
|
|
||||||
|
type MediaListEntry {
|
||||||
|
status: WatchStatus
|
||||||
|
progress: Int
|
||||||
|
id: Int!
|
||||||
|
updatedAt: Int
|
||||||
|
}
|
||||||
|
|
||||||
|
type Episode {
|
||||||
|
id: String!
|
||||||
|
number: Float!
|
||||||
|
title: String
|
||||||
|
img: String
|
||||||
|
description: String
|
||||||
|
rating: Int
|
||||||
|
updatedAt: Int!
|
||||||
|
}
|
||||||
|
|
||||||
|
type Title {
|
||||||
|
id: Int!
|
||||||
|
idMal: Int
|
||||||
|
title: String!
|
||||||
|
description: String
|
||||||
|
numEpisodes: Int
|
||||||
|
genres: [String]
|
||||||
|
status: MediaStatus
|
||||||
|
bannerImage: Image
|
||||||
|
averageScore: Int
|
||||||
|
coverImage: Image
|
||||||
|
countryOfOrigin: String!
|
||||||
|
mediaListEntry: MediaListEntry
|
||||||
|
nextAiringEpisode: NextAiringEpisode
|
||||||
|
episodes: [Episode!]!
|
||||||
|
}
|
||||||
|
|
||||||
|
# ====================
|
||||||
|
# Home/Preview Title Type (simplified)
|
||||||
|
# ====================
|
||||||
|
|
||||||
|
type HomeTitle {
|
||||||
|
id: Int!
|
||||||
|
idMal: Int
|
||||||
|
title: String!
|
||||||
|
description: String
|
||||||
|
numEpisodes: Int
|
||||||
|
genres: [String]
|
||||||
|
status: MediaStatus
|
||||||
|
bannerImage: String
|
||||||
|
averageScore: Int
|
||||||
|
coverImage: Image
|
||||||
|
countryOfOrigin: String!
|
||||||
|
}
|
||||||
|
|
||||||
|
# ====================
|
||||||
|
# Response Types
|
||||||
|
# ====================
|
||||||
|
|
||||||
|
type SearchResult {
|
||||||
|
results: [HomeTitle!]!
|
||||||
|
hasNextPage: Boolean!
|
||||||
|
}
|
||||||
|
|
||||||
|
type PopularBrowse {
|
||||||
|
trending: [HomeTitle!]!
|
||||||
|
popular: [HomeTitle!]!
|
||||||
|
upcoming: [HomeTitle!]
|
||||||
|
}
|
||||||
|
|
||||||
|
type PopularResult {
|
||||||
|
results: [HomeTitle!]!
|
||||||
|
hasNextPage: Boolean!
|
||||||
|
}
|
||||||
|
|
||||||
|
type EpisodeStream {
|
||||||
|
url: String!
|
||||||
|
subtitles: [LangUrl!]!
|
||||||
|
audio: [LangUrl!]!
|
||||||
|
intro: [Int!]
|
||||||
|
outro: [Int!]
|
||||||
|
headers: JSONObject
|
||||||
|
}
|
||||||
|
|
||||||
|
type LangUrl {
|
||||||
|
lang: String!
|
||||||
|
url: String!
|
||||||
|
}
|
||||||
|
|
||||||
|
# ====================
|
||||||
|
# Input Types
|
||||||
|
# ====================
|
||||||
|
|
||||||
|
input UpdateWatchStatusInput {
|
||||||
|
titleId: Int!
|
||||||
|
watchStatus: WatchStatus
|
||||||
|
}
|
||||||
|
|
||||||
|
input MarkEpisodeAsWatchedInput {
|
||||||
|
titleId: Int!
|
||||||
|
episodeNumber: Float!
|
||||||
|
}
|
||||||
|
|
||||||
|
# ====================
|
||||||
|
# Queries
|
||||||
|
# ====================
|
||||||
|
|
||||||
|
type Query {
|
||||||
|
"""
|
||||||
|
Simple health check to verify API is running
|
||||||
|
"""
|
||||||
|
healthCheck: Boolean!
|
||||||
|
|
||||||
|
"""
|
||||||
|
Fetch a title by AniList ID
|
||||||
|
"""
|
||||||
|
title(id: Int!): Title!
|
||||||
|
|
||||||
|
"""
|
||||||
|
Fetch an episode stream by ID
|
||||||
|
"""
|
||||||
|
episodeStream(id: String!): EpisodeStream!
|
||||||
|
|
||||||
|
"""
|
||||||
|
Search for titles
|
||||||
|
"""
|
||||||
|
search(query: String!, page: Int = 1, limit: Int = 10): SearchResult!
|
||||||
|
|
||||||
|
"""
|
||||||
|
Browse popular titles across all categories (trending, popular, upcoming)
|
||||||
|
"""
|
||||||
|
popularBrowse(limit: Int = 10): PopularBrowse!
|
||||||
|
|
||||||
|
"""
|
||||||
|
Fetch paginated popular titles for a specific category
|
||||||
|
"""
|
||||||
|
popularByCategory(
|
||||||
|
category: PopularCategory!
|
||||||
|
page: Int = 1
|
||||||
|
limit: Int = 10
|
||||||
|
): PopularResult!
|
||||||
|
}
|
||||||
|
|
||||||
|
# ====================
|
||||||
|
# Mutations
|
||||||
|
# ====================
|
||||||
|
|
||||||
|
type Mutation {
|
||||||
|
"""
|
||||||
|
Update watch status for a title. Device ID must be provided via X-Device-ID header.
|
||||||
|
"""
|
||||||
|
updateWatchStatus(input: UpdateWatchStatusInput!): Boolean!
|
||||||
|
|
||||||
|
"""
|
||||||
|
Mark an episode as watched. Device ID must be provided via X-Device-ID header.
|
||||||
|
"""
|
||||||
|
markEpisodeAsWatched(input: MarkEpisodeAsWatchedInput!): Boolean!
|
||||||
|
|
||||||
|
"""
|
||||||
|
Update the user's FCM token. Device ID must be provided via X-Device-ID header.
|
||||||
|
"""
|
||||||
|
updateToken(token: String!): Boolean!
|
||||||
|
}
|
||||||
|
`;
|
||||||
63
src/index.ts
63
src/index.ts
@@ -1,5 +1,4 @@
|
|||||||
import { swaggerUI } from "@hono/swagger-ui";
|
import { Hono } from "hono";
|
||||||
import { OpenAPIHono } from "@hono/zod-openapi";
|
|
||||||
|
|
||||||
import { maybeUpdateLastConnectedAt } from "~/controllers/maybeUpdateLastConnectedAt";
|
import { maybeUpdateLastConnectedAt } from "~/controllers/maybeUpdateLastConnectedAt";
|
||||||
import type { QueueName } from "~/libs/tasks/queueName.ts";
|
import type { QueueName } from "~/libs/tasks/queueName.ts";
|
||||||
@@ -7,67 +6,15 @@ import type { QueueName } from "~/libs/tasks/queueName.ts";
|
|||||||
import { onNewEpisode } from "./controllers/internal/new-episode";
|
import { onNewEpisode } from "./controllers/internal/new-episode";
|
||||||
import type { QueueBody } from "./libs/tasks/queueTask";
|
import type { QueueBody } from "./libs/tasks/queueTask";
|
||||||
|
|
||||||
const app = new OpenAPIHono();
|
const app = new Hono<Cloudflare.Env>();
|
||||||
|
|
||||||
app.use(maybeUpdateLastConnectedAt);
|
app.use(maybeUpdateLastConnectedAt);
|
||||||
|
|
||||||
|
// GraphQL endpoint replaces all REST routes
|
||||||
app.route(
|
app.route(
|
||||||
"/",
|
"/graphql",
|
||||||
await import("~/controllers/health-check").then(
|
await import("~/graphql").then((module) => module.default),
|
||||||
(controller) => controller.default,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
app.route(
|
|
||||||
"/title",
|
|
||||||
await import("~/controllers/title").then((controller) => controller.default),
|
|
||||||
);
|
|
||||||
app.route(
|
|
||||||
"/episodes",
|
|
||||||
await import("~/controllers/episodes").then(
|
|
||||||
(controller) => controller.default,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
app.route(
|
|
||||||
"/search",
|
|
||||||
await import("~/controllers/search").then((controller) => controller.default),
|
|
||||||
);
|
|
||||||
app.route(
|
|
||||||
"/watch-status",
|
|
||||||
await import("~/controllers/watch-status").then(
|
|
||||||
(controller) => controller.default,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
app.route(
|
|
||||||
"/token",
|
|
||||||
await import("~/controllers/token").then((controller) => controller.default),
|
|
||||||
);
|
|
||||||
app.route(
|
|
||||||
"/auth",
|
|
||||||
await import("~/controllers/auth").then((controller) => controller.default),
|
|
||||||
);
|
|
||||||
app.route(
|
|
||||||
"/popular",
|
|
||||||
await import("~/controllers/popular").then(
|
|
||||||
(controller) => controller.default,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
app.route(
|
|
||||||
"/internal",
|
|
||||||
await import("~/controllers/internal").then(
|
|
||||||
(controller) => controller.default,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
// The OpenAPI documentation will be available at /doc
|
|
||||||
app.doc("/openapi.json", {
|
|
||||||
openapi: "3.0.0",
|
|
||||||
info: {
|
|
||||||
version: "1.0.0",
|
|
||||||
title: "Aniplay API",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get("/docs", swaggerUI({ url: "/openapi.json" }));
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
fetch: app.fetch,
|
fetch: app.fetch,
|
||||||
|
|||||||
@@ -10,7 +10,9 @@ import {
|
|||||||
GetTitleQuery,
|
GetTitleQuery,
|
||||||
GetTrendingTitlesQuery,
|
GetTrendingTitlesQuery,
|
||||||
GetUpcomingTitlesQuery,
|
GetUpcomingTitlesQuery,
|
||||||
|
GetUserProfileQuery,
|
||||||
GetUserQuery,
|
GetUserQuery,
|
||||||
|
GetWatchingTitlesQuery,
|
||||||
MarkEpisodeAsWatchedMutation,
|
MarkEpisodeAsWatchedMutation,
|
||||||
MarkTitleAsWatchedMutation,
|
MarkTitleAsWatchedMutation,
|
||||||
NextSeasonPopularQuery,
|
NextSeasonPopularQuery,
|
||||||
@@ -49,6 +51,9 @@ export class AnilistDurableObject extends DurableObject {
|
|||||||
{ id },
|
{ id },
|
||||||
token,
|
token,
|
||||||
);
|
);
|
||||||
|
if (!anilistResponse) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
// Extract next airing episode for alarm
|
// Extract next airing episode for alarm
|
||||||
const media = anilistResponse.Media as ResultOf<
|
const media = anilistResponse.Media as ResultOf<
|
||||||
@@ -212,7 +217,25 @@ export class AnilistDurableObject extends DurableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getUser(token: string): Promise<any> {
|
async getUser(token: string): Promise<any> {
|
||||||
const data = await this.fetchFromAnilist(GetUserQuery, { token }, token);
|
const storageKey = `user:${token}`;
|
||||||
|
// 1 month
|
||||||
|
const TTL = 60 * 60 * 24 * 30 * 1000;
|
||||||
|
return this.handleCachedRequest(
|
||||||
|
storageKey,
|
||||||
|
async () => {
|
||||||
|
const data = await this.fetchFromAnilist(GetUserQuery, {}, token);
|
||||||
|
return data?.Viewer;
|
||||||
|
},
|
||||||
|
TTL,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async getUserProfile(token: string): Promise<any> {
|
||||||
|
const data = await this.fetchFromAnilist(
|
||||||
|
GetUserProfileQuery,
|
||||||
|
{ token },
|
||||||
|
token,
|
||||||
|
);
|
||||||
return data?.Viewer;
|
return data?.Viewer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -238,6 +261,33 @@ export class AnilistDurableObject extends DurableObject {
|
|||||||
return data?.SaveMediaListEntry;
|
return data?.SaveMediaListEntry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getTitles(
|
||||||
|
userName: string,
|
||||||
|
page: number,
|
||||||
|
statusFilters: (
|
||||||
|
| "CURRENT"
|
||||||
|
| "COMPLETED"
|
||||||
|
| "PLANNING"
|
||||||
|
| "DROPPED"
|
||||||
|
| "PAUSED"
|
||||||
|
| "REPEATING"
|
||||||
|
)[],
|
||||||
|
aniListToken: string,
|
||||||
|
) {
|
||||||
|
return await this.handleCachedRequest(
|
||||||
|
`titles:${JSON.stringify({ page, statusFilters })}`,
|
||||||
|
async () => {
|
||||||
|
const data = await this.fetchFromAnilist(
|
||||||
|
GetWatchingTitlesQuery,
|
||||||
|
{ userName, page, statusFilters },
|
||||||
|
aniListToken,
|
||||||
|
);
|
||||||
|
return data?.Page;
|
||||||
|
},
|
||||||
|
60 * 60 * 1000,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Helper to handle caching logic
|
// Helper to handle caching logic
|
||||||
async handleCachedRequest(
|
async handleCachedRequest(
|
||||||
key: string,
|
key: string,
|
||||||
|
|||||||
@@ -98,6 +98,15 @@ export const MarkTitleAsWatchedMutation = graphql(`
|
|||||||
|
|
||||||
export const GetUserQuery = graphql(`
|
export const GetUserQuery = graphql(`
|
||||||
query GetUser {
|
query GetUser {
|
||||||
|
Viewer {
|
||||||
|
id
|
||||||
|
name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`);
|
||||||
|
|
||||||
|
export const GetUserProfileQuery = graphql(`
|
||||||
|
query GetUserProfile {
|
||||||
Viewer {
|
Viewer {
|
||||||
id
|
id
|
||||||
name
|
name
|
||||||
@@ -257,3 +266,36 @@ export const NextSeasonPopularQuery = graphql(
|
|||||||
`,
|
`,
|
||||||
[HomeTitleFragment],
|
[HomeTitleFragment],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
export const GetWatchingTitlesQuery = graphql(
|
||||||
|
`
|
||||||
|
query GetWatchingTitles(
|
||||||
|
$userName: String!
|
||||||
|
$page: Int!
|
||||||
|
$statusFilters: [MediaListStatus!]
|
||||||
|
) {
|
||||||
|
Page(page: $page, perPage: 50) {
|
||||||
|
mediaList(
|
||||||
|
userName: $userName
|
||||||
|
type: ANIME
|
||||||
|
sort: UPDATED_TIME_DESC
|
||||||
|
status_in: $statusFilters
|
||||||
|
) {
|
||||||
|
media {
|
||||||
|
...Media
|
||||||
|
mediaListEntry {
|
||||||
|
updatedAt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pageInfo {
|
||||||
|
currentPage
|
||||||
|
hasNextPage
|
||||||
|
perPage
|
||||||
|
total
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
[MediaFragment],
|
||||||
|
);
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
import { SkippableSchema, SuccessResponseSchema } from "~/types/schema";
|
import {
|
||||||
|
ErrorResponseSchema,
|
||||||
|
SkippableSchema,
|
||||||
|
SuccessResponseSchema,
|
||||||
|
} from "~/types/schema";
|
||||||
|
|
||||||
export type FetchUrlResponseSchema = z.infer<typeof FetchUrlResponseSchema>;
|
export type FetchUrlResponseSchema = z.infer<typeof FetchUrlResponseSchema>;
|
||||||
export const FetchUrlResponseSchema = z.object({
|
export const FetchUrlResponseSchema = z.object({
|
||||||
@@ -15,4 +19,7 @@ export const FetchUrlResponseSchema = z.object({
|
|||||||
export type FetchUrlResponse = z.infer<typeof FetchUrlResponse> & {
|
export type FetchUrlResponse = z.infer<typeof FetchUrlResponse> & {
|
||||||
result: FetchUrlResponseSchema;
|
result: FetchUrlResponseSchema;
|
||||||
};
|
};
|
||||||
export const FetchUrlResponse = SuccessResponseSchema(FetchUrlResponseSchema);
|
export const FetchUrlResponse = z.union([
|
||||||
|
SuccessResponseSchema(FetchUrlResponseSchema),
|
||||||
|
ErrorResponseSchema,
|
||||||
|
]);
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ export const Episode = z.object({
|
|||||||
img: z.string().nullish(),
|
img: z.string().nullish(),
|
||||||
description: z.string().nullish(),
|
description: z.string().nullish(),
|
||||||
rating: z.number().int().nullish(),
|
rating: z.number().int().nullish(),
|
||||||
updatedAt: z.number().int().default(0).openapi({ format: "int64" }),
|
updatedAt: z.number().int().default(0) /* .openapi({ format: "int64" }) */,
|
||||||
});
|
});
|
||||||
|
|
||||||
export type EpisodesResponse = z.infer<typeof EpisodesResponse>;
|
export type EpisodesResponse = z.infer<typeof EpisodesResponse>;
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { type ZodSchema, z } from "zod";
|
|||||||
|
|
||||||
export const SuccessResponse = { success: true } as const;
|
export const SuccessResponse = { success: true } as const;
|
||||||
export const SuccessResponseSchema = <T extends ZodSchema>(schema?: T) => {
|
export const SuccessResponseSchema = <T extends ZodSchema>(schema?: T) => {
|
||||||
const success = z.literal(true).openapi({ type: "boolean" });
|
const success = z.literal(true); /* .openapi({ type: "boolean" }) */
|
||||||
|
|
||||||
if (!schema) {
|
if (!schema) {
|
||||||
return z.object({ success });
|
return z.object({ success });
|
||||||
@@ -21,25 +21,27 @@ export const PaginatedResponseSchema = <T extends ZodSchema>(schema: T) => {
|
|||||||
|
|
||||||
export const ErrorResponse = { success: false } as const;
|
export const ErrorResponse = { success: false } as const;
|
||||||
export const ErrorResponseSchema = z.object({
|
export const ErrorResponseSchema = z.object({
|
||||||
success: z.literal(false).openapi({ type: "boolean" }),
|
success: z.literal(false) /* .openapi({ type: "boolean" }) */,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const NullableNumberSchema = z.number().int().nullable();
|
export const NullableNumberSchema = z.number().int().nullable();
|
||||||
|
|
||||||
export const AniListIdSchema = z.number().int().openapi({ format: "int64" });
|
export const AniListIdSchema = z
|
||||||
export const AniListIdQuerySchema = z
|
.number()
|
||||||
.string()
|
.int(); /* .openapi({ format: "int64" }) */
|
||||||
.openapi({ type: "integer", format: "int64" });
|
export const AniListIdQuerySchema = z.string();
|
||||||
|
/* .openapi({ type: "integer", format: "int64" }) */ export const EpisodeNumberSchema =
|
||||||
export const EpisodeNumberSchema = z.number().openapi({
|
z.number(); /* .openapi({
|
||||||
minimum: 0,
|
minimum: 0,
|
||||||
multipleOf: 0.5,
|
multipleOf: 0.5,
|
||||||
examples: [1, 2, 3.5],
|
examples: [1, 2, 3.5],
|
||||||
type: "number",
|
type: "number",
|
||||||
format: "float",
|
format: "float",
|
||||||
});
|
}) */
|
||||||
|
|
||||||
export const SkippableSchema = z
|
export const SkippableSchema = z
|
||||||
.array(z.number().openapi({ minimum: 0, type: "integer", format: "int64" }))
|
.array(
|
||||||
.nullish()
|
z.number() /* .openapi({ minimum: 0, type: "integer", format: "int64" }) */,
|
||||||
.openapi({ examples: [[200, 289]], minItems: 2, maxItems: 2 });
|
)
|
||||||
|
.nullish();
|
||||||
|
/* .openapi({ examples: [[200, 289]], minItems: 2, maxItems: 2 }) */
|
||||||
|
|||||||
24
vitest.config.ts
Normal file
24
vitest.config.ts
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
|
||||||
|
|
||||||
|
export default defineWorkersConfig({
|
||||||
|
test: {
|
||||||
|
globals: true,
|
||||||
|
setupFiles: ["./testSetup.ts"],
|
||||||
|
poolOptions: {
|
||||||
|
workers: {
|
||||||
|
wrangler: { configPath: "./wrangler.toml" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
coverage: {
|
||||||
|
provider: "v8",
|
||||||
|
reporter: ["text", "json", "html"],
|
||||||
|
exclude: [
|
||||||
|
"node_modules/**",
|
||||||
|
"dist/**",
|
||||||
|
"**/*.spec.ts",
|
||||||
|
"**/*.d.ts",
|
||||||
|
"**/mocks/**",
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user