@yeying-community/web3-bs 1.0.6 → 1.0.8

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.
@@ -1137,6 +1137,16 @@
1137
1137
  function resolveSessionTokenKey(options) {
1138
1138
  return options?.sessionTokenStorageKey || DEFAULT_SESSION_TOKEN_KEY;
1139
1139
  }
1140
+ function resolveAccessToken(options) {
1141
+ if (typeof options?.accessToken === 'string') {
1142
+ const token = options.accessToken.trim();
1143
+ return token || null;
1144
+ }
1145
+ if (options?.accessToken === null) {
1146
+ return null;
1147
+ }
1148
+ return getAccessToken(options);
1149
+ }
1140
1150
  function shouldStoreSessionToken(options) {
1141
1151
  return options?.storeSessionToken !== false;
1142
1152
  }
@@ -1184,6 +1194,9 @@
1184
1194
  }
1185
1195
  return undefined;
1186
1196
  }
1197
+ function parseIssuerDidField(obj) {
1198
+ return parseStringField(obj, ['issuerDid', 'issuer', 'did']);
1199
+ }
1187
1200
  function readStoredSessionToken(options) {
1188
1201
  if (!shouldStoreSessionToken(options))
1189
1202
  return null;
@@ -1240,11 +1253,16 @@
1240
1253
  const fetcher = resolveFetcher(options);
1241
1254
  const credentials = resolveCredentials(options);
1242
1255
  const url = joinUrl$1(resolveBaseUrl(options), options.issuerPath || DEFAULT_ISSUER_PATH);
1256
+ const token = resolveAccessToken(options);
1257
+ const headers = new Headers({
1258
+ accept: 'application/json',
1259
+ });
1260
+ if (token) {
1261
+ headers.set('Authorization', `Bearer ${token}`);
1262
+ }
1243
1263
  const response = await fetcher(url, {
1244
1264
  method: 'GET',
1245
- headers: {
1246
- accept: 'application/json',
1247
- },
1265
+ headers,
1248
1266
  credentials,
1249
1267
  });
1250
1268
  const payload = await parseJsonBody(response);
@@ -1254,7 +1272,7 @@
1254
1272
  const data = parseEnvelopeData(payload);
1255
1273
  return {
1256
1274
  enabled: typeof data.enabled === 'boolean' ? data.enabled : undefined,
1257
- issuerDid: parseStringField(data, ['issuerDid']),
1275
+ issuerDid: parseIssuerDidField(data),
1258
1276
  defaultAudience: parseStringField(data, ['defaultAudience']),
1259
1277
  defaultCapabilities: parseCapabilitiesField(data, ['defaultCapabilities']),
1260
1278
  response: payload,
@@ -1267,13 +1285,18 @@
1267
1285
  }
1268
1286
  const fetcher = resolveFetcher(options);
1269
1287
  const credentials = resolveCredentials(options);
1288
+ const accessToken = resolveAccessToken(options);
1270
1289
  const url = joinUrl$1(resolveBaseUrl(options), options.sessionPath || DEFAULT_SESSION_PATH);
1290
+ const headers = new Headers({
1291
+ 'Content-Type': 'application/json',
1292
+ accept: 'application/json',
1293
+ });
1294
+ if (accessToken) {
1295
+ headers.set('Authorization', `Bearer ${accessToken}`);
1296
+ }
1271
1297
  const response = await fetcher(url, {
1272
1298
  method: 'POST',
1273
- headers: {
1274
- 'Content-Type': 'application/json',
1275
- accept: 'application/json',
1276
- },
1299
+ headers,
1277
1300
  credentials,
1278
1301
  body: JSON.stringify({
1279
1302
  subject,
@@ -1294,7 +1317,7 @@
1294
1317
  subject: parseStringField(data, ['subject']) || subject,
1295
1318
  sessionToken,
1296
1319
  expiresAt: parseNumberField(data, ['expiresAt']),
1297
- issuerDid: parseStringField(data, ['issuerDid']),
1320
+ issuerDid: parseIssuerDidField(data),
1298
1321
  response: payload,
1299
1322
  };
1300
1323
  }
@@ -1335,13 +1358,13 @@
1335
1358
  }
1336
1359
  return {
1337
1360
  ucan,
1338
- issuerDid: parseStringField(data, ['issuerDid']),
1361
+ issuerDid: parseIssuerDidField(data),
1339
1362
  subject: parseStringField(data, ['subject']),
1340
1363
  audience: parseStringField(data, ['audience']),
1341
1364
  capabilities: parseCapabilitiesField(data, ['capabilities']),
1342
- exp: parseNumberField(data, ['exp']),
1343
- nbf: parseNumberField(data, ['nbf']),
1344
- iat: parseNumberField(data, ['iat']),
1365
+ exp: parseNumberField(data, ['exp', 'expiresAt']),
1366
+ nbf: parseNumberField(data, ['nbf', 'notBefore']),
1367
+ iat: parseNumberField(data, ['iat', 'issuedAt']),
1345
1368
  response: payload,
1346
1369
  };
1347
1370
  }
@@ -1418,6 +1441,9 @@
1418
1441
  const suffix = path.startsWith('/') ? path : `/${path}`;
1419
1442
  return `${base}${suffix}`;
1420
1443
  }
