Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #291: Removes error code from node errors #292

Draft
wants to merge 21 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 45 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Continuous Integration
on:
# branches pushed by collaborators
push:
branches:
- master
# pull request from non-collaborators
pull_request: {}
# nightly
schedule:
- cron: '0 0 * * *'
jobs:
build:
name: "Test: ${{ matrix.os }}, node ${{ matrix.node }}"
runs-on: ${{ matrix.os }}-latest
strategy:
fail-fast: false
matrix:
os: [ubuntu, windows]
node:
- 16
- 14
- 12
steps:
# checkout code
- uses: actions/checkout@v2
# install node
- name: Use Node.js ${{ matrix.os }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}

# npm install with caching
- run: |
npm config set cache "$( node -p "process.cwd()" )/temp/npm-cache"
- name: Cache dependencies
uses: actions/cache@v2
with:
path: temp/npm-cache
key: npm-cache-${{ matrix.os }} ${{ matrix.node }}-${{ hashFiles('package-lock.json') }}
# restore-keys: npm-cache-${{ matrix.os }} ${{ matrix.node }}-
- run: npm install

# Run tests
- run: npm test
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"test": "mocha"
},
"dependencies": {
"buffer-from": "^1.0.0",
"source-map": "^0.6.0"
},
"devDependencies": {
Expand All @@ -27,5 +26,8 @@
"bugs": {
"url": "https://github.com/evanw/node-source-map-support/issues"
},
"license": "MIT"
"license": "MIT",
"engines": {
"node": ">=12"
}
}
38 changes: 30 additions & 8 deletions source-map-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ try {
/* nop */
}

var bufferFrom = require('buffer-from');

/**
* Requires a module which is protected against bundler minification.
*
Expand Down Expand Up @@ -171,7 +169,7 @@ retrieveMapHandlers.push(function(source) {
if (reSourceMap.test(sourceMappingURL)) {
// Support source map URL as a data url
var rawData = sourceMappingURL.slice(sourceMappingURL.indexOf(',') + 1);
sourceMapData = bufferFrom(rawData, "base64").toString();
sourceMapData = Buffer.from(rawData, "base64").toString();
sourceMappingURL = source;
} else {
// Support source map URLs relative to the source URL
Expand Down Expand Up @@ -408,6 +406,19 @@ function wrapCallSite(frame, state) {
return frame;
}

var kIsNodeError = undefined;
try {
// Get a deliberate ERR_INVALID_ARG_TYPE
// TODO is there a better way to reliably get an instance of NodeError?
path.resolve(123);
} catch(e) {
const symbols = Object.getOwnPropertySymbols(e);
const symbol = symbols.find(function (s) {return s.toString().indexOf('kIsNodeError') >= 0});
if(symbol) kIsNodeError = symbol;
}

const ErrorPrototypeToString = (err) =>Error.prototype.toString.call(err);

// This function is part of the V8 stack trace API, for more info see:
// https://v8.dev/docs/stack-trace-api
function prepareStackTrace(error, stack) {
Expand All @@ -416,9 +427,21 @@ function prepareStackTrace(error, stack) {
sourceMapCache = {};
}

var name = error.name || 'Error';
var message = error.message || '';
var errorString = name + ": " + message;
// node gives its own errors special treatment. Mimic that behavior
// https://github.com/nodejs/node/blob/3cbaabc4622df1b4009b9d026a1a970bdbae6e89/lib/internal/errors.js#L118-L128
// https://github.com/nodejs/node/pull/39182
var errorString;
if (kIsNodeError) {
if(kIsNodeError in error) {
errorString = `${error.name} [${error.code}]: ${error.message}`;
} else {
errorString = ErrorPrototypeToString(error);
}
} else {
var name = error.name || 'Error';
var message = error.message || '';
errorString = name + ": " + message;
}

var state = { nextPosition: null, curPosition: null };
var processedStack = [];
Expand Down Expand Up @@ -471,11 +494,10 @@ function printErrorAndExit (error) {
}

if (source) {
console.error();
console.error(source);
}

console.error(error.stack);
console.error(error);
process.exit(1);
}

Expand Down
8 changes: 4 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var SourceMapGenerator = require('source-map').SourceMapGenerator;
var child_process = require('child_process');
var assert = require('assert');
var fs = require('fs');
var bufferFrom = require('buffer-from');
var bufferFrom = Buffer.from;

function compareLines(actual, expected) {
assert(actual.length >= expected.length, 'got ' + actual.length + ' lines but expected at least ' + expected.length + ' lines');
Expand Down Expand Up @@ -86,7 +86,7 @@ function createMultiLineSourceMapWithSourcesContent() {

function compareStackTrace(sourceMap, source, expected) {
// Check once with a separate source map
fs.writeFileSync('.generated.js.map', sourceMap);
fs.writeFileSync('.generated.js.map', sourceMap.toString());
fs.writeFileSync('.generated.js', 'exports.test = function() {' +
source.join('\n') + '};//@ sourceMappingURL=.generated.js.map');
try {
Expand All @@ -113,7 +113,7 @@ function compareStackTrace(sourceMap, source, expected) {

function compareStdout(done, sourceMap, source, expected) {
fs.writeFileSync('.original.js', 'this is the original code');
fs.writeFileSync('.generated.js.map', sourceMap);
fs.writeFileSync('.generated.js.map', sourceMap.toString());
fs.writeFileSync('.generated.js', source.join('\n') +
'//@ sourceMappingURL=.generated.js.map');
child_process.exec('node ./.generated', function(error, stdout, stderr) {
Expand Down Expand Up @@ -616,7 +616,7 @@ it('handleUncaughtExceptions is true with existing listener', function(done) {
];

fs.writeFileSync('.original.js', 'this is the original code');
fs.writeFileSync('.generated.js.map', createSingleLineSourceMap());
fs.writeFileSync('.generated.js.map', createSingleLineSourceMap().toString());
fs.writeFileSync('.generated.js', source.join('\n'));

child_process.exec('node ./.generated', function(error, stdout, stderr) {
Expand Down