How to use the readSync function from fs

Find comprehensive JavaScript fs.readSync code examples handpicked from public code repositorys.

68
69
70
71
72
73
74
75
76
77
exports.mkdtemp = co.promisify(fs.mkdtemp);
exports.mkdtempSync = fs.mkdtempSync;
exports.open = co.promisify(fs.open);
exports.openSync = fs.openSync;
exports.read = co.promisify(fs.read);
exports.readSync = fs.readSync;
exports.readdir = co.promisify(fs.readdir);
exports.readdirSync = fs.readdirSync;
exports.readFile = co.promisify(fs.readFile);
exports.readFileSync = fs.readFileSync;
fork icon22
star icon45
watch icon26

738
739
740
741
742
743
744
745
746
747
748
  common.runWithInvalidFD((fd) => {
    const buf = Buffer.alloc(5);
    fs.read(fd, buf, 0, 1, 1, common.mustCall(validateError));


    assert.throws(
      () => fs.readSync(fd, buf, 0, 1, 1),
      validateError
    );
  });
}
fork icon42
star icon19
watch icon0

348
349
350
351
352
353
354
355
356
357
}
if (typeof file === 'number') {
    return zipFile(fs.fstatSync(file).size, function (offset, length) {
        var buf = new Buffer(length);
        for (var red = 0; red < length;) {
            red += fs.readSync(file, buf, red, length - red, offset + red);
        }
        return buf;
    }, false)._closeHook(function () {
        fs.closeSync(file);
fork icon6
star icon1
watch icon0

57
58
59
60
61
62
63
64
65
66
const st = fs.fstatSync(fd)
const headBuf = Buffer.alloc(512)

POSITION: for (position = 0; position < st.size; position += 512) {
  for (let bufPos = 0, bytes = 0; bufPos < 512; bufPos += bytes) {
    bytes = fs.readSync(
      fd, headBuf, bufPos, headBuf.length - bufPos, position + bufPos
    )

    if (position === 0 && headBuf[0] === 0x1f && headBuf[1] === 0x8b) {
fork icon0
star icon0
watch icon0

66
67
68
69
70
71
72
73
74
75
  require('cluster').isMaster) {
// The copyright notice is relatively big and the flags could come afterwards.
const bytesToRead = 1500;
const buffer = Buffer.allocUnsafe(bytesToRead);
const fd = fs.openSync(require.main.filename, 'r');
const bytesRead = fs.readSync(fd, buffer, 0, bytesToRead);
fs.closeSync(fd);
const source = buffer.toString('utf8', 0, bytesRead);

const flagStart = source.indexOf('// Flags: --') + 10;
fork icon0
star icon0
watch icon0

38
39
40
41
42
43
44
45
46
fs.ftruncateSync(fd, offset);
assert.strictEqual(fs.statSync(filepath).size, offset);
const writeBuf = Buffer.from(message);
fs.writeSync(fd, writeBuf, 0, writeBuf.length, offset);
const readBuf = Buffer.allocUnsafe(writeBuf.length);
fs.readSync(fd, readBuf, 0, readBuf.length, offset);
assert.strictEqual(readBuf.toString(), message);
fs.readSync(fd, readBuf, 0, 1, 0);
assert.strictEqual(readBuf[0], 0);
fork icon0
star icon0
watch icon0

+ 3 other calls in file