diff --git a/.gitignore b/.gitignore index 521020a022..1b0f45f0fe 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ android/app/build/ android/app/src/main/arm64-v8a/ android/build/ *.sw[pomnlk] +src/DatArchive.bundle.js diff --git a/builddat.js b/builddat.js new file mode 100644 index 0000000000..e84adc7c99 --- /dev/null +++ b/builddat.js @@ -0,0 +1,15 @@ +const browserify = require('browserify') +const defaultBuiltins = require('browserify/lib/builtins') + +browserify('./node_modules/dat-sdk/auto', { + insertGlobalVars: { + process: () => 'require("process/browser.js")' + }, + standalone: 'DatArchive', + browserField: false, + detectGlobals: true, +}) +.exclude(Object.keys(defaultBuiltins)) +.exclude('utp-native') +.bundle() +.pipe(process.stdout) diff --git a/package.json b/package.json index 74fcf8ed27..97383df8eb 100644 --- a/package.json +++ b/package.json @@ -11,8 +11,9 @@ "main": "src/index.js", "scripts": { "build": "node-gyp build", + "build-dat": "mkdirp ./build/js/ && node builddat > ./build/js/DatArchive.bundle.js", "debug": "ndb", - "install": "node ./scripts/preinstall.js && node-gyp configure --msvs_version=2017 && node-gyp rebuild", + "install": "node ./scripts/preinstall.js && node-gyp configure --msvs_version=2017 && node-gyp rebuild && npm run build-dat", "lint": "eslint src tests", "rebuild": "shx rm -rf node_modules && npm cache clean --force && npm install", "serve": "serve examples", @@ -48,6 +49,8 @@ "@rgrove/parse-xml": "^1.1.1", "cross-env": "^5.2.0", "css": "^2.2.4", + "dat-fetch": "^1.2.0", + "dat-sdk": "^0.3.1", "events": "^3.0.0", "fake-indexeddb": "^2.0.4", "find": "^0.2.9", @@ -89,6 +92,7 @@ "electron": "^5.0.3" }, "devDependencies": { + "browserify": "^16.5.0", "chai": "^4.1.2", "eslint": "^5.2.0", "express": "^4.16.3", diff --git a/src/fetch.js b/src/fetch.js index de7bde671f..5609876ec0 100644 --- a/src/fetch.js +++ b/src/fetch.js @@ -4,8 +4,12 @@ const utils = require('./utils'); const windowFetch = require('window-fetch'); const {Response} = windowFetch; const GlobalContext = require('./GlobalContext'); +const { DatArchive } = require('../build/js/DatArchive.bundle.js'); +const datFetch = require('dat-fetch')(DatArchive); -const protocols = {}; +const protocols = { + dat: datFetch +}; ['http', 'https', 'file', 'data', 'blob'].forEach(p => { protocols[p] = windowFetch; }); @@ -26,7 +30,8 @@ async function fetch(u, options) { }); return new Response(body); } else { - return _protocolFetch(utils._normalizeUrl(u, GlobalContext.baseUrl), options); + const normalized = utils._normalizeUrl(u, GlobalContext.baseUrl) + return _protocolFetch(normalized, options); } } else { // Fetch is being called with a Request object diff --git a/src/utils.js b/src/utils.js index 74009f8891..dcf342b979 100644 --- a/src/utils.js +++ b/src/utils.js @@ -19,6 +19,7 @@ function _getBaseUrl(u, currentBaseUrl = '') { protocol: parsedUrl.protocol || 'http:', host: parsedUrl.host || '127.0.0.1', pathname: parsedUrl.pathname.replace(/\/[^\/]*\.[^\/]*$/, '') || '/', + slashes: true, // This is needed to have the `://` for all protocols. }); } if (!/\/$/.test(result) && !/\/[^\/]*?\.[^\/]*?$/.test(result)) { @@ -35,7 +36,7 @@ function _normalizeUrl(src, baseUrl) { /* if (!/^(?:\/|[a-z]+:)/.test(src)) { src = baseUrl + (!/\/$/.test(baseUrl) ? '/' : '') + src; } */ - if (!/^(?:https?|data|blob):/.test(src)) { + if (!/^(?:https?|data|blob|dat):/.test(src)) { return new URL(src, baseUrl).href .replace(/^(file:\/\/)\/([a-z]:.*)$/i, '$1$2'); } else { @@ -82,7 +83,7 @@ const _elementSetter = (self, attribute, cb) => { self.removeEventListener(attribute, listener); listener[symbols.listenerSymbol] = false; } - + if (typeof cb === 'function') { self.addEventListener(attribute, cb); cb[symbols.listenerSymbol] = true;