@heybox/hb-sdk 0.6.2 → 0.6.4-alpha.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 (30) hide show
  1. package/README.md +5 -3
  2. package/dist/cli-chunks/{context-DmxbC_iV.cjs → context-BCwksd8H.cjs} +1 -37
  3. package/dist/cli-chunks/{create-CXBnqr5r.cjs → create-BO8GHVi0.cjs} +1 -1
  4. package/dist/cli-chunks/{dev-OCij_ecL.cjs → dev-DmgKnEC2.cjs} +59 -133
  5. package/dist/cli-chunks/{doctor-DQIzL_3_.cjs → doctor-BlKB4f78.cjs} +1 -1
  6. package/dist/cli-chunks/{index-Ulhxo0YK.cjs → index-PxkpQ5of.cjs} +2 -2
  7. package/dist/cli-chunks/{index-CTyguEza.cjs → index-Q0G1SsEs.cjs} +13 -13
  8. package/dist/cli-chunks/{login-BWN2-2Gk.cjs → login-Cxdo4B6Z.cjs} +2 -2
  9. package/dist/cli-chunks/{remote-DxKtJ21x.cjs → remote-C1Rsb1dx.cjs} +5 -5
  10. package/dist/cli-chunks/{session-D7mPwyx4.cjs → session-CC3Oz3Xc.cjs} +1 -2
  11. package/dist/cli.cjs +1 -1
  12. package/dist/devtools/mock-host/index.html +54 -1
  13. package/dist/devtools/mock-host/main.js +196 -6
  14. package/dist/index.cjs.js +1 -1
  15. package/dist/index.esm.js +1 -1
  16. package/dist/miniapp-publish.cjs.js +0 -4
  17. package/dist/miniapp-publish.esm.js +1 -4
  18. package/dist/vite.cjs.js +2 -344
  19. package/dist/vite.esm.js +2 -344
  20. package/package.json +2 -2
  21. package/skill/SKILL.md +5 -3
  22. package/skill/references/api-root.md +6 -26
  23. package/skill/references/cli.md +7 -5
  24. package/skill/skill.json +4 -4
  25. package/types/miniapp-publish/index.d.ts +0 -1
  26. package/types/vite/index.d.ts +1 -7
  27. package/types/cli/auth/base-url.d.ts +0 -20
  28. package/types/cli/config.d.ts +0 -11
  29. package/types/miniapp-manifest/sdk-version-policy.d.ts +0 -9
  30. package/types/vite/sdk-version-gate.d.ts +0 -8
package/dist/vite.cjs.js CHANGED
@@ -2,19 +2,12 @@
2
2
 
3
3
  var node_fs = require('node:fs');
4
4
  var path = require('node:path');
5
- var require$$0 = require('path');
6
- var require$$1 = require('os');
7
- var promises = require('node:fs/promises');
8
5
 
9
6
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
10
7
  /** 构建时替换为当前发布包的实际版本。 */
11
8
  const HB_SDK_VERSION = typeof undefined === 'string'
12
9
  ? undefined
13
- : '0.6.2';
14
-
15
- function getDefaultExportFromCjs (x) {
16
- return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
17
- }
10
+ : '0.6.4-alpha.0';
18
11
 
19
12
  var re = {exports: {}};
20
13
 
@@ -11587,328 +11580,9 @@ function walk(node, visit) {
11587
11580
  walk(node.content, visit);
11588
11581
  }
11589
11582
 
