cabinjs / bson-objectid Goto Github PK
View Code? Open in Web Editor NEWConstruct ObjectIDs without the mongodb driver or bson module. Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.
License: Apache License 2.0
Construct ObjectIDs without the mongodb driver or bson module. Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.
License: Apache License 2.0
I'm working on cordova based application and want to generate ObjectId
in browser. From source code I see that MACHINE_ID
is a random number and it means that I potentially may have issues when 2 clients of my application generate the same ObjectId
.
In order to overcome this issue I could use cordova-plugin-device
, retrieve uuid and use it as MACHINE_ID
. That way I will make sure that all my clients generate unique ObjectId
s.
Alternatevely it's possible to use userId
as MACHINE_ID
which is provided by my API.
I can create a PR if you are ok with suggestion:
import ObjectId from 'bson-objectid'
ObjectId.setMachineId(device.uuid)
We need to change it from if (isBuffer(id)) { return true; }
to if (isBuffer(id)) return ObjectID.isValid(id.toString('hex'));
Hi there,
keep getting
Value of type 'typeof ObjectID' is not callable
error with Typescript
snippet
import ObjectID from 'bson-objectid';
const value = 'object-id-string';
const id = ObjectID(value);
package.json
"typescript": "~4.0.5"
"bson-objectid": "^1.3.1",
Ignoring ts lint check will do the trick but still kind'a strange.
Anyone had some problems with that?
Using Vue with Typescript...
Running production build throws this error:
ERROR in C:/GIT/iAM/BridgeCareApp/VuejsApp/node_modules/bson-objectid/objectid.d.ts
21:25 Cannot use namespace 'Buffer' as a type.
19 | constructor(idString: string);
20 | constructor(array: number[]);
21 | constructor(buffer: Buffer);
| ^
22 |
23 | readonly id: string;
24 | readonly str: string;
The createFromTime
should take as an input number of seconds at the moment it is number of milliseconds
ObjectID.createFromTime(/* Should be number of seconds*/)
If you are using a typescript project and are trying to use the bson-objectid imported ObjectId it is missing 4 properties.
TS2739: Type 'ObjectID' is missing the following properties from type 'ObjectId': _bsontype, generationTime, toJSON,
Listed for convenience:
_bsontype
generationTime
toJSON
inspect
I was wondering if this library exactly replicated how mongodb generates their random objectids?
[email protected] work well, @1.2.0 tsd error
'xxx/node_modules/_bson-objectid@1.2.0@bson-objectid/objectid' resolves to a non-module entity and cannot be imported using this construct.
The .idea
folder (webstorm generated I think) is been pushed to npm, please put it in the gitignore.
Great package btw.
$ ls -a
. .. .editorconfig .idea LICENSE .npmignore objectid.d.ts objectid.js package.json README.md test typing-tests
Description
_Buffer is not defined in Node.js. If the program reaches this statement, an error will be thrown and, if uncaught, will crash the node process.
Minimal Reproduction
In Node.js REPL (tested against v16.13.2):
> _Buffer
Uncaught ReferenceError: _Buffer is not defined
Workaround
For Node.js, alias the Buffer global as _Buffer in your program entrypoint so that it is defined before calling anything in bson-objectid:
global['_Buffer'] = Buffer;
Possible Fix
Attempt to use _Buffer, else fall back to using Buffer. Just using normal Buffer global fixes things for Node.js so a global alias is not required.
Using bson-objectid with browserify, occasionally ObjectID.generate()
generates a 26 character id (and new ObjectID.toString()
returns a blank string).
I suspect it happens because the pid for browsers is set to a random number between 0 and 100000. It seems to happen when the generated pid is > 0xFFFF (ex: once when this occurred, pid was set to 81783)
I'm using this module with browserify and ObjectId.generate()
is returning strings like 565c8e5032d34b0NaNa0ad07
. Found that this is because when this line executes on the browser, process
exists but process.pid
does not.
A workaround is to use new ObjectId().toString()
, which generates a correct id.
This would come in handy if you could make this work in a browser.
Hi,
I pulled the npm package (version 1.2.3) into an Angular 5 project, using typescript. When I try to build the project I get two errors:
ERROR in node_modules/bson-objectid/objectid.d.ts(20,25): error TS2304: Cannot find name 'Buffer'.
src/app/term.service.ts(7,10): error TS2305: Module '"<path>/node_modules/bson-objectid/objectid"' has no exported member 'ObjectID'.
Does anyone know why this might happen and how to solve that problem?
> ObjectId.isValid("�½��ļ���.zip")
true
> ObjectId("�½��ļ���.zip")
ObjectID(undefinedbdundefinedundefinedundefinedundefinedundefinedundefined2e7a6970)
No matching version found for bson-objectid 1.2.4
I was hopping this could fix the issue with typescript and buffer reference.
Thanks
Hi,
I have problem with ObjectID.isValid
function which returns true for number. I think that number is not valid ObjectID or I miss something important. I found that behaviour when I update to [email protected].
There is no change log associated with this project.
ObjectID.str provided in version 1.x the ObjectID.toString() value. While still mentioned in the version 2.x readme it returns undefined.
In objectid.d.ts
, the return type of getTimestamp()
should be Date
.
Is this id Globally Unique in distributed architecture?
We found that ObjectID() allows an attacker to generate a malformed objectid by inserting an additional property to his user-input.
The vulnerable code is as follows: bson-objectid will return early if it detects _bsontype == ObjectID in user-input object. As a result, objects in arbitrary forms can bypass formatting if they have a valid bsontype.
Unlike bson-objectid, the official implementation (shown in the following code) enforces a stricter early return condition, i.e., only if the constructor of the user-input object is ObjectID.
https://github.com/mongodb/js-bson/blob/cea186763527e4d3749a8234d335e9c395effe5b/lib/objectid.js#L59-L61
Reproduce script
var ObjectID = require("bson-objectid");
var json = {
"mal_formkey": {
"payload": "xxxx"
},
"_bsontype" : "ObjectID"
};
console.log(ObjectID(json));
console.log(ObjectID.isValid(ObjectID(json)));
In objectid.d.ts the comment shows the types are still for 1.3.1
// Type definitions for bson-objectid 1.3.1
ObjectID.generate = generate
has been removed but the types are not updated.
Get error
TypeError: bson_objectid_1.default.generate is not a function
getTimestamp
should return the Date
at the moment is returning the number
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.