2017-01-03 21:16:48 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2018-01-23 00:17:21 +00:00
|
|
|
// Flags: --expose_externalize_string
|
2015-05-19 11:00:06 +00:00
|
|
|
'use strict';
|
2016-12-01 17:34:21 +00:00
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const fs = require('fs');
|
2017-12-25 06:38:11 +00:00
|
|
|
const tmpdir = require('../common/tmpdir');
|
|
|
|
|
|
|
|
tmpdir.refresh();
|
|
|
|
|
2023-08-15 13:45:14 +00:00
|
|
|
const fn = tmpdir.resolve('write.txt');
|
|
|
|
const fn2 = tmpdir.resolve('write2.txt');
|
|
|
|
const fn3 = tmpdir.resolve('write3.txt');
|
|
|
|
const fn4 = tmpdir.resolve('write4.txt');
|
2016-12-01 17:34:21 +00:00
|
|
|
const expected = 'ümlaut.';
|
|
|
|
const constants = fs.constants;
|
2009-09-17 06:28:31 +00:00
|
|
|
|
2023-05-06 11:50:44 +00:00
|
|
|
const {
|
|
|
|
createExternalizableString,
|
|
|
|
externalizeString,
|
|
|
|
isOneByteString,
|
|
|
|
} = global;
|
2020-12-13 13:16:23 +00:00
|
|
|
|
|
|
|
// Account for extra globals exposed by --expose_externalize_string.
|
2023-05-06 11:50:44 +00:00
|
|
|
common.allowGlobals(
|
|
|
|
createExternalizableString,
|
|
|
|
externalizeString,
|
|
|
|
isOneByteString,
|
|
|
|
global.x,
|
|
|
|
);
|
2018-01-23 00:17:21 +00:00
|
|
|
|
|
|
|
{
|
2023-05-06 11:50:44 +00:00
|
|
|
// Must be a unique string.
|
|
|
|
const expected = createExternalizableString('ümlaut sechzig');
|
2018-01-23 00:17:21 +00:00
|
|
|
externalizeString(expected);
|
2018-10-12 17:03:05 +00:00
|
|
|
assert.strictEqual(isOneByteString(expected), true);
|
2018-01-23 00:17:21 +00:00
|
|
|
const fd = fs.openSync(fn, 'w');
|
|
|
|
fs.writeSync(fd, expected, 0, 'latin1');
|
|
|
|
fs.closeSync(fd);
|
2018-10-12 17:03:05 +00:00
|
|
|
assert.strictEqual(fs.readFileSync(fn, 'latin1'), expected);
|
2018-01-23 00:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-05-06 11:50:44 +00:00
|
|
|
// Must be a unique string.
|
|
|
|
const expected = createExternalizableString('ümlaut neunzig');
|
2018-01-23 00:17:21 +00:00
|
|
|
externalizeString(expected);
|
2018-10-12 17:03:05 +00:00
|
|
|
assert.strictEqual(isOneByteString(expected), true);
|
2018-01-23 00:17:21 +00:00
|
|
|
const fd = fs.openSync(fn, 'w');
|
|
|
|
fs.writeSync(fd, expected, 0, 'utf8');
|
|
|
|
fs.closeSync(fd);
|
2018-10-12 17:03:05 +00:00
|
|
|
assert.strictEqual(fs.readFileSync(fn, 'utf8'), expected);
|
2018-01-23 00:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-05-06 11:50:44 +00:00
|
|
|
// Must be a unique string.
|
|
|
|
const expected = createExternalizableString('Zhōngwén 1');
|
2018-01-23 00:17:21 +00:00
|
|
|
externalizeString(expected);
|
2018-10-12 17:03:05 +00:00
|
|
|
assert.strictEqual(isOneByteString(expected), false);
|
2018-01-23 00:17:21 +00:00
|
|
|
const fd = fs.openSync(fn, 'w');
|
|
|
|
fs.writeSync(fd, expected, 0, 'ucs2');
|
|
|
|
fs.closeSync(fd);
|
2018-10-12 17:03:05 +00:00
|
|
|
assert.strictEqual(fs.readFileSync(fn, 'ucs2'), expected);
|
2018-01-23 00:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-05-06 11:50:44 +00:00
|
|
|
// Must be a unique string.
|
|
|
|
const expected = createExternalizableString('Zhōngwén 2');
|
2018-01-23 00:17:21 +00:00
|
|
|
externalizeString(expected);
|
2018-10-12 17:03:05 +00:00
|
|
|
assert.strictEqual(isOneByteString(expected), false);
|
2018-01-23 00:17:21 +00:00
|
|
|
const fd = fs.openSync(fn, 'w');
|
|
|
|
fs.writeSync(fd, expected, 0, 'utf8');
|
|
|
|
fs.closeSync(fd);
|
2018-10-12 17:03:05 +00:00
|
|
|
assert.strictEqual(fs.readFileSync(fn, 'utf8'), expected);
|
2018-01-23 00:17:21 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 20:27:07 +00:00
|
|
|
fs.open(fn, 'w', 0o644, common.mustSucceed((fd) => {
|
|
|
|
const done = common.mustSucceed((written) => {
|
2018-10-12 17:03:05 +00:00
|
|
|
assert.strictEqual(written, Buffer.byteLength(expected));
|
2010-02-20 00:02:30 +00:00
|
|
|
fs.closeSync(fd);
|
2016-07-15 19:43:24 +00:00
|
|
|
const found = fs.readFileSync(fn, 'utf8');
|
2010-02-20 00:02:30 +00:00
|
|
|
fs.unlinkSync(fn);
|
2018-10-12 17:03:05 +00:00
|
|
|
assert.strictEqual(found, expected);
|
2017-11-06 15:52:33 +00:00
|
|
|
});
|
|
|
|
|
2020-09-06 20:27:07 +00:00
|
|
|
const written = common.mustSucceed((written) => {
|
2018-10-12 17:03:05 +00:00
|
|
|
assert.strictEqual(written, 0);
|
2018-01-25 15:01:01 +00:00
|
|
|
fs.write(fd, expected, 0, 'utf8', done);
|
2017-11-06 15:52:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
fs.write(fd, '', 0, 'utf8', written);
|
2016-07-15 19:43:24 +00:00
|
|
|
}));
|
2009-09-17 06:28:31 +00:00
|
|
|
|
2017-11-06 15:52:33 +00:00
|
|
|
const args = constants.O_CREAT | constants.O_WRONLY | constants.O_TRUNC;
|
2020-09-06 20:27:07 +00:00
|
|
|
fs.open(fn2, args, 0o644, common.mustSucceed((fd) => {
|
|
|
|
const done = common.mustSucceed((written) => {
|
2018-10-12 17:03:05 +00:00
|
|
|
assert.strictEqual(written, Buffer.byteLength(expected));
|
2017-11-06 15:52:33 +00:00
|
|
|
fs.closeSync(fd);
|
|
|
|
const found = fs.readFileSync(fn2, 'utf8');
|
|
|
|
fs.unlinkSync(fn2);
|
2018-10-12 17:03:05 +00:00
|
|
|
assert.strictEqual(found, expected);
|
2017-11-06 15:52:33 +00:00
|
|
|
});
|
|
|
|
|
2020-09-06 20:27:07 +00:00
|
|
|
const written = common.mustSucceed((written) => {
|
2018-10-12 17:03:05 +00:00
|
|
|
assert.strictEqual(written, 0);
|
2018-01-25 15:01:01 +00:00
|
|
|
fs.write(fd, expected, 0, 'utf8', done);
|
2017-11-06 15:52:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
fs.write(fd, '', 0, 'utf8', written);
|
|
|
|
}));
|
2017-11-15 15:23:32 +00:00
|
|
|
|
2020-09-06 20:27:07 +00:00
|
|
|
fs.open(fn3, 'w', 0o644, common.mustSucceed((fd) => {
|
|
|
|
const done = common.mustSucceed((written) => {
|
2018-10-12 17:03:05 +00:00
|
|
|
assert.strictEqual(written, Buffer.byteLength(expected));
|
2017-11-15 15:23:32 +00:00
|
|
|
fs.closeSync(fd);
|
|
|
|
});
|
|
|
|
|
|
|
|
fs.write(fd, expected, done);
|
|
|
|
}));
|
2017-12-13 22:24:34 +00:00
|
|
|
|
2022-04-08 10:16:08 +00:00
|
|
|
|
2017-12-13 22:24:34 +00:00
|
|
|
[false, 'test', {}, [], null, undefined].forEach((i) => {
|
2019-12-25 17:02:16 +00:00
|
|
|
assert.throws(
|
2017-12-13 22:24:34 +00:00
|
|
|
() => fs.write(i, common.mustNotCall()),
|
|
|
|
{
|
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-12-25 17:02:16 +00:00
|
|
|
name: 'TypeError'
|
2017-12-13 22:24:34 +00:00
|
|
|
}
|
|
|
|
);
|
2019-12-25 17:02:16 +00:00
|
|
|
assert.throws(
|
2017-12-13 22:24:34 +00:00
|
|
|
() => fs.writeSync(i),
|
|
|
|
{
|
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-12-25 17:02:16 +00:00
|
|
|
name: 'TypeError'
|
2017-12-13 22:24:34 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2019-12-19 18:00:45 +00:00
|
|
|
|
2022-04-04 10:57:59 +00:00
|
|
|
[
|
2022-09-12 09:29:43 +00:00
|
|
|
false, 5, {}, [], null, undefined, true, 5n, () => {}, Symbol(), new Map(),
|
2022-04-04 10:57:59 +00:00
|
|
|
new String('notPrimitive'),
|
|
|
|
{ [Symbol.toPrimitive]: (hint) => 'amObject' },
|
2022-09-12 09:29:43 +00:00
|
|
|
{ toString() { return 'amObject'; } },
|
|
|
|
Promise.resolve('amPromise'),
|
|
|
|
common.mustNotCall(),
|
2022-04-04 10:57:59 +00:00
|
|
|
].forEach((data) => {
|
2019-12-19 18:00:45 +00:00
|
|
|
assert.throws(
|
|
|
|
() => fs.write(1, data, common.mustNotCall()),
|
|
|
|
{
|
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
|
|
message: /"buffer"/
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert.throws(
|
|
|
|
() => fs.writeSync(1, data),
|
|
|
|
{
|
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
|
|
message: /"buffer"/
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2021-04-10 01:39:43 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// Regression test for https://github.com/nodejs/node/issues/38168
|
2022-09-12 09:29:43 +00:00
|
|
|
const fd = fs.openSync(fn4, 'w');
|
2021-04-10 01:39:43 +00:00
|
|
|
|
|
|
|
assert.throws(
|
|
|
|
() => fs.writeSync(fd, 'abc', 0, 'hex'),
|
|
|
|
{
|
|
|
|
code: 'ERR_INVALID_ARG_VALUE',
|
|
|
|
message: /'encoding' is invalid for data of length 3/
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.throws(
|
|
|
|
() => fs.writeSync(fd, 'abc', 0, 'hex', common.mustNotCall()),
|
|
|
|
{
|
|
|
|
code: 'ERR_INVALID_ARG_VALUE',
|
|
|
|
message: /'encoding' is invalid for data of length 3/
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.strictEqual(fs.writeSync(fd, 'abcd', 0, 'hex'), 2);
|
|
|
|
|
|
|
|
fs.write(fd, 'abcd', 0, 'hex', common.mustSucceed((written) => {
|
|
|
|
assert.strictEqual(written, 2);
|
|
|
|
fs.closeSync(fd);
|
|
|
|
}));
|
|
|
|
}
|