UNPKG

@moonlightos/moonlight-hard

Version:

A way for open source developers to get paid

143 lines (130 loc) 3.88 kB
/** * A file for checking things related to this package. * For instance, not showing six ads in a row if there are six packages * with 'moonlight' as a dependency */ const { tmpdir } = require('os') const { statSync, unlinkSync, readFileSync, writeFileSync } = require('fs') const { join } = require('path') const { Agent, get } = require('https') const CHECK_FILE_PATH = join(tmpdir(), 'moonlight-show') const CHECK_TIMEOUT = 1000 * 60 const config = { AD_PATH: 'https://moonlightos.com/install/#/npm/', API_ENDPOINT: 'moonlightos.com' } /** * Returns whether an ad should be shown */ function shouldShowAd() { try { const { mtime: lastTime } = statSync(CHECK_FILE_PATH) return Date.now() - lastTime > CHECK_TIMEOUT } catch (e) {} return true } /** * Returns the saved install identifier */ function getIdentifier() { try { return readFileSync(CHECK_FILE_PATH).toString() } catch (e) { return '' } } /** * Removes the install identifier file, used for testing purposes. */ function removeIdentifier() { try { unlinkSync(CHECK_FILE_PATH) } catch (e) {} } /** * When ad is shown, dont show another one for a minute. Save install identifier, * so that fractional impressions can get rewarded accordingly. * @param {string} identifier The 32 byte identifier for the current install */ function setAdShown(identifier) { try { writeFileSync(CHECK_FILE_PATH, identifier) } catch (e) {} } /* Operating System checks */ const isWindows = process.platform === 'win32' const isMac = process.platform === 'darwin' const isLinux = process.platfrom === 'linux' /** * Returns the absolute path to the sandbox browser */ function getSandboxPath() { const executableName = (isMac ? 'neutrino' : 'neutrino-win.exe') const executablePath = join(__dirname, '..', 'bin', executableName) return executablePath } /** * Opens the default browser with specified URL * @param {string} url Url to open */ function openBrowser(url, sandbox, callback) { // TODO: On Windows, &'s in the URL should be escaped with ^& try { if (sandbox && !isLinux && (isWindows || isMac)) { var browserExecutable = getSandboxPath() var childProcess = require('child_process') childProcess.execFile(browserExecutable, [url], function (err, stdout, stderr) { if (typeof callback === 'function') callback(stdout === 'impression', stderr) }) } else { var start = (isMac ? 'open' : isWindows ? 'start' : 'xdg-open') require('child_process').exec(start + ' ' + url, function() { if (typeof callback === 'function') callback(true) }) } } catch (e) { callback(false, e) } } /** * Generates an identifier that is used to identify a queue of package installs. */ function generateIdentifier() { chars = 'abcdefghijklmnopqrstuvwxyz1234567890'.split('') const bytes = 32 let hash = '' for (let i = 0; i < bytes; i++){ hash += chars[Math.floor(Math.random() * chars.length)] } return hash } /** * Notifies the server of a fractional impression * @param {string} repository The full-name of the repository (eg. MoonlightOS/moonlight-easy) * @param {string} packageManager Package manager, in this case 'npm' */ function fractionalImpression(repository, packageManager) { try { const identifier = getIdentifier() const agent = new Agent() const path = '/fractional?repo=' + repository + '&identifier=' + identifier + '&packageManager=' + packageManager const options = { hostname: config.API_ENDPOINT, port: 3001, path: path, agent: agent, } /* https.get(options) */ get(options) return true } catch (e) { return false } } module.exports = { shouldShowAd, setAdShown, getSandboxPath, openBrowser, generateIdentifier, fractionalImpression, getIdentifier, removeIdentifier, config }