Skip to content

Improve performance for packstream in browser. #809

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

Merged
merged 4 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions packages/bolt-connection/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/bolt-connection/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"typescript": "^4.4.4"
},
"dependencies": {
"buffer": "^6.0.3",
"neo4j-driver-core": "^4.4.0-dev",
"text-encoding-utf-8": "^1.0.2"
"string_decoder": "^1.3.0"
}
}
76 changes: 0 additions & 76 deletions packages/bolt-connection/src/channel/browser/browser-buf.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* limitations under the License.
*/

import HeapBuffer from './browser-buf'
import ChannelBuffer from '../channel-buf'
import { newError, internal } from 'neo4j-driver-core'

const {
Expand Down Expand Up @@ -83,7 +83,7 @@ export default class WebSocketChannel {
}
this._ws.onmessage = event => {
if (self.onmessage) {
const b = new HeapBuffer(event.data)
const b = new ChannelBuffer(event.data)
self.onmessage(b)
}
}
Expand Down Expand Up @@ -130,14 +130,14 @@ export default class WebSocketChannel {

/**
* Write the passed in buffer to connection
* @param {HeapBuffer} buffer - Buffer to write
* @param {ChannelBuffer} buffer - Buffer to write
*/
write (buffer) {
// If there is a pending queue, push this on that queue. This means
// we are not yet connected, so we queue things locally.
if (this._pending !== null) {
this._pending.push(buffer)
} else if (buffer instanceof HeapBuffer) {
} else if (buffer instanceof ChannelBuffer) {
try {
this._ws.send(buffer._buffer)
} catch (error) {
Expand Down
49 changes: 0 additions & 49 deletions packages/bolt-connection/src/channel/browser/browser-utf8.js

This file was deleted.

5 changes: 0 additions & 5 deletions packages/bolt-connection/src/channel/browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
* limitations under the License.
*/

import HeapBuffer from './browser-buf'
import WebSocketChannel from './browser-channel'
import BrowserHosNameResolver from './browser-host-name-resolver'
import utf8Codec from './browser-utf8'

/*

Expand All @@ -32,8 +30,5 @@ Such imports are replaced at build time with `browser/index.js` when building a
NOTE: exports in this module should have exactly the same names/structure as exports in `node/index.js`.

*/

export const alloc = arg => new HeapBuffer(arg)
export const Channel = WebSocketChannel
export const HostNameResolver = BrowserHosNameResolver
export const utf8 = utf8Codec
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
* limitations under the License.
*/

import node from 'buffer'
import BaseBuffer from '../../buf'
import buffer from 'buffer'
import BaseBuffer from '../buf'

export default class NodeBuffer extends BaseBuffer {
export default class ChannelBuffer extends BaseBuffer {
constructor (arg) {
const buffer = newNodeJSBuffer(arg)
const buffer = newChannelJSBuffer(arg)
super(buffer.length)
this._buffer = buffer
}
Expand Down Expand Up @@ -52,7 +52,7 @@ export default class NodeBuffer extends BaseBuffer {
}

putBytes (position, val) {
if (val instanceof NodeBuffer) {
if (val instanceof ChannelBuffer) {
const bytesToCopy = Math.min(
val.length - val.position,
this.length - position
Expand All @@ -70,22 +70,33 @@ export default class NodeBuffer extends BaseBuffer {
}

getSlice (start, length) {
return new NodeBuffer(this._buffer.slice(start, start + length))
return new ChannelBuffer(this._buffer.slice(start, start + length))
}
}

function newNodeJSBuffer (arg) {
if (arg instanceof node.Buffer) {
/**
* Allocate a buffer
*
* @param {number} size The buffer sizzer
* @returns {BaseBuffer} The buffer
*/
export function alloc (size) {
return new ChannelBuffer(size)
}


function newChannelJSBuffer (arg) {
if (arg instanceof buffer.Buffer) {
return arg
} else if (
typeof arg === 'number' &&
typeof node.Buffer.alloc === 'function'
typeof buffer.Buffer.alloc === 'function'
) {
// use static factory function present in newer NodeJS versions to allocate new buffer with specified size
return node.Buffer.alloc(arg)
return buffer.Buffer.alloc(arg)
} else {
// fallback to the old, potentially deprecated constructor
// eslint-disable-next-line node/no-deprecated-api
return new node.Buffer(arg)
return new buffer.Buffer(arg)
}
}
2 changes: 1 addition & 1 deletion packages/bolt-connection/src/channel/chunking.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import BaseBuffer from '../buf/base-buf'
import { alloc } from './node'
import { alloc } from './channel-buf'
import CombinedBuffer from './combined-buf'

const _CHUNK_HEADER_SIZE = 2
Expand Down
2 changes: 1 addition & 1 deletion packages/bolt-connection/src/channel/combined-buf.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { BaseBuffer } from '../buf'
import { alloc } from './node'
import { alloc } from './channel-buf'

/**
* Buffer that combines multiple buffers, exposing them as one single buffer.
Expand Down
5 changes: 3 additions & 2 deletions packages/bolt-connection/src/channel/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import ChannelConfig from './channel-config'

/**
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
Expand All @@ -18,6 +16,9 @@ import ChannelConfig from './channel-config'
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export * from './node'
export * from './chunking'
export { default as ChannelConfig } from './channel-config'
export { alloc } from './channel-buf'
export { default as utf8 } from './utf8'
5 changes: 1 addition & 4 deletions packages/bolt-connection/src/channel/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
* limitations under the License.
*/

import NodeBuffer from './node-buf'
import NodeChannel from './node-channel'
import NodeHostNameResolver from './node-host-name-resolver'
import utf8Codec from './node-utf8'

/*

Expand All @@ -33,7 +31,6 @@ NOTE: exports in this module should have exactly the same names/structure as exp

*/

export const alloc = arg => new NodeBuffer(arg)
export const Channel = NodeChannel
export const HostNameResolver = NodeHostNameResolver
export const utf8 = utf8Codec

Loading