Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
f93d03d
node-api: define version 8
gabrielschulhof Mar 7, 2021
5ff4959
crypto: add optional callback to crypto.sign and crypto.verify
panva Feb 24, 2021
1c62f2d
deps: V8: cherry-pick 1648e050cade
cjihrig Mar 9, 2021
7fd2a69
test: fixup flaky test-crypto-x509
panva Mar 11, 2021
7530873
doc: crypto esm examples
jasnell Mar 3, 2021
4f892af
src: fix variable name of OnCloseReceived callback
tniessen Feb 26, 2021
4233d6c
doc: use sentence case in README.md headers
marsonya Mar 6, 2021
6ef1d33
crypto: add internal error codes
RaisinTen Mar 7, 2021
bb1967c
module: refactor NativeModule to avoid unsafe array iteration
aduh95 Feb 12, 2021
71150ee
doc: fix typo in description of close event
tniessen Mar 8, 2021
756328a
stream,util: fix "the the" typo in comments
lpinca Mar 9, 2021
5d12de3
lib: fix typo in lib/internal/http2/core.js
marsonya Mar 10, 2021
ce0350d
doc: fix link to googletest fixtures
tniessen Mar 10, 2021
28e827f
crypto: reconcile duplicated code
jasnell Mar 11, 2021
2577f10
test,crypto: ensure promises resolve in webcrypto tests
aduh95 Mar 8, 2021
77c5c2f
http: refactor to avoid unsafe array iteration
aduh95 Feb 12, 2021
311ddc9
tls: refactor to avoid unsafe array iteration
aduh95 Feb 12, 2021
e556215
fs: improve fsPromises writeFile performance
Linkgoron Mar 5, 2021
ea10155
benchmark: add benchmark for fsPromises.writeFile
Linkgoron Mar 5, 2021
ce8ac4d
assert,util: fix commutativity edge case
BridgeAR Mar 11, 2021
efd14a9
deps: upgrade npm to 7.6.3
ruyadorno Mar 11, 2021
3acba1d
events: remove return value on addEventListener
jasnell Mar 10, 2021
e4667eb
http2: make res.req a normal property
cjihrig Mar 11, 2021
8c084d9
lib: use AbortError consistently
jasnell Mar 11, 2021
0563260
doc: recommend checking abortSignal.aborted first
jasnell Mar 11, 2021
8fdc4a5
util: inspect __proto__ key as written in object literal
addaleax Mar 11, 2021
e6e2112
test: address flaky wpt/test-timers
Trott Mar 13, 2021
8d0af3e
deps: switch openssl to quictls/openssl
jasnell Mar 4, 2021
53b9805
deps: update archs files for OpenSSL-1.1.1+quic
jasnell Mar 4, 2021
345af4d
test: fixup test to account for quic openssl version
jasnell Mar 4, 2021
faffb61
doc: update maintaining-openssl guide
jasnell Mar 4, 2021
b62c08b
worker: add setEnvironmentData/getEnvironmentData
jasnell Feb 23, 2021
a4e9edb
lib: implement AbortSignal.abort()
jasnell Mar 10, 2021
ba5a0d9
test: update dom/abort tests
jasnell Mar 15, 2021
fb46d03
doc: add hints to http.request() options
lpinca Mar 13, 2021
35b0395
errors: remove experimental from --enable-source-maps
Mar 13, 2021
dba24b3
tools: use bundled npm in update scripts
ruyadorno Mar 4, 2021
bcbdcfc
src,test: support dynamically linking OpenSSL 3.0
danbev Oct 26, 2020
9fad821
doc: add marsonya as a triager
marsonya Mar 9, 2021
8a94870
doc: update v14 and v12 changelogs
danielleadams Mar 16, 2021
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
Prev Previous commit
Next Next commit
lib: use AbortError consistently
Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: #37715
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
jasnell authored and danielleadams committed Mar 16, 2021
commit 8c084d938a781d69bfeac9cb2372607684e2a29a
32 changes: 11 additions & 21 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,14 @@ const kRejection = SymbolFor('nodejs.rejection');
let spliceOne;

const {
hideStackFrames,
AbortError,
kEnhanceStackBeforeInspector,
codes
codes: {
ERR_INVALID_ARG_TYPE,
ERR_OUT_OF_RANGE,
ERR_UNHANDLED_ERROR
},
} = require('internal/errors');
const {
ERR_INVALID_ARG_TYPE,
ERR_OUT_OF_RANGE,
ERR_UNHANDLED_ERROR
} = codes;

