Delete server.mjs
Browse files- server.mjs +0 -166
server.mjs
DELETED
|
@@ -1,166 +0,0 @@
|
|
| 1 |
-
import http from 'node:http';
|
| 2 |
-
import { resolveMatchStream } from './resolver.mjs';
|
| 3 |
-
|
| 4 |
-
const PORT = Number(process.env.PORT || 7860);
|
| 5 |
-
|
| 6 |
-
const CACHE_TTL_MS = Math.max(
|
| 7 |
-
0,
|
| 8 |
-
Number(process.env.RESOLVE_CACHE_TTL_MS || 60000)
|
| 9 |
-
);
|
| 10 |
-
|
| 11 |
-
const cache = new Map();
|
| 12 |
-
const inFlight = new Map();
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
function sendJson(res, statusCode, payload) {
|
| 16 |
-
const body = JSON.stringify(payload, null, 2);
|
| 17 |
-
|
| 18 |
-
res.writeHead(statusCode, {
|
| 19 |
-
'Content-Type': 'application/json; charset=utf-8',
|
| 20 |
-
'Cache-Control': 'no-store',
|
| 21 |
-
'Access-Control-Allow-Origin': '*',
|
| 22 |
-
'Access-Control-Allow-Headers': 'Content-Type',
|
| 23 |
-
'Access-Control-Allow-Methods': 'GET, OPTIONS'
|
| 24 |
-
});
|
| 25 |
-
|
| 26 |
-
res.end(body);
|
| 27 |
-
}
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
async function resolveCached(matchId) {
|
| 31 |
-
const now = Date.now();
|
| 32 |
-
const cached = cache.get(matchId);
|
| 33 |
-
|
| 34 |
-
if (
|
| 35 |
-
cached &&
|
| 36 |
-
CACHE_TTL_MS > 0 &&
|
| 37 |
-
now - cached.createdAt < CACHE_TTL_MS
|
| 38 |
-
) {
|
| 39 |
-
return {
|
| 40 |
-
...cached.result,
|
| 41 |
-
cached: true
|
| 42 |
-
};
|
| 43 |
-
}
|
| 44 |
-
|
| 45 |
-
if (inFlight.has(matchId)) {
|
| 46 |
-
return inFlight.get(matchId);
|
| 47 |
-
}
|
| 48 |
-
|
| 49 |
-
const job = resolveMatchStream(matchId)
|
| 50 |
-
.then(result => {
|
| 51 |
-
/*
|
| 52 |
-
* Cache successful stream resolutions only.
|
| 53 |
-
* Waiting/not-found results should be checked again next click.
|
| 54 |
-
*/
|
| 55 |
-
if (result?.success === true && result?.stream_url) {
|
| 56 |
-
cache.set(matchId, {
|
| 57 |
-
createdAt: Date.now(),
|
| 58 |
-
result
|
| 59 |
-
});
|
| 60 |
-
}
|
| 61 |
-
|
| 62 |
-
return {
|
| 63 |
-
...result,
|
| 64 |
-
cached: false
|
| 65 |
-
};
|
| 66 |
-
})
|
| 67 |
-
.finally(() => {
|
| 68 |
-
inFlight.delete(matchId);
|
| 69 |
-
});
|
| 70 |
-
|
| 71 |
-
inFlight.set(matchId, job);
|
| 72 |
-
|
| 73 |
-
return job;
|
| 74 |
-
}
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
const server = http.createServer(async (req, res) => {
|
| 78 |
-
const url = new URL(
|
| 79 |
-
req.url || '/',
|
| 80 |
-
`http://${req.headers.host || 'localhost'}`
|
| 81 |
-
);
|
| 82 |
-
|
| 83 |
-
if (req.method === 'OPTIONS') {
|
| 84 |
-
res.writeHead(204, {
|
| 85 |
-
'Access-Control-Allow-Origin': '*',
|
| 86 |
-
'Access-Control-Allow-Headers': 'Content-Type',
|
| 87 |
-
'Access-Control-Allow-Methods': 'GET, OPTIONS'
|
| 88 |
-
});
|
| 89 |
-
res.end();
|
| 90 |
-
return;
|
| 91 |
-
}
|
| 92 |
-
|
| 93 |
-
if (
|
| 94 |
-
req.method === 'GET' &&
|
| 95 |
-
(url.pathname === '/' || url.pathname === '/health')
|
| 96 |
-
) {
|
| 97 |
-
sendJson(res, 200, {
|
| 98 |
-
status: 'ok',
|
| 99 |
-
service: 'fotstream-resolver',
|
| 100 |
-
endpoint: '/resolve?id=MATCH_ID',
|
| 101 |
-
cache_ttl_ms: CACHE_TTL_MS,
|
| 102 |
-
active_resolutions: inFlight.size
|
| 103 |
-
});
|
| 104 |
-
return;
|
| 105 |
-
}
|
| 106 |
-
|
| 107 |
-
if (
|
| 108 |
-
req.method === 'GET' &&
|
| 109 |
-
url.pathname === '/resolve'
|
| 110 |
-
) {
|
| 111 |
-
const matchId =
|
| 112 |
-
String(url.searchParams.get('id') || '').trim();
|
| 113 |
-
|
| 114 |
-
if (!matchId) {
|
| 115 |
-
sendJson(res, 400, {
|
| 116 |
-
success: false,
|
| 117 |
-
error: 'Missing id parameter',
|
| 118 |
-
example: '/resolve?id=5970091'
|
| 119 |
-
});
|
| 120 |
-
return;
|
| 121 |
-
}
|
| 122 |
-
|
| 123 |
-
try {
|
| 124 |
-
const result =
|
| 125 |
-
await resolveCached(matchId);
|
| 126 |
-
|
| 127 |
-
/*
|
| 128 |
-
* "waiting" is not an HTTP failure.
|
| 129 |
-
* Blogger can inspect result.success/status.
|
| 130 |
-
*/
|
| 131 |
-
sendJson(res, 200, result);
|
| 132 |
-
} catch (error) {
|
| 133 |
-
console.error(
|
| 134 |
-
`Resolve error for ${matchId}:`,
|
| 135 |
-
error
|
| 136 |
-
);
|
| 137 |
-
|
| 138 |
-
sendJson(
|
| 139 |
-
res,
|
| 140 |
-
Number(error?.statusCode || 500),
|
| 141 |
-
{
|
| 142 |
-
success: false,
|
| 143 |
-
status: 'error',
|
| 144 |
-
match_id: matchId,
|
| 145 |
-
error:
|
| 146 |
-
error?.message ||
|
| 147 |
-
'Failed to resolve stream'
|
| 148 |
-
}
|
| 149 |
-
);
|
| 150 |
-
}
|
| 151 |
-
|
| 152 |
-
return;
|
| 153 |
-
}
|
| 154 |
-
|
| 155 |
-
sendJson(res, 404, {
|
| 156 |
-
success: false,
|
| 157 |
-
error: 'Not found'
|
| 158 |
-
});
|
| 159 |
-
});
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
server.listen(PORT, '0.0.0.0', () => {
|
| 163 |
-
console.log(
|
| 164 |
-
`FotStream resolver listening on port ${PORT}`
|
| 165 |
-
);
|
| 166 |
-
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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