@plasmicapp/loader-edge
Advanced tools
Comparing version
@@ -1,1 +0,1 @@ | ||
export { generateAllPaths, getActiveSplits, getMiddlewareResponse, rewriteWithoutVariation, rewriteWithVariation, } from './variation'; | ||
export { generateAllPaths, getActiveVariation, getMiddlewareResponse, rewriteWithoutTraits, rewriteWithTraits, } from './variation'; |
@@ -5,9 +5,60 @@ 'use strict'; | ||
var loaderFetcher = require('@plasmicapp/loader-fetcher'); | ||
var loaderSplits = require('@plasmicapp/loader-splits'); | ||
const getSeededRandomFunction = strSeed => { | ||
// https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript | ||
function cyrb128(str) { | ||
let h1 = 1779033703, | ||
h2 = 3144134277, | ||
h3 = 1013904242, | ||
h4 = 2773480762; | ||
for (let i = 0, k; i < str.length; i++) { | ||
k = str.charCodeAt(i); | ||
h1 = h2 ^ Math.imul(h1 ^ k, 597399067); | ||
h2 = h3 ^ Math.imul(h2 ^ k, 2869860233); | ||
h3 = h4 ^ Math.imul(h3 ^ k, 951274213); | ||
h4 = h1 ^ Math.imul(h4 ^ k, 2716044179); | ||
} | ||
h1 = Math.imul(h3 ^ h1 >>> 18, 597399067); | ||
h2 = Math.imul(h4 ^ h2 >>> 22, 2869860233); | ||
h3 = Math.imul(h1 ^ h3 >>> 17, 951274213); | ||
h4 = Math.imul(h2 ^ h4 >>> 19, 2716044179); | ||
return [(h1 ^ h2 ^ h3 ^ h4) >>> 0, (h2 ^ h1) >>> 0, (h3 ^ h1) >>> 0, (h4 ^ h1) >>> 0]; | ||
} | ||
function sfc32(a, b, c, d) { | ||
return function () { | ||
a >>>= 0; | ||
b >>>= 0; | ||
c >>>= 0; | ||
d >>>= 0; | ||
var t = a + b | 0; | ||
a = b ^ b >>> 9; | ||
b = c + (c << 3) | 0; | ||
c = c << 21 | c >>> 11; | ||
d = d + 1 | 0; | ||
t = t + d | 0; | ||
c = c + t | 0; | ||
return (t >>> 0) / 4294967296; | ||
}; | ||
} | ||
const seed = cyrb128(strSeed); | ||
const rand = sfc32(seed[0], seed[1], seed[2], seed[3]); | ||
return rand; | ||
}; | ||
const DELIMITER = '__pm__'; | ||
const rewriteWithoutVariation = url => { | ||
const [path, ...variationsArr] = url.split(DELIMITER); | ||
const variation = variationsArr.reduce((acc, elem) => { | ||
const PLASMIC_SEED = 'plasmic_seed'; | ||
const SEED_RANGE = 16; | ||
const getSeed = () => { | ||
return `${Math.floor(Math.random() * SEED_RANGE)}`; | ||
}; | ||
const rewriteWithoutTraits = url => { | ||
const [path, ...traitssArr] = url.split(DELIMITER); | ||
const traits = traitssArr.reduce((acc, elem) => { | ||
const [key, value] = elem.split('='); | ||
@@ -19,96 +70,69 @@ return { ...acc, | ||
return { | ||
path, | ||
variation | ||
path: path === '/' ? path : path.endsWith('/') ? path.substring(0, path.length - 1) : path, | ||
traits | ||
}; | ||
}; | ||
const expandVariation = variation => { | ||
const expandTraits = traits => { | ||
const cmp = (a, b) => { | ||
const idA = a.split('.')[1]; | ||
const idB = b.split('.')[1]; | ||
return idA < idB ? -1 : idA > idB ? 1 : 0; | ||
return a < b ? -1 : a > b ? 1 : 0; | ||
}; | ||
return Object.keys(variation).filter(key => !key.startsWith('ext')) // remove external variations | ||
.sort(cmp).map(key => `${DELIMITER}${key}=${variation[key]}`).join(''); | ||
return Object.keys(traits).sort(cmp).map(key => `${DELIMITER}${key}=${traits[key]}`).join(''); | ||
}; | ||
const rewriteWithVariation = (url, variation) => { | ||
return `${url}${url.endsWith('/') ? '' : '/'}${expandVariation(variation)}`; | ||
const rewriteWithTraits = (path, traits) => { | ||
return `${path}${path.endsWith('/') ? '' : '/'}${expandTraits(traits)}`; | ||
}; | ||
const generateAllPaths = path => { | ||
const paths = [path, ...Array(SEED_RANGE).fill(0).map((_, idx) => `${path}${path.endsWith('/') ? '' : '/'}__pm__${PLASMIC_SEED}=${idx}`)]; | ||
return paths; | ||
}; | ||
const getMiddlewareResponse = opts => { | ||
const newCookies = []; | ||
const seed = opts.cookies[PLASMIC_SEED] || getSeed(); | ||
const generateAllSplitPaths = splits => { | ||
if (splits.length === 0) { | ||
return ['']; | ||
if (!opts.cookies[PLASMIC_SEED]) { | ||
newCookies.push({ | ||
key: PLASMIC_SEED, | ||
value: seed | ||
}); | ||
} | ||
const [curSplit, ...tail] = splits; | ||
const tailPaths = generateAllSplitPaths(tail); | ||
const curPaths = ['', ...curSplit.slices.map(slice => expandVariation({ | ||
[loaderSplits.getSplitKey(curSplit)]: slice.id | ||
}))]; | ||
const paths = []; | ||
for (let i = 0; i < curPaths.length; i++) { | ||
for (let j = 0; j < tailPaths.length; j++) { | ||
paths.push(curPaths[i] + tailPaths[j]); | ||
} | ||
} | ||
return paths; | ||
}; | ||
const generateAllPaths = (path, splits) => { | ||
return generateAllSplitPaths(splits.sort((a, b) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0)).map(meta => `${path}${path.endsWith('/') ? '' : '/'}${meta}`); | ||
}; | ||
const getActiveSplits = async opts => { | ||
const fetcher = new loaderFetcher.PlasmicModulesFetcher(opts); | ||
const all = await fetcher.fetchAllData(); | ||
const splits = all.activeSplits; | ||
const paths = all.components.filter(comp => comp.isPage && comp.path).map(comp => comp.path); | ||
return { | ||
splits, | ||
paths | ||
pathname: rewriteWithTraits(opts.path, { | ||
[PLASMIC_SEED]: seed, | ||
...opts.traits | ||
}), | ||
cookies: newCookies | ||
}; | ||
}; | ||
const getMiddlewareResponse = async ({ | ||
opts, | ||
cookies, | ||
traits, | ||
url | ||
}) => { | ||
const getActiveVariation = opts => { | ||
const { | ||
splits | ||
} = await getActiveSplits(opts); | ||
const newCookies = []; | ||
const variation = loaderSplits.getActiveVariation({ | ||
splits, | ||
traits, | ||
getKnownValue: key => { | ||
return cookies[`plasmic:${key}`]; | ||
path | ||
} = opts; | ||
return loaderSplits.getActiveVariation({ | ||
splits, | ||
traits: { | ||
pageUrl: path, | ||
...traits | ||
}, | ||
updateKnownValue: (key, value) => { | ||
newCookies.push({ | ||
key: `plasmic:${key}`, | ||
value | ||
}); | ||
getKnownValue: () => undefined, | ||
updateKnownValue: () => null, | ||
getRandomValue: key => { | ||
var _traits$PLASMIC_SEED; | ||
const rand = getSeededRandomFunction(((_traits$PLASMIC_SEED = traits[PLASMIC_SEED]) != null ? _traits$PLASMIC_SEED : '') + key); | ||
return rand(); | ||
} | ||
}); | ||
let newUrl = url; | ||
if (Object.keys(variation).length) { | ||
newUrl = rewriteWithVariation(url, variation); | ||
} | ||
return { | ||
url: newUrl, | ||
cookies: newCookies | ||
}; | ||
}; | ||
exports.generateAllPaths = generateAllPaths; | ||
exports.getActiveSplits = getActiveSplits; | ||
exports.getActiveVariation = getActiveVariation; | ||
exports.getMiddlewareResponse = getMiddlewareResponse; | ||
exports.rewriteWithVariation = rewriteWithVariation; | ||
exports.rewriteWithoutVariation = rewriteWithoutVariation; | ||
exports.rewriteWithTraits = rewriteWithTraits; | ||
exports.rewriteWithoutTraits = rewriteWithoutTraits; | ||
//# sourceMappingURL=loader-edge.cjs.development.js.map |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("@plasmicapp/loader-fetcher"),e=require("@plasmicapp/loader-splits");const s=t=>Object.keys(t).filter(t=>!t.startsWith("ext")).sort((t,e)=>{const s=t.split(".")[1],i=e.split(".")[1];return s<i?-1:s>i?1:0}).map(e=>`__pm__${e}=${t[e]}`).join(""),i=(t,e)=>`${t}${t.endsWith("/")?"":"/"}${s(e)}`,r=t=>{if(0===t.length)return[""];const[i,...a]=t,p=r(a),l=["",...i.slices.map(t=>s({[e.getSplitKey(i)]:t.id}))],o=[];for(let t=0;t<l.length;t++)for(let e=0;e<p.length;e++)o.push(l[t]+p[e]);return o},a=async e=>{const s=new t.PlasmicModulesFetcher(e),i=await s.fetchAllData();return{splits:i.activeSplits,paths:i.components.filter(t=>t.isPage&&t.path).map(t=>t.path)}};exports.generateAllPaths=(t,e)=>r(e.sort((t,e)=>t.id<e.id?-1:t.id>e.id?1:0)).map(e=>`${t}${t.endsWith("/")?"":"/"}${e}`),exports.getActiveSplits=a,exports.getMiddlewareResponse=async({opts:t,cookies:s,traits:r,url:p})=>{const{splits:l}=await a(t),o=[],n=e.getActiveVariation({splits:l,traits:r,getKnownValue:t=>s["plasmic:"+t],updateKnownValue:(t,e)=>{o.push({key:"plasmic:"+t,value:e})}});let c=p;return Object.keys(n).length&&(c=i(p,n)),{url:c,cookies:o}},exports.rewriteWithVariation=i,exports.rewriteWithoutVariation=t=>{const[e,...s]=t.split("__pm__");return{path:e,variation:s.reduce((t,e)=>{const[s,i]=e.split("=");return{...t,[s]:i}},{})}}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("@plasmicapp/loader-splits");const e=(t,e)=>`${t}${t.endsWith("/")?"":"/"}${(t=>Object.keys(t).sort((t,e)=>t<e?-1:t>e?1:0).map(e=>`__pm__${e}=${t[e]}`).join(""))(e)}`;exports.generateAllPaths=t=>[t,...Array(16).fill(0).map((e,r)=>`${t}${t.endsWith("/")?"":"/"}__pm__plasmic_seed=${r}`)],exports.getActiveVariation=e=>{const{splits:r,traits:s,path:a}=e;return t.getActiveVariation({splits:r,traits:{pageUrl:a,...s},getKnownValue:()=>{},updateKnownValue:()=>null,getRandomValue:t=>{var e;return(t=>{const e=function(t){let e=1779033703,r=3144134277,s=1013904242,a=2773480762;for(let i,l=0;l<t.length;l++)i=t.charCodeAt(l),e=r^Math.imul(e^i,597399067),r=s^Math.imul(r^i,2869860233),s=a^Math.imul(s^i,951274213),a=e^Math.imul(a^i,2716044179);return e=Math.imul(s^e>>>18,597399067),r=Math.imul(a^r>>>22,2869860233),s=Math.imul(e^s>>>17,951274213),a=Math.imul(r^a>>>19,2716044179),[(e^r^s^a)>>>0,(r^e)>>>0,(s^e)>>>0,(a^e)>>>0]}(t);var r,s,a,i;return r=e[0],s=e[1],a=e[2],i=e[3],function(){var t=(r>>>=0)+(s>>>=0)|0;return r=s^s>>>9,s=(a>>>=0)+(a<<3)|0,a=(a=a<<21|a>>>11)+(t=t+(i=1+(i>>>=0)|0)|0)|0,(t>>>0)/4294967296}})((null!=(e=s.plasmic_seed)?e:"")+t)()}})},exports.getMiddlewareResponse=t=>{const r=[],s=t.cookies.plasmic_seed||""+Math.floor(16*Math.random());return t.cookies.plasmic_seed||r.push({key:"plasmic_seed",value:s}),{pathname:e(t.path,{plasmic_seed:s,...t.traits}),cookies:r}},exports.rewriteWithTraits=e,exports.rewriteWithoutTraits=t=>{const[e,...r]=t.split("__pm__"),s=r.reduce((t,e)=>{const[r,s]=e.split("=");return{...t,[r]:s}},{});return{path:"/"===e?e:e.endsWith("/")?e.substring(0,e.length-1):e,traits:s}}; | ||
//# sourceMappingURL=loader-edge.cjs.production.min.js.map |
@@ -1,8 +0,59 @@ | ||
import { PlasmicModulesFetcher } from '@plasmicapp/loader-fetcher'; | ||
import { getActiveVariation, getSplitKey } from '@plasmicapp/loader-splits'; | ||
import { getActiveVariation as getActiveVariation$1 } from '@plasmicapp/loader-splits'; | ||
const getSeededRandomFunction = strSeed => { | ||
// https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript | ||
function cyrb128(str) { | ||
let h1 = 1779033703, | ||
h2 = 3144134277, | ||
h3 = 1013904242, | ||
h4 = 2773480762; | ||
for (let i = 0, k; i < str.length; i++) { | ||
k = str.charCodeAt(i); | ||
h1 = h2 ^ Math.imul(h1 ^ k, 597399067); | ||
h2 = h3 ^ Math.imul(h2 ^ k, 2869860233); | ||
h3 = h4 ^ Math.imul(h3 ^ k, 951274213); | ||
h4 = h1 ^ Math.imul(h4 ^ k, 2716044179); | ||
} | ||
h1 = Math.imul(h3 ^ h1 >>> 18, 597399067); | ||
h2 = Math.imul(h4 ^ h2 >>> 22, 2869860233); | ||
h3 = Math.imul(h1 ^ h3 >>> 17, 951274213); | ||
h4 = Math.imul(h2 ^ h4 >>> 19, 2716044179); | ||
return [(h1 ^ h2 ^ h3 ^ h4) >>> 0, (h2 ^ h1) >>> 0, (h3 ^ h1) >>> 0, (h4 ^ h1) >>> 0]; | ||
} | ||
function sfc32(a, b, c, d) { | ||
return function () { | ||
a >>>= 0; | ||
b >>>= 0; | ||
c >>>= 0; | ||
d >>>= 0; | ||
var t = a + b | 0; | ||
a = b ^ b >>> 9; | ||
b = c + (c << 3) | 0; | ||
c = c << 21 | c >>> 11; | ||
d = d + 1 | 0; | ||
t = t + d | 0; | ||
c = c + t | 0; | ||
return (t >>> 0) / 4294967296; | ||
}; | ||
} | ||
const seed = cyrb128(strSeed); | ||
const rand = sfc32(seed[0], seed[1], seed[2], seed[3]); | ||
return rand; | ||
}; | ||
const DELIMITER = '__pm__'; | ||
const rewriteWithoutVariation = url => { | ||
const [path, ...variationsArr] = url.split(DELIMITER); | ||
const variation = variationsArr.reduce((acc, elem) => { | ||
const PLASMIC_SEED = 'plasmic_seed'; | ||
const SEED_RANGE = 16; | ||
const getSeed = () => { | ||
return `${Math.floor(Math.random() * SEED_RANGE)}`; | ||
}; | ||
const rewriteWithoutTraits = url => { | ||
const [path, ...traitssArr] = url.split(DELIMITER); | ||
const traits = traitssArr.reduce((acc, elem) => { | ||
const [key, value] = elem.split('='); | ||
@@ -14,92 +65,65 @@ return { ...acc, | ||
return { | ||
path, | ||
variation | ||
path: path === '/' ? path : path.endsWith('/') ? path.substring(0, path.length - 1) : path, | ||
traits | ||
}; | ||
}; | ||
const expandVariation = variation => { | ||
const expandTraits = traits => { | ||
const cmp = (a, b) => { | ||
const idA = a.split('.')[1]; | ||
const idB = b.split('.')[1]; | ||
return idA < idB ? -1 : idA > idB ? 1 : 0; | ||
return a < b ? -1 : a > b ? 1 : 0; | ||
}; | ||
return Object.keys(variation).filter(key => !key.startsWith('ext')) // remove external variations | ||
.sort(cmp).map(key => `${DELIMITER}${key}=${variation[key]}`).join(''); | ||
return Object.keys(traits).sort(cmp).map(key => `${DELIMITER}${key}=${traits[key]}`).join(''); | ||
}; | ||
const rewriteWithVariation = (url, variation) => { | ||
return `${url}${url.endsWith('/') ? '' : '/'}${expandVariation(variation)}`; | ||
const rewriteWithTraits = (path, traits) => { | ||
return `${path}${path.endsWith('/') ? '' : '/'}${expandTraits(traits)}`; | ||
}; | ||
const generateAllPaths = path => { | ||
const paths = [path, ...Array(SEED_RANGE).fill(0).map((_, idx) => `${path}${path.endsWith('/') ? '' : '/'}__pm__${PLASMIC_SEED}=${idx}`)]; | ||
return paths; | ||
}; | ||
const getMiddlewareResponse = opts => { | ||
const newCookies = []; | ||
const seed = opts.cookies[PLASMIC_SEED] || getSeed(); | ||
const generateAllSplitPaths = splits => { | ||
if (splits.length === 0) { | ||
return ['']; | ||
if (!opts.cookies[PLASMIC_SEED]) { | ||
newCookies.push({ | ||
key: PLASMIC_SEED, | ||
value: seed | ||
}); | ||
} | ||
const [curSplit, ...tail] = splits; | ||
const tailPaths = generateAllSplitPaths(tail); | ||
const curPaths = ['', ...curSplit.slices.map(slice => expandVariation({ | ||
[getSplitKey(curSplit)]: slice.id | ||
}))]; | ||
const paths = []; | ||
for (let i = 0; i < curPaths.length; i++) { | ||
for (let j = 0; j < tailPaths.length; j++) { | ||
paths.push(curPaths[i] + tailPaths[j]); | ||
} | ||
} | ||
return paths; | ||
}; | ||
const generateAllPaths = (path, splits) => { | ||
return generateAllSplitPaths(splits.sort((a, b) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0)).map(meta => `${path}${path.endsWith('/') ? '' : '/'}${meta}`); | ||
}; | ||
const getActiveSplits = async opts => { | ||
const fetcher = new PlasmicModulesFetcher(opts); | ||
const all = await fetcher.fetchAllData(); | ||
const splits = all.activeSplits; | ||
const paths = all.components.filter(comp => comp.isPage && comp.path).map(comp => comp.path); | ||
return { | ||
splits, | ||
paths | ||
pathname: rewriteWithTraits(opts.path, { | ||
[PLASMIC_SEED]: seed, | ||
...opts.traits | ||
}), | ||
cookies: newCookies | ||
}; | ||
}; | ||
const getMiddlewareResponse = async ({ | ||
opts, | ||
cookies, | ||
traits, | ||
url | ||
}) => { | ||
const getActiveVariation = opts => { | ||
const { | ||
splits | ||
} = await getActiveSplits(opts); | ||
const newCookies = []; | ||
const variation = getActiveVariation({ | ||
splits, | ||
traits, | ||
getKnownValue: key => { | ||
return cookies[`plasmic:${key}`]; | ||
path | ||
} = opts; | ||
return getActiveVariation$1({ | ||
splits, | ||
traits: { | ||
pageUrl: path, | ||
...traits | ||
}, | ||
updateKnownValue: (key, value) => { | ||
newCookies.push({ | ||
key: `plasmic:${key}`, | ||
value | ||
}); | ||
getKnownValue: () => undefined, | ||
updateKnownValue: () => null, | ||
getRandomValue: key => { | ||
var _traits$PLASMIC_SEED; | ||
const rand = getSeededRandomFunction(((_traits$PLASMIC_SEED = traits[PLASMIC_SEED]) != null ? _traits$PLASMIC_SEED : '') + key); | ||
return rand(); | ||
} | ||
}); | ||
let newUrl = url; | ||
if (Object.keys(variation).length) { | ||
newUrl = rewriteWithVariation(url, variation); | ||
} | ||
return { | ||
url: newUrl, | ||
cookies: newCookies | ||
}; | ||
}; | ||
export { generateAllPaths, getActiveSplits, getMiddlewareResponse, rewriteWithVariation, rewriteWithoutVariation }; | ||
export { generateAllPaths, getActiveVariation, getMiddlewareResponse, rewriteWithTraits, rewriteWithoutTraits }; | ||
//# sourceMappingURL=loader-edge.esm.js.map |
@@ -1,21 +0,15 @@ | ||
import { FetcherOptions, Split } from '@plasmicapp/loader-fetcher'; | ||
export declare const DELIMITER = "__pm__"; | ||
export declare const rewriteWithoutVariation: (url: string) => { | ||
import { Split } from '@plasmicapp/loader-fetcher'; | ||
declare type Traits = Record<string, string | number | boolean>; | ||
export declare const rewriteWithoutTraits: (url: string) => { | ||
path: string; | ||
variation: {}; | ||
traits: {}; | ||
}; | ||
export declare const rewriteWithVariation: (url: string, variation: Record<string, string>) => string; | ||
export declare const generateAllPaths: (path: string, splits: Split[]) => string[]; | ||
export declare type MiddlewareOptions = Pick<FetcherOptions, 'projects' | 'host' | 'preview'>; | ||
export declare const getActiveSplits: (opts: MiddlewareOptions) => Promise<{ | ||
splits: Split[]; | ||
paths: string[]; | ||
}>; | ||
export declare const getMiddlewareResponse: ({ opts, cookies, traits, url, }: { | ||
opts: MiddlewareOptions; | ||
export declare const rewriteWithTraits: (path: string, traits: Traits) => string; | ||
export declare const generateAllPaths: (path: string) => string[]; | ||
export declare const getMiddlewareResponse: (opts: { | ||
path: string; | ||
traits: Traits; | ||
cookies: Record<string, string>; | ||
traits: Record<string, string>; | ||
url: string; | ||
}) => Promise<{ | ||
url: string; | ||
}) => { | ||
pathname: string; | ||
cookies: { | ||
@@ -25,2 +19,8 @@ key: string; | ||
}[]; | ||
}>; | ||
}; | ||
export declare const getActiveVariation: (opts: { | ||
splits: Split[]; | ||
traits: Record<string, string | number | boolean>; | ||
path: string; | ||
}) => Record<string, string>; | ||
export {}; |
{ | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"license": "MIT", | ||
@@ -57,3 +57,3 @@ "main": "dist/index.js", | ||
"@plasmicapp/loader-fetcher": "1.0.3", | ||
"@plasmicapp/loader-splits": "1.0.6" | ||
"@plasmicapp/loader-splits": "1.0.7" | ||
}, | ||
@@ -63,3 +63,3 @@ "publishConfig": { | ||
}, | ||
"gitHead": "bf8ca88e4d71e07b368eb012bddf69c67532ead7" | ||
"gitHead": "82d225454e795bc101aa3ae685090658b7aab113" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
37910
21.28%13
8.33%273
19.21%1
Infinity%+ Added
- Removed