@stdlib/esm
Version:
ES module distribution for Stdlib, a standard library for JavaScript and Node.js.
3 lines • 4.37 kB
JavaScript
// This file is a part of stdlib. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0
import e from"./../../utils/define-nonenumerable-read-only-property.js";import i from"./../../vendor/process.js";import r from"./../../vendor/readable-stream.js";import t from"./../../assert/has-own-property.js";import o from"./../../assert/is-iterator-like.js";import s from"./../../assert/is-error.js";import n from"./../../assert/is-buffer.js";import a from"./../../utils/copy.js";import p from"./../../utils/inherit.js";import m from"./../../utils/define-nonenumerable-property.js";import u from"./../../buffer/from-string.js";import l from"./../../buffer/ctor.js";import f from"./../../assert/is-plain-object.js";import h from"./../../assert/is-boolean.js";import d from"./../../assert/is-nonnegative-number.js";import c from"./../../assert/is-string.js";import g from"./../../assert/is-function.js";import b from"./../../vendor/debug.js";var j=!1,v=null,y="\n",_={objectMode:j,encoding:v,sep:y},w=f,M=t,z=h.isPrimitive,O=d.isPrimitive,E=c.isPrimitive,T=g;function k(e,i){return w(i)?M(i,"sep")&&(e.sep=i.sep,!E(e.sep))?new TypeError("invalid option. `sep` option must be a primitive string. Option: `"+e.sep+"`."):M(i,"objectMode")&&(e.objectMode=i.objectMode,!z(e.objectMode))?new TypeError("invalid option. `objectMode` option must be a primitive boolean. Option: `"+e.objectMode+"`."):M(i,"encoding")&&(e.encoding=i.encoding,!E(e.encoding)&&null!==e.encoding)?new TypeError("invalid option. `encoding` option must be a primitive string or null. Option: `"+e.encoding+"`."):M(i,"highWaterMark")&&(e.highWaterMark=i.highWaterMark,!O(e.highWaterMark))?new TypeError("invalid option. `highWaterMark` option must be a nonnegative number. Option: `"+e.highWaterMark+"`."):M(i,"serialize")&&(e.serialize=i.serialize,!T(e.serialize))?new TypeError("invalid option. `serialize` option must be a function. Option: `"+e.serialize+"`."):null:new TypeError("invalid argument. Options must be an object. Value: `"+i+"`.")}var S=b,V=S("from-iterator-stream"),W=r.Readable,J=t,N=o,x=s,P=n,B=a,C=p,F=m,I=e,A=u,R=l,q=_,D=k,G=V;function H(){var e,i,r;if(!this._destroyed)for(e=!0;e;){if(r=this._iterator.next(),this._i+=1,r.done)return J(r,"value")&&(r=r.value,G("Value: %s. Iter: %d.",JSON.stringify(r),this._i),!1===this._objectMode&&("string"==typeof(r=this._serialize(r))?(r=1===this._i?A(r):A(this._sep+r),this.push(r)):P(r)?(this._i>1&&(r=R.concat([A(this._sep),r])),this.push(r)):(i=new Error("invalid operation. Serialization function must return a string or Buffer. Value: `"+r+"`."),this.emit("error",i)))),G("Finished iteration."),this.push(null);if(r=r.value,G("Value: %s. Iter: %d.",JSON.stringify(r),this._i),!1===this._objectMode)if("string"==typeof(r=this._serialize(r)))r=1===this._i?A(r):A(this._sep+r);else{if(!P(r)){i=new Error("invalid operation. Serialization function must return a string or Buffer. Value: `"+r+"`."),this.emit("error",i);continue}this._i>1&&(r=R.concat([A(this._sep),r]))}e=this.push(r)}}function K(e){var r;return this._destroyed?(G("Attempted to destroy an already destroyed stream."),this):(r=this,this._destroyed=!0,i.nextTick((function(){e&&(G("Stream was destroyed due to an error. Error: %s.",x(e)?e.message:JSON.stringify(e)),r.emit("error",e));G("Closing the stream..."),r.emit("close")})),this)}function L(e,i){var r,t;if(!(this instanceof L))return arguments.length>1?new L(e,i):new L(e);if(!N(e))throw new TypeError("invalid argument. First argument must be an iterator. Value: `"+e+"`.");if(r=B(q),arguments.length>1&&(t=D(r,i)))throw t;return G("Creating a readable stream configured with the following options: %s.",JSON.stringify(r)),W.call(this,r),F(this,"_destroyed",!1),I(this,"_objectMode",r.objectMode),I(this,"_sep",r.sep),I(this,"_serialize",r.serialize||JSON.stringify),I(this,"_iterator",e),F(this,"_i",0),this}C(L,W),I(L.prototype,"_read",H),I(L.prototype,"destroy",K);var Q=f,U=a,X=L;function Y(e,i){var r;if(arguments.length>1){if(!Q(r=i))throw new TypeError("invalid argument. Options must be an object. Value: `"+r+"`.");r=U(i,1)}else r={};return r.objectMode=!0,new X(e,r)}var Z=a,$=L;function ee(e){var i;return i=arguments.length?Z(e,1):{},r;function r(e){return new $(e,i)}}var ie=e,re=L,te=Y,oe=ee;ie(re,"objectMode",te),ie(re,"factory",oe);export default re;
//# sourceMappingURL=from-iterator.js.map