@saidsef/aws-kinesis-local 0.0.0-stage → 1.0.0

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 (69) hide show
  1. package/LICENSE.md +9 -0
  2. package/README.md +68 -2
  3. package/dist/arguments.d.ts +3 -0
  4. package/dist/arguments.js +35 -0
  5. package/dist/certificate.d.ts +4 -0
  6. package/dist/certificate.js +63 -0
  7. package/dist/cli.d.ts +2 -0
  8. package/dist/cli.js +48 -0
  9. package/dist/errors.d.ts +16 -0
  10. package/dist/errors.js +40 -0
  11. package/dist/generated/model.d.ts +4 -0
  12. package/dist/generated/model.js +3 -0
  13. package/dist/handler.d.ts +8 -0
  14. package/dist/handler.js +225 -0
  15. package/dist/index.d.ts +1 -0
  16. package/dist/index.js +1 -0
  17. package/dist/kinesis/iterator.d.ts +9 -0
  18. package/dist/kinesis/iterator.js +29 -0
  19. package/dist/kinesis/sequence.d.ts +25 -0
  20. package/dist/kinesis/sequence.js +126 -0
  21. package/dist/model.d.ts +45 -0
  22. package/dist/model.js +1 -0
  23. package/dist/operations/account.d.ts +25 -0
  24. package/dist/operations/account.js +30 -0
  25. package/dist/operations/capacity.d.ts +14 -0
  26. package/dist/operations/capacity.js +89 -0
  27. package/dist/operations/channels.d.ts +14 -0
  28. package/dist/operations/channels.js +173 -0
  29. package/dist/operations/common.d.ts +45 -0
  30. package/dist/operations/common.js +123 -0
  31. package/dist/operations/consumers.d.ts +25 -0
  32. package/dist/operations/consumers.js +108 -0
  33. package/dist/operations/encryption.d.ts +5 -0
  34. package/dist/operations/encryption.js +49 -0
  35. package/dist/operations/index.d.ts +4 -0
  36. package/dist/operations/index.js +68 -0
  37. package/dist/operations/monitoring.d.ts +13 -0
  38. package/dist/operations/monitoring.js +24 -0
  39. package/dist/operations/policies.d.ts +6 -0
  40. package/dist/operations/policies.js +38 -0
  41. package/dist/operations/records.d.ts +29 -0
  42. package/dist/operations/records.js +313 -0
  43. package/dist/operations/retention.d.ts +3 -0
  44. package/dist/operations/retention.js +34 -0
  45. package/dist/operations/shards.d.ts +16 -0
  46. package/dist/operations/shards.js +261 -0
  47. package/dist/operations/streams.d.ts +16 -0
  48. package/dist/operations/streams.js +185 -0
  49. package/dist/operations/subscribe.d.ts +38 -0
  50. package/dist/operations/subscribe.js +195 -0
  51. package/dist/operations/tags.d.ts +18 -0
  52. package/dist/operations/tags.js +59 -0
  53. package/dist/protocol/cbor.d.ts +4 -0
  54. package/dist/protocol/cbor.js +263 -0
  55. package/dist/protocol/eventstream.d.ts +2 -0
  56. package/dist/protocol/eventstream.js +26 -0
  57. package/dist/protocol/serialize.d.ts +5 -0
  58. package/dist/protocol/serialize.js +49 -0
  59. package/dist/server.d.ts +23 -0
  60. package/dist/server.js +100 -0
  61. package/dist/store/store.d.ts +61 -0
  62. package/dist/store/store.js +246 -0
  63. package/dist/store/types.d.ts +96 -0
  64. package/dist/store/types.js +1 -0
  65. package/dist/validation/member-order.d.ts +6 -0
  66. package/dist/validation/member-order.js +29 -0
  67. package/dist/validation/validate.d.ts +9 -0
  68. package/dist/validation/validate.js +370 -0
  69. package/package.json +69 -4
