@bitrix24/b24jssdk 0.4.1 → 0.4.2

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/dist/umd/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @version @bitrix24/b24jssdk v0.4.1
2
+ * @version @bitrix24/b24jssdk v0.4.2
3
3
  * @copyright (c) 2025 Bitrix24
4
4
  * @licence MIT
5
5
  * @links https://github.com/bitrix24/b24jssdk - GitHub
@@ -8538,89 +8538,44 @@
8538
8538
  }
8539
8539
  }
8540
8540
 
8541
- const _state = {};
8542
- function getCrypto() {
8543
- if (typeof window !== "undefined" && window.crypto) {
8544
- return window.crypto;
8545
- }
8546
- if (typeof globalThis.crypto !== "undefined") {
8547
- return globalThis.crypto;
8548
- }
8549
- throw new Error("Crypto API not available");
8550
- }
8551
- function initRng() {
8552
- const crypto = getCrypto();
8553
- if (!crypto?.getRandomValues) {
8554
- throw new Error("Crypto API not available");
8555
- }
8556
- return {
8557
- rng: () => {
8558
- const rnds8 = new Uint8Array(16);
8559
- return crypto.getRandomValues(rnds8);
8560
- }
8561
- };
8562
- }
8563
8541
  const byteToHex = [];
8564
8542
  for (let i = 0; i < 256; ++i) {
8565
8543
  byteToHex.push((i + 256).toString(16).slice(1));
8566
8544
  }
