@leftium/gg 0.0.13 → 0.0.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/dist/gg.d.ts +1 -1
- package/dist/gg.js +83 -48
- package/package.json +1 -1
package/dist/gg.d.ts
CHANGED
package/dist/gg.js
CHANGED
|
@@ -1,11 +1,36 @@
|
|
|
1
|
-
import * as dotenv from 'dotenv';
|
|
2
1
|
import debugFactory from 'debug';
|
|
3
2
|
import ErrorStackParser from 'error-stack-parser';
|
|
4
3
|
import { BROWSER } from 'esm-env';
|
|
5
|
-
|
|
4
|
+
// Helper to detect if we're running in CloudFlare Workers
|
|
5
|
+
const isCloudflareWorker = () => {
|
|
6
|
+
// Check for CloudFlare Workers-specific global
|
|
7
|
+
const globalWithWorkerAPIs = globalThis;
|
|
8
|
+
return (typeof globalThis !== 'undefined' &&
|
|
9
|
+
'caches' in globalThis &&
|
|
10
|
+
typeof globalWithWorkerAPIs.WebSocketPair !== 'undefined');
|
|
11
|
+
};
|
|
12
|
+
// Check if we're in CloudFlare Workers and warn early
|
|
13
|
+
if (isCloudflareWorker()) {
|
|
14
|
+
console.warn('gg: CloudFlare not supported.');
|
|
15
|
+
}
|
|
16
|
+
// Try to load Node.js modules if not in CloudFlare Workers
|
|
17
|
+
let dotenvModule = null;
|
|
18
|
+
let httpModule = null;
|
|
19
|
+
if (!isCloudflareWorker() && !BROWSER) {
|
|
20
|
+
try {
|
|
21
|
+
dotenvModule = await import('dotenv');
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
httpModule = await import('http');
|
|
25
|
+
// Failed to import Node.js modules
|
|
26
|
+
console.warn('gg: Node.js modules not available');
|
|
27
|
+
}
|
|
28
|
+
}
|
|
6
29
|
function findAvailablePort(startingPort) {
|
|
30
|
+
if (!httpModule)
|
|
31
|
+
return Promise.resolve(startingPort);
|
|
7
32
|
return new Promise((resolve) => {
|
|
8
|
-
const server =
|
|
33
|
+
const server = httpModule.createServer();
|
|
9
34
|
server.listen(startingPort, () => {
|
|
10
35
|
const actualPort = server?.address()?.port;
|
|
11
36
|
server.close(() => resolve(actualPort));
|
|
@@ -24,9 +49,13 @@ function getServerPort() {
|
|
|
24
49
|
// Resolve the promise with the detected port
|
|
25
50
|
resolve(currentPort);
|
|
26
51
|
}
|
|
52
|
+
else if (isCloudflareWorker()) {
|
|
53
|
+
// CloudFlare Workers - return default port
|
|
54
|
+
resolve('5173');
|
|
55
|
+
}
|
|
27
56
|
else {
|
|
28
57
|
// Node.js environment
|
|
29
|
-
const startingPort = Number(process
|
|
58
|
+
const startingPort = Number(process?.env?.PORT) || 5173; // Default to Vite's default port
|
|
30
59
|
findAvailablePort(startingPort).then((actualPort) => {
|
|
31
60
|
resolve(actualPort);
|
|
32
61
|
});
|
|
@@ -36,8 +65,8 @@ function getServerPort() {
|
|
|
36
65
|
const timestampRegex = /(\?t=\d+)?$/;
|
|
37
66
|
const port = await getServerPort();
|
|
38
67
|
const ggConfig = {
|
|
39
|
-
enabled:
|
|
40
|
-
showHints:
|
|
68
|
+
enabled: !isCloudflareWorker(), // Disable in CloudFlare Workers
|
|
69
|
+
showHints: !isCloudflareWorker(), // Don't show hints in CloudFlare Workers
|
|
41
70
|
editorLink: false,
|
|
42
71
|
openInEditorUrlTemplate: `http://localhost:${port}/__open-in-editor?file=$FILENAME`,
|
|
43
72
|
// The srcRoot contains all source files.
|
|
@@ -56,44 +85,8 @@ let maxCallpointLength = 0;
|
|
|
56
85
|
function openInEditorUrl(fileName) {
|
|
57
86
|
return ggConfig.openInEditorUrlTemplate.replace('$FILENAME', encodeURIComponent(fileName).replaceAll('%2F', '/'));
|
|
58
87
|
}
|
|
59
|
-
// http://localhost:5173/__open-in-editor?file=src%2Froutes%2F%2Bpage.svelte
|
|
60
|
-
const ggLog = debugFactory('gg');
|
|
61
|
-
// Log some gg info to the JS console/terminal:
|
|
62
|
-
if (ggConfig.showHints) {
|
|
63
|
-
const ggLogTest = ggLog.extend('TEST');
|
|
64
|
-
let ggMessage = '\n';
|
|
65
|
-
// Utilities for forming ggMessage:
|
|
66
|
-
const message = (s) => (ggMessage += `${s}\n`);
|
|
67
|
-
const checkbox = (test) => (test ? '✅' : '❌');
|
|
68
|
-
const makeHint = (test, ifTrue, ifFalse = '') => (test ? ifTrue : ifFalse);
|
|
69
|
-
console.log(`Loaded gg module. Checking configuration...`);
|
|
70
|
-
if (ggConfig.enabled && ggLogTest.enabled) {
|
|
71
|
-
gg('If you can see this logg, gg configured correctly!');
|
|
72
|
-
message(`No problems detected:`);
|
|
73
|
-
if (BROWSER) {
|
|
74
|
-
message(`ℹ️ If gg output still not visible above, enable "Verbose" log level in browser DevTools.`);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
else {
|
|
78
|
-
message(`Problems detected; fix all ❌:`);
|
|
79
|
-
}
|
|
80
|
-
const hint = makeHint(!ggConfig.enabled, ' (Update value in gg.ts file.)');
|
|
81
|
-
message(`${checkbox(ggConfig.enabled)} ggConfig.enabled: ${ggConfig.enabled}${hint}`);
|
|
82
|
-
if (BROWSER) {
|
|
83
|
-
const hint = makeHint(!ggLogTest.enabled, " (Try `localStorage.debug = 'gg:*'`)");
|
|
84
|
-
message(`${checkbox(ggLogTest.enabled)} localStorage.debug: ${localStorage?.debug}${hint}`);
|
|
85
|
-
const { status } = await fetch('/__open-in-editor?file=+');
|
|
86
|
-
message(makeHint(status === 222, `✅ (optional) open-in-editor vite plugin detected! (status code: ${status})`, `⚠️ (optional) open-in-editor vite plugin not detected. (status code: ${status}.) Add plugin in vite.config.ts`));
|
|
87
|
-
}
|
|
88
|
-
else {
|
|
89
|
-
const hint = makeHint(!ggLogTest.enabled, ' (Try `DEBUG=gg:* npm dev`)');
|
|
90
|
-
dotenv.config(); // Load the environment variables
|
|
91
|
-
message(`${checkbox(ggLogTest.enabled)} DEBUG env variable: ${process?.env?.DEBUG}${hint}`);
|
|
92
|
-
}
|
|
93
|
-
console.log(ggMessage);
|
|
94
|
-
}
|
|
95
88
|
export function gg(...args) {
|
|
96
|
-
if (!ggConfig.enabled) {
|
|
89
|
+
if (!ggConfig.enabled || isCloudflareWorker()) {
|
|
97
90
|
return args.length ? args[0] : { url: '', stack: [] };
|
|
98
91
|
}
|
|
99
92
|
// Ignore first stack frame, which is always the call to gg() itself.
|
|
@@ -112,9 +105,9 @@ export function gg(...args) {
|
|
|
112
105
|
if (callpoint.length < 80 && callpoint.length > maxCallpointLength) {
|
|
113
106
|
maxCallpointLength = callpoint.length;
|
|
114
107
|
}
|
|
115
|
-
const namespace =
|
|
108
|
+
const namespace = `gg:${callpoint.padEnd(maxCallpointLength, ' ')}${ggConfig.editorLink ? url : ''}`;
|
|
116
109
|
const ggLogFunction = namespaceToLogFunction.get(namespace) ||
|
|
117
|
-
namespaceToLogFunction.set(namespace,
|
|
110
|
+
namespaceToLogFunction.set(namespace, debugFactory(namespace)).get(namespace);
|
|
118
111
|
if (!args.length) {
|
|
119
112
|
ggLogFunction(` 📝📝 ${url} 👀👀`);
|
|
120
113
|
return {
|
|
@@ -124,8 +117,50 @@ export function gg(...args) {
|
|
|
124
117
|
stack
|
|
125
118
|
};
|
|
126
119
|
}
|
|
127
|
-
|
|
120
|
+
// Handle the case where args might be empty or have any number of arguments
|
|
121
|
+
if (args.length === 1) {
|
|
122
|
+
ggLogFunction(args[0]);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
ggLogFunction(args[0], ...args.slice(1));
|
|
126
|
+
}
|
|
128
127
|
return args[0];
|
|
129
128
|
}
|
|
130
|
-
gg.disable = debugFactory.disable;
|
|
131
|
-
gg.enable = debugFactory.enable;
|
|
129
|
+
gg.disable = isCloudflareWorker() ? () => { } : debugFactory.disable;
|
|
130
|
+
gg.enable = isCloudflareWorker() ? () => { } : debugFactory.enable;
|
|
131
|
+
// Log some gg info to the JS console/terminal:
|
|
132
|
+
if (ggConfig.showHints && !isCloudflareWorker()) {
|
|
133
|
+
const ggLogTest = debugFactory('gg:TEST');
|
|
134
|
+
let ggMessage = '\n';
|
|
135
|
+
// Utilities for forming ggMessage:
|
|
136
|
+
const message = (s) => (ggMessage += `${s}\n`);
|
|
137
|
+
const checkbox = (test) => (test ? '✅' : '❌');
|
|
138
|
+
const makeHint = (test, ifTrue, ifFalse = '') => (test ? ifTrue : ifFalse);
|
|
139
|
+
console.log(`Loaded gg module. Checking configuration...`);
|
|
140
|
+
if (ggConfig.enabled && ggLogTest.enabled) {
|
|
141
|
+
gg('If you can see this logg, gg configured correctly!');
|
|
142
|
+
message(`No problems detected:`);
|
|
143
|
+
if (BROWSER) {
|
|
144
|
+
message(`ℹ️ If gg output still not visible above, enable "Verbose" log level in browser DevTools.`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
message(`Problems detected; fix all ❌:`);
|
|
149
|
+
}
|
|
150
|
+
const hint = makeHint(!ggConfig.enabled, ' (Update value in gg.ts file.)');
|
|
151
|
+
message(`${checkbox(ggConfig.enabled)} ggConfig.enabled: ${ggConfig.enabled}${hint}`);
|
|
152
|
+
if (BROWSER) {
|
|
153
|
+
const hint = makeHint(!ggLogTest.enabled, " (Try `localStorage.debug = 'gg:*'`)");
|
|
154
|
+
message(`${checkbox(ggLogTest.enabled)} localStorage.debug: ${localStorage?.debug}${hint}`);
|
|
155
|
+
const { status } = await fetch('/__open-in-editor?file=+');
|
|
156
|
+
message(makeHint(status === 222, `✅ (optional) open-in-editor vite plugin detected! (status code: ${status})`, `⚠️ (optional) open-in-editor vite plugin not detected. (status code: ${status}.) Add plugin in vite.config.ts`));
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
const hint = makeHint(!ggLogTest.enabled, ' (Try `DEBUG=gg:* npm dev`)');
|
|
160
|
+
if (dotenvModule) {
|
|
161
|
+
dotenvModule.config(); // Load the environment variables
|
|
162
|
+
}
|
|
163
|
+
message(`${checkbox(ggLogTest.enabled)} DEBUG env variable: ${process?.env?.DEBUG}${hint}`);
|
|
164
|
+
}
|
|
165
|
+
console.log(ggMessage);
|
|
166
|
+
}
|