Add import meta script

This commit is contained in:
nolash 2021-02-12 20:40:18 +01:00
parent a9880c05a0
commit 08e8c58c30
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 3221 additions and 5 deletions

View File

@ -1,3 +1,6 @@
* 0.0.7-pending
- Add immutable content
- Add db lock on server
* 0.0.6
- Add server build
* 0.0.5

View File

@ -1,19 +1,16 @@
{
"name": "cic-client-meta",
"version": "0.0.6",
"version": "0.0.7-alpha.1",
"description": "Signed CRDT metadata graphs for the CIC network",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"test": "mocha -r node_modules/node-localstorage/register -r ts-node/register tests/*.ts",
"build": "node_modules/typescript/bin/tsc -d --outDir dist",
"build": "node_modules/typescript/bin/tsc -d --outDir dist && npm run build-server",
"build-server": "tsc -d --outDir dist-server scripts/server/*.ts",
"pack": "node_modules/typescript/bin/tsc -d --outDir dist && webpack",
"clean": "rm -rf dist"
},
"bin": {
"cic-meta-server": "./dist-server/scripts/server/server.js"
},
"dependencies": {
"@ethereumjs/tx": "^3.0.0-beta.1",
"automerge": "^0.14.1",

View File

@ -2,3 +2,4 @@ export { PGPSigner, PGPKeyStore } from './auth';
export { Envelope, Syncable } from './sync';
export { User } from './assets/user';
export { Phone } from './assets/phone';
export { Config } from './config';

View File

@ -0,0 +1,88 @@
const fs = require('fs');
const path = require('path');
const cic = require('cic-client-meta');
const http = require('http');
//const conf = JSON.parse(fs.readFileSync('./cic.conf'));
const config = new cic.Config('./config');
config.process();
console.log(config);
// Stolen from https://coderrocketfuel.com/article/recursively-list-all-the-files-in-a-directory-using-node-js
// Thanks!
const getAllFiles = function(dirPath, arrayOfFiles) {
files = fs.readdirSync(dirPath)
arrayOfFiles = arrayOfFiles || []
files.forEach(function(file) {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles)
} else if (file.substr(-5) == '.json') {
arrayOfFiles.push(path.join(dirPath, "/", file))
}
})
return arrayOfFiles
}
async function sendit(uid, envelope) {
const d = envelope.toJSON();
const opts = {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Content-Length': d.length,
'X-CIC-AUTOMERGE': 'client',
},
};
let url = config.get('META_URL');
url = url.replace(new RegExp('^(.+://[^/]+)/*$'), '$1/');
console.debug('url: ' + url);
const req = http.request(url + uid, opts, (res) => {
res.on('data', process.stdout.write);
res.on('end', () => {
console.log('result', res.statusCode, res.headers);
});
});
req.write(d);
req.end();
}
function doit(keystore) {
getAllFiles(process.argv[2]).forEach((filename) => {
const signer = new cic.PGPSigner(keystore);
const parts = filename.split('.');
const uid = path.basename(parts[0]);
const d = fs.readFileSync(filename, 'utf-8');
const o = JSON.parse(d);
console.log(o);
const s = new cic.Syncable(uid, o);
s.setSigner(signer);
s.onwrap = (env) => {
sendit(uid, env);
};
s.sign();
});
}
const privateKeyPath = path.join(config.get('PGP_EXPORTS_DIR'), config.get('PGP_PRIVATE_KEY_FILE'));
const publicKeyPath = path.join(config.get('PGP_EXPORTS_DIR'), config.get('PGP_PRIVATE_KEY_FILE'));
pk = fs.readFileSync(privateKeyPath);
pubk = fs.readFileSync(publicKeyPath);
new cic.PGPKeyStore(
config.get('PGP_PASSPHRASE'),
pk,
pubk,
undefined,
undefined,
doit,
);

File diff suppressed because it is too large Load Diff