11590
- var envPaths$1 = {exports: {}};
11591
-
11592
- var hasRequiredEnvPaths;
11593
-
11594
- function requireEnvPaths () {
11595
- if (hasRequiredEnvPaths) return envPaths$1.exports;
11596
- hasRequiredEnvPaths = 1;
11597
- const path = require$$0;
11598
- const os = require$$1;
11599
-
11600
- const homedir = os.homedir();
11601
- const tmpdir = os.tmpdir();
11602
- const {env} = process;
11603
-
11604
- const macos = name => {
11605
- const library = path.join(homedir, 'Library');
11606
-
11607
- return {
11608
- data: path.join(library, 'Application Support', name),
11609
- config: path.join(library, 'Preferences', name),
11610
- cache: path.join(library, 'Caches', name),
11611
- log: path.join(library, 'Logs', name),
11612
- temp: path.join(tmpdir, name)
11613
- };
11614
- };
11615
-
11616
- const windows = name => {
11617
- const appData = env.APPDATA || path.join(homedir, 'AppData', 'Roaming');
11618
- const localAppData = env.LOCALAPPDATA || path.join(homedir, 'AppData', 'Local');
11619
-
11620
- return {
11621
- // Data/config/cache/log are invented by me as Windows isn't opinionated about this
11622
- data: path.join(localAppData, name, 'Data'),
11623
- config: path.join(appData, name, 'Config'),
11624
- cache: path.join(localAppData, name, 'Cache'),
11625
- log: path.join(localAppData, name, 'Log'),
11626
- temp: path.join(tmpdir, name)
11627
- };
11628
- };
11629
-
11630
- // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
11631
- const linux = name => {
11632
- const username = path.basename(homedir);
11633
-
11634
- return {
11635
- data: path.join(env.XDG_DATA_HOME || path.join(homedir, '.local', 'share'), name),
11636
- config: path.join(env.XDG_CONFIG_HOME || path.join(homedir, '.config'), name),
11637
- cache: path.join(env.XDG_CACHE_HOME || path.join(homedir, '.cache'), name),
11638
- // https://wiki.debian.org/XDGBaseDirectorySpecification#state
11639
- log: path.join(env.XDG_STATE_HOME || path.join(homedir, '.local', 'state'), name),
11640
- temp: path.join(tmpdir, username, name)
11641
- };
11642
- };
11643
-
11644
- const envPaths = (name, options) => {
11645
- if (typeof name !== 'string') {
11646
- throw new TypeError(`Expected string, got ${typeof name}`);
11647
- }
11648
-
11649
- options = Object.assign({suffix: 'nodejs'}, options);
11650
-
11651
- if (options.suffix) {
11652
- // Add suffix to prevent possible conflict with native apps
11653
- name += `-${options.suffix}`;
11654
- }
11655
-
11656
- if (process.platform === 'darwin') {
11657
- return macos(name);
11658
- }
11659
-
11660
- if (process.platform === 'win32') {
11661
- return windows(name);
11662
- }
11663
-
11664
- return linux(name);
11665
- };
11666
-
11667
- envPaths$1.exports = envPaths;
11668
- // TODO: Remove this for the next major release
11669
- envPaths$1.exports.default = envPaths;
11670
- return envPaths$1.exports;
11671
- }
11672
-
11673
- var envPathsExports = requireEnvPaths();
11674
- var envPaths = /*@__PURE__*/getDefaultExportFromCjs(envPathsExports);
11675
-
11676
- const APIUserMiniprogramPublicSdkConfigPath = '/user_miniprogram/public/sdk_config';
11677
- function createUserMiniprogramPublicSdkConfigRequest(serviceTag) {
11678
- return {
11679
- path: APIUserMiniprogramPublicSdkConfigPath,
11680
- searchParams: serviceTag ? { special_tag: serviceTag } : {},
11681
- headers: serviceTag ? { 'x-rylai-service-tag': serviceTag } : {},
11682
- };
11683
- }
11684
- function parseUserMiniprogramPublicSdkConfigResult(value) {
11685
- if (!isRecord(value) ||
11686
- typeof value.minimum_sdk_version !== 'string') {
11687
- throw new Error('公共 SDK 配置响应无效');
11688
- }
11689
- return value;
11690
- }
11691
- function parseUserMiniprogramPublicSdkConfigEnvelope(value) {
11692
- if (!isRecord(value) || value.status !== 'ok') {
11693
- throw new Error('公共 SDK 配置响应无效');
11694
- }
11695
- return parseUserMiniprogramPublicSdkConfigResult(value.result);
11696
- }
11697
- function isRecord(value) {
11698
- return typeof value === 'object' && value !== null;
11699
- }
11700
-
11701
- const PUBLIC_USER_MINIPROGRAM_SDK_CONFIG_API_PATH = APIUserMiniprogramPublicSdkConfigPath;
11702
-
11703
- const CACHE_MAX_AGE_MS = 24 * 60 * 60 * 1_000;
11704
- const CACHE_FILE_NAME = 'minimum-sdk-versions.json';
11705
- async function resolveMinimumSdkVersion(options) {
11706
- const now = options.now ?? Date.now();
11707
- const cachePath = path.join(options.cacheDir, CACHE_FILE_NAME);
11708
- const cache = await readCache(cachePath);
11709
- let minimumSdkVersion;
11710
- try {
11711
- minimumSdkVersion = validateStableSdkVersion(await options.fetchMinimumSdkVersion(), 'minimumSdkVersion');
11712
- }
11713
- catch {
11714
- const cached = cache.environments[options.environment];
11715
- const cacheAge = cached ? now - cached.fetchedAt : Number.NaN;
11716
- if (cached && Number.isFinite(cached.fetchedAt) && cacheAge >= 0 && cacheAge <= CACHE_MAX_AGE_MS) {
11717
- try {
11718
- return validateStableSdkVersion(cached.minimumSdkVersion, 'cached minimumSdkVersion');
11719
- }
11720
- catch {
11721
- // 非法缓存按不存在处理。
11722
- }
11723
- }
11724
- throw new Error('MINIMUM_SDK_VERSION_UNAVAILABLE:无法获取最低 SDK 版本,且没有 24 小时内的有效缓存');
11725
- }
11726
- cache.environments[options.environment] = { fetchedAt: now, minimumSdkVersion };
11727
- try {
11728
- await writeCache(cachePath, cache);
11729
- }
11730
- catch {
11731
- // 已校验的远端策略仍然可信;缓存只是离线回退优化。
11732
- }
11733
- return minimumSdkVersion;
11734
- }
11735
- function validateCurrentSdkVersion(currentSdkVersion, minimumSdkVersion, options) {
11736
- const current = validateSdkVersion(currentSdkVersion, '当前 SDK 版本');
11737
- const minimum = validateStableSdkVersion(minimumSdkVersion, 'minimumSdkVersion');
11738
- if (!options.allowPrerelease && semverExports.prerelease(current) !== null) {
11739
- throw new Error('正式发布不允许使用预发布 SDK,请运行 pnpm add @heybox/hb-sdk@latest 后重新构建');
11740
- }
11741
- if (!semverExports.satisfies(current, `>=${minimum}`, { includePrerelease: options.allowPrerelease })) {
11742
- throw new Error(`SDK_VERSION_TOO_LOW:当前 @heybox/hb-sdk@${current} 低于最低版本 ${minimum},请运行 pnpm add @heybox/hb-sdk@latest`);
11743
- }
11744
- return current;
11745
- }
11746
- function validateSdkVersion(value, label) {
11747
- const normalized = String(value).trim();
11748
- const parsed = semverExports.parse(normalized);
11749
- if (!parsed || parsed.version !== normalized || parsed.build.length > 0) {
11750
- throw new Error(`${label} 必须是合法 SemVer:${normalized}`);
11751
- }
11752
- return normalized;
11753
- }
11754
- function validateStableSdkVersion(value, label) {
11755
- const normalized = validateSdkVersion(value, label);
11756
- if (semverExports.prerelease(normalized) !== null)
11757
- throw new Error(`${label} 必须是稳定 SemVer:${normalized}`);
11758
- return normalized;
11759
- }
11760
- async function readCache(cachePath) {
11761
- try {
11762
- const parsed = JSON.parse(await promises.readFile(cachePath, 'utf8'));
11763
- if (parsed && typeof parsed === 'object' && parsed.environments && typeof parsed.environments === 'object')
11764
- return parsed;
11765
- }
11766
- catch {
11767
- // 缓存缺失或损坏时继续请求最新配置。
11768
- }
11769
- return { environments: {} };
11770
- }
11771
- async function writeCache(cachePath, cache) {
11772
- await promises.mkdir(path.dirname(cachePath), { recursive: true });
11773
- const temporaryPath = `${cachePath}.${process.pid}.tmp`;
11774
- await promises.writeFile(temporaryPath, `${JSON.stringify(cache, null, 2)}\n`, { mode: 0o600 });
11775
- await promises.rename(temporaryPath, cachePath);
11776
- }
11777
-
11778
- const globalHeyboxCliRequestConfig = {};
11779
- function readHeyboxCliRequestConfig() {
11780
- return normalizeHeyboxRylaiServiceTagConfig(globalHeyboxCliRequestConfig);
11781
- }
11782
- function resolveHeyboxRylaiServiceTag(pathWithQuery, config = readHeyboxCliRequestConfig()) {
11783
- const normalized = normalizeHeyboxRylaiServiceTagConfig(config);
11784
- const pathname = getPathname(pathWithQuery) ;
11785
- const specialTag = normalized.special_tag;
11786
- if (pathname && specialTag?.path_prefix_list.some((prefix) => pathname.startsWith(prefix))) {
11787
- return specialTag.tag_name;
11788
- }
11789
- return normalized.default_tag;
11790
- }
11791
- function normalizeHeyboxRylaiServiceTagConfig(config) {
11792
- const defaultTag = normalizeHeyboxRylaiServiceTag(config.default_tag);
11793
- const specialTagName = normalizeHeyboxRylaiServiceTag(config.special_tag?.tag_name);
11794
- const pathPrefixList = config.special_tag?.path_prefix_list?.filter(isValidPathPrefix) || [];
11795
- return {
11796
- ...(defaultTag ? { default_tag: defaultTag } : {}),
11797
- ...(specialTagName && pathPrefixList.length
11798
- ? {
11799
- special_tag: {
11800
- tag_name: specialTagName,
11801
- path_prefix_list: pathPrefixList,
11802
- },
11803
- }
11804
- : {}),
11805
- };
11806
- }
11807
- function normalizeHeyboxRylaiServiceTag(value) {
11808
- const tag = String(value ?? '').trim();
11809
- if (!tag) {
11810
- return undefined;
11811
- }
11812
- if (/[\r\n]/.test(tag)) {
11813
- throw new Error('x-rylai-service-tag 不允许包含换行符');
11814
- }
11815
- return tag;
11816
- }
11817
- function isValidPathPrefix(value) {
11818
- return typeof value === 'string' && value.startsWith('/') && !/[\r\n]/.test(value);
11819
- }
11820
- function getPathname(pathWithQuery) {
11821
- try {
11822
- return new URL(pathWithQuery, 'https://api.xiaoheihe.cn').pathname;
11823
- }
11824
- catch {
11825
- return '';
11826
- }
11827
- }
11828
-
11829
- const paths = envPaths('hb-sdk');
11830
- async function enforceViteSdkVersion(options) {
11831
- const fetchImpl = options.fetchImpl ?? fetch;
11832
- const serviceTag = resolveHeyboxRylaiServiceTag(PUBLIC_USER_MINIPROGRAM_SDK_CONFIG_API_PATH, {
11833
- default_tag: options.serviceTag,
11834
- });
11835
- const minimumSdkVersion = await resolveMinimumSdkVersion({
11836
- cacheDir: options.cacheDir ?? paths.cache,
11837
- environment: options.environment,
11838
- fetchMinimumSdkVersion: async () => {
11839
- const request = createUserMiniprogramPublicSdkConfigRequest(serviceTag);
11840
- const url = new URL(request.path, options.environment);
11841
- for (const [key, value] of Object.entries(request.searchParams)) {
11842
- url.searchParams.set(key, value);
11843
- }
11844
- const response = await fetchImpl(url, {
11845
- headers: request.headers,
11846
- });
11847
- if (!response.ok) {
11848
- throw new Error('公共 SDK 配置响应无效');
11849
- }
11850
- return parseUserMiniprogramPublicSdkConfigEnvelope(await response.json())
11851
- .minimum_sdk_version;
11852
- },
11853
- });
11854
- return validateCurrentSdkVersion(options.sdkVersion, minimumSdkVersion, { allowPrerelease: options.allowPrerelease });
11855
- }
11856
-
11857
- const HEYBOX_API_BASE_URL = 'https://api.xiaoheihe.cn';
11858
- const TRUSTED_HEYBOX_API_HOST_SUFFIXES = ['.xiaoheihe.cn', '.debugmode.cn'];
11859
- function resolveHeyboxApiBaseUrl(options = {}) {
11860
- const env = options.env;
11861
- const baseUrl = normalizeHeyboxOriginUrl(options.apiBaseUrl ?? env?.HB_SDK_API_BASE_URL ?? HEYBOX_API_BASE_URL, 'Heybox API baseUrl');
11862
- assertSafeHeyboxApiBaseUrl(baseUrl, {
11863
- allowUnsafeApiBaseUrl: options.allowUnsafeApiBaseUrl === true || isTruthyEnvFlag(env?.HB_SDK_ALLOW_UNSAFE_API_BASE_URL),
11864
- });
11865
- return baseUrl;
11866
- }
11867
- function normalizeHeyboxOriginUrl(value, label) {
11868
- const raw = String(value ?? '').trim();
11869
- if (!raw) {
11870
- throw new Error(`${label} 不能为空`);
11871
- }
11872
- let url;
11873
- try {
11874
- url = new URL(raw);
11875
- }
11876
- catch {
11877
- throw new Error(`${label} 必须是合法的 http(s) origin:${raw}`);
11878
- }
11879
- if (url.protocol !== 'http:' && url.protocol !== 'https:') {
11880
- throw new Error(`${label} 只支持 http(s) 协议:${raw}`);
11881
- }
11882
- if (url.username || url.password) {
11883
- throw new Error(`${label} 不允许包含用户名或密码:${raw}`);
11884
- }
11885
- const rawSuffix = raw.match(/^[a-z][a-z\d+.-]*:\/\/[^/?#]+(.*)$/i)?.[1] ?? '';
11886
- if ((rawSuffix && rawSuffix !== '/') || url.search || url.hash) {
11887
- throw new Error(`${label} 只支持 origin,不允许包含 path、query 或 hash:${raw}`);
11888
- }
11889
- return url.origin;
11890
- }
11891
- function assertSafeHeyboxApiBaseUrl(origin, options) {
11892
- if (options.allowUnsafeApiBaseUrl) {
11893
- return;
11894
- }
11895
- const url = new URL(origin);
11896
- if (url.protocol !== 'https:' || !isTrustedHeyboxApiHost(url.hostname)) {
11897
- throw new Error('Heybox API baseUrl 默认只允许 Heybox 受信 HTTPS origin;本地调试请显式使用 --allow-unsafe-api-base-url 或 HB_SDK_ALLOW_UNSAFE_API_BASE_URL=1');
11898
- }
11899
- }
11900
- function isTrustedHeyboxApiHost(hostname) {
11901
- return TRUSTED_HEYBOX_API_HOST_SUFFIXES.some((suffix) => hostname === suffix.slice(1) || hostname.endsWith(suffix));
11902
- }
11903
- function isTruthyEnvFlag(value) {
11904
- return ['1', 'true', 'yes', 'on'].includes(String(value ?? '')
11905
- .trim()
11906
- .toLowerCase());
11907
- }
11908
-
11909
11583
  const sdkVersionPlaceholder = ['__HB_SDK', 'VERSION__'].join('_');
11910
11584
  const sdkVersionBuildConstant = ['__HB_SDK_BUILD', 'VERSION__'].join('_');
11911
- function miniappManifest(options = {}) {
11585
+ function miniappManifest() {
11912
11586
  let root = process.cwd();
11913
11587
  let outDir = 'dist';
11914
11588
  let command = 'build';
@@ -11932,22 +11606,6 @@ function miniappManifest(options = {}) {
11932
11606
  command = resolved.command;
11933
11607
  hmrWebSocketUrl = resolveHmrWebSocketUrl(resolved);
11934
11608
  },
11935
- async buildStart() {
11936
- if (command !== 'build' || process.env.HB_SDK_DEPLOY_BUILD === '1')
11937
- return;
11938
- try {
11939
- await (options.enforceSdkVersion ?? enforceViteSdkVersion)({
11940
- sdkVersion: resolveSdkVersion(),
11941
- environment: resolveHeyboxApiBaseUrl({ env: process.env }),
11942
- serviceTag: process.env.HB_SDK_SERVICE_TAG,
11943
- allowPrerelease: true,
11944
- });
11945
- }
11946
- catch (error) {
11947
- priorBuildError = toError(error);
11948
- throw priorBuildError;
11949
- }
11950
- },
11951
11609
  buildEnd(error) {
11952
11610
  if (error) {
11953
11611
  priorBuildError = priorBuildError ?? toError(error);