Skip to content

Commit

Permalink
lib: remove aix directory case for package reader
Browse files Browse the repository at this point in the history
PR-URL: #48605
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Rafael Gonzaga <[email protected]>
  • Loading branch information
anonrig authored Jul 7, 2023
1 parent d9438cc commit 3205b19
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 56 deletions.
15 changes: 2 additions & 13 deletions lib/internal/modules/package_json_reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const { kEmptyObject, setOwnProperty } = require('internal/util');
const { fileURLToPath, pathToFileURL } = require('internal/url');

const cache = new SafeMap();
const isAIX = process.platform === 'aix';

let manifest;

Expand Down Expand Up @@ -45,10 +44,7 @@ function read(jsonPath, { base, specifier, isESM } = kEmptyObject) {
return cache.get(jsonPath);
}

const {
0: string,
1: containsKeys,
} = internalModuleReadJSON(
const string = internalModuleReadJSON(
toNamespacedPath(jsonPath),
);
const result = {
Expand All @@ -62,14 +58,7 @@ function read(jsonPath, { base, specifier, isESM } = kEmptyObject) {
imports: undefined,
};

// Folder read operation succeeds in AIX.
// For libuv change, see https://github.com/libuv/libuv/pull/2025.
// https://github.com/nodejs/node/pull/48477#issuecomment-1604586650
// TODO(anonrig): Follow-up on this change and remove it since it is a
// semver-major change.
const isResultValid = isAIX && !isESM ? containsKeys : string !== undefined;

if (isResultValid) {
if (string !== undefined) {
let parsed;
try {
parsed = JSONParse(string);
Expand Down
40 changes: 2 additions & 38 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ namespace fs {

using v8::Array;
using v8::BigInt;
using v8::Boolean;
using v8::Context;
using v8::EscapableHandleScope;
using v8::Function;
Expand Down Expand Up @@ -1035,15 +1034,13 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
env, permission::PermissionScope::kFileSystemRead, path.ToStringView());

if (strlen(*path) != path.length()) {
args.GetReturnValue().Set(Array::New(isolate));
return; // Contains a nul byte.
}
uv_fs_t open_req;
const int fd = uv_fs_open(loop, &open_req, *path, O_RDONLY, 0, nullptr);
uv_fs_req_cleanup(&open_req);

if (fd < 0) {
args.GetReturnValue().Set(Array::New(isolate));
return;
}

Expand All @@ -1070,7 +1067,6 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
uv_fs_req_cleanup(&read_req);

if (numchars < 0) {
args.GetReturnValue().Set(Array::New(isolate));
return;
}
offset += numchars;
Expand All @@ -1082,42 +1078,10 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
}
const size_t size = offset - start;

// TODO(anonrig): Follow-up on removing the following changes for AIX.
char* p = &chars[start];
char* pe = &chars[size];
char* pos[2];
char** ppos = &pos[0];

while (p < pe) {
char c = *p++;
if (c == '\\' && p < pe && *p == '"') p++;
if (c != '"') continue;
*ppos++ = p;
if (ppos < &pos[2]) continue;
ppos = &pos[0];

char* s = &pos[0][0];
char* se = &pos[1][-1]; // Exclude quote.
size_t n = se - s;

if (n == 4) {
if (0 == memcmp(s, "main", 4)) break;
if (0 == memcmp(s, "name", 4)) break;
if (0 == memcmp(s, "type", 4)) break;
} else if (n == 7) {
if (0 == memcmp(s, "exports", 7)) break;
if (0 == memcmp(s, "imports", 7)) break;
}
}

Local<Value> return_value[] = {
args.GetReturnValue().Set(
String::NewFromUtf8(
isolate, &chars[start], v8::NewStringType::kNormal, size)
.ToLocalChecked(),
Boolean::New(isolate, p < pe ? true : false)};

args.GetReturnValue().Set(
Array::New(isolate, return_value, arraysize(return_value)));
.ToLocalChecked());
}

// Used to speed up module loading. Returns 0 if the path refers to
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-module-binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ const { readFileSync } = require('fs');
const { strictEqual, deepStrictEqual } = require('assert');

{
strictEqual(internalModuleReadJSON('nosuchfile')[0], undefined);
strictEqual(internalModuleReadJSON('nosuchfile'), undefined);
}
{
strictEqual(internalModuleReadJSON(fixtures.path('empty.txt'))[0], '');
strictEqual(internalModuleReadJSON(fixtures.path('empty.txt')), '');
}
{
strictEqual(internalModuleReadJSON(fixtures.path('empty-with-bom.txt'))[0], '');
strictEqual(internalModuleReadJSON(fixtures.path('empty-with-bom.txt')), '');
}
{
const filename = fixtures.path('require-bin/package.json');
const returnValue = JSON.parse(internalModuleReadJSON(filename)[0]);
const returnValue = JSON.parse(internalModuleReadJSON(filename));
const file = JSON.parse(readFileSync(filename, 'utf-8'));
const expectedValue = filterOwnProperties(file, ['name', 'main', 'exports', 'imports', 'type']);
deepStrictEqual({
Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-module-loading-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@ assert.throws(
message: 'The argument \'id\' must be a non-empty string. Received \'\''
});

// Folder read operation succeeds in AIX.
// For libuv change, see https://github.com/libuv/libuv/pull/2025.
// https://github.com/nodejs/node/pull/48477#issuecomment-1604586650
assert.throws(
() => { require('../fixtures/packages/is-dir'); },
{
common.isAIX ? { code: 'ERR_INVALID_PACKAGE_CONFIG' } : {
code: 'MODULE_NOT_FOUND',
message: /Cannot find module '\.\.\/fixtures\/packages\/is-dir'/
}
Expand Down

0 comments on commit 3205b19

Please sign in to comment.