@ethicly/react-native 0.0.0-stage → 0.2.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/CHANGELOG.md +75 -0
- package/LICENSE +21 -0
- package/README.md +313 -2
- package/core/package.json +6 -0
- package/interstitial/package.json +6 -0
- package/lib/commonjs/core/adapters.js +287 -0
- package/lib/commonjs/core/beacons.js +70 -0
- package/lib/commonjs/core/cache.js +206 -0
- package/lib/commonjs/core/click.js +19 -0
- package/lib/commonjs/core/client.js +489 -0
- package/lib/commonjs/core/config.js +113 -0
- package/lib/commonjs/core/http.js +115 -0
- package/lib/commonjs/core/index.js +20 -0
- package/lib/commonjs/core/outbox.js +192 -0
- package/lib/commonjs/core/tracker.js +374 -0
- package/lib/commonjs/core/types.js +4 -0
- package/lib/commonjs/index.js +22 -0
- package/lib/commonjs/interstitial.js +7 -0
- package/lib/commonjs/react/EthiclyInterstitial.js +286 -0
- package/lib/commonjs/react/useEthiclyInterstitial.js +54 -0
- package/lib/commonjs/version.js +5 -0
- package/lib/module/core/adapters.js +276 -0
- package/lib/module/core/beacons.js +64 -0
- package/lib/module/core/cache.js +202 -0
- package/lib/module/core/click.js +15 -0
- package/lib/module/core/client.js +485 -0
- package/lib/module/core/config.js +104 -0
- package/lib/module/core/http.js +105 -0
- package/lib/module/core/index.js +6 -0
- package/lib/module/core/outbox.js +186 -0
- package/lib/module/core/tracker.js +370 -0
- package/lib/module/core/types.js +3 -0
- package/lib/module/index.js +4 -0
- package/lib/module/interstitial.js +3 -0
- package/lib/module/react/EthiclyInterstitial.js +260 -0
- package/lib/module/react/useEthiclyInterstitial.js +51 -0
- package/lib/module/version.js +2 -0
- package/lib/typescript/core/adapters.d.ts +138 -0
- package/lib/typescript/core/beacons.d.ts +32 -0
- package/lib/typescript/core/cache.d.ts +53 -0
- package/lib/typescript/core/click.d.ts +8 -0
- package/lib/typescript/core/client.d.ts +58 -0
- package/lib/typescript/core/config.d.ts +24 -0
- package/lib/typescript/core/http.d.ts +34 -0
- package/lib/typescript/core/index.d.ts +8 -0
- package/lib/typescript/core/outbox.d.ts +56 -0
- package/lib/typescript/core/tracker.d.ts +95 -0
- package/lib/typescript/core/types.d.ts +170 -0
- package/lib/typescript/index.d.ts +3 -0
- package/lib/typescript/interstitial.d.ts +2 -0
- package/lib/typescript/react/EthiclyInterstitial.d.ts +39 -0
- package/lib/typescript/react/useEthiclyInterstitial.d.ts +20 -0
- package/lib/typescript/version.d.ts +2 -0
- package/package.json +101 -4
- package/src/core/adapters.ts +337 -0
- package/src/core/beacons.ts +81 -0
- package/src/core/cache.ts +216 -0
- package/src/core/click.ts +16 -0
- package/src/core/client.ts +547 -0
- package/src/core/config.ts +111 -0
- package/src/core/http.ts +115 -0
- package/src/core/index.ts +14 -0
- package/src/core/outbox.ts +193 -0
- package/src/core/tracker.ts +371 -0
- package/src/core/types.ts +180 -0
- package/src/index.ts +5 -0
- package/src/interstitial.ts +4 -0
- package/src/react/EthiclyInterstitial.tsx +386 -0
- package/src/react/useEthiclyInterstitial.ts +74 -0
- package/src/version.ts +2 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Beacon delivery with the retry rules of doc "Rules that hold for every
|
|
2
|
+
// route": retry timeouts and 5xx with backoff, wait Retry-After on 429, never
|
|
3
|
+
// retry any other 4xx (422 included). /click never comes through here.
|
|
4
|
+
import { httpRequest, sleep } from './http';
|
|
5
|
+
export const BEACON_TIMEOUT_MS = 10000;
|
|
6
|
+
export const ONE_HOUR_MS = 3600 * 1000;
|
|
7
|
+
/** The backoff ceiling for a retry: 1 s doubling per attempt, capped at 60 s. */
|
|
8
|
+
export function backoffCapMs(attempt) {
|
|
9
|
+
return Math.min(60000, 1000 * 2 ** Math.max(0, attempt - 1));
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Full jitter: a uniformly random wait between 0 and the ceiling, so installs
|
|
13
|
+
* that failed together (one outage, one carrier NAT) do not retry together.
|
|
14
|
+
*/
|
|
15
|
+
export function backoffMs(attempt, random = Math.random) {
|
|
16
|
+
const r = random();
|
|
17
|
+
return Math.floor(backoffCapMs(attempt) * (r >= 0 && r < 1 ? r : 0.5));
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Send one beacon, retrying timeouts, network errors and 5xx with backoff
|
|
21
|
+
* (full jitter under a ceiling of 1 s doubling, capped at 60 s) and 429
|
|
22
|
+
* after Retry-After, until the deadline (doc lifecycle rule 7: give up after
|
|
23
|
+
* one hour). Resolves with the last answer, or null when the deadline passed
|
|
24
|
+
* before it could be sent.
|
|
25
|
+
* `maxAttempts` is unlimited unless given. Never throws.
|
|
26
|
+
*/
|
|
27
|
+
export async function sendWithRetry(ctx, route, body, opts) {
|
|
28
|
+
var _a, _b;
|
|
29
|
+
const max = (_a = opts.maxAttempts) !== null && _a !== void 0 ? _a : Infinity;
|
|
30
|
+
let last = null;
|
|
31
|
+
for (let attempt = 1; attempt <= max; attempt++) {
|
|
32
|
+
const wait = ctx.gate.waitMs(route);
|
|
33
|
+
if (wait > 0) {
|
|
34
|
+
if (ctx.now() + wait > opts.deadline)
|
|
35
|
+
return last;
|
|
36
|
+
await sleep(wait);
|
|
37
|
+
}
|
|
38
|
+
if (ctx.now() > opts.deadline)
|
|
39
|
+
return last;
|
|
40
|
+
last = await httpRequest(ctx.fetch, `${ctx.serveBase()}/${route}`, {
|
|
41
|
+
method: 'POST',
|
|
42
|
+
headers: ctx.headers(),
|
|
43
|
+
body,
|
|
44
|
+
timeoutMs: BEACON_TIMEOUT_MS,
|
|
45
|
+
});
|
|
46
|
+
if (last.status === 429) {
|
|
47
|
+
ctx.gate.block(route, last.retryAfterS);
|
|
48
|
+
ctx.log.warn(`/${route} rate limited, retry after ${(_b = last.retryAfterS) !== null && _b !== void 0 ? _b : 1}s`);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (last.status === 0 || last.status >= 500) {
|
|
52
|
+
const b = backoffMs(attempt);
|
|
53
|
+
ctx.log.debug(`/${route} ${last.status === 0 ? 'timed out or failed' : `answered ${last.status}`}, attempt ${attempt}`);
|
|
54
|
+
if (attempt >= max || ctx.now() + b > opts.deadline)
|
|
55
|
+
return last;
|
|
56
|
+
await sleep(b);
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (last.status >= 400)
|
|
60
|
+
ctx.log.warn(`/${route} answered ${last.status}, not retried`, last.body);
|
|
61
|
+
return last;
|
|
62
|
+
}
|
|
63
|
+
return last;
|
|
64
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
// Creative cache: downloads the video and poster of a served ad (doc
|
|
2
|
+
// "Lifecycle and timing" step 3) within prefetch.max_cache_mb and
|
|
3
|
+
// prefetch.max_cached_creatives, evicting least recently used first.
|
|
4
|
+
//
|
|
5
|
+
// Disk use stays bounded even if the app dies mid-download or the index is
|
|
6
|
+
// lost (for example with the default in-memory storage):
|
|
7
|
+
// - a file's size is checked before it is written (HEAD Content-Length, and
|
|
8
|
+
// the adapter aborts a download that grows past the remaining budget);
|
|
9
|
+
// - the index records each file name as pending BEFORE its download starts,
|
|
10
|
+
// and is written again as soon as the download lands;
|
|
11
|
+
// - on first use every pending file, and every file in the SDK's cache
|
|
12
|
+
// directory that the index does not list, is deleted.
|
|
13
|
+
function hash(s) {
|
|
14
|
+
let h = 5381;
|
|
15
|
+
for (let i = 0; i < s.length; i++)
|
|
16
|
+
h = ((h << 5) + h + s.charCodeAt(i)) >>> 0;
|
|
17
|
+
return h.toString(36);
|
|
18
|
+
}
|
|
19
|
+
function extOf(url, fallback) {
|
|
20
|
+
const m = /\.([a-z0-9]{2,5})(?:[?#]|$)/i.exec(url);
|
|
21
|
+
return m ? m[1].toLowerCase() : fallback;
|
|
22
|
+
}
|
|
23
|
+
function baseName(uri) {
|
|
24
|
+
return uri.replace(/[?#].*$/, '').split('/').pop() || uri;
|
|
25
|
+
}
|
|
26
|
+
export class CreativeCache {
|
|
27
|
+
constructor(files, storage, storageKey, now, log, probeSize = async () => null) {
|
|
28
|
+
this.files = files;
|
|
29
|
+
this.storage = storage;
|
|
30
|
+
this.storageKey = storageKey;
|
|
31
|
+
this.now = now;
|
|
32
|
+
this.log = log;
|
|
33
|
+
this.probeSize = probeSize;
|
|
34
|
+
this.entries = [];
|
|
35
|
+
/** File names whose download has started but not landed. */
|
|
36
|
+
this.pending = [];
|
|
37
|
+
this.loaded = null;
|
|
38
|
+
this.chain = Promise.resolve();
|
|
39
|
+
}
|
|
40
|
+
load() {
|
|
41
|
+
if (!this.loaded)
|
|
42
|
+
this.loaded = this.loadNow();
|
|
43
|
+
return this.loaded;
|
|
44
|
+
}
|
|
45
|
+
async loadNow() {
|
|
46
|
+
let pending = [];
|
|
47
|
+
try {
|
|
48
|
+
const raw = await this.storage.getItem(this.storageKey);
|
|
49
|
+
const parsed = raw ? JSON.parse(raw) : null;
|
|
50
|
+
if (Array.isArray(parsed))
|
|
51
|
+
this.entries = parsed; // 0.1.0 pre-release shape
|
|
52
|
+
else if (parsed && Array.isArray(parsed.entries)) {
|
|
53
|
+
this.entries = parsed.entries;
|
|
54
|
+
pending = Array.isArray(parsed.pending) ? parsed.pending : [];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
this.entries = [];
|
|
59
|
+
}
|
|
60
|
+
await this.sweep(pending);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Delete the downloads a previous run left unfinished, and every file in the
|
|
64
|
+
* cache directory that the index does not list.
|
|
65
|
+
*/
|
|
66
|
+
async sweep(pending) {
|
|
67
|
+
const known = new Set();
|
|
68
|
+
for (const e of this.entries)
|
|
69
|
+
for (const f of e.files)
|
|
70
|
+
known.add(baseName(f.uri));
|
|
71
|
+
// The directory listing below covers these too; this also works for an
|
|
72
|
+
// adapter that cannot list its directory.
|
|
73
|
+
if (this.files.uriFor) {
|
|
74
|
+
for (const name of pending) {
|
|
75
|
+
if (!known.has(name))
|
|
76
|
+
await this.files.delete(this.files.uriFor(name)).catch(() => undefined);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
let listed = [];
|
|
80
|
+
try {
|
|
81
|
+
listed = this.files.list ? await this.files.list() : [];
|
|
82
|
+
}
|
|
83
|
+
catch (e) {
|
|
84
|
+
this.log.warn('could not list the creative cache', e);
|
|
85
|
+
}
|
|
86
|
+
for (const uri of listed) {
|
|
87
|
+
if (!known.has(baseName(uri))) {
|
|
88
|
+
await this.files.delete(uri).catch(() => undefined);
|
|
89
|
+
this.log.debug(`swept unindexed file ${baseName(uri)}`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
await this.persist();
|
|
93
|
+
}
|
|
94
|
+
persist() {
|
|
95
|
+
const index = { entries: this.entries, pending: this.pending };
|
|
96
|
+
return this.storage.setItem(this.storageKey, JSON.stringify(index)).catch(() => undefined);
|
|
97
|
+
}
|
|
98
|
+
/** Number of cached creatives and their total size, for tests and debugging. */
|
|
99
|
+
async stats() {
|
|
100
|
+
await this.load();
|
|
101
|
+
return {
|
|
102
|
+
creatives: this.entries.length,
|
|
103
|
+
bytes: this.entries.reduce((n, e) => n + this.size(e), 0),
|
|
104
|
+
keys: this.entries.map((e) => e.key),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Make sure the files of one creative are on disk. Never throws: a file that
|
|
109
|
+
* cannot be cached is simply left out of the result and streamed instead.
|
|
110
|
+
* `allowed()` is checked before every download so setAdsEnabled(false)
|
|
111
|
+
* stops a download that has not started yet.
|
|
112
|
+
*/
|
|
113
|
+
ensure(adId, urls, limits, allowed) {
|
|
114
|
+
const run = this.chain.then(() => this.ensureNow(adId, urls, limits, allowed));
|
|
115
|
+
this.chain = run.catch(() => undefined);
|
|
116
|
+
return run;
|
|
117
|
+
}
|
|
118
|
+
async ensureNow(adId, urls, limits, allowed) {
|
|
119
|
+
await this.load();
|
|
120
|
+
if (limits.maxCreatives <= 0 || limits.maxBytes <= 0)
|
|
121
|
+
return { uris: {} };
|
|
122
|
+
const key = `${adId}:${hash(urls.join('|'))}`;
|
|
123
|
+
let entry = this.entries.find((e) => e.key === key);
|
|
124
|
+
if (entry) {
|
|
125
|
+
const present = await Promise.all(entry.files.map((f) => this.files.exists(f.uri).catch(() => false)));
|
|
126
|
+
entry.files = entry.files.filter((_, i) => present[i]);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
entry = { key, files: [], lastUsed: 0 };
|
|
130
|
+
this.entries.push(entry);
|
|
131
|
+
}
|
|
132
|
+
entry.lastUsed = this.now();
|
|
133
|
+
for (const url of urls) {
|
|
134
|
+
if (entry.files.some((f) => f.url === url))
|
|
135
|
+
continue;
|
|
136
|
+
if (!allowed())
|
|
137
|
+
break;
|
|
138
|
+
// One creative may use the whole budget (older ones are evicted), never more.
|
|
139
|
+
const budget = limits.maxBytes - this.size(entry);
|
|
140
|
+
const known = await this.probeSize(url).catch(() => null);
|
|
141
|
+
if (known !== null && known > budget) {
|
|
142
|
+
this.log.info(`${url} is ${known} bytes, over the cache budget: it will stream`);
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
// Known size: make room first, so the disk never holds more than the budget.
|
|
146
|
+
if (known !== null)
|
|
147
|
+
await this.evict(limits, entry, known);
|
|
148
|
+
const name = `ethicly-${adId}-${hash(url)}.${extOf(url, 'bin')}`;
|
|
149
|
+
this.pending = [name];
|
|
150
|
+
await this.persist();
|
|
151
|
+
try {
|
|
152
|
+
const res = await this.files.download(url, name, { maxBytes: budget });
|
|
153
|
+
const bytes = typeof res.bytes === 'number' && res.bytes > 0 ? res.bytes : await this.files.size(res.uri).catch(() => 0);
|
|
154
|
+
if (bytes > budget) {
|
|
155
|
+
await this.files.delete(res.uri).catch(() => undefined);
|
|
156
|
+
this.log.info(`${url} is over the cache budget: it will stream`);
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
entry.files.push({ url, uri: res.uri, bytes });
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
catch (e) {
|
|
163
|
+
this.log.warn(`could not cache ${url}, it will stream`, e);
|
|
164
|
+
}
|
|
165
|
+
this.pending = [];
|
|
166
|
+
// Persist right after each file lands, so the index never lags the disk
|
|
167
|
+
// by more than the download in flight (which the sweep covers).
|
|
168
|
+
await this.evict(limits, entry);
|
|
169
|
+
await this.persist();
|
|
170
|
+
}
|
|
171
|
+
if (entry.files.length === 0)
|
|
172
|
+
this.entries = this.entries.filter((e) => e !== entry);
|
|
173
|
+
await this.evict(limits, entry);
|
|
174
|
+
await this.persist();
|
|
175
|
+
const uris = {};
|
|
176
|
+
for (const f of this.entries.includes(entry) ? entry.files : [])
|
|
177
|
+
uris[f.url] = f.uri;
|
|
178
|
+
return { uris };
|
|
179
|
+
}
|
|
180
|
+
size(e) {
|
|
181
|
+
return e.files.reduce((n, f) => n + f.bytes, 0);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* LRU eviction, leaving room for `incoming` more bytes. The current entry
|
|
185
|
+
* goes last, and only if it alone is too big.
|
|
186
|
+
*/
|
|
187
|
+
async evict(limits, current, incoming = 0) {
|
|
188
|
+
const total = () => this.entries.reduce((n, e) => n + this.size(e), 0) + incoming;
|
|
189
|
+
const victims = () => this.entries.filter((e) => e !== current).sort((a, b) => a.lastUsed - b.lastUsed);
|
|
190
|
+
while (this.entries.length > limits.maxCreatives || total() > limits.maxBytes) {
|
|
191
|
+
if (incoming > 0 && victims().length === 0)
|
|
192
|
+
break;
|
|
193
|
+
const v = victims()[0] || (this.entries.includes(current) ? current : undefined);
|
|
194
|
+
if (!v)
|
|
195
|
+
break;
|
|
196
|
+
this.entries = this.entries.filter((e) => e !== v);
|
|
197
|
+
for (const f of v.files)
|
|
198
|
+
await this.files.delete(f.uri).catch(() => undefined);
|
|
199
|
+
this.log.debug(`evicted creative ${v.key}`);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Doc "Click handling" step 3, the same rule as trackClickAndNavigate in
|
|
2
|
+
// frontend/public/ethicly.js.
|
|
3
|
+
/**
|
|
4
|
+
* Append ethicly_click=<token> to the destination. The separator is "&" when
|
|
5
|
+
* the URL already contains a "?" anywhere, otherwise "?". The token is
|
|
6
|
+
* percent-encoded and nothing else in the URL changes.
|
|
7
|
+
*/
|
|
8
|
+
export function appendClickToken(destinationUrl, clickToken) {
|
|
9
|
+
if (typeof clickToken !== 'string' || clickToken === '')
|
|
10
|
+
return destinationUrl;
|
|
11
|
+
const sep = destinationUrl.indexOf('?') >= 0 ? '&' : '?';
|
|
12
|
+
return `${destinationUrl}${sep}ethicly_click=${encodeURIComponent(clickToken)}`;
|
|
13
|
+
}
|
|
14
|
+
/** The /click budget: the website snippet aborts at exactly 500 ms. */
|
|
15
|
+
export const CLICK_TIMEOUT_MS = 500;
|