MLE-22707 : Vector base64 encode and decode functions in the MarkLogic Node.js client#938
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR implements vector base64 encoding and decoding functionality for the MarkLogic Node.js client. The changes add client-side utilities to convert between floating-point vector arrays and base64-encoded binary representations, enabling interoperability with MarkLogic's server-side vector functions.
- Added
vector-util.jslibrary withbase64Encodeandbase64Decodefunctions - Implemented comprehensive test suite covering client-side encoding/decoding and cross-compatibility with server-side functions
- Added validation for unsupported vector versions in the decode function
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 10 comments.
| File | Description |
|---|---|
lib/vector-util.js |
Core implementation of vector base64 encoding/decoding utilities with buffer manipulation |
test-basic/vector-util-test.js |
Complete test suite validating encoding/decoding accuracy and server-client interoperability |
test-basic/vector-util-test.js
Outdated
| const input = 'AAAAAAMAAADD9UhAH4XLP5qZKUA='; | ||
| const decoded = vectorUtil.base64Decode(input); | ||
| try { | ||
| assert(Object.getPrototypeOf(decoded) == Array.prototype); |
There was a problem hiding this comment.
Use Array.isArray(decoded) instead of Object.getPrototypeOf(decoded) == Array.prototype for more reliable array type checking.
| assert(Object.getPrototypeOf(decoded) == Array.prototype); | |
| assert(Array.isArray(decoded)); |
test-basic/vector-util-test.js
Outdated
| const input = response.rows[0].t.value; | ||
| const decoded = vectorUtil.base64Decode(input); | ||
| assert(input =='AAAAAAEAAABvEgM7'); | ||
| assert(Object.getPrototypeOf(decoded) == Array.prototype); |
There was a problem hiding this comment.
Use Array.isArray(decoded) instead of Object.getPrototypeOf(decoded) == Array.prototype for more reliable array type checking.
| assert(Object.getPrototypeOf(decoded) == Array.prototype); | |
| assert(Array.isArray(decoded)); |
test-basic/vector-util-test.js
Outdated
| .then(function(response) { | ||
| const input = response.rows[0].t.value; | ||
| const decoded = vectorUtil.base64Decode(input); | ||
| assert(input =='AAAAAAEAAABvEgM7'); |
There was a problem hiding this comment.
Use assert.strictEqual() instead of loose equality comparison for more precise testing.
| assert(input =='AAAAAAEAAABvEgM7'); | |
| assert.strictEqual(input, 'AAAAAAEAAABvEgM7'); |
test-basic/vector-util-test.js
Outdated
| assert(Object.getPrototypeOf(decoded) == Array.prototype); | ||
| assert(decoded[0] == 3.140000104904175); | ||
| assert(decoded[1] == 1.590000033378601); | ||
| assert(decoded[2] == 2.6500000953674316); |
There was a problem hiding this comment.
Use assert.strictEqual() instead of loose equality comparison for more precise testing.
| assert(Object.getPrototypeOf(decoded) == Array.prototype); | |
| assert(decoded[0] == 3.140000104904175); | |
| assert(decoded[1] == 1.590000033378601); | |
| assert(decoded[2] == 2.6500000953674316); | |
| assert(Object.getPrototypeOf(decoded) === Array.prototype); | |
| assert(Math.abs(decoded[0] - 3.140000104904175) < delta, 'Value mismatch for decoded[0]'); | |
| assert(Math.abs(decoded[1] - 1.590000033378601) < delta, 'Value mismatch for decoded[1]'); | |
| assert(Math.abs(decoded[2] - 2.6500000953674316) < delta, 'Value mismatch for decoded[2]'); |
test-basic/vector-util-test.js
Outdated
| assert(decoded[0] == 3.140000104904175); | ||
| assert(decoded[1] == 1.590000033378601); | ||
| assert(decoded[2] == 2.6500000953674316); |
There was a problem hiding this comment.
Use assert.strictEqual() instead of loose equality comparison for more precise testing.
| assert(decoded[0] == 3.140000104904175); | |
| assert(decoded[1] == 1.590000033378601); | |
| assert(decoded[2] == 2.6500000953674316); | |
| assert.strictEqual(decoded[0], 3.140000104904175); | |
| assert.strictEqual(decoded[1], 1.590000033378601); | |
| assert.strictEqual(decoded[2], 2.6500000953674316); |
test-basic/vector-util-test.js
Outdated
| assert(Object.getPrototypeOf(decoded) == Array.prototype); | ||
| assert(decoded[0] == 3.140000104904175); | ||
| assert(decoded[1] == 1.590000033378601); | ||
| assert(decoded[2] == 2.6500000953674316); |
There was a problem hiding this comment.
Use assert.strictEqual() instead of loose equality comparison for more precise testing.
| assert(decoded[2] == 2.6500000953674316); | |
| assert(Math.abs(decoded[2] - 2.6500000953674316) < delta, 'Value mismatch for decoded[2]'); |
test-basic/vector-util-test.js
Outdated
| assert(Object.getPrototypeOf(decoded) == Array.prototype); | ||
| assert(decoded[0] == 0.0020000000949949026); |
There was a problem hiding this comment.
Use assert.strictEqual() instead of loose equality comparison for more precise testing.
| assert(Object.getPrototypeOf(decoded) == Array.prototype); | |
| assert(decoded[0] == 0.0020000000949949026); | |
| assert(Object.getPrototypeOf(decoded) === Array.prototype); | |
| assert(Math.abs(decoded[0] - 0.0020000000949949026) < delta, 'Value mismatch'); |
test-basic/vector-util-test.js
Outdated
| const input = vectorUtil.base64Encode(vector); | ||
| const vectorString = '[ '+vector[0].toString()+', '+vector[1].toString()+', '+vector[2].toString()+' ]'; | ||
| dbWriter.eval(`vec.base64Decode('${input}')`).result(res=>{ | ||
| assert(res[0].value == vectorString); |
There was a problem hiding this comment.
Use assert.strictEqual() instead of loose equality comparison for more precise testing.
| assert(res[0].value == vectorString); | |
| assert.strictEqual(res[0].value, vectorString); |
| 'use strict'; | ||
| const { Buffer } = require('buffer'); | ||
|
|
||
| const base64Encode = (vector) => { |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| }; | ||
|
|
||
| const base64Decode = (encodedVector) => { | ||
|
|
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
rjrudin
left a comment
There was a problem hiding this comment.
Looks good, though I think all the Copilot suggestions are good ones, especially the ones to assert using "strict equals".
No description provided.