How to Encode and Decode the Closures

Is there a way to encode and decode the closures?

Closures are not encodable or decodable as they are blocks code compiled into your program. Besides the fact that they might need to capture parts of the state of your program dynamically (which leads to a difficult technical challenge of making this work), consider the security implications of allowing you to arbitrarily encode and decode parts of the executable portions of your program: what happens when you attempt to reload the encoded class back into your app and run it? What if someone has maliciously edited an archive to insert code into your program? There is no reasonable way to tell whether the closure is valid/safe to run or not, past loading it back into your process.

This would be terribly insecure, and is generally not even allowed by the system. (Perhaps look into writable vs. executable memory — for this security reason, many operating systems designate blocks of memory as either being executable [e.g. the code in your program] or writable [e.g. data that you read into memory/manipulate], but not both.)

Google Closure Compiler inlines a repeatedly used private property - a flaw or am I missing something?

This is covered in the Closure Compiler FAQ:
https://github.com/google/closure-compiler/wiki/FAQ#closure-compiler-inlined-all-my-strings-which-made-my-code-size-bigger-why-did-it-do-that

There are cases where inlining a string will make code size larger post-gzip, but I don't expect that will be the case here as it is unlikely to "flood" the gzip compression window.

How can I encode and decode a objedct to Base64?

Use Buffer.

const object = {
foo: 'bar',
};

// convert it to base64
const base64 = Buffer.from(JSON.stringify(object)).toString('base64');

console.table({ base64 });

// convert it back
const back = JSON.parse(Buffer.from(base64, 'base64').toString('utf-8'));

console.log(back);

How to encode a string for displaying in HTML in JavaScript?

You can decode HTML content by using decodeURIComponent. for more details (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent)

  const uri = "<p>How are you?</p>";
const decodeUri = decodeURIComponent(uri);
console.log(decodeUri)


Related Topics



Leave a reply



Submit