UNPKG

@sasjs/cli

Version:

Command line interface for SASjs

6 lines (5 loc) 8.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sasjsout = void 0; var sasjsout = function () { return "\n/* this macro is derived from: https://core.sasjs.io/mp__replace_8sas.html */\noptions mprint;\n%macro mp_replace(infile,\n findvar=,\n replacevar=,\n outfile=0\n)/*/STORE SOURCE*/;\n\n%local inref outref dttm ds1 rc;\n/* create filerefs */\n%let rc=%sysfunc(filename(inref,,temp,lrecl=200));\n%let rc=%sysfunc(filename(outref,,temp,lrecl=200));\n\n%if &outfile=0 %then %let outfile=&infile;\n\n/* uniquely named 32 char datasets */\n%let ds1=all%substr(%sysfunc(compress(%sysfunc(uuidgen()),-)),1,29);\n%let ds2=start%substr(%sysfunc(compress(%sysfunc(uuidgen()),-)),1,27);\n\n/* START */\n%let dttm=%sysfunc(datetime());\n\nfilename &inref &infile lrecl=1 recfm=n;\n\ndata &ds1;\n infile &inref;\n input sourcechar $char1. @@;\n format sourcechar hex2.;\nrun;\n\ndata &ds2;\n /* set find string to length in bytes to cover trailing spaces */\n length string $ %length(%superq(&findvar));\n string =symget(\"&findvar\");\n drop string;\n\n firstchar=char(string,1);\n findlen=lengthm(string); /* <- for trailing bytes */\n\n do _N_=1 to nobs;\n set &ds1 nobs=nobs point=_N_;\n if sourcechar=firstchar then do;\n pos=1;\n s=0;\n do point=_N_ to min(_N_ + findlen -1,nobs);\n set &ds1 point=point;\n if sourcechar=char(string, pos) then s + 1;\n else goto _leave_;\n pos+1;\n end;\n _leave_:\n if s=findlen then do;\n START =_N_;\n _N_ =_N_+ s - 1;\n STOP =_N_;\n output;\n end;\n end;\n end;\n stop;\n keep START STOP;\nrun;\n\ndata &ds1;\n declare hash HS(dataset:\"&ds2(keep=start)\");\n HS.defineKey(\"start\");\n HS.defineDone();\n declare hash HE(dataset:\"&ds2(keep=stop)\");\n HE.defineKey(\"stop\");\n HE.defineDone();\n do until(eof);\n set &ds1 end=eof curobs =n;\n start = ^HS.check(key:n);\n stop = ^HE.check(key:n);\n length strt $ 1;\n strt =put(start,best. -L);\n retain out 1;\n if out then output;\n if start then out=0;\n if stop then out=1;\n end;\n stop;\n keep sourcechar strt;\nrun;\n\nfilename &outref &outfile recfm=n;\n\ndata _null_;\n length replace $ %length(%superq(&replacevar));\n replace=symget(\"&replacevar\");\n file &outref;\n do until(eof);\n set &ds1 end=eof;\n if strt =\"1\" then put replace char.;\n else put sourcechar char1.;\n end;\n stop;\nrun;\n\n/* END */\n%put &sysmacroname took %sysevalf(%sysfunc(datetime())-&dttm) seconds to run;\n\n%mend mp_replace;\n\n%macro sasjsout(type,fref=sasjs);\n%global sysprocessmode SYS_JES_JOB_URI;\n\n%if &type=JS or &type=JS64 %then %do;\n %let rc=%sysfunc(stpsrv_header(\n Content-type,application/javascript%str(;)charset=UTF-8\n ));\n%end;\n%else %if &type=CSS or &type=CSS64 %then %do;\n %let rc=%sysfunc(stpsrv_header(Content-type,text/css));\n%end;\n%else %if &type=PNG or &type=GIF or &type=JPEG or &type=JPG %then %do;\n %let rc=%sysfunc(stpsrv_header(Content-type,image/%lowcase(&type)));\n%end;\n%else %if &type=SVG or &type=SVG64 %then %do;\n %let rc=%sysfunc(stpsrv_header(Content-type,image/svg+xml));\n%end;\n%else %if &type=ICO %then %do;\n %let rc=%sysfunc(stpsrv_header(Content-type,image/vnd.microsoft.icon));\n%end;\n%else %if &type=JSON %then %do;\n %let rc=%sysfunc(stpsrv_header(Content-type,application/json));\n%end;\n%else %if &type=MP3 %then %do;\n %let rc=%sysfunc(stpsrv_header(Content-type,audio/mpeg));\n%end;\n%else %if &type=WAV %then %do;\n %let rc=%sysfunc(stpsrv_header(Content-type,audio/x-wav));\n%end;\n%else %if &type=OGG %then %do;\n %let rc=%sysfunc(stpsrv_header(Content-type,audio/ogg));\n%end;\n%else %if &type=WOFF or &type=WOFF2 or &type=TTF %then %do;\n %let rc=%sysfunc(stpsrv_header(Content-type,font/%lowcase(&type)));\n%end;\n%else %if &type=MP4 %then %do;\n %let rc=%sysfunc(stpsrv_header(Content-type,video/mp4));\n%end;\n\n%if &type=HTML %then %do;\n /*\n We need to perform some substitutions -eg to get the APPLOC and SERVERTYPE.\n Therefore the developer should avoid writing lines that exceed 32k\n characters (eg base64 encoded images) as they will get truncated when passing\n through the datastep. This could of course be re-written using LUA, removing\n the length restriction. Pull requests are welcome!\n */\n filename _sjsout temp;\n data _null_;\n file _sjsout lrecl=32767 encoding='utf-8';\n infile &fref lrecl=32767;\n input;\n length appLoc expanded_path $1048;\n if _n_=1 then do;\n retain pgm \"&_program\" appLoc expanded_path;\n /* index is deployed in the /services/ folder under the appLoc */\n appLoc=substr(pgm,1,find(pgm,'/services/')-1);\n expanded_path=cats('?_PROGRAM=',appLoc,'/services/');\n end;\n if find(_infile_,' appLoc: ') then put ' appLoc: ' apploc:$quote1048. ',';\n else if find(_infile_,' serverType: ') then do;\n put ' serverType: \"SAS9\" ,';\n end;\n else if find(_infile_,' serverUrl: ') then do;\n /* nothing - we are streaming, so remove to default as hostname */\n end;\n else do;\n /* More recently, SASjs apps avoid inline JS to allow strict CSP */\n length infile in1 in2 $32767;\n infile=cats(_infile_);\n spos1=index(upcase(infile),'APPLOC=\"');\n if spos1>0 then do;\n in1=substr(infile,1,spos1+7);\n in2=subpad(infile,spos1+8);\n in2=substr(in2,index(in2,'\"'));\n infile=cats(in1,appLoc,in2);\n putlog \"new apploc: \" infile=;\n end;\n /* find & replace serverType in HTML attributes */\n spos2=index(upcase(infile),'SERVERTYPE=\"');\n if spos2>0 then do;\n in1=substr(infile,1,spos2+11);\n in2=subpad(infile,spos2+12);\n in2=substr(in2,index(in2,'\"'));\n infile=cats(in1,\"SAS9\",in2);\n putlog \"new servertype: \" infile=;\n end;\n /* find & replace serverUrl in HTML attributes */\n spos3=index(upcase(infile),'SERVERURL=\"');\n if spos3>0 then do;\n in1=substr(infile,1,spos3+10);\n in2=subpad(infile,spos3+11);\n in2=substr(in2,index(in2,'\"'));\n infile=cats(in1,in2);\n putlog \"new serverUrl: \" infile=;\n end;\n if sum(spos1,spos2,spos3)>0 then put infile;\n else do;\n /* during SAS9 sasjs compile, dependencies get static GUID */\n infile=tranwrd(_infile_,'?_PROGRAM=".concat(process.sasjsConstants.sas9GUID, "',cats(expanded_path));\n put infile;\n end;\n end;\n run;\n %let fref=_sjsout;\n%end;\n%else %if &type=JS64 or &type=CSS64 %then %do;\n options nobomfile;\n %let fvar=").concat(process.sasjsConstants.sas9GUID, ";\n %let stgfile=\"%sysfunc(pathname(work))/stgfile.txt\";\n %let newfile=\"%sysfunc(pathname(work))/newfile.txt\";\n\n /* convert from base64 */\n filename _out64 &stgfile encoding='utf-8';\n data _null_;\n length filein 8 fileout 8;\n filein = fopen(\"&fref\",'I',4,'B');\n fileout = fopen(\"_out64\",'A',1,'B');\n char= '20'x;\n do while(fread(filein)=0);\n length raw $4 ;\n do i=1 to 4;\n rc=fget(filein,char,1);\n substr(raw,i,1)=char;\n end;\n rc = fput(fileout, input(raw,$base64X4.));\n rc =fwrite(fileout);\n end;\n rc = fclose(filein);\n rc = fclose(fileout);\n run;\n\n /* get appLoc from _program */\n data _null_;\n pgm=\"&_program\";\n appLoc=substr(pgm,1,find(pgm,'/services/')-1);\n call symputx('apploc',cats(appLoc,'/services/'));\n run;\n %put &=fvar;\n %put &=apploc;\n /* replace the GUID with appLoc */\n %mp_replace(&stgfile,\n findvar=fvar,\n replacevar=apploc,\n outfile=&newfile\n )\n filename &fref &newfile encoding='utf-8';\n%end;\n\n\n/**\n * In SAS9, JS & CSS files are base64 encoded to avoid UTF8 issues in WLATIN1\n * metadata - so in this case, decode and stream byte by byte.\n * */\n%if &type=GIF or &type=PNG or &type=JPG or &type=JPEG or &type=ICO or &type=MP3\nor &type=WAV or &type=OGG or &type=WOFF or &type=WOFF2 or &type=TTF or &type=MP4\nor &type=SVG64\n%then %do;\n data _null_;\n length filein 8 fileout 8;\n filein = fopen(\"&fref\",'I',4,'B');\n fileout = fopen(\"_webout\",'A',1,'B');\n char = '20'x;\n do while(fread(filein)=0);\n length raw $4 ;\n do i=1 to 4;\n rc=fget(filein,char,1);\n substr(raw,i,1)=char;\n end;\n rc = fput(fileout, input(raw,$base64X4.));\n rc = fwrite(fileout);\n end;\n rc = fclose(filein);\n rc = fclose(fileout);\n run;\n%end;\n%else %do;\n %put streaming to _webout;\n data _null_;\n infile &fref lrecl=1 recfm=n;\n file _webout mod lrecl=1 recfm=n;\n input sourcechar $char1. @@;\n format sourcechar hex2.;\n put sourcechar char1. @@;\n run;\n%end;\n%mend sasjsout;"); }; exports.sasjsout = sasjsout;