Skip to content

Commit b5b2b16

Browse files
committed
Merge remote-tracking branch 'origin/master' into ab/upgrade-source-map-dep
2 parents fc7475d + d354453 commit b5b2b16

File tree

5 files changed

+55
-37
lines changed

5 files changed

+55
-37
lines changed

.npmignore

-9
This file was deleted.

README.md

+19-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Source Map Support
2-
[![Build Status](https://travis-ci.org/evanw/node-source-map-support.svg?branch=master)](https://travis-ci.org/evanw/node-source-map-support)
2+
3+
[![NPM version](https://img.shields.io/npm/v/@cspotcode/source-map-support.svg?style=flat)](https://npmjs.org/package/@cspotcode/source-map-support)
4+
[![NPM downloads](https://img.shields.io/npm/dm/@cspotcode/source-map-support.svg?style=flat)](https://npmjs.org/package/@cspotcode/source-map-support)
5+
[![Build status](https://img.shields.io/github/workflow/status/cspotcode/node-source-map-support/Continuous%20Integration)](https://github.com/cspotcode/node-source-map-support/actions?query=workflow%3A%22Continuous+Integration%22)
36

47
This module provides source map support for stack traces in node via the [V8 stack trace API](https://github.com/v8/v8/wiki/Stack-Trace-API). It uses the [source-map](https://github.com/mozilla/source-map) module to replace the paths and line numbers of source-mapped files with their original paths and line numbers. The output mimics node's stack trace format with the goal of making every compile-to-JS language more of a first-class citizen. Source maps are completely general (not specific to any one language) so you can use source maps with multiple compile-to-JS languages in the same node process.
58

@@ -8,7 +11,7 @@ This module provides source map support for stack traces in node via the [V8 sta
811
#### Node support
912

1013
```
11-
$ npm install source-map-support
14+
$ npm install @cspotcode/source-map-support
1215
```
1316

1417
Source maps can be generated using libraries such as [source-map-index-generator](https://github.com/twolfson/source-map-index-generator). Once you have a valid source map, place a source mapping comment somewhere in the file (usually done automatically or with an option by your transpiler):
@@ -26,33 +29,33 @@ From here you have two options.
2629
##### CLI Usage
2730

2831
```bash
29-
node -r source-map-support/register compiled.js
32+
node -r @cspotcode/source-map-support/register compiled.js
3033
```
3134

3235
##### Programmatic Usage
3336

3437
Put the following line at the top of the compiled file.
3538

3639
```js
37-
require('source-map-support').install();
40+
require('@cspotcode/source-map-support').install();
3841
```
3942

4043
It is also possible to install the source map support directly by
4144
requiring the `register` module which can be handy with ES6:
4245

4346
```js
44-
import 'source-map-support/register'
47+
import '@cspotcode/source-map-support/register'
4548

4649
// Instead of:
47-
import sourceMapSupport from 'source-map-support'
50+
import sourceMapSupport from '@cspotcode/source-map-support'
4851
sourceMapSupport.install()
4952
```
5053
Note: if you're using babel-register, it includes source-map-support already.
5154

5255
It is also very useful with Mocha:
5356

5457
```
55-
$ mocha --require source-map-support/register tests/
58+
$ mocha --require @cspotcode/source-map-support/register tests/
5659
```
5760

5861
#### Browser support
@@ -81,15 +84,15 @@ This library also works if you use AMD (Asynchronous Module Definition), which i
8184
This module installs two things: a change to the `stack` property on `Error` objects and a handler for uncaught exceptions that mimics node's default exception handler (the handler can be seen in the demos below). You may want to disable the handler if you have your own uncaught exception handler. This can be done by passing an argument to the installer:
8285

8386
```js
84-
require('source-map-support').install({
87+
require('@cspotcode/source-map-support').install({
8588
handleUncaughtExceptions: false
8689
});
8790
```
8891

8992
This module loads source maps from the filesystem by default. You can provide alternate loading behavior through a callback as shown below. For example, [Meteor](https://github.com/meteor) keeps all source maps cached in memory to avoid disk access.
9093

9194
```js
92-
require('source-map-support').install({
95+
require('@cspotcode/source-map-support').install({
9396
retrieveSourceMap: function(source) {
9497
if (source === 'compiled.js') {
9598
return {
@@ -106,7 +109,7 @@ The module will by default assume a browser environment if XMLHttpRequest and wi
106109
In some rare cases, e.g. when running a browser emulation and where both variables are also set, you can explictly specify the environment to be either 'browser' or 'node'.
107110

108111
```js
109-
require('source-map-support').install({
112+
require('@cspotcode/source-map-support').install({
110113
environment: 'node'
111114
});
112115
```
@@ -115,7 +118,7 @@ To support files with inline source maps, the `hookRequire` options can be speci
115118

116119

117120
```js
118-
require('source-map-support').install({
121+
require('@cspotcode/source-map-support').install({
119122
hookRequire: true
120123
});
121124
```
@@ -135,7 +138,7 @@ throw new Error('test'); // This is the original code
135138
compiled.js:
136139

137140
```js
138-
require('source-map-support').install();
141+
require('@cspotcode/source-map-support').install();
139142

140143
throw new Error('test'); // This is the compiled code
141144
// The next line defines the sourceMapping.
@@ -179,7 +182,7 @@ demo.ts:
179182

180183
```typescript
181184
declare function require(name: string);
182-
require('source-map-support').install();
185+
require('@cspotcode/source-map-support').install();
183186
class Foo {
184187
constructor() { this.bar(); }
185188
bar() { throw new Error('this is a demo'); }
@@ -210,7 +213,7 @@ Error: this is a demo
210213
at node.js:901:3
211214
```
212215

213-
There is also the option to use `-r source-map-support/register` with typescript, without the need add the `require('source-map-support').install()` in the code base:
216+
There is also the option to use `-r source-map-support/register` with typescript, without the need add the `require('@cspotcode/source-map-support').install()` in the code base:
214217

215218
```
216219
$ npm install source-map-support typescript
@@ -238,7 +241,7 @@ Error: this is a demo
238241
demo.coffee:
239242

240243
```coffee
241-
require('source-map-support').install()
244+
require('@cspotcode/source-map-support').install()
242245
foo = ->
243246
bar = -> throw new Error 'this is a demo'
244247
bar()
@@ -248,7 +251,7 @@ foo()
248251
Compile and run the file using the CoffeeScript compiler from the terminal:
249252

250253
```sh
251-
$ npm install source-map-support coffeescript
254+
$ npm install @cspotcode/source-map-support coffeescript
252255
$ node_modules/.bin/coffee --map --compile demo.coffee
253256
$ node demo.js
254257

package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
{
2-
"name": "source-map-support",
2+
"name": "@cspotcode/source-map-support",
33
"description": "Fixes stack traces for files with source maps",
44
"version": "0.5.19",
55
"main": "./source-map-support.js",
66
"scripts": {
77
"build": "node build.js",
88
"serve-tests": "http-server -p 1336",
9-
"prepublish": "npm run build",
109
"test": "mocha"
1110
},
11+
"files": [
12+
"source-map-support.js",
13+
"register.js"
14+
],
1215
"dependencies": {
13-
"buffer-from": "^1.0.0",
1416
"source-map": "^0.7.3"
1517
},
1618
"devDependencies": {

source-map-support.js

+30-8
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ try {
1212
/* nop */
1313
}
1414

15-
var bufferFrom = require('buffer-from');
16-
1715
/**
1816
* Requires a module which is protected against bundler minification.
1917
*
@@ -171,7 +169,7 @@ retrieveMapHandlers.push(function(source) {
171169
if (reSourceMap.test(sourceMappingURL)) {
172170
// Support source map URL as a data url
173171
var rawData = sourceMappingURL.slice(sourceMappingURL.indexOf(',') + 1);
174-
sourceMapData = bufferFrom(rawData, "base64").toString();
172+
sourceMapData = Buffer.from(rawData, "base64").toString();
175173
sourceMappingURL = source;
176174
} else {
177175
// Support source map URLs relative to the source URL
@@ -408,6 +406,19 @@ function wrapCallSite(frame, state) {
408406
return frame;
409407
}
410408

409+
var kIsNodeError = undefined;
410+
try {
411+
// Get a deliberate ERR_INVALID_ARG_TYPE
412+
// TODO is there a better way to reliably get an instance of NodeError?
413+
path.resolve(123);
414+
} catch(e) {
415+
const symbols = Object.getOwnPropertySymbols(e);
416+
const symbol = symbols.find(function (s) {return s.toString().indexOf('kIsNodeError') >= 0});
417+
if(symbol) kIsNodeError = symbol;
418+
}
419+
420+
const ErrorPrototypeToString = (err) =>Error.prototype.toString.call(err);
421+
411422
// This function is part of the V8 stack trace API, for more info see:
412423
// https://v8.dev/docs/stack-trace-api
413424
function prepareStackTrace(error, stack) {
@@ -416,9 +427,21 @@ function prepareStackTrace(error, stack) {
416427
sourceMapCache = {};
417428
}
418429

419-
var name = error.name || 'Error';
420-
var message = error.message || '';
421-
var errorString = name + ": " + message;
430+
// node gives its own errors special treatment. Mimic that behavior
431+
// https://github.com/nodejs/node/blob/3cbaabc4622df1b4009b9d026a1a970bdbae6e89/lib/internal/errors.js#L118-L128
432+
// https://github.com/nodejs/node/pull/39182
433+
var errorString;
434+
if (kIsNodeError) {
435+
if(kIsNodeError in error) {
436+
errorString = `${error.name} [${error.code}]: ${error.message}`;
437+
} else {
438+
errorString = ErrorPrototypeToString(error);
439+
}
440+
} else {
441+
var name = error.name || 'Error';
442+
var message = error.message || '';
443+
errorString = name + ": " + message;
444+
}
422445

423446
var state = { nextPosition: null, curPosition: null };
424447
var processedStack = [];
@@ -471,11 +494,10 @@ function printErrorAndExit (error) {
471494
}
472495

473496
if (source) {
474-
console.error();
475497
console.error(source);
476498
}
477499

478-
console.error(error.stack);
500+
console.error(error);
479501
process.exit(1);
480502
}
481503

test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var SourceMapGenerator = require('source-map').SourceMapGenerator;
66
var child_process = require('child_process');
77
var assert = require('assert');
88
var fs = require('fs');
9-
var bufferFrom = require('buffer-from');
9+
var bufferFrom = Buffer.from;
1010

1111
function compareLines(actual, expected) {
1212
assert(actual.length >= expected.length, 'got ' + actual.length + ' lines but expected at least ' + expected.length + ' lines');

0 commit comments

Comments
 (0)