-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
155 lines (140 loc) · 4.23 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
let cachedAccessControlToken = null;
async function getAccessControlToken() {
if (cachedAccessControlToken) {
return cachedAccessControlToken;
}
const tokenResp = await fetch(".netlify/functions/generate-token", {
credentials: "include",
});
if (tokenResp.status !== 200) {
cachedAccessControlToken = null;
throw new Error("failed to access control token for user");
}
const { accessControlToken } = await tokenResp.json();
cachedAccessControlToken = accessControlToken;
return accessControlToken;
}
class NetlifyLogsService {
constructor(options = {}) {
this.options = options;
this.logs = [];
this.shouldReconnect = true;
// this.ws = this.connect();
}
destroy() {
this.shouldReconnect = false;
window.clearInterval(this.reconnectTimeout);
this.notifyLogsUpdated.cancel();
this.ws.close();
}
connect(options) {
this.ws = new WebSocket("wss://socketeer.services.netlify.com/build/logs");
this.ws.addEventListener("open", () => {
getAccessControlToken()
.then((accessToken) => {
this.ws.send(
JSON.stringify({
deploy_id: options.deployId || this.options.deployId,
site_id: options.siteId || this.options.siteId,
access_token: accessToken,
})
);
})
.catch((error) => {
console.error(
"NetlifyLogsService failed to get access control token",
error
);
});
});
this.ws.addEventListener("message", (event) => {
try {
const data = JSON.parse(event.data);
const ts = new Date(data.ts).getTime();
if (data.type === "error") {
throw data;
}
this.logs ??= [];
this.logs.push({
id: `${ts}${this.logs.length}`,
timestamp: ts,
message: data.message,
});
this.notifyLogsUpdated();
} catch (e) {
if (e?.type === "error" && e.status === 401) {
console.error("NetlifyLogsService no permission");
this.options.onForbidden?.();
return;
}
console.error(`NetlifyLogsService can't decode socket message`, e);
}
});
this.ws.addEventListener("close", () => {
console.info(`NetlifyLogsService socket closed`);
if (this.shouldReconnect) {
this.reconnectTimeout = window.setTimeout(
() => this.connect(),
this.options.reconnect ?? 1000
);
}
});
this.ws.addEventListener("error", (error) => {
console.error(`NetlifyLogsService socket got error`, error);
this.ws.close();
});
return this.ws;
}
notifyLogsUpdated = (function () {
let timeout;
return function () {
clearTimeout(timeout);
timeout = setTimeout(() => {
this.options.onLogsUpdated?.([...(this.logs ?? [])]);
}, 250);
};
})();
}
const logsService = new NetlifyLogsService({
deployId: "your-deploy-id",
siteId: "your-site-id",
onLogsUpdated: (logs) => {
// Handle updated logs
},
onForbidden: () => {
// Handle forbidden access
},
reconnect: 2000, // Optional reconnect timeout in ms
});
// Initialize the logs service
const netlifyLogs = new NetlifyLogsService({
onLogsUpdated: (logs) => {
// Update UI with new logs
const logContainer = document.querySelector("#logs");
if (logContainer) {
logContainer.innerHTML = logs
.map(
(log) => `
<div class="log-entry">
<span class="timestamp">${new Date(
log.timestamp
).toLocaleTimeString()}</span>
<span class="message">${log.message}</span>
</div>
`
)
.join("");
}
},
onForbidden: () => {
console.error("Access forbidden - please check your credentials");
// Optionally show error message to user
alert("Unable to access logs - permission denied");
},
reconnect: 3000,
});
document.getElementById("connect").addEventListener("click", () => {
const deployId = document.getElementById("deployId").value;
const siteId = document.getElementById("siteId").value;
netlifyLogs.connect({ deployId, siteId });
});