@justai/core 0.1.0 → 0.2.0
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/package.json +5 -2
- package/src/index.d.ts +17 -0
- package/src/index.js +130 -0
- package/src/index.ts +0 -58
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@justai/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "The framework-free heart of the persona shell — the load-bearing behaviour (auth capture, reveal, ready) as plain strings, so Astro and Next inject the SAME logic instead of each re-writing it.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"repository": {
|
|
@@ -13,7 +13,10 @@
|
|
|
13
13
|
"src"
|
|
14
14
|
],
|
|
15
15
|
"exports": {
|
|
16
|
-
".":
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./src/index.d.ts",
|
|
18
|
+
"default": "./src/index.js"
|
|
19
|
+
}
|
|
17
20
|
},
|
|
18
21
|
"publishConfig": {
|
|
19
22
|
"access": "public"
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** Capture the SSO token from the URL fragment, synchronously. MUST run first in the head — see index.js. */
|
|
2
|
+
export declare const AUTH_CAPTURE_JS: string;
|
|
3
|
+
|
|
4
|
+
/** Mark that JavaScript is running, so the reveal can hold paint only when JS can un-hold it. */
|
|
5
|
+
export declare const JS_CLASS_JS: string;
|
|
6
|
+
|
|
7
|
+
/** Hold paint until a page sets `body.ready`. A page with no kernel script must carry READY_JS. */
|
|
8
|
+
export declare const REVEAL_CSS: string;
|
|
9
|
+
|
|
10
|
+
/** Self-reveal for a page with no kernel script (a 404, a sub-persona like feed). */
|
|
11
|
+
export declare const READY_JS: string;
|
|
12
|
+
|
|
13
|
+
/** Which GA4 measurement id a host reports into. The ecosystem's stream layout, declared once. */
|
|
14
|
+
export declare function measurementIdFor(host: string | null | undefined): string;
|
|
15
|
+
|
|
16
|
+
/** The whole analytics head block for a measurement id — non-production guard, consent defaults, config, deferred loader. */
|
|
17
|
+
export declare function analyticsJs(gaId: string): string;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @justai/core — the framework-free heart of the persona shell.
|
|
3
|
+
*
|
|
4
|
+
* The load-bearing behaviour of every persona page is plain JavaScript and CSS. It does not care
|
|
5
|
+
* whether an Astro template or a React component put it in the head — the SAME string of JS captures
|
|
6
|
+
* the SSO token whether it is injected with `set:html` or `dangerouslySetInnerHTML`. So it lives HERE,
|
|
7
|
+
* once, and the framework bindings (@justai/cuts for Astro, and a Next equivalent) inject it. When a
|
|
8
|
+
* string of shell logic lives inside a `.astro` component, a `.tsx` file that needs the same thing has
|
|
9
|
+
* no choice but to hand-copy it — which is exactly how `feed` (Next) came to carry its own byte-for-byte
|
|
10
|
+
* copy of the auth capture below. Two copies in two frameworks drift the same way nine copies in one
|
|
11
|
+
* framework did; this package is the fix, one level lower than @justai/cuts.
|
|
12
|
+
*
|
|
13
|
+
* These are STRINGS, deliberately, not functions that build DOM — because the thing that is identical
|
|
14
|
+
* across frameworks is the behaviour, and the thing that differs is only how a head is assembled. Keep
|
|
15
|
+
* anything framework-specific (JSX, Astro directives, `define:vars`) OUT of here.
|
|
16
|
+
*
|
|
17
|
+
* Whenever you change one of these, you are changing it for every persona on both frameworks at once —
|
|
18
|
+
* which is the point, and the responsibility. The reasons that make each one load-bearing live in
|
|
19
|
+
* @justai/web's expert corpus (the-shell-guarantees-you-cannot-see), where they survive a refactor of
|
|
20
|
+
* the code that carries them.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Capture the SSO token from the URL fragment, synchronously, and stash it for the deferred auth SDK.
|
|
25
|
+
*
|
|
26
|
+
* MUST run before anything else can touch `location.hash`. Two later scripts would otherwise take it:
|
|
27
|
+
* a scroll-spy that rewrites the hash to `#kernel` CLOBBERS `#auth_token` before the SDK reads it, and
|
|
28
|
+
* a language picker that copies the current hash onto the next locale's URL would PROPAGATE the raw
|
|
29
|
+
* token into history. Injecting this first, in the head, defeats both. Move it later and sign-in breaks
|
|
30
|
+
* SILENTLY — no error, the visitor simply lands back signed out.
|
|
31
|
+
*/
|
|
32
|
+
export const AUTH_CAPTURE_JS =
|
|
33
|
+
`(function(){try{var h=location.hash||"";var m=h.match(/(?:^#|&)auth_token=([^&]+)/);` +
|
|
34
|
+
`if(m){localStorage.setItem("justai_token",decodeURIComponent(m[1]))}` +
|
|
35
|
+
`else if(/(?:^#|&)auth_signout=1\\b/.test(h)){localStorage.removeItem("justai_token")}else{return}` +
|
|
36
|
+
`history.replaceState(null,"",location.pathname+location.search)}catch(e){}})();`;
|
|
37
|
+
|
|
38
|
+
/** Mark that JavaScript is running, so the reveal below can hold paint only when JS can un-hold it. */
|
|
39
|
+
export const JS_CLASS_JS = `document.documentElement.classList.add("js")`;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Hold the paint until a page marks itself ready (`body.ready`), gated behind `.js` so the page is
|
|
43
|
+
* always visible with JS off and never flashes before positioning at the Kernel.
|
|
44
|
+
*
|
|
45
|
+
* This is a TRAP as much as a feature, and the warning belongs beside it: ANY page using this without a
|
|
46
|
+
* script that sets `body.ready` stays invisible FOREVER, and nothing errors — the page is simply never
|
|
47
|
+
* shown. A page with no kernel script (a 404, or a sub-persona like feed) must carry {@link READY_JS}.
|
|
48
|
+
*/
|
|
49
|
+
export const REVEAL_CSS = `html.js body{opacity:0}html.js body.ready{opacity:1}`;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The other half of {@link REVEAL_CSS}, for a page that has no kernel script to reveal it: set
|
|
53
|
+
* `body.ready` as soon as the body exists, and once more after load as a backstop. This is what keeps
|
|
54
|
+
* a sub-persona page (feed, a 404) from falling into the reveal trap.
|
|
55
|
+
*/
|
|
56
|
+
export const READY_JS =
|
|
57
|
+
`var r=function(){document.body&&document.body.classList.add("ready")};` +
|
|
58
|
+
`if(document.readyState!=="loading")r();else addEventListener("DOMContentLoaded",r);setTimeout(r,400);`;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* queries: which measurement id does this host use · wrong GA4 stream · a persona reports into the
|
|
62
|
+
* wrong property · G-13L3BP6XCW vs G-K2J4FQTQ8L · how does feed pick its analytics tag
|
|
63
|
+
*
|
|
64
|
+
* THE ECOSYSTEM'S STREAM LAYOUT, declared once.
|
|
65
|
+
*
|
|
66
|
+
* One GA4 property, two web streams: `G-13L3BP6XCW` for app.zone and its personas, `G-K2J4FQTQ8L`
|
|
67
|
+
* for justai.pro and its own sub-domains. A page reports into whichever stream ITS CODE loads, so
|
|
68
|
+
* "app.zone versus justai.pro" is a fact about our code rather than a rule the platform enforces —
|
|
69
|
+
* put a persona on the wrong one and nothing errors, nothing is missing, the numbers simply land in
|
|
70
|
+
* the wrong column. That is why the mapping is a function here and not a constant copied per repo.
|
|
71
|
+
*
|
|
72
|
+
* A STATIC persona cannot call this — it has no host at build time — so @justai/cuts keeps its own
|
|
73
|
+
* default and `justai` passes the override. A SERVER-rendered surface (feed) knows the Host header
|
|
74
|
+
* and must use this. The two must agree; if a third stream ever appears, this is the one place it
|
|
75
|
+
* is added.
|
|
76
|
+
*/
|
|
77
|
+
export function measurementIdFor(host) {
|
|
78
|
+
const h = String(host || "").toLowerCase().split(":")[0];
|
|
79
|
+
return h === "justai.pro" || h.endsWith(".justai.pro") ? "G-K2J4FQTQ8L" : "G-13L3BP6XCW";
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* queries: analytics inline script · gtag block · consent defaults · why is dev traffic in GA4 ·
|
|
84
|
+
* non-production guard · localhost 127.0.0.1 in analytics · how does a page start measuring
|
|
85
|
+
*
|
|
86
|
+
* The whole analytics head block, for any framework, given a measurement id.
|
|
87
|
+
*
|
|
88
|
+
* Everything load-bearing about it, in the order it matters:
|
|
89
|
+
*
|
|
90
|
+
* A NON-PRODUCTION PAGE MEASURES NOTHING. Found by reading the property through the analytics MCP:
|
|
91
|
+
* `127.0.0.1` and `localhost` were its TOP "persona" — 546 sessions in four days, and on one of them
|
|
92
|
+
* 127.0.0.1 alone outranked every real persona. Local dev and every headless verification sweep
|
|
93
|
+
* loading built HTML. The guard also covers the edge's `dev-` preview hosts: previews are rehearsals,
|
|
94
|
+
* not visits. Everything before 2026-07-24 in that property is inflated by this.
|
|
95
|
+
*
|
|
96
|
+
* `gtag` GOES ON `window`, never `function gtag`. A framework that scopes an injected variable wraps
|
|
97
|
+
* this in an IIFE, and a function declaration would then be trapped inside it — while the consent
|
|
98
|
+
* dialog's own script, elsewhere on the page, calls `gtag`. The failure is silent and specific: a
|
|
99
|
+
* European visitor clicks Allow, "gtag is not defined", the choice is never applied and the sheet
|
|
100
|
+
* never closes. The stub is also assigned BEFORE the guard returns, so a silenced host still has a
|
|
101
|
+
* callable `gtag` and that same script cannot throw there either.
|
|
102
|
+
*
|
|
103
|
+
* CONSENT DEFAULTS, twice: once globally granting only `analytics_storage`, once again for the EEA +
|
|
104
|
+
* UK + CH region list denying even that until the visitor consents. `ad_storage`, `ad_user_data` and
|
|
105
|
+
* `ad_personalization` are denied EVERYWHERE with no path to grant them — a decision, not an
|
|
106
|
+
* oversight, and the reason Google Ads reports zero conversions and a permanent "0% consent rate".
|
|
107
|
+
* The region list has a sibling: the timezone list in the consent dialog's script. Change one and the
|
|
108
|
+
* other must change, or a visitor is denied by region while never being offered the dialog.
|
|
109
|
+
*
|
|
110
|
+
* `page_title` IS PINNED at load. Only one persona mutates `document.title` today — time shows a live
|
|
111
|
+
* clock — and without this its analytics reports "13:32 · Amsterdam" as the page name. It costs every
|
|
112
|
+
* other persona nothing and removes a trap the next persona that animates its tab would walk into.
|
|
113
|
+
*
|
|
114
|
+
* The tag itself loads on idle after `load`: measurement must never compete with the kernel.
|
|
115
|
+
*/
|
|
116
|
+
export function analyticsJs(gaId) {
|
|
117
|
+
return `window.gtag=window.gtag||function(){(window.dataLayer=window.dataLayer||[]).push(arguments)};
|
|
118
|
+
var __h=location.hostname;
|
|
119
|
+
if(!(__h==="localhost"||__h==="127.0.0.1"||__h==="::1"||__h==="[::1]"||__h.indexOf("dev-")===0)){
|
|
120
|
+
var __g=${JSON.stringify(gaId)};
|
|
121
|
+
window.dataLayer=window.dataLayer||[];
|
|
122
|
+
window.gtag=window.gtag||function(){window.dataLayer.push(arguments)};
|
|
123
|
+
gtag("js",new Date());
|
|
124
|
+
gtag("consent","default",{ad_storage:"denied",ad_user_data:"denied",ad_personalization:"denied",analytics_storage:"granted"});
|
|
125
|
+
gtag("consent","default",{region:["AT","BE","BG","HR","CY","CZ","DK","EE","FI","FR","DE","GR","HU","IE","IT","LV","LT","LU","MT","NL","PL","PT","RO","SK","SI","ES","SE","IS","LI","NO","GB","CH"],ad_storage:"denied",ad_user_data:"denied",ad_personalization:"denied",analytics_storage:"denied"});
|
|
126
|
+
try{var __c=localStorage.getItem("consent");if(__c)gtag("consent","update",{analytics_storage:__c})}catch(e){}
|
|
127
|
+
gtag("config",__g,{cookie_expires:60*60*24*60,page_title:document.title,allow_google_signals:false,allow_ad_personalization_signals:false});
|
|
128
|
+
addEventListener("load",function(){(window.requestIdleCallback||function(f){setTimeout(f,1200)})(function(){var s=document.createElement("script");s.async=true;s.src="https://www.googletagmanager.com/gtag/js?id="+__g;document.head.appendChild(s)})});
|
|
129
|
+
}`;
|
|
130
|
+
}
|
package/src/index.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @justai/core — the framework-free heart of the persona shell.
|
|
3
|
-
*
|
|
4
|
-
* The load-bearing behaviour of every persona page is plain JavaScript and CSS. It does not care
|
|
5
|
-
* whether an Astro template or a React component put it in the head — the SAME string of JS captures
|
|
6
|
-
* the SSO token whether it is injected with `set:html` or `dangerouslySetInnerHTML`. So it lives HERE,
|
|
7
|
-
* once, and the framework bindings (@justai/cuts for Astro, and a Next equivalent) inject it. When a
|
|
8
|
-
* string of shell logic lives inside a `.astro` component, a `.tsx` file that needs the same thing has
|
|
9
|
-
* no choice but to hand-copy it — which is exactly how `feed` (Next) came to carry its own byte-for-byte
|
|
10
|
-
* copy of the auth capture below. Two copies in two frameworks drift the same way nine copies in one
|
|
11
|
-
* framework did; this package is the fix, one level lower than @justai/cuts.
|
|
12
|
-
*
|
|
13
|
-
* These are STRINGS, deliberately, not functions that build DOM — because the thing that is identical
|
|
14
|
-
* across frameworks is the behaviour, and the thing that differs is only how a head is assembled. Keep
|
|
15
|
-
* anything framework-specific (JSX, Astro directives, `define:vars`) OUT of here.
|
|
16
|
-
*
|
|
17
|
-
* Whenever you change one of these, you are changing it for every persona on both frameworks at once —
|
|
18
|
-
* which is the point, and the responsibility. The reasons that make each one load-bearing live in
|
|
19
|
-
* @justai/web's expert corpus (the-shell-guarantees-you-cannot-see), where they survive a refactor of
|
|
20
|
-
* the code that carries them.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Capture the SSO token from the URL fragment, synchronously, and stash it for the deferred auth SDK.
|
|
25
|
-
*
|
|
26
|
-
* MUST run before anything else can touch `location.hash`. Two later scripts would otherwise take it:
|
|
27
|
-
* a scroll-spy that rewrites the hash to `#kernel` CLOBBERS `#auth_token` before the SDK reads it, and
|
|
28
|
-
* a language picker that copies the current hash onto the next locale's URL would PROPAGATE the raw
|
|
29
|
-
* token into history. Injecting this first, in the head, defeats both. Move it later and sign-in breaks
|
|
30
|
-
* SILENTLY — no error, the visitor simply lands back signed out.
|
|
31
|
-
*/
|
|
32
|
-
export const AUTH_CAPTURE_JS =
|
|
33
|
-
`(function(){try{var h=location.hash||"";var m=h.match(/(?:^#|&)auth_token=([^&]+)/);` +
|
|
34
|
-
`if(m){localStorage.setItem("justai_token",decodeURIComponent(m[1]))}` +
|
|
35
|
-
`else if(/(?:^#|&)auth_signout=1\\b/.test(h)){localStorage.removeItem("justai_token")}else{return}` +
|
|
36
|
-
`history.replaceState(null,"",location.pathname+location.search)}catch(e){}})();`;
|
|
37
|
-
|
|
38
|
-
/** Mark that JavaScript is running, so the reveal below can hold paint only when JS can un-hold it. */
|
|
39
|
-
export const JS_CLASS_JS = `document.documentElement.classList.add("js")`;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Hold the paint until a page marks itself ready (`body.ready`), gated behind `.js` so the page is
|
|
43
|
-
* always visible with JS off and never flashes before positioning at the Kernel.
|
|
44
|
-
*
|
|
45
|
-
* This is a TRAP as much as a feature, and the warning belongs beside it: ANY page using this without a
|
|
46
|
-
* script that sets `body.ready` stays invisible FOREVER, and nothing errors — the page is simply never
|
|
47
|
-
* shown. A page with no kernel script (a 404, or a sub-persona like feed) must carry {@link READY_JS}.
|
|
48
|
-
*/
|
|
49
|
-
export const REVEAL_CSS = `html.js body{opacity:0}html.js body.ready{opacity:1}`;
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* The other half of {@link REVEAL_CSS}, for a page that has no kernel script to reveal it: set
|
|
53
|
-
* `body.ready` as soon as the body exists, and once more after load as a backstop. This is what keeps
|
|
54
|
-
* a sub-persona page (feed, a 404) from falling into the reveal trap.
|
|
55
|
-
*/
|
|
56
|
-
export const READY_JS =
|
|
57
|
-
`var r=function(){document.body&&document.body.classList.add("ready")};` +
|
|
58
|
-
`if(document.readyState!=="loading")r();else addEventListener("DOMContentLoaded",r);setTimeout(r,400);`;
|