@lvce-editor/auth-worker 1.1.0 → 1.2.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.
@@ -1029,6 +1029,57 @@ const getBackendRefreshUrl = backendUrl => {
1029
1029
  return getBackendAuthUrl(backendUrl, '/auth/refresh');
1030
1030
  };
1031
1031
 
1032
+ const delay = async ms => {
1033
+ await new Promise(resolve => setTimeout(resolve, ms));
1034
+ };
1035
+
1036
+ let nextLoginResponse;
1037
+ let nextRefreshResponse;
1038
+ const setNextLoginResponse = response => {
1039
+ nextLoginResponse = response;
1040
+ };
1041
+ const setNextRefreshResponse = response => {
1042
+ nextRefreshResponse = response;
1043
+ };
1044
+ const clear = () => {
1045
+ nextLoginResponse = undefined;
1046
+ nextRefreshResponse = undefined;
1047
+ };
1048
+ const hasPendingMockLoginResponse = () => {
1049
+ return !!nextLoginResponse;
1050
+ };
1051
+ const hasPendingMockRefreshResponse = () => {
1052
+ return !!nextRefreshResponse;
1053
+ };
1054
+ const consumeNextLoginResponse = async () => {
1055
+ if (!nextLoginResponse) {
1056
+ return undefined;
1057
+ }
1058
+ const response = nextLoginResponse;
1059
+ nextLoginResponse = undefined;
1060
+ if (response.delay > 0) {
1061
+ await delay(response.delay);
1062
+ }
1063
+ if (response.type === 'error') {
1064
+ throw new Error(response.message);
1065
+ }
1066
+ return response.response;
1067
+ };
1068
+ const consumeNextRefreshResponse = async () => {
1069
+ if (!nextRefreshResponse) {
1070
+ return undefined;
1071
+ }
1072
+ const response = nextRefreshResponse;
1073
+ nextRefreshResponse = undefined;
1074
+ if (response.delay > 0) {
1075
+ await new Promise(resolve => setTimeout(resolve, response.delay));
1076
+ }
1077
+ if (response.type === 'error') {
1078
+ throw new Error(response.message);
1079
+ }
1080
+ return response.response;
1081
+ };
1082
+
1032
1083
  const isObject = value => {
1033
1084
  return !!value && typeof value === 'object';
1034
1085
  };
@@ -1068,6 +1119,10 @@ const syncBackendAuth = async backendUrl => {
1068
1119
  return getLoggedOutBackendAuthState('Backend URL is missing.');
1069
1120
  }
1070
1121
  try {
1122
+ if (hasPendingMockRefreshResponse()) {
1123
+ const mockResponse = await consumeNextRefreshResponse();
1124
+ return parseBackendAuthResponse(mockResponse);
1125
+ }
1071
1126
  const response = await fetch(getBackendRefreshUrl(backendUrl), {
1072
1127
  credentials: 'include',
1073
1128
  headers: {
@@ -1102,10 +1157,6 @@ const syncBackendAuth = async backendUrl => {
1102
1157
  }
1103
1158
  };
1104
1159
 
1105
- const delay = async ms => {
1106
- await new Promise(resolve => setTimeout(resolve, ms));
1107
- };
1108
-
1109
1160
  const waitForBackendLogin = async (backendUrl, timeoutMs = 30_000, pollIntervalMs = 1000) => {
1110
1161
  const deadline = Date.now() + timeoutMs;
1111
1162
  let lastErrorMessage = '';
@@ -1142,28 +1193,6 @@ const isLoginResponse = value => {
1142
1193
  return true;
1143
1194
  };
1144
1195
 
1145
- let nextLoginResponse;
1146
- const clear = () => {
1147
- nextLoginResponse = undefined;
1148
- };
1149
- const hasPendingMockLoginResponse = () => {
1150
- return !!nextLoginResponse;
1151
- };
1152
- const consumeNextLoginResponse = async () => {
1153
- if (!nextLoginResponse) {
1154
- return undefined;
1155
- }
1156
- const response = nextLoginResponse;
1157
- nextLoginResponse = undefined;
1158
- if (response.delay > 0) {
1159
- await delay(response.delay);
1160
- }
1161
- if (response.type === 'error') {
1162
- throw new Error(response.message);
1163
- }
1164
- return response.response;
1165
- };
1166
-
1167
1196
  const handleClickLogin = async state => {
1168
1197
  if (!state.backendUrl) {
1169
1198
  return {
@@ -1225,9 +1254,16 @@ const logout = async state => {
1225
1254
 
1226
1255
  const commandMap = {
1227
1256
  'Auth.clearMocks': clear,
1257
+ 'Auth.consumeNextLoginResponse': consumeNextLoginResponse,
1258
+ 'Auth.consumeNextRefreshResponse': consumeNextRefreshResponse,
1259
+ 'Auth.hasPendingMockLoginResponse': hasPendingMockLoginResponse,
1260
+ 'Auth.hasPendingMockRefreshResponse': hasPendingMockRefreshResponse,
1228
1261
  'Auth.initialize': initialize,
1229
1262
  'Auth.login': handleClickLogin,
1230
1263
  'Auth.logout': logout,
1264
+ 'Auth.setNextLoginResponse': setNextLoginResponse,
1265
+ 'Auth.setNextRefreshResponse': setNextRefreshResponse,
1266
+ 'Auth.syncBackendAuth': syncBackendAuth,
1231
1267
  'HandleMessagePort.handleMessagePort': handleMessagePort
1232
1268
  };
1233
1269
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/auth-worker",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Auth Worker",
5
5
  "repository": {
6
6
  "type": "git",