@mndw/cli 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +12 -2
- package/dist/meta.json +1821 -0
- package/dist/mndw.js +3993 -0
- package/host-dev/assets/_virtual_sandbox-manifest-DbTeSIus.js +1 -0
- package/host-dev/assets/_virtual_sandbox-plugin-Cg-1CvkF.js +1 -0
- package/host-dev/assets/index-CADLhduS.js +94 -0
- package/host-dev/assets/index-TmL5gfki.css +1 -0
- package/host-dev/assets/sandbox-url-D9gV-Yo0.js +1 -0
- package/host-dev/assets/sprite-DIkKar2y.svg +189 -0
- package/host-dev/assets/ticker-pool.worker-DJA1ERT2.js +1 -0
- package/host-dev/index.html +33 -0
- package/host-dev/shared/angular-common@21.2.18.js +30 -0
- package/host-dev/shared/angular-compiler@21.2.18.js +84 -0
- package/host-dev/shared/angular-core@21.2.18.js +122 -0
- package/host-dev/shared/angular-platform-browser@21.2.18.js +34 -0
- package/host-dev/shared/d3@7.9.0.js +5 -0
- package/host-dev/shared/hash-wasm@4.12.0.js +10 -0
- package/host-dev/shared/mndw-ui@0.1.1.js +1656 -0
- package/host-dev/shared/rci-tools-core@0.6.0.js +15 -0
- package/host-dev/shared/rxjs-operators@7.8.2.js +3 -0
- package/host-dev/shared/rxjs@7.8.2.js +3 -0
- package/host-dev/shared/vue@3.5.39.js +56 -0
- package/host-dev/shared/zone@0.15.1.js +11 -0
- package/package.json +77 -3
- package/templates/angular/src/app.component.ts +57 -0
- package/templates/angular/src/index.ts +21 -0
- package/templates/angular/src/l10n.test.ts +50 -0
- package/templates/angular/src/table.test.ts +59 -0
- package/templates/angular/tsconfig.json +18 -0
- package/templates/backend/main.sh +7 -0
- package/templates/go/backend/cmd/__MNDW_NAME__d/main.go +20 -0
- package/templates/go/backend/go.mod +6 -0
- package/templates/go/backend/integration_test.go +174 -0
- package/templates/go/backend/internal/httpapi/server.go +87 -0
- package/templates/go/backend/internal/httpapi/server_test.go +63 -0
- package/templates/go/gitignore +6 -0
- package/templates/go/i18n/en.json +8 -0
- package/templates/go/i18n/ru.json +8 -0
- package/templates/go/manifest.json +25 -0
- package/templates/go/package.json +20 -0
- package/templates/go/src/index.css +31 -0
- package/templates/go/src/index.test.ts +59 -0
- package/templates/go/src/index.ts +92 -0
- package/templates/go/src/l10n.test.ts +66 -0
- package/templates/go/src/test-helpers.ts +6 -0
- package/templates/svelte/src/App.svelte +102 -0
- package/templates/svelte/src/TableNameCell.svelte +10 -0
- package/templates/svelte/src/index.ts +14 -0
- package/templates/svelte/src/l10n.test.ts +45 -0
- package/templates/svelte/src/table.test.ts +53 -0
- package/templates/svelte/vitest.config.ts +9 -0
- package/templates/vanilla/src/index.css +31 -0
- package/templates/vanilla/src/index.ts +65 -0
- package/templates/vanilla/src/l10n.test.ts +44 -0
- package/templates/vue/src/App.vue +80 -0
- package/templates/vue/src/TableNameCell.vue +12 -0
- package/templates/vue/src/env.d.ts +6 -0
- package/templates/vue/src/index.ts +16 -0
- package/templates/vue/src/l10n.test.ts +45 -0
- package/templates/vue/src/table.test.ts +53 -0
- package/templates/vue/vitest.config.ts +6 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {CUSTOM_ELEMENTS_SCHEMA, Component, InjectionToken, computed, inject, input, signal} from '@angular/core';
|
|
2
|
+
import {toSignal} from '@angular/core/rxjs-interop';
|
|
3
|
+
import type {PluginContext} from '@mndw/sdk';
|
|
4
|
+
import {TRANSLATOR} from '@mndw/sdk/angular';
|
|
5
|
+
import {type Column, column} from '@mndw/ui';
|
|
6
|
+
import {MndwCellDirective, MndwTableDirective} from '@mndw/ui-angular';
|
|
7
|
+
|
|
8
|
+
interface TableRow {
|
|
9
|
+
id: string;
|
|
10
|
+
name: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function isTableRow(value: unknown): value is TableRow {
|
|
14
|
+
return typeof value === 'object' && value !== null && 'id' in value && 'name' in value
|
|
15
|
+
&& typeof value.id === 'string' && typeof value.name === 'string';
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const PLUGIN_CONTEXT = new InjectionToken<PluginContext>('PLUGIN_CONTEXT');
|
|
19
|
+
|
|
20
|
+
@Component({
|
|
21
|
+
selector: 'app-root',
|
|
22
|
+
standalone: true,
|
|
23
|
+
imports: [MndwTableDirective, MndwCellDirective],
|
|
24
|
+
schemas: [CUSTOM_ELEMENTS_SCHEMA],
|
|
25
|
+
template: `
|
|
26
|
+
<div [attr.data-theme]="theme()" style="padding:var(--mndw-space-4);font-family:sans-serif">
|
|
27
|
+
<h3 data-testid="l10n-probe" data-plugin-label="__MNDW_LABEL__">{{ translator.t('l10n.probe') }}</h3>
|
|
28
|
+
<p>Device: {{ deviceName() }}</p>
|
|
29
|
+
<button (click)="count.set(count() + 1)">{{ translator.t('l10n.button') }} {{ count() }}</button>
|
|
30
|
+
<p>{{ translator.t('l10n.result') }}</p>
|
|
31
|
+
<mndw-table [columns]="columns()" [rows]="rows()" rowKey="id" [caption]="translator.t('table.caption')"
|
|
32
|
+
[columnMenu]="false">
|
|
33
|
+
<ng-template mndwCell="name" let-row>
|
|
34
|
+
<span data-testid="table-name-cell">{{ $any(row).name }}</span>
|
|
35
|
+
</ng-template>
|
|
36
|
+
</mndw-table>
|
|
37
|
+
</div>
|
|
38
|
+
`,
|
|
39
|
+
})
|
|
40
|
+
export class AppComponent {
|
|
41
|
+
private readonly ctx = inject(PLUGIN_CONTEXT);
|
|
42
|
+
protected readonly translator = inject(TRANSLATOR);
|
|
43
|
+
protected readonly theme = toSignal(this.ctx.theme$, {initialValue: this.ctx.theme});
|
|
44
|
+
protected readonly rows = computed<readonly TableRow[]>(() => [
|
|
45
|
+
{id: 'first', name: this.translator.t('table.first')},
|
|
46
|
+
{id: 'second', name: this.translator.t('table.second')},
|
|
47
|
+
]);
|
|
48
|
+
protected readonly columns = computed<Column<unknown>[]>(() => [
|
|
49
|
+
column<unknown>()({
|
|
50
|
+
id: 'name',
|
|
51
|
+
label: this.translator.t('table.column'),
|
|
52
|
+
accessor: (row) => isTableRow(row) ? row.name : '',
|
|
53
|
+
}),
|
|
54
|
+
]);
|
|
55
|
+
public readonly deviceName = input('');
|
|
56
|
+
protected readonly count = signal(0);
|
|
57
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import {reflectComponentType} from '@angular/core';
|
|
2
|
+
import type {PluginContext, PluginInstance} from '@mndw/sdk';
|
|
3
|
+
import {createAngularPluginInstance, provideTranslator} from '@mndw/sdk/angular';
|
|
4
|
+
import 'zone.js';
|
|
5
|
+
import '@angular/compiler';
|
|
6
|
+
|
|
7
|
+
import {AppComponent, PLUGIN_CONTEXT} from './app.component';
|
|
8
|
+
|
|
9
|
+
export function createInstance(el: HTMLElement, ctx: PluginContext): PluginInstance {
|
|
10
|
+
return createAngularPluginInstance(el, {
|
|
11
|
+
rootComponent: AppComponent,
|
|
12
|
+
providers: [provideTranslator(ctx.i18n), {provide: PLUGIN_CONTEXT, useValue: ctx}],
|
|
13
|
+
configure: ({component}) => {
|
|
14
|
+
if (
|
|
15
|
+
reflectComponentType(component.componentType)?.inputs.some(({templateName}) => templateName === 'deviceName')
|
|
16
|
+
) {
|
|
17
|
+
component.setInput('deviceName', ctx.deviceInfo.deviceName);
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
|
|
3
|
+
import 'zone.js';
|
|
4
|
+
import '@angular/compiler';
|
|
5
|
+
import type {PluginContext} from '@mndw/sdk';
|
|
6
|
+
import {createTestI18n, createTestPluginContext} from '@mndw/sdk/testing';
|
|
7
|
+
import {describe, expect, it} from 'vitest';
|
|
8
|
+
import {createInstance} from './index.js';
|
|
9
|
+
|
|
10
|
+
async function stabilize(): Promise<void> {
|
|
11
|
+
await Promise.resolve();
|
|
12
|
+
await new Promise<void>((resolve) => setTimeout(resolve, 0));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
describe('generated Angular locale reactivity', () => {
|
|
16
|
+
it('updates the same probe node without remounting', async () => {
|
|
17
|
+
const en = {l10n: {probe: 'English probe', button: 'Run English', result: 'English result'}};
|
|
18
|
+
const ru = {l10n: {probe: 'Русский probe', button: 'Запустить', result: 'Русский результат'}};
|
|
19
|
+
const i18n = createTestI18n(en, 'en');
|
|
20
|
+
const root = document.createElement('div');
|
|
21
|
+
const context = createTestPluginContext({i18n}).context;
|
|
22
|
+
const instance = createInstance(root, context);
|
|
23
|
+
await stabilize();
|
|
24
|
+
const probe = root.querySelector('[data-testid="l10n-probe"]');
|
|
25
|
+
|
|
26
|
+
expect(en.l10n.probe).not.toBe(ru.l10n.probe);
|
|
27
|
+
expect(probe?.textContent).toBe(en.l10n.probe);
|
|
28
|
+
i18n.setLocale('ru', ru);
|
|
29
|
+
await stabilize();
|
|
30
|
+
|
|
31
|
+
expect(root.querySelector('[data-testid="l10n-probe"]')).toBe(probe);
|
|
32
|
+
expect(probe?.textContent).toBe(ru.l10n.probe);
|
|
33
|
+
instance.unmount();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('updates data-theme when the host theme changes', async () => {
|
|
37
|
+
const fixture = createTestPluginContext();
|
|
38
|
+
const root = document.createElement('div');
|
|
39
|
+
const instance = createInstance(root, fixture.context);
|
|
40
|
+
await stabilize();
|
|
41
|
+
|
|
42
|
+
expect(root.querySelector('[data-theme]')?.getAttribute('data-theme')).toBe('light');
|
|
43
|
+
fixture.setTheme('dark');
|
|
44
|
+
await stabilize();
|
|
45
|
+
expect(root.querySelector('[data-theme]')?.getAttribute('data-theme')).toBe('dark');
|
|
46
|
+
|
|
47
|
+
instance.unmount();
|
|
48
|
+
fixture.unmount();
|
|
49
|
+
});
|
|
50
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
|
|
3
|
+
import 'zone.js';
|
|
4
|
+
import '@angular/compiler';
|
|
5
|
+
import {defineMndwUi} from '@mndw/ui';
|
|
6
|
+
import {createTestI18n, createTestPluginContext} from '@mndw/sdk/testing';
|
|
7
|
+
import {afterEach, describe, expect, it} from 'vitest';
|
|
8
|
+
import {createInstance} from './index.js';
|
|
9
|
+
|
|
10
|
+
type TableElement = HTMLElement & {updateComplete: Promise<unknown>};
|
|
11
|
+
|
|
12
|
+
const catalog = {
|
|
13
|
+
table: {
|
|
14
|
+
caption: 'Example records',
|
|
15
|
+
column: 'Name',
|
|
16
|
+
first: 'Alpha',
|
|
17
|
+
second: 'Beta',
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
defineMndwUi();
|
|
22
|
+
|
|
23
|
+
async function stabilize(): Promise<void> {
|
|
24
|
+
await Promise.resolve();
|
|
25
|
+
await new Promise<void>((resolve) => setTimeout(resolve, 0));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
afterEach(() => {
|
|
29
|
+
document.body.innerHTML = '';
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('generated Angular table', () => {
|
|
33
|
+
it('renders localized rows through MndwCellDirective', async () => {
|
|
34
|
+
const fixture = createTestPluginContext({i18n: createTestI18n(catalog)});
|
|
35
|
+
const container = document.createElement('div');
|
|
36
|
+
document.body.append(container);
|
|
37
|
+
const instance = createInstance(container, fixture.context);
|
|
38
|
+
await stabilize();
|
|
39
|
+
|
|
40
|
+
const table = container.querySelector('mndw-table') as TableElement | null;
|
|
41
|
+
|
|
42
|
+
if (table === null) {
|
|
43
|
+
throw new Error('expected generated mndw-table');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
await table.updateComplete;
|
|
47
|
+
expect(table.shadowRoot?.querySelectorAll('[part="row"]')).toHaveLength(2);
|
|
48
|
+
expect(table.shadowRoot?.querySelector('[part="caption"]')?.textContent).toBe(catalog.table.caption);
|
|
49
|
+
expect(table.shadowRoot?.querySelector('[part="header-cell"]')?.textContent).toContain(catalog.table.column);
|
|
50
|
+
expect(Array.from(table.querySelectorAll('[data-testid="table-name-cell"]')).map((cell) => cell.textContent))
|
|
51
|
+
.toEqual([
|
|
52
|
+
catalog.table.first,
|
|
53
|
+
catalog.table.second,
|
|
54
|
+
]);
|
|
55
|
+
|
|
56
|
+
instance.unmount();
|
|
57
|
+
fixture.unmount();
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"strict": true,
|
|
7
|
+
"skipLibCheck": true,
|
|
8
|
+
"experimentalDecorators": true,
|
|
9
|
+
"useDefineForClassFields": false,
|
|
10
|
+
"outDir": ".ngc-cache",
|
|
11
|
+
"rootDir": "src"
|
|
12
|
+
},
|
|
13
|
+
"angularCompilerOptions": {
|
|
14
|
+
"enableI18nLegacyMessageIdFormat": false,
|
|
15
|
+
"strictInjectionParameters": true
|
|
16
|
+
},
|
|
17
|
+
"include": ["src/**/*.ts"]
|
|
18
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
# Backend exec command for "__MNDW_NAME__", invoked for GET hello.
|
|
3
|
+
# The daemon serves this route at /api/plugins/__MNDW_NAME__/hello.
|
|
4
|
+
# Emit an HTTP header block, a blank line, then the response body on stdout.
|
|
5
|
+
|
|
6
|
+
printf 'Content-Type: application/json\r\n\r\n'
|
|
7
|
+
printf '{"message":"hello from __MNDW_NAME__"}\n'
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
package main
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"context"
|
|
5
|
+
"log"
|
|
6
|
+
"os/signal"
|
|
7
|
+
"syscall"
|
|
8
|
+
|
|
9
|
+
"__MNDW_NAME__/internal/httpapi"
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
func main() {
|
|
13
|
+
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
14
|
+
|
|
15
|
+
defer stop()
|
|
16
|
+
|
|
17
|
+
if err := httpapi.Run(ctx); err != nil {
|
|
18
|
+
log.Fatalf("backend exited: %v", err)
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
package main
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"context"
|
|
5
|
+
"encoding/json"
|
|
6
|
+
"fmt"
|
|
7
|
+
"net"
|
|
8
|
+
"net/http"
|
|
9
|
+
"os"
|
|
10
|
+
"path/filepath"
|
|
11
|
+
"strings"
|
|
12
|
+
"testing"
|
|
13
|
+
"time"
|
|
14
|
+
|
|
15
|
+
"__MNDW_NAME__/internal/httpapi"
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
func TestRunServesReadinessAndHelloThenShutsDown(t *testing.T) {
|
|
19
|
+
socket := testSocket(t)
|
|
20
|
+
|
|
21
|
+
t.Setenv("MNDW_BACKEND_SOCKET", socket)
|
|
22
|
+
t.Setenv("MNDW_PLUGIN_ID", "generated-go-page")
|
|
23
|
+
t.Setenv("MNDW_BACKEND_PROTOCOL", "1")
|
|
24
|
+
|
|
25
|
+
ctx, cancel := context.WithCancel(context.Background())
|
|
26
|
+
done := make(chan error, 1)
|
|
27
|
+
|
|
28
|
+
go func() { done <- httpapi.Run(ctx) }()
|
|
29
|
+
|
|
30
|
+
stopped := false
|
|
31
|
+
|
|
32
|
+
t.Cleanup(func() {
|
|
33
|
+
cancel()
|
|
34
|
+
|
|
35
|
+
if !stopped {
|
|
36
|
+
waitStopped(t, done)
|
|
37
|
+
}
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
client := unixClient(socket)
|
|
41
|
+
ready := waitReady(t, client)
|
|
42
|
+
|
|
43
|
+
if ready.PluginID != "generated-go-page" {
|
|
44
|
+
t.Fatalf("readiness pluginId = %q, want generated-go-page", ready.PluginID)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if ready.ProtocolVersion != 1 {
|
|
48
|
+
t.Fatalf("readiness protocolVersion = %d, want 1", ready.ProtocolVersion)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
response, err := client.Get("http://unix/hello")
|
|
52
|
+
if err != nil {
|
|
53
|
+
t.Fatal(err)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
defer response.Body.Close()
|
|
57
|
+
|
|
58
|
+
if response.StatusCode != http.StatusOK {
|
|
59
|
+
t.Fatalf("/hello status = %d", response.StatusCode)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
var hello struct {
|
|
63
|
+
Message string `json:"message"`
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if err := json.NewDecoder(response.Body).Decode(&hello); err != nil {
|
|
67
|
+
t.Fatal(err)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if !strings.HasPrefix(hello.Message, "hello + ") {
|
|
71
|
+
t.Fatalf("/hello message = %q, want hello + timestamp", hello.Message)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if _, err := time.Parse(time.RFC3339Nano, strings.TrimPrefix(hello.Message, "hello + ")); err != nil {
|
|
75
|
+
t.Fatalf("/hello message timestamp is invalid: %q: %v", hello.Message, err)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
cancel()
|
|
79
|
+
waitStopped(t, done)
|
|
80
|
+
|
|
81
|
+
stopped = true
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
func TestRunReflectsMalformedProtocolAsZero(t *testing.T) {
|
|
85
|
+
socket := testSocket(t)
|
|
86
|
+
|
|
87
|
+
t.Setenv("MNDW_BACKEND_SOCKET", socket)
|
|
88
|
+
t.Setenv("MNDW_PLUGIN_ID", "generated-go-page")
|
|
89
|
+
t.Setenv("MNDW_BACKEND_PROTOCOL", "not-a-number")
|
|
90
|
+
|
|
91
|
+
ctx, cancel := context.WithCancel(context.Background())
|
|
92
|
+
done := make(chan error, 1)
|
|
93
|
+
|
|
94
|
+
go func() { done <- httpapi.Run(ctx) }()
|
|
95
|
+
|
|
96
|
+
t.Cleanup(func() {
|
|
97
|
+
cancel()
|
|
98
|
+
waitStopped(t, done)
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
ready := waitReady(t, unixClient(socket))
|
|
102
|
+
|
|
103
|
+
if ready.ProtocolVersion != 0 {
|
|
104
|
+
t.Fatalf("malformed protocol should reflect 0, got %d", ready.ProtocolVersion)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
type readyBody struct {
|
|
109
|
+
PluginID string `json:"pluginId"`
|
|
110
|
+
ProtocolVersion int `json:"protocolVersion"`
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
func testSocket(t *testing.T) string {
|
|
114
|
+
t.Helper()
|
|
115
|
+
|
|
116
|
+
socket := filepath.Join("/tmp", fmt.Sprintf("mndw-go-backend-%d.sock", os.Getpid()))
|
|
117
|
+
_ = os.Remove(socket)
|
|
118
|
+
|
|
119
|
+
t.Cleanup(func() { _ = os.Remove(socket) })
|
|
120
|
+
|
|
121
|
+
return socket
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
func unixClient(socket string) *http.Client {
|
|
125
|
+
return &http.Client{Transport: &http.Transport{
|
|
126
|
+
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
|
|
127
|
+
return (&net.Dialer{}).DialContext(ctx, "unix", socket)
|
|
128
|
+
},
|
|
129
|
+
}}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
func waitReady(t *testing.T, client *http.Client) readyBody {
|
|
133
|
+
t.Helper()
|
|
134
|
+
|
|
135
|
+
deadline := time.Now().Add(2 * time.Second)
|
|
136
|
+
|
|
137
|
+
for time.Now().Before(deadline) {
|
|
138
|
+
response, err := client.Get("http://unix/_mndw/ready")
|
|
139
|
+
if err == nil {
|
|
140
|
+
if response.StatusCode == http.StatusOK {
|
|
141
|
+
var body readyBody
|
|
142
|
+
decodeErr := json.NewDecoder(response.Body).Decode(&body)
|
|
143
|
+
_ = response.Body.Close()
|
|
144
|
+
|
|
145
|
+
if decodeErr != nil {
|
|
146
|
+
t.Fatalf("decode readiness: %v", decodeErr)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return body
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
_ = response.Body.Close()
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
time.Sleep(10 * time.Millisecond)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
t.Fatal("backend never became ready")
|
|
159
|
+
|
|
160
|
+
return readyBody{}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
func waitStopped(t *testing.T, done <-chan error) {
|
|
164
|
+
t.Helper()
|
|
165
|
+
|
|
166
|
+
select {
|
|
167
|
+
case err := <-done:
|
|
168
|
+
if err != nil {
|
|
169
|
+
t.Fatalf("Run returned error: %v", err)
|
|
170
|
+
}
|
|
171
|
+
case <-time.After(2 * time.Second):
|
|
172
|
+
t.Fatal("Run goroutine did not exit within 2s of cancellation")
|
|
173
|
+
}
|
|
174
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
package httpapi
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"context"
|
|
5
|
+
"encoding/json"
|
|
6
|
+
"errors"
|
|
7
|
+
"net"
|
|
8
|
+
"net/http"
|
|
9
|
+
"os"
|
|
10
|
+
"strconv"
|
|
11
|
+
"time"
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
const (
|
|
15
|
+
readHeaderTimeout = 5 * time.Second
|
|
16
|
+
shutdownTimeout = 500 * time.Millisecond
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
type readyResponse struct {
|
|
20
|
+
PluginID string `json:"pluginId"`
|
|
21
|
+
ProtocolVersion int `json:"protocolVersion"`
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
type helloResponse struct {
|
|
25
|
+
Message string `json:"message"`
|
|
26
|
+
Plugin string `json:"plugin"`
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Run serves the plugin backend on the daemon-injected Unix socket until ctx is
|
|
30
|
+
// cancelled. It never unlinks the socket: the daemon provides a clean path and
|
|
31
|
+
// owns runtime-directory cleanup.
|
|
32
|
+
func Run(ctx context.Context) error {
|
|
33
|
+
socketPath := os.Getenv("MNDW_BACKEND_SOCKET")
|
|
34
|
+
pluginID := os.Getenv("MNDW_PLUGIN_ID")
|
|
35
|
+
protocol, _ := strconv.Atoi(os.Getenv("MNDW_BACKEND_PROTOCOL"))
|
|
36
|
+
|
|
37
|
+
listener, err := net.Listen("unix", socketPath)
|
|
38
|
+
if err != nil {
|
|
39
|
+
return err
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
mux := http.NewServeMux()
|
|
43
|
+
|
|
44
|
+
mux.HandleFunc("GET /_mndw/ready", func(w http.ResponseWriter, _ *http.Request) {
|
|
45
|
+
writeJSON(w, http.StatusOK, readyResponse{PluginID: pluginID, ProtocolVersion: protocol})
|
|
46
|
+
})
|
|
47
|
+
mux.HandleFunc("GET /hello", func(w http.ResponseWriter, _ *http.Request) {
|
|
48
|
+
writeJSON(w, http.StatusOK, helloResponse{Message: helloMessage(time.Now()), Plugin: pluginID})
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
server := &http.Server{Handler: mux, ReadHeaderTimeout: readHeaderTimeout}
|
|
52
|
+
serveErr := make(chan error, 1)
|
|
53
|
+
|
|
54
|
+
go func() { serveErr <- server.Serve(listener) }()
|
|
55
|
+
|
|
56
|
+
select {
|
|
57
|
+
case <-ctx.Done():
|
|
58
|
+
shutdownCtx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
|
|
59
|
+
|
|
60
|
+
defer cancel()
|
|
61
|
+
|
|
62
|
+
_ = server.Shutdown(shutdownCtx)
|
|
63
|
+
|
|
64
|
+
if err := <-serveErr; err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
65
|
+
return err
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return nil
|
|
69
|
+
case err := <-serveErr:
|
|
70
|
+
if errors.Is(err, http.ErrServerClosed) {
|
|
71
|
+
return nil
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return err
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
func helloMessage(now time.Time) string {
|
|
79
|
+
return "hello + " + now.UTC().Format(time.RFC3339Nano)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
func writeJSON(w http.ResponseWriter, status int, value any) {
|
|
83
|
+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
84
|
+
w.WriteHeader(status)
|
|
85
|
+
|
|
86
|
+
_ = json.NewEncoder(w).Encode(value)
|
|
87
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
package httpapi
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"encoding/json"
|
|
5
|
+
"net/http"
|
|
6
|
+
"net/http/httptest"
|
|
7
|
+
"testing"
|
|
8
|
+
"time"
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
func TestReadyResponseHasExactKeys(t *testing.T) {
|
|
12
|
+
recorder := httptest.NewRecorder()
|
|
13
|
+
|
|
14
|
+
writeJSON(recorder, http.StatusOK, readyResponse{PluginID: "p", ProtocolVersion: 1})
|
|
15
|
+
|
|
16
|
+
if contentType := recorder.Header().Get("Content-Type"); contentType != "application/json; charset=utf-8" {
|
|
17
|
+
t.Fatalf("content-type = %q", contentType)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
var decoded map[string]any
|
|
21
|
+
|
|
22
|
+
if err := json.Unmarshal(recorder.Body.Bytes(), &decoded); err != nil {
|
|
23
|
+
t.Fatal(err)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if _, ok := decoded["pluginId"].(string); !ok {
|
|
27
|
+
t.Fatalf("pluginId missing or not string: %#v", decoded["pluginId"])
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if _, ok := decoded["protocolVersion"].(float64); !ok {
|
|
31
|
+
t.Fatalf("protocolVersion missing or not number: %#v", decoded["protocolVersion"])
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if len(decoded) != 2 {
|
|
35
|
+
t.Fatalf("unexpected extra keys: %#v", decoded)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
func TestHelloRejectsNonGetWith405(t *testing.T) {
|
|
40
|
+
mux := http.NewServeMux()
|
|
41
|
+
|
|
42
|
+
mux.HandleFunc("GET /hello", func(w http.ResponseWriter, _ *http.Request) {
|
|
43
|
+
writeJSON(w, http.StatusOK, helloResponse{})
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
recorder := httptest.NewRecorder()
|
|
47
|
+
|
|
48
|
+
mux.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/hello", nil))
|
|
49
|
+
|
|
50
|
+
if recorder.Code != http.StatusMethodNotAllowed {
|
|
51
|
+
t.Fatalf("status = %d, want 405", recorder.Code)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
func TestTimeoutConstants(t *testing.T) {
|
|
56
|
+
if readHeaderTimeout != 5*time.Second {
|
|
57
|
+
t.Fatalf("readHeaderTimeout = %v, want 5s", readHeaderTimeout)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if shutdownTimeout != 500*time.Millisecond {
|
|
61
|
+
t.Fatalf("shutdownTimeout = %v, want 500ms", shutdownTimeout)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "__MNDW_SCHEMA__",
|
|
3
|
+
"manifestVersion": 1,
|
|
4
|
+
"name": "__MNDW_NAME__",
|
|
5
|
+
"version": "0.1.0",
|
|
6
|
+
"type": "page",
|
|
7
|
+
"label": "__MNDW_LABEL__",
|
|
8
|
+
"entry": "index.js",
|
|
9
|
+
"route": "__MNDW_ROUTE__",
|
|
10
|
+
"i18n": {
|
|
11
|
+
"defaultLocale": "en",
|
|
12
|
+
"locales": {
|
|
13
|
+
"en": "i18n/en.json",
|
|
14
|
+
"ru": "i18n/ru.json"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"backend": {
|
|
18
|
+
"kind": "service",
|
|
19
|
+
"protocolVersion": 1,
|
|
20
|
+
"command": "backend/__MNDW_NAME__d",
|
|
21
|
+
"routes": [
|
|
22
|
+
{"id": "hello", "transport": "http", "method": "GET", "path": "hello"}
|
|
23
|
+
]
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "__MNDW_NAME__",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"scripts": {
|
|
5
|
+
"build-backend": "mndw build-backend",
|
|
6
|
+
"test": "cd backend && GOWORK=off go test ./... && cd .. && vitest run"
|
|
7
|
+
},
|
|
8
|
+
"mndw": {
|
|
9
|
+
"backendBuildScript": "build-backend",
|
|
10
|
+
"backend": {
|
|
11
|
+
"binaryName": "__MNDW_NAME__d",
|
|
12
|
+
"entryPackage": "./cmd/__MNDW_NAME__d"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"tsx": "^4.23.0",
|
|
17
|
+
"vitest": "^3.2.7",
|
|
18
|
+
"happy-dom": "^20.11.1"
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
.mndw-__MNDW_NAME__ {
|
|
2
|
+
padding: var(--mndw-space-4);
|
|
3
|
+
background: var(--mndw-surface);
|
|
4
|
+
color: var(--mndw-text);
|
|
5
|
+
font-family: var(--mndw-font-sans);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
.mndw-__MNDW_NAME____title {
|
|
9
|
+
margin: 0 0 var(--mndw-space-2);
|
|
10
|
+
font-weight: var(--mndw-weight-semibold);
|
|
11
|
+
font-size: var(--mndw-text-lg);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
.mndw-__MNDW_NAME____device {
|
|
15
|
+
margin: 0 0 var(--mndw-space-3);
|
|
16
|
+
color: var(--mndw-text-muted);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
.mndw-__MNDW_NAME____count {
|
|
20
|
+
padding: var(--mndw-space-2) var(--mndw-space-3);
|
|
21
|
+
border: none;
|
|
22
|
+
border-radius: var(--mndw-radius-md);
|
|
23
|
+
background: var(--mndw-accent);
|
|
24
|
+
color: var(--mndw-accent-contrast);
|
|
25
|
+
font: inherit;
|
|
26
|
+
cursor: pointer;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.mndw-__MNDW_NAME____count:hover {
|
|
30
|
+
background: var(--mndw-accent-hover);
|
|
31
|
+
}
|