@saidsef/aws-kinesis-local 1.3.0 → 1.4.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.
package/dist/handler.js CHANGED
@@ -10,6 +10,7 @@ import { coerceStructure, decodeBlobs, validateStructure } from './validation/va
10
10
  const MAX_REQUEST_BYTES = 16 * 1024 * 1024;
11
11
  const EVENT_STREAM = 'application/vnd.amazon.eventstream';
12
12
  const HEALTH_PATHS = ['/healthz', '/readyz'];
13
+ const RESET_PATH = '/_control/reset';
13
14
  const EXPOSED_HEADERS = 'x-amzn-RequestId,x-amzn-ErrorType,x-amz-request-id,x-amz-id-2,x-amzn-ErrorMessage,Date';
14
15
  function header(req, name) {
15
16
  const value = req.headers[name];
@@ -43,6 +44,22 @@ function sendHealth(store, exchange, path) {
43
44
  exchange.res.setHeader('Content-Type', 'text/plain');
44
45
  sendRaw(exchange, healthy ? 'ok\n' : 'unavailable\n', healthy ? 200 : 503);
45
46
  }
47
+ function sendReset(store, exchange, body) {
48
+ let input = {};
49
+ try {
50
+ if (body)
51
+ input = JSON.parse(body.toString('utf8'));
52
+ }
53
+ catch {
54
+ input = undefined;
55
+ }
56
+ const stream = input?.stream;
57
+ exchange.contentType = 'application/json';
58
+ if (typeof input !== 'object' || input === null || Array.isArray(input) || (stream !== undefined && typeof stream !== 'string')) {
59
+ return sendResponse(exchange, { message: 'The body must be empty or a JSON object whose stream member is a string.' }, 400);
60
+ }
61
+ sendResponse(exchange, { streams: store.reset(stream) });
62
+ }
46
63
  function validationContext(store, data) {
47
64
  let name = typeof data.StreamName === 'string' ? data.StreamName : undefined;
48
65
  if (name === undefined && typeof data.StreamARN === 'string')
@@ -155,6 +172,8 @@ function respond(store, exchange, body) {
155
172
  const path = req.url?.split('?')[0] ?? '';
156
173
  if (req.method === 'GET' && HEALTH_PATHS.includes(path))
157
174
  return sendHealth(store, exchange, path);
175
+ if (req.method === 'POST' && path === RESET_PATH)
176
+ return sendReset(store, exchange, body);
158
177
  if (req.method !== 'POST') {
159
178
  return sendError(exchange, false, 'AccessDeniedException', 'Unable to determine service/operation name to be authorized');
160
179
  }
@@ -14,7 +14,13 @@ const states = new WeakMap();
14
14
  function stateOf(store) {
15
15
  let state = states.get(store);
16
16
  if (!state) {
17
- state = { lastSubscribed: new Map(), active: new Map() };
17
+ const lastSubscribed = new Map();
18
+ store.events.on('consumer-removed', (arn) => {
19
+ for (const key of lastSubscribed.keys())
20
+ if (key.startsWith(`${arn}|`))
21
+ lastSubscribed.delete(key);
22
+ });
23
+ state = { lastSubscribed, active: new Map() };
18
24
  states.set(store, state);
19
25
  }
20
26
  return state;
package/dist/server.d.ts CHANGED
@@ -19,6 +19,7 @@ export interface KinesisLocal {
19
19
  endpoint: string;
20
20
  address: AddressInfo;
21
21
  protocol: 'http' | 'https';
22
+ reset(stream?: string): string[];
22
23
  close(): Promise<void>;
23
24
  }
24
25
  export declare function defaultOptions(): StartOptions;
package/dist/server.js CHANGED
@@ -140,6 +140,7 @@ export async function start(overrides = {}) {
140
140
  endpoint: `${protocol}://${hostname(address)}:${address.port}`,
141
141
  address,
142
142
  protocol,
143
+ reset: stream => store.reset(stream),
143
144
  close: () => closing ??= shutdown(),
144
145
  };
145
146
  }
@@ -42,6 +42,7 @@ export declare class Store {
42
42
  savePolicy(arn: string, policy: string): void;
43
43
  removePolicy(arn: string): void;
44
44
  saveSettings(settings: AccountSettings): void;
45
+ reset(name?: string): string[];
45
46
  insertRecords(stream: string, records: NewRecord[]): void;
46
47
  readRecords(stream: string, shardIx: number, fromSequence: string, limit: number): StoredRecord[];
47
48
  firstSequenceAtOrAfter(stream: string, shardIx: number, timestamp: number): string | undefined;
@@ -5,6 +5,7 @@ import { DatabaseSync } from 'node:sqlite';
5
5
  import { parseSequence } from '../kinesis/sequence.js';
6
6
  export const DATABASE_FILE = 'aws-kinesis-local.sqlite';
7
7
  const EXPIRY_BATCH_SIZE = 500;
8
+ const defaultSettings = () => ({ MinimumThroughputBillingCommitment: { Status: 'DISABLED' } });
8
9
  export class Store {
9
10
  options;
10
11
  streams = new Map();
@@ -12,7 +13,7 @@ export class Store {
12
13
  channels = new Map();
13
14
  policies = new Map();
14
15
  events = new EventEmitter();
15
- settings = { MinimumThroughputBillingCommitment: { Status: 'DISABLED' } };
16
+ settings = defaultSettings();
16
17
  draining = false;
17
18
  db;
18
19
  transitions = new Map();
@@ -147,6 +148,30 @@ export class Store {
147
148
  this.settings = settings;
148
149
  this.write('settings', 'account', settings);
149
150
  }
151
+ reset(name) {
152
+ const names = name === undefined ? [...this.streams.keys()].sort() : this.streams.has(name) ? [name] : [];
153
+ const arns = new Set(names.map(stream => this.streams.get(stream).StreamARN));
154
+ const consumers = [...this.consumers.values()].filter(consumer => names.includes(consumer.StreamName));
155
+ const channels = [...this.channels.values()].filter(channel => name === undefined || channel.StreamConfigurationList.some(entry => arns.has(entry.StreamARN)));
156
+ this.transaction(() => {
157
+ for (const stream of names) {
158
+ this.events.emit('stream-deleted', stream);
159
+ this.removePolicy(this.streams.get(stream).StreamARN);
160
+ this.removeStream(stream);
161
+ }
162
+ for (const consumer of consumers)
163
+ this.removeConsumer(consumer.ConsumerARN);
164
+ for (const channel of channels)
165
+ this.removeChannel(channel.ChannelARN);
166
+ if (name === undefined) {
167
+ for (const arn of [...this.policies.keys()])
168
+ this.removePolicy(arn);
169
+ this.settings = defaultSettings();
170
+ this.erase('settings', 'account');
171
+ }
172
+ });
173
+ return names;
174
+ }
150
175
  insertRecords(stream, records) {
151
176
  for (const record of records) {
152
177
  this.statements.putRecord.run(stream, record.shardIx, record.sequenceNumber, record.partitionKey, record.data, record.arrival, record.encryptionType);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saidsef/aws-kinesis-local",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Local Amazon Kinesis Data Streams server for development and CI, with all 45 operations, enhanced fan-out, JSON and CBOR over HTTP/1.1 and HTTP/2",
5
5
  "keywords": [
6
6
  "aws",