Merge pull request #1077 from atom/asar-standard
Remove asar: protocol, use file: protocol instead
This commit is contained in:
commit
fd2b2003b4
7 changed files with 33 additions and 31 deletions
|
@ -16,7 +16,6 @@
|
||||||
#include "content/public/browser/browser_thread.h"
|
#include "content/public/browser/browser_thread.h"
|
||||||
#include "content/public/common/url_constants.h"
|
#include "content/public/common/url_constants.h"
|
||||||
#include "net/url_request/data_protocol_handler.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 "net/url_request/url_request_intercepting_job_factory.h"
|
||||||
#include "url/url_constants.h"
|
#include "url/url_constants.h"
|
||||||
|
|
||||||
|
@ -26,8 +25,6 @@ namespace atom {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
const char* kAsarScheme = "asar";
|
|
||||||
|
|
||||||
class NoCacheBackend : public net::HttpCache::BackendFactory {
|
class NoCacheBackend : public net::HttpCache::BackendFactory {
|
||||||
int CreateBackend(net::NetLog* net_log,
|
int CreateBackend(net::NetLog* net_log,
|
||||||
scoped_ptr<disk_cache::Backend>* backend,
|
scoped_ptr<disk_cache::Backend>* backend,
|
||||||
|
@ -59,11 +56,7 @@ net::URLRequestJobFactory* AtomBrowserContext::CreateURLRequestJobFactory(
|
||||||
job_factory->SetProtocolHandler(
|
job_factory->SetProtocolHandler(
|
||||||
url::kDataScheme, new net::DataProtocolHandler);
|
url::kDataScheme, new net::DataProtocolHandler);
|
||||||
job_factory->SetProtocolHandler(
|
job_factory->SetProtocolHandler(
|
||||||
url::kFileScheme, new net::FileProtocolHandler(
|
url::kFileScheme, new asar::AsarProtocolHandler(
|
||||||
BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
|
|
||||||
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
|
|
||||||
job_factory->SetProtocolHandler(
|
|
||||||
kAsarScheme, new asar::AsarProtocolHandler(
|
|
||||||
BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
|
BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
|
||||||
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
|
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
|
||||||
|
|
||||||
|
|
|
@ -60,8 +60,8 @@ if nodeIntegration in ['true', 'all', 'except-iframe', 'manual-enable-iframe']
|
||||||
global.require = require
|
global.require = require
|
||||||
global.module = module
|
global.module = module
|
||||||
|
|
||||||
# Set the __filename to the path of html file if it's file: or asar: protocol.
|
# Set the __filename to the path of html file if it is file: protocol.
|
||||||
if window.location.protocol in ['file:', 'asar:']
|
if window.location.protocol is 'file:'
|
||||||
pathname =
|
pathname =
|
||||||
if process.platform is 'win32' and window.location.pathname[0] is '/'
|
if process.platform is 'win32' and window.location.pathname[0] is '/'
|
||||||
window.location.pathname.substr 1
|
window.location.pathname.substr 1
|
||||||
|
|
|
@ -179,7 +179,7 @@ class PreloadAttribute extends WebViewAttribute
|
||||||
return '' unless @webViewImpl.webviewNode.hasAttribute @name
|
return '' unless @webViewImpl.webviewNode.hasAttribute @name
|
||||||
preload = resolveUrl @webViewImpl.webviewNode.getAttribute(@name)
|
preload = resolveUrl @webViewImpl.webviewNode.getAttribute(@name)
|
||||||
protocol = preload.substr 0, 5
|
protocol = preload.substr 0, 5
|
||||||
unless protocol in ['file:', 'asar:']
|
unless protocol is 'file:'
|
||||||
console.error webViewConstants.ERROR_MSG_INVALID_PRELOAD_ATTRIBUTE
|
console.error webViewConstants.ERROR_MSG_INVALID_PRELOAD_ATTRIBUTE
|
||||||
preload = ''
|
preload = ''
|
||||||
preload
|
preload
|
||||||
|
|
|
@ -23,4 +23,4 @@ module.exports =
|
||||||
ERROR_MSG_CANNOT_INJECT_SCRIPT: '<webview>: ' +
|
ERROR_MSG_CANNOT_INJECT_SCRIPT: '<webview>: ' +
|
||||||
'Script cannot be injected into content until the page has loaded.'
|
'Script cannot be injected into content until the page has loaded.'
|
||||||
ERROR_MSG_INVALID_PARTITION_ATTRIBUTE: 'Invalid partition attribute.'
|
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.'
|
||||||
|
|
|
@ -69,30 +69,26 @@ require('/path/to/example.asar/dir/module.js');
|
||||||
|
|
||||||
### Web API
|
### Web API
|
||||||
|
|
||||||
In a web page, files in archive can be requested by using the `asar:` protocol.
|
In a web page, files in archive can be requested with the `file:` protocol. Like
|
||||||
Like the Node API, `asar` archives are treated as directories.
|
the Node API, `asar` archives are treated as directories.
|
||||||
|
|
||||||
For example, to get a file with `$.get`:
|
For example, to get a file with `$.get`:
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<script>
|
<script>
|
||||||
var $ = require('./jquery.min.js');
|
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);
|
console.log(data);
|
||||||
});
|
});
|
||||||
</script>
|
</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`:
|
You can also display a web page in an `asar` archive with `BrowserWindow`:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var BrowserWindow = require('browser-window');
|
var BrowserWindow = require('browser-window');
|
||||||
var win = new BrowserWindow({width: 800, height: 600});
|
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
|
### Treating `asar` archive as normal file
|
||||||
|
|
|
@ -348,50 +348,63 @@ describe 'asar package', ->
|
||||||
assert.throws throws, /ENOENT/
|
assert.throws throws, /ENOENT/
|
||||||
|
|
||||||
describe 'asar protocol', ->
|
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) ->
|
it 'can request a file in package', (done) ->
|
||||||
p = path.resolve fixtures, 'asar', 'a.asar', 'file1'
|
p = path.resolve fixtures, 'asar', 'a.asar', 'file1'
|
||||||
$.get "asar:#{p}", (data) ->
|
$.get "file://#{p}", (data) ->
|
||||||
assert.equal data, 'file1\n'
|
assert.equal data, 'file1\n'
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'can request a linked file in package', (done) ->
|
it 'can request a linked file in package', (done) ->
|
||||||
p = path.resolve fixtures, 'asar', 'a.asar', 'link2', 'link1'
|
p = path.resolve fixtures, 'asar', 'a.asar', 'link2', 'link1'
|
||||||
$.get "asar:#{p}", (data) ->
|
$.get "file://#{p}", (data) ->
|
||||||
assert.equal data, 'file1\n'
|
assert.equal data, 'file1\n'
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'can request a file in filesystem', (done) ->
|
it 'can request a file in filesystem', (done) ->
|
||||||
p = path.resolve fixtures, 'asar', 'file'
|
p = path.resolve fixtures, 'asar', 'file'
|
||||||
$.get "asar:#{p}", (data) ->
|
$.get "file://#{p}", (data) ->
|
||||||
assert.equal data, 'file\n'
|
assert.equal data, 'file\n'
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'gets 404 when file is not found', (done) ->
|
it 'gets 404 when file is not found', (done) ->
|
||||||
p = path.resolve fixtures, 'asar', 'a.asar', 'no-exist'
|
p = path.resolve fixtures, 'asar', 'a.asar', 'no-exist'
|
||||||
$.ajax
|
$.ajax
|
||||||
url: "asar:#{p}"
|
url: "file://#{p}"
|
||||||
error: (err) ->
|
error: (err) ->
|
||||||
assert.equal err.status, 404
|
assert.equal err.status, 404
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'sets __dirname correctly', (done) ->
|
it 'sets __dirname correctly', (done) ->
|
||||||
url = require 'url'
|
|
||||||
remote = require 'remote'
|
|
||||||
ipc = remote.require 'ipc'
|
|
||||||
BrowserWindow = remote.require 'browser-window'
|
|
||||||
|
|
||||||
after ->
|
after ->
|
||||||
w.destroy()
|
w.destroy()
|
||||||
ipc.removeAllListeners 'dirname'
|
ipc.removeAllListeners 'dirname'
|
||||||
|
|
||||||
w = new BrowserWindow(show: false, width: 400, height: 400)
|
w = new BrowserWindow(show: false, width: 400, height: 400)
|
||||||
p = path.resolve fixtures, 'asar', 'web.asar', 'index.html'
|
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
|
w.loadUrl u
|
||||||
ipc.on 'dirname', (event, dirname) ->
|
ipc.once 'dirname', (event, dirname) ->
|
||||||
assert.equal dirname, path.dirname(p)
|
assert.equal dirname, path.dirname(p)
|
||||||
done()
|
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', ->
|
describe 'original-fs module', ->
|
||||||
originalFs = require 'original-fs'
|
originalFs = require 'original-fs'
|
||||||
|
|
||||||
|
|
BIN
spec/fixtures/asar/script.asar
vendored
Executable file
BIN
spec/fixtures/asar/script.asar
vendored
Executable file
Binary file not shown.
Loading…
Reference in a new issue