Skip to content

Commit dabd3c5

Browse files
committed
Improve packaging and split browser and Node-specific code
This commit separates components that depend on browser environment from components that use Node features. No separation was previously needed because code used runtime feature detection. Such feature detection made it impossible for bundlers, like Webpack, to strip away Node-specific code. Presence of it caused failures at build time and made it hard to use the driver package. Driver contains couple env-dependent components: * network channel (`WebSocket` in browser, `net.Socket` in Node) * byte buffer (`ArrayBuffer` in browser, `buffer.Buffer` in Node) * host name resolver (no-op in browser, `dns` lookup in Node) * UTF8 encoding & decoding (`text-encoding` library in browser, `StringDecoder` in Node) Implementations of these components now live in separate folders `src/internal/node` and `src/internal/browser`. Both contain `index.js` files that export the same set of components and follow the same "interface". Env-dependent tests are also split into separate folders. All sources import/require `node/index.js` by default. `package.json` now contains a "browser" field which instructs bundlers to replace all imports/require of `node/index.js` with `browser/index.js` for client-side apps. This way code that depends on Node APIs will not even be included in the resulting bundle. Gulp build for UMD `neo4j-web.js` and `neo4j-web.test.js` bundles excludes `node` folders and only includes `browser`. It also replaces all require calls of "/node" to "/browser". Dedicated browserify transformation is added for this to the bundling process. Resulting bundles no longer contain code that depends on Node APIs.
1 parent eeb2662 commit dabd3c5

38 files changed

+956
-923
lines changed

gulpfile.babel.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ var sharedNeo4j = require('./test/internal/shared-neo4j').default;
4545
var ts = require('gulp-typescript');
4646
var JasmineConsoleReporter = require('jasmine-console-reporter');
4747
var karmaServer = require('karma').Server;
48+
var transformTools = require('browserify-transform-tools');
4849

4950
/**
5051
* Useful to investigate resource leaks in tests. Enable to see active sockets and file handles after the 'test' task.
@@ -66,9 +67,9 @@ gulp.task('build-browser', function () {
6667
cache: {},
6768
standalone: 'neo4j',
6869
packageCache: {}
69-
}).transform(babelify.configure({
70-
presets: ['env'], ignore: /external/
71-
})).bundle();
70+
}).transform(babelify.configure({presets: ['env'], ignore: /external/}))
71+
.transform(browserifyTransformNodeToBrowserRequire())
72+
.bundle();
7273

7374
// Un-minified browser package
7475
appBundler
@@ -87,7 +88,7 @@ gulp.task('build-browser', function () {
8788
gulp.task('build-browser-test', function(){
8889
var browserOutput = 'lib/browser/';
8990
var testFiles = [];
90-
return gulp.src('./test/**/*.test.js')
91+
return gulp.src(['./test/**/*.test.js', '!./test/**/node/*.js'])
9192
.pipe( through.obj( function( file, enc, cb ) {
9293
if(file.path.indexOf('examples.test.js') < 0) {
9394
testFiles.push( file.path );
@@ -106,6 +107,7 @@ gulp.task('build-browser-test', function(){
106107
}).transform(babelify.configure({
107108
presets: ['env'], plugins: ['transform-runtime'], ignore: /external/
108109
}))
110+
.transform(browserifyTransformNodeToBrowserRequire())
109111
.bundle(function(err, res){
110112
cb();
111113
})
@@ -164,7 +166,7 @@ gulp.task('test', function (cb) {
164166
});
165167

166168
gulp.task('test-nodejs', ['install-driver-into-sandbox'], function () {
167-
return gulp.src('test/**/*.test.js')
169+
return gulp.src(['./test/**/*.test.js', '!./test/**/browser/*.js'])
168170
.pipe(jasmine({
169171
includeStackTrace: true,
170172
reporter: newJasmineConsoleReporter()
@@ -306,3 +308,21 @@ function newJasmineConsoleReporter() {
306308
activity: false
307309
});
308310
}
311+
312+
function browserifyTransformNodeToBrowserRequire() {
313+
var nodeRequire = '/node';
314+
var browserRequire = '/browser';
315+
316+
return transformTools.makeRequireTransform('bodeToBrowserRequireTransform',
317+
{evaluateArguments: true},
318+
function (args, opts, cb) {
319+
var requireArg = args[0];
320+
var endsWithNodeRequire = requireArg.slice(-nodeRequire.length) === nodeRequire;
321+
if (endsWithNodeRequire) {
322+
var newRequireArg = requireArg.replace(nodeRequire, browserRequire);
323+
return cb(null, 'require(\'' + newRequireArg + '\')');
324+
} else {
325+
return cb();
326+
}
327+
});
328+
}

package-lock.json

Lines changed: 117 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,7 @@
2222
},
2323
"main": "lib/index.js",
2424
"browser": {
25-
"./lib/v1/internal/utf8.js": "./lib/v1/internal/browser/browser-utf8.js",
26-
"dns": false,
27-
"buffer": false,
28-
"net": false,
29-
"readline": false,
30-
"crypto": false,
31-
"tls": false
25+
"./lib/v1/internal/node/index.js": "./lib/v1/internal/browser/index.js"
3226
},
3327
"unpkg": "lib/browser/neo4j-web.js",
3428
"jsdelivr": "lib/browser/neo4j-web.js",
@@ -41,6 +35,7 @@
4135
"babel-register": "^6.26.0",
4236
"babelify": "^7.3.0",
4337
"browserify": "^13.3.0",
38+
"browserify-transform-tools": "^1.7.0",
4439
"esdoc": "^1.0.4",
4540
"esdoc-importpath-plugin": "^1.0.1",
4641
"esdoc-standard-plugin": "^1.0.0",

src/v1/internal/browser/browser-buf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* limitations under the License.
1818
*/
1919

20-
import {BaseBuffer} from '../buf';
20+
import BaseBuffer from '../buf/base-buf';
2121

2222
export default class HeapBuffer extends BaseBuffer {
2323

src/v1/internal/browser/browser-host-name-resolver.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
* limitations under the License.
1818
*/
1919

20-
import {HostNameResolver} from '../host-name-resolver';
20+
import BaseHostNameResolver from '../resolver/base-host-name-resolver';
2121

22-
export default class BrowserHostNameResolver extends HostNameResolver {
22+
export default class BrowserHostNameResolver extends BaseHostNameResolver {
2323

2424
resolve(address) {
2525
return this._resolveToItself(address);

src/v1/internal/browser/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (c) 2002-2018 "Neo4j,"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
import HeapBuffer from './browser-buf';
21+
import WebSocketChannel from './browser-channel';
22+
import BrowserHosNameResolver from './browser-host-name-resolver';
23+
import utf8Codec from './browser-utf8';
24+
25+
export const alloc = arg => new HeapBuffer(arg);
26+
export const Channel = WebSocketChannel;
27+
export const HostNameResolver = BrowserHosNameResolver;
28+
export const utf8 = utf8Codec;

0 commit comments

Comments
 (0)