const {
inspect
Expand All @@ -76,14 +75,6 @@ const kMaxEventTargetListeners = Symbol('events.maxEventTargetListeners');
const kMaxEventTargetListenersWarned =
Symbol('events.maxEventTargetListenersWarned');

let DOMException;
const lazyDOMException = hideStackFrames((message, name) => {
if (DOMException === undefined)
DOMException = internalBinding('messaging').DOMException;
return new DOMException(message, name);
});


function EventEmitter(opts) {
FunctionPrototypeCall(EventEmitter.init, this, opts);
}
Expand Down Expand Up @@ -713,7 +704,7 @@ async function once(emitter, name, options = {}) {
const signal = options?.signal;
validateAbortSignal(signal, 'options.signal');
if (signal?.aborted)
throw lazyDOMException('The operation was aborted', 'AbortError');
throw new AbortError();
return new Promise((resolve, reject) => {
const errorListener = (err) => {
emitter.removeListener(name, resolver);
Expand All @@ -738,7 +729,7 @@ async function once(emitter, name, options = {}) {
function abortListener() {
eventTargetAgnosticRemoveListener(emitter, name, resolver);
eventTargetAgnosticRemoveListener(emitter, 'error', errorListener);
reject(lazyDOMException('The operation was aborted', 'AbortError'));
reject(new AbortError());
}
if (signal != null) {
eventTargetAgnosticAddListener(
Expand Down Expand Up @@ -783,9 +774,8 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
function on(emitter, event, options) {
const signal = options?.signal;
validateAbortSignal(signal, 'options.signal');
if (signal?.aborted) {
throw lazyDOMException('The operation was aborted', 'AbortError');
}
if (signal?.aborted)
throw new AbortError();

const unconsumedEvents = [];
const unconsumedPromises = [];
Expand Down Expand Up @@ -873,7 +863,7 @@ function on(emitter, event, options) {
return iterator;

function abortListener() {
errorHandler(lazyDOMException('The operation was aborted', 'AbortError'));
errorHandler(new AbortError());
}

function eventHandler(...args) {
Expand Down
30 changes: 14 additions & 16 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const {
ERR_INVALID_ARG_TYPE,
ERR_FEATURE_UNAVAILABLE_ON_PLATFORM,
},
hideStackFrames,
AbortError,
uvErrmapGet,
uvException
} = require('internal/errors');
Expand Down Expand Up @@ -148,13 +148,6 @@ let ReadStream;
let WriteStream;
let rimraf;
let rimrafSync;
let DOMException;

const lazyDOMException = hideStackFrames((message, name) => {
if (DOMException === undefined)
DOMException = internalBinding('messaging').DOMException;
return new DOMException(message, name);
});

// These have to be separate because of how graceful-fs happens to do it's
// monkeypatching.
Expand Down Expand Up @@ -324,6 +317,14 @@ function readFileAfterStat(err, stats) {
context.read();
}

function checkAborted(signal, callback) {
if (signal?.aborted) {
callback(new AbortError());
return true;
}
return false;
}

function readFile(path, options, callback) {
callback = maybeCallback(callback || options);
options = getOptions(options, { flag: 'r' });
Expand All @@ -342,10 +343,8 @@ function readFile(path, options, callback) {
return;
}

if (options.signal?.aborted) {
callback(lazyDOMException('The operation was aborted', 'AbortError'));
if (checkAborted(options.signal, callback))
return;
}

const flagsNumber = stringToFlags(options.flag);
path = getValidatedPath(path);
Expand Down Expand Up @@ -1459,10 +1458,10 @@ function lutimesSync(path, atime, mtime) {
function writeAll(fd, isUserFd, buffer, offset, length, signal, callback) {
if (signal?.aborted) {
if (isUserFd) {
callback(lazyDOMException('The operation was aborted', 'AbortError'));
callback(new AbortError());
} else {
fs.close(fd, function() {
callback(lazyDOMException('The operation was aborted', 'AbortError'));
callback(new AbortError());
});
}
return;
Expand Down Expand Up @@ -1508,10 +1507,9 @@ function writeFile(path, data, options, callback) {
return;
}

if (options.signal?.aborted) {
callback(lazyDOMException('The operation was aborted', 'AbortError'));
if (checkAborted(options.signal, callback))
return;
}

fs.open(path, flag, options.mode, (openErr, fd) => {
if (openErr) {
callback(openErr);
Expand Down
51 changes: 20 additions & 31 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ const {
const binding = internalBinding('fs');
const { Buffer } = require('buffer');

const { codes, hideStackFrames } = require('internal/errors');
const {
ERR_FS_FILE_TOO_LARGE,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_METHOD_NOT_IMPLEMENTED,
} = codes;
codes: {
ERR_FS_FILE_TOO_LARGE,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_METHOD_NOT_IMPLEMENTED,
},
AbortError,
} = require('internal/errors');
const { isArrayBufferView } = require('internal/util/types');
const { rimrafPromises } = require('internal/fs/rimraf');
const {
Expand Down Expand Up @@ -93,13 +95,6 @@ const {
const getDirectoryEntriesPromise = promisify(getDirents);
const validateRmOptionsPromise = promisify(validateRmOptions);

let DOMException;
const lazyDOMException = hideStackFrames((message, name) => {
if (DOMException === undefined)
DOMException = internalBinding('messaging').DOMException;
return new DOMException(message, name);
});

class FileHandle extends EventEmitterMixin(JSTransferable) {
constructor(filehandle) {
super();
Expand Down Expand Up @@ -272,15 +267,18 @@ async function fsCall(fn, handle, ...args) {
}
}

function checkAborted(signal) {
if (signal?.aborted)
throw new AbortError();
}

async function writeFileHandle(filehandle, data, signal) {
// `data` could be any kind of typed array.
data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
let remaining = data.length;
if (remaining === 0) return;
do {
if (signal?.aborted) {
throw lazyDOMException('The operation was aborted', 'AbortError');
}
checkAborted(signal);
const { bytesWritten } =
await write(filehandle, data, 0,
MathMin(kWriteFileMaxChunkSize, data.length));
Expand All @@ -296,14 +294,11 @@ async function writeFileHandle(filehandle, data, signal) {
async function readFileHandle(filehandle, options) {
const signal = options?.signal;

if (signal?.aborted) {
throw lazyDOMException('The operation was aborted', 'AbortError');
}
checkAborted(signal);

const statFields = await binding.fstat(filehandle.fd, false, kUsePromises);

if (signal?.aborted) {
throw lazyDOMException('The operation was aborted', 'AbortError');
}
checkAborted(signal);

let size;
if ((statFields[1/* mode */] & S_IFMT) === S_IFREG) {
Expand All @@ -321,9 +316,7 @@ async function readFileHandle(filehandle, options) {
const buffers = [];
const fullBuffer = noSize ? undefined : Buffer.allocUnsafeSlow(size);
do {
if (signal?.aborted) {
throw lazyDOMException('The operation was aborted', 'AbortError');
}
checkAborted(signal);
let buffer;
let offset;
let length;
Expand Down Expand Up @@ -693,9 +686,7 @@ async function writeFile(path, data, options) {
if (path instanceof FileHandle)
return writeFileHandle(path, data, options.signal);

if (options.signal?.aborted) {
throw lazyDOMException('The operation was aborted', 'AbortError');
}
checkAborted(options.signal);

const fd = await open(path, flag, options.mode);
const { signal } = options;
Expand All @@ -716,9 +707,7 @@ async function readFile(path, options) {
if (path instanceof FileHandle)
return readFileHandle(path, options);

if (options.signal?.aborted) {
throw lazyDOMException('The operation was aborted', 'AbortError');
}
checkAborted(options.signal);

const fd = await open(path, flag, 0o666);
return PromisePrototypeFinally(readFileHandle(fd, options), fd.close);
Expand Down
16 changes: 4 additions & 12 deletions lib/internal/fs/read_file_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,9 @@ const { Buffer } = require('buffer');

const { FSReqCallback, close, read } = internalBinding('fs');

const { hideStackFrames } = require('internal/errors');


let DOMException;
const lazyDOMException = hideStackFrames((message, name) => {
if (DOMException === undefined)
DOMException = internalBinding('messaging').DOMException;
return new DOMException(message, name);
});
const {
AbortError,
} = require('internal/errors');

// Use 64kb in case the file type is not a regular file and thus do not know the
// actual file size. Increasing the value further results in more frequent over
Expand Down Expand Up @@ -95,9 +89,7 @@ class ReadFileContext {
let length;

if (this.signal?.aborted) {
return this.close(
lazyDOMException('The operation was aborted', 'AbortError')
);
return this.close(new AbortError());
}
if (this.size === 0) {
buffer = Buffer.allocUnsafeSlow(kReadFileUnknownBufferLength);
Expand Down