@where-org/where-bridge 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-present, Masafumi Sasahara and contributors.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # where-bridge
2
+ Bridge to where-server / where-socket.
3
+
4
+ Usage
5
+ ```sh
6
+ npm create where-bridge@latest [where-bridge-name]
7
+ ```
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env node
2
+
3
+ import path from 'node:path';
4
+ import * as where from '../index.js';
5
+
6
+ const resolve = (module) => {
7
+ return module.indexOf('#module') === -1 ? import.meta.resolve(module) : module;
8
+ };
9
+
10
+ // main
11
+ const main = async (dir) => {
12
+
13
+ // config
14
+ const bridgeConfig = await where.loadConfig(dir);
15
+
16
+ const bridgeAppConfig = Object.entries(bridgeConfig.app).reduce((o, [k, v]) => (
17
+ { ...o, [k]: { ...v, importPath: resolve(v.app.module) } }
18
+ ), {});
19
+
20
+ const bridgeAppMergedConfig = await where.mergeSpec(bridgeAppConfig, bridgeConfig.spec);
21
+
22
+ // bridge
23
+ const bridge = await where.createBridge({ ...bridgeConfig, app: bridgeAppMergedConfig });
24
+ await bridge.listen();
25
+
26
+ }
27
+
28
+ const [, , dir] = process.argv;
29
+ main(path.resolve(...(dir ? [dir] : [import.meta.dirname, '../config'])));
@@ -0,0 +1,93 @@
1
+ #ts:
2
+ #app:
3
+ #module: '/Users/sasahara/Documents/devel/node/@where-org/module/bridge/where-bridge-app-typescript/src/index.ts'
4
+
5
+ #credential: {}
6
+
7
+ #midi0:
8
+ #app:
9
+ #module: '@where-org/where-bridge-app-midi/socket'
10
+
11
+ #credential:
12
+ #port : 0
13
+ #url : ws://localhost:5570/where/general/midi/p0
14
+ #style: osc # || midi
15
+ #hex : false
16
+ #normarize: false
17
+
18
+ #midi1:
19
+ #app:
20
+ #module: '@where-org/where-bridge-app-midi/socket'
21
+
22
+ #credential:
23
+ #port : 1
24
+ #url : ws://localhost:5570/where/general/a/p0
25
+ #style: osc # || midi
26
+ #hex : false
27
+ #normarize: false
28
+
29
+
30
+ #p1:
31
+ #port : 1
32
+ #url : ws://localhost:5570/where/general/a/p1
33
+ #style: osc # || midi
34
+ #hex : false
35
+ #normarize: true
36
+
37
+ #mcp-http:
38
+ #app:
39
+ #module: '@where-org/where-bridge-app-mcp/http'
40
+
41
+ #meta:
42
+ #credential: example
43
+
44
+ #example:
45
+ #url: http://localhost:5570/where/ex/server
46
+ #readOnly: true
47
+ #port: 5600
48
+
49
+ mcp-bridge:
50
+ app:
51
+ module: '@where-org/where-bridge-app-mcp/stdio'
52
+
53
+ meta:
54
+ credential: example
55
+
56
+ example:
57
+ url: http://localhost:5570/where/ex/server
58
+ readOnly: true
59
+
60
+ #osc9000:
61
+ #app:
62
+ #module: '@where-org/where-bridge-app-osc/socket'
63
+
64
+ #meta:
65
+ #credential: example
66
+
67
+ #example:
68
+ #bind: 0.0.0.0
69
+ #port: 9000
70
+ #url : ws://localhost:5570/where/general/g/osc-server-9000
71
+ #style: osc
72
+ #type: emitter
73
+
74
+ #osc9001:
75
+ #app:
76
+ #module: '@where-org/where-bridge-app-osc/socket'
77
+
78
+ #credential:
79
+ #bind: 0.0.0.0
80
+ #port: 9001
81
+ #url : ws://localhost:5570/where/general/a/osc-server-9001
82
+ #type: sender
83
+
84
+ #osc9002:
85
+ #app:
86
+ #module: '@where-org/where-bridge-app-osc/server'
87
+
88
+ #credential:
89
+ #bind : 0.0.0.0
90
+ #port : 9002
91
+ #url : http://localhost:5570/where/v2/osc-sender
92
+ #scope: send1
93
+ #type : sender
package/index.d.ts ADDED
@@ -0,0 +1,69 @@
1
+ export * from './lib/types/module.js';
2
+ export * from './lib/types/bridge.js';
3
+
4
+ // common
5
+
6
+ export {
7
+
8
+ // CommonFile
9
+ CommonFile, CommonFileRead, CommonFileReadJson, CommonFileReadYaml,
10
+
11
+ // CommonInitLog
12
+ Log,
13
+
14
+ // CommonUtil
15
+ CommonUtil,
16
+
17
+ // CommonUtilCast
18
+ CommonUtilCast,
19
+
20
+ // CommonUtilDate
21
+ CommonUtilDate, CommonUtilDateIso8601, CommonUtilDateIsIsoString, CommonUtilDateIsString, CommonUtilDateIsDate, CommonUtilDateString,
22
+
23
+ // CommonUtilFormat
24
+ CommonUtilFormat,
25
+
26
+ // CommonUtilFormatPascal
27
+ CommonUtilFormatPascal,CommonUtilFormatPascalToCamel, CommonUtilFormatPascalToSnake, CommonUtilFormatPascalToKebab, CommonUtilFormatPascalKeys,
28
+
29
+ // CommonUtilFormatCamel
30
+ CommonUtilFormatCamel, CommonUtilFormatCamelToPascal, CommonUtilFormatCamelToSnake, CommonUtilFormatCamelToKebab, CommonUtilFormatCamelKeys,
31
+
32
+ // CommonUtilFormatSnake
33
+ CommonUtilFormatSnake, CommonUtilFormatSnakeToPascal, CommonUtilFormatSnakeToCamel, CommonUtilFormatSnakeToKebab, CommonUtilFormatSnakeKeys,
34
+
35
+ // CommonUtilFormatKebab
36
+ CommonUtilFormatKebab, CommonUtilFormatKebabToPascal, CommonUtilFormatKebabToCamel, CommonUtilFormatKebabToSnake, CommonUtilFormatKebabKeys,
37
+
38
+ // CommonUtilUrl
39
+ CommonUtilUrl, CommonUtilUrlSocket, CommonUtilUrlSocketParse, CommonUtilUrlSocketString, CommonUtilUrlSocketEither,
40
+
41
+ // SocketUrl
42
+ SocketUrlString, SocketUrlConfig, SocketUrlPreObject, SocketUrlObject,
43
+
44
+ // Cq
45
+ ConditionQuery, ConditionObject, ConditionString, Condition, ConditionSelect, CqParse, CqString,
46
+
47
+ // Da
48
+ DataObject, DataArrayBase, DataArray, DaFilter,
49
+
50
+ } from '@where-org/where-common';
51
+
52
+ // client
53
+
54
+ export {
55
+
56
+ // Client
57
+ Client, Server, Socket,
58
+
59
+ // ServerClient
60
+ ServerClient, ServerClientInit, ServerClientConfig, ServerClientToken, ServerClientScope, ServerClientFile, ServerClientFiles,
61
+
62
+ // SocketClient
63
+ SocketClient, SocketClientInit, SocketClientConfig, SocketClientAttribute,
64
+
65
+ // SocketEmitter
66
+ SocketEmitter, SocketEmitterInit,
67
+
68
+ } from '@where-org/where-client';
69
+
package/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from './lib/bridge.js';
@@ -0,0 +1 @@
1
+ export { init } from './init.js';
@@ -0,0 +1,49 @@
1
+ import path from 'node:path';
2
+
3
+ const init = async (name, c, w) => {
4
+
5
+ // where
6
+ const { init, ...common } = w.common;
7
+
8
+ const log = init.log({ module: 'bridge-app', app: name }),
9
+ where = { ...w, log, common };
10
+
11
+ // config
12
+ const config = c.app[name];
13
+
14
+ // app
15
+ const { module: appModule } = config.app;
16
+
17
+ const appConfig = { ...config.app, ...await init.credential(config.app) },
18
+ args = [appConfig, where];
19
+
20
+ const appImport = await import(appModule).catch(async err => {
21
+
22
+ ;;; where.log({ error: err.stack });
23
+
24
+ return await import(path.resolve(process.cwd(), 'node_modules', appModule, 'index.js')).catch(err => {
25
+
26
+ ;;; where.log({ error: err.stack });
27
+ return false;
28
+
29
+ });
30
+
31
+ });
32
+
33
+ return (!appImport) ? appImport : await appImport[(appImport.app) ? 'app' : 'default'](...args);
34
+
35
+ }
36
+
37
+ //wrapper
38
+ const wrapper = async (config, where) => {
39
+
40
+ return await Object.entries(config.app).reduce(async (o, [k, v]) => {
41
+
42
+ const app = await init(k, config, where);
43
+ return (app) ? { ...await o, [k]: app } : await o;
44
+
45
+ }, {});
46
+
47
+ }
48
+
49
+ export { wrapper as init };
package/lib/bridge.js ADDED
@@ -0,0 +1,104 @@
1
+ import path from 'node:path';
2
+
3
+ import { common, cq, da, define } from '@where-org/where-common';
4
+
5
+ const { status } = define,
6
+ file = { read: common.file.read };
7
+
8
+ const { filter } = da;
9
+
10
+ import { client, server, socket } from '@where-org/where-client';
11
+ import { init } from './bridge/index.js';
12
+
13
+ // mode
14
+ const mode = process.env.npm_config_mode || process.env.NODE_ENV || 'development',
15
+ development = (mode === 'development') ? true : false;
16
+
17
+ /* log */
18
+ const log = common.init.log('bridge');
19
+
20
+ /* loadConfig */
21
+ const loadConfig = async (dir) => {
22
+ return await common.config.load(dir, mode, 'bridge', true);
23
+ };
24
+
25
+ /* mergeSpec */
26
+ const mergeSpec = async (appConfig, specConfig) => {
27
+
28
+ const resolveImportPathAppConfig = Object.entries(appConfig).reduce((o, [k, v]) => (
29
+ { ...o, [k]: { ...v, importPath: v.importPath.indexOf('#module') === 0 ? import.meta.resolve(v.importPath) : v.importPath } }
30
+ ), {});
31
+
32
+ return await common.config.mergeSpec(resolveImportPathAppConfig, specConfig);
33
+
34
+ };
35
+
36
+ /* createBridge */
37
+ const createBridge = async (c) => {
38
+
39
+ // config
40
+ //const c = await common.config.load(dir, mode, 'bridge', true);
41
+
42
+ // env
43
+ const { name, version } = await common.file.read.json(path.resolve(import.meta.dirname, '../package.json'));
44
+
45
+ const env = Object.entries({ name, version, mode }).reduce((o, [k, v]) => {
46
+
47
+ const key = common.util.casing.camelToSnake(k).toUpperCase();
48
+ return { ...o, [`BRIDGE_${key}`]: v };
49
+
50
+ }, {});
51
+
52
+ // where object
53
+ const where = {
54
+ log,
55
+
56
+ app: {
57
+ env, define: { status }, cq, da: { filter }, common: { ...common, file },
58
+ client, server, socket,
59
+ },
60
+ };
61
+
62
+ // where-bridge apps
63
+ const apps = await init(c, where.app);
64
+
65
+ // bridge
66
+ const bridge = {
67
+
68
+ listen: async () => {
69
+ ;;; where.log({ message: 'Hello!', mode });
70
+
71
+ await Promise.all(Object.entries(apps).map(async ([k, v]) => {
72
+ if ('listen' in v) {
73
+ await v.listen();
74
+ }
75
+ }));
76
+ },
77
+
78
+ end: async (code) => {
79
+
80
+ await Promise.all(Object.entries(apps).map(async ([k, v]) => {
81
+ if ('end' in v) {
82
+ await v.end();
83
+ }
84
+ }));
85
+
86
+ process.exit(code);
87
+
88
+ },
89
+
90
+ };
91
+
92
+ Object.entries({ SIGINT: 2, SIGTERM: 15 }).forEach(([k, v]) => {
93
+ process.on(k, async () => await bridge.end(v));
94
+ });
95
+
96
+ process.on('exit', async (code) => {
97
+ ;;; where.log('See you!');
98
+ });
99
+
100
+ return bridge;
101
+
102
+ };
103
+
104
+ export { createBridge, loadConfig, mergeSpec, log, };
@@ -0,0 +1,11 @@
1
+ import type * as Where from '@where-org/where-common';
2
+
3
+ // lib/bridge.js
4
+
5
+ export type Bridge = null;
6
+ export type BridgeInit = (dir: string) => Promise<Bridge>;
7
+
8
+ // const
9
+
10
+ export declare const bridge: BridgeInit;
11
+ export declare const log: Where.Log;
@@ -0,0 +1,61 @@
1
+ import type * as Where from '@where-org/where-common';
2
+ import type { Client, Server, Socket } from '@where-org/where-client';
3
+
4
+ // where-toward-app-module
5
+
6
+ // interface
7
+
8
+ export interface TowardApp {
9
+
10
+ end?
11
+ (): Promise<void>;
12
+
13
+ }
14
+
15
+ // constructor parameters
16
+
17
+ // TowardAppConfig
18
+ export type TowardAppConfig = Where.Credentials;
19
+
20
+ // Env - where.env
21
+ export type Env<T = string | number> = Where.DataObject<T>;
22
+
23
+ // Common - where.common
24
+ export type Common = {
25
+
26
+ file: Where.CommonFile,
27
+ util: Where.CommonUtil,
28
+
29
+ };
30
+
31
+ // Cq - where.cq
32
+ export type Cq = {
33
+
34
+ parse : Where.CqParse,
35
+ string: Where.CqString,
36
+
37
+ };
38
+
39
+ // Da - where.da
40
+ export type Da = {
41
+ filter: Where.DaFilter,
42
+ };
43
+
44
+ // TowardAppLib
45
+ export type TowardAppLib = {
46
+
47
+ log: Where.Log,
48
+ env: Env,
49
+
50
+ common: Common,
51
+ cq: Cq,
52
+ da: Da,
53
+
54
+ client: Client,
55
+ server: Server,
56
+ socket: Socket,
57
+
58
+ }
59
+
60
+ // shortcut
61
+
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@where-org/where-bridge",
3
+ "version": "1.0.0",
4
+ "description": "Bridge to where-server / where-socket",
5
+ "license": "MIT",
6
+ "author": "Masafumi Sasahara <masafumi@sashara.io> (https://github.com/masafumi-sasahara/)",
7
+ "homepage": "https://where-org.com/",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/where-org/where-bridge.git"
11
+ },
12
+ "type": "module",
13
+ "main": "index.js",
14
+ "types": "index.d.ts",
15
+ "scripts": {
16
+ "start": "NODE_ENV=$npm_config_mode node ./bin/where-bridge.js ./config",
17
+ "dev": "NODE_ENV=$npm_config_mode node --watch ./bin/where-bridge.js ./config"
18
+ },
19
+ "keywords": [
20
+ "where-bridge",
21
+ "where-client"
22
+ ],
23
+ "dependencies": {
24
+ "@where-org/where-client": "^2.0.0",
25
+ "@where-org/where-common": "^2.0.2"
26
+ }
27
+ }