Compare commits

..

6 Commits
2.7.4 ... main

Author SHA1 Message Date
zboris12 45995fd2ae
Merge pull request #12 from Th3G33k/main
fix character encoding + fix signAndProtect
2025-09-28 16:27:01 +09:00
Th3G33k bb8faab7d7
Update zgapdfsigner.js - revert commit 2025-09-23 15:12:28 -10:00
zboris12 66bec77187 In Node.js mode, changed it to use Node.js' built-in fetch method if available.
And this may also solve issue #11.
2025-09-13 20:13:00 +09:00
Th3G33k 32a3b5f1e0
Update zgapdfsigner.js - fix signAndProtect 2025-09-08 08:59:30 -10:00
Th3G33k 4bdbeabaa2
Update zgapdfsigner.js - fix signAndProtect 2025-09-08 08:57:06 -10:00
Th3G33k d427170408
Update zgapdfcryptor.js - fix character encoding 2025-09-08 08:49:20 -10:00
6 changed files with 174 additions and 97 deletions

View File

@ -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,8 +18,51 @@ z.isBrowser = function(){return this === globalThis.self;}();
*/
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//
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){
// @type {URL}
var opts = m_urlparser.parse(url);
@ -81,6 +119,7 @@ z.urlFetch = function(url, params){
hreq.end(dat, encoding);
});
}
}
//Only for nodejs End//
// Google Apps Script
@ -94,39 +133,7 @@ z.urlFetch = function(url, params){
// browser
if(z.isBrowser && globalThis.self.fetch){
/**
* @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 fetchfunc();
}
return null;
};

View File

@ -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");

View File

@ -60,7 +60,8 @@ z.Crypto = {
},
/** @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'

View File

@ -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",

View File

@ -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);
}
</script>
<style>
body {

View File

@ -187,6 +187,55 @@ async function main1(angle){
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(){
/** @type {Array<number>} */
var arr = [0, 90, 180, 270];
@ -197,4 +246,10 @@ async function main(){
}
}
main();
if(process.argv[2] == "webserver"){
webserver();
}else if(process.argv[2] == "fetch"){
main2();
}else{
main();
}