@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.
Files changed (70) hide show
  1. package/CHANGELOG.md +75 -0
  2. package/LICENSE +21 -0
  3. package/README.md +313 -2
  4. package/core/package.json +6 -0
  5. package/interstitial/package.json +6 -0
  6. package/lib/commonjs/core/adapters.js +287 -0
  7. package/lib/commonjs/core/beacons.js +70 -0
  8. package/lib/commonjs/core/cache.js +206 -0
  9. package/lib/commonjs/core/click.js +19 -0
  10. package/lib/commonjs/core/client.js +489 -0
  11. package/lib/commonjs/core/config.js +113 -0
  12. package/lib/commonjs/core/http.js +115 -0
  13. package/lib/commonjs/core/index.js +20 -0
  14. package/lib/commonjs/core/outbox.js +192 -0
  15. package/lib/commonjs/core/tracker.js +374 -0
  16. package/lib/commonjs/core/types.js +4 -0
  17. package/lib/commonjs/index.js +22 -0
  18. package/lib/commonjs/interstitial.js +7 -0
  19. package/lib/commonjs/react/EthiclyInterstitial.js +286 -0
  20. package/lib/commonjs/react/useEthiclyInterstitial.js +54 -0
  21. package/lib/commonjs/version.js +5 -0
  22. package/lib/module/core/adapters.js +276 -0
  23. package/lib/module/core/beacons.js +64 -0
  24. package/lib/module/core/cache.js +202 -0
  25. package/lib/module/core/click.js +15 -0
  26. package/lib/module/core/client.js +485 -0
  27. package/lib/module/core/config.js +104 -0
  28. package/lib/module/core/http.js +105 -0
  29. package/lib/module/core/index.js +6 -0
  30. package/lib/module/core/outbox.js +186 -0
  31. package/lib/module/core/tracker.js +370 -0
  32. package/lib/module/core/types.js +3 -0
  33. package/lib/module/index.js +4 -0
  34. package/lib/module/interstitial.js +3 -0
  35. package/lib/module/react/EthiclyInterstitial.js +260 -0
  36. package/lib/module/react/useEthiclyInterstitial.js +51 -0
  37. package/lib/module/version.js +2 -0
  38. package/lib/typescript/core/adapters.d.ts +138 -0
  39. package/lib/typescript/core/beacons.d.ts +32 -0
  40. package/lib/typescript/core/cache.d.ts +53 -0
  41. package/lib/typescript/core/click.d.ts +8 -0
  42. package/lib/typescript/core/client.d.ts +58 -0
  43. package/lib/typescript/core/config.d.ts +24 -0
  44. package/lib/typescript/core/http.d.ts +34 -0
  45. package/lib/typescript/core/index.d.ts +8 -0
  46. package/lib/typescript/core/outbox.d.ts +56 -0
  47. package/lib/typescript/core/tracker.d.ts +95 -0
  48. package/lib/typescript/core/types.d.ts +170 -0
  49. package/lib/typescript/index.d.ts +3 -0
  50. package/lib/typescript/interstitial.d.ts +2 -0
  51. package/lib/typescript/react/EthiclyInterstitial.d.ts +39 -0
  52. package/lib/typescript/react/useEthiclyInterstitial.d.ts +20 -0
  53. package/lib/typescript/version.d.ts +2 -0
  54. package/package.json +101 -4
  55. package/src/core/adapters.ts +337 -0
  56. package/src/core/beacons.ts +81 -0
  57. package/src/core/cache.ts +216 -0
  58. package/src/core/click.ts +16 -0
  59. package/src/core/client.ts +547 -0
  60. package/src/core/config.ts +111 -0
  61. package/src/core/http.ts +115 -0
  62. package/src/core/index.ts +14 -0
  63. package/src/core/outbox.ts +193 -0
  64. package/src/core/tracker.ts +371 -0
  65. package/src/core/types.ts +180 -0
  66. package/src/index.ts +5 -0
  67. package/src/interstitial.ts +4 -0
  68. package/src/react/EthiclyInterstitial.tsx +386 -0
  69. package/src/react/useEthiclyInterstitial.ts +74 -0
  70. package/src/version.ts +2 -0
