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

src: return proper URLs from node_api_get_module_file_name #41758

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "node_buffer.h"
#include "node_errors.h"
#include "node_internals.h"
#include "node_url.h"
#include "threadpoolwork-inl.h"
#include "tracing/traced_value.h"
#include "util-inl.h"
Expand Down Expand Up @@ -589,13 +590,13 @@ void napi_module_register_by_symbol(v8::Local<v8::Object> exports,
if (module->ToObject(context).ToLocal(&modobj) &&
modobj->Get(context, node_env->filename_string()).ToLocal(&filename_js) &&
filename_js->IsString()) {
node::Utf8Value filename(node_env->isolate(), filename_js); // Cast
node::Utf8Value filename(node_env->isolate(), filename_js);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was no cast here, since we can only end up here if filename_js is a string, hence the removed comment


// Turn the absolute path into a URL. Currently the absolute path is always
// a file system path.
// TODO(gabrielschulhof): Pass the `filename` through unchanged if/when we
// receive it as a URL already.
module_filename = std::string("file://") + (*filename);
module_filename = node::url::URL::FromFilePath(filename.ToString()).href();
}

// Create a new napi_env for this specific module.
Expand Down
34 changes: 27 additions & 7 deletions test/node-api/test_general/test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
'use strict';

const common = require('../../common');
const tmpdir = require('../../common/tmpdir');
const fs = require('fs');
const path = require('path');
const url = require('url');
const filename = require.resolve(`./build/${common.buildType}/test_general`);
const test_general = require(filename);
const assert = require('assert');

// TODO(gabrielschulhof): This test may need updating if/when the filename
// becomes a full-fledged URL.
assert.strictEqual(test_general.filename, `file://${filename}`);
tmpdir.refresh();

const [ major, minor, patch, release ] = test_general.testGetNodeVersion();
assert.strictEqual(process.version.split('-')[0],
`v${major}.${minor}.${patch}`);
assert.strictEqual(release, process.release.name);
{
// TODO(gabrielschulhof): This test may need updating if/when the filename
// becomes a full-fledged URL.
assert.strictEqual(test_general.filename, url.pathToFileURL(filename).href);
}

{
const urlTestDir = path.join(tmpdir.path, 'foo%#bar');
const urlTestFile = path.join(urlTestDir, path.basename(filename));
fs.mkdirSync(urlTestDir, { recursive: true });
fs.copyFileSync(filename, urlTestFile);
const reportedFilename = require(urlTestFile).filename;
assert.doesNotMatch(reportedFilename, /foo%#bar/);
assert.strictEqual(reportedFilename, url.pathToFileURL(urlTestFile).href);
}

{
const [ major, minor, patch, release ] = test_general.testGetNodeVersion();
assert.strictEqual(process.version.split('-')[0],
`v${major}.${minor}.${patch}`);
assert.strictEqual(release, process.release.name);
}