Spaces:
Running
Running
Update app.js
Browse files
app.js
CHANGED
@@ -2,8 +2,10 @@ const express = require('express');
|
|
2 |
const fetch = require('node-fetch');
|
3 |
const zlib = require('zlib');
|
4 |
const { promisify } = require('util');
|
|
|
5 |
|
6 |
const gunzip = promisify(zlib.gunzip);
|
|
|
7 |
|
8 |
const PROJECT_ID = process.env.PROJECT_ID;
|
9 |
const CLIENT_ID = process.env.CLIENT_ID;
|
@@ -28,43 +30,7 @@ function logRequest(req, status, message) {
|
|
28 |
}
|
29 |
|
30 |
async function getAccessToken() {
|
31 |
-
|
32 |
-
|
33 |
-
if (tokenCache.accessToken && now < tokenCache.expiry - 120) {
|
34 |
-
return tokenCache.accessToken;
|
35 |
-
}
|
36 |
-
|
37 |
-
if (tokenCache.refreshPromise) {
|
38 |
-
await tokenCache.refreshPromise;
|
39 |
-
return tokenCache.accessToken;
|
40 |
-
}
|
41 |
-
|
42 |
-
tokenCache.refreshPromise = (async () => {
|
43 |
-
try {
|
44 |
-
const response = await fetch(TOKEN_URL, {
|
45 |
-
method: 'POST',
|
46 |
-
headers: {
|
47 |
-
'Content-Type': 'application/json'
|
48 |
-
},
|
49 |
-
body: JSON.stringify({
|
50 |
-
client_id: CLIENT_ID,
|
51 |
-
client_secret: CLIENT_SECRET,
|
52 |
-
refresh_token: REFRESH_TOKEN,
|
53 |
-
grant_type: 'refresh_token'
|
54 |
-
})
|
55 |
-
});
|
56 |
-
|
57 |
-
const data = await response.json();
|
58 |
-
|
59 |
-
tokenCache.accessToken = data.access_token;
|
60 |
-
tokenCache.expiry = now + data.expires_in;
|
61 |
-
} finally {
|
62 |
-
tokenCache.refreshPromise = null;
|
63 |
-
}
|
64 |
-
})();
|
65 |
-
|
66 |
-
await tokenCache.refreshPromise;
|
67 |
-
return tokenCache.accessToken;
|
68 |
}
|
69 |
|
70 |
function getLocation() {
|
@@ -160,34 +126,23 @@ async function handleRequest(req, res) {
|
|
160 |
res.setHeader('Cache-Control', 'no-cache');
|
161 |
res.setHeader('Connection', 'keep-alive');
|
162 |
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
const line = buffer.slice(0, pos).toString();
|
169 |
-
buffer = buffer.slice(pos + 1);
|
170 |
-
res.write(line + '\n');
|
171 |
-
}
|
172 |
-
});
|
173 |
-
|
174 |
-
response.body.on('end', () => {
|
175 |
-
if (buffer.length > 0) {
|
176 |
-
res.write(buffer.toString());
|
177 |
}
|
178 |
-
res.end();
|
179 |
});
|
180 |
|
181 |
-
response.body
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
message: "An error occurred while streaming the response."
|
188 |
-
}
|
189 |
});
|
190 |
-
|
|
|
191 |
} else {
|
192 |
// ιζ΅εΌεεΊηε€η
|
193 |
const buffer = await response.buffer();
|
@@ -195,17 +150,12 @@ async function handleRequest(req, res) {
|
|
195 |
|
196 |
let data;
|
197 |
if (contentEncoding === 'gzip') {
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
} catch (error) {
|
205 |
-
console.error('Gunzip error:', error);
|
206 |
-
console.log('First 100 bytes of response:', buffer.slice(0, 100).toString('hex'));
|
207 |
-
throw new Error('Failed to decompress the response');
|
208 |
-
}
|
209 |
}
|
210 |
} else {
|
211 |
data = buffer;
|
|
|
2 |
const fetch = require('node-fetch');
|
3 |
const zlib = require('zlib');
|
4 |
const { promisify } = require('util');
|
5 |
+
const stream = require('stream');
|
6 |
|
7 |
const gunzip = promisify(zlib.gunzip);
|
8 |
+
const pipeline = promisify(stream.pipeline);
|
9 |
|
10 |
const PROJECT_ID = process.env.PROJECT_ID;
|
11 |
const CLIENT_ID = process.env.CLIENT_ID;
|
|
|
30 |
}
|
31 |
|
32 |
async function getAccessToken() {
|
33 |
+
// ... (δΏζδΈε)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
}
|
35 |
|
36 |
function getLocation() {
|
|
|
126 |
res.setHeader('Cache-Control', 'no-cache');
|
127 |
res.setHeader('Connection', 'keep-alive');
|
128 |
|
129 |
+
const gunzipStream = zlib.createGunzip();
|
130 |
+
const transformStream = new stream.Transform({
|
131 |
+
transform(chunk, encoding, callback) {
|
132 |
+
this.push(chunk);
|
133 |
+
callback();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
}
|
|
|
135 |
});
|
136 |
|
137 |
+
pipeline(response.body, gunzipStream, transformStream)
|
138 |
+
.then(() => {
|
139 |
+
console.log('Stream processing completed');
|
140 |
+
})
|
141 |
+
.catch((err) => {
|
142 |
+
console.error('Stream processing error:', err);
|
|
|
|
|
143 |
});
|
144 |
+
|
145 |
+
transformStream.pipe(res);
|
146 |
} else {
|
147 |
// ιζ΅εΌεεΊηε€η
|
148 |
const buffer = await response.buffer();
|
|
|
150 |
|
151 |
let data;
|
152 |
if (contentEncoding === 'gzip') {
|
153 |
+
try {
|
154 |
+
data = await gunzip(buffer);
|
155 |
+
} catch (error) {
|
156 |
+
console.error('Gunzip error:', error);
|
157 |
+
console.log('First 100 bytes of response:', buffer.slice(0, 100).toString('hex'));
|
158 |
+
throw new Error('Failed to decompress the response');
|
|
|
|
|
|
|
|
|
|
|
159 |
}
|
160 |
} else {
|
161 |
data = buffer;
|