├── .github └── FUNDING.yml ├── .gitignore ├── .npmignore ├── .testem.json ├── .travis.yml ├── CHANGELOG.md ├── LICENCE ├── README.md ├── index.js ├── package.json ├── security.md └── test ├── index.js └── static ├── index.html └── test-adapter.js /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: npm/console-browserify 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .monitor 3 | .*.swp 4 | .nodemonignore 5 | releases 6 | *.log 7 | *.err 8 | fleet.json 9 | public/browserify 10 | bin/*.json 11 | .bin 12 | build 13 | compile 14 | .lock-wscript 15 | node_modules 16 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .monitor 3 | .*.swp 4 | .nodemonignore 5 | releases 6 | *.log 7 | *.err 8 | fleet.json 9 | public/browserify 10 | bin/*.json 11 | .bin 12 | build 13 | compile 14 | .lock-wscript 15 | -------------------------------------------------------------------------------- /.testem.json: -------------------------------------------------------------------------------- 1 | { 2 | "launchers": { 3 | "node": { 4 | "command": "npm test" 5 | } 6 | }, 7 | "src_files": [ 8 | "./**/*.js" 9 | ], 10 | "before_tests": "npm run build", 11 | "on_exit": "rm test/static/bundle.js", 12 | "test_page": "test/static/index.html", 13 | "launch_in_dev": ["node", "phantomjs"] 14 | } 15 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "stable" 4 | - "12" 5 | - "11" 6 | - "10" 7 | - "8" 8 | - "6" 9 | - "4" 10 | - "0.12" 11 | - "0.10" 12 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # console-browserify change log 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | This project adheres to [Semantic Versioning](http://semver.org/). 6 | 7 | ## 1.2.0 8 | * Move to the browserify org. 9 | * Remove `date-now` dependency. ([@jakepusateri](https://github.com/jakepusateri) in [#10](https://github.com/browserify/console-browserify/pull/10)) 10 | * Fix memory leak in `time`/`timeEnd`. ([@maxnordlund](https://github.com/maxnordlund) in [#11](https://github.com/browserify/console-browserify/pull/11)) 11 | -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Raynos. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # console-browserify [![Build Status](https://travis-ci.org/browserify/console-browserify.png?branch=master)](https://travis-ci.org/browserify/console-browserify) 2 | 3 | Emulate console for all the browsers 4 | 5 | ## Install 6 | 7 | You usually do not have to install `console-browserify` yourself! If your code runs in Node.js, `console` is built in. If your code runs in the browser, bundlers like [browserify](https://github.com/browserify/browserify) or [webpack](https://github.com/webpack/webpack) also include the `console-browserify` module when you do `require('console')`. 8 | 9 | But if none of those apply, with npm do: 10 | 11 | ``` 12 | npm install console-browserify 13 | ``` 14 | 15 | ## Usage 16 | 17 | ```js 18 | var console = require("console") 19 | // Or when manually using console-browserify directly: 20 | // var console = require("console-browserify") 21 | 22 | console.log("hello world!") 23 | ``` 24 | 25 | ## API 26 | 27 | See the [Node.js Console docs](https://nodejs.org/api/console.html). `console-browserify` does not support creating new `Console` instances and does not support the Inspector-only methods. 28 | 29 | ## Contributing 30 | 31 | PRs are very welcome! The main way to contribute to `console-browserify` is by porting features, bugfixes and tests from Node.js. Ideally, code contributions to this module are copy-pasted from Node.js and transpiled to ES5, rather than reimplemented from scratch. Matching the Node.js code as closely as possible makes maintenance simpler when new changes land in Node.js. 32 | This module intends to provide exactly the same API as Node.js, so features that are not available in the core `console` module will not be accepted. Feature requests should instead be directed at [nodejs/node](https://github.com/nodejs/node) and will be added to this module once they are implemented in Node.js. 33 | 34 | If there is a difference in behaviour between Node.js's `console` module and this module, please open an issue! 35 | 36 | ## Contributors 37 | 38 | - Raynos 39 | 40 | ## License 41 | 42 | [MIT](./LICENSE) 43 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*global window, global*/ 2 | var util = require("util") 3 | function now() { return new Date().getTime() } 4 | 5 | var slice = Array.prototype.slice 6 | var concat = Array.prototype.concat 7 | var console 8 | var times = {} 9 | 10 | if (typeof global !== "undefined" && global.console) { 11 | console = global.console 12 | } else if (typeof window !== "undefined" && window.console) { 13 | console = window.console 14 | } else { 15 | console = {} 16 | } 17 | 18 | var functions = [ 19 | [log, "log"], 20 | [info, "info"], 21 | [warn, "warn"], 22 | [error, "error"], 23 | [time, "time"], 24 | [timeEnd, "timeEnd"], 25 | [trace, "trace"], 26 | [dir, "dir"], 27 | [assert, "assert"] 28 | ] 29 | 30 | for (var i = 0; i < functions.length; i++) { 31 | var tuple = functions[i] 32 | var f = tuple[0] 33 | var name = tuple[1] 34 | 35 | if (!console[name]) { 36 | console[name] = f 37 | } 38 | } 39 | 40 | module.exports = console 41 | 42 | function log() {} 43 | 44 | function info() { 45 | console.log.apply(console, arguments) 46 | } 47 | 48 | function warn() { 49 | console.log.apply(console, arguments) 50 | } 51 | 52 | function error() { 53 | console.warn.apply(console, arguments) 54 | } 55 | 56 | function time(label) { 57 | times[label] = now() 58 | } 59 | 60 | function timeEnd(label) { 61 | var time = times[label] 62 | if (!time) { 63 | throw new Error("No such label: " + label) 64 | } 65 | 66 | delete times[label] 67 | var duration = now() - time 68 | console.log(label + ": " + duration + "ms") 69 | } 70 | 71 | function trace() { 72 | var err = new Error() 73 | err.name = "Trace" 74 | err.message = util.format.apply(null, arguments) 75 | console.error(err.stack) 76 | } 77 | 78 | function dir(object) { 79 | console.log(util.inspect(object) + "\n") 80 | } 81 | 82 | function assert(expression) { 83 | if (!expression) { 84 | console.error.apply(null, concat.call(["Assertion failed:"], slice.call(arguments, 1))) 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "console-browserify", 3 | "version": "1.2.0", 4 | "description": "Emulate console for all the browsers", 5 | "keywords": [], 6 | "author": "Raynos ", 7 | "repository": "git://github.com/browserify/console-browserify.git", 8 | "main": "index", 9 | "homepage": "https://github.com/browserify/console-browserify", 10 | "contributors": [ 11 | { 12 | "name": "Raynos" 13 | } 14 | ], 15 | "bugs": { 16 | "url": "https://github.com/browserify/console-browserify/issues", 17 | "email": "raynos2@gmail.com" 18 | }, 19 | "devDependencies": { 20 | "tape": "^2.12.3", 21 | "jsonify": "0.0.0", 22 | "tap-spec": "^0.1.8", 23 | "run-browser": "^1.3.0", 24 | "tap-dot": "^0.2.1" 25 | }, 26 | "licenses": [ 27 | { 28 | "type": "MIT", 29 | "url": "http://github.com/browserify/console-browserify/raw/master/LICENSE" 30 | } 31 | ], 32 | "scripts": { 33 | "test": "node ./test/index.js | tap-spec", 34 | "dot": "node ./test/index.js | tap-dot", 35 | "start": "node ./index.js", 36 | "cover": "istanbul cover --report none --print detail ./test/index.js", 37 | "view-cover": "istanbul report html && google-chrome ./coverage/index.html", 38 | "browser": "run-browser test/index.js", 39 | "phantom": "run-browser test/index.js -b | tap-spec", 40 | "build": "browserify test/index.js -o test/static/bundle.js", 41 | "testem": "testem" 42 | }, 43 | "testling": { 44 | "files": "test/index.js", 45 | "browsers": [ 46 | "ie/8..latest", 47 | "firefox/16..latest", 48 | "firefox/nightly", 49 | "chrome/22..latest", 50 | "chrome/canary", 51 | "opera/12..latest", 52 | "opera/next", 53 | "safari/5.1..latest", 54 | "ipad/6.0..latest", 55 | "iphone/6.0..latest", 56 | "android-browser/4.2..latest" 57 | ] 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Supported Versions 4 | Only the latest major version is supported at any given time. 5 | 6 | ## Reporting a Vulnerability 7 | 8 | To report a security vulnerability, please use the 9 | [Tidelift security contact](https://tidelift.com/security). 10 | Tidelift will coordinate the fix and disclosure. 11 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var console = require("../index") 2 | var test = require("tape") 3 | 4 | if (typeof window !== "undefined" && !window.JSON) { 5 | window.JSON = require("jsonify") 6 | } 7 | 8 | test("console has expected methods", function (assert) { 9 | assert.ok(console.log) 10 | assert.ok(console.info) 11 | assert.ok(console.warn) 12 | assert.ok(console.dir) 13 | assert.ok(console.time, "time") 14 | assert.ok(console.timeEnd, "timeEnd") 15 | assert.ok(console.trace, "trace") 16 | assert.ok(console.assert) 17 | 18 | assert.end() 19 | }) 20 | 21 | test("invoke console.log", function (assert) { 22 | console.log("test-log") 23 | 24 | assert.end() 25 | }) 26 | 27 | test("invoke console.info", function (assert) { 28 | console.info("test-info") 29 | 30 | assert.end() 31 | }) 32 | 33 | test("invoke console.warn", function (assert) { 34 | console.warn("test-warn") 35 | 36 | assert.end() 37 | }) 38 | 39 | test("invoke console.time", function (assert) { 40 | console.time("label") 41 | 42 | assert.end() 43 | }) 44 | 45 | test("invoke console.trace", function (assert) { 46 | console.trace("test-trace") 47 | 48 | assert.end() 49 | }) 50 | 51 | test("invoke console.assert", function (assert) { 52 | console.assert(true) 53 | 54 | assert.end() 55 | }) 56 | 57 | test("invoke console.dir", function (assert) { 58 | console.dir("test-dir") 59 | 60 | assert.end() 61 | }) 62 | 63 | test("invoke console.timeEnd", function (assert) { 64 | console.timeEnd("label") 65 | 66 | assert.end() 67 | }) 68 | -------------------------------------------------------------------------------- /test/static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TAPE Example 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /test/static/test-adapter.js: -------------------------------------------------------------------------------- 1 | (function () { 2 | var Testem = window.Testem 3 | var regex = /^((?:not )?ok) (\d+) (.+)$/ 4 | 5 | Testem.useCustomAdapter(tapAdapter) 6 | 7 | function tapAdapter(socket){ 8 | var results = { 9 | failed: 0 10 | , passed: 0 11 | , total: 0 12 | , tests: [] 13 | } 14 | 15 | socket.emit('tests-start') 16 | 17 | Testem.handleConsoleMessage = function(msg){ 18 | var m = msg.match(regex) 19 | if (m) { 20 | var passed = m[1] === 'ok' 21 | var test = { 22 | passed: passed ? 1 : 0, 23 | failed: passed ? 0 : 1, 24 | total: 1, 25 | id: m[2], 26 | name: m[3], 27 | items: [] 28 | } 29 | 30 | if (passed) { 31 | results.passed++ 32 | } else { 33 | console.error("failure", m) 34 | 35 | results.failed++ 36 | } 37 | 38 | results.total++ 39 | 40 | // console.log("emitted test", test) 41 | socket.emit('test-result', test) 42 | results.tests.push(test) 43 | } else if (msg === '# ok' || msg.match(/^# tests \d+/)){ 44 | // console.log("emitted all test") 45 | socket.emit('all-test-results', results) 46 | } 47 | 48 | // return false if you want to prevent the console message from 49 | // going to the console 50 | // return false 51 | } 52 | } 53 | }()) 54 | --------------------------------------------------------------------------------