<commit_before>// Copyright (c) The LHTML team
// See LICENSE for details.

//
// This script is executed right when an LHTML file is loaded.
//
const {ipcRenderer, remote} = require('electron');
const {RPCService} = require('../rpc.js');
const _ = require('lodash');
const formsync = require('./formsync.js');
const {ChrootFS} = require('../chrootfs.js');
const {RPCLock} = require('../locks.js');
const log = require('electron-log');
const {getPrefValue} = require('../prefs/prefs.js');

log.transports.console.level = process.env.JS_LOGLEVEL || 'warn';

let LHTML = {};

//----------------------------------------------------------------------------
// RPCService
//----------------------------------------------------------------------------


var RPC = new RPCService(ipcRenderer, {
  default_target: ipcRenderer,
  default_receiver: ipcRenderer,
  sender_id: window.location.hostname,
});
RPC.listen();
RPC.handlers = {
  echo: (ctx, data) => {
    return 'echo:' + data;
  },
  save_your_stuff: (ctx, data) => {
    var p;
    if (LHTML.saving.onBeforeSave) {
      p = Promise.resolve(LHTML.saving.onBeforeSave());
    } else {
      console.log('no onBeforeSave');
      p = Promise.resolve({})
    }
    return p.then(() => {
      TMP_DOC_EDITED = false;
      SAVING = true;
      return;
    })
  },
  set_chrootfs_root: (ctx, new_root) => {
    if (chfs) {
      chfs.setRoot(new_root);
    }
  },
  emit_event: (ctx, data) => {
    var event = data.key;
    var event_data = data.data;
    _.each(EVENT_HANDLERS[data.key], (func) => {
      func(data.data);
    });
  }
}
let rpc_lock = new RPCLock(RPC);

//----------------------------------------------------------------------------
// Public API
//
//  Every function on LHTML is available to guest files.
//----------------------------------------------------------------------------

//
//  Register something to handle events.
//
//  Some events:
//    saved
let EVENT_HANDLERS = {};
LHTML.on = (event, handler) => {
  if (!EVENT_HANDLERS[event]) {
    EVENT_HANDLERS[event] = [];
  }
  EVENT_HANDLERS[event].push(handler);
}

//---------------------------
// FileSystem stuff
//---------------------------
LHTML.fs = {};
let chfs;
let fs_attrs = [
  'writeFile',
  'readFile',
  'remove',
  'listdir',
]
let pending = {};
_.each(fs_attrs, attr => {
  pending[attr] = [];
  LHTML.fs[attr] = (...args) => {
    return new Promise((resolve, reject) => {
      pending[attr].push({args, resolve, reject});
    })
  }
})

const MEG = 2 ** 20;

function ceilToNearest(x, nearest) {
  return Math.ceil(x/nearest)*nearest;
}

let maxBytes = (parseInt(getPrefValue('max_doc_size')) * MEG);
maxBytes = maxBytes < (5 * MEG) ? (5 * MEG) : maxBytes;

function increaseSizePrompt(requestedSize, currentMaxBytes) {
  let currentmax_MB = Math.ceil(currentMaxBytes / MEG);
  let requested_MB = Math.ceil(requestedSize / MEG);
  let reasonable_MB = ceilToNearest(requested_MB * 1.1, 5);
  let message = `Do you want to allow this document to take ${reasonable_MB}MB of space?`
  let detail = `The document is requesting ${requested_MB}MB which exceeds the limit set in Preferences (${currentmax_MB}MB).`;<commit_msg>Make the parenthetical match<commit_after>// Copyright (c) The LHTML team
// See LICENSE for details.

//
// This script is executed right when an LHTML file is loaded.
//
const {ipcRenderer, remote} = require('electron');
const {RPCService} = require('../rpc.js');
const _ = require('lodash');
const formsync = require('./formsync.js');
const {ChrootFS} = require('../chrootfs.js');
const {RPCLock} = require('../locks.js');
const log = require('electron-log');
const {getPrefValue} = require('../prefs/prefs.js');

log.transports.console.level = process.env.JS_LOGLEVEL || 'warn';

let LHTML = {};

//----------------------------------------------------------------------------
// RPCService
//----------------------------------------------------------------------------


var RPC = new RPCService(ipcRenderer, {
  default_target: ipcRenderer,
  default_receiver: ipcRenderer,
  sender_id: window.location.hostname,
});
RPC.listen();
RPC.handlers = {
  echo: (ctx, data) => {
    return 'echo:' + data;
  },
  save_your_stuff: (ctx, data) => {
    var p;
    if (LHTML.saving.onBeforeSave) {
      p = Promise.resolve(LHTML.saving.onBeforeSave());
    } else {
      console.log('no onBeforeSave');
      p = Promise.resolve({})
    }
    return p.then(() => {
      TMP_DOC_EDITED = false;
      SAVING = true;
      return;
    })
  },
  set_chrootfs_root: (ctx, new_root) => {
    if (chfs) {
      chfs.setRoot(new_root);
    }
  },
  emit_event: (ctx, data) => {
    var event = data.key;
    var event_data = data.data;
    _.each(EVENT_HANDLERS[data.key], (func) => {
      func(data.data);
    });
  }
}
let rpc_lock = new RPCLock(RPC);

//----------------------------------------------------------------------------
// Public API
//
//  Every function on LHTML is available to guest files.
//----------------------------------------------------------------------------

//
//  Register something to handle events.
//
//  Some events:
//    saved
let EVENT_HANDLERS = {};
LHTML.on = (event, handler) => {
  if (!EVENT_HANDLERS[event]) {
    EVENT_HANDLERS[event] = [];
  }
  EVENT_HANDLERS[event].push(handler);
}

//---------------------------
// FileSystem stuff
//---------------------------
LHTML.fs = {};
let chfs;
let fs_attrs = [
  'writeFile',
  'readFile',
  'remove',
  'listdir',
]
let pending = {};
_.each(fs_attrs, attr => {
  pending[attr] = [];
  LHTML.fs[attr] = (...args) => {
    return new Promise((resolve, reject) => {
      pending[attr].push({args, resolve, reject});
    })
  }
})

const MEG = 2 ** 20;

function ceilToNearest(x, nearest) {
  return Math.ceil(x/nearest)*nearest;
}

let maxBytes = (parseInt(getPrefValue('max_doc_size')) * MEG);
maxBytes = maxBytes < (5 * MEG) ? (5 * MEG) : maxBytes;

function increaseSizePrompt(requestedSize, currentMaxBytes) {
  let currentmax_MB = Math.ceil(currentMaxBytes / MEG);
  let requested_MB = Math.ceil(requestedSize / MEG);
  let reasonable_MB = ceilToNearest(requested_MB * 1.1, 5);
  let message = `Do you want to allow this document to take ${reasonable_MB}MB of space?`
  let detail = `The document is requesting ${requested_MB}MB which exceeds the current limit of ${currentmax_MB}MB (set in Preferences).`;
{'fraction_non_alphanumeric': 0.26107445805843543, 'fraction_numerical': 0.0025133521834747093, 'mean_word_length': 2.1551040634291376, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7109555', 'n_tokens_mistral': 2048, 'n_tokens_neox': 2010, 'n_words': 464}