1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const crypto_1 = require("crypto");
|
4 | const path_1 = require("path");
|
5 | exports.sha256 = (preimage) => {
|
6 | return crypto_1.createHash('sha256').update(preimage).digest();
|
7 | };
|
8 | function moduleExists(path) {
|
9 | try {
|
10 | require.resolve(path);
|
11 | return true;
|
12 | }
|
13 | catch (err) {
|
14 | return false;
|
15 | }
|
16 | }
|
17 | exports.moduleExists = moduleExists;
|
18 | exports.loadModuleFromPathOrDirectly = (searchPath, module) => {
|
19 | const localPath = path_1.resolve(searchPath, module);
|
20 | if (moduleExists(localPath)) {
|
21 | return localPath;
|
22 | }
|
23 | else if (moduleExists(module)) {
|
24 | return module;
|
25 | }
|
26 | else {
|
27 | return null;
|
28 | }
|
29 | };
|
30 | exports.loadModuleOfType = (type, name) => {
|
31 | const module = exports.loadModuleFromPathOrDirectly(path_1.resolve(__dirname, `../${type}s/`), name);
|
32 | if (!module) {
|
33 | throw new Error(`${type} not found as a module name or under /${type}s/. moduleName=${name}`);
|
34 | }
|
35 | const loadedModule = require(module);
|
36 | if (loadedModule && typeof loadedModule === 'object' && typeof loadedModule.default === 'function') {
|
37 | return loadedModule.default;
|
38 | }
|
39 | else if (typeof loadedModule === 'function') {
|
40 | return loadedModule;
|
41 | }
|
42 | else {
|
43 | throw new TypeError(`${type} does not export a constructor. module=${module}`);
|
44 | }
|
45 | };
|
46 | exports.extractDefaultsFromSchema = (schema, path = '') => {
|
47 | if (typeof schema.default !== 'undefined') {
|
48 | return schema.default;
|
49 | }
|
50 | switch (schema.type) {
|
51 | case 'object':
|
52 | const result = {};
|
53 | for (let key of Object.keys(schema.properties)) {
|
54 | result[key] = exports.extractDefaultsFromSchema(schema.properties[key], path + '.' + key);
|
55 | }
|
56 | return result;
|
57 | default:
|
58 | throw new Error('No default found for schema path: ' + path);
|
59 | }
|
60 | };
|
61 | function composeMiddleware(middleware) {
|
62 | return function (val, next) {
|
63 | let index = -1;
|
64 | return dispatch(0, val);
|
65 | async function dispatch(i, val) {
|
66 | if (i <= index) {
|
67 | throw new Error('next() called multiple times.');
|
68 | }
|
69 | index = i;
|
70 | const fn = (i === middleware.length) ? next : middleware[i];
|
71 | return fn(val, function next(val) {
|
72 | return dispatch(i + 1, val);
|
73 | });
|
74 | }
|
75 | };
|
76 | }
|
77 | exports.composeMiddleware = composeMiddleware;
|
78 | function uuid() {
|
79 | const random = crypto_1.randomBytes(16);
|
80 | random[6] = (random[6] & 0x0f) | 0x40;
|
81 | random[8] = (random[8] & 0x3f) | 0x80;
|
82 | return random.toString('hex')
|
83 | .replace(/(.{8})(.{4})(.{4})(.{4})(.{12})/, '$1-$2-$3-$4-$5');
|
84 | }
|
85 | exports.uuid = uuid;
|
86 | function hmac(secret, message) {
|
87 | const hmac = crypto_1.createHmac('sha256', secret);
|
88 | if (message instanceof Buffer) {
|
89 | hmac.update(message);
|
90 | }
|
91 | else {
|
92 | hmac.update(message, 'utf8');
|
93 | }
|
94 | return hmac.digest();
|
95 | }
|
96 | exports.hmac = hmac;
|
97 |
|
\ | No newline at end of file |