xshell 1.2.12 → 1.2.15

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/server.d.ts CHANGED
@@ -136,6 +136,7 @@ export declare class Server {
136
136
  sea?: boolean;
137
137
  log_404?: boolean;
138
138
  }): Promise<boolean>;
139
+ log_404(ctx: Context): void;
139
140
  /** 将 body 设置为 fp 所指文件的 ReadStream
140
141
  检查 fp 是否合法,设置对应的 content-type,支持缓存,支持分块下载
141
142
  - ctx: Koa.Context
package/server.js CHANGED
@@ -572,7 +572,7 @@ export class Server {
572
572
  return
573
573
  } */
574
574
  async try_send(ctx, fp, { fpd_root, assets_root, sea: _sea = sea, log_404 = true, } = {}) {
575
- const { request: { _path, path, method }, response, } = ctx;
575
+ const { request: { method }, response, } = ctx;
576
576
  if (response.body !== undefined || response.status !== 404)
577
577
  return true;
578
578
  if (method !== 'HEAD' && method !== 'GET')
@@ -584,15 +584,15 @@ export class Server {
584
584
  catch (error) {
585
585
  if (error.status !== 404)
586
586
  throw error;
587
- if (log_404 && this.print.logs) {
588
- let s = `${' '.repeat(11)} ${method.toLowerCase()} 404: ${path}`;
589
- if (_path !== path)
590
- s += ` ${_path.bracket()}`;
591
- console.log(s.red);
592
- }
587
+ if (log_404 && this.print.logs)
588
+ this.log_404(ctx);
593
589
  return false;
594
590
  }
595
591
  }
592
+ log_404(ctx) {
593
+ const { request: { _path, path, method } } = ctx;
594
+ console.log(`${' '.repeat(15)}${method.toLowerCase()} 404: ${path}${_path === path ? '' : ` ${_path.bracket()}`}`.red);
595
+ }
596
596
  /** 将 body 设置为 fp 所指文件的 ReadStream
597
597
  检查 fp 是否合法,设置对应的 content-type,支持缓存,支持分块下载
598
598
  - ctx: Koa.Context
@@ -133,3 +133,7 @@ export declare class Timer {
133
133
  export declare function lowercase_first_letter(str: string): string;
134
134
  /** 大于 n 的最小的 2 的幂次 */
135
135
  export declare function ceil2(n: number): number;
136
+ /** 节流,最多只在时间间隔末尾调用一次,首次调用也延后 */
137
+ export declare function throttle(duration: number, func: Function, delay_first?: boolean): (this: any, ...args: any[]) => void;
138
+ /** 防抖,间隔一段时间不再触发时调用 */
139
+ export declare function debounce(duration: number, func: Function): (this: any, ...args: any[]) => void;
package/utils.browser.js CHANGED
@@ -211,9 +211,6 @@ export class Lock {
211
211
  reject(error);
212
212
  }
213
213
  // 下面开始释放锁
214
- // assert(this.locked)
215
- if (!this.locked)
216
- reject(new Error(t('尝试释放未锁定的锁,这不应该发生')));
217
214
  this.locked = false;
218
215
  pcurrent.resolve();
219
216
  });
@@ -419,4 +416,49 @@ export function ceil2(n) {
419
416
  ;
420
417
  return power;
421
418
  }
