gun/sea/shim.js
Mark Nadal 638c2c3c23 have unbuild function wrap to prevent scope leaks & allow RETURN hehehe so I can reject @bmatusiak 's lS change O:) O:) I love you you're a hero!
later with @bmatusiak check sea.then for '../gun.js' vs '../' vs ...
note: src/index -> core.js
TODO: something about WebRTC candidates hitting ack decrement limits?
2024-01-17 10:13:00 -08:00

49 lines
1.8 KiB
JavaScript

;(function(){
const SEA = require('./root')
const api = {Buffer: require('./buffer')}
var o = {}, u;
// ideally we can move away from JSON entirely? unlikely due to compatibility issues... oh well.
JSON.parseAsync = JSON.parseAsync || function(t,cb,r){ var u; try{ cb(u, JSON.parse(t,r)) }catch(e){ cb(e) } }
JSON.stringifyAsync = JSON.stringifyAsync || function(v,cb,r,s){ var u; try{ cb(u, JSON.stringify(v,r,s)) }catch(e){ cb(e) } }
api.parse = function(t,r){ return new Promise(function(res, rej){
JSON.parseAsync(t,function(err, raw){ err? rej(err) : res(raw) },r);
})}
api.stringify = function(v,r,s){ return new Promise(function(res, rej){
JSON.stringifyAsync(v,function(err, raw){ err? rej(err) : res(raw) },r,s);
})}
if(SEA.window){
api.crypto = SEA.window.crypto || SEA.window.msCrypto
api.subtle = (api.crypto||o).subtle || (api.crypto||o).webkitSubtle;
api.TextEncoder = SEA.window.TextEncoder;
api.TextDecoder = SEA.window.TextDecoder;
api.random = (len) => api.Buffer.from(api.crypto.getRandomValues(new Uint8Array(api.Buffer.alloc(len))));
}
if(!api.TextDecoder)
{
const { TextEncoder, TextDecoder } = require((u+'' == typeof MODULE?'.':'')+'./lib/text-encoding', 1);
api.TextDecoder = TextDecoder;
api.TextEncoder = TextEncoder;
}
if(!api.crypto)
{
try
{
var crypto = require('crypto', 1);
Object.assign(api, {
crypto,
random: (len) => api.Buffer.from(crypto.randomBytes(len))
});
const { Crypto: WebCrypto } = require('@peculiar/webcrypto', 1);
api.ossl = api.subtle = new WebCrypto({directory: 'ossl'}).subtle // ECDH
}
catch(e){
console.log("Please `npm install @peculiar/webcrypto` or add it to your package.json !");
}}
module.exports = api
}());