@bifravst/aws-cdk-lambda-helpers 1.2.1 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/dist/cdk/TestApp.d.ts +5 -0
  2. package/dist/cdk/TestApp.js +12 -0
  3. package/dist/cdk/TestStack.d.ts +12 -0
  4. package/dist/cdk/TestStack.js +39 -0
  5. package/dist/cdk/baseLayer.d.ts +2 -0
  6. package/dist/cdk/baseLayer.js +5 -0
  7. package/dist/cdk/e2e.js +11 -0
  8. package/dist/cdk/lambda.d.ts +2 -0
  9. package/dist/cdk/lambda.js +5 -0
  10. package/dist/cdk/packTestLambdas.d.ts +5 -0
  11. package/dist/cdk/packTestLambdas.js +4 -0
  12. package/dist/e2e.spec.js +17 -0
  13. package/dist/lambdas/test.zip +0 -0
  14. package/dist/layers/baseLayer/nodejs/node_modules/.package-lock.json +16 -0
  15. package/dist/layers/baseLayer/nodejs/node_modules/id128/CHANGES +20 -0
  16. package/dist/layers/baseLayer/nodejs/node_modules/id128/LICENSE +18 -0
  17. package/dist/layers/baseLayer/nodejs/node_modules/id128/README.md +853 -0
  18. package/dist/layers/baseLayer/nodejs/node_modules/id128/index.d.ts +167 -0
  19. package/dist/layers/baseLayer/nodejs/node_modules/id128/index.js +49 -0
  20. package/dist/layers/baseLayer/nodejs/node_modules/id128/package.json +68 -0
  21. package/dist/layers/baseLayer/nodejs/node_modules/id128/src/coder/base.js +53 -0
  22. package/dist/layers/baseLayer/nodejs/node_modules/id128/src/coder/crockford32.js +114 -0
  23. package/dist/layers/baseLayer/nodejs/node_modules/id128/src/coder/hex.js +63 -0
  24. package/dist/layers/baseLayer/nodejs/node_modules/id128/src/coder/uuid.js +77 -0
  25. package/dist/layers/baseLayer/nodejs/node_modules/id128/src/common/byte-array.js +26 -0
  26. package/dist/layers/baseLayer/nodejs/node_modules/id128/src/common/epoch-converter.js +32 -0
  27. package/dist/layers/baseLayer/nodejs/node_modules/id128/src/common/exception.js +18 -0
  28. package/dist/layers/baseLayer/nodejs/node_modules/id128/src/common/fake-machine.js +26 -0
  29. package/dist/layers/baseLayer/nodejs/node_modules/id128/src/common/machine.js +37 -0
  30. package/dist/layers/baseLayer/nodejs/node_modules/id128/src/common/random-bytes-browser.js +9 -0
  31. package/dist/layers/baseLayer/nodejs/node_modules/id128/src/common/random-bytes.js +17 -0
  32. package/dist/layers/baseLayer/nodejs/node_modules/id128/src/factory/id.js +96 -0
  33. package/dist/layers/baseLayer/nodejs/node_modules/id128/src/factory/versioned-id.js +72 -0
  34. package/dist/layers/baseLayer/nodejs/node_modules/id128/src/id/base.js +39 -0
  35. package/dist/layers/baseLayer/nodejs/node_modules/id128/src/id/ulid-monotonic.js +71 -0
  36. package/dist/layers/baseLayer/nodejs/node_modules/id128/src/id/ulid.js +64 -0
  37. package/dist/layers/baseLayer/nodejs/node_modules/id128/src/id/uuid-1.js +149 -0
  38. package/dist/layers/baseLayer/nodejs/node_modules/id128/src/id/uuid-4.js +24 -0
  39. package/dist/layers/baseLayer/nodejs/node_modules/id128/src/id/uuid-6.js +146 -0
  40. package/dist/layers/baseLayer/nodejs/node_modules/id128/src/id/uuid-nil.js +33 -0
  41. package/dist/layers/baseLayer/nodejs/node_modules/id128/src/id/uuid.js +58 -0
  42. package/dist/layers/baseLayer/nodejs/node_modules/id128/utils.js +35 -0
  43. package/dist/layers/baseLayer/nodejs/package-lock.json +6671 -0
  44. package/dist/layers/baseLayer/nodejs/package.json +1 -0
  45. package/dist/layers/baseLayer.zip +0 -0
  46. package/dist/src/checkSumOfFiles.spec.d.ts +1 -0
  47. package/dist/src/commonParent.spec.d.ts +1 -0
  48. package/dist/{packLambda.d.ts → src/packLambda.d.ts} +2 -2
  49. package/package.json +20 -14
  50. /package/dist/{checkSumOfFiles.spec.d.ts → cdk/e2e.d.ts} +0 -0
  51. /package/dist/{commonParent.spec.d.ts → e2e.spec.d.ts} +0 -0
  52. /package/dist/{LambdaLogGroup.d.ts → src/LambdaLogGroup.d.ts} +0 -0
  53. /package/dist/{LambdaLogGroup.js → src/LambdaLogGroup.js} +0 -0
  54. /package/dist/{LambdaSource.d.ts → src/LambdaSource.d.ts} +0 -0
  55. /package/dist/{LambdaSource.js → src/LambdaSource.js} +0 -0
  56. /package/dist/{cdk.d.ts → src/cdk.d.ts} +0 -0
  57. /package/dist/{cdk.js → src/cdk.js} +0 -0
  58. /package/dist/{checkSumOfFiles.spec.js → src/checkSumOfFiles.spec.js} +0 -0
  59. /package/dist/{checksumOfFiles.d.ts → src/checksumOfFiles.d.ts} +0 -0
  60. /package/dist/{checksumOfFiles.js → src/checksumOfFiles.js} +0 -0
  61. /package/dist/{commonParent.d.ts → src/commonParent.d.ts} +0 -0
  62. /package/dist/{commonParent.js → src/commonParent.js} +0 -0
  63. /package/dist/{commonParent.spec.js → src/commonParent.spec.js} +0 -0
  64. /package/dist/{findDependencies.d.ts → src/findDependencies.d.ts} +0 -0
  65. /package/dist/{findDependencies.js → src/findDependencies.js} +0 -0
  66. /package/dist/{lambda.d.ts → src/lambda.d.ts} +0 -0
  67. /package/dist/{lambda.js → src/lambda.js} +0 -0
  68. /package/dist/{layer.d.ts → src/layer.d.ts} +0 -0
  69. /package/dist/{layer.js → src/layer.js} +0 -0
  70. /package/dist/{packLambda.js → src/packLambda.js} +0 -0
  71. /package/dist/{packLambdaFromPath.d.ts → src/packLambdaFromPath.d.ts} +0 -0
  72. /package/dist/{packLambdaFromPath.js → src/packLambdaFromPath.js} +0 -0
  73. /package/dist/{packLayer.d.ts → src/packLayer.d.ts} +0 -0
  74. /package/dist/{packLayer.js → src/packLayer.js} +0 -0
  75. /package/dist/{util.d.ts → src/util.d.ts} +0 -0
  76. /package/dist/{util.js → src/util.js} +0 -0
