@suss/packs 0.29.0 → 0.31.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.
@@ -29,6 +29,43 @@ var RETURNS_A_RECORD = [
29
29
  "create!",
30
30
  "reload"
31
31
  ];
32
+ var RUNS_A_QUERY = [
33
+ "find",
34
+ "find_by",
35
+ "find_by!",
36
+ "find_sole_by",
37
+ "sole",
38
+ "first",
39
+ "first!",
40
+ "last",
41
+ "last!",
42
+ "take",
43
+ "take!",
44
+ "exists?",
45
+ "count",
46
+ "size",
47
+ "any?",
48
+ "none?",
49
+ "many?",
50
+ "empty?",
51
+ "pluck",
52
+ "pick",
53
+ "ids",
54
+ "sum",
55
+ "average",
56
+ "minimum",
57
+ "maximum",
58
+ "calculate",
59
+ "to_a",
60
+ "each",
61
+ "map",
62
+ "find_each",
63
+ "find_in_batches",
64
+ "in_batches",
65
+ "load",
66
+ "reload",
67
+ "find_or_initialize_by"
68
+ ];
32
69
  var RETURNS_A_RELATION = [
33
70
  "where",
34
71
  "rewhere",
@@ -60,6 +97,7 @@ var RETURNS_A_RELATION = [
60
97
  "only",
61
98
  "except"
62
99
  ];
100
+ var READS = [...RUNS_A_QUERY, ...RETURNS_A_RELATION, "not"];
63
101
  function activeRecordStorage(options) {
64
102
  return [
65
103
  {
@@ -81,9 +119,24 @@ function activeRecordStorage(options) {
81
119
  "destroy_all",
82
120
  "delete",
83
121
  "delete_all",
84
- "touch"
122
+ "touch",
123
+ // These two read first and store only when the read found
124
+ // nothing, and a write is the stronger of the two claims.
125
+ "find_or_create_by",
126
+ "find_or_create_by!"
85
127
  ],
128
+ reads: READS,
86
129
  givesBack: [...RETURNS_A_RECORD, ...RETURNS_A_RELATION],
130
+ byPrimaryKey: {
131
+ methods: ["find", "exists?", "update", "destroy", "delete"],
132
+ column: "id"
133
+ },
134
+ columnArguments: ["select", "pluck", "pick"],
135
+ associations: {
136
+ singular: ["has_one", "belongs_to"],
137
+ plural: ["has_many", "has_and_belongs_to_many"],
138
+ classNameKeyword: "class_name"
139
+ },
87
140
  storageSystem: options.storageSystem
88
141
  }
89
142
  ];
@@ -108,7 +161,7 @@ var declares = {
108
161
  { ecosystem: "rubygems", name: "activerecord" },
109
162
  { ecosystem: "rubygems", name: "rails" }
110
163
  ],
111
- reads: "ActiveRecord calls (Ruby): a call matches when the constant its receivers start at reaches `ActiveRecord::Base`, following what each class extends through the project.",
164
+ reads: "ActiveRecord calls (Ruby): a call matches when its method is one ActiveRecord defines as a read or a write and the class behind its receiver reaches `ActiveRecord::Base`, following what each class extends through the project.",
112
165
  configuration: {
113
166
  file: "suss.activerecord.json",
114
167
  example: { storageSystem: "postgresql" },
package/dist/aiohttp.js CHANGED
@@ -27,7 +27,10 @@ function aiohttpClient() {
27
27
  },
28
28
  receiverConstructors: ["ClientSession"]
29
29
  }
30
- ]
30
+ ],
31
+ // `ClientSession.__aenter__` returns the session, so `async with
32
+ // aiohttp.ClientSession() as s` puts the constructed session in s.
33
+ contextManagers: [{ module: "aiohttp", returnsSelf: ["ClientSession"] }]
31
34
  };
32
35
  }
