1
0

constants.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { purifyObj, randomId } from "@sv443-network/userutils";
  2. import { LogLevel } from "./types.js";
  3. type ConstTypes = {
  4. mode: "production" | "development";
  5. branch: "main" | "develop";
  6. host: "github" | "greasyfork" | "openuserjs";
  7. buildNumber: string;
  8. assetSource: "github" | "jsdelivr" | "local";
  9. devServerPort: number;
  10. };
  11. // these strings will have their values replaced by the post-build script:
  12. const rawConsts = {
  13. mode: "#{{MODE}}",
  14. branch: "#{{BRANCH}}",
  15. host: "#{{HOST}}",
  16. buildNumber: "#{{BUILD_NUMBER}}",
  17. assetSource: "#{{ASSET_SOURCE}}",
  18. devServerPort: "#{{DEV_SERVER_PORT}}",
  19. } as const satisfies Record<keyof ConstTypes, string>;
  20. const getConst = <TKey extends keyof typeof rawConsts, TDefault extends string | number>(constKey: TKey, defaultVal: TDefault) => {
  21. const val = rawConsts[constKey];
  22. return (val.match(/^#{{.+}}$/) ? defaultVal : val) as ConstTypes[TKey] | TDefault;
  23. };
  24. /** Path to the GitHub repo */
  25. export const repo = "Sv443/BetterYTM";
  26. /** The mode in which the script was built (production or development) */
  27. export const mode = getConst("mode", "production");
  28. /** The branch to use in various URLs that point to the GitHub repo */
  29. export const branch = getConst("branch", "main");
  30. /** Which host the userscript was installed from */
  31. export const host = getConst("host", "github");
  32. /** The build number of the userscript */
  33. export const buildNumber = getConst("buildNumber", "!BUILD_ERROR!");
  34. /** The source of the assets - github, jsdelivr or local */
  35. export const assetSource = getConst("assetSource", "jsdelivr");
  36. /** The port of the dev server */
  37. export const devServerPort = Number(getConst("devServerPort", 8710));
  38. /** URL to the changelog file */
  39. export const changelogUrl = `https://raw.githubusercontent.com/${repo}/${buildNumber??branch}/changelog.md`;
  40. /** The URL search parameters at the earliest possible time */
  41. export const initialParams = new URL(location.href).searchParams;
  42. /** Names of platforms by key of {@linkcode host} */
  43. export const platformNames = purifyObj({
  44. github: "GitHub",
  45. greasyfork: "GreasyFork",
  46. openuserjs: "OpenUserJS",
  47. } as const);
  48. /** Default compression format used throughout BYTM */
  49. export const compressionFormat: CompressionFormat = "deflate-raw";
  50. /** Whether sessionStorage is available and working */
  51. export const sessionStorageAvailable =
  52. typeof sessionStorage?.setItem === "function"
  53. && (() => {
  54. try {
  55. const key = `_bytm_test_${randomId(6, 36, false, true)}`;
  56. sessionStorage.setItem(key, "test");
  57. sessionStorage.removeItem(key);
  58. return true;
  59. }
  60. catch {
  61. return false;
  62. }
  63. })();
  64. /**
  65. * Fallback and initial value of how much info should be logged to the devtools console
  66. * 0 = Debug (show everything) or 1 = Info (show only important stuff)
  67. */
  68. export const defaultLogLevel: LogLevel = mode === "production" ? LogLevel.Info : LogLevel.Debug;
  69. /** Info about the userscript, parsed from the userscript header (tools/post-build.js) */
  70. export const scriptInfo = purifyObj({
  71. name: GM.info.script.name,
  72. version: GM.info.script.version,
  73. namespace: GM.info.script.namespace,
  74. } as const);