@eggjs/watcher 1.0.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.
Files changed (76) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +147 -0
  3. package/dist/commonjs/agent.d.ts +2 -0
  4. package/dist/commonjs/agent.js +5 -0
  5. package/dist/commonjs/app.d.ts +2 -0
  6. package/dist/commonjs/app.js +5 -0
  7. package/dist/commonjs/config/config.default.d.ts +15 -0
  8. package/dist/commonjs/config/config.default.js +22 -0
  9. package/dist/commonjs/config/config.local.d.ts +6 -0
  10. package/dist/commonjs/config/config.local.js +8 -0
  11. package/dist/commonjs/config/config.unittest.d.ts +6 -0
  12. package/dist/commonjs/config/config.unittest.js +8 -0
  13. package/dist/commonjs/index.d.ts +3 -0
  14. package/dist/commonjs/index.js +20 -0
  15. package/dist/commonjs/lib/boot.d.ts +6 -0
  16. package/dist/commonjs/lib/boot.js +23 -0
  17. package/dist/commonjs/lib/event-sources/base.d.ts +5 -0
  18. package/dist/commonjs/lib/event-sources/base.js +8 -0
  19. package/dist/commonjs/lib/event-sources/default.d.ts +6 -0
  20. package/dist/commonjs/lib/event-sources/default.js +19 -0
  21. package/dist/commonjs/lib/event-sources/development.d.ts +7 -0
  22. package/dist/commonjs/lib/event-sources/development.js +101 -0
  23. package/dist/commonjs/lib/event-sources/index.d.ts +3 -0
  24. package/dist/commonjs/lib/event-sources/index.js +20 -0
  25. package/dist/commonjs/lib/types.d.ts +33 -0
  26. package/dist/commonjs/lib/types.js +3 -0
  27. package/dist/commonjs/lib/utils.d.ts +2 -0
  28. package/dist/commonjs/lib/utils.js +25 -0
  29. package/dist/commonjs/lib/watcher.d.ts +9 -0
  30. package/dist/commonjs/lib/watcher.js +111 -0
  31. package/dist/commonjs/package.json +3 -0
  32. package/dist/esm/agent.d.ts +2 -0
  33. package/dist/esm/agent.js +3 -0
  34. package/dist/esm/app.d.ts +2 -0
  35. package/dist/esm/app.js +3 -0
  36. package/dist/esm/config/config.default.d.ts +15 -0
  37. package/dist/esm/config/config.default.js +17 -0
  38. package/dist/esm/config/config.local.d.ts +6 -0
  39. package/dist/esm/config/config.local.js +6 -0
  40. package/dist/esm/config/config.unittest.d.ts +6 -0
  41. package/dist/esm/config/config.unittest.js +6 -0
  42. package/dist/esm/index.d.ts +3 -0
  43. package/dist/esm/index.js +4 -0
  44. package/dist/esm/lib/boot.d.ts +6 -0
  45. package/dist/esm/lib/boot.js +19 -0
  46. package/dist/esm/lib/event-sources/base.d.ts +5 -0
  47. package/dist/esm/lib/event-sources/base.js +4 -0
  48. package/dist/esm/lib/event-sources/default.d.ts +6 -0
  49. package/dist/esm/lib/event-sources/default.js +16 -0
  50. package/dist/esm/lib/event-sources/development.d.ts +7 -0
  51. package/dist/esm/lib/event-sources/development.js +95 -0
  52. package/dist/esm/lib/event-sources/index.d.ts +3 -0
  53. package/dist/esm/lib/event-sources/index.js +4 -0
  54. package/dist/esm/lib/types.d.ts +33 -0
  55. package/dist/esm/lib/types.js +2 -0
  56. package/dist/esm/lib/utils.d.ts +2 -0
  57. package/dist/esm/lib/utils.js +18 -0
  58. package/dist/esm/lib/watcher.d.ts +9 -0
  59. package/dist/esm/lib/watcher.js +104 -0
  60. package/dist/esm/package.json +3 -0
  61. package/dist/package.json +4 -0
  62. package/package.json +83 -0
  63. package/src/agent.ts +3 -0
  64. package/src/app.ts +3 -0
  65. package/src/config/config.default.ts +17 -0
  66. package/src/config/config.local.ts +5 -0
  67. package/src/config/config.unittest.ts +5 -0
  68. package/src/index.ts +3 -0
  69. package/src/lib/boot.ts +22 -0
  70. package/src/lib/event-sources/base.ts +6 -0
  71. package/src/lib/event-sources/default.ts +18 -0
  72. package/src/lib/event-sources/development.ts +99 -0
  73. package/src/lib/event-sources/index.ts +3 -0
  74. package/src/lib/types.ts +35 -0
  75. package/src/lib/utils.ts +20 -0
  76. package/src/lib/watcher.ts +117 -0
