├── .gitignore ├── .travis.yml ├── ISSUE_TEMPLATE ├── LICENSE.md ├── PULL_REQUEST_TEMPLATE ├── README.md ├── appveyor.yml ├── index.js ├── package.json ├── protocol.js └── test └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /.nyc_output 3 | /test/cache 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | sudo: false 3 | node_js: 4 | - "7" 5 | - "6" 6 | - "4" 7 | -------------------------------------------------------------------------------- /ISSUE_TEMPLATE: -------------------------------------------------------------------------------- 1 | 8 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | To the extent possible under law, maintainers for this project have waived all copyright and related or neighboring rights to this project. 2 | 3 | For more information on this waiver, see: https://creativecommons.org/publicdomain/zero/1.0/ 4 | -------------------------------------------------------------------------------- /PULL_REQUEST_TEMPLATE: -------------------------------------------------------------------------------- 1 | 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fetch-cache [![npm version](https://img.shields.io/npm/v/fetch-cache.svg)](https://npm.im/fetch-cache) [![license](https://img.shields.io/npm/l/fetch-cache.svg)](https://npm.im/fetch-cache) [![Travis](https://img.shields.io/travis/zkat/fetch-cache.svg)](https://travis-ci.org/zkat/fetch-cache) [![AppVeyor](https://ci.appveyor.com/api/projects/status/github/zkat/fetch-cache?svg=true)](https://ci.appveyor.com/project/zkat/fetch-cache) [![Coverage Status](https://coveralls.io/repos/github/zkat/fetch-cache/badge.svg?branch=latest)](https://coveralls.io/github/zkat/fetch-cache?branch=latest) 2 | 3 | [`fetch-cache`](https://github.com/zkat/fetch-cache) implements the fetch Cache API 4 | 5 | ## Install 6 | 7 | `$ npm install --save fetch-cache` 8 | 9 | ## Table of Contents 10 | 11 | * [Example](#example) 12 | * [Features](#features) 13 | * [Contributing](#contributing) 14 | * [API](#api) 15 | 16 | ### Example 17 | 18 | ```javascript 19 | ``` 20 | 21 | ### Features 22 | 23 | ### Contributing 24 | 25 | The fetch-cache team enthusiastically welcomes contributions and project 26 | participation! There's a bunch of things you can do if you want to contribute! 27 | The [Contributor Guide](CONTRIBUTING.md) has all the information you need for 28 | everything from reporting bugs to contributing entire new features. Please don't 29 | hesitate to jump in if you'd like to, or even ask us questions if something 30 | isn't clear. 31 | 32 | ### API 33 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | environment: 2 | matrix: 3 | - nodejs_version: "7" 4 | - nodejs_version: "6" 5 | - nodejs_version: "4" 6 | 7 | platform: 8 | - x64 9 | 10 | install: 11 | - ps: Install-Product node $env:nodejs_version $env:platform 12 | - npm config set spin false 13 | - npm i -g npm@latest 14 | - npm install 15 | 16 | test_script: 17 | - npm test 18 | 19 | matrix: 20 | fast_finish: true 21 | 22 | build: off 23 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const CacheProtocol = require('./protocol') 4 | const LRU = require('lru-cache') 5 | 6 | const Cache = module.exports = class Cache { 7 | constructor (opts) { 8 | this.lru = new LRU(opts) 9 | } 10 | } 11 | 12 | CacheProtocol.impl(Cache, { 13 | match (req) { 14 | return Promise.resolve(req) 15 | }, 16 | matchAll (req) { 17 | return Promise.resolve([req]) 18 | }, 19 | add (req) { 20 | return Promise.resolve(req) 21 | }, 22 | addAll (reqs) { 23 | return Promise.resolve(reqs) 24 | }, 25 | put (req, res) { 26 | return Promise.resolve(res) 27 | }, 28 | delete () { 29 | return Promise.resolve(false) 30 | }, 31 | keys () { 32 | return Promise.resolve([]) 33 | } 34 | }) 35 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fetch-cache", 3 | "version": "1.0.1", 4 | "description": "Protocol and base implementation the standard fetch Cache API", 5 | "main": "index.js", 6 | "files": [ 7 | "*.js" 8 | ], 9 | "scripts": { 10 | "prerelease": "npm t", 11 | "postrelease": "npm publish && git push --follow-tags", 12 | "pretest": "standard", 13 | "release": "standard-version -s", 14 | "test": "tap -J --coverage test/*.js", 15 | "update-coc": "weallbehave -o . && git add CODE_OF_CONDUCT.md && git commit -m 'docs(coc): updated CODE_OF_CONDUCT.md'", 16 | "update-contrib": "weallcontribute -o . && git add CONTRIBUTING.md && git commit -m 'docs(contributing): updated CONTRIBUTING.md'" 17 | }, 18 | "repository": "https://github.com/zkat/fetch-cache", 19 | "keywords": [ 20 | "w3c", 21 | "web", 22 | "request", 23 | "fetch", 24 | "caching", 25 | "cache", 26 | "http", 27 | "cachestorage", 28 | "cache api" 29 | ], 30 | "author": { 31 | "name": "Kat Marchán", 32 | "email": "kzm@sykosomatic.org", 33 | "twitter": "maybekatz" 34 | }, 35 | "license": "CC0-1.0", 36 | "dependencies": { 37 | "lru-cache": "^4.0.2", 38 | "protoduck": "^4.0.0" 39 | }, 40 | "devDependencies": { 41 | "nyc": "^10.2.0", 42 | "standard": "^9.0.2", 43 | "standard-version": "^4.0.0", 44 | "tap": "^10.3.2", 45 | "weallbehave": "^1.0.0", 46 | "weallcontribute": "^1.0.8" 47 | }, 48 | "config": { 49 | "nyc": { 50 | "exclude": [ 51 | "node_modules/**", 52 | "test/**" 53 | ] 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /protocol.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const duck = require('protoduck') 4 | 5 | // CacheProtocol 6 | // 7 | // https://developer.mozilla.org/en-US/docs/Web/API/Cache 8 | // 9 | module.exports = duck.define(['req', 'res'], { 10 | // Returns a Promise that resolves to the response associated with the first 11 | // matching request in the Cache object. 12 | match: ['req'], 13 | 14 | // Returns a Promise that resolves to the response associated with the first 15 | // matching request in the Cache object. 16 | matchAll: ['req'], 17 | 18 | // Takes a URL, retrieves it and adds the resulting response object to the 19 | // given cache. This is fuctionally equivalent to calling fetch(), then using 20 | // Cache.put() to add the results to the cache. 21 | add: ['req'], 22 | 23 | // Takes an array of URLs, retrieves them, and adds the resulting response 24 | // objects to the given cache. 25 | addAll: [], 26 | 27 | // Takes both a request and its response and adds it to the given cache. 28 | put: ['req', 'res'], 29 | 30 | // Finds the Cache entry whose key is the request, and if found, deletes the 31 | // Cache entry and returns a Promise that resolves to true. If no Cache entry 32 | // is found, it returns false. 33 | delete: ['req'], 34 | 35 | // Returns a Promise that resolves to an array of Cache keys. 36 | keys: ['req'] 37 | }) 38 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const test = require('tap').test 4 | 5 | test('defines Cache API') 6 | --------------------------------------------------------------------------------