@pikku/inspector 0.10.1 → 0.10.2

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/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { inspect } from './inspector.js'
1
+ export { inspect, getInitialInspectorState } from './inspector.js'
2
2
  export { getFilesAndMethods } from './utils/get-files-and-methods.js'
3
3
  export type { TypesMap } from './types-map.js'
4
4
  export type * from './types.js'
package/src/inspector.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import * as ts from 'typescript'
2
+ import { performance } from 'perf_hooks'
2
3
  import { visitSetup, visitRoutes } from './visit.js'
3
4
  import { TypesMap } from './types-map.js'
4
5
  import { InspectorState, InspectorLogger, InspectorOptions } from './types.js'
@@ -6,22 +7,13 @@ import { getFilesAndMethods } from './utils/get-files-and-methods.js'
6
7
  import { findCommonAncestor } from './utils/find-root-dir.js'
7
8
  import { aggregateRequiredServices } from './utils/post-process.js'
8
9
 
9
- export const inspect = (
10
- logger: InspectorLogger,
11
- routeFiles: string[],
12
- options: InspectorOptions = {}
13
- ): InspectorState => {
14
- const program = ts.createProgram(routeFiles, {
15
- target: ts.ScriptTarget.ESNext,
16
- module: ts.ModuleKind.CommonJS,
17
- })
18
- const checker = program.getTypeChecker()
19
- const sourceFiles = program.getSourceFiles()
20
-
21
- // Infer root directory from source files
22
- const rootDir = findCommonAncestor(routeFiles)
23
-
24
- const state: InspectorState = {
10
+ /**
11
+ * Creates an initial/empty inspector state with all required properties initialized
12
+ * @param rootDir - The root directory for the project
13
+ * @returns A fresh InspectorState with empty collections
14
+ */
15
+ export function getInitialInspectorState(rootDir: string): InspectorState {
16
+ return {
25
17
  rootDir,
26
18
  singletonServicesTypeImportMap: new Map(),
27
19
  sessionServicesTypeImportMap: new Map(),
@@ -99,30 +91,89 @@ export const inspect = (
99
91
  usedFunctions: new Set(),
100
92
  usedMiddleware: new Set(),
101
93
  usedPermissions: new Set(),
94
+ allSingletonServices: [],
95
+ allSessionServices: [],
102
96
  },
103
97
  }
98
+ }
99
+
100
+ export const inspect = (
101
+ logger: InspectorLogger,
102
+ routeFiles: string[],
103
+ options: InspectorOptions = {}
104
+ ): InspectorState => {
105
+ const startProgram = performance.now()
106
+ const program = ts.createProgram(routeFiles, {
107
+ target: ts.ScriptTarget.ESNext,
108
+ module: ts.ModuleKind.CommonJS,
109
+ skipLibCheck: true,
110
+ skipDefaultLibCheck: true,
111
+ moduleResolution: ts.ModuleResolutionKind.Node10,
112
+ types: [],
113
+ allowJs: false,
114
+ checkJs: false,
115
+ })
116
+ logger.debug(
117
+ `Created program in ${(performance.now() - startProgram).toFixed(2)}ms`
118
+ )
119
+
120
+ const startChecker = performance.now()
121
+ const checker = program.getTypeChecker()
122
+ logger.debug(
123
+ `Got type checker in ${(performance.now() - startChecker).toFixed(2)}ms`
124
+ )
125
+
126
+ const startSourceFiles = performance.now()
127
+ const sourceFiles = program.getSourceFiles()
128
+ logger.debug(
129
+ `Got source files in ${(performance.now() - startSourceFiles).toFixed(2)}ms`
130
+ )
131
+
132
+ // Infer root directory from source files
133
+ const rootDir = findCommonAncestor(routeFiles)
134
+
135
+ const state = getInitialInspectorState(rootDir)
104
136
 
105
137
  // First sweep: add all functions
138
+ const startSetup = performance.now()
106
139
  for (const sourceFile of sourceFiles) {
107
140
  ts.forEachChild(sourceFile, (child) =>
108
141
  visitSetup(logger, checker, child, state, options)
109
142
  )
110
143
  }
144
+ logger.debug(
145
+ `Visit setup phase completed in ${(performance.now() - startSetup).toFixed(2)}ms`
146
+ )
111
147
 
112
- // Second sweep: add all transports
113
- for (const sourceFile of sourceFiles) {
114
- ts.forEachChild(sourceFile, (child) =>
115
- visitRoutes(logger, checker, child, state, options)
148
+ if (!options.setupOnly) {
149
+ // Second sweep: add all transports
150
+ const startRoutes = performance.now()
151
+ for (const sourceFile of sourceFiles) {
152
+ ts.forEachChild(sourceFile, (child) =>
153
+ visitRoutes(logger, checker, child, state, options)
154
+ )
155
+ }
156
+ logger.debug(
157
+ `Visit routes phase completed in ${(performance.now() - startRoutes).toFixed(2)}ms`
116
158
  )
117
159
  }
118
160
 
119
161
  // Populate filesAndMethods
162
+ const startFilesAndMethods = performance.now()
120
163
  const { result, errors } = getFilesAndMethods(state, options.types)
121
164
  state.filesAndMethods = result
122
165
  state.filesAndMethodsErrors = errors
166
+ logger.debug(
167
+ `Get files and methods completed in ${(performance.now() - startFilesAndMethods).toFixed(2)}ms`
168
+ )
123
169
 
124
- // Post-processing: Aggregate required services from wired functions/middleware/permissions
125
- aggregateRequiredServices(state)
170
+ if (!options.setupOnly) {
171
+ const startAggregate = performance.now()
172
+ aggregateRequiredServices(state)
173
+ logger.debug(
174
+ `Aggregate required services completed in ${(performance.now() - startAggregate).toFixed(2)}ms`
175
+ )
176
+ }
126
177
 
127
178
  return state
128
179
  }
package/src/types.ts CHANGED
@@ -114,6 +114,7 @@ export type InspectorFilters = {
114
114
  }
115
115
 
116
116
  export type InspectorOptions = Partial<{
117
+ setupOnly: boolean
117
118
  types: Partial<{
118
119
  configFileType: string
119
120
  userSessionType: string
@@ -231,5 +232,7 @@ export interface InspectorState {
231
232
  usedFunctions: Set<string> // Function names actually wired/exposed
232
233
  usedMiddleware: Set<string> // Middleware names used by wired functions
233
234
  usedPermissions: Set<string> // Permission names used by wired functions
235
+ allSingletonServices: string[] // All services available in SingletonServices type
236
+ allSessionServices: string[] // All services available in Services type (excluding SingletonServices)
234
237
  }
235
238
  }
@@ -4,6 +4,7 @@ import {
4
4
  MiddlewareMetadata,
5
5
  PermissionMetadata,
6
6
  } from '@pikku/core'
7
+ import { extractTypeKeys } from './type-utils.js'
7
8
 
8
9
  /**
9
10
  * Helper to extract wire-level middleware/permission names from metadata.
@@ -28,7 +29,7 @@ export function extractWireNames(
28
29
  */
29
30
  function expandAndAddGroupServices(
30
31
  list: MiddlewareMetadata[] | PermissionMetadata[] | undefined,
31
- state: InspectorState,
32
+ state: InspectorState | Omit<InspectorState, 'typesLookup'>,
32
33
  addServices: (services: FunctionServicesMeta | undefined) => void,
33
34
  isMiddleware: boolean
34
35
  ): void {
@@ -57,6 +58,38 @@ function expandAndAddGroupServices(
57
58
  }
58
59
  }
59
60
 
61
+ /**
62
+ * Extracts all service names from SingletonServices and Services types.
63
+ * This provides the complete list of available services for code generation.
64
+ * Only runs if typesLookup is available (omitted in deserialized states).
65
+ */
66
+ function extractAllServices(
67
+ state: InspectorState | Omit<InspectorState, 'typesLookup'>
68
+ ): void {
69
+ // Skip if typesLookup is not available (e.g., deserialized state)
70
+ if (!('typesLookup' in state)) {
71
+ return
72
+ }
73
+
74
+ // Extract all singleton services from the SingletonServices type
75
+ const singletonServicesTypes = state.typesLookup.get('SingletonServices')
76
+ if (singletonServicesTypes && singletonServicesTypes.length > 0) {
77
+ const singletonServiceNames = extractTypeKeys(singletonServicesTypes[0])
78
+ state.serviceAggregation.allSingletonServices = singletonServiceNames.sort()
79
+ }
80
+
81
+ // Extract all services from the Services type
82
+ const servicesTypes = state.typesLookup.get('Services')
83
+ if (servicesTypes && servicesTypes.length > 0) {
84
+ const allServiceNames = extractTypeKeys(servicesTypes[0])
85
+ // Session services are those in Services but not in SingletonServices
86
+ const singletonSet = new Set(state.serviceAggregation.allSingletonServices)
87
+ state.serviceAggregation.allSessionServices = allServiceNames
88
+ .filter((name) => !singletonSet.has(name))
89
+ .sort()
90
+ }
91
+ }
92
+
60
93
  /**
61
94
  * Aggregates all required services from wired functions, middleware, and permissions.
62
95
  * Must be called after AST traversal completes.
@@ -64,7 +97,12 @@ function expandAndAddGroupServices(
64
97
  * Note: usedFunctions, usedMiddleware, and usedPermissions are tracked directly
65
98
  * in the add-* methods during AST traversal for efficiency.
66
99
  */
67
- export function aggregateRequiredServices(state: InspectorState): void {
100
+ export function aggregateRequiredServices(
101
+ state: InspectorState | Omit<InspectorState, 'typesLookup'>
102
+ ): void {
103
+ // First, extract all available services from types
104
+ extractAllServices(state)
105
+
68
106
  const { requiredServices, usedFunctions, usedMiddleware, usedPermissions } =
69
107
  state.serviceAggregation
70
108
 
@@ -162,6 +162,8 @@ export interface SerializableInspectorState {
162
162
  usedFunctions: string[]
163
163
  usedMiddleware: string[]
164
164
  usedPermissions: string[]
165
+ allSingletonServices: string[]
166
+ allSessionServices: string[]
165
167
  }
166
168
  }
167
169
 
@@ -271,6 +273,8 @@ export function serializeInspectorState(
271
273
  usedFunctions: Array.from(state.serviceAggregation.usedFunctions),
272
274
  usedMiddleware: Array.from(state.serviceAggregation.usedMiddleware),
273
275
  usedPermissions: Array.from(state.serviceAggregation.usedPermissions),
276
+ allSingletonServices: state.serviceAggregation.allSingletonServices,
277
+ allSessionServices: state.serviceAggregation.allSessionServices,
274
278
  },
275
279
  }
276
280
  }
@@ -370,6 +374,8 @@ export function deserializeInspectorState(
370
374
  usedFunctions: new Set(data.serviceAggregation.usedFunctions),
371
375
  usedMiddleware: new Set(data.serviceAggregation.usedMiddleware),
372
376
  usedPermissions: new Set(data.serviceAggregation.usedPermissions),
377
+ allSingletonServices: data.serviceAggregation.allSingletonServices,
378
+ allSessionServices: data.serviceAggregation.allSessionServices,
373
379
  },
374
380
  }
375
381
  }
@@ -37,20 +37,22 @@ export function getPropertyAssignmentInitializer(
37
37
  ) {
38
38
  if (!checker) return prop.name // best effort without a checker
39
39
 
40
- let sym = checker.getSymbolAtLocation(prop.name)
40
+ // Use the proper TypeScript API for shorthand property resolution
41
+ let sym = checker.getShorthandAssignmentValueSymbol(prop)
41
42
  if (sym && sym.flags & ts.SymbolFlags.Alias) {
42
43
  sym = checker.getAliasedSymbol(sym)
43
44
  }
44
45
 
45
46
  const decl = sym?.declarations?.[0]
46
47
 
47
- // const foo = () => {}
48
+ // const foo = () => {} or const foo = pikkuFunc(...)
48
49
  if (
49
50
  decl &&
50
51
  ts.isVariableDeclaration(decl) &&
51
52
  decl.initializer &&
52
53
  (ts.isArrowFunction(decl.initializer) ||
53
- ts.isFunctionExpression(decl.initializer))
54
+ ts.isFunctionExpression(decl.initializer) ||
55
+ ts.isCallExpression(decl.initializer))
54
56
  ) {
55
57
  return decl.initializer
56
58
  }
package/src/visit.ts CHANGED
@@ -70,8 +70,8 @@ export const visitSetup = (
70
70
  )
71
71
 
72
72
  addFileWithFactory(node, checker, state.configFactories, 'CreateConfig')
73
- addRPCInvocations(node, state, logger)
74
73
 
74
+ addRPCInvocations(node, state, logger)
75
75
  addMiddleware(logger, node, checker, state, options)
76
76
  addPermission(logger, node, checker, state, options)
77
77