package/package.json ADDED
@@ -0,0 +1,83 @@
1
+ {
2
+ "name": "@eggjs/watcher",
3
+ "version": "1.0.0",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "description": "file watcher plugin for egg",
8
+ "eggPlugin": {
9
+ "name": "watcher",
10
+ "exports": {
11
+ "import": "./dist/esm",
12
+ "require": "./dist/commonjs"
13
+ }
14
+ },
15
+ "scripts": {
16
+ "lint": "eslint --cache src test --ext .ts",
17
+ "pretest": "npm run lint -- --fix && npm run prepublishOnly",
18
+ "test": "egg-bin test",
19
+ "preci": "npm run lint && npm run prepublishOnly && attw --pack",
20
+ "ci": "egg-bin cov",
21
+ "prepublishOnly": "tshy && tshy-after"
22
+ },
23
+ "homepage": "https://github.com/eggjs/egg-watcher",
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git@github.com:eggjs/egg-watcher.git"
27
+ },
28
+ "keywords": [
29
+ "egg-watcher",
30
+ "egg",
31
+ "watcher",
32
+ "watch"
33
+ ],
34
+ "engines": {
35
+ "node": ">= 18.19.0"
36
+ },
37
+ "dependencies": {
38
+ "@eggjs/utils": "^4.0.3",
39
+ "camelcase": "^5.0.0",
40
+ "sdk-base": "^5.0.0"
41
+ },
42
+ "devDependencies": {
43
+ "@arethetypeswrong/cli": "^0.17.1",
44
+ "@eggjs/tsconfig": "1",
45
+ "@types/node": "22",
46
+ "@types/mocha": "10",
47
+ "egg": "beta",
48
+ "egg-bin": "beta",
49
+ "egg-mock": "beta",
50
+ "eslint": "8",
51
+ "eslint-config-egg": "14",
52
+ "tshy": "3",
53
+ "tshy-after": "1",
54
+ "typescript": "5"
55
+ },
56
+ "type": "module",
57
+ "tshy": {
58
+ "exports": {
59
+ ".": "./src/index.ts",
60
+ "./package.json": "./package.json"
61
+ }
62
+ },
63
+ "exports": {
64
+ ".": {
65
+ "import": {
66
+ "types": "./dist/esm/index.d.ts",
67
+ "default": "./dist/esm/index.js"
68
+ },
69
+ "require": {
70
+ "types": "./dist/commonjs/index.d.ts",
71
+ "default": "./dist/commonjs/index.js"
72
+ }
73
+ },
74
+ "./package.json": "./package.json"
75
+ },
76
+ "files": [
77
+ "dist",
78
+ "src"
79
+ ],
80
+ "types": "./dist/commonjs/index.d.ts",
81
+ "main": "./dist/commonjs/index.js",
82
+ "module": "./dist/esm/index.js"
83
+ }
package/src/agent.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { Boot } from './lib/boot.js';
2
+
3
+ export default Boot;
package/src/app.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { Boot } from './lib/boot.js';
2
+
3
+ export default Boot;
@@ -0,0 +1,17 @@
1
+ import path from 'node:path';
2
+ import { getSourceDirname } from '../lib/utils.js';
3
+
4
+ export default {
5
+ /**
6
+ * watcher options
7
+ * @member Config#watcher
8
+ * @property {string} type - event source type
9
+ */
10
+ watcher: {
11
+ type: 'default', // default event source
12
+ eventSources: {
13
+ default: path.join(getSourceDirname(), 'lib', 'event-sources', 'default'),
14
+ development: path.join(getSourceDirname(), 'lib', 'event-sources', 'development'),
15
+ },
16
+ },
17
+ };
@@ -0,0 +1,5 @@
1
+ export default {
2
+ watcher: {
3
+ type: 'development',
4
+ },
5
+ };
@@ -0,0 +1,5 @@
1
+ export default {
2
+ watcher: {
3
+ type: 'development',
4
+ },
5
+ };
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './lib/types.js';
2
+ export * from './lib/watcher.js';
3
+ export * from './lib/event-sources/index.js';
@@ -0,0 +1,22 @@
1
+ import type { Application, Agent, ILifecycleBoot } from 'egg';
2
+ import { Watcher } from './watcher.js';
3
+
4
+ export class Boot implements ILifecycleBoot {
5
+ #app: Application | Agent;
6
+ #watcher: Watcher;
7
+
8
+ constructor(appOrAgent: Application | Agent) {
9
+ this.#app = appOrAgent;
10
+ this.#watcher = this.#app.watcher = this.#app.cluster(Watcher, {})
11
+ .delegate('watch', 'subscribe')
12
+ .create(appOrAgent.config)
13
+ .on('info', (msg: string, ...args: any[]) => this.#app.coreLogger.info(msg, ...args))
14
+ .on('warn', (msg: string, ...args: any[]) => this.#app.coreLogger.warn(msg, ...args))
15
+ .on('error', (msg: string, ...args: any[]) => this.#app.coreLogger.error(msg, ...args));
16
+ }
17
+
18
+ async didLoad(): Promise<void> {
19
+ await this.#watcher.ready();
20
+ this.#app.coreLogger.info('[@eggjs/watcher:%s] watcher start success', this.#app.type);
21
+ }
22
+ }
@@ -0,0 +1,6 @@
1
+ import { Base } from 'sdk-base';
2
+
3
+ export abstract class BaseEventSource extends Base {
4
+ abstract watch(file: string): void;
5
+ abstract unwatch(file: string): void;
6
+ }
@@ -0,0 +1,18 @@
1
+ import { BaseEventSource } from './base.js';
2
+
3
+ export default class DefaultEventSource extends BaseEventSource {
4
+ constructor() {
5
+ super();
6
+ // delay emit so that can be listened
7
+ setImmediate(() => this.emit('info', '[@eggjs/watcher] defaultEventSource watcher will NOT take effect'));
8
+ this.ready(true);
9
+ }
10
+
11
+ watch() {
12
+ this.emit('info', '[@eggjs/watcher] using defaultEventSource watcher.watch() does NOTHING');
13
+ }
14
+
15
+ unwatch() {
16
+ this.emit('info', '[@eggjs/watcher] using defaultEventSource watcher.unwatch() does NOTHING');
17
+ }
18
+ }
@@ -0,0 +1,99 @@
1
+ import { debuglog } from 'node:util';
2
+ import path from 'node:path';
3
+ import fs, { FSWatcher, WatchEventType } from 'node:fs';
4
+ import { BaseEventSource } from './base.js';
5
+ import type { ChangeInfo } from '../types.js';
6
+
7
+ const debug = debuglog('@eggjs/watcher/lib/event-sources/development');
8
+
9
+ // only used by local dev environment
10
+ export default class DevelopmentEventSource extends BaseEventSource {
11
+ #fileWatching = new Map<string, FSWatcher>();
12
+
13
+ constructor() {
14
+ super();
15
+ this.ready(true);
16
+ }
17
+
18
+ watch(file: string) {
19
+ try {
20
+ const stat = fs.statSync(file, { throwIfNoEntry: false });
21
+ if (!stat) {
22
+ debug('watch %o ignore, file not exists', file);
23
+ return;
24
+ }
25
+ debug('watch %o, isFile: %o', file, stat.isFile());
26
+ // https://nodejs.org/docs/latest/api/fs.html#fswatchfilename-options-listener
27
+ let recursive = true;
28
+ if (process.platform === 'linux' && process.version.startsWith('v18.')) {
29
+ // https://github.com/fgnass/filewatcher/pull/6
30
+ // disable recursive on linux + Node.js <= 18
31
+ recursive = false;
32
+ }
33
+ const handler = fs.watch(file, {
34
+ persistent: true,
35
+ recursive,
36
+ }, (event, filename) => {
37
+ debug('watch %o => event: %o, filename: %o', file, event, filename);
38
+ let changePath = file;
39
+ if (stat.isFile()) {
40
+ this.#onFsWatchChange(event, changePath);
41
+ } else {
42
+ // dir
43
+ if (filename) {
44
+ changePath = path.join(file, filename);
45
+ }
46
+ this.#onFsWatchChange(event, changePath);
47
+ }
48
+ });
49
+ // 保存 handler,用于解除监听
50
+ this.#fileWatching.set(file, handler);
51
+ } catch (e) {
52
+ // file not exist, do nothing
53
+ // do not emit error, in case of too many logs
54
+ this.emit('warn', '[@eggjs/watcher:DevelopmentEventSource] watch %o error: %s', file, e);
55
+ }
56
+ }
57
+
58
+ unwatch(file: string) {
59
+ if (!file) return;
60
+
61
+ const h = this.#fileWatching.get(file);
62
+ if (!h) return;
63
+
64
+ // fs.watch 文件监听
65
+ h.removeAllListeners();
66
+ h.close();
67
+ this.#fileWatching.delete(file);
68
+ }
69
+
70
+ #onFsWatchChange(event: WatchEventType, file: string) {
71
+ if (!file) {
72
+ this.emit('warn', '[@eggjs/watcher:DevelopmentEventSource] event: %o', event);
73
+ return;
74
+ }
75
+ // { event: 'change',
76
+ // path: '/Users/mk2/git/changing/test/fixtures/foo.js',
77
+ // stat:
78
+ // { dev: 16777220,
79
+ // mode: 33188,
80
+ // nlink: 1,
81
+ // uid: 501,
82
+ // gid: 20,
83
+ // rdev: 0,
84
+ // blksize: 4096,
85
+ // ino: 72656587,
86
+ // size: 11,
87
+ // blocks: 8,
88
+ // atime: Wed Jun 17 2015 00:08:11 GMT+0800 (CST),
89
+ // mtime: Wed Jun 17 2015 00:08:38 GMT+0800 (CST),
90
+ // ctime: Wed Jun 17 2015 00:08:38 GMT+0800 (CST),
91
+ // birthtime: Tue Jun 16 2015 23:19:13 GMT+0800 (CST) } }
92
+ const info = {
93
+ path: file,
94
+ event,
95
+ stat: fs.statSync(file, { throwIfNoEntry: false }),
96
+ } as ChangeInfo;
97
+ this.emit('change', info);
98
+ }
99
+ }
@@ -0,0 +1,3 @@
1
+ export * from './base.js';
2
+ export * from './default.js';
3
+ export * from './development.js';
@@ -0,0 +1,35 @@
1
+ import type { WatchEventType, Stats } from 'node:fs';
2
+ import type { Watcher } from './watcher.js';
3
+
4
+ export interface WatcherConfig {
5
+ watcher: {
6
+ type: string;
7
+ eventSources: Record<string, string>;
8
+ };
9
+ [key: string]: Record<string, any>;
10
+ }
11
+
12
+ export interface ChangeInfo {
13
+ event: WatchEventType;
14
+ /**
15
+ * file stat if path exists
16
+ */
17
+ stat?: Stats;
18
+ path: string;
19
+ }
20
+
21
+ declare module 'egg' {
22
+ export interface EggWatcherAgent {
23
+ watcher: Watcher;
24
+ }
25
+ export interface Agent extends EggWatcherAgent {}
26
+
27
+ export interface EggWatcherApplication {
28
+ watcher: Watcher;
29
+ }
30
+ export interface Application extends EggWatcherApplication {}
31
+
32
+ export interface EggWatcherAppConfig extends WatcherConfig {}
33
+
34
+ export interface EggAppConfig extends EggWatcherAppConfig {}
35
+ }
@@ -0,0 +1,20 @@
1
+ import path from 'node:path';
2
+ import { fileURLToPath } from 'node:url';
3
+
4
+ // judge if parent is child's parent path
5
+ // isEqualOrParentPath('/foo', '/foo/bar') => true
6
+ // isEqualOrParentPath('/foo/bar', '/foo') => false
7
+ export function isEqualOrParentPath(parent: string, child: string) {
8
+ return !path.relative(parent, child).startsWith('..');
9
+ }
10
+
11
+ export function getSourceDirname() {
12
+ if (typeof __dirname === 'string') {
13
+ return path.dirname(__dirname);
14
+ }
15
+
16
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
17
+ // @ts-ignore
18
+ const __filename = fileURLToPath(import.meta.url);
19
+ return path.dirname(path.dirname(__filename));
20
+ }
@@ -0,0 +1,117 @@
1
+ import { debuglog } from 'node:util';
2
+ import { Base } from 'sdk-base';
3
+ import camelcase from 'camelcase';
4
+ import { importModule } from '@eggjs/utils';
5
+ import { BaseEventSource } from './event-sources/base.js';
6
+ import { isEqualOrParentPath } from './utils.js';
7
+ import type { ChangeInfo, WatcherConfig } from './types.js';
8
+
9
+ const debug = debuglog('@eggjs/watcher/lib/watcher');
10
+
11
+ export type WatchListener = (info: ChangeInfo) => void;
12
+
13
+ export class Watcher extends Base {
14
+ #config: WatcherConfig;
15
+ #eventSource: BaseEventSource;
16
+
17
+ constructor(config: WatcherConfig) {
18
+ super({
19
+ initMethod: '_init',
20
+ });
21
+ this.#config = config;
22
+ }
23
+
24
+ protected async _init() {
25
+ const watcherType = this.#config.watcher.type;
26
+ let EventSource: typeof BaseEventSource = this.#config.watcher.eventSources[watcherType] as any;
27
+ if (typeof EventSource === 'string') {
28
+ EventSource = await importModule(EventSource);
29
+ }
30
+
31
+ // chokidar => watcherChokidar
32
+ // custom => watcherCustom
33
+ //
34
+ // e.g:
35
+ // config => { watcher: { type: 'custom' }, watcherCustom: { ... } }
36
+ const key = camelcase([ 'watcher', watcherType ]);
37
+ const eventSourceOptions = this.#config[key] ?? {};
38
+ this.#eventSource = Reflect.construct(EventSource, [ eventSourceOptions ]);
39
+ this.#eventSource.on('change', this.#onChange.bind(this))
40
+ .on('fuzzy-change', this.#onFuzzyChange.bind(this))
41
+ .on('info', (...args) => this.emit('info', ...args))
42
+ .on('warn', (...args) => this.emit('warn', ...args))
43
+ .on('error', (...args) => this.emit('error', ...args));
44
+ await this.#eventSource.ready();
45
+ }
46
+
47
+ watch(path: string | string[], listener: WatchListener) {
48
+ this.emit('info', '[@eggjs/watcher] Start watching: %j', path);
49
+ if (!path) return;
50
+
51
+ // support array
52
+ if (Array.isArray(path)) {
53
+ path.forEach(p => this.watch(p, listener));
54
+ return;
55
+ }
56
+
57
+ // one file only watch once
58
+ if (!this.listenerCount(path)) {
59
+ this.#eventSource.watch(path);
60
+ }
61
+ this.on(path, listener);
62
+ }
63
+
64
+ /*
65
+ // TODO wait unsubscribe implementation of cluster-client
66
+ unwatch(path, callback) {
67
+ if (!path) return;
68
+
69
+ // support array
70
+ if (Array.isArray(path)) {
71
+ path.forEach(p => this.unwatch(p, callback));
72
+ return;
73
+ }
74
+
75
+ if (callback) {
76
+ this.removeListener(path, callback);
77
+ // stop watching when no listener bound to the path
78
+ if (this.listenerCount(path) === 0) {
79
+ this._eventSource.unwatch(path);
80
+ }
81
+ return;
82
+ }
83
+
84
+ this.removeAllListeners(path);
85
+ this._eventSource.unwatch(path);
86
+ }
87
+ */
88
+
89
+ #onChange(info: ChangeInfo) {
90
+ debug('onChange %o', info);
91
+ this.emit('info', '[@eggjs/watcher] Received a change event from eventSource: %j', info);
92
+ const path = info.path;
93
+
94
+ for (const p of this.eventNames()) {
95
+ if (typeof p !== 'string') continue;
96
+ // if it is a sub path, emit a `change` event
97
+ if (isEqualOrParentPath(p, path)) {
98
+ this.emit(p, info);
99
+ }
100
+ }
101
+ }
102
+
103
+ #onFuzzyChange(info: ChangeInfo) {
104
+ debug('onFuzzyChange %o', info);
105
+ this.emit('info', '[@eggjs/watcher] Received a fuzzy-change event from eventSource: %j', info);
106
+ const path = info.path;
107
+
108
+ for (const p of this.eventNames()) {
109
+ if (typeof p !== 'string') continue;
110
+ // if it is a parent path, emit a `change` event
111
+ // just the opposite to `_onChange`
112
+ if (isEqualOrParentPath(path, p)) {
113
+ this.emit(p, info);
114
+ }
115
+ }
116
+ }
117
+ }