├── .eslintignore ├── .eslintrc.json ├── .github ├── release-please.yml └── workflows │ ├── ci.yaml │ └── release.yml ├── .gitignore ├── .mocharc.json ├── .prettierignore ├── .prettierrc.js ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── benchmark └── benchmark.js ├── package-lock.json ├── package.json ├── renovate.json ├── src └── index.ts ├── test └── test.ts └── tsconfig.json /.eslintignore: -------------------------------------------------------------------------------- 1 | **/node_modules 2 | build/ 3 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./node_modules/gts" 3 | } 4 | -------------------------------------------------------------------------------- /.github/release-please.yml: -------------------------------------------------------------------------------- 1 | releaseType: node 2 | handleGHRelease: true 3 | primaryBranch: main 4 | -------------------------------------------------------------------------------- /.github/workflows/ci.yaml: -------------------------------------------------------------------------------- 1 | on: 2 | push: 3 | branches: 4 | - main 5 | pull_request: 6 | name: ci 7 | jobs: 8 | test: 9 | runs-on: ubuntu-latest 10 | strategy: 11 | matrix: 12 | node: [10, 12, 14, 15] 13 | steps: 14 | - uses: actions/checkout@v2 15 | - uses: actions/setup-node@v2 16 | with: 17 | node-version: ${{ matrix.node }} 18 | - run: node --version 19 | - run: npm ci 20 | - run: npm test 21 | - name: coverage 22 | uses: codecov/codecov-action@v1 23 | with: 24 | name: actions ${{ matrix.node }} 25 | windows: 26 | runs-on: windows-latest 27 | steps: 28 | - uses: actions/checkout@v2 29 | - uses: actions/setup-node@v2 30 | with: 31 | node-version: 14 32 | - run: npm ci 33 | - run: npm test 34 | lint: 35 | runs-on: ubuntu-latest 36 | steps: 37 | - uses: actions/checkout@v2 38 | - uses: actions/setup-node@v2 39 | with: 40 | node-version: 14 41 | - run: npm ci 42 | - run: npm run lint 43 | license_check: 44 | runs-on: ubuntu-latest 45 | steps: 46 | - uses: actions/checkout@v2 47 | - uses: actions/setup-node@v2 48 | with: 49 | node-version: 14 50 | - run: npm ci 51 | - run: npm run license-check 52 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | on: 2 | release: 3 | types: [published] 4 | name: release 5 | jobs: 6 | release-please: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@v2 10 | - uses: actions/setup-node@v2 11 | with: 12 | node-version: 14 13 | registry-url: 'https://wombat-dressing-room.appspot.com' 14 | - run: npm ci 15 | - run: npm publish 16 | env: 17 | NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}} 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | coverage 4 | build 5 | -------------------------------------------------------------------------------- /.mocharc.json: -------------------------------------------------------------------------------- 1 | { 2 | "check-leaks": true, 3 | "timeout": 60000, 4 | "throw-deprecation": true, 5 | "enable-source-maps": true 6 | } 7 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | coverage/ 2 | build/ 3 | **/node_modules -------------------------------------------------------------------------------- /.prettierrc.js: -------------------------------------------------------------------------------- 1 | // Copyright 2020 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | module.exports = { 16 | ...require('gts/.prettierrc.json') 17 | } 18 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ### [2.0.1](https://www.github.com/google/eventid-js/compare/v2.0.0...v2.0.1) (2021-12-06) 4 | 5 | 6 | ### Bug Fixes 7 | 8 | * **deps:** remove dependency on d64 ([#161](https://www.github.com/google/eventid-js/issues/161)) ([58753da](https://www.github.com/google/eventid-js/commit/58753da30cb4443395c4436ac95471ca2d2de6aa)) 9 | 10 | ## [2.0.0](https://www.github.com/google/eventid-js/compare/v1.0.0...v2.0.0) (2021-08-31) 11 | 12 | 13 | ### ⚠ BREAKING CHANGES 14 | 15 | * require node.js 10 and up (#88) 16 | 17 | ### Bug Fixes 18 | 19 | * **deps:** update dependency uuid to v7 ([#79](https://www.github.com/google/eventid-js/issues/79)) ([1e8c47f](https://www.github.com/google/eventid-js/commit/1e8c47f982ecadad583952847bcc3919d8ca498e)) 20 | * **deps:** update dependency uuid to v8 ([#92](https://www.github.com/google/eventid-js/issues/92)) ([0a5ecea](https://www.github.com/google/eventid-js/commit/0a5eceae8dca5ae751fbee7dc0c3822e707af826)) 21 | 22 | 23 | ### Build System 24 | 25 | * require node.js 10 and up ([#88](https://www.github.com/google/eventid-js/issues/88)) ([c0cea90](https://www.github.com/google/eventid-js/commit/c0cea904922fc8e34a13ace7777cca54e6cff30a)) 26 | 27 | ## [1.0.0](https://www.github.com/google/eventid-js/compare/v0.1.2...v1.0.0) (2019-11-05) 28 | 29 | 30 | ### ⚠ BREAKING CHANGES 31 | 32 | * Stop supporting Node 6 as it reaches EOL. 33 | * drop support for node.js 4.x and 9.x (#25) 34 | 35 | ### Features 36 | 37 | * convert to TypeScript ([#24](https://www.github.com/google/eventid-js/issues/24)) ([18b8d24](https://www.github.com/google/eventid-js/commit/18b8d24ea0eaf878cf2beef1b153da9fae1cc691)) 38 | 39 | 40 | ### Build System 41 | 42 | * drop support for Node 6 ([#41](https://www.github.com/google/eventid-js/issues/41)) ([41409f4](https://www.github.com/google/eventid-js/commit/41409f4ac4f4591b1e67767ba14254d92247c152)) 43 | 44 | 45 | ### Miscellaneous Chores 46 | 47 | * drop support for node.js 4.x and 9.x ([#25](https://www.github.com/google/eventid-js/issues/25)) ([f69a1b0](https://www.github.com/google/eventid-js/commit/f69a1b0630bbf8b7d6b163795915558819f8e5c1)) 48 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, 4 | and in the interest of fostering an open and welcoming community, 5 | we pledge to respect all people who contribute through reporting issues, 6 | posting feature requests, updating documentation, 7 | submitting pull requests or patches, and other activities. 8 | 9 | We are committed to making participation in this project 10 | a harassment-free experience for everyone, 11 | regardless of level of experience, gender, gender identity and expression, 12 | sexual orientation, disability, personal appearance, 13 | body size, race, ethnicity, age, religion, or nationality. 14 | 15 | Examples of unacceptable behavior by participants include: 16 | 17 | * The use of sexualized language or imagery 18 | * Personal attacks 19 | * Trolling or insulting/derogatory comments 20 | * Public or private harassment 21 | * Publishing other's private information, 22 | such as physical or electronic 23 | addresses, without explicit permission 24 | * Other unethical or unprofessional conduct. 25 | 26 | Project maintainers have the right and responsibility to remove, edit, or reject 27 | comments, commits, code, wiki edits, issues, and other contributions 28 | that are not aligned to this Code of Conduct. 29 | By adopting this Code of Conduct, 30 | project maintainers commit themselves to fairly and consistently 31 | applying these principles to every aspect of managing this project. 32 | Project maintainers who do not follow or enforce the Code of Conduct 33 | may be permanently removed from the project team. 34 | 35 | This code of conduct applies both within project spaces and in public spaces 36 | when an individual is representing the project or its community. 37 | 38 | Instances of abusive, harassing, or otherwise unacceptable behavior 39 | may be reported by opening an issue 40 | or contacting one or more of the project maintainers. 41 | 42 | This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0, 43 | available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/) 44 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to become a contributor and submit your own code 2 | 3 | ## Contributor License Agreements 4 | 5 | We'd love to accept your patches! Before we can take them, we have to jump a couple of legal hurdles. 6 | 7 | Please fill out either the individual or corporate Contributor License Agreement (CLA). 8 | 9 | * If you are an individual writing original source code and you're sure you own the intellectual property, then you'll need to sign an [individual CLA](http://code.google.com/legal/individual-cla-v1.0.html). 10 | * If you work for a company that wants to allow you to contribute your work, then you'll need to sign a [corporate CLA](http://code.google.com/legal/corporate-cla-v1.0.html). 11 | 12 | Follow either of the two links above to access the appropriate CLA and instructions for how to sign and return it. Once we receive it, we'll be able to accept your pull requests. 13 | 14 | ## Contributing A Patch 15 | 16 | 1. Submit an issue describing your proposed change to the repo in question. 17 | 1. The repo owner will respond to your issue promptly. 18 | 1. If your proposed change is accepted, and you haven't already done so, sign a Contributor License Agreement (see details above). 19 | 1. Fork the desired repo, develop and test your code changes. 20 | 1. Submit a pull request. 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2014 Google Inc. 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # eventid 2 | > Monotonically increasing per machine, globally unique eventids 3 | 4 | [![npm version](https://img.shields.io/npm/v/eventid.svg)](https://www.npmjs.org/package/eventid) 5 | [![Build Status](https://github.com/google/eventid-js/workflows/ci/badge.svg)](https://github.com/google/eventid-js/actions) 6 | [![Dependencies](https://david-dm.org/google/eventid-js.svg)](https://david-dm.org/google/eventid-js) 7 | [![Known Vulnerabilities](https://snyk.io/test/github/google/eventid-js/badge.svg)](https://snyk.io/test/github/google/eventid-js) 8 | 9 | ***Note: This is not an official Google product.*** 10 | 11 | An eventId uniquely identifies an event across a network of services. It is 12 | globally unique, and is monotically increasing locally. This makes eventids 13 | useful for lexically comparable identifiers for events in a distributed system. 14 | 15 | This can be used instead of timestamps – JavaScript timestamps only have 16 | millisecond resolution making them unsuitable for the purpose of building 17 | monotonically increasing local ids. 18 | 19 | 20 | ## Installation 21 | 22 | ```sh 23 | $ npm install eventid 24 | ``` 25 | 26 | ## Usage 27 | 28 | ```js 29 | const EventId = require('eventid'); 30 | 31 | // Instantiate a generator. 32 | const eventId = new EventId(); 33 | 34 | // Generate a globally unique identifier. 35 | const id1 = eventId.new(); // -> "..........37qqNkj4K24ulWyeuWxpZh" 36 | // Use the same generator to get monotonically increasing local ids. 37 | const id2 = eventId.new(); // -> "..........77qqNkj4K24ulWyeuWxpZh" 38 | // You can lexicographically compare the ids. 39 | assert(id1 < id2); // -> true 40 | 41 | // Another instance will use a different guid 42 | const another = new EventId(); 43 | const id3 = another.new(); // -> "..........5rkLYOc5W8ZAHAmVSyrixJ" 44 | ``` 45 | 46 | ## License 47 | 48 | [Apache 2.0](LICENSE) 49 | -------------------------------------------------------------------------------- /benchmark/benchmark.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2017 Google Inc. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 'use strict'; 18 | 19 | // eslint-disable-next-line node/no-missing-require 20 | const EventId = require('../src'); 21 | const eid = new EventId(); 22 | for (let i = 0; i < 1e7; i++) { 23 | eid.new(); 24 | } 25 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "eventid", 3 | "version": "2.0.1", 4 | "description": "A utility for generating monotonically increasing unique event ids across a network of services.", 5 | "main": "build/src/index.js", 6 | "types": "build/src/index.d.ts", 7 | "files": [ 8 | "build/src" 9 | ], 10 | "scripts": { 11 | "test": "c8 mocha build/test", 12 | "lint": "gts check", 13 | "license-check": "jsgl --local .", 14 | "clean": "gts clean", 15 | "compile": "tsc -p .", 16 | "fix": "gts fix", 17 | "prepare": "npm run compile", 18 | "pretest": "npm run compile" 19 | }, 20 | "repository": { 21 | "type": "git", 22 | "url": "git+https://github.com/google/eventid-js.git" 23 | }, 24 | "keywords": [ 25 | "uid" 26 | ], 27 | "author": "Google Inc.", 28 | "license": "Apache-2.0", 29 | "bugs": { 30 | "url": "https://github.com/google/eventid-js/issues" 31 | }, 32 | "homepage": "https://github.com/google/eventid-js#readme", 33 | "dependencies": { 34 | "uuid": "^8.0.0" 35 | }, 36 | "devDependencies": { 37 | "@types/mocha": "^9.0.0", 38 | "@types/node": "^16.0.0", 39 | "@types/uuid": "^8.0.0", 40 | "c8": "^7.3.0", 41 | "gts": "^3.0.0", 42 | "js-green-licenses": "^3.0.0", 43 | "mocha": "^9.0.0", 44 | "typescript": "^4.0.0" 45 | }, 46 | "engines": { 47 | "node": ">=10" 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "config:base", 4 | "docker:disable" 5 | ], 6 | "pinVersions": false, 7 | "rebaseStalePrs": true, 8 | "schedule": [ 9 | "after 10pm and before 5am" 10 | ], 11 | "lockFileMaintenance": { 12 | "enabled": true, 13 | "recreateClosed": true, 14 | "schedule": [ 15 | "before 10am on tuesday" 16 | ] 17 | }, 18 | "gitAuthor": null 19 | } 20 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | import * as uuid from 'uuid'; 16 | 17 | // This function is pulled directly from the d64 module: 18 | // https://github.com/dominictarr/d64 19 | // That module hasn't been updated in 8 years, and misuses the Buffer package. 20 | // See issue here: https://github.com/google/eventid-js/issues/160 21 | const chars = '.PYFGCRLAOEUIDHTNSQJKXBMWVZ_pyfgcrlaoeuidhtnsqjkxbmwvz1234567890' 22 | .split('') 23 | .sort() 24 | .join(''); 25 | 26 | function encode(data: Uint8Array) { 27 | let s = ''; 28 | const l = data.length; 29 | let hang = 0; 30 | for (let i = 0; i < l; i++) { 31 | const v = data[i]; 32 | switch (i % 3) { 33 | case 0: 34 | s += chars[v >> 2]; 35 | hang = (v & 3) << 4; 36 | break; 37 | case 1: 38 | s += chars[hang | (v >> 4)]; 39 | hang = (v & 0xf) << 2; 40 | break; 41 | case 2: 42 | s += chars[hang | (v >> 6)]; 43 | s += chars[v & 0x3f]; 44 | hang = 0; 45 | break; 46 | } 47 | } 48 | if (l % 3) s += chars[hang]; 49 | return s; 50 | } 51 | 52 | export class EventId { 53 | b: Uint8Array; 54 | constructor() { 55 | this.b = new Uint8Array(24); 56 | uuid.v4(null, this.b, 8); 57 | } 58 | 59 | new() { 60 | for (let i = 7; i >= 0; i--) { 61 | if (this.b[i] !== 255) { 62 | this.b[i]++; 63 | break; 64 | } 65 | this.b[i] = 0; 66 | } 67 | return encode(this.b); 68 | } 69 | } 70 | 71 | // preserve `const EventId = require('EventId')` syntax 72 | const existingExports = module.exports; 73 | module.exports = EventId; 74 | module.exports = Object.assign(module.exports, existingExports); 75 | -------------------------------------------------------------------------------- /test/test.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2017 Google Inc. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import * as assert from 'assert'; 18 | import {describe, it} from 'mocha'; 19 | import {EventId} from '../src'; 20 | 21 | describe('eventid', () => { 22 | it('should generate monotonically increasing numbers', () => { 23 | const eid = new EventId(); 24 | let previous = eid.new(); 25 | for (let i = 0; i < 20; i++) { 26 | const current = eid.new(); 27 | assert.ok(current > previous, 'Failed on iteration: ' + i); 28 | previous = current; 29 | } 30 | }); 31 | 32 | // Ensure our writes have the correct endianness 33 | it('should preserve monotonicity across byte boundary', () => { 34 | const eid = new EventId(); 35 | // Want to cross byte boundary at 255. 36 | eid.b[7] = 250; 37 | let previous = eid.new(); 38 | for (let i = 0; i < 20; i++) { 39 | const current = eid.new(); 40 | assert.ok(current > previous, 'Failed on iteration: ' + i); 41 | previous = current; 42 | } 43 | }); 44 | 45 | // Ensure we transition between low and high bits of counter correctly 46 | it('should preserve monotonicity across word boundary', () => { 47 | const eid = new EventId(); 48 | // Want to cross word boundary at 0xFFFFFFFF. 49 | eid.b[4] = 0xff; 50 | eid.b[5] = 0xff; 51 | eid.b[6] = 0xff; 52 | eid.b[7] = 0xf8; 53 | let previous = eid.new(); 54 | for (let i = 0; i < 20; i++) { 55 | const current = eid.new(); 56 | assert.ok(current > previous, 'Failed on iteration: ' + i); 57 | previous = current; 58 | } 59 | }); 60 | 61 | it('should allow non destructed import', () => { 62 | // eslint-disable-next-line @typescript-eslint/no-var-requires 63 | const EventId = require('../src'); 64 | const eid = new EventId(); 65 | eid.new(); 66 | }); 67 | }); 68 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./node_modules/gts/tsconfig-google.json", 3 | "compilerOptions": { 4 | "rootDir": ".", 5 | "outDir": "build" 6 | }, 7 | "include": [ 8 | "src/*.ts", 9 | "test/*.ts" 10 | ] 11 | } 12 | --------------------------------------------------------------------------------