33
36
  var declares = {
package/dist/aws-sqs.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // ../framework/aws-sqs/dist/index.js
2
2
  import { Node as N } from "ts-morph";
3
3
  import { z } from "zod";
4
- import { readConfiguredCall } from "@suss/adapter-typescript";
4
+ import { climbSyntax, readConfiguredCall } from "@suss/adapter-typescript";
5
5
  import { messageBusBinding } from "@suss/behavioral-ir";
6
6
  import { configuredCallOption } from "@suss/extractor";
7
7
  import { constructedFrom, messageSends, pack } from "@suss/recognize";
@@ -123,10 +123,7 @@ function isSqsRecordIdentifier(recordExpr) {
123
123
  return false;
124
124
  }
125
125
  function extractDestructuredFields(call) {
126
- let parent = call.getParent();
127
- while (parent !== void 0 && (N.isAsExpression(parent) || N.isParenthesizedExpression(parent))) {
128
- parent = parent.getParent();
129
- }
126
+ const parent = climbSyntax(call).getParent();
130
127
  if (parent === void 0 || !N.isVariableDeclaration(parent)) {
131
128
  return null;
132
129
  }
@@ -96,7 +96,8 @@ function sqlalchemyModels() {
96
96
  baseNames: MODEL_BASE_NAMES,
97
97
  givesBack: [...NARROWS_A_QUERY, ...RUNS_A_QUERY],
98
98
  entryMethods: SESSION_ENTRY_METHODS,
99
- entryFunctions: [{ module: "sqlalchemy", name: "select", argument: 0 }]
99
+ entryFunctions: [{ module: "sqlalchemy", name: "select", argument: 0 }],
100
+ relationships: [{ module: "sqlalchemy.orm", name: "relationship" }]
100
101
  }
101
102
  ];
102
103
  }
@@ -1,10 +1,21 @@
1
1
  // ../framework/cloudflare-workers/dist/index.js
2
2
  import { z } from "zod";
3
3
  import { Node as N } from "ts-morph";
4
+ import {
5
+ functionValueOf,
6
+ propertiesOf,
7
+ propertyFunctionOf,
8
+ propertyNameOf
9
+ } from "@suss/adapter-typescript";
4
10
  import { Node as N2 } from "ts-morph";
11
+ import {
12
+ isDefaultedAt,
13
+ propertyFunctionOf as propertyFunctionOf2,
14
+ propertyNameOf as propertyNameOf2
15
+ } from "@suss/adapter-typescript";
5
16
  import { runtimeConfigBinding } from "@suss/behavioral-ir";
6
17
  import { Node as N3 } from "ts-morph";
7
- import { readName as readName2 } from "@suss/adapter-typescript";
18
+ import { readName as readName2, stringValueOf } from "@suss/adapter-typescript";
8
19
  import { storageBinding } from "@suss/behavioral-ir";
9
20
  import { readSqlAccess } from "@suss/sql";
