@contember/engine-actions 2.2.0-alpha.15 → 2.2.0-alpha.17
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/CLAUDE.md +18 -2
- package/dist/development/index.cjs +1 -1
- package/dist/development/index.cjs.map +1 -1
- package/dist/development/index.js +1 -1
- package/dist/development/index.js.map +1 -1
- package/dist/development/src/dispatch/EventDispatcher.cjs +30 -2
- package/dist/development/src/dispatch/EventDispatcher.cjs.map +1 -1
- package/dist/development/src/dispatch/EventDispatcher.js +30 -2
- package/dist/development/src/dispatch/EventDispatcher.js.map +1 -1
- package/dist/development/src/dispatch/EventsRepository.cjs +38 -2
- package/dist/development/src/dispatch/EventsRepository.cjs.map +1 -1
- package/dist/development/src/dispatch/EventsRepository.js +38 -2
- package/dist/development/src/dispatch/EventsRepository.js.map +1 -1
- package/dist/development/src/dispatch/WebhookTargetHandler.cjs +45 -22
- package/dist/development/src/dispatch/WebhookTargetHandler.cjs.map +1 -1
- package/dist/development/src/dispatch/WebhookTargetHandler.js +45 -22
- package/dist/development/src/dispatch/WebhookTargetHandler.js.map +1 -1
- package/dist/development/src/dispatch/redactRequestError.cjs +24 -0
- package/dist/development/src/dispatch/redactRequestError.cjs.map +1 -0
- package/dist/development/src/dispatch/redactRequestError.js +24 -0
- package/dist/development/src/dispatch/redactRequestError.js.map +1 -0
- package/dist/development/src/graphql/resolvers/mutation/SetVariablesMutationResolver.cjs +6 -1
- package/dist/development/src/graphql/resolvers/mutation/SetVariablesMutationResolver.cjs.map +1 -1
- package/dist/development/src/graphql/resolvers/mutation/SetVariablesMutationResolver.js +6 -1
- package/dist/development/src/graphql/resolvers/mutation/SetVariablesMutationResolver.js.map +1 -1
- package/dist/development/src/model/VariablesManager.cjs +5 -0
- package/dist/development/src/model/VariablesManager.cjs.map +1 -1
- package/dist/development/src/model/VariablesManager.js +6 -1
- package/dist/development/src/model/VariablesManager.js.map +1 -1
- package/dist/production/index.cjs +1 -1
- package/dist/production/index.cjs.map +1 -1
- package/dist/production/index.js +1 -1
- package/dist/production/index.js.map +1 -1
- package/dist/production/src/dispatch/EventDispatcher.cjs +30 -2
- package/dist/production/src/dispatch/EventDispatcher.cjs.map +1 -1
- package/dist/production/src/dispatch/EventDispatcher.js +30 -2
- package/dist/production/src/dispatch/EventDispatcher.js.map +1 -1
- package/dist/production/src/dispatch/EventsRepository.cjs +38 -2
- package/dist/production/src/dispatch/EventsRepository.cjs.map +1 -1
- package/dist/production/src/dispatch/EventsRepository.js +38 -2
- package/dist/production/src/dispatch/EventsRepository.js.map +1 -1
- package/dist/production/src/dispatch/WebhookTargetHandler.cjs +45 -22
- package/dist/production/src/dispatch/WebhookTargetHandler.cjs.map +1 -1
- package/dist/production/src/dispatch/WebhookTargetHandler.js +45 -22
- package/dist/production/src/dispatch/WebhookTargetHandler.js.map +1 -1
- package/dist/production/src/dispatch/redactRequestError.cjs +24 -0
- package/dist/production/src/dispatch/redactRequestError.cjs.map +1 -0
- package/dist/production/src/dispatch/redactRequestError.js +24 -0
- package/dist/production/src/dispatch/redactRequestError.js.map +1 -0
- package/dist/production/src/graphql/resolvers/mutation/SetVariablesMutationResolver.cjs +6 -1
- package/dist/production/src/graphql/resolvers/mutation/SetVariablesMutationResolver.cjs.map +1 -1
- package/dist/production/src/graphql/resolvers/mutation/SetVariablesMutationResolver.js +6 -1
- package/dist/production/src/graphql/resolvers/mutation/SetVariablesMutationResolver.js.map +1 -1
- package/dist/production/src/model/VariablesManager.cjs +5 -0
- package/dist/production/src/model/VariablesManager.cjs.map +1 -1
- package/dist/production/src/model/VariablesManager.js +6 -1
- package/dist/production/src/model/VariablesManager.js.map +1 -1
- package/dist/tests/cases/unit/redactRequestError.test.d.ts +2 -0
- package/dist/tests/cases/unit/redactRequestError.test.d.ts.map +1 -0
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/dist/types/dispatch/EventDispatcher.d.ts.map +1 -1
- package/dist/types/dispatch/EventsRepository.d.ts +20 -1
- package/dist/types/dispatch/EventsRepository.d.ts.map +1 -1
- package/dist/types/dispatch/WebhookTargetHandler.d.ts +6 -0
- package/dist/types/dispatch/WebhookTargetHandler.d.ts.map +1 -1
- package/dist/types/dispatch/redactRequestError.d.ts +7 -0
- package/dist/types/dispatch/redactRequestError.d.ts.map +1 -0
- package/dist/types/graphql/resolvers/mutation/SetVariablesMutationResolver.d.ts +3 -1
- package/dist/types/graphql/resolvers/mutation/SetVariablesMutationResolver.d.ts.map +1 -1
- package/dist/types/model/VariablesManager.d.ts +2 -0
- package/dist/types/model/VariablesManager.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +17 -17
- package/src/dispatch/EventDispatcher.ts +32 -3
- package/src/dispatch/EventsRepository.ts +77 -2
- package/src/dispatch/WebhookTargetHandler.ts +58 -27
- package/src/dispatch/redactRequestError.ts +32 -0
- package/src/graphql/resolvers/mutation/SetVariablesMutationResolver.ts +7 -0
- package/src/index.ts +1 -1
- package/src/model/VariablesManager.ts +6 -0
- package/tests/cases/unit/dispatchTracing.test.ts +79 -6
- package/tests/cases/unit/redactRequestError.test.ts +45 -0
- package/tests/cases/unit/webhookHandler.test.ts +104 -1
package/package.json
CHANGED
|
@@ -22,27 +22,27 @@
|
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@contember/authorization": "2.2.0-alpha.
|
|
26
|
-
"@contember/database": "2.2.0-alpha.
|
|
27
|
-
"@contember/database-migrations": "2.2.0-alpha.
|
|
28
|
-
"@contember/engine-common": "2.2.0-alpha.
|
|
29
|
-
"@contember/engine-content-api": "2.2.0-alpha.
|
|
30
|
-
"@contember/engine-http": "2.2.0-alpha.
|
|
31
|
-
"@contember/engine-plugins": "2.2.0-alpha.
|
|
32
|
-
"@contember/engine-system-api": "2.2.0-alpha.
|
|
33
|
-
"@contember/engine-tenant-api": "2.2.0-alpha.
|
|
34
|
-
"@contember/graphql-utils": "2.2.0-alpha.
|
|
35
|
-
"@contember/logger": "2.2.0-alpha.
|
|
36
|
-
"@contember/schema": "2.2.0-alpha.
|
|
37
|
-
"@contember/schema-utils": "2.2.0-alpha.
|
|
38
|
-
"@contember/telemetry": "2.2.0-alpha.
|
|
39
|
-
"@contember/typesafe": "2.2.0-alpha.
|
|
25
|
+
"@contember/authorization": "2.2.0-alpha.17",
|
|
26
|
+
"@contember/database": "2.2.0-alpha.17",
|
|
27
|
+
"@contember/database-migrations": "2.2.0-alpha.17",
|
|
28
|
+
"@contember/engine-common": "2.2.0-alpha.17",
|
|
29
|
+
"@contember/engine-content-api": "2.2.0-alpha.17",
|
|
30
|
+
"@contember/engine-http": "2.2.0-alpha.17",
|
|
31
|
+
"@contember/engine-plugins": "2.2.0-alpha.17",
|
|
32
|
+
"@contember/engine-system-api": "2.2.0-alpha.17",
|
|
33
|
+
"@contember/engine-tenant-api": "2.2.0-alpha.17",
|
|
34
|
+
"@contember/graphql-utils": "2.2.0-alpha.17",
|
|
35
|
+
"@contember/logger": "2.2.0-alpha.17",
|
|
36
|
+
"@contember/schema": "2.2.0-alpha.17",
|
|
37
|
+
"@contember/schema-utils": "2.2.0-alpha.17",
|
|
38
|
+
"@contember/telemetry": "2.2.0-alpha.17",
|
|
39
|
+
"@contember/typesafe": "2.2.0-alpha.17",
|
|
40
40
|
"@graphql-tools/schema": "^10.0.31",
|
|
41
41
|
"graphql-tag": "^2.12.6",
|
|
42
42
|
"prom-client": "^12.0.0"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"@contember/database-tester": "2.2.0-alpha.
|
|
45
|
+
"@contember/database-tester": "2.2.0-alpha.17",
|
|
46
46
|
"@types/bun": "^1.3.10",
|
|
47
47
|
"@types/ws": "^8.18.1",
|
|
48
48
|
"graphql": "^16.13.1"
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"pg": "^8.20.0"
|
|
53
53
|
},
|
|
54
54
|
"type": "module",
|
|
55
|
-
"version": "2.2.0-alpha.
|
|
55
|
+
"version": "2.2.0-alpha.17",
|
|
56
56
|
"repository": {
|
|
57
57
|
"type": "git",
|
|
58
58
|
"url": "git+https://github.com/contember/contember.git",
|
|
@@ -4,8 +4,9 @@ import { HandledEvent } from './types.js'
|
|
|
4
4
|
import { Logger } from '@contember/logger'
|
|
5
5
|
import { ContentSchemaResolver } from '@contember/engine-http'
|
|
6
6
|
import { DatabaseContext } from '@contember/engine-system-api'
|
|
7
|
-
import { VariablesManager } from '../model/VariablesManager.js'
|
|
7
|
+
import { resolveVariable, VariablesManager } from '../model/VariablesManager.js'
|
|
8
8
|
import { Tracer } from '@contember/telemetry'
|
|
9
|
+
import { UndefinedVariableError } from './WebhookTargetHandler.js'
|
|
9
10
|
|
|
10
11
|
type ProcessBatchArgs = {
|
|
11
12
|
db: DatabaseContext
|
|
@@ -38,7 +39,7 @@ export class EventDispatcher {
|
|
|
38
39
|
async processBatch({ db, contentSchemaResolver, logger, project }: ProcessBatchArgs): Promise<ProcessBatchResult> {
|
|
39
40
|
const schema = (await contentSchemaResolver.getSchema({ db, normalize: true })).schema
|
|
40
41
|
const batchId = Math.random().toString().substring(2)
|
|
41
|
-
const batchLogger = logger.child({ loc: 'Actions.EventDispatcher', batchId })
|
|
42
|
+
const batchLogger = logger.child({ loc: 'Actions.EventDispatcher', project: project.slug, batchId })
|
|
42
43
|
const batch = await this.eventsRepository.fetchBatch(schema.actions, db.client, batchLogger)
|
|
43
44
|
if (!batch.ok) {
|
|
44
45
|
batchLogger.debug('Nothing to process', {
|
|
@@ -76,9 +77,37 @@ export class EventDispatcher {
|
|
|
76
77
|
backoffMs: 0,
|
|
77
78
|
}
|
|
78
79
|
} catch (e) {
|
|
80
|
+
if (e instanceof UndefinedVariableError) {
|
|
81
|
+
const { held, joined } = await this.eventsRepository.holdForUndefinedVariables(db.client, {
|
|
82
|
+
target: target.name,
|
|
83
|
+
batch: events.map(it => it.id),
|
|
84
|
+
variables: e.variables,
|
|
85
|
+
})
|
|
86
|
+
const message = 'Webhook target references an undefined variable, its events wait until it is set'
|
|
87
|
+
const attributes = { target: target.name, variables: e.variables, events: held }
|
|
88
|
+
// One warning per target and re-check; an event arriving meanwhile only joins the hold.
|
|
89
|
+
if (joined) {
|
|
90
|
+
batchLogger.debug(message, attributes)
|
|
91
|
+
} else {
|
|
92
|
+
batchLogger.warn(message, attributes)
|
|
93
|
+
}
|
|
94
|
+
span.setAttribute('contember.actions.held', held)
|
|
95
|
+
// setVariables may have committed after the variables were read and before the hold, when it had nothing to release yet.
|
|
96
|
+
const current = await this.variablesManager.fetchVariables(db, project.slug)
|
|
97
|
+
if (e.variables.every(name => resolveVariable(current, name) !== undefined)) {
|
|
98
|
+
await this.eventsRepository.resumeHeldForUndefinedVariables(db.client)
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
succeeded: 0,
|
|
102
|
+
retried: 0,
|
|
103
|
+
failedAfterAttempt: 0,
|
|
104
|
+
failedUnknownTarget: batch.unknownTargetFailed,
|
|
105
|
+
backoffMs: 0,
|
|
106
|
+
}
|
|
107
|
+
}
|
|
79
108
|
span.recordException(e)
|
|
80
109
|
span.setStatus('error')
|
|
81
|
-
|
|
110
|
+
batchLogger.error(e)
|
|
82
111
|
const failedEvents = events.map((it): HandledEvent => ({
|
|
83
112
|
row: it,
|
|
84
113
|
result: { ok: false, errorMessage: `Internal error` },
|
|
@@ -9,6 +9,10 @@ const ACK_TIMEOUT_MS = 1_000 * 60 * 10 // 10 minutes
|
|
|
9
9
|
const DEFAULT_REPEAT_INTERVAL_MS = 5_000 // 5 seconds
|
|
10
10
|
const DEFAULT_MAX_ATTEMPTS = 10
|
|
11
11
|
const DEFAULT_BATCH_SIZE = 1
|
|
12
|
+
/** How long a target with an undefined variable is held back before it is checked again without a variable change. */
|
|
13
|
+
const UNDEFINED_VARIABLE_RECHECK_MS = 1_000 * 60 * 10 // 10 minutes
|
|
14
|
+
/** The last log entry of an event held by {@link EventsRepository.holdForUndefinedVariables}. */
|
|
15
|
+
const heldMarkerCondition = `log -> -1 -> 'undefinedVariables' IS NOT NULL`
|
|
12
16
|
|
|
13
17
|
export class EventsRepository {
|
|
14
18
|
public async fetchBatch(actions: Actions.Schema, db: Client, logger: Logger): Promise<FetchBatchResult> {
|
|
@@ -106,7 +110,71 @@ export class EventsRepository {
|
|
|
106
110
|
})
|
|
107
111
|
}
|
|
108
112
|
|
|
109
|
-
|
|
113
|
+
/**
|
|
114
|
+
* Holds back the due events of a target that interpolates an undefined variable, the given batch included,
|
|
115
|
+
* without spending an attempt. Events arriving while the target is held join that hold, so a target is checked
|
|
116
|
+
* again once per interval and each check rewrites only what came due. {@link resumeHeldForUndefinedVariables}
|
|
117
|
+
* releases them all once variables change.
|
|
118
|
+
*/
|
|
119
|
+
public async holdForUndefinedVariables(db: Client, { target, batch, variables }: {
|
|
120
|
+
target: string
|
|
121
|
+
batch: string[]
|
|
122
|
+
variables: readonly string[]
|
|
123
|
+
}): Promise<HoldResult> {
|
|
124
|
+
const now = new Date()
|
|
125
|
+
const entry = JSON.stringify({ ok: false, errorMessage: `Undefined variable ${variables.join(', ')}`, undefinedVariables: variables })
|
|
126
|
+
return await db.transaction(async trx => {
|
|
127
|
+
const current = await SelectBuilder.create<{ visible_at: Date }>()
|
|
128
|
+
.from('actions_event')
|
|
129
|
+
.select('visible_at')
|
|
130
|
+
.where(it =>
|
|
131
|
+
it
|
|
132
|
+
.in('state', ['created', 'retrying'])
|
|
133
|
+
.compare('target', Operator.eq, target)
|
|
134
|
+
.compare('visible_at', Operator.gt, now)
|
|
135
|
+
.raw(heldMarkerCondition)
|
|
136
|
+
)
|
|
137
|
+
.orderBy('visible_at')
|
|
138
|
+
.limit(1)
|
|
139
|
+
.getResult(trx)
|
|
140
|
+
const until = current[0]?.visible_at ?? new Date(now.getTime() + UNDEFINED_VARIABLE_RECHECK_MS)
|
|
141
|
+
await this.requeue(trx, batch, until)
|
|
142
|
+
const held = await UpdateBuilder.create()
|
|
143
|
+
.table('actions_event')
|
|
144
|
+
.values<EventRow>({
|
|
145
|
+
visible_at: until,
|
|
146
|
+
// a re-check that finds the same variables missing does not grow the log
|
|
147
|
+
log: it => it.raw('CASE WHEN log -> -1 = ?::jsonb THEN log ELSE log || ?::jsonb END', entry, entry),
|
|
148
|
+
})
|
|
149
|
+
.where(it =>
|
|
150
|
+
it
|
|
151
|
+
.in('state', ['created', 'retrying'])
|
|
152
|
+
.compare('target', Operator.eq, target)
|
|
153
|
+
.or(it => it.in('id', batch).compare('visible_at', Operator.lte, now))
|
|
154
|
+
)
|
|
155
|
+
.execute(trx)
|
|
156
|
+
return { held, joined: current.length > 0 }
|
|
157
|
+
})
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Makes events held by {@link holdForUndefinedVariables} due now. Returns how many were released. */
|
|
161
|
+
public async resumeHeldForUndefinedVariables(db: Client): Promise<number> {
|
|
162
|
+
const now = new Date()
|
|
163
|
+
return await UpdateBuilder.create()
|
|
164
|
+
.table('actions_event')
|
|
165
|
+
.values<EventRow>({
|
|
166
|
+
visible_at: now,
|
|
167
|
+
})
|
|
168
|
+
.where(it =>
|
|
169
|
+
it
|
|
170
|
+
.in('state', ['created', 'retrying'])
|
|
171
|
+
.compare('visible_at', Operator.gt, now)
|
|
172
|
+
.raw(heldMarkerCondition)
|
|
173
|
+
)
|
|
174
|
+
.execute(db)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
public async requeue(db: Client, events: string[], visibleAt: Date = new Date()): Promise<void> {
|
|
110
178
|
await UpdateBuilder.create()
|
|
111
179
|
.table('actions_event')
|
|
112
180
|
.values<EventRow>({
|
|
@@ -123,7 +191,7 @@ export class EventsRepository {
|
|
|
123
191
|
'retrying',
|
|
124
192
|
),
|
|
125
193
|
last_state_change: new Date(),
|
|
126
|
-
visible_at:
|
|
194
|
+
visible_at: visibleAt,
|
|
127
195
|
})
|
|
128
196
|
.where(it => it.in('id', events))
|
|
129
197
|
.execute(db)
|
|
@@ -218,6 +286,13 @@ export type FetchBatchResult =
|
|
|
218
286
|
| { ok: true; events: EventRow[]; target: Actions.AnyTarget; unknownTargetFailed: number }
|
|
219
287
|
| { ok: false; backoffMs?: number; unknownTargetFailed: number }
|
|
220
288
|
|
|
289
|
+
export type HoldResult = {
|
|
290
|
+
/** Events held by this call. */
|
|
291
|
+
held: number
|
|
292
|
+
/** The target was already held, these events joined that hold. */
|
|
293
|
+
joined: boolean
|
|
294
|
+
}
|
|
295
|
+
|
|
221
296
|
export type PersistProcessedResult = {
|
|
222
297
|
/** Events delivered successfully (terminal). */
|
|
223
298
|
succeeded: number
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { Actions, ActionsPayload } from '@contember/schema'
|
|
2
2
|
import { EventRow, HandledEvent, InvokeHandler, InvokeHandlerArgs } from './types.js'
|
|
3
|
-
import { VariablesMap } from '../model/VariablesManager.js'
|
|
3
|
+
import { resolveVariable, VariablesMap } from '../model/VariablesManager.js'
|
|
4
4
|
import * as Typesafe from '@contember/typesafe'
|
|
5
5
|
import { FetcherResponse, WebhookFetcher } from './WebhookFetcher.js'
|
|
6
6
|
import { Logger } from '@contember/logger'
|
|
7
7
|
import { formatTraceparent, INVALID_TRACE_ID, Tracer } from '@contember/telemetry'
|
|
8
|
+
import { replaceActionsVariables } from '@contember/schema-utils'
|
|
9
|
+
import { redactRequestError } from './redactRequestError.js'
|
|
8
10
|
|
|
9
11
|
const DEFAULT_TIMEOUT_MS = 30_000 // 30 seconds
|
|
10
12
|
|
|
@@ -23,11 +25,12 @@ const ResponseType = Typesafe.noExtraProps(Typesafe.object({
|
|
|
23
25
|
|
|
24
26
|
type EventResponseFactory = (eventRow: EventRow) => { ok: boolean; code?: number; response?: string; errorMessage?: string; durationMs?: number }
|
|
25
27
|
|
|
28
|
+
/** Carries the name and code of a failed request, and a reason only when it was redacted. Never the cause, which may quote credentials. */
|
|
26
29
|
class WebhookRequestError extends Error {
|
|
27
30
|
public readonly code?: string | number
|
|
28
31
|
|
|
29
|
-
constructor(error: unknown) {
|
|
30
|
-
super('Webhook request failed')
|
|
32
|
+
constructor(error: unknown, redactedReason?: string) {
|
|
33
|
+
super(redactedReason === undefined ? 'Webhook request failed' : `Webhook request failed: ${redactedReason}`)
|
|
31
34
|
try {
|
|
32
35
|
if (error instanceof Error) {
|
|
33
36
|
this.name = error.name
|
|
@@ -52,6 +55,20 @@ class WebhookRequestError extends Error {
|
|
|
52
55
|
}
|
|
53
56
|
}
|
|
54
57
|
|
|
58
|
+
/** The target interpolates a variable that has no value, so no request was made. */
|
|
59
|
+
export class UndefinedVariableError extends Error {
|
|
60
|
+
constructor(public readonly variables: readonly string[]) {
|
|
61
|
+
super(`Undefined variable ${variables.join(', ')}`)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
type ResolvedRequest = {
|
|
66
|
+
url: string
|
|
67
|
+
headers: Record<string, string>
|
|
68
|
+
/** The resolved URL, header values and interpolated variable values, which an error message must not quote. */
|
|
69
|
+
secrets: ReadonlySet<string>
|
|
70
|
+
}
|
|
71
|
+
|
|
55
72
|
export type WebhookTargetHandlerOptions = {
|
|
56
73
|
propagateToWebhooks: boolean
|
|
57
74
|
}
|
|
@@ -66,6 +83,8 @@ export class WebhookTargetHandler implements InvokeHandler<Actions.WebhookTarget
|
|
|
66
83
|
|
|
67
84
|
public async handle({ target, events, logger, variables }: InvokeHandlerArgs<Actions.WebhookTarget>): Promise<HandledEvent[]> {
|
|
68
85
|
const timeoutMs = target.timeoutMs
|
|
86
|
+
// Outside the try below: an undefined variable is no failed delivery, the dispatcher holds the target back instead.
|
|
87
|
+
const request = this.resolveVariables(target, variables)
|
|
69
88
|
|
|
70
89
|
const start = process.hrtime.bigint()
|
|
71
90
|
const getDuration = () => Math.floor(Number((process.hrtime.bigint() - start) / BigInt(1_000_000)))
|
|
@@ -82,7 +101,7 @@ export class WebhookTargetHandler implements InvokeHandler<Actions.WebhookTarget
|
|
|
82
101
|
})
|
|
83
102
|
const response = await this.tracer.withSpan(span, async () => {
|
|
84
103
|
try {
|
|
85
|
-
const response = await this.fetch(timeoutMs ?? DEFAULT_TIMEOUT_MS, target,
|
|
104
|
+
const response = await this.fetch(timeoutMs ?? DEFAULT_TIMEOUT_MS, target, request, events)
|
|
86
105
|
span.setAttribute('http.response.status_code', response.status)
|
|
87
106
|
if (!response.ok) {
|
|
88
107
|
span.setStatus('error')
|
|
@@ -104,14 +123,15 @@ export class WebhookTargetHandler implements InvokeHandler<Actions.WebhookTarget
|
|
|
104
123
|
logger,
|
|
105
124
|
})
|
|
106
125
|
} catch (e) {
|
|
107
|
-
|
|
108
|
-
const errorMessages = this.extractErrorMessages(e)
|
|
126
|
+
// Request errors may quote the resolved URL or a header value, so only a redacted reason is logged and stored.
|
|
127
|
+
const errorMessages = this.extractErrorMessages(e).map(it => redactRequestError(it, request.secrets))
|
|
128
|
+
const errorMessage = errorMessages.length ? errorMessages.join('; ') : undefined
|
|
129
|
+
logger.warn(new WebhookRequestError(e, errorMessage), { target: target.name })
|
|
109
130
|
const result = {
|
|
110
131
|
ok: false,
|
|
111
|
-
errorMessage
|
|
132
|
+
errorMessage,
|
|
112
133
|
durationMs: getDuration(),
|
|
113
134
|
}
|
|
114
|
-
eventResponseFactory = () => ({ ok: false, errorMessage: errorMessages.length ? errorMessages.join('; ') : undefined })
|
|
115
135
|
|
|
116
136
|
return events.map(it => ({ target, row: it, result }))
|
|
117
137
|
}
|
|
@@ -227,30 +247,24 @@ export class WebhookTargetHandler implements InvokeHandler<Actions.WebhookTarget
|
|
|
227
247
|
return errorMessages
|
|
228
248
|
}
|
|
229
249
|
|
|
230
|
-
private async fetch(timeoutMs: number, target: Actions.WebhookTarget,
|
|
250
|
+
private async fetch(timeoutMs: number, target: Actions.WebhookTarget, request: ResolvedRequest, events: EventRow[]): Promise<FetcherResponse> {
|
|
231
251
|
return await withTimeout(timeoutMs, async abortController => {
|
|
232
|
-
return await this.doFetch(target,
|
|
252
|
+
return await this.doFetch(target, request, abortController, events)
|
|
233
253
|
})
|
|
234
254
|
}
|
|
235
255
|
|
|
236
256
|
private async doFetch(
|
|
237
257
|
target: Actions.WebhookTarget,
|
|
238
|
-
|
|
258
|
+
request: ResolvedRequest,
|
|
239
259
|
abortController: AbortController,
|
|
240
260
|
events: EventRow[],
|
|
241
261
|
): Promise<FetcherResponse> {
|
|
242
|
-
|
|
243
|
-
const resolvedHeaders = Object.fromEntries(
|
|
244
|
-
Object.entries(target.headers ?? {})
|
|
245
|
-
.map(([key, value]) => [key, this.resolveVariables(value, variables)]),
|
|
246
|
-
)
|
|
247
|
-
|
|
248
|
-
return await this.fetcher.fetch(resolvedUrl, {
|
|
262
|
+
return await this.fetcher.fetch(request.url, {
|
|
249
263
|
method: 'POST',
|
|
250
264
|
headers: {
|
|
251
265
|
['User-Agent']: 'Contember Actions',
|
|
252
|
-
...this.createTraceHeaders(
|
|
253
|
-
...
|
|
266
|
+
...this.createTraceHeaders(request.headers),
|
|
267
|
+
...request.headers,
|
|
254
268
|
['Content-type']: 'application/json',
|
|
255
269
|
},
|
|
256
270
|
signal: abortController.signal,
|
|
@@ -308,13 +322,30 @@ export class WebhookTargetHandler implements InvokeHandler<Actions.WebhookTarget
|
|
|
308
322
|
}
|
|
309
323
|
}
|
|
310
324
|
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
325
|
+
/** Throws {@link UndefinedVariableError} naming every variable without a value, an empty one included. */
|
|
326
|
+
private resolveVariables(target: Actions.WebhookTarget, variables: VariablesMap): ResolvedRequest {
|
|
327
|
+
const undefinedVariables = new Set<string>()
|
|
328
|
+
const secrets = new Set<string>()
|
|
329
|
+
const resolve = (subject: string) =>
|
|
330
|
+
replaceActionsVariables(subject, name => {
|
|
331
|
+
const value = resolveVariable(variables, name)
|
|
332
|
+
if (value === undefined) {
|
|
333
|
+
undefinedVariables.add(name)
|
|
334
|
+
return ''
|
|
335
|
+
}
|
|
336
|
+
secrets.add(value)
|
|
337
|
+
return value
|
|
338
|
+
})
|
|
339
|
+
const url = resolve(target.url)
|
|
340
|
+
const headers = Object.fromEntries(Object.entries(target.headers ?? {}).map(([key, value]) => [key, resolve(value)]))
|
|
341
|
+
if (undefinedVariables.size > 0) {
|
|
342
|
+
throw new UndefinedVariableError([...undefinedVariables])
|
|
343
|
+
}
|
|
344
|
+
secrets.add(url)
|
|
345
|
+
for (const value of Object.values(headers)) {
|
|
346
|
+
secrets.add(value)
|
|
347
|
+
}
|
|
348
|
+
return { url, headers, secrets }
|
|
318
349
|
}
|
|
319
350
|
}
|
|
320
351
|
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// scheme, optional userinfo up to the last @ of the authority, host and port, then path, query and fragment;
|
|
2
|
+
// a URL ends at whitespace or a quote, and starts only where a run of scheme characters starts, which keeps the scan linear
|
|
3
|
+
const urlPattern = /(?<![a-z\d+.-])([a-z][a-z\d+.-]*:\/\/)(?:([^\s/?#"'`<>]*)@)?([^\s/?#"'`<>]*)([^\s"'`<>]*)/gi
|
|
4
|
+
|
|
5
|
+
const redactedMarker = '[redacted]'
|
|
6
|
+
|
|
7
|
+
// fetch strips HTTP whitespace around a header value before quoting it in an error
|
|
8
|
+
const trimHttpWhitespace = (value: string): string => value.replace(/^[\t\n\r ]+/, '').replace(/[\t\n\r ]+$/, '')
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Removes what may carry a credential from a message about a failed webhook request: every occurrence of the given secrets,
|
|
12
|
+
* also with surrounding HTTP whitespace stripped, and then the userinfo, path, query and fragment of every URL it still quotes.
|
|
13
|
+
* The scheme, host and port of such a URL stay for diagnostics.
|
|
14
|
+
*/
|
|
15
|
+
export const redactRequestError = (message: string, secrets: Iterable<string>): string => {
|
|
16
|
+
const variants = new Set<string>()
|
|
17
|
+
for (const secret of secrets) {
|
|
18
|
+
variants.add(secret)
|
|
19
|
+
variants.add(trimHttpWhitespace(secret))
|
|
20
|
+
}
|
|
21
|
+
// the longest first, so a secret containing a shorter one is not left partly visible
|
|
22
|
+
const sortedSecrets = [...variants].filter(it => it !== '').sort((a, b) => b.length - a.length)
|
|
23
|
+
let redacted = message
|
|
24
|
+
for (const secret of sortedSecrets) {
|
|
25
|
+
redacted = redacted.replaceAll(secret, redactedMarker)
|
|
26
|
+
}
|
|
27
|
+
return redacted.replace(urlPattern, (_, scheme: string, userinfo: string | undefined, host: string, rest: string) => {
|
|
28
|
+
const redactedUserinfo = userinfo === undefined ? '' : `${redactedMarker}@`
|
|
29
|
+
const redactedRest = rest === '' || rest === '/' ? rest : `/${redactedMarker}`
|
|
30
|
+
return `${scheme}${redactedUserinfo}${host}${redactedRest}`
|
|
31
|
+
})
|
|
32
|
+
}
|
|
@@ -2,10 +2,13 @@ import { MutationResolvers, MutationSetVariablesArgs } from '../../schema/index.
|
|
|
2
2
|
import { ActionsContext } from '../ActionsContext.js'
|
|
3
3
|
import { VariablesManager } from '../../../model/VariablesManager.js'
|
|
4
4
|
import { ActionsAuthorizationActions } from '../../../authorization/index.js'
|
|
5
|
+
import { EventsRepository } from '../../../dispatch/EventsRepository.js'
|
|
6
|
+
import { notify } from '../../../utils/notifyChannel.js'
|
|
5
7
|
|
|
6
8
|
export class SetVariablesMutationResolver implements MutationResolvers<ActionsContext> {
|
|
7
9
|
constructor(
|
|
8
10
|
private readonly variablesManager: VariablesManager,
|
|
11
|
+
private readonly eventsRepository: EventsRepository,
|
|
9
12
|
) {
|
|
10
13
|
}
|
|
11
14
|
|
|
@@ -13,6 +16,10 @@ export class SetVariablesMutationResolver implements MutationResolvers<ActionsCo
|
|
|
13
16
|
await ctx.requireAccess(ActionsAuthorizationActions.VARIABLES_SET)
|
|
14
17
|
|
|
15
18
|
await this.variablesManager.setVariables(ctx.db, args, ctx.project.slug)
|
|
19
|
+
// After the variables are committed, so the worker re-checks held events against the new values.
|
|
20
|
+
if (await this.eventsRepository.resumeHeldForUndefinedVariables(ctx.db.client) > 0) {
|
|
21
|
+
await notify(ctx.db.client, ctx.project.slug)
|
|
22
|
+
}
|
|
16
23
|
|
|
17
24
|
return {
|
|
18
25
|
ok: true,
|
package/src/index.ts
CHANGED
|
@@ -107,7 +107,7 @@ export default class ActionsPlugin implements Plugin {
|
|
|
107
107
|
const eventsQueryResolver = new EventsQueryResolver()
|
|
108
108
|
const processBatchMutationResolver = new ProcessBatchMutationResolver(actions_eventDispatcher)
|
|
109
109
|
const variablesQueryResolver = new VariablesQueryResolver(actions_variableManager)
|
|
110
|
-
const setVariablesMutationResolver = new SetVariablesMutationResolver(actions_variableManager)
|
|
110
|
+
const setVariablesMutationResolver = new SetVariablesMutationResolver(actions_variableManager, actions_eventRepository)
|
|
111
111
|
const resolversFactory = new ResolversFactory(
|
|
112
112
|
eventsQueryResolver,
|
|
113
113
|
processBatchMutationResolver,
|
|
@@ -8,6 +8,12 @@ import { UserInputError } from '@contember/graphql-utils'
|
|
|
8
8
|
|
|
9
9
|
export type VariablesMap = Record<string, string>
|
|
10
10
|
|
|
11
|
+
/** A variable's value for interpolation; an empty value counts as unset, and only own entries count at all. */
|
|
12
|
+
export const resolveVariable = (variables: VariablesMap, name: string): string | undefined => {
|
|
13
|
+
const value = Object.hasOwn(variables, name) ? variables[name] : ''
|
|
14
|
+
return value === '' ? undefined : value
|
|
15
|
+
}
|
|
16
|
+
|
|
11
17
|
/** Between the project (or DEFAULT) prefix and the variable name: `MY_PROJECT_ACTIONS_VARIABLE_API_KEY`. */
|
|
12
18
|
const ENV_NAME_INFIX = '_ACTIONS_VARIABLE_'
|
|
13
19
|
|
|
@@ -7,11 +7,11 @@ import { Actions } from '@contember/schema'
|
|
|
7
7
|
import { emptySchema } from '@contember/schema-utils'
|
|
8
8
|
import { noopTracer, ReadableSpan } from '@contember/telemetry'
|
|
9
9
|
import { EventDispatcher } from '../../../src/dispatch/EventDispatcher.js'
|
|
10
|
-
import { EventsRepository, FetchBatchResult, PersistProcessedResult } from '../../../src/dispatch/EventsRepository.js'
|
|
10
|
+
import { EventsRepository, FetchBatchResult, HoldResult, PersistProcessedResult } from '../../../src/dispatch/EventsRepository.js'
|
|
11
11
|
import { TargetHandlerResolver } from '../../../src/dispatch/TargetHandlerResolver.js'
|
|
12
12
|
import { HandledEvent, InvokeHandler, InvokeHandlerArgs } from '../../../src/dispatch/types.js'
|
|
13
13
|
import { FetcherResponse, WebhookFetcher } from '../../../src/dispatch/WebhookFetcher.js'
|
|
14
|
-
import { WebhookTargetHandler } from '../../../src/dispatch/WebhookTargetHandler.js'
|
|
14
|
+
import { UndefinedVariableError, WebhookTargetHandler } from '../../../src/dispatch/WebhookTargetHandler.js'
|
|
15
15
|
import { VariablesManager, VariablesMap } from '../../../src/model/VariablesManager.js'
|
|
16
16
|
import { createTestEvent } from '../../src/event.js'
|
|
17
17
|
import { createRecordingTracer } from '../../src/tracer.js'
|
|
@@ -31,10 +31,13 @@ const idleBatch: FetchBatchResult = { ok: false, backoffMs: 100, unknownTargetFa
|
|
|
31
31
|
|
|
32
32
|
class StubEventsRepository extends EventsRepository {
|
|
33
33
|
public readonly persisted: HandledEvent[][] = []
|
|
34
|
+
public readonly held: Parameters<EventsRepository['holdForUndefinedVariables']>[1][] = []
|
|
35
|
+
public resumed = 0
|
|
34
36
|
|
|
35
37
|
constructor(
|
|
36
38
|
private readonly batch: FetchBatchResult,
|
|
37
39
|
private readonly result: PersistProcessedResult,
|
|
40
|
+
private readonly holdResult: HoldResult = { held: 5, joined: false },
|
|
38
41
|
) {
|
|
39
42
|
super()
|
|
40
43
|
}
|
|
@@ -47,15 +50,26 @@ class StubEventsRepository extends EventsRepository {
|
|
|
47
50
|
this.persisted.push(handled)
|
|
48
51
|
return this.result
|
|
49
52
|
}
|
|
53
|
+
|
|
54
|
+
override async holdForUndefinedVariables(db: Client, args: Parameters<EventsRepository['holdForUndefinedVariables']>[1]): Promise<HoldResult> {
|
|
55
|
+
this.held.push(args)
|
|
56
|
+
return this.holdResult
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
override async resumeHeldForUndefinedVariables(): Promise<number> {
|
|
60
|
+
this.resumed++
|
|
61
|
+
return 1
|
|
62
|
+
}
|
|
50
63
|
}
|
|
51
64
|
|
|
52
65
|
class StubVariablesManager extends VariablesManager {
|
|
53
|
-
constructor() {
|
|
66
|
+
constructor(private readonly variables: VariablesMap[] = [{}]) {
|
|
54
67
|
super({})
|
|
55
68
|
}
|
|
56
69
|
|
|
70
|
+
/** Answers with the given maps in turn, the last one from then on. */
|
|
57
71
|
override async fetchVariables(): Promise<VariablesMap> {
|
|
58
|
-
return {}
|
|
72
|
+
return (this.variables.length > 1 ? this.variables.shift() : this.variables[0]) ?? {}
|
|
59
73
|
}
|
|
60
74
|
}
|
|
61
75
|
|
|
@@ -106,11 +120,11 @@ const contentSchemaResolver = createMock<ContentSchemaResolver>({
|
|
|
106
120
|
getSchema: async () => ({ schema: emptySchema, meta: {} }),
|
|
107
121
|
})
|
|
108
122
|
|
|
109
|
-
const processBatch = async (dispatcher: EventDispatcher) =>
|
|
123
|
+
const processBatch = async (dispatcher: EventDispatcher, loggerHandler = new TestLoggerHandler()) =>
|
|
110
124
|
await dispatcher.processBatch({
|
|
111
125
|
db: createDatabaseContext(),
|
|
112
126
|
contentSchemaResolver,
|
|
113
|
-
logger: createLogger(
|
|
127
|
+
logger: createLogger(loggerHandler),
|
|
114
128
|
project: { slug: 'blog' },
|
|
115
129
|
})
|
|
116
130
|
|
|
@@ -202,3 +216,62 @@ test('dispatch: the webhook span nests under the dispatch span', async () => {
|
|
|
202
216
|
expect(webhookSpan?.context.traceId).toBe(dispatchSpan.context.traceId)
|
|
203
217
|
expect(requests[0].get('traceparent')).toContain(dispatchSpan.context.traceId)
|
|
204
218
|
})
|
|
219
|
+
|
|
220
|
+
test('dispatch: an undefined variable holds the whole target back with one warning and no spent attempt', async () => {
|
|
221
|
+
const { exporter, tracer, flush } = createRecordingTracer()
|
|
222
|
+
const loggerHandler = new TestLoggerHandler()
|
|
223
|
+
const repository = new StubEventsRepository(okBatch, { succeeded: 0, retried: 0, failed: 0 })
|
|
224
|
+
const resolver = new StubTargetHandlerResolver(async () => {
|
|
225
|
+
throw new UndefinedVariableError(['TOKEN'])
|
|
226
|
+
})
|
|
227
|
+
const dispatcher = new EventDispatcher(repository, new StubVariablesManager(), resolver, tracer)
|
|
228
|
+
|
|
229
|
+
const result = await processBatch(dispatcher, loggerHandler)
|
|
230
|
+
await flush()
|
|
231
|
+
|
|
232
|
+
expect(result).toStrictEqual({ succeeded: 0, retried: 0, failedAfterAttempt: 0, failedUnknownTarget: 0, backoffMs: 0 })
|
|
233
|
+
expect(repository.persisted).toStrictEqual([])
|
|
234
|
+
expect(repository.held).toStrictEqual([{ target: 'test_target', batch: events.map(it => it.id), variables: ['TOKEN'] }])
|
|
235
|
+
const reported = loggerHandler.messages.filter(it => it.level.name !== 'debug')
|
|
236
|
+
expect(reported).toHaveLength(1)
|
|
237
|
+
const [warning] = reported
|
|
238
|
+
expect(warning.level.name).toBe('warn')
|
|
239
|
+
expect(warning.message).toBe('Webhook target references an undefined variable, its events wait until it is set')
|
|
240
|
+
expect(warning.loggerAttributes.project).toBe('blog')
|
|
241
|
+
// exact match: the variable names only, never a value
|
|
242
|
+
expect(warning.ownAttributes).toStrictEqual({ target: 'test_target', variables: ['TOKEN'], events: 5 })
|
|
243
|
+
const span = dispatchSpanOf(exporter.spans)
|
|
244
|
+
expect(span.status.code).toBe('unset')
|
|
245
|
+
expect(span.attributes['contember.actions.held']).toBe(5)
|
|
246
|
+
expect(repository.resumed).toBe(0)
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
const undefinedTokenResolver = () =>
|
|
250
|
+
new StubTargetHandlerResolver(async () => {
|
|
251
|
+
throw new UndefinedVariableError(['TOKEN'])
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
test('dispatch: an event joining a held target is logged at debug level only', async () => {
|
|
255
|
+
const loggerHandler = new TestLoggerHandler()
|
|
256
|
+
const repository = new StubEventsRepository(okBatch, { succeeded: 0, retried: 0, failed: 0 }, { held: 1, joined: true })
|
|
257
|
+
const dispatcher = new EventDispatcher(repository, new StubVariablesManager(), undefinedTokenResolver(), createRecordingTracer().tracer)
|
|
258
|
+
|
|
259
|
+
await processBatch(dispatcher, loggerHandler)
|
|
260
|
+
|
|
261
|
+
expect(repository.held).toHaveLength(1)
|
|
262
|
+
expect(loggerHandler.messages.filter(it => it.level.name !== 'debug')).toStrictEqual([])
|
|
263
|
+
const joined = loggerHandler.messages.find(it => it.message === 'Webhook target references an undefined variable, its events wait until it is set')
|
|
264
|
+
expect(joined?.ownAttributes).toStrictEqual({ target: 'test_target', variables: ['TOKEN'], events: 1 })
|
|
265
|
+
})
|
|
266
|
+
|
|
267
|
+
test('dispatch: a variable set while the batch was being held releases the hold at once', async () => {
|
|
268
|
+
const repository = new StubEventsRepository(okBatch, { succeeded: 0, retried: 0, failed: 0 })
|
|
269
|
+
// read once for the request, then again after the hold, by when setVariables has committed
|
|
270
|
+
const variablesManager = new StubVariablesManager([{}, { TOKEN: 'secret' }])
|
|
271
|
+
const dispatcher = new EventDispatcher(repository, variablesManager, undefinedTokenResolver(), createRecordingTracer().tracer)
|
|
272
|
+
|
|
273
|
+
await processBatch(dispatcher)
|
|
274
|
+
|
|
275
|
+
expect(repository.held).toHaveLength(1)
|
|
276
|
+
expect(repository.resumed).toBe(1)
|
|
277
|
+
})
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { expect, test } from 'bun:test'
|
|
2
|
+
import { redactRequestError } from '../../../src/dispatch/redactRequestError.js'
|
|
3
|
+
|
|
4
|
+
test('keeps a message without a URL or a secret', () => {
|
|
5
|
+
expect(redactRequestError('getaddrinfo ENOTFOUND example.com', [])).toBe('getaddrinfo ENOTFOUND example.com')
|
|
6
|
+
})
|
|
7
|
+
|
|
8
|
+
test('keeps only the scheme, host and port of a URL', () => {
|
|
9
|
+
expect(redactRequestError('Failed to parse URL from https://user:pw@example.com:8443/a/b?c=d#e', []))
|
|
10
|
+
.toBe('Failed to parse URL from https://[redacted]@example.com:8443/[redacted]')
|
|
11
|
+
expect(redactRequestError('Invalid URL http://example.com/?token=abc.', [])).toBe('Invalid URL http://example.com/[redacted]')
|
|
12
|
+
expect(redactRequestError('Unable to connect to http://example.com/ or http://example.org', []))
|
|
13
|
+
.toBe('Unable to connect to http://example.com/ or http://example.org')
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
test('redacts every URL, a quoted one included', () => {
|
|
17
|
+
expect(redactRequestError('Redirect from "https://a.example/x?t=1" to https://b.example/y', []))
|
|
18
|
+
.toBe('Redirect from "https://a.example/[redacted]" to https://b.example/[redacted]')
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
test('redacts every occurrence of a secret, the longest first', () => {
|
|
22
|
+
expect(redactRequestError('expected abc, got abcdef and abc', ['abc', 'abcdef', ''])).toBe('expected [redacted], got [redacted] and [redacted]')
|
|
23
|
+
expect(redactRequestError('invalid header value "Bearer abc"', ['Bearer abc', 'abc'])).toBe('invalid header value "[redacted]"')
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
test('redacts a password containing @, / or ?', () => {
|
|
27
|
+
expect(redactRequestError('Request cannot be constructed from a URL that includes credentials: https://user:p@ss@example.com/hooks', []))
|
|
28
|
+
.toBe('Request cannot be constructed from a URL that includes credentials: https://[redacted]@example.com/[redacted]')
|
|
29
|
+
// such a password ends the authority early, so only the known value redacts all of it
|
|
30
|
+
expect(redactRequestError('Failed to parse URL from https://user:pa/s?s@example.com/hooks', ['pa/s?s']))
|
|
31
|
+
.toBe('Failed to parse URL from https://[redacted]@example.com/[redacted]')
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
test('redacts a secret quoted without its surrounding whitespace', () => {
|
|
35
|
+
expect(redactRequestError('Headers.append: "Bearer abc\ndef" is an invalid header value.', [' Bearer abc\ndef\n', 'abc\ndef\n']))
|
|
36
|
+
.toBe('Headers.append: "[redacted]" is an invalid header value.')
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
test('scans a long message without a URL in linear time', () => {
|
|
40
|
+
const start = performance.now()
|
|
41
|
+
expect(redactRequestError('a-'.repeat(100_000), [])).toHaveLength(200_000)
|
|
42
|
+
expect(redactRequestError('a.'.repeat(100_000), [])).toHaveLength(200_000)
|
|
43
|
+
// the quadratic scan took seconds on a tenth of this
|
|
44
|
+
expect(performance.now() - start).toBeLessThan(1_000)
|
|
45
|
+
})
|