How to use buf method in wpt

Best JavaScript code snippet using wpt

mimeOfBuffer.js

Source:mimeOfBuffer.js Github

copy

Full Screen

1/* eslint complexity: 0 */2// from file-type by @sindresorhus under the MIT license3// https://github.com/sindresorhus/file-type4function mimeOfBuffer(input) {5 const buf = new Uint8Array(input);6 if (!(buf && buf.length > 1)) {7 return null;8 }9 if (buf[0] === 0xFF && buf[1] === 0xD8 && buf[2] === 0xFF) {10 return {11 ext: 'jpg',12 mime: 'image/jpeg',13 };14 }15 if (buf[0] === 0x89 && buf[1] === 0x50 && buf[2] === 0x4E && buf[3] === 0x47) {16 return {17 ext: 'png',18 mime: 'image/png',19 };20 }21 if (buf[0] === 0x47 && buf[1] === 0x49 && buf[2] === 0x46) {22 return {23 ext: 'gif',24 mime: 'image/gif',25 };26 }27 if (buf[8] === 0x57 && buf[9] === 0x45 && buf[10] === 0x42 && buf[11] === 0x50) {28 return {29 ext: 'webp',30 mime: 'image/webp',31 };32 }33 if (buf[0] === 0x46 && buf[1] === 0x4C && buf[2] === 0x49 && buf[3] === 0x46) {34 return {35 ext: 'flif',36 mime: 'image/flif',37 };38 }39 // needs to be before `tif` check40 if (41 ((buf[0] === 0x49 && buf[1] === 0x49 && buf[2] === 0x2A && buf[3] === 0x0) ||42 (buf[0] === 0x4D && buf[1] === 0x4D && buf[2] === 0x0 && buf[3] === 0x2A)) && buf[8] === 0x43 && buf[9] === 0x5243 ) {44 return {45 ext: 'cr2',46 mime: 'image/x-canon-cr2',47 };48 }49 if (50 (buf[0] === 0x49 && buf[1] === 0x49 && buf[2] === 0x2A && buf[3] === 0x0) ||51 (buf[0] === 0x4D && buf[1] === 0x4D && buf[2] === 0x0 && buf[3] === 0x2A)52 ) {53 return {54 ext: 'tif',55 mime: 'image/tiff',56 };57 }58 if (buf[0] === 0x42 && buf[1] === 0x4D) {59 return {60 ext: 'bmp',61 mime: 'image/bmp',62 };63 }64 if (buf[0] === 0x49 && buf[1] === 0x49 && buf[2] === 0xBC) {65 return {66 ext: 'jxr',67 mime: 'image/vnd.ms-photo',68 };69 }70 if (buf[0] === 0x38 && buf[1] === 0x42 && buf[2] === 0x50 && buf[3] === 0x53) {71 return {72 ext: 'psd',73 mime: 'image/vnd.adobe.photoshop',74 };75 }76 // needs to be before `zip` check77 if (78 buf[0] === 0x50 && buf[1] === 0x4B && buf[2] === 0x3 && buf[3] === 0x4 && buf[30] === 0x6D && buf[31] === 0x69 &&79 buf[32] === 0x6D && buf[33] === 0x65 && buf[34] === 0x74 && buf[35] === 0x79 && buf[36] === 0x70 &&80 buf[37] === 0x65 && buf[38] === 0x61 && buf[39] === 0x70 && buf[40] === 0x70 && buf[41] === 0x6C &&81 buf[42] === 0x69 && buf[43] === 0x63 && buf[44] === 0x61 && buf[45] === 0x74 && buf[46] === 0x69 &&82 buf[47] === 0x6F && buf[48] === 0x6E && buf[49] === 0x2F && buf[50] === 0x65 && buf[51] === 0x70 &&83 buf[52] === 0x75 && buf[53] === 0x62 && buf[54] === 0x2B && buf[55] === 0x7A && buf[56] === 0x69 &&84 buf[57] === 0x7085 ) {86 return {87 ext: 'epub',88 mime: 'application/epub+zip',89 };90 }91 // needs to be before `zip` check92 // assumes signed .xpi from addons.mozilla.org93 if (94 buf[0] === 0x50 && buf[1] === 0x4B && buf[2] === 0x3 && buf[3] === 0x4 && buf[30] === 0x4D && buf[31] === 0x45 &&95 buf[32] === 0x54 && buf[33] === 0x41 && buf[34] === 0x2D && buf[35] === 0x49 && buf[36] === 0x4E &&96 buf[37] === 0x46 && buf[38] === 0x2F && buf[39] === 0x6D && buf[40] === 0x6F && buf[41] === 0x7A &&97 buf[42] === 0x69 && buf[43] === 0x6C && buf[44] === 0x6C && buf[45] === 0x61 && buf[46] === 0x2E &&98 buf[47] === 0x72 && buf[48] === 0x73 && buf[49] === 0x6199 ) {100 return {101 ext: 'xpi',102 mime: 'application/x-xpinstall',103 };104 }105 if (106 buf[0] === 0x50 && buf[1] === 0x4B && (buf[2] === 0x3 || buf[2] === 0x5 || buf[2] === 0x7) &&107 (buf[3] === 0x4 || buf[3] === 0x6 || buf[3] === 0x8)108 ) {109 return {110 ext: 'zip',111 mime: 'application/zip',112 };113 }114 if (buf[257] === 0x75 && buf[258] === 0x73 && buf[259] === 0x74 && buf[260] === 0x61 && buf[261] === 0x72) {115 return {116 ext: 'tar',117 mime: 'application/x-tar',118 };119 }120 if (121 buf[0] === 0x52 && buf[1] === 0x61 && buf[2] === 0x72 && buf[3] === 0x21 && buf[4] === 0x1A && buf[5] === 0x7 &&122 (buf[6] === 0x0 || buf[6] === 0x1)123 ) {124 return {125 ext: 'rar',126 mime: 'application/x-rar-compressed',127 };128 }129 if (buf[0] === 0x1F && buf[1] === 0x8B && buf[2] === 0x8) {130 return {131 ext: 'gz',132 mime: 'application/gzip',133 };134 }135 if (buf[0] === 0x42 && buf[1] === 0x5A && buf[2] === 0x68) {136 return {137 ext: 'bz2',138 mime: 'application/x-bzip2',139 };140 }141 if (buf[0] === 0x37 && buf[1] === 0x7A && buf[2] === 0xBC && buf[3] === 0xAF && buf[4] === 0x27 && buf[5] === 0x1C) {142 return {143 ext: '7z',144 mime: 'application/x-7z-compressed',145 };146 }147 if (buf[0] === 0x78 && buf[1] === 0x01) {148 return {149 ext: 'dmg',150 mime: 'application/x-apple-diskimage',151 };152 }153 if (154 (buf[0] === 0x0 && buf[1] === 0x0 && buf[2] === 0x0 && (buf[3] === 0x18 || buf[3] === 0x20) && buf[4] === 0x66 &&155 buf[5] === 0x74 && buf[6] === 0x79 && buf[7] === 0x70) ||156 (buf[0] === 0x33 && buf[1] === 0x67 && buf[2] === 0x70 && buf[3] === 0x35) ||157 (buf[0] === 0x0 && buf[1] === 0x0 && buf[2] === 0x0 && buf[3] === 0x1C && buf[4] === 0x66 && buf[5] === 0x74 &&158 buf[6] === 0x79 && buf[7] === 0x70 && buf[8] === 0x6D && buf[9] === 0x70 && buf[10] === 0x34 &&159 buf[11] === 0x32 && buf[16] === 0x6D && buf[17] === 0x70 && buf[18] === 0x34 && buf[19] === 0x31 &&160 buf[20] === 0x6D && buf[21] === 0x70 && buf[22] === 0x34 && buf[23] === 0x32 && buf[24] === 0x69 &&161 buf[25] === 0x73 && buf[26] === 0x6F && buf[27] === 0x6D) ||162 (buf[0] === 0x0 && buf[1] === 0x0 && buf[2] === 0x0 && buf[3] === 0x1C && buf[4] === 0x66 && buf[5] === 0x74 &&163 buf[6] === 0x79 && buf[7] === 0x70 && buf[8] === 0x69 && buf[9] === 0x73 && buf[10] === 0x6F &&164 buf[11] === 0x6D) ||165 (buf[0] === 0x0 && buf[1] === 0x0 && buf[2] === 0x0 && buf[3] === 0x1c && buf[4] === 0x66 && buf[5] === 0x74 &&166 buf[6] === 0x79 && buf[7] === 0x70 && buf[8] === 0x6D && buf[9] === 0x70 && buf[10] === 0x34 &&167 buf[11] === 0x32 && buf[12] === 0x0 && buf[13] === 0x0 && buf[14] === 0x0 && buf[15] === 0x0)168 ) {169 return {170 ext: 'mp4',171 mime: 'video/mp4',172 };173 }174 if (175 buf[0] === 0x0 && buf[1] === 0x0 && buf[2] === 0x0 && buf[3] === 0x1C && buf[4] === 0x66 &&176 buf[5] === 0x74 && buf[6] === 0x79 && buf[7] === 0x70 && buf[8] === 0x4D && buf[9] === 0x34 && buf[10] === 0x56177 ) {178 return {179 ext: 'm4v',180 mime: 'video/x-m4v',181 };182 }183 if (buf[0] === 0x4D && buf[1] === 0x54 && buf[2] === 0x68 && buf[3] === 0x64) {184 return {185 ext: 'mid',186 mime: 'audio/midi',187 };188 }189 // https://github.com/threatstack/libmagic/blob/master/magic/Magdir/matroska190 if (buf[0] === 0x1A && buf[1] === 0x45 && buf[2] === 0xDF && buf[3] === 0xA3) {191 const sliced = buf.subarray(4, 4 + 4096);192 const idPos = sliced.findIndex((el, i, arr) => arr[i] === 0x42 && arr[i + 1] === 0x82);193 if (idPos >= 0) {194 const docTypePos = idPos + 3;195 const findDocType = (type) => Array.from(type).every((c, i) => sliced[docTypePos + i] === c.charCodeAt(0));196 if (findDocType('matroska')) {197 return {198 ext: 'mkv',199 mime: 'video/x-matroska',200 };201 }202 if (findDocType('webm')) {203 return {204 ext: 'webm',205 mime: 'video/webm',206 };207 }208 }209 }210 if (211 buf[0] === 0x0 && buf[1] === 0x0 && buf[2] === 0x0 && buf[3] === 0x14 && buf[4] === 0x66 && buf[5] === 0x74 &&212 buf[6] === 0x79 && buf[7] === 0x70213 ) {214 return {215 ext: 'mov',216 mime: 'video/quicktime',217 };218 }219 if (220 buf[0] === 0x52 && buf[1] === 0x49 && buf[2] === 0x46 && buf[3] === 0x46 && buf[8] === 0x41 && buf[9] === 0x56 &&221 buf[10] === 0x49222 ) {223 return {224 ext: 'avi',225 mime: 'video/x-msvideo',226 };227 }228 if (229 buf[0] === 0x30 && buf[1] === 0x26 && buf[2] === 0xB2 && buf[3] === 0x75 && buf[4] === 0x8E && buf[5] === 0x66 &&230 buf[6] === 0xCF && buf[7] === 0x11 && buf[8] === 0xA6 && buf[9] === 0xD9231 ) {232 return {233 ext: 'wmv',234 mime: 'video/x-ms-wmv',235 };236 }237 if (buf[0] === 0x0 && buf[1] === 0x0 && buf[2] === 0x1 && buf[3].toString(16)[0] === 'b') {238 return {239 ext: 'mpg',240 mime: 'video/mpeg',241 };242 }243 if ((buf[0] === 0x49 && buf[1] === 0x44 && buf[2] === 0x33) || (buf[0] === 0xFF && buf[1] === 0xfb)) {244 return {245 ext: 'mp3',246 mime: 'audio/mpeg',247 };248 }249 if ((buf[4] === 0x66 && buf[5] === 0x74 && buf[6] === 0x79 && buf[7] === 0x70 && buf[8] === 0x4D &&250 buf[9] === 0x34 && buf[10] === 0x41) || (buf[0] === 0x4D && buf[1] === 0x34 && buf[2] === 0x41 && buf[3] === 0x20)251 ) {252 return {253 ext: 'm4a',254 mime: 'audio/m4a',255 };256 }257 // needs to be before `ogg` check258 if (259 buf[28] === 0x4F && buf[29] === 0x70 && buf[30] === 0x75 && buf[31] === 0x73 && buf[32] === 0x48 &&260 buf[33] === 0x65 && buf[34] === 0x61 && buf[35] === 0x64261 ) {262 return {263 ext: 'opus',264 mime: 'audio/opus',265 };266 }267 if (buf[0] === 0x4F && buf[1] === 0x67 && buf[2] === 0x67 && buf[3] === 0x53) {268 return {269 ext: 'ogg',270 mime: 'audio/ogg',271 };272 }273 if (buf[0] === 0x66 && buf[1] === 0x4C && buf[2] === 0x61 && buf[3] === 0x43) {274 return {275 ext: 'flac',276 mime: 'audio/x-flac',277 };278 }279 if (280 buf[0] === 0x52 && buf[1] === 0x49 && buf[2] === 0x46 && buf[3] === 0x46 && buf[8] === 0x57 && buf[9] === 0x41 &&281 buf[10] === 0x56 && buf[11] === 0x45282 ) {283 return {284 ext: 'wav',285 mime: 'audio/x-wav',286 };287 }288 if (buf[0] === 0x23 && buf[1] === 0x21 && buf[2] === 0x41 && buf[3] === 0x4D && buf[4] === 0x52 && buf[5] === 0x0A) {289 return {290 ext: 'amr',291 mime: 'audio/amr',292 };293 }294 if (buf[0] === 0x25 && buf[1] === 0x50 && buf[2] === 0x44 && buf[3] === 0x46) {295 return {296 ext: 'pdf',297 mime: 'application/pdf',298 };299 }300 if (buf[0] === 0x4D && buf[1] === 0x5A) {301 return {302 ext: 'exe',303 mime: 'application/x-msdownload',304 };305 }306 if ((buf[0] === 0x43 || buf[0] === 0x46) && buf[1] === 0x57 && buf[2] === 0x53) {307 return {308 ext: 'swf',309 mime: 'application/x-shockwave-flash',310 };311 }312 if (buf[0] === 0x7B && buf[1] === 0x5C && buf[2] === 0x72 && buf[3] === 0x74 && buf[4] === 0x66) {313 return {314 ext: 'rtf',315 mime: 'application/rtf',316 };317 }318 if (319 (buf[0] === 0x77 && buf[1] === 0x4F && buf[2] === 0x46 && buf[3] === 0x46) &&320 (321 (buf[4] === 0x00 && buf[5] === 0x01 && buf[6] === 0x00 && buf[7] === 0x00) ||322 (buf[4] === 0x4F && buf[5] === 0x54 && buf[6] === 0x54 && buf[7] === 0x4F)323 )324 ) {325 return {326 ext: 'woff',327 mime: 'application/font-woff',328 };329 }330 if (331 (buf[0] === 0x77 && buf[1] === 0x4F && buf[2] === 0x46 && buf[3] === 0x32) &&332 (333 (buf[4] === 0x00 && buf[5] === 0x01 && buf[6] === 0x00 && buf[7] === 0x00) ||334 (buf[4] === 0x4F && buf[5] === 0x54 && buf[6] === 0x54 && buf[7] === 0x4F)335 )336 ) {337 return {338 ext: 'woff2',339 mime: 'application/font-woff',340 };341 }342 if (343 (buf[34] === 0x4C && buf[35] === 0x50) &&344 (345 (buf[8] === 0x00 && buf[9] === 0x00 && buf[10] === 0x01) ||346 (buf[8] === 0x01 && buf[9] === 0x00 && buf[10] === 0x02) ||347 (buf[8] === 0x02 && buf[9] === 0x00 && buf[10] === 0x02)348 )349 ) {350 return {351 ext: 'eot',352 mime: 'application/octet-stream',353 };354 }355 if (buf[0] === 0x00 && buf[1] === 0x01 && buf[2] === 0x00 && buf[3] === 0x00 && buf[4] === 0x00) {356 return {357 ext: 'ttf',358 mime: 'application/font-sfnt',359 };360 }361 if (buf[0] === 0x4F && buf[1] === 0x54 && buf[2] === 0x54 && buf[3] === 0x4F && buf[4] === 0x00) {362 return {363 ext: 'otf',364 mime: 'application/font-sfnt',365 };366 }367 if (buf[0] === 0x00 && buf[1] === 0x00 && buf[2] === 0x01 && buf[3] === 0x00) {368 return {369 ext: 'ico',370 mime: 'image/x-icon',371 };372 }373 if (buf[0] === 0x46 && buf[1] === 0x4C && buf[2] === 0x56 && buf[3] === 0x01) {374 return {375 ext: 'flv',376 mime: 'video/x-flv',377 };378 }379 if (buf[0] === 0x25 && buf[1] === 0x21) {380 return {381 ext: 'ps',382 mime: 'application/postscript',383 };384 }385 if (buf[0] === 0xFD && buf[1] === 0x37 && buf[2] === 0x7A && buf[3] === 0x58 && buf[4] === 0x5A && buf[5] === 0x00) {386 return {387 ext: 'xz',388 mime: 'application/x-xz',389 };390 }391 if (buf[0] === 0x53 && buf[1] === 0x51 && buf[2] === 0x4C && buf[3] === 0x69) {392 return {393 ext: 'sqlite',394 mime: 'application/x-sqlite3',395 };396 }397 if (buf[0] === 0x4E && buf[1] === 0x45 && buf[2] === 0x53 && buf[3] === 0x1A) {398 return {399 ext: 'nes',400 mime: 'application/x-nintendo-nes-rom',401 };402 }403 if (buf[0] === 0x43 && buf[1] === 0x72 && buf[2] === 0x32 && buf[3] === 0x34) {404 return {405 ext: 'crx',406 mime: 'application/x-google-chrome-extension',407 };408 }409 if (410 (buf[0] === 0x4D && buf[1] === 0x53 && buf[2] === 0x43 && buf[3] === 0x46) ||411 (buf[0] === 0x49 && buf[1] === 0x53 && buf[2] === 0x63 && buf[3] === 0x28)412 ) {413 return {414 ext: 'cab',415 mime: 'application/vnd.ms-cab-compressed',416 };417 }418 // needs to be before `ar` check419 if (420 buf[0] === 0x21 && buf[1] === 0x3C && buf[2] === 0x61 && buf[3] === 0x72 && buf[4] === 0x63 && buf[5] === 0x68 &&421 buf[6] === 0x3E && buf[7] === 0x0A && buf[8] === 0x64 && buf[9] === 0x65 && buf[10] === 0x62 && buf[11] === 0x69 &&422 buf[12] === 0x61 && buf[13] === 0x6E && buf[14] === 0x2D && buf[15] === 0x62 && buf[16] === 0x69 &&423 buf[17] === 0x6E && buf[18] === 0x61 && buf[19] === 0x72 && buf[20] === 0x79424 ) {425 return {426 ext: 'deb',427 mime: 'application/x-deb',428 };429 }430 if (431 buf[0] === 0x21 && buf[1] === 0x3C && buf[2] === 0x61 && buf[3] === 0x72 && buf[4] === 0x63 && buf[5] === 0x68 &&432 buf[6] === 0x3E433 ) {434 return {435 ext: 'ar',436 mime: 'application/x-unix-archive',437 };438 }439 if (buf[0] === 0xED && buf[1] === 0xAB && buf[2] === 0xEE && buf[3] === 0xDB) {440 return {441 ext: 'rpm',442 mime: 'application/x-rpm',443 };444 }445 if (446 (buf[0] === 0x1F && buf[1] === 0xA0) ||447 (buf[0] === 0x1F && buf[1] === 0x9D)448 ) {449 return {450 ext: 'Z',451 mime: 'application/x-compress',452 };453 }454 if (buf[0] === 0x4C && buf[1] === 0x5A && buf[2] === 0x49 && buf[3] === 0x50) {455 return {456 ext: 'lz',457 mime: 'application/x-lzip',458 };459 }460 if (461 buf[0] === 0xD0 && buf[1] === 0xCF && buf[2] === 0x11 && buf[3] === 0xE0 && buf[4] === 0xA1 && buf[5] === 0xB1 &&462 buf[6] === 0x1A && buf[7] === 0xE1463 ) {464 return {465 ext: 'msi',466 mime: 'application/x-msi',467 };468 }469 if (470 buf[0] === 0x06 && buf[1] === 0x0E && buf[2] === 0x2B && buf[3] === 0x34 && buf[4] === 0x02 && buf[5] === 0x05 &&471 buf[6] === 0x01 && buf[7] === 0x01 && buf[8] === 0x0D && buf[9] === 0x01 && buf[10] === 0x02 && buf[11] === 0x01 &&472 buf[12] === 0x01 && buf[13] === 0x02473 ) {474 return {475 ext: 'mxf',476 mime: 'application/mxf',477 };478 }479 return null;480}...

