@anzar-auth/anzar 0.2.5

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/README.md ADDED
@@ -0,0 +1,79 @@
1
+ ## Anzar CLI
2
+
3
+ Anzar is a lightweight authentication and authorization framework designed to run as a separate microservice. It provides tools to initialize, configure, and manage your project’s authentication system with ease.
4
+
5
+ ## Features
6
+ * Lightweight and modular
7
+ * Easy setup for any project
8
+ * Database schema generation and migration support
9
+ * Service status monitoring
10
+ * Simple CLI commands for developers
11
+
12
+ ---
13
+
14
+ ## Installation
15
+ Install Anzar globally using npm (or your preferred package manager):
16
+ ```bash
17
+ npm install -g anzar
18
+ ```
19
+
20
+ ---
21
+
22
+ ## Usage
23
+ Anzar provides a command-line interface to manage your authentication service.
24
+ ```bash
25
+ anzar <COMMAND> [OPTIONS]
26
+ ```
27
+
28
+ ### Commands
29
+
30
+ | Command | Description |
31
+ |---------------|---------------------------------------------------|
32
+ | `init` | Initialize Anzar for your project |
33
+ | `check` | Check current configuration and setup |
34
+ | `status` | Show Anzar service status |
35
+ | `generate` | Generate database schemas |
36
+ | `migrate` | Apply database migrations |
37
+ | `help` | Print this message or the help of a subcommand |
38
+
39
+ ### Options
40
+ | Option | Description |
41
+ |------------------|---------------------------------------------------|
42
+ | `-h, --help` | Print help |
43
+ | `-V, --version` | Print version |
44
+
45
+ ### Examples
46
+ Initialize a new Anzar project:
47
+ ```bash
48
+ anzar init
49
+ ```
50
+
51
+ Check your current configuration:
52
+ ```bash
53
+ anzar check
54
+ ```
55
+
56
+ Generate database schemas:
57
+ ```bash
58
+ anzar generate
59
+ ```
60
+ Apply pending migrations:
61
+ ```bash
62
+ anzar migrate
63
+ ```
64
+
65
+ Show the current service status:
66
+ ```bash
67
+ anzar status
68
+ ```
69
+
70
+ Get help for a specific command:
71
+ ```bash
72
+ anzar help migrate
73
+ ```
74
+
75
+
76
+
77
+
78
+
79
+
@@ -0,0 +1,212 @@
1
+ const { createWriteStream, existsSync, mkdirSync, mkdtemp } = require("fs");
2
+ const { join, sep } = require("path");
3
+ const { spawnSync } = require("child_process");
4
+ const { tmpdir } = require("os");
5
+
6
+ const axios = require("axios");
7
+ const rimraf = require("rimraf");
8
+ const tmpDir = tmpdir();
9
+
10
+ const error = (msg) => {
11
+ console.error(msg);
12
+ process.exit(1);
13
+ };
14
+
15
+ class Package {
16
+ constructor(platform, name, url, filename, zipExt, binaries) {
17
+ let errors = [];
18
+ if (typeof url !== "string") {
19
+ errors.push("url must be a string");
20
+ } else {
21
+ try {
22
+ new URL(url);
23
+ } catch (e) {
24
+ errors.push(e);
25
+ }
26
+ }
27
+ if (name && typeof name !== "string") {
28
+ errors.push("package name must be a string");
29
+ }
30
+ if (!name) {
31
+ errors.push("You must specify the name of your package");
32
+ }
33
+ if (binaries && typeof binaries !== "object") {
34
+ errors.push("binaries must be a string => string map");
35
+ }
36
+ if (!binaries) {
37
+ errors.push("You must specify the binaries in the package");
38
+ }
39
+
40
+ if (errors.length > 0) {
41
+ let errorMsg =
42
+ "One or more of the parameters you passed to the Binary constructor are invalid:\n";
43
+ errors.forEach((error) => {
44
+ errorMsg += error;
45
+ });
46
+ errorMsg +=
47
+ '\n\nCorrect usage: new Package("my-binary", "https://example.com/binary/download.tar.gz", {"my-binary": "my-binary"})';
48
+ error(errorMsg);
49
+ }
50
+
51
+ this.platform = platform;
52
+ this.url = url;
53
+ this.name = name;
54
+ this.filename = filename;
55
+ this.zipExt = zipExt;
56
+ this.installDirectory = join(__dirname, "node_modules", ".bin_real");
57
+ this.binaries = binaries;
58
+
59
+ if (!existsSync(this.installDirectory)) {
60
+ mkdirSync(this.installDirectory, { recursive: true });
61
+ }
62
+ }
63
+
64
+ exists() {
65
+ for (const binaryName in this.binaries) {
66
+ const binRelPath = this.binaries[binaryName];
67
+ const binPath = join(this.installDirectory, binRelPath);
68
+ if (!existsSync(binPath)) {
69
+ return false;
70
+ }
71
+ }
72
+ return true;
73
+ }
74
+
75
+ install(fetchOptions, suppressLogs = false) {
76
+ if (this.exists()) {
77
+ if (!suppressLogs) {
78
+ console.error(
79
+ `${this.name} is already installed, skipping installation.`,
80
+ );
81
+ }
82
+ return Promise.resolve();
83
+ }
84
+
85
+ if (existsSync(this.installDirectory)) {
86
+ rimraf.sync(this.installDirectory);
87
+ }
88
+
89
+ mkdirSync(this.installDirectory, { recursive: true });
90
+
91
+ if (!suppressLogs) {
92
+ console.error(`Downloading release from ${this.url}`);
93
+ }
94
+
95
+ return axios({ ...fetchOptions, url: this.url, responseType: "stream" })
96
+ .then((res) => {
97
+ return new Promise((resolve, reject) => {
98
+ mkdtemp(`${tmpDir}${sep}`, (err, directory) => {
99
+ let tempFile = join(directory, this.filename);
100
+ const sink = res.data.pipe(createWriteStream(tempFile));
101
+ sink.on("error", (err) => reject(err));
102
+ sink.on("close", () => {
103
+ if (/\.tar\.*/.test(this.zipExt)) {
104
+ const result = spawnSync("tar", [
105
+ "xf",
106
+ tempFile,
107
+ // The tarballs are stored with a leading directory
108
+ // component; we strip one component in the
109
+ // shell installers too.
110
+ "--strip-components",
111
+ "1",
112
+ "-C",
113
+ this.installDirectory,
114
+ ]);
115
+ if (result.status == 0) {
116
+ resolve();
117
+ } else if (result.error) {
118
+ reject(result.error);
119
+ } else {
120
+ reject(
121
+ new Error(
122
+ `An error occurred untarring the artifact: stdout: ${result.stdout}; stderr: ${result.stderr}`,
123
+ ),
124
+ );
125
+ }
126
+ } else if (this.zipExt == ".zip") {
127
+ let result;
128
+ if (this.platform.artifactName.includes("windows")) {
129
+ // Windows does not have "unzip" by default on many installations, instead
130
+ // we use Expand-Archive from powershell
131
+ result = spawnSync("powershell.exe", [
132
+ "-NoProfile",
133
+ "-NonInteractive",
134
+ "-Command",
135
+ `& {
136
+ param([string]$LiteralPath, [string]$DestinationPath)
137
+ Expand-Archive -LiteralPath $LiteralPath -DestinationPath $DestinationPath -Force
138
+ }`,
139
+ tempFile,
140
+ this.installDirectory,
141
+ ]);
142
+ } else {
143
+ result = spawnSync("unzip", [
144
+ "-q",
145
+ tempFile,
146
+ "-d",
147
+ this.installDirectory,
148
+ ]);
149
+ }
150
+
151
+ if (result.status == 0) {
152
+ resolve();
153
+ } else if (result.error) {
154
+ reject(result.error);
155
+ } else {
156
+ reject(
157
+ new Error(
158
+ `An error occurred unzipping the artifact: stdout: ${result.stdout}; stderr: ${result.stderr}`,
159
+ ),
160
+ );
161
+ }
162
+ } else {
163
+ reject(
164
+ new Error(`Unrecognized file extension: ${this.zipExt}`),
165
+ );
166
+ }
167
+ });
168
+ });
169
+ });
170
+ })
171
+ .then(() => {
172
+ if (!suppressLogs) {
173
+ console.error(`${this.name} has been installed!`);
174
+ }
175
+ })
176
+ .catch((e) => {
177
+ error(`Error fetching release: ${e.message}`);
178
+ });
179
+ }
180
+
181
+ run(binaryName, fetchOptions) {
182
+ const promise = !this.exists()
183
+ ? this.install(fetchOptions, true)
184
+ : Promise.resolve();
185
+
186
+ promise
187
+ .then(() => {
188
+ const [, , ...args] = process.argv;
189
+
190
+ const options = { cwd: process.cwd(), stdio: "inherit" };
191
+
192
+ const binRelPath = this.binaries[binaryName];
193
+ if (!binRelPath) {
194
+ error(`${binaryName} is not a known binary in ${this.name}`);
195
+ }
196
+ const binPath = join(this.installDirectory, binRelPath);
197
+ const result = spawnSync(binPath, args, options);
198
+
199
+ if (result.error) {
200
+ error(result.error);
201
+ }
202
+
203
+ process.exit(result.status);
204
+ })
205
+ .catch((e) => {
206
+ error(e.message);
207
+ process.exit(1);
208
+ });
209
+ }
210
+ }
211
+
212
+ module.exports.Package = Package;
package/binary.js ADDED
@@ -0,0 +1,128 @@
1
+ const { Package } = require("./binary-install");
2
+ const os = require("os");
3
+ const cTable = require("console.table");
4
+ const libc = require("detect-libc");
5
+ const { configureProxy } = require("axios-proxy-builder");
6
+
7
+ const error = (msg) => {
8
+ console.error(msg);
9
+ process.exit(1);
10
+ };
11
+
12
+ const {
13
+ name,
14
+ artifactDownloadUrls,
15
+ supportedPlatforms,
16
+ glibcMinimum,
17
+ } = require("./package.json");
18
+
19
+ // FIXME: implement NPM installer handling of fallback download URLs
20
+ const artifactDownloadUrl = artifactDownloadUrls[0];
21
+ const builderGlibcMajorVersion = glibcMinimum.major;
22
+ const builderGlibcMinorVersion = glibcMinimum.series;
23
+
24
+ const getPlatform = () => {
25
+ const rawOsType = os.type();
26
+ const rawArchitecture = os.arch();
27
+
28
+ // We want to use rust-style target triples as the canonical key
29
+ // for a platform, so translate the "os" library's concepts into rust ones
30
+ let osType = "";
31
+ switch (rawOsType) {
32
+ case "Windows_NT":
33
+ osType = "pc-windows-msvc";
34
+ break;
35
+ case "Darwin":
36
+ osType = "apple-darwin";
37
+ break;
38
+ case "Linux":
39
+ osType = "unknown-linux-gnu";
40
+ break;
41
+ }
42
+
43
+ let arch = "";
44
+ switch (rawArchitecture) {
45
+ case "x64":
46
+ arch = "x86_64";
47
+ break;
48
+ case "arm64":
49
+ arch = "aarch64";
50
+ break;
51
+ }
52
+
53
+ if (rawOsType === "Linux") {
54
+ if (libc.familySync() == "musl") {
55
+ osType = "unknown-linux-musl-dynamic";
56
+ } else if (libc.isNonGlibcLinuxSync()) {
57
+ console.warn(
58
+ "Your libc is neither glibc nor musl; trying static musl binary instead",
59
+ );
60
+ osType = "unknown-linux-musl-static";
61
+ } else {
62
+ let libcVersion = libc.versionSync();
63
+ let splitLibcVersion = libcVersion.split(".");
64
+ let libcMajorVersion = splitLibcVersion[0];
65
+ let libcMinorVersion = splitLibcVersion[1];
66
+ if (
67
+ libcMajorVersion != builderGlibcMajorVersion ||
68
+ libcMinorVersion < builderGlibcMinorVersion
69
+ ) {
70
+ // We can't run the glibc binaries, but we can run the static musl ones
71
+ // if they exist
72
+ console.warn(
73
+ "Your glibc isn't compatible; trying static musl binary instead",
74
+ );
75
+ osType = "unknown-linux-musl-static";
76
+ }
77
+ }
78
+ }
79
+
80
+ // Assume the above succeeded and build a target triple to look things up with.
81
+ // If any of it failed, this lookup will fail and we'll handle it like normal.
82
+ let targetTriple = `${arch}-${osType}`;
83
+ let platform = supportedPlatforms[targetTriple];
84
+
85
+ if (!platform) {
86
+ error(
87
+ `Platform with type "${rawOsType}" and architecture "${rawArchitecture}" is not supported by ${name}.\nYour system must be one of the following:\n\n${Object.keys(
88
+ supportedPlatforms,
89
+ ).join(",")}`,
90
+ );
91
+ }
92
+
93
+ return platform;
94
+ };
95
+
96
+ const getPackage = () => {
97
+ const platform = getPlatform();
98
+ const url = `${artifactDownloadUrl}/${platform.artifactName}`;
99
+ let filename = platform.artifactName;
100
+ let ext = platform.zipExt;
101
+ let binary = new Package(platform, name, url, filename, ext, platform.bins);
102
+
103
+ return binary;
104
+ };
105
+
106
+ const install = (suppressLogs) => {
107
+ if (!artifactDownloadUrl || artifactDownloadUrl.length === 0) {
108
+ console.warn("in demo mode, not installing binaries");
109
+ return;
110
+ }
111
+ const package = getPackage();
112
+ const proxy = configureProxy(package.url);
113
+
114
+ return package.install(proxy, suppressLogs);
115
+ };
116
+
117
+ const run = (binaryName) => {
118
+ const package = getPackage();
119
+ const proxy = configureProxy(package.url);
120
+
121
+ package.run(binaryName, proxy);
122
+ };
123
+
124
+ module.exports = {
125
+ install,
126
+ run,
127
+ getPackage,
128
+ };
package/install.js ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+
3
+ const { install } = require("./binary");
4
+ install(false);