@justai/core 0.1.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 +21 -0
- package/src/index.ts +58 -0
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@justai/core",
|
|
3
|
+
"version": "0.1.0",
|
|
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
|
+
"license": "UNLICENSED",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/justai-pro/web.git",
|
|
9
|
+
"directory": "packages/core"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"files": [
|
|
13
|
+
"src"
|
|
14
|
+
],
|
|
15
|
+
"exports": {
|
|
16
|
+
".": "./src/index.ts"
|
|
17
|
+
},
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
}
|
|
21
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
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);`;
|