@szymonrybczak/playwright-mcp 0.0.2 → 0.0.4

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 (115) hide show
  1. package/index.d.ts +21 -8
  2. package/lib/cjs/_virtual/_commonjsHelpers.js +9 -0
  3. package/lib/cjs/_virtual/browser.js +11 -0
  4. package/lib/cjs/_virtual/browser2.js +7 -0
  5. package/lib/cjs/cloudflare/package.json.js +10 -0
  6. package/lib/cjs/index.js +28 -0
  7. package/lib/cjs/node_modules/debug/src/browser.js +290 -0
  8. package/lib/cjs/node_modules/debug/src/common.js +307 -0
  9. package/lib/cjs/node_modules/ms/index.js +176 -0
  10. package/lib/cjs/package.js +9 -0
  11. package/lib/cjs/src/browserContextFactory.js +240 -0
  12. package/lib/cjs/src/config.js +77 -0
  13. package/lib/cjs/src/connection.js +74 -0
  14. package/lib/cjs/src/context.js +281 -0
  15. package/lib/cjs/src/fileUtils.js +25 -0
  16. package/lib/cjs/src/index.js +15 -0
  17. package/lib/cjs/src/javascript.js +41 -0
  18. package/lib/cjs/src/manualPromise.js +39 -0
  19. package/lib/cjs/src/pageSnapshot.js +35 -0
  20. package/lib/cjs/src/tab.js +91 -0
  21. package/lib/cjs/src/tools/common.js +60 -0
  22. package/lib/cjs/src/tools/console.js +36 -0
  23. package/lib/cjs/src/tools/dialogs.js +44 -0
  24. package/lib/cjs/src/tools/files.js +43 -0
  25. package/lib/cjs/src/tools/install.js +49 -0
  26. package/lib/cjs/src/tools/keyboard.js +38 -0
  27. package/lib/cjs/src/tools/navigate.js +85 -0
  28. package/lib/cjs/src/tools/network.js +43 -0
  29. package/lib/cjs/src/tools/pdf.js +42 -0
  30. package/lib/cjs/src/tools/screenshot.js +69 -0
  31. package/lib/cjs/src/tools/snapshot.js +195 -0
  32. package/lib/cjs/src/tools/tabs.js +110 -0
  33. package/lib/cjs/src/tools/testing.js +52 -0
  34. package/lib/cjs/src/tools/tool.js +9 -0
  35. package/lib/cjs/src/tools/utils.js +75 -0
  36. package/lib/cjs/src/tools/vision.js +181 -0
  37. package/lib/cjs/src/tools/wait.js +51 -0
  38. package/lib/cjs/src/tools.js +54 -0
  39. package/lib/esm/_virtual/_commonjsHelpers.js +5 -0
  40. package/lib/esm/_virtual/browser.js +7 -0
  41. package/lib/esm/_virtual/browser2.js +3 -0
  42. package/lib/esm/cloudflare/package.json.js +5 -0
  43. package/lib/esm/index.js +24 -0
  44. package/lib/esm/node_modules/debug/src/browser.js +286 -0
  45. package/lib/esm/node_modules/debug/src/common.js +303 -0
  46. package/lib/esm/node_modules/ms/index.js +172 -0
  47. package/lib/esm/package.js +5 -0
  48. package/lib/esm/src/browserContextFactory.js +216 -0
  49. package/lib/esm/src/config.js +72 -0
  50. package/lib/esm/src/connection.js +69 -0
  51. package/lib/esm/src/context.js +277 -0
  52. package/lib/esm/src/fileUtils.js +20 -0
  53. package/lib/esm/src/index.js +11 -0
  54. package/lib/esm/src/javascript.js +35 -0
  55. package/lib/esm/src/manualPromise.js +35 -0
  56. package/lib/esm/src/pageSnapshot.js +31 -0
  57. package/lib/esm/src/tab.js +87 -0
  58. package/lib/esm/src/tools/common.js +56 -0
  59. package/lib/esm/src/tools/console.js +32 -0
  60. package/lib/esm/src/tools/dialogs.js +40 -0
  61. package/lib/esm/src/tools/files.js +39 -0
  62. package/lib/esm/src/tools/install.js +45 -0
  63. package/lib/esm/src/tools/keyboard.js +34 -0
  64. package/lib/esm/src/tools/navigate.js +81 -0
  65. package/lib/esm/src/tools/network.js +39 -0
  66. package/lib/esm/src/tools/pdf.js +38 -0
  67. package/lib/esm/src/tools/screenshot.js +65 -0
  68. package/lib/esm/src/tools/snapshot.js +191 -0
  69. package/lib/esm/src/tools/tabs.js +106 -0
  70. package/lib/esm/src/tools/testing.js +48 -0
  71. package/lib/esm/src/tools/tool.js +5 -0
  72. package/lib/esm/src/tools/utils.js +68 -0
  73. package/lib/esm/src/tools/vision.js +177 -0
  74. package/lib/esm/src/tools/wait.js +47 -0
  75. package/lib/esm/src/tools.js +49 -0
  76. package/package.json +15 -44
  77. package/LICENSE +0 -202
  78. package/README.md +0 -508
  79. package/cli.js +0 -18
  80. package/config.d.ts +0 -128
  81. package/index.js +0 -19
  82. package/lib/browserContextFactory.js +0 -227
  83. package/lib/browserServer.js +0 -151
  84. package/lib/config.js +0 -189
  85. package/lib/connection.js +0 -82
  86. package/lib/context.js +0 -291
  87. package/lib/fileUtils.js +0 -32
  88. package/lib/httpServer.js +0 -201
  89. package/lib/index.js +0 -36
  90. package/lib/javascript.js +0 -49
  91. package/lib/manualPromise.js +0 -111
  92. package/lib/package.js +0 -20
  93. package/lib/pageSnapshot.js +0 -43
  94. package/lib/program.js +0 -72
  95. package/lib/server.js +0 -48
  96. package/lib/tab.js +0 -101
  97. package/lib/tools/common.js +0 -68
  98. package/lib/tools/console.js +0 -44
  99. package/lib/tools/dialogs.js +0 -52
  100. package/lib/tools/files.js +0 -51
  101. package/lib/tools/install.js +0 -57
  102. package/lib/tools/keyboard.js +0 -46
  103. package/lib/tools/navigate.js +0 -93
  104. package/lib/tools/network.js +0 -51
  105. package/lib/tools/pdf.js +0 -49
  106. package/lib/tools/screenshot.js +0 -77
  107. package/lib/tools/snapshot.js +0 -204
  108. package/lib/tools/tabs.js +0 -118
  109. package/lib/tools/testing.js +0 -60
  110. package/lib/tools/tool.js +0 -18
  111. package/lib/tools/utils.js +0 -80
  112. package/lib/tools/vision.js +0 -189
  113. package/lib/tools/wait.js +0 -59
  114. package/lib/tools.js +0 -61
  115. package/lib/transport.js +0 -133
