UNPKG

@solid-mediakit/auth-plugin

Version:

Package for easily creating server rpc functions in solid-start with goodies baked in

25 lines (24 loc) 5.85 kB
"use strict";var I=Object.create;var b=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var R=Object.getPrototypeOf,T=Object.prototype.hasOwnProperty;var F=(r,n)=>{for(var e in n)b(r,e,{get:n[e],enumerable:!0})},E=(r,n,e,s)=>{if(n&&typeof n=="object"||typeof n=="function")for(let t of w(n))!T.call(r,t)&&t!==e&&b(r,t,{get:()=>n[t],enumerable:!(s=O(n,t))||s.enumerable});return r};var _=(r,n,e)=>(e=r!=null?I(R(r)):{},E(n||!r||!r.__esModule?b(e,"default",{value:r,enumerable:!0}):e,r)),k=r=>E(b({},"__esModule",{value:!0}),r);var S={};F(S,{authVite:()=>B});module.exports=k(S);var j=_(require("@babel/core"),1);var c=require("@solid-mediakit/shared"),$=(r,n)=>{let{callee:e}=r.node,s=n.isIdentifier(e,{name:"protected$"});return{callee:e,isProtected:s}},N=(r,n)=>{let e=r.findParent(t=>t.isProgram()).node.body,s=e.findLast(t=>t.type==="ImportDeclaration");s?e.splice(e.indexOf(s)+1,0,n):e.unshift(n)},P=(r,n,e,s)=>{c.babel.importIfNotThere(r,n,"query","@solidjs/router"),c.babel.importIfNotThere(r,n,"createAsync","@solidjs/router"),c.babel.importIfNotThere(r,n,"getRequestEvent","solid-js/web"),c.babel.importIfNotThere(r,n,"getSession","@solid-mediakit/auth"),c.babel.importIfNotThere(r,n,"Show","solid-js"),c.babel.importIfNotThere(r,n,e.authOpts.name,e.authOpts.dir),s.fallBack||c.babel.importIfNotThere(r,n,"redirect","@solidjs/router")},v=(r,n)=>{let e=[r.jsxAttribute(r.jsxIdentifier("when"),r.jsxExpressionContainer(n.reverse?r.unaryExpression("!",r.optionalMemberExpression(r.callExpression(r.identifier("_$$session"),[]),r.identifier("user"),!1,!0)):r.optionalMemberExpression(r.callExpression(r.identifier("_$$session"),[]),r.identifier("user"),!1,!0)))];return n.fallBack&&e.push(r.jsxAttribute(r.jsxIdentifier("fallback"),r.jsxExpressionContainer(r.jsxElement(r.jsxOpeningElement(r.jsxIdentifier("_$$RenderFallBack"),[],!0),r.jsxClosingElement(r.jsxIdentifier("_$$RenderFallBack")),[],!0)))),r.jsxElement(r.jsxOpeningElement(r.jsxIdentifier("Show"),e),r.jsxClosingElement(r.jsxIdentifier("Show")),[r.jsxElement(r.jsxOpeningElement(r.jsxIdentifier("_$$RenderProtected"),[],!0),r.jsxClosingElement(r.jsxIdentifier("_$$RenderProtected")),[],!0)])},h=(r,n,e,s,t)=>{var a,p;let i=(a=s==null?void 0:s.userKey)!=null?a:"media-user",o;t.fallBack?o=r`const _$$getUser = query(async () => { 'use server' const event = getRequestEvent() return await getSession(event.request, authOptions) }, '${i}'); `():t.reverse?o=r`const _$$getUser = query(async () => { 'use server' const event = getRequestEvent() const session = await getSession(event.request, authOptions) if (session) { throw redirect('${t.redirectTo}') } return null }, '${i}'); `():o=r`const _$$getUser = query(async () => { 'use server' const event = getRequestEvent() const session = await getSession(event.request, authOptions) if (!session) { throw redirect('${t.redirectTo}') } return session }, '${i}'); `(),N(n,o);let u=(p=n.scope.getBinding("route"))==null?void 0:p.path;if(u){let l=u.node.init.properties.find(d=>d.key.name==="load");l&&(l.value=e.arrowFunctionExpression([],e.arrayExpression([e.callExpression(e.identifier("_$$getUser"),[]),e.isArrowFunctionExpression(l.value)?l.value.body:l.value])))}else n.insertAfter(e.exportNamedDeclaration(e.variableDeclaration("const",[e.variableDeclarator(e.identifier("route"),e.objectExpression([e.objectProperty(e.identifier("load"),e.arrowFunctionExpression([],e.callExpression(e.identifier("_$$getUser"),[])))]))])))},A=(r,n,e)=>{let s=r.node.arguments[0],t,i;return r.node.arguments.length>=2&&(n.isArrowFunctionExpression(r.node.arguments[1])?t=r.node.arguments[1]:i=r.node.arguments[1]),i||(i=e.redirectTo),{page:s,fallBack:t,redirectTo:i,reverse:n.isBooleanLiteral(r.node.arguments[2])?r.node.arguments[2].value:!1}};function C(r){return function({types:e,template:s}){return{visitor:{CallExpression(t){let i=$(t,e);if(i.isProtected){let o=A(t,e,r);if(i.isProtected){let u=t.node.arguments[0];t.traverse({Identifier(d){var g,x;d.node.name==="session$"&&((x=(g=d.scope)==null?void 0:g.path)==null?void 0:x.listKey)!=="params"&&(d.node.name="_$$session()")}}),P(t,e,r,o),h(s,t,e,r,o),u.params=[];let a=u.body,p=e.variableDeclaration("const",[e.variableDeclarator(e.identifier("_$$session"),e.callExpression(e.identifier("createAsync"),[e.arrowFunctionExpression([],e.callExpression(e.identifier("_$$getUser"),[])),e.objectExpression([e.objectProperty(e.identifier("deferStream"),e.booleanLiteral(!0))])]))]),f=e.variableDeclaration("const",[e.variableDeclarator(e.identifier("_$$RenderProtected"),e.arrowFunctionExpression([],e.blockStatement(a.body)))]);if(a.body=[f],o.fallBack){let d=e.variableDeclaration("const",[e.variableDeclarator(e.identifier("_$$RenderFallBack"),o.fallBack)]);a.body.unshift(d)}a.body.unshift(p);let l=e.arrowFunctionExpression([],a);l.body.body.push(e.returnStatement(v(e,o))),l.body.body=[...l.body.body],t.replaceWith(l)}}}}}}}async function y(r,n,e){var o,u,a,p,f;let s=["typescript","jsx"],t=C(e),i=await j.transformAsync(r,{presets:[["@babel/preset-typescript"],...(u=(o=e==null?void 0:e.babel)==null?void 0:o.presets)!=null?u:[]],parserOpts:{plugins:s},plugins:[[t],...(p=(a=e==null?void 0:e.babel)==null?void 0:a.plugins)!=null?p:[]],filename:n});return i?(e!=null&&e.log&&console.log(n,i.code),{code:(f=i.code)!=null?f:"",map:i.map}):null}var m=require("@solid-mediakit/shared");function B(r){let n=m.babel.getFilter(r==null?void 0:r.filter),e={enforce:"pre",name:"auth",async transform(s,t){if(!n(t))return s;if(s.includes("protected$(")||s.includes("session$"))return await y(s,t,r)},configResolved(s){(0,m.repushPlugin)(s.plugins,e,["vite-server-references","solid","vinxi:routes"])}};return e}