Compare commits
6 Commits
Author | SHA1 | Date |
---|---|---|
![]() |
45995fd2ae | |
![]() |
bb8faab7d7 | |
![]() |
66bec77187 | |
![]() |
32a3b5f1e0 | |
![]() |
4bdbeabaa2 | |
![]() |
d427170408 |
|
@ -4,11 +4,6 @@
|
||||||
function supplyZgaUrlFetch(z){
|
function supplyZgaUrlFetch(z){
|
||||||
|
|
||||||
//Only for nodejs Start//
|
//Only for nodejs Start//
|
||||||
const m_urlparser = require("url");
|
|
||||||
const m_h = {
|
|
||||||
"http:": require('follow-redirects').http,
|
|
||||||
"https:": require('follow-redirects').https,
|
|
||||||
};
|
|
||||||
// @type {boolean}
|
// @type {boolean}
|
||||||
z.isNode = function(){return this === globalThis.global;}();
|
z.isNode = function(){return this === globalThis.global;}();
|
||||||
//Only for nodejs End//
|
//Only for nodejs End//
|
||||||
|
@ -23,8 +18,51 @@ z.isBrowser = function(){return this === globalThis.self;}();
|
||||||
*/
|
*/
|
||||||
z.urlFetch = function(url, params){
|
z.urlFetch = function(url, params){
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return {Promise<Uint8Array>}
|
||||||
|
*/
|
||||||
|
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//
|
//Only for nodejs Start//
|
||||||
if(z.isNode){
|
if(z.isNode){
|
||||||
|
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){
|
return new Promise(function(resolve, reject){
|
||||||
// @type {URL}
|
// @type {URL}
|
||||||
var opts = m_urlparser.parse(url);
|
var opts = m_urlparser.parse(url);
|
||||||
|
@ -81,6 +119,7 @@ z.urlFetch = function(url, params){
|
||||||
hreq.end(dat, encoding);
|
hreq.end(dat, encoding);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
//Only for nodejs End//
|
//Only for nodejs End//
|
||||||
|
|
||||||
// Google Apps Script
|
// Google Apps Script
|
||||||
|
@ -94,39 +133,7 @@ z.urlFetch = function(url, params){
|
||||||
|
|
||||||
// browser
|
// browser
|
||||||
if(z.isBrowser && globalThis.self.fetch){
|
if(z.isBrowser && globalThis.self.fetch){
|
||||||
/**
|
return fetchfunc();
|
||||||
* @return {Promise<Uint8Array>}
|
|
||||||
*/
|
|
||||||
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 null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
|
@ -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");
|
const z = require("./zgaindex.js");
|
||||||
z.forge = require("node-forge");
|
z.forge = require("node-forge");
|
||||||
z.PDFLib = require("pdf-lib");
|
z.PDFLib = require("pdf-lib");
|
||||||
|
|
|
@ -60,7 +60,8 @@ z.Crypto = {
|
||||||
},
|
},
|
||||||
|
|
||||||
/** @type {string} */
|
/** @type {string} */
|
||||||
EncPadding: "\x28\xBF\x4E\x5E\x4E\x75\x8A\x41\x64\x00\x4E\x56\xFF\xFA\x01\x08\x2E\x2E\x00\xB6\xD0\x68\x3E\x80\x2F\x0C\xA9\xFE\x64\x53\x69\x7A",
|
// EncPadding: "\x28\xBF\x4E\x5E\x4E\x75\x8A\x41\x64\x00\x4E\x56\xFF\xFA\x01\x08\x2E\x2E\x00\xB6\xD0\x68\x3E\x80\x2F\x0C\xA9\xFE\x64\x53\x69\x7A",
|
||||||
|
EncPadding: atob('KL9OXk51ikFkAE5W//oBCC4uALbQaD6ALwyp/mRTaXo='),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add "\" before "\", "(" and ")", and chr(13) => '\r'
|
* Add "\" before "\", "(" and ")", and chr(13) => '\r'
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "zgapdfsigner",
|
"name": "zgapdfsigner",
|
||||||
"version": "2.7.3",
|
"version": "2.7.5",
|
||||||
"author": "zboris12",
|
"author": "zboris12",
|
||||||
"description": "A javascript tool to sign a pdf or set protection to a pdf in web browser, Google Apps Script and nodejs.",
|
"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",
|
"homepage": "https://github.com/zboris12/zgapdfsigner",
|
||||||
|
@ -32,7 +32,9 @@
|
||||||
],
|
],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "./build.sh",
|
"build": "./build.sh",
|
||||||
"test": "node test4node.js ${pfxpwd}"
|
"server": "node test4node.js webserver",
|
||||||
|
"test": "node test4node.js ${pfxpwd}",
|
||||||
|
"test2": "node test4node.js fetch"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"follow-redirects": "1.15.6",
|
"follow-redirects": "1.15.6",
|
||||||
|
|
17
test.html
17
test.html
|
@ -81,6 +81,7 @@ async function testMe(){
|
||||||
sopt = {
|
sopt = {
|
||||||
p12cert: pfx,
|
p12cert: pfx,
|
||||||
pwd: ps,
|
pwd: ps,
|
||||||
|
// signdate: "/1",
|
||||||
permission: parseInt(document.getElementById("sperm").value),
|
permission: parseInt(document.getElementById("sperm").value),
|
||||||
reason: document.getElementById("tReason").value,
|
reason: document.getElementById("tReason").value,
|
||||||
location: document.getElementById("tLocation").value,
|
location: document.getElementById("tLocation").value,
|
||||||
|
@ -189,6 +190,22 @@ function changeSperm(){
|
||||||
break;
|
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);
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
<style>
|
<style>
|
||||||
body {
|
body {
|
||||||
|
|
55
test4node.js
55
test4node.js
|
@ -187,6 +187,55 @@ async function main1(angle){
|
||||||
console.log("Done");
|
console.log("Done");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// test urlFetch
|
||||||
|
async function main2(){
|
||||||
|
/** @type {Uint8Array} */
|
||||||
|
var u8arr = await Zga.urlFetch("http://localhost:8080", {
|
||||||
|
"headers": {
|
||||||
|
"testzb": "pineapple"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// /** @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);
|
||||||
|
}
|
||||||
|
|
||||||
|
function webserver(){
|
||||||
|
require("http").createServer(function(req, res){
|
||||||
|
if(req.method == "GET"){
|
||||||
|
if(req.headers["testzb"]){
|
||||||
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||||
|
if(req.url == "/"){
|
||||||
|
console.log(req.headers["testzb"]);
|
||||||
|
res.writeHead(302, {"Location": "/testzb"});
|
||||||
|
res.end();
|
||||||
|
}else if(req.url == "/testzb"){
|
||||||
|
res.writeHead(200, {"Content-Type": "text/plain"});
|
||||||
|
res.end("I am redirected!\n");
|
||||||
|
}else{
|
||||||
|
res.statusCode = 500;
|
||||||
|
res.statusMessage = "Bad Request.";
|
||||||
|
res.end();
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
res.writeHead(200, {"Content-Type": "text/plain"});
|
||||||
|
res.end("Hello World\n");
|
||||||
|
}
|
||||||
|
}else if(req.method == "OPTIONS"){
|
||||||
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||||
|
res.setHeader("Access-Control-Allow-Method", "GET, OPTIONS, HEAD");
|
||||||
|
res.setHeader("Access-Control-Allow-Headers", "Accept, Accept-Language, Content-Language, Content-Type, Range, testzb");
|
||||||
|
res.statusCode = 200;
|
||||||
|
res.statusMessage = "CORS OK";
|
||||||
|
res.end();
|
||||||
|
}
|
||||||
|
}).listen(8080, function(){console.log("Server http://localhost:8080")});
|
||||||
|
}
|
||||||
|
|
||||||
async function main(){
|
async function main(){
|
||||||
/** @type {Array<number>} */
|
/** @type {Array<number>} */
|
||||||
var arr = [0, 90, 180, 270];
|
var arr = [0, 90, 180, 270];
|
||||||
|
@ -197,4 +246,10 @@ async function main(){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(process.argv[2] == "webserver"){
|
||||||
|
webserver();
|
||||||
|
}else if(process.argv[2] == "fetch"){
|
||||||
|
main2();
|
||||||
|
}else{
|
||||||
main();
|
main();
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue