restream
Version:
Regular Expression Detection & Replacement streams.
28 lines (22 loc) • 5.62 kB
JavaScript
const stream = require('stream');
const os = require('os'); var n=stream;const p=stream.Transform,q=stream.Writable;function r(a){if("object"!=typeof a)return!1;const b=a.re instanceof RegExp;a=-1!=["string","function"].indexOf(typeof a.replacement);return b&&a}const t=(a,b)=>{if(!(b instanceof Error))throw b;[,,a]=a.stack.split("\n",3);a=b.stack.indexOf(a);if(-1==a)throw b;a=b.stack.substr(0,a-1);const c=a.lastIndexOf("\n");b.stack=a.substr(0,c);throw b;};function u(a,b){function c(){Array.isArray(b)||(b=[b]);return b.filter(r).reduce((d,{re:e,replacement:f})=>{if(this.a)return d;if("string"==typeof f)return d=d.replace(e,f);{let g;return d.replace(e,(h,...l)=>{g=Error();try{return this.a?h:f.call(this,h,...l)}catch(k){t(g,k)}})}},`${a}`)}c.brake=()=>{c.a=!0};return c.call(c)};const v=a=>new RegExp(`%%_RESTREAM_${a.toUpperCase()}_REPLACEMENT_(\\d+)_%%`,"g"),w=(a,b)=>`%%_RESTREAM_${a.toUpperCase()}_REPLACEMENT_${b}_%%`;const x=os.homedir;const y=/\s+at.*(?:\(|\s)(.*)\)?/,z=/^(?:(?:(?:node|(?:internal\/[\w/]*|.*node_modules\/(?:IGNORED_MODULES)\/.*)?\w+)\.js:\d+:\d+)|native)/,A=a=>{const {pretty:b=!1,ignoredModules:c=["pirates","@artdeco/pirates"]}={},d=new RegExp(z.source.replace("IGNORED_MODULES",c.join("|")));return a.replace(/\\/g,"/").split("\n").filter(e=>{e=e.match(y);if(null===e||!e[1])return!0;e=e[1];return e.includes(".app/Contents/Resources/electron.asar")||e.includes(".app/Contents/Resources/default_app.asar")?!1:!d.test(e)}).filter(e=>
e.trim()).map(e=>{if(b){const f=x().replace(/\\/g,"/");return e.replace(/\s+at.*(?:\(|\s)(.*)\)?/,(g,h)=>g.replace(h,h.replace(f,"~")))}return e}).join("\n")};const B=(a,b=0,c=!1)=>{if(0===b&&!c)return a;a=a.split("\n",c?b+1:void 0);return c?a[a.length-1]:a.slice(b).join("\n")},C=(a,b=!1)=>B(a,2+(b?1:0)),D=a=>{({callee:{caller:a}}=a);return a};function E(a,b,c=!1){return function(d){var e=D(arguments),{stack:f}=Error();const g=B(f,2,!0),h=(f=d instanceof Error)?d.message:d;e=[`Error: ${h}`,...null!==e&&a===e||c?[b]:[g,b]].join("\n");e=A(e);return Object.assign(f?d:Error(),{message:h,stack:e})}};function F(a){var {stack:b}=Error();const c=D(arguments);b=C(b,a);return E(c,b,a)};const G=(a,b)=>{b.once("error",c=>{a.emit("error",c)});return b};class H extends q{constructor(a){const {binary:b=!1,rs:c=null,...d}=a||{},{h:e=F(!0),proxyError:f}=a||{},g=(h,l)=>e(l);super(d);this.b=[];this.f=new Promise((h,l)=>{this.on("finish",()=>{let k;b?k=Buffer.concat(this.b):k=this.b.join("");h(k);this.b=[]});this.once("error",k=>{if(-1==k.stack.indexOf("\n"))g`${k}`;else{const m=A(k.stack);k.stack=m;f&&g`${k}`}l(k)});c&&G(this,c).pipe(this)})}_write(a,b,c){this.b.push(a);c()}get c(){return this.f}}const I=async a=>{({c:a}=new H({rs:a,h:F(!0)}));return await a};class J extends p{constructor(a,b){super(b)}brake(){}async replace(){}};async function K(a,b){return L(a,b)}async function M(a,b){return await a.b.reduce(async(c,{re:d,replacement:e})=>{c=await c;if(a.a)return c;if("string"==typeof e)c=c.replace(d,e);else{const f=[];let g;const h=c.replace(d,(l,...k)=>{g=Error();try{if(a.a)return f.length?f.push(Promise.resolve(l)):l;const m=e.call(a,l,...k);m instanceof Promise&&f.push(m);return m}catch(m){t(g,m)}});if(f.length)try{const l=await Promise.all(f);c=c.replace(d,()=>l.shift())}catch(l){t(g,l)}else c=h}return c},`${b}`)}
class N extends J{constructor(a,b){super(a,b);this.b=(Array.isArray(a)?a:[a]).filter(r);this.a=!1;this.g=b}async _transform(a,b,c){try{const d=await M(this,a);this.push(d);c()}catch(d){a=A(d.stack),d.stack=a,c(d)}}}N.replace=K;
{var O=N,P=function(){}.prototype={brake(){this.a=!0},async replace(b,c){const d=new N(this.b,this.g);c&&Object.assign(d,c);b=await K(d,b);d.a&&this.brake();c&&Object.keys(c).forEach(e=>{c[e]=d[e]});return b}};Object.setPrototypeOf(P,J.prototype);const a=Object.getOwnPropertyDescriptors(P);Object.defineProperties(O.prototype,a)}async function L(a,b){b instanceof n?b.pipe(a):a.end(b);return await I(a)}
class Q extends N{constructor(a){super(a);this.c=Promise.resolve()}addItem(a){return this.c=a=this.c.then(a)}};/*
restream: Regular Expression Detection & Replacement Streams.
Copyright (C) 2020 Art Deco Code Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
module.exports={_Replaceable:N,_SyncReplaceable:u,_SerialAsyncReplaceable:Q,_restream:function(a){let b="";const c=new p({transform(d,e,f){let g;for(b+=d.toString();(d=a.exec(b))&&(c.push(d),g=d,a.global););g&&(b=b.slice(g.index+g[0].length));f()},objectMode:!0});return c},_replace:L,_makePasteRule:(a,b=[])=>{const c=a.map;return{re:a.regExp,replacement(d,e){d=c[e];delete c[e];return u(d,Array.isArray(b)?b:[b])}}},_makeCutRule:a=>{const b=a.map,c=a.getReplacement,d=a.name;return{re:a.re,replacement(e){const f=
a.lastIndex;b[f]=e;a.lastIndex+=1;return c(d,f)}}},_makeMarkers:(a,b)=>Object.keys(a).reduce((c,d)=>{{var e=a[d];const {getReplacement:f=w,getRegex:g=v}=b||{},h=g(d);e={name:d,re:e,regExp:h,getReplacement:f,map:{},lastIndex:0}}return{...c,[d]:e}},{})};