bundling node-forge
work in progress
please find me in
for more discussion
here is some code for generating https self signed certificate and key
import nodeForge from 'https://cdn.jsdelivr.net/npm/node-forge/+esm';
//var nodeForge = require('node-forge');
var pki = nodeForge.pki;
var today = new Date();
var one_year = new Date();
one_year.setFullYear(one_year.getFullYear()+1);
var config = {};
config.from = today;
config.to = one_year;
config.dns = ['localhost'];
config.ip = ['127.0.0.1','127.0.0.2'];
config.rdn = {};
config.rdn.cn = 'localhost test certificate';
config.rdn.o = '';
config.rdn.ou = '';
config.rdn.l = '';
config.rdn.st = '';
config.rdn.c = '';
var attrs = [];
for(var name in config.rdn){
attrs.push({shortName:name.toUpperCase(),value:config.rdn[name]});
}//for
var altNames = [config.dns.map(dns=>{return {type:2,value:dns}}),config.ip.map(ip=>{return {type:7,ip}})].flat();
var keys = pki.rsa.generateKeyPair(2048);
var cert = pki.createCertificate();
cert.publicKey = keys.publicKey;
cert.serialNumber = '01';
cert.validity.notBefore = config.from;
cert.validity.notAfter = config.to;
cert.setSubject(attrs);
cert.setIssuer(attrs);
cert.setExtensions([
{
name : 'basicConstraints',
cA : false
},
{
name : 'keyUsage',
keyCertSign : true,
digitalSignature : true,
nonRepudiation : true,
keyEncipherment : true,
dataEncipherment : true
},
{
name : 'extKeyUsage',
serverAuth : true,
clientAuth : true,
codeSigning : true,
emailProtection : true,
timeStamping : true
},
{
name : 'nsCertType',
client : true,
server : true,
email : true,
objsign : true,
sslCA : true,
emailCA : true,
objCA : true
},
{
name : 'subjectAltName',
altNames : [config.dns.map(dns=>{return {type:2,value:dns}}),config.ip.map(ip=>{return {type:7,ip}})].flat()
},
{
name : 'subjectKeyIdentifier'
}
]);
cert.sign(keys.privateKey);
var key = pki.privateKeyToPem(keys.privateKey);
var cert = pki.certificateToPem(cert);
console.log(key);
console.log(cert);
for some reason, unknown to me at the time of writing, we cant just import the node-forge library into node.js like this
//nodeforge.mjs
import nodeForge from 'https://cdn.jsdelivr.net/npm/node-forge/+esm';
// ...
and then run with
node --experimental-network-imports ./nodeforge.mjs
result
ReferenceError: window is not defined
at https://cdn.jsdelivr.net/npm/node-forge/+esm:7:27570
disclaimer here : i am busy working on other things, so my approach is somewhat naïve, anyone more familiar with webpack that can help to further this process would be greatly appreciated, please leave comments or find me in the [stackoverflow javascript chat room][5] and i'll update this procedure
ok, so just directly importing from the cdn in node.js didnt, i attempted to use webpack to create a module bundle that could be hosted on github and imported from there, ( i did the following in docker )
npm install node-forge webpack webpack-cli
//index.mjs
import nodeforge from 'node-forge';
export default nodeforge;
//webpack.config.js
module.exports = {
entry: './index.mjs',
target: 'node',
mode:'production',
experiments: {
outputModule: true,
} ,
output: {
path: __dirname+'',
filename: 'bundle.mjs',
chunkFormat: 'module',
library: {
type: 'module',
},
}
};
npx webpack
and it works locally
//test.mjs
import nodeforge from './bundle.mjs';
console.log(nodeforge.pki.createCertificate);
node ./test.mjs
// [Function (anonymous)]
but not from a url import
//test.mjs
import nodeForge from 'https://raw.githubusercontent.com/javascript-2020/npm/main/node-forge/node-forge-nodejs-import.mjs';
console.log(nodeForge);
node --experimental-network-imports ./test.mjs
// RangeError [ERR_UNKNOWN_MODULE_FORMAT]: Unknown module format: null for URL https://raw.githubusercontent.com/javascript-2020/npm/...
i must have built the module wrong from webpack
so now i do
//index.js
var nodeforge = require('node-forge');
modules.exports = nodeforge;
//webpack.config.js
module.exports = {
entry: './index.js',
target: 'node',
mode:'production',
output: {
path: __dirname+'',
filename: 'bundle.js',
library: {
name: 'nodeforge',
type: 'umd',
},
}
};
npx webpack
and it works locally
//test.js
var nodeforge=require('./bundle.js');
console.log(nodeforge.pki.createCertificate);
node test.js
// [Function (anonymous)]
and to get it into node.js i use
//test.js
(async()=>{
var nodeforge=await fetch('https://raw.githubusercontent.com/javascript-2020/npm/main/node-forge/node-forge-nodejs-eval.js')
.then(res=>res.text().then(txt=>new Promise(res=>{eval(txt);res(module.exports)})));
console.log(nodeforge.pki.createCertificate);
})();
node test.js
// [Function (anonymous)]
if anyone can shed any light onto how to bundle pacakages with webpack, rollup etc for url imports any help would be greatly appreciated
please find me in
for more discussion