@awsless/dynamodb-server 0.0.1

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,24 @@
1
+ import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
2
+ import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb';
3
+
4
+ declare class DynamoDBServer {
5
+ private region;
6
+ private client?;
7
+ private documentClient?;
8
+ private endpoint;
9
+ private process;
10
+ constructor(region?: string);
11
+ listen(port: number): Promise<void>;
12
+ /** Kill the DynamoDB server. */
13
+ kill(): Promise<void>;
14
+ /** Ping the DynamoDB server if its ready. */
15
+ ping(): Promise<boolean>;
16
+ /** Ping the DynamoDB server untill its ready. */
17
+ wait(times?: number): Promise<void>;
18
+ /** Get DynamoDBClient connected to dynamodb local. */
19
+ getClient(): DynamoDBClient;
20
+ /** Get DynamoDBDocumentClient connected to dynamodb local. */
21
+ getDocumentClient(): DynamoDBDocumentClient;
22
+ }
23
+
24
+ export { DynamoDBServer };
package/dist/index.js ADDED
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ DynamoDBServer: () => DynamoDBServer
34
+ });
35
+ module.exports = __toCommonJS(src_exports);
36
+ var import_client_dynamodb = require("@aws-sdk/client-dynamodb");
37
+ var import_lib_dynamodb = require("@aws-sdk/lib-dynamodb");
38
+ var import_url_parser = require("@aws-sdk/url-parser");
39
+ var import_sleep_await = require("sleep-await");
40
+ var import_dynamo_db_local = __toESM(require("dynamo-db-local"));
41
+ var DynamoDBServer = class {
42
+ constructor(region = "us-east-1") {
43
+ this.region = region;
44
+ this.endpoint = (0, import_url_parser.parseUrl)(`http://localhost`);
45
+ }
46
+ client;
47
+ documentClient;
48
+ endpoint;
49
+ process;
50
+ async listen(port) {
51
+ if (this.process) {
52
+ throw new Error(`DynamoDB server is already listening on port: ${this.endpoint.port}`);
53
+ }
54
+ if (port < 0 || port >= 65536) {
55
+ throw new RangeError(`Port should be >= 0 and < 65536. Received ${port}.`);
56
+ }
57
+ this.endpoint.port = port;
58
+ this.process = await import_dynamo_db_local.default.spawn({ port });
59
+ }
60
+ /** Kill the DynamoDB server. */
61
+ async kill() {
62
+ if (this.process) {
63
+ await this.process.kill();
64
+ this.process = void 0;
65
+ }
66
+ }
67
+ /** Ping the DynamoDB server if its ready. */
68
+ async ping() {
69
+ const client = this.getClient();
70
+ const command = new import_client_dynamodb.ListTablesCommand({});
71
+ try {
72
+ const response = await client.send(command);
73
+ return Array.isArray(response.TableNames);
74
+ } catch (error) {
75
+ return false;
76
+ }
77
+ }
78
+ /** Ping the DynamoDB server untill its ready. */
79
+ async wait(times = 10) {
80
+ while (times--) {
81
+ if (await this.ping()) {
82
+ return;
83
+ }
84
+ await (0, import_sleep_await.sleepAwait)(100 * times);
85
+ }
86
+ throw new Error("DynamoDB server is unavailable");
87
+ }
88
+ /** Get DynamoDBClient connected to dynamodb local. */
89
+ getClient() {
90
+ if (!this.client) {
91
+ this.client = new import_client_dynamodb.DynamoDBClient({
92
+ maxAttempts: 10,
93
+ endpoint: this.endpoint,
94
+ region: this.region,
95
+ tls: false,
96
+ credentials: {
97
+ accessKeyId: "fake",
98
+ secretAccessKey: "fake"
99
+ }
100
+ });
101
+ }
102
+ return this.client;
103
+ }
104
+ /** Get DynamoDBDocumentClient connected to dynamodb local. */
105
+ getDocumentClient() {
106
+ if (!this.documentClient) {
107
+ this.documentClient = import_lib_dynamodb.DynamoDBDocumentClient.from(this.getClient(), {
108
+ marshallOptions: {
109
+ removeUndefinedValues: true
110
+ }
111
+ });
112
+ }
113
+ return this.documentClient;
114
+ }
115
+ };
116
+ // Annotate the CommonJS export names for ESM import in node:
117
+ 0 && (module.exports = {
118
+ DynamoDBServer
119
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,84 @@
1
+ // src/index.ts
2
+ import { DynamoDBClient, ListTablesCommand } from "@aws-sdk/client-dynamodb";
3
+ import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
4
+ import { parseUrl } from "@aws-sdk/url-parser";
5
+ import { sleepAwait } from "sleep-await";
6
+ import dynamoDbLocal from "dynamo-db-local";
7
+ var DynamoDBServer = class {
8
+ constructor(region = "us-east-1") {
9
+ this.region = region;
10
+ this.endpoint = parseUrl(`http://localhost`);
11
+ }
12
+ client;
13
+ documentClient;
14
+ endpoint;
15
+ process;
16
+ async listen(port) {
17
+ if (this.process) {
18
+ throw new Error(`DynamoDB server is already listening on port: ${this.endpoint.port}`);
19
+ }
20
+ if (port < 0 || port >= 65536) {
21
+ throw new RangeError(`Port should be >= 0 and < 65536. Received ${port}.`);
22
+ }
23
+ this.endpoint.port = port;
24
+ this.process = await dynamoDbLocal.spawn({ port });
25
+ }
26
+ /** Kill the DynamoDB server. */
27
+ async kill() {
28
+ if (this.process) {
29
+ await this.process.kill();
30
+ this.process = void 0;
31
+ }
32
+ }
33
+ /** Ping the DynamoDB server if its ready. */
34
+ async ping() {
35
+ const client = this.getClient();
36
+ const command = new ListTablesCommand({});
37
+ try {
38
+ const response = await client.send(command);
39
+ return Array.isArray(response.TableNames);
40
+ } catch (error) {
41
+ return false;
42
+ }
43
+ }
44
+ /** Ping the DynamoDB server untill its ready. */
45
+ async wait(times = 10) {
46
+ while (times--) {
47
+ if (await this.ping()) {
48
+ return;
49
+ }
50
+ await sleepAwait(100 * times);
51
+ }
52
+ throw new Error("DynamoDB server is unavailable");
53
+ }
54
+ /** Get DynamoDBClient connected to dynamodb local. */
55
+ getClient() {
56
+ if (!this.client) {
57
+ this.client = new DynamoDBClient({
58
+ maxAttempts: 10,
59
+ endpoint: this.endpoint,
60
+ region: this.region,
61
+ tls: false,
62
+ credentials: {
63
+ accessKeyId: "fake",
64
+ secretAccessKey: "fake"
65
+ }
66
+ });
67
+ }
68
+ return this.client;
69
+ }
70
+ /** Get DynamoDBDocumentClient connected to dynamodb local. */
71
+ getDocumentClient() {
72
+ if (!this.documentClient) {
73
+ this.documentClient = DynamoDBDocumentClient.from(this.getClient(), {
74
+ marshallOptions: {
75
+ removeUndefinedValues: true
76
+ }
77
+ });
78
+ }
79
+ return this.documentClient;
80
+ }
81
+ };
82
+ export {
83
+ DynamoDBServer
84
+ };
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@awsless/dynamodb-server",
3
+ "version": "0.0.1",
4
+ "license": "MIT",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/awsless/awsless.git"
14
+ },
15
+ "bugs": {
16
+ "url": "https://github.com/awsless/awsless/issues"
17
+ },
18
+ "dependencies": {
19
+ "@aws-sdk/client-dynamodb": "3.272.0",
20
+ "@aws-sdk/lib-dynamodb": "3.258.0",
21
+ "@aws-sdk/smithy-client": "3.272.0",
22
+ "@aws-sdk/url-parser": "^3.272.0",
23
+ "dynamo-db-local": "^4.1.3",
24
+ "sleep-await": "^1.0.2"
25
+ },
26
+ "scripts": {
27
+ "test": "pnpm code test",
28
+ "build": "pnpm tsup src/index.ts --format cjs,esm --dts --clean --external vitest",
29
+ "prepublish": "pnpm test; pnpm build"
30
+ }
31
+ }