419
+ /** 节流,最多只在时间间隔末尾调用一次,首次调用也延后 */
420
+ export function throttle(duration, func, delay_first = false) {
421
+ let timeout = 0;
422
+ let last = 0;
423
+ let saved_this = null;
424
+ let saved_args = null;
425
+ return function throttled(...args) {
426
+ // 当前时间间隔已预定执行,本次调用仅更新调用参数
427
+ if (timeout) {
428
+ saved_this = this;
429
+ saved_args = args;
430
+ return;
431
+ }
432
+ const now = Date.now();
433
+ if (last === 0 && delay_first)
434
+ last = now;
435
+ const ellapsed = now - last;
436
+ // 过了时间间隔末尾,直接执行
437
+ if (ellapsed >= duration) {
438
+ last = now;
439
+ func.apply(this, args);
440
+ }
441
+ else { // 预定在间隔末尾执行
442
+ saved_this = this;
443
+ saved_args = args;
444
+ timeout = setTimeout(() => {
445
+ timeout = null;
446
+ last = Date.now();
447
+ func.apply(saved_this, saved_args);
448
+ saved_this = null;
449
+ saved_args = null;
450
+ }, duration - ellapsed);
451
+ }
452
+ };
453
+ }
454
+ /** 防抖,间隔一段时间不再触发时调用 */
455
+ export function debounce(duration, func) {
456
+ let timeout = null;
457
+ return function debounced(...args) {
458
+ clearTimeout(timeout);
459
+ timeout = setTimeout(() => {
460
+ func.apply(this, args);
461
+ }, duration);
462
+ };
463
+ }
422
464
  //# sourceMappingURL=utils.browser.js.map
package/utils.d.ts CHANGED
@@ -210,3 +210,7 @@ export declare function consume_stream(stream: Readable, ignore_error?: boolean)
210
210
  export declare function range_to_numbers(range: string, reverse?: boolean): Generator<number, void, unknown>;
211
211
  /** 大于 n 的最小的 2 的幂次 */
212
212
  export declare function ceil2(n: number): number;
213
+ /** 节流,最多只在时间间隔末尾调用一次,首次调用也延后 */
214
+ export declare function throttle(duration: number, func: Function, delay_first?: boolean): (this: any, ...args: any[]) => void;
215
+ /** 防抖,间隔一段时间不再触发时调用 */
216
+ export declare function debounce(duration: number, func: Function): (this: any, ...args: any[]) => void;
package/utils.js CHANGED
@@ -415,9 +415,6 @@ export class Lock {
415
415
  reject(error);
416
416
  }
417
417
  // 下面开始释放锁
418
- // assert(this.locked)
419
- if (!this.locked)
420
- reject(new Error(t('尝试释放未锁定的锁,这不应该发生')));
421
418
  this.locked = false;
422
419
  pcurrent.resolve();
423
420
  });
@@ -695,4 +692,49 @@ export function ceil2(n) {
695
692
  ;
696
693
  return power;
697
694
  }
695
+ /** 节流,最多只在时间间隔末尾调用一次,首次调用也延后 */
696
+ export function throttle(duration, func, delay_first = false) {
697
+ let timeout = null;
698
+ let last = 0;
699
+ let saved_this = null;
700
+ let saved_args = null;
701
+ return function throttled(...args) {
702
+ // 当前时间间隔已预定执行,本次调用仅更新调用参数
703
+ if (timeout) {
704
+ saved_this = this;
705
+ saved_args = args;
706
+ return;
707
+ }
708
+ const now = Date.now();
709
+ if (last === 0 && delay_first)
710
+ last = now;
711
+ const ellapsed = now - last;
712
+ // 过了时间间隔末尾,直接执行
713
+ if (ellapsed >= duration) {
714
+ last = now;
715
+ func.apply(this, args);
716
+ }
717
+ else { // 预定在间隔末尾执行
718
+ saved_this = this;
719
+ saved_args = args;
720
+ timeout = setTimeout(() => {
721
+ timeout = null;
722
+ last = Date.now();
723
+ func.apply(saved_this, saved_args);
724
+ saved_this = null;
725
+ saved_args = null;
726
+ }, duration - ellapsed);
727
+ }
728
+ };
729
+ }
730
+ /** 防抖,间隔一段时间不再触发时调用 */
731
+ export function debounce(duration, func) {
732
+ let timeout = null;
733
+ return function debounced(...args) {
734
+ clearTimeout(timeout);
735
+ timeout = setTimeout(() => {
736
+ func.apply(this, args);
737
+ }, duration);
738
+ };
739
+ }
698
740
  //# sourceMappingURL=utils.js.map
File without changes