10
21
  var TRIGGERS = {
@@ -29,9 +40,13 @@ var TRIGGERS = {
29
40
  };
30
41
  var METADATA_NAMESPACE = "cloudflareWorkers";
31
42
  function cloudflareWorkersDiscovery(options = {}) {
32
- return (sourceFile) => {
43
+ return (sourceFile, ctx) => {
33
44
  const sf = sourceFile;
34
- const triggers = [...defaultExportTriggers(sf), ...listenerTriggers(sf)];
45
+ const { resolution } = ctx;
46
+ const triggers = [
47
+ ...defaultExportTriggers(sf, resolution),
48
+ ...listenerTriggers(sf, resolution)
49
+ ];
35
50
  const units = [];
36
51
  const taken = /* @__PURE__ */ new Set();
37
52
  for (const trigger of triggers) {
@@ -66,124 +81,58 @@ function unitFor(trigger, scriptName) {
66
81
  }
67
82
  };
68
83
  }
69
- function defaultExportTriggers(sf) {
70
- const literal = defaultExportObject(sf);
84
+ function defaultExportTriggers(sf, resolution) {
85
+ const literal = defaultExportObject(sf, resolution);
71
86
  if (literal === null) {
72
87
  return [];
73
88
  }
74
89
  const triggers = [];
75
- for (const property of literal.getProperties()) {
76
- const found = triggerOfProperty(property, sf);
77
- if (found !== null) {
78
- triggers.push(found);
90
+ for (const property of propertiesOf(literal, resolution)) {
91
+ const name = propertyNameOf(property);
92
+ if (name === null || TRIGGERS[name] === void 0) {
93
+ continue;
94
+ }
95
+ const func = propertyFunctionOf(property, resolution);
96
+ if (func !== null) {
97
+ triggers.push({ name, func, registration: "default-export" });
79
98
  }
80
99
  }
81
100
  return triggers;
82
101
  }
83
- function triggerOfProperty(property, sf) {
84
- const named = propertyName(property);
85
- if (named === null || TRIGGERS[named] === void 0) {
86
- return null;
87
- }
88
- const func = functionOfProperty(property, sf);
89
- return func === null ? null : { name: named, func, registration: "default-export" };
102
+ function entrypointTriggerFunctions(sf, resolution) {
103
+ let perRun = triggerFunctionsPerRun.get(resolution);
104
+ if (perRun === void 0) {
105
+ perRun = /* @__PURE__ */ new Map();
106
+ triggerFunctionsPerRun.set(resolution, perRun);
107
+ }
108
+ const filePath = sf.getFilePath();
109
+ let found = perRun.get(filePath);
110
+ if (found === void 0) {
111
+ found = new Set(
112
+ defaultExportTriggers(sf, resolution).map((trigger) => trigger.func)
113
+ );
114
+ perRun.set(filePath, found);
115
+ }
116
+ return found;
90
117
  }
91
- function propertyName(property) {
92
- if (N.isMethodDeclaration(property) || N.isPropertyAssignment(property)) {
93
- const name = property.getNameNode();
94
- if (N.isIdentifier(name)) {
95
- return name.getText();
118
+ var triggerFunctionsPerRun = /* @__PURE__ */ new WeakMap();
119
+ function defaultExportObject(sf, resolution) {
120
+ for (const exported of resolution.exportsOf(sf).get("default") ?? []) {
121
+ const object = resolution.resolveObject(exported);
122
+ if (object !== null && N.isObjectLiteralExpression(object)) {
123
+ return object;
96
124
  }
97
- return N.isStringLiteral(name) ? name.getLiteralValue() : null;
98
- }
99
- return N.isShorthandPropertyAssignment(property) ? property.getName() : null;
100
- }
101
- function functionOfProperty(property, sf) {
102
- if (N.isMethodDeclaration(property)) {
103
- return property;
104
- }
105
- if (N.isPropertyAssignment(property)) {
106
- const written = property.getInitializer();
107
- return written === void 0 ? null : functionBehind(written, sf);
108
- }
109
- if (N.isShorthandPropertyAssignment(property)) {
110
- return functionBehind(property.getNameNode(), sf);
111
125
  }
112
126
  return null;
113
127
  }
114
- function functionBehind(expression, sf) {
115
- if (N.isArrowFunction(expression) || N.isFunctionExpression(expression)) {
116
- return expression;
117
- }
118
- if (!N.isIdentifier(expression)) {
119
- return null;
120
- }
121
- for (const definition of expression.getDefinitionNodes()) {
122
- const declared = declaredFunction(definition);
123
- if (declared !== null) {
124
- return declared;
125
- }
126
- }
127
- return localFunction(sf, expression.getText());
128
- }
129
- function declaredFunction(definition) {
130
- if (N.isFunctionDeclaration(definition)) {
131
- return definition;
132
- }
133
- if (!N.isVariableDeclaration(definition)) {
134
- return null;
135
- }
136
- const written = definition.getInitializer();
137
- if (written === void 0) {
138
- return null;
139
- }
140
- return N.isArrowFunction(written) || N.isFunctionExpression(written) ? written : null;
141
- }
142
- function localFunction(sf, name) {
143
- const declaration = sf.getFunction(name);
144
- if (declaration !== void 0) {
145
- return declaration;
146
- }
147
- const variable = sf.getVariableDeclaration(name);
148
- return variable === void 0 ? null : declaredFunction(variable);
149
- }
150
- function defaultExportObject(sf) {
151
- const assignment = sf.getExportAssignment((a) => !a.isExportEquals());
152
- if (assignment === void 0) {
153
- return null;
154
- }
155
- return objectBehind(unwrap(assignment.getExpression()), sf);
156
- }
157
- function unwrap(expression) {
158
- let inner = expression;
159
- while (N.isSatisfiesExpression(inner) || N.isAsExpression(inner) || N.isParenthesizedExpression(inner)) {
160
- inner = inner.getExpression();
161
- }
162
- return inner;
163
- }
164
- function objectBehind(expression, sf) {
165
- if (N.isObjectLiteralExpression(expression)) {
166
- return expression;
167
- }
168
- if (!N.isIdentifier(expression)) {
169
- return null;
170
- }
171
- const declaration = sf.getVariableDeclaration(expression.getText());
172
- const written = declaration?.getInitializer();
173
- if (written === void 0) {
174
- return null;
175
- }
176
- const inner = unwrap(written);
177
- return N.isObjectLiteralExpression(inner) ? inner : null;
178
- }
179
- function listenerTriggers(sf) {
128
+ function listenerTriggers(sf, resolution) {
180
129
  const triggers = [];
181
130
  sf.forEachDescendant((node) => {
182
131
  const registered = listenerAt(node);
183
132
  if (registered === null) {
184
133
  return;
185
134
  }
186
- const func = functionBehind(registered.handler, sf);
135
+ const func = functionValueOf(registered.handler, resolution);
187
136
  if (func !== null) {
188
137
  triggers.push({
189
138
  name: registered.event,
@@ -215,118 +164,57 @@ var ENV_PARAMETER_POSITION = 1;
215
164
  var readName = (name) => `env.${name}`;
216
165
  function envBindingRecognizer(options = {}) {
217
166
  const instanceName = options.scriptName ?? "<unknown>";
218
- return (access) => {
219
- const read = envReadAt(access);
167
+ return (access, ctx) => {
168
+ const { resolution } = ctx;
169
+ const read = envReadAt(access, resolution);
220
170
  return read === null ? null : [configReadEffect(read, instanceName)];
221
171
  };
222
172
  }
223
- function envReadAt(node) {
173
+ function envReadAt(node, resolution) {
224
174
  if (!N2.isPropertyAccessExpression(node)) {
225
175
  return null;
226
176
  }
227
177
  const subject = node.getExpression();
228
- if (!N2.isIdentifier(subject) || !isTriggerEnvArgument(subject)) {
178
+ if (!N2.isIdentifier(subject) || !isTriggerEnvArgument(subject, resolution)) {
229
179
  return null;
230
180
  }
231
181
  const name = node.getName();
232
182
  return name.length === 0 ? null : { name, defaulted: isDefaultedAt(node) };
233
183
  }
234
- function isTriggerEnvArgument(subject) {
184
+ function isTriggerEnvArgument(subject, resolution) {
235
185
  for (const definition of subject.getSymbol()?.getDeclarations() ?? []) {
236
- if (N2.isParameterDeclaration(definition) && isTriggerEnvParameter(definition)) {
186
+ if (N2.isParameterDeclaration(definition) && isTriggerEnvParameter(definition, resolution)) {
237
187
  return true;
238
188
  }
239
189
  }
240
190
  return false;
241
191
  }
242
- function isTriggerEnvParameter(parameter) {
192
+ function isTriggerEnvParameter(parameter, resolution) {
243
193
  const owner = parameter.getParent();
244
194
  const parameters = owner.getParameters?.() ?? [];
245
195
  if (parameters[ENV_PARAMETER_POSITION] !== parameter) {
246
196
  return false;
247
197
  }
248
- return isTriggerBody(owner);
249
- }
250
- function isTriggerBody(owner) {
251
- const named = triggerNameOf(owner);
252
- if (named !== null) {
253
- return TRIGGERS[named] !== void 0;
254
- }
255
- return referredToByEntrypoint(owner);
198
+ return isTriggerBody(owner, resolution);
256
199
  }
257
- function triggerNameOf(owner) {
258
- if (N2.isMethodDeclaration(owner)) {
259
- const name = owner.getNameNode();
260
- return N2.isIdentifier(name) ? name.getText() : null;
200
+ function isTriggerBody(owner, resolution) {
201
+ if (resolution !== void 0) {
202
+ return entrypointTriggerFunctions(owner.getSourceFile(), resolution).has(
203
+ owner
204
+ );
261
205
  }
262
- const parent = owner.getParent();
263
- if (parent !== void 0 && N2.isPropertyAssignment(parent)) {
264
- const name = parent.getNameNode();
265
- return N2.isIdentifier(name) ? name.getText() : null;
266
- }
267
- return null;
206
+ return writtenUnderTriggerName(owner);
268
207
  }
269
- function referredToByEntrypoint(owner) {
270
- const name = declaredName(owner);
271
- if (name === null) {
208
+ function writtenUnderTriggerName(owner) {
209
+ const property = N2.isMethodDeclaration(owner) ? owner : owner.getParent();
210
+ if (property === void 0) {
272
211
  return false;
273
212
  }
274
- const assignment = owner.getSourceFile().getExportAssignment((a) => !a.isExportEquals());
275
- if (assignment === void 0) {
213
+ const name = propertyNameOf2(property);
214
+ if (name === null || TRIGGERS[name] === void 0) {
276
215
  return false;
277
216
  }
278
- return triggerReferences(assignment.getExpression()).has(name);
279
- }
280
- function declaredName(owner) {
281
- if (N2.isFunctionDeclaration(owner)) {
282
- return owner.getName() ?? null;
283
- }
284
- const parent = owner.getParent();
285
- return parent !== void 0 && N2.isVariableDeclaration(parent) ? parent.getName() : null;
286
- }
287
- function triggerReferences(expression) {
288
- const names = /* @__PURE__ */ new Set();
289
- if (!N2.isObjectLiteralExpression(expression)) {
290
- return names;
291
- }
292
- for (const property of expression.getProperties()) {
293
- if (!N2.isPropertyAssignment(property)) {
294
- continue;
295
- }
296
- const key = property.getNameNode();
297
- if (!N2.isIdentifier(key) || TRIGGERS[key.getText()] === void 0) {
298
- continue;
299
- }
300
- const written = property.getInitializer();
301
- if (written !== void 0 && N2.isIdentifier(written)) {
302
- names.add(written.getText());
303
- }
304
- }
305
- return names;
306
- }
307
- function isDefaultedAt(node) {
308
- let child = node;
309
- let parent = child.getParent();
310
- while (parent !== void 0) {
311
- if (N2.isParenthesizedExpression(parent)) {
312
- child = parent;
313
- parent = parent.getParent();
314
- continue;
315
- }
316
- if (!N2.isBinaryExpression(parent)) {
317
- return false;
318
- }
319
- const operator = parent.getOperatorToken().getText();
320
- if (operator !== "??" && operator !== "||") {
321
- return false;
322
- }
323
- if (parent.getLeft() === child) {
324
- return true;
325
- }
326
- child = parent;
327
- parent = parent.getParent();
328
- }
329
- return false;
217
+ return propertyFunctionOf2(property, void 0) === owner;
330
218
  }
331
219
  function configReadEffect(read, instanceName) {
332
220
  return {
@@ -360,21 +248,25 @@ var KEYED_OPERATIONS = /* @__PURE__ */ new Set([
360
248
  "put",
361
249
  "delete"
362
250
  ]);
363
- var objectStoreAccess = (method, call, resolve) => {
251
+ var objectStoreAccess = (method, call, reading) => {
364
252
  const kind = OBJECT_OPERATIONS[method];
365
253
  if (kind === void 0) {
366
254
  return null;
367
255
  }
368
256
  const argument = call.getArguments()[0];
369
- const selector = argument !== void 0 && KEYED_OPERATIONS.has(method) ? readName2(argument, { resolve, unsettled: "reference" }) : null;
257
+ const selector = argument !== void 0 && KEYED_OPERATIONS.has(method) ? readName2(argument, {
258
+ resolve: reading.resolve,
259
+ unsettled: "reference"
260
+ }) : null;
370
261
  return { kind, ...selector !== null ? { selector } : {} };
371
262
  };
372
263
  var D1_STATEMENT_METHODS = /* @__PURE__ */ new Set(["prepare", "exec"]);
373
- var d1Access = (method, call, resolve) => {
264
+ var d1Access = (method, call, reading) => {
374
265
  if (!D1_STATEMENT_METHODS.has(method)) {
375
266
  return null;
376
267
  }
377
- const sql = literalText(call.getArguments()[0], resolve);
268
+ const statement = call.getArguments()[0];
269
+ const sql = statement === void 0 ? null : stringValueOf(statement, reading.resolution);
378
270
  if (sql === null) {
379
271
  return null;
380
272
  }
@@ -392,12 +284,16 @@ var STORES = {
392
284
  };
393
285
  function storeBindingRecognizer(call, ctx) {
394
286
  const callNode = call;
395
- const resolve = ctx.resolveWrittenValue ?? (() => null);
287
+ const given = ctx;
288
+ const reading = {
289
+ resolve: given.resolveWrittenValue ?? (() => null),
290
+ resolution: given.resolution
291
+ };
396
292
  const callee = callNode.getExpression();
397
293
  if (!N3.isPropertyAccessExpression(callee)) {
398
294
  return null;
399
295
  }
400
- const binding = boundReceiver(callee.getExpression(), resolve);
296
+ const binding = boundReceiver(callee.getExpression(), reading);
401
297
  if (binding === null) {
402
298
  return null;
403
299
  }
@@ -406,7 +302,7 @@ function storeBindingRecognizer(call, ctx) {
406
302
  return null;
407
303
  }
408
304
  const method = callee.getName();
409
- const access = store.read(method, callNode, resolve);
305
+ const access = store.read(method, callNode, reading);
410
306
  if (access === null) {
411
307
  return null;
412
308
  }
@@ -433,10 +329,10 @@ function storeBindingRecognizer(call, ctx) {
433
329
  }
434
330
  ];
435
331
  }
436
- function boundReceiver(subject, resolve) {
332
+ function boundReceiver(subject, reading) {
437
333
  let receiver = subject;
438
334
  if (N3.isIdentifier(receiver)) {
439
- const written = declaredInitializer(receiver) ?? resolve(receiver);
335
+ const written = declaredInitializer(receiver) ?? reading.resolve(receiver);
440
336
  if (written !== null && written !== receiver) {
441
337
  receiver = written;
442
338
  }
@@ -445,7 +341,7 @@ function boundReceiver(subject, resolve) {
445
341
  return null;
446
342
  }
447
343
  const env = receiver.getExpression();
448
- if (!N3.isIdentifier(env) || !isTriggerEnvArgument(env)) {
344
+ if (!N3.isIdentifier(env) || !isTriggerEnvArgument(env, reading.resolution)) {
449
345
  return null;
450
346
  }
451
347
  const typeName = declaredTypeName(receiver);
@@ -475,16 +371,6 @@ function declaredTypeName(receiver) {
475
371
  }
476
372
  return null;
477
373
  }
478
- function literalText(argument, resolve) {
479
- if (argument === void 0) {
480
- return null;
481
- }
482
- const written = resolve(argument) ?? argument;
483
- if (N3.isStringLiteral(written) || N3.isNoSubstitutionTemplateLiteral(written)) {
484
- return written.getLiteralValue();
485
- }
486
- return null;
487
- }
488
374
  var optionsSchema = z.object({
489
375
  /**
490
376
  * The name the deployment gives this Worker, which is `name` in