@rspack/dev-server 1.2.1 → 2.0.0-beta.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.
Files changed (47) hide show
  1. package/README.md +17 -17
  2. package/client/clients/WebSocketClient.d.ts +17 -0
  3. package/client/clients/WebSocketClient.js +28 -28
  4. package/client/index.d.ts +17 -0
  5. package/client/index.js +224 -363
  6. package/client/modules/logger/Logger.d.ts +40 -0
  7. package/client/modules/logger/Logger.js +123 -0
  8. package/client/modules/logger/createConsoleLogger.d.ts +12 -0
  9. package/client/modules/logger/createConsoleLogger.js +119 -0
  10. package/client/modules/logger/index.d.ts +18 -0
  11. package/client/modules/logger/index.js +20 -712
  12. package/client/modules/types.d.ts +45 -0
  13. package/client/modules/types.js +17 -0
  14. package/client/overlay.d.ts +44 -0
  15. package/client/overlay.js +243 -292
  16. package/client/progress.d.ts +11 -0
  17. package/client/progress.js +178 -111
  18. package/client/socket.d.ts +15 -0
  19. package/client/socket.js +19 -46
  20. package/client/utils/ansiHTML.d.ts +30 -0
  21. package/client/utils/ansiHTML.js +98 -145
  22. package/client/utils/log.d.ts +13 -0
  23. package/client/utils/log.js +7 -17
  24. package/{dist/servers/SockJSServer.d.ts → client/utils/sendMessage.d.ts} +2 -1
  25. package/client/utils/sendMessage.js +6 -15
  26. package/dist/0~launch-editor.js +618 -0
  27. package/dist/0~open.js +547 -0
  28. package/dist/0~p-retry.js +158 -0
  29. package/dist/131.js +1398 -0
  30. package/dist/config.d.ts +1 -3
  31. package/dist/getPort.d.ts +4 -1
  32. package/dist/index.js +1 -5
  33. package/dist/rslib-runtime.js +66 -0
  34. package/dist/server.d.ts +7 -18
  35. package/dist/servers/WebsocketServer.d.ts +8 -1
  36. package/dist/types.d.ts +14 -29
  37. package/package.json +74 -103
  38. package/client/clients/SockJSClient.js +0 -34
  39. package/client/modules/sockjs-client/index.js +0 -4506
  40. package/dist/config.js +0 -2
  41. package/dist/getPort.js +0 -131
  42. package/dist/options.json +0 -1034
  43. package/dist/server.js +0 -2222
  44. package/dist/servers/BaseServer.js +0 -20
  45. package/dist/servers/SockJSServer.js +0 -110
  46. package/dist/servers/WebsocketServer.js +0 -72
  47. package/dist/types.js +0 -5
