This commit is contained in:
darenhsu
2022-07-17 13:16:16 +08:00
parent 84759556ff
commit befd344ab0
28070 changed files with 4008428 additions and 1 deletions
+89
View File
@@ -0,0 +1,89 @@
# Changelog
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
### [1.2.2](https://github.com/nuxt/serve-placeholder/compare/v1.2.1...v1.2.2) (2020-02-02)
## [1.2.1](https://github.com/nuxt/serve-placeholder/compare/v1.2.0...v1.2.1) (2019-03-13)
### Bug Fixes
* don't check ext on query params (fixes [#17](https://github.com/nuxt/serve-placeholder/issues/17)) ([9f0a73e](https://github.com/nuxt/serve-placeholder/commit/9f0a73e))
# [1.2.0](https://github.com/nuxt/serve-placeholder/compare/v1.1.1...v1.2.0) (2019-03-07)
### Features
* set headers to prevent caching. fixes [#12](https://github.com/nuxt/serve-placeholder/issues/12) ([6272c87](https://github.com/nuxt/serve-placeholder/commit/6272c87))
<a name="1.1.1"></a>
## [1.1.1](https://github.com/nuxt/serve-placeholder/compare/v1.1.0...v1.1.1) (2019-02-07)
### Bug Fixes
* use defu instead of defaults-deep ([3dc0535](https://github.com/nuxt/serve-placeholder/commit/3dc0535)), closes [#7](https://github.com/nuxt/serve-placeholder/issues/7) [nuxt/nuxt.js#4971](https://github.com/nuxt/nuxt.js/issues/4971)
<a name="1.1.0"></a>
# [1.1.0](https://github.com/nuxt/serve-placeholder/compare/v1.0.0...v1.1.0) (2018-11-12)
### Features
* plain handler ([029a02e](https://github.com/nuxt/serve-placeholder/commit/029a02e))
* support ico ([834131b](https://github.com/nuxt/serve-placeholder/commit/834131b))
<a name="1.0.0"></a>
# [1.0.0](https://github.com/nuxt/serve-placeholder/compare/v0.1.1...v1.0.0) (2018-11-12)
### Code Refactoring
* rename options ([9e3de71](https://github.com/nuxt/serve-placeholder/commit/9e3de71))
### Features
* add handler for map files ([6709714](https://github.com/nuxt/serve-placeholder/commit/6709714))
* default handler support ([3954503](https://github.com/nuxt/serve-placeholder/commit/3954503))
### Performance Improvements
* **pkg:** use smaller defaults-deep ([1155339](https://github.com/nuxt/serve-placeholder/commit/1155339))
### BREAKING CHANGES
* handler~> handlers, placeholder~>placeholders,mime~> mimes
<a name="0.1.1"></a>
## [0.1.1](https://github.com/nuxt/serve-placeholder/compare/v0.1.0...v0.1.1) (2018-09-28)
### Bug Fixes
* **package:** only include src ([d5f560d](https://github.com/nuxt/serve-placeholder/commit/d5f560d))
<a name="0.1.0"></a>
# 0.1.0 (2018-09-28)
### Features
* initial commit ([65e354a](https://github.com/nuxt/serve-placeholder/commit/65e354a))
* initial commit ([f499bb0](https://github.com/nuxt/serve-placeholder/commit/f499bb0))
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2018 - Nuxt.js Team
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+149
View File
@@ -0,0 +1,149 @@
# ♡ serve-placeholder
> Connect/Express middleware to respond with better placeholders based on request instead of 404 page
[![Standard JS][standard-src]][standard-href]
[![david dm][david-src]][david-href]
[![codecov][codecov-src]][codecov-href]
[![circleci][circleci-src]][circleci-href]
[![npm version][npm-v-src]][npm-v-href]
[![npm downloads][npm-dt-src]][npm-dt-href]
[![package phobia][packagephobia-src]][packagephobia-href]
## Why?
**💵 Rendering 404 errors is costly**
- Each 404 error for assets means a new SSR request that adds extra loads to the server and increases crash chances.
**👌 Graceful Responses**
- Sometimes, we can send better responses alongside with 404 code instead of nothing. For example, for images, we send a fallback transparent 1x1 image.
**🔍 SEO Friendly**
- Don't allow indexing invalid URLs with ugly html pages.
- Remove extra SSR loads when assets like `robots.txt` or `favicon.ico` doesn't exist.
## Usage
Install package:
```bash
npm install serve-placeholder
```
OR
```bash
yarn add serve-placeholder
```
Import and use middleware:
```js
const placeholder = require('serve-placeholder')
// import placeholder from 'serve-placeholder'
// [regular middleware such as serve-static]
// Response with appreciate placeholders
app.use(placeholder())
//app.use(placeholder({ /* options */ }))
// [global error handler]
```
## Options
### `handlers`
A mapping from file extensions to the handler. Extensions should start with *dot* like `.js`.
You can disable any of the handlers by setting the value to `null`
If the value of a handler is set to `false`, middleware will be ignored for that extension.
### `statusCode`
- Default: `404`
Sets `statusCode` for all handled responses. Set to `false` to disable overriding statusCode.
### `skipUnknown`
- Default: `false`
Skip middleware when no handler is defined for the current request.
Please note that if this option is set to `true`, then `default` handler will be disabled!
### `placeholders`
- Type: `Object`
A mapping from handler to placeholder. Values can be `String` or `Buffer`. You can disable any of the placeholders by setting the value to `false`.
### `mimes`
- Type: `Object`
A mapping from handler to the mime type. Mime type will be set as `Content-Type` header. You can disable sending any of the mimes by setting the value to `false`.
### `noCache`
- Default: `true`
Set headers to prevent accidentally caching 404 resources.
When enabled, these headers will be sent:
```js
{
'cache-control': 'no-cache, no-store, must-revalidate',
'expires': '0',
'pragma': 'no-cache'
}
```
## Defaults
These are [default handlers](./src/defaults.js). You can override every of them using provided options.
Handler | Extensions | Mime type | Placeholder
-----------|------------------------|--------------------------|-------------------
`default` | any unknown extension | - | -
`css` | `.css` | `text/css` | `/* style not found */`
`html` | `.html`, `.htm` | `text/html` | `<!-- page not found -->`
`js` | `.js` | `application/javascript` | `/* script not found */`
`json` | `.json` | `application/json` | `{}`
`map` | `.map` | `application/json` | [empty sourcemap v3 json]
`plain` | `.txt`, `.text`, `.md` | `text/plain` | [empty]
`image` | `.png`, `.jpg`, `.jpeg`, `.gif`, `.svg`, `.webp`, `.bmp`, `.ico` | `image/gif` | [transparent 1x1 image]
## License
MIT. Made with 💖 by [Nuxt.js](https://nuxtjs.org) team!
<!-- Refs -->
[standard-src]: https://flat.badgen.net/badge/code%20style/standard/green
[standard-href]: https://standardjs.com
[npm-v-src]: https://flat.badgen.net/npm/v/serve-placeholder/latest
[npm-v-href]: https://npmjs.com/package/serve-placeholder
[npm-dt-src]: https://flat.badgen.net/npm/dt/serve-placeholder
[npm-dt-href]: https://npmjs.com/package/serve-placeholder
[packagephobia-src]: https://flat.badgen.net/packagephobia/install/serve-placeholder
[packagephobia-href]: https://packagephobia.now.sh/result?p=serve-placeholder
[david-src]: https://flat.badgen.net/david/dep/nuxt/serve-placeholder
[david-href]: https://david-dm.org/nuxt/serve-placeholder
[codecov-src]: https://flat.badgen.net/codecov/c/github/nuxt/serve-placeholder/master
[codecov-href]: https://codecov.io/gh/nuxt/serve-placeholder
[circleci-src]: https://flat.badgen.net/circleci/github/nuxt/serve-placeholder/master
[circleci-href]: https://circleci.com/gh/nuxt/serve-placeholder
+36
View File
@@ -0,0 +1,36 @@
# Change Log
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
<a name="1.0.0"></a>
# [1.0.0](https://github.com/jsless/defu/compare/v0.0.4...v1.0.0) (2020-02-02)
<a name="0.0.4"></a>
## [0.0.4](https://github.com/jsless/defu/compare/v0.0.3...v0.0.4) (2020-01-01)
### Bug Fixes
* improve es5 compatibility ([#2](https://github.com/jsless/defu/issues/2), [#9](https://github.com/jsless/defu/issues/9)) ([5a6de7c](https://github.com/jsless/defu/commit/5a6de7c))
<a name="0.0.3"></a>
## [0.0.3](https://github.com/jsless/defu/compare/v0.0.1...v0.0.3) (2019-05-25)
<a name="0.0.2"></a>
## [0.0.2](https://github.com/jesless/defu/compare/v0.0.1...v0.0.2) (2019-05-25)
<a name="0.0.1"></a>
## 0.0.1 (2019-02-07)
### Bug Fixes
* imrpove non-object handlers ([f89fa28](https://github.com/jesless/defu/commit/f89fa28))
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2019 - Pooya Parsa <pooya@pi0.ir>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+80
View File
@@ -0,0 +1,80 @@
# 🇩 defu
> Recursively assign default properties. Lightweight and Fast!
[![Standard JS][standard-src]][standard-href]
[![david dm][david-src]][david-href]
[![codecov][codecov-src]][codecov-href]
[![circleci][circleci-src]][circleci-href]
[![npm version][npm-v-src]][npm-v-href]
[![npm downloads][npm-dt-src]][npm-dt-href]
[![package phobia][packagephobia-src]][packagephobia-href]
[![bundle phobia][bundlephobia-src]][bundlephobia-href]
## Install
Install package:
```bash
npm install defu
```
OR
```bash
yarn add defu
```
## Usage
```js
const options = defu (object, defaults)
```
### Arguments
- **object (Object):** The destination object.
- **source (Object):** The source object.
```js
const defu = require('defu')
console.log(defu({ 'a': { 'b': 2 } }, { 'a': { 'b': 1, 'c': 3 } }))
// => { a: { b: 2, c: 3 } }
```
### Remarks
- `object` and `defaults` are not modified
- Assignment of `__proto__` and `constructor` keys will be skipped to prevent security issues with object pollution.
## License
MIT. Made with 💖
<!-- Refs -->
[standard-src]: https://flat.badgen.net/badge/code%20style/standard/green
[standard-href]: https://standardjs.com
[npm-v-src]: https://flat.badgen.net/npm/v/defu/latest
[npm-v-href]: https://npmjs.com/package/defu
[npm-dt-src]: https://flat.badgen.net/npm/dt/defu
[npm-dt-href]: https://npmjs.com/package/defu
[packagephobia-src]: https://flat.badgen.net/packagephobia/install/defu
[packagephobia-href]: https://packagephobia.now.sh/result?p=defu
[bundlephobia-src]: https://flat.badgen.net/bundlephobia/min/defu
[bundlephobia-href]: https://bundlephobia.com/result?p=defu
[david-src]: https://flat.badgen.net/david/dep/jsless/defu
[david-href]: https://david-dm.org/jsless/defu
[codecov-src]: https://flat.badgen.net/codecov/c/github/jsless/defu/master
[codecov-href]: https://codecov.io/gh/jsless/defu
[circleci-src]: https://flat.badgen.net/circleci/github/jsless/defu/master
[circleci-href]: https://circleci.com/gh/jsless/defu
+35
View File
@@ -0,0 +1,35 @@
function isObject (val) {
return val !== null && typeof val === 'object' && !Array.isArray(val)
}
function defu (_obj, _defaults) {
if (!isObject(_obj)) {
return defu({}, _defaults)
}
if (!isObject(_defaults)) {
return defu(_obj, {})
}
var obj = Object.assign({}, _defaults)
Object.keys(_obj).forEach(function (key) {
if (key === '__proto__' || key === 'constructor') {
return
}
var val = _obj[key]
if (val === null) {
return
}
if (isObject(val) && isObject(obj[key])) {
obj[key] = defu(val, obj[key])
} else {
obj[key] = val
}
})
return obj
}
module.exports = defu
+68
View File
@@ -0,0 +1,68 @@
{
"_args": [
[
"defu@1.0.0",
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
]
],
"_from": "defu@1.0.0",
"_id": "defu@1.0.0",
"_inBundle": false,
"_integrity": "sha512-1Y1KRFxiiq+LYsZ3iP7xYSR8bHfmHFOUpDunZCN1ld1fGfDJWJIvkUBtjl3apnBwPuJtL/H7cwwlLYX8xPkraQ==",
"_location": "/serve-placeholder/defu",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "defu@1.0.0",
"name": "defu",
"escapedName": "defu",
"rawSpec": "1.0.0",
"saveSpec": null,
"fetchSpec": "1.0.0"
},
"_requiredBy": [
"/serve-placeholder"
],
"_resolved": "https://registry.npmjs.org/defu/-/defu-1.0.0.tgz",
"_spec": "1.0.0",
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
"bugs": {
"url": "https://github.com/jsless/defu/issues"
},
"contributes": [
"Pooya Parsa <pooya@pi0.ir>"
],
"description": "Recursively assign default properties. Lightweight and Fast!",
"devDependencies": {
"codecov": "^3.1.0",
"eslint": "^5.9.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jest": "^22.1.0",
"eslint-plugin-node": "^8.0.0",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"jest": "^24.0.0",
"standard-version": "^4.4.0"
},
"files": [
"lib"
],
"homepage": "https://github.com/jsless/defu#readme",
"license": "MIT",
"main": "lib/index.js",
"name": "defu",
"repository": {
"type": "git",
"url": "git+https://github.com/jsless/defu.git"
},
"scripts": {
"jest": "jest",
"lint": "eslint lib",
"prepare": "npm run test",
"release": "standard-version && git push --follow-tags && npm publish",
"test": "npm run lint && npm run jest"
},
"version": "1.0.0"
}
+73
View File
@@ -0,0 +1,73 @@
{
"_args": [
[
"serve-placeholder@1.2.2",
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
]
],
"_from": "serve-placeholder@1.2.2",
"_id": "serve-placeholder@1.2.2",
"_inBundle": false,
"_integrity": "sha512-4zczakjRPVbX5XBcJqUoyiwiMEXWPBj0rEexQkiQitKEkj36szAmw5DgcgOpWiXtRTU7aqJ47hjnYJg+2FSj5g==",
"_location": "/serve-placeholder",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "serve-placeholder@1.2.2",
"name": "serve-placeholder",
"escapedName": "serve-placeholder",
"rawSpec": "1.2.2",
"saveSpec": null,
"fetchSpec": "1.2.2"
},
"_requiredBy": [
"/@nuxt/server"
],
"_resolved": "https://registry.npmjs.org/serve-placeholder/-/serve-placeholder-1.2.2.tgz",
"_spec": "1.2.2",
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
"bugs": {
"url": "https://github.com/nuxt/serve-placeholder/issues"
},
"contributes": [
"Pooya Parsa <pooya@pi0.ir>"
],
"dependencies": {
"defu": "^1.0.0"
},
"description": "Connect/Express middleware to respond with better placeholders based on request instead of 404 page",
"devDependencies": {
"axios": "latest",
"codecov": "latest",
"connect": "latest",
"eslint": "latest",
"eslint-config-standard": "latest",
"eslint-plugin-import": "latest",
"eslint-plugin-jest": "latest",
"eslint-plugin-node": "latest",
"eslint-plugin-promise": "latest",
"eslint-plugin-standard": "latest",
"jest": "latest",
"standard-version": "latest"
},
"files": [
"src"
],
"homepage": "https://github.com/nuxt/serve-placeholder#readme",
"license": "MIT",
"main": "src/index.js",
"name": "serve-placeholder",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/nuxt/serve-placeholder.git"
},
"scripts": {
"jest": "jest",
"lint": "eslint src",
"prepare": "npm run test",
"release": "standard-version && git push --follow-tags && npm publish",
"test": "npm run lint && npm run jest"
},
"version": "1.2.2"
}
+58
View File
@@ -0,0 +1,58 @@
const EMPTY_GIF = Buffer.from('R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==', 'base64')
module.exports = {
statusCode: 404,
skipUnknown: false,
noCache: true,
handlers: {
// css
'.css': 'css',
// html
'.html': 'html',
'.htm': 'html',
// image
'.png': 'image',
'.jpg': 'image',
'.jpeg': 'image',
'.gif': 'image',
'.svg': 'image',
'.webp': 'image',
'.bmp': 'image',
'.ico': 'image',
// js
'.js': 'js',
// json
'.json': 'json',
// map
'.map': 'map',
// plain
'.txt': 'plain',
'.text': 'plain',
'.md': 'plain'
},
placeholders: {
css: '/* style not found */',
default: undefined,
html: '<!-- page not found -->',
image: EMPTY_GIF,
js: '/* script not found */',
json: '{}',
map: '{"version": "3", "sources": [], "mappings": "" }',
plain: ''
},
mimes: {
css: 'text/css',
default: undefined,
html: 'text/html',
js: 'application/javascript',
json: 'application/json',
image: 'image/gif',
map: 'application/json',
plain: 'text/plain'
}
}
+60
View File
@@ -0,0 +1,60 @@
const path = require('path')
const defu = require('defu')
const defaults = require('./defaults')
module.exports = function createServePlaceholder (_options) {
// Assign default options
const options = defu(_options, defaults)
return function servePlaceholderMiddleware (req, res, next) {
// Get url from req object
const url = req.url.split('?')[0]
// Get ext of requst url
const ext = path.extname(url)
// Try to find a handler based on ext
let handler = options.handlers[ext]
// Skip middleware is handler is explictly set to false
if (handler === false) {
return next()
}
// In case of no handler guessed
if (typeof handler === 'undefined') {
if (options.skipUnknown) {
// Skip this middleware
return next()
} else {
// Use 'default' handler
handler = 'default'
}
}
// Set statusCode
if (options.statusCode) {
res.statusCode = options.statusCode
}
// Try to set appreciate mime type based on handler
const mime = options.mimes[handler]
if (mime) {
res.setHeader('Content-Type', mime)
}
// Prevent caching
if (options.noCache) {
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1
res.setHeader('Pragma', 'no-cache') // HTTP 1.0
res.setHeader('Expires', '0') // Proxies
}
// Try to find placeholder based on handler
const placeholder = options.placeholders[handler]
// Send placeholder and end response
res.end(placeholder)
}
}