-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for Buffer objects in "fs" methods.
fs.writeFile(), fs.appendFile() and friends may accept an instance of Buffer as an argument. Also, fs.readFile() and friends now return an instance of Buffer instead of Byte-string when encoding is not provided. Added Buffer encoding for fs.readdir(), fs.realpath() and friends.
- Loading branch information
Showing
11 changed files
with
692 additions
and
525 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
var fs = require('fs'); | ||
var fname = './build/test/fs_appendFile'; | ||
|
||
var argv = process.argv.slice(2); | ||
|
||
var data = (() => { | ||
var value = argv[0]; | ||
var type = argv[1]; | ||
var offset = argv[2] ? parseInt(argv[2]) : 0; | ||
|
||
switch (type) { | ||
case 'Buffer': | ||
return Buffer.from(Buffer.from(value).buffer, offset); | ||
case 'DataView': | ||
return new DataView(Buffer.from(value).buffer, offset); | ||
case 'Object': | ||
return {toString(){return value}}; | ||
case 'String': | ||
return String(value); | ||
case 'Symbol': | ||
return Symbol(value); | ||
case 'Uint8Array': | ||
return new Uint8Array(Buffer.from(value).buffer, offset); | ||
default: | ||
throw new Error(`Unknown data type:${type}`); | ||
} | ||
})(); | ||
|
||
var options = (() => { | ||
var encoding = argv[2]; | ||
var mode = argv[3] ? parseInt(argv[3].slice(2), 8) : 0; | ||
|
||
if (encoding && mode) { | ||
return {encoding, mode}; | ||
|
||
} else if (encoding) { | ||
return encoding; | ||
} | ||
|
||
return undefined; | ||
})(); | ||
|
||
try { fs.unlinkSync(fname); } catch (e) {} | ||
|
||
function done(e) { | ||
if (e) {throw e}; | ||
var data = fs.readFileSync(fname); | ||
console.log(String(data)); | ||
} | ||
|
||
function append(cb) { | ||
if (options) { | ||
var path = Buffer.from(`@${fname}`).slice(1); | ||
fs.appendFile(path, data, options, cb); | ||
|
||
} else { | ||
fs.appendFile(fname, data, cb); | ||
} | ||
} | ||
|
||
append((e) => {if (e) {throw e}; append(done);}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
var fs = require('fs'); | ||
var fname = './build/test/fs_appendFileSync'; | ||
|
||
var argv = process.argv.slice(2); | ||
|
||
var data = (() => { | ||
var value = argv[0]; | ||
var type = argv[1]; | ||
var offset = argv[2] ? parseInt(argv[2]) : 0; | ||
|
||
switch (type) { | ||
case 'Buffer': | ||
return Buffer.from(Buffer.from(value).buffer, offset); | ||
case 'DataView': | ||
return new DataView(Buffer.from(value).buffer, offset); | ||
case 'Object': | ||
return {toString(){return value}}; | ||
case 'String': | ||
return String(value); | ||
case 'Symbol': | ||
return Symbol(value); | ||
case 'Uint8Array': | ||
return new Uint8Array(Buffer.from(value).buffer, offset); | ||
default: | ||
throw new Error(`Unknown data type:${type}`); | ||
} | ||
})(); | ||
|
||
var options = (() => { | ||
var encoding = argv[2]; | ||
var mode = argv[3] ? parseInt(argv[3].slice(2), 8) : 0; | ||
|
||
if (encoding && mode) { | ||
return {encoding, mode}; | ||
|
||
} else if (encoding) { | ||
return encoding; | ||
} | ||
|
||
return undefined; | ||
})(); | ||
|
||
function append() { | ||
if (options) { | ||
var path = Buffer.from(`@${fname}`).slice(1); | ||
fs.appendFileSync(path, data, options); | ||
|
||
} else { | ||
fs.appendFileSync(fname, data); | ||
} | ||
} | ||
|
||
try { fs.unlinkSync(fname); } catch (e) {} | ||
|
||
append(); | ||
append(); | ||
|
||
var ret = fs.readFileSync(fname); | ||
console.log(String(ret)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.