Skip to content

Commit eeb2662

Browse files
committed
Split Node and browser components into separate modules
This makes it easier to override Node modules in browser environment.
1 parent c2c40c9 commit eeb2662

25 files changed

+512
-375
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
},
2323
"main": "lib/index.js",
2424
"browser": {
25+
"./lib/v1/internal/utf8.js": "./lib/v1/internal/browser/browser-utf8.js",
2526
"dns": false,
2627
"buffer": false,
2728
"net": false,
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 {BaseBuffer} from '../buf';
21+
22+
export default class HeapBuffer extends BaseBuffer {
23+
24+
constructor(arg) {
25+
const buffer = arg instanceof ArrayBuffer ? arg : new ArrayBuffer(arg);
26+
super(buffer.byteLength);
27+
this._buffer = buffer;
28+
this._view = new DataView(this._buffer);
29+
}
30+
31+
putUInt8(position, val) {
32+
this._view.setUint8(position, val);
33+
}
34+
35+
getUInt8(position) {
36+
return this._view.getUint8(position);
37+
}
38+
39+
putInt8(position, val) {
40+
this._view.setInt8(position, val);
41+
}
42+
43+
getInt8(position) {
44+
return this._view.getInt8(position);
45+
}
46+
47+
getFloat64(position) {
48+
return this._view.getFloat64(position);
49+
}
50+
51+
putFloat64(position, val) {
52+
this._view.setFloat64(position, val);
53+
}
54+
55+
getSlice(start, length) {
56+
if (this._buffer.slice) {
57+
return new HeapBuffer(this._buffer.slice(start, start + length));
58+
} else {
59+
// Some platforms (eg. phantomjs) don't support slice, so fall back to a copy
60+
// We do this rather than return a SliceBuffer, because sliceBuffer cannot
61+
// be passed to native network write ops etc - we need ArrayBuffer for that
62+
const copy = new HeapBuffer(length);
63+
for (let i = 0; i < length; i++) {
64+
copy.putUInt8(i, this.getUInt8(i + start));
65+
}
66+
return copy;
67+
}
68+
}
69+
70+
/**
71+
* Specific to HeapBuffer, this gets a DataView from the
72+
* current position and of the specified length.
73+
*/
74+
readView(length) {
75+
return new DataView(this._buffer, this._updatePos(length), length);
76+
}
77+
}

src/v1/internal/ch-websocket.js renamed to src/v1/internal/browser/browser-channel.js

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

20-
import {HeapBuffer} from './buf';
21-
import {newError} from './../error';
22-
import {ENCRYPTION_OFF, ENCRYPTION_ON} from './util';
20+
import HeapBuffer from './browser-buf';
21+
import {newError} from '../../error';
22+
import {ENCRYPTION_OFF, ENCRYPTION_ON} from '../util';
2323

2424
/**
2525
* Create a new WebSocketChannel to be used in web browsers.
2626
* @access private
2727
*/
28-
class WebSocketChannel {
28+
export default class WebSocketChannel {
2929

3030
/**
3131
* Create new instance
@@ -169,9 +169,6 @@ class WebSocketChannel {
169169
}
170170
}
171171

172-
let available = typeof WebSocket !== 'undefined';
173-
let _websocketChannelModule = {channel: WebSocketChannel, available: available};
174-
175172
function createWebSocket(scheme, parsedUrl) {
176173
const url = scheme + '://' + parsedUrl.hostAndPort;
177174

@@ -305,5 +302,3 @@ function verifyEncryptionSettings(encryptionOn, encryptionOff, secureProtocol) {
305302
function detectWebPageProtocol() {
306303
return window && window.location ? window.location.protocol : null;
307304
}
308-
309-
export default _websocketChannelModule
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 {HostNameResolver} from '../host-name-resolver';
21+
22+
export default class BrowserHostNameResolver extends HostNameResolver {
23+
24+
resolve(address) {
25+
return this._resolveToItself(address);
26+
}
27+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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/browser-buf';
21+
import textEncoding from 'text-encoding';
22+
23+
const encoder = new textEncoding.TextEncoder('utf-8');
24+
const decoder = new textEncoding.TextDecoder('utf-8');
25+
26+
function encode(str) {
27+
return new HeapBuffer(encoder.encode(str).buffer);
28+
}
29+
30+
function decode(buffer, length) {
31+
if (buffer instanceof HeapBuffer) {
32+
return decoder.decode(buffer.readView(Math.min(length, buffer.length - buffer.position)));
33+
} else {
34+
// Copy the given buffer into a regular buffer and decode that
35+
const tmpBuf = new HeapBuffer(length);
36+
for (let i = 0; i < length; i++) {
37+
tmpBuf.writeUInt8(buffer.readUInt8());
38+
}
39+
tmpBuf.reset();
40+
return decoder.decode(tmpBuf.readView(length));
41+
}
42+
}
43+
44+
export default {
45+
encode,
46+
decode
47+
};

src/v1/internal/buf.js

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

20-
import Platform from './platform';
21-
import node from 'buffer';
20+
import Feature from './feature';
21+
import NodeBuffer from './node/node-buf';
22+
import HeapBuffer from './browser/browser-buf';
2223

2324
/**
2425
* This module defines a common API for dealing with binary data that
@@ -365,66 +366,6 @@ class BaseBuffer
365366
}
366367
}
367368

368-
/**
369-
* Basic buffer implementation that should work in most any modern JS env.
370-
* @access private
371-
*/
372-
class HeapBuffer extends BaseBuffer {
373-
constructor (arg) {
374-
let buffer = arg instanceof ArrayBuffer ? arg : new ArrayBuffer(arg)
375-
super(buffer.byteLength );
376-
this._buffer = buffer;
377-
this._view = new DataView( this._buffer );
378-
}
379-
380-
putUInt8 ( position, val ) {
381-
this._view.setUint8(position, val);
382-
}
383-
384-
getUInt8 ( position ) {
385-
return this._view.getUint8(position);
386-
}
387-
388-
putInt8 ( position, val ) {
389-
this._view.setInt8(position, val);
390-
}
391-
392-
getInt8 ( position ) {
393-
return this._view.getInt8(position);
394-
}
395-
396-
getFloat64 ( position ) {
397-
return this._view.getFloat64(position);
398-
}
399-
400-
putFloat64 ( position, val ) {
401-
this._view.setFloat64( position, val );
402-
}
403-
404-
getSlice ( start, length ) {
405-
if( this._buffer.slice ) {
406-
return new HeapBuffer( this._buffer.slice( start, start + length ) );
407-
} else {
408-
// Some platforms (eg. phantomjs) don't support slice, so fall back to a copy
409-
// We do this rather than return a SliceBuffer, because sliceBuffer cannot
410-
// be passed to native network write ops etc - we need ArrayBuffer for that
411-
let copy = new HeapBuffer(length);
412-
for (var i = 0; i < length; i++) {
413-
copy.putUInt8( i, this.getUInt8( i + start ) );
414-
}
415-
return copy;
416-
}
417-
}
418-
419-
/**
420-
* Specific to HeapBuffer, this gets a DataView from the
421-
* current position and of the specified length.
422-
*/
423-
readView ( length ) {
424-
return new DataView( this._buffer, this._updatePos(length), length );
425-
}
426-
}
427-
428369
/**
429370
* Represents a view as slice of another buffer.
430371
* @access private
@@ -510,73 +451,7 @@ class CombinedBuffer extends BaseBuffer {
510451
}
511452
}
512453

513-
/**
514-
* Buffer used in a Node.js environment
515-
* @access private
516-
*/
517-
class NodeBuffer extends BaseBuffer {
518-
constructor(arg) {
519-
const buffer = newNodeJSBuffer(arg);
520-
super(buffer.length);
521-
this._buffer = buffer;
522-
}
523-
524-
getUInt8 (position) {
525-
return this._buffer.readUInt8( position );
526-
}
527-
528-
getInt8 (position) {
529-
return this._buffer.readInt8( position );
530-
}
531-
532-
getFloat64 (position) {
533-
return this._buffer.readDoubleBE(position);
534-
}
535-
536-
putUInt8 (position, val) {
537-
this._buffer.writeUInt8( val, position );
538-
}
539-
540-
putInt8 (position, val) {
541-
this._buffer.writeInt8( val, position );
542-
}
543-
544-
putFloat64 ( position, val ) {
545-
this._buffer.writeDoubleBE( val, position );
546-
}
547-
548-
putBytes ( position, val ) {
549-
if( val instanceof NodeBuffer ) {
550-
let bytesToCopy = Math.min( val.length - val.position, this.length - position );
551-
val._buffer.copy(
552-
this._buffer,
553-
position,
554-
val.position,
555-
val.position + bytesToCopy );
556-
val.position += bytesToCopy;
557-
} else {
558-
super.putBytes(position, val);
559-
}
560-
};
561-
562-
getSlice ( start, length ) {
563-
return new NodeBuffer( this._buffer.slice( start, start + length ) );
564-
}
565-
}
566-
567-
function newNodeJSBuffer(arg) {
568-
if (arg instanceof node.Buffer) {
569-
return arg;
570-
} else if (typeof arg === 'number' && typeof node.Buffer.alloc === 'function') {
571-
// use static factory function present in newer NodeJS versions to allocate new buffer with specified size
572-
return node.Buffer.alloc(arg);
573-
} else {
574-
// fallback to the old, potentially deprecated constructor
575-
return new node.Buffer(arg);
576-
}
577-
}
578-
579-
const _DefaultBuffer = Platform.nodeBufferAvailable() ? NodeBuffer : HeapBuffer;
454+
const DefaultBuffer = Feature.nodeBufferAvailable() ? NodeBuffer : HeapBuffer;
580455

581456
/**
582457
* Allocate a new buffer using whatever mechanism is most sensible for the current platform.
@@ -585,14 +460,12 @@ const _DefaultBuffer = Platform.nodeBufferAvailable() ? NodeBuffer : HeapBuffer;
585460
* @return {BaseBuffer} new buffer
586461
*/
587462
function alloc (size) {
588-
return new _DefaultBuffer(size);
463+
return new DefaultBuffer(size);
589464
}
590465

591466
export {
592467
BaseBuffer,
593-
HeapBuffer,
594468
SliceBuffer,
595469
CombinedBuffer,
596-
NodeBuffer,
597470
alloc
598471
}

0 commit comments

Comments
 (0)