orvyn-lang 0.2.0 → 0.4.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/GUIDE.md +198 -0
- package/LANGUAGE.md +818 -30
- package/README.md +211 -250
- package/brand/orvyn-sunset-animated.svg +15 -0
- package/package.json +3 -1
- package/src/cli/orvyn.js +207 -16
- package/src/core/signal.js +104 -6
- package/src/devtools/runtime.js +25 -1
- package/src/lang/a11y.js +152 -0
- package/src/lang/auth.js +239 -0
- package/src/lang/bundle-native.js +10 -6
- package/src/lang/codegen.js +615 -58
- package/src/lang/device.js +41 -0
- package/src/lang/doctor.js +60 -0
- package/src/lang/env.js +27 -0
- package/src/lang/errors.js +513 -0
- package/src/lang/form.js +69 -8
- package/src/lang/index.js +2 -1
- package/src/lang/lexer.js +2 -0
- package/src/lang/list.js +44 -0
- package/src/lang/native/android/MainActivity.kt +65 -4
- package/src/lang/native/android/OrvynDevice.kt +163 -0
- package/src/lang/native/android/OrvynEngine.kt +176 -7
- package/src/lang/native/android/OrvynRenderer.kt +970 -50
- package/src/lang/native/android.js +74 -11
- package/src/lang/native/assets.js +39 -0
- package/src/lang/native/build.js +140 -63
- package/src/lang/native/compatibility.js +57 -0
- package/src/lang/native/config.js +108 -0
- package/src/lang/native/ios/OrvynApp.swift +6 -0
- package/src/lang/native/ios/OrvynEngine.swift +291 -4
- package/src/lang/native/ios/OrvynViews.swift +769 -41
- package/src/lang/native/ios.js +81 -2
- package/src/lang/native-runtime.js +541 -55
- package/src/lang/native-size.js +53 -0
- package/src/lang/page-files.js +71 -0
- package/src/lang/page.js +63 -0
- package/src/lang/parser.js +278 -15
- package/src/lang/prerender.js +300 -0
- package/src/lang/read-import.js +12 -0
- package/src/lang/router.js +266 -0
- package/src/lang/runtime-shared.js +796 -10
- package/src/lang/runtime.js +399 -51
- package/src/lang/select.js +13 -0
- package/src/lang/server-code.js +233 -0
- package/src/lang/server-export.js +78 -0
- package/src/lang/server-libs.js +53 -0
- package/src/lang/server.js +409 -0
- package/src/lang/style-check.js +60 -0
- package/src/lang/test-runner.js +134 -0
- package/src/lang/types.js +317 -0
- package/src/lang/vite-plugin.js +108 -6
- package/src/lang/web-overlay.js +578 -0
- package/src/lang/widgets.js +94 -0
- package/src/lang/words.js +38 -0
- package/src/runtime/animations.js +24 -0
- package/src/runtime/appear.js +73 -0
- package/src/runtime/marquee.js +57 -0
- package/src/runtime/mount.js +120 -27
- package/src/runtime/patch.js +90 -11
- package/src/runtime/style.js +50 -9
- package/src/styling/fluid.js +129 -6
- package/src/styling/spec.js +319 -0
package/src/lang/a11y.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { theme } from "../styling/spec.js";
|
|
2
|
+
const NAMED = {
|
|
3
|
+
black: "#000000",
|
|
4
|
+
white: "#ffffff",
|
|
5
|
+
gray: "#808080",
|
|
6
|
+
grey: "#808080",
|
|
7
|
+
red: "#ff0000",
|
|
8
|
+
green: "#008000",
|
|
9
|
+
blue: "#0000ff",
|
|
10
|
+
yellow: "#ffff00",
|
|
11
|
+
orange: "#ffa500",
|
|
12
|
+
silver: "#c0c0c0"
|
|
13
|
+
};
|
|
14
|
+
function literal(expr) {
|
|
15
|
+
if (!expr) return null;
|
|
16
|
+
if (expr.kind === "Color") return expr.value;
|
|
17
|
+
if (expr.kind === "Ident") return expr.name;
|
|
18
|
+
if (expr.kind === "String" && expr.parts.length === 1 && typeof expr.parts[0] === "string") return expr.parts[0];
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
function rgb(value, colors, consts = {}) {
|
|
22
|
+
if (!value) return null;
|
|
23
|
+
let hex = (colors[value] ?? consts[value] ?? NAMED[value] ?? value).replace("#", "");
|
|
24
|
+
if (hex.length === 3) hex = [...hex].map((c) => c + c).join("");
|
|
25
|
+
return /^[0-9a-fA-F]{6}$/.test(hex) ? [0, 2, 4].map((i) => parseInt(hex.slice(i, i + 2), 16)) : null;
|
|
26
|
+
}
|
|
27
|
+
function contrast(a, b) {
|
|
28
|
+
const light = (color) => color.map((v) => v / 255).map((v) => v <= 0.03928 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4).reduce((sum, v, i) => sum + v * [0.2126, 0.7152, 0.0722][i], 0);
|
|
29
|
+
const [high, low] = [light(a), light(b)].sort((x, y) => y - x);
|
|
30
|
+
return (high + 0.05) / (low + 0.05);
|
|
31
|
+
}
|
|
32
|
+
function extractThemePalette(programs) {
|
|
33
|
+
const colors = { ...theme().colors };
|
|
34
|
+
const consts = {};
|
|
35
|
+
const hex = (value) => value && /^#[0-9a-fA-F]{3,6}$/.test(value) ? value : null;
|
|
36
|
+
for (const program of programs) {
|
|
37
|
+
for (const decl of program.body) {
|
|
38
|
+
if (decl.kind === "Const" || decl.kind === "State") {
|
|
39
|
+
const value = hex(literal(decl.init));
|
|
40
|
+
if (value) consts[decl.name] = value;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
for (const program of programs) {
|
|
45
|
+
for (const decl of program.body) {
|
|
46
|
+
if (decl.kind !== "Start" && decl.kind !== "Fn") continue;
|
|
47
|
+
for (const stmt of decl.body) {
|
|
48
|
+
const call = stmt.kind === "ExprStmt" ? stmt.expr : null;
|
|
49
|
+
if (call?.kind !== "Call" || call.callee.kind !== "Ident" || call.callee.name !== "theme") continue;
|
|
50
|
+
const palette = call.args[0];
|
|
51
|
+
if (palette?.kind !== "Object") continue;
|
|
52
|
+
for (const entry of palette.entries) {
|
|
53
|
+
const raw = literal(entry.value);
|
|
54
|
+
const value = hex(raw) ?? (raw ? consts[raw] ?? null : null);
|
|
55
|
+
if (value) colors[entry.key] = value;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return { colors, consts };
|
|
61
|
+
}
|
|
62
|
+
function checkAccessibility(program, palette = extractThemePalette([program]), options = { checkSeo: true }) {
|
|
63
|
+
const warnings = [];
|
|
64
|
+
const { colors, consts } = palette;
|
|
65
|
+
const visit = (node, background) => {
|
|
66
|
+
if (node.kind === "ViewIf") {
|
|
67
|
+
node.then.forEach((n) => visit(n, background));
|
|
68
|
+
node.else?.forEach((n) => visit(n, background));
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (node.kind === "ViewFor") return node.body.forEach((n) => visit(n, background));
|
|
72
|
+
const prop = (key) => {
|
|
73
|
+
const found = node.props.find((p) => p.key === key);
|
|
74
|
+
return found?.value.kind === "Expr" ? found.value.expr : void 0;
|
|
75
|
+
};
|
|
76
|
+
const has = (key) => node.props.some((p) => p.key === key);
|
|
77
|
+
const warn = (message) => warnings.push({ line: node.line, col: node.col, message });
|
|
78
|
+
const text = literal(node.arg ?? void 0);
|
|
79
|
+
if (node.name === "Image" && !has("label") && !has("alt")) {
|
|
80
|
+
warn(`Image needs a label for screen readers, like label: "Photo of the product" (label: "" if it is decoration)`);
|
|
81
|
+
}
|
|
82
|
+
if ((node.name === "Button" || node.name === "Link") && !has("label")) {
|
|
83
|
+
const quoted = node.arg?.kind === "String" ? node.arg.parts.filter((p) => typeof p === "string").join("") : null;
|
|
84
|
+
const onlySymbols = node.arg?.kind === "String" && node.arg.parts.length === 1 && !/[\p{L}\p{N}]/u.test(quoted);
|
|
85
|
+
if (!node.arg && !node.children.length || onlySymbols) {
|
|
86
|
+
warn(`${node.name} ${quoted ? `"${quoted}"` : ""} needs a label for screen readers, like label: "Remove"`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
const ownBackground = literal(prop("bg")) ?? background;
|
|
90
|
+
const color = literal(prop("color"));
|
|
91
|
+
const onButton = node.name === "Button" && has("bg");
|
|
92
|
+
if (color && ["Text", "Heading", "Button", "Link", "SubmitButton", "Checkbox", "Switch"].includes(node.name)) {
|
|
93
|
+
const front = rgb(color, colors, consts);
|
|
94
|
+
const back = rgb(ownBackground, colors, consts);
|
|
95
|
+
const size = Number(literal(prop("size")) ?? (prop("size")?.kind === "Number" ? prop("size").value : 0));
|
|
96
|
+
const needed = node.name === "Heading" || size >= 24 || onButton ? 3 : 4.5;
|
|
97
|
+
if (front && back) {
|
|
98
|
+
const ratio = contrast(front, back);
|
|
99
|
+
if (ratio < needed) warn(`"${color}" on "${ownBackground}" is hard to read (contrast ${ratio.toFixed(1)}, needs ${needed})${text ? `: ${text}` : ""}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
node.children.forEach((child) => visit(child, ownBackground));
|
|
103
|
+
};
|
|
104
|
+
if (options.checkSeo !== false) {
|
|
105
|
+
const screens = program.body.filter((d) => d.kind === "Screen" && d.type === "screen").map((screen) => ({ screen }));
|
|
106
|
+
warnings.push(...checkSeo(screens));
|
|
107
|
+
}
|
|
108
|
+
for (const decl of program.body) {
|
|
109
|
+
if (decl.kind === "Screen") decl.body.forEach((node) => visit(node, "bg"));
|
|
110
|
+
}
|
|
111
|
+
return warnings;
|
|
112
|
+
}
|
|
113
|
+
function checkSeo(screens) {
|
|
114
|
+
const warnings = [];
|
|
115
|
+
const usesPage = screens.some((item) => item.screen.page);
|
|
116
|
+
const titles = /* @__PURE__ */ new Map();
|
|
117
|
+
for (const { file, screen } of screens) {
|
|
118
|
+
if (!usesPage || screen.requiresAuth || ["NotFound", "ErrorPage"].includes(screen.name)) continue;
|
|
119
|
+
const field = (key) => {
|
|
120
|
+
const value = screen.page?.find((p) => p.key === key)?.value;
|
|
121
|
+
return value?.kind === "Expr" ? value.expr : void 0;
|
|
122
|
+
};
|
|
123
|
+
const warn = (message) => warnings.push({ ...file ? { file } : {}, line: screen.line, col: screen.col, message: `SEO: ${message}` });
|
|
124
|
+
for (const key of ["title", "description"]) {
|
|
125
|
+
if (!field(key)) warn(`screen ${screen.name} has no ${key}. Add: page title: "...", description: "..."`);
|
|
126
|
+
}
|
|
127
|
+
const title = literal(field("title"));
|
|
128
|
+
if (title) {
|
|
129
|
+
if (title.length > 60) warn(`the title of ${screen.name} is long (${title.length}); search results cut it near 60`);
|
|
130
|
+
if (titles.has(title)) warn(`${screen.name} and ${titles.get(title)} have the same title`);
|
|
131
|
+
titles.set(title, screen.name);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return warnings;
|
|
135
|
+
}
|
|
136
|
+
function checkProjectAccessibility(files) {
|
|
137
|
+
const palette = extractThemePalette(files.map((f) => f.program));
|
|
138
|
+
const byScreen = files.flatMap(
|
|
139
|
+
({ file, program }) => program.body.filter((d) => d.kind === "Screen" && d.type === "screen").map((screen) => ({ file, screen }))
|
|
140
|
+
);
|
|
141
|
+
const warnings = checkSeo(byScreen);
|
|
142
|
+
for (const { file, program } of files) {
|
|
143
|
+
warnings.push(...checkAccessibility(program, palette, { checkSeo: false }).map((w) => ({ ...w, file })));
|
|
144
|
+
}
|
|
145
|
+
return warnings;
|
|
146
|
+
}
|
|
147
|
+
export {
|
|
148
|
+
checkAccessibility,
|
|
149
|
+
checkProjectAccessibility,
|
|
150
|
+
contrast,
|
|
151
|
+
extractThemePalette
|
|
152
|
+
};
|
package/src/lang/auth.js
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { signal } from "../core/signal.js";
|
|
2
|
+
const TOKEN = "__auth_token";
|
|
3
|
+
const REFRESH = "__auth_refresh";
|
|
4
|
+
const USER = "__auth_user";
|
|
5
|
+
function originOf(address) {
|
|
6
|
+
const found = String(address).match(/^(https?):\/\/([a-z0-9.-]+|\[[0-9a-f:]+\])(?::(\d{1,5}))?(?=[\/?#]|$)/i);
|
|
7
|
+
if (!found) return null;
|
|
8
|
+
const scheme = found[1].toLowerCase();
|
|
9
|
+
const port = found[3] && Number(found[3]) !== (scheme === "https" ? 443 : 80) ? `:${Number(found[3])}` : "";
|
|
10
|
+
return `${scheme}://${found[2].toLowerCase()}${port}`;
|
|
11
|
+
}
|
|
12
|
+
function isUnauthorized(error) {
|
|
13
|
+
return Number(error?.status) === 401;
|
|
14
|
+
}
|
|
15
|
+
function createAuth(secure, deps = {}) {
|
|
16
|
+
let config = {};
|
|
17
|
+
let router = null;
|
|
18
|
+
let pending = null;
|
|
19
|
+
let refreshing = null;
|
|
20
|
+
let rawFetch = null;
|
|
21
|
+
const readUser = () => {
|
|
22
|
+
const raw = secure.get(USER);
|
|
23
|
+
if (raw == null) return null;
|
|
24
|
+
try {
|
|
25
|
+
return JSON.parse(raw);
|
|
26
|
+
} catch {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const tokenSig = signal(secure.get(TOKEN), "__orvyn_auth_token");
|
|
31
|
+
const userSig = signal(readUser(), "__orvyn_auth_user");
|
|
32
|
+
const errorSig = signal("", "__orvyn_auth_error");
|
|
33
|
+
function store(token, refresh, user) {
|
|
34
|
+
if (token) secure.set(TOKEN, token);
|
|
35
|
+
else secure.remove(TOKEN);
|
|
36
|
+
if (refresh !== void 0) {
|
|
37
|
+
if (refresh) secure.set(REFRESH, refresh);
|
|
38
|
+
else secure.remove(REFRESH);
|
|
39
|
+
}
|
|
40
|
+
if (user !== void 0) {
|
|
41
|
+
if (user == null) secure.remove(USER);
|
|
42
|
+
else secure.set(USER, JSON.stringify(user));
|
|
43
|
+
userSig.set(user ?? null);
|
|
44
|
+
}
|
|
45
|
+
tokenSig.set(token);
|
|
46
|
+
}
|
|
47
|
+
function normalize(input) {
|
|
48
|
+
const out = { ...input };
|
|
49
|
+
const alias = (from, to) => {
|
|
50
|
+
if (from in out) {
|
|
51
|
+
if (!(to in out)) out[to] = out[from];
|
|
52
|
+
delete out[from];
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
alias("login", "loginScreen");
|
|
56
|
+
alias("home", "homeScreen");
|
|
57
|
+
alias("logout", "logoutScreen");
|
|
58
|
+
if (typeof out.refresh === "string") {
|
|
59
|
+
out.refreshUrl = out.refresh;
|
|
60
|
+
delete out.refresh;
|
|
61
|
+
}
|
|
62
|
+
if ("api" in out) {
|
|
63
|
+
out.apiOrigins = [...out.apiOrigins || [], ...Array.isArray(out.api) ? out.api : [out.api]];
|
|
64
|
+
delete out.api;
|
|
65
|
+
}
|
|
66
|
+
for (const key of ["loginScreen", "homeScreen", "logoutScreen"]) {
|
|
67
|
+
if (key in out && typeof out[key] !== "function") {
|
|
68
|
+
throw new Error(`[Orvyn] auth ${key.replace("Screen", "")}: needs a screen, like login: Login`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
function setup(input = {}) {
|
|
74
|
+
const next = normalize(input);
|
|
75
|
+
config = { ...config, ...next };
|
|
76
|
+
const current = router?.current();
|
|
77
|
+
if (current && config.loginScreen && current.screen.__requiresAuth && !tokenSig.get()) {
|
|
78
|
+
pending = { screen: current.screen, params: current.params };
|
|
79
|
+
router.replace(config.loginScreen);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function login(tokenOrData, refresh, user) {
|
|
83
|
+
let token = tokenOrData;
|
|
84
|
+
if (tokenOrData && typeof tokenOrData === "object") {
|
|
85
|
+
token = tokenOrData.token ?? tokenOrData.accessToken ?? tokenOrData.access_token;
|
|
86
|
+
refresh = tokenOrData.refresh ?? tokenOrData.refreshToken ?? tokenOrData.refresh_token ?? refresh;
|
|
87
|
+
user = tokenOrData.user ?? user;
|
|
88
|
+
}
|
|
89
|
+
if (typeof token !== "string" || !token) {
|
|
90
|
+
throw new Error("[Orvyn] auth.login() needs a token, like auth.login(res.token)");
|
|
91
|
+
}
|
|
92
|
+
errorSig.set("");
|
|
93
|
+
store(token, refresh ?? null, user ?? null);
|
|
94
|
+
const target = pending;
|
|
95
|
+
pending = null;
|
|
96
|
+
if (router) {
|
|
97
|
+
if (target) router.replace(target.screen, target.params);
|
|
98
|
+
else if (config.homeScreen) router.replace(config.homeScreen);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function logout() {
|
|
102
|
+
const wasLoggedIn = Boolean(tokenSig.get());
|
|
103
|
+
store(null, null, null);
|
|
104
|
+
pending = null;
|
|
105
|
+
refreshing = null;
|
|
106
|
+
if (!config.keepSaved) try {
|
|
107
|
+
deps.onLogoutReset?.();
|
|
108
|
+
} catch {
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
config.onLogout?.();
|
|
112
|
+
} catch (error) {
|
|
113
|
+
console.warn("[Orvyn] auth onLogout failed:", error);
|
|
114
|
+
}
|
|
115
|
+
const to = config.logoutScreen ?? config.loginScreen;
|
|
116
|
+
if (router && to && (wasLoggedIn || router.current()?.screen !== to)) router.resetTo(to);
|
|
117
|
+
}
|
|
118
|
+
function setUser(user) {
|
|
119
|
+
if (user == null) secure.remove(USER);
|
|
120
|
+
else secure.set(USER, JSON.stringify(user));
|
|
121
|
+
userSig.set(user ?? null);
|
|
122
|
+
}
|
|
123
|
+
function mayReceiveToken(url) {
|
|
124
|
+
const s = String(url);
|
|
125
|
+
if (!/^[a-z][a-z0-9+.-]*:/i.test(s) && !s.startsWith("//")) return true;
|
|
126
|
+
const origin = originOf(s.startsWith("//") ? `https:${s}` : s);
|
|
127
|
+
return origin !== null && (config.apiOrigins || []).some((o) => originOf(String(o)) === origin);
|
|
128
|
+
}
|
|
129
|
+
async function doRefresh() {
|
|
130
|
+
const refreshToken = secure.get(REFRESH);
|
|
131
|
+
if (!config.refresh && !(config.refreshUrl && refreshToken)) return false;
|
|
132
|
+
try {
|
|
133
|
+
const res = config.refresh ? await config.refresh(refreshToken) : await rawFetch(config.refreshUrl, { method: "POST", body: { refresh: refreshToken } });
|
|
134
|
+
const token = typeof res === "string" ? res : res?.token ?? res?.accessToken ?? res?.access_token;
|
|
135
|
+
if (typeof token !== "string" || !token) return false;
|
|
136
|
+
const nextRefresh = typeof res === "object" ? res.refresh ?? res.refreshToken ?? res.refresh_token : void 0;
|
|
137
|
+
store(token, nextRefresh === void 0 ? void 0 : nextRefresh);
|
|
138
|
+
return true;
|
|
139
|
+
} catch {
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function refreshOnce() {
|
|
144
|
+
if (!refreshing) {
|
|
145
|
+
refreshing = doRefresh().finally(() => {
|
|
146
|
+
refreshing = null;
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
return refreshing;
|
|
150
|
+
}
|
|
151
|
+
function withAuthHeader(url, options) {
|
|
152
|
+
const token = tokenSig.get();
|
|
153
|
+
if (!token || options?.auth === false || options?.__noAuth || !mayReceiveToken(url)) return options;
|
|
154
|
+
const name = config.header || "Authorization";
|
|
155
|
+
const scheme = config.scheme ?? "Bearer ";
|
|
156
|
+
const headers = { ...options?.headers || {} };
|
|
157
|
+
const hasHeader = Object.keys(headers).some((k) => k.toLowerCase() === name.toLowerCase());
|
|
158
|
+
if (!hasHeader) headers[name] = `${scheme}${token}`;
|
|
159
|
+
return { ...options, headers };
|
|
160
|
+
}
|
|
161
|
+
function wrapFetch(fetchData) {
|
|
162
|
+
rawFetch = fetchData;
|
|
163
|
+
return async (url, options = {}) => {
|
|
164
|
+
const { auth: _auth, __noAuth: _noAuth, ...clean } = options || {};
|
|
165
|
+
void _auth;
|
|
166
|
+
void _noAuth;
|
|
167
|
+
const attached = withAuthHeader(url, options);
|
|
168
|
+
const sentToken = attached !== options;
|
|
169
|
+
const send = (opts) => {
|
|
170
|
+
const { auth: _a, __noAuth: _n, ...rest } = opts || {};
|
|
171
|
+
void _a;
|
|
172
|
+
void _n;
|
|
173
|
+
return fetchData(url, rest);
|
|
174
|
+
};
|
|
175
|
+
try {
|
|
176
|
+
return await send(sentToken ? attached : clean);
|
|
177
|
+
} catch (error) {
|
|
178
|
+
if (!sentToken || !isUnauthorized(error)) throw error;
|
|
179
|
+
const ok = await refreshOnce();
|
|
180
|
+
if (!ok) {
|
|
181
|
+
errorSig.set("Your session has expired. Please log in again.");
|
|
182
|
+
logout();
|
|
183
|
+
throw error;
|
|
184
|
+
}
|
|
185
|
+
try {
|
|
186
|
+
return await send(withAuthHeader(url, options));
|
|
187
|
+
} catch (retryError) {
|
|
188
|
+
if (isUnauthorized(retryError)) {
|
|
189
|
+
errorSig.set("Your session has expired. Please log in again.");
|
|
190
|
+
logout();
|
|
191
|
+
}
|
|
192
|
+
throw retryError;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
function guard(screen, params) {
|
|
198
|
+
if (!screen.__requiresAuth || tokenSig.get()) return null;
|
|
199
|
+
if (!config.loginScreen) {
|
|
200
|
+
console.warn(`[Orvyn] ${screen.name || "This screen"} requires auth. Call auth.setup({ loginScreen: Login }) so users can log in.`);
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
pending = { screen, params };
|
|
204
|
+
return config.loginScreen;
|
|
205
|
+
}
|
|
206
|
+
return {
|
|
207
|
+
get isLoggedIn() {
|
|
208
|
+
return Boolean(tokenSig.get());
|
|
209
|
+
},
|
|
210
|
+
get token() {
|
|
211
|
+
return tokenSig.get();
|
|
212
|
+
},
|
|
213
|
+
get user() {
|
|
214
|
+
return userSig.get();
|
|
215
|
+
},
|
|
216
|
+
get error() {
|
|
217
|
+
return errorSig.get();
|
|
218
|
+
},
|
|
219
|
+
clearError() {
|
|
220
|
+
errorSig.set("");
|
|
221
|
+
},
|
|
222
|
+
setup,
|
|
223
|
+
login,
|
|
224
|
+
logout,
|
|
225
|
+
setUser,
|
|
226
|
+
refresh: refreshOnce,
|
|
227
|
+
// internal hooks for the runtimes
|
|
228
|
+
__wrapFetch: wrapFetch,
|
|
229
|
+
__guard: guard,
|
|
230
|
+
__attachRouter(next) {
|
|
231
|
+
router = next;
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
export {
|
|
236
|
+
createAuth,
|
|
237
|
+
isUnauthorized,
|
|
238
|
+
originOf
|
|
239
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { fileURLToPath } from "node:url";
|
|
2
|
-
import { resolve } from "node:path";
|
|
2
|
+
import { basename, dirname, resolve } from "node:path";
|
|
3
3
|
import { orvynLang } from "./vite-plugin.js";
|
|
4
4
|
const here = fileURLToPath(new URL(".", import.meta.url));
|
|
5
5
|
const ext = import.meta.url.endsWith(".ts") ? ".ts" : ".js";
|
|
@@ -11,12 +11,15 @@ if (typeof console === "undefined") {
|
|
|
11
11
|
this.console = { log: function () {}, error: function () {}, warn: function () {} };
|
|
12
12
|
}
|
|
13
13
|
`;
|
|
14
|
-
async function bundleNative(entry) {
|
|
14
|
+
async function bundleNative(entry, options = {}) {
|
|
15
15
|
const { build } = await import("vite");
|
|
16
|
+
const entryPath = resolve(entry);
|
|
17
|
+
const fallbackAppId = basename(entryPath, ".orv").toLowerCase().replace(/[^a-z0-9._-]/g, "") || "app";
|
|
18
|
+
const appId = options.appId || fallbackAppId;
|
|
16
19
|
const result = await build({
|
|
17
20
|
configFile: false,
|
|
18
21
|
logLevel: "warn",
|
|
19
|
-
plugins: [orvynLang()],
|
|
22
|
+
plugins: [orvynLang({ root: dirname(entryPath), dev: Boolean(options.dev), env: options.env, tests: options.tests })],
|
|
20
23
|
resolve: {
|
|
21
24
|
alias: [
|
|
22
25
|
{ find: /^orvyn\/lang\/runtime$/, replacement: resolve(here, `native-runtime${ext}`) }
|
|
@@ -28,7 +31,7 @@ async function bundleNative(entry) {
|
|
|
28
31
|
target: "es2019",
|
|
29
32
|
rollupOptions: { output: { exports: "named" } },
|
|
30
33
|
lib: {
|
|
31
|
-
entry:
|
|
34
|
+
entry: entryPath,
|
|
32
35
|
formats: ["iife"],
|
|
33
36
|
name: "OrvynApp",
|
|
34
37
|
fileName: () => "app.js"
|
|
@@ -38,8 +41,9 @@ async function bundleNative(entry) {
|
|
|
38
41
|
const output = Array.isArray(result) ? result[0].output : result.output;
|
|
39
42
|
const chunk = output.find((item) => item.type === "chunk");
|
|
40
43
|
return `${PRELUDE}
|
|
41
|
-
${
|
|
42
|
-
|
|
44
|
+
var __orvynAppId = ${JSON.stringify(appId)};
|
|
45
|
+
${options.dev ? "globalThis.__orvynDev = true;\n" : ""}${chunk.code}
|
|
46
|
+
var __orvynStop = OrvynApp.mountApp(null);
|
|
43
47
|
`;
|
|
44
48
|
}
|
|
45
49
|
export {
|