@@ -0,0 +1,167 @@
1
+ /* eslint-disable @typescript-eslint/no-empty-interface */
2
+ export {};
3
+
4
+ // Utility Types
5
+
6
+ interface ConstructorOf<C> {
7
+ new(...args: any[]): C;
8
+ }
9
+
10
+ // Id Types
11
+
12
+ export interface Id {
13
+ readonly bytes: Uint8Array;
14
+ readonly [Symbol.toStringTag]: string;
15
+
16
+ clone(): this;
17
+
18
+ toCanonical(): string;
19
+ toRaw(): string;
20
+
21
+ compare(rhs: Id): number;
22
+ equal(rhs: Id): boolean;
23
+ }
24
+
25
+ export interface Uuid extends Id {
26
+ readonly variant: number;
27
+ readonly version: number;
28
+ }
29
+
30
+ export interface Ulid extends Id {
31
+ readonly time: Date;
32
+ }
33
+
34
+ export interface UlidMonotonic extends Id {
35
+ readonly time: Date;
36
+ }
37
+
38
+ export interface Uuid1 extends Uuid {
39
+ readonly clock_sequence: number;
40
+ readonly hires_time: number;
41
+ readonly node: Uint8Array;
42
+ readonly time: Date;
43
+ }
44
+
45
+ export interface Uuid4 extends Uuid {}
46
+
47
+ export interface Uuid6 extends Uuid {
48
+ readonly clock_sequence: number;
49
+ readonly hires_time: number;
50
+ readonly node: Uint8Array;
51
+ readonly time: Date;
52
+ }
53
+
54
+ export interface UuidNil extends Uuid {}
55
+
56
+ // Id Factories
57
+
58
+ interface IdFactory<T> {
59
+ readonly name: string;
60
+ readonly type: ConstructorOf<T>;
61
+
62
+ construct(bytes: Uint8Array): T;
63
+ generate(options?: {}): T;
64
+ MIN(options?: {}): T;
65
+ MAX(options?: {}): T;
66
+
67
+ fromCanonical(canonical: string): T;
68
+ fromCanonicalTrusted(canonical: string): T;
69
+ fromRaw(raw: string): T;
70
+ fromRawTrusted(raw: string): T;
71
+
72
+ toCanonical(id: T): string;
73
+ toRaw(id: T): string;
74
+
75
+ compare(lhs: Id, rhs: Id): number;
76
+ equal(lhs: Id, rhs: Id): boolean;
77
+
78
+ isCanonical(canonical: string): boolean;
79
+ isRaw(raw: string): boolean;
80
+ }
81
+
82
+ interface VersionedIdFactory<T> extends IdFactory<T> {
83
+ readonly versioned_ids: Array<IdFactory<T>>;
84
+
85
+ MIN(options: VersionOption): T;
86
+ MAX(options: VersionOption): T;
87
+ }
88
+
89
+ interface NodeOption {
90
+ node?: Uint8Array | null;
91
+ }
92
+
93
+ interface TimeOption {
94
+ time?: Date | number | null;
95
+ }
96
+
97
+ interface VersionOption {
98
+ version: number;
99
+ }
100
+
101
+ export type AnyIdFactory = IdFactory<Id>;
102
+
103
+ export interface UlidFactory extends IdFactory<Ulid> {
104
+ generate(options?: TimeOption): Ulid;
105
+ }
106
+
107
+ export interface UlidMonotonicFactory extends IdFactory<UlidMonotonic> {
108
+ generate(options?: TimeOption): UlidMonotonic;
109
+ }
110
+
111
+ export interface UuidFactory extends VersionedIdFactory<Uuid> {
112
+ generate(options: NodeOption & TimeOption & VersionOption): Uuid;
113
+ }
114
+
115
+ export interface Uuid1Factory extends IdFactory<Uuid1> {
116
+ generate(options?: NodeOption & TimeOption): Uuid1;
117
+ }
118
+
119
+ export interface Uuid4Factory extends IdFactory<Uuid4> {
120
+ generate(options?: {}): Uuid4;
121
+ }
122
+
123
+ export interface Uuid6Factory extends IdFactory<Uuid6> {
124
+ generate(options?: NodeOption & TimeOption): Uuid6;
125
+ }
126
+
127
+ export interface UuidNilFactory extends IdFactory<UuidNil> {
128
+ generate(options?: {}): UuidNil;
129
+ }
130
+
131
+ // Errors
132
+
133
+ // NOTE: Actually extends Error, but typescript breaks instanceof support
134
+ export interface Id128Error extends Function {
135
+ readonly name: string;
136
+ message: string;
137
+ stack?: string;
138
+ }
139
+ export interface ClockSequenceOverflow extends Id128Error {}
140
+ export interface InvalidBytes extends Id128Error {}
141
+ export interface InvalidEncoding extends Id128Error {}
142
+ export interface InvalidEpoch extends Id128Error {}
143
+ export interface UnsupportedVersion extends Id128Error {}
144
+
145
+ // Exported Functions
146
+
147
+ export function idCompare(lhs: Id, rhs: Id): number;
148
+ export function idEqual(lhs: Id, rhs: Id): boolean;
149
+
150
+ // Exported Constants
151
+
152
+ export const Ulid: UlidFactory;
153
+ export const UlidMonotonic: UlidMonotonicFactory;
154
+ export const Uuid: UuidFactory;
155
+ export const Uuid1: Uuid1Factory;
156
+ export const Uuid4: Uuid4Factory;
157
+ export const Uuid6: Uuid6Factory;
158
+ export const UuidNil: UuidNilFactory;
159
+
160
+ export namespace Exception {
161
+ const Id128Error: ConstructorOf<Id128Error>;
162
+ const ClockSequenceOverflow: ConstructorOf<ClockSequenceOverflow>;
163
+ const InvalidBytes: ConstructorOf<InvalidBytes>;
164
+ const InvalidEncoding: ConstructorOf<InvalidEncoding>;
165
+ const InvalidEpoch: ConstructorOf<InvalidEpoch>;
166
+ const UnsupportedVersion: ConstructorOf<UnsupportedVersion>;
167
+ }
@@ -0,0 +1,49 @@
1
+ const { Ulid } = require('./src/id/ulid');
2
+ const { UlidMonotonic } = require('./src/id/ulid-monotonic');
3
+ const { Uuid } = require('./src/id/uuid');
4
+ const { Uuid1 } = require('./src/id/uuid-1');
5
+ const { Uuid4 } = require('./src/id/uuid-4');
6
+ const { Uuid6 } = require('./src/id/uuid-6');
7
+ const { UuidNil } = require('./src/id/uuid-nil');
8
+
9
+ const Crockford32Coder = require('./src/coder/crockford32');
10
+ const HexCoder = require('./src/coder/hex');
11
+ const UuidCoder = require('./src/coder/uuid');
12
+
13
+ const { IdFactory } = require('./src/factory/id');
14
+ const { VersionedIdFactory } = require('./src/factory/versioned-id');
15
+ const Exception = require('./src/common/exception');
16
+
17
+ const namespace = {
18
+ idCompare: function(lhs, rhs) { return lhs.compare(rhs); },
19
+ idEqual: function(lhs, rhs) { return lhs.equal(rhs); },
20
+ Exception,
21
+ Ulid: new IdFactory({
22
+ id: Ulid,
23
+ canonical_coder: Crockford32Coder,
24
+ raw_coder: HexCoder,
25
+ }),
26
+ UlidMonotonic: new IdFactory({
27
+ id: UlidMonotonic,
28
+ canonical_coder: Crockford32Coder,
29
+ raw_coder: HexCoder,
30
+ }),
31
+ Uuid: new VersionedIdFactory({
32
+ abstract_id: Uuid,
33
+ versioned_ids: [
34
+ Uuid1,
35
+ Uuid4,
36
+ Uuid6,
37
+ UuidNil,
38
+ ],
39
+ canonical_coder: UuidCoder,
40
+ raw_coder: HexCoder,
41
+ }),
42
+ };
43
+
44
+ namespace.Uuid.versioned_ids.reduce(
45
+ (ns, uuid) => Object.assign(ns, {[uuid.name]: uuid}),
46
+ namespace
47
+ );
48
+
49
+ module.exports = namespace;
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "id128",
3
+ "description": "Collection of 128-bit Id generators",
4
+ "version": "1.6.6",
5
+ "author": "Aaron Cohen <aarondcohen@gmail.com>",
6
+ "license": "MIT",
7
+ "main": "index.js",
8
+ "types": "index.d.ts",
9
+ "files": [
10
+ "index.d.ts",
11
+ "index.js",
12
+ "src/",
13
+ "utils.js"
14
+ ],
15
+ "homepage": "https://github.com/aarondcohen/id128#id128",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/aarondcohen/id128.git"
19
+ },
20
+ "bugs": {
21
+ "url": "https://github.com/aarondcohen/id128/issues"
22
+ },
23
+ "browser": {
24
+ "./src/common/machine.js": "./src/common/fake-machine.js",
25
+ "./src/common/random-bytes.js": "./src/common/random-bytes-browser.js"
26
+ },
27
+ "scripts": {
28
+ "benchmark": "NODE_PATH='./src/' node benchmark/stats.js",
29
+ "call": "NODE_PATH='./src/' node",
30
+ "dtslint": "dtslint types",
31
+ "test": "NODE_PATH='./src/' mocha",
32
+ "test-all": "NODE_PATH='./src/' mocha test/*"
33
+ },
34
+ "engines": {
35
+ "node": ">=v6.9.0"
36
+ },
37
+ "devDependencies": {
38
+ "benchmarkify": "github:aarondcohen/benchmarkify",
39
+ "chai": "*",
40
+ "dtslint": "^3.4.1",
41
+ "mocha": "*",
42
+ "sinon": "*"
43
+ },
44
+ "keywords": [
45
+ "128-bit",
46
+ "crockford32",
47
+ "guid",
48
+ "id",
49
+ "monotonic",
50
+ "random",
51
+ "rfc-4122",
52
+ "rfc4122",
53
+ "uid",
54
+ "ulid",
55
+ "unique",
56
+ "universal",
57
+ "uuid",
58
+ "uuid-v1",
59
+ "uuid-v4",
60
+ "uuid-v6",
61
+ "uuid1",
62
+ "uuid4",
63
+ "uuid6",
64
+ "uuidv1",
65
+ "uuidv4",
66
+ "uuidv6"
67
+ ]
68
+ }
@@ -0,0 +1,53 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ InvalidEncoding,
5
+ InvalidBytes,
6
+ } = require('../common/exception');
7
+
8
+ const _valid_encoding_pattern = Symbol('valid_encoding_pattern');
9
+
10
+ class BaseCoder {
11
+ constructor({
12
+ valid_encoding_pattern,
13
+ } = {}) {
14
+ this[_valid_encoding_pattern] = valid_encoding_pattern;
15
+ }
16
+
17
+ decode(encoding) {
18
+ if (this.isValidEncoding(encoding)) {
19
+ return this.decodeTrusted(encoding);
20
+ }
21
+ else {
22
+ throw new InvalidEncoding(`Encoding [${encoding}] does not satisfy ${this[_valid_encoding_pattern]}`);
23
+ }
24
+ }
25
+
26
+ decodeTrusted(encoding) { return ByteArray.generateRandomFilled() }
27
+
28
+ encode(bytes) {
29
+ if (this.isValidBytes(bytes)) {
30
+ return this.encodeTrusted(bytes);
31
+ }
32
+ else {
33
+ throw new InvalidBytes('Requires a 16-byte Uint8Array');
34
+ }
35
+
36
+ }
37
+
38
+ encodeTrusted(bytes) { return '' }
39
+
40
+ isValidBytes(bytes) {
41
+ return true
42
+ && (bytes instanceof Uint8Array)
43
+ && bytes.length === 16;
44
+ }
45
+
46
+ isValidEncoding(encoding) {
47
+ return true
48
+ && (typeof encoding === 'string' || encoding instanceof String)
49
+ && this[_valid_encoding_pattern].test(encoding);
50
+ }
51
+ }
52
+
53
+ module.exports = { BaseCoder };
@@ -0,0 +1,114 @@
1
+ 'use strict';
2
+
3
+ const { BaseCoder } = require('./base');
4
+
5
+ const ALPHABET = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
6
+ const MAX_QUINTET = 0b11111;
7
+
8
+ const CHAR_TO_QUINTET = Array.from(ALPHABET).reduce(
9
+ (acc, chr, idx) => (acc[chr] = acc[chr.toLowerCase()] = idx, acc),
10
+ {
11
+ 'I': ALPHABET.indexOf('1'),
12
+ 'i': ALPHABET.indexOf('1'),
13
+ 'L': ALPHABET.indexOf('1'),
14
+ 'l': ALPHABET.indexOf('1'),
15
+ 'O': ALPHABET.indexOf('0'),
16
+ 'o': ALPHABET.indexOf('0'),
17
+ 'U': ALPHABET.indexOf('V'),
18
+ 'u': ALPHABET.indexOf('V'),
19
+ }
20
+ );
21
+ const QUINTET_TO_CHAR = Array.from(ALPHABET);
22
+
23
+ function _charToQuintet(chr) {
24
+ return CHAR_TO_QUINTET[chr];
25
+ }
26
+
27
+ function _quintetToChar(quintet) {
28
+ return QUINTET_TO_CHAR[quintet & MAX_QUINTET];
29
+ }
30
+
31
+ class Crockford32Coder extends BaseCoder {
32
+ constructor() {
33
+ super({
34
+ valid_encoding_pattern: /^[0-7][^\W_]{25}$/,
35
+ });
36
+ }
37
+
38
+ decodeTrusted(encoding) {
39
+ const bytes = new Uint8Array(16);
40
+ const quintets = [];
41
+
42
+ for (let idx = 0, end = encoding.length; idx < end; ++idx) {
43
+ quintets.push(_charToQuintet(encoding[idx]));
44
+ }
45
+
46
+ //Note: unrolled for performance
47
+ bytes[0] = quintets[0] << 5 | quintets[1];
48
+
49
+ bytes[1] = quintets[2] << 3 | quintets[3] >> 2;
50
+ bytes[2] = quintets[3] << 6 | quintets[4] << 1 | quintets[5] >> 4;
51
+ bytes[3] = quintets[5] << 4 | quintets[6] >> 1;
52
+ bytes[4] = quintets[6] << 7 | quintets[7] << 2 | quintets[8] >> 3;
53
+ bytes[5] = quintets[8] << 5 | quintets[9];
54
+
55
+ bytes[6] = quintets[10] << 3 | quintets[11] >> 2;
56
+ bytes[7] = quintets[11] << 6 | quintets[12] << 1 | quintets[13] >> 4;
57
+ bytes[8] = quintets[13] << 4 | quintets[14] >> 1;
58
+ bytes[9] = quintets[14] << 7 | quintets[15] << 2 | quintets[16] >> 3;
59
+ bytes[10] = quintets[16] << 5 | quintets[17];
60
+
61
+ bytes[11] = quintets[18] << 3 | quintets[19] >> 2;
62
+ bytes[12] = quintets[19] << 6 | quintets[20] << 1 | quintets[21] >> 4;
63
+ bytes[13] = quintets[21] << 4 | quintets[22] >> 1;
64
+ bytes[14] = quintets[22] << 7 | quintets[23] << 2 | quintets[24] >> 3;
65
+ bytes[15] = quintets[24] << 5 | quintets[25];
66
+
67
+ return bytes;
68
+ }
69
+
70
+ encodeTrusted(bytes) {
71
+ //Note: unrolled for performance
72
+ let quintets = [
73
+ (bytes[0] >> 5),
74
+ (bytes[0]),
75
+
76
+ (bytes[1] >> 3),
77
+ (bytes[1] << 2 | bytes[2] >> 6),
78
+ (bytes[2] >> 1),
79
+ (bytes[2] << 4 | bytes[3] >> 4),
80
+ (bytes[3] << 1 | bytes[4] >> 7),
81
+ (bytes[4] >> 2),
82
+ (bytes[4] << 3 | bytes[5] >> 5),
83
+ (bytes[5]),
84
+
85
+ (bytes[6] >> 3),
86
+ (bytes[6] << 2 | bytes[7] >> 6),
87
+ (bytes[7] >> 1),
88
+ (bytes[7] << 4 | bytes[8] >> 4),
89
+ (bytes[8] << 1 | bytes[9] >> 7),
90
+ (bytes[9] >> 2),
91
+ (bytes[9] << 3 | bytes[10] >> 5),
92
+ (bytes[10]),
93
+
94
+ (bytes[11] >> 3),
95
+ (bytes[11] << 2 | bytes[12] >> 6),
96
+ (bytes[12] >> 1),
97
+ (bytes[12] << 4 | bytes[13] >> 4),
98
+ (bytes[13] << 1 | bytes[14] >> 7),
99
+ (bytes[14] >> 2),
100
+ (bytes[14] << 3 | bytes[15] >> 5),
101
+ (bytes[15]),
102
+ ];
103
+
104
+ //Note: Massive performance losses occured when
105
+ // using the more legible Array.map and Array.join
106
+ let encoding = '';
107
+ for (let idx = 0, end = quintets.length; idx < end; ++idx) {
108
+ encoding += _quintetToChar(quintets[idx]);
109
+ }
110
+ return encoding;
111
+ }
112
+ }
113
+
114
+ module.exports = new Crockford32Coder;
@@ -0,0 +1,63 @@
1
+ 'use strict';
2
+
3
+ const { BaseCoder } = require('./base');
4
+
5
+ const ALPHABET = '0123456789ABCDEF';
6
+
7
+ const BYTE_TO_HEX = Array
8
+ .from({ length: ALPHABET.length * ALPHABET.length })
9
+ .map((_, key) => (
10
+ ''
11
+ + ALPHABET.charAt(key / ALPHABET.length)
12
+ + ALPHABET.charAt(key % ALPHABET.length)
13
+ ));
14
+
15
+ const HEX_TO_BYTE = Array.from(ALPHABET).reduce(
16
+ (mapping, hex, idx) => Object.assign(mapping, {
17
+ [hex.toUpperCase()]: idx,
18
+ [hex.toLowerCase()]: idx,
19
+ }),
20
+ Object.create(null)
21
+ );
22
+
23
+ class HexCoder extends BaseCoder {
24
+ constructor() {
25
+ super({
26
+ valid_encoding_pattern: /^[0-9A-Fa-f]{32}$/,
27
+ });
28
+ }
29
+
30
+ decodeTrusted(encoding) {
31
+ let bytes = new Uint8Array(16);
32
+
33
+ for (
34
+ let
35
+ dst = 0,
36
+ hi_hex = true,
37
+ src = 0,
38
+ end = encoding.length;
39
+ src < end;
40
+ ++src
41
+ ) {
42
+ const hex = encoding[src];
43
+ if (hi_hex) {
44
+ bytes[dst] = HEX_TO_BYTE[hex] << 4;
45
+ } else {
46
+ bytes[dst++] |= HEX_TO_BYTE[hex];
47
+ }
48
+ hi_hex = !hi_hex;
49
+ }
50
+
51
+ return bytes;
52
+ }
53
+
54
+ encodeTrusted(bytes) {
55
+ let encoding = '';
56
+ for (let idx = 0, end = bytes.length; idx < end; ++idx) {
57
+ encoding += BYTE_TO_HEX[bytes[idx]];
58
+ }
59
+ return encoding;
60
+ }
61
+ }
62
+
63
+ module.exports = new HexCoder;
@@ -0,0 +1,77 @@
1
+ 'use strict';
2
+
3
+ const { BaseCoder } = require('./base');
4
+
5
+ const ALPHABET = '0123456789ABCDEF';
6
+
7
+ const BYTE_TO_HEX = Array
8
+ .from({ length: ALPHABET.length * ALPHABET.length })
9
+ .map((_, key) => (
10
+ ''
11
+ + ALPHABET.charAt(key / ALPHABET.length)
12
+ + ALPHABET.charAt(key % ALPHABET.length)
13
+ ));
14
+
15
+ const HEX_TO_BYTE = Array.from(ALPHABET).reduce(
16
+ (mapping, hex, idx) => Object.assign(mapping, {
17
+ [hex.toUpperCase()]: idx,
18
+ [hex.toLowerCase()]: idx,
19
+ }),
20
+ Object.create(null)
21
+ );
22
+
23
+ class UuidCoder extends BaseCoder {
24
+ constructor() {
25
+ super({
26
+ valid_encoding_pattern: /^[0-9A-Fa-f]{4}(?:-?[0-9A-Fa-f]{4}){7}$/,
27
+ });
28
+ }
29
+
30
+ decodeTrusted(encoding) {
31
+ let bytes = new Uint8Array(16);
32
+
33
+ for (
34
+ let
35
+ dst = 0,
36
+ hi_hex = true,
37
+ src = 0,
38
+ end = encoding.length;
39
+ src < end;
40
+ ++src
41
+ ) {
42
+ const hex = encoding[src];
43
+ if(hex !== '-') {
44
+ if (hi_hex) {
45
+ bytes[dst] = HEX_TO_BYTE[hex] << 4;
46
+ } else {
47
+ bytes[dst++] |= HEX_TO_BYTE[hex];
48
+ }
49
+ hi_hex = ! hi_hex;
50
+ }
51
+ }
52
+
53
+ return bytes;
54
+ }
55
+
56
+ encodeTrusted(bytes) {
57
+ let idx = -1;
58
+ const encoding = (''
59
+ + BYTE_TO_HEX[bytes[++idx]] + BYTE_TO_HEX[bytes[++idx]]
60
+ + BYTE_TO_HEX[bytes[++idx]] + BYTE_TO_HEX[bytes[++idx]]
61
+ + '-'
62
+ + BYTE_TO_HEX[bytes[++idx]] + BYTE_TO_HEX[bytes[++idx]]
63
+ + '-'
64
+ + BYTE_TO_HEX[bytes[++idx]] + BYTE_TO_HEX[bytes[++idx]]
65
+ + '-'
66
+ + BYTE_TO_HEX[bytes[++idx]] + BYTE_TO_HEX[bytes[++idx]]
67
+ + '-'
68
+ + BYTE_TO_HEX[bytes[++idx]] + BYTE_TO_HEX[bytes[++idx]]
69
+ + BYTE_TO_HEX[bytes[++idx]] + BYTE_TO_HEX[bytes[++idx]]
70
+ + BYTE_TO_HEX[bytes[++idx]] + BYTE_TO_HEX[bytes[++idx]]
71
+ );
72
+ return encoding;
73
+ }
74
+ }
75
+
76
+ module.exports = new UuidCoder;
77
+
@@ -0,0 +1,26 @@
1
+ const { randomBytes } = require('./random-bytes');
2
+
3
+ const MAX_BYTES = 16;
4
+
5
+ class ByteArray {
6
+ compare(lhs, rhs) {
7
+ const mismatch_idx =
8
+ lhs.findIndex((byt, idx) => (byt !== rhs[idx]));
9
+ return ~mismatch_idx
10
+ && Math.sign(lhs[mismatch_idx] - rhs[mismatch_idx]);
11
+ }
12
+
13
+ generateOneFilled() {
14
+ return new Uint8Array(MAX_BYTES).fill(0xFF);
15
+ }
16
+
17
+ generateRandomFilled() {
18
+ return randomBytes(MAX_BYTES);
19
+ }
20
+
21
+ generateZeroFilled() {
22
+ return new Uint8Array(MAX_BYTES).fill(0);
23
+ }
24
+ }
25
+
26
+ module.exports = new ByteArray;
@@ -0,0 +1,32 @@
1
+ const { InvalidEpoch } = require('./exception');
2
+
3
+ const MIN_MS = 0;
4
+ const MAX_MS = Math.pow(2, 48);
5
+
6
+ class EpochConverter {
7
+ fromEpoch(origin_ms, epoch_ms) {
8
+ return new Date(epoch_ms + origin_ms);
9
+ }
10
+
11
+ toEpoch(origin_ms, time = null) {
12
+ const coerced_ms =
13
+ time === null ? Date.now() :
14
+ Number.isInteger(time) ? time :
15
+ time instanceof Date ? time.getTime() :
16
+ (() => {
17
+ throw new InvalidEpoch(`Failed to coerce time [${time}] to epoch`);
18
+ })();
19
+
20
+ const epoch_ms = coerced_ms - origin_ms;
21
+
22
+ if (epoch_ms < MIN_MS || epoch_ms >= MAX_MS) {
23
+ const min_iso = new Date(MIN_MS + origin_ms).toISOString();
24
+ const max_iso = new Date(MAX_MS - 1 + origin_ms).toISOString();
25
+ throw new InvalidEpoch(`Epoch must be between ${min_iso} and ${max_iso}`);
26
+ }
27
+
28
+ return epoch_ms;
29
+ }
30
+ }
31
+
32
+ module.exports = new EpochConverter;
@@ -0,0 +1,18 @@
1
+ class Id128Error extends Error {
2
+ get name() { return this.constructor.name }
3
+ }
4
+
5
+ class ClockSequenceOverflow extends Id128Error {}
6
+ class InvalidBytes extends Id128Error {}
7
+ class InvalidEncoding extends Id128Error {}
8
+ class InvalidEpoch extends Id128Error {}
9
+ class UnsupportedVersion extends Id128Error {}
10
+
11
+ module.exports = {
12
+ Id128Error,
13
+ ClockSequenceOverflow,
14
+ InvalidBytes,
15
+ InvalidEncoding,
16
+ InvalidEpoch,
17
+ UnsupportedVersion,
18
+ };
@@ -0,0 +1,26 @@
1
+ const { randomBytes } = require('./random-bytes');
2
+
3
+ const _mac_address = Symbol('mac-address');
4
+
5
+ class FakeMachine {
6
+ constructor() {
7
+ this.reset();
8
+ }
9
+
10
+ get mac_address() {
11
+ let mac_address = this[_mac_address];
12
+
13
+ if (! mac_address) {
14
+ mac_address = this[_mac_address] = randomBytes(6);
15
+ mac_address[0] |= 0b00000001;
16
+ }
17
+
18
+ return mac_address;
19
+ }
20
+
21
+ reset() {
22
+ this[_mac_address] = null;
23
+ }
24
+ }
25
+
26
+ module.exports = new FakeMachine;