package/index.d.ts CHANGED
@@ -15,14 +15,27 @@
15
15
  * limitations under the License.
16
16
  */
17
17
 
18
- import type { Server } from '@modelcontextprotocol/sdk/server/index.js';
19
- import type { Config } from './config.js';
20
- import type { BrowserContext } from '@szymonrybczak/patchright';
18
+ import { env } from 'cloudflare:workers';
19
+ import { McpAgent } from 'agents/mcp';
20
+ import { BrowserEndpoint } from '@szymonrybczak/patchright';
21
21
 
22
- export type Connection = {
23
- server: Server;
24
- close(): Promise<void>;
25
- };
22
+ type ToolCapability = 'core' | 'tabs' | 'pdf' | 'history' | 'wait' | 'files';
26
23
 
27
- export declare function createConnection(config?: Config, contextGetter?: () => Promise<BrowserContext>): Promise<Connection>;
24
+ type Options = {
25
+ /**
26
+ * Enable vision capabilities (e.g., visual automation or OCR).
27
+ */
28
+ vision?: boolean;
29
+ /**
30
+ * List of enabled tool capabilities. Possible values:
31
+ * - 'core': Core browser automation features.
32
+ * - 'tabs': Tab management features.
33
+ * - 'pdf': PDF generation and manipulation.
34
+ * - 'history': Browser history access.
35
+ * - 'wait': Wait and timing utilities.
36
+ * - 'files': File upload/download support.
37
+ */
38
+ capabilities?: ToolCapability[];
39
+ };
40
+ export declare function createMcpAgent(endpoint: BrowserEndpoint, options?: Options): typeof McpAgent<typeof env, {}, {}>;
28
41
  export {};
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ function getDefaultExportFromCjs (x) {
6
+ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
7
+ }
8
+
9
+ exports.getDefaultExportFromCjs = getDefaultExportFromCjs;
@@ -0,0 +1,11 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
4
+
5
+ const _commonjsHelpers = require('./_commonjsHelpers.js');
6
+ const browser = require('../node_modules/debug/src/browser.js');
7
+
8
+ var browserExports = browser.__require();
9
+ const debug = /*@__PURE__*/_commonjsHelpers.getDefaultExportFromCjs(browserExports);
10
+
11
+ exports.default = debug;
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ var browser = {exports: {}};
6
+
7
+ exports.__module = browser;
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
4
+
5
+ const version = "0.0.4";
6
+ const json = {
7
+ version};
8
+
9
+ exports.default = json;
10
+ exports.version = version;
@@ -0,0 +1,28 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const mcp = require('agents/mcp');
6
+ const playwright = require('@szymonrybczak/patchright');
7
+ const index = require('./src/index.js');
8
+
9
+ function createMcpAgent(endpoint, options) {
10
+ const cdpEndpoint = typeof endpoint === "string" ? endpoint : endpoint instanceof URL ? endpoint.toString() : playwright.endpointURLString(endpoint);
11
+ const connection = index.createConnection({
12
+ capabilities: ["core", "tabs", "pdf", "history", "wait", "files", "testing"],
13
+ browser: {
14
+ cdpEndpoint
15
+ },
16
+ ...options
17
+ });
18
+ return class PlaywrightMcpAgent extends mcp.McpAgent {
19
+ constructor() {
20
+ super(...arguments);
21
+ this.server = connection.then((server) => server.server);
22
+ }
23
+ async init() {
24
+ }
25
+ };
26
+ }
27
+
28
+ exports.createMcpAgent = createMcpAgent;
@@ -0,0 +1,290 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const browser = require('../../../_virtual/browser2.js');
6
+ const common = require('./common.js');
7
+
8
+ /* eslint-env browser */
9
+
10
+ var hasRequiredBrowser;
11
+
12
+ function requireBrowser () {
13
+ if (hasRequiredBrowser) return browser.__module.exports;
14
+ hasRequiredBrowser = 1;
15
+ (function (module, exports) {
16
+ /**
17
+ * This is the web browser implementation of `debug()`.
18
+ */
19
+
20
+ exports.formatArgs = formatArgs;
21
+ exports.save = save;
22
+ exports.load = load;
23
+ exports.useColors = useColors;
24
+ exports.storage = localstorage();
25
+ exports.destroy = (() => {
26
+ let warned = false;
27
+
28
+ return () => {
29
+ if (!warned) {
30
+ warned = true;
31
+ console.warn('Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.');
32
+ }
33
+ };
34
+ })();
35
+
36
+ /**
37
+ * Colors.
38
+ */
39
+
40
+ exports.colors = [
41
+ '#0000CC',
42
+ '#0000FF',
43
+ '#0033CC',
44
+ '#0033FF',
45
+ '#0066CC',
46
+ '#0066FF',
47
+ '#0099CC',
48
+ '#0099FF',
49
+ '#00CC00',
50
+ '#00CC33',
51
+ '#00CC66',
52
+ '#00CC99',
53
+ '#00CCCC',
54
+ '#00CCFF',
55
+ '#3300CC',
56
+ '#3300FF',
57
+ '#3333CC',
58
+ '#3333FF',
59
+ '#3366CC',
60
+ '#3366FF',
61
+ '#3399CC',
62
+ '#3399FF',
63
+ '#33CC00',
64
+ '#33CC33',
65
+ '#33CC66',
66
+ '#33CC99',
67
+ '#33CCCC',
68
+ '#33CCFF',
69
+ '#6600CC',
70
+ '#6600FF',
71
+ '#6633CC',
72
+ '#6633FF',
73
+ '#66CC00',
74
+ '#66CC33',
75
+ '#9900CC',
76
+ '#9900FF',
77
+ '#9933CC',
78
+ '#9933FF',
79
+ '#99CC00',
80
+ '#99CC33',
81
+ '#CC0000',
82
+ '#CC0033',
83
+ '#CC0066',
84
+ '#CC0099',
85
+ '#CC00CC',
86
+ '#CC00FF',
87
+ '#CC3300',
88
+ '#CC3333',
89
+ '#CC3366',
90
+ '#CC3399',
91
+ '#CC33CC',
92
+ '#CC33FF',
93
+ '#CC6600',
94
+ '#CC6633',
95
+ '#CC9900',
96
+ '#CC9933',
97
+ '#CCCC00',
98
+ '#CCCC33',
99
+ '#FF0000',
100
+ '#FF0033',
101
+ '#FF0066',
102
+ '#FF0099',
103
+ '#FF00CC',
104
+ '#FF00FF',
105
+ '#FF3300',
106
+ '#FF3333',
107
+ '#FF3366',
108
+ '#FF3399',
109
+ '#FF33CC',
110
+ '#FF33FF',
111
+ '#FF6600',
112
+ '#FF6633',
113
+ '#FF9900',
114
+ '#FF9933',
115
+ '#FFCC00',
116
+ '#FFCC33'
117
+ ];
118
+
119
+ /**
120
+ * Currently only WebKit-based Web Inspectors, Firefox >= v31,
121
+ * and the Firebug extension (any Firefox version) are known
122
+ * to support "%c" CSS customizations.
123
+ *
124
+ * TODO: add a `localStorage` variable to explicitly enable/disable colors
125
+ */
126
+
127
+ // eslint-disable-next-line complexity
128
+ function useColors() {
129
+ // NB: In an Electron preload script, document will be defined but not fully
130
+ // initialized. Since we know we're in Chrome, we'll just detect this case
131
+ // explicitly
132
+ if (typeof window !== 'undefined' && window.process && (window.process.type === 'renderer' || window.process.__nwjs)) {
133
+ return true;
134
+ }
135
+
136
+ // Internet Explorer and Edge do not support colors.
137
+ if (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/)) {
138
+ return false;
139
+ }
140
+
141
+ let m;
142
+
143
+ // Is webkit? http://stackoverflow.com/a/16459606/376773
144
+ // document is undefined in react-native: https://github.com/facebook/react-native/pull/1632
145
+ // eslint-disable-next-line no-return-assign
146
+ return (typeof document !== 'undefined' && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance) ||
147
+ // Is firebug? http://stackoverflow.com/a/398120/376773
148
+ (typeof window !== 'undefined' && window.console && (window.console.firebug || (window.console.exception && window.console.table))) ||
149
+ // Is firefox >= v31?
150
+ // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
151
+ (typeof navigator !== 'undefined' && navigator.userAgent && (m = navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/)) && parseInt(m[1], 10) >= 31) ||
152
+ // Double check webkit in userAgent just in case we are in a worker
153
+ (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/));
154
+ }
155
+
156
+ /**
157
+ * Colorize log arguments if enabled.
158
+ *
159
+ * @api public
160
+ */
161
+
162
+ function formatArgs(args) {
163
+ args[0] = (this.useColors ? '%c' : '') +
164
+ this.namespace +
165
+ (this.useColors ? ' %c' : ' ') +
166
+ args[0] +
167
+ (this.useColors ? '%c ' : ' ') +
168
+ '+' + module.exports.humanize(this.diff);
169
+
170
+ if (!this.useColors) {
171
+ return;
172
+ }
173
+
174
+ const c = 'color: ' + this.color;
175
+ args.splice(1, 0, c, 'color: inherit');
176
+
177
+ // The final "%c" is somewhat tricky, because there could be other
178
+ // arguments passed either before or after the %c, so we need to
179
+ // figure out the correct index to insert the CSS into
180
+ let index = 0;
181
+ let lastC = 0;
182
+ args[0].replace(/%[a-zA-Z%]/g, match => {
183
+ if (match === '%%') {
184
+ return;
185
+ }
186
+ index++;
187
+ if (match === '%c') {
188
+ // We only are interested in the *last* %c
189
+ // (the user may have provided their own)
190
+ lastC = index;
191
+ }
192
+ });
193
+
194
+ args.splice(lastC, 0, c);
195
+ }
196
+
197
+ /**
198
+ * Invokes `console.debug()` when available.
199
+ * No-op when `console.debug` is not a "function".
200
+ * If `console.debug` is not available, falls back
201
+ * to `console.log`.
202
+ *
203
+ * @api public
204
+ */
205
+ exports.log = console.debug || console.log || (() => {});
206
+
207
+ /**
208
+ * Save `namespaces`.
209
+ *
210
+ * @param {String} namespaces
211
+ * @api private
212
+ */
213
+ function save(namespaces) {
214
+ try {
215
+ if (namespaces) {
216
+ exports.storage.setItem('debug', namespaces);
217
+ } else {
218
+ exports.storage.removeItem('debug');
219
+ }
220
+ } catch (error) {
221
+ // Swallow
222
+ // XXX (@Qix-) should we be logging these?
223
+ }
224
+ }
225
+
226
+ /**
227
+ * Load `namespaces`.
228
+ *
229
+ * @return {String} returns the previously persisted debug modes
230
+ * @api private
231
+ */
232
+ function load() {
233
+ let r;
234
+ try {
235
+ r = exports.storage.getItem('debug') || exports.storage.getItem('DEBUG') ;
236
+ } catch (error) {
237
+ // Swallow
238
+ // XXX (@Qix-) should we be logging these?
239
+ }
240
+
241
+ // If debug isn't set in LS, and we're in Electron, try to load $DEBUG
242
+ if (!r && typeof process !== 'undefined' && 'env' in process) {
243
+ r = process.env.DEBUG;
244
+ }
245
+
246
+ return r;
247
+ }
248
+
249
+ /**
250
+ * Localstorage attempts to return the localstorage.
251
+ *
252
+ * This is necessary because safari throws
253
+ * when a user disables cookies/localstorage
254
+ * and you attempt to access it.
255
+ *
256
+ * @return {LocalStorage}
257
+ * @api private
258
+ */
259
+
260
+ function localstorage() {
261
+ try {
262
+ // TVMLKit (Apple TV JS Runtime) does not have a window object, just localStorage in the global context
263
+ // The Browser also has localStorage in the global context.
264
+ return localStorage;
265
+ } catch (error) {
266
+ // Swallow
267
+ // XXX (@Qix-) should we be logging these?
268
+ }
269
+ }
270
+
271
+ module.exports = common.__require()(exports);
272
+
273
+ const {formatters} = module.exports;
274
+
275
+ /**
276
+ * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
277
+ */
278
+
279
+ formatters.j = function (v) {
280
+ try {
281
+ return JSON.stringify(v);
282
+ } catch (error) {
283
+ return '[UnexpectedJSONParseError]: ' + error.message;
284
+ }
285
+ };
286
+ } (browser.__module, browser.__module.exports));
287
+ return browser.__module.exports;
288
+ }
289
+
290
+ exports.__require = requireBrowser;
@@ -0,0 +1,307 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const index = require('../../ms/index.js');
6
+
7
+ var common;
8
+ var hasRequiredCommon;
9
+
10
+ function requireCommon () {
11
+ if (hasRequiredCommon) return common;
12
+ hasRequiredCommon = 1;
13
+ /**
14
+ * This is the common logic for both the Node.js and web browser
15
+ * implementations of `debug()`.
16
+ */
17
+
18
+ function setup(env) {
19
+ createDebug.debug = createDebug;
20
+ createDebug.default = createDebug;
21
+ createDebug.coerce = coerce;
22
+ createDebug.disable = disable;
23
+ createDebug.enable = enable;
24
+ createDebug.enabled = enabled;
25
+ createDebug.humanize = index.__require();
26
+ createDebug.destroy = destroy;
27
+
28
+ Object.keys(env).forEach(key => {
29
+ createDebug[key] = env[key];
30
+ });
31
+
32
+ /**
33
+ * The currently active debug mode names, and names to skip.
34
+ */
35
+
36
+ createDebug.names = [];
37
+ createDebug.skips = [];
38
+
39
+ /**
40
+ * Map of special "%n" handling functions, for the debug "format" argument.
41
+ *
42
+ * Valid key names are a single, lower or upper-case letter, i.e. "n" and "N".
43
+ */
44
+ createDebug.formatters = {};
45
+
46
+ /**
47
+ * Selects a color for a debug namespace
48
+ * @param {String} namespace The namespace string for the debug instance to be colored
49
+ * @return {Number|String} An ANSI color code for the given namespace
50
+ * @api private
51
+ */
52
+ function selectColor(namespace) {
53
+ let hash = 0;
54
+
55
+ for (let i = 0; i < namespace.length; i++) {
56
+ hash = ((hash << 5) - hash) + namespace.charCodeAt(i);
57
+ hash |= 0; // Convert to 32bit integer
58
+ }
59
+
60
+ return createDebug.colors[Math.abs(hash) % createDebug.colors.length];
61
+ }
62
+ createDebug.selectColor = selectColor;
63
+
64
+ /**
65
+ * Create a debugger with the given `namespace`.
66
+ *
67
+ * @param {String} namespace
68
+ * @return {Function}
69
+ * @api public
70
+ */
71
+ function createDebug(namespace) {
72
+ let prevTime;
73
+ let enableOverride = null;
74
+ let namespacesCache;
75
+ let enabledCache;
76
+
77
+ function debug(...args) {
78
+ // Disabled?
79
+ if (!debug.enabled) {
80
+ return;
81
+ }
82
+
83
+ const self = debug;
84
+
85
+ // Set `diff` timestamp
86
+ const curr = Number(new Date());
87
+ const ms = curr - (prevTime || curr);
88
+ self.diff = ms;
89
+ self.prev = prevTime;
90
+ self.curr = curr;
91
+ prevTime = curr;
92
+
93
+ args[0] = createDebug.coerce(args[0]);
94
+
95
+ if (typeof args[0] !== 'string') {
96
+ // Anything else let's inspect with %O
97
+ args.unshift('%O');
98
+ }
99
+
100
+ // Apply any `formatters` transformations
101
+ let index = 0;
102
+ args[0] = args[0].replace(/%([a-zA-Z%])/g, (match, format) => {
103
+ // If we encounter an escaped % then don't increase the array index
104
+ if (match === '%%') {
105
+ return '%';
106
+ }
107
+ index++;
108
+ const formatter = createDebug.formatters[format];
109
+ if (typeof formatter === 'function') {
110
+ const val = args[index];
111
+ match = formatter.call(self, val);
112
+
113
+ // Now we need to remove `args[index]` since it's inlined in the `format`
114
+ args.splice(index, 1);
115
+ index--;
116
+ }
117
+ return match;
118
+ });
119
+
120
+ // Apply env-specific formatting (colors, etc.)
121
+ createDebug.formatArgs.call(self, args);
122
+
123
+ const logFn = self.log || createDebug.log;
124
+ logFn.apply(self, args);
125
+ }
126
+
127
+ debug.namespace = namespace;
128
+ debug.useColors = createDebug.useColors();
129
+ debug.color = createDebug.selectColor(namespace);
130
+ debug.extend = extend;
131
+ debug.destroy = createDebug.destroy; // XXX Temporary. Will be removed in the next major release.
132
+
133
+ Object.defineProperty(debug, 'enabled', {
134
+ enumerable: true,
135
+ configurable: false,
136
+ get: () => {
137
+ if (enableOverride !== null) {
138
+ return enableOverride;
139
+ }
140
+ if (namespacesCache !== createDebug.namespaces) {
141
+ namespacesCache = createDebug.namespaces;
142
+ enabledCache = createDebug.enabled(namespace);
143
+ }
144
+
145
+ return enabledCache;
146
+ },
147
+ set: v => {
148
+ enableOverride = v;
149
+ }
150
+ });
151
+
152
+ // Env-specific initialization logic for debug instances
153
+ if (typeof createDebug.init === 'function') {
154
+ createDebug.init(debug);
155
+ }
156
+
157
+ return debug;
158
+ }
159
+
160
+ function extend(namespace, delimiter) {
161
+ const newDebug = createDebug(this.namespace + (typeof delimiter === 'undefined' ? ':' : delimiter) + namespace);
162
+ newDebug.log = this.log;
163
+ return newDebug;
164
+ }
165
+
166
+ /**
167
+ * Enables a debug mode by namespaces. This can include modes
168
+ * separated by a colon and wildcards.
169
+ *
170
+ * @param {String} namespaces
171
+ * @api public
172
+ */
173
+ function enable(namespaces) {
174
+ createDebug.save(namespaces);
175
+ createDebug.namespaces = namespaces;
176
+
177
+ createDebug.names = [];
178
+ createDebug.skips = [];
179
+
180
+ const split = (typeof namespaces === 'string' ? namespaces : '')
181
+ .trim()
182
+ .replace(/\s+/g, ',')
183
+ .split(',')
184
+ .filter(Boolean);
185
+
186
+ for (const ns of split) {
187
+ if (ns[0] === '-') {
188
+ createDebug.skips.push(ns.slice(1));
189
+ } else {
190
+ createDebug.names.push(ns);
191
+ }
192
+ }
193
+ }
194
+
195
+ /**
196
+ * Checks if the given string matches a namespace template, honoring
197
+ * asterisks as wildcards.
198
+ *
199
+ * @param {String} search
200
+ * @param {String} template
201
+ * @return {Boolean}
202
+ */
203
+ function matchesTemplate(search, template) {
204
+ let searchIndex = 0;
205
+ let templateIndex = 0;
206
+ let starIndex = -1;
207
+ let matchIndex = 0;
208
+
209
+ while (searchIndex < search.length) {
210
+ if (templateIndex < template.length && (template[templateIndex] === search[searchIndex] || template[templateIndex] === '*')) {
211
+ // Match character or proceed with wildcard
212
+ if (template[templateIndex] === '*') {
213
+ starIndex = templateIndex;
214
+ matchIndex = searchIndex;
215
+ templateIndex++; // Skip the '*'
216
+ } else {
217
+ searchIndex++;
218
+ templateIndex++;
219
+ }
220
+ } else if (starIndex !== -1) { // eslint-disable-line no-negated-condition
221
+ // Backtrack to the last '*' and try to match more characters
222
+ templateIndex = starIndex + 1;
223
+ matchIndex++;
224
+ searchIndex = matchIndex;
225
+ } else {
226
+ return false; // No match
227
+ }
228
+ }
229
+
230
+ // Handle trailing '*' in template
231
+ while (templateIndex < template.length && template[templateIndex] === '*') {
232
+ templateIndex++;
233
+ }
234
+
235
+ return templateIndex === template.length;
236
+ }
237
+
238
+ /**
239
+ * Disable debug output.
240
+ *
241
+ * @return {String} namespaces
242
+ * @api public
243
+ */
244
+ function disable() {
245
+ const namespaces = [
246
+ ...createDebug.names,
247
+ ...createDebug.skips.map(namespace => '-' + namespace)
248
+ ].join(',');
249
+ createDebug.enable('');
250
+ return namespaces;
251
+ }
252
+
253
+ /**
254
+ * Returns true if the given mode name is enabled, false otherwise.
255
+ *
256
+ * @param {String} name
257
+ * @return {Boolean}
258
+ * @api public
259
+ */
260
+ function enabled(name) {
261
+ for (const skip of createDebug.skips) {
262
+ if (matchesTemplate(name, skip)) {
263
+ return false;
264
+ }
265
+ }
266
+
267
+ for (const ns of createDebug.names) {
268
+ if (matchesTemplate(name, ns)) {
269
+ return true;
270
+ }
271
+ }
272
+
273
+ return false;
274
+ }
275
+
276
+ /**
277
+ * Coerce `val`.
278
+ *
279
+ * @param {Mixed} val
280
+ * @return {Mixed}
281
+ * @api private
282
+ */
283
+ function coerce(val) {
284
+ if (val instanceof Error) {
285
+ return val.stack || val.message;
286
+ }
287
+ return val;
288
+ }
289
+
290
+ /**
291
+ * XXX DO NOT USE. This is a temporary stub function.
292
+ * XXX It WILL be removed in the next major release.
293
+ */
294
+ function destroy() {
295
+ console.warn('Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.');
296
+ }
297
+
298
+ createDebug.enable(createDebug.load());
299
+
300
+ return createDebug;
301
+ }
302
+
303
+ common = setup;
304
+ return common;
305
+ }
306
+
307
+ exports.__require = requireCommon;