├── .github └── workflows │ └── ci-plugin.yml ├── .gitignore ├── API.md ├── LICENSE.md ├── README.md ├── lib └── index.js ├── package.json └── test ├── esm.js └── index.js /.github/workflows/ci-plugin.yml: -------------------------------------------------------------------------------- 1 | name: ci 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | workflow_dispatch: 9 | 10 | jobs: 11 | test: 12 | uses: hapijs/.github/.github/workflows/ci-plugin.yml@master 13 | with: 14 | min-node-version: 14 15 | min-hapi-version: 20 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | **/node_modules 2 | **/package-lock.json 3 | 4 | coverage.* 5 | 6 | **/.DS_Store 7 | **/._* 8 | 9 | **/*.pem 10 | 11 | **/.vs 12 | **/.vscode 13 | **/.idea 14 | -------------------------------------------------------------------------------- /API.md: -------------------------------------------------------------------------------- 1 | 2 | Scooter uses the [useragent] package to provide user-agent information. For 3 | more details of what information scooter provides, please see the [useragent](https://www.npmjs.org/package/useragent). 4 | 5 | # Usage 6 | 7 | ``` javascript 8 | const Hapi = require('@hapi/hapi'); 9 | const Scooter = require('@hapi/scooter'); 10 | 11 | const start = async () => { 12 | 13 | const server = new Hapi.Server(); 14 | 15 | server.route({ 16 | method: 'GET', 17 | path: '/user-agent', 18 | handler: (request, h) => { 19 | 20 | return request.plugins.scooter; 21 | } 22 | }); 23 | 24 | await server.register(Scooter); 25 | await server.start(); 26 | console.log(server.info.uri + '/user-agent'); 27 | }; 28 | 29 | start(); 30 | ``` 31 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013-2022, Project contributors 2 | Copyright (c) 2013-2020, Sideway Inc 3 | Copyright (c) 2013-2014, Walmart. 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 7 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 8 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 9 | * The names of any contributors may not be used to endorse or promote products derived from this software without specific prior written permission. 10 | 11 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS OFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # @hapi/scooter 4 | 5 | #### User-agent information plugin. 6 | 7 | **scooter** is part of the **hapi** ecosystem and was designed to work seamlessly with the [hapi web framework](https://hapi.dev) and its other components (but works great on its own or with other frameworks). If you are using a different web framework and find this module useful, check out [hapi](https://hapi.dev) – they work even better together. 8 | 9 | ### Visit the [hapi.dev](https://hapi.dev) Developer Portal for tutorials, documentation, and support 10 | 11 | ## Useful resources 12 | 13 | - [Documentation and API](https://hapi.dev/family/scooter/) 14 | - [Version status](https://hapi.dev/resources/status/#scooter) (builds, dependencies, node versions, licenses, eol) 15 | - [Changelog](https://hapi.dev/family/scooter/changelog/) 16 | - [Project policies](https://hapi.dev/policies/) 17 | - [Free and commercial support options](https://hapi.dev/support/) 18 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Useragent = require('useragent'); 4 | 5 | // Requires semver be installed 6 | require('useragent/features'); // Enhances Useragent 7 | 8 | 9 | const internals = {}; 10 | 11 | 12 | exports.plugin = { 13 | pkg: require('../package.json'), 14 | once: true, 15 | requirements: { 16 | hapi: '>=20.0.0' 17 | }, 18 | register: function (server, options) { 19 | 20 | server.ext('onRequest', internals.onRequest); 21 | } 22 | }; 23 | 24 | 25 | internals.onRequest = function (request, h) { 26 | 27 | request.plugins.scooter = Useragent.lookup(request.headers['user-agent']); 28 | return h.continue; 29 | }; 30 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@hapi/scooter", 3 | "description": "User-agent information plugin for hapi", 4 | "version": "7.0.0", 5 | "repository": "git://github.com/hapijs/scooter", 6 | "main": "lib/index.js", 7 | "files": [ 8 | "lib" 9 | ], 10 | "keywords": [ 11 | "hapi", 12 | "plugin", 13 | "user-agent" 14 | ], 15 | "eslintConfig": { 16 | "extends": [ 17 | "plugin:@hapi/module" 18 | ] 19 | }, 20 | "dependencies": { 21 | "semver": "^7.3.8", 22 | "useragent": "^2.3.0" 23 | }, 24 | "devDependencies": { 25 | "@hapi/code": "^9.0.0", 26 | "@hapi/eslint-plugin": "^6.0.0", 27 | "@hapi/hapi": "^21.0.0-beta.1", 28 | "@hapi/lab": "^25.0.1" 29 | }, 30 | "scripts": { 31 | "test": "lab -a @hapi/code -t 100 -L", 32 | "test-cov-html": "lab -a @hapi/code -r html -o coverage.html" 33 | }, 34 | "license": "BSD-3-Clause" 35 | } 36 | -------------------------------------------------------------------------------- /test/esm.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Code = require('@hapi/code'); 4 | const Lab = require('@hapi/lab'); 5 | 6 | 7 | const { before, describe, it } = exports.lab = Lab.script(); 8 | const expect = Code.expect; 9 | 10 | 11 | describe('import()', () => { 12 | 13 | let Scooter; 14 | 15 | before(async () => { 16 | 17 | Scooter = await import('../lib/index.js'); 18 | }); 19 | 20 | it('exposes all methods and classes as named imports', () => { 21 | 22 | expect(Object.keys(Scooter)).to.equal([ 23 | 'default', 24 | 'plugin' 25 | ]); 26 | }); 27 | }); 28 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Code = require('@hapi/code'); 4 | const Hapi = require('@hapi/hapi'); 5 | const Lab = require('@hapi/lab'); 6 | const Scooter = require('..'); 7 | 8 | 9 | const internals = {}; 10 | 11 | 12 | const { it, describe } = exports.lab = Lab.script(); 13 | const expect = Code.expect; 14 | 15 | 16 | describe('scooter', () => { 17 | 18 | it('may be registered multiple times', async () => { 19 | 20 | const server = Hapi.server(); 21 | await server.register(Scooter); 22 | await expect(server.register(Scooter)).not.to.reject(); 23 | }); 24 | 25 | it('parses and sets user-agent information for an incoming request', async () => { 26 | 27 | const server = Hapi.server(); 28 | await server.register(Scooter); 29 | 30 | server.route({ method: 'GET', path: '/', handler: (request, h) => request.plugins.scooter.os.family }); 31 | 32 | const res = await server.inject({ method: 'GET', url: '/', headers: { 'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3' } }); 33 | expect(res.result).to.equal('iOS'); 34 | }); 35 | }); 36 | --------------------------------------------------------------------------------