Merge pull request #1077 from atom/asar-standard

Remove asar: protocol, use file: protocol instead
This commit is contained in:
Cheng Zhao 2015-01-31 22:30:29 -08:00
commit fd2b2003b4
7 changed files with 33 additions and 31 deletions

View file

@ -16,7 +16,6 @@
#include "content/public/browser/browser_thread.h"
#include "content/public/common/url_constants.h"
#include "net/url_request/data_protocol_handler.h"
#include "net/url_request/file_protocol_handler.h"
#include "net/url_request/url_request_intercepting_job_factory.h"
#include "url/url_constants.h"
@ -26,8 +25,6 @@ namespace atom {
namespace {
const char* kAsarScheme = "asar";
class NoCacheBackend : public net::HttpCache::BackendFactory {
int CreateBackend(net::NetLog* net_log,
scoped_ptr<disk_cache::Backend>* backend,
@ -59,11 +56,7 @@ net::URLRequestJobFactory* AtomBrowserContext::CreateURLRequestJobFactory(
job_factory->SetProtocolHandler(
url::kDataScheme, new net::DataProtocolHandler);
job_factory->SetProtocolHandler(
url::kFileScheme, new net::FileProtocolHandler(
BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
job_factory->SetProtocolHandler(
kAsarScheme, new asar::AsarProtocolHandler(
url::kFileScheme, new asar::AsarProtocolHandler(
BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));

View file

@ -60,8 +60,8 @@ if nodeIntegration in ['true', 'all', 'except-iframe', 'manual-enable-iframe']
global.require = require
global.module = module
# Set the __filename to the path of html file if it's file: or asar: protocol.
if window.location.protocol in ['file:', 'asar:']
# Set the __filename to the path of html file if it is file: protocol.
if window.location.protocol is 'file:'
pathname =
if process.platform is 'win32' and window.location.pathname[0] is '/'
window.location.pathname.substr 1

View file

@ -179,7 +179,7 @@ class PreloadAttribute extends WebViewAttribute
return '' unless @webViewImpl.webviewNode.hasAttribute @name
preload = resolveUrl @webViewImpl.webviewNode.getAttribute(@name)
protocol = preload.substr 0, 5
unless protocol in ['file:', 'asar:']
unless protocol is 'file:'
console.error webViewConstants.ERROR_MSG_INVALID_PRELOAD_ATTRIBUTE
preload = ''
preload

View file

@ -23,4 +23,4 @@ module.exports =
ERROR_MSG_CANNOT_INJECT_SCRIPT: '<webview>: ' +
'Script cannot be injected into content until the page has loaded.'
ERROR_MSG_INVALID_PARTITION_ATTRIBUTE: 'Invalid partition attribute.'
ERROR_MSG_INVALID_PRELOAD_ATTRIBUTE: 'Only "file:" or "asar:" protocol is supported in "preload" attribute.'
ERROR_MSG_INVALID_PRELOAD_ATTRIBUTE: 'Only "file:" protocol is supported in "preload" attribute.'

View file

@ -69,30 +69,26 @@ require('/path/to/example.asar/dir/module.js');
### Web API
In a web page, files in archive can be requested by using the `asar:` protocol.
Like the Node API, `asar` archives are treated as directories.
In a web page, files in archive can be requested with the `file:` protocol. Like
the Node API, `asar` archives are treated as directories.
For example, to get a file with `$.get`:
```html
<script>
var $ = require('./jquery.min.js');
$.get('asar:/path/to/example.asar/file.txt', function(data) {
$.get('file:///path/to/example.asar/file.txt', function(data) {
console.log(data);
});
</script>
```
The `asar:` protocol can also be used to request normal files in filesystem,
just like the `file:` protocol. But unlike `file:` protocol, there are no
slashes (`//`) after `asar:`.
You can also display a web page in an `asar` archive with `BrowserWindow`:
```javascript
var BrowserWindow = require('browser-window');
var win = new BrowserWindow({width: 800, height: 600});
win.loadUrl('asar:/path/to/example.asar/static/index.html');
win.loadUrl('file:///path/to/example.asar/static/index.html');
```
### Treating `asar` archive as normal file

View file

@ -348,50 +348,63 @@ describe 'asar package', ->
assert.throws throws, /ENOENT/
describe 'asar protocol', ->
url = require 'url'
remote = require 'remote'
ipc = remote.require 'ipc'
BrowserWindow = remote.require 'browser-window'
it 'can request a file in package', (done) ->
p = path.resolve fixtures, 'asar', 'a.asar', 'file1'
$.get "asar:#{p}", (data) ->
$.get "file://#{p}", (data) ->
assert.equal data, 'file1\n'
done()
it 'can request a linked file in package', (done) ->
p = path.resolve fixtures, 'asar', 'a.asar', 'link2', 'link1'
$.get "asar:#{p}", (data) ->
$.get "file://#{p}", (data) ->
assert.equal data, 'file1\n'
done()
it 'can request a file in filesystem', (done) ->
p = path.resolve fixtures, 'asar', 'file'
$.get "asar:#{p}", (data) ->
$.get "file://#{p}", (data) ->
assert.equal data, 'file\n'
done()
it 'gets 404 when file is not found', (done) ->
p = path.resolve fixtures, 'asar', 'a.asar', 'no-exist'
$.ajax
url: "asar:#{p}"
url: "file://#{p}"
error: (err) ->
assert.equal err.status, 404
done()
it 'sets __dirname correctly', (done) ->
url = require 'url'
remote = require 'remote'
ipc = remote.require 'ipc'
BrowserWindow = remote.require 'browser-window'
after ->
w.destroy()
ipc.removeAllListeners 'dirname'
w = new BrowserWindow(show: false, width: 400, height: 400)
p = path.resolve fixtures, 'asar', 'web.asar', 'index.html'
u = url.format protocol: 'asar', slashed: false, pathname: p
u = url.format protocol: 'file', slashed: true, pathname: p
w.loadUrl u
ipc.on 'dirname', (event, dirname) ->
ipc.once 'dirname', (event, dirname) ->
assert.equal dirname, path.dirname(p)
done()
it 'loads script tag in html', (done) ->
after ->
w.destroy()
ipc.removeAllListeners 'ping'
w = new BrowserWindow(show: false, width: 400, height: 400)
p = path.resolve fixtures, 'asar', 'script.asar', 'index.html'
u = url.format protocol: 'file', slashed: true, pathname: p
w.loadUrl u
ipc.once 'ping', (event, message) ->
assert.equal message, 'pong'
done()
describe 'original-fs module', ->
originalFs = require 'original-fs'

BIN
spec/fixtures/asar/script.asar vendored Executable file

Binary file not shown.