mirror of
https://github.com/certd/certd.git
synced 2026-05-18 06:17:31 +08:00
feat: 升级midway,支持esm
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"parser": "@typescript-eslint/parser",
|
||||
"plugins": [
|
||||
"@typescript-eslint"
|
||||
],
|
||||
"extends": [
|
||||
"plugin:@typescript-eslint/recommended",
|
||||
"plugin:prettier/recommended",
|
||||
"prettier"
|
||||
],
|
||||
"env": {
|
||||
"mocha": true
|
||||
},
|
||||
"rules": {
|
||||
"@typescript-eslint/no-var-requires": "off",
|
||||
"@typescript-eslint/ban-ts-comment": "off",
|
||||
"@typescript-eslint/ban-ts-ignore": "off",
|
||||
"@typescript-eslint/no-explicit-any": "off",
|
||||
"@typescript-eslint/no-empty-function": "off",
|
||||
// "no-unused-expressions": "off",
|
||||
"max-len": [0, 160, 2, { "ignoreUrls": true }]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
lerna-debug.log*
|
||||
|
||||
node_modules
|
||||
dist
|
||||
dist-ssr
|
||||
*.local
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/*
|
||||
!.vscode/extensions.json
|
||||
.idea
|
||||
.DS_Store
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
||||
|
||||
test/user.secret.ts
|
||||
|
||||
rollup.cache
|
||||
@@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
src
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"printWidth": 160
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
# Change Log
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||
|
||||
# [1.21.0](https://github.com/certd/certd/compare/v1.20.17...v1.21.0) (2024-07-03)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.20.17](https://github.com/certd/certd/compare/v1.20.16...v1.20.17) (2024-07-03)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.20.16](https://github.com/certd/certd/compare/v1.20.15...v1.20.16) (2024-07-01)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.20.15](https://github.com/certd/certd/compare/v1.20.14...v1.20.15) (2024-06-28)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.20.14](https://github.com/certd/certd/compare/v1.20.13...v1.20.14) (2024-06-23)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.20.13](https://github.com/certd/certd/compare/v1.20.12...v1.20.13) (2024-06-18)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.20.12](https://github.com/certd/certd/compare/v1.20.10...v1.20.12) (2024-06-17)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.20.10](https://github.com/certd/certd/compare/v1.20.9...v1.20.10) (2024-05-30)
|
||||
|
||||
### Performance Improvements
|
||||
|
||||
* 优化文件下载包名 ([d9eb927](https://github.com/certd/certd/commit/d9eb927b0a1445feab08b1958aa9ea80637a5ae6))
|
||||
|
||||
## [1.20.9](https://github.com/certd/certd/compare/v1.20.8...v1.20.9) (2024-03-22)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.20.8](https://github.com/certd/certd/compare/v1.20.7...v1.20.8) (2024-03-22)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.20.7](https://github.com/certd/certd/compare/v1.20.6...v1.20.7) (2024-03-22)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.20.6](https://github.com/certd/certd/compare/v1.20.5...v1.20.6) (2024-03-21)
|
||||
|
||||
### Performance Improvements
|
||||
|
||||
* 插件贡献文档及示例 ([72fb20a](https://github.com/certd/certd/commit/72fb20abf3ba5bdd862575d2907703a52fd7eb17))
|
||||
|
||||
## [1.20.5](https://github.com/certd/certd/compare/v1.20.2...v1.20.5) (2024-03-11)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.20.2](https://github.com/certd/certd/compare/v1.2.1...v1.20.2) (2024-02-28)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.2.1](https://github.com/certd/certd/compare/v1.2.0...v1.2.1) (2023-12-12)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
# [1.2.0](https://github.com/certd/certd/compare/v1.1.6...v1.2.0) (2023-10-27)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.1.6](https://github.com/certd/certd/compare/v1.1.5...v1.1.6) (2023-07-10)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.1.5](https://github.com/certd/certd/compare/v1.1.4...v1.1.5) (2023-07-03)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.1.4](https://github.com/certd/certd/compare/v1.1.3...v1.1.4) (2023-07-03)
|
||||
|
||||
### Performance Improvements
|
||||
|
||||
* timeout ([3eeb1f7](https://github.com/certd/certd/commit/3eeb1f77aa2922f3545f3d2067f561d95621d54f))
|
||||
|
||||
## [1.1.3](https://github.com/certd/certd/compare/v1.1.2...v1.1.3) (2023-07-03)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.1.2](https://github.com/certd/certd/compare/v1.1.1...v1.1.2) (2023-07-03)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.1.1](https://github.com/certd/certd/compare/v1.1.0...v1.1.1) (2023-06-28)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
# [1.1.0](https://github.com/certd/certd/compare/v1.0.6...v1.1.0) (2023-06-28)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.0.6](https://github.com/certd/certd/compare/v1.0.5...v1.0.6) (2023-05-25)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.0.5](https://github.com/certd/certd/compare/v1.0.4...v1.0.5) (2023-05-25)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.0.4](https://github.com/certd/certd/compare/v1.0.3...v1.0.4) (2023-05-25)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.0.3](https://github.com/certd/certd/compare/v1.0.2...v1.0.3) (2023-05-25)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.0.2](https://github.com/certd/certd/compare/v1.0.1...v1.0.2) (2023-05-24)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.0.1](https://github.com/certd/certd/compare/v1.0.0...v1.0.1) (2023-05-24)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
@@ -0,0 +1,16 @@
|
||||
# Vue 3 + TypeScript + Vite
|
||||
|
||||
This template should help get you started developing with Vue 3 and TypeScript in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.
|
||||
|
||||
## Recommended IDE Setup
|
||||
|
||||
- [VS Code](https://code.visualstudio.com/) + [Volar](https://marketplace.visualstudio.com/items?itemName=Vue.volar)
|
||||
|
||||
## Type Support For `.vue` Imports in TS
|
||||
|
||||
Since TypeScript cannot handle type information for `.vue` imports, they are shimmed to be a generic Vue component type by default. In most cases this is fine if you don't really care about component prop types outside of templates. However, if you wish to get actual prop types in `.vue` imports (for example to get props validation when using manual `h(...)` calls), you can enable Volar's Take Over mode by following these steps:
|
||||
|
||||
1. Run `Extensions: Show Built-in Extensions` from VS Code's command palette, look for `TypeScript and JavaScript Language Features`, then right click and select `Disable (Workspace)`. By default, Take Over mode will enable itself if the default TypeScript extension is disabled.
|
||||
2. Reload the VS Code window by running `Developer: Reload Window` from the command palette.
|
||||
|
||||
You can learn more about Take Over mode [here](https://github.com/johnsoncodehk/volar/discussions/471).
|
||||
@@ -0,0 +1,96 @@
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
|
||||
// https://gist.github.com/lovasoa/8691344
|
||||
async function* walk(dir) {
|
||||
for await (const d of await fs.promises.opendir(dir)) {
|
||||
const entry = path.join(dir, d.name);
|
||||
if (d.isDirectory()) {
|
||||
yield* walk(entry);
|
||||
} else if (d.isFile()) {
|
||||
yield entry;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function resolveImportPath(sourceFile, importPath, options) {
|
||||
const sourceFileAbs = path.resolve(process.cwd(), sourceFile);
|
||||
const root = path.dirname(sourceFileAbs);
|
||||
const { moduleFilter = defaultModuleFilter } = options;
|
||||
|
||||
if (moduleFilter(importPath)) {
|
||||
const importPathAbs = path.resolve(root, importPath);
|
||||
let possiblePath = [path.resolve(importPathAbs, "./index.ts"), path.resolve(importPathAbs, "./index.js"), importPathAbs + ".ts", importPathAbs + ".js"];
|
||||
|
||||
if (possiblePath.length) {
|
||||
for (let i = 0; i < possiblePath.length; i++) {
|
||||
let entry = possiblePath[i];
|
||||
if (fs.existsSync(entry)) {
|
||||
const resolved = path.relative(root, entry.replace(/\.ts$/, ".js"));
|
||||
|
||||
if (!resolved.startsWith(".")) {
|
||||
return "./" + resolved;
|
||||
}
|
||||
|
||||
return resolved;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function replace(filePath, outFilePath, options) {
|
||||
const code = fs.readFileSync(filePath).toString();
|
||||
const newCode = code.replace(/(import|export) (.+?) from ('[^\n']+'|"[^\n"]+");/gs, function (found, action, imported, from) {
|
||||
const importPath = from.slice(1, -1);
|
||||
let resolvedPath = resolveImportPath(filePath, importPath, options);
|
||||
|
||||
if (resolvedPath) {
|
||||
resolvedPath = resolvedPath.replaceAll("\\", "/");
|
||||
console.log("\t", importPath, resolvedPath);
|
||||
return `${action} ${imported} from "${resolvedPath}";`;
|
||||
}
|
||||
|
||||
return found;
|
||||
});
|
||||
|
||||
if (code !== newCode) {
|
||||
fs.writeFileSync(outFilePath, newCode);
|
||||
}
|
||||
}
|
||||
|
||||
// Then, use it with a simple async for loop
|
||||
async function run(srcDir, options = defaultOptions) {
|
||||
const { sourceFileFilter = defaultSourceFileFilter } = options;
|
||||
|
||||
for await (const entry of walk(srcDir)) {
|
||||
if (sourceFileFilter(entry)) {
|
||||
console.log(entry);
|
||||
replace(entry, entry, options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const defaultSourceFileFilter = function (sourceFilePath) {
|
||||
return /\.(js|ts)$/.test(sourceFilePath) && !/node_modules/.test(sourceFilePath);
|
||||
};
|
||||
|
||||
const defaultModuleFilter = function (importedModule) {
|
||||
return !path.isAbsolute(importedModule) && !importedModule.startsWith("@") && !importedModule.endsWith(".js");
|
||||
};
|
||||
|
||||
const defaultOptions = {
|
||||
sourceFileFilter: defaultSourceFileFilter,
|
||||
moduleFilter: defaultModuleFilter,
|
||||
};
|
||||
|
||||
// Switch this to test on one file or directly run on a directory.
|
||||
const DEBUG = false;
|
||||
|
||||
if (DEBUG) {
|
||||
replace("./src/index.ts", "./out.ts", defaultOptions);
|
||||
} else {
|
||||
await run("./src/", defaultOptions);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "@certd/lib-huawei",
|
||||
"private": false,
|
||||
"version": "1.21.0",
|
||||
"main": "./dist/bundle.js",
|
||||
"module": "./dist/bundle.js",
|
||||
"types": "./dist/d/index.d.ts",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "rollup -c ",
|
||||
"build2": "vue-tsc --noEmit && vite build",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"@certd/pipeline": "1.21.0",
|
||||
"axios": "^1.7.2",
|
||||
"rollup": "^3.7.4"
|
||||
},
|
||||
"gitHead": "a31f1c7f5e71fa946de9bf0283e11d6ce049b3e9"
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
const resolve = require("@rollup/plugin-node-resolve");
|
||||
const commonjs = require("@rollup/plugin-commonjs");
|
||||
//const Typescript = require("rollup-plugin-typescript2");
|
||||
const Typescript = require("@rollup/plugin-typescript");
|
||||
const json = require("@rollup/plugin-json");
|
||||
const terser = require("@rollup/plugin-terser");
|
||||
module.exports = {
|
||||
input: "src/index.ts",
|
||||
output: {
|
||||
file: "dist/bundle.js",
|
||||
format: "cjs",
|
||||
},
|
||||
plugins: [
|
||||
// 解析第三方依赖
|
||||
resolve(),
|
||||
// 识别 commonjs 模式第三方依赖
|
||||
commonjs({
|
||||
// dynamicRequireRoot: "../../../../",
|
||||
// dynamicRequireTargets: [
|
||||
// // include using a glob pattern (either a string or an array of strings)
|
||||
// "../../../../**/shelljs/src/*",
|
||||
// ],
|
||||
}),
|
||||
Typescript({
|
||||
target: "esnext",
|
||||
rootDir: "src",
|
||||
declaration: true,
|
||||
declarationDir: "dist/d",
|
||||
exclude: ["./node_modules/**", "./src/**/*.vue"],
|
||||
allowSyntheticDefaultImports: true,
|
||||
}),
|
||||
json(),
|
||||
terser(),
|
||||
],
|
||||
external: ["vue", "lodash-es", "dayjs", "log4js", "@midwayjs/core", "@certd/pipeline", "axios"],
|
||||
};
|
||||
@@ -0,0 +1,2 @@
|
||||
export { HuaweiYunClient } from "./lib/client.js";
|
||||
export { ApiRequestOptions } from "./lib/client.js";
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
# License
|
||||
|
||||
[The MIT License (MIT)](http://opensource.org/licenses/MIT)
|
||||
|
||||
Copyright (c) 2009-2013 Jeff Mott
|
||||
Copyright (c) 2013-2016 Evan Vosberg
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// Copyright Joyent, Inc. and other Node contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
||||
// persons to whom the Software is furnished to do so, subject to the
|
||||
// following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included
|
||||
// in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
||||
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
@@ -0,0 +1,35 @@
|
||||
var signer = require("./signer");
|
||||
var https = require("https");
|
||||
var sig = new signer.Signer();
|
||||
//Set the AK/SK to sign and authenticate the request.
|
||||
sig.Key = "QTWAOYTTINDUT2QVKYUC";
|
||||
sig.Secret = "MFyfvK41ba2giqM7**********KGpownRZlmVmHc";
|
||||
|
||||
//The following example shows how to set the request URL and parameters to query a VPC list.
|
||||
//Specify a request method, such as GET, PUT, POST, DELETE, HEAD, and PATCH.
|
||||
//Set request host.
|
||||
//Set request URI.
|
||||
//Set parameters for the request URL.
|
||||
var r = new signer.HttpRequest("GET", "endpoint.example.com/v1/77b6a44cba5143ab91d13ab9a8ff44fd/vpcs?limie=1");
|
||||
//Add header parameters, for example, x-domain-id for invoking a global service and x-project-id for invoking a project-level service.
|
||||
r.headers = { "Content-Type": "application/json" };
|
||||
//Add a body if you have specified the PUT or POST method. Special characters, such as the double quotation mark ("), contained in the body must be escaped.
|
||||
r.body = "";
|
||||
|
||||
var opt = sig.Sign(r);
|
||||
console.log(opt.headers["X-Sdk-Date"]);
|
||||
console.log(opt.headers["Authorization"]);
|
||||
|
||||
var req = https.request(opt, function (res) {
|
||||
console.log(res.statusCode);
|
||||
console.log("headers:", JSON.stringify(res.headers));
|
||||
res.on("data", function (chunk) {
|
||||
console.log(chunk.toString());
|
||||
});
|
||||
});
|
||||
|
||||
req.on("error", function (err) {
|
||||
console.log(err.message);
|
||||
});
|
||||
req.write(r.body);
|
||||
req.end();
|
||||
@@ -0,0 +1,501 @@
|
||||
// HWS API Gateway Signature
|
||||
(function (root, factory) {
|
||||
"use strict";
|
||||
|
||||
/*global define*/
|
||||
if (typeof define === "function" && define.amd) {
|
||||
// AMD
|
||||
define(["CryptoJS"], function (CryptoJS) {
|
||||
var crypto_wrapper = {
|
||||
hmacsha256: function (keyByte, message) {
|
||||
return CryptoJS.HmacSHA256(message, keyByte).toString(CryptoJS.enc.Hex);
|
||||
},
|
||||
HexEncodeSHA256Hash: function (body) {
|
||||
return CryptoJS.SHA256(body);
|
||||
},
|
||||
};
|
||||
return factory(crypto_wrapper);
|
||||
});
|
||||
} else if (typeof wx === "object") {
|
||||
// wechat
|
||||
var CryptoJS = require("./js/hmac-sha256.js");
|
||||
var crypto_wrapper = {
|
||||
hmacsha256: function (keyByte, message) {
|
||||
return CryptoJS.HmacSHA256(message, keyByte).toString(CryptoJS.enc.Hex);
|
||||
},
|
||||
HexEncodeSHA256Hash: function (body) {
|
||||
return CryptoJS.SHA256(body);
|
||||
},
|
||||
};
|
||||
module.exports = factory(crypto_wrapper);
|
||||
} else if (typeof module === "object" && module.exports) {
|
||||
// Node
|
||||
var crypto = require("crypto");
|
||||
var crypto_wrapper = {
|
||||
hmacsha256: function (keyByte, message) {
|
||||
return crypto.createHmac("SHA256", keyByte).update(message).digest().toString("hex");
|
||||
},
|
||||
HexEncodeSHA256Hash: function (body) {
|
||||
return crypto.createHash("SHA256").update(body).digest().toString("hex");
|
||||
},
|
||||
};
|
||||
module.exports = factory(crypto_wrapper);
|
||||
} else {
|
||||
// Browser
|
||||
var CryptoJS = root.CryptoJS;
|
||||
var crypto_wrapper = {
|
||||
hmacsha256: function (keyByte, message) {
|
||||
return CryptoJS.HmacSHA256(message, keyByte).toString(CryptoJS.enc.Hex);
|
||||
},
|
||||
HexEncodeSHA256Hash: function (body) {
|
||||
return CryptoJS.SHA256(body);
|
||||
},
|
||||
};
|
||||
root.signer = factory(crypto_wrapper);
|
||||
}
|
||||
})(this, function (crypto_wrapper) {
|
||||
"use strict";
|
||||
|
||||
var Algorithm = "SDK-HMAC-SHA256";
|
||||
var HeaderXDate = "X-Sdk-Date";
|
||||
var HeaderAuthorization = "Authorization";
|
||||
var HeaderContentSha256 = "x-sdk-content-sha256";
|
||||
|
||||
const hexTable = new Array(256);
|
||||
for (var i = 0; i < 256; ++i) hexTable[i] = "%" + ((i < 16 ? "0" : "") + i.toString(16)).toUpperCase();
|
||||
|
||||
const noEscape = [
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0, // 0 - 15
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0, // 16 - 31
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
0, // 32 - 47
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0, // 48 - 63
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1, // 64 - 79
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
1, // 80 - 95
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1, // 96 - 111
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
0, // 112 - 127
|
||||
];
|
||||
|
||||
// function urlEncode is based on https://github.com/nodejs/node/blob/master/lib/querystring.js
|
||||
// Copyright Joyent, Inc. and other Node contributors.
|
||||
function urlEncode(str) {
|
||||
if (typeof str !== "string") {
|
||||
if (typeof str === "object") str = String(str);
|
||||
else str += "";
|
||||
}
|
||||
var out = "";
|
||||
var lastPos = 0;
|
||||
|
||||
for (var i = 0; i < str.length; ++i) {
|
||||
var c = str.charCodeAt(i);
|
||||
|
||||
// ASCII
|
||||
if (c < 0x80) {
|
||||
if (noEscape[c] === 1) continue;
|
||||
if (lastPos < i) out += str.slice(lastPos, i);
|
||||
lastPos = i + 1;
|
||||
out += hexTable[c];
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lastPos < i) out += str.slice(lastPos, i);
|
||||
|
||||
// Multi-byte characters ...
|
||||
if (c < 0x800) {
|
||||
lastPos = i + 1;
|
||||
out += hexTable[0xc0 | (c >> 6)] + hexTable[0x80 | (c & 0x3f)];
|
||||
continue;
|
||||
}
|
||||
if (c < 0xd800 || c >= 0xe000) {
|
||||
lastPos = i + 1;
|
||||
out += hexTable[0xe0 | (c >> 12)] + hexTable[0x80 | ((c >> 6) & 0x3f)] + hexTable[0x80 | (c & 0x3f)];
|
||||
continue;
|
||||
}
|
||||
// Surrogate pair
|
||||
++i;
|
||||
|
||||
if (i >= str.length) throw new errors.URIError("ERR_INVALID_URI");
|
||||
|
||||
var c2 = str.charCodeAt(i) & 0x3ff;
|
||||
|
||||
lastPos = i + 1;
|
||||
c = 0x10000 + (((c & 0x3ff) << 10) | c2);
|
||||
out += hexTable[0xf0 | (c >> 18)] + hexTable[0x80 | ((c >> 12) & 0x3f)] + hexTable[0x80 | ((c >> 6) & 0x3f)] + hexTable[0x80 | (c & 0x3f)];
|
||||
}
|
||||
if (lastPos === 0) return str;
|
||||
if (lastPos < str.length) return out + str.slice(lastPos);
|
||||
return out;
|
||||
}
|
||||
|
||||
function HttpRequest(method, url, headers, body) {
|
||||
if (method === undefined) {
|
||||
this.method = "";
|
||||
} else {
|
||||
this.method = method;
|
||||
}
|
||||
if (url === undefined) {
|
||||
this.host = "";
|
||||
this.uri = "";
|
||||
this.query = {};
|
||||
} else {
|
||||
this.query = {};
|
||||
var host, path;
|
||||
var i = url.indexOf("://");
|
||||
if (i !== -1) {
|
||||
url = url.substr(i + 3);
|
||||
}
|
||||
var i = url.indexOf("?");
|
||||
if (i !== -1) {
|
||||
var query_str = url.substr(i + 1);
|
||||
url = url.substr(0, i);
|
||||
var spl = query_str.split("&");
|
||||
for (var i in spl) {
|
||||
var kv = spl[i];
|
||||
var index = kv.indexOf("=");
|
||||
var key, value;
|
||||
if (index >= 0) {
|
||||
key = kv.substr(0, index);
|
||||
value = kv.substr(index + 1);
|
||||
} else {
|
||||
key = kv;
|
||||
value = "";
|
||||
}
|
||||
if (key !== "") {
|
||||
key = decodeURI(key);
|
||||
value = decodeURI(value);
|
||||
if (this.query[key] === undefined) {
|
||||
this.query[key] = [value];
|
||||
} else {
|
||||
this.query[key].push(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
var i = url.indexOf("/");
|
||||
if (i === -1) {
|
||||
host = url;
|
||||
path = "/";
|
||||
} else {
|
||||
host = url.substr(0, i);
|
||||
path = url.substr(i);
|
||||
}
|
||||
this.host = host;
|
||||
this.uri = decodeURI(path);
|
||||
}
|
||||
if (headers === undefined) {
|
||||
this.headers = {};
|
||||
} else {
|
||||
this.headers = headers;
|
||||
}
|
||||
if (body === undefined) {
|
||||
this.body = "";
|
||||
} else {
|
||||
this.body = body;
|
||||
}
|
||||
}
|
||||
|
||||
function findHeader(r, header) {
|
||||
for (var k in r.headers) {
|
||||
if (k.toLowerCase() === header.toLowerCase()) {
|
||||
return r.headers[k];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Build a CanonicalRequest from a regular request string
|
||||
//
|
||||
// CanonicalRequest =
|
||||
// HTTPRequestMethod + '\n' +
|
||||
// CanonicalURI + '\n' +
|
||||
// CanonicalQueryString + '\n' +
|
||||
// CanonicalHeaders + '\n' +
|
||||
// SignedHeaders + '\n' +
|
||||
// HexEncode(Hash(RequestPayload))
|
||||
function CanonicalRequest(r, signedHeaders) {
|
||||
var hexencode = findHeader(r, HeaderContentSha256);
|
||||
if (hexencode === null) {
|
||||
var data = RequestPayload(r);
|
||||
hexencode = crypto_wrapper.HexEncodeSHA256Hash(data);
|
||||
}
|
||||
return (
|
||||
r.method +
|
||||
"\n" +
|
||||
CanonicalURI(r) +
|
||||
"\n" +
|
||||
CanonicalQueryString(r) +
|
||||
"\n" +
|
||||
CanonicalHeaders(r, signedHeaders) +
|
||||
"\n" +
|
||||
signedHeaders.join(";") +
|
||||
"\n" +
|
||||
hexencode
|
||||
);
|
||||
}
|
||||
|
||||
function CanonicalURI(r) {
|
||||
var pattens = r.uri.split("/");
|
||||
var uri = [];
|
||||
for (var k in pattens) {
|
||||
var v = pattens[k];
|
||||
uri.push(urlEncode(v));
|
||||
}
|
||||
var urlpath = uri.join("/");
|
||||
if (urlpath[urlpath.length - 1] !== "/") {
|
||||
urlpath = urlpath + "/";
|
||||
}
|
||||
//r.uri = urlpath
|
||||
return urlpath;
|
||||
}
|
||||
|
||||
function CanonicalQueryString(r) {
|
||||
var keys = [];
|
||||
for (var key in r.query) {
|
||||
keys.push(key);
|
||||
}
|
||||
keys.sort();
|
||||
var a = [];
|
||||
for (var i in keys) {
|
||||
var key = urlEncode(keys[i]);
|
||||
var value = r.query[keys[i]];
|
||||
if (Array.isArray(value)) {
|
||||
value.sort();
|
||||
for (var iv in value) {
|
||||
a.push(key + "=" + urlEncode(value[iv]));
|
||||
}
|
||||
} else {
|
||||
a.push(key + "=" + urlEncode(value));
|
||||
}
|
||||
}
|
||||
return a.join("&");
|
||||
}
|
||||
|
||||
function CanonicalHeaders(r, signedHeaders) {
|
||||
var headers = {};
|
||||
for (var key in r.headers) {
|
||||
headers[key.toLowerCase()] = r.headers[key];
|
||||
}
|
||||
var a = [];
|
||||
for (var i in signedHeaders) {
|
||||
var value = headers[signedHeaders[i]];
|
||||
a.push(signedHeaders[i] + ":" + value.trim());
|
||||
}
|
||||
return a.join("\n") + "\n";
|
||||
}
|
||||
|
||||
function SignedHeaders(r) {
|
||||
var a = [];
|
||||
for (var key in r.headers) {
|
||||
a.push(key.toLowerCase());
|
||||
}
|
||||
a.sort();
|
||||
return a;
|
||||
}
|
||||
|
||||
function RequestPayload(r) {
|
||||
return r.body;
|
||||
}
|
||||
|
||||
// Create a "String to Sign".
|
||||
function StringToSign(canonicalRequest, t) {
|
||||
var bytes = crypto_wrapper.HexEncodeSHA256Hash(canonicalRequest);
|
||||
return Algorithm + "\n" + t + "\n" + bytes;
|
||||
}
|
||||
|
||||
// Create the HWS Signature.
|
||||
function SignStringToSign(stringToSign, signingKey) {
|
||||
return crypto_wrapper.hmacsha256(signingKey, stringToSign);
|
||||
}
|
||||
|
||||
// Get the finalized value for the "Authorization" header. The signature
|
||||
// parameter is the output from SignStringToSign
|
||||
function AuthHeaderValue(signature, Key, signedHeaders) {
|
||||
return Algorithm + " Access=" + Key + ", SignedHeaders=" + signedHeaders.join(";") + ", Signature=" + signature;
|
||||
}
|
||||
|
||||
function twoChar(s) {
|
||||
if (s >= 10) {
|
||||
return "" + s;
|
||||
} else {
|
||||
return "0" + s;
|
||||
}
|
||||
}
|
||||
|
||||
function getTime() {
|
||||
var date = new Date();
|
||||
return (
|
||||
"" +
|
||||
date.getUTCFullYear() +
|
||||
twoChar(date.getUTCMonth() + 1) +
|
||||
twoChar(date.getUTCDate()) +
|
||||
"T" +
|
||||
twoChar(date.getUTCHours()) +
|
||||
twoChar(date.getUTCMinutes()) +
|
||||
twoChar(date.getUTCSeconds()) +
|
||||
"Z"
|
||||
);
|
||||
}
|
||||
|
||||
function Signer() {
|
||||
this.Key = "";
|
||||
this.Secret = "";
|
||||
}
|
||||
|
||||
Signer.prototype.Sign = function (r) {
|
||||
var headerTime = findHeader(r, HeaderXDate);
|
||||
if (headerTime === null) {
|
||||
headerTime = getTime();
|
||||
r.headers[HeaderXDate] = headerTime;
|
||||
}
|
||||
if (r.method !== "PUT" && r.method !== "PATCH" && r.method !== "POST") {
|
||||
r.body = "";
|
||||
}
|
||||
var queryString = CanonicalQueryString(r);
|
||||
if (queryString !== "") {
|
||||
queryString = "?" + queryString;
|
||||
}
|
||||
var options = {
|
||||
hostname: r.host,
|
||||
path: encodeURI(r.uri) + queryString,
|
||||
method: r.method,
|
||||
headers: r.headers,
|
||||
};
|
||||
if (findHeader(r, "host") === null) {
|
||||
r.headers.host = r.host;
|
||||
}
|
||||
var signedHeaders = SignedHeaders(r);
|
||||
var canonicalRequest = CanonicalRequest(r, signedHeaders);
|
||||
var stringToSign = StringToSign(canonicalRequest, headerTime);
|
||||
var signature = SignStringToSign(stringToSign, this.Secret);
|
||||
options.headers[HeaderAuthorization] = AuthHeaderValue(signature, this.Key, signedHeaders);
|
||||
return options;
|
||||
};
|
||||
return {
|
||||
HttpRequest: HttpRequest,
|
||||
Signer: Signer,
|
||||
urlEncode: urlEncode,
|
||||
findHeader: findHeader,
|
||||
SignedHeaders: SignedHeaders,
|
||||
CanonicalRequest: CanonicalRequest,
|
||||
StringToSign: StringToSign,
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,10 @@
|
||||
var assert = require("assert");
|
||||
var signer = require('./signer')
|
||||
|
||||
var s = ""
|
||||
for (i = 0; i < 0x80; i++) {
|
||||
s = s + signer.urlEncode(String.fromCharCode(i))
|
||||
}
|
||||
console.log(s)
|
||||
assert.equal(s, "%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%7F")
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
import { HuaweiAccess } from "../access/index.js";
|
||||
export type ApiRequestOptions = {
|
||||
method: string;
|
||||
url: string;
|
||||
headers?: any;
|
||||
data?: any;
|
||||
};
|
||||
export declare class HuaweiYunClient {
|
||||
access: HuaweiAccess;
|
||||
constructor(access: HuaweiAccess);
|
||||
request(options: ApiRequestOptions): Promise<any>;
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import { Signer, SigHttpRequest } from "./signer.js";
|
||||
import axios from "axios";
|
||||
export class HuaweiYunClient {
|
||||
access;
|
||||
constructor(access, logger) {
|
||||
this.access = access;
|
||||
}
|
||||
async request(options) {
|
||||
const sig = new Signer(this.access.accessKeyId, this.access.accessKeySecret);
|
||||
//The following example shows how to set the request URL and parameters to query a VPC list.
|
||||
//Specify a request method, such as GET, PUT, POST, DELETE, HEAD, and PATCH.
|
||||
//Set request host.
|
||||
//Set request URI.
|
||||
//Set parameters for the request URL.
|
||||
let body = undefined;
|
||||
if (options.data) {
|
||||
body = JSON.stringify(options.data);
|
||||
}
|
||||
const r = new SigHttpRequest(options.method, options.url, options.headers, body);
|
||||
//Add header parameters, for example, x-domain-id for invoking a global service and x-project-id for invoking a project-level service.
|
||||
r.headers = { "Content-Type": "application/json" };
|
||||
//Add a body if you have specified the PUT or POST method. Special characters, such as the double quotation mark ("), contained in the body must be escaped.
|
||||
// r.body = option;
|
||||
const opt = sig.Sign(r);
|
||||
try {
|
||||
const res = await axios.request({
|
||||
url: options.url,
|
||||
method: options.method,
|
||||
headers: opt.headers,
|
||||
data: body,
|
||||
});
|
||||
return res.data;
|
||||
} catch (e) {
|
||||
this.logger.error("华为云接口请求出错:", e?.response?.data);
|
||||
const error = new Error(e?.response?.data.message);
|
||||
error.code = e?.response?.code;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2xpZW50LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vc3JjL3BsdWdpbnMvcGx1Z2luLWh1YXdlaS9saWIvY2xpZW50LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxNQUFNLEVBQUUsY0FBYyxFQUFFLE1BQU0sYUFBYSxDQUFDO0FBRXJELE9BQU8sS0FBSyxNQUFNLE9BQU8sQ0FBQztBQUMxQixPQUFPLEVBQUUsTUFBTSxFQUFFLE1BQU0saUJBQWlCLENBQUM7QUFRekMsTUFBTSxPQUFPLGVBQWU7SUFDMUIsTUFBTSxDQUFlO0lBQ3JCLFlBQVksTUFBb0I7UUFDOUIsSUFBSSxDQUFDLE1BQU0sR0FBRyxNQUFNLENBQUM7SUFDdkIsQ0FBQztJQUNELEtBQUssQ0FBQyxPQUFPLENBQUMsT0FBMEI7UUFDdEMsTUFBTSxHQUFHLEdBQUcsSUFBSSxNQUFNLENBQ3BCLElBQUksQ0FBQyxNQUFNLENBQUMsV0FBVyxFQUN2QixJQUFJLENBQUMsTUFBTSxDQUFDLGVBQWUsQ0FDNUIsQ0FBQztRQUVGLDRGQUE0RjtRQUM1Riw0RUFBNEU7UUFDNUUsbUJBQW1CO1FBQ25CLGtCQUFrQjtRQUNsQixxQ0FBcUM7UUFDckMsSUFBSSxJQUFJLEdBQUcsU0FBUyxDQUFDO1FBQ3JCLElBQUksT0FBTyxDQUFDLElBQUksRUFBRTtZQUNoQixJQUFJLEdBQUcsSUFBSSxDQUFDLFNBQVMsQ0FBQyxPQUFPLENBQUMsSUFBSSxDQUFDLENBQUM7U0FDckM7UUFDRCxNQUFNLENBQUMsR0FBRyxJQUFJLGNBQWMsQ0FDMUIsT0FBTyxDQUFDLE1BQU0sRUFDZCxPQUFPLENBQUMsR0FBRyxFQUNYLE9BQU8sQ0FBQyxPQUFPLEVBQ2YsSUFBSSxDQUNMLENBQUM7UUFDRixzSUFBc0k7UUFDdEksQ0FBQyxDQUFDLE9BQU8sR0FBRyxFQUFFLGNBQWMsRUFBRSxrQkFBa0IsRUFBRSxDQUFDO1FBQ25ELDRKQUE0SjtRQUM1SixtQkFBbUI7UUFDbkIsTUFBTSxHQUFHLEdBQUcsR0FBRyxDQUFDLElBQUksQ0FBQyxDQUFDLENBQUMsQ0FBQztRQUN4QixJQUFJO1lBQ0YsTUFBTSxHQUFHLEdBQUcsTUFBTSxLQUFLLENBQUMsT0FBTyxDQUFDO2dCQUM5QixHQUFHLEVBQUUsT0FBTyxDQUFDLEdBQUc7Z0JBQ2hCLE1BQU0sRUFBRSxPQUFPLENBQUMsTUFBTTtnQkFDdEIsT0FBTyxFQUFFLEdBQUcsQ0FBQyxPQUFPO2dCQUNwQixJQUFJLEVBQUUsSUFBSTthQUNYLENBQUMsQ0FBQztZQUNILE9BQU8sR0FBRyxDQUFDLElBQUksQ0FBQztTQUNqQjtRQUFDLE9BQU8sQ0FBTSxFQUFFO1lBQ2YsTUFBTSxDQUFDLEtBQUssQ0FBQyxZQUFZLEVBQUUsQ0FBQyxFQUFFLFFBQVEsRUFBRSxJQUFJLENBQUMsQ0FBQztZQUM5QyxNQUFNLEtBQUssR0FBUSxJQUFJLEtBQUssQ0FBQyxDQUFDLEVBQUUsUUFBUSxFQUFFLElBQUksQ0FBQyxPQUFPLENBQUMsQ0FBQztZQUN4RCxLQUFLLENBQUMsSUFBSSxHQUFHLENBQUMsRUFBRSxRQUFRLEVBQUUsSUFBSSxDQUFDO1lBQy9CLE1BQU0sS0FBSyxDQUFDO1NBQ2I7SUFDSCxDQUFDO0NBQ0YifQ==
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
export declare class SigHttpRequest {
|
||||
method: string;
|
||||
host: string;
|
||||
uri: string;
|
||||
query: any;
|
||||
headers: any;
|
||||
body: string;
|
||||
constructor(method: any, url: any, headers: any, body: any);
|
||||
}
|
||||
export declare class Signer {
|
||||
Key: string;
|
||||
Secret: string;
|
||||
constructor(Key: any, Secret: any);
|
||||
Sign(r: any): {
|
||||
hostname: any;
|
||||
path: string;
|
||||
method: any;
|
||||
headers: any;
|
||||
};
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"compileOnSave": true,
|
||||
"compilerOptions": {
|
||||
"target": "ESNext",
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "node",
|
||||
"esModuleInterop": true,
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"inlineSourceMap":true,
|
||||
"noImplicitThis": true,
|
||||
"noUnusedLocals": true,
|
||||
"stripInternal": true,
|
||||
"skipLibCheck": true,
|
||||
"pretty": true,
|
||||
"declaration": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"typeRoots": [ "./typings", "./node_modules/@types"],
|
||||
"outDir": "dist",
|
||||
"rootDir": "src",
|
||||
"composite": true,
|
||||
"useDefineForClassFields": true,
|
||||
"strict": false,
|
||||
"sourceMap": true,
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": false,
|
||||
"lib": ["ESNext", "DOM"],
|
||||
},
|
||||
"include": [
|
||||
"src/**/*.ts",
|
||||
"src/**/*.d.ts",
|
||||
"src/**/*.js",
|
||||
"src/**/*.json"
|
||||
],
|
||||
"exclude": [
|
||||
"*.ts",
|
||||
"dist",
|
||||
"node_modules",
|
||||
"test"
|
||||
],
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"parser": "@typescript-eslint/parser",
|
||||
"plugins": [
|
||||
"@typescript-eslint"
|
||||
],
|
||||
"extends": [
|
||||
"plugin:@typescript-eslint/recommended",
|
||||
"plugin:prettier/recommended",
|
||||
"prettier"
|
||||
],
|
||||
"env": {
|
||||
"mocha": true
|
||||
},
|
||||
"rules": {
|
||||
"@typescript-eslint/no-var-requires": "off",
|
||||
"@typescript-eslint/ban-ts-comment": "off",
|
||||
"@typescript-eslint/ban-ts-ignore": "off",
|
||||
"@typescript-eslint/no-explicit-any": "off",
|
||||
"@typescript-eslint/no-empty-function": "off",
|
||||
// "no-unused-expressions": "off",
|
||||
"max-len": [0, 160, 2, { "ignoreUrls": true }]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
lerna-debug.log*
|
||||
|
||||
node_modules
|
||||
dist
|
||||
dist-ssr
|
||||
*.local
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/*
|
||||
!.vscode/extensions.json
|
||||
.idea
|
||||
.DS_Store
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
||||
|
||||
test/user.secret.ts
|
||||
@@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
src
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"printWidth": 160,
|
||||
"bracketSpacing": true,
|
||||
"singleQuote": true,
|
||||
"trailingComma": "es5",
|
||||
"arrowParens": "avoid"
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
# Change Log
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||
|
||||
# [1.21.0](https://github.com/certd/certd/compare/v1.20.17...v1.21.0) (2024-07-03)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.20.17](https://github.com/certd/certd/compare/v1.20.16...v1.20.17) (2024-07-03)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.20.16](https://github.com/certd/certd/compare/v1.20.15...v1.20.16) (2024-07-01)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.20.15](https://github.com/certd/certd/compare/v1.20.14...v1.20.15) (2024-06-28)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.20.14](https://github.com/certd/certd/compare/v1.20.13...v1.20.14) (2024-06-23)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.20.13](https://github.com/certd/certd/compare/v1.20.12...v1.20.13) (2024-06-18)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.20.12](https://github.com/certd/certd/compare/v1.20.10...v1.20.12) (2024-06-17)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.20.10](https://github.com/certd/certd/compare/v1.20.9...v1.20.10) (2024-05-30)
|
||||
|
||||
### Performance Improvements
|
||||
|
||||
* 优化文件下载包名 ([d9eb927](https://github.com/certd/certd/commit/d9eb927b0a1445feab08b1958aa9ea80637a5ae6))
|
||||
|
||||
## [1.20.9](https://github.com/certd/certd/compare/v1.20.8...v1.20.9) (2024-03-22)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.20.8](https://github.com/certd/certd/compare/v1.20.7...v1.20.8) (2024-03-22)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.20.7](https://github.com/certd/certd/compare/v1.20.6...v1.20.7) (2024-03-22)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.20.6](https://github.com/certd/certd/compare/v1.20.5...v1.20.6) (2024-03-21)
|
||||
|
||||
### Performance Improvements
|
||||
|
||||
* 插件贡献文档及示例 ([72fb20a](https://github.com/certd/certd/commit/72fb20abf3ba5bdd862575d2907703a52fd7eb17))
|
||||
|
||||
## [1.20.5](https://github.com/certd/certd/compare/v1.20.2...v1.20.5) (2024-03-11)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.20.2](https://github.com/certd/certd/compare/v1.2.1...v1.20.2) (2024-02-28)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.2.1](https://github.com/certd/certd/compare/v1.2.0...v1.2.1) (2023-12-12)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
# [1.2.0](https://github.com/certd/certd/compare/v1.1.6...v1.2.0) (2023-10-27)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.1.6](https://github.com/certd/certd/compare/v1.1.5...v1.1.6) (2023-07-10)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.1.5](https://github.com/certd/certd/compare/v1.1.4...v1.1.5) (2023-07-03)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.1.4](https://github.com/certd/certd/compare/v1.1.3...v1.1.4) (2023-07-03)
|
||||
|
||||
### Performance Improvements
|
||||
|
||||
* timeout ([3eeb1f7](https://github.com/certd/certd/commit/3eeb1f77aa2922f3545f3d2067f561d95621d54f))
|
||||
|
||||
## [1.1.3](https://github.com/certd/certd/compare/v1.1.2...v1.1.3) (2023-07-03)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.1.2](https://github.com/certd/certd/compare/v1.1.1...v1.1.2) (2023-07-03)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.1.1](https://github.com/certd/certd/compare/v1.1.0...v1.1.1) (2023-06-28)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
# [1.1.0](https://github.com/certd/certd/compare/v1.0.6...v1.1.0) (2023-06-28)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.0.6](https://github.com/certd/certd/compare/v1.0.5...v1.0.6) (2023-05-25)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.0.5](https://github.com/certd/certd/compare/v1.0.4...v1.0.5) (2023-05-25)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.0.4](https://github.com/certd/certd/compare/v1.0.3...v1.0.4) (2023-05-25)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.0.3](https://github.com/certd/certd/compare/v1.0.2...v1.0.3) (2023-05-25)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.0.2](https://github.com/certd/certd/compare/v1.0.1...v1.0.2) (2023-05-24)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
|
||||
## [1.0.1](https://github.com/certd/certd/compare/v1.0.0...v1.0.1) (2023-05-24)
|
||||
|
||||
**Note:** Version bump only for package @certd/plugin-util
|
||||
@@ -0,0 +1,16 @@
|
||||
# Vue 3 + TypeScript + Vite
|
||||
|
||||
This template should help get you started developing with Vue 3 and TypeScript in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.
|
||||
|
||||
## Recommended IDE Setup
|
||||
|
||||
- [VS Code](https://code.visualstudio.com/) + [Volar](https://marketplace.visualstudio.com/items?itemName=Vue.volar)
|
||||
|
||||
## Type Support For `.vue` Imports in TS
|
||||
|
||||
Since TypeScript cannot handle type information for `.vue` imports, they are shimmed to be a generic Vue component type by default. In most cases this is fine if you don't really care about component prop types outside of templates. However, if you wish to get actual prop types in `.vue` imports (for example to get props validation when using manual `h(...)` calls), you can enable Volar's Take Over mode by following these steps:
|
||||
|
||||
1. Run `Extensions: Show Built-in Extensions` from VS Code's command palette, look for `TypeScript and JavaScript Language Features`, then right click and select `Disable (Workspace)`. By default, Take Over mode will enable itself if the default TypeScript extension is disabled.
|
||||
2. Reload the VS Code window by running `Developer: Reload Window` from the command palette.
|
||||
|
||||
You can learn more about Take Over mode [here](https://github.com/johnsoncodehk/volar/discussions/471).
|
||||
@@ -0,0 +1,96 @@
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
|
||||
// https://gist.github.com/lovasoa/8691344
|
||||
async function* walk(dir) {
|
||||
for await (const d of await fs.promises.opendir(dir)) {
|
||||
const entry = path.join(dir, d.name);
|
||||
if (d.isDirectory()) {
|
||||
yield* walk(entry);
|
||||
} else if (d.isFile()) {
|
||||
yield entry;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function resolveImportPath(sourceFile, importPath, options) {
|
||||
const sourceFileAbs = path.resolve(process.cwd(), sourceFile);
|
||||
const root = path.dirname(sourceFileAbs);
|
||||
const { moduleFilter = defaultModuleFilter } = options;
|
||||
|
||||
if (moduleFilter(importPath)) {
|
||||
const importPathAbs = path.resolve(root, importPath);
|
||||
let possiblePath = [path.resolve(importPathAbs, "./index.ts"), path.resolve(importPathAbs, "./index.js"), importPathAbs + ".ts", importPathAbs + ".js"];
|
||||
|
||||
if (possiblePath.length) {
|
||||
for (let i = 0; i < possiblePath.length; i++) {
|
||||
let entry = possiblePath[i];
|
||||
if (fs.existsSync(entry)) {
|
||||
const resolved = path.relative(root, entry.replace(/\.ts$/, ".js"));
|
||||
|
||||
if (!resolved.startsWith(".")) {
|
||||
return "./" + resolved;
|
||||
}
|
||||
|
||||
return resolved;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function replace(filePath, outFilePath, options) {
|
||||
const code = fs.readFileSync(filePath).toString();
|
||||
const newCode = code.replace(/(import|export) (.+?) from ('[^\n']+'|"[^\n"]+");/gs, function (found, action, imported, from) {
|
||||
const importPath = from.slice(1, -1);
|
||||
let resolvedPath = resolveImportPath(filePath, importPath, options);
|
||||
|
||||
if (resolvedPath) {
|
||||
resolvedPath = resolvedPath.replaceAll("\\", "/");
|
||||
console.log("\t", importPath, resolvedPath);
|
||||
return `${action} ${imported} from "${resolvedPath}";`;
|
||||
}
|
||||
|
||||
return found;
|
||||
});
|
||||
|
||||
if (code !== newCode) {
|
||||
fs.writeFileSync(outFilePath, newCode);
|
||||
}
|
||||
}
|
||||
|
||||
// Then, use it with a simple async for loop
|
||||
async function run(srcDir, options = defaultOptions) {
|
||||
const { sourceFileFilter = defaultSourceFileFilter } = options;
|
||||
|
||||
for await (const entry of walk(srcDir)) {
|
||||
if (sourceFileFilter(entry)) {
|
||||
console.log(entry);
|
||||
replace(entry, entry, options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const defaultSourceFileFilter = function (sourceFilePath) {
|
||||
return /\.(js|ts)$/.test(sourceFilePath) && !/node_modules/.test(sourceFilePath);
|
||||
};
|
||||
|
||||
const defaultModuleFilter = function (importedModule) {
|
||||
return !path.isAbsolute(importedModule) && !importedModule.startsWith("@") && !importedModule.endsWith(".js");
|
||||
};
|
||||
|
||||
const defaultOptions = {
|
||||
sourceFileFilter: defaultSourceFileFilter,
|
||||
moduleFilter: defaultModuleFilter,
|
||||
};
|
||||
|
||||
// Switch this to test on one file or directly run on a directory.
|
||||
const DEBUG = false;
|
||||
|
||||
if (DEBUG) {
|
||||
replace("./src/index.ts", "./out.ts", defaultOptions);
|
||||
} else {
|
||||
await run("./src/", defaultOptions);
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
{
|
||||
"name": "@certd/lib-k8s",
|
||||
"private": false,
|
||||
"version": "1.21.0",
|
||||
"type": "module",
|
||||
"main": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "tsc --skipLibCheck",
|
||||
"build3": "rollup -c",
|
||||
"build2": "vue-tsc --noEmit && vite build",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"dns": "^0.2.2",
|
||||
"kubernetes-client": "^9.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@certd/pipeline": "workspace:^1.21.0",
|
||||
"@rollup/plugin-commonjs": "^23.0.4",
|
||||
"@rollup/plugin-json": "^6.0.0",
|
||||
"@rollup/plugin-node-resolve": "^15.0.1",
|
||||
"@rollup/plugin-terser": "^0.4.3",
|
||||
"@rollup/plugin-typescript": "^11.0.0",
|
||||
"@types/chai": "^4.3.3",
|
||||
"@typescript-eslint/eslint-plugin": "^5.38.1",
|
||||
"@typescript-eslint/parser": "^5.38.1",
|
||||
"eslint": "^8.24.0",
|
||||
"eslint-config-prettier": "^8.5.0",
|
||||
"eslint-plugin-import": "^2.26.0",
|
||||
"eslint-plugin-node": "^11.1.0",
|
||||
"eslint-plugin-prettier": "^4.2.1",
|
||||
"prettier": "^2.8.8",
|
||||
"rollup": "^3.7.4",
|
||||
"rollup-plugin-visualizer": "^5.8.2",
|
||||
"ts-node": "^10.9.1",
|
||||
"tslib": "^2.5.2",
|
||||
"typescript": "^4.8.4"
|
||||
},
|
||||
"gitHead": "a31f1c7f5e71fa946de9bf0283e11d6ce049b3e9"
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export * from "./lib/k8s.client.js";
|
||||
@@ -0,0 +1,116 @@
|
||||
import kubernetesClient from 'kubernetes-client';
|
||||
import dns from 'dns';
|
||||
import { logger } from '@certd/pipeline';
|
||||
|
||||
// @ts-ignore
|
||||
const { KubeConfig, Client, Request } = kubernetesClient;
|
||||
|
||||
export class K8sClient {
|
||||
kubeConfigStr: string;
|
||||
lookup!: any;
|
||||
client!: any;
|
||||
constructor(kubeConfigStr: string) {
|
||||
this.kubeConfigStr = kubeConfigStr;
|
||||
this.init();
|
||||
}
|
||||
|
||||
init() {
|
||||
const kubeconfig = new KubeConfig();
|
||||
kubeconfig.loadFromString(this.kubeConfigStr);
|
||||
const reqOpts = { kubeconfig, request: {} } as any;
|
||||
if (this.lookup) {
|
||||
reqOpts.request.lookup = this.lookup;
|
||||
}
|
||||
|
||||
const backend = new Request(reqOpts);
|
||||
this.client = new Client({ backend, version: '1.13' });
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param localRecords { [domain]:{ip:'xxx.xx.xxx'} }
|
||||
*/
|
||||
setLookup(localRecords: { [key: string]: { ip: string } }) {
|
||||
this.lookup = (hostnameReq: any, options: any, callback: any) => {
|
||||
logger.info('custom lookup', hostnameReq, localRecords);
|
||||
if (localRecords[hostnameReq]) {
|
||||
logger.info('local record', hostnameReq, localRecords[hostnameReq]);
|
||||
callback(null, localRecords[hostnameReq].ip, 4);
|
||||
} else {
|
||||
dns.lookup(hostnameReq, options, callback);
|
||||
}
|
||||
};
|
||||
this.init();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询 secret列表
|
||||
* @param opts = {namespace:default}
|
||||
* @returns secretsList
|
||||
*/
|
||||
async getSecret(opts: { namespace: string }) {
|
||||
const namespace = opts.namespace || 'default';
|
||||
return await this.client.api.v1.namespaces(namespace).secrets.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建Secret
|
||||
* @param opts {namespace:default, body:yamlStr}
|
||||
* @returns {Promise<*>}
|
||||
*/
|
||||
async createSecret(opts: any) {
|
||||
const namespace = opts.namespace || 'default';
|
||||
const created = await this.client.api.v1.namespaces(namespace).secrets.post({
|
||||
body: opts.body,
|
||||
});
|
||||
logger.info('new secrets:', created);
|
||||
return created;
|
||||
}
|
||||
|
||||
async updateSecret(opts: any) {
|
||||
const namespace = opts.namespace || 'default';
|
||||
const secretName = opts.secretName;
|
||||
if (secretName == null) {
|
||||
throw new Error('secretName 不能为空');
|
||||
}
|
||||
return await this.client.api.v1.namespaces(namespace).secrets(secretName).put({
|
||||
body: opts.body,
|
||||
});
|
||||
}
|
||||
|
||||
async patchSecret(opts: any) {
|
||||
const namespace = opts.namespace || 'default';
|
||||
const secretName = opts.secretName;
|
||||
if (secretName == null) {
|
||||
throw new Error('secretName 不能为空');
|
||||
}
|
||||
return await this.client.api.v1.namespaces(namespace).secrets(secretName).patch({
|
||||
body: opts.body,
|
||||
});
|
||||
}
|
||||
|
||||
async getIngressList(opts: any) {
|
||||
const namespace = opts.namespace || 'default';
|
||||
return await this.client.apis.extensions.v1beta1.namespaces(namespace).ingresses.get();
|
||||
}
|
||||
|
||||
async getIngress(opts: any) {
|
||||
const namespace = opts.namespace || 'default';
|
||||
const ingressName = opts.ingressName;
|
||||
if (!ingressName) {
|
||||
throw new Error('ingressName 不能为空');
|
||||
}
|
||||
return await this.client.apis.extensions.v1beta1.namespaces(namespace).ingresses(ingressName).get();
|
||||
}
|
||||
|
||||
async patchIngress(opts: any) {
|
||||
const namespace = opts.namespace || 'default';
|
||||
const ingressName = opts.ingressName;
|
||||
if (!ingressName) {
|
||||
throw new Error('ingressName 不能为空');
|
||||
}
|
||||
return await this.client.apis.extensions.v1beta1.namespaces(namespace).ingresses(ingressName).patch({
|
||||
body: opts.body,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"compileOnSave": true,
|
||||
"compilerOptions": {
|
||||
"target": "ESNext",
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "node",
|
||||
"esModuleInterop": true,
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"inlineSourceMap":true,
|
||||
"noImplicitThis": true,
|
||||
"noUnusedLocals": true,
|
||||
"stripInternal": true,
|
||||
"skipLibCheck": true,
|
||||
"pretty": true,
|
||||
"declaration": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"typeRoots": [ "./typings", "./node_modules/@types"],
|
||||
"outDir": "dist",
|
||||
"rootDir": "src",
|
||||
"composite": true,
|
||||
"useDefineForClassFields": true,
|
||||
"strict": false,
|
||||
// "sourceMap": true,
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"lib": ["ESNext", "DOM"],
|
||||
},
|
||||
"include": [
|
||||
"src/**/*.ts",
|
||||
"src/**/*.d.ts",
|
||||
"src/**/*.json"
|
||||
],
|
||||
"exclude": [
|
||||
"*.js",
|
||||
"*.ts",
|
||||
"dist",
|
||||
"node_modules",
|
||||
"test"
|
||||
],
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
logs/
|
||||
npm-debug.log
|
||||
yarn-error.log
|
||||
node_modules/
|
||||
package-lock.json
|
||||
yarn.lock
|
||||
coverage/
|
||||
!dist/
|
||||
.idea/
|
||||
run/
|
||||
.DS_Store
|
||||
*.sw*
|
||||
*.un~
|
||||
.tsbuildinfo
|
||||
.tsbuildinfo.*
|
||||
/data/db.sqlite
|
||||
@@ -0,0 +1,11 @@
|
||||
# 🎨 editorconfig.org
|
||||
|
||||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"extends": "./node_modules/mwts/",
|
||||
"ignorePatterns": ["node_modules", "dist", "test", "jest.config.js", "typings"],
|
||||
"env": {
|
||||
"jest": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
lerna-debug.log*
|
||||
|
||||
node_modules
|
||||
dist
|
||||
dist-ssr
|
||||
*.local
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/*
|
||||
!.vscode/extensions.json
|
||||
.idea
|
||||
.DS_Store
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
||||
|
||||
test/user.secret.ts
|
||||
|
||||
tsconfig.tsbuildinfo
|
||||
@@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
src
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"printWidth": 160,
|
||||
"bracketSpacing": true,
|
||||
"singleQuote": true,
|
||||
"trailingComma": "es5",
|
||||
"arrowParens": "avoid"
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 Greper
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,16 @@
|
||||
# Vue 3 + TypeScript + Vite
|
||||
|
||||
This template should help get you started developing with Vue 3 and TypeScript in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.
|
||||
|
||||
## Recommended IDE Setup
|
||||
|
||||
- [VS Code](https://code.visualstudio.com/) + [Volar](https://marketplace.visualstudio.com/items?itemName=Vue.volar)
|
||||
|
||||
## Type Support For `.vue` Imports in TS
|
||||
|
||||
Since TypeScript cannot handle type information for `.vue` imports, they are shimmed to be a generic Vue component type by default. In most cases this is fine if you don't really care about component prop types outside of templates. However, if you wish to get actual prop types in `.vue` imports (for example to get props validation when using manual `h(...)` calls), you can enable Volar's Take Over mode by following these steps:
|
||||
|
||||
1. Run `Extensions: Show Built-in Extensions` from VS Code's command palette, look for `TypeScript and JavaScript Language Features`, then right click and select `Disable (Workspace)`. By default, Take Over mode will enable itself if the default TypeScript extension is disabled.
|
||||
2. Reload the VS Code window by running `Developer: Reload Window` from the command palette.
|
||||
|
||||
You can learn more about Take Over mode [here](https://github.com/johnsoncodehk/volar/discussions/471).
|
||||
@@ -0,0 +1,125 @@
|
||||
# midway-flyway-js
|
||||
|
||||
[English](./README.md) | [简体中文](./README_zhCN.md)
|
||||
|
||||
|
||||
`midway-flyway-js`是基于typeorm的flyway的js实现。
|
||||
本项目被构建为midway组件,可与midway无缝集成。
|
||||
|
||||
# flyway
|
||||
flyway是一款java版本的数据库升级迁移解决方案。
|
||||
它能在server启动时自动检查脚本目录,执行sql升级脚本,记录执行历史。
|
||||
|
||||
本项目根据类似flyway的思路实现数据库升级迁移方案
|
||||
|
||||
# 快速开始
|
||||
|
||||
## 1. 准备
|
||||
* nodejs环境
|
||||
* midway项目
|
||||
* [配置typeorm](https://www.yuque.com/midwayjs/midway_v2/orm)
|
||||
|
||||
## 2. 安装
|
||||
```
|
||||
npm install midway-flyway-js
|
||||
# or
|
||||
yarn add midway-flyway-js
|
||||
```
|
||||
## 3. 集成
|
||||
```js
|
||||
import * as orm from 'typeorm';
|
||||
import * as flyway from 'midway-flyway-js';
|
||||
@Configuration({
|
||||
imports: [
|
||||
orm, // 加载 orm 组件
|
||||
flyway, //加载flyway组件
|
||||
],
|
||||
})
|
||||
export class ContainerConfiguration {}
|
||||
```
|
||||
|
||||
|
||||
## 4. 配置参数【可选】
|
||||
`/src/config/config.default.js`文件
|
||||
```js
|
||||
export const flyway ={
|
||||
// 脚本目录
|
||||
// 默认值 "./db/migrition"
|
||||
scriptDir:"./db/migrition",
|
||||
// 基线,基线脚本及之前的脚本都跳过不执行
|
||||
// 默认值:null
|
||||
// 如果你原本就是空数据库,那么不需要配置此项
|
||||
baseline: 'v1__init.sql',
|
||||
// 执行记录表名
|
||||
// 默认值 flyway_history
|
||||
flywayTableName:'flyway_history',
|
||||
// 是否允许hash值不同
|
||||
// 默认值:false
|
||||
// 相同名称sql文件被改动后,hash会变化
|
||||
// 此时运行会报hash conflict错误
|
||||
// 配置此参数为true,将忽略hash conflict错误
|
||||
allowHashNotMatch:false
|
||||
}
|
||||
|
||||
```
|
||||
## 5. 编写升级sql
|
||||
|
||||
将你的sql升级脚本,放到 `/src/db/migrition`目录下
|
||||
|
||||
建议命名规则`v{version}__{name}.sql`,例如`v1__init.sql`
|
||||
|
||||
|
||||
## 6. 启动你的midway服务
|
||||
```
|
||||
npm run dev
|
||||
```
|
||||
|
||||
## 7. 运行效果
|
||||
以下效果为midway自动启动后,自动执行`v1__init.sql`脚本的记录
|
||||
```
|
||||
2021-06-26 15:45:39,630 INFO 12245 [ midfly ] start-------------
|
||||
query: SELECT * FROM "sqlite_master" WHERE "type" = 'table' AND "name" = 'flyway_history'
|
||||
query: CREATE TABLE "flyway_history" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "timestamp" bigint NOT NULL, "name" varchar NOT NULL, "hash" varchar, "success" boolean)
|
||||
query: BEGIN TRANSACTION
|
||||
query: SELECT "FlywayHistory"."id" AS "FlywayHistory_id", "FlywayHistory"."name" AS "FlywayHistory_name", "FlywayHistory"."hash" AS "FlywayHistory_hash", "FlywayHistory"."timestamp" AS "FlywayHistory_timestamp", "FlywayHistory"."success" AS "FlywayHistory_success" FROM "flyway_history" "FlywayHistory" WHERE "FlywayHistory"."name" = ? AND "FlywayHistory"."success" = ? LIMIT 1 -- PARAMETERS: ["v1__init.sql",1]
|
||||
2021-06-26 15:45:39,664 INFO 12245 need exec script file:
|
||||
2021-06-26 15:45:39,666 INFO 12245 [ midfly ] exec
|
||||
query: -- 表:sys_permission
|
||||
CREATE TABLE "sys_permission" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(100) NOT NULL, "permission" varchar(100), "parent_id" integer NOT NULL DEFAULT (-1), "sort" integer NOT NULL, "create_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "update_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP));
|
||||
query: INSERT INTO sys_permission (id, title, permission, parent_id, sort, create_time, update_time) VALUES (1, '系统管理', 'sys', -1, 1, 1, 1624085863636);
|
||||
query: -- 表:sys_role
|
||||
CREATE TABLE "sys_role" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(100) NOT NULL, "create_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "update_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP));
|
||||
query: INSERT INTO sys_role (id, name, create_time, update_time) VALUES (1, '管理员', 1, 1623749138537);
|
||||
query: -- 表:sys_role_permission
|
||||
CREATE TABLE "sys_role_permission" ("role_id" integer NOT NULL, "permission_id" integer NOT NULL, PRIMARY KEY ("role_id", "permission_id"));
|
||||
query: INSERT INTO sys_role_permission (role_id, permission_id) VALUES (1, 1);
|
||||
query: -- 表:sys_user
|
||||
CREATE TABLE "sys_user" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "username" varchar(100) NOT NULL, "password" varchar(50) NOT NULL, "nick_name" varchar(50), "avatar" varchar(255), "phone_code" varchar(20), "mobile" varchar(20), "email" varchar(100),"remark" varchar(100), "status" integer NOT NULL DEFAULT (1), "create_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "update_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP));
|
||||
query: INSERT INTO sys_user (id, username, password, nick_name, avatar, phone_code, mobile, email, status, create_time, update_time,remark) VALUES (1, 'admin', 'e10adc3949ba59abbe56e057f20f883e', 'admin', NULL, NULL, NULL, NULL, 1, 2011123132, 123132,NULL);
|
||||
query: -- 表:sys_user_role
|
||||
CREATE TABLE "sys_user_role" ("role_id" integer NOT NULL, "user_id" integer NOT NULL, PRIMARY KEY ("role_id", "user_id"));
|
||||
query: INSERT INTO sys_user_role (role_id, user_id) VALUES (1, 1);
|
||||
query: -- 索引:IDX_223de54d6badbe43a5490450c3
|
||||
CREATE UNIQUE INDEX "IDX_223de54d6badbe43a5490450c3" ON "sys_role" ("name");
|
||||
query: -- 索引:IDX_9e7164b2f1ea1348bc0eb0a7da
|
||||
CREATE UNIQUE INDEX "IDX_9e7164b2f1ea1348bc0eb0a7da" ON "sys_user" ("username");
|
||||
query: DELETE FROM "flyway_history" WHERE "name" = ? -- PARAMETERS: ["v1__init.sql"]
|
||||
query: INSERT INTO "flyway_history"("id", "name", "hash", "timestamp", "success") VALUES (NULL, ?, ?, ?, ?) -- PARAMETERS: ["v1__init.sql","0c661bd7afebac224bbaa60bc5bb56e9",1624693539781,1]
|
||||
query: SELECT "FlywayHistory"."id" AS "FlywayHistory_id", "FlywayHistory"."success" AS "FlywayHistory_success" FROM "flyway_history" "FlywayHistory" WHERE "FlywayHistory"."id" = ? -- PARAMETERS: [1]
|
||||
query: COMMIT
|
||||
2021-06-26 15:45:39,800 INFO 12245 [ midfly ] end-------------
|
||||
```
|
||||
|
||||
# 注意事项
|
||||
1. 升级sql文件最后一行请不要有注释,应该以一条sql语句的分号结尾。
|
||||
|
||||
# 他们在用
|
||||
* [fs-server-js](https://github.com/fast-crud/fs-server-js)
|
||||
|
||||
# 参考项目
|
||||
* [flyway](https://github.com/flyway/flyway) : java版flyway
|
||||
* [flyway-js](https://github.com/wanglihui/flyway-js) : Sequelize版flyway
|
||||
|
||||
感谢以上项目
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
module.exports = {
|
||||
preset: 'ts-jest',
|
||||
testEnvironment: 'node',
|
||||
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
|
||||
coveragePathIgnorePatterns: ['<rootDir>/test/'],
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"type": "sqlite",
|
||||
"database": "./data/db.sqlite",
|
||||
"synchronize": false,
|
||||
"logging": true,
|
||||
"entities": [ "src/**/entity.ts"]
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
{
|
||||
"name": "@certd/midway-flyway-js",
|
||||
"version": "1.21.0",
|
||||
"description": "midway with flyway, sql upgrade way ",
|
||||
"private": false,
|
||||
"type": "module",
|
||||
"main": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"scripts": {
|
||||
"build": "tsc --skipLibCheck",
|
||||
"test": "midway-bin test --ts -V",
|
||||
"test1": "midway-bin test --ts -V -f test/blank.test.ts -t 'hash-check'",
|
||||
"cov": "midway-bin cov --ts",
|
||||
"lint": "mwts check",
|
||||
"lint:fix": "mwts fix",
|
||||
"prepublish": "npm run build",
|
||||
"pub": "npm publish"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "greper",
|
||||
"files": [
|
||||
"dist/**/*.js",
|
||||
"dist/**/*.d.ts"
|
||||
],
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@midwayjs/core": "^3",
|
||||
"@midwayjs/logger": "^3",
|
||||
"@midwayjs/typeorm": "^3",
|
||||
"typeorm": "^0.3.11",
|
||||
"@types/node": "16",
|
||||
"cross-env": "^6.0.0",
|
||||
"mwts": "^1.3.0",
|
||||
"mwtsc": "^1.4.0",
|
||||
"sqlite3": "^5.0.2",
|
||||
"typescript": "~5.1.0",
|
||||
"@rollup/plugin-commonjs": "^23.0.4",
|
||||
"@rollup/plugin-json": "^6.0.0",
|
||||
"@rollup/plugin-node-resolve": "^15.0.1",
|
||||
"@rollup/plugin-terser": "^0.4.3",
|
||||
"@rollup/plugin-typescript": "^11.0.0",
|
||||
"@types/chai": "^4.3.3",
|
||||
"@typescript-eslint/eslint-plugin": "^5.38.1",
|
||||
"@typescript-eslint/parser": "^5.38.1",
|
||||
"eslint": "^8.24.0",
|
||||
"eslint-config-prettier": "^8.5.0",
|
||||
"eslint-plugin-import": "^2.26.0",
|
||||
"eslint-plugin-node": "^11.1.0",
|
||||
"eslint-plugin-prettier": "^4.2.1",
|
||||
"prettier": "^2.8.8",
|
||||
"rollup": "^3.7.4",
|
||||
"rollup-plugin-visualizer": "^5.8.2",
|
||||
"ts-node": "^10.9.1",
|
||||
"tslib": "^2.5.2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Config, Configuration, Logger } from '@midwayjs/core';
|
||||
import { Flyway } from './flyway.js';
|
||||
import type { ILogger } from '@midwayjs/logger';
|
||||
import { TypeORMDataSourceManager } from '@midwayjs/typeorm';
|
||||
import type { IMidwayContainer } from '@midwayjs/core';
|
||||
|
||||
@Configuration({
|
||||
namespace: 'flyway',
|
||||
//importConfigs: [join(__dirname, './config')],
|
||||
})
|
||||
export class FlywayConfiguration {
|
||||
@Config()
|
||||
flyway!: any;
|
||||
@Logger()
|
||||
logger!: ILogger;
|
||||
async onReady(container: IMidwayContainer) {
|
||||
this.logger.info('flyway start:' + JSON.stringify(this.flyway));
|
||||
const dataSourceManager = await container.getAsync(TypeORMDataSourceManager);
|
||||
const dataSourceName = this.flyway.dataSourceName || 'default';
|
||||
const connection = dataSourceManager.getDataSource(dataSourceName);
|
||||
await new Flyway({ ...this.flyway, logger: this.logger, connection }).run();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
||||
|
||||
@Entity('flyway_history')
|
||||
export class FlywayHistory {
|
||||
@PrimaryGeneratedColumn()
|
||||
id?: number;
|
||||
|
||||
@Column({ comment: '文件名', length: 100 })
|
||||
name?: string;
|
||||
|
||||
@Column({ comment: 'hash', length: 32 })
|
||||
hash?: string;
|
||||
|
||||
@Column({
|
||||
comment: '执行时间',
|
||||
})
|
||||
timestamp?: Date;
|
||||
|
||||
@Column({
|
||||
comment: '执行成功',
|
||||
default: true,
|
||||
})
|
||||
success?: boolean;
|
||||
}
|
||||
@@ -0,0 +1,315 @@
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
import { QueryRunner, Table } from 'typeorm';
|
||||
import { FlywayHistory } from './entity.js';
|
||||
import * as crypto from 'crypto';
|
||||
|
||||
/**
|
||||
* 脚本文件信息
|
||||
*/
|
||||
class ScriptFile {
|
||||
script: string;
|
||||
isBaseline = false;
|
||||
|
||||
constructor(fileName: any, isBaseline: any) {
|
||||
this.script = fileName;
|
||||
this.isBaseline = isBaseline;
|
||||
}
|
||||
}
|
||||
|
||||
const DefaultLogger = {
|
||||
debug: function (...args: any) {
|
||||
console.log(args);
|
||||
},
|
||||
info: function (...args: any) {
|
||||
console.log(args);
|
||||
},
|
||||
warn: function (...args: any) {
|
||||
console.warn(args);
|
||||
},
|
||||
error: function (...args: any) {
|
||||
console.error(args);
|
||||
},
|
||||
};
|
||||
export class Flyway {
|
||||
scriptDir;
|
||||
flywayTableName;
|
||||
baseline;
|
||||
allowHashNotMatch;
|
||||
connection;
|
||||
logger;
|
||||
constructor(opts: any) {
|
||||
this.scriptDir = opts.scriptDir ?? 'db/migration';
|
||||
this.flywayTableName = opts.flywayTableName ?? 'flyway_history';
|
||||
this.baseline = opts.baseline ?? false;
|
||||
this.allowHashNotMatch = opts.allowHashNotMatch ?? false;
|
||||
this.logger = opts.logger || DefaultLogger;
|
||||
this.connection = opts.connection;
|
||||
}
|
||||
|
||||
async run(ignores?: (RegExp | string)[]) {
|
||||
this.logger.info('[ midfly ] start-------------');
|
||||
if (!fs.existsSync(this.scriptDir)) {
|
||||
this.logger.info('[ midfly ] scriptDir<' + this.scriptDir + '> not found');
|
||||
return;
|
||||
}
|
||||
|
||||
const scriptFiles = await this.loadScriptFiles();
|
||||
const queryRunner = this.connection.createQueryRunner();
|
||||
await this.prepare(queryRunner);
|
||||
for (const file of scriptFiles) {
|
||||
if (this.isNeedIgnore(file.script, ignores)) {
|
||||
continue;
|
||||
}
|
||||
const filepath = path.resolve(this.scriptDir, file.script);
|
||||
|
||||
await queryRunner.startTransaction();
|
||||
try {
|
||||
//查找是否已经执行
|
||||
if (await this.hasExec(file.script, filepath, queryRunner)) {
|
||||
await queryRunner.commitTransaction();
|
||||
continue;
|
||||
}
|
||||
if (!file.isBaseline) {
|
||||
this.logger.info('need exec script file: ', file.script);
|
||||
//执行sql文件
|
||||
if (/\.sql$/.test(file.script)) {
|
||||
await this.execSql(filepath, queryRunner);
|
||||
}
|
||||
// 执行js或者ts
|
||||
// if (/\.(js|ts)$/.test(file.script)) {
|
||||
// await this.execJsOrTs(filepath, t);
|
||||
// }
|
||||
} else {
|
||||
this.logger.info('baseline script file: ', file.script);
|
||||
}
|
||||
await this.storeSqlExecLog(file.script, filepath, true, queryRunner);
|
||||
await queryRunner.commitTransaction();
|
||||
} catch (err) {
|
||||
this.logger.error(err);
|
||||
await this.storeSqlExecLog(file.script, filepath, false, queryRunner);
|
||||
await queryRunner.rollbackTransaction();
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
this.logger.info('[ midfly ] end-------------');
|
||||
}
|
||||
|
||||
private async storeSqlExecLog(filename: string, filepath: string, success: boolean, queryRunner: QueryRunner) {
|
||||
const hash = await this.getFileHash(filepath);
|
||||
//先删除
|
||||
await queryRunner.manager.delete(FlywayHistory, {
|
||||
name: filename,
|
||||
});
|
||||
const history = await queryRunner.manager.insert(FlywayHistory, {
|
||||
name: filename,
|
||||
hash,
|
||||
timestamp: new Date().getTime(),
|
||||
success,
|
||||
});
|
||||
return history;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取升级脚本文件
|
||||
* @private
|
||||
*/
|
||||
private async loadScriptFiles() {
|
||||
const files = fs.readdirSync(this.scriptDir);
|
||||
files.sort();
|
||||
// 获取基准脚本的位置
|
||||
const local = files.indexOf(this.baseline);
|
||||
const scriptFiles = new Array<ScriptFile>();
|
||||
files.forEach((file, index) => {
|
||||
if (index <= local) {
|
||||
// 基准脚本和基准脚本之前的脚本都不执行
|
||||
scriptFiles.push(new ScriptFile(file, true));
|
||||
} else {
|
||||
scriptFiles.push(new ScriptFile(file, false));
|
||||
}
|
||||
});
|
||||
return scriptFiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建history表
|
||||
* @private
|
||||
*/
|
||||
private async prepare(queryRunner: QueryRunner) {
|
||||
await this.createFlywayTableIfNotExist(queryRunner);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates table "flyway_history" that will store information about executed migrations.
|
||||
*/
|
||||
protected async createFlywayTableIfNotExist(queryRunner: QueryRunner): Promise<void> {
|
||||
// If driver is mongo no need to create
|
||||
// if (this.connection.driver instanceof MongoDriver) {
|
||||
// return;
|
||||
// }
|
||||
const tableExist = await queryRunner.hasTable(this.flywayTableName); // todo: table name should be configurable
|
||||
if (!tableExist) {
|
||||
await queryRunner.createTable(
|
||||
new Table({
|
||||
name: this.flywayTableName,
|
||||
columns: [
|
||||
{
|
||||
name: 'id',
|
||||
type: this.connection.driver.normalizeType({
|
||||
type: this.connection.driver.mappedDataTypes.migrationId,
|
||||
}),
|
||||
isGenerated: true,
|
||||
generationStrategy: 'increment',
|
||||
isPrimary: true,
|
||||
isNullable: false,
|
||||
},
|
||||
{
|
||||
name: 'timestamp',
|
||||
type: this.connection.driver.normalizeType({
|
||||
type: this.connection.driver.mappedDataTypes.migrationTimestamp,
|
||||
}),
|
||||
isPrimary: false,
|
||||
isNullable: false,
|
||||
},
|
||||
{
|
||||
name: 'name',
|
||||
type: this.connection.driver.normalizeType({
|
||||
type: this.connection.driver.mappedDataTypes.migrationName,
|
||||
}),
|
||||
isNullable: false,
|
||||
},
|
||||
{
|
||||
name: 'hash',
|
||||
type: this.connection.driver.normalizeType({
|
||||
type: this.connection.driver.mappedDataTypes.migrationName,
|
||||
}),
|
||||
isNullable: true,
|
||||
},
|
||||
{
|
||||
name: 'success',
|
||||
type: this.connection.driver.normalizeType({
|
||||
type: 'boolean',
|
||||
}),
|
||||
isNullable: true,
|
||||
},
|
||||
],
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private isNeedIgnore(file: string, ignores?: (RegExp | string)[]): boolean {
|
||||
if (!ignores) {
|
||||
ignores = [/\.js\.map$/, /\.d\.ts$/];
|
||||
}
|
||||
let ret = false;
|
||||
for (const ignore of ignores) {
|
||||
if (typeof ignore === 'string' && file === ignore) {
|
||||
ret = true;
|
||||
break;
|
||||
}
|
||||
if (ignore instanceof RegExp && ignore.test(file)) {
|
||||
ret = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
private async hasExec(file: string, filepath: string, queryRunner: QueryRunner): Promise<boolean> {
|
||||
const hash = await this.getFileHash(filepath);
|
||||
|
||||
const history = await queryRunner.manager.findOne(FlywayHistory, {
|
||||
where: { name: file, success: true },
|
||||
});
|
||||
|
||||
if (history) {
|
||||
if (history.hash !== hash && this.allowHashNotMatch === false) {
|
||||
throw new Error(file + `hash conflict ,old: ${history.hash} != new: ${hash}`);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private async getFileHash(filepath: string) {
|
||||
const content = fs.readFileSync(filepath).toString();
|
||||
return crypto.createHash('md5').update(content.toString()).digest('hex');
|
||||
}
|
||||
|
||||
private async execSql(filepath: string, queryRunner: QueryRunner) {
|
||||
this.logger.info('[ midfly ] exec ', filepath);
|
||||
const content = fs.readFileSync(filepath).toString().trim();
|
||||
const arr = this.splitSql2Array(content);
|
||||
for (const s of arr) {
|
||||
await this.execOnePart(s, queryRunner);
|
||||
}
|
||||
}
|
||||
|
||||
private async execOnePart(sql: string, queryRunner: QueryRunner) {
|
||||
this.logger.debug('exec sql index: ', sql);
|
||||
try {
|
||||
await queryRunner.query(sql);
|
||||
} catch (err: any) {
|
||||
this.logger.error('exec sql error : ', err.message, err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串分割为数组
|
||||
* @param {string} str 字符串
|
||||
*/
|
||||
splitSql2Array(str: any) {
|
||||
if (!str) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const temp = String(str).trim();
|
||||
|
||||
if (temp === 'null') {
|
||||
return [];
|
||||
}
|
||||
|
||||
const semicolon = ';';
|
||||
const deepChars = ['"', "'"];
|
||||
const splits = [];
|
||||
|
||||
const deepQueue: any = [];
|
||||
for (let i = 0; i < temp.length; i++) {
|
||||
const charAt = temp.charAt(i);
|
||||
|
||||
if (deepChars.indexOf(charAt) >= 0) {
|
||||
//如果是深度char
|
||||
if (i !== 0 && temp.charAt(i - 1) === '\\') {
|
||||
//如果前一个是转义字符,忽略它
|
||||
} else {
|
||||
//说明需要进出深度了
|
||||
if (deepQueue.length === 0 || deepQueue[deepQueue.length - 1] !== charAt) {
|
||||
//进入深度
|
||||
deepQueue.push(charAt);
|
||||
} else {
|
||||
//退出深度
|
||||
deepQueue.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
//当深度为0,则记录分割点
|
||||
if (charAt === semicolon && deepQueue.length === 0) {
|
||||
splits.push(i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
//分割sql
|
||||
|
||||
const arr = [];
|
||||
let lastIndex = 0;
|
||||
for (const index of splits) {
|
||||
const sql = temp.substring(lastIndex, index);
|
||||
lastIndex = index;
|
||||
arr.push(sql.trim());
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// src/index.ts
|
||||
export { FlywayConfiguration as Configuration } from './configuration.js';
|
||||
// eslint-disable-next-line node/no-unpublished-import
|
||||
export { Flyway } from './flyway.js';
|
||||
// eslint-disable-next-line node/no-unpublished-import
|
||||
export { FlywayHistory } from './entity.js';
|
||||
@@ -0,0 +1,4 @@
|
||||
end -----
|
||||
```
|
||||
# test
|
||||
The last line of the SQL file should be uncommented and should end with a semicolon of the SQL statement.
|
||||
@@ -0,0 +1,3 @@
|
||||
-- no sql
|
||||
|
||||
CREATE TABLE "sys_permission" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(100) NOT NULL, "permission" varchar(100), "parent_id" integer NOT NULL DEFAULT (-1), "sort" integer NOT NULL, "create_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "update_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP));
|
||||
@@ -0,0 +1,32 @@
|
||||
-- 表:sys_permission
|
||||
CREATE TABLE "sys_permission" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(100) NOT NULL, "permission" varchar(100), "parent_id" integer NOT NULL DEFAULT (-1), "sort" integer NOT NULL, "create_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "update_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP));
|
||||
INSERT INTO sys_permission (id, title, permission, parent_id, sort, create_time, update_time) VALUES (1, '系统管理', 'sys', -1, 1, 1, 1624085863636);
|
||||
|
||||
|
||||
|
||||
-- 表:sys_role
|
||||
CREATE TABLE "sys_role" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(100) NOT NULL, "create_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "update_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP));
|
||||
INSERT INTO sys_role (id, name, create_time, update_time) VALUES (1, '管理员', 1, 1623749138537);
|
||||
INSERT INTO sys_role (id, name, create_time, update_time) VALUES (2, '只读角色', 1, 1623749138537);
|
||||
|
||||
-- 表:sys_role_permission
|
||||
CREATE TABLE "sys_role_permission" ("role_id" integer NOT NULL, "permission_id" integer NOT NULL, PRIMARY KEY ("role_id", "permission_id"));
|
||||
INSERT INTO sys_role_permission (role_id, permission_id) VALUES (1, 1);
|
||||
|
||||
-- 表:sys_user
|
||||
CREATE TABLE "sys_user" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "username" varchar(100) NOT NULL, "password" varchar(50) NOT NULL, "nick_name" varchar(50), "avatar" varchar(255), "phone_code" varchar(20), "mobile" varchar(20), "email" varchar(100),"remark" varchar(100), "status" integer NOT NULL DEFAULT (1), "create_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "update_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP));
|
||||
INSERT INTO sys_user (id, username, password, nick_name, avatar, phone_code, mobile, email, status, create_time, update_time,remark) VALUES (1, 'admin', 'e10adc3949ba59abbe56e057f20f883e', 'admin', NULL, NULL, NULL, NULL, 1, 2011123132, 123132,NULL);
|
||||
INSERT INTO sys_user (id, username, password, nick_name, avatar, phone_code, mobile, email, status, create_time, update_time,remark) VALUES (2, 'readonly', 'e10adc3949ba59abbe56e057f20f883e', '只读用户', NULL, NULL, NULL, NULL, 1, 2011123132, 123132,'密码:123456');
|
||||
|
||||
-- 表:sys_user_role
|
||||
CREATE TABLE "sys_user_role" ("role_id" integer NOT NULL, "user_id" integer NOT NULL, PRIMARY KEY ("role_id", "user_id"));
|
||||
INSERT INTO sys_user_role (role_id, user_id) VALUES (1, 1);
|
||||
INSERT INTO sys_user_role (role_id, user_id) VALUES (2, 2);
|
||||
|
||||
-- 索引:IDX_223de54d6badbe43a5490450c3
|
||||
CREATE UNIQUE INDEX "IDX_223de54d6badbe43a5490450c3" ON "sys_role" ("name");
|
||||
|
||||
-- 索引:IDX_9e7164b2f1ea1348bc0eb0a7da
|
||||
CREATE UNIQUE INDEX "IDX_9e7164b2f1ea1348bc0eb0a7da" ON "sys_user" ("username");
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
|
||||
-- this is blank sql, 注释不要放在结尾
|
||||
select 1;
|
||||
@@ -0,0 +1,32 @@
|
||||
-- 表:sys_permission
|
||||
CREATE TABLE "sys_permission" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(100) NOT NULL, "permission" varchar(100), "parent_id" integer NOT NULL DEFAULT (-1), "sort" integer NOT NULL, "create_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "update_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP));
|
||||
INSERT INTO sys_permission (id, title, permission, parent_id, sort, create_time, update_time) VALUES (1, '系统管理', 'sys', -1, 1, 1, 1624085863636);
|
||||
|
||||
|
||||
|
||||
-- 表:sys_role
|
||||
CREATE TABLE "sys_role" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(100) NOT NULL, "create_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "update_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP));
|
||||
INSERT INTO sys_role (id, name, create_time, update_time) VALUES (1, '管理员', 1, 1623749138537);
|
||||
INSERT INTO sys_role (id, name, create_time, update_time) VALUES (2, '只读角色', 1, 1623749138537);
|
||||
|
||||
-- 表:sys_role_permission
|
||||
CREATE TABLE "sys_role_permission" ("role_id" integer NOT NULL, "permission_id" integer NOT NULL, PRIMARY KEY ("role_id", "permission_id"));
|
||||
INSERT INTO sys_role_permission (role_id, permission_id) VALUES (1, 1);
|
||||
|
||||
-- 表:sys_user
|
||||
CREATE TABLE "sys_user" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "username" varchar(100) NOT NULL, "password" varchar(50) NOT NULL, "nick_name" varchar(50), "avatar" varchar(255), "phone_code" varchar(20), "mobile" varchar(20), "email" varchar(100),"remark" varchar(100), "status" integer NOT NULL DEFAULT (1), "create_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "update_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP));
|
||||
INSERT INTO sys_user (id, username, password, nick_name, avatar, phone_code, mobile, email, status, create_time, update_time,remark) VALUES (1, 'admin', 'e10adc3949ba59abbe56e057f20f883e', 'admin', NULL, NULL, NULL, NULL, 1, 2011123132, 123132,NULL);
|
||||
INSERT INTO sys_user (id, username, password, nick_name, avatar, phone_code, mobile, email, status, create_time, update_time,remark) VALUES (2, 'readonly', 'e10adc3949ba59abbe56e057f20f883e', '只读用户', NULL, NULL, NULL, NULL, 1, 2011123132, 123132,'密码:123456');
|
||||
|
||||
-- 表:sys_user_role
|
||||
CREATE TABLE "sys_user_role" ("role_id" integer NOT NULL, "user_id" integer NOT NULL, PRIMARY KEY ("role_id", "user_id"));
|
||||
INSERT INTO sys_user_role (role_id, user_id) VALUES (1, 1);
|
||||
INSERT INTO sys_user_role (role_id, user_id) VALUES (2, 2);
|
||||
|
||||
-- 索引:IDX_223de54d6badbe43a5490450c3
|
||||
CREATE UNIQUE INDEX "IDX_223de54d6badbe43a5490450c3" ON "sys_role" ("name");
|
||||
|
||||
-- 索引:IDX_9e7164b2f1ea1348bc0eb0a7da
|
||||
CREATE UNIQUE INDEX "IDX_9e7164b2f1ea1348bc0eb0a7da" ON "sys_user" ("username");
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
INSERT INTO sys_user (id, username, password, nick_name, avatar, phone_code, mobile, email, status, create_time, update_time,remark) VALUES (3, 'admin1', 'e10adc3949ba59abbe56e057f20f883e', 'admin', NULL, NULL, NULL, NULL, 1, 2011123132, 123132,NULL);
|
||||
INSERT INTO sys_user (id, username, password, nick_name, avatar, phone_code, mobile, email, status, create_time, update_time,remark) VALUES (4, 'readonly1', 'e10adc3949ba59abbe56e057f20f883e', '只读用户', NULL, NULL, NULL, NULL, 1, 2011123132, 123132,'密码:123456');
|
||||
@@ -0,0 +1,3 @@
|
||||
INSERT INTO sys_role (id, name, create_time, update_time) VALUES (3, '管理员1', 1, 1623749138537);
|
||||
-- hash check
|
||||
select 1;
|
||||
@@ -0,0 +1,32 @@
|
||||
-- 表:sys_permission
|
||||
CREATE TABLE "sys_permission" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(100) NOT NULL, "permission" varchar(100), "parent_id" integer NOT NULL DEFAULT (-1), "sort" integer NOT NULL, "create_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "update_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP));
|
||||
INSERT INTO sys_permission (id, title, permission, parent_id, sort, create_time, update_time) VALUES (1, '系统管理', 'sys', -1, 1, 1, 1624085863636);
|
||||
|
||||
|
||||
|
||||
-- 表:sys_role
|
||||
CREATE TABLE "sys_role" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(100) NOT NULL, "create_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "update_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP));
|
||||
INSERT INTO sys_role (id, name, create_time, update_time) VALUES (1, '管理员', 1, 1623749138537);
|
||||
INSERT INTO sys_role (id, name, create_time, update_time) VALUES (2, '只读角色', 1, 1623749138537);
|
||||
|
||||
-- 表:sys_role_permission
|
||||
CREATE TABLE "sys_role_permission" ("role_id" integer NOT NULL, "permission_id" integer NOT NULL, PRIMARY KEY ("role_id", "permission_id"));
|
||||
INSERT INTO sys_role_permission (role_id, permission_id) VALUES (1, 1);
|
||||
|
||||
-- 表:sys_user
|
||||
CREATE TABLE "sys_user" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "username" varchar(100) NOT NULL, "password" varchar(50) NOT NULL, "nick_name" varchar(50), "avatar" varchar(255), "phone_code" varchar(20), "mobile" varchar(20), "email" varchar(100),"remark" varchar(100), "status" integer NOT NULL DEFAULT (1), "create_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "update_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP));
|
||||
INSERT INTO sys_user (id, username, password, nick_name, avatar, phone_code, mobile, email, status, create_time, update_time,remark) VALUES (1, 'admin', 'e10adc3949ba59abbe56e057f20f883e', 'admin', NULL, NULL, NULL, NULL, 1, 2011123132, 123132,NULL);
|
||||
INSERT INTO sys_user (id, username, password, nick_name, avatar, phone_code, mobile, email, status, create_time, update_time,remark) VALUES (2, 'readonly', 'e10adc3949ba59abbe56e057f20f883e', '只读用户', NULL, NULL, NULL, NULL, 1, 2011123132, 123132,'密码:123456');
|
||||
|
||||
-- 表:sys_user_role
|
||||
CREATE TABLE "sys_user_role" ("role_id" integer NOT NULL, "user_id" integer NOT NULL, PRIMARY KEY ("role_id", "user_id"));
|
||||
INSERT INTO sys_user_role (role_id, user_id) VALUES (1, 1);
|
||||
INSERT INTO sys_user_role (role_id, user_id) VALUES (2, 2);
|
||||
|
||||
-- 索引:IDX_223de54d6badbe43a5490450c3
|
||||
CREATE UNIQUE INDEX "IDX_223de54d6badbe43a5490450c3" ON "sys_role" ("name");
|
||||
|
||||
-- 索引:IDX_9e7164b2f1ea1348bc0eb0a7da
|
||||
CREATE UNIQUE INDEX "IDX_9e7164b2f1ea1348bc0eb0a7da" ON "sys_user" ("username");
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
INSERT INTO sys_user (id, username, password, nick_name, avatar, phone_code, mobile, email, status, create_time, update_time,remark) VALUES (3, 'admin1', 'e10adc3949ba59abbe56e057f20f883e', 'admin', NULL, NULL, NULL, NULL, 1, 2011123132, 123132,NULL);
|
||||
INSERT INTO sys_user (id, username, password, nick_name, avatar, phone_code, mobile, email, status, create_time, update_time,remark) VALUES (4, 'readonly1', 'e10adc3949ba59abbe56e057f20f883e', '只读用户', NULL, NULL, NULL, NULL, 1, 2011123132, 123132,'密码:123456');
|
||||
@@ -0,0 +1,2 @@
|
||||
INSERT INTO sys_role (id, name, create_time, update_time) VALUES (3, '管理员1', 1, 1623749138537);
|
||||
INSERT INTO sys_role (id, name, create_time, update_time) VALUES (4, '只读角色1', 1, 1623749138537);
|
||||
@@ -0,0 +1,5 @@
|
||||
-- 表:sys_permission
|
||||
CREATE TABLE "sys_permission" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(100) NOT NULL, "permission" varchar(100), "parent_id" integer NOT NULL DEFAULT (-1), "sort" integer NOT NULL, "create_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "update_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP));
|
||||
INSERT INTO sys_permission (id, title, permission, parent_id, sort, create_time, update_time)
|
||||
VALUES (1, '系统管理;', 'sys', -1, 1, 1, 1624085863636);
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
'"test;";\'test;\'';
|
||||
|
||||
@@ -0,0 +1,273 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
||||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
||||
function verb(n) { return function (v) { return step([n, v]); }; }
|
||||
function step(op) {
|
||||
if (f) throw new TypeError("Generator is already executing.");
|
||||
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
||||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
||||
if (y = 0, t) op = [op[0] & 2, t.value];
|
||||
switch (op[0]) {
|
||||
case 0: case 1: t = op; break;
|
||||
case 4: _.label++; return { value: op[1], done: false };
|
||||
case 5: _.label++; y = op[1]; op = [0]; continue;
|
||||
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
||||
default:
|
||||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
||||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
||||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
||||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
||||
if (t[2]) _.ops.pop();
|
||||
_.trys.pop(); continue;
|
||||
}
|
||||
op = body.call(thisArg, _);
|
||||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
||||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
||||
}
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var flyway_1 = require("../src/flyway");
|
||||
var typeorm_1 = require("typeorm");
|
||||
var fs = require("fs");
|
||||
var entity_1 = require("../src/entity");
|
||||
var dbPath = "./data/db.sqlite";
|
||||
var AppDataSource = new typeorm_1.DataSource({
|
||||
type: "sqlite",
|
||||
database: dbPath,
|
||||
entities: [entity_1.FlywayHistory]
|
||||
});
|
||||
describe('test/flyway/flyway.test.ts', function () {
|
||||
beforeEach(function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
if (fs.existsSync(dbPath)) {
|
||||
fs.rmSync(dbPath);
|
||||
}
|
||||
return [4 /*yield*/, AppDataSource.initialize()];
|
||||
case 1:
|
||||
_a.sent();
|
||||
console.log('before each');
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
afterEach(function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
// close app
|
||||
return [4 /*yield*/, AppDataSource.destroy()];
|
||||
case 1:
|
||||
// close app
|
||||
_a.sent();
|
||||
if (fs.existsSync(dbPath)) {
|
||||
fs.rmSync(dbPath);
|
||||
}
|
||||
console.log('after each');
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
/**
|
||||
* sql分割测试
|
||||
*/
|
||||
it('split', function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
var opts, content, sqlArray;
|
||||
return __generator(this, function (_a) {
|
||||
opts = {
|
||||
scriptDir: "./test/db/split"
|
||||
};
|
||||
content = fs.readFileSync("./test/db/split/split.sql").toString();
|
||||
sqlArray = new flyway_1.Flyway(opts).splitSql2Array(content);
|
||||
console.log('sql array', sqlArray);
|
||||
expect(sqlArray.length).toBe(1);
|
||||
return [2 /*return*/];
|
||||
});
|
||||
}); });
|
||||
/**
|
||||
* sql分号测试
|
||||
*/
|
||||
it('semicolon', function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
var connection, opts, queryRunner, flywayHistoryRet;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
connection = AppDataSource;
|
||||
opts = {
|
||||
scriptDir: "./test/db/semicolon",
|
||||
connection: connection
|
||||
};
|
||||
return [4 /*yield*/, new flyway_1.Flyway(opts).run()];
|
||||
case 1:
|
||||
_a.sent();
|
||||
queryRunner = connection.createQueryRunner();
|
||||
return [4 /*yield*/, queryRunner.query("select * from flyway_history")];
|
||||
case 2:
|
||||
flywayHistoryRet = _a.sent();
|
||||
console.log('flywayHistoryRet', flywayHistoryRet);
|
||||
expect(flywayHistoryRet.length).toBe(1);
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
/**
|
||||
* 正常执行
|
||||
*/
|
||||
it('success', function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
var connection, opts, queryRunner, ret, flywayHistoryRet, flywayHistoryRet2;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
connection = AppDataSource;
|
||||
opts = {
|
||||
scriptDir: "./test/db/migration",
|
||||
connection: connection
|
||||
};
|
||||
return [4 /*yield*/, new flyway_1.Flyway(opts).run()];
|
||||
case 1:
|
||||
_a.sent();
|
||||
queryRunner = connection.createQueryRunner();
|
||||
return [4 /*yield*/, queryRunner.query("select count(*) as count from sys_user")];
|
||||
case 2:
|
||||
ret = _a.sent();
|
||||
console.log('useCount', ret);
|
||||
expect(ret[0].count).toBe(4);
|
||||
return [4 /*yield*/, queryRunner.query("select * from flyway_history")];
|
||||
case 3:
|
||||
flywayHistoryRet = _a.sent();
|
||||
console.log('flywayHistoryRet', flywayHistoryRet);
|
||||
expect(flywayHistoryRet.length).toBe(3);
|
||||
expect(flywayHistoryRet[0].id).toBe(1);
|
||||
expect(flywayHistoryRet[0].success).toBe(1);
|
||||
expect(flywayHistoryRet[0].name).toBe('v1__init.sql');
|
||||
//再运行一次,应该没有变化
|
||||
return [4 /*yield*/, new flyway_1.Flyway(opts).run()];
|
||||
case 4:
|
||||
//再运行一次,应该没有变化
|
||||
_a.sent();
|
||||
return [4 /*yield*/, queryRunner.query("select * from flyway_history")];
|
||||
case 5:
|
||||
flywayHistoryRet2 = _a.sent();
|
||||
expect(flywayHistoryRet.length).toBe(3);
|
||||
expect(flywayHistoryRet2[0].id).toBe(1);
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
/**
|
||||
* 测试基准线,基准线之前的sql不执行
|
||||
*/
|
||||
it('base line', function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
var connection, opts, e_1, queryRunner, ret, flywayHistoryRet;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
connection = AppDataSource;
|
||||
opts = {
|
||||
scriptDir: "./test/db/baseline",
|
||||
baseline: 'v0__baseline.sql',
|
||||
connection: connection
|
||||
};
|
||||
_a.label = 1;
|
||||
case 1:
|
||||
_a.trys.push([1, 3, , 4]);
|
||||
return [4 /*yield*/, new flyway_1.Flyway(opts).run()];
|
||||
case 2:
|
||||
_a.sent();
|
||||
return [3 /*break*/, 4];
|
||||
case 3:
|
||||
e_1 = _a.sent();
|
||||
console.log('error', e_1);
|
||||
throw e_1;
|
||||
case 4:
|
||||
queryRunner = connection.createQueryRunner();
|
||||
return [4 /*yield*/, queryRunner.query("select count(*) as count from sys_user")];
|
||||
case 5:
|
||||
ret = _a.sent();
|
||||
console.log('useCount', ret);
|
||||
expect(ret[0].count).toBe(2);
|
||||
return [4 /*yield*/, queryRunner.query("select * from flyway_history")];
|
||||
case 6:
|
||||
flywayHistoryRet = _a.sent();
|
||||
console.log('flywayHistoryRet', flywayHistoryRet);
|
||||
expect(flywayHistoryRet.length).toBe(2);
|
||||
expect(flywayHistoryRet[0].id).toBe(1);
|
||||
expect(flywayHistoryRet[0].success).toBe(1);
|
||||
expect(flywayHistoryRet[0].name).toBe('v0__baseline.sql');
|
||||
expect(flywayHistoryRet[1].name).toBe('v1__init.sql');
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
it('hash check', function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
var connection, opts, queryRunner, ret, error, e_2;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
connection = AppDataSource;
|
||||
opts = {
|
||||
scriptDir: "./test/db/migration",
|
||||
connection: connection
|
||||
};
|
||||
return [4 /*yield*/, new flyway_1.Flyway(opts).run()];
|
||||
case 1:
|
||||
_a.sent();
|
||||
queryRunner = connection.createQueryRunner();
|
||||
return [4 /*yield*/, queryRunner.query("select count(*) as count from sys_user")];
|
||||
case 2:
|
||||
ret = _a.sent();
|
||||
console.log('useCount', ret);
|
||||
expect(ret[0].count).toBe(4);
|
||||
_a.label = 3;
|
||||
case 3:
|
||||
_a.trys.push([3, 5, , 6]);
|
||||
opts.scriptDir = "./test/db/hash-check";
|
||||
return [4 /*yield*/, new flyway_1.Flyway(opts).run()];
|
||||
case 4:
|
||||
_a.sent();
|
||||
return [3 /*break*/, 6];
|
||||
case 5:
|
||||
e_2 = _a.sent();
|
||||
error = e_2.message;
|
||||
return [3 /*break*/, 6];
|
||||
case 6:
|
||||
expect(error).toContain('hash conflict');
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
it('blank sql', function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
var connection, opts, queryRunner, flywayHistoryRet;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
connection = AppDataSource;
|
||||
opts = {
|
||||
scriptDir: "./test/db/blank",
|
||||
connection: connection
|
||||
};
|
||||
return [4 /*yield*/, new flyway_1.Flyway(opts).run()];
|
||||
case 1:
|
||||
_a.sent();
|
||||
queryRunner = connection.createQueryRunner();
|
||||
return [4 /*yield*/, queryRunner.query("select * from flyway_history")];
|
||||
case 2:
|
||||
flywayHistoryRet = _a.sent();
|
||||
console.log('flywayHistoryRet', flywayHistoryRet);
|
||||
expect(flywayHistoryRet.length).toBe(1);
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
});
|
||||
@@ -0,0 +1,163 @@
|
||||
import {Flyway} from "../src/flyway";
|
||||
import {DataSource} from 'typeorm';
|
||||
import * as fs from 'fs'
|
||||
import {FlywayHistory} from "../src/entity";
|
||||
|
||||
const dbPath = "./data/db.sqlite"
|
||||
const AppDataSource = new DataSource({
|
||||
type: "sqlite",
|
||||
database: dbPath,
|
||||
entities: [FlywayHistory]
|
||||
})
|
||||
|
||||
describe('test/flyway/flyway.test.ts', () => {
|
||||
|
||||
beforeEach(async () => {
|
||||
if (fs.existsSync(dbPath)) {
|
||||
fs.rmSync(dbPath)
|
||||
}
|
||||
await AppDataSource.initialize()
|
||||
console.log('before each')
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
// close app
|
||||
await AppDataSource.destroy()
|
||||
if (fs.existsSync(dbPath)) {
|
||||
fs.rmSync(dbPath)
|
||||
}
|
||||
console.log('after each')
|
||||
});
|
||||
|
||||
/**
|
||||
* sql分割测试
|
||||
*/
|
||||
it('split', async () => {
|
||||
let opts = {
|
||||
scriptDir: "./test/db/split"
|
||||
};
|
||||
const content = fs.readFileSync("./test/db/split/split.sql").toString();
|
||||
const sqlArray = new Flyway(opts).splitSql2Array(content)
|
||||
console.log('sql array',sqlArray)
|
||||
expect(sqlArray.length).toBe(1);
|
||||
});
|
||||
|
||||
/**
|
||||
* sql分号测试
|
||||
*/
|
||||
it('semicolon', async () => {
|
||||
const connection = AppDataSource
|
||||
let opts = {
|
||||
scriptDir: "./test/db/semicolon",
|
||||
connection: connection
|
||||
};
|
||||
await new Flyway(opts).run();
|
||||
const queryRunner = connection.createQueryRunner();
|
||||
const flywayHistoryRet = await queryRunner.query("select * from flyway_history");
|
||||
console.log('flywayHistoryRet', flywayHistoryRet)
|
||||
expect(flywayHistoryRet.length).toBe(1);
|
||||
});
|
||||
|
||||
/**
|
||||
* 正常执行
|
||||
*/
|
||||
it('success', async () => {
|
||||
const connection = AppDataSource
|
||||
let opts = {
|
||||
scriptDir: "./test/db/migration",
|
||||
connection: connection
|
||||
};
|
||||
await new Flyway(opts).run();
|
||||
const queryRunner = connection.createQueryRunner();
|
||||
const ret = await queryRunner.query("select count(*) as count from sys_user");
|
||||
console.log('useCount',ret)
|
||||
expect(ret[0].count).toBe(4);
|
||||
|
||||
const flywayHistoryRet = await queryRunner.query("select * from flyway_history");
|
||||
console.log('flywayHistoryRet',flywayHistoryRet)
|
||||
expect(flywayHistoryRet.length).toBe(3);
|
||||
expect(flywayHistoryRet[0].id).toBe(1);
|
||||
expect(flywayHistoryRet[0].success).toBe(1);
|
||||
expect(flywayHistoryRet[0].name).toBe('v1__init.sql');
|
||||
|
||||
//再运行一次,应该没有变化
|
||||
await new Flyway(opts).run();
|
||||
const flywayHistoryRet2 = await queryRunner.query("select * from flyway_history");
|
||||
expect(flywayHistoryRet.length).toBe(3);
|
||||
expect(flywayHistoryRet2[0].id).toBe(1);
|
||||
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* 测试基准线,基准线之前的sql不执行
|
||||
*/
|
||||
it('base line', async () => {
|
||||
const connection = AppDataSource
|
||||
let opts = {
|
||||
scriptDir: "./test/db/baseline",
|
||||
baseline: 'v0__baseline.sql',
|
||||
connection
|
||||
};
|
||||
try {
|
||||
await new Flyway(opts).run();
|
||||
} catch (e) {
|
||||
console.log('error',e)
|
||||
throw e
|
||||
}
|
||||
const queryRunner = connection.createQueryRunner();
|
||||
const ret = await queryRunner.query("select count(*) as count from sys_user");
|
||||
console.log('useCount', ret)
|
||||
expect(ret[0].count).toBe(2);
|
||||
|
||||
const flywayHistoryRet = await queryRunner.query("select * from flyway_history");
|
||||
console.log('flywayHistoryRet', flywayHistoryRet)
|
||||
expect(flywayHistoryRet.length).toBe(2);
|
||||
expect(flywayHistoryRet[0].id).toBe(1);
|
||||
expect(flywayHistoryRet[0].success).toBe(1);
|
||||
expect(flywayHistoryRet[0].name).toBe('v0__baseline.sql');
|
||||
expect(flywayHistoryRet[1].name).toBe('v1__init.sql');
|
||||
|
||||
});
|
||||
|
||||
it('hash check', async () => {
|
||||
const connection = AppDataSource;
|
||||
let opts = {
|
||||
scriptDir: "./test/db/migration",
|
||||
connection
|
||||
};
|
||||
await new Flyway(opts).run();
|
||||
const queryRunner = connection.createQueryRunner();
|
||||
const ret = await queryRunner.query("select count(*) as count from sys_user");
|
||||
console.log('useCount',ret)
|
||||
expect(ret[0].count).toBe(4);
|
||||
|
||||
//再运行一次,应该抛异常
|
||||
let error
|
||||
try{
|
||||
opts.scriptDir="./test/db/hash-check"
|
||||
await new Flyway(opts).run();
|
||||
}catch (e){
|
||||
error = e.message;
|
||||
}
|
||||
expect(error).toContain('hash conflict');
|
||||
|
||||
});
|
||||
|
||||
|
||||
it('blank sql', async () => {
|
||||
const connection = AppDataSource;
|
||||
let opts = {
|
||||
scriptDir: "./test/db/blank",
|
||||
connection
|
||||
};
|
||||
await new Flyway(opts).run();
|
||||
|
||||
const queryRunner = connection.createQueryRunner();
|
||||
const flywayHistoryRet = await queryRunner.query("select * from flyway_history");
|
||||
console.log('flywayHistoryRet', flywayHistoryRet)
|
||||
expect(flywayHistoryRet.length).toBe(1);
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"compileOnSave": true,
|
||||
"compilerOptions": {
|
||||
"target": "ESNext",
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "node",
|
||||
"esModuleInterop": true,
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"inlineSourceMap":true,
|
||||
"noImplicitThis": true,
|
||||
"noUnusedLocals": true,
|
||||
"stripInternal": true,
|
||||
"skipLibCheck": true,
|
||||
"pretty": true,
|
||||
"declaration": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"typeRoots": [ "./typings", "./node_modules/@types"],
|
||||
"outDir": "dist",
|
||||
"rootDir": "src",
|
||||
"composite": true,
|
||||
"useDefineForClassFields": true,
|
||||
"strict": false,
|
||||
// "sourceMap": true,
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"lib": ["ESNext", "DOM"],
|
||||
},
|
||||
"include": [
|
||||
"src/**/*.ts",
|
||||
"src/**/*.d.ts",
|
||||
"src/**/*.json"
|
||||
],
|
||||
"exclude": [
|
||||
"*.js",
|
||||
"*.ts",
|
||||
"dist",
|
||||
"node_modules",
|
||||
"test"
|
||||
],
|
||||
}
|
||||
Reference in New Issue
Block a user