1444
+ function isRecord(value) {
1445
+ return typeof value === 'object' && value !== null;
1446
+ }
1421
1447
  function resolveAuthHeader(auth, token) {
1422
1448
  if (auth?.type === 'bearer') {
1423
1449
  return `Bearer ${auth.token}`;
@@ -1606,6 +1632,72 @@
1606
1632
  }
1607
1633
  return await res.json();
1608
1634
  }
1635
+ async requestApiJson(method, apiPath, body, options) {
1636
+ const headers = this.buildHeaders({
1637
+ auth: options?.auth,
1638
+ token: options?.token,
1639
+ contentType: body === undefined ? undefined : 'application/json',
1640
+ });
1641
+ const response = await this.fetcher(joinUrl(this.baseUrl, apiPath), {
1642
+ method,
1643
+ headers,
1644
+ body: body === undefined ? undefined : JSON.stringify(body),
1645
+ credentials: this.credentials,
1646
+ signal: options?.signal,
1647
+ });
1648
+ if (!response.ok) {
1649
+ throw new Error(`WebDAV ${method} ${apiPath} failed: ${response.status} ${response.statusText}`);
1650
+ }
1651
+ return await response.json();
1652
+ }
1653
+ getShareAccessUrl(token, fileName) {
1654
+ const normalizedToken = encodeURIComponent(String(token || '').trim());
1655
+ if (!normalizedToken) {
1656
+ throw new Error('Share token is required');
1657
+ }
1658
+ const encodedFileName = String(fileName || '').trim()
1659
+ ? `/${encodeURIComponent(String(fileName || '').trim())}`
1660
+ : '';
1661
+ return joinUrl(this.baseUrl, `/api/v1/public/share/${normalizedToken}${encodedFileName}`);
1662
+ }
1663
+ async createShareLink(options) {
1664
+ const normalizedPath = String(options.path || '').trim();
1665
+ if (!normalizedPath) {
1666
+ throw new Error('Share path is required');
1667
+ }
1668
+ const payload = await this.requestApiJson('POST', '/api/v1/public/share/create', {
1669
+ path: normalizedPath,
1670
+ expiresIn: options.expiresIn,
1671
+ expiresValue: options.expiresValue,
1672
+ expiresUnit: options.expiresUnit,
1673
+ }, options);
1674
+ if (!isRecord(payload)) {
1675
+ throw new Error('WebDAV share create response is invalid');
1676
+ }
1677
+ return payload;
1678
+ }
1679
+ async listShareLinks(options = {}) {
1680
+ const payload = await this.requestApiJson('GET', '/api/v1/public/share/list', undefined, options);
1681
+ if (!isRecord(payload)) {
1682
+ return [];
1683
+ }
1684
+ const items = payload.items;
1685
+ if (!Array.isArray(items)) {
1686
+ return [];
1687
+ }
1688
+ return items.filter(isRecord);
1689
+ }
1690
+ async revokeShareLink(token, options = {}) {
1691
+ const normalizedToken = String(token || '').trim();
1692
+ if (!normalizedToken) {
1693
+ throw new Error('Share token is required');
1694
+ }
1695
+ const payload = await this.requestApiJson('POST', '/api/v1/public/share/revoke', { token: normalizedToken }, options);
1696
+ if (!isRecord(payload)) {
1697
+ return {};
1698
+ }
1699
+ return payload;
1700
+ }
1609
1701
  }