Full Screen

Full Screen

test-buffer-swap.js

Source:test-buffer-swap.js Github

copy

Full Screen

1'use strict';2require('../common');3const assert = require('assert');4// Test buffers small enough to use the JS implementation5{6 const buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,7 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]);8 assert.strictEqual(buf, buf.swap16());9 assert.deepStrictEqual(buf, Buffer.from([0x02, 0x01, 0x04, 0x03, 0x06, 0x05,10 0x08, 0x07, 0x0a, 0x09, 0x0c, 0x0b,11 0x0e, 0x0d, 0x10, 0x0f]));12 buf.swap16(); // restore13 assert.strictEqual(buf, buf.swap32());14 assert.deepStrictEqual(buf, Buffer.from([0x04, 0x03, 0x02, 0x01, 0x08, 0x07,15 0x06, 0x05, 0x0c, 0x0b, 0x0a, 0x09,16 0x10, 0x0f, 0x0e, 0x0d]));17 buf.swap32(); // restore18 assert.strictEqual(buf, buf.swap64());19 assert.deepStrictEqual(buf, Buffer.from([0x08, 0x07, 0x06, 0x05, 0x04, 0x03,20 0x02, 0x01, 0x10, 0x0f, 0x0e, 0x0d,21 0x0c, 0x0b, 0x0a, 0x09]));22}23// Operates in-place24{25 const buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7]);26 buf.slice(1, 5).swap32();27 assert.deepStrictEqual(buf, Buffer.from([0x1, 0x5, 0x4, 0x3, 0x2, 0x6, 0x7]));28 buf.slice(1, 5).swap16();29 assert.deepStrictEqual(buf, Buffer.from([0x1, 0x4, 0x5, 0x2, 0x3, 0x6, 0x7]));30 // Length assertions31 const re16 = /Buffer size must be a multiple of 16-bits/;32 const re32 = /Buffer size must be a multiple of 32-bits/;33 const re64 = /Buffer size must be a multiple of 64-bits/;34 assert.throws(() => Buffer.from(buf).swap16(), re16);35 assert.throws(() => Buffer.alloc(1025).swap16(), re16);36 assert.throws(() => Buffer.from(buf).swap32(), re32);37 assert.throws(() => buf.slice(1, 3).swap32(), re32);38 assert.throws(() => Buffer.alloc(1025).swap32(), re32);39 assert.throws(() => buf.slice(1, 3).swap64(), re64);40 assert.throws(() => Buffer.alloc(1025).swap64(), re64);41}42{43 const buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,44 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,45 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,46 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]);47 buf.slice(2, 18).swap64();48 assert.deepStrictEqual(buf, Buffer.from([0x01, 0x02, 0x0a, 0x09, 0x08, 0x07,49 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,50 0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b,51 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,52 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,53 0x0f, 0x10]));54}55// Force use of native code (Buffer size above threshold limit for js impl)56{57 const bufData = new Uint32Array(256).fill(0x04030201);58 const buf = Buffer.from(bufData.buffer, bufData.byteOffset);59 const otherBufData = new Uint32Array(256).fill(0x03040102);60 const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset);61 buf.swap16();62 assert.deepStrictEqual(buf, otherBuf);63}64{65 const bufData = new Uint32Array(256).fill(0x04030201);66 const buf = Buffer.from(bufData.buffer);67 const otherBufData = new Uint32Array(256).fill(0x01020304);68 const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset);69 buf.swap32();70 assert.deepStrictEqual(buf, otherBuf);71}72{73 const bufData = new Uint8Array(256 * 8);74 const otherBufData = new Uint8Array(256 * 8);75 for (let i = 0; i < bufData.length; i++) {76 bufData[i] = i % 8;77 otherBufData[otherBufData.length - i - 1] = i % 8;78 }79 const buf = Buffer.from(bufData.buffer, bufData.byteOffset);80 const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset);81 buf.swap64();82 assert.deepStrictEqual(buf, otherBuf);83}84// Test native code with buffers that are not memory-aligned85{86 const bufData = new Uint8Array(256 * 8);87 const otherBufData = new Uint8Array(256 * 8 - 2);88 for (let i = 0; i < bufData.length; i++) {89 bufData[i] = i % 2;90 }91 for (let i = 1; i < otherBufData.length; i++) {92 otherBufData[otherBufData.length - i] = (i + 1) % 2;93 }94 const buf = Buffer.from(bufData.buffer, bufData.byteOffset);95 // 0|1 0|1 0|1...96 const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset);97 // 0|0 1|0 1|0...98 buf.slice(1, buf.length - 1).swap16();99 assert.deepStrictEqual(buf.slice(0, otherBuf.length), otherBuf);100}101{102 const bufData = new Uint8Array(256 * 8);103 const otherBufData = new Uint8Array(256 * 8 - 4);104 for (let i = 0; i < bufData.length; i++) {105 bufData[i] = i % 4;106 }107 for (let i = 1; i < otherBufData.length; i++) {108 otherBufData[otherBufData.length - i] = (i + 1) % 4;109 }110 const buf = Buffer.from(bufData.buffer, bufData.byteOffset);111 // 0|1 2 3 0|1 2 3...112 const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset);113 // 0|0 3 2 1|0 3 2...114 buf.slice(1, buf.length - 3).swap32();115 assert.deepStrictEqual(buf.slice(0, otherBuf.length), otherBuf);116}117{118 const bufData = new Uint8Array(256 * 8);119 const otherBufData = new Uint8Array(256 * 8 - 8);120 for (let i = 0; i < bufData.length; i++) {121 bufData[i] = i % 8;122 }123 for (let i = 1; i < otherBufData.length; i++) {124 otherBufData[otherBufData.length - i] = (i + 1) % 8;125 }126 const buf = Buffer.from(bufData.buffer, bufData.byteOffset);127 // 0|1 2 3 4 5 6 7 0|1 2 3 4...128 const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset);129 // 0|0 7 6 5 4 3 2 1|0 7 6 5...130 buf.slice(1, buf.length - 7).swap64();131 assert.deepStrictEqual(buf.slice(0, otherBuf.length), otherBuf);...

