@sendbird/uikit-utils 2.4.1 → 2.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/README.md +1 -1
- package/lib/commonjs/hooks/index.js +3 -30
- package/lib/commonjs/hooks/index.js.map +1 -1
- package/lib/commonjs/hooks/react-native.js +2 -11
- package/lib/commonjs/hooks/react-native.js.map +1 -1
- package/lib/commonjs/index.js +0 -42
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/sendbird/attrs.js +8 -11
- package/lib/commonjs/sendbird/attrs.js.map +1 -1
- package/lib/commonjs/sendbird/channel.js +0 -16
- package/lib/commonjs/sendbird/channel.js.map +1 -1
- package/lib/commonjs/sendbird/message.js +3 -34
- package/lib/commonjs/sendbird/message.js.map +1 -1
- package/lib/commonjs/sendbird/user.js +0 -2
- package/lib/commonjs/sendbird/user.js.map +1 -1
- package/lib/commonjs/shared/arrayToMap.js +0 -6
- package/lib/commonjs/shared/arrayToMap.js.map +1 -1
- package/lib/commonjs/shared/bufferedRequest.js +5 -26
- package/lib/commonjs/shared/bufferedRequest.js.map +1 -1
- package/lib/commonjs/shared/file.js +11 -28
- package/lib/commonjs/shared/file.js.map +1 -1
- package/lib/commonjs/shared/index.js +3 -7
- package/lib/commonjs/shared/index.js.map +1 -1
- package/lib/commonjs/shared/logger.js +3 -22
- package/lib/commonjs/shared/logger.js.map +1 -1
- package/lib/commonjs/shared/regex.js +2 -9
- package/lib/commonjs/shared/regex.js.map +1 -1
- package/lib/commonjs/ui-format/common.js +6 -18
- package/lib/commonjs/ui-format/common.js.map +1 -1
- package/lib/commonjs/ui-format/groupChannel.js +0 -15
- package/lib/commonjs/ui-format/groupChannel.js.map +1 -1
- package/lib/commonjs/ui-format/openChannel.js +0 -4
- package/lib/commonjs/ui-format/openChannel.js.map +1 -1
- package/lib/module/hooks/index.js +3 -7
- package/lib/module/hooks/index.js.map +1 -1
- package/lib/module/hooks/react-native.js +2 -2
- package/lib/module/hooks/react-native.js.map +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/sendbird/attrs.js +6 -9
- package/lib/module/sendbird/attrs.js.map +1 -1
- package/lib/module/sendbird/channel.js +0 -1
- package/lib/module/sendbird/channel.js.map +1 -1
- package/lib/module/sendbird/message.js +3 -17
- package/lib/module/sendbird/message.js.map +1 -1
- package/lib/module/sendbird/user.js.map +1 -1
- package/lib/module/shared/arrayToMap.js +0 -4
- package/lib/module/shared/arrayToMap.js.map +1 -1
- package/lib/module/shared/bufferedRequest.js +5 -24
- package/lib/module/shared/bufferedRequest.js.map +1 -1
- package/lib/module/shared/file.js +11 -16
- package/lib/module/shared/file.js.map +1 -1
- package/lib/module/shared/index.js +3 -3
- package/lib/module/shared/index.js.map +1 -1
- package/lib/module/shared/logger.js +3 -21
- package/lib/module/shared/logger.js.map +1 -1
- package/lib/module/shared/regex.js +3 -2
- package/lib/module/shared/regex.js.map +1 -1
- package/lib/module/ui-format/common.js +6 -9
- package/lib/module/ui-format/common.js.map +1 -1
- package/lib/module/ui-format/groupChannel.js +0 -7
- package/lib/module/ui-format/groupChannel.js.map +1 -1
- package/lib/module/ui-format/openChannel.js.map +1 -1
- package/package.json +11 -7
|
@@ -18,33 +18,28 @@ exports.messageComparator = messageComparator;
|
|
|
18
18
|
exports.messageKeyExtractor = messageKeyExtractor;
|
|
19
19
|
exports.parseSendbirdNotification = parseSendbirdNotification;
|
|
20
20
|
exports.shouldRenderReaction = shouldRenderReaction;
|
|
21
|
-
|
|
22
21
|
var _file = require("../shared/file");
|
|
23
|
-
|
|
24
22
|
var _common = require("../ui-format/common");
|
|
25
|
-
|
|
26
23
|
function isNewMessage(msg, currentUserId) {
|
|
27
24
|
const myMessage = isMyMessage(msg, currentUserId);
|
|
28
25
|
if (myMessage) return false;
|
|
29
26
|
if (msg.isAdminMessage()) return false;
|
|
30
27
|
return msg.updatedAt === 0;
|
|
31
28
|
}
|
|
32
|
-
|
|
33
29
|
function isSendableMessage(msg) {
|
|
34
30
|
return msg !== undefined && msg !== null && 'sendingStatus' in msg;
|
|
35
31
|
}
|
|
36
|
-
|
|
37
32
|
function isMyMessage(msg) {
|
|
38
33
|
var _msg$sender;
|
|
39
|
-
|
|
40
34
|
let currentUserId = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '##__USER_ID_IS_NOT_PROVIDED__##';
|
|
41
35
|
if (!isSendableMessage(msg)) return false;
|
|
42
36
|
return 'sender' in msg && ((_msg$sender = msg.sender) === null || _msg$sender === void 0 ? void 0 : _msg$sender.userId) === currentUserId || msg.sendingStatus === 'pending' || msg.sendingStatus === 'failed' || msg.sendingStatus === 'canceled';
|
|
43
37
|
}
|
|
44
|
-
|
|
45
38
|
function messageKeyExtractor(message) {
|
|
46
39
|
return getMessageUniqId(message);
|
|
47
|
-
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// |-------------------|-------------------|-----------------|-------------------|
|
|
48
43
|
// | sending status | reqId | messageId | createdAt |
|
|
49
44
|
// |-------------------|-------------------|-----------------|-------------------|
|
|
50
45
|
// | pending | timestamp(A) | 0 | timestamp(B) |
|
|
@@ -52,8 +47,6 @@ function messageKeyExtractor(message) {
|
|
|
52
47
|
// | failed | timestamp(A) | 0 | ? |
|
|
53
48
|
// | succeeded | timestamp(A) / '' | id from DB | timestamp(C) |
|
|
54
49
|
// |-------------------|-------------------|-----------------|-------------------|
|
|
55
|
-
|
|
56
|
-
|
|
57
50
|
function messageComparator(a, b) {
|
|
58
51
|
let aStatusOffset = 0;
|
|
59
52
|
let bStatusOffset = 0;
|
|
@@ -61,15 +54,12 @@ function messageComparator(a, b) {
|
|
|
61
54
|
if (isSendableMessage(b) && b.sendingStatus !== 'succeeded') bStatusOffset = 999999;
|
|
62
55
|
return b.createdAt + bStatusOffset - (a.createdAt + aStatusOffset);
|
|
63
56
|
}
|
|
64
|
-
|
|
65
57
|
function hasSameSender(a, b) {
|
|
66
58
|
var _a$sender, _b$sender;
|
|
67
|
-
|
|
68
59
|
if (!a || !b) return false;
|
|
69
60
|
if ('sender' in a && 'sender' in b) return ((_a$sender = a.sender) === null || _a$sender === void 0 ? void 0 : _a$sender.userId) === ((_b$sender = b.sender) === null || _b$sender === void 0 ? void 0 : _b$sender.userId);
|
|
70
61
|
return false;
|
|
71
62
|
}
|
|
72
|
-
|
|
73
63
|
function calcMessageGrouping(groupEnabled, curr, prev, next) {
|
|
74
64
|
const getPrev = () => {
|
|
75
65
|
if (!groupEnabled) return false;
|
|
@@ -79,7 +69,6 @@ function calcMessageGrouping(groupEnabled, curr, prev, next) {
|
|
|
79
69
|
if ((0, _common.getMessageTimeFormat)(new Date(curr.createdAt)) !== (0, _common.getMessageTimeFormat)(new Date(prev.createdAt))) return false;
|
|
80
70
|
return true;
|
|
81
71
|
};
|
|
82
|
-
|
|
83
72
|
const getNext = () => {
|
|
84
73
|
if (!groupEnabled) return false;
|
|
85
74
|
if (!next) return false;
|
|
@@ -88,95 +77,75 @@ function calcMessageGrouping(groupEnabled, curr, prev, next) {
|
|
|
88
77
|
if ((0, _common.getMessageTimeFormat)(new Date(curr.createdAt)) !== (0, _common.getMessageTimeFormat)(new Date(next.createdAt))) return false;
|
|
89
78
|
return true;
|
|
90
79
|
};
|
|
91
|
-
|
|
92
80
|
return {
|
|
93
81
|
groupWithPrev: getPrev(),
|
|
94
82
|
groupWithNext: getNext()
|
|
95
83
|
};
|
|
96
84
|
}
|
|
97
|
-
|
|
98
85
|
function getMessageUniqId(msg) {
|
|
99
86
|
if (msg.isUserMessage() || msg.isFileMessage()) {
|
|
100
87
|
if (msg.sendingStatus === 'succeeded') return String(msg.messageId);
|
|
101
88
|
return msg.reqId;
|
|
102
89
|
}
|
|
103
|
-
|
|
104
90
|
return String(msg.messageId);
|
|
105
91
|
}
|
|
106
|
-
|
|
107
92
|
function getAvailableUriFromFileMessage(message) {
|
|
108
93
|
if (!message.url && message.messageParams.file && 'uri' in message.messageParams.file) {
|
|
109
94
|
return message.messageParams.file.uri;
|
|
110
95
|
}
|
|
111
|
-
|
|
112
96
|
return message.url;
|
|
113
97
|
}
|
|
114
|
-
|
|
115
98
|
function isSendbirdNotification(dataPayload) {
|
|
116
99
|
if (!dataPayload) return false;
|
|
117
100
|
return Boolean(dataPayload['sendbird']);
|
|
118
101
|
}
|
|
119
|
-
|
|
120
102
|
function parseSendbirdNotification(dataPayload) {
|
|
121
103
|
return typeof dataPayload.sendbird === 'string' ? JSON.parse(dataPayload.sendbird) : dataPayload.sendbird;
|
|
122
104
|
}
|
|
123
|
-
|
|
124
105
|
function shouldRenderReaction(channel, reactionEnabled) {
|
|
125
106
|
if (channel.isOpenChannel()) {
|
|
126
107
|
return false;
|
|
127
108
|
}
|
|
128
|
-
|
|
129
109
|
if (channel.isGroupChannel()) {
|
|
130
110
|
if (channel.isBroadcast) return false;
|
|
131
111
|
if (channel.isSuper) return false;
|
|
132
112
|
}
|
|
133
|
-
|
|
134
113
|
return reactionEnabled;
|
|
135
114
|
}
|
|
136
|
-
|
|
137
115
|
function getReactionCount(reaction) {
|
|
138
116
|
return reaction.userIds.length;
|
|
139
117
|
}
|
|
140
|
-
|
|
141
118
|
function getFileTypeFromMessage(message) {
|
|
142
119
|
return (0, _file.getFileType)(message.type || (0, _file.getFileExtension)(message.name));
|
|
143
120
|
}
|
|
144
|
-
|
|
145
121
|
function getMessageType(message) {
|
|
146
122
|
if (message.isAdminMessage()) {
|
|
147
123
|
return 'admin';
|
|
148
124
|
}
|
|
149
|
-
|
|
150
125
|
if (message.isUserMessage()) {
|
|
151
126
|
if (message.ogMetaData) {
|
|
152
127
|
return 'user.opengraph';
|
|
153
128
|
}
|
|
154
|
-
|
|
155
129
|
return 'user';
|
|
156
130
|
}
|
|
157
|
-
|
|
158
131
|
if (message.isFileMessage()) {
|
|
159
132
|
const fileType = getFileTypeFromMessage(message);
|
|
160
|
-
|
|
161
133
|
switch (fileType) {
|
|
162
134
|
case 'image':
|
|
163
135
|
case 'video':
|
|
164
136
|
{
|
|
165
137
|
return `file.${fileType}`;
|
|
166
138
|
}
|
|
167
|
-
|
|
168
139
|
case 'audio':
|
|
169
140
|
{
|
|
170
141
|
return `file.${fileType}`;
|
|
171
142
|
}
|
|
172
|
-
|
|
173
143
|
default:
|
|
174
144
|
{
|
|
175
145
|
return 'file';
|
|
176
146
|
}
|
|
177
147
|
}
|
|
178
148
|
}
|
|
179
|
-
|
|
180
149
|
return 'unknown';
|
|
181
150
|
}
|
|
182
151
|
//# sourceMappingURL=message.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["isNewMessage","msg","currentUserId","myMessage","isMyMessage","isAdminMessage","updatedAt","isSendableMessage","undefined","sender","userId","sendingStatus","messageKeyExtractor","message","getMessageUniqId","messageComparator","a","b","aStatusOffset","bStatusOffset","createdAt","hasSameSender","calcMessageGrouping","groupEnabled","curr","prev","next","getPrev","getMessageTimeFormat","Date","getNext","groupWithPrev","groupWithNext","isUserMessage","isFileMessage","String","messageId","reqId","getAvailableUriFromFileMessage","url","messageParams","file","uri","isSendbirdNotification","dataPayload","Boolean","parseSendbirdNotification","sendbird","JSON","parse","shouldRenderReaction","channel","reactionEnabled","isOpenChannel","isGroupChannel","isBroadcast","isSuper","getReactionCount","reaction","userIds","length","getFileTypeFromMessage","getFileType","type","getFileExtension","name","getMessageType","ogMetaData","fileType"],"sources":["message.ts"],"sourcesContent":["import { getFileExtension, getFileType } from '../shared/file';\nimport type {\n SendbirdBaseChannel,\n SendbirdBaseMessage,\n SendbirdDataPayload,\n SendbirdFileMessage,\n SendbirdMessage,\n SendbirdReaction,\n SendbirdSendableMessage,\n} from '../types';\nimport { getMessageTimeFormat } from '../ui-format/common';\n\nexport function isNewMessage(msg: SendbirdMessage, currentUserId?: string) {\n const myMessage = isMyMessage(msg, currentUserId);\n if (myMessage) return false;\n if (msg.isAdminMessage()) return false;\n return msg.updatedAt === 0;\n}\n\nexport function isSendableMessage(msg?: SendbirdMessage | null): msg is SendbirdSendableMessage {\n return msg !== undefined && msg !== null && 'sendingStatus' in msg;\n}\n\nexport function isMyMessage(msg?: SendbirdMessage | null, currentUserId = '##__USER_ID_IS_NOT_PROVIDED__##') {\n if (!isSendableMessage(msg)) return false;\n return (\n ('sender' in msg && msg.sender?.userId === currentUserId) ||\n msg.sendingStatus === 'pending' ||\n msg.sendingStatus === 'failed' ||\n msg.sendingStatus === 'canceled'\n );\n}\n\nexport function messageKeyExtractor(message: SendbirdMessage): string {\n return getMessageUniqId(message);\n}\n\n// |-------------------|-------------------|-----------------|-------------------|\n// | sending status | reqId | messageId | createdAt |\n// |-------------------|-------------------|-----------------|-------------------|\n// | pending | timestamp(A) | 0 | timestamp(B) |\n// | canceled | timestamp(A) | 0 | ? |\n// | failed | timestamp(A) | 0 | ? |\n// | succeeded | timestamp(A) / '' | id from DB | timestamp(C) |\n// |-------------------|-------------------|-----------------|-------------------|\nexport function messageComparator(a: SendbirdMessage, b: SendbirdMessage) {\n let aStatusOffset = 0;\n let bStatusOffset = 0;\n\n if (isSendableMessage(a) && a.sendingStatus !== 'succeeded') aStatusOffset = 999999;\n if (isSendableMessage(b) && b.sendingStatus !== 'succeeded') bStatusOffset = 999999;\n\n return b.createdAt + bStatusOffset - (a.createdAt + aStatusOffset);\n}\n\nexport function hasSameSender(a?: SendbirdMessage, b?: SendbirdMessage) {\n if (!a || !b) return false;\n if ('sender' in a && 'sender' in b) return a.sender?.userId === b.sender?.userId;\n return false;\n}\n\nexport function calcMessageGrouping(\n groupEnabled: boolean,\n curr: SendbirdMessage,\n prev?: SendbirdMessage,\n next?: SendbirdMessage,\n) {\n const getPrev = () => {\n if (!groupEnabled) return false;\n if (!prev) return false;\n if (curr.isAdminMessage()) return false;\n if (!hasSameSender(curr, prev)) return false;\n if (getMessageTimeFormat(new Date(curr.createdAt)) !== getMessageTimeFormat(new Date(prev.createdAt))) return false;\n return true;\n };\n\n const getNext = () => {\n if (!groupEnabled) return false;\n if (!next) return false;\n if (curr.isAdminMessage()) return false;\n if (!hasSameSender(curr, next)) return false;\n if (getMessageTimeFormat(new Date(curr.createdAt)) !== getMessageTimeFormat(new Date(next.createdAt))) return false;\n return true;\n };\n\n return { groupWithPrev: getPrev(), groupWithNext: getNext() };\n}\n\nexport function getMessageUniqId(msg: SendbirdBaseMessage) {\n if (msg.isUserMessage() || msg.isFileMessage()) {\n if (msg.sendingStatus === 'succeeded') return String(msg.messageId);\n return msg.reqId;\n }\n\n return String(msg.messageId);\n}\n\nexport function getAvailableUriFromFileMessage(message: SendbirdFileMessage) {\n if (!message.url && message.messageParams.file && 'uri' in message.messageParams.file) {\n return message.messageParams.file.uri;\n }\n return message.url;\n}\n\ntype RawSendbirdDataPayload = { sendbird: string | object };\nexport function isSendbirdNotification(dataPayload?: {\n [key: string]: string | object;\n}): dataPayload is RawSendbirdDataPayload {\n if (!dataPayload) return false;\n return Boolean(dataPayload['sendbird']);\n}\n\nexport function parseSendbirdNotification(dataPayload: RawSendbirdDataPayload): SendbirdDataPayload {\n return typeof dataPayload.sendbird === 'string' ? JSON.parse(dataPayload.sendbird) : dataPayload.sendbird;\n}\n\nexport function shouldRenderReaction(channel: SendbirdBaseChannel, reactionEnabled: boolean) {\n if (channel.isOpenChannel()) {\n return false;\n }\n\n if (channel.isGroupChannel()) {\n if (channel.isBroadcast) return false;\n if (channel.isSuper) return false;\n }\n\n return reactionEnabled;\n}\n\nexport function getReactionCount(reaction: SendbirdReaction) {\n return reaction.userIds.length;\n}\n\ntype MessageType =\n | 'user'\n | 'admin'\n | 'file'\n | 'unknown'\n | `user.${'opengraph'}`\n | `file.${'image' | 'video' | 'audio'}`;\n\nexport function getFileTypeFromMessage(message: SendbirdFileMessage) {\n return getFileType(message.type || getFileExtension(message.name));\n}\n\nexport function getMessageType(message: SendbirdMessage): MessageType {\n if (message.isAdminMessage()) {\n return 'admin';\n }\n\n if (message.isUserMessage()) {\n if (message.ogMetaData) {\n return 'user.opengraph';\n }\n return 'user';\n }\n\n if (message.isFileMessage()) {\n const fileType = getFileTypeFromMessage(message);\n switch (fileType) {\n case 'image':\n case 'video': {\n return `file.${fileType}`;\n }\n case 'audio': {\n return `file.${fileType}`;\n }\n default: {\n return 'file';\n }\n }\n }\n\n return 'unknown';\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;AAUA;;AAEO,SAASA,YAAT,CAAsBC,GAAtB,EAA4CC,aAA5C,EAAoE;EACzE,MAAMC,SAAS,GAAGC,WAAW,CAACH,GAAD,EAAMC,aAAN,CAA7B;EACA,IAAIC,SAAJ,EAAe,OAAO,KAAP;EACf,IAAIF,GAAG,CAACI,cAAJ,EAAJ,EAA0B,OAAO,KAAP;EAC1B,OAAOJ,GAAG,CAACK,SAAJ,KAAkB,CAAzB;AACD;;AAEM,SAASC,iBAAT,CAA2BN,GAA3B,EAAyF;EAC9F,OAAOA,GAAG,KAAKO,SAAR,IAAqBP,GAAG,KAAK,IAA7B,IAAqC,mBAAmBA,GAA/D;AACD;;AAEM,SAASG,WAAT,CAAqBH,GAArB,EAAsG;EAAA;;EAAA,IAAnDC,aAAmD,uEAAnC,iCAAmC;EAC3G,IAAI,CAACK,iBAAiB,CAACN,GAAD,CAAtB,EAA6B,OAAO,KAAP;EAC7B,OACG,YAAYA,GAAZ,IAAmB,gBAAAA,GAAG,CAACQ,MAAJ,4DAAYC,MAAZ,MAAuBR,aAA3C,IACAD,GAAG,CAACU,aAAJ,KAAsB,SADtB,IAEAV,GAAG,CAACU,aAAJ,KAAsB,QAFtB,IAGAV,GAAG,CAACU,aAAJ,KAAsB,UAJxB;AAMD;;AAEM,SAASC,mBAAT,CAA6BC,OAA7B,EAA+D;EACpE,OAAOC,gBAAgB,CAACD,OAAD,CAAvB;AACD,C,CAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASE,iBAAT,CAA2BC,CAA3B,EAA+CC,CAA/C,EAAmE;EACxE,IAAIC,aAAa,GAAG,CAApB;EACA,IAAIC,aAAa,GAAG,CAApB;EAEA,IAAIZ,iBAAiB,CAACS,CAAD,CAAjB,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,OAAQ,QAAOA,QAAS,EAAxB;QACD;;MACD;QAAS;UACP,OAAO,MAAP;QACD;IAVH;EAYD;;EAED,OAAO,SAAP;AACD"}
|
|
1
|
+
{"version":3,"names":["_file","require","_common","isNewMessage","msg","currentUserId","myMessage","isMyMessage","isAdminMessage","updatedAt","isSendableMessage","undefined","_msg$sender","arguments","length","sender","userId","sendingStatus","messageKeyExtractor","message","getMessageUniqId","messageComparator","a","b","aStatusOffset","bStatusOffset","createdAt","hasSameSender","_a$sender","_b$sender","calcMessageGrouping","groupEnabled","curr","prev","next","getPrev","getMessageTimeFormat","Date","getNext","groupWithPrev","groupWithNext","isUserMessage","isFileMessage","String","messageId","reqId","getAvailableUriFromFileMessage","url","messageParams","file","uri","isSendbirdNotification","dataPayload","Boolean","parseSendbirdNotification","sendbird","JSON","parse","shouldRenderReaction","channel","reactionEnabled","isOpenChannel","isGroupChannel","isBroadcast","isSuper","getReactionCount","reaction","userIds","getFileTypeFromMessage","getFileType","type","getFileExtension","name","getMessageType","ogMetaData","fileType"],"sources":["message.ts"],"sourcesContent":["import { getFileExtension, getFileType } from '../shared/file';\nimport type {\n SendbirdBaseChannel,\n SendbirdBaseMessage,\n SendbirdDataPayload,\n SendbirdFileMessage,\n SendbirdMessage,\n SendbirdReaction,\n SendbirdSendableMessage,\n} from '../types';\nimport { getMessageTimeFormat } from '../ui-format/common';\n\nexport function isNewMessage(msg: SendbirdMessage, currentUserId?: string) {\n const myMessage = isMyMessage(msg, currentUserId);\n if (myMessage) return false;\n if (msg.isAdminMessage()) return false;\n return msg.updatedAt === 0;\n}\n\nexport function isSendableMessage(msg?: SendbirdMessage | null): msg is SendbirdSendableMessage {\n return msg !== undefined && msg !== null && 'sendingStatus' in msg;\n}\n\nexport function isMyMessage(msg?: SendbirdMessage | null, currentUserId = '##__USER_ID_IS_NOT_PROVIDED__##') {\n if (!isSendableMessage(msg)) return false;\n return (\n ('sender' in msg && msg.sender?.userId === currentUserId) ||\n msg.sendingStatus === 'pending' ||\n msg.sendingStatus === 'failed' ||\n msg.sendingStatus === 'canceled'\n );\n}\n\nexport function messageKeyExtractor(message: SendbirdMessage): string {\n return getMessageUniqId(message);\n}\n\n// |-------------------|-------------------|-----------------|-------------------|\n// | sending status | reqId | messageId | createdAt |\n// |-------------------|-------------------|-----------------|-------------------|\n// | pending | timestamp(A) | 0 | timestamp(B) |\n// | canceled | timestamp(A) | 0 | ? |\n// | failed | timestamp(A) | 0 | ? |\n// | succeeded | timestamp(A) / '' | id from DB | timestamp(C) |\n// |-------------------|-------------------|-----------------|-------------------|\nexport function messageComparator(a: SendbirdMessage, b: SendbirdMessage) {\n let aStatusOffset = 0;\n let bStatusOffset = 0;\n\n if (isSendableMessage(a) && a.sendingStatus !== 'succeeded') aStatusOffset = 999999;\n if (isSendableMessage(b) && b.sendingStatus !== 'succeeded') bStatusOffset = 999999;\n\n return b.createdAt + bStatusOffset - (a.createdAt + aStatusOffset);\n}\n\nexport function hasSameSender(a?: SendbirdMessage, b?: SendbirdMessage) {\n if (!a || !b) return false;\n if ('sender' in a && 'sender' in b) return a.sender?.userId === b.sender?.userId;\n return false;\n}\n\nexport function calcMessageGrouping(\n groupEnabled: boolean,\n curr: SendbirdMessage,\n prev?: SendbirdMessage,\n next?: SendbirdMessage,\n) {\n const getPrev = () => {\n if (!groupEnabled) return false;\n if (!prev) return false;\n if (curr.isAdminMessage()) return false;\n if (!hasSameSender(curr, prev)) return false;\n if (getMessageTimeFormat(new Date(curr.createdAt)) !== getMessageTimeFormat(new Date(prev.createdAt))) return false;\n return true;\n };\n\n const getNext = () => {\n if (!groupEnabled) return false;\n if (!next) return false;\n if (curr.isAdminMessage()) return false;\n if (!hasSameSender(curr, next)) return false;\n if (getMessageTimeFormat(new Date(curr.createdAt)) !== getMessageTimeFormat(new Date(next.createdAt))) return false;\n return true;\n };\n\n return { groupWithPrev: getPrev(), groupWithNext: getNext() };\n}\n\nexport function getMessageUniqId(msg: SendbirdBaseMessage) {\n if (msg.isUserMessage() || msg.isFileMessage()) {\n if (msg.sendingStatus === 'succeeded') return String(msg.messageId);\n return msg.reqId;\n }\n\n return String(msg.messageId);\n}\n\nexport function getAvailableUriFromFileMessage(message: SendbirdFileMessage) {\n if (!message.url && message.messageParams.file && 'uri' in message.messageParams.file) {\n return message.messageParams.file.uri;\n }\n return message.url;\n}\n\ntype RawSendbirdDataPayload = { sendbird: string | object };\nexport function isSendbirdNotification(dataPayload?: {\n [key: string]: string | object;\n}): dataPayload is RawSendbirdDataPayload {\n if (!dataPayload) return false;\n return Boolean(dataPayload['sendbird']);\n}\n\nexport function parseSendbirdNotification(dataPayload: RawSendbirdDataPayload): SendbirdDataPayload {\n return typeof dataPayload.sendbird === 'string' ? JSON.parse(dataPayload.sendbird) : dataPayload.sendbird;\n}\n\nexport function shouldRenderReaction(channel: SendbirdBaseChannel, reactionEnabled: boolean) {\n if (channel.isOpenChannel()) {\n return false;\n }\n\n if (channel.isGroupChannel()) {\n if (channel.isBroadcast) return false;\n if (channel.isSuper) return false;\n }\n\n return reactionEnabled;\n}\n\nexport function getReactionCount(reaction: SendbirdReaction) {\n return reaction.userIds.length;\n}\n\ntype MessageType =\n | 'user'\n | 'admin'\n | 'file'\n | 'unknown'\n | `user.${'opengraph'}`\n | `file.${'image' | 'video' | 'audio'}`;\n\nexport function getFileTypeFromMessage(message: SendbirdFileMessage) {\n return getFileType(message.type || getFileExtension(message.name));\n}\n\nexport function getMessageType(message: SendbirdMessage): MessageType {\n if (message.isAdminMessage()) {\n return 'admin';\n }\n\n if (message.isUserMessage()) {\n if (message.ogMetaData) {\n return 'user.opengraph';\n }\n return 'user';\n }\n\n if (message.isFileMessage()) {\n const fileType = getFileTypeFromMessage(message);\n switch (fileType) {\n case 'image':\n case 'video': {\n return `file.${fileType}`;\n }\n case 'audio': {\n return `file.${fileType}`;\n }\n default: {\n return 'file';\n }\n }\n }\n\n return 'unknown';\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,IAAAA,KAAA,GAAAC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AAQ,QAAOA,QAAS,EAAC;QAC3B;MACA;QAAS;UACP,OAAO,MAAM;QACf;IAAC;EAEL;EAEA,OAAO,SAAS;AAClB"}
|
|
@@ -5,12 +5,10 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.getUserUniqId = getUserUniqId;
|
|
7
7
|
exports.ifOperator = ifOperator;
|
|
8
|
-
|
|
9
8
|
function ifOperator(role, then, or) {
|
|
10
9
|
if (role === 'operator') return then;
|
|
11
10
|
return or;
|
|
12
11
|
}
|
|
13
|
-
|
|
14
12
|
function getUserUniqId(user) {
|
|
15
13
|
return user.userId;
|
|
16
14
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["ifOperator","role","then","or","getUserUniqId","user","userId"],"sources":["user.ts"],"sourcesContent":["import type { Role } from '@sendbird/chat';\n\nimport type { UserStruct } from '../types';\n\nexport function ifOperator<T>(role: Role, then: T): T | undefined;\nexport function ifOperator<T, V>(role: Role, then: T, or: V): T | V;\nexport function ifOperator(role: Role, then: unknown, or?: unknown) {\n if (role === 'operator') return then;\n return or;\n}\n\nexport function getUserUniqId<T extends UserStruct>(user: T) {\n return user.userId;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"names":["ifOperator","role","then","or","getUserUniqId","user","userId"],"sources":["user.ts"],"sourcesContent":["import type { Role } from '@sendbird/chat';\n\nimport type { UserStruct } from '../types';\n\nexport function ifOperator<T>(role: Role, then: T): T | undefined;\nexport function ifOperator<T, V>(role: Role, then: T, or: V): T | V;\nexport function ifOperator(role: Role, then: unknown, or?: unknown) {\n if (role === 'operator') return then;\n return or;\n}\n\nexport function getUserUniqId<T extends UserStruct>(user: T) {\n return user.userId;\n}\n"],"mappings":";;;;;;;AAMO,SAASA,UAAUA,CAACC,IAAU,EAAEC,IAAa,EAAEC,EAAY,EAAE;EAClE,IAAIF,IAAI,KAAK,UAAU,EAAE,OAAOC,IAAI;EACpC,OAAOC,EAAE;AACX;AAEO,SAASC,aAAaA,CAAuBC,IAAO,EAAE;EAC3D,OAAOA,IAAI,CAACC,MAAM;AACpB"}
|
|
@@ -5,7 +5,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.arrayToMapWithGetter = arrayToMapWithGetter;
|
|
7
7
|
exports.default = arrayToMap;
|
|
8
|
-
|
|
9
8
|
/**
|
|
10
9
|
* Convert Array of the object to Object
|
|
11
10
|
* @param {object[]} arr
|
|
@@ -31,22 +30,17 @@ function arrayToMap(arr, selector, fallbackSelector, combine) {
|
|
|
31
30
|
return arr.reduce((accum, curr) => {
|
|
32
31
|
if (combine && fallbackSelector) {
|
|
33
32
|
const _key = curr[selector] + curr[fallbackSelector];
|
|
34
|
-
|
|
35
33
|
accum[_key] = curr;
|
|
36
34
|
} else {
|
|
37
35
|
const _key = curr[selector] || curr[fallbackSelector];
|
|
38
|
-
|
|
39
36
|
accum[_key] = curr;
|
|
40
37
|
}
|
|
41
|
-
|
|
42
38
|
return accum;
|
|
43
39
|
}, {});
|
|
44
40
|
}
|
|
45
|
-
|
|
46
41
|
function arrayToMapWithGetter(arr, getSelector) {
|
|
47
42
|
return arr.reduce((accum, curr) => {
|
|
48
43
|
const _key = getSelector(curr);
|
|
49
|
-
|
|
50
44
|
accum[_key] = curr;
|
|
51
45
|
return accum;
|
|
52
46
|
}, {});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["arrayToMap","arr","selector","fallbackSelector","combine","reduce","accum","curr","_key","arrayToMapWithGetter","getSelector"],"sources":["arrayToMap.ts"],"sourcesContent":["import type { FilterByValueType } from '../types';\n\nexport default function arrayToMap<T extends Record<K, unknown>, K extends keyof T = keyof T>(\n arr: T[],\n selector: keyof FilterByValueType<T, string | number>,\n): Record<string, T>;\n\nexport default function arrayToMap<T extends Record<K, unknown>, K extends keyof T = keyof T>(\n arr: T[],\n selector: string,\n fallbackSelector: K,\n): Record<string, T>;\n\nexport default function arrayToMap<T extends Record<K, unknown>, K extends keyof T = keyof T>(\n arr: T[],\n selector: string,\n selector2: string,\n combineKey: true,\n): Record<string, T>;\n\n/**\n * Convert Array of the object to Object\n * @param {object[]} arr\n * @param {keyof object} selector\n * @param {keyof object} [fallbackSelector]\n * @param {boolean} [combine]\n * @returns {Record<keyof object, object>}\n * @example\n * ```\n * const arr = [{name: 'john'}, {name: 'chris'}, {name: 'sravan'}, {name: 'hoon'}];\n * const obj = arrayToMap(arr, 'name');\n *\n * console.log(obj);\n * // {\n * // 'john': {name: 'john'},\n * // 'chris': {name: 'chris'},\n * // 'sravan': {name: 'sravan'},\n * // 'hoon': {name: 'hoon'}\n * // }\n * ```\n * */\nexport default function arrayToMap<T extends Record<K, unknown>, K extends keyof T = keyof T>(\n arr: T[],\n selector: K,\n fallbackSelector?: K,\n combine?: boolean,\n) {\n return arr.reduce((accum, curr) => {\n if (combine && fallbackSelector) {\n const _key = curr[selector] + (curr[fallbackSelector] as string);\n accum[_key] = curr;\n } else {\n const _key = (curr[selector] || curr[fallbackSelector as K]) as string;\n accum[_key] = curr;\n }\n\n return accum;\n }, {} as Record<string, T>);\n}\n\nexport function arrayToMapWithGetter<T>(arr: T[], getSelector: (item: T) => string) {\n return arr.reduce((accum, curr) => {\n const _key = getSelector(curr);\n accum[_key] = curr;\n return accum;\n }, {} as Record<string, T>);\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"names":["arrayToMap","arr","selector","fallbackSelector","combine","reduce","accum","curr","_key","arrayToMapWithGetter","getSelector"],"sources":["arrayToMap.ts"],"sourcesContent":["import type { FilterByValueType } from '../types';\n\nexport default function arrayToMap<T extends Record<K, unknown>, K extends keyof T = keyof T>(\n arr: T[],\n selector: keyof FilterByValueType<T, string | number>,\n): Record<string, T>;\n\nexport default function arrayToMap<T extends Record<K, unknown>, K extends keyof T = keyof T>(\n arr: T[],\n selector: string,\n fallbackSelector: K,\n): Record<string, T>;\n\nexport default function arrayToMap<T extends Record<K, unknown>, K extends keyof T = keyof T>(\n arr: T[],\n selector: string,\n selector2: string,\n combineKey: true,\n): Record<string, T>;\n\n/**\n * Convert Array of the object to Object\n * @param {object[]} arr\n * @param {keyof object} selector\n * @param {keyof object} [fallbackSelector]\n * @param {boolean} [combine]\n * @returns {Record<keyof object, object>}\n * @example\n * ```\n * const arr = [{name: 'john'}, {name: 'chris'}, {name: 'sravan'}, {name: 'hoon'}];\n * const obj = arrayToMap(arr, 'name');\n *\n * console.log(obj);\n * // {\n * // 'john': {name: 'john'},\n * // 'chris': {name: 'chris'},\n * // 'sravan': {name: 'sravan'},\n * // 'hoon': {name: 'hoon'}\n * // }\n * ```\n * */\nexport default function arrayToMap<T extends Record<K, unknown>, K extends keyof T = keyof T>(\n arr: T[],\n selector: K,\n fallbackSelector?: K,\n combine?: boolean,\n) {\n return arr.reduce((accum, curr) => {\n if (combine && fallbackSelector) {\n const _key = curr[selector] + (curr[fallbackSelector] as string);\n accum[_key] = curr;\n } else {\n const _key = (curr[selector] || curr[fallbackSelector as K]) as string;\n accum[_key] = curr;\n }\n\n return accum;\n }, {} as Record<string, T>);\n}\n\nexport function arrayToMapWithGetter<T>(arr: T[], getSelector: (item: T) => string) {\n return arr.reduce((accum, curr) => {\n const _key = getSelector(curr);\n accum[_key] = curr;\n return accum;\n }, {} as Record<string, T>);\n}\n"],"mappings":";;;;;;;AAoBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASA,UAAUA,CAChCC,GAAQ,EACRC,QAAW,EACXC,gBAAoB,EACpBC,OAAiB,EACjB;EACA,OAAOH,GAAG,CAACI,MAAM,CAAC,CAACC,KAAK,EAAEC,IAAI,KAAK;IACjC,IAAIH,OAAO,IAAID,gBAAgB,EAAE;MAC/B,MAAMK,IAAI,GAAGD,IAAI,CAACL,QAAQ,CAAC,GAAIK,IAAI,CAACJ,gBAAgB,CAAY;MAChEG,KAAK,CAACE,IAAI,CAAC,GAAGD,IAAI;IACpB,CAAC,MAAM;MACL,MAAMC,IAAI,GAAID,IAAI,CAACL,QAAQ,CAAC,IAAIK,IAAI,CAACJ,gBAAgB,CAAiB;MACtEG,KAAK,CAACE,IAAI,CAAC,GAAGD,IAAI;IACpB;IAEA,OAAOD,KAAK;EACd,CAAC,EAAE,CAAC,CAAC,CAAsB;AAC7B;AAEO,SAASG,oBAAoBA,CAAIR,GAAQ,EAAES,WAAgC,EAAE;EAClF,OAAOT,GAAG,CAACI,MAAM,CAAC,CAACC,KAAK,EAAEC,IAAI,KAAK;IACjC,MAAMC,IAAI,GAAGE,WAAW,CAACH,IAAI,CAAC;IAC9BD,KAAK,CAACE,IAAI,CAAC,GAAGD,IAAI;IAClB,OAAOD,KAAK;EACd,CAAC,EAAE,CAAC,CAAC,CAAsB;AAC7B"}
|
|
@@ -4,46 +4,38 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.BufferedRequest = void 0;
|
|
7
|
-
|
|
8
|
-
function
|
|
9
|
-
|
|
7
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
8
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|
9
|
+
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
10
10
|
let REQ_PER_TIMEOUT = 5;
|
|
11
11
|
let TIMEOUT_MILLS = 1000;
|
|
12
12
|
const SAFE_TIMEOUT_BUFFER = 100;
|
|
13
|
-
|
|
14
13
|
function generateRandomId() {
|
|
15
14
|
return Math.random().toString(16).slice(2);
|
|
16
15
|
}
|
|
17
|
-
|
|
18
16
|
class BufferedRequest {
|
|
19
17
|
static updateMarkAsReadOptions(reqPerTimeout, timeoutMills) {
|
|
20
18
|
BufferedRequest.markAsRead = BufferedRequest.create(reqPerTimeout, timeoutMills);
|
|
21
19
|
}
|
|
22
|
-
|
|
23
20
|
static updateMarkAsDeliveredOptions(reqPerTimeout, timeoutMills) {
|
|
24
21
|
BufferedRequest.markAsDelivered = BufferedRequest.create(reqPerTimeout, timeoutMills);
|
|
25
22
|
}
|
|
26
|
-
|
|
27
23
|
static get reqPerTimeout() {
|
|
28
24
|
return REQ_PER_TIMEOUT;
|
|
29
25
|
}
|
|
30
|
-
|
|
31
26
|
static set reqPerTimeout(value) {
|
|
32
27
|
REQ_PER_TIMEOUT = value;
|
|
33
28
|
BufferedRequest.markAsRead = BufferedRequest.create();
|
|
34
29
|
BufferedRequest.markAsDelivered = BufferedRequest.create();
|
|
35
30
|
}
|
|
36
|
-
|
|
37
31
|
static get timeoutMills() {
|
|
38
32
|
return TIMEOUT_MILLS;
|
|
39
33
|
}
|
|
40
|
-
|
|
41
34
|
static set timeoutMills(value) {
|
|
42
35
|
TIMEOUT_MILLS = value;
|
|
43
36
|
BufferedRequest.markAsRead = BufferedRequest.create();
|
|
44
37
|
BufferedRequest.markAsDelivered = BufferedRequest.create();
|
|
45
38
|
}
|
|
46
|
-
|
|
47
39
|
static create() {
|
|
48
40
|
let reqPerTimeout = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : REQ_PER_TIMEOUT;
|
|
49
41
|
let timeoutMills = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : TIMEOUT_MILLS;
|
|
@@ -56,16 +48,13 @@ class BufferedRequest {
|
|
|
56
48
|
waitQueue.set(lane ?? generateRandomId(), func);
|
|
57
49
|
this.invoke();
|
|
58
50
|
},
|
|
59
|
-
|
|
60
51
|
shift() {
|
|
61
52
|
if (nextQueue.size < reqPerTimeout) {
|
|
62
53
|
const nextRemains = Math.min(reqPerTimeout - nextQueue.size, waitQueue.size);
|
|
63
54
|
const lanes = [...waitQueue.keys()];
|
|
64
|
-
|
|
65
55
|
for (let n = 0; n < nextRemains; n++) {
|
|
66
56
|
const lane = lanes[n];
|
|
67
57
|
const func = waitQueue.get(lane);
|
|
68
|
-
|
|
69
58
|
if (func) {
|
|
70
59
|
waitQueue.delete(lane);
|
|
71
60
|
nextQueue.set(lane, func);
|
|
@@ -73,19 +62,16 @@ class BufferedRequest {
|
|
|
73
62
|
}
|
|
74
63
|
}
|
|
75
64
|
},
|
|
76
|
-
|
|
77
65
|
handleIdle() {
|
|
78
66
|
if (0 < nextQueue.size) {
|
|
79
67
|
state = 'processing';
|
|
80
68
|
this.invoke();
|
|
81
69
|
}
|
|
82
70
|
},
|
|
83
|
-
|
|
84
71
|
handleProcessing() {
|
|
85
72
|
if (timeout) return;
|
|
86
73
|
timeout = setTimeout(() => {
|
|
87
74
|
timeout = undefined;
|
|
88
|
-
|
|
89
75
|
if (0 < nextQueue.size || 0 < waitQueue.size) {
|
|
90
76
|
this.invoke();
|
|
91
77
|
} else {
|
|
@@ -96,34 +82,27 @@ class BufferedRequest {
|
|
|
96
82
|
const nextRequestBaseTimeout = timeoutMills / nextQueue.size;
|
|
97
83
|
nextQueue.forEach(func => {
|
|
98
84
|
setTimeout(() => {
|
|
99
|
-
func();
|
|
85
|
+
func();
|
|
86
|
+
// TODO: Add retry
|
|
100
87
|
//.catch(() => waitQueue.set(lane, func));
|
|
101
88
|
}, nextRequestBaseTimeout * index);
|
|
102
89
|
index++;
|
|
103
90
|
});
|
|
104
91
|
nextQueue.clear();
|
|
105
92
|
},
|
|
106
|
-
|
|
107
93
|
async invoke() {
|
|
108
94
|
this.shift();
|
|
109
|
-
|
|
110
95
|
if (state === 'idle') {
|
|
111
96
|
this.handleIdle();
|
|
112
97
|
}
|
|
113
|
-
|
|
114
98
|
if (state === 'processing') {
|
|
115
99
|
this.handleProcessing();
|
|
116
100
|
}
|
|
117
101
|
}
|
|
118
|
-
|
|
119
102
|
};
|
|
120
103
|
}
|
|
121
|
-
|
|
122
104
|
}
|
|
123
|
-
|
|
124
105
|
exports.BufferedRequest = BufferedRequest;
|
|
125
|
-
|
|
126
106
|
_defineProperty(BufferedRequest, "markAsRead", BufferedRequest.create());
|
|
127
|
-
|
|
128
107
|
_defineProperty(BufferedRequest, "markAsDelivered", BufferedRequest.create());
|
|
129
108
|
//# sourceMappingURL=bufferedRequest.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["REQ_PER_TIMEOUT","TIMEOUT_MILLS","SAFE_TIMEOUT_BUFFER","generateRandomId","Math","random","toString","slice","BufferedRequest","updateMarkAsReadOptions","reqPerTimeout","timeoutMills","markAsRead","create","updateMarkAsDeliveredOptions","markAsDelivered","value","waitQueue","Map","nextQueue","state","timeout","push","func","lane","set","invoke","shift","size","nextRemains","min","lanes","keys","n","get","delete","handleIdle","handleProcessing","setTimeout","
|
|
1
|
+
{"version":3,"names":["REQ_PER_TIMEOUT","TIMEOUT_MILLS","SAFE_TIMEOUT_BUFFER","generateRandomId","Math","random","toString","slice","BufferedRequest","updateMarkAsReadOptions","reqPerTimeout","timeoutMills","markAsRead","create","updateMarkAsDeliveredOptions","markAsDelivered","value","arguments","length","undefined","waitQueue","Map","nextQueue","state","timeout","push","func","lane","set","invoke","shift","size","nextRemains","min","lanes","keys","n","get","delete","handleIdle","handleProcessing","setTimeout","index","nextRequestBaseTimeout","forEach","clear","exports","_defineProperty"],"sources":["bufferedRequest.ts"],"sourcesContent":["type Func = (...args: unknown[]) => Promise<unknown>;\ntype State = 'idle' | 'processing';\n\nlet REQ_PER_TIMEOUT = 5;\nlet TIMEOUT_MILLS = 1000;\nconst SAFE_TIMEOUT_BUFFER = 100;\n\nfunction generateRandomId() {\n return Math.random().toString(16).slice(2);\n}\n\nexport class BufferedRequest {\n public static markAsRead = BufferedRequest.create();\n public static markAsDelivered = BufferedRequest.create();\n\n public static updateMarkAsReadOptions(reqPerTimeout: number, timeoutMills: number) {\n BufferedRequest.markAsRead = BufferedRequest.create(reqPerTimeout, timeoutMills);\n }\n public static updateMarkAsDeliveredOptions(reqPerTimeout: number, timeoutMills: number) {\n BufferedRequest.markAsDelivered = BufferedRequest.create(reqPerTimeout, timeoutMills);\n }\n\n public static get reqPerTimeout() {\n return REQ_PER_TIMEOUT;\n }\n public static set reqPerTimeout(value: number) {\n REQ_PER_TIMEOUT = value;\n BufferedRequest.markAsRead = BufferedRequest.create();\n BufferedRequest.markAsDelivered = BufferedRequest.create();\n }\n\n public static get timeoutMills() {\n return TIMEOUT_MILLS;\n }\n public static set timeoutMills(value: number) {\n TIMEOUT_MILLS = value;\n BufferedRequest.markAsRead = BufferedRequest.create();\n BufferedRequest.markAsDelivered = BufferedRequest.create();\n }\n\n public static create(reqPerTimeout = REQ_PER_TIMEOUT, timeoutMills = TIMEOUT_MILLS) {\n const waitQueue = new Map<string, Func>();\n const nextQueue = new Map<string, Func>();\n\n let state: State = 'idle';\n let timeout: NodeJS.Timeout | undefined;\n\n return {\n push(func: Func, lane?: string) {\n waitQueue.set(lane ?? generateRandomId(), func);\n this.invoke();\n },\n shift() {\n if (nextQueue.size < reqPerTimeout) {\n const nextRemains = Math.min(reqPerTimeout - nextQueue.size, waitQueue.size);\n const lanes = [...waitQueue.keys()];\n for (let n = 0; n < nextRemains; n++) {\n const lane = lanes[n];\n const func = waitQueue.get(lane);\n if (func) {\n waitQueue.delete(lane);\n nextQueue.set(lane, func);\n }\n }\n }\n },\n handleIdle() {\n if (0 < nextQueue.size) {\n state = 'processing';\n this.invoke();\n }\n },\n handleProcessing() {\n if (timeout) return;\n\n timeout = setTimeout(() => {\n timeout = undefined;\n if (0 < nextQueue.size || 0 < waitQueue.size) {\n this.invoke();\n } else {\n state = 'idle';\n }\n }, timeoutMills + SAFE_TIMEOUT_BUFFER);\n\n let index = 0;\n const nextRequestBaseTimeout = timeoutMills / nextQueue.size;\n nextQueue.forEach((func) => {\n setTimeout(() => {\n func();\n // TODO: Add retry\n //.catch(() => waitQueue.set(lane, func));\n }, nextRequestBaseTimeout * index);\n index++;\n });\n nextQueue.clear();\n },\n async invoke() {\n this.shift();\n\n if (state === 'idle') {\n this.handleIdle();\n }\n\n if (state === 'processing') {\n this.handleProcessing();\n }\n },\n };\n }\n}\n"],"mappings":";;;;;;;;;AAGA,IAAIA,eAAe,GAAG,CAAC;AACvB,IAAIC,aAAa,GAAG,IAAI;AACxB,MAAMC,mBAAmB,GAAG,GAAG;AAE/B,SAASC,gBAAgBA,CAAA,EAAG;EAC1B,OAAOC,IAAI,CAACC,MAAM,EAAE,CAACC,QAAQ,CAAC,EAAE,CAAC,CAACC,KAAK,CAAC,CAAC,CAAC;AAC5C;AAEO,MAAMC,eAAe,CAAC;EAI3B,OAAcC,uBAAuBA,CAACC,aAAqB,EAAEC,YAAoB,EAAE;IACjFH,eAAe,CAACI,UAAU,GAAGJ,eAAe,CAACK,MAAM,CAACH,aAAa,EAAEC,YAAY,CAAC;EAClF;EACA,OAAcG,4BAA4BA,CAACJ,aAAqB,EAAEC,YAAoB,EAAE;IACtFH,eAAe,CAACO,eAAe,GAAGP,eAAe,CAACK,MAAM,CAACH,aAAa,EAAEC,YAAY,CAAC;EACvF;EAEA,WAAkBD,aAAaA,CAAA,EAAG;IAChC,OAAOV,eAAe;EACxB;EACA,WAAkBU,aAAaA,CAACM,KAAa,EAAE;IAC7ChB,eAAe,GAAGgB,KAAK;IACvBR,eAAe,CAACI,UAAU,GAAGJ,eAAe,CAACK,MAAM,EAAE;IACrDL,eAAe,CAACO,eAAe,GAAGP,eAAe,CAACK,MAAM,EAAE;EAC5D;EAEA,WAAkBF,YAAYA,CAAA,EAAG;IAC/B,OAAOV,aAAa;EACtB;EACA,WAAkBU,YAAYA,CAACK,KAAa,EAAE;IAC5Cf,aAAa,GAAGe,KAAK;IACrBR,eAAe,CAACI,UAAU,GAAGJ,eAAe,CAACK,MAAM,EAAE;IACrDL,eAAe,CAACO,eAAe,GAAGP,eA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|act|aiff|flac|gsm|m4a|m4b|m4p|tta|wma|mp3|web
|
|
|
58
58
|
exports.audioExtRegex = audioExtRegex;
|
|
59
59
|
const videoExtRegex = /mov|vod|mp4|avi/i;
|
|
60
60
|
exports.videoExtRegex = videoExtRegex;
|
|
61
|
-
|
|
62
61
|
const getFileType = extOrType => {
|
|
63
62
|
if (extOrType.indexOf('/') > -1) {
|
|
64
63
|
const type = extOrType.split('/')[0];
|
|
@@ -67,12 +66,12 @@ const getFileType = extOrType => {
|
|
|
67
66
|
if (type === 'image') return 'image';
|
|
68
67
|
return 'file';
|
|
69
68
|
}
|
|
70
|
-
|
|
71
69
|
if (extOrType.match(imageExtRegex)) return 'image';
|
|
72
70
|
if (extOrType.match(audioExtRegex)) return 'audio';
|
|
73
71
|
if (extOrType.match(videoExtRegex)) return 'video';
|
|
74
72
|
return 'file';
|
|
75
73
|
};
|
|
74
|
+
|
|
76
75
|
/**
|
|
77
76
|
* Calculates the downscaled size of an image while preserving its aspect ratio.
|
|
78
77
|
*
|
|
@@ -84,15 +83,11 @@ const getFileType = extOrType => {
|
|
|
84
83
|
* getDownscaleSize({ width: 1200, height: 800 }, { width: 600 }); // returns { width: 600, height: 400 }
|
|
85
84
|
* ```
|
|
86
85
|
*/
|
|
87
|
-
|
|
88
|
-
|
|
89
86
|
exports.getFileType = getFileType;
|
|
90
|
-
|
|
91
87
|
function getDownscaleSize(origin, resizing) {
|
|
92
88
|
let ratio;
|
|
93
89
|
const maxWidth = resizing.width || origin.width,
|
|
94
|
-
|
|
95
|
-
|
|
90
|
+
maxHeight = resizing.height || origin.height;
|
|
96
91
|
if (origin.width <= maxWidth && origin.height <= maxHeight) {
|
|
97
92
|
ratio = 1;
|
|
98
93
|
} else if (origin.width > maxWidth && origin.height <= maxHeight) {
|
|
@@ -102,12 +97,12 @@ function getDownscaleSize(origin, resizing) {
|
|
|
102
97
|
} else {
|
|
103
98
|
ratio = Math.min(maxWidth / origin.width, maxHeight / origin.height);
|
|
104
99
|
}
|
|
105
|
-
|
|
106
100
|
return {
|
|
107
101
|
width: origin.width * ratio,
|
|
108
102
|
height: origin.height * ratio
|
|
109
103
|
};
|
|
110
104
|
}
|
|
105
|
+
|
|
111
106
|
/**
|
|
112
107
|
* Normalize a file name by ensuring it has the given extension, if it doesn't already.
|
|
113
108
|
*
|
|
@@ -115,22 +110,18 @@ function getDownscaleSize(origin, resizing) {
|
|
|
115
110
|
* @param {string} extension - The desired extension, without a leading period.
|
|
116
111
|
* @returns {string} - The normalized file name, with the extension.
|
|
117
112
|
*/
|
|
118
|
-
|
|
119
|
-
|
|
120
113
|
function normalizeFileName(fileName, extension) {
|
|
121
|
-
if (!extension) return fileName;
|
|
114
|
+
if (!extension) return fileName;
|
|
122
115
|
|
|
116
|
+
// .extension
|
|
123
117
|
let _extension = extension.toLowerCase();
|
|
124
|
-
|
|
125
118
|
if (_extension.indexOf('.') !== 0) {
|
|
126
119
|
_extension = '.' + _extension;
|
|
127
|
-
}
|
|
128
|
-
|
|
120
|
+
}
|
|
129
121
|
|
|
122
|
+
// filename.extension | filename
|
|
130
123
|
const _filename = fileName.toLowerCase();
|
|
131
|
-
|
|
132
124
|
const hasExtension = _filename.lastIndexOf(_extension) === _filename.length - _extension.length;
|
|
133
|
-
|
|
134
125
|
if (!hasExtension) {
|
|
135
126
|
// filename.extension
|
|
136
127
|
return fileName + _extension;
|
|
@@ -139,6 +130,7 @@ function normalizeFileName(fileName, extension) {
|
|
|
139
130
|
return fileName;
|
|
140
131
|
}
|
|
141
132
|
}
|
|
133
|
+
|
|
142
134
|
/**
|
|
143
135
|
* Parses a MIME type string into its components.
|
|
144
136
|
*
|
|
@@ -146,31 +138,27 @@ function normalizeFileName(fileName, extension) {
|
|
|
146
138
|
* @returns An object containing the type, subtype, and parameters of the MIME type.
|
|
147
139
|
*/
|
|
148
140
|
|
|
149
|
-
|
|
150
141
|
function parseMimeType(mimeType) {
|
|
151
142
|
const [fullType, ...parts] = mimeType.split(';');
|
|
152
143
|
const [type, subtype] = fullType.split('/');
|
|
153
144
|
const parameters = {};
|
|
154
|
-
|
|
155
145
|
for (const part of parts) {
|
|
156
146
|
const [name, value] = part.trim().split('=');
|
|
157
147
|
parameters[name] = value;
|
|
158
148
|
}
|
|
159
|
-
|
|
160
149
|
return {
|
|
161
150
|
type,
|
|
162
151
|
subtype,
|
|
163
152
|
parameters
|
|
164
153
|
};
|
|
165
154
|
}
|
|
155
|
+
|
|
166
156
|
/**
|
|
167
157
|
* Returns the file extension based on the MIME type.
|
|
168
158
|
*
|
|
169
159
|
* @param {string | null | undefined} mimeType - The MIME type to look up.
|
|
170
160
|
* @returns {string} - The file extension for the given MIME type, or an empty string if no matching file extension was found.
|
|
171
161
|
*/
|
|
172
|
-
|
|
173
|
-
|
|
174
162
|
function getFileExtensionFromMime(mimeType) {
|
|
175
163
|
if (!mimeType) return '';
|
|
176
164
|
const MIME_EXTENSION_MAP = Object.entries(EXTENSION_MIME_MAP).reduce((acc, _ref) => {
|
|
@@ -180,26 +168,24 @@ function getFileExtensionFromMime(mimeType) {
|
|
|
180
168
|
}, {});
|
|
181
169
|
return MIME_EXTENSION_MAP[mimeType.toLowerCase()] || '';
|
|
182
170
|
}
|
|
171
|
+
|
|
183
172
|
/**
|
|
184
173
|
* Returns the MIME type based on the file extension.
|
|
185
174
|
*
|
|
186
175
|
* @param {string | null | undefined} ext - The file extension to look up.
|
|
187
176
|
* @returns {string} - The MIME type for the given file extension, or an empty string if no matching MIME type was found.
|
|
188
177
|
*/
|
|
189
|
-
|
|
190
|
-
|
|
191
178
|
function getMimeFromFileExtension(ext) {
|
|
192
179
|
if (!ext) return '';
|
|
193
180
|
return EXTENSION_MIME_MAP[ext.toLowerCase()] || '';
|
|
194
181
|
}
|
|
182
|
+
|
|
195
183
|
/**
|
|
196
184
|
* Returns the file extension of a file path.
|
|
197
185
|
*
|
|
198
186
|
* @param {string} filePath - The file path to extract the extension from.
|
|
199
187
|
* @returns {string} - The file extension, or an empty string if the file path does not have an extension.
|
|
200
188
|
*/
|
|
201
|
-
|
|
202
|
-
|
|
203
189
|
function getFileExtension(filePath) {
|
|
204
190
|
const pathWithoutParams = filePath.split('?')[0];
|
|
205
191
|
const idx = pathWithoutParams.lastIndexOf('.');
|
|
@@ -207,16 +193,13 @@ function getFileExtension(filePath) {
|
|
|
207
193
|
const result = pathWithoutParams.slice(idx - pathWithoutParams.length).toLowerCase();
|
|
208
194
|
if (result === '.') return '';else return result;
|
|
209
195
|
}
|
|
210
|
-
|
|
211
196
|
function getFileExtensionFromUri(uri) {
|
|
212
197
|
return fetch(uri).then(response => response.headers.get('content-type'));
|
|
213
198
|
}
|
|
214
|
-
|
|
215
199
|
function isImage(filePath, mimeType) {
|
|
216
200
|
const type = getFileType(mimeType || getFileExtension(filePath));
|
|
217
201
|
return type === 'image';
|
|
218
202
|
}
|
|
219
|
-
|
|
220
203
|
function shouldCompressImage(mime) {
|
|
221
204
|
let compressionEnabled = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
|
|
222
205
|
const extension = getFileExtensionFromMime(mime);
|