@saidsef/aws-kinesis-local 1.5.0 → 1.6.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.
@@ -0,0 +1,28 @@
1
+ export interface Credentials {
2
+ accessKeyId: string;
3
+ secretAccessKey: string;
4
+ }
5
+ export interface S3Client {
6
+ endpoint: string;
7
+ region: string;
8
+ credentials: Credentials;
9
+ }
10
+ export interface S3Object {
11
+ bucket: string;
12
+ key: string;
13
+ body: Buffer | string;
14
+ headers?: Record<string, string>;
15
+ }
16
+ export interface SignableRequest {
17
+ method: string;
18
+ path: string;
19
+ query?: Record<string, string>;
20
+ headers: Record<string, string>;
21
+ }
22
+ export declare class S3Error extends Error {
23
+ readonly statusCode: number;
24
+ readonly code: string | undefined;
25
+ constructor(statusCode: number, code: string | undefined, message: string);
26
+ }
27
+ export declare function authorization(request: SignableRequest, region: string, credentials: Credentials): string;
28
+ export declare function putObject(client: S3Client, object: S3Object): Promise<void>;
@@ -0,0 +1,108 @@
1
+ import { createHash, createHmac } from 'node:crypto';
2
+ import { request as httpRequest } from 'node:http';
3
+ import { request as httpsRequest } from 'node:https';
4
+ export class S3Error extends Error {
5
+ statusCode;
6
+ code;
7
+ constructor(statusCode, code, message) {
8
+ super(message);
9
+ this.name = 'S3Error';
10
+ this.statusCode = statusCode;
11
+ this.code = code;
12
+ }
13
+ }
14
+ function uriEncode(value) {
15
+ return encodeURIComponent(value).replace(/[!'()*]/g, char => `%${char.charCodeAt(0).toString(16).toUpperCase()}`);
16
+ }
17
+ function encodePath(path) {
18
+ return path.split('/').map(uriEncode).join('/');
19
+ }
20
+ function sha256(data) {
21
+ return createHash('sha256').update(data).digest('hex');
22
+ }
23
+ function hmac(key, data) {
24
+ return createHmac('sha256', key).update(data).digest();
25
+ }
26
+ function byName(a, b) {
27
+ return a.name < b.name ? -1 : a.name > b.name ? 1 : 0;
28
+ }
29
+ function canonicalQuery(query) {
30
+ return Object.entries(query)
31
+ .map(([name, value]) => ({ name: uriEncode(name), value: uriEncode(value) }))
32
+ .sort(byName)
33
+ .map(({ name, value }) => `${name}=${value}`)
34
+ .join('&');
35
+ }
36
+ function canonicalHeaders(headers) {
37
+ return Object.entries(headers)
38
+ .map(([name, value]) => ({ name: name.toLowerCase(), value: value.trim().replace(/\s+/g, ' ') }))
39
+ .sort(byName);
40
+ }
41
+ function headerValue(headers, name) {
42
+ const value = headers.find(header => header.name === name)?.value;
43
+ if (value === undefined)
44
+ throw new Error(`A signed S3 request needs the ${name} header.`);
45
+ return value;
46
+ }
47
+ export function authorization(request, region, credentials) {
48
+ const headers = canonicalHeaders(request.headers);
49
+ const timestamp = headerValue(headers, 'x-amz-date');
50
+ const date = timestamp.slice(0, 8);
51
+ const scope = `${date}/${region}/s3/aws4_request`;
52
+ const signedHeaders = headers.map(({ name }) => name).join(';');
53
+ const canonicalRequest = [
54
+ request.method,
55
+ encodePath(request.path),
56
+ canonicalQuery(request.query ?? {}),
57
+ headers.map(({ name, value }) => `${name}:${value}\n`).join(''),
58
+ signedHeaders,
59
+ headerValue(headers, 'x-amz-content-sha256'),
60
+ ].join('\n');
61
+ const stringToSign = ['AWS4-HMAC-SHA256', timestamp, scope, sha256(canonicalRequest)].join('\n');
62
+ const signingKey = [date, region, 's3', 'aws4_request'].reduce(hmac, `AWS4${credentials.secretAccessKey}`);
63
+ const signature = createHmac('sha256', signingKey).update(stringToSign).digest('hex');
64
+ return `AWS4-HMAC-SHA256 Credential=${credentials.accessKeyId}/${scope},SignedHeaders=${signedHeaders},Signature=${signature}`;
65
+ }
66
+ function xmlElement(xml, name) {
67
+ return xml.match(new RegExp(`<${name}>([^<]*)</${name}>`))?.[1];
68
+ }
69
+ function uploadError(statusCode, body, object) {
70
+ const code = xmlElement(body, 'Code');
71
+ const detail = xmlElement(body, 'Message');
72
+ let message = `PutObject ${object.bucket}/${object.key} failed with status ${statusCode}`;
73
+ if (code)
74
+ message += ` and error code ${code}`;
75
+ if (detail)
76
+ message += `: ${detail}`;
77
+ return new S3Error(statusCode, code, message);
78
+ }
79
+ export function putObject(client, object) {
80
+ const url = new URL(client.endpoint);
81
+ const path = `${decodeURIComponent(url.pathname).replace(/\/+$/, '')}/${object.bucket}/${object.key}`;
82
+ const body = typeof object.body === 'string' ? Buffer.from(object.body) : object.body;
83
+ const headers = {
84
+ ...object.headers,
85
+ host: url.host,
86
+ 'content-length': String(body.length),
87
+ 'x-amz-content-sha256': sha256(body),
88
+ 'x-amz-date': new Date().toISOString().replace(/[-:]|\.\d{3}/g, ''),
89
+ };
90
+ headers.authorization = authorization({ method: 'PUT', path, headers }, client.region, client.credentials);
91
+ const send = url.protocol === 'https:' ? httpsRequest : httpRequest;
92
+ return new Promise((resolve, reject) => {
93
+ const req = send(url, { method: 'PUT', path: encodePath(path), headers }, res => {
94
+ const chunks = [];
95
+ res.on('data', chunk => chunks.push(chunk));
96
+ res.on('error', reject);
97
+ res.on('end', () => {
98
+ const statusCode = res.statusCode ?? 0;
99
+ if (statusCode >= 200 && statusCode < 300)
100
+ resolve();
101
+ else
102
+ reject(uploadError(statusCode, Buffer.concat(chunks).toString('utf8'), object));
103
+ });
104
+ });
105
+ req.on('error', reject);
106
+ req.end(body);
107
+ });
108
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saidsef/aws-kinesis-local",
3
- "version": "1.5.0",
3
+ "version": "1.6.0",
4
4
  "description": "Local Amazon Kinesis Data Streams server for development and CI, with all 45 operations, enhanced fan-out, JSON and CBOR over HTTP/1.1 and HTTP/2",
5
5
  "keywords": [
6
6
  "aws",
@@ -60,11 +60,11 @@
60
60
  "start": "node src/cli.ts"
61
61
  },
62
62
  "devDependencies": {
63
- "@aws-sdk/client-kinesis": "3.1165.0",
63
+ "@aws-sdk/client-kinesis": "3.1170.0",
64
64
  "@smithy/eventstream-codec": "4.5.2",
65
65
  "@smithy/node-http-handler": "4.14.2",
66
66
  "@types/node": "26.6.6",
67
- "cbor2": "2.4.1",
67
+ "cbor2": "2.4.2",
68
68
  "typescript": "7.0.2"
69
69
  }
70
70
  }