diff --git a/lib/zgafetch.js b/lib/zgafetch.js index ccd9d9f..0f8392c 100644 --- a/lib/zgafetch.js +++ b/lib/zgafetch.js @@ -4,11 +4,6 @@ function supplyZgaUrlFetch(z){ //Only for nodejs Start// -const m_urlparser = require("url"); -const m_h = { - "http:": require('follow-redirects').http, - "https:": require('follow-redirects').https, -}; // @type {boolean} z.isNode = function(){return this === globalThis.global;}(); //Only for nodejs End// @@ -23,63 +18,107 @@ z.isBrowser = function(){return this === globalThis.self;}(); */ z.urlFetch = function(url, params){ + /** + * @return {Promise} + */ + var fetchfunc = async function(){ + /** @type {!RequestInit} */ + var reqinf = { + method: "GET", + redirect: "follow", + }; + if(params){ + if(params.payload){ + reqinf.body = params.payload; + } + if(params.headers){ + reqinf.headers = params.headers; + } + if(params.method){ + reqinf.method = params.method; + } + } + /** @type {Response} */ + var resp = await fetch(url, reqinf); + if(resp.ok){ + /** @type {ArrayBuffer} */ + var abdat = await resp.arrayBuffer(); + return new Uint8Array(abdat); + }else{ + /** @type {string} */ + var msg = await resp.text(); + throw new Error("Fetch failed." + resp.status + ": " + msg); + } + }; + //Only for nodejs Start// if(z.isNode){ - return new Promise(function(resolve, reject){ - // @type {URL} - var opts = m_urlparser.parse(url); - var http = m_h[opts.protocol]; - // @type {string|Buffer} - var dat = null; - var encoding = undefined; - opts.method = "GET"; - if(params){ - if(params.payload instanceof Buffer){ - dat = params.payload; - }else if(params.payload instanceof Uint8Array){ - dat = Buffer.from(params.payload.buffer); - }else if(params.payload instanceof ArrayBuffer){ - dat = Buffer.from(params.payload); - }else{ - dat = params.payload; - encoding = "binary"; + if(globalThis.fetch){ + console.log("Use built in fetch."); + return fetchfunc(); + }else{ + console.log("Use http module."); + const m_urlparser = require("url"); + const m_h = { + "http:": require('follow-redirects').http, + "https:": require('follow-redirects').https, + }; + return new Promise(function(resolve, reject){ + // @type {URL} + var opts = m_urlparser.parse(url); + var http = m_h[opts.protocol]; + // @type {string|Buffer} + var dat = null; + var encoding = undefined; + opts.method = "GET"; + if(params){ + if(params.payload instanceof Buffer){ + dat = params.payload; + }else if(params.payload instanceof Uint8Array){ + dat = Buffer.from(params.payload.buffer); + }else if(params.payload instanceof ArrayBuffer){ + dat = Buffer.from(params.payload); + }else{ + dat = params.payload; + encoding = "binary"; + } + if(params.headers){ + opts.headers = params.headers; + } + if(params.method){ + opts.method = params.method; + } + if(params.validateHttpsCertificates === false){ + opts.rejectUnauthorized = false; + } } - if(params.headers){ - opts.headers = params.headers; - } - if(params.method){ - opts.method = params.method; - } - if(params.validateHttpsCertificates === false){ - opts.rejectUnauthorized = false; - } - } - // @type {http.ClientRequest} - var hreq = http.request(opts, function(a_res){ // @type {http.IncomingMessage} a_res - if(a_res.statusCode !== 200){ - var a_err = new Error("Failed to request url. " + url + "\n Status Code: " + a_res.statusCode); - a_res.resume(); + // @type {http.ClientRequest} + var hreq = http.request(opts, function(a_res){ // @type {http.IncomingMessage} a_res + if(a_res.statusCode !== 200){ + var a_err = new Error("Failed to request url. " + url + "\n Status Code: " + a_res.statusCode); + a_res.resume(); + throw a_err; + } + // @type {Array} + var a_bufs = []; + var a_bufs_len = 0; + a_res.on("data", function(b_chunk){ // @type {Buffer} b_chunk + a_bufs.push(b_chunk); + a_bufs_len += b_chunk.length; + }); + a_res.on("end", function(){ + // @type {Buffer} + var b_bdat = Buffer.concat(a_bufs, a_bufs_len); + resolve(b_bdat); + }); + }); + hreq.on("error", function(a_err){ throw a_err; - } - // @type {Array} - var a_bufs = []; - var a_bufs_len = 0; - a_res.on("data", function(b_chunk){ // @type {Buffer} b_chunk - a_bufs.push(b_chunk); - a_bufs_len += b_chunk.length; - }); - a_res.on("end", function(){ - // @type {Buffer} - var b_bdat = Buffer.concat(a_bufs, a_bufs_len); - resolve(b_bdat); }); + hreq.end(dat, encoding); }); - hreq.on("error", function(a_err){ - throw a_err; - }); - hreq.end(dat, encoding); - }); + } } //Only for nodejs End// @@ -94,39 +133,7 @@ z.urlFetch = function(url, params){ // browser if(z.isBrowser && globalThis.self.fetch){ - /** - * @return {Promise} - */ - var func = async function(){ - /** @type {!RequestInit} */ - var reqinf = { - method: "GET", - redirect: "follow", - }; - if(params){ - if(params.payload){ - reqinf.body = params.payload; - } - if(params.headers){ - reqinf.headers = params.headers; - } - if(params.method){ - reqinf.method = params.method; - } - } - /** @type {Response} */ - var resp = await fetch(url, reqinf); - if(resp.ok){ - /** @type {ArrayBuffer} */ - var abdat = await resp.arrayBuffer(); - return new Uint8Array(abdat); - }else{ - /** @type {string} */ - var msg = await resp.text(); - throw new Error("Fetch failed." + resp.status + ": " + msg); - } - }; - return func(); + return fetchfunc(); } return null; }; diff --git a/lib/zganode.js b/lib/zganode.js index 4bd9024..c0c96cb 100644 --- a/lib/zganode.js +++ b/lib/zganode.js @@ -1,8 +1,3 @@ -const m_urlparser = require("url"); -const m_h = { - "http:": require('follow-redirects').http, - "https:": require('follow-redirects').https, -}; const z = require("./zgaindex.js"); z.forge = require("node-forge"); z.PDFLib = require("pdf-lib"); diff --git a/package.json b/package.json index 24648f0..861e401 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zgapdfsigner", - "version": "2.7.3", + "version": "2.7.5", "author": "zboris12", "description": "A javascript tool to sign a pdf or set protection to a pdf in web browser, Google Apps Script and nodejs.", "homepage": "https://github.com/zboris12/zgapdfsigner", @@ -32,7 +32,9 @@ ], "scripts": { "build": "./build.sh", - "test": "node test4node.js ${pfxpwd}" + "server": "node test4node.js webserver", + "test": "node test4node.js ${pfxpwd}", + "test2": "node test4node.js fetch" }, "dependencies": { "follow-redirects": "1.15.6", diff --git a/test.html b/test.html index 8350174..9facabc 100644 --- a/test.html +++ b/test.html @@ -81,6 +81,7 @@ async function testMe(){ sopt = { p12cert: pfx, pwd: ps, + // signdate: "/1", permission: parseInt(document.getElementById("sperm").value), reason: document.getElementById("tReason").value, location: document.getElementById("tLocation").value, @@ -189,6 +190,22 @@ function changeSperm(){ break; } } +// test urlFetch +async function main2(){ + /** @type {Uint8Array} */ + var u8arr = await Zga.urlFetch("http://localhost:8080", { + "headers": { + "testzb": "orange" + } + }); + // /** @type {string} */ + // var str = btoa(Zga.u8arrToRaw(u8arr)); + /** @type {TextDecoder} */ + var txtdec = new TextDecoder('utf-8'); + /** @type {string} */ + var str = txtdec.decode(u8arr); + console.log(str); +}