8567
- function updateV7State(state, now, randoms) {
8568
- state.msecs ??= -Infinity;
8569
- state.seq ??= 0;
8570
- if (now > state.msecs) {
8571
- state.seq = randoms[6] << 23 | randoms[7] << 16 | randoms[8] << 8 | randoms[9];
8572
- state.msecs = now;
8573
- } else {
8574
- state.seq = Math.trunc(state.seq + 1);
8575
- if (state.seq === 0) {
8576
- state.msecs++;
8577
- }
8578
- }
8579
- return state;
8580
- }
8581
- function v7Bytes(randoms, msecs, seq, buf, offset = 0) {
8582
- if (!buf) {
8583
- buf = new Uint8Array(16);
8584
- offset = 0;
8585
- }
8586
- msecs ??= Date.now();
8587
- seq ??= randoms[6] * 127 << 24 | randoms[7] << 16 | randoms[8] << 8 | randoms[9];
8588
- buf[offset++] = msecs / 1099511627776 & 255;
8589
- buf[offset++] = msecs / 4294967296 & 255;
8590
- buf[offset++] = msecs / 16777216 & 255;
8591
- buf[offset++] = msecs / 65536 & 255;
8592
- buf[offset++] = msecs / 256 & 255;
8593
- buf[offset++] = msecs & 255;
8594
- buf[offset++] = 112 | seq >>> 28 & 15;
8595
- buf[offset++] = seq >>> 20 & 255;
8596
- buf[offset++] = 128 | seq >>> 14 & 63;
8597
- buf[offset++] = seq >>> 6 & 255;
8598
- buf[offset++] = seq << 2 & 255 | randoms[10] & 3;
8599
- buf[offset++] = randoms[11];
8600
- buf[offset++] = randoms[12];
8601
- buf[offset++] = randoms[13];
8602
- buf[offset++] = randoms[14];
8603
- buf[offset++] = randoms[15];
8604
- return buf;
8605
- }
8606
- function unsafeStringify(arr, offset = 0) {
8607
- return (byteToHex[arr[offset]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + "-" + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + "-" + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + "-" + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + "-" + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
8545
+ function sfc32(a, b, c, d) {
8546
+ return () => {
8547
+ a |= 0;
8548
+ b |= 0;
8549
+ c |= 0;
8550
+ d |= 0;
8551
+ const t = (a + b | 0) + d | 0;
8552
+ d = d + 1 | 0;
8553
+ a = b ^ b >>> 9;
8554
+ b = c + (c << 3) | 0;
8555
+ c = (c << 21 | c >>> 11) + t | 0;
8556
+ return t >>> 0;
8557
+ };
8608
8558
  }
8609
8559
  function uuidv7() {
8610
- const buf = void 0;
8611
- const offset = void 0;
8612
- const now = Date.now();
8613
- const { rng } = initRng();
8614
- const randoms = rng();
8615
- updateV7State(_state, now, randoms);
8616
- const bytes = v7Bytes(
8617
- randoms,
8618
- _state.msecs,
8619
- _state.seq,
8620
- buf,
8621
- offset
8622
- );
8623
- return unsafeStringify(bytes);
8560
+ const bytes = new Uint8Array(16);
8561
+ const timestamp = BigInt(Date.now());
8562
+ const perf = BigInt(Math.floor(performance.now() * 1e3) % 65535);
8563
+ const combinedTime = timestamp << 16n | perf;
8564
+ bytes[0] = Number(combinedTime >> 40n & 0xffn);
8565
+ bytes[1] = Number(combinedTime >> 32n & 0xffn);
8566
+ bytes[2] = Number(combinedTime >> 24n & 0xffn);
8567
+ bytes[3] = Number(combinedTime >> 16n & 0xffn);
8568
+ bytes[4] = Number(combinedTime >> 8n & 0xffn);
8569
+ bytes[5] = Number(combinedTime & 0xffn);
8570
+ const seed = (Math.random() * 4294967295 ^ Date.now() ^ performance.now()) >>> 0;
8571
+ const rand = sfc32(2654435769, 608135816, 3084996962, seed);
8572
+ const randView = new DataView(bytes.buffer);
8573
+ randView.setUint32(6, rand());
8574
+ randView.setUint32(10, rand());
8575
+ randView.setUint16(14, rand());
8576
+ bytes[6] = 112 | bytes[6] & 15;
8577
+ bytes[8] = 128 | bytes[8] & 63;
8578
+ return (byteToHex[bytes[0]] + byteToHex[bytes[1]] + byteToHex[bytes[2]] + byteToHex[bytes[3]] + "-" + byteToHex[bytes[4]] + byteToHex[bytes[5]] + "-" + byteToHex[bytes[6]] + byteToHex[bytes[7]] + "-" + byteToHex[bytes[8]] + byteToHex[bytes[9]] + "-" + byteToHex[bytes[10]] + byteToHex[bytes[11]] + byteToHex[bytes[12]] + byteToHex[bytes[13]] + byteToHex[bytes[14]] + byteToHex[bytes[15]]).toLowerCase();
8624
8579
  }
8625
8580
 
8626
8581
  const reEscape = /[&<>'"]/g;
@@ -14099,7 +14054,7 @@ ${this.stack}`;
14099
14054
  #clientSideWarningMessage = "";
14100
14055
  constructor(baseURL, authActions, options) {
14101
14056
  const defaultHeaders = {
14102
- // 'X-Sdk': 'b24-js-sdk-v-0.4.1'
14057
+ // 'X-Sdk': 'b24-js-sdk-v-0.4.2'
14103
14058
  };
14104
14059
  this.#clientAxios = axios.create({
14105
14060
  baseURL,
@@ -14527,7 +14482,7 @@ ${this.stack}`;
14527
14482
  const baseUrl = `${encodeURIComponent(method)}.json`;
14528
14483
  const queryParams = new URLSearchParams({
14529
14484
  [this.#requestIdGenerator.getQueryStringParameterName()]: this.#requestIdGenerator.getRequestId(),
14530
- [this.#requestIdGenerator.getQueryStringSdkParameterName()]: "0.4.1",
14485
+ [this.#requestIdGenerator.getQueryStringSdkParameterName()]: "0.4.2",
14531
14486
  [this.#requestIdGenerator.getQueryStringSdkTypeParameterName()]: "b24-js-sdk"
14532
14487
  });
14533
14488
  return `${baseUrl}?${queryParams.toString()}`;
@@ -16743,6 +16698,142 @@ ${this.stack}`;
16743
16698
  // endregion ////
16744
16699
  }
16745
16700
 
16701
+ class AuthOAuthManager {
16702
+ #authOptions;
16703
+ #oAuthSecret;
16704
+ #authExpires = 0;
16705
+ #domain;
16706
+ #b24Target;
16707
+ #b24TargetRest;
16708
+ // 'https://oauth.bitrix.info' ////
16709
+ #oAuthTarget;
16710
+ #isAdmin = null;
16711
+ constructor(b24OAuthParams, oAuthSecret) {
16712
+ this.#authOptions = Object.assign({}, b24OAuthParams);
16713
+ this.#oAuthSecret = Object.freeze(Object.assign({}, oAuthSecret));
16714
+ this.#domain = this.#authOptions.domain.replaceAll("https://", "").replaceAll("http://", "").replace(/:(80|443)$/, "");
16715
+ this.#b24TargetRest = this.#authOptions.clientEndpoint;
16716
+ this.#b24Target = this.#b24TargetRest.replace("/rest", "");
16717
+ this.#oAuthTarget = this.#authOptions.serverEndpoint.replace("/rest/", "");
16718
+ this.#authExpires = this.#authOptions.expires * 1e3;
16719
+ }
16720
+ /**
16721
+ * Returns authorization data
16722
+ * @see Http.#prepareParams
16723
+ */
16724
+ getAuthData() {
16725
+ return this.#authExpires > Date.now() ? {
16726
+ access_token: this.#authOptions.accessToken,
16727
+ refresh_token: this.#authOptions.refreshToken,
16728
+ expires_in: this.#authOptions.expiresIn,
16729
+ domain: this.#domain,
16730
+ member_id: this.#authOptions.memberId
16731
+ } : false;
16732
+ }
16733
+ /**
16734
+ * Updates authorization data
16735
+ */
16736
+ async refreshAuth() {
16737
+ try {
16738
+ const response = await fetch(`${this.#oAuthTarget}/oauth/token/`, {
16739
+ method: "POST",
16740
+ headers: {
16741
+ "Content-Type": "application/json"
16742
+ },
16743
+ body: JSON.stringify({
16744
+ params: {
16745
+ client_id: this.#oAuthSecret.clientId,
16746
+ grant_type: "refresh_token",
16747
+ client_secret: this.#oAuthSecret.clientSecret,
16748
+ refresh_token: this.#authOptions.refreshToken
16749
+ }
16750
+ })
16751
+ });
16752
+ if (!response.ok) {
16753
+ throw new Error(`Token update error: ${response.statusText}`);
16754
+ }
16755
+ const data = await response.json();
16756
+ if (data.error) {
16757
+ throw new Error(`Token update error: ${data.error}`);
16758
+ }
16759
+ this.#authOptions.accessToken = data.access_token;
16760
+ this.#authOptions.refreshToken = data.refresh_token;
16761
+ this.#authExpires = data.expires * 1e3;
16762
+ } catch (error) {
16763
+ throw new Error(`Token update error: ${error instanceof Error ? error.message : error}`);
16764
+ }
16765
+ return this.getAuthData();
16766
+ }
16767
+ getUniq(prefix) {
16768
+ return [prefix, this.#authOptions.memberId || ""].join("_");
16769
+ }
16770
+ /**
16771
+ * Get the account address BX24 ( https://name.bitrix24.com )
16772
+ */
16773
+ getTargetOrigin() {
16774
+ return `${this.#b24Target}`;
16775
+ }
16776
+ /**
16777
+ * Get the account address BX24 with Path ( https://name.bitrix24.com/rest )
16778
+ */
16779
+ getTargetOriginWithPath() {
16780
+ return `${this.#b24TargetRest}`;
16781
+ }
16782
+ /**
16783
+ * Determines whether the current user has administrator rights
16784
+ */
16785
+ get isAdmin() {
16786
+ if (null === this.#isAdmin) {
16787
+ throw new Error("isAdmin not init. You need call B24OAuth::initIsAdmin().");
16788
+ }
16789
+ return this.#isAdmin;
16790
+ }
16791
+ async initIsAdmin(http) {
16792
+ const response = await http.call("profile", {}, 0);
16793
+ if (!response.isSuccess) {
16794
+ throw new Error(response.getErrorMessages().join(";"));
16795
+ }
16796
+ const data = response.getData().result;
16797
+ if (data?.ADMIN) {
16798
+ this.#isAdmin = true;
16799
+ }
16800
+ }
16801
+ }
16802
+
16803
+ class B24OAuth extends AbstractB24 {
16804
+ #authOAuthManager;
16805
+ constructor(authOptions, oAuthSecret) {
16806
+ super();
16807
+ this.#authOAuthManager = new AuthOAuthManager(authOptions, oAuthSecret);
16808
+ this._http = new Http(
16809
+ this.#authOAuthManager.getTargetOriginWithPath(),
16810
+ this.#authOAuthManager,
16811
+ this._getHttpOptions()
16812
+ );
16813
+ this._isInit = true;
16814
+ }
16815
+ async initIsAdmin() {
16816
+ if (!this._http) {
16817
+ throw new Error("Http Not init");
16818
+ }
16819
+ return this.#authOAuthManager.initIsAdmin(this._http);
16820
+ }
16821
+ setLogger(logger) {
16822
+ super.setLogger(logger);
16823
+ }
16824
+ get auth() {
16825
+ return this.#authOAuthManager;
16826
+ }
16827
+ getTargetOrigin() {
16828
+ this._ensureInitialized();
16829
+ return this.#authOAuthManager.getTargetOrigin();
16830
+ }
16831
+ getTargetOriginWithPath() {
16832
+ this._ensureInitialized();
16833
+ return this.#authOAuthManager.getTargetOriginWithPath();
16834
+ }
16835
+ }
16836
+
16746
16837
  class UnhandledMatchError extends Error {
16747
16838
  constructor(value, ...args) {
16748
16839
  super(...args);
@@ -26213,9 +26304,11 @@ Data string: ${pullEvent}
26213
26304
  exports.AppFrame = AppFrame;
26214
26305
  exports.AuthHookManager = AuthHookManager;
26215
26306
  exports.AuthManager = AuthManager;
26307
+ exports.AuthOAuthManager = AuthOAuthManager;
26216
26308
  exports.B24Frame = B24Frame;
26217
26309
  exports.B24Hook = B24Hook;
26218
26310
  exports.B24LangList = B24LangList;
26311
+ exports.B24OAuth = B24OAuth;
26219
26312
  exports.B24PullClientManager = PullClient;
26220
26313
  exports.Browser = Browser;
26221
26314
  exports.CatalogProductImageType = CatalogProductImageType;