@@ -0,0 +1,287 @@
1
+ "use strict";
2
+ // Default in-memory adapters and helpers that wrap the usual Expo modules.
3
+ // Nothing here imports a native module: the app passes the module in.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.DEFAULT_CACHE_SUBDIRECTORY = void 0;
6
+ exports.createMemoryStorage = createMemoryStorage;
7
+ exports.createMemoryFileCache = createMemoryFileCache;
8
+ exports.safeCacheSubdirectory = safeCacheSubdirectory;
9
+ exports.createExpoFileCache = createExpoFileCache;
10
+ exports.isReleaseBuild = isReleaseBuild;
11
+ exports.defaultAppState = defaultAppState;
12
+ exports.safeLogger = safeLogger;
13
+ exports.uuidv4 = uuidv4;
14
+ /** In-memory key-value store. Lost when the process ends. */
15
+ function createMemoryStorage() {
16
+ const data = new Map();
17
+ return {
18
+ data,
19
+ async getItem(k) { return data.has(k) ? data.get(k) : null; },
20
+ async setItem(k, v) { data.set(k, v); },
21
+ async removeItem(k) { data.delete(k); },
22
+ };
23
+ }
24
+ /**
25
+ * In-memory "cache" that downloads nothing: the uri it returns is the remote
26
+ * URL, so the player streams. The default when no fileCache is given.
27
+ */
28
+ function createMemoryFileCache() {
29
+ const files = new Set();
30
+ return {
31
+ async download(url) { files.add(url); return { uri: url, bytes: 0 }; },
32
+ async exists(uri) { return files.has(uri); },
33
+ async delete(uri) { files.delete(uri); },
34
+ async size() { return 0; },
35
+ async list() { return Array.from(files); },
36
+ };
37
+ }
38
+ /**
39
+ * Creative cache on disk via expo-file-system. Pass the module as your app
40
+ * imports it, on any Expo SDK from 50; the adapter works out which API it has:
41
+ * import * as FileSystem from 'expo-file-system'; // or 'expo-file-system/legacy'
42
+ * createEthicly({ ..., fileCache: createExpoFileCache(FileSystem) })
43
+ */
44
+ exports.DEFAULT_CACHE_SUBDIRECTORY = 'ethicly-ads/';
45
+ /**
46
+ * The cache sweep deletes every file in this directory that the SDK's index
47
+ * does not list, so it must be a folder of its own: one plain name directly
48
+ * under the app's cache directory. An empty, nested or traversing name ('',
49
+ * '/', '..', 'a/b') would point the sweep at the host app's files, so anything
50
+ * else falls back to the default rather than risk deleting them.
51
+ */
52
+ function safeCacheSubdirectory(subdirectory) {
53
+ if (typeof subdirectory !== 'string')
54
+ return exports.DEFAULT_CACHE_SUBDIRECTORY;
55
+ const name = subdirectory.trim().replace(/\/+$/, '');
56
+ if (!/^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/.test(name) || name === '.' || name.includes('..')) {
57
+ return exports.DEFAULT_CACHE_SUBDIRECTORY;
58
+ }
59
+ return `${name}/`;
60
+ }
61
+ function isModernFs(fs) {
62
+ const m = fs;
63
+ return !!m && typeof m.File === 'function' && typeof m.Directory === 'function'
64
+ && typeof m.File.downloadFileAsync === 'function' && !!m.Paths;
65
+ }
66
+ function isLegacyFs(fs) {
67
+ const l = fs;
68
+ return !!l && typeof l.cacheDirectory === 'string' && !!l.cacheDirectory
69
+ && typeof l.downloadAsync === 'function' && typeof l.getInfoAsync === 'function'
70
+ && typeof l.deleteAsync === 'function' && typeof l.makeDirectoryAsync === 'function';
71
+ }
72
+ /**
73
+ * A file cache over either expo-file-system API: the object API of SDK 54+
74
+ * (`File`, `Directory`, `Paths`), else the legacy functions (`getInfoAsync`,
75
+ * `downloadAsync`, ...). Anything else gets the streaming cache, which writes
76
+ * nothing to disk, and one warning through `logger` (default `console`).
77
+ */
78
+ function createExpoFileCache(fs, subdirectory = exports.DEFAULT_CACHE_SUBDIRECTORY, logger) {
79
+ const sub = safeCacheSubdirectory(subdirectory);
80
+ if (isModernFs(fs))
81
+ return modernFileCache(fs, sub);
82
+ if (isLegacyFs(fs))
83
+ return legacyFileCache(fs, sub);
84
+ safeLogger(logger || consoleLogger()).warn('createExpoFileCache: not an expo-file-system module (no File/Directory/Paths, no cacheDirectory); creatives will stream');
85
+ return createMemoryFileCache();
86
+ }
87
+ function consoleLogger() {
88
+ const c = globalThis.console;
89
+ return { warn: (m, ...rest) => { var _a; return (_a = c === null || c === void 0 ? void 0 : c.warn) === null || _a === void 0 ? void 0 : _a.call(c, m, ...rest); }, error: (m, ...rest) => { var _a; return (_a = c === null || c === void 0 ? void 0 : c.error) === null || _a === void 0 ? void 0 : _a.call(c, m, ...rest); } };
90
+ }
91
+ function legacyFileCache(fs, sub) {
92
+ const dir = `${fs.cacheDirectory}${sub}`;
93
+ let made = null;
94
+ const sizeOf = async (uri) => {
95
+ const info = await fs.getInfoAsync(uri, { size: true });
96
+ return info.exists && typeof info.size === 'number' ? info.size : 0;
97
+ };
98
+ return {
99
+ async download(url, fileName, opts) {
100
+ if (!made)
101
+ made = fs.makeDirectoryAsync(dir, { intermediates: true }).catch(() => undefined);
102
+ await made;
103
+ const target = dir + fileName;
104
+ const max = opts === null || opts === void 0 ? void 0 : opts.maxBytes;
105
+ let res;
106
+ let tooBig = false;
107
+ if (max !== undefined && fs.createDownloadResumable) {
108
+ // Stop as soon as the server announces, or we have written, more than the budget.
109
+ const dl = fs.createDownloadResumable(url, target, {}, (p) => {
110
+ if (tooBig)
111
+ return;
112
+ if (p.totalBytesExpectedToWrite > max || p.totalBytesWritten > max) {
113
+ tooBig = true;
114
+ void (dl.cancelAsync ? dl.cancelAsync() : dl.pauseAsync()).catch(() => undefined);
115
+ }
116
+ });
117
+ res = await dl.downloadAsync().catch(() => undefined);
118
+ }
119
+ else {
120
+ res = await fs.downloadAsync(url, target);
121
+ }
122
+ if (tooBig || !res || res.status < 200 || res.status >= 300) {
123
+ await fs.deleteAsync(target, { idempotent: true }).catch(() => undefined);
124
+ throw new Error(tooBig ? 'creative over the cache budget' : `download failed (${res ? res.status : 'cancelled'})`);
125
+ }
126
+ return { uri: res.uri, bytes: await sizeOf(res.uri) };
127
+ },
128
+ async exists(uri) { return (await fs.getInfoAsync(uri)).exists; },
129
+ async delete(uri) { await fs.deleteAsync(uri, { idempotent: true }); },
130
+ size: sizeOf,
131
+ uriFor: (fileName) => dir + fileName,
132
+ async list() {
133
+ if (!fs.readDirectoryAsync)
134
+ return [];
135
+ const names = await fs.readDirectoryAsync(dir).catch(() => []);
136
+ return names.map((n) => dir + n);
137
+ },
138
+ };
139
+ }
140
+ /**
141
+ * The same cache over the object API. Its calls are synchronous and throw on
142
+ * failure; every adapter method is async, so a throw becomes a rejection and
143
+ * never reaches the host. Files are addressed by uri strings built from the
144
+ * cache directory's uri, so the index and the sweep compare like with like.
145
+ */
146
+ function modernFileCache(fs, sub) {
147
+ let dirUri = null;
148
+ const dir = () => {
149
+ if (dirUri === null) {
150
+ const cache = fs.Paths.cache.uri;
151
+ dirUri = `${cache.endsWith('/') ? cache : `${cache}/`}${sub}`;
152
+ }
153
+ return dirUri;
154
+ };
155
+ const file = (uri) => new fs.File(uri);
156
+ const remove = (uri) => {
157
+ const f = file(uri);
158
+ if (f.exists)
159
+ f.delete();
160
+ };
161
+ const sizeOf = async (uri) => {
162
+ const f = file(uri);
163
+ return f.exists && typeof f.size === 'number' && f.size > 0 ? f.size : 0;
164
+ };
165
+ let made = false;
166
+ return {
167
+ async download(url, fileName, opts) {
168
+ if (!made) {
169
+ const d = new fs.Directory(dir());
170
+ if (!d.exists)
171
+ d.create({ intermediates: true, idempotent: true });
172
+ made = true;
173
+ }
174
+ const target = dir() + fileName;
175
+ const max = opts === null || opts === void 0 ? void 0 : opts.maxBytes;
176
+ let tooBig = false;
177
+ try {
178
+ remove(target);
179
+ const createTask = fs.File.createDownloadTask;
180
+ if (max !== undefined && typeof createTask === 'function') {
181
+ // Stop as soon as the server announces, or we have written, more than the budget.
182
+ const task = createTask.call(fs.File, url, file(target), {
183
+ onProgress: (p) => {
184
+ if (tooBig || !(p.totalBytes > max || p.bytesWritten > max))
185
+ return;
186
+ tooBig = true;
187
+ try {
188
+ task.cancel();
189
+ }
190
+ catch { /* already settled */ }
191
+ },
192
+ });
193
+ await task.downloadAsync();
194
+ }
195
+ else {
196
+ await fs.File.downloadFileAsync(url, file(target), { idempotent: true });
197
+ }
198
+ }
199
+ catch (e) {
200
+ try {
201
+ remove(target);
202
+ }
203
+ catch { /* nothing to remove */ }
204
+ throw tooBig ? new Error('creative over the cache budget') : e;
205
+ }
206
+ // Without a progress callback the whole file lands first: check it now
207
+ // and delete it if it is over the budget.
208
+ const bytes = await sizeOf(target);
209
+ if (tooBig || (max !== undefined && bytes > max)) {
210
+ try {
211
+ remove(target);
212
+ }
213
+ catch { /* ignore */ }
214
+ throw new Error('creative over the cache budget');
215
+ }
216
+ return { uri: target, bytes };
217
+ },
218
+ async exists(uri) { return file(uri).exists; },
219
+ async delete(uri) { remove(uri); },
220
+ size: sizeOf,
221
+ uriFor: (fileName) => dir() + fileName,
222
+ async list() {
223
+ const d = new fs.Directory(dir());
224
+ if (!d.exists)
225
+ return [];
226
+ // Files only: the SDK never creates folders here, so it never deletes one.
227
+ return d.list()
228
+ .filter((e) => e instanceof fs.File)
229
+ .map((e) => e.name || e.uri.replace(/\/+$/, '').split('/').pop() || '')
230
+ .filter((n) => n && n !== '.' && n !== '..' && !n.includes('/'))
231
+ .map((n) => dir() + n);
232
+ },
233
+ };
234
+ }
235
+ /**
236
+ * True only in a React Native release build (`__DEV__ === false`). Where
237
+ * `__DEV__` is not defined at all the code is not running from a Metro bundle
238
+ * (a Node script such as scripts/live-test.mjs, or tests), which is never a
239
+ * shipped app, so that counts as not release.
240
+ */
241
+ function isReleaseBuild() {
242
+ return typeof __DEV__ !== 'undefined' && __DEV__ === false;
243
+ }
244
+ /**
245
+ * React Native's AppState, or null outside React Native. The core takes no
246
+ * hard dependency on react-native: a require inside try/catch is an optional
247
+ * dependency for Metro (the React Native and Expo Metro configs both set
248
+ * allowOptionalDependencies), and in Node it simply fails and returns null.
249
+ */
250
+ function defaultAppState() {
251
+ try {
252
+ // @ts-ignore: the build compiles without Node's types, where require is undeclared.
253
+ const rn = typeof require === 'function' ? require('react-native') : null;
254
+ const s = rn && rn.AppState;
255
+ return s && typeof s.addEventListener === 'function' ? s : null;
256
+ }
257
+ catch {
258
+ return null;
259
+ }
260
+ }
261
+ /** Wrap a logger so a throwing logger can never reach the host app. */
262
+ function safeLogger(logger) {
263
+ const call = (level) => (message, ...rest) => {
264
+ var _a;
265
+ try {
266
+ (_a = logger === null || logger === void 0 ? void 0 : logger[level]) === null || _a === void 0 ? void 0 : _a.call(logger, `[ethicly] ${message}`, ...rest);
267
+ }
268
+ catch { /* never throw */ }
269
+ };
270
+ return { debug: call('debug'), info: call('info'), warn: call('warn'), error: call('error') };
271
+ }
272
+ /** UUID v4. Uses the platform RNG when there is one (Hermes has none by default). */
273
+ function uuidv4() {
274
+ const c = globalThis.crypto;
275
+ if (c && typeof c.randomUUID === 'function')
276
+ return c.randomUUID();
277
+ const b = new Uint8Array(16);
278
+ if (c && typeof c.getRandomValues === 'function')
279
+ c.getRandomValues(b);
280
+ else
281
+ for (let i = 0; i < 16; i++)
282
+ b[i] = Math.floor(Math.random() * 256);
283
+ b[6] = (b[6] & 0x0f) | 0x40;
284
+ b[8] = (b[8] & 0x3f) | 0x80;
285
+ const h = Array.from(b, (x) => x.toString(16).padStart(2, '0')).join('');
286
+ return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20)}`;
287
+ }
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ // Beacon delivery with the retry rules of doc "Rules that hold for every
3
+ // route": retry timeouts and 5xx with backoff, wait Retry-After on 429, never
4
+ // retry any other 4xx (422 included). /click never comes through here.
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ONE_HOUR_MS = exports.BEACON_TIMEOUT_MS = void 0;
7
+ exports.backoffCapMs = backoffCapMs;
8
+ exports.backoffMs = backoffMs;
9
+ exports.sendWithRetry = sendWithRetry;
10
+ const http_1 = require("./http");
11
+ exports.BEACON_TIMEOUT_MS = 10000;
12
+ exports.ONE_HOUR_MS = 3600 * 1000;
13
+ /** The backoff ceiling for a retry: 1 s doubling per attempt, capped at 60 s. */
14
+ function backoffCapMs(attempt) {
15
+ return Math.min(60000, 1000 * 2 ** Math.max(0, attempt - 1));
16
+ }
17
+ /**
18
+ * Full jitter: a uniformly random wait between 0 and the ceiling, so installs
19
+ * that failed together (one outage, one carrier NAT) do not retry together.
20
+ */
21
+ function backoffMs(attempt, random = Math.random) {
22
+ const r = random();
23
+ return Math.floor(backoffCapMs(attempt) * (r >= 0 && r < 1 ? r : 0.5));
24
+ }
25
+ /**
26
+ * Send one beacon, retrying timeouts, network errors and 5xx with backoff
27
+ * (full jitter under a ceiling of 1 s doubling, capped at 60 s) and 429
28
+ * after Retry-After, until the deadline (doc lifecycle rule 7: give up after
29
+ * one hour). Resolves with the last answer, or null when the deadline passed
30
+ * before it could be sent.
31
+ * `maxAttempts` is unlimited unless given. Never throws.
32
+ */
33
+ async function sendWithRetry(ctx, route, body, opts) {
34
+ var _a, _b;
35
+ const max = (_a = opts.maxAttempts) !== null && _a !== void 0 ? _a : Infinity;
36
+ let last = null;
37
+ for (let attempt = 1; attempt <= max; attempt++) {
38
+ const wait = ctx.gate.waitMs(route);
39
+ if (wait > 0) {
40
+ if (ctx.now() + wait > opts.deadline)
41
+ return last;
42
+ await (0, http_1.sleep)(wait);
43
+ }
44
+ if (ctx.now() > opts.deadline)
45
+ return last;
46
+ last = await (0, http_1.httpRequest)(ctx.fetch, `${ctx.serveBase()}/${route}`, {
47
+ method: 'POST',
48
+ headers: ctx.headers(),
49
+ body,
50
+ timeoutMs: exports.BEACON_TIMEOUT_MS,
51
+ });
52
+ if (last.status === 429) {
53
+ ctx.gate.block(route, last.retryAfterS);
54
+ ctx.log.warn(`/${route} rate limited, retry after ${(_b = last.retryAfterS) !== null && _b !== void 0 ? _b : 1}s`);
55
+ continue;
56
+ }
57
+ if (last.status === 0 || last.status >= 500) {
58
+ const b = backoffMs(attempt);
59
+ ctx.log.debug(`/${route} ${last.status === 0 ? 'timed out or failed' : `answered ${last.status}`}, attempt ${attempt}`);
60
+ if (attempt >= max || ctx.now() + b > opts.deadline)
61
+ return last;
62
+ await (0, http_1.sleep)(b);
63
+ continue;
64
+ }
65
+ if (last.status >= 400)
66
+ ctx.log.warn(`/${route} answered ${last.status}, not retried`, last.body);
67
+ return last;
68
+ }
69
+ return last;
70
+ }
@@ -0,0 +1,206 @@
1
+ "use strict";
2
+ // Creative cache: downloads the video and poster of a served ad (doc
3
+ // "Lifecycle and timing" step 3) within prefetch.max_cache_mb and
4
+ // prefetch.max_cached_creatives, evicting least recently used first.
5
+ //
6
+ // Disk use stays bounded even if the app dies mid-download or the index is
7
+ // lost (for example with the default in-memory storage):
8
+ // - a file's size is checked before it is written (HEAD Content-Length, and
9
+ // the adapter aborts a download that grows past the remaining budget);
10
+ // - the index records each file name as pending BEFORE its download starts,
11
+ // and is written again as soon as the download lands;
12
+ // - on first use every pending file, and every file in the SDK's cache
13
+ // directory that the index does not list, is deleted.
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.CreativeCache = void 0;
16
+ function hash(s) {
17
+ let h = 5381;
18
+ for (let i = 0; i < s.length; i++)
19
+ h = ((h << 5) + h + s.charCodeAt(i)) >>> 0;
20
+ return h.toString(36);
21
+ }
22
+ function extOf(url, fallback) {
23
+ const m = /\.([a-z0-9]{2,5})(?:[?#]|$)/i.exec(url);
24
+ return m ? m[1].toLowerCase() : fallback;
25
+ }
26
+ function baseName(uri) {
27
+ return uri.replace(/[?#].*$/, '').split('/').pop() || uri;
28
+ }
29
+ class CreativeCache {
30
+ constructor(files, storage, storageKey, now, log, probeSize = async () => null) {
31
+ this.files = files;
32
+ this.storage = storage;
33
+ this.storageKey = storageKey;
34
+ this.now = now;
35
+ this.log = log;
36
+ this.probeSize = probeSize;
37
+ this.entries = [];
38
+ /** File names whose download has started but not landed. */
39
+ this.pending = [];
40
+ this.loaded = null;
41
+ this.chain = Promise.resolve();
42
+ }
43
+ load() {
44
+ if (!this.loaded)
45
+ this.loaded = this.loadNow();
46
+ return this.loaded;
47
+ }
48
+ async loadNow() {
49
+ let pending = [];
50
+ try {
51
+ const raw = await this.storage.getItem(this.storageKey);
52
+ const parsed = raw ? JSON.parse(raw) : null;
53
+ if (Array.isArray(parsed))
54
+ this.entries = parsed; // 0.1.0 pre-release shape
55
+ else if (parsed && Array.isArray(parsed.entries)) {
56
+ this.entries = parsed.entries;
57
+ pending = Array.isArray(parsed.pending) ? parsed.pending : [];
58
+ }
59
+ }
60
+ catch {
61
+ this.entries = [];
62
+ }
63
+ await this.sweep(pending);
64
+ }
65
+ /**
66
+ * Delete the downloads a previous run left unfinished, and every file in the
67
+ * cache directory that the index does not list.
68
+ */
69
+ async sweep(pending) {
70
+ const known = new Set();
71
+ for (const e of this.entries)
72
+ for (const f of e.files)
73
+ known.add(baseName(f.uri));
74
+ // The directory listing below covers these too; this also works for an
75
+ // adapter that cannot list its directory.
76
+ if (this.files.uriFor) {
77
+ for (const name of pending) {
78
+ if (!known.has(name))
79
+ await this.files.delete(this.files.uriFor(name)).catch(() => undefined);
80
+ }
81
+ }
82
+ let listed = [];
83
+ try {
84
+ listed = this.files.list ? await this.files.list() : [];
85
+ }
86
+ catch (e) {
87
+ this.log.warn('could not list the creative cache', e);
88
+ }
89
+ for (const uri of listed) {
90
+ if (!known.has(baseName(uri))) {
91
+ await this.files.delete(uri).catch(() => undefined);
92
+ this.log.debug(`swept unindexed file ${baseName(uri)}`);
93
+ }
94
+ }
95
+ await this.persist();
96
+ }
97
+ persist() {
98
+ const index = { entries: this.entries, pending: this.pending };
99
+ return this.storage.setItem(this.storageKey, JSON.stringify(index)).catch(() => undefined);
100
+ }
101
+ /** Number of cached creatives and their total size, for tests and debugging. */
102
+ async stats() {
103
+ await this.load();
104
+ return {
105
+ creatives: this.entries.length,
106
+ bytes: this.entries.reduce((n, e) => n + this.size(e), 0),
107
+ keys: this.entries.map((e) => e.key),
108
+ };
109
+ }
110
+ /**
111
+ * Make sure the files of one creative are on disk. Never throws: a file that
112
+ * cannot be cached is simply left out of the result and streamed instead.
113
+ * `allowed()` is checked before every download so setAdsEnabled(false)
114
+ * stops a download that has not started yet.
115
+ */
116
+ ensure(adId, urls, limits, allowed) {
117
+ const run = this.chain.then(() => this.ensureNow(adId, urls, limits, allowed));
118
+ this.chain = run.catch(() => undefined);
119
+ return run;
120
+ }
121
+ async ensureNow(adId, urls, limits, allowed) {
122
+ await this.load();
123
+ if (limits.maxCreatives <= 0 || limits.maxBytes <= 0)
124
+ return { uris: {} };
125
+ const key = `${adId}:${hash(urls.join('|'))}`;
126
+ let entry = this.entries.find((e) => e.key === key);
127
+ if (entry) {
128
+ const present = await Promise.all(entry.files.map((f) => this.files.exists(f.uri).catch(() => false)));
129
+ entry.files = entry.files.filter((_, i) => present[i]);
130
+ }
131
+ else {
132
+ entry = { key, files: [], lastUsed: 0 };
133
+ this.entries.push(entry);
134
+ }
135
+ entry.lastUsed = this.now();
136
+ for (const url of urls) {
137
+ if (entry.files.some((f) => f.url === url))
138
+ continue;
139
+ if (!allowed())
140
+ break;
141
+ // One creative may use the whole budget (older ones are evicted), never more.
142
+ const budget = limits.maxBytes - this.size(entry);
143
+ const known = await this.probeSize(url).catch(() => null);
144
+ if (known !== null && known > budget) {
145
+ this.log.info(`${url} is ${known} bytes, over the cache budget: it will stream`);
146
+ continue;
147
+ }
148
+ // Known size: make room first, so the disk never holds more than the budget.
149
+ if (known !== null)
150
+ await this.evict(limits, entry, known);
151
+ const name = `ethicly-${adId}-${hash(url)}.${extOf(url, 'bin')}`;
152
+ this.pending = [name];
153
+ await this.persist();
154
+ try {
155
+ const res = await this.files.download(url, name, { maxBytes: budget });
156
+ const bytes = typeof res.bytes === 'number' && res.bytes > 0 ? res.bytes : await this.files.size(res.uri).catch(() => 0);
157
+ if (bytes > budget) {
158
+ await this.files.delete(res.uri).catch(() => undefined);
159
+ this.log.info(`${url} is over the cache budget: it will stream`);
160
+ }
161
+ else {
162
+ entry.files.push({ url, uri: res.uri, bytes });
163
+ }
164
+ }
165
+ catch (e) {
166
+ this.log.warn(`could not cache ${url}, it will stream`, e);
167
+ }
168
+ this.pending = [];
169
+ // Persist right after each file lands, so the index never lags the disk
170
+ // by more than the download in flight (which the sweep covers).
171
+ await this.evict(limits, entry);
172
+ await this.persist();
173
+ }
174
+ if (entry.files.length === 0)
175
+ this.entries = this.entries.filter((e) => e !== entry);
176
+ await this.evict(limits, entry);
177
+ await this.persist();
178
+ const uris = {};
179
+ for (const f of this.entries.includes(entry) ? entry.files : [])
180
+ uris[f.url] = f.uri;
181
+ return { uris };
182
+ }
183
+ size(e) {
184
+ return e.files.reduce((n, f) => n + f.bytes, 0);
185
+ }
186
+ /**
187
+ * LRU eviction, leaving room for `incoming` more bytes. The current entry
188
+ * goes last, and only if it alone is too big.
189
+ */
190
+ async evict(limits, current, incoming = 0) {
191
+ const total = () => this.entries.reduce((n, e) => n + this.size(e), 0) + incoming;
192
+ const victims = () => this.entries.filter((e) => e !== current).sort((a, b) => a.lastUsed - b.lastUsed);
193
+ while (this.entries.length > limits.maxCreatives || total() > limits.maxBytes) {
194
+ if (incoming > 0 && victims().length === 0)
195
+ break;
196
+ const v = victims()[0] || (this.entries.includes(current) ? current : undefined);
197
+ if (!v)
198
+ break;
199
+ this.entries = this.entries.filter((e) => e !== v);
200
+ for (const f of v.files)
201
+ await this.files.delete(f.uri).catch(() => undefined);
202
+ this.log.debug(`evicted creative ${v.key}`);
203
+ }
204
+ }
205
+ }
206
+ exports.CreativeCache = CreativeCache;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ // Doc "Click handling" step 3, the same rule as trackClickAndNavigate in
3
+ // frontend/public/ethicly.js.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.CLICK_TIMEOUT_MS = void 0;
6
+ exports.appendClickToken = appendClickToken;
7
+ /**
8
+ * Append ethicly_click=<token> to the destination. The separator is "&" when
9
+ * the URL already contains a "?" anywhere, otherwise "?". The token is
10
+ * percent-encoded and nothing else in the URL changes.
11
+ */
12
+ function appendClickToken(destinationUrl, clickToken) {
13
+ if (typeof clickToken !== 'string' || clickToken === '')
14
+ return destinationUrl;
15
+ const sep = destinationUrl.indexOf('?') >= 0 ? '&' : '?';
16
+ return `${destinationUrl}${sep}ethicly_click=${encodeURIComponent(clickToken)}`;
17
+ }
18
+ /** The /click budget: the website snippet aborts at exactly 500 ms. */
19
+ exports.CLICK_TIMEOUT_MS = 500;