Skip to content

Commit

Permalink
Minor cleanup of signals, minor tweaks for smaller bundle size (#4)
Browse files Browse the repository at this point in the history
* Reusable performance.now helper

* Remove unused signal typing (in sdk), shorten some names

* Set date in changelog
  • Loading branch information
gzuidhof authored Dec 13, 2023
1 parent 6d5739e commit eb99561
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 44 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# changelog

## 0.1.2
**Date**: 2023-12-13

* Removed some unused typings, renamed some things to save on bundle size a bit.

## 0.1.1
**Date**: 2023-12-12

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@friendlycaptcha/sdk",
"version": "0.1.1",
"version": "0.1.2",
"description": "In-browser SDK for Friendly Captcha v2 (currently in preview only)",
"main": "dist/sdk.js",
"type": "module",
Expand Down
6 changes: 3 additions & 3 deletions src/signals/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
RootSignalsV1Raw,
TouchMoveSignal,
} from "../types/signals";
import { windowPerformanceNow } from "../util/performance";
import { takeRecords as takeTraceRecords } from "./collectStacktrace";
import { buildOnlineMetric } from "./online";

Expand Down Expand Up @@ -461,7 +462,7 @@ export class Signals {
wid: widgetId,
conv: 0,
t: {
pnow: (p && p.now && p.now()) || 0,
pnow: windowPerformanceNow(),
pto: (p && p.timeOrigin) || 0,
ts: Date.now(),
},
Expand All @@ -485,8 +486,7 @@ export class Signals {
tm: this.gtm(),
bh: this.bh,
stack: new Error().stack || "",
trace: takeTraceRecords(),
ptrace: [],
trc: takeTraceRecords(),
};

return sig;
Expand Down
62 changes: 29 additions & 33 deletions src/signals/collectStacktrace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,23 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

import { windowPerformanceNow } from "../util/performance";

/**
* @internal
*/
export type RootTraceRecord = {
dateNow: number;
perfNow: number;
name: string;
stack: string;
/** Date.now() */
d: number;
/** Performance.now() */
pnow: number;
/** Name */
n: string;
/** Stack trace */
st: string;
};

/**
* @internal
*/
export type ProcessedTraceRecord = [
/** performance now, at first occurance */
number,
/** patch source */
number,
/** stack depth */
number,
/** bottom frame */
string,
];

const isFunc = function (value: unknown): value is Function {
return typeof value === "function";
};
Expand Down Expand Up @@ -84,23 +77,27 @@ export const takeRecords = (function () {
*/
};

// We save some bundle size by using these constants.
const p = "prototype";
const w = window;

/**
* The names of patches must not change, as they are used to
* identify the source of the stack trace by the Agent and Backend
*/
const patches: [string, object, string][] = [
// Getters as non-function
["Document.prototype.documentElement", window.Document.prototype, "documentElement"],
["Element.prototype.shadowRoot", window.Element.prototype, "shadowRoot"],
["Node.prototype.nodeType", window.Node.prototype, "nodeType"],
["Document." + p + ".documentElement", w.Document[p], "documentElement"],
["Element." + p + ".shadowRoot", w.Element[p], "shadowRoot"],
["Node." + p + ".nodeType", w.Node[p], "nodeType"],
// Values holding functions
["eval", window, "eval"],
["Object.is", window.Object, "is"],
["Array.prototype.slice", window.Array.prototype, "slice"],
["Document.prototype.querySelectorAll", window.Document.prototype, "querySelectorAll"],
["Document.prototype.createElement", window.Document.prototype, "createElement"],
["EventTarget.prototype.dispatchEvent", window.EventTarget.prototype, "dispatchEvent"],
["Promise.prototype.constructor", window.Promise.prototype, "constructor"],
["eval", w, "eval"],
["Object.is", w.Object, "is"],
["Array." + p + "e.slice", w.Array[p], "slice"],
["Document." + p + ".querySelectorAll", w.Document[p], "querySelectorAll"],
["Document." + p + ".createElement", w.Document[p], "createElement"],
["EventTarget." + p + ".dispatchEvent", w.EventTarget[p], "dispatchEvent"],
["Promise." + p + "e.constructor", w.Promise[p], "constructor"],
];

patches.forEach(function ([name, target, prop]) {
Expand All @@ -120,12 +117,11 @@ export const takeRecords = (function () {
}

const newAccessor = function fcPatch(this: unknown, ...args: unknown[]) {
const perf = window.performance;
const record: RootTraceRecord = {
dateNow: Date.now(),
perfNow: (perf && perf.now()) || 0,
name,
stack: getStackSafely(),
d: Date.now(),
pnow: windowPerformanceNow(),
n: name,
st: getStackSafely(),
};
queue.push(record);

Expand Down
4 changes: 2 additions & 2 deletions src/signals/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
import { _FocusTrigger, _RootTrigger, _TriggerBase } from "../types/trigger";
import { StartMode } from "../types/widget";
import { windowPerformanceNow } from "../util/performance";

/**
* @internal
Expand All @@ -16,8 +17,7 @@ export function getTrigger(
el: HTMLElement,
ev?: FocusEvent | UIEvent,
): _RootTrigger {
const p = window.performance;
const t = p ? p.now() : 0;
const t = windowPerformanceNow();
const bcr = el.getBoundingClientRect();

const trigger: _TriggerBase = {
Expand Down
5 changes: 2 additions & 3 deletions src/types/signals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

import { ProcessedTraceRecord, RootTraceRecord } from "../signals/collectStacktrace";
import { RootTraceRecord } from "../signals/collectStacktrace";

/**
* 0 1 2 3 4 5 6
Expand Down Expand Up @@ -52,8 +52,7 @@ export type RootSignalsV1Raw<MetricVectorType = OnlineMetricStateVector> = {
bh: BehaviorSignal<MetricVectorType>;

stack: string;
trace: RootTraceRecord[];
ptrace: ProcessedTraceRecord[];
trc: RootTraceRecord[];
};

/**
Expand Down
5 changes: 5 additions & 0 deletions src/util/performance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** Returns window.performance.now() if it is available, otherwise returns 0 */
export function windowPerformanceNow() {
const p = window.performance;
return p ? p.now() : 0
}

0 comments on commit eb99561

Please sign in to comment.