1610
1702
  function createWebDavClient(options) {
1611
1703
  return new WebDavClient(options);
@@ -1614,6 +1706,13 @@
1614
1706
  const tokenCache = new Map();
1615
1707
  const TOKEN_SKEW_MS = 5000;
1616
1708
  const DEFAULT_APP_ACTION = 'write';
1709
+ const LOOPBACK_HOST_ALIASES = new Set([
1710
+ 'localhost',
1711
+ '127.0.0.1',
1712
+ '::1',
1713
+ '0:0:0:0:0:0:0:1',
1714
+ '0.0.0.0',
1715
+ ]);
1617
1716
  function normalizeAppDir(path) {
1618
1717
  const trimmed = path.trim();
1619
1718
  if (!trimmed)
@@ -1625,6 +1724,69 @@
1625
1724
  function sanitizeAppId(appId) {
1626
1725
  return appId.trim().replace(/[^a-zA-Z0-9._-]/g, '-');
1627
1726
  }
1727
+ function parseHostPort(rawHost) {
1728
+ const host = rawHost.trim();
1729
+ if (!host)
1730
+ return { hostname: '', port: '' };
1731
+ const bracketMatch = host.match(/^\[([^\]]+)\](?::([0-9]+))?$/);
1732
+ if (bracketMatch) {
1733
+ return {
1734
+ hostname: bracketMatch[1] || '',
1735
+ port: bracketMatch[2] || '',
1736
+ };
1737
+ }
1738
+ const firstColon = host.indexOf(':');
1739
+ const lastColon = host.lastIndexOf(':');
1740
+ if (firstColon > -1 && firstColon === lastColon) {
1741
+ const hostname = host.slice(0, firstColon).trim();
1742
+ const port = host.slice(firstColon + 1).trim();
1743
+ if (/^[0-9]+$/.test(port)) {
1744
+ return { hostname, port };
1745
+ }
1746
+ }
1747
+ return { hostname: host, port: '' };
1748
+ }
1749
+ function normalizeAppHostnameForAppId(hostname) {
1750
+ const normalized = (hostname || '').trim().toLowerCase();
1751
+ if (!normalized)
1752
+ return '';
1753
+ const bare = normalized.replace(/^\[(.*)\]$/, '$1');
1754
+ if (LOOPBACK_HOST_ALIASES.has(normalized) || LOOPBACK_HOST_ALIASES.has(bare)) {
1755
+ return 'localhost';
1756
+ }
1757
+ return bare;
1758
+ }
1759
+ function buildSanitizedAppId(hostname, port) {
1760
+ const normalizedHostname = normalizeAppHostnameForAppId(hostname);
1761
+ if (!normalizedHostname)
1762
+ return '';
1763
+ const normalizedPort = port === undefined || port === null ? '' : String(port).trim();
1764
+ const host = normalizedPort
1765
+ ? `${normalizedHostname}:${normalizedPort}`
1766
+ : normalizedHostname;
1767
+ return sanitizeAppId(host);
1768
+ }
1769
+ function deriveAppIdFromHost(host) {
1770
+ const parsed = parseHostPort(host || '');
1771
+ return buildSanitizedAppId(parsed.hostname, parsed.port);
1772
+ }
1773
+ function deriveAppIdFromLocation(locationLike) {
1774
+ const source = locationLike ||
1775
+ (typeof window !== 'undefined' ? window.location : undefined);
1776
+ if (!source)
1777
+ return '';
1778
+ const hostname = typeof source.hostname === 'string' ? source.hostname : '';
1779
+ const port = source.port;
1780
+ if (hostname) {
1781
+ const appId = buildSanitizedAppId(hostname, port);
1782
+ if (appId)
1783
+ return appId;
1784
+ }
1785
+ if (typeof source.host === 'string') {
1786
+ return deriveAppIdFromHost(source.host);
1787
+ }
1788
+ return '';
1789
+ }
1628
1790
  function normalizeAction(action) {
1629
1791
  const trimmed = (action || '').trim();
1630
1792
  return trimmed ? trimmed : null;
@@ -1862,6 +2024,8 @@
1862
2024
  exports.createRootUcan = createRootUcan;
1863
2025
  exports.createUcanSession = createUcanSession;
1864
2026
  exports.createWebDavClient = createWebDavClient;
2027
+ exports.deriveAppIdFromHost = deriveAppIdFromHost;
2028
+ exports.deriveAppIdFromLocation = deriveAppIdFromLocation;
1865
2029
  exports.getAccessToken = getAccessToken;
1866
2030
  exports.getAccounts = getAccounts;
1867
2031
  exports.getBalance = getBalance;
@@ -1881,6 +2045,7 @@
1881
2045
  exports.issueCentralUcan = issueCentralUcan;
1882
2046
  exports.loginWithChallenge = loginWithChallenge;
1883
2047
  exports.logout = logout;
2048
+ exports.normalizeAppHostnameForAppId = normalizeAppHostnameForAppId;
1884
2049
  exports.normalizeUcanCapabilities = normalizeUcanCapabilities;
1885
2050
  exports.normalizeUcanCapability = normalizeUcanCapability;
1886
2051
  exports.onAccountsChanged = onAccountsChanged;