@superdurable/connector-amazon-redshift 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +4 -2
- package/dex-connector.json +294 -0
- package/dist/chunk-V4IZMGI7.js +293 -0
- package/dist/connection-QfejfWYW.d.ts +185 -0
- package/dist/index.d.ts +343 -0
- package/dist/index.js +638 -0
- package/dist/mock.d.ts +117 -0
- package/dist/mock.js +277 -0
- package/package.json +59 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,638 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CancelStatementBranch,
|
|
3
|
+
CancelStatementBranchCanceled,
|
|
4
|
+
CancelStatementBranchDefect,
|
|
5
|
+
CancelStatementBranchInvalidResponse,
|
|
6
|
+
CancelStatementBranchMissing,
|
|
7
|
+
CancelStatementBranchNotCanceled,
|
|
8
|
+
CancelStatementBranchProviderRejected,
|
|
9
|
+
CancelStatementDefinition,
|
|
10
|
+
CancelStatementInputSchema,
|
|
11
|
+
CancelStatementMetadata,
|
|
12
|
+
ConfigSchema,
|
|
13
|
+
Connection,
|
|
14
|
+
ConnectorID,
|
|
15
|
+
CredentialMaterialError,
|
|
16
|
+
DescribeStatementBranch,
|
|
17
|
+
DescribeStatementBranchDefect,
|
|
18
|
+
DescribeStatementBranchDescribed,
|
|
19
|
+
DescribeStatementBranchInvalidResponse,
|
|
20
|
+
DescribeStatementBranchMissing,
|
|
21
|
+
DescribeStatementBranchProviderRejected,
|
|
22
|
+
DescribeStatementDefinition,
|
|
23
|
+
DescribeStatementInputSchema,
|
|
24
|
+
DescribeStatementMetadata,
|
|
25
|
+
ExecuteStatementBranch,
|
|
26
|
+
ExecuteStatementBranchDefect,
|
|
27
|
+
ExecuteStatementBranchInvalidResponse,
|
|
28
|
+
ExecuteStatementBranchProviderRejected,
|
|
29
|
+
ExecuteStatementBranchSubmitted,
|
|
30
|
+
ExecuteStatementDefinition,
|
|
31
|
+
ExecuteStatementInputSchema,
|
|
32
|
+
ExecuteStatementMetadata,
|
|
33
|
+
GetStatementResultBranch,
|
|
34
|
+
GetStatementResultBranchDefect,
|
|
35
|
+
GetStatementResultBranchFetched,
|
|
36
|
+
GetStatementResultBranchInvalidResponse,
|
|
37
|
+
GetStatementResultBranchMissing,
|
|
38
|
+
GetStatementResultBranchProviderRejected,
|
|
39
|
+
GetStatementResultDefinition,
|
|
40
|
+
GetStatementResultInputSchema,
|
|
41
|
+
GetStatementResultMetadata,
|
|
42
|
+
ListStatementsBranch,
|
|
43
|
+
ListStatementsBranchDefect,
|
|
44
|
+
ListStatementsBranchInvalidResponse,
|
|
45
|
+
ListStatementsBranchListed,
|
|
46
|
+
ListStatementsBranchProviderRejected,
|
|
47
|
+
ListStatementsDefinition,
|
|
48
|
+
ListStatementsInputSchema,
|
|
49
|
+
ListStatementsMetadata,
|
|
50
|
+
ProviderID,
|
|
51
|
+
StatementCancellationSchema,
|
|
52
|
+
StatementIdentitySchema,
|
|
53
|
+
StatementPageSchema,
|
|
54
|
+
StatementResultPageSchema,
|
|
55
|
+
StatementStatusSchema,
|
|
56
|
+
decodeCredentials,
|
|
57
|
+
isCredentialMaterialError,
|
|
58
|
+
newConnection,
|
|
59
|
+
newConnectionWithOperations,
|
|
60
|
+
newProjectConnection
|
|
61
|
+
} from "./chunk-V4IZMGI7.js";
|
|
62
|
+
|
|
63
|
+
// src/generated/steps.ts
|
|
64
|
+
import { goToBranch, newMutationStep, newQueryStep } from "@superdurable/connectors-sdk";
|
|
65
|
+
function newGetStatementResultStep(configuration) {
|
|
66
|
+
const { connection, connectionName, fetched, missing, providerRejected, invalidResponse: invalidResponse2, defect: defect2, ...stepConfiguration } = configuration;
|
|
67
|
+
if (!connection || connection.reference.name !== connectionName) throw new TypeError("Step connection name differs from the bound connection");
|
|
68
|
+
return newQueryStep({ ...stepConfiguration, operation: connection.operations.getStatementResult(), connection: connection.reference, branches: [
|
|
69
|
+
goToBranch("fetched", fetched),
|
|
70
|
+
...missing ? [goToBranch("missing", missing)] : [],
|
|
71
|
+
...providerRejected ? [goToBranch("providerRejected", providerRejected)] : [],
|
|
72
|
+
...invalidResponse2 ? [goToBranch("invalidResponse", invalidResponse2)] : [],
|
|
73
|
+
...defect2 ? [goToBranch("defect", defect2)] : []
|
|
74
|
+
] });
|
|
75
|
+
}
|
|
76
|
+
function newExecuteStatementStep(configuration) {
|
|
77
|
+
const { connection, connectionName, submitted, providerRejected, invalidResponse: invalidResponse2, defect: defect2, maxSends, ...stepConfiguration } = configuration;
|
|
78
|
+
if (!connection || connection.reference.name !== connectionName) throw new TypeError("Step connection name differs from the bound connection");
|
|
79
|
+
return newMutationStep({ ...stepConfiguration, operation: connection.operations.executeStatement(), connection: connection.reference, branches: [
|
|
80
|
+
goToBranch("submitted", submitted),
|
|
81
|
+
...providerRejected ? [goToBranch("providerRejected", providerRejected)] : [],
|
|
82
|
+
...invalidResponse2 ? [goToBranch("invalidResponse", invalidResponse2)] : [],
|
|
83
|
+
...defect2 ? [goToBranch("defect", defect2)] : []
|
|
84
|
+
] });
|
|
85
|
+
}
|
|
86
|
+
function newDescribeStatementStep(configuration) {
|
|
87
|
+
const { connection, connectionName, described, missing, providerRejected, invalidResponse: invalidResponse2, defect: defect2, ...stepConfiguration } = configuration;
|
|
88
|
+
if (!connection || connection.reference.name !== connectionName) throw new TypeError("Step connection name differs from the bound connection");
|
|
89
|
+
return newQueryStep({ ...stepConfiguration, operation: connection.operations.describeStatement(), connection: connection.reference, branches: [
|
|
90
|
+
goToBranch("described", described),
|
|
91
|
+
...missing ? [goToBranch("missing", missing)] : [],
|
|
92
|
+
...providerRejected ? [goToBranch("providerRejected", providerRejected)] : [],
|
|
93
|
+
...invalidResponse2 ? [goToBranch("invalidResponse", invalidResponse2)] : [],
|
|
94
|
+
...defect2 ? [goToBranch("defect", defect2)] : []
|
|
95
|
+
] });
|
|
96
|
+
}
|
|
97
|
+
function newListStatementsStep(configuration) {
|
|
98
|
+
const { connection, connectionName, listed, providerRejected, invalidResponse: invalidResponse2, defect: defect2, ...stepConfiguration } = configuration;
|
|
99
|
+
if (!connection || connection.reference.name !== connectionName) throw new TypeError("Step connection name differs from the bound connection");
|
|
100
|
+
return newQueryStep({ ...stepConfiguration, operation: connection.operations.listStatements(), connection: connection.reference, branches: [
|
|
101
|
+
goToBranch("listed", listed),
|
|
102
|
+
...providerRejected ? [goToBranch("providerRejected", providerRejected)] : [],
|
|
103
|
+
...invalidResponse2 ? [goToBranch("invalidResponse", invalidResponse2)] : [],
|
|
104
|
+
...defect2 ? [goToBranch("defect", defect2)] : []
|
|
105
|
+
] });
|
|
106
|
+
}
|
|
107
|
+
function newCancelStatementStep(configuration) {
|
|
108
|
+
const { connection, connectionName, canceled, notCanceled, missing, providerRejected, invalidResponse: invalidResponse2, defect: defect2, maxSends, ...stepConfiguration } = configuration;
|
|
109
|
+
if (!connection || connection.reference.name !== connectionName) throw new TypeError("Step connection name differs from the bound connection");
|
|
110
|
+
return newMutationStep({ ...stepConfiguration, operation: connection.operations.cancelStatement(), connection: connection.reference, branches: [
|
|
111
|
+
goToBranch("canceled", canceled),
|
|
112
|
+
...notCanceled ? [goToBranch("notCanceled", notCanceled)] : [],
|
|
113
|
+
...missing ? [goToBranch("missing", missing)] : [],
|
|
114
|
+
...providerRejected ? [goToBranch("providerRejected", providerRejected)] : [],
|
|
115
|
+
...invalidResponse2 ? [goToBranch("invalidResponse", invalidResponse2)] : [],
|
|
116
|
+
...defect2 ? [goToBranch("defect", defect2)] : []
|
|
117
|
+
] });
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// src/project.ts
|
|
121
|
+
import { SecretString as SecretString2 } from "@superdurable/connectors-sdk";
|
|
122
|
+
import { z } from "zod";
|
|
123
|
+
|
|
124
|
+
// src/client.ts
|
|
125
|
+
import { createHash, createHmac } from "crypto";
|
|
126
|
+
import { SecretString, newMutationBranch, newMutationRetry, newQueryBranch, newQueryRetry } from "@superdurable/connectors-sdk";
|
|
127
|
+
import { BodyTooLargeError, ProviderStalledError, ProviderTransportError, isHeaderSafeCredential, parseRetryAfter, sendProviderRequest, validateBaseURL } from "@superdurable/connectors-sdk/providerhttp";
|
|
128
|
+
var emptyPage = () => ({ records: [], nextToken: "", totalNumRows: 0 });
|
|
129
|
+
var regionPattern = /^[a-z]{2}(?:-gov)?-[a-z]+-[0-9]+$/;
|
|
130
|
+
var statementIDPattern = /^[a-z0-9]{8}(?:-[a-z0-9]{4}){3}-[a-z0-9]{12}(?::[0-9]{1,2})?$/;
|
|
131
|
+
var AmazonRedshiftClient = class {
|
|
132
|
+
/** Validate the Region, endpoint, and limits without contacting AWS. */
|
|
133
|
+
constructor(config, credentials, options = {}) {
|
|
134
|
+
this.credentials = credentials;
|
|
135
|
+
if (!config || !credentials) throw new TypeError("Redshift configuration and credentials are required");
|
|
136
|
+
this.region = config.region || "us-east-1";
|
|
137
|
+
if (!regionPattern.test(this.region)) throw new TypeError("Redshift Region is invalid");
|
|
138
|
+
this.endpoint = validateBaseURL(config.endpoint || `https://redshift-data.${this.region}.amazonaws.com`).replace(/\/+$/, "");
|
|
139
|
+
this.timeoutMs = config.timeoutMs || 15e3;
|
|
140
|
+
this.maxResponseBytes = config.maxResponseBytes || 2097152;
|
|
141
|
+
if (!Number.isSafeInteger(this.timeoutMs) || this.timeoutMs <= 0 || !Number.isSafeInteger(this.maxResponseBytes) || this.maxResponseBytes <= 0)
|
|
142
|
+
throw new TypeError("Redshift request limits must be positive integers");
|
|
143
|
+
this.send = options.send ?? sendProviderRequest;
|
|
144
|
+
this.now = options.now ?? (() => /* @__PURE__ */ new Date());
|
|
145
|
+
}
|
|
146
|
+
credentials;
|
|
147
|
+
endpoint;
|
|
148
|
+
region;
|
|
149
|
+
timeoutMs;
|
|
150
|
+
maxResponseBytes;
|
|
151
|
+
send;
|
|
152
|
+
now;
|
|
153
|
+
/** Bind the getStatementResult Query. */
|
|
154
|
+
getStatementResult() {
|
|
155
|
+
return new GetStatementResultOperation(this);
|
|
156
|
+
}
|
|
157
|
+
/** Bind the idempotent submission Mutation. */
|
|
158
|
+
executeStatement() {
|
|
159
|
+
return new ExecuteStatementOperation(this);
|
|
160
|
+
}
|
|
161
|
+
/** Bind the statement status Query. */
|
|
162
|
+
describeStatement() {
|
|
163
|
+
return new DescribeStatementOperation(this);
|
|
164
|
+
}
|
|
165
|
+
/** Bind the bounded statement listing Query. */
|
|
166
|
+
listStatements() {
|
|
167
|
+
return new ListStatementsOperation(this);
|
|
168
|
+
}
|
|
169
|
+
/** Bind the repeat-safe cancellation Mutation. */
|
|
170
|
+
cancelStatement() {
|
|
171
|
+
return new CancelStatementOperation(this);
|
|
172
|
+
}
|
|
173
|
+
/** Sign and send one bounded GetStatementResult request. */
|
|
174
|
+
async readStatementResult(call, body) {
|
|
175
|
+
return this.sendStatementRequest(call, "GetStatementResult", body);
|
|
176
|
+
}
|
|
177
|
+
/** Sign and send one bounded Redshift Data API request. */
|
|
178
|
+
async sendStatementRequest(call, target, body) {
|
|
179
|
+
let accessKey;
|
|
180
|
+
let secretKey;
|
|
181
|
+
let sessionToken;
|
|
182
|
+
try {
|
|
183
|
+
const credentials = await this.credentials.resolve(call, call.context.cancellationSignal);
|
|
184
|
+
accessKey = credentials.accessKeyId instanceof SecretString ? credentials.accessKeyId.reveal() : "";
|
|
185
|
+
secretKey = credentials.secretAccessKey instanceof SecretString ? credentials.secretAccessKey.reveal() : "";
|
|
186
|
+
sessionToken = credentials.sessionToken instanceof SecretString ? credentials.sessionToken.reveal() : "";
|
|
187
|
+
} catch {
|
|
188
|
+
return emptyExchange("credential");
|
|
189
|
+
}
|
|
190
|
+
if (!accessKey || !secretKey || !isHeaderSafeCredential(accessKey) || !isHeaderSafeCredential(secretKey) || sessionToken && !isHeaderSafeCredential(sessionToken)) return emptyExchange("credential");
|
|
191
|
+
const url = `${this.endpoint}/`;
|
|
192
|
+
const timestamp = this.now().toISOString().replace(/[-:]|\.\d{3}/g, "");
|
|
193
|
+
const day = timestamp.slice(0, 8);
|
|
194
|
+
const headersForSignature = {
|
|
195
|
+
"content-length": String(body.length),
|
|
196
|
+
"content-type": "application/x-amz-json-1.1",
|
|
197
|
+
host: new URL(url).host,
|
|
198
|
+
"x-amz-date": timestamp,
|
|
199
|
+
"x-amz-target": `RedshiftData.${target}`
|
|
200
|
+
};
|
|
201
|
+
if (sessionToken) headersForSignature["x-amz-security-token"] = sessionToken;
|
|
202
|
+
const signedHeaders = Object.keys(headersForSignature).sort().join(";");
|
|
203
|
+
const canonicalHeaders = Object.entries(headersForSignature).sort(([left], [right]) => left.localeCompare(right)).map(([name, value]) => `${name}:${value.trim()}
|
|
204
|
+
`).join("");
|
|
205
|
+
const payloadHash = createHash("sha256").update(body).digest("hex");
|
|
206
|
+
const canonicalRequest = `POST
|
|
207
|
+
/
|
|
208
|
+
|
|
209
|
+
${canonicalHeaders}
|
|
210
|
+
${signedHeaders}
|
|
211
|
+
${payloadHash}`;
|
|
212
|
+
const scope = `${day}/${this.region}/redshift-data/aws4_request`;
|
|
213
|
+
const stringToSign = `AWS4-HMAC-SHA256
|
|
214
|
+
${timestamp}
|
|
215
|
+
${scope}
|
|
216
|
+
${createHash("sha256").update(canonicalRequest).digest("hex")}`;
|
|
217
|
+
const hmac = (key, value) => createHmac("sha256", key).update(value).digest();
|
|
218
|
+
const signingKey = hmac(hmac(hmac(hmac(`AWS4${secretKey}`, day), this.region), "redshift-data"), "aws4_request");
|
|
219
|
+
const signature = createHmac("sha256", signingKey).update(stringToSign).digest("hex");
|
|
220
|
+
const headers = {
|
|
221
|
+
"Content-Type": headersForSignature["content-type"],
|
|
222
|
+
"X-Amz-Date": timestamp,
|
|
223
|
+
"X-Amz-Target": headersForSignature["x-amz-target"],
|
|
224
|
+
Authorization: `AWS4-HMAC-SHA256 Credential=${accessKey}/${scope}, SignedHeaders=${signedHeaders}, Signature=${signature}`
|
|
225
|
+
};
|
|
226
|
+
if (sessionToken) headers["X-Amz-Security-Token"] = sessionToken;
|
|
227
|
+
const request = {
|
|
228
|
+
url,
|
|
229
|
+
method: "POST",
|
|
230
|
+
headers,
|
|
231
|
+
body,
|
|
232
|
+
maxResponseBytes: this.maxResponseBytes,
|
|
233
|
+
timeoutMs: this.timeoutMs,
|
|
234
|
+
addressPolicy: this.endpoint.startsWith("https://") ? "publicHTTPS" : "privateNetworkHTTP",
|
|
235
|
+
signal: call.context.cancellationSignal
|
|
236
|
+
};
|
|
237
|
+
let response;
|
|
238
|
+
let isTooLarge = false;
|
|
239
|
+
try {
|
|
240
|
+
response = await this.send(request);
|
|
241
|
+
} catch (error) {
|
|
242
|
+
if (error instanceof BodyTooLargeError && error.status !== void 0) {
|
|
243
|
+
response = { status: error.status, headers: error.headers ?? {}, body: new Uint8Array() };
|
|
244
|
+
isTooLarge = true;
|
|
245
|
+
} else if ((error instanceof ProviderTransportError || error instanceof ProviderStalledError) && error.status !== void 0) {
|
|
246
|
+
return {
|
|
247
|
+
status: error.status,
|
|
248
|
+
body: new Uint8Array(),
|
|
249
|
+
retryAfterMs: parseRetryAfter(header(error.headers ?? {}, "retry-after")),
|
|
250
|
+
error: "interrupted"
|
|
251
|
+
};
|
|
252
|
+
} else return emptyExchange("request");
|
|
253
|
+
}
|
|
254
|
+
const retryAfterMs = parseRetryAfter(header(response.headers, "retry-after"));
|
|
255
|
+
if (isTooLarge || response.body.length > this.maxResponseBytes)
|
|
256
|
+
return { status: response.status, body: new Uint8Array(), retryAfterMs, error: "tooLarge" };
|
|
257
|
+
if ([accessKey, secretKey, sessionToken].some((secret) => secret && Buffer.from(response.body).includes(secret)))
|
|
258
|
+
return { ...emptyExchange("credential"), status: response.status, retryAfterMs };
|
|
259
|
+
return { status: response.status, body: response.body, retryAfterMs };
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
var GetStatementResultOperation = class {
|
|
263
|
+
/** Bind the provider client. */
|
|
264
|
+
constructor(client) {
|
|
265
|
+
this.client = client;
|
|
266
|
+
}
|
|
267
|
+
client;
|
|
268
|
+
/** Return the generated Query contract. */
|
|
269
|
+
definition() {
|
|
270
|
+
return GetStatementResultDefinition;
|
|
271
|
+
}
|
|
272
|
+
/** Classify provider outcomes consistently with the Go implementation. */
|
|
273
|
+
async invoke(call, input) {
|
|
274
|
+
if (!input || typeof input.statementId !== "string" || !statementIDPattern.test(input.statementId) || typeof input.nextToken !== "string" || input.nextToken.length > 4096 || [...input.nextToken].some((character) => character.charCodeAt(0) === 0 || character === "\r" || character === "\n"))
|
|
275
|
+
return terminal("defect", "VALIDATION", "Redshift statement ID or page token is invalid");
|
|
276
|
+
const body = Buffer.from(JSON.stringify({ Id: input.statementId, ...input.nextToken ? { NextToken: input.nextToken } : {} }));
|
|
277
|
+
const exchange = await this.client.readStatementResult(call, body);
|
|
278
|
+
if (exchange.error === "credential") return terminal("defect", "AUTHENTICATION", "Redshift credentials are unavailable or invalid");
|
|
279
|
+
if (exchange.error === "request") return retry("TRANSPORT", "Redshift request failed");
|
|
280
|
+
if (exchange.status === 429 || exchange.status !== 200 && isThrottled(exchange.body)) return retry("RATE_LIMIT", "Redshift rate limited the request", exchange.retryAfterMs);
|
|
281
|
+
if (exchange.status === 408 || exchange.status >= 500) return retry("AVAILABILITY", "Redshift is temporarily unavailable", exchange.retryAfterMs);
|
|
282
|
+
if (exchange.status === 404 || exchange.status === 400 && hasErrorCode(exchange.body, "ResourceNotFoundException"))
|
|
283
|
+
return newQueryBranch("missing", emptyPage());
|
|
284
|
+
if (exchange.status !== 200) return terminal("providerRejected", "PROVIDER_REJECTION", `Redshift rejected HTTP ${exchange.status}`);
|
|
285
|
+
if (exchange.error === "tooLarge") return terminal("invalidResponse", "RESPONSE_TOO_LARGE", "Redshift response exceeded the configured limit");
|
|
286
|
+
if (exchange.error === "interrupted") return retry("TRANSPORT", "Redshift response was interrupted");
|
|
287
|
+
let document;
|
|
288
|
+
try {
|
|
289
|
+
document = JSON.parse(Buffer.from(exchange.body).toString("utf8"));
|
|
290
|
+
} catch {
|
|
291
|
+
return terminal("invalidResponse", "PROTOCOL", "Redshift returned invalid result JSON");
|
|
292
|
+
}
|
|
293
|
+
if (!isObject(document) || !Array.isArray(document.Records) || document.NextToken !== void 0 && typeof document.NextToken !== "string" || document.TotalNumRows !== void 0 && (typeof document.TotalNumRows !== "number" || !Number.isSafeInteger(document.TotalNumRows) || document.TotalNumRows < 0) || document.Records.some((row) => !Array.isArray(row) || row.some((field) => !isObject(field) || Object.keys(field).length !== 1)))
|
|
294
|
+
return terminal("invalidResponse", "PROTOCOL", "Redshift returned invalid result JSON");
|
|
295
|
+
return newQueryBranch("fetched", { records: document.Records, nextToken: document.NextToken ?? "", totalNumRows: document.TotalNumRows ?? 0 });
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
function isObject(value) {
|
|
299
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
300
|
+
}
|
|
301
|
+
function hasErrorCode(body, expected) {
|
|
302
|
+
try {
|
|
303
|
+
const document = JSON.parse(Buffer.from(body).toString("utf8"));
|
|
304
|
+
return isObject(document) && typeof document.__type === "string" && document.__type.split("#").at(-1) === expected;
|
|
305
|
+
} catch {
|
|
306
|
+
return false;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
function isThrottled(body) {
|
|
310
|
+
return ["ThrottlingException", "ActiveStatementsExceededException", "ActiveSessionsExceededException"].some((code) => hasErrorCode(body, code));
|
|
311
|
+
}
|
|
312
|
+
function emptyExchange(error) {
|
|
313
|
+
return { status: 0, body: new Uint8Array(), retryAfterMs: 0, error };
|
|
314
|
+
}
|
|
315
|
+
function failure(kind, message) {
|
|
316
|
+
return { provider: "amazon-redshift", operation: "getStatementResult", kind, message };
|
|
317
|
+
}
|
|
318
|
+
function terminal(branch, kind, message) {
|
|
319
|
+
return newQueryBranch(branch, emptyPage(), {}, failure(kind, message));
|
|
320
|
+
}
|
|
321
|
+
function retry(kind, message, delay = 0) {
|
|
322
|
+
return newQueryRetry(failure(kind, message), delay);
|
|
323
|
+
}
|
|
324
|
+
function header(headers, name) {
|
|
325
|
+
const entry = Object.entries(headers).find(([key]) => key.toLowerCase() === name.toLowerCase())?.[1];
|
|
326
|
+
return Array.isArray(entry) ? entry[0] ?? "" : entry ?? "";
|
|
327
|
+
}
|
|
328
|
+
var ExecuteStatementOperation = class {
|
|
329
|
+
/** Bind the provider client. */
|
|
330
|
+
constructor(client) {
|
|
331
|
+
this.client = client;
|
|
332
|
+
}
|
|
333
|
+
client;
|
|
334
|
+
/** Return the generated submission contract. */
|
|
335
|
+
definition() {
|
|
336
|
+
return ExecuteStatementDefinition;
|
|
337
|
+
}
|
|
338
|
+
/** Use the stable Dex call ID to deduplicate submissions. */
|
|
339
|
+
idempotencyKey(callId) {
|
|
340
|
+
return callId;
|
|
341
|
+
}
|
|
342
|
+
/** Submit one statement and retry unknown outcomes with the same token. */
|
|
343
|
+
async invoke(call, input) {
|
|
344
|
+
const operation = "executeStatement";
|
|
345
|
+
const empty = { id: "" };
|
|
346
|
+
if (!workgroupNamePattern.test(input.workgroupName) || !input.database.trim() || Buffer.byteLength(input.database) > 127 || !input.sql.trim() || Buffer.byteLength(input.sql) > 200 * 1024 || !call.idempotencyKey)
|
|
347
|
+
return classifiedMutation(defect(operation, "Redshift execution input is invalid"), empty);
|
|
348
|
+
const body = encodeRequest({
|
|
349
|
+
ClientToken: call.idempotencyKey,
|
|
350
|
+
WorkgroupName: input.workgroupName,
|
|
351
|
+
Database: input.database,
|
|
352
|
+
Sql: input.sql,
|
|
353
|
+
ResultFormat: "JSON"
|
|
354
|
+
});
|
|
355
|
+
const exchange = await this.client.sendStatementRequest(call, "ExecuteStatement", body);
|
|
356
|
+
const rejected = classifyResponse(exchange, operation, call);
|
|
357
|
+
if (rejected) return classifiedMutation(rejected, empty);
|
|
358
|
+
const result = decodeObject(exchange.body);
|
|
359
|
+
if (!isStatementID(result?.Id)) return newMutationRetry(operationFailure(operation, "PROTOCOL", "Redshift returned invalid statement ID"));
|
|
360
|
+
return newMutationBranch("submitted", { id: result.Id });
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
var DescribeStatementOperation = class {
|
|
364
|
+
/** Bind the provider client. */
|
|
365
|
+
constructor(client) {
|
|
366
|
+
this.client = client;
|
|
367
|
+
}
|
|
368
|
+
client;
|
|
369
|
+
/** Return the generated status query contract. */
|
|
370
|
+
definition() {
|
|
371
|
+
return DescribeStatementDefinition;
|
|
372
|
+
}
|
|
373
|
+
/** Validate the returned ID and status before returning the result. */
|
|
374
|
+
async invoke(call, input) {
|
|
375
|
+
const operation = "describeStatement";
|
|
376
|
+
const empty = { id: "", status: "", hasResultSet: false };
|
|
377
|
+
if (!isStatementID(input.statementId))
|
|
378
|
+
return classifiedQuery(defect(operation, "Redshift statement ID is invalid"), empty);
|
|
379
|
+
const exchange = await this.client.sendStatementRequest(call, "DescribeStatement", encodeRequest({ Id: input.statementId }));
|
|
380
|
+
const rejected = classifyResponse(exchange, operation);
|
|
381
|
+
if (rejected) return classifiedQuery(rejected, empty);
|
|
382
|
+
const result = decodeObject(exchange.body);
|
|
383
|
+
const hasResultSet = result?.HasResultSet ?? false;
|
|
384
|
+
if (result?.Id !== input.statementId || !isStatementStatus(result.Status) || typeof hasResultSet !== "boolean")
|
|
385
|
+
return classifiedQuery(invalidResponse(operation, "Redshift returned invalid statement status"), empty);
|
|
386
|
+
return newQueryBranch("described", { id: input.statementId, status: result.Status, hasResultSet });
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
var ListStatementsOperation = class {
|
|
390
|
+
/** Bind the provider client. */
|
|
391
|
+
constructor(client) {
|
|
392
|
+
this.client = client;
|
|
393
|
+
}
|
|
394
|
+
client;
|
|
395
|
+
/** Return the generated list query contract. */
|
|
396
|
+
definition() {
|
|
397
|
+
return ListStatementsDefinition;
|
|
398
|
+
}
|
|
399
|
+
/** List one page with the requested limit and continuation. */
|
|
400
|
+
async invoke(call, input) {
|
|
401
|
+
const operation = "listStatements";
|
|
402
|
+
const empty = { statements: [], nextToken: "" };
|
|
403
|
+
const { maxResults, nextToken } = input;
|
|
404
|
+
if (!Number.isInteger(maxResults) || maxResults < 0 || maxResults > 100 || Buffer.byteLength(nextToken) > 4096 || nextToken.includes("\0") || nextToken.includes("\r") || nextToken.includes("\n"))
|
|
405
|
+
return classifiedQuery(defect(operation, "Redshift list input is invalid"), empty);
|
|
406
|
+
const body = encodeRequest({ ...maxResults ? { MaxResults: maxResults } : {}, ...nextToken ? { NextToken: nextToken } : {} });
|
|
407
|
+
const exchange = await this.client.sendStatementRequest(call, "ListStatements", body);
|
|
408
|
+
const rejected = classifyResponse(exchange, operation);
|
|
409
|
+
if (rejected) return classifiedQuery(rejected, empty);
|
|
410
|
+
const result = decodeObject(exchange.body);
|
|
411
|
+
const token = result?.NextToken ?? "";
|
|
412
|
+
if (!Array.isArray(result?.Statements) || result.Statements.length > 100 || typeof token !== "string" || Buffer.byteLength(token) > 4096)
|
|
413
|
+
return classifiedQuery(invalidResponse(operation, "Redshift returned invalid statement page"), empty);
|
|
414
|
+
const statements = [];
|
|
415
|
+
for (const statement of result.Statements) {
|
|
416
|
+
if (!isObject(statement) || !isStatementID(statement.Id) || !isStatementStatus(statement.Status))
|
|
417
|
+
return classifiedQuery(invalidResponse(operation, "Redshift returned invalid statement identity"), empty);
|
|
418
|
+
statements.push({ id: statement.Id, status: statement.Status, hasResultSet: false });
|
|
419
|
+
}
|
|
420
|
+
return newQueryBranch("listed", { statements, nextToken: token });
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
var CancelStatementOperation = class {
|
|
424
|
+
/** Bind the provider client. */
|
|
425
|
+
constructor(client) {
|
|
426
|
+
this.client = client;
|
|
427
|
+
}
|
|
428
|
+
client;
|
|
429
|
+
/** Return the generated cancellation contract. */
|
|
430
|
+
definition() {
|
|
431
|
+
return CancelStatementDefinition;
|
|
432
|
+
}
|
|
433
|
+
/** Identify one repeat-safe Dex cancellation call. */
|
|
434
|
+
idempotencyKey(callId) {
|
|
435
|
+
return callId;
|
|
436
|
+
}
|
|
437
|
+
/** Confirm cancellation or classify a statement that already finished or failed. */
|
|
438
|
+
async invoke(call, input) {
|
|
439
|
+
const operation = "cancelStatement";
|
|
440
|
+
const empty = { canceled: false };
|
|
441
|
+
if (!isStatementID(input.statementId))
|
|
442
|
+
return classifiedMutation(defect(operation, "Redshift statement ID is invalid"), empty);
|
|
443
|
+
const body = encodeRequest({ Id: input.statementId });
|
|
444
|
+
const exchange = await this.client.sendStatementRequest(call, "CancelStatement", body);
|
|
445
|
+
const rejected = classifyResponse(exchange, operation, call);
|
|
446
|
+
if (rejected) return classifiedMutation(rejected, empty);
|
|
447
|
+
const canceled = decodeObject(exchange.body)?.Status;
|
|
448
|
+
if (typeof canceled !== "boolean")
|
|
449
|
+
return newMutationRetry(operationFailure(operation, "PROTOCOL", "Redshift returned invalid cancellation status"));
|
|
450
|
+
if (canceled) return newMutationBranch("canceled", { canceled: true });
|
|
451
|
+
const confirmation = await this.client.sendStatementRequest(call, "DescribeStatement", body);
|
|
452
|
+
if (confirmation.error || confirmation.status !== 200)
|
|
453
|
+
return newMutationRetry(operationFailure(operation, "AVAILABILITY", "Redshift cancellation could not be confirmed"), confirmation.retryAfterMs);
|
|
454
|
+
const status = decodeObject(confirmation.body)?.Status;
|
|
455
|
+
if (!isStatementStatus(status))
|
|
456
|
+
return newMutationRetry(operationFailure(operation, "PROTOCOL", "Redshift cancellation status is unknown"));
|
|
457
|
+
if (status === "ABORTED") return newMutationBranch("canceled", { canceled: true });
|
|
458
|
+
if (status === "FINISHED" || status === "FAILED") return newMutationBranch("notCanceled", empty);
|
|
459
|
+
return newMutationRetry(operationFailure(operation, "AVAILABILITY", "Redshift statement is still running"));
|
|
460
|
+
}
|
|
461
|
+
};
|
|
462
|
+
var workgroupNamePattern = /^[a-z0-9-]{3,63}$/;
|
|
463
|
+
var statementStatuses = /* @__PURE__ */ new Set(["SUBMITTED", "PICKED", "STARTED", "FINISHED", "ABORTED", "FAILED"]);
|
|
464
|
+
var refusalStatuses = /* @__PURE__ */ new Set([400, 401, 403, 404, 411, 413, 414, 415, 422]);
|
|
465
|
+
function classifyResponse(exchange, operation, mutationCall) {
|
|
466
|
+
if (exchange.error === "credential") return defect(operation, "Redshift credentials are unavailable", "AUTHENTICATION");
|
|
467
|
+
if (exchange.error === "request") return { failure: operationFailure(operation, "TRANSPORT", "Redshift request failed") };
|
|
468
|
+
if (exchange.status === 429 || isThrottled(exchange.body))
|
|
469
|
+
return { failure: operationFailure(operation, "RATE_LIMIT", "Redshift rate limited the request"), delay: exchange.retryAfterMs };
|
|
470
|
+
if (exchange.status === 408 || exchange.status >= 500)
|
|
471
|
+
return { failure: operationFailure(operation, "AVAILABILITY", "Redshift is temporarily unavailable"), delay: exchange.retryAfterMs };
|
|
472
|
+
if (exchange.status !== 200) {
|
|
473
|
+
const definite = !mutationCall || isDefiniteRefusal(mutationCall, exchange);
|
|
474
|
+
const missing = exchange.status === 404 || exchange.status === 400 && hasErrorCode(exchange.body, "ResourceNotFoundException");
|
|
475
|
+
if (definite && missing && (operation === "describeStatement" || operation === "cancelStatement"))
|
|
476
|
+
return { branch: "missing" };
|
|
477
|
+
if (definite)
|
|
478
|
+
return { branch: "providerRejected", failure: operationFailure(operation, "PROVIDER_REJECTION", `Redshift rejected HTTP ${exchange.status}`) };
|
|
479
|
+
return { failure: operationFailure(operation, "TRANSPORT", "Redshift write outcome is unknown") };
|
|
480
|
+
}
|
|
481
|
+
if (exchange.error === "tooLarge") {
|
|
482
|
+
const failure2 = operationFailure(operation, "RESPONSE_TOO_LARGE", "Redshift response exceeded the configured limit");
|
|
483
|
+
return mutationCall ? { failure: failure2 } : { branch: "invalidResponse", failure: failure2 };
|
|
484
|
+
}
|
|
485
|
+
if (exchange.error) return { failure: operationFailure(operation, "TRANSPORT", "Redshift response was interrupted") };
|
|
486
|
+
return void 0;
|
|
487
|
+
}
|
|
488
|
+
function isDefiniteRefusal(call, exchange) {
|
|
489
|
+
if (call.context.attempt > 1 || !refusalStatuses.has(exchange.status)) return false;
|
|
490
|
+
const body = decodeObject(exchange.body);
|
|
491
|
+
if (!body) return false;
|
|
492
|
+
const fields = Object.fromEntries(Object.entries(body).map(([key, value]) => [key.toLowerCase(), value]));
|
|
493
|
+
const { message, id, status } = fields;
|
|
494
|
+
return typeof message === "string" && message.length > 0 && (id == null || id === "") && status == null;
|
|
495
|
+
}
|
|
496
|
+
function isStatementID(value) {
|
|
497
|
+
return typeof value === "string" && statementIDPattern.test(value);
|
|
498
|
+
}
|
|
499
|
+
function isStatementStatus(value) {
|
|
500
|
+
return typeof value === "string" && statementStatuses.has(value);
|
|
501
|
+
}
|
|
502
|
+
function decodeObject(body) {
|
|
503
|
+
try {
|
|
504
|
+
const value = JSON.parse(Buffer.from(body).toString("utf8"));
|
|
505
|
+
return isObject(value) ? value : void 0;
|
|
506
|
+
} catch {
|
|
507
|
+
return void 0;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
function encodeRequest(fields) {
|
|
511
|
+
return Buffer.from(JSON.stringify(fields).replace(/[<>&\u2028\u2029]/g, (character) => "\\u" + character.charCodeAt(0).toString(16).padStart(4, "0")));
|
|
512
|
+
}
|
|
513
|
+
function classifiedQuery(response, empty) {
|
|
514
|
+
if (response.branch !== void 0) return newQueryBranch(response.branch, empty, {}, response.failure);
|
|
515
|
+
return newQueryRetry(response.failure, response.delay);
|
|
516
|
+
}
|
|
517
|
+
function classifiedMutation(response, empty) {
|
|
518
|
+
if (response.branch !== void 0) return newMutationBranch(response.branch, empty, {}, response.failure);
|
|
519
|
+
return newMutationRetry(response.failure, response.delay);
|
|
520
|
+
}
|
|
521
|
+
function operationFailure(operation, kind, message) {
|
|
522
|
+
return { provider: "amazon-redshift", operation, kind, message };
|
|
523
|
+
}
|
|
524
|
+
function defect(operation, message, kind = "VALIDATION") {
|
|
525
|
+
return { branch: "defect", failure: operationFailure(operation, kind, message) };
|
|
526
|
+
}
|
|
527
|
+
function invalidResponse(operation, message) {
|
|
528
|
+
return { branch: "invalidResponse", failure: operationFailure(operation, "PROTOCOL", message) };
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
// src/project.ts
|
|
532
|
+
function newProjectConnection2(project, connectionName) {
|
|
533
|
+
return newProjectConnection(project, connectionName, (loadedProject, name) => {
|
|
534
|
+
const selected = loadedProject.configuration.connections.find((entry) => entry.connectorId === "amazon-redshift" && entry.connectionName === name);
|
|
535
|
+
if (!selected) throw new TypeError("Amazon Redshift project connection is not configured");
|
|
536
|
+
const fields = z.strictObject({ region: z.string().optional(), endpoint: z.string().optional(), timeout: z.union([z.string(), z.number().int()]).optional(), maxResponseBytes: z.number().int().optional() }).parse(selected.configuration);
|
|
537
|
+
const timeoutMs = typeof fields.timeout === "number" ? Math.ceil(fields.timeout / 1e6) : fields.timeout ? parseDurationMilliseconds(fields.timeout) : void 0;
|
|
538
|
+
const config = {
|
|
539
|
+
...fields.region ? { region: fields.region } : {},
|
|
540
|
+
...fields.endpoint ? { endpoint: fields.endpoint } : {},
|
|
541
|
+
...timeoutMs !== void 0 ? { timeoutMs } : {},
|
|
542
|
+
...fields.maxResponseBytes !== void 0 ? { maxResponseBytes: fields.maxResponseBytes } : {}
|
|
543
|
+
};
|
|
544
|
+
const credentials = {
|
|
545
|
+
async resolve(_call, signal) {
|
|
546
|
+
const privateFields = await loadedProject.resolveCredential("amazon-redshift", name, signal);
|
|
547
|
+
if (typeof privateFields.access_key_id !== "string" || typeof privateFields.secret_access_key !== "string") throw new TypeError("Amazon Redshift credentials are unavailable");
|
|
548
|
+
return { accessKeyId: new SecretString2(privateFields.access_key_id), secretAccessKey: new SecretString2(privateFields.secret_access_key), sessionToken: new SecretString2(typeof privateFields.session_token === "string" ? privateFields.session_token : "") };
|
|
549
|
+
}
|
|
550
|
+
};
|
|
551
|
+
return new AmazonRedshiftClient(config, credentials);
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
function parseDurationMilliseconds(value) {
|
|
555
|
+
let remaining = value;
|
|
556
|
+
let milliseconds = 0;
|
|
557
|
+
const units = { ns: 1e-6, us: 1e-3, "\xB5s": 1e-3, ms: 1, s: 1e3, m: 6e4, h: 36e5 };
|
|
558
|
+
while (remaining) {
|
|
559
|
+
const part = /^(\d+(?:\.\d+)?)(ns|us|µs|ms|s|m|h)/.exec(remaining);
|
|
560
|
+
if (!part) throw new TypeError("Amazon Redshift timeout must be a positive Go duration");
|
|
561
|
+
milliseconds += Number(part[1]) * (units[part[2] ?? ""] ?? NaN);
|
|
562
|
+
remaining = remaining.slice(part[0].length);
|
|
563
|
+
}
|
|
564
|
+
if (!Number.isSafeInteger(milliseconds) || milliseconds <= 0) throw new TypeError("Amazon Redshift timeout must resolve to positive whole milliseconds");
|
|
565
|
+
return milliseconds;
|
|
566
|
+
}
|
|
567
|
+
export {
|
|
568
|
+
AmazonRedshiftClient,
|
|
569
|
+
CancelStatementBranch,
|
|
570
|
+
CancelStatementBranchCanceled,
|
|
571
|
+
CancelStatementBranchDefect,
|
|
572
|
+
CancelStatementBranchInvalidResponse,
|
|
573
|
+
CancelStatementBranchMissing,
|
|
574
|
+
CancelStatementBranchNotCanceled,
|
|
575
|
+
CancelStatementBranchProviderRejected,
|
|
576
|
+
CancelStatementDefinition,
|
|
577
|
+
CancelStatementInputSchema,
|
|
578
|
+
CancelStatementMetadata,
|
|
579
|
+
CancelStatementOperation,
|
|
580
|
+
ConfigSchema,
|
|
581
|
+
Connection,
|
|
582
|
+
ConnectorID,
|
|
583
|
+
CredentialMaterialError,
|
|
584
|
+
DescribeStatementBranch,
|
|
585
|
+
DescribeStatementBranchDefect,
|
|
586
|
+
DescribeStatementBranchDescribed,
|
|
587
|
+
DescribeStatementBranchInvalidResponse,
|
|
588
|
+
DescribeStatementBranchMissing,
|
|
589
|
+
DescribeStatementBranchProviderRejected,
|
|
590
|
+
DescribeStatementDefinition,
|
|
591
|
+
DescribeStatementInputSchema,
|
|
592
|
+
DescribeStatementMetadata,
|
|
593
|
+
DescribeStatementOperation,
|
|
594
|
+
ExecuteStatementBranch,
|
|
595
|
+
ExecuteStatementBranchDefect,
|
|
596
|
+
ExecuteStatementBranchInvalidResponse,
|
|
597
|
+
ExecuteStatementBranchProviderRejected,
|
|
598
|
+
ExecuteStatementBranchSubmitted,
|
|
599
|
+
ExecuteStatementDefinition,
|
|
600
|
+
ExecuteStatementInputSchema,
|
|
601
|
+
ExecuteStatementMetadata,
|
|
602
|
+
ExecuteStatementOperation,
|
|
603
|
+
GetStatementResultBranch,
|
|
604
|
+
GetStatementResultBranchDefect,
|
|
605
|
+
GetStatementResultBranchFetched,
|
|
606
|
+
GetStatementResultBranchInvalidResponse,
|
|
607
|
+
GetStatementResultBranchMissing,
|
|
608
|
+
GetStatementResultBranchProviderRejected,
|
|
609
|
+
GetStatementResultDefinition,
|
|
610
|
+
GetStatementResultInputSchema,
|
|
611
|
+
GetStatementResultMetadata,
|
|
612
|
+
GetStatementResultOperation,
|
|
613
|
+
ListStatementsBranch,
|
|
614
|
+
ListStatementsBranchDefect,
|
|
615
|
+
ListStatementsBranchInvalidResponse,
|
|
616
|
+
ListStatementsBranchListed,
|
|
617
|
+
ListStatementsBranchProviderRejected,
|
|
618
|
+
ListStatementsDefinition,
|
|
619
|
+
ListStatementsInputSchema,
|
|
620
|
+
ListStatementsMetadata,
|
|
621
|
+
ListStatementsOperation,
|
|
622
|
+
ProviderID,
|
|
623
|
+
StatementCancellationSchema,
|
|
624
|
+
StatementIdentitySchema,
|
|
625
|
+
StatementPageSchema,
|
|
626
|
+
StatementResultPageSchema,
|
|
627
|
+
StatementStatusSchema,
|
|
628
|
+
decodeCredentials,
|
|
629
|
+
isCredentialMaterialError,
|
|
630
|
+
newCancelStatementStep,
|
|
631
|
+
newConnection,
|
|
632
|
+
newConnectionWithOperations,
|
|
633
|
+
newDescribeStatementStep,
|
|
634
|
+
newExecuteStatementStep,
|
|
635
|
+
newGetStatementResultStep,
|
|
636
|
+
newListStatementsStep,
|
|
637
|
+
newProjectConnection2 as newProjectConnection
|
|
638
|
+
};
|