@zerogpu/browser-extension-sdk 0.2.3 → 0.3.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/README.md +35 -0
- package/bin/zerogpu.mjs +171 -29
- package/dist/assets/zerogpu-assets.json +2 -2
- package/dist/extension/zerogpu-background.js +42 -1
- package/dist/extension/zerogpu-background.js.map +1 -1
- package/dist/extension/zerogpu-offscreen.js +182 -10
- package/dist/extension/zerogpu-offscreen.js.map +1 -1
- package/dist/extension/zerogpu-settings-link.js +9 -0
- package/dist/extension/zerogpu-settings-link.js.map +1 -1
- package/dist/loader.js +1 -1
- package/dist/loader.js.map +1 -1
- package/dist/manifest.json +5 -5
- package/dist/types/extension/background.d.ts +10 -0
- package/dist/types/extension/consent.d.ts +0 -5
- package/dist/types/extension/offscreen.d.ts +23 -0
- package/dist/types/public-api.d.ts +14 -0
- package/dist/types/runtime/custom-params.d.ts +34 -0
- package/dist/types/transport/http.d.ts +3 -0
- package/dist/v/{zerogpu-browser-sdk.85b3f7e3cc11.umd.js → zerogpu-browser-sdk.8d29febc744c.umd.js} +177 -9
- package/dist/v/zerogpu-browser-sdk.8d29febc744c.umd.js.map +1 -0
- package/dist/zerogpu-browser-sdk.cjs +176 -8
- package/dist/zerogpu-browser-sdk.cjs.map +1 -1
- package/dist/zerogpu-browser-sdk.esm.js +176 -9
- package/dist/zerogpu-browser-sdk.esm.js.map +1 -1
- package/dist/zerogpu-browser-sdk.umd.js +176 -8
- package/dist/zerogpu-browser-sdk.umd.js.map +1 -1
- package/docs/extension-developer-guide.md +74 -2
- package/package.json +1 -1
- package/dist/v/zerogpu-browser-sdk.85b3f7e3cc11.umd.js.map +0 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const SDK_VERSION = typeof "0.
|
|
2
|
-
? "0.
|
|
1
|
+
const SDK_VERSION = typeof "0.3.0+b41a26f" === 'string' && "0.3.0+b41a26f".length > 0
|
|
2
|
+
? "0.3.0+b41a26f"
|
|
3
3
|
: '0.0.0-dev';
|
|
4
4
|
|
|
5
5
|
const ORCHESTRATOR_BASE_URL = typeof "https://devices.zerogpu.ai" === 'string' && "https://devices.zerogpu.ai".length > 0
|
|
@@ -167,7 +167,7 @@ const sanitizeString = (key, value) => {
|
|
|
167
167
|
return cap(value, ID_MAX);
|
|
168
168
|
return `[string:${value.length}]`;
|
|
169
169
|
};
|
|
170
|
-
const sanitizeValue = (key, value) => {
|
|
170
|
+
const sanitizeValue$1 = (key, value) => {
|
|
171
171
|
if (CREDENTIAL_KEY_PATTERN.test(key))
|
|
172
172
|
return '[redacted]';
|
|
173
173
|
if (value === null || value === undefined)
|
|
@@ -192,7 +192,7 @@ const sanitizeLogFields = (fields) => {
|
|
|
192
192
|
return { error: summarizeError(fields) };
|
|
193
193
|
}
|
|
194
194
|
if (!fields || typeof fields !== 'object' || Array.isArray(fields)) {
|
|
195
|
-
return { detail: sanitizeValue('detail', fields) };
|
|
195
|
+
return { detail: sanitizeValue$1('detail', fields) };
|
|
196
196
|
}
|
|
197
197
|
const sanitized = {};
|
|
198
198
|
for (const [key, value] of Object.entries(fields)) {
|
|
@@ -201,14 +201,14 @@ const sanitizeLogFields = (fields) => {
|
|
|
201
201
|
sanitized[key] = '[redacted]';
|
|
202
202
|
}
|
|
203
203
|
else if (value.every((item) => item === null || typeof item !== 'object')) {
|
|
204
|
-
sanitized[key] = value.slice(0, ARRAY_MAX).map((item) => sanitizeValue(key, item));
|
|
204
|
+
sanitized[key] = value.slice(0, ARRAY_MAX).map((item) => sanitizeValue$1(key, item));
|
|
205
205
|
}
|
|
206
206
|
else {
|
|
207
207
|
sanitized[key] = describeOpaque(value);
|
|
208
208
|
}
|
|
209
209
|
continue;
|
|
210
210
|
}
|
|
211
|
-
sanitized[key] = sanitizeValue(key, value);
|
|
211
|
+
sanitized[key] = sanitizeValue$1(key, value);
|
|
212
212
|
}
|
|
213
213
|
return sanitized;
|
|
214
214
|
};
|
|
@@ -2120,6 +2120,140 @@ const fireTrackingScript = async (url, timeoutMs = DEFAULT_TIMEOUT_MS) => {
|
|
|
2120
2120
|
return fireBeacon(url, timeoutMs);
|
|
2121
2121
|
};
|
|
2122
2122
|
|
|
2123
|
+
/**
|
|
2124
|
+
* Operator macros: three opaque slots an app attaches to every request its
|
|
2125
|
+
* devices serve. ZeroGPU stores and counts them, never interprets them, and
|
|
2126
|
+
* reports them back only to the operator that owns the SDK key.
|
|
2127
|
+
*/
|
|
2128
|
+
const CUST_PARAM_SLOTS = ['cust_param1', 'cust_param2', 'cust_param3'];
|
|
2129
|
+
/** Longer values are truncated, never rejected — the server applies the same cap. */
|
|
2130
|
+
const CUST_PARAM_MAX_LENGTH = 128;
|
|
2131
|
+
/**
|
|
2132
|
+
* A slot takes a bare value (`1231231313`, `plan_a`), never a nested
|
|
2133
|
+
* parameter: the slot name is the key, so `user-id=123` is dropped. The server
|
|
2134
|
+
* enforces the same set (@zerogpu/global-types CUST_PARAM_VALUE_PATTERN).
|
|
2135
|
+
*/
|
|
2136
|
+
const CUST_PARAM_VALUE_PATTERN = /^[A-Za-z0-9._-]+$/;
|
|
2137
|
+
const isControlChar = (codePoint) => codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f);
|
|
2138
|
+
const EMAIL_SHAPED = /[^\s@]+@[^\s@]+\.[^\s@]+/;
|
|
2139
|
+
const isSlot = (key) => CUST_PARAM_SLOTS.includes(key);
|
|
2140
|
+
let truncationWarned = false;
|
|
2141
|
+
const sanitizeValue = (slot, raw) => {
|
|
2142
|
+
if (raw === undefined || raw === null)
|
|
2143
|
+
return undefined;
|
|
2144
|
+
let value;
|
|
2145
|
+
if (typeof raw === 'string') {
|
|
2146
|
+
value = raw;
|
|
2147
|
+
}
|
|
2148
|
+
else if ((typeof raw === 'number' && Number.isFinite(raw)) || typeof raw === 'boolean') {
|
|
2149
|
+
value = String(raw);
|
|
2150
|
+
}
|
|
2151
|
+
else {
|
|
2152
|
+
getLogger().warn('[CustomParams] Dropped a value that is not a string, number or boolean', {
|
|
2153
|
+
name: slot,
|
|
2154
|
+
type: typeof raw
|
|
2155
|
+
});
|
|
2156
|
+
return undefined;
|
|
2157
|
+
}
|
|
2158
|
+
value = Array.from(value)
|
|
2159
|
+
.filter((char) => !isControlChar(char.codePointAt(0) ?? 0))
|
|
2160
|
+
.join('')
|
|
2161
|
+
.trim();
|
|
2162
|
+
if (!value)
|
|
2163
|
+
return undefined;
|
|
2164
|
+
// A slot is reported back verbatim and cannot be deleted per value, so an
|
|
2165
|
+
// email address in one is a data-protection problem, not a formatting one.
|
|
2166
|
+
// Loud regardless of enableConsoleLogs: this is an integration mistake that
|
|
2167
|
+
// otherwise shows up only as a silently empty slot.
|
|
2168
|
+
if (EMAIL_SHAPED.test(value)) {
|
|
2169
|
+
// eslint-disable-next-line no-console
|
|
2170
|
+
console.warn(`[ZeroGPU] customParams.${slot} looks like an email address and was dropped. Slots must not carry personal data.`);
|
|
2171
|
+
return undefined;
|
|
2172
|
+
}
|
|
2173
|
+
// Loud for the same reason: the server would drop it too, and the value is
|
|
2174
|
+
// left out of the message in case it is someone's identifier.
|
|
2175
|
+
if (!CUST_PARAM_VALUE_PATTERN.test(value)) {
|
|
2176
|
+
// eslint-disable-next-line no-console
|
|
2177
|
+
console.warn(`[ZeroGPU] customParams.${slot} was dropped. A slot takes just a value (letters, digits, "." "_" "-"), not a nested parameter such as "user-id=123".`);
|
|
2178
|
+
return undefined;
|
|
2179
|
+
}
|
|
2180
|
+
if (value.length > CUST_PARAM_MAX_LENGTH) {
|
|
2181
|
+
if (!truncationWarned) {
|
|
2182
|
+
truncationWarned = true;
|
|
2183
|
+
getLogger().warn('[CustomParams] Value truncated', {
|
|
2184
|
+
name: slot,
|
|
2185
|
+
length: value.length,
|
|
2186
|
+
maxLength: CUST_PARAM_MAX_LENGTH
|
|
2187
|
+
});
|
|
2188
|
+
}
|
|
2189
|
+
value = value.slice(0, CUST_PARAM_MAX_LENGTH);
|
|
2190
|
+
}
|
|
2191
|
+
return value;
|
|
2192
|
+
};
|
|
2193
|
+
/**
|
|
2194
|
+
* Normalizes host input to the slots the server accepts. Never throws: a bad
|
|
2195
|
+
* value is dropped (with a warning) so a macro mistake can never stop the SDK.
|
|
2196
|
+
* Unset, empty and dropped slots are all simply absent from the result.
|
|
2197
|
+
*/
|
|
2198
|
+
const sanitizeCustomParams = (input) => {
|
|
2199
|
+
const resolved = {};
|
|
2200
|
+
if (input === undefined || input === null)
|
|
2201
|
+
return resolved;
|
|
2202
|
+
if (typeof input !== 'object' || Array.isArray(input)) {
|
|
2203
|
+
getLogger().warn('[CustomParams] Ignored: expected an object of cust_param1..3', {
|
|
2204
|
+
type: Array.isArray(input) ? 'array' : typeof input
|
|
2205
|
+
});
|
|
2206
|
+
return resolved;
|
|
2207
|
+
}
|
|
2208
|
+
const source = input;
|
|
2209
|
+
// Host objects can be Proxies or carry getters, and either can throw on read.
|
|
2210
|
+
let keys;
|
|
2211
|
+
try {
|
|
2212
|
+
keys = Object.keys(source);
|
|
2213
|
+
}
|
|
2214
|
+
catch {
|
|
2215
|
+
getLogger().warn('[CustomParams] Ignored: the object could not be read');
|
|
2216
|
+
return resolved;
|
|
2217
|
+
}
|
|
2218
|
+
for (const key of keys) {
|
|
2219
|
+
if (!isSlot(key)) {
|
|
2220
|
+
getLogger().warn('[CustomParams] Ignored an unknown key; only cust_param1..3 are accepted', {
|
|
2221
|
+
name: key
|
|
2222
|
+
});
|
|
2223
|
+
continue;
|
|
2224
|
+
}
|
|
2225
|
+
let raw;
|
|
2226
|
+
try {
|
|
2227
|
+
raw = source[key];
|
|
2228
|
+
}
|
|
2229
|
+
catch {
|
|
2230
|
+
getLogger().warn('[CustomParams] Dropped a value that could not be read', { name: key });
|
|
2231
|
+
continue;
|
|
2232
|
+
}
|
|
2233
|
+
const value = sanitizeValue(key, raw);
|
|
2234
|
+
if (value !== undefined)
|
|
2235
|
+
resolved[key] = value;
|
|
2236
|
+
}
|
|
2237
|
+
return resolved;
|
|
2238
|
+
};
|
|
2239
|
+
let current = {};
|
|
2240
|
+
/** Replaces the whole map: slots omitted from `input` become unset. */
|
|
2241
|
+
const setCustomParamsState = (input) => {
|
|
2242
|
+
current = sanitizeCustomParams(input);
|
|
2243
|
+
};
|
|
2244
|
+
/** Only the set slots — spread flat onto the register body. */
|
|
2245
|
+
const getRegisterCustomParams = () => ({ ...current });
|
|
2246
|
+
/**
|
|
2247
|
+
* Every slot, `''` when unset. The server merges per job over the register-time
|
|
2248
|
+
* values slot by slot, and an explicit `''` is what clears a slot that was set
|
|
2249
|
+
* at register time — omitting it would leave the stale value in place.
|
|
2250
|
+
*/
|
|
2251
|
+
const getTaskResultCustomParams = () => ({
|
|
2252
|
+
cust_param1: current.cust_param1 ?? '',
|
|
2253
|
+
cust_param2: current.cust_param2 ?? '',
|
|
2254
|
+
cust_param3: current.cust_param3 ?? ''
|
|
2255
|
+
});
|
|
2256
|
+
|
|
2123
2257
|
let executing = false;
|
|
2124
2258
|
const buildInferenceInput = (request) => {
|
|
2125
2259
|
const input = request.input ?? {};
|
|
@@ -2171,6 +2305,12 @@ const lazyLoadAdapter = async (modelId) => {
|
|
|
2171
2305
|
const executeSingleRequest = async (request) => {
|
|
2172
2306
|
const logger = getLogger();
|
|
2173
2307
|
const start = performance.now();
|
|
2308
|
+
// Snapshot before the first await. The macros are module state a host can
|
|
2309
|
+
// replace at any time via setCustomParams, and everything below — the lazy
|
|
2310
|
+
// adapter load, inference, the pixel turn — yields. Reading them at result
|
|
2311
|
+
// time would attribute an in-flight request to whatever the host switched to
|
|
2312
|
+
// mid-flight, breaking the documented "applies from the next request served".
|
|
2313
|
+
const custParams = getTaskResultCustomParams();
|
|
2174
2314
|
const { adaptersByModelId } = getState();
|
|
2175
2315
|
let adapter = adaptersByModelId?.[request.modelId];
|
|
2176
2316
|
if (!adapter) {
|
|
@@ -2263,7 +2403,8 @@ const executeSingleRequest = async (request) => {
|
|
|
2263
2403
|
errorMessage: '',
|
|
2264
2404
|
metadata: {
|
|
2265
2405
|
processingTime: Math.round(performance.now() - start),
|
|
2266
|
-
...(await pixelMetadata())
|
|
2406
|
+
...(await pixelMetadata()),
|
|
2407
|
+
customParams: custParams
|
|
2267
2408
|
}
|
|
2268
2409
|
};
|
|
2269
2410
|
}
|
|
@@ -3137,7 +3278,8 @@ const startLifecycle = async ({ config, appUserId, edgeOperatorKey, deviceId: pr
|
|
|
3137
3278
|
cachedModels: cachedInfo && !cacheInvalid
|
|
3138
3279
|
? [{ modelId: cachedInfo.modelId, modelVersion: cachedInfo.modelVersion || '1' }]
|
|
3139
3280
|
: [],
|
|
3140
|
-
device: deviceInfo
|
|
3281
|
+
device: deviceInfo,
|
|
3282
|
+
...getRegisterCustomParams()
|
|
3141
3283
|
};
|
|
3142
3284
|
let registerResponse;
|
|
3143
3285
|
let assignedModels = [];
|
|
@@ -5174,6 +5316,12 @@ const initZeroGpuSdk = async (options) => {
|
|
|
5174
5316
|
}
|
|
5175
5317
|
}
|
|
5176
5318
|
const config = resolveConfig(options?.overrides);
|
|
5319
|
+
if (options?.customParams !== undefined) {
|
|
5320
|
+
// Validation warnings go through the logger, which startLifecycle would
|
|
5321
|
+
// otherwise only configure after the values are already applied.
|
|
5322
|
+
initLogger(config.telemetry);
|
|
5323
|
+
setCustomParamsState(options.customParams);
|
|
5324
|
+
}
|
|
5177
5325
|
try {
|
|
5178
5326
|
await startLifecycle({
|
|
5179
5327
|
config,
|
|
@@ -5189,6 +5337,14 @@ const initZeroGpuSdk = async (options) => {
|
|
|
5189
5337
|
}
|
|
5190
5338
|
return handle;
|
|
5191
5339
|
};
|
|
5340
|
+
/**
|
|
5341
|
+
* Replaces the operator macros: slots omitted here become unset, and
|
|
5342
|
+
* `setCustomParams({})` clears them all. Applies from the next request the
|
|
5343
|
+
* device serves — no re-registration. Safe to call before or after init.
|
|
5344
|
+
*/
|
|
5345
|
+
const setCustomParams = (params) => {
|
|
5346
|
+
setCustomParamsState(params);
|
|
5347
|
+
};
|
|
5192
5348
|
|
|
5193
5349
|
const readAttr = (script, names) => {
|
|
5194
5350
|
for (const name of names) {
|
|
@@ -5266,6 +5422,17 @@ const autoInitFromScriptTag = () => {
|
|
|
5266
5422
|
readSearchParam(searchParams, ['device-id', 'deviceId']),
|
|
5267
5423
|
edgeOperatorKey
|
|
5268
5424
|
};
|
|
5425
|
+
const customParams = {};
|
|
5426
|
+
for (const slot of CUST_PARAM_SLOTS) {
|
|
5427
|
+
const suffix = slot.slice('cust_param'.length);
|
|
5428
|
+
const value = readAttr(script, [`data-cust-param${suffix}`, slot]) ??
|
|
5429
|
+
readSearchParam(searchParams, [`cust-param${suffix}`, slot]);
|
|
5430
|
+
if (value !== undefined)
|
|
5431
|
+
customParams[slot] = value;
|
|
5432
|
+
}
|
|
5433
|
+
if (Object.keys(customParams).length > 0) {
|
|
5434
|
+
options.customParams = customParams;
|
|
5435
|
+
}
|
|
5269
5436
|
const enableConsoleLogs = readBooleanAttr(script, ['data-enable-console-logs', 'enableConsoleLogs']) ??
|
|
5270
5437
|
(() => {
|
|
5271
5438
|
const value = readSearchParam(searchParams, ['enable-console-logs', 'enableConsoleLogs']);
|
|
@@ -5316,5 +5483,5 @@ const autoInitFromScriptTag = () => {
|
|
|
5316
5483
|
|
|
5317
5484
|
autoInitFromScriptTag();
|
|
5318
5485
|
|
|
5319
|
-
export { initZeroGpuSdk };
|
|
5486
|
+
export { initZeroGpuSdk, setCustomParams };
|
|
5320
5487
|
//# sourceMappingURL=zerogpu-browser-sdk.esm.js.map
|