-
Notifications
You must be signed in to change notification settings - Fork 258
fix: Fix backwards compatibility with older BSON package versions #411
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
'use strict'; | ||
|
||
const newBSON = require('./register-bson'); | ||
const fs = require('fs'); | ||
const fetch = require('node-fetch').default; | ||
const rimraf = require('rimraf'); | ||
const cp = require('child_process'); | ||
|
||
/* | ||
* This file tests that previous versions of BSON | ||
* serialize and deserialize correctly in the most recent version of BSON | ||
* | ||
* This is an unusual situation to run into as users should be using one BSON lib version | ||
* but it does arise with sub deps etc. and we wish to protect against unexpected behavior | ||
* | ||
* If backwards compatibility breaks there should be clear warnings/failures | ||
* rather than empty or zero-ed values. | ||
*/ | ||
|
||
const OLD_VERSIONS = ['v1.1.5', 'v1.1.4']; | ||
const getZipUrl = ver => `https://github.com/mongodb/js-bson/archive/${ver}.zip`; | ||
const getImportPath = ver => `../bson-${ver}/js-bson-${ver.substring(1)}`; | ||
|
||
function downloadZip(version, done) { | ||
// downloads a zip of previous BSON version | ||
fetch(getZipUrl(version)) | ||
.then(r => { | ||
return r.arrayBuffer(); | ||
}) | ||
.then(r => { | ||
fs.writeFileSync(`bson-${version}.zip`, new Uint8Array(r)); | ||
try { | ||
// unzips the code, right now these test won't handle versions written in TS | ||
cp.execSync(`unzip bson-${version}.zip -d bson-${version}`); | ||
} catch (err) { | ||
return done(err); | ||
} | ||
done(); | ||
}); | ||
} | ||
|
||
describe('Current version', function () { | ||
OLD_VERSIONS.forEach(version => { | ||
before(function (done) { | ||
if (Number(process.version.split('.')[0].substring(1)) < 8) { | ||
// WHATWG fetch doesn't download correctly prior to node 8 | ||
// but we should be safe by testing on node 8 + | ||
this.skip(); | ||
} | ||
if (fs.existsSync(`bson-${version}.zip`)) { | ||
fs.unlinkSync(`bson-${version}.zip`); | ||
rimraf(`./bson-${version}`, err => { | ||
if (err) done(err); | ||
|
||
// download old versions | ||
downloadZip(version, done); | ||
}); | ||
} else { | ||
// download old versions | ||
downloadZip(version, done); | ||
} | ||
}); | ||
|
||
after(function (done) { | ||
try { | ||
fs.unlinkSync(`bson-${version}.zip`); | ||
} catch (e) { | ||
// ignore | ||
} | ||
rimraf(`./bson-${version}`, err => { | ||
if (err) done(err); | ||
done(); | ||
}); | ||
}); | ||
|
||
it(`serializes correctly against ${version} Binary class`, function () { | ||
const oldBSON = require(getImportPath(version)); | ||
const binFromNew = { | ||
binary: new newBSON.Binary('aaaa') | ||
}; | ||
const binFromOld = { | ||
binary: new oldBSON.Binary('aaaa') | ||
}; | ||
expect(oldBSON.prototype.serialize(binFromNew).toString('hex')).to.equal( | ||
newBSON.serialize(binFromOld).toString('hex') | ||
); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a comment block somewhere at the top of this file would be useful to future maintainers, explaining what it is doing and why it's needed.