Best JavaScript code snippet using wpt
admin.js
Source:admin.js
1jQuery( document ).ready( function( $ ) {2 var msg_ara = $("#fd_form_msg");3 msg_ara.hide();4 var form = $( '#deploy-form');5 function send_deploy( zip_file ){6 $.ajax({7 type: 'POST',8 data: {9 action: 'fd_ajax_deploy',10 zip_file: zip_file,11 _nonce: FD_Config.nonce12 },13 url: ajaxurl,14 cache: false,15 error: function ( xhr, ajaxOptions, thrownError ) {16 $( 'input[type="file"]', form).removeAttr( 'disabled');17 form.removeClass( 'loading' );18 msg_ara.hide();19 },20 beforeSend: function () {21 form.addClass( 'loading' );22 msg_ara.show();23 // msg_ara.html( res.data );24 msg_ara.html( FD_Config.deploying );25 },26 complete: function () {27 $( 'input[type="file"]', form ).removeAttr( 'disabled');28 form.removeClass( 'loading' );29 },30 success: function ( res ) {31 form.removeClass( 'loading' );32 if ( res.success ) {33 msg_ara.show();34 msg_ara.html( res.data );35 } else {36 msg_ara.show();37 $( 'input[type="file"]', form ).removeAttr( 'disabled');38 msg_ara.html( res.data );39 }40 }41 });42 }43 form.submit( function( e ){44 e.preventDefault();45 if ( form.hasClass( 'loading' ) ) {46 return true;47 }48 var formData = new FormData(form[0]);49 $.ajax({50 type: 'POST',51 data: formData,52 async: false,53 url: ajaxurl,54 cache: false,55 contentType: false,56 processData: false,57 error: function ( xhr, ajaxOptions, thrownError ) {58 $( '.reset_button', form ).trigger( 'click' );59 form.removeClass( 'loading' );60 msg_ara.hide();61 },62 beforeSend: function () {63 form.addClass( 'loading' );64 $( 'input[type="file"]', form ).attr( 'disabled', 'disabled' );65 },66 complete: function () {67 $( 'input[type="file"]', form ).removeAttr( 'disabled');68 form.removeClass( 'loading' );69 },70 success: function ( res ) {71 form.removeClass( 'loading' );72 $( '.reset_button', form ).trigger( 'click' );73 if ( res.success ) {74 send_deploy( res.data );75 } else {76 msg_ara.show();77 msg_ara.html( res.data );78 $( 'input[type="file"]', form ).removeAttr( 'disabled');79 }80 }81 });82 return false;83 } );84 //--------------------------------------------------------85 // Upload handle86 window._fd_media_current = {};87 if ( ! window._upload_fame ) {88 window._upload_fame = wp.media({89 title: wp.media.view.l10n.addMedia,90 multiple: false,91 //frame: 'post',92 //library: {type: 'all' },93 //button : { text : 'Insert' }94 });95 }96 window._upload_fame.on('close', function () {97 // get selections and save to hidden input plus other AJAX stuff etc.98 // var selection = window._upload_fame.state().get('selection');99 // console.log(selection);100 });101 window._upload_fame.on('select', function () {102 // Grab our attachment selection and construct a JSON representation of the model.103 var attachment = window._upload_fame.state().get('selection').first().toJSON();104 //$('.image_id', window.media_current).val(media_attachment.id);105 if ( 'application/zip' !== attachment.mime ) {106 return false;107 }108 if ( window._fd_media_current ) {109 window._fd_media_current.metabox.fdDeploy.updateRowData( window._fd_media_current.key, {110 type: 'upload',111 attachment_id: attachment.id112 } );113 }114 });115 $.ajax({116 type: 'GET',117 data: {118 action: 'fd_ajax_github',119 doing: 'get_repos',120 _nonce: FD_Config.nonce121 },122 url: ajaxurl,123 //cache: false,124 error: function (xhr, ajaxOptions, thrownError) {125 },126 complete: function () {127 },128 success: function (res) {129 console.log( 'Github loaded', res );130 $( 'select.github_repo').html(res.data);131 $( 'select.github_repo').trigger( 'option_added' );132 }133 });134 $( '.fd-meta-box').each( function(){135 var metabox = $( this );136 metabox.fdDeploy = {};137 metabox.fdDeploy.input = $( '.fd_versions', metabox );138 var value = metabox.fdDeploy.input.val() || '{}';139 try {140 metabox.fdDeploy.versions = JSON.parse(value);141 } catch ( e ){ }142 if( typeof metabox.fdDeploy.versions !== 'object' ) {143 metabox.fdDeploy.versions = {};144 }145 //console.log( 'metabox.fdDeploy.versions', metabox.fdDeploy.versions );146 metabox.fdDeploy.versionTable = $( this).parent().find( 'table.fd-versions-table' );147 metabox.fdDeploy.updateVersionData = function( save ){148 // Delete all duplicate versions149 var versions = {};150 $.each( metabox.fdDeploy.versions, function( index, value ) {151 if ( typeof versions[ value.version ] === "undefined" ) {152 versions[ value.version ] = true;153 } else {154 var rows = metabox.fdDeploy.versionTable.find( 'tr[data-ver="'+value.version+'"]' );155 if ( rows.length > 1 ) {156 $.each( rows, function(_index){157 if ( _index > 0 ) {158 $( this ).remove();159 }160 } );161 }162 delete metabox.fdDeploy.versions[ index ];163 }164 } );165 metabox.fdDeploy.input.val( JSON.stringify( metabox.fdDeploy.versions ) );166 if ( typeof save === "undefined" || save === true ) {167 metabox.fdDeploy.save();168 }169 };170 metabox.fdDeploy.save = function(){171 $.ajax({172 type: 'POST',173 data: {174 action: 'fd_ajax_deployment',175 post_id: $( '#post_ID').val(),176 _nonce: FD_Config.nonce,177 versions: metabox.fdDeploy.versions178 },179 url: ajaxurl,180 dataType: 'json',181 error: function ( xhr, ajaxOptions, thrownError) {182 },183 complete: function () {184 },185 success: function ( res ) {186 console.log( 'save_versions success', res );187 }188 });189 };190 metabox.fdDeploy.ajaxDeploy = function( type, data, rowKey ){191 type = type || 'free';192 var row = metabox.fdDeploy.versionTable.find( 'tbody tr[row-id="'+rowKey+'"]');193 $( '.fd-update-button', row ).addClass( 'spin' );194 console.log( 'ajaxDeploy data ' + type + ': ', data );195 $.ajax({196 type: 'POST',197 data: {198 action: 'fd_ajax_deploy',199 data: data,200 type: type,201 _nonce: FD_Config.nonce202 },203 url: ajaxurl,204 dataType: 'json',205 error: function ( xhr, ajaxOptions, thrownError) {206 },207 complete: function () {208 },209 success: function ( res ) {210 console.log( 'ajaxDeploy respond', res );211 if ( res.success ) {212 metabox.fdDeploy.updateRowData( rowKey, res.data );213 if ( type != 'premium') {214 metabox.fdDeploy.ajaxDeploy( 'premium', data, rowKey );215 } else {216 $( '.fd-update-button', row ).removeClass( 'spin' );217 metabox.fdDeploy.updateVersionData( true );218 }219 }220 }221 });222 };223 metabox.fdDeploy.updateRowData = function( key, data, save ){224 var currentData = metabox.fdDeploy.versions[ key ] || {};225 var args = _.defaults( data || {}, currentData );226 args = _.defaults( args, {227 free_name: '',228 free_url: '',229 premium_name: '',230 premium_url: '',231 version: '',232 current: '',233 type: '', // upload or github234 attachment_id: '', // Attachment Post ID235 repo_name: '', // Github repo full name236 repo_version: '', // Github version tag237 });238 var row;239 row = metabox.fdDeploy.versionTable.find( 'tbody tr[row-id="'+key+'"]');240 if ( ! row.length ) {241 row = metabox.fdDeploy.versionTable.find( 'tbody tr[data-ver="'+args.version+'"]');242 }243 if ( row.length ) {244 if ( args.free_name && args.free_url ) {245 $( '.free-version', row ).html( '<a target="_blank" href="' + FD_Config.deploy_url + args.free_url+ '">'+ args.free_name + '</a>' );246 } else if ( args.free_name ) {247 $( '.free-version', row ).html( args.free_name );248 }249 if ( args.premium_name && args.premium_url ) {250 $( '.premium-version', row ).html( '<a target="_blank" href="' + FD_Config.deploy_url + args.premium_url+'">'+ args.premium_name + '</a>' );251 } else if ( args.premium_name ) {252 $( '.premium-version', row ).html( args.premium_name );253 }254 row.attr( 'row-id', key );255 if ( args.current ) {256 row.addClass( 'current' );257 } else {258 row.removeClass( 'current' );259 }260 if ( args.version ) {261 $( '.version-number', row ).html( args.version );262 row.attr( 'data-ver', args.version );263 }264 if (args.type == 'github' ) {265 $( '.file-select-button', row ).hide();266 $( '.fd-github-icon', row ).show();267 } else {268 $( '.file-select-button', row ).show();269 $( '.fd-github-icon', row ).hide();270 }271 if ( args.attachment_id > 0 || args.repo_name != '' ) {272 $( '.fd-update-button', row ).show();273 } else {274 $( '.fd-update-button', row ).hide();275 }276 }277 metabox.fdDeploy.versions[ key ] = args;278 metabox.fdDeploy.updateVersionData( save );279 };280 metabox.fdDeploy.addRowToTable = function( data, key, save ){281 console.log( 'Add row to table: ', data );282 var tplR = metabox.fdDeploy.versionTable.find( 'tbody tr.tpl').eq( 0 );283 var row_c = tplR.clone();284 row_c.removeClass( 'tpl' );285 metabox.fdDeploy.versionTable.find( 'tbody').prepend( row_c );286 if ( typeof key === "undefined" || ! key ) {287 key = new Date().getTime();288 key = 'k'+key;289 }290 row_c.data( 'key', key );291 row_c.attr( 'row-id', key );292 if ( data.version ) {293 row_c.attr( 'data-ver', data.version.replace( 'v', '' ) );294 }295 metabox.fdDeploy.updateRowData( key, data, save );296 return key;297 };298 // Add exists item to table299 $.each( metabox.fdDeploy.versions, function( key, data ){300 metabox.fdDeploy.addRowToTable( data, key, false );301 } );302 $( '.github-repos', metabox ).each( function(){303 var item = $( this );304 var repoSelect = $( 'select.github_repo', item);305 var versionSelect = $( 'select.github_version', item);306 var button = $( '.fetch-repo', item );307 repoSelect.on( 'change', function( e ){308 var repoName = $( this).val();309 if ( repoName ) {310 $.ajax({311 type: 'GET',312 data: {313 action: 'fd_ajax_github',314 repo: repoName,315 doing: 'get_tags',316 _nonce: FD_Config.nonce317 },318 url: ajaxurl,319 //cache: false,320 error: function (xhr, ajaxOptions, thrownError) {321 },322 complete: function () {323 },324 success: function (res) {325 versionSelect.html(res.data);326 }327 });328 } else {329 versionSelect.html( '<option value=""> --- </option>' );330 }331 } );332 repoSelect.on( 'option_added', function(){333 var current = repoSelect.attr( 'data-value' ) || '';334 console.log( 'Github option_added', current );335 if ( current ) {336 $( 'option[value="'+current+'"]').attr( 'selected', 'selected' );337 repoSelect.trigger( 'change' );338 }339 } );340 repoSelect.trigger( 'change' );341 //repoSelect.trigger( 'change' );342 button.on( 'click', function( e ) {343 e.preventDefault();344 var repoName = repoSelect.val();345 var version = versionSelect.val();346 var post_id = $( '#post_ID').val() || '';347 if ( repoName && version ) {348 var key = metabox.fdDeploy.addRowToTable( {349 free_name: FD_Config.deploying,350 premium_name: FD_Config.deploying,351 version: version,352 type: 'github',353 repo_name: repoName,354 repo_version: version,355 } );356 button.addClass( 'updating-message' );357 $.ajax({358 type: 'GET',359 data: {360 action: 'fd_ajax_github',361 repo: repoName,362 version: version,363 doing: 'fetch',364 post_id: post_id,365 _nonce: FD_Config.nonce366 },367 url: ajaxurl,368 //cache: false,369 error: function (xhr, ajaxOptions, thrownError) {370 },371 complete: function () {372 button.removeClass( 'updating-message' );373 },374 success: function (res) {375 console.log( 'fd_ajax_github ' + repoName + ': ', res );376 metabox.fdDeploy.ajaxDeploy( 'free', metabox.fdDeploy.versions[ key ] || {}, key );377 }378 });379 }380 } );381 } );382 //--------------------------------------------------------383 // Add new upload row384 $( '.fd-new-upload-item', metabox ).on( 'click', function( e ){385 e.preventDefault();386 metabox.fdDeploy.addRowToTable();387 } );388 // When remove a version389 metabox.on( 'click', '.fd-versions-table tr td.remove a', function( e ){390 e.preventDefault();391 var tr = $( this ).closest( 'tr' );392 var key = tr.data( 'key' );393 var ver = tr.find( '.version-number').text();394 var c;395 if ( metabox.fdDeploy.versions[ key].version == '' ) {396 c = true;397 } else {398 c = confirm( 'Confirm remove version '+ver+' ?' );399 }400 if ( c ) {401 delete metabox.fdDeploy.versions[ key ];402 metabox.fdDeploy.updateVersionData( true );403 $( this ).closest( 'tr' ).remove();404 }405 } );406 // Upload media407 metabox.on( 'click', 'tr .file-select-button', function( e ) {408 e.preventDefault();409 window._upload_fame.open();410 var tr = $( this).closest( 'tr' );411 var key = tr.data( 'key' );412 window._fd_media_current = {413 row: tr,414 button: $( this ),415 metabox: metabox,416 data: metabox.fdDeploy.versions[ key ] || {},417 key: key418 }419 } );420 // Upload button421 metabox.on( 'click', 'tr .fd-update-button', function( e ){422 e.preventDefault();423 var tr = $( this).closest( 'tr' );424 $( '.free-version, .premium-version', tr ).html( FD_Config.deploying );425 $( '.fd-update-button', tr ).addClass( 'spin' );426 var key = tr.data( 'key' );427 metabox.fdDeploy.ajaxDeploy( 'free', metabox.fdDeploy.versions[ key ] || {}, key );428 } );429 } );...
index.js
Source:index.js
1'use strict'2const MiniPass = require('minipass')3const EE = require('events').EventEmitter4const fs = require('fs')5let writev = fs.writev6/* istanbul ignore next */7if (!writev) {8 // This entire block can be removed if support for earlier than Node.js9 // 12.9.0 is not needed.10 const binding = process.binding('fs')11 const FSReqWrap = binding.FSReqWrap || binding.FSReqCallback12 writev = (fd, iovec, pos, cb) => {13 const done = (er, bw) => cb(er, bw, iovec)14 const req = new FSReqWrap()15 req.oncomplete = done16 binding.writeBuffers(fd, iovec, pos, req)17 }18}19const _autoClose = Symbol('_autoClose')20const _close = Symbol('_close')21const _ended = Symbol('_ended')22const _fd = Symbol('_fd')23const _finished = Symbol('_finished')24const _flags = Symbol('_flags')25const _flush = Symbol('_flush')26const _handleChunk = Symbol('_handleChunk')27const _makeBuf = Symbol('_makeBuf')28const _mode = Symbol('_mode')29const _needDrain = Symbol('_needDrain')30const _onerror = Symbol('_onerror')31const _onopen = Symbol('_onopen')32const _onread = Symbol('_onread')33const _onwrite = Symbol('_onwrite')34const _open = Symbol('_open')35const _path = Symbol('_path')36const _pos = Symbol('_pos')37const _queue = Symbol('_queue')38const _read = Symbol('_read')39const _readSize = Symbol('_readSize')40const _reading = Symbol('_reading')41const _remain = Symbol('_remain')42const _size = Symbol('_size')43const _write = Symbol('_write')44const _writing = Symbol('_writing')45const _defaultFlag = Symbol('_defaultFlag')46const _errored = Symbol('_errored')47class ReadStream extends MiniPass {48 constructor (path, opt) {49 opt = opt || {}50 super(opt)51 this.readable = true52 this.writable = false53 if (typeof path !== 'string')54 throw new TypeError('path must be a string')55 this[_errored] = false56 this[_fd] = typeof opt.fd === 'number' ? opt.fd : null57 this[_path] = path58 this[_readSize] = opt.readSize || 16*1024*102459 this[_reading] = false60 this[_size] = typeof opt.size === 'number' ? opt.size : Infinity61 this[_remain] = this[_size]62 this[_autoClose] = typeof opt.autoClose === 'boolean' ?63 opt.autoClose : true64 if (typeof this[_fd] === 'number')65 this[_read]()66 else67 this[_open]()68 }69 get fd () { return this[_fd] }70 get path () { return this[_path] }71 write () {72 throw new TypeError('this is a readable stream')73 }74 end () {75 throw new TypeError('this is a readable stream')76 }77 [_open] () {78 fs.open(this[_path], 'r', (er, fd) => this[_onopen](er, fd))79 }80 [_onopen] (er, fd) {81 if (er)82 this[_onerror](er)83 else {84 this[_fd] = fd85 this.emit('open', fd)86 this[_read]()87 }88 }89 [_makeBuf] () {90 return Buffer.allocUnsafe(Math.min(this[_readSize], this[_remain]))91 }92 [_read] () {93 if (!this[_reading]) {94 this[_reading] = true95 const buf = this[_makeBuf]()96 /* istanbul ignore if */97 if (buf.length === 0)98 return process.nextTick(() => this[_onread](null, 0, buf))99 fs.read(this[_fd], buf, 0, buf.length, null, (er, br, buf) =>100 this[_onread](er, br, buf))101 }102 }103 [_onread] (er, br, buf) {104 this[_reading] = false105 if (er)106 this[_onerror](er)107 else if (this[_handleChunk](br, buf))108 this[_read]()109 }110 [_close] () {111 if (this[_autoClose] && typeof this[_fd] === 'number') {112 const fd = this[_fd]113 this[_fd] = null114 fs.close(fd, er => er ? this.emit('error', er) : this.emit('close'))115 }116 }117 [_onerror] (er) {118 this[_reading] = true119 this[_close]()120 this.emit('error', er)121 }122 [_handleChunk] (br, buf) {123 let ret = false124 // no effect if infinite125 this[_remain] -= br126 if (br > 0)127 ret = super.write(br < buf.length ? buf.slice(0, br) : buf)128 if (br === 0 || this[_remain] <= 0) {129 ret = false130 this[_close]()131 super.end()132 }133 return ret134 }135 emit (ev, data) {136 switch (ev) {137 case 'prefinish':138 case 'finish':139 break140 case 'drain':141 if (typeof this[_fd] === 'number')142 this[_read]()143 break144 case 'error':145 if (this[_errored])146 return147 this[_errored] = true148 return super.emit(ev, data)149 default:150 return super.emit(ev, data)151 }152 }153}154class ReadStreamSync extends ReadStream {155 [_open] () {156 let threw = true157 try {158 this[_onopen](null, fs.openSync(this[_path], 'r'))159 threw = false160 } finally {161 if (threw)162 this[_close]()163 }164 }165 [_read] () {166 let threw = true167 try {168 if (!this[_reading]) {169 this[_reading] = true170 do {171 const buf = this[_makeBuf]()172 /* istanbul ignore next */173 const br = buf.length === 0 ? 0174 : fs.readSync(this[_fd], buf, 0, buf.length, null)175 if (!this[_handleChunk](br, buf))176 break177 } while (true)178 this[_reading] = false179 }180 threw = false181 } finally {182 if (threw)183 this[_close]()184 }185 }186 [_close] () {187 if (this[_autoClose] && typeof this[_fd] === 'number') {188 const fd = this[_fd]189 this[_fd] = null190 fs.closeSync(fd)191 this.emit('close')192 }193 }194}195class WriteStream extends EE {196 constructor (path, opt) {197 opt = opt || {}198 super(opt)199 this.readable = false200 this.writable = true201 this[_errored] = false202 this[_writing] = false203 this[_ended] = false204 this[_needDrain] = false205 this[_queue] = []206 this[_path] = path207 this[_fd] = typeof opt.fd === 'number' ? opt.fd : null208 this[_mode] = opt.mode === undefined ? 0o666 : opt.mode209 this[_pos] = typeof opt.start === 'number' ? opt.start : null210 this[_autoClose] = typeof opt.autoClose === 'boolean' ?211 opt.autoClose : true212 // truncating makes no sense when writing into the middle213 const defaultFlag = this[_pos] !== null ? 'r+' : 'w'214 this[_defaultFlag] = opt.flags === undefined215 this[_flags] = this[_defaultFlag] ? defaultFlag : opt.flags216 if (this[_fd] === null)217 this[_open]()218 }219 emit (ev, data) {220 if (ev === 'error') {221 if (this[_errored])222 return223 this[_errored] = true224 }225 return super.emit(ev, data)226 }227 get fd () { return this[_fd] }228 get path () { return this[_path] }229 [_onerror] (er) {230 this[_close]()231 this[_writing] = true232 this.emit('error', er)233 }234 [_open] () {235 fs.open(this[_path], this[_flags], this[_mode],236 (er, fd) => this[_onopen](er, fd))237 }238 [_onopen] (er, fd) {239 if (this[_defaultFlag] &&240 this[_flags] === 'r+' &&241 er && er.code === 'ENOENT') {242 this[_flags] = 'w'243 this[_open]()244 } else if (er)245 this[_onerror](er)246 else {247 this[_fd] = fd248 this.emit('open', fd)249 this[_flush]()250 }251 }252 end (buf, enc) {253 if (buf)254 this.write(buf, enc)255 this[_ended] = true256 // synthetic after-write logic, where drain/finish live257 if (!this[_writing] && !this[_queue].length &&258 typeof this[_fd] === 'number')259 this[_onwrite](null, 0)260 return this261 }262 write (buf, enc) {263 if (typeof buf === 'string')264 buf = Buffer.from(buf, enc)265 if (this[_ended]) {266 this.emit('error', new Error('write() after end()'))267 return false268 }269 if (this[_fd] === null || this[_writing] || this[_queue].length) {270 this[_queue].push(buf)271 this[_needDrain] = true272 return false273 }274 this[_writing] = true275 this[_write](buf)276 return true277 }278 [_write] (buf) {279 fs.write(this[_fd], buf, 0, buf.length, this[_pos], (er, bw) =>280 this[_onwrite](er, bw))281 }282 [_onwrite] (er, bw) {283 if (er)284 this[_onerror](er)285 else {286 if (this[_pos] !== null)287 this[_pos] += bw288 if (this[_queue].length)289 this[_flush]()290 else {291 this[_writing] = false292 if (this[_ended] && !this[_finished]) {293 this[_finished] = true294 this[_close]()295 this.emit('finish')296 } else if (this[_needDrain]) {297 this[_needDrain] = false298 this.emit('drain')299 }300 }301 }302 }303 [_flush] () {304 if (this[_queue].length === 0) {305 if (this[_ended])306 this[_onwrite](null, 0)307 } else if (this[_queue].length === 1)308 this[_write](this[_queue].pop())309 else {310 const iovec = this[_queue]311 this[_queue] = []312 writev(this[_fd], iovec, this[_pos],313 (er, bw) => this[_onwrite](er, bw))314 }315 }316 [_close] () {317 if (this[_autoClose] && typeof this[_fd] === 'number') {318 const fd = this[_fd]319 this[_fd] = null320 fs.close(fd, er => er ? this.emit('error', er) : this.emit('close'))321 }322 }323}324class WriteStreamSync extends WriteStream {325 [_open] () {326 let fd327 // only wrap in a try{} block if we know we'll retry, to avoid328 // the rethrow obscuring the error's source frame in most cases.329 if (this[_defaultFlag] && this[_flags] === 'r+') {330 try {331 fd = fs.openSync(this[_path], this[_flags], this[_mode])332 } catch (er) {333 if (er.code === 'ENOENT') {334 this[_flags] = 'w'335 return this[_open]()336 } else337 throw er338 }339 } else340 fd = fs.openSync(this[_path], this[_flags], this[_mode])341 this[_onopen](null, fd)342 }343 [_close] () {344 if (this[_autoClose] && typeof this[_fd] === 'number') {345 const fd = this[_fd]346 this[_fd] = null347 fs.closeSync(fd)348 this.emit('close')349 }350 }351 [_write] (buf) {352 // throw the original, but try to close if it fails353 let threw = true354 try {355 this[_onwrite](null,356 fs.writeSync(this[_fd], buf, 0, buf.length, this[_pos]))357 threw = false358 } finally {359 if (threw)360 try { this[_close]() } catch (_) {}361 }362 }363}364exports.ReadStream = ReadStream365exports.ReadStreamSync = ReadStreamSync366exports.WriteStream = WriteStream...
Using AI Code Generation
1var fs = require('fs');2var fd = fs.openSync('test.txt', 'r');3var buffer = new Buffer(1024);4fs.read(fd, buffer, 0, 1024, 0, function(err, num) {5 console.log(buffer.toString('utf8', 0, num));6});
Using AI Code Generation
1var fs = require('fs');2var fd = fs.openSync('test.txt', 'w');3fs.writeSync(fd, 'hello world');4fs.closeSync(fd);5var fs = require('fs');6fs.writeFile('test.txt', 'hello world', function (err) {7 if (err) throw err;8 console.log('Saved!');9});10var fs = require('fs');11fs.appendFile('test.txt', 'hello world', function (err) {12 if (err) throw err;13 console.log('Saved!');14});15var fs = require('fs');16fs.readFile('test.txt', function (err, data) {17 if (err) throw err;18 console.log(data.toString());19});20var fs = require('fs');21fs.readdir('test', function (err, files) {22 if (err) throw err;23 files.forEach(function (file) {24 console.log(file);25 });26});27var fs = require('fs');28fs.rename('test.txt', 'test1.txt', function (err) {29 if (err) throw err;30 console.log('File Renamed!');31});32var fs = require('fs');33fs.unlink('test.txt', function (err) {34 if (err) throw err;35 console.log('File deleted!');36});37var fs = require('fs');38fs.rmdir('test', function (err) {
Using AI Code Generation
1var fd = require('fd');2var assert = require('assert');3var fd1 = new fd.FD(1);4fd1.write("hello world\n");5assert.equal(fd1.read(), "hello world\n");6var fd2 = new fd.FD(2);7fd2.write("hello world\n");8assert.equal(fd2.read(), "hello world\n");9var fd3 = new fd.FD(3);10fd3.write("hello world\n");11assert.equal(fd3.read(), "hello world\n");12var fd4 = new fd.FD(4);13fd4.write("hello world\n");14assert.equal(fd4.read(), "hello world\n");15var fd5 = new fd.FD(5);16fd5.write("hello world\n");17assert.equal(fd5.read(), "hello world\n");18var fd6 = new fd.FD(6);19fd6.write("hello world\n");20assert.equal(fd6.read(), "hello world\n");21var fd7 = new fd.FD(7);22fd7.write("hello world\n");23assert.equal(fd7.read(), "hello world\n");24var fd8 = new fd.FD(8);25fd8.write("hello world\n");26assert.equal(fd8.read(), "hello world\n");27var fd9 = new fd.FD(9);28fd9.write("hello world\n");29assert.equal(fd9.read(), "hello world\n");30var fd10 = new fd.FD(10);31fd10.write("hello world\n");32assert.equal(fd10.read(), "hello world\n");33var fd11 = new fd.FD(11);34fd11.write("hello world\n");35assert.equal(fd11.read(), "hello world\n");36var fd12 = new fd.FD(12);37fd12.write("hello world\n");38assert.equal(fd12.read(), "hello world\n");39var fd13 = new fd.FD(13);40fd13.write("hello world\n");41assert.equal(fd13.read(), "hello world\n");42var fd14 = new fd.FD(14
Using AI Code Generation
1var wpt = require('webpagetest');2var options = {3};4var webpagetest = new wpt('www.webpagetest.org', options.key);5webpagetest.runTest(url, options, function(err, data) {6 if (err) return console.error(err);7 console.log('Test submitted to WebPagetest for %s: %s', url, data.data.userUrl);8 console.log('Test ID: %s', data.data.testId);9 console.log('View the test: %s', data.data.userUrl);10 console.log('View the test results: %s', data.data.summaryCSV);11 console.log('View the test results: %s', data.data.jsonUrl);12 console.log('View the test results: %s', data.data.xmlUrl);13 console.log('View the test results: %s', data.data.userUrl);14 console.log('View the test results: %s', data.data.summaryCSV);15 console.log('View the test results: %s', dat
Using AI Code Generation
1var wpt = require('webpagetest');2var fs = require('fs');3var options = {4};5var wpt = new wpt('A.3a7f3a3e3d7c3c9b9a7b9a1a1a1a1a1a');6var testId = '180210_4Y_4a8e8c2e2b2f2f2f2f2f2f2f2f2f2f2f';7wpt.getTestResults(testId, function(err, data) {8 if (err) return console.error(err);9 console.log(data);10});11wpt.getTestStatus(testId, function(err, data) {12 if (err) return console.error(err);13 console.log(data);14});15wpt.getLocations(function(err, data) {16 if (err) return console.error(err);17 console.log(data);18});19wpt.getTesters(function(err, data) {20 if (err) return console.error(err);21 console.log(data);22});
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!!