Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dat support #1381

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ android/app/build/
android/app/src/main/arm64-v8a/
android/build/
*.sw[pomnlk]
src/DatArchive.bundle.js
15 changes: 15 additions & 0 deletions builddat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const browserify = require('browserify')
const defaultBuiltins = require('browserify/lib/builtins')

browserify('./node_modules/node-dat-archive/', {
insertGlobalVars: {
process: () => 'require("process/browser.js")'
},
standalone: 'DatArchive',
browserField: false,
detectGlobals: true,
})
.exclude(Object.keys(defaultBuiltins))
.exclude('utp-native')
.bundle()
.pipe(process.stdout)
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this trigger?

-- Would it be compatible with both the Android and the Magic Leap build toolchains?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just builds up the browserify-ed bundle from the JS source.

Since browserify is just JS it should work on any platform without any hassle.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, that makes sense.

Maybe we could put the output somewhere other than src in that case? That directory feels like it should only have checked-in source and we already have a build directory. 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM. Would just build/DatArchive.bundle.js work for that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe build/js/DatArchive.bundle.js, so it doesn't get too messy with the other binary stuff there.

Capture 87

"lint": "eslint src tests",
"rebuild": "shx rm -rf node_modules && npm cache clean --force && npm install",
"serve": "serve examples",
Expand Down Expand Up @@ -48,6 +49,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",
Expand All @@ -72,6 +74,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",
Expand All @@ -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",
Expand Down
9 changes: 7 additions & 2 deletions src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ const utils = require('./utils');
const windowFetch = require('window-fetch');
const {Response} = windowFetch;
const GlobalContext = require('./GlobalContext');
const DatArchive = require('./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;
});
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down