11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci'use strict'; 231cb0ef41Sopenharmony_ciconst common = require('../common'); 241cb0ef41Sopenharmony_ciconst assert = require('assert'); 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ciconst stream = require('stream'); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ciconst queue = []; 291cb0ef41Sopenharmony_cifor (let decode = 0; decode < 2; decode++) { 301cb0ef41Sopenharmony_ci for (let uncork = 0; uncork < 2; uncork++) { 311cb0ef41Sopenharmony_ci for (let multi = 0; multi < 2; multi++) { 321cb0ef41Sopenharmony_ci queue.push([!!decode, !!uncork, !!multi]); 331cb0ef41Sopenharmony_ci } 341cb0ef41Sopenharmony_ci } 351cb0ef41Sopenharmony_ci} 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_cirun(); 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_cifunction run() { 401cb0ef41Sopenharmony_ci const t = queue.pop(); 411cb0ef41Sopenharmony_ci if (t) 421cb0ef41Sopenharmony_ci test(t[0], t[1], t[2], run); 431cb0ef41Sopenharmony_ci else 441cb0ef41Sopenharmony_ci console.log('ok'); 451cb0ef41Sopenharmony_ci} 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_cifunction test(decode, uncork, multi, next) { 481cb0ef41Sopenharmony_ci console.log(`# decode=${decode} uncork=${uncork} multi=${multi}`); 491cb0ef41Sopenharmony_ci let counter = 0; 501cb0ef41Sopenharmony_ci let expectCount = 0; 511cb0ef41Sopenharmony_ci function cnt(msg) { 521cb0ef41Sopenharmony_ci expectCount++; 531cb0ef41Sopenharmony_ci const expect = expectCount; 541cb0ef41Sopenharmony_ci return function(er) { 551cb0ef41Sopenharmony_ci assert.ifError(er); 561cb0ef41Sopenharmony_ci counter++; 571cb0ef41Sopenharmony_ci assert.strictEqual(counter, expect); 581cb0ef41Sopenharmony_ci }; 591cb0ef41Sopenharmony_ci } 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci const w = new stream.Writable({ decodeStrings: decode }); 621cb0ef41Sopenharmony_ci w._write = common.mustNotCall('Should not call _write'); 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci const expectChunks = decode ? [ 651cb0ef41Sopenharmony_ci { encoding: 'buffer', 661cb0ef41Sopenharmony_ci chunk: [104, 101, 108, 108, 111, 44, 32] }, 671cb0ef41Sopenharmony_ci { encoding: 'buffer', 681cb0ef41Sopenharmony_ci chunk: [119, 111, 114, 108, 100] }, 691cb0ef41Sopenharmony_ci { encoding: 'buffer', 701cb0ef41Sopenharmony_ci chunk: [33] }, 711cb0ef41Sopenharmony_ci { encoding: 'buffer', 721cb0ef41Sopenharmony_ci chunk: [10, 97, 110, 100, 32, 116, 104, 101, 110, 46, 46, 46] }, 731cb0ef41Sopenharmony_ci { encoding: 'buffer', 741cb0ef41Sopenharmony_ci chunk: [250, 206, 190, 167, 222, 173, 190, 239, 222, 202, 251, 173] }, 751cb0ef41Sopenharmony_ci ] : [ 761cb0ef41Sopenharmony_ci { encoding: 'ascii', chunk: 'hello, ' }, 771cb0ef41Sopenharmony_ci { encoding: 'utf8', chunk: 'world' }, 781cb0ef41Sopenharmony_ci { encoding: 'buffer', chunk: [33] }, 791cb0ef41Sopenharmony_ci { encoding: 'latin1', chunk: '\nand then...' }, 801cb0ef41Sopenharmony_ci { encoding: 'hex', chunk: 'facebea7deadbeefdecafbad' }, 811cb0ef41Sopenharmony_ci ]; 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci let actualChunks; 841cb0ef41Sopenharmony_ci w._writev = function(chunks, cb) { 851cb0ef41Sopenharmony_ci actualChunks = chunks.map(function(chunk) { 861cb0ef41Sopenharmony_ci return { 871cb0ef41Sopenharmony_ci encoding: chunk.encoding, 881cb0ef41Sopenharmony_ci chunk: Buffer.isBuffer(chunk.chunk) ? 891cb0ef41Sopenharmony_ci Array.prototype.slice.call(chunk.chunk) : chunk.chunk 901cb0ef41Sopenharmony_ci }; 911cb0ef41Sopenharmony_ci }); 921cb0ef41Sopenharmony_ci cb(); 931cb0ef41Sopenharmony_ci }; 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ci w.cork(); 961cb0ef41Sopenharmony_ci w.write('hello, ', 'ascii', cnt('hello')); 971cb0ef41Sopenharmony_ci w.write('world', 'utf8', cnt('world')); 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci if (multi) 1001cb0ef41Sopenharmony_ci w.cork(); 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci w.write(Buffer.from('!'), 'buffer', cnt('!')); 1031cb0ef41Sopenharmony_ci w.write('\nand then...', 'latin1', cnt('and then')); 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ci if (multi) 1061cb0ef41Sopenharmony_ci w.uncork(); 1071cb0ef41Sopenharmony_ci 1081cb0ef41Sopenharmony_ci w.write('facebea7deadbeefdecafbad', 'hex', cnt('hex')); 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ci if (uncork) 1111cb0ef41Sopenharmony_ci w.uncork(); 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci w.end(cnt('end')); 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ci w.on('finish', function() { 1161cb0ef41Sopenharmony_ci // Make sure finish comes after all the write cb 1171cb0ef41Sopenharmony_ci cnt('finish')(); 1181cb0ef41Sopenharmony_ci assert.deepStrictEqual(actualChunks, expectChunks); 1191cb0ef41Sopenharmony_ci next(); 1201cb0ef41Sopenharmony_ci }); 1211cb0ef41Sopenharmony_ci} 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ci{ 1241cb0ef41Sopenharmony_ci const w = new stream.Writable({ 1251cb0ef41Sopenharmony_ci writev: common.mustCall(function(chunks, cb) { 1261cb0ef41Sopenharmony_ci cb(); 1271cb0ef41Sopenharmony_ci }) 1281cb0ef41Sopenharmony_ci }); 1291cb0ef41Sopenharmony_ci w.write('asd', common.mustCall()); 1301cb0ef41Sopenharmony_ci} 131