Full Screen

Full Screen

message.js

Source:message.js Github

copy

Full Screen

1"use strict";23const Buffer = require("buffer").Buffer;4const torrentParser = require("./torrent-parser");5const util = require("./util");67module.exports.buildHandshake = (torrent) => {8 const buf = Buffer.alloc(68);9 // pstrlen10 buf.writeUInt8(19, 0);11 // pstr12 buf.write("BitTorrent protocol", 1);13 // reserved14 buf.writeUInt32BE(0, 20);15 buf.writeUInt32BE(0, 24);16 // info hash17 torrentParser.infoHash(torrent).copy(buf, 28);18 // peer id19 util.genId().copy(buf, 48);20 return buf;21};2223module.exports.buildKeepAlive = () => Buffer.alloc(4);2425module.exports.buildChoke = () => {26 const buf = Buffer.alloc(5);27 // length28 buf.writeUInt32BE(1, 0);29 // id30 buf.writeUInt8(0, 4);31 return buf;32};3334module.exports.buildUnchoke = () => {35 const buf = Buffer.alloc(5);36 // length37 buf.writeUInt32BE(1, 0);38 // id39 buf.writeUInt8(1, 4);40 return buf;41};4243module.exports.buildInterested = () => {44 const buf = Buffer.alloc(5);45 // length46 buf.writeUInt32BE(1, 0);47 // id48 buf.writeUInt8(2, 4);49 return buf;50};5152module.exports.buildUninterested = () => {53 const buf = Buffer.alloc(5);54 // length55 buf.writeUInt32BE(1, 0);56 // id57 buf.writeUInt8(3, 4);58 return buf;59};6061module.exports.buildHave = (payload) => {62 const buf = Buffer.alloc(9);63 // length64 buf.writeUInt32BE(5, 0);65 // id66 buf.writeUInt8(4, 4);67 // piece index68 buf.writeUInt32BE(payload, 5);69 return buf;70};7172module.exports.buildBitfield = (bitfield) => {73 const buf = Buffer.alloc(14);74 // length75 buf.writeUInt32BE(payload.length + 1, 0);76 // id77 buf.writeUInt8(5, 4);78 // bitfield79 bitfield.copy(buf, 5);80 return buf;81};8283module.exports.buildRequest = (payload) => {84 const buf = Buffer.alloc(17);85 // length86 buf.writeUInt32BE(13, 0);87 // id88 buf.writeUInt8(6, 4);89 // piece index90 buf.writeUInt32BE(payload.index, 5);91 // begin92 buf.writeUInt32BE(payload.begin, 9);93 // length94 buf.writeUInt32BE(payload.length, 13);95 return buf;96};9798module.exports.buildPiece = (payload) => {99 const buf = Buffer.alloc(payload.block.length + 13);100 // length101 buf.writeUInt32BE(payload.block.length + 9, 0);102 // id103 buf.writeUInt8(7, 4);104 // piece index105 buf.writeUInt32BE(payload.index, 5);106 // begin107 buf.writeUInt32BE(payload.begin, 9);108 // block109 payload.block.copy(buf, 13);110 return buf;111};112113module.exports.buildCancel = (payload) => {114 const buf = Buffer.alloc(17);115 // length116 buf.writeUInt32BE(13, 0);117 // id118 buf.writeUInt8(8, 4);119 // piece index120 buf.writeUInt32BE(payload.index, 5);121 // begin122 buf.writeUInt32BE(payload.begin, 9);123 // length124 buf.writeUInt32BE(payload.length, 13);125 return buf;126};127128module.exports.buildPort = (payload) => {129 const buf = Buffer.alloc(7);130 // length131 buf.writeUInt32BE(3, 0);132 // id133 buf.writeUInt8(9, 4);134 // listen-port135 buf.writeUInt16BE(payload, 5);136 return buf;137};138139module.exports.parse = (msg) => {140 const id = msg.length > 4 ? msg.readInt8(4) : null;141 let payload = msg.length > 5 ? msg.slice(5) : null;142 if (id === 6 || id === 7 || id === 8) {143 const rest = payload.slice(8);144 payload = {145 index: payload.readInt32BE(0),146 begin: payload.readInt32BE(4),147 };148 payload[id === 7 ? "block" : "length"] = rest;149 }150151 return {152 size: msg.readInt32BE(0),153 id: id,154 payload: payload,155 }; ...

Full Screen

Full Screen

auto.js

Source:auto.js Github

copy

Full Screen

1// Copyright 2018 Joyent, Inc.2module.exports = {3 read: read,4 write: write5};6var assert = require('assert-plus');7var Buffer = require('safer-buffer').Buffer;8var utils = require('../utils');9var Key = require('../key');10var PrivateKey = require('../private-key');11var pem = require('./pem');12var ssh = require('./ssh');13var rfc4253 = require('./rfc4253');14var dnssec = require('./dnssec');15var putty = require('./putty');16var DNSSEC_PRIVKEY_HEADER_PREFIX = 'Private-key-format: v1';17function read(buf, options) {18 if (typeof (buf) === 'string') {19 if (buf.trim().match(/^[-]+[ ]*BEGIN/))20 return (pem.read(buf, options));21 if (buf.match(/^\s*ssh-[a-z]/))22 return (ssh.read(buf, options));23 if (buf.match(/^\s*ecdsa-/))24 return (ssh.read(buf, options));25 if (buf.match(/^putty-user-key-file-2:/i))26 return (putty.read(buf, options));27 if (findDNSSECHeader(buf))28 return (dnssec.read(buf, options));29 buf = Buffer.from(buf, 'binary');30 } else {31 assert.buffer(buf);32 if (findPEMHeader(buf))33 return (pem.read(buf, options));34 if (findSSHHeader(buf))35 return (ssh.read(buf, options));36 if (findPuTTYHeader(buf))37 return (putty.read(buf, options));38 if (findDNSSECHeader(buf))39 return (dnssec.read(buf, options));40 }41 if (buf.readUInt32BE(0) < buf.length)42 return (rfc4253.read(buf, options));43 throw (new Error('Failed to auto-detect format of key'));44}45function findPuTTYHeader(buf) {46 var offset = 0;47 while (offset < buf.length &&48 (buf[offset] === 32 || buf[offset] === 10 || buf[offset] === 9))49 ++offset;50 if (offset + 22 <= buf.length &&51 buf.slice(offset, offset + 22).toString('ascii').toLowerCase() ===52 'putty-user-key-file-2:')53 return (true);54 return (false);55}56function findSSHHeader(buf) {57 var offset = 0;58 while (offset < buf.length &&59 (buf[offset] === 32 || buf[offset] === 10 || buf[offset] === 9))60 ++offset;61 if (offset + 4 <= buf.length &&62 buf.slice(offset, offset + 4).toString('ascii') === 'ssh-')63 return (true);64 if (offset + 6 <= buf.length &&65 buf.slice(offset, offset + 6).toString('ascii') === 'ecdsa-')66 return (true);67 return (false);68}69function findPEMHeader(buf) {70 var offset = 0;71 while (offset < buf.length &&72 (buf[offset] === 32 || buf[offset] === 10))73 ++offset;74 if (buf[offset] !== 45)75 return (false);76 while (offset < buf.length &&77 (buf[offset] === 45))78 ++offset;79 while (offset < buf.length &&80 (buf[offset] === 32))81 ++offset;82 if (offset + 5 > buf.length ||83 buf.slice(offset, offset + 5).toString('ascii') !== 'BEGIN')84 return (false);85 return (true);86}87function findDNSSECHeader(buf) {88 // private case first89 if (buf.length <= DNSSEC_PRIVKEY_HEADER_PREFIX.length)90 return (false);91 var headerCheck = buf.slice(0, DNSSEC_PRIVKEY_HEADER_PREFIX.length);92 if (headerCheck.toString('ascii') === DNSSEC_PRIVKEY_HEADER_PREFIX)93 return (true);94 // public-key RFC3110 ?95 // 'domain.com. IN KEY ...' or 'domain.com. IN DNSKEY ...'96 // skip any comment-lines97 if (typeof (buf) !== 'string') {98 buf = buf.toString('ascii');99 }100 var lines = buf.split('\n');101 var line = 0;102 /* JSSTYLED */103 while (lines[line].match(/^\;/))104 line++;105 if (lines[line].toString('ascii').match(/\. IN KEY /))106 return (true);107 if (lines[line].toString('ascii').match(/\. IN DNSKEY /))108 return (true);109 return (false);110}111function write(key, options) {112 throw (new Error('"auto" format cannot be used for writing'));...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var buf = new Buffer(256);2buf.fill(0);3var len = buf.write("Simply Easy Learning");4console.log("Octets written : "+ len);5var buf = new Buffer(26);6for (var i = 0 ; i < 26 ; i++) {7 buf[i] = i + 97;8}9var buf = new Buffer('Simply Easy Learning');10var json = buf.toJSON(buf);11console.log(json);12var buffer1 = new Buffer('TutorialsPoint ');13var buffer2 = new Buffer('Simply Easy Learning');14var buffer3 = Buffer.concat([buffer1,buffer2]);15console.log("buffer3 content: " + buffer3.toString());16var buffer1 = new Buffer('ABC');17var buffer2 = new Buffer('ABCD');18var result = buffer1.compare(buffer2);19if(result < 0) {20 console.log(buffer1 +" comes before " + buffer2);21}else if(result == 0){22 console.log(buffer1 +" is same as " + buffer2);23}else {24 console.log(buffer1 +" comes after " + buffer2);25}26var buffer1 = new Buffer('ABC');27var buffer2 = buffer1.slice(0,2);28console.log("buffer2 content: " + buffer2.toString());29var buffer1 = new Buffer('TutorialsPoint');30var buffer2 = new Buffer(3);31buffer1.copy(buffer2);32console.log("buffer2 content: " + buffer2.toString());

Full Screen

Using AI Code Generation

copy

Full Screen

1var buf = new Buffer(256);2buf.fill(0);3buf.write("hello world", 0);4buf.write("hello world", 0, "ascii");5buf.write("hello world", 0, "ascii", function (err, written, buffer) { });6buf.write("hello world", 0, "ascii", 0);7buf.write("hello world", 0, "ascii", 0, function (err, written, buffer) { });8buf.write("hello world", 0, 11);9buf.write("hello world", 0, 11, "ascii");10buf.write("hello world", 0, 11, "ascii", function (err, written, buffer) { });11buf.write("hello world", 0, 11, "ascii", 0);12buf.write("hello world", 0, 11, "ascii", 0, function (err, written, buffer) { });13buf.write("hello world", 0, 11, 0);14buf.write("hello world", 0, 11, 0, "ascii");15buf.write("hello world", 0, 11, 0, "ascii", function (err, written, buffer) { });16buf.write("hello world", 0, 11, 0, "ascii", 0);17buf.write("hello world", 0, 11, 0, "ascii", 0, function (err, written, buffer) { });18buf.write("hello world", 0, 11, 0, 11);19buf.write("hello world", 0, 11, 0, 11, "ascii");20buf.write("hello world", 0, 11, 0, 11, "ascii", function (err, written, buffer) { });21buf.write("hello world", 0, 11, 0, 11, "ascii", 0);22buf.write("hello world", 0, 11, 0, 11, "ascii", 0, function (err, written, buffer) { });23buf.write("hello world", 0, 11, 0, 11, 0);24buf.write("hello world", 0, 11, 0, 11, 0, "ascii");25buf.write("hello world", 0, 11, 0, 11,

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var fs = require('fs');3var api = new wpt('www.webpagetest.org');4var options = {location: 'Dulles:Chrome', runs: 1, video: 1};5var buf = new Buffer(1000000);6api.runTest(url, options, function(err, data) {7 if (err) return console.error(err);8 api.getVideo('video_' + data.data.testId + '.webm', buf, function(err, data) {9 if (err) return console.error(err);10 fs.writeFile('video.webm', buf, function(err) {11 if(err) {12 console.log(err);13 } else {14 console.log("The file was saved!");15 }16 });17 });18});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'A.f1a8d7d0c4e4c7a4a1e8d1d2e2c4e4e4');3var options = {4};5wpt.runTest(url, options, function(err, data) {6 if (err) return console.log(err);7 console.log(data);8});9var wpt = require('webpagetest');10var wpt = new WebPageTest('www.webpagetest.org', 'A.f1a8d7d0c4e4c7a4a1e8d1d2e2c4e4e4');11var options = {12};13wpt.runTest(url, options, function(err, data) {14 if (err) return console.log(err);15 console.log(data);16});17var wpt = require('webpagetest');18var wpt = new WebPageTest('www.webpagetest.org', 'A.f1a8d7d0c4e4c7a4a1e8d1d2e2c4e4e4');19var options = {

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful