const promise = new TPromise<string>((c, e) => { const input = fs.createReadStream(path); const hash = crypto.createHash('sha1'); const hashStream = hash as any as stream.PassThrough; input.pipe(hashStream); const done = once((err?: Error, result?: string) => { input.removeAllListeners(); hashStream.removeAllListeners(); if (err) { e(err); } else { c(result); } }); input.once('error', done); input.once('end', done); hashStream.once('error', done); hashStream.once('data', (data: NodeBuffer) => done(null, data.toString('hex'))); });
server = http.createServer(async (request, response) => { if (request.url) { if (request.url === "/") { response.setHeader("Content-Type", "text/html"); response.end(await getRenderedSourceCode()); } else { try { let filePath: string = commandArgs.fsPath.replace(/\\/g, "/"); let lastSlashPosition = filePath.lastIndexOf('/'); let basePath = filePath.substr(0, lastSlashPosition); filePath = `${basePath}${request.url}`; let cb = fs.statSync(filePath).size; response.setHeader("Content-Type", `image/${request.url.substr(request.url.lastIndexOf('.'))}`); response.setHeader("Content-Length", cb); fs.createReadStream(filePath).pipe(response); } catch { //fail response.end(); } } } });
node.redirs.forEach(function (redir) { const direction = redir.direction.type.toString(); const target = redir.target; var fd = redir.direction.number; if (direction === 'LT') { if (!utils.isNumber(fd)) { fd = 0; } cmdStreams[fd] = fs.createReadStream(target.text); } else if (direction === 'GT') { if (!utils.isNumber(fd)) { fd = 1; } cmdStreams[fd] = fs.createWriteStream(target.text); // truncates } else if (direction === 'GTGT') { if (!utils.isNumber(fd)) { fd = 1; } cmdStreams[fd] = fs.createWriteStream(target.text, {flags: 'a'}); } else if (direction === 'LTGT') { if (!utils.isNumber(fd)) { fd = 0; } cmdStreams[fd] = fs.createWriteStream(target.text, {flags: 'a'}); } else if (direction === 'GTAMP') { // TODO THIS DOESN'T REALLY WORK RIGHT NOW, FD VALUE IS PASSED DIRECTLY TO SPAWN SO IT REFERS TO PARENT'S FD. SAME GOES FOR LTAMP if (!utils.isNumber(fd)) { fd = 1; } cmdStreams[fd] = cmdStreams[Number(target.text)]; } else if (direction === 'LTAMP') { if (!utils.isNumber(fd)) { fd = 0; } cmdStreams[fd] = cmdStreams[Number(target.text)]; } // TODO MUST DETECT DUPLICATE FDs AND OPEN DUPLEX STREAM FOR THEM (e.g., "somecommand 7>thefile 7<thefile") });
(request, callback) => { const urlPath = url.parse(request.url).pathname; const decodedPath = decodeURI(urlPath); const rootlessPath = decodedPath.replace(/^\//, ""); const filePath = join(fileRoot, rootlessPath); try { var stats = statSync(filePath); var stream = createReadStream(filePath); callback({ headers: { server: "itch", "content-type": mime.lookup(filePath), "content-length": stats.size, "access-control-allow-origin": "*", }, statusCode: 200, data: stream as any, // *sigh* }); } catch (e) { logger.warn(`while serving ${request.url}, got ${e.stack}`); let statusCode = 400; switch (e.code) { case "ENOENT": statusCode = 404; break; case "EPERM": statusCode = 401; break; } callback({ headers: {}, statusCode, data: null, }); return; } },
function readExistingTweets(outputFilepath: string, callback: (error: Error, fetchedStatuses?: {[index: string]: number}) => void) { let fetchedStatuses: {[index: string]: number} = {}; // quick exit if output is undefined if (outputFilepath === undefined) return callback(null, fetchedStatuses); logger.info(`Reading existing tweets from "${outputFilepath}"`); let inputStream = createReadStream(outputFilepath, {flags: 'r', encoding: 'utf8'}); inputStream.pipe(new JSONParser()) .on('data', (status: Status) => { fetchedStatuses[status.id_str] = 1; }) .on('error', (error: Error) => { callback(error); }) .on('end', () => { logger.info(`Found ${Object.keys(fetchedStatuses).length} tweets in "${outputFilepath}"`); callback(null, fetchedStatuses); }); }
export function copyFile(source, target, cb) { var cbCalled = false; var rd = fs.createReadStream(source); rd.on('error', function(err) { done(err); }); var wr = fs.createWriteStream(target); wr.on('error', function(err) { done(err); }); wr.on('close', function(ex) { done(); }); rd.pipe(wr); function done(err?) { if (!cbCalled) { cb(err); cbCalled = true; } } }
export function streamFile(filename: string, res: ServerResponse) { if (!path.isAbsolute(filename)) { throw new Error(`stream file expects an absolute path found ${filename}`); } let mimeType = mime.lookup(filename); // res.writeHead(200, { "Content-Type": mimeType}); let readstream = fs.createReadStream(filename); readstream.on("error", err => { if ((err.code = "ENOENT")) { // file not found res.statusCode = 404; res.end("Not Found"); } else { console.error(err); res.statusCode = 500; res.end("Unknown Server Error"); } }); res.statusCode = 200; res.setHeader("Content-Type", mimeType); readstream.pipe(res); }
return new Promise(resolve => { const icons: Icon[] = []; const codepointsPath = require.resolve( 'material-design-icons/iconfont/codepoints' ); const rl = createInterface({ input: createReadStream(codepointsPath, 'utf8') }); rl.on('line', (line: string) => { const [id, unicode] = line.split(' '); icons.push({ id, unicode }); }); rl.on('close', () => { resolve({ icons }); }); });
function copyFile(source: string | Buffer, target: string | Buffer, cb: (err?: string) => void) { let cbCalled = false; let rd = fs.createReadStream(source); rd.on("error", function (err) { done(err); }); let wr = fs.createWriteStream(target); wr.on("error", function (err) { done(err); }); wr.on("close", function (ex) { done(); }); rd.pipe(wr); function done(err?) { if (!cbCalled) { cb(err); cbCalled = true; } } }
it("should include column", function (done) { var rs = fs.createReadStream(__dirname + "/data/dataWithQoutes"); csv({ includeColumns: /TIMESTAMP/ }) .fromStream(rs) .on("header", function (header) { assert.equal(header.indexOf("TIMESTAMP"), 0); assert.equal(header.indexOf("UPDATE"), -1); assert.equal(header.length, 1); }) .subscribe(function (j, idx) { assert(idx >= 0); if (idx === 1) { assert.equal(j.TIMESTAMP, "abc, def, ccc"); } assert(!j.UID) assert(!j['BYTES SENT']) }) .on("done", function () { done(); }); });