CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
README.md120 linesDownload Raw Back to fresh
1# fresh2 3[![NPM Version][npm-image]][npm-url]4[![NPM Downloads][downloads-image]][downloads-url]5[![Node.js Version][node-version-image]][node-version-url]6[![Build Status][travis-image]][travis-url]7[![Test Coverage][coveralls-image]][coveralls-url]8 9HTTP response freshness testing10 11## Installation12 13This is a [Node.js](https://nodejs.org/en/) module available through the14[npm registry](https://www.npmjs.com/). Installation is done using the15[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):16 17```18$ npm install fresh19```20 21## API22 23<!-- eslint-disable no-unused-vars -->24 25```js26var fresh = require('fresh')27```28 29### fresh(reqHeaders, resHeaders)30 31Check freshness of the response using request and response headers.32 33When the response is still "fresh" in the client's cache `true` is34returned, otherwise `false` is returned to indicate that the client35cache is now stale and the full response should be sent.36 37When a client sends the `Cache-Control: no-cache` request header to38indicate an end-to-end reload request, this module will return `false`39to make handling these requests transparent.40 41## Known Issues42 43This module is designed to only follow the HTTP specifications, not44to work-around all kinda of client bugs (especially since this module45typically does not recieve enough information to understand what the46client actually is).47 48There is a known issue that in certain versions of Safari, Safari49will incorrectly make a request that allows this module to validate50freshness of the resource even when Safari does not have a51representation of the resource in the cache. The module52[jumanji](https://www.npmjs.com/package/jumanji) can be used in53an Express application to work-around this issue and also provides54links to further reading on this Safari bug.55 56## Example57 58### API usage59 60<!-- eslint-disable no-redeclare, no-undef -->61 62```js63var reqHeaders = { 'if-none-match': '"foo"' }64var resHeaders = { 'etag': '"bar"' }65fresh(reqHeaders, resHeaders)66// => false67 68var reqHeaders = { 'if-none-match': '"foo"' }69var resHeaders = { 'etag': '"foo"' }70fresh(reqHeaders, resHeaders)71// => true72```73 74### Using with Node.js http server75 76```js77var fresh = require('fresh')78var http = require('http')79 80var server = http.createServer(function (req, res) {81  // perform server logic82  // ... including adding ETag / Last-Modified response headers83 84  if (isFresh(req, res)) {85    // client has a fresh copy of resource86    res.statusCode = 30487    res.end()88    return89  }90 91  // send the resource92  res.statusCode = 20093  res.end('hello, world!')94})95 96function isFresh (req, res) {97  return fresh(req.headers, {98    'etag': res.getHeader('ETag'),99    'last-modified': res.getHeader('Last-Modified')100  })101}102 103server.listen(3000)104```105 106## License107 108[MIT](LICENSE)109 110[npm-image]: https://img.shields.io/npm/v/fresh.svg111[npm-url]: https://npmjs.org/package/fresh112[node-version-image]: https://img.shields.io/node/v/fresh.svg113[node-version-url]: https://nodejs.org/en/114[travis-image]: https://img.shields.io/travis/jshttp/fresh/master.svg115[travis-url]: https://travis-ci.org/jshttp/fresh116[coveralls-image]: https://img.shields.io/coveralls/jshttp/fresh/master.svg117[coveralls-url]: https://coveralls.io/r/jshttp/fresh?branch=master118[downloads-image]: https://img.shields.io/npm/dm/fresh.svg119[downloads-url]: https://npmjs.org/package/fresh120