dynamodb-reactive 0.2.2 → 0.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.
package/dist/server.cjs CHANGED
@@ -2074,7 +2074,7 @@ async function mapWithConcurrency(values, concurrency, mapper) {
2074
2074
  await Promise.all(workers);
2075
2075
  return results;
2076
2076
  }
2077
- function extractRecordAffectedKeys(record) {
2077
+ async function processRecord(record, utils) {
2078
2078
  const tableName = extractTableName(record);
2079
2079
  if (!tableName || !record.dynamodb) {
2080
2080
  throw new Error("Malformed DynamoDB stream record");
@@ -2085,84 +2085,53 @@ function extractRecordAffectedKeys(record) {
2085
2085
  const item = utilDynamodb.unmarshall(image);
2086
2086
  for (const key of extractAffectedKeys(tableName, item)) affectedKeys.add(key);
2087
2087
  }
2088
- return affectedKeys;
2089
- }
2090
- function subscriptionKey(subscription) {
2091
- return JSON.stringify([
2092
- subscription.connectionId,
2093
- subscription.subscriptionId
2094
- ]);
2095
- }
2096
- function failureIdentifier(record, cause) {
2097
- const itemIdentifier = record.dynamodb?.SequenceNumber;
2098
- if (!itemIdentifier) {
2099
- throw new Error("Failed DynamoDB stream record has no sequence number", {
2100
- cause
2101
- });
2102
- }
2103
- return itemIdentifier;
2104
- }
2105
- async function processStreamEvent(event, utils) {
2106
- const failures = /* @__PURE__ */ new Map();
2107
- const dependencies = /* @__PURE__ */ new Map();
2108
- for (const [recordIndex, record] of event.Records.entries()) {
2109
- try {
2110
- for (const dependencyKey of extractRecordAffectedKeys(record)) {
2111
- const recordIndexes = dependencies.get(dependencyKey) ?? /* @__PURE__ */ new Set();
2112
- recordIndexes.add(recordIndex);
2113
- dependencies.set(dependencyKey, recordIndexes);
2114
- }
2115
- } catch (error2) {
2116
- error("[stream] Record dependency extraction failed:", error2);
2117
- failures.set(recordIndex, error2);
2118
- }
2119
- }
2120
2088
  const subscriptions = /* @__PURE__ */ new Map();
2121
- await mapWithConcurrency(
2122
- [...dependencies],
2089
+ const lookupResults = await mapWithConcurrency(
2090
+ [...affectedKeys],
2123
2091
  MAX_FANOUT_CONCURRENCY,
2124
- async ([dependencyKey, recordIndexes]) => {
2125
- try {
2126
- const affectedSubscriptions = await utils.findAffectedSubscriptions(dependencyKey);
2127
- for (const subscription of affectedSubscriptions) {
2128
- const key = subscriptionKey(subscription);
2129
- const work = subscriptions.get(key) ?? {
2130
- ...subscription,
2131
- recordIndexes: /* @__PURE__ */ new Set()
2132
- };
2133
- for (const recordIndex of recordIndexes) {
2134
- work.recordIndexes.add(recordIndex);
2135
- }
2136
- subscriptions.set(key, work);
2137
- }
2138
- } catch (error2) {
2139
- error("[stream] Subscription dependency lookup failed:", error2);
2140
- for (const recordIndex of recordIndexes) {
2141
- failures.set(recordIndex, error2);
2142
- }
2143
- }
2144
- }
2092
+ (key) => utils.findAffectedSubscriptions(key)
2145
2093
  );
2094
+ for (const result of lookupResults) {
2095
+ for (const subscription of result) {
2096
+ subscriptions.set(
2097
+ `${subscription.connectionId}\0${subscription.subscriptionId}`,
2098
+ subscription
2099
+ );
2100
+ }
2101
+ }
2146
2102
  await mapWithConcurrency(
2147
2103
  [...subscriptions.values()],
2148
2104
  MAX_FANOUT_CONCURRENCY,
2149
- async ({ connectionId, subscriptionId, recordIndexes }) => {
2105
+ ({ connectionId, subscriptionId }) => utils.processSubscription(connectionId, subscriptionId)
2106
+ );
2107
+ }
2108
+ async function processStreamEvent(event, utils) {
2109
+ const results = await mapWithConcurrency(
2110
+ event.Records,
2111
+ MAX_FANOUT_CONCURRENCY,
2112
+ async (record) => {
2150
2113
  try {
2151
- await utils.processSubscription(connectionId, subscriptionId);
2114
+ await processRecord(record, utils);
2115
+ return null;
2152
2116
  } catch (error2) {
2153
- error("[stream] Subscription refresh failed:", error2);
2154
- for (const recordIndex of recordIndexes) {
2155
- failures.set(recordIndex, error2);
2117
+ error("[stream] Record failed:", error2);
2118
+ const itemIdentifier = record.dynamodb?.SequenceNumber;
2119
+ if (!itemIdentifier) {
2120
+ throw new Error(
2121
+ "Failed DynamoDB stream record has no sequence number",
2122
+ { cause: error2 }
2123
+ );
2156
2124
  }
2125
+ return {
2126
+ itemIdentifier
2127
+ };
2157
2128
  }
2158
2129
  }
2159
2130
  );
2160
2131
  return {
2161
- batchItemFailures: event.Records.flatMap((record, recordIndex) => {
2162
- if (!failures.has(recordIndex)) return [];
2163
- const cause = failures.get(recordIndex);
2164
- return [{ itemIdentifier: failureIdentifier(record, cause) }];
2165
- })
2132
+ batchItemFailures: results.filter(
2133
+ (failure) => failure !== null
2134
+ )
2166
2135
  };
2167
2136
  }
2168
2137
  function clients(config) {