import { core, primordials } from "ext:core/mod.js";
import { op_bootstrap_user_agent } from "ext:core/ops";
const {
ArrayPrototypeMap,
ObjectFreeze,
ObjectPrototypeIsPrototypeOf,
PromiseReject,
PromiseResolve,
SafeArrayIterator,
StringPrototypeIndexOf,
StringPrototypeSlice,
StringPrototypeStartsWith,
SymbolFor,
} = primordials;
const console = core.loadExtScript("ext:deno_web/01_console.js");
const webidl = core.loadExtScript("ext:deno_webidl/00_webidl.js");
function getPlatform(os) {
switch (os) {
case "darwin":
return "macOS";
case "windows":
return "Windows";
case "linux":
return "Linux";
case "android":
return "Android";
case "freebsd":
return "FreeBSD";
case "openbsd":
return "OpenBSD";
case "netbsd":
return "NetBSD";
case "solaris":
case "illumos":
return "SunOS";
case "aix":
return "AIX";
default:
return "Unknown";
}
}
function getArchitecture(arch) {
switch (arch) {
case "x86_64":
return "x86";
case "aarch64":
return "arm";
default:
return arch;
}
}
let fullVersion_ = null;
function fullVersion() {
if (fullVersion_ === null) {
const ua = op_bootstrap_user_agent();
fullVersion_ = StringPrototypeStartsWith(ua, "Deno/")
? StringPrototypeSlice(ua, "Deno/".length)
: ua;
}
return fullVersion_;
}
function majorVersion() {
const version = fullVersion();
const dot = StringPrototypeIndexOf(version, ".");
return dot === -1 ? version : StringPrototypeSlice(version, 0, dot);
}
function lowEntropyBrands() {
return [{ brand: "Deno", version: majorVersion() }];
}
function fullVersionList() {
return [{ brand: "Deno", version: fullVersion() }];
}
const highEntropyValues = {
architecture: () => getArchitecture(core.build.arch),
bitness: () => "64",
brands: () => lowEntropyBrands(),
formFactors: () => ["Desktop"],
fullVersionList: () => fullVersionList(),
mobile: () => false,
model: () => "",
platform: () => getPlatform(core.build.os),
platformVersion: () => "",
uaFullVersion: () => fullVersion(),
wow64: () => false,
};
class NavigatorUAData {
constructor() {
webidl.illegalConstructor();
}
get brands() {
webidl.assertBranded(this, NavigatorUADataPrototype);
return ArrayPrototypeMap(
lowEntropyBrands(),
(b) => ObjectFreeze({ brand: b.brand, version: b.version }),
);
}
get mobile() {
webidl.assertBranded(this, NavigatorUADataPrototype);
return false;
}
get platform() {
webidl.assertBranded(this, NavigatorUADataPrototype);
return getPlatform(core.build.os);
}
getHighEntropyValues(hints) {
try {
webidl.assertBranded(this, NavigatorUADataPrototype);
const prefix =
"Failed to execute 'getHighEntropyValues' on 'NavigatorUAData'";
webidl.requiredArguments(arguments.length, 1, prefix);
hints = webidl.converters["sequence<DOMString>"](
hints,
prefix,
"Argument 1",
);
const result = {
brands: this.brands,
mobile: false,
platform: getPlatform(core.build.os),
};
for (const hint of new SafeArrayIterator(hints)) {
const getter = highEntropyValues[hint];
if (getter !== undefined) {
result[hint] = getter();
}
}
return PromiseResolve(result);
} catch (err) {
return PromiseReject(err);
}
}
toJSON() {
webidl.assertBranded(this, NavigatorUADataPrototype);
return {
brands: this.brands,
mobile: false,
platform: getPlatform(core.build.os),
};
}
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
return inspect(
console.createFilteredInspectProxy({
object: this,
evaluate: ObjectPrototypeIsPrototypeOf(NavigatorUADataPrototype, this),
keys: [
"brands",
"mobile",
"platform",
],
}),
inspectOptions,
);
}
}
const NavigatorUADataPrototype = NavigatorUAData.prototype;
const navigatorUAData = webidl.createBranded(NavigatorUAData);
export { NavigatorUAData, navigatorUAData };