Brunobkr/llama.cpp_AlgMor24_github
ΩFFFΣLLIa • llama.cpp • AlgMor24 ██████╗ ███████╗███████╗███████╗██╗ ██╗ ██╗ █████╗ ██╔═══██╗██╔════╝██╔════╝██╔════╝██║ ██║ ██║██╔══██╗ ██║ ██║█████╗ █████╗ █████╗ ██║ ██║ ██║███████║ ██║ ██║██╔══╝ ██╔══╝ ██╔══╝ ██║ ██║ ██║██╔══██║ ╚██████╔╝██║ ██║ ███████╗███████╗███████╗██║██║ ██║ ╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝╚═╝ ╚═╝ High-Performance LLM / VLM Inference & Autonomous Agentic Ecosystem… See the full description on the dataset page: https://huggingface.co/datasets/Brunobkr/llama.cpp_AlgMor24_github.
03.1k
1#! /usr/bin/env node2 3'use strict';4 5const path = require('path');6const fs = require('fs');7const url = require('url');8const { Readable } = require('stream');9const buffer = require('buffer');10const mime = require('mime');11const urlJoin = require('url-join');12const showDir = require('./show-dir');13const version = require('../../package.json').version;14const status = require('./status-handlers');15const generateEtag = require('./etag');16const optsParser = require('./opts');17const htmlEncodingSniffer = require('html-encoding-sniffer');18 19let httpServerCore = null;20 21function decodePathname(pathname) {22 const pieces = pathname.replace(/\\/g, '/').split('/');23 24 const normalized = path.normalize(pieces.map((rawPiece) => {25 const piece = decodeURIComponent(rawPiece);26 27 if (process.platform === 'win32' && /\\/.test(piece)) {28 throw new Error('Invalid forward slash character');29 }30 31 return piece;32 }).join('/'));33 return process.platform === 'win32'34 ? normalized.replace(/\\/g, '/') : normalized;35}36 37const nonUrlSafeCharsRgx = /[\x00-\x1F\x20\x7F-\uFFFF]+/g;38function ensureUriEncoded(text) {39 return text40 return String(text).replace(nonUrlSafeCharsRgx, encodeURIComponent);41}42 43// Check to see if we should try to compress a file with gzip.44function shouldCompressGzip(req) {45 const headers = req.headers;46 47 return headers && headers['accept-encoding'] &&48 headers['accept-encoding']49 .split(',')50 .some(el => ['*', 'compress', 'gzip', 'deflate'].indexOf(el.trim()) !== -1)51 ;52}53 54function shouldCompressBrotli(req) {55 const headers = req.headers;56 57 return headers && headers['accept-encoding'] &&58 headers['accept-encoding']59 .split(',')60 .some(el => ['*', 'br'].indexOf(el.trim()) !== -1)61 ;62}63 64function hasGzipId12(gzipped, cb) {65 const stream = fs.createReadStream(gzipped, { start: 0, end: 1 });66 let buffer = Buffer.from('');67 let hasBeenCalled = false;68 69 stream.on('data', (chunk) => {70 buffer = Buffer.concat([buffer, chunk], 2);71 });72 73 stream.on('error', (err) => {74 if (hasBeenCalled) {75 throw err;76 }77 78 hasBeenCalled = true;79 cb(err);80 });81 82 stream.on('close', () => {83 if (hasBeenCalled) {84 return;85 }86 87 hasBeenCalled = true;88 cb(null, buffer[0] === 31 && buffer[1] === 139);89 });90}91 92 93module.exports = function createMiddleware(_dir, _options) {94 let dir;95 let options;96 if (typeof _dir === 'string') {97 dir = _dir;98 options = _options;99 } else {100 options = _dir;101 dir = options.root;102 }103 104 const root = path.join(path.resolve(dir), '/');105 const opts = optsParser(options);106 const cache = opts.cache;107 const autoIndex = opts.autoIndex;108 const baseDir = opts.baseDir;109 let defaultExt = opts.defaultExt;110 const handleError = opts.handleError;111 const headers = opts.headers;112 const weakEtags = opts.weakEtags;113 const handleOptionsMethod = opts.handleOptionsMethod;114 115 opts.root = dir;116 if (defaultExt && /^\./.test(defaultExt)) {117 defaultExt = defaultExt.replace(/^\./, '');118 }119 120 // Support hashes and .types files in mimeTypes @since 0.8121 if (opts.mimeTypes) {122 try {123 // You can pass a JSON blob here---useful for CLI use124 opts.mimeTypes = JSON.parse(opts.mimeTypes);125 } catch (e) {126 // swallow parse errors, treat this as a string mimetype input127 }128 if (typeof opts.mimeTypes === 'string') {129 mime.load(opts.mimeTypes);130 } else if (typeof opts.mimeTypes === 'object') {131 mime.define(opts.mimeTypes);132 }133 }134 135 function shouldReturn304(req, serverLastModified, serverEtag) {136 if (!req || !req.headers) {137 return false;138 }139 140 const clientModifiedSince = req.headers['if-modified-since'];141 const clientEtag = req.headers['if-none-match'];142 let clientModifiedDate;143 144 if (!clientModifiedSince && !clientEtag) {145 // Client did not provide any conditional caching headers146 return false;147 }148 149 if (clientModifiedSince) {150 // Catch "illegal access" dates that will crash v8151 try {152 clientModifiedDate = new Date(Date.parse(clientModifiedSince));153 } catch (err) {154 return false;155 }156 157 if (clientModifiedDate.toString() === 'Invalid Date') {158 return false;159 }160 // If the client's copy is older than the server's, don't return 304161 if (clientModifiedDate < new Date(serverLastModified)) {162 return false;163 }164 }165 166 if (clientEtag) {167 // Do a strong or weak etag comparison based on setting168 // https://www.ietf.org/rfc/rfc2616.txt Section 13.3.3169 if (opts.weakCompare && clientEtag !== serverEtag170 && clientEtag !== `W/${serverEtag}` && `W/${clientEtag}` !== serverEtag) {171 return false;172 }173 if (!opts.weakCompare && (clientEtag !== serverEtag || clientEtag.indexOf('W/') === 0)) {174 return false;175 }176 }177 178 return true;179 }180 181 return function middleware(req, res, next) {182 // Figure out the path for the file from the given url183 const parsed = url.parse(req.url);184 let pathname = null;185 let file = null;186 let gzippedFile = null;187 let brotliFile = null;188 189 try {190 decodeURIComponent(req.url); // check validity of url191 pathname = decodePathname(parsed.pathname);192 } catch (err) {193 status[400](res, next, { error: err });194 return;195 }196 197 file = path.normalize(198 path.join(199 root,200 path.relative(path.join('/', baseDir), pathname)201 )202 );203 // determine compressed forms if they were to exist204 gzippedFile = `${file}.gz`;205 brotliFile = `${file}.br`;206 207 Object.keys(headers).forEach((key) => {208 res.setHeader(key, headers[key]);209 });210 211 if (req.method === 'OPTIONS' && handleOptionsMethod) {212 res.end();213 return;214 }215 216 // TODO: This check is broken, which causes the 403 on the217 // expected 404.218 if (file.slice(0, root.length) !== root) {219 status[403](res, next);220 return;221 }222 223 if (req.method && (req.method !== 'GET' && req.method !== 'HEAD')) {224 status[405](res, next);225 return;226 }227 228 229 function serve(stat) {230 // Do a MIME lookup, fall back to octet-stream and handle gzip231 // and brotli special case.232 const defaultType = opts.contentType || 'application/octet-stream';233 let contentType = mime.lookup(file, defaultType);234 const range = (req.headers && req.headers.range);235 const lastModified = (new Date(stat.mtime)).toUTCString();236 const etag = generateEtag(stat, weakEtags);237 let cacheControl = cache;238 let stream = null;239 if (contentType && isTextFile(contentType)) {240 if (stat.size < buffer.constants.MAX_LENGTH) {241 const bytes = fs.readFileSync(file);242 const sniffedEncoding = htmlEncodingSniffer(bytes, {243 defaultEncoding: 'UTF-8'244 });245 contentType += `; charset=${sniffedEncoding}`;246 stream = Readable.from(bytes)247 } else {248 // Assume text types are utf8249 contentType += '; charset=UTF-8';250 }251 }252 253 if (file === gzippedFile) { // is .gz picked up254 res.setHeader('Content-Encoding', 'gzip');255 // strip gz ending and lookup mime type256 contentType = mime.lookup(path.basename(file, '.gz'), defaultType);257 } else if (file === brotliFile) { // is .br picked up258 res.setHeader('Content-Encoding', 'br');259 // strip br ending and lookup mime type260 contentType = mime.lookup(path.basename(file, '.br'), defaultType);261 }262 263 if (typeof cacheControl === 'function') {264 cacheControl = cache(pathname);265 }266 if (typeof cacheControl === 'number') {267 cacheControl = `max-age=${cacheControl}`;268 }269 270 if (range) {271 const total = stat.size;272 const parts = range.trim().replace(/bytes=/, '').split('-');273 const partialstart = parts[0];274 const partialend = parts[1];275 const start = parseInt(partialstart, 10);276 const end = Math.min(277 total - 1,278 partialend ? parseInt(partialend, 10) : total - 1279 );280 const chunksize = (end - start) + 1;281 let fstream = null;282 283 if (start > end || isNaN(start) || isNaN(end)) {284 status['416'](res, next);285 return;286 }287 288 fstream = fs.createReadStream(file, { start, end });289 fstream.on('error', (err) => {290 status['500'](res, next, { error: err });291 });292 res.on('close', () => {293 fstream.destroy();294 });295 res.writeHead(206, {296 'Content-Range': `bytes ${start}-${end}/${total}`,297 'Accept-Ranges': 'bytes',298 'Content-Length': chunksize,299 'Content-Type': contentType,300 'cache-control': cacheControl,301 'last-modified': lastModified,302 etag,303 });304 fstream.pipe(res);305 return;306 }307 308 // TODO: Helper for this, with default headers.309 res.setHeader('cache-control', cacheControl);310 res.setHeader('last-modified', lastModified);311 res.setHeader('etag', etag);312 313 // Return a 304 if necessary314 if (shouldReturn304(req, lastModified, etag)) {315 status[304](res, next);316 return;317 }318 319 res.setHeader('content-length', stat.size);320 res.setHeader('content-type', contentType);321 322 // set the response statusCode if we have a request statusCode.323 // This only can happen if we have a 404 with some kind of 404.html324 // In all other cases where we have a file we serve the 200325 res.statusCode = req.statusCode || 200;326 327 if (req.method === 'HEAD') {328 res.end();329 return;330 }331 332 // stream may already have been assigned during encoding sniffing.333 if (stream === null) {334 stream = fs.createReadStream(file);335 }336 337 stream.pipe(res);338 stream.on('error', (err) => {339 status['500'](res, next, { error: err });340 });341 stream.on('close', () => {342 stream.destroy();343 })344 }345 346 347 function statFile() {348 try {349 fs.stat(file, (err, stat) => {350 if (err && (err.code === 'ENOENT' || err.code === 'ENOTDIR')) {351 if (req.statusCode === 404) {352 // This means we're already trying ./404.html and can not find it.353 // So send plain text response with 404 status code354 status[404](res, next);355 } else if (!path.extname(parsed.pathname).length && defaultExt) {356 // If there is no file extension in the path and we have a default357 // extension try filename and default extension combination before rendering 404.html.358 middleware({359 url: `${parsed.pathname}.${defaultExt}${(parsed.search) ? parsed.search : ''}`,360 headers: req.headers,361 }, res, next);362 } else {363 // Try to serve default ./404.html364 const rawUrl = (handleError ? `/${path.join(baseDir, `404.${defaultExt}`)}` : req.url);365 const encodedUrl = ensureUriEncoded(rawUrl);366 middleware({367 url: encodedUrl,368 headers: req.headers,369 statusCode: 404,370 }, res, next);371 }372 } else if (err) {373 status[500](res, next, { error: err });374 } else if (stat.isDirectory()) {375 if (!autoIndex && !opts.showDir) {376 status[404](res, next);377 return;378 }379 380 381 // 302 to / if necessary382 if (!pathname.match(/\/$/)) {383 res.statusCode = 302;384 const q = parsed.query ? `?${parsed.query}` : '';385 res.setHeader(386 'location',387 ensureUriEncoded(`${parsed.pathname}/${q}`)388 );389 res.end();390 return;391 }392 393 if (autoIndex) {394 middleware({395 url: urlJoin(396 encodeURIComponent(pathname),397 `/index.${defaultExt}`398 ),399 headers: req.headers,400 }, res, (autoIndexError) => {401 if (autoIndexError) {402 status[500](res, next, { error: autoIndexError });403 return;404 }405 if (opts.showDir) {406 showDir(opts, stat)(req, res);407 return;408 }409 410 status[403](res, next);411 });412 return;413 }414 415 if (opts.showDir) {416 showDir(opts, stat)(req, res);417 }418 } else {419 serve(stat);420 }421 });422 } catch (err) {423 status[500](res, next, { error: err.message });424 }425 }426 427 function isTextFile(mimeType) {428 return (/^text\/|^application\/(javascript|json)/).test(mimeType);429 }430 431 // serve gzip file if exists and is valid432 function tryServeWithGzip() {433 try {434 fs.stat(gzippedFile, (err, stat) => {435 if (!err && stat.isFile()) {436 hasGzipId12(gzippedFile, (gzipErr, isGzip) => {437 if (!gzipErr && isGzip) {438 file = gzippedFile;439 serve(stat);440 } else {441 statFile();442 }443 });444 } else {445 statFile();446 }447 });448 } catch (err) {449 status[500](res, next, { error: err.message });450 }451 }452 453 // serve brotli file if exists, otherwise try gzip454 function tryServeWithBrotli(shouldTryGzip) {455 try {456 fs.stat(brotliFile, (err, stat) => {457 if (!err && stat.isFile()) {458 file = brotliFile;459 serve(stat);460 } else if (shouldTryGzip) {461 tryServeWithGzip();462 } else {463 statFile();464 }465 });466 } catch (err) {467 status[500](res, next, { error: err.message });468 }469 }470 471 const shouldTryBrotli = opts.brotli && shouldCompressBrotli(req);472 const shouldTryGzip = opts.gzip && shouldCompressGzip(req);473 // always try brotli first, next try gzip, finally serve without compression474 if (shouldTryBrotli) {475 tryServeWithBrotli(shouldTryGzip);476 } else if (shouldTryGzip) {477 tryServeWithGzip();478 } else {479 statFile();480 }481 };482};483 484 485httpServerCore = module.exports;486httpServerCore.version = version;487httpServerCore.showDir = showDir;488 