From 0085bc83e40319072aeba31bd7cead3893a728b9 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 5 Jan 2018 13:20:26 -0800 Subject: [PATCH] Decaffeinate AtomProtocolHandler --- src/main-process/atom-protocol-handler.coffee | 43 --------------- src/main-process/atom-protocol-handler.js | 54 +++++++++++++++++++ 2 files changed, 54 insertions(+), 43 deletions(-) delete mode 100644 src/main-process/atom-protocol-handler.coffee create mode 100644 src/main-process/atom-protocol-handler.js diff --git a/src/main-process/atom-protocol-handler.coffee b/src/main-process/atom-protocol-handler.coffee deleted file mode 100644 index db385b4b7..000000000 --- a/src/main-process/atom-protocol-handler.coffee +++ /dev/null @@ -1,43 +0,0 @@ -{protocol} = require 'electron' -fs = require 'fs' -path = require 'path' - -# Handles requests with 'atom' protocol. -# -# It's created by {AtomApplication} upon instantiation and is used to create a -# custom resource loader for 'atom://' URLs. -# -# The following directories are searched in order: -# * ~/.atom/assets -# * ~/.atom/dev/packages (unless in safe mode) -# * ~/.atom/packages -# * RESOURCE_PATH/node_modules -# -module.exports = -class AtomProtocolHandler - constructor: (resourcePath, safeMode) -> - @loadPaths = [] - - unless safeMode - @loadPaths.push(path.join(process.env.ATOM_HOME, 'dev', 'packages')) - - @loadPaths.push(path.join(process.env.ATOM_HOME, 'packages')) - @loadPaths.push(path.join(resourcePath, 'node_modules')) - - @registerAtomProtocol() - - # Creates the 'atom' custom protocol handler. - registerAtomProtocol: -> - protocol.registerFileProtocol 'atom', (request, callback) => - relativePath = path.normalize(request.url.substr(7)) - - if relativePath.indexOf('assets/') is 0 - assetsPath = path.join(process.env.ATOM_HOME, relativePath) - filePath = assetsPath if fs.statSyncNoException(assetsPath).isFile?() - - unless filePath - for loadPath in @loadPaths - filePath = path.join(loadPath, relativePath) - break if fs.statSyncNoException(filePath).isFile?() - - callback(filePath) diff --git a/src/main-process/atom-protocol-handler.js b/src/main-process/atom-protocol-handler.js new file mode 100644 index 000000000..1affba02a --- /dev/null +++ b/src/main-process/atom-protocol-handler.js @@ -0,0 +1,54 @@ +const {protocol} = require('electron') +const fs = require('fs') +const path = require('path') + +// Handles requests with 'atom' protocol. +// +// It's created by {AtomApplication} upon instantiation and is used to create a +// custom resource loader for 'atom://' URLs. +// +// The following directories are searched in order: +// * ~/.atom/assets +// * ~/.atom/dev/packages (unless in safe mode) +// * ~/.atom/packages +// * RESOURCE_PATH/node_modules +// +module.exports = +class AtomProtocolHandler { + constructor (resourcePath, safeMode) { + this.loadPaths = [] + + if (!safeMode) { + this.loadPaths.push(path.join(process.env.ATOM_HOME, 'dev', 'packages')) + } + + this.loadPaths.push(path.join(process.env.ATOM_HOME, 'packages')) + this.loadPaths.push(path.join(resourcePath, 'node_modules')) + + this.registerAtomProtocol() + } + + // Creates the 'atom' custom protocol handler. + registerAtomProtocol () { + protocol.registerFileProtocol('atom', (request, callback) => { + const relativePath = path.normalize(request.url.substr(7)) + + let filePath + if (relativePath.indexOf('assets/') === 0) { + const assetsPath = path.join(process.env.ATOM_HOME, relativePath) + const stat = fs.statSyncNoException(assetsPath) + if (stat && stat.isFile()) filePath = assetsPath + } + + if (!filePath) { + for (let loadPath of this.loadPaths) { + filePath = path.join(loadPath, relativePath) + const stat = fs.statSyncNoException(filePath) + if (stat && stat.isFile()) break + } + } + + callback(filePath) + }) + } +}