├── .github └── CONTRIBUTING.md ├── .gitignore ├── .npmignore ├── LICENSE.md ├── README.md ├── SECURITY.md ├── package-lock.json ├── package.json ├── rollup.config.js └── src ├── index.js ├── payloads ├── ColorPayload.js ├── CustomPayload.js ├── HidePayload.js ├── JsonPayload.js ├── LogPayload.js ├── NewScreenPayload.js ├── NotifyPayload.js ├── RemovePayload.js └── SizePayload.js └── utils └── type.js /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 44 | 45 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 46 | 47 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 48 | 49 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 50 | 51 | **Happy coding**! 52 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | src 2 | .github 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Miguel Piedrafita 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Debug your Javascript with Ray to fix problems faster 2 | 3 | This package can be installed in any JS application to send messages to [the Ray app](https://myray.app). 4 | 5 | ## Installation 6 | 7 | ```bash 8 | npm install js-ray 9 | ``` 10 | or 11 | ```bash 12 | yarn install js-ray 13 | ``` 14 | 15 | ## Usage 16 | 17 | Using ESM import 18 | 19 | ```js 20 | import { ray } from 'js-ray'; 21 | ``` 22 | 23 | Using CommonJS require 24 | 25 | ```js 26 | const { ray } = require('js-ray'); 27 | ``` 28 | 29 | Quick examples 30 | 31 | ```js 32 | ray('a string') 33 | 34 | ray(['an array']) 35 | 36 | ray({ text: 'an object' }) 37 | 38 | ray('as' 'many' , 'arguments', 'as', 'you', 'like') 39 | 40 | ray('this is blue').color('blue') 41 | 42 | ray().newScreen('My debug screen') 43 | ``` 44 | 45 | ## Documentation 46 | 47 | You can find the full documentation on [our documentation site](https://spatie.be/docs/ray/v1/usage/javascript). 48 | 49 | ## Contributing 50 | 51 | Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details. 52 | 53 | ## Security Vulnerabilities 54 | 55 | Please review [our security policy](../../security/policy) on how to report security vulnerabilities. 56 | 57 | ## Credits 58 | 59 | - [Miguel Piedrafita](https://github.com/m1guelpf) 60 | - [All Contributors](../../contributors) 61 | 62 | ## License 63 | 64 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 65 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | If you discover any security related issues, please email soy@miguelpiedrafita.com instead of using the issue tracker. 4 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "js-ray", 3 | "version": "0.0.7", 4 | "description": "Debug with Ray to fix problems faster", 5 | "main": "./dist/index.mjs", 6 | "exports": { 7 | "import": "./dist/index.mjs", 8 | "require": "./dist/index.cjs" 9 | }, 10 | "repository": "https://github.com/m1guelpf/ray-js", 11 | "author": "Miguel Piedrafita", 12 | "license": "MIT", 13 | "type": "module", 14 | "scripts": { 15 | "build": "rollup --config", 16 | "preversion": "npm run build" 17 | }, 18 | "private": false, 19 | "dependencies": { 20 | "axios": "^0.21.1", 21 | "stacktrace-js": "^2.0.2", 22 | "uuid": "^8.3.2" 23 | }, 24 | "devDependencies": { 25 | "@babel/cli": "7.12.1", 26 | "@babel/core": "7.12.3", 27 | "@babel/plugin-proposal-class-properties": "7.12.1", 28 | "@babel/preset-env": "7.12.1", 29 | "@rollup/plugin-babel": "5.2.1", 30 | "@rollup/plugin-node-resolve": "^11.1.0", 31 | "rollup": "2.33.3", 32 | "rollup-plugin-terser": "7.0.2" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import babel from '@rollup/plugin-babel'; 2 | import { terser } from 'rollup-plugin-terser'; 3 | 4 | export default { 5 | input: 'src/index.js', 6 | output: [ 7 | { file: 'dist/index.cjs', format: 'cjs', sourcemap: true, exports: 'auto' }, 8 | { 9 | file: 'dist/index.min.cjs', 10 | format: 'cjs', 11 | plugins: [terser()], 12 | sourcemap: true, 13 | exports: 'auto', 14 | }, 15 | { file: 'dist/index.mjs', format: 'esm', sourcemap: true }, 16 | { file: 'dist/index.min.mjs', format: 'esm', plugins: [terser()], sourcemap: true }, 17 | ], 18 | plugins: [ 19 | babel({ babelHelpers: 'bundled', plugins: ['@babel/plugin-proposal-class-properties'] }), 20 | ], 21 | external: ['axios', 'uuid', 'stacktrace-js'] 22 | }; 23 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import axios from 'axios' 2 | import { v4 as uuid } from 'uuid'; 3 | import ColorPayload from './payloads/ColorPayload' 4 | import HidePayload from './payloads/HidePayload' 5 | import LogPayload from './payloads/LogPayload' 6 | import NewScreenPayload from './payloads/NewScreenPayload' 7 | import RemovePayload from './payloads/RemovePayload' 8 | import SizePayload from './payloads/SizePayload' 9 | import NotifyPayload from './payloads/NotifyPayload' 10 | import CustomPayload from './payloads/CustomPayload' 11 | import JsonPayload from './payloads/JsonPayload' 12 | import StackTrace from 'stacktrace-js' 13 | import type from './utils/type' 14 | 15 | function makePayload(value) { 16 | if (type(value) === 'object') { 17 | return JsonPayload(value) 18 | } 19 | 20 | return LogPayload(value) 21 | } 22 | 23 | class Ray { 24 | static client 25 | 26 | constructor(host = '127.0.0.1', port = 23517) { 27 | this.uuid = uuid() 28 | this.client = axios.create({ 29 | baseURL: `http://${host}:${port}/`, 30 | }) 31 | } 32 | 33 | newScreen(name = '') { 34 | this.sendRequest(NewScreenPayload(name)) 35 | 36 | return this 37 | } 38 | 39 | clearScreen() { 40 | return this.newScreen() 41 | } 42 | 43 | color(color) { 44 | this.sendRequest(ColorPayload(color)) 45 | 46 | return this 47 | } 48 | 49 | size(size) { 50 | this.sendRequest(SizePayload(size)) 51 | 52 | return this 53 | } 54 | 55 | remove() { 56 | this.sendRequest(RemovePayload()) 57 | 58 | return this 59 | } 60 | 61 | hide() { 62 | this.sendRequest(HidePayload()) 63 | 64 | return this 65 | } 66 | 67 | notify(text) { 68 | this.sendRequest(NotifyPayload(text)) 69 | 70 | return this 71 | } 72 | 73 | die() { 74 | process.exit() 75 | } 76 | 77 | showWhen(boolOrFunc) { 78 | if (typeof boolOrFunc == 'function') boolOrFunc = boolOrFunc() 79 | 80 | if (!boolOrFunc) this.remove() 81 | 82 | return this 83 | } 84 | 85 | showIf(boolOrFunc) { 86 | return this.showWhen(boolOrFunc) 87 | } 88 | 89 | removeWhen(boolOrFunc) { 90 | if (typeof boolOrFunc == 'function') boolOrFunc = boolOrFunc() 91 | 92 | if (boolOrFunc) this.remove() 93 | 94 | return this 95 | } 96 | 97 | removeIf(boolOrFunc) { 98 | return this.removeWhen(boolOrFunc) 99 | } 100 | 101 | ban() { 102 | return this.send('🕶') 103 | } 104 | 105 | charles() { 106 | return this.send('🎶 🎹 🎷 🕺') 107 | } 108 | 109 | send(...values) { 110 | if (values.length == 0) return this 111 | 112 | this.sendRequest(...values.map(makePayload)) 113 | 114 | return this 115 | } 116 | 117 | json(value) { 118 | this.sendRequest(JsonPayload(value)) 119 | 120 | return this 121 | } 122 | 123 | pass(value) { 124 | this.send(value) 125 | 126 | return value 127 | } 128 | 129 | sendCustom(content, label = '') { 130 | this.sendRequest(CustomPayload(content, label)) 131 | 132 | return this 133 | } 134 | 135 | getOrigin() { 136 | const st = StackTrace.getSync() 137 | return st.find(({ fileName }) => !fileName.includes('js-ray/dist/index') ) 138 | } 139 | 140 | sendRequest(...payloads) { 141 | const origin = this.getOrigin() 142 | const requestPayload = { 143 | uuid: this.uuid, 144 | payloads: payloads.map(payload => { 145 | payload.origin = { 146 | file: origin.fileName || 'unknown.js', 147 | line_number: origin.lineNumber || 1, 148 | } 149 | 150 | return payload 151 | }), 152 | meta: [], 153 | } 154 | 155 | this.client.post('/', requestPayload) 156 | } 157 | } 158 | 159 | export const ray = (...args) => new Ray().send(...args) 160 | -------------------------------------------------------------------------------- /src/payloads/ColorPayload.js: -------------------------------------------------------------------------------- 1 | export default color => ({ 2 | type: 'color', 3 | content: { color }, 4 | }) 5 | -------------------------------------------------------------------------------- /src/payloads/CustomPayload.js: -------------------------------------------------------------------------------- 1 | export default (content, label) => ({ 2 | type: 'custom', 3 | content: { content, label }, 4 | }) 5 | -------------------------------------------------------------------------------- /src/payloads/HidePayload.js: -------------------------------------------------------------------------------- 1 | export default () => ({ 2 | type: 'hide', 3 | content: [], 4 | }) 5 | -------------------------------------------------------------------------------- /src/payloads/JsonPayload.js: -------------------------------------------------------------------------------- 1 | export default (value) => { 2 | return { 3 | type: 'json_string', 4 | content: { value: JSON.stringify(value) }, 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /src/payloads/LogPayload.js: -------------------------------------------------------------------------------- 1 | export default (...values) => { 2 | return { 3 | type: 'log', 4 | content: { values }, 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /src/payloads/NewScreenPayload.js: -------------------------------------------------------------------------------- 1 | export default name => ({ 2 | type: 'new_screen', 3 | content: { name }, 4 | }) 5 | -------------------------------------------------------------------------------- /src/payloads/NotifyPayload.js: -------------------------------------------------------------------------------- 1 | export default value => ({ 2 | type: 'notify', 3 | content: { value }, 4 | }) 5 | -------------------------------------------------------------------------------- /src/payloads/RemovePayload.js: -------------------------------------------------------------------------------- 1 | export default () => ({ 2 | type: 'remove', 3 | content: [], 4 | }) 5 | -------------------------------------------------------------------------------- /src/payloads/SizePayload.js: -------------------------------------------------------------------------------- 1 | export default size => ({ 2 | type: 'size', 3 | content: { size }, 4 | }) 5 | -------------------------------------------------------------------------------- /src/utils/type.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Check type of operand with more specificity than `typeof`. 3 | * Slightly modified version of MDN helper found in `typeof` definition page. 4 | * @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof#real-world_usage 5 | * 6 | * @param {*} obj 7 | * @returns {string} 8 | */ 9 | export default function type(obj) { 10 | if (obj == null) { 11 | return (obj + '').toLowerCase() // implicit toString() conversion 12 | } 13 | 14 | const deepType = Object.prototype.toString.call(obj).slice(8, -1).toLowerCase() 15 | 16 | if (deepType === 'generatorfunction') { 17 | return 'function' 18 | } 19 | 20 | // Prevent over-specificity (for example, [object HTMLDivElement], etc). 21 | // Account for functionish Regexp (Android <=2.3), functionish element (Chrome <=57, Firefox <=52), etc. 22 | // String.prototype.match is universally supported. 23 | 24 | if (deepType.match(/^(array|bigint|date|error|function|generator|regexp|symbol)$/)) { 25 | return deepType 26 | } 27 | 28 | return (typeof obj === 'object' || typeof obj === 'function') 29 | ? 'object' 30 | : typeof obj 31 | } 32 | --------------------------------------------------------------------------------