Skip to content

Commit 8968610

Browse files
chore: update deps, switch to jemallocator, some tracking script golfing
Signed-off-by: Henry Gressmann <[email protected]>
1 parent 91ed122 commit 8968610

File tree

7 files changed

+117
-86
lines changed

7 files changed

+117
-86
lines changed

Cargo.lock

Lines changed: 34 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ bs58="0.5"
2929
serde={version="1.0", features=["derive"]}
3030
serde_json={version="1.0"}
3131
md-5="0.10"
32-
async-compression={version="0.4", default-features=false, features=["gzip"]}
32+
async-compression={version="0.4", default-features=false, features=["gzip", "tokio"]}
3333
tokio-tar={package="astral-tokio-tar", version="0.5"}
3434
sha3={version="0.10"}
3535
argon2={version="0.5"}
@@ -46,7 +46,6 @@ tracing-subscriber={version="0.3", features=["env-filter"]}
4646

4747
# web
4848
poem={version="3.1", default-features=false, features=[
49-
"time",
5049
"embed",
5150
"cookie",
5251
"compression",
@@ -80,10 +79,13 @@ refinery-core={version="0.8", default-features=false}
8079
maxminddb={version="0.26", optional=true}
8180
ahash="0.8"
8281

82+
[target.'cfg(not(target_env = "msvc"))'.dependencies]
83+
tikv-jemallocator="0.6"
84+
8385
[dev-dependencies]
8486
figment={version="*", features=["test"]}
8587
poem={version="*", features=["test"]}
86-
cookie={version="*"}
88+
cookie={version="*", default-features=false}
8789

8890
[features]
8991
default=["geoip"]

src/main.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,10 @@ fn setup_logger(log_level: tracing::Level) -> Result<()> {
4444
tracing::info!("Running in debug mode");
4545
Ok(())
4646
}
47+
48+
#[cfg(not(target_env = "msvc"))]
49+
use tikv_jemallocator::Jemalloc;
50+
51+
#[cfg(not(target_env = "msvc"))]
52+
#[global_allocator]
53+
static GLOBAL: Jemalloc = Jemalloc;

tracker/script.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tracker/script.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,20 @@ let scriptEl: HTMLScriptElement | null = null;
4949
let endpoint: string | null = null;
5050
let entity: string | null = null;
5151
let referrer: string | null = null;
52+
const noWindow = typeof window === "undefined";
5253

5354
if (typeof document !== "undefined") {
5455
scriptEl = document.querySelector(`script[src^="${import.meta.url}"]`);
55-
endpoint = scriptEl?.getAttribute("data-api") || (scriptEl && `${new URL(scriptEl.src).origin}/api/event`);
56+
endpoint = scriptEl?.getAttribute("data-api") || (scriptEl && new URL(scriptEl.src).origin + "/api/event");
5657
entity = scriptEl?.getAttribute("data-entity") || null;
5758
referrer = document.referrer;
5859
}
5960

60-
const LOCALHOST_REGEX = /^localhost$|^127(\.[0-9]+){0,2}\.[0-9]+$|^\[::1?\]$/;
61-
const log = (message: string) => console.info(`[liwan]: ${message}`);
62-
const ignore = (reason: string) => log(`Ignoring event: ${reason}`);
61+
const log = (message: string) => console.info("[liwan]: " + message);
62+
const ignore = (reason: string) => log("Ignoring event: " + reason);
63+
const reject = (message: string) => {
64+
throw new Error("Failed to send event: " + message);
65+
};
6366

6467
/**
6568
* Sends an event to the Liwan API.
@@ -82,36 +85,33 @@ const ignore = (reason: string) => log(`Ignoring event: ${reason}`);
8285
* ```
8386
*/
8487
export async function event(name = "pageview", options?: EventOptions): Promise<void> {
85-
if (typeof window === "undefined" && !options?.endpoint)
86-
return Promise.reject(new Error("endpoint is required in server-side environments"));
88+
const endpoint_url = options?.endpoint || endpoint;
89+
if (!endpoint_url) return reject("endpoint is required");
8790
if (typeof localStorage !== "undefined" && localStorage.getItem("disable-liwan")) return ignore("localStorage flag");
88-
if (LOCALHOST_REGEX.test(location.hostname) || location.protocol === "file:") return ignore("localhost");
89-
if (!endpoint && !options?.endpoint) return ignore("no endpoint");
90-
const utm = new URLSearchParams(location.search);
91+
if (/^localhost$|^127(?:\.\d+){0,2}\.\d+$|^\[::1?\]$/.test(location.hostname) || location.protocol === "file:")
92+
return ignore("localhost");
9193

92-
// biome-ignore lint/style/noNonNullAssertion: we know that endpoint is not null
93-
return fetch((options?.endpoint || endpoint)!, {
94+
const response = await fetch(endpoint_url, {
9495
method: "POST",
9596
headers: { "Content-Type": "application/json" },
9697
body: JSON.stringify(<Payload>{
97-
entity_id: options?.entity || entity,
9898
name,
99+
entity_id: options?.entity || entity,
99100
referrer: options?.referrer || referrer,
100-
url: options?.url || `${location.origin}${location.pathname}`,
101+
url: options?.url || location.origin + location.pathname,
101102
utm: {
102-
campaign: utm.get("utm_campaign"),
103-
content: utm.get("utm_content"),
104-
medium: utm.get("utm_medium"),
105-
source: utm.get("utm_source"),
106-
term: utm.get("utm_term"),
103+
...["campaign", "content", "medium", "source", "term"].map((v) => [
104+
v,
105+
new URLSearchParams(location.search).get("utm_" + v),
106+
]),
107107
},
108108
}),
109-
}).then((response) => {
110-
if (!response.ok) {
111-
log(`Failed to send event: ${response.statusText}`);
112-
return Promise.reject(new Error(`Failed to send event: ${response.statusText}`));
113-
}
114109
});
110+
111+
if (!response.ok) {
112+
log("Failed to send event: " + response.statusText);
113+
reject(response.statusText);
114+
}
115115
}
116116

117117
const trackPageviews = () => {
@@ -150,6 +150,6 @@ const trackPageviews = () => {
150150
page();
151151
};
152152

153-
if (typeof window !== "undefined" && !window.__liwan_loaded && scriptEl) {
153+
if (!noWindow && !window.__liwan_loaded && scriptEl) {
154154
trackPageviews();
155155
}

0 commit comments

Comments
 (0)