Skip to content

Commit 059c29b

Browse files
committed
chore: remove luxon for native dates
1 parent df2903b commit 059c29b

File tree

11 files changed

+50
-43
lines changed

11 files changed

+50
-43
lines changed

package-lock.json

Lines changed: 0 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@
3434
},
3535
"homepage": "https://github.com/cino/aws-lambda-stubs#readme",
3636
"private": false,
37-
"dependencies": {
38-
"luxon": "^3.7.2"
39-
},
4037
"devDependencies": {
4138
"@biomejs/biome": "2.3.1",
4239
"@commitlint/cli": "^20.1.0",

src/api-gateway-proxy.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ import type {
1313
APIGatewayProxyWithCognitoAuthorizerEvent,
1414
APIGatewayProxyWithLambdaAuthorizerEvent,
1515
} from 'aws-lambda';
16-
import { DateTime } from 'luxon';
1716
import type { Merge } from 'type-fest';
1817
import {
1918
APIGatewayEventRequestContextV2Stub,
2019
APIGatewayEventRequestContextV2WithAuthorizerStub,
2120
APIGatewayEventRequestContextWithAuthorizerStub,
2221
DEFAULT_REGION,
22+
eventParsedDateTime,
2323
type PartialAPIGatewayEventRequestContext,
2424
type PartialAPIGatewayEventRequestContextV2,
2525
randomIpAddress,
2626
} from './common';
27-
import { deepMerge } from './utils';
27+
import { currentEpochTime, deepMerge } from './utils';
2828

2929
// V1
3030

@@ -181,7 +181,7 @@ type PartialAPIGatewayProxyWebsocketEventV2 = Merge<
181181
export const APIGatewayProxyWebsocketEventV2Stub = (
182182
overrides: PartialAPIGatewayProxyWebsocketEventV2 = {}
183183
): APIGatewayProxyWebsocketEventV2 => {
184-
const now = DateTime.now();
184+
const currentEpoch = currentEpochTime();
185185

186186
return deepMerge<APIGatewayProxyWebsocketEventV2>(
187187
{
@@ -192,9 +192,9 @@ export const APIGatewayProxyWebsocketEventV2Stub = (
192192
extendedRequestId: crypto.randomUUID(),
193193
messageDirection: 'IN',
194194
stage: 'prod',
195-
connectedAt: now.toUnixInteger(),
196-
requestTime: now.toFormat('dd/MMM/yyyy:HH:mm:ss ZZZ'),
197-
requestTimeEpoch: now.toUnixInteger(),
195+
connectedAt: currentEpoch,
196+
requestTime: eventParsedDateTime(new Date()),
197+
requestTimeEpoch: currentEpoch,
198198
requestId: crypto.randomUUID(),
199199
domainName: `id.execute-api.${DEFAULT_REGION}.amazonaws.com`,
200200
connectionId: crypto.randomUUID(),

src/cloudwatch-logs.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type { CloudWatchLogsDecodedData, CloudWatchLogsEvent, CloudWatchLogsLogEvent } from 'aws-lambda';
2-
import { DateTime } from 'luxon';
32
import { DEFAULT_ACCOUNT_ID } from './common';
4-
import { deepMerge } from './utils';
3+
import { currentEpochTime, deepMerge } from './utils';
54

65
export const CloudWatchLogsEventStub = (overrides: Partial<CloudWatchLogsDecodedData> = {}): CloudWatchLogsEvent => {
6+
const currentEpoch = currentEpochTime();
7+
78
return {
89
awslogs: {
910
data: Buffer.from(
@@ -18,12 +19,12 @@ export const CloudWatchLogsEventStub = (overrides: Partial<CloudWatchLogsDecoded
1819
logEvents: [
1920
{
2021
id: 'eventId1',
21-
timestamp: DateTime.now().toUnixInteger(),
22+
timestamp: currentEpoch,
2223
message: 'This is a sample log message 1',
2324
},
2425
{
2526
id: 'eventId2',
26-
timestamp: DateTime.now().toUnixInteger(),
27+
timestamp: currentEpoch,
2728
message: 'This is a sample log message 2',
2829
},
2930
],
@@ -39,7 +40,7 @@ export const CloudWatchLogsEventStub = (overrides: Partial<CloudWatchLogsDecoded
3940
export const CloudWatchLogsLogEventStub = (overrides: Partial<CloudWatchLogsLogEvent> = {}): CloudWatchLogsLogEvent => {
4041
return {
4142
id: 'eventId1',
42-
timestamp: DateTime.now().toUnixInteger(),
43+
timestamp: currentEpochTime(),
4344
message: 'This is a sample log message',
4445

4546
...overrides,

src/common/api-gateway.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import type {
33
APIGatewayEventRequestContextV2,
44
APIGatewayEventRequestContextWithAuthorizer,
55
} from 'aws-lambda';
6-
import { DateTime } from 'luxon';
76
import type { Merge } from 'type-fest';
8-
import { deepMerge, randomIpAddress } from '../utils';
7+
import { currentEpochTime, deepMerge, randomIpAddress } from '../utils';
98
import { DEFAULT_ACCOUNT_ID, DEFAULT_REGION } from './consts';
109

1110
export type PartialAPIGatewayEventRequestContext<TAuthorizer> = Merge<
@@ -16,6 +15,26 @@ export type PartialAPIGatewayEventRequestContext<TAuthorizer> = Merge<
1615
}
1716
>;
1817

18+
export const eventParsedDateTime = (date: Date): string => {
19+
const day = String(date.getDate()).padStart(2, '0');
20+
const month = date.toLocaleString('en-US', { month: 'short' });
21+
const year = date.getFullYear();
22+
const hour = String(date.getHours());
23+
const minute = String(date.getMinutes());
24+
const second = String(date.getSeconds());
25+
26+
const offset = -date.getTimezoneOffset(); // Reverse the sign
27+
28+
const sign = offset >= 0 ? '+' : '-';
29+
const absOffset = Math.abs(offset);
30+
const hours = String(Math.floor(absOffset / 60)).padStart(2, '0');
31+
const minutes = String(absOffset % 60).padStart(2, '0');
32+
33+
const formattedTz = `${sign}${hours}${minutes}`;
34+
35+
return `${day}/${month}/${year}:${hour}:${minute}:${second} ${formattedTz}`;
36+
};
37+
1938
export const APIGatewayEventRequestContextWithAuthorizerStub = <TAuthorizer>(
2039
overrides: PartialAPIGatewayEventRequestContext<TAuthorizer> = {
2140
authorizer: undefined,
@@ -31,7 +50,7 @@ export const APIGatewayEventRequestContextWithAuthorizerStub = <TAuthorizer>(
3150
path: '/prod/resource',
3251
stage: 'prod',
3352
requestId: crypto.randomUUID(),
34-
requestTimeEpoch: DateTime.now().toUnixInteger(),
53+
requestTimeEpoch: currentEpochTime(),
3554
resourceId: 'resource-id',
3655
resourcePath: '/resource',
3756
authorizer: undefined as TAuthorizer, // Will be overridden by deepMerge
@@ -54,7 +73,6 @@ export type PartialAPIGatewayEventRequestContextV2 = Merge<
5473
export const APIGatewayEventRequestContextV2Stub = (
5574
overrides: PartialAPIGatewayEventRequestContextV2 = {}
5675
): APIGatewayEventRequestContextV2 => {
57-
const dateTime = DateTime.now();
5876

5977
return deepMerge(
6078
{
@@ -72,8 +90,8 @@ export const APIGatewayEventRequestContextV2Stub = (
7290
requestId: crypto.randomUUID(),
7391
routeKey: '$default',
7492
stage: 'prod',
75-
time: dateTime.toFormat('dd/MMM/yyyy:HH:mm:ss ZZZ'),
76-
timeEpoch: dateTime.toUnixInteger(),
93+
time: eventParsedDateTime(new Date()),
94+
timeEpoch: currentEpochTime(),
7795
},
7896
overrides as Partial<APIGatewayEventRequestContextV2>
7997
);
@@ -92,7 +110,7 @@ export const APIGatewayEventRequestContextV2WithAuthorizerStub = <TAuthorizer>(
92110
>(
93111
{
94112
...APIGatewayEventRequestContextV2Stub(overrides),
95-
authorizer: undefined as TAuthorizer, // will be overwrtitten by deepMerge
113+
authorizer: undefined as TAuthorizer, // will be overwritten by deepMerge
96114
},
97115
{ authorizer }
98116
);

src/kinesis-firehose-transformation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { FirehoseTransformationEvent, FirehoseTransformationEventRecord } from 'aws-lambda';
2-
import { DateTime } from 'luxon';
32
import { DEFAULT_ACCOUNT_ID, DEFAULT_REGION } from './common';
3+
import { currentEpochTime } from './utils';
44

55
const FireHoseTransformationEventRecordStub = (
66
overrides: Partial<FirehoseTransformationEventRecord> = {}
77
): FirehoseTransformationEventRecord => {
88
return {
99
recordId: 'record-id-123',
10-
approximateArrivalTimestamp: DateTime.now().toUnixInteger(),
10+
approximateArrivalTimestamp: currentEpochTime(),
1111
data: 'SGVsbG8sIHRoaXMgaXMgYSB0ZXN0IG1lc3NhZ2Uh', // "Hello, this is a test message!" in base64
1212
...overrides,
1313
};

src/msk.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { MSKEvent } from 'aws-lambda';
2-
import { DateTime } from 'luxon';
32
import { DEFAULT_ACCOUNT_ID, DEFAULT_REGION } from './common';
3+
import { currentEpochTime } from './utils';
44

55
export const MSKEventStub = (overrides: Partial<MSKEvent> = {}): MSKEvent => {
66
return {
@@ -13,7 +13,7 @@ export const MSKEventStub = (overrides: Partial<MSKEvent> = {}): MSKEvent => {
1313
topic: 'test-topic',
1414
partition: 0,
1515
offset: 123,
16-
timestamp: DateTime.now().toUnixInteger(),
16+
timestamp: currentEpochTime(),
1717
timestampType: 'CREATE_TIME',
1818
key: 'abcDEFghiJKLmnoPQRstuVWXyz1234==',
1919
value: 'SGVsbG8sIHRoaXMgaXMgYSB0ZXN0Lg==',

src/self-managed-kafka.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { SelfManagedKafkaEvent } from 'aws-lambda';
2-
import { DateTime } from 'luxon';
32
import { DEFAULT_REGION } from './common';
3+
import { currentEpochTime } from './utils';
44

55
export const SelfManagedKafkaStub = (overrides: Partial<SelfManagedKafkaEvent> = {}): SelfManagedKafkaEvent => {
66
return {
@@ -12,7 +12,7 @@ export const SelfManagedKafkaStub = (overrides: Partial<SelfManagedKafkaEvent> =
1212
topic: 'test-topic',
1313
partition: 0,
1414
offset: 123,
15-
timestamp: DateTime.now().toUnixInteger(),
15+
timestamp: currentEpochTime(),
1616
timestampType: 'CREATE_TIME',
1717
key: 'abcDEFghiJKLmnoPQRstuVWXyz1234==',
1818
value: 'SGVsbG8sIHRoaXMgaXMgYSB0ZXN0Lg==',

src/sqs.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import crypto from 'node:crypto';
22
import type { SQSEvent, SQSRecord } from 'aws-lambda';
3-
import { DateTime } from 'luxon';
43
import { DEFAULT_ACCOUNT_ID, DEFAULT_REGION } from './common';
54
import { deepMerge } from './utils';
65

@@ -14,7 +13,7 @@ interface PartialSQSRecord extends Omit<Partial<SQSRecord>, omittedKeys> {
1413

1514
const SQSRecordStub = (overrides: PartialSQSRecord = {}): SQSRecord => {
1615
const region = overrides.awsRegion ?? DEFAULT_REGION;
17-
const now = DateTime.now();
16+
const nowIso = new Date().toISOString();
1817

1918
const body = overrides.body || { key: 'value' };
2019
const stringifiedBody = JSON.stringify(body);
@@ -27,9 +26,9 @@ const SQSRecordStub = (overrides: PartialSQSRecord = {}): SQSRecord => {
2726
body: stringifiedBody,
2827
attributes: {
2928
ApproximateReceiveCount: '1',
30-
SentTimestamp: now.toISO(),
29+
SentTimestamp: nowIso,
3130
SenderId: 'sender-id',
32-
ApproximateFirstReceiveTimestamp: now.toISO(),
31+
ApproximateFirstReceiveTimestamp: nowIso,
3332
},
3433
messageAttributes: {},
3534
md5OfBody: crypto.createHash('md5').update(stringifiedBody).digest('hex'),

src/utils/date.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const currentEpochTime = (): number => {
2+
return Math.floor(Date.now() / 1000);
3+
};

0 commit comments

Comments
 (0)