Best JavaScript code snippet using devicefarmer-stf
index.ts
Source:index.ts
1import * as fs from 'fs';2import * as path from 'path';3import { Server, Middlewares, Router, RouterMapFactory, CommonError } from '../../packages/auf';4const Config = {5 chunkSize: 5 * 1024 * 1024,6 publicDir: path.resolve(__dirname, '../public')7};8const serverTag = '[auf]';9const errorTag = 'Error:';10const port = 60000;11const timeout = 5000;12const callback = () => {13 console.log(serverTag, 'Server Listening on', port);14}15const errorHandler = (e: Error) => {16 console.error(serverTag, errorTag, e.message, '\n', e.stack);17}18const routerMap = RouterMapFactory(/* base= */'/api');19const fsredirPromise = (path: string) => new Promise<string[]>((resolve, reject) => {20 fs.readdir(path, (err, files) => {21 if (err) {22 reject(new CommonError({23 message: err.message,24 statusCode: 500,25 statusMessage: 'File not exists'26 }));27 }28 resolve(files);29 });30});31const server = new Server({32 port,33 assetsRoot: Config.publicDir34});35routerMap.get('/test-multi', async (ctx, next) => {36 ctx.body = 'hi~';37 next && await next(ctx);38 next && await next(ctx);39});40routerMap.get('/upload/config', async (ctx, next) => {41 ctx.body = JSON.stringify(Object.assign({}, { chunkSize: Config.chunkSize }))42 // @ts-ignore43 next && await next(ctx);44});45routerMap.head('/upload/config', async (ctx, next) => {46 ctx.body = JSON.stringify(Object.assign({}, { chunkSize: Config.chunkSize }));47 next && await next(ctx);48});49routerMap.get('/upload/chunkIndex', async (ctx, next) => {50 const res = await fsredirPromise(Config.publicDir);51 // const res = [];52 const hash = ctx.query.hash;53 const fileWithHash = res.filter(file => file.indexOf(hash) === 0)54 ctx.body = JSON.stringify({55 success: true,56 message: '',57 data: fileWithHash.length58 });59 next && await next(ctx);60});61routerMap.post('/upload', async (ctx, next) => {62 const body = ctx.reqBody;63 const { files, index, filename: _filename, chunkCount, fileHash } = body;64 const _file = files[0];65 const { file } = _file;66 const filename = decodeURIComponent(_filename);67 const stream = fs.createWriteStream(path.resolve(Config.publicDir, `${fileHash}-${index}`));68 stream.write(file, 'binary');69 ctx.body = JSON.stringify({ msg: 'success', success: true, data: (Number(index) + 1) / Number(chunkCount) });70 stream.end();71 stream.on('finish', async () => {72 console.log('Writing Finished')73 if (Number(chunkCount) === Number(index) + 1) {74 const mergeStream = fs.createWriteStream(path.resolve(Config.publicDir, filename));75 // const chunkArr = Array.from({ length: chunkCount }).map((_, index) => index);76 77 let promise = new Promise<number>((resolve) => {78 const fd = path.resolve(Config.publicDir, `${fileHash}-${0}`)79 const readData = fs.readFileSync(fd)80 81 const writable = mergeStream.write(readData, 'binary');82 83 if (!writable) {84 console.log('Waiting Drain Event For', index);85 const drainListener = () => {86 resolve(1);87 fs.existsSync(fd) && fs.unlink(fd, err => err && console.error(err));88 mergeStream.removeListener('drain', drainListener);89 }90 mergeStream.on('drain', drainListener)91 }92 })93 94 let q = 0;95 while (q < chunkCount - 1) {96 promise = promise.then(index => {97 return new Promise<number>(resolve => {98 const fd = path.resolve(Config.publicDir, `${fileHash}-${index}`)99 const readData = fs.readFileSync(fd)100 101 const writable = mergeStream.write(readData, 'binary');102 103 if (!writable) {104 console.log('Waiting Drain Event For', index);105 const drainListener = () => {106 resolve(index + 1);107 mergeStream.removeListener('drain', drainListener);108 }109 mergeStream.on('drain', drainListener)110 } else {111 resolve(index + 1)112 }113 fs.unlink(fd, err => err && console.error(err))114 })115 })116 q ++;117 }118 119 }120 });121 next && await next(ctx);122});123const CORS = function () {124 return async function (ctx, next) {125 await next(ctx)126 const { res } = ctx127 // console.log(res)128 res.setHeader('Access-Control-Allow-Origin', '*')129 }130}131server.applyMiddleware([132 CORS(),133 Middlewares.ErrorBoundary({134 errorHandler,135 renderError: true136 }),137 Middlewares.Timeout({ timeout }),138 Middlewares.Logger(console),139 Middlewares.StaticRoutes({140 fileSystem: fs141 }),142 Middlewares.BodyParser(),143 Router()144])145try {146 server.setup(callback);147} catch (e) {148 console.error(e.message);...
manager.js
Source:manager.js
...23 }24 function readableListener() {25 maybePipeManually()26 }27 function drainListener() {28 maybePipeManually()29 }30 function endListener() {31 conn.removeListener('drain', drainListener)32 writer.removeListener('readable', readableListener)33 this.emit('end')34 }35 function errorListener() {36 writer.end()37 }38 writer.on('end', endListener.bind(this))39 writer.on('readable', readableListener)40 dest.on('error', errorListener)41 conn.on('drain', drainListener)...
drainwaiting_buffered_writer.ts
Source:drainwaiting_buffered_writer.ts
1export class DrainwaitingBufferedWriter {2 private buf = ''3 private bufSize = 1024 ** 24 private drainPromise = Promise.resolve()5 private drainListener: () => void6 constructor(private stream: NodeJS.WritableStream) {7 if ('writableHighWaterMark' in stream) {8 this.bufSize = stream['writableHighWaterMark'] // / 2 - 19 }10 }11 get drained() {12 return this.drainPromise13 }14 write(what: string) {15 this.buf += what16 if (this.buf.length > this.bufSize) {17 // todo: buffer?18 return this.flush()19 }20 return true21 }22 writeLn(what: string) {23 let ok1 = this.write(what)24 let ok2 = this.write('\n')25 return ok1 && ok226 }27 flush() {28 if (this.buf) {29 let ret = this.stream.write(this.buf)30 this.buf = ''31 if (!ret) {32 if (this.drainListener) {33 this.stream.off('drain', this.drainListener)34 }35 this.drainPromise = new Promise((resolve, reject) => {36 this.drainListener = () => {37 this.drainListener = undefined38 resolve()39 }40 this.stream.once('drain', this.drainListener)41 })42 }43 }44 return !this.drainListener45 }...
Using AI Code Generation
1const DeviceFarmer = require('devicefarmer-stf-client');2const deviceFarmer = new DeviceFarmer();3deviceFarmer.drainListener().then((drain) => {4 console.log('Drain status: ', drain);5}).catch((err) => {6 console.log('Error: ', err);7});8const DeviceFarmer = require('devicefarmer-stf-client');9const deviceFarmer = new DeviceFarmer();10deviceFarmer.drainListener().then((drain) => {11 console.log('Drain status: ', drain);12}).catch((err) => {13 console.log('Error: ', err);14});15const DeviceFarmer = require('devicefarmer-stf-client');16const deviceFarmer = new DeviceFarmer();17deviceFarmer.drainListener().then((drain) => {18 console.log('Drain status: ', drain);19}).catch((err) => {20 console.log('Error: ', err);21});22const DeviceFarmer = require('devicefarmer-stf-client');23const deviceFarmer = new DeviceFarmer();24deviceFarmer.drainListener().then((drain) => {25 console.log('Drain status: ', drain);26}).catch((err) => {27 console.log('Error: ', err);28});29const DeviceFarmer = require('devicefarmer-stf-client');30const deviceFarmer = new DeviceFarmer();31deviceFarmer.drainListener().then((drain) => {32 console.log('Drain status: ', drain);33}).catch((err) => {34 console.log('Error: ', err);35});36const DeviceFarmer = require('devicefarmer-stf-client');37const deviceFarmer = new DeviceFarmer();38deviceFarmer.drainListener().then((drain) => {39 console.log('Drain status: ', drain);40}).catch((err) => {
Using AI Code Generation
1var devicefarmer = require('devicefarmer-stf');2device.drainListener('test', function(data) {3 console.log(data);4});5var devicefarmer = require('devicefarmer-stf');6device.drainListener('test', function(data) {7 console.log(data);8});9var devicefarmer = require('devicefarmer-stf');10device.drainListener('test', function(data) {11 console.log(data);12});13var devicefarmer = require('devicefarmer-stf');14device.drainListener('test', function(data) {15 console.log(data);16});17var devicefarmer = require('devicefarmer-stf');18device.drainListener('test', function(data) {19 console.log(data);20});21var devicefarmer = require('devicefarmer-stf');22device.drainListener('test', function(data) {23 console.log(data);24});25var devicefarmer = require('devicefarmer-stf');26device.drainListener('test', function(data) {27 console.log(data);28});29var devicefarmer = require('devicefarmer-stf');30device.drainListener('test',
Using AI Code Generation
1var device = require("devicefarmer-stf-device");2device.drainListener(function(){3 console.log("Drain Listener");4});5var device = require("devicefarmer-stf-device");6device.killServer(function(){7 console.log("Kill Server");8});9var device = require("devicefarmer-stf-device");10device.listDevices(function(){11 console.log("List Devices");12});13var device = require("devicefarmer-stf-device");14device.listDevices(function(){15 console.log("List Devices");16});17var device = require("devicefarmer-stf-device");18device.listDevices(function(){19 console.log("List Devices");20});21var device = require("devicefarmer-stf-device");22device.listDevices(function(){23 console.log("List Devices");24});25var device = require("devicefarmer-stf-device");26device.listDevices(function(){27 console.log("List Devices");28});
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!