@ceeblue/web-utils 2.2.1 → 2.3.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.
@@ -130,6 +130,11 @@ type Params = {
130
130
  * iceServer to use while connecting to a WebRTC stream
131
131
  */
132
132
  iceServer?: RTCIceServer;
133
+ /**
134
+ * Optional media extension (mp4, flv, ts, rts), usefull for protocol like WebRTS which supports different container type.
135
+ * When not set, it's also an output parameter to indicate what is the media type selected
136
+ */
137
+ mediaExt?: string;
133
138
  /**
134
139
  * Optional query to add into the generated url of connection
135
140
  */
@@ -148,6 +153,14 @@ declare enum Type {
148
153
  /**
149
154
  * Some connection utility functions
150
155
  */
156
+ /**
157
+ * Defines the {@link Params.mediaExt} based on the type of parameters and its endpoint.
158
+ * This method always assigns a value to params.mediaExt, defaulting to an empty string if indeterminable,
159
+ * allowing detection of whether the function has been applied to the parameters.
160
+ * @param type The type of parameters to define.
161
+ * @param params The parameters for which the media extension is to be defined
162
+ */
163
+ declare function defineMediaExt(type: Type, params: Params): void;
151
164
  /**
152
165
  * Build an URL from {@link Type | type} and {@link Params | params}
153
166
  * @param type Type of the connection wanted
@@ -161,8 +174,9 @@ type Connect_Params = Params;
161
174
  type Connect_Type = Type;
162
175
  declare const Connect_Type: typeof Type;
163
176
  declare const Connect_buildURL: typeof buildURL;
177
+ declare const Connect_defineMediaExt: typeof defineMediaExt;
164
178
  declare namespace Connect {
165
- export { type Connect_Params as Params, Connect_Type as Type, Connect_buildURL as buildURL };
179
+ export { type Connect_Params as Params, Connect_Type as Type, Connect_buildURL as buildURL, Connect_defineMediaExt as defineMediaExt };
166
180
  }
167
181
 
168
182
  /**
@@ -668,27 +682,58 @@ declare function equal(a: any, b: any): boolean;
668
682
  */
669
683
  declare function fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>;
670
684
  /**
671
- * Extension parser
685
+ * Get Extension part from path
672
686
  * @param path path to parse
673
687
  * @returns the extension
674
688
  */
675
- declare function parseExtension(path: string): string;
689
+ declare function getExtension(path: string): string;
690
+ /**
691
+ * Get File part from path
692
+ * @param path path to parse
693
+ * @returns the file name
694
+ */
695
+ declare function getFile(path: string): string;
696
+ /**
697
+ * String Trim function with customizable chars
698
+ * @param value string to trim
699
+ * @param chars chars to use to trim
700
+ * @returns string trimmed
701
+ */
702
+ declare function trim(value: string, chars?: string): string;
703
+ /**
704
+ * String Trim Start function with customizable chars
705
+ * @param value string to trim start
706
+ * @param chars chars to use to trim start
707
+ * @returns string trimmed
708
+ */
709
+ declare function trimStart(value: string, chars?: string): string;
710
+ /**
711
+ * String Trim End function with customizable chars
712
+ * @param value string to trim end
713
+ * @param chars chars to use to trim end
714
+ * @returns string trimmed
715
+ */
716
+ declare function trimEnd(value: string, chars?: string): string;
676
717
 
677
718
  declare const Util_EMPTY_FUNCTION: typeof EMPTY_FUNCTION;
678
719
  declare const Util_equal: typeof equal;
679
720
  declare const Util_fetch: typeof fetch;
721
+ declare const Util_getExtension: typeof getExtension;
722
+ declare const Util_getFile: typeof getFile;
680
723
  declare const Util_objectEntries: typeof objectEntries;
681
724
  declare const Util_objectFrom: typeof objectFrom;
682
725
  declare const Util_options: typeof options;
683
- declare const Util_parseExtension: typeof parseExtension;
684
726
  declare const Util_safePromise: typeof safePromise;
685
727
  declare const Util_sleep: typeof sleep;
686
728
  declare const Util_stringify: typeof stringify;
687
729
  declare const Util_time: typeof time;
688
730
  declare const Util_timeOrigin: typeof timeOrigin;
689
731
  declare const Util_toBin: typeof toBin;
732
+ declare const Util_trim: typeof trim;
733
+ declare const Util_trimEnd: typeof trimEnd;
734
+ declare const Util_trimStart: typeof trimStart;
690
735
  declare namespace Util {
691
- export { Util_EMPTY_FUNCTION as EMPTY_FUNCTION, Util_equal as equal, Util_fetch as fetch, Util_objectEntries as objectEntries, Util_objectFrom as objectFrom, Util_options as options, Util_parseExtension as parseExtension, Util_safePromise as safePromise, Util_sleep as sleep, Util_stringify as stringify, Util_time as time, Util_timeOrigin as timeOrigin, Util_toBin as toBin };
736
+ export { Util_EMPTY_FUNCTION as EMPTY_FUNCTION, Util_equal as equal, Util_fetch as fetch, Util_getExtension as getExtension, Util_getFile as getFile, Util_objectEntries as objectEntries, Util_objectFrom as objectFrom, Util_options as options, Util_safePromise as safePromise, Util_sleep as sleep, Util_stringify as stringify, Util_time as time, Util_timeOrigin as timeOrigin, Util_toBin as toBin, Util_trim as trim, Util_trimEnd as trimEnd, Util_trimStart as trimStart };
692
737
  }
693
738
 
694
739
  /**
package/dist/web-utils.js CHANGED
@@ -642,15 +642,76 @@ function fetch(input, init) {
642
642
  });
643
643
  }
644
644
  /**
645
- * Extension parser
645
+ * Get Extension part from path
646
646
  * @param path path to parse
647
647
  * @returns the extension
648
648
  */
649
- function parseExtension(path) {
649
+ function getExtension(path) {
650
650
  const dot = path.lastIndexOf('.');
651
651
  const ext = dot >= 0 && dot > path.lastIndexOf('/') ? path.substring(dot) : '';
652
652
  return ext;
653
- }var Util=/*#__PURE__*/Object.freeze({__proto__:null,EMPTY_FUNCTION:EMPTY_FUNCTION,equal:equal,fetch:fetch,objectEntries:objectEntries,objectFrom:objectFrom,options:options,parseExtension:parseExtension,safePromise:safePromise,sleep:sleep,stringify:stringify,time:time,timeOrigin:timeOrigin,toBin:toBin});/**
653
+ }
654
+ /**
655
+ * Get File part from path
656
+ * @param path path to parse
657
+ * @returns the file name
658
+ */
659
+ function getFile(path) {
660
+ return path.substring(path.lastIndexOf('/') + 1);
661
+ }
662
+ function codesFromString(value) {
663
+ const codes = [];
664
+ for (let i = 0; i < value.length; ++i) {
665
+ codes.push(value.charCodeAt(i));
666
+ }
667
+ return codes;
668
+ }
669
+ /**
670
+ * String Trim function with customizable chars
671
+ * @param value string to trim
672
+ * @param chars chars to use to trim
673
+ * @returns string trimmed
674
+ */
675
+ function trim(value, chars = ' ') {
676
+ const codes = codesFromString(chars);
677
+ let start = 0;
678
+ while (start < value.length && codes.includes(value.charCodeAt(start))) {
679
+ ++start;
680
+ }
681
+ let end = value.length;
682
+ while (end > 0 && codes.includes(value.charCodeAt(end - 1))) {
683
+ --end;
684
+ }
685
+ return value.substring(start, end);
686
+ }
687
+ /**
688
+ * String Trim Start function with customizable chars
689
+ * @param value string to trim start
690
+ * @param chars chars to use to trim start
691
+ * @returns string trimmed
692
+ */
693
+ function trimStart(value, chars = ' ') {
694
+ const codes = codesFromString(chars);
695
+ let i = 0;
696
+ while (i < value.length && codes.includes(value.charCodeAt(i))) {
697
+ ++i;
698
+ }
699
+ return value.substring(i);
700
+ }
701
+ /**
702
+ * String Trim End function with customizable chars
703
+ * @param value string to trim end
704
+ * @param chars chars to use to trim end
705
+ * @returns string trimmed
706
+ */
707
+ function trimEnd(value, chars = ' ') {
708
+ const codes = codesFromString(chars);
709
+ let i = value.length;
710
+ while (i > 0 && codes.includes(value.charCodeAt(i - 1))) {
711
+ --i;
712
+ }
713
+ return value.substring(0, i);
714
+ }var Util=/*#__PURE__*/Object.freeze({__proto__:null,EMPTY_FUNCTION:EMPTY_FUNCTION,equal:equal,fetch:fetch,getExtension:getExtension,getFile:getFile,objectEntries:objectEntries,objectFrom:objectFrom,options:options,safePromise:safePromise,sleep:sleep,stringify:stringify,time:time,timeOrigin:timeOrigin,toBin:toBin,trim:trim,trimEnd:trimEnd,trimStart:trimStart});/**
654
715
  * Copyright 2024 Ceeblue B.V.
655
716
  * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
656
717
  * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
@@ -831,6 +892,56 @@ var Type;
831
892
  /**
832
893
  * Some connection utility functions
833
894
  */
895
+ /**
896
+ * Defines the {@link Params.mediaExt} based on the type of parameters and its endpoint.
897
+ * This method always assigns a value to params.mediaExt, defaulting to an empty string if indeterminable,
898
+ * allowing detection of whether the function has been applied to the parameters.
899
+ * @param type The type of parameters to define.
900
+ * @param params The parameters for which the media extension is to be defined
901
+ */
902
+ function defineMediaExt(type, params) {
903
+ // Fix mediaExt in removing the possible '.' prefix
904
+ if (params.mediaExt) {
905
+ params.mediaExt = trimStart(params.mediaExt, '.');
906
+ }
907
+ // Compute appropriate mediaExt out parameter
908
+ switch (type) {
909
+ case Type.HESP:
910
+ params.mediaExt = 'mp4';
911
+ break;
912
+ case Type.WEBRTC:
913
+ params.mediaExt = 'rtp';
914
+ break;
915
+ case Type.WRTS: {
916
+ try {
917
+ const url = new URL(params.endPoint);
918
+ const ext = getExtension(getFile(url.pathname));
919
+ // set extension just if not json, json means a manifest file endPoint
920
+ if (ext && ext !== 'json') {
921
+ params.mediaExt = ext;
922
+ }
923
+ }
924
+ catch (_) {
925
+ // not an URL, it's only a host => keep mediaExt unchanged to build the URL
926
+ }
927
+ if (!params.mediaExt) {
928
+ // set to its default rts value => always set for WRTS!
929
+ params.mediaExt = 'rts';
930
+ }
931
+ break;
932
+ }
933
+ case Type.META:
934
+ params.mediaExt = 'js';
935
+ break;
936
+ case Type.DATA:
937
+ params.mediaExt = 'json';
938
+ break;
939
+ default:
940
+ params.mediaExt = ''; // set always a value to know that the parameters have been fixed
941
+ console.warn('Unknown params type ' + type);
942
+ break;
943
+ }
944
+ }
834
945
  /**
835
946
  * Build an URL from {@link Type | type} and {@link Params | params}
836
947
  * @param type Type of the connection wanted
@@ -839,10 +950,8 @@ var Type;
839
950
  * @returns The URL of connection
840
951
  */
841
952
  function buildURL(type, params, protocol = 'wss') {
953
+ defineMediaExt(type, params);
842
954
  const url = new URL(NetAddress.fixProtocol(protocol, params.endPoint));
843
- // Remove possible extension of streamName put sometimes to decide format when multiple choices are possible like with WRTS
844
- const ext = parseExtension(params.streamName);
845
- params.streamName = params.streamName.substring(0, params.streamName.length - ext.length);
846
955
  if (url.pathname.length <= 1) {
847
956
  // build ceeblue path!
848
957
  switch (type) {
@@ -853,7 +962,7 @@ function buildURL(type, params, protocol = 'wss') {
853
962
  url.pathname = '/webrtc/' + params.streamName;
854
963
  break;
855
964
  case Type.WRTS:
856
- url.pathname = '/wrts/' + params.streamName + ext;
965
+ url.pathname = '/wrts/' + params.streamName;
857
966
  break;
858
967
  case Type.META:
859
968
  url.pathname = '/json_' + params.streamName + '.js';
@@ -873,7 +982,7 @@ function buildURL(type, params, protocol = 'wss') {
873
982
  url.searchParams.set(key, params.query[key]);
874
983
  }
875
984
  return url;
876
- }var Connect=/*#__PURE__*/Object.freeze({__proto__:null,get Type(){return Type},buildURL:buildURL});/**
985
+ }var Connect=/*#__PURE__*/Object.freeze({__proto__:null,get Type(){return Type},buildURL:buildURL,defineMediaExt:defineMediaExt});/**
877
986
  * Copyright 2024 Ceeblue B.V.
878
987
  * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
879
988
  * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
@@ -1711,4 +1820,4 @@ class WebSocketReliable extends EventEmitter {
1711
1820
  * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
1712
1821
  * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
1713
1822
  */
1714
- const VERSION = '2.2.1';export{BinaryReader,BinaryWriter,BitReader,ByteRate,Connect,EventEmitter,FixMap,NetAddress,Numbers,Queue,SDP,Util,VERSION,WebSocketReliable};//# sourceMappingURL=web-utils.js.map
1823
+ const VERSION = '2.3.0';export{BinaryReader,BinaryWriter,BitReader,ByteRate,Connect,EventEmitter,FixMap,NetAddress,Numbers,Queue,SDP,Util,VERSION,WebSocketReliable};//# sourceMappingURL=web-utils.js.map