@@ -0,0 +1,225 @@
1
+ import { randomBytes, randomUUID } from 'node:crypto';
2
+ import { KinesisError } from './errors.js';
3
+ import { operations, serviceTarget } from './generated/model.js';
4
+ import { handlers } from './operations/index.js';
5
+ import { DEFAULT_MAX_RECORD_SIZE_KIB, streamNameFromArn } from './operations/common.js';
6
+ import { Subscription } from './operations/subscribe.js';
7
+ import { decodeCbor } from './protocol/cbor.js';
8
+ import { AMZ_CBOR, AMZ_JSON, encodeBody, serializeShape } from './protocol/serialize.js';
9
+ import { coerceStructure, decodeBlobs, validateStructure } from './validation/validate.js';
10
+ export const MAX_REQUEST_BYTES = 16 * 1024 * 1024;
11
+ const EVENT_STREAM = 'application/vnd.amazon.eventstream';
12
+ const EXPOSED_HEADERS = 'x-amzn-RequestId,x-amzn-ErrorType,x-amz-request-id,x-amz-id-2,x-amzn-ErrorMessage,Date';
13
+ function header(req, name) {
14
+ const value = req.headers[name];
15
+ return Array.isArray(value) ? value.join(',') : value ?? '';
16
+ }
17
+ function sendRaw(exchange, body, statusCode = 200) {
18
+ const { req, res } = exchange;
19
+ req.removeAllListeners('data');
20
+ res.statusCode = statusCode;
21
+ if (body === null)
22
+ return res.end();
23
+ res.setHeader('Content-Length', Buffer.byteLength(body));
24
+ res.end(body);
25
+ }
26
+ function protocolOf(exchange) {
27
+ return exchange.contentType === AMZ_CBOR ? 'cbor' : 'json';
28
+ }
29
+ function sendResponse(exchange, data, statusCode = 200) {
30
+ exchange.res.setHeader('Content-Type', exchange.contentType);
31
+ sendRaw(exchange, encodeBody(data, protocolOf(exchange)), statusCode);
32
+ }
33
+ function sendError(exchange, contentValid, type, message) {
34
+ if (contentValid)
35
+ return sendResponse(exchange, { __type: type, message }, 400);
36
+ if (typeof message === 'number')
37
+ return sendRaw(exchange, `<${type}/>\n`, message);
38
+ return sendRaw(exchange, `<${type}>\n <Message>${message}</Message>\n</${type}>\n`, 403);
39
+ }
40
+ function validationContext(store, data) {
41
+ let name = typeof data.StreamName === 'string' ? data.StreamName : undefined;
42
+ if (name === undefined && typeof data.StreamARN === 'string')
43
+ name = streamNameFromArn(data.StreamARN);
44
+ const stream = name === undefined ? undefined : store.streams.get(name);
45
+ return {
46
+ partitionKeyRequired: stream?.RecordDistributionStrategy !== 'AUTO',
47
+ maxRecordBytes: (stream?.MaxRecordSizeInKiB ?? DEFAULT_MAX_RECORD_SIZE_KIB) * 1024,
48
+ };
49
+ }
50
+ function checkAuthentication(exchange) {
51
+ const { req } = exchange;
52
+ const authHeader = header(req, 'authorization');
53
+ const url = new URL(req.url ?? '/', 'http://localhost');
54
+ const query = url.searchParams;
55
+ const authQuery = query.has('X-Amz-Algorithm');
56
+ if (authHeader && authQuery) {
57
+ return new KinesisError('InvalidSignatureException', 'Found both \'X-Amz-Algorithm\' as a query-string param and \'Authorization\' as HTTP header.');
58
+ }
59
+ if (!authHeader && !authQuery)
60
+ return new KinesisError('MissingAuthenticationTokenException', 'Missing Authentication Token');
61
+ let message = '';
62
+ if (authHeader) {
63
+ const params = {};
64
+ for (const part of authHeader.split(/,| /).slice(1).filter(Boolean)) {
65
+ const [key, value] = part.trim().split('=');
66
+ params[key] = value;
67
+ }
68
+ for (const param of ['Credential', 'Signature', 'SignedHeaders']) {
69
+ if (!params[param])
70
+ message += `Authorization header requires '${param}' parameter. `;
71
+ }
72
+ if (!header(req, 'x-amz-date') && !header(req, 'date')) {
73
+ message += 'Authorization header requires existence of either a \'X-Amz-Date\' or a \'Date\' header. ';
74
+ }
75
+ if (message)
76
+ message += `Authorization=${authHeader}`;
77
+ }
78
+ else {
79
+ for (const param of ['X-Amz-Algorithm', 'X-Amz-Credential', 'X-Amz-Signature', 'X-Amz-SignedHeaders', 'X-Amz-Date']) {
80
+ if (!query.get(param))
81
+ message += `AWS query-string parameters must include '${param}'. `;
82
+ }
83
+ if (message)
84
+ message += 'Re-examine the query-string parameters.';
85
+ }
86
+ return message ? new KinesisError('IncompleteSignatureException', message) : null;
87
+ }
88
+ function streamEvents(exchange, subscription) {
89
+ const { req, res } = exchange;
90
+ req.removeAllListeners('data');
91
+ res.statusCode = 200;
92
+ res.setHeader('Content-Type', EVENT_STREAM);
93
+ const closeListeners = [];
94
+ let ended = false;
95
+ const closed = () => {
96
+ if (ended)
97
+ return;
98
+ ended = true;
99
+ for (const listener of closeListeners)
100
+ listener();
101
+ };
102
+ res.on('close', closed);
103
+ subscription.start({
104
+ write: chunk => {
105
+ if (!ended)
106
+ res.write(chunk);
107
+ },
108
+ end: () => {
109
+ if (!ended) {
110
+ ended = true;
111
+ res.end();
112
+ }
113
+ },
114
+ onClose: listener => {
115
+ closeListeners.push(listener);
116
+ },
117
+ });
118
+ }
119
+ function dispatch(store, exchange, operation, input) {
120
+ const definition = operations[operation];
121
+ const context = { store, protocol: protocolOf(exchange) };
122
+ let data = coerceStructure(input, definition.input);
123
+ validateStructure(data, definition.input, validationContext(store, data));
124
+ data = decodeBlobs(data, definition.input);
125
+ const result = handlers[operation](context, data);
126
+ if (result instanceof Subscription)
127
+ return streamEvents(exchange, result);
128
+ sendResponse(exchange, result === undefined ? undefined : serializeShape(result, definition.output, context.protocol));
129
+ }
130
+ function respond(store, exchange, body) {
131
+ const { req, res } = exchange;
132
+ res.setHeader('x-amzn-RequestId', randomUUID());
133
+ if (req.method !== 'OPTIONS' || !req.headers.origin)
134
+ res.setHeader('x-amz-id-2', randomBytes(72).toString('base64'));
135
+ if (req.headers.origin) {
136
+ res.setHeader('Access-Control-Allow-Origin', '*');
137
+ if (req.method === 'OPTIONS') {
138
+ const requestHeaders = header(req, 'access-control-request-headers');
139
+ const requestMethod = header(req, 'access-control-request-method');
140
+ if (requestHeaders)
141
+ res.setHeader('Access-Control-Allow-Headers', requestHeaders);
142
+ if (requestMethod)
143
+ res.setHeader('Access-Control-Allow-Methods', requestMethod);
144
+ res.setHeader('Access-Control-Max-Age', 172800);
145
+ return sendRaw(exchange, '');
146
+ }
147
+ res.setHeader('Access-Control-Expose-Headers', EXPOSED_HEADERS);
148
+ }
149
+ if (req.method !== 'POST') {
150
+ return sendError(exchange, false, 'AccessDeniedException', 'Unable to determine service/operation name to be authorized');
151
+ }
152
+ const contentType = header(req, 'content-type').split(';')[0].trim();
153
+ const contentValid = [AMZ_JSON, AMZ_CBOR, 'application/json'].includes(contentType);
154
+ const [service, operation] = header(req, 'x-amz-target').split('.');
155
+ const serviceValid = !!service && service === serviceTarget;
156
+ const operationValid = !!operation && Object.hasOwn(handlers, operation);
157
+ if (!body) {
158
+ exchange.contentType = contentType === AMZ_JSON ? AMZ_JSON : AMZ_CBOR;
159
+ return sendResponse(exchange, { __type: serviceValid && operationValid ? 'SerializationException' : 'UnknownOperationException' }, 400);
160
+ }
161
+ if (!contentValid) {
162
+ if (!service || !operation) {
163
+ return sendError(exchange, false, 'AccessDeniedException', 'Unable to determine service/operation name to be authorized');
164
+ }
165
+ return sendError(exchange, false, 'UnknownOperationException', 404);
166
+ }
167
+ exchange.contentType = contentType;
168
+ let data;
169
+ try {
170
+ data = contentType === AMZ_CBOR ? decodeCbor(body) : JSON.parse(body.toString('utf8'));
171
+ }
172
+ catch {
173
+ data = undefined;
174
+ }
175
+ if (typeof data !== 'object' || data === null) {
176
+ if (contentType === 'application/json') {
177
+ return sendResponse(exchange, { Output: { __type: 'com.amazon.coral.service#SerializationException' }, Version: '1.0' }, 400);
178
+ }
179
+ return sendResponse(exchange, { __type: 'SerializationException' }, 400);
180
+ }
181
+ if (contentType === 'application/json') {
182
+ return sendResponse(exchange, { Output: { __type: 'com.amazon.coral.service#UnknownOperationException' }, Version: '1.0' }, 404);
183
+ }
184
+ if (!serviceValid || !operationValid)
185
+ return sendResponse(exchange, { __type: 'UnknownOperationException' }, 400);
186
+ const authError = checkAuthentication(exchange);
187
+ if (authError)
188
+ return sendResponse(exchange, authError.body, authError.statusCode);
189
+ try {
190
+ dispatch(store, exchange, operation, data);
191
+ }
192
+ catch (error) {
193
+ if (error instanceof KinesisError)
194
+ return sendResponse(exchange, error.body, error.statusCode);
195
+ console.error(error instanceof Error ? error.stack : error);
196
+ return sendResponse(exchange, { __type: 'InternalFailure' }, 500);
197
+ }
198
+ }
199
+ export function createHandler(store) {
200
+ return (req, res) => {
201
+ const exchange = { req, res: res, contentType: AMZ_JSON };
202
+ const chunks = [];
203
+ let size = 0;
204
+ let rejected = false;
205
+ req.on('error', () => res.destroy());
206
+ req.on('data', (chunk) => {
207
+ if (rejected)
208
+ return;
209
+ size += chunk.length;
210
+ if (size > MAX_REQUEST_BYTES) {
211
+ rejected = true;
212
+ if (req.httpVersionMajor < 2)
213
+ res.setHeader('Transfer-Encoding', 'chunked');
214
+ sendRaw(exchange, null, 413);
215
+ return;
216
+ }
217
+ chunks.push(chunk);
218
+ });
219
+ req.on('end', () => {
220
+ if (rejected)
221
+ return;
222
+ respond(store, exchange, size ? Buffer.concat(chunks, size) : undefined);
223
+ });
224
+ };
225
+ }
@@ -0,0 +1 @@
1
+ export { defaultOptions, start, type KinesisLocal, type StartOptions } from './server.ts';
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { defaultOptions, start } from './server.js';
@@ -0,0 +1,9 @@
1
+ export declare const ITERATOR_TTL_MS = 300000;
2
+ export interface IteratorContents {
3
+ createdAt: number;
4
+ streamName: string;
5
+ shardId: string;
6
+ sequenceNumber: string;
7
+ }
8
+ export declare function createShardIterator(streamName: string, shardId: string, sequenceNumber: string, now?: number): string;
9
+ export declare function readShardIterator(iterator: string): IteratorContents | null;
@@ -0,0 +1,29 @@
1
+ import { createCipheriv, createDecipheriv } from 'node:crypto';
2
+ const KEY = Buffer.from('1133a5a833666b49abf28c8ba302930f0b2fb240dccd43cf4dfbc0ca91f17751', 'hex');
3
+ const IV = Buffer.from('7bf139dbabbea2d9995d6fcae1dff7da', 'hex');
4
+ const HEADER = Buffer.from([0, 0, 0, 0, 0, 0, 0, 1]);
5
+ export const ITERATOR_TTL_MS = 300000;
6
+ export function createShardIterator(streamName, shardId, sequenceNumber, now = Date.now()) {
7
+ const plain = [('0000000000000' + now).slice(-14), streamName, shardId, sequenceNumber, '0'.repeat(36)].join('/');
8
+ const cipher = createCipheriv('aes-256-cbc', KEY, IV);
9
+ return Buffer.concat([HEADER, cipher.update(plain, 'utf8'), cipher.final()]).toString('base64');
10
+ }
11
+ export function readShardIterator(iterator) {
12
+ const buffer = Buffer.from(iterator, 'base64');
13
+ if (buffer.length < 152 || buffer.length > 280 || buffer.toString('base64') !== iterator)
14
+ return null;
15
+ if (!buffer.subarray(0, 8).equals(HEADER))
16
+ return null;
17
+ let pieces;
18
+ try {
19
+ const decipher = createDecipheriv('aes-256-cbc', KEY, IV);
20
+ pieces = Buffer.concat([decipher.update(buffer.subarray(8)), decipher.final()]).toString('utf8').split('/');
21
+ }
22
+ catch {
23
+ return null;
24
+ }
25
+ if (pieces.length !== 5)
26
+ return null;
27
+ const [createdAt, streamName, shardId, sequenceNumber] = pieces;
28
+ return { createdAt: +createdAt, streamName, shardId, sequenceNumber };
29
+ }
@@ -0,0 +1,25 @@
1
+ export interface SequenceParts {
2
+ shardCreateTime: number;
3
+ shardIx: number;
4
+ seqIx?: string | number | bigint | null;
5
+ byte1?: string;
6
+ seqTime?: number;
7
+ seqRand?: string;
8
+ version?: number;
9
+ }
10
+ export interface ParsedSequence extends SequenceParts {
11
+ seqTime: number;
12
+ version: number;
13
+ }
14
+ export declare const POW_2_128: bigint;
15
+ export declare const MAX_SEQUENCE_INDEX = 9223372036854775807n;
16
+ export declare function parseSequence(sequence: string): ParsedSequence;
17
+ export declare function stringifySequence(parts: SequenceParts): string;
18
+ export declare function incrementSequence(sequence: ParsedSequence | string, seqTime?: number): string;
19
+ export declare function shardIxToHex(shardIx: number): string;
20
+ export declare function shardIdName(shardIx: number): string;
21
+ export declare function resolveShardId(shardId: string): {
22
+ shardId: string;
23
+ shardIx: number;
24
+ } | null;
25
+ export declare function partitionKeyToHashKey(partitionKey: string): bigint;
@@ -0,0 +1,126 @@
1
+ import { createHash } from 'node:crypto';
2
+ const POW_2_124 = 2n ** 124n;
3
+ const POW_2_124_NEXT = 2n ** 129n;
4
+ const POW_2_185 = 2n ** 185n;
5
+ const POW_2_185_NEXT = 3n * 2n ** 184n;
6
+ export const POW_2_128 = 2n ** 128n;
7
+ export const MAX_SEQUENCE_INDEX = 0x7fffffffffffffffn;
8
+ export function parseSequence(sequence) {
9
+ if (!/^\d+$/.test(sequence))
10
+ throw new Error(`Invalid sequence number: ${sequence}`);
11
+ let value = BigInt(sequence);
12
+ if (value < POW_2_124)
13
+ value += POW_2_124;
14
+ const hex = value.toString(16);
15
+ const version = value < POW_2_124_NEXT ? 0
16
+ : value > POW_2_185 && value < POW_2_185_NEXT ? parseInt(hex.slice(-1), 16) : null;
17
+ if (version === 2) {
18
+ const seqIxHex = hex.slice(11, 27);
19
+ let shardIxHex = hex.slice(38, 46);
20
+ if (parseInt(seqIxHex[0], 16) > 7)
21
+ throw new Error('Sequence index too high');
22
+ if (parseInt(shardIxHex[0], 16) > 7)
23
+ shardIxHex = '-' + shardIxHex;
24
+ const shardCreateSecs = parseInt(hex.slice(1, 10), 16);
25
+ if (shardCreateSecs >= 16025175000)
26
+ throw new Error(`Date too large: ${shardCreateSecs}`);
27
+ return {
28
+ shardCreateTime: shardCreateSecs * 1000,
29
+ seqIx: BigInt('0x' + seqIxHex).toString(),
30
+ byte1: hex.slice(27, 29),
31
+ seqTime: parseInt(hex.slice(29, 38), 16) * 1000,
32
+ shardIx: parseInt(shardIxHex, 16),
33
+ version,
34
+ };
35
+ }
36
+ if (version === 1) {
37
+ return {
38
+ shardCreateTime: parseInt(hex.slice(1, 10), 16) * 1000,
39
+ byte1: hex.slice(11, 13),
40
+ seqTime: parseInt(hex.slice(13, 22), 16) * 1000,
41
+ seqRand: hex.slice(22, 36),
42
+ seqIx: parseInt(hex.slice(36, 38), 16),
43
+ shardIx: parseInt(hex.slice(38, 46), 16),
44
+ version,
45
+ };
46
+ }
47
+ if (version === 0) {
48
+ const shardCreateSecs = parseInt(hex.slice(1, 10), 16);
49
+ if (shardCreateSecs >= 16025175000)
50
+ throw new Error(`Date too large: ${shardCreateSecs}`);
51
+ return {
52
+ shardCreateTime: shardCreateSecs * 1000,
53
+ byte1: hex.slice(10, 12),
54
+ seqRand: hex.slice(12, 28),
55
+ seqTime: NaN,
56
+ shardIx: parseInt(hex.slice(28, 32), 16),
57
+ version,
58
+ };
59
+ }
60
+ throw new Error(`Unknown version: ${version}`);
61
+ }
62
+ function hexOfSeconds(milliseconds) {
63
+ return ('00000000' + Math.floor(milliseconds / 1000).toString(16)).slice(-9);
64
+ }
65
+ export function stringifySequence(parts) {
66
+ if (parts.version == null || parts.version === 2) {
67
+ return BigInt('0x' + [
68
+ '2',
69
+ hexOfSeconds(parts.shardCreateTime),
70
+ (parts.shardIx || 0).toString(16).slice(-1),
71
+ ('0000000000000000' + BigInt(parts.seqIx || 0).toString(16)).slice(-16),
72
+ parts.byte1 || '00',
73
+ hexOfSeconds(parts.seqTime || parts.shardCreateTime),
74
+ shardIxToHex(parts.shardIx),
75
+ '2',
76
+ ].join('')).toString();
77
+ }
78
+ if (parts.version === 1) {
79
+ return BigInt('0x' + [
80
+ '2',
81
+ hexOfSeconds(parts.shardCreateTime),
82
+ (parts.shardIx || 0).toString(16).slice(-1),
83
+ parts.byte1 || '00',
84
+ hexOfSeconds(parts.seqTime || parts.shardCreateTime),
85
+ parts.seqRand || '00000000000000',
86
+ ('0' + Number(parts.seqIx || 0).toString(16)).slice(-2),
87
+ shardIxToHex(parts.shardIx),
88
+ '1',
89
+ ].join('')).toString();
90
+ }
91
+ if (parts.version === 0) {
92
+ return BigInt('0x' + [
93
+ '1',
94
+ hexOfSeconds(parts.shardCreateTime),
95
+ parts.byte1 || '00',
96
+ parts.seqRand || '0000000000000000',
97
+ shardIxToHex(parts.shardIx).slice(-4),
98
+ ].join('')).toString();
99
+ }
100
+ throw new Error(`Unknown version: ${parts.version}`);
101
+ }
102
+ export function incrementSequence(sequence, seqTime) {
103
+ const parsed = typeof sequence === 'string' ? parseSequence(sequence) : sequence;
104
+ return stringifySequence({
105
+ shardCreateTime: parsed.shardCreateTime,
106
+ seqIx: parsed.seqIx ?? null,
107
+ seqTime: seqTime || parsed.seqTime + 1000,
108
+ shardIx: parsed.shardIx,
109
+ });
110
+ }
111
+ export function shardIxToHex(shardIx) {
112
+ return ('0000000' + (shardIx || 0).toString(16)).slice(-8);
113
+ }
114
+ export function shardIdName(shardIx) {
115
+ return 'shardId-' + (shardIx < 0 ? '-' : '') + ('00000000000' + Math.abs(shardIx)).slice(shardIx < 0 ? -11 : -12);
116
+ }
117
+ export function resolveShardId(shardId) {
118
+ const suffix = shardId.split('-')[1] || shardId;
119
+ const shardIx = /^\d+$/.test(suffix) ? parseInt(suffix, 10) : NaN;
120
+ if (!(shardIx >= 0 && shardIx <= 2147483647))
121
+ return null;
122
+ return { shardId: shardIdName(shardIx), shardIx };
123
+ }
124
+ export function partitionKeyToHashKey(partitionKey) {
125
+ return BigInt('0x' + createHash('md5').update(partitionKey, 'utf8').digest('hex'));
126
+ }
@@ -0,0 +1,45 @@
1
+ export interface Bounds {
2
+ min?: number;
3
+ max?: number;
4
+ }
5
+ export interface Member {
6
+ name: string;
7
+ target: string;
8
+ required: boolean;
9
+ }
10
+ export type Shape = {
11
+ type: 'structure';
12
+ members: Member[];
13
+ } | {
14
+ type: 'union';
15
+ members: Member[];
16
+ } | {
17
+ type: 'list';
18
+ member: string;
19
+ length?: Bounds;
20
+ } | {
21
+ type: 'map';
22
+ key: string;
23
+ value: string;
24
+ length?: Bounds;
25
+ } | {
26
+ type: 'string';
27
+ length?: Bounds;
28
+ pattern?: string;
29
+ enum?: string[];
30
+ } | {
31
+ type: 'integer' | 'long' | 'double';
32
+ range?: Bounds;
33
+ } | {
34
+ type: 'blob';
35
+ length?: Bounds;
36
+ } | {
37
+ type: 'boolean';
38
+ } | {
39
+ type: 'timestamp';
40
+ };
41
+ export interface Operation {
42
+ input: string;
43
+ output: string | null;
44
+ errors: string[];
45
+ }
package/dist/model.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,25 @@
1
+ import { type Context, type Input } from './common.ts';
2
+ export declare function describeLimits(context: Context): {
3
+ ShardLimit: number;
4
+ OpenShardCount: number;
5
+ OnDemandStreamCount: number;
6
+ OnDemandStreamCountLimit: number;
7
+ ChannelCount: number;
8
+ ChannelCountLimit: number;
9
+ };
10
+ export declare function describeAccountSettings(context: Context): {
11
+ MinimumThroughputBillingCommitment: {
12
+ Status: 'ENABLED' | 'DISABLED' | 'ENABLED_UNTIL_EARLIEST_ALLOWED_END';
13
+ StartedAt?: number;
14
+ EndedAt?: number;
15
+ EarliestAllowedEndAt?: number;
16
+ };
17
+ };
18
+ export declare function updateAccountSettings(context: Context, data: Input): {
19
+ MinimumThroughputBillingCommitment: {
20
+ Status: 'ENABLED' | 'DISABLED' | 'ENABLED_UNTIL_EARLIEST_ALLOWED_END';
21
+ StartedAt?: number;
22
+ EndedAt?: number;
23
+ EarliestAllowedEndAt?: number;
24
+ };
25
+ };
@@ -0,0 +1,30 @@
1
+ import { CHANNEL_LIMIT, ON_DEMAND_STREAM_LIMIT, nowSeconds, provisionedOpenShards, streamMode, } from './common.js';
2
+ const COMMITMENT_WINDOW_SECONDS = 24 * 60 * 60;
3
+ export function describeLimits(context) {
4
+ const { store } = context;
5
+ return {
6
+ ShardLimit: store.options.shardLimit,
7
+ OpenShardCount: provisionedOpenShards(context),
8
+ OnDemandStreamCount: [...store.streams.values()].filter(stream => streamMode(stream) === 'ON_DEMAND').length,
9
+ OnDemandStreamCountLimit: ON_DEMAND_STREAM_LIMIT,
10
+ ChannelCount: store.channels.size,
11
+ ChannelCountLimit: CHANNEL_LIMIT,
12
+ };
13
+ }
14
+ export function describeAccountSettings(context) {
15
+ return { MinimumThroughputBillingCommitment: context.store.settings.MinimumThroughputBillingCommitment };
16
+ }
17
+ export function updateAccountSettings(context, data) {
18
+ const requested = data.MinimumThroughputBillingCommitment.Status;
19
+ const current = context.store.settings.MinimumThroughputBillingCommitment;
20
+ const now = nowSeconds();
21
+ let next;
22
+ if (requested === 'ENABLED') {
23
+ next = current.Status === 'ENABLED' ? current : { Status: 'ENABLED', StartedAt: now, EarliestAllowedEndAt: now + COMMITMENT_WINDOW_SECONDS };
24
+ }
25
+ else {
26
+ next = current.Status === 'DISABLED' ? current : { Status: 'DISABLED', ...(current.StartedAt ? { StartedAt: current.StartedAt } : {}), EndedAt: now };
27
+ }
28
+ context.store.saveSettings({ MinimumThroughputBillingCommitment: next });
29
+ return { MinimumThroughputBillingCommitment: next };
30
+ }
@@ -0,0 +1,14 @@
1
+ import type { Store } from '../store/store.ts';
2
+ import { type Context, type Input } from './common.ts';
3
+ export declare function updateStreamMode(context: Context, data: Input): void;
4
+ export declare function updateMaxRecordSize(context: Context, data: Input): void;
5
+ export declare function updateStreamWarmThroughput(context: Context, data: Input): {
6
+ StreamARN: string;
7
+ StreamName: string;
8
+ WarmThroughput: {
9
+ TargetMiBps: number;
10
+ CurrentMiBps: number;
11
+ };
12
+ };
13
+ export declare function updateStreamRecordDistributionStrategy(context: Context, data: Input): void;
14
+ export declare function registerCapacityTransitions(store: Store): void;
@@ -0,0 +1,89 @@
1
+ import { invalidArgument, limitExceeded } from '../errors.js';
2
+ import { ON_DEMAND_STREAM_LIMIT, account, getStream, streamMode, streamNameOf, } from './common.js';
3
+ import { requireBillingCommitment } from './streams.js';
4
+ function activeStream(context, name) {
5
+ const stream = getStream(context, name);
6
+ if (stream.StreamStatus !== 'ACTIVE') {
7
+ throw invalidArgument(`Stream ${name} under account ${account(context)} not ACTIVE, instead in state ${stream.StreamStatus}`);
8
+ }
9
+ return stream;
10
+ }
11
+ export function updateStreamMode(context, data) {
12
+ const name = streamNameOf(data);
13
+ const stream = activeStream(context, name);
14
+ const mode = data.StreamModeDetails.StreamMode;
15
+ if (mode === 'ON_DEMAND' && streamMode(stream) !== 'ON_DEMAND') {
16
+ const onDemand = [...context.store.streams.values()].filter(other => streamMode(other) === 'ON_DEMAND').length;
17
+ if (onDemand >= ON_DEMAND_STREAM_LIMIT) {
18
+ throw limitExceeded(`This request would exceed the limit of ${ON_DEMAND_STREAM_LIMIT} ON_DEMAND streams for the account ${account(context)}.`);
19
+ }
20
+ }
21
+ if (data.WarmThroughputMiBps != null) {
22
+ if (mode !== 'ON_DEMAND')
23
+ throw invalidArgument('WarmThroughputMiBps is supported only for streams in ON_DEMAND capacity mode.');
24
+ requireBillingCommitment(context, 'WarmThroughputMiBps');
25
+ }
26
+ stream.StreamStatus = 'UPDATING';
27
+ context.store.saveStream(stream);
28
+ context.store.schedule('stream', name, 'updateStreamMode', context.store.options.updateStreamMs, {
29
+ mode,
30
+ warmThroughput: data.WarmThroughputMiBps ?? null,
31
+ });
32
+ }
33
+ export function updateMaxRecordSize(context, data) {
34
+ const name = streamNameOf(data);
35
+ const stream = activeStream(context, name);
36
+ stream.MaxRecordSizeInKiB = data.MaxRecordSizeInKiB;
37
+ context.store.saveStream(stream);
38
+ }
39
+ export function updateStreamWarmThroughput(context, data) {
40
+ const name = streamNameOf(data);
41
+ const stream = activeStream(context, name);
42
+ if (streamMode(stream) !== 'ON_DEMAND') {
43
+ throw invalidArgument(`UpdateStreamWarmThroughput is supported only for streams in ON_DEMAND capacity mode. Stream ${name} is PROVISIONED.`);
44
+ }
45
+ requireBillingCommitment(context, 'WarmThroughputMiBps');
46
+ const target = data.WarmThroughputMiBps;
47
+ const current = stream.WarmThroughput?.CurrentMiBps ?? 0;
48
+ stream.WarmThroughput = { TargetMiBps: target, CurrentMiBps: current };
49
+ stream.StreamStatus = 'UPDATING';
50
+ context.store.saveStream(stream);
51
+ context.store.schedule('stream', name, 'updateWarmThroughput', context.store.options.updateStreamMs, { target });
52
+ return { StreamARN: stream.StreamARN, StreamName: name, WarmThroughput: { TargetMiBps: target, CurrentMiBps: current } };
53
+ }
54
+ export function updateStreamRecordDistributionStrategy(context, data) {
55
+ const name = streamNameOf(data);
56
+ const stream = activeStream(context, name);
57
+ if (streamMode(stream) !== 'ON_DEMAND') {
58
+ throw invalidArgument(`UpdateStreamRecordDistributionStrategy is supported only for streams in ON_DEMAND capacity mode. Stream ${name} is PROVISIONED.`);
59
+ }
60
+ stream.RecordDistributionStrategy = data.RecordDistributionStrategy;
61
+ context.store.saveStream(stream);
62
+ }
63
+ export function registerCapacityTransitions(store) {
64
+ store.registerTransition('updateStreamMode', (current, name, data) => {
65
+ const stream = current.streams.get(name);
66
+ if (!stream)
67
+ return;
68
+ const { mode, warmThroughput } = data;
69
+ stream.StreamModeDetails = { StreamMode: mode };
70
+ if (mode === 'PROVISIONED') {
71
+ delete stream.WarmThroughput;
72
+ delete stream.RecordDistributionStrategy;
73
+ }
74
+ else if (warmThroughput !== null) {
75
+ stream.WarmThroughput = { TargetMiBps: warmThroughput, CurrentMiBps: warmThroughput };
76
+ }
77
+ stream.StreamStatus = 'ACTIVE';
78
+ current.saveStream(stream);
79
+ });
80
+ store.registerTransition('updateWarmThroughput', (current, name, data) => {
81
+ const stream = current.streams.get(name);
82
+ if (!stream)
83
+ return;
84
+ const { target } = data;
85
+ stream.WarmThroughput = { TargetMiBps: target, CurrentMiBps: target };
86
+ stream.StreamStatus = 'ACTIVE';
87
+ current.saveStream(stream);
88
+ });
89
+ }
@@ -0,0 +1,14 @@
1
+ import type { Store } from '../store/store.ts';
2
+ import { type Context, type Input } from './common.ts';
3
+ export declare function createChannel(context: Context, data: Input): {
4
+ ChannelDescription: Record<string, unknown>;
5
+ };
6
+ export declare function describeChannel(context: Context, data: Input): {
7
+ ChannelDescription: Record<string, unknown>;
8
+ };
9
+ export declare function listChannels(context: Context, data: Input): Record<string, unknown>;
10
+ export declare function updateChannel(context: Context, data: Input): {
11
+ ChannelDescription: Record<string, unknown>;
12
+ };
13
+ export declare function deleteChannel(context: Context, data: Input): void;
14
+ export declare function registerChannelTransitions(store: Store): void;