Comparing version 1.2.0 to 2.0.0
@@ -7,3 +7,3 @@ 'use strict' | ||
function encode (buffer, fixedLength) { | ||
return baseConvertIntArray(buffer, {from: 256, to: 62, fixedLength}) | ||
return baseConvertIntArray(buffer, { from: 256, to: 62, fixedLength }) | ||
.map(value => CHARS[value]) | ||
@@ -22,4 +22,4 @@ .join('') | ||
}) | ||
return Buffer.from(baseConvertIntArray(input, {from: 62, to: 256, fixedLength})) | ||
return Buffer.from(baseConvertIntArray(input, { from: 62, to: 256, fixedLength })) | ||
} | ||
exports.decode = decode |
@@ -13,3 +13,7 @@ /// <reference types="node" /> | ||
static random(): Promise<KSUID>; | ||
static random(timeInMs: number): Promise<KSUID>; | ||
static random(date: Date): Promise<KSUID>; | ||
static randomSync(): KSUID; | ||
static randomSync(timeInMs: number): KSUID; | ||
static randomSync(date: Date): KSUID; | ||
static fromParts(timeInMs: number, payload: Buffer): KSUID; | ||
@@ -16,0 +20,0 @@ static isValid(buffer: Buffer): boolean; |
38
index.js
'use strict' | ||
const {randomBytes} = require('crypto') | ||
const {inspect: {custom: customInspectSymbol}} = require('util') | ||
const padStart = require('string.prototype.padstart') | ||
const { randomBytes } = require('crypto') | ||
const { inspect: { custom: customInspectSymbol }, promisify } = require('util') | ||
const base62 = require('./base62') | ||
function asyncRandomBytes (size) { | ||
return new Promise((resolve, reject) => { | ||
randomBytes(size, (err, bytes) => { | ||
/* istanbul ignore if */ | ||
if (err) { | ||
reject(err) | ||
} else { | ||
resolve(bytes) | ||
} | ||
}) | ||
}) | ||
} | ||
const asyncRandomBytes = promisify(randomBytes) | ||
@@ -26,3 +13,3 @@ // KSUID's epoch starts more recently so that the 32-bit number space gives a | ||
const MAX_TIME_IN_MS = 1e3 * (Math.pow(2, 32) - 1) + EPOCH_IN_MS | ||
const MAX_TIME_IN_MS = 1e3 * (2 ** 32 - 1) + EPOCH_IN_MS | ||
@@ -93,3 +80,3 @@ // Timestamp is a uint32 | ||
const encoded = base62.encode(bufferLookup.get(this), STRING_ENCODED_LENGTH) | ||
return padStart(encoded, STRING_ENCODED_LENGTH, '0') | ||
return encoded.padStart(STRING_ENCODED_LENGTH, '0') | ||
} | ||
@@ -117,9 +104,10 @@ | ||
static random () { | ||
return asyncRandomBytes(PAYLOAD_BYTE_LENGTH).then(payload => new KSUID(fromParts(Date.now(), payload))) | ||
static async random (time = Date.now()) { | ||
const payload = await asyncRandomBytes(PAYLOAD_BYTE_LENGTH) | ||
return new KSUID(fromParts(Number(time), payload)) | ||
} | ||
static randomSync () { | ||
static randomSync (time = Date.now()) { | ||
const payload = randomBytes(PAYLOAD_BYTE_LENGTH) | ||
return new KSUID(fromParts(Date.now(), payload)) | ||
return new KSUID(fromParts(Number(time), payload)) | ||
} | ||
@@ -159,8 +147,8 @@ | ||
} | ||
Object.defineProperty(KSUID.prototype, Symbol.toStringTag, {value: 'KSUID'}) | ||
Object.defineProperty(KSUID.prototype, Symbol.toStringTag, { value: 'KSUID' }) | ||
// A string-encoded maximum value for a KSUID | ||
Object.defineProperty(KSUID, 'MAX_STRING_ENCODED', {value: 'aWgEPTl1tmebfsQzFP4bxwgy80V'}) | ||
Object.defineProperty(KSUID, 'MAX_STRING_ENCODED', { value: 'aWgEPTl1tmebfsQzFP4bxwgy80V' }) | ||
// A string-encoded minimum value for a KSUID | ||
Object.defineProperty(KSUID, 'MIN_STRING_ENCODED', {value: '000000000000000000000000000'}) | ||
Object.defineProperty(KSUID, 'MIN_STRING_ENCODED', { value: '000000000000000000000000000' }) | ||
module.exports = KSUID |
{ | ||
"name": "ksuid", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "Node.js implementation of K-Sortable Globally Unique IDs", | ||
"engine": { | ||
"node": ">=6 <7 || >=8 <9 || >=10" | ||
"node": ">=10 <11 || >=12 <13 || >=14" | ||
}, | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
@@ -16,4 +15,3 @@ "index.js", | ||
"scripts": { | ||
"lint": "as-i-preach", | ||
"test": "npm run -s lint && tsc --noEmit test/typings.ts && nyc ava" | ||
"test": "standard && tsd && c8 ava" | ||
}, | ||
@@ -35,22 +33,12 @@ "repository": { | ||
"dependencies": { | ||
"base-convert-int-array": "^1.0.1", | ||
"string.prototype.padstart": "^3.0.0" | ||
"base-convert-int-array": "^1.0.1" | ||
}, | ||
"devDependencies": { | ||
"@novemberborn/as-i-preach": "^10.1.0", | ||
"@types/node": "^10.12.18", | ||
"ava": "^1.1.0", | ||
"codecov": "^3.1.0", | ||
"lolex": "^3.0.0", | ||
"nyc": "^13.1.0", | ||
"typescript": "^3.2.4" | ||
}, | ||
"nyc": { | ||
"reporter": [ | ||
"html", | ||
"lcov", | ||
"text" | ||
] | ||
}, | ||
"standard-engine": "@novemberborn/as-i-preach" | ||
"@sinonjs/fake-timers": "^6.0.1", | ||
"@types/node": "^10.17.28", | ||
"ava": "^3.11.1", | ||
"c8": "^7.3.0", | ||
"standard": "^14.3.4", | ||
"tsd": "^0.13.1" | ||
} | ||
} |
# ksuid | ||
A Node.js implementation of [Segment's KSUID | ||
library](https://github.com/segmentio/ksuid). Supports Node.js 6, 8 and 10. | ||
A Node.js implementation of [Segment's KSUID library](https://github.com/segmentio/ksuid). Supports Node.js 10, 12, 14 and newer. | ||
You may also be interested in | ||
[`ksuid-cli`](https://www.npmjs.com/package/ksuid-cli). | ||
You may also be interested in [`ksuid-cli`](https://www.npmjs.com/package/ksuid-cli). | ||
@@ -23,8 +21,2 @@ ## Installation | ||
Or, using [TypeScript](https://www.typescriptlang.org/): | ||
```typescript | ||
import KSUID = require('ksuid') | ||
``` | ||
### Creation | ||
@@ -44,2 +36,9 @@ | ||
You can also specify a specific time, either in milliseconds or as a `Date` object: | ||
```js | ||
const ksuidFromDate = KSUID.randomSync(new Date("2014-05-25T16:53:20Z")) | ||
const ksuidFromMillisecondsAsync = await KSUID.random(1401036800000) | ||
``` | ||
Or you can compose it using a timestamp and a 16-byte payload: | ||
@@ -46,0 +45,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1
6
11452
157
99
- Removedstring.prototype.padstart@^3.0.0
- Removedarray-buffer-byte-length@1.0.1(transitive)
- Removedarraybuffer.prototype.slice@1.0.3(transitive)
- Removedavailable-typed-arrays@1.0.7(transitive)
- Removedcall-bind@1.0.7(transitive)
- Removeddata-view-buffer@1.0.1(transitive)
- Removeddata-view-byte-length@1.0.1(transitive)
- Removeddata-view-byte-offset@1.0.0(transitive)
- Removeddefine-data-property@1.1.4(transitive)
- Removeddefine-properties@1.2.1(transitive)
- Removedes-abstract@1.23.5(transitive)
- Removedes-define-property@1.0.0(transitive)
- Removedes-errors@1.3.0(transitive)
- Removedes-object-atoms@1.0.0(transitive)
- Removedes-set-tostringtag@2.0.3(transitive)
- Removedes-to-primitive@1.2.1(transitive)
- Removedfor-each@0.3.3(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedfunction.prototype.name@1.1.6(transitive)
- Removedfunctions-have-names@1.2.3(transitive)
- Removedget-intrinsic@1.2.4(transitive)
- Removedget-symbol-description@1.0.2(transitive)
- Removedglobalthis@1.0.4(transitive)
- Removedgopd@1.0.1(transitive)
- Removedhas-bigints@1.0.2(transitive)
- Removedhas-property-descriptors@1.0.2(transitive)
- Removedhas-proto@1.0.3(transitive)
- Removedhas-symbols@1.0.3(transitive)
- Removedhas-tostringtag@1.0.2(transitive)
- Removedhasown@2.0.2(transitive)
- Removedinternal-slot@1.0.7(transitive)
- Removedis-array-buffer@3.0.4(transitive)
- Removedis-bigint@1.0.4(transitive)
- Removedis-boolean-object@1.1.2(transitive)
- Removedis-callable@1.2.7(transitive)
- Removedis-data-view@1.0.1(transitive)
- Removedis-date-object@1.0.5(transitive)
- Removedis-negative-zero@2.0.3(transitive)
- Removedis-number-object@1.0.7(transitive)
- Removedis-regex@1.1.4(transitive)
- Removedis-shared-array-buffer@1.0.3(transitive)
- Removedis-string@1.0.7(transitive)
- Removedis-symbol@1.0.4(transitive)
- Removedis-typed-array@1.1.13(transitive)
- Removedis-weakref@1.0.2(transitive)
- Removedisarray@2.0.5(transitive)
- Removedobject-inspect@1.13.3(transitive)
- Removedobject-keys@1.1.1(transitive)
- Removedobject.assign@4.1.5(transitive)
- Removedpossible-typed-array-names@1.0.0(transitive)
- Removedregexp.prototype.flags@1.5.3(transitive)
- Removedsafe-array-concat@1.1.2(transitive)
- Removedsafe-regex-test@1.0.3(transitive)
- Removedset-function-length@1.2.2(transitive)
- Removedset-function-name@2.0.2(transitive)
- Removedside-channel@1.0.6(transitive)
- Removedstring.prototype.padstart@3.1.6(transitive)
- Removedstring.prototype.trim@1.2.9(transitive)
- Removedstring.prototype.trimend@1.0.8(transitive)
- Removedstring.prototype.trimstart@1.0.8(transitive)
- Removedtyped-array-buffer@1.0.2(transitive)
- Removedtyped-array-byte-length@1.0.1(transitive)
- Removedtyped-array-byte-offset@1.0.2(transitive)
- Removedtyped-array-length@1.0.6(transitive)
- Removedunbox-primitive@1.0.2(transitive)
- Removedwhich-boxed-primitive@1.0.2(transitive)
- Removedwhich-typed-array@1.1.15(transitive)