@c9up/aurora 0.1.5 → 0.1.7
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/README.md +1 -1
- package/dist/browser.d.ts +136 -4
- package/dist/browser.js +324 -15
- package/dist/component.js +7 -0
- package/dist/http.d.ts +79 -0
- package/dist/http.js +199 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +2 -7
- package/dist/reactive.d.ts +7 -0
- package/dist/reactive.js +25 -1
- package/dist/relay.js +16 -13
- package/dist/server/renderPage.js +1 -1
- package/package.json +1 -1
- package/src/browser.ts +425 -16
- package/src/component.ts +7 -0
- package/src/http.ts +278 -0
- package/src/index.ts +32 -1
- package/src/reactive.ts +29 -1
- package/src/relay.ts +19 -11
- package/src/server/renderPage.ts +1 -1
package/dist/http.js
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `HttpClient` — a small typed wrapper over `fetch` so call sites read
|
|
3
|
+
* `await http.get<User>("/auth/me")` instead of hand-rolling headers,
|
|
4
|
+
* `res.json()`, and status checks.
|
|
5
|
+
*
|
|
6
|
+
* - Auto JSON: a plain-object/array body is `JSON.stringify`-d with a
|
|
7
|
+
* `Content-Type: application/json` header; a JSON response is parsed.
|
|
8
|
+
* `FormData`/`Blob`/`URLSearchParams`/`string`/binary bodies pass through
|
|
9
|
+
* untouched.
|
|
10
|
+
* - Bearer auth: a `token` (string or getter, read fresh per request) is sent
|
|
11
|
+
* as `Authorization: Bearer …` unless the caller set the header themselves.
|
|
12
|
+
* - Errors: a non-2xx response rejects with an {@link HttpError} carrying the
|
|
13
|
+
* status, the `Response`, and the parsed body.
|
|
14
|
+
*
|
|
15
|
+
* Node-free and isomorphic — uses the global `fetch` (browsers, Node 18+,
|
|
16
|
+
* Workers, Bun, Deno). Part of the client barrel.
|
|
17
|
+
*/
|
|
18
|
+
/** Thrown on a non-2xx response. Carries the status, the `Response`, and the parsed body. */
|
|
19
|
+
export class HttpError extends Error {
|
|
20
|
+
status;
|
|
21
|
+
response;
|
|
22
|
+
data;
|
|
23
|
+
constructor(response, data) {
|
|
24
|
+
super(`HTTP ${response.status} ${response.statusText} for ${response.url}`);
|
|
25
|
+
this.name = "HttpError";
|
|
26
|
+
this.status = response.status;
|
|
27
|
+
this.response = response;
|
|
28
|
+
this.data = data;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/** Whether `body` should be JSON-encoded (vs. passed to `fetch` untouched). */
|
|
32
|
+
function shouldJsonEncode(body) {
|
|
33
|
+
if (body === null || typeof body !== "object") {
|
|
34
|
+
return typeof body !== "string";
|
|
35
|
+
}
|
|
36
|
+
if (body instanceof FormData ||
|
|
37
|
+
body instanceof Blob ||
|
|
38
|
+
body instanceof URLSearchParams ||
|
|
39
|
+
body instanceof ArrayBuffer ||
|
|
40
|
+
ArrayBuffer.isView(body)) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
if (typeof ReadableStream !== "undefined" && body instanceof ReadableStream) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
/** Case-insensitive header presence check. */
|
|
49
|
+
function hasHeader(headers, name) {
|
|
50
|
+
const lower = name.toLowerCase();
|
|
51
|
+
for (const key of Object.keys(headers)) {
|
|
52
|
+
if (key.toLowerCase() === lower)
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
/** Parse a response by content-type; `null` for empty / no-content bodies. */
|
|
58
|
+
async function parseBody(response) {
|
|
59
|
+
if (response.status === 204 || response.status === 205)
|
|
60
|
+
return null;
|
|
61
|
+
const type = response.headers.get("content-type") ?? "";
|
|
62
|
+
const text = await response.text();
|
|
63
|
+
if (text === "")
|
|
64
|
+
return null;
|
|
65
|
+
if (type.includes("application/json"))
|
|
66
|
+
return JSON.parse(text);
|
|
67
|
+
return text;
|
|
68
|
+
}
|
|
69
|
+
export class HttpClient {
|
|
70
|
+
#baseURL;
|
|
71
|
+
#headers;
|
|
72
|
+
#token;
|
|
73
|
+
#credentials;
|
|
74
|
+
constructor(options = {}) {
|
|
75
|
+
this.#baseURL = options.baseURL ?? "";
|
|
76
|
+
this.#headers = { ...options.headers };
|
|
77
|
+
this.#token = options.token;
|
|
78
|
+
this.#credentials = options.credentials;
|
|
79
|
+
}
|
|
80
|
+
/** Set a default header for every subsequent request (case-insensitive replace). Chainable. */
|
|
81
|
+
setHeader(name, value) {
|
|
82
|
+
this.#deleteHeader(name);
|
|
83
|
+
this.#headers[name] = value;
|
|
84
|
+
return this;
|
|
85
|
+
}
|
|
86
|
+
/** Merge several default headers at once. Chainable. */
|
|
87
|
+
setHeaders(headers) {
|
|
88
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
89
|
+
this.setHeader(name, value);
|
|
90
|
+
}
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
/** Remove a default header (case-insensitive). Chainable. */
|
|
94
|
+
removeHeader(name) {
|
|
95
|
+
this.#deleteHeader(name);
|
|
96
|
+
return this;
|
|
97
|
+
}
|
|
98
|
+
/** A copy of the current default headers. */
|
|
99
|
+
getHeaders() {
|
|
100
|
+
return { ...this.#headers };
|
|
101
|
+
}
|
|
102
|
+
#deleteHeader(name) {
|
|
103
|
+
const lower = name.toLowerCase();
|
|
104
|
+
for (const key of Object.keys(this.#headers)) {
|
|
105
|
+
if (key.toLowerCase() === lower)
|
|
106
|
+
delete this.#headers[key];
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
get(url, options) {
|
|
110
|
+
return this.#request("GET", url, undefined, options);
|
|
111
|
+
}
|
|
112
|
+
delete(url, options) {
|
|
113
|
+
return this.#request("DELETE", url, undefined, options);
|
|
114
|
+
}
|
|
115
|
+
post(url, body, options) {
|
|
116
|
+
return this.#request("POST", url, body, options);
|
|
117
|
+
}
|
|
118
|
+
put(url, body, options) {
|
|
119
|
+
return this.#request("PUT", url, body, options);
|
|
120
|
+
}
|
|
121
|
+
patch(url, body, options) {
|
|
122
|
+
return this.#request("PATCH", url, body, options);
|
|
123
|
+
}
|
|
124
|
+
/** Send a request and return the raw `Response` (no parsing, no throw on non-2xx). */
|
|
125
|
+
raw(method, url, body, options = {}) {
|
|
126
|
+
return this.#send(method, url, body, options);
|
|
127
|
+
}
|
|
128
|
+
/** Derive a new client with merged defaults (e.g. a scope that adds a token). */
|
|
129
|
+
extend(options) {
|
|
130
|
+
return new HttpClient({
|
|
131
|
+
baseURL: options.baseURL ?? this.#baseURL,
|
|
132
|
+
headers: { ...this.#headers, ...options.headers },
|
|
133
|
+
token: options.token ?? this.#token,
|
|
134
|
+
credentials: options.credentials ?? this.#credentials,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
#resolveToken(override) {
|
|
138
|
+
if (override !== undefined)
|
|
139
|
+
return override;
|
|
140
|
+
return typeof this.#token === "function" ? this.#token() : this.#token;
|
|
141
|
+
}
|
|
142
|
+
#buildUrl(url, query) {
|
|
143
|
+
const base = /^[a-z][a-z\d+\-.]*:\/\//i.test(url)
|
|
144
|
+
? url
|
|
145
|
+
: this.#baseURL + url;
|
|
146
|
+
if (!query)
|
|
147
|
+
return base;
|
|
148
|
+
const params = new URLSearchParams();
|
|
149
|
+
for (const [key, value] of Object.entries(query)) {
|
|
150
|
+
if (value !== null && value !== undefined)
|
|
151
|
+
params.append(key, String(value));
|
|
152
|
+
}
|
|
153
|
+
const qs = params.toString();
|
|
154
|
+
if (qs === "")
|
|
155
|
+
return base;
|
|
156
|
+
return `${base}${base.includes("?") ? "&" : "?"}${qs}`;
|
|
157
|
+
}
|
|
158
|
+
#send(method, url, body, options) {
|
|
159
|
+
const headers = {
|
|
160
|
+
...this.#headers,
|
|
161
|
+
...options.headers,
|
|
162
|
+
};
|
|
163
|
+
const token = this.#resolveToken(options.token);
|
|
164
|
+
if (token != null && !hasHeader(headers, "authorization")) {
|
|
165
|
+
headers.Authorization = `Bearer ${token}`;
|
|
166
|
+
}
|
|
167
|
+
let payload;
|
|
168
|
+
if (body !== undefined && body !== null) {
|
|
169
|
+
if (shouldJsonEncode(body)) {
|
|
170
|
+
payload = JSON.stringify(body);
|
|
171
|
+
if (!hasHeader(headers, "content-type")) {
|
|
172
|
+
headers["Content-Type"] = "application/json";
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
// Already a valid BodyInit (string / FormData / Blob / …).
|
|
177
|
+
payload = body;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return fetch(this.#buildUrl(url, options.query), {
|
|
181
|
+
method,
|
|
182
|
+
headers,
|
|
183
|
+
body: payload,
|
|
184
|
+
signal: options.signal,
|
|
185
|
+
credentials: options.credentials ?? this.#credentials,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
async #request(method, url, body, options = {}) {
|
|
189
|
+
const response = await this.#send(method, url, body, options);
|
|
190
|
+
const data = await parseBody(response);
|
|
191
|
+
if (!response.ok)
|
|
192
|
+
throw new HttpError(response, data);
|
|
193
|
+
// `parse` validates at runtime; without it, `T` is the caller's
|
|
194
|
+
// unchecked assertion of the response shape (the usual HTTP boundary).
|
|
195
|
+
return options.parse ? options.parse(data) : data;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
/** Default same-origin client. Configure your own via `new HttpClient({ … })`. */
|
|
199
|
+
export const http = new HttpClient();
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export type { CookieOptions, PersistedSignalOptions, ShareData, StorageArea, WebStorageOptions, WindowSize, } from "./browser.js";
|
|
2
|
+
export { back, clipboard, cookie, forward, hash, mediaQuery, navigate, online, persistedSignal, prefersDark, queryParam, redirect, reload, replace, session, share, storage, visibility, WebStorage, windowSize, } from "./browser.js";
|
|
2
3
|
export { component, onMount, onUnmount } from "./component.js";
|
|
3
4
|
export { html, isTemplateResult } from "./html.js";
|
|
5
|
+
export type { HttpClientOptions, HttpRequestOptions } from "./http.js";
|
|
6
|
+
export { HttpClient, HttpError, http } from "./http.js";
|
|
4
7
|
export { hydrate } from "./hydrate.js";
|
|
5
8
|
export { batch, effect, isSignal, memo, onCleanup, type ReadSignal, type Signal, signal, untrack, } from "./reactive.js";
|
|
6
9
|
export { type Disposer, render } from "./render.js";
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
//
|
|
3
|
-
// Server-only exports (AuroraManager, Pages, renderPage, serveAssets) that pull
|
|
4
|
-
// node:fs / node:path / node:url live in `@c9up/aurora/server`. Keeping them off
|
|
5
|
-
// this barrel is what lets a browser bundle import the client primitives without
|
|
6
|
-
// the bundler dragging Node built-ins through the import graph.
|
|
7
|
-
export { redirect, reload, replace, storage } from "./browser.js";
|
|
1
|
+
export { back, clipboard, cookie, forward, hash, mediaQuery, navigate, online, persistedSignal, prefersDark, queryParam, redirect, reload, replace, session, share, storage, visibility, WebStorage, windowSize, } from "./browser.js";
|
|
8
2
|
export { component, onMount, onUnmount } from "./component.js";
|
|
9
3
|
export { html, isTemplateResult } from "./html.js";
|
|
4
|
+
export { HttpClient, HttpError, http } from "./http.js";
|
|
10
5
|
export { hydrate } from "./hydrate.js";
|
|
11
6
|
export { batch, effect, isSignal, memo, onCleanup, signal, untrack, } from "./reactive.js";
|
|
12
7
|
export { render } from "./render.js";
|
package/dist/reactive.d.ts
CHANGED
|
@@ -28,6 +28,13 @@ export interface Signal<T> {
|
|
|
28
28
|
* sites need to import this directly.
|
|
29
29
|
*/
|
|
30
30
|
export declare const SIGNAL_BRAND: unique symbol;
|
|
31
|
+
/**
|
|
32
|
+
* @internal Swap the ambient owner, returning the previous one so the
|
|
33
|
+
* caller can restore it. `component()` uses this to own the effects and
|
|
34
|
+
* memos a setup function creates, so they dispose at unmount instead of
|
|
35
|
+
* keeping their signal subscriptions alive forever.
|
|
36
|
+
*/
|
|
37
|
+
export declare function setOwner(owner: Array<() => void> | undefined): Array<() => void> | undefined;
|
|
31
38
|
/**
|
|
32
39
|
* Create a writable signal seeded with `initial`. Reads register the
|
|
33
40
|
* current observer; writes notify every observer that previously read.
|
package/dist/reactive.js
CHANGED
|
@@ -23,6 +23,24 @@ const pendingNotifications = new Set();
|
|
|
23
23
|
function activeObserver() {
|
|
24
24
|
return observerStack[observerStack.length - 1];
|
|
25
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Ambient disposal owner. A non-reactive scope (e.g. a component's setup
|
|
28
|
+
* run) registers an array here so effects/memos created during its
|
|
29
|
+
* execution push their disposer into it and are torn down when the scope
|
|
30
|
+
* ends. `undefined` at top level — no scope, no auto-disposal.
|
|
31
|
+
*/
|
|
32
|
+
let currentOwner;
|
|
33
|
+
/**
|
|
34
|
+
* @internal Swap the ambient owner, returning the previous one so the
|
|
35
|
+
* caller can restore it. `component()` uses this to own the effects and
|
|
36
|
+
* memos a setup function creates, so they dispose at unmount instead of
|
|
37
|
+
* keeping their signal subscriptions alive forever.
|
|
38
|
+
*/
|
|
39
|
+
export function setOwner(owner) {
|
|
40
|
+
const prev = currentOwner;
|
|
41
|
+
currentOwner = owner;
|
|
42
|
+
return prev;
|
|
43
|
+
}
|
|
26
44
|
/**
|
|
27
45
|
* Create a writable signal seeded with `initial`. Reads register the
|
|
28
46
|
* current observer; writes notify every observer that previously read.
|
|
@@ -129,7 +147,13 @@ export function effect(fn) {
|
|
|
129
147
|
},
|
|
130
148
|
};
|
|
131
149
|
eff.run();
|
|
132
|
-
|
|
150
|
+
const dispose = () => eff.dispose();
|
|
151
|
+
// Register with the ambient owner (e.g. a component's setup scope) so the
|
|
152
|
+
// effect is torn down when that scope ends. `memo()` builds on this — its
|
|
153
|
+
// internal recompute effect inherits the same ownership, which is what
|
|
154
|
+
// stops a memo created in component setup from leaking after unmount.
|
|
155
|
+
currentOwner?.push(dispose);
|
|
156
|
+
return dispose;
|
|
133
157
|
}
|
|
134
158
|
/**
|
|
135
159
|
* Register a cleanup callback against the currently-running effect.
|
package/dist/relay.js
CHANGED
|
@@ -21,7 +21,6 @@ const STATE = {
|
|
|
21
21
|
sse: null,
|
|
22
22
|
uid: null,
|
|
23
23
|
channels: new Map(),
|
|
24
|
-
pending: [],
|
|
25
24
|
};
|
|
26
25
|
let CONFIG = {
|
|
27
26
|
sseUrl: "/__relay/events",
|
|
@@ -60,17 +59,15 @@ const CLIENT = {
|
|
|
60
59
|
}
|
|
61
60
|
const adapted = handler;
|
|
62
61
|
handlers.add(adapted);
|
|
63
|
-
// Subscribe over POST as soon as we have a uid.
|
|
64
|
-
//
|
|
65
|
-
|
|
62
|
+
// Subscribe over POST as soon as we have a uid. Before the first uid (or
|
|
63
|
+
// during an auto-reconnect) the channel already lives in STATE.channels
|
|
64
|
+
// and is (re-)subscribed by the `connected` handler — so the server,
|
|
65
|
+
// which assigns a fresh uid per connection, always learns every channel.
|
|
66
|
+
if (STATE.uid) {
|
|
66
67
|
postSubscribe(channel).catch((err) => {
|
|
67
68
|
console.warn(`[aurora/relay] subscribe to ${channel} failed:`, err);
|
|
68
69
|
});
|
|
69
|
-
}
|
|
70
|
-
if (STATE.uid)
|
|
71
|
-
doSubscribe();
|
|
72
|
-
else
|
|
73
|
-
STATE.pending.push(doSubscribe);
|
|
70
|
+
}
|
|
74
71
|
// Detacher — only removes the local listener. The server-side
|
|
75
72
|
// subscription stays open; closing it would interrupt other
|
|
76
73
|
// listeners on the same channel.
|
|
@@ -85,7 +82,6 @@ const CLIENT = {
|
|
|
85
82
|
}
|
|
86
83
|
STATE.uid = null;
|
|
87
84
|
STATE.channels.clear();
|
|
88
|
-
STATE.pending.length = 0;
|
|
89
85
|
},
|
|
90
86
|
};
|
|
91
87
|
function open() {
|
|
@@ -95,9 +91,16 @@ function open() {
|
|
|
95
91
|
const data = safeJson(ev.data);
|
|
96
92
|
if (data && typeof data.uid === "string") {
|
|
97
93
|
STATE.uid = data.uid;
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
94
|
+
// Re-apply EVERY active subscription on each (re)connect. The server
|
|
95
|
+
// assigns a fresh uid per connection and has no memory of prior
|
|
96
|
+
// subscriptions, so both the first connect AND browser auto-reconnects
|
|
97
|
+
// must re-POST every live channel — otherwise the client silently
|
|
98
|
+
// stops receiving after a reconnect.
|
|
99
|
+
for (const channel of STATE.channels.keys()) {
|
|
100
|
+
postSubscribe(channel).catch((err) => {
|
|
101
|
+
console.warn(`[aurora/relay] re-subscribe to ${channel} failed:`, err);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
101
104
|
}
|
|
102
105
|
});
|
|
103
106
|
sse.onmessage = (ev) => {
|
|
@@ -41,7 +41,7 @@ export async function renderPage(ctx, pages, name, props, options = {}) {
|
|
|
41
41
|
<head>
|
|
42
42
|
<meta charset="utf-8" />
|
|
43
43
|
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
|
44
|
-
<script type="importmap">${
|
|
44
|
+
<script type="importmap">${escapeJsonForScript({ imports: importmap })}</script>
|
|
45
45
|
${options.headExtra ?? ""}
|
|
46
46
|
</head>
|
|
47
47
|
<body>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@c9up/aurora",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "Aurora — reactive UI runtime for the Ream framework. Tagged-template DOM, signal-based state, isomorphic SSR + hydration, zero build step.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|