@baishancloud/fnedge 0.1.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.

Potentially problematic release.


This version of @baishancloud/fnedge might be problematic. Click here for more details.

package/README.md ADDED
File without changes
package/fnedge.js ADDED
@@ -0,0 +1,112 @@
1
+ import { join } from "path"
2
+ import os from "os";
3
+ import fs from "fs"
4
+ import { existsSync, mkdirSync } from "fs";
5
+ import fetch from 'node-fetch';
6
+ import { spawnSync } from "child_process";
7
+ import rimraf from "rimraf";
8
+
9
+ class App {
10
+ constructor(props) {
11
+ const type = os.type();
12
+ const arch = os.arch();
13
+ if (arch !== "x64" && arch !== "arm64") {
14
+ throw new Error(`Unsupported arch: ${type} ${arch}`);
15
+ }
16
+ switch (type) {
17
+ case "Windows_NT":
18
+ this.osType = "windows"
19
+ break
20
+ case "Linux":
21
+ this.osType = "linux"
22
+ break
23
+ case "Darwin":
24
+ this.osType = "darwin"
25
+ break
26
+ default:
27
+ throw new Error(`Unsupported os type: ${type} ${arch}`);
28
+ }
29
+ this.fnedgeDir = join(os.homedir(), ".fnedge")
30
+ }
31
+
32
+ async install() {
33
+ try {
34
+ await this.downloadBinary()
35
+ }catch (e) {}
36
+ }
37
+
38
+ async downloadBinary() {
39
+ if (!existsSync(this.fnedgeDir)) {
40
+ mkdirSync(this.fnedgeDir, { recursive: true, mode: '0755'});
41
+ }else{
42
+ rimraf.sync(this.fnedgeDir)
43
+ }
44
+
45
+ const binaryDir = join(this.fnedgeDir, "bin")
46
+ if (!existsSync(binaryDir)) {
47
+ mkdirSync(binaryDir, { recursive: true, mode: '0755'});
48
+ }
49
+
50
+ const url = this.getReleaseUrl(this.osType)
51
+ await this.fetchBinary(url, binaryDir)
52
+ }
53
+
54
+ uninstall() {
55
+ try {
56
+ rimraf.sync(join(this.fnedgeDir, "bin"));
57
+ rimraf.sync(this.fnedgeDir);
58
+ }catch (e){}
59
+ }
60
+
61
+ async run() {
62
+ if (this.osType != "windows") {
63
+ const binaryPath = join(this.fnedgeDir, "/bin/" + this.getBinaryName())
64
+ if (!existsSync(binaryPath)) {
65
+ await this.downloadBinary()
66
+ }
67
+ }
68
+
69
+ const binaryPath = join(this.fnedgeDir, "bin", this.getBinaryName())
70
+ const [, , ...args] = process.argv;
71
+ const options = { cwd: process.cwd(), stdio: "inherit" };
72
+ const result = spawnSync(binaryPath, args, options);
73
+ if (result.error) {
74
+ console.log(result.error)
75
+ process.exit(1);
76
+ }
77
+ process.exit(result.status);
78
+ }
79
+
80
+ getReleaseUrl(osType) {
81
+ return `https://fnedge-api.console.baishan.com/api/v1/cli/download?os=${osType}`
82
+ }
83
+
84
+ async fetchBinary(url, binaryDir) {
85
+ const response = await fetch(url, { method: 'GET' });
86
+ if (response.status == 200) {
87
+ const fileStream = fs.createWriteStream(join(binaryDir, this.getBinaryName()), { mode: 755 });
88
+ await new Promise((resolve, reject) => {
89
+ response.body.pipe(fileStream);
90
+ response.body.on("error", reject);
91
+ fileStream.on("finish", resolve);
92
+ });
93
+ return;
94
+ } else if (response.status == 403) {
95
+ throw new Error("403 forbidden")
96
+ console.log(response.status)
97
+ }else{
98
+ let json = await response.json()
99
+ console.log(JSON.stringify(json))
100
+ throw new Error("download fnedge binary failed")
101
+ }
102
+ }
103
+
104
+ getBinaryName() {
105
+ let name = "fnedge"
106
+ if (this.osType == "windows") {
107
+ name += ".exe"
108
+ }
109
+ return name
110
+ }
111
+ }
112
+ export default App
package/install.js ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env node
2
+
3
+ import fnedge from "./fnedge.js"
4
+ let app = new fnedge()
5
+ await app.install()
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@baishancloud/fnedge",
3
+ "version": "0.1.1",
4
+ "description": "command line tool for function@edge",
5
+ "main": "fnedge.js",
6
+ "bin": {
7
+ "fnedge": "run.js"
8
+ },
9
+ "type": "module",
10
+ "scripts": {
11
+ "postinstall": "node ./install.js",
12
+ "preuninstall": "node ./uninstall.js"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/baishanyun/fnedge.git"
17
+ },
18
+ "author": "",
19
+ "license": "ISC",
20
+ "keywords": [
21
+ "fnedge",
22
+ "baishanyun",
23
+ "function@edge",
24
+ "function edge",
25
+ "edge",
26
+ "compute",
27
+ "serverless",
28
+ "serverless application",
29
+ "serverless module",
30
+ "wasm",
31
+ "web",
32
+ "assembly",
33
+ "webassembly",
34
+ "rust",
35
+ "emscripten",
36
+ "typescript",
37
+ "router",
38
+ "http",
39
+ "cli"
40
+ ],
41
+ "dependencies": {
42
+ "md5": "^2.3.0",
43
+ "node-fetch": "^3.0.0",
44
+ "rimraf": "^3.0.2",
45
+ "url-parse": "^1.5.3"
46
+ }
47
+ }
package/run.js ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+
3
+ import fnedge from "./fnedge.js"
4
+ let app = new fnedge()
5
+ await app.run()
6
+
package/uninstall.js ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env node
2
+
3
+ import fnedge from "./fnedge.js"
4
+ let app = new fnedge()
5
+ await app.uninstall()