@@ -0,0 +1,158 @@
1
+ const objectToString = Object.prototype.toString;
2
+ const isError = (value)=>'[object Error]' === objectToString.call(value);
3
+ const errorMessages = new Set([
4
+ 'network error',
5
+ 'Failed to fetch',
6
+ 'NetworkError when attempting to fetch resource.',
7
+ 'The Internet connection appears to be offline.',
8
+ 'Load failed',
9
+ 'Network request failed',
10
+ 'fetch failed',
11
+ 'terminated'
12
+ ]);
13
+ function isNetworkError(error) {
14
+ const isValid = error && isError(error) && 'TypeError' === error.name && 'string' == typeof error.message;
15
+ if (!isValid) return false;
16
+ if ('Load failed' === error.message) return void 0 === error.stack;
17
+ return errorMessages.has(error.message);
18
+ }
19
+ function validateRetries(retries) {
20
+ if ('number' == typeof retries) {
21
+ if (retries < 0) throw new TypeError('Expected `retries` to be a non-negative number.');
22
+ if (Number.isNaN(retries)) throw new TypeError('Expected `retries` to be a valid number or Infinity, got NaN.');
23
+ } else if (void 0 !== retries) throw new TypeError('Expected `retries` to be a number or Infinity.');
24
+ }
25
+ function validateNumberOption(name, value, { min = 0, allowInfinity = false } = {}) {
26
+ if (void 0 === value) return;
27
+ if ('number' != typeof value || Number.isNaN(value)) throw new TypeError(`Expected \`${name}\` to be a number${allowInfinity ? ' or Infinity' : ''}.`);
28
+ if (!allowInfinity && !Number.isFinite(value)) throw new TypeError(`Expected \`${name}\` to be a finite number.`);
29
+ if (value < min) throw new TypeError(`Expected \`${name}\` to be \u2265 ${min}.`);
30
+ }
31
+ class AbortError extends Error {
32
+ constructor(message){
33
+ super();
34
+ if (message instanceof Error) {
35
+ this.originalError = message;
36
+ ({ message } = message);
37
+ } else {
38
+ this.originalError = new Error(message);
39
+ this.originalError.stack = this.stack;
40
+ }
41
+ this.name = 'AbortError';
42
+ this.message = message;
43
+ }
44
+ }
45
+ function calculateDelay(retriesConsumed, options) {
46
+ const attempt = Math.max(1, retriesConsumed + 1);
47
+ const random = options.randomize ? Math.random() + 1 : 1;
48
+ let timeout = Math.round(random * options.minTimeout * options.factor ** (attempt - 1));
49
+ timeout = Math.min(timeout, options.maxTimeout);
50
+ return timeout;
51
+ }
52
+ function calculateRemainingTime(start, max) {
53
+ if (!Number.isFinite(max)) return max;
54
+ return max - (performance.now() - start);
55
+ }
56
+ async function onAttemptFailure({ error, attemptNumber, retriesConsumed, startTime, options }) {
57
+ const normalizedError = error instanceof Error ? error : new TypeError(`Non-error was thrown: "${error}". You should only throw errors.`);
58
+ if (normalizedError instanceof AbortError) throw normalizedError.originalError;
59
+ const retriesLeft = Number.isFinite(options.retries) ? Math.max(0, options.retries - retriesConsumed) : options.retries;
60
+ const maxRetryTime = options.maxRetryTime ?? 1 / 0;
61
+ const context = Object.freeze({
62
+ error: normalizedError,
63
+ attemptNumber,
64
+ retriesLeft,
65
+ retriesConsumed
66
+ });
67
+ await options.onFailedAttempt(context);
68
+ if (calculateRemainingTime(startTime, maxRetryTime) <= 0) throw normalizedError;
69
+ const consumeRetry = await options.shouldConsumeRetry(context);
70
+ const remainingTime = calculateRemainingTime(startTime, maxRetryTime);
71
+ if (remainingTime <= 0 || retriesLeft <= 0) throw normalizedError;
72
+ if (normalizedError instanceof TypeError && !isNetworkError(normalizedError)) {
73
+ if (consumeRetry) throw normalizedError;
74
+ options.signal?.throwIfAborted();
75
+ return false;
76
+ }
77
+ if (!await options.shouldRetry(context)) throw normalizedError;
78
+ if (!consumeRetry) {
79
+ options.signal?.throwIfAborted();
80
+ return false;
81
+ }
82
+ const delayTime = calculateDelay(retriesConsumed, options);
83
+ const finalDelay = Math.min(delayTime, remainingTime);
84
+ options.signal?.throwIfAborted();
85
+ if (finalDelay > 0) await new Promise((resolve, reject)=>{
86
+ const onAbort = ()=>{
87
+ clearTimeout(timeoutToken);
88
+ options.signal?.removeEventListener('abort', onAbort);
89
+ reject(options.signal.reason);
90
+ };
91
+ const timeoutToken = setTimeout(()=>{
92
+ options.signal?.removeEventListener('abort', onAbort);
93
+ resolve();
94
+ }, finalDelay);
95
+ if (options.unref) timeoutToken.unref?.();
96
+ options.signal?.addEventListener('abort', onAbort, {
97
+ once: true
98
+ });
99
+ });
100
+ options.signal?.throwIfAborted();
101
+ return true;
102
+ }
103
+ async function pRetry(input, options = {}) {
104
+ options = {
105
+ ...options
106
+ };
107
+ validateRetries(options.retries);
108
+ if (Object.hasOwn(options, 'forever')) throw new Error('The `forever` option is no longer supported. For many use-cases, you can set `retries: Infinity` instead.');
109
+ options.retries ??= 10;
110
+ options.factor ??= 2;
111
+ options.minTimeout ??= 1000;
112
+ options.maxTimeout ??= 1 / 0;
113
+ options.maxRetryTime ??= 1 / 0;
114
+ options.randomize ??= false;
115
+ options.onFailedAttempt ??= ()=>{};
116
+ options.shouldRetry ??= ()=>true;
117
+ options.shouldConsumeRetry ??= ()=>true;
118
+ validateNumberOption('factor', options.factor, {
119
+ min: 0,
120
+ allowInfinity: false
121
+ });
122
+ validateNumberOption('minTimeout', options.minTimeout, {
123
+ min: 0,
124
+ allowInfinity: false
125
+ });
126
+ validateNumberOption('maxTimeout', options.maxTimeout, {
127
+ min: 0,
128
+ allowInfinity: true
129
+ });
130
+ validateNumberOption('maxRetryTime', options.maxRetryTime, {
131
+ min: 0,
132
+ allowInfinity: true
133
+ });
134
+ if (!(options.factor > 0)) options.factor = 1;
135
+ options.signal?.throwIfAborted();
136
+ let attemptNumber = 0;
137
+ let retriesConsumed = 0;
138
+ const startTime = performance.now();
139
+ while(Number.isFinite(options.retries) ? retriesConsumed <= options.retries : true){
140
+ attemptNumber++;
141
+ try {
142
+ options.signal?.throwIfAborted();
143
+ const result = await input(attemptNumber);
144
+ options.signal?.throwIfAborted();
145
+ return result;
146
+ } catch (error) {
147
+ if (await onAttemptFailure({
148
+ error,
149
+ attemptNumber,
150
+ retriesConsumed,
151
+ startTime,
152
+ options
153
+ })) retriesConsumed++;
154
+ }
155
+ }
156
+ throw new Error('Retry attempts exhausted without throwing an error.');
157
+ }
158
+ export { pRetry };