From dd6809c8d62b0cd009e49d607bacd07538be4129 Mon Sep 17 00:00:00 2001 From: rangermauve Date: Wed, 17 Jul 2019 23:26:54 -0400 Subject: [PATCH 1/4] Added initial dat support --- package.json | 2 ++ src/WindowBase.js | 2 +- src/fetch.js | 9 +++++++-- src/utils.js | 5 +++-- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 79183d5e4b..4decf27f1c 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "@rgrove/parse-xml": "^1.1.1", "cross-env": "^5.2.0", "css": "^2.2.4", + "dat-fetch": "^1.2.0", "events": "^3.0.0", "fake-indexeddb": "^2.0.4", "find": "^0.2.9", @@ -72,6 +73,7 @@ "native-openvr-deps": "0.0.19", "native-video-deps": "0.0.35", "native-webrtc-deps": "0.0.45", + "node-dat-archive": "^2.2.0", "node-ipc": "^9.1.1", "parse-int": "^1.0.2", "parse5": "^5.0.0", diff --git a/src/WindowBase.js b/src/WindowBase.js index 5558f40dee..06f7d1ce63 100644 --- a/src/WindowBase.js +++ b/src/WindowBase.js @@ -380,4 +380,4 @@ if (onbeforeload) { if (!args.require) { global.require = undefined; } -global.process = undefined; +// global.process = undefined; diff --git a/src/fetch.js b/src/fetch.js index de7bde671f..7a120d9e65 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('node-dat-archive'); +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 fb6cf439bb..b5f04fc47d 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)) { @@ -32,7 +33,7 @@ function _normalizeUrl(src, baseUrl) { if (/^\/\//.test(src)) { src = new URL(baseUrl).protocol + 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 { @@ -79,7 +80,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; From 96d7e378c9fbfbdbdc2eda6811f3e0ba376418bf Mon Sep 17 00:00:00 2001 From: rangermauve Date: Sat, 17 Aug 2019 16:24:19 -0400 Subject: [PATCH 2/4] Added browserify bundle for DatArchive --- .gitignore | 1 + builddat.js | 20 ++++++++++++++++++++ package.json | 4 +++- src/WindowBase.js | 2 +- src/fetch.js | 3 ++- 5 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 builddat.js 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..2aae6ab16c --- /dev/null +++ b/builddat.js @@ -0,0 +1,20 @@ +const browserify = require('browserify') +const defaultBuiltins = require('browserify/lib/builtins') + + +browserify('./node_modules/node-dat-archive/', { + // builtins: { + // process: require.resolve('process/browser') + // }, + insertGlobalVars: { + process: function() {return 'require("process/browser.js")' } + }, + standalone: 'DatArchive', + // builtins: false, + browserField: false, + detectGlobals: true, +}) +.exclude(Object.keys(defaultBuiltins)) +.exclude('utp-native') +.bundle() +.pipe(process.stdout) diff --git a/package.json b/package.json index 4decf27f1c..766b5a1e90 100644 --- a/package.json +++ b/package.json @@ -11,8 +11,9 @@ "main": "src/index.js", "scripts": { "build": "node-gyp build", + "build-dat": "node builddat > src/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", @@ -91,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/WindowBase.js b/src/WindowBase.js index 06f7d1ce63..5558f40dee 100644 --- a/src/WindowBase.js +++ b/src/WindowBase.js @@ -380,4 +380,4 @@ if (onbeforeload) { if (!args.require) { global.require = undefined; } -// global.process = undefined; +global.process = undefined; diff --git a/src/fetch.js b/src/fetch.js index 7a120d9e65..16e39524f7 100644 --- a/src/fetch.js +++ b/src/fetch.js @@ -4,7 +4,8 @@ const utils = require('./utils'); const windowFetch = require('window-fetch'); const {Response} = windowFetch; const GlobalContext = require('./GlobalContext'); -const DatArchive = require('node-dat-archive'); +const DatArchive = require('./DatArchive.bundle.js'); +console.log(DatArchive) const datFetch = require('dat-fetch')(DatArchive); const protocols = { From 459f5b193929bed264933d1bbe3012c6e0a238c9 Mon Sep 17 00:00:00 2001 From: rangermauve Date: Sat, 17 Aug 2019 16:29:38 -0400 Subject: [PATCH 3/4] Minor cleanup --- builddat.js | 7 +------ src/fetch.js | 1 - 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/builddat.js b/builddat.js index 2aae6ab16c..8784b27e8d 100644 --- a/builddat.js +++ b/builddat.js @@ -1,16 +1,11 @@ const browserify = require('browserify') const defaultBuiltins = require('browserify/lib/builtins') - browserify('./node_modules/node-dat-archive/', { - // builtins: { - // process: require.resolve('process/browser') - // }, insertGlobalVars: { - process: function() {return 'require("process/browser.js")' } + process: () => 'require("process/browser.js")' }, standalone: 'DatArchive', - // builtins: false, browserField: false, detectGlobals: true, }) diff --git a/src/fetch.js b/src/fetch.js index 16e39524f7..70dda30705 100644 --- a/src/fetch.js +++ b/src/fetch.js @@ -5,7 +5,6 @@ const windowFetch = require('window-fetch'); const {Response} = windowFetch; const GlobalContext = require('./GlobalContext'); const DatArchive = require('./DatArchive.bundle.js'); -console.log(DatArchive) const datFetch = require('dat-fetch')(DatArchive); const protocols = { From c121a7317d0eae8ff1b311927b5fb5d9b3d54f1c Mon Sep 17 00:00:00 2001 From: RangerMauve Date: Sun, 1 Sep 2019 17:02:33 -0400 Subject: [PATCH 4/4] Use the Dat SDK --- builddat.js | 2 +- package.json | 4 ++-- src/fetch.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/builddat.js b/builddat.js index 8784b27e8d..e84adc7c99 100644 --- a/builddat.js +++ b/builddat.js @@ -1,7 +1,7 @@ const browserify = require('browserify') const defaultBuiltins = require('browserify/lib/builtins') -browserify('./node_modules/node-dat-archive/', { +browserify('./node_modules/dat-sdk/auto', { insertGlobalVars: { process: () => 'require("process/browser.js")' }, diff --git a/package.json b/package.json index 2ee3e67d8b..97383df8eb 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "main": "src/index.js", "scripts": { "build": "node-gyp build", - "build-dat": "node builddat > src/DatArchive.bundle.js", + "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 && npm run build-dat", "lint": "eslint src tests", @@ -50,6 +50,7 @@ "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", @@ -74,7 +75,6 @@ "native-openvr-deps": "0.0.19", "native-video-deps": "0.0.35", "native-webrtc-deps": "0.0.45", - "node-dat-archive": "^2.2.0", "node-ipc": "^9.1.1", "parse-int": "^1.0.2", "parse5": "^5.0.0", diff --git a/src/fetch.js b/src/fetch.js index 70dda30705..5609876ec0 100644 --- a/src/fetch.js +++ b/src/fetch.js @@ -4,7 +4,7 @@ const utils = require('./utils'); const windowFetch = require('window-fetch'); const {Response} = windowFetch; const GlobalContext = require('./GlobalContext'); -const DatArchive = require('./DatArchive.bundle.js'); +const { DatArchive } = require('../build/js/DatArchive.bundle.js'); const datFetch = require('dat-fetch')(DatArchive); const protocols = {