@peers-app/peers-sdk 0.19.13 → 0.20.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/dist/contracts/__tests__/builder.test.js +24 -0
- package/dist/contracts/__tests__/contract-events.test.d.ts +1 -0
- package/dist/contracts/__tests__/contract-events.test.js +199 -0
- package/dist/contracts/__tests__/contract-observables.test.d.ts +1 -0
- package/dist/contracts/__tests__/contract-observables.test.js +290 -0
- package/dist/contracts/__tests__/contract-provider-router.test.d.ts +1 -0
- package/dist/contracts/__tests__/contract-provider-router.test.js +193 -0
- package/dist/contracts/__tests__/contract-proxy.test.d.ts +1 -0
- package/dist/contracts/__tests__/contract-proxy.test.js +424 -0
- package/dist/contracts/__tests__/contract-resolvers.test.d.ts +1 -0
- package/dist/contracts/__tests__/contract-resolvers.test.js +148 -0
- package/dist/contracts/__tests__/contract-subscription-lifecycle.test.d.ts +1 -0
- package/dist/contracts/__tests__/contract-subscription-lifecycle.test.js +353 -0
- package/dist/contracts/__tests__/contract-table-events.test.d.ts +1 -0
- package/dist/contracts/__tests__/contract-table-events.test.js +209 -0
- package/dist/contracts/__tests__/contract-transport.test.d.ts +1 -0
- package/dist/contracts/__tests__/contract-transport.test.js +163 -0
- package/dist/contracts/__tests__/persistent-registry.test.d.ts +1 -0
- package/dist/contracts/__tests__/persistent-registry.test.js +109 -0
- package/dist/contracts/__tests__/playground.test.d.ts +1 -0
- package/dist/contracts/__tests__/playground.test.js +213 -0
- package/dist/contracts/__tests__/validate.test.js +122 -0
- package/dist/contracts/builder.d.ts +9 -1
- package/dist/contracts/builder.js +13 -0
- package/dist/contracts/contract-provider-router.d.ts +72 -0
- package/dist/contracts/contract-provider-router.js +164 -0
- package/dist/contracts/contract-proxy.d.ts +333 -0
- package/dist/contracts/contract-proxy.js +663 -0
- package/dist/contracts/contract-resolvers.d.ts +66 -0
- package/dist/contracts/contract-resolvers.js +138 -0
- package/dist/contracts/contract-transport.d.ts +94 -0
- package/dist/contracts/contract-transport.js +159 -0
- package/dist/contracts/contracts.table.d.ts +1 -1
- package/dist/contracts/contracts.table.js +2 -2
- package/dist/contracts/index.d.ts +6 -1
- package/dist/contracts/index.js +25 -1
- package/dist/contracts/persistent-registry.js +3 -1
- package/dist/contracts/system/logs-contract.d.ts +24 -0
- package/dist/contracts/system/logs-contract.js +60 -0
- package/dist/contracts/types.d.ts +27 -4
- package/dist/contracts/validate.d.ts +2 -2
- package/dist/contracts/validate.js +44 -2
- package/dist/data/orm/decorators.d.ts +6 -0
- package/dist/data/orm/decorators.js +21 -0
- package/dist/data/user-trust-levels.d.ts +2 -0
- package/dist/data/user-trust-levels.js +2 -1
- package/dist/data/user-trust-levels.test.d.ts +1 -0
- package/dist/data/user-trust-levels.test.js +37 -0
- package/dist/device/binary-peer-connection-v2.d.ts +2 -0
- package/dist/device/binary-peer-connection-v2.js +12 -2
- package/dist/device/binary-peer-connection-v2.test.js +23 -0
- package/dist/device/get-trust-level-fn.d.ts +6 -0
- package/dist/device/get-trust-level-fn.js +12 -7
- package/dist/device/get-trust-level-fn.test.js +16 -2
- package/dist/logging/console-logs.table.d.ts +3 -0
- package/dist/logging/console-logs.table.js +2 -1
- package/dist/system-ids.d.ts +11 -0
- package/dist/system-ids.js +12 -1
- package/package.json +1 -1
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const utils_1 = require("../../utils");
|
|
4
|
+
const index_1 = require("../index");
|
|
5
|
+
const makeCall = (overrides = {}) => ({
|
|
6
|
+
contractId: "contract-a",
|
|
7
|
+
version: 1,
|
|
8
|
+
member: "table",
|
|
9
|
+
name: "Items",
|
|
10
|
+
operation: "list",
|
|
11
|
+
args: [],
|
|
12
|
+
...overrides,
|
|
13
|
+
});
|
|
14
|
+
const makeEndpoint = (label) => ({
|
|
15
|
+
handleCall: jest.fn(async (call) => `${label}:${call.operation}`),
|
|
16
|
+
dispose: jest.fn(),
|
|
17
|
+
});
|
|
18
|
+
describe("contract provider router", () => {
|
|
19
|
+
it("multiplexes routes and caches normalized contract/version/data-context endpoints", async () => {
|
|
20
|
+
const [consumerTransport, providerTransport] = (0, index_1.inProcessTransportPair)();
|
|
21
|
+
const endpoints = new Map();
|
|
22
|
+
const resolveEndpoint = jest.fn((request) => {
|
|
23
|
+
const key = JSON.stringify([request.contractId, request.version, request.dataContextId]);
|
|
24
|
+
const endpoint = makeEndpoint(key);
|
|
25
|
+
endpoints.set(key, endpoint);
|
|
26
|
+
return endpoint;
|
|
27
|
+
});
|
|
28
|
+
const router = (0, index_1.createContractProviderRouter)(providerTransport, {
|
|
29
|
+
authorizeCall: (call) => ({
|
|
30
|
+
dataContextId: call.dataContextId || "personal-context",
|
|
31
|
+
}),
|
|
32
|
+
resolveEndpoint,
|
|
33
|
+
});
|
|
34
|
+
await consumerTransport.emit(index_1.CONTRACT_REQUEST_CHANNEL, makeCall());
|
|
35
|
+
await consumerTransport.emit(index_1.CONTRACT_REQUEST_CHANNEL, makeCall({ dataContextId: "personal-context", operation: "count" }));
|
|
36
|
+
await consumerTransport.emit(index_1.CONTRACT_REQUEST_CHANNEL, makeCall({ dataContextId: "group-context" }));
|
|
37
|
+
await consumerTransport.emit(index_1.CONTRACT_REQUEST_CHANNEL, makeCall({ contractId: "contract-b" }));
|
|
38
|
+
expect(resolveEndpoint).toHaveBeenCalledTimes(3);
|
|
39
|
+
expect(resolveEndpoint.mock.calls.map(([request]) => [
|
|
40
|
+
request.contractId,
|
|
41
|
+
request.version,
|
|
42
|
+
request.dataContextId,
|
|
43
|
+
])).toEqual([
|
|
44
|
+
["contract-a", 1, "personal-context"],
|
|
45
|
+
["contract-a", 1, "group-context"],
|
|
46
|
+
["contract-b", 1, "personal-context"],
|
|
47
|
+
]);
|
|
48
|
+
expect((endpoints.get('["contract-a",1,"personal-context"]')?.handleCall).mock.calls).toHaveLength(2);
|
|
49
|
+
router.dispose();
|
|
50
|
+
for (const endpoint of endpoints.values()) {
|
|
51
|
+
expect(endpoint.dispose).toHaveBeenCalledTimes(1);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
it("denies before invoking the endpoint resolver", async () => {
|
|
55
|
+
const [consumerTransport, providerTransport] = (0, index_1.inProcessTransportPair)();
|
|
56
|
+
const resolveEndpoint = jest.fn(() => makeEndpoint("denied"));
|
|
57
|
+
const router = (0, index_1.createContractProviderRouter)(providerTransport, {
|
|
58
|
+
authorizeCall: async () => {
|
|
59
|
+
throw new Error("Permission denied by host");
|
|
60
|
+
},
|
|
61
|
+
resolveEndpoint,
|
|
62
|
+
});
|
|
63
|
+
await expect(consumerTransport.emit(index_1.CONTRACT_REQUEST_CHANNEL, makeCall())).rejects.toThrow("Permission denied by host");
|
|
64
|
+
expect(resolveEndpoint).not.toHaveBeenCalled();
|
|
65
|
+
router.dispose();
|
|
66
|
+
});
|
|
67
|
+
it("ignores peer-supplied context in favor of trusted host context", async () => {
|
|
68
|
+
const [consumerTransport, providerTransport] = (0, index_1.inProcessTransportPair)();
|
|
69
|
+
const trustedContext = {
|
|
70
|
+
callerUserId: "trusted-caller",
|
|
71
|
+
localUserId: "trusted-local",
|
|
72
|
+
};
|
|
73
|
+
const endpoint = makeEndpoint("trusted");
|
|
74
|
+
const authorizeCall = jest.fn((_call, context) => ({
|
|
75
|
+
dataContextId: "personal-context",
|
|
76
|
+
context,
|
|
77
|
+
}));
|
|
78
|
+
const resolveEndpoint = jest.fn(() => endpoint);
|
|
79
|
+
const router = (0, index_1.createContractProviderRouter)(providerTransport, {
|
|
80
|
+
context: trustedContext,
|
|
81
|
+
authorizeCall,
|
|
82
|
+
resolveEndpoint,
|
|
83
|
+
});
|
|
84
|
+
await consumerTransport.emit(index_1.CONTRACT_REQUEST_CHANNEL, makeCall(), {
|
|
85
|
+
callerUserId: "spoofed-caller",
|
|
86
|
+
localUserId: "spoofed-local",
|
|
87
|
+
});
|
|
88
|
+
expect(authorizeCall).toHaveBeenCalledWith(expect.anything(), trustedContext);
|
|
89
|
+
expect(resolveEndpoint).toHaveBeenCalledWith(expect.objectContaining({ context: trustedContext }));
|
|
90
|
+
expect(endpoint.handleCall).toHaveBeenCalledWith(expect.anything(), trustedContext);
|
|
91
|
+
router.dispose();
|
|
92
|
+
});
|
|
93
|
+
it("routes independent subscriptions and skips authorization for idempotent unsubscribe", async () => {
|
|
94
|
+
const [consumerTransport, providerTransport] = (0, index_1.inProcessTransportPair)();
|
|
95
|
+
const endpoint = makeEndpoint("subscriptions");
|
|
96
|
+
const authorizeCall = jest.fn((call) => ({
|
|
97
|
+
dataContextId: call.dataContextId || "personal-context",
|
|
98
|
+
}));
|
|
99
|
+
const resolveEndpoint = jest.fn(() => endpoint);
|
|
100
|
+
const router = (0, index_1.createContractProviderRouter)(providerTransport, {
|
|
101
|
+
authorizeCall,
|
|
102
|
+
resolveEndpoint,
|
|
103
|
+
});
|
|
104
|
+
const firstId = (0, utils_1.newid)();
|
|
105
|
+
const secondId = (0, utils_1.newid)();
|
|
106
|
+
await consumerTransport.emit(index_1.CONTRACT_REQUEST_CHANNEL, makeCall({
|
|
107
|
+
member: "event",
|
|
108
|
+
name: "changed",
|
|
109
|
+
operation: "subscribe",
|
|
110
|
+
subscriptionId: firstId,
|
|
111
|
+
}));
|
|
112
|
+
await consumerTransport.emit(index_1.CONTRACT_REQUEST_CHANNEL, makeCall({
|
|
113
|
+
member: "event",
|
|
114
|
+
name: "changed",
|
|
115
|
+
operation: "subscribe",
|
|
116
|
+
subscriptionId: secondId,
|
|
117
|
+
}));
|
|
118
|
+
await consumerTransport.emit(index_1.CONTRACT_REQUEST_CHANNEL, makeCall({
|
|
119
|
+
member: "event",
|
|
120
|
+
name: "ignored-by-router",
|
|
121
|
+
operation: "unsubscribe",
|
|
122
|
+
subscriptionId: firstId,
|
|
123
|
+
}));
|
|
124
|
+
await consumerTransport.emit(index_1.CONTRACT_REQUEST_CHANNEL, makeCall({
|
|
125
|
+
member: "event",
|
|
126
|
+
name: "changed",
|
|
127
|
+
operation: "unsubscribe",
|
|
128
|
+
subscriptionId: (0, utils_1.newid)(),
|
|
129
|
+
}));
|
|
130
|
+
expect(authorizeCall).toHaveBeenCalledTimes(2);
|
|
131
|
+
expect(resolveEndpoint).toHaveBeenCalledTimes(1);
|
|
132
|
+
expect(endpoint.handleCall).toHaveBeenCalledTimes(3);
|
|
133
|
+
expect(endpoint.handleCall).toHaveBeenLastCalledWith(expect.objectContaining({
|
|
134
|
+
name: "changed",
|
|
135
|
+
operation: "unsubscribe",
|
|
136
|
+
subscriptionId: firstId,
|
|
137
|
+
}), {});
|
|
138
|
+
router.dispose();
|
|
139
|
+
});
|
|
140
|
+
it("tears down a subscription cancelled while authorization is pending", async () => {
|
|
141
|
+
const [consumerTransport, providerTransport] = (0, index_1.inProcessTransportPair)();
|
|
142
|
+
const endpoint = makeEndpoint("pending");
|
|
143
|
+
let finishAuthorization;
|
|
144
|
+
const authorizationReady = new Promise((resolve) => {
|
|
145
|
+
finishAuthorization = resolve;
|
|
146
|
+
});
|
|
147
|
+
const router = (0, index_1.createContractProviderRouter)(providerTransport, {
|
|
148
|
+
authorizeCall: async () => {
|
|
149
|
+
await authorizationReady;
|
|
150
|
+
return { dataContextId: "personal-context" };
|
|
151
|
+
},
|
|
152
|
+
resolveEndpoint: () => endpoint,
|
|
153
|
+
});
|
|
154
|
+
const subscriptionId = (0, utils_1.newid)();
|
|
155
|
+
const subscribeCall = makeCall({
|
|
156
|
+
member: "event",
|
|
157
|
+
name: "changed",
|
|
158
|
+
operation: "subscribe",
|
|
159
|
+
subscriptionId,
|
|
160
|
+
});
|
|
161
|
+
const subscribePromise = consumerTransport.emit(index_1.CONTRACT_REQUEST_CHANNEL, subscribeCall);
|
|
162
|
+
await expect(consumerTransport.emit(index_1.CONTRACT_REQUEST_CHANNEL, {
|
|
163
|
+
...subscribeCall,
|
|
164
|
+
operation: "unsubscribe",
|
|
165
|
+
})).resolves.toBeUndefined();
|
|
166
|
+
finishAuthorization();
|
|
167
|
+
await expect(subscribePromise).resolves.toBe("pending:subscribe");
|
|
168
|
+
expect(endpoint.handleCall.mock.calls.map(([call]) => call.operation)).toEqual(["subscribe", "unsubscribe"]);
|
|
169
|
+
router.dispose();
|
|
170
|
+
});
|
|
171
|
+
it("disposes an endpoint whose asynchronous resolution finishes during teardown", async () => {
|
|
172
|
+
const [consumerTransport, providerTransport] = (0, index_1.inProcessTransportPair)();
|
|
173
|
+
const endpoint = makeEndpoint("late");
|
|
174
|
+
let resolveEndpointPromise;
|
|
175
|
+
const endpointPromise = new Promise((resolve) => {
|
|
176
|
+
resolveEndpointPromise = resolve;
|
|
177
|
+
});
|
|
178
|
+
const resolveEndpoint = jest.fn(() => endpointPromise);
|
|
179
|
+
const router = (0, index_1.createContractProviderRouter)(providerTransport, {
|
|
180
|
+
authorizeCall: () => ({ dataContextId: "personal-context" }),
|
|
181
|
+
resolveEndpoint,
|
|
182
|
+
});
|
|
183
|
+
const callPromise = consumerTransport.emit(index_1.CONTRACT_REQUEST_CHANNEL, makeCall());
|
|
184
|
+
await Promise.resolve();
|
|
185
|
+
await Promise.resolve();
|
|
186
|
+
expect(resolveEndpoint).toHaveBeenCalledTimes(1);
|
|
187
|
+
router.dispose();
|
|
188
|
+
resolveEndpointPromise(endpoint);
|
|
189
|
+
await expect(callPromise).rejects.toThrow("Contract provider router is disposed");
|
|
190
|
+
expect(endpoint.dispose).toHaveBeenCalledTimes(1);
|
|
191
|
+
expect(await consumerTransport.emit(index_1.CONTRACT_REQUEST_CHANNEL, makeCall())).toBeUndefined();
|
|
192
|
+
});
|
|
193
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const decorators_1 = require("../../data/orm/decorators");
|
|
4
|
+
const observable_1 = require("../../observable");
|
|
5
|
+
const field_type_1 = require("../../types/field-type");
|
|
6
|
+
const utils_1 = require("../../utils");
|
|
7
|
+
const contract_proxy_1 = require("../contract-proxy");
|
|
8
|
+
const contract_transport_1 = require("../contract-transport");
|
|
9
|
+
const CONTRACT_ID = (0, utils_1.newid)();
|
|
10
|
+
const cleanups = [];
|
|
11
|
+
afterEach(() => {
|
|
12
|
+
for (const cleanup of cleanups.splice(0).reverse()) {
|
|
13
|
+
cleanup();
|
|
14
|
+
}
|
|
15
|
+
jest.restoreAllMocks();
|
|
16
|
+
});
|
|
17
|
+
/** A small contract with one table, one tool, and one observable. */
|
|
18
|
+
function makeContract(opts = {}) {
|
|
19
|
+
return {
|
|
20
|
+
contractId: CONTRACT_ID,
|
|
21
|
+
version: 1,
|
|
22
|
+
devTag: "dev",
|
|
23
|
+
name: "Test Contract",
|
|
24
|
+
description: "For proxy tests",
|
|
25
|
+
tables: [
|
|
26
|
+
{
|
|
27
|
+
name: "Widgets",
|
|
28
|
+
description: "Widget table",
|
|
29
|
+
primaryKeyName: "widgetId",
|
|
30
|
+
fields: [{ name: "widgetId", type: field_type_1.FieldType.id, description: "PK" }],
|
|
31
|
+
},
|
|
32
|
+
],
|
|
33
|
+
tools: [
|
|
34
|
+
{
|
|
35
|
+
name: "make-widget",
|
|
36
|
+
usageDescription: "Makes a widget",
|
|
37
|
+
inputFields: [{ name: "color", type: field_type_1.FieldType.string, description: "Color" }],
|
|
38
|
+
outputFields: [{ name: "widgetId", type: field_type_1.FieldType.id, description: "ID" }],
|
|
39
|
+
},
|
|
40
|
+
],
|
|
41
|
+
observables: opts.observable
|
|
42
|
+
? [
|
|
43
|
+
{
|
|
44
|
+
name: "widgetCount",
|
|
45
|
+
description: "Count",
|
|
46
|
+
valueType: field_type_1.FieldType.number,
|
|
47
|
+
writable: true,
|
|
48
|
+
},
|
|
49
|
+
]
|
|
50
|
+
: [],
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* A fake resolution with an in-memory table (list/get/save), a tool fn, and an observable.
|
|
55
|
+
* Tables in a real resolution are `Table` instances; for dispatch the provider only needs
|
|
56
|
+
* indexable methods, so a plain object cast to the resolution type is sufficient here.
|
|
57
|
+
*/
|
|
58
|
+
function makeResolution() {
|
|
59
|
+
const rows = [];
|
|
60
|
+
const table = {
|
|
61
|
+
list: jest.fn(async (filter = {}) => rows.filter((r) => Object.entries(filter).every(([k, v]) => r[k] === v))),
|
|
62
|
+
get: jest.fn(async (id) => rows.find((r) => r.widgetId === id)),
|
|
63
|
+
save: jest.fn(async (record) => {
|
|
64
|
+
rows.push(record);
|
|
65
|
+
return record;
|
|
66
|
+
}),
|
|
67
|
+
};
|
|
68
|
+
const makeWidget = jest.fn(async ({ color }) => ({
|
|
69
|
+
widgetId: (0, utils_1.newid)(),
|
|
70
|
+
color,
|
|
71
|
+
}));
|
|
72
|
+
const widgetCount = (0, observable_1.observable)(0);
|
|
73
|
+
const resolution = {
|
|
74
|
+
contractId: CONTRACT_ID,
|
|
75
|
+
version: 1,
|
|
76
|
+
tables: { Widgets: table },
|
|
77
|
+
tools: {
|
|
78
|
+
"make-widget": {
|
|
79
|
+
tool: { toolId: (0, utils_1.newid)(), name: "make-widget" },
|
|
80
|
+
toolFn: makeWidget,
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
observables: { widgetCount },
|
|
84
|
+
observableWritability: { widgetCount: true },
|
|
85
|
+
};
|
|
86
|
+
return { resolution, table, makeWidget, widgetCount, rows };
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Build a consumer wired to a provider dispatch fn over an in-process transport pair:
|
|
90
|
+
* the provider handler is registered on the provider end's request channel, and the
|
|
91
|
+
* consumer is built on the other end.
|
|
92
|
+
*/
|
|
93
|
+
function consumerVia(contract, provider, context = {}) {
|
|
94
|
+
const [consumerTransport, providerTransport] = (0, contract_transport_1.inProcessTransportPair)();
|
|
95
|
+
const offRequest = providerTransport.on(contract_transport_1.CONTRACT_REQUEST_CHANNEL, (call) => provider(call, context));
|
|
96
|
+
const consumer = (0, contract_proxy_1.createContractConsumer)(contract, consumerTransport);
|
|
97
|
+
cleanups.push(() => {
|
|
98
|
+
consumer.dispose();
|
|
99
|
+
offRequest();
|
|
100
|
+
});
|
|
101
|
+
return consumer;
|
|
102
|
+
}
|
|
103
|
+
/** Wire a request/response-only consumer directly to a stateless provider. */
|
|
104
|
+
function wire(opts = {}) {
|
|
105
|
+
const contract = makeContract();
|
|
106
|
+
const { resolution, ...rest } = makeResolution();
|
|
107
|
+
const provider = (0, contract_proxy_1.createContractProvider)(resolution, { permissionCheck: opts.permissionCheck });
|
|
108
|
+
const consumer = consumerVia(contract, provider, opts.context ?? {});
|
|
109
|
+
return { consumer, provider, contract, ...rest };
|
|
110
|
+
}
|
|
111
|
+
/** Wire an observable-bearing consumer to a stateful provider session. */
|
|
112
|
+
function wireSession(opts = {}) {
|
|
113
|
+
const contract = makeContract({ observable: true });
|
|
114
|
+
const { resolution, ...rest } = makeResolution();
|
|
115
|
+
const [consumerTransport, providerTransport] = (0, contract_transport_1.inProcessTransportPair)();
|
|
116
|
+
const session = (0, contract_proxy_1.createContractProviderSession)(resolution, providerTransport, {
|
|
117
|
+
permissionCheck: opts.permissionCheck,
|
|
118
|
+
context: opts.context,
|
|
119
|
+
});
|
|
120
|
+
const consumer = (0, contract_proxy_1.createContractConsumer)(contract, consumerTransport);
|
|
121
|
+
cleanups.push(() => {
|
|
122
|
+
consumer.dispose();
|
|
123
|
+
session.dispose();
|
|
124
|
+
});
|
|
125
|
+
return { consumer, session, contract, ...rest };
|
|
126
|
+
}
|
|
127
|
+
describe("contract proxy — consumer/provider round-trip", () => {
|
|
128
|
+
it("proxies a table method (save then list) through to the live table", async () => {
|
|
129
|
+
const { consumer, table } = wire();
|
|
130
|
+
const saved = await consumer.tables.Widgets.save({ widgetId: "w1", color: "red" });
|
|
131
|
+
expect(saved).toEqual({ widgetId: "w1", color: "red" });
|
|
132
|
+
expect(table.save).toHaveBeenCalledWith({ widgetId: "w1", color: "red" });
|
|
133
|
+
const listed = await consumer.tables.Widgets.list({ color: "red" });
|
|
134
|
+
expect(listed).toEqual([{ widgetId: "w1", color: "red" }]);
|
|
135
|
+
expect(table.list).toHaveBeenCalledWith({ color: "red" });
|
|
136
|
+
});
|
|
137
|
+
it("passes multiple positional args to a table method", async () => {
|
|
138
|
+
const { consumer, table } = wire();
|
|
139
|
+
const filter = { color: "blue" };
|
|
140
|
+
const opts = { pageSize: 10 };
|
|
141
|
+
await consumer.tables.Widgets.list(filter, opts);
|
|
142
|
+
expect(table.list).toHaveBeenCalledWith(filter, opts);
|
|
143
|
+
});
|
|
144
|
+
it("proxies a tool call", async () => {
|
|
145
|
+
const { consumer, makeWidget } = wire();
|
|
146
|
+
const result = await consumer.tools["make-widget"]({ color: "blue" });
|
|
147
|
+
expect(makeWidget).toHaveBeenCalledWith({ color: "blue" });
|
|
148
|
+
expect(result.color).toBe("blue");
|
|
149
|
+
});
|
|
150
|
+
it("forwards dataContextId on methods, tools, observables, and subscriptions", async () => {
|
|
151
|
+
const dataContextId = (0, utils_1.newid)();
|
|
152
|
+
const calls = [];
|
|
153
|
+
const transport = {
|
|
154
|
+
emit: jest.fn(async (channel, call) => {
|
|
155
|
+
expect(channel).toBe(contract_transport_1.CONTRACT_REQUEST_CHANNEL);
|
|
156
|
+
calls.push(call);
|
|
157
|
+
return call.member === "observable" && call.operation === "get" ? 0 : undefined;
|
|
158
|
+
}),
|
|
159
|
+
on: () => () => { },
|
|
160
|
+
};
|
|
161
|
+
const contract = {
|
|
162
|
+
...makeContract({ observable: true }),
|
|
163
|
+
events: [
|
|
164
|
+
{
|
|
165
|
+
name: "changed",
|
|
166
|
+
description: "Changed",
|
|
167
|
+
payloadFields: [],
|
|
168
|
+
},
|
|
169
|
+
],
|
|
170
|
+
};
|
|
171
|
+
const consumer = (0, contract_proxy_1.createContractConsumer)(contract, transport, { dataContextId });
|
|
172
|
+
cleanups.push(() => consumer.dispose());
|
|
173
|
+
await consumer.loadingPromise;
|
|
174
|
+
await consumer.tables.Widgets.list();
|
|
175
|
+
await consumer.tools["make-widget"]({ color: "blue" });
|
|
176
|
+
consumer.events.changed.subscribe(() => { });
|
|
177
|
+
consumer.tables.Widgets.dataChanged.subscribe(() => { });
|
|
178
|
+
consumer.observables.widgetCount(2);
|
|
179
|
+
await consumer.loadingPromise;
|
|
180
|
+
expect(calls).toEqual(expect.arrayContaining([
|
|
181
|
+
expect.objectContaining({ member: "table", operation: "list" }),
|
|
182
|
+
expect.objectContaining({ member: "tool", operation: "run" }),
|
|
183
|
+
expect.objectContaining({ member: "observable", operation: "subscribe" }),
|
|
184
|
+
expect.objectContaining({ member: "observable", operation: "get" }),
|
|
185
|
+
expect.objectContaining({ member: "observable", operation: "set" }),
|
|
186
|
+
expect.objectContaining({ member: "event", operation: "subscribe" }),
|
|
187
|
+
expect.objectContaining({
|
|
188
|
+
member: "table",
|
|
189
|
+
operation: "subscribe",
|
|
190
|
+
event: "dataChanged",
|
|
191
|
+
}),
|
|
192
|
+
]));
|
|
193
|
+
expect(calls.every((call) => call.dataContextId === dataContextId)).toBe(true);
|
|
194
|
+
});
|
|
195
|
+
it("mirrors an observable snapshot and provider pushes through a session", async () => {
|
|
196
|
+
const { consumer, widgetCount } = wireSession();
|
|
197
|
+
await consumer.loadingPromise;
|
|
198
|
+
expect(consumer.observables.widgetCount()).toBe(0);
|
|
199
|
+
const pushed = new Promise((resolve) => {
|
|
200
|
+
const sub = consumer.observables.widgetCount.subscribe((value) => {
|
|
201
|
+
if (value === 4) {
|
|
202
|
+
sub.dispose();
|
|
203
|
+
resolve();
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
widgetCount(4);
|
|
208
|
+
await pushed;
|
|
209
|
+
expect(consumer.observables.widgetCount()).toBe(4);
|
|
210
|
+
});
|
|
211
|
+
it("forwards writable observable writes through a session", async () => {
|
|
212
|
+
const { consumer, widgetCount } = wireSession();
|
|
213
|
+
await consumer.loadingPromise;
|
|
214
|
+
consumer.observables.widgetCount(7);
|
|
215
|
+
await consumer.loadingPromise;
|
|
216
|
+
expect(widgetCount()).toBe(7);
|
|
217
|
+
expect(consumer.observables.widgetCount()).toBe(7);
|
|
218
|
+
});
|
|
219
|
+
it("dispatches stateless observable get and set calls directly", async () => {
|
|
220
|
+
const { resolution, widgetCount } = makeResolution();
|
|
221
|
+
const provider = (0, contract_proxy_1.createContractProvider)(resolution);
|
|
222
|
+
await expect(provider({
|
|
223
|
+
contractId: CONTRACT_ID,
|
|
224
|
+
version: 1,
|
|
225
|
+
member: "observable",
|
|
226
|
+
name: "widgetCount",
|
|
227
|
+
operation: "get",
|
|
228
|
+
args: [],
|
|
229
|
+
})).resolves.toBe(0);
|
|
230
|
+
await expect(provider({
|
|
231
|
+
contractId: CONTRACT_ID,
|
|
232
|
+
version: 1,
|
|
233
|
+
member: "observable",
|
|
234
|
+
name: "widgetCount",
|
|
235
|
+
operation: "set",
|
|
236
|
+
args: [8],
|
|
237
|
+
})).resolves.toBe(8);
|
|
238
|
+
expect(widgetCount()).toBe(8);
|
|
239
|
+
});
|
|
240
|
+
it("unwraps a client-proxy (__original) table method", async () => {
|
|
241
|
+
const contract = makeContract();
|
|
242
|
+
const original = jest.fn(async () => "original-result");
|
|
243
|
+
(0, decorators_1.ProxyClientTableMethodCalls)()(original, { kind: "method", name: "deleteOldLogs" });
|
|
244
|
+
const proxied = jest.fn(async () => "proxied-result");
|
|
245
|
+
proxied.__original = original;
|
|
246
|
+
const resolution = {
|
|
247
|
+
contractId: CONTRACT_ID,
|
|
248
|
+
version: 1,
|
|
249
|
+
tables: { Widgets: { deleteOldLogs: proxied } },
|
|
250
|
+
};
|
|
251
|
+
const provider = (0, contract_proxy_1.createContractProvider)(resolution);
|
|
252
|
+
const consumer = consumerVia(contract, provider);
|
|
253
|
+
const result = await consumer.tables.Widgets.deleteOldLogs();
|
|
254
|
+
expect(result).toBe("original-result");
|
|
255
|
+
expect(original).toHaveBeenCalled();
|
|
256
|
+
expect(proxied).not.toHaveBeenCalled();
|
|
257
|
+
});
|
|
258
|
+
});
|
|
259
|
+
describe("contract proxy — error handling", () => {
|
|
260
|
+
it("rejects an unknown table method", async () => {
|
|
261
|
+
const { consumer } = wire();
|
|
262
|
+
await expect(consumer.tables.Widgets.nope()).rejects.toThrow(/no method 'nope'/);
|
|
263
|
+
});
|
|
264
|
+
it.each([
|
|
265
|
+
"getRecordId",
|
|
266
|
+
"cursor",
|
|
267
|
+
"initDoc",
|
|
268
|
+
])("rejects unmarked internal or non-serializable table method %s", async (operation) => {
|
|
269
|
+
const contract = makeContract();
|
|
270
|
+
const method = jest.fn();
|
|
271
|
+
const resolution = {
|
|
272
|
+
contractId: CONTRACT_ID,
|
|
273
|
+
version: 1,
|
|
274
|
+
tables: { Widgets: { [operation]: method } },
|
|
275
|
+
};
|
|
276
|
+
const provider = (0, contract_proxy_1.createContractProvider)(resolution);
|
|
277
|
+
const consumer = consumerVia(contract, provider);
|
|
278
|
+
await expect(consumer.tables.Widgets[operation]()).rejects.toThrow(/not remotely callable/);
|
|
279
|
+
expect(method).not.toHaveBeenCalled();
|
|
280
|
+
});
|
|
281
|
+
it.each([
|
|
282
|
+
"constructor",
|
|
283
|
+
"__proto__",
|
|
284
|
+
"dataChanged",
|
|
285
|
+
"then",
|
|
286
|
+
])("rejects dangerous table property %s before reading it", async (operation) => {
|
|
287
|
+
const table = Object.create(null);
|
|
288
|
+
const getter = jest.fn();
|
|
289
|
+
Object.defineProperty(table, operation, { get: getter });
|
|
290
|
+
const provider = (0, contract_proxy_1.createContractProvider)({
|
|
291
|
+
contractId: CONTRACT_ID,
|
|
292
|
+
version: 1,
|
|
293
|
+
tables: { Widgets: table },
|
|
294
|
+
});
|
|
295
|
+
await expect(provider({
|
|
296
|
+
contractId: CONTRACT_ID,
|
|
297
|
+
version: 1,
|
|
298
|
+
member: "table",
|
|
299
|
+
name: "Widgets",
|
|
300
|
+
operation,
|
|
301
|
+
args: [],
|
|
302
|
+
})).rejects.toThrow(/not remotely callable/);
|
|
303
|
+
expect(getter).not.toHaveBeenCalled();
|
|
304
|
+
});
|
|
305
|
+
it("rejects a call for a table not in the resolution", async () => {
|
|
306
|
+
const contract = makeContract();
|
|
307
|
+
const resolution = { contractId: CONTRACT_ID, version: 1, tables: {} };
|
|
308
|
+
const provider = (0, contract_proxy_1.createContractProvider)(resolution);
|
|
309
|
+
const consumer = consumerVia(contract, provider);
|
|
310
|
+
await expect(consumer.tables.Widgets.list()).rejects.toThrow(/does not provide table 'Widgets'/);
|
|
311
|
+
});
|
|
312
|
+
it.each([
|
|
313
|
+
["tool", "missingTool"],
|
|
314
|
+
["observable", "missingObservable"],
|
|
315
|
+
])("rejects an unknown %s", async (member, name) => {
|
|
316
|
+
const { resolution } = makeResolution();
|
|
317
|
+
const provider = (0, contract_proxy_1.createContractProvider)(resolution);
|
|
318
|
+
await expect(provider({
|
|
319
|
+
contractId: CONTRACT_ID,
|
|
320
|
+
version: 1,
|
|
321
|
+
member,
|
|
322
|
+
name,
|
|
323
|
+
operation: member === "tool" ? "run" : "get",
|
|
324
|
+
args: [],
|
|
325
|
+
})).rejects.toThrow(new RegExp(`does not provide ${member} '${name}'`));
|
|
326
|
+
});
|
|
327
|
+
it("rejects a contract id/version mismatch", async () => {
|
|
328
|
+
const { provider } = wire();
|
|
329
|
+
const badCall = {
|
|
330
|
+
contractId: (0, utils_1.newid)(),
|
|
331
|
+
version: 1,
|
|
332
|
+
member: "table",
|
|
333
|
+
name: "Widgets",
|
|
334
|
+
operation: "list",
|
|
335
|
+
args: [],
|
|
336
|
+
};
|
|
337
|
+
await expect(provider(badCall)).rejects.toThrow(/Contract mismatch/);
|
|
338
|
+
});
|
|
339
|
+
it("rejects a version-only contract mismatch", async () => {
|
|
340
|
+
const { provider } = wire();
|
|
341
|
+
await expect(provider({
|
|
342
|
+
contractId: CONTRACT_ID,
|
|
343
|
+
version: 2,
|
|
344
|
+
member: "table",
|
|
345
|
+
name: "Widgets",
|
|
346
|
+
operation: "list",
|
|
347
|
+
args: [],
|
|
348
|
+
})).rejects.toThrow(/Contract mismatch/);
|
|
349
|
+
});
|
|
350
|
+
it("rejects an unsupported observable operation", async () => {
|
|
351
|
+
const { provider } = wire();
|
|
352
|
+
const call = {
|
|
353
|
+
contractId: CONTRACT_ID,
|
|
354
|
+
version: 1,
|
|
355
|
+
member: "observable",
|
|
356
|
+
name: "widgetCount",
|
|
357
|
+
operation: "increment",
|
|
358
|
+
args: [],
|
|
359
|
+
};
|
|
360
|
+
await expect(provider(call)).rejects.toThrow(/Unsupported observable operation/);
|
|
361
|
+
});
|
|
362
|
+
it("rejects an unsupported tool operation without invoking the tool", async () => {
|
|
363
|
+
const { provider, makeWidget } = wire();
|
|
364
|
+
await expect(provider({
|
|
365
|
+
contractId: CONTRACT_ID,
|
|
366
|
+
version: 1,
|
|
367
|
+
member: "tool",
|
|
368
|
+
name: "make-widget",
|
|
369
|
+
operation: "get",
|
|
370
|
+
args: [],
|
|
371
|
+
})).rejects.toThrow(/Unsupported tool operation/);
|
|
372
|
+
expect(makeWidget).not.toHaveBeenCalled();
|
|
373
|
+
});
|
|
374
|
+
});
|
|
375
|
+
describe("contract proxy — permission checks", () => {
|
|
376
|
+
it("allows same-user calls and denies cross-user calls (stub)", async () => {
|
|
377
|
+
const localUserId = (0, utils_1.newid)();
|
|
378
|
+
const allowed = wire({
|
|
379
|
+
permissionCheck: contract_proxy_1.sameUserContractPermissionCheck,
|
|
380
|
+
context: { localUserId, callerUserId: localUserId },
|
|
381
|
+
});
|
|
382
|
+
await expect(allowed.consumer.tables.Widgets.list()).resolves.toEqual([]);
|
|
383
|
+
const denied = wire({
|
|
384
|
+
permissionCheck: contract_proxy_1.sameUserContractPermissionCheck,
|
|
385
|
+
context: { localUserId, callerUserId: (0, utils_1.newid)() },
|
|
386
|
+
});
|
|
387
|
+
await expect(denied.consumer.tables.Widgets.list()).rejects.toThrow(/Permission denied/);
|
|
388
|
+
});
|
|
389
|
+
it("denies when the caller identity is unknown", async () => {
|
|
390
|
+
const denied = wire({
|
|
391
|
+
permissionCheck: contract_proxy_1.sameUserContractPermissionCheck,
|
|
392
|
+
context: { localUserId: (0, utils_1.newid)() },
|
|
393
|
+
});
|
|
394
|
+
await expect(denied.consumer.tools["make-widget"]({ color: "x" })).rejects.toThrow(/Permission denied/);
|
|
395
|
+
});
|
|
396
|
+
it("does not dispatch to the table when permission is denied", async () => {
|
|
397
|
+
const localUserId = (0, utils_1.newid)();
|
|
398
|
+
const denied = wire({
|
|
399
|
+
permissionCheck: contract_proxy_1.sameUserContractPermissionCheck,
|
|
400
|
+
context: { localUserId, callerUserId: (0, utils_1.newid)() },
|
|
401
|
+
});
|
|
402
|
+
await expect(denied.consumer.tables.Widgets.save({ widgetId: "z" })).rejects.toThrow(/Permission denied/);
|
|
403
|
+
expect(denied.table.save).not.toHaveBeenCalled();
|
|
404
|
+
});
|
|
405
|
+
});
|
|
406
|
+
describe("connectionContractTransport", () => {
|
|
407
|
+
it("emits on the request channel with the call and returns the response", async () => {
|
|
408
|
+
const emit = jest.fn(async (_event, _call) => ["ok"]);
|
|
409
|
+
const emitter = { emit, on: jest.fn() };
|
|
410
|
+
const contract = makeContract();
|
|
411
|
+
const consumer = (0, contract_proxy_1.createContractConsumer)(contract, (0, contract_transport_1.connectionContractTransport)(emitter));
|
|
412
|
+
cleanups.push(() => consumer.dispose());
|
|
413
|
+
const result = await consumer.tables.Widgets.list({ color: "red" });
|
|
414
|
+
expect(result).toEqual(["ok"]);
|
|
415
|
+
expect(emit).toHaveBeenCalledWith("contractCall", expect.objectContaining({
|
|
416
|
+
contractId: CONTRACT_ID,
|
|
417
|
+
version: 1,
|
|
418
|
+
member: "table",
|
|
419
|
+
name: "Widgets",
|
|
420
|
+
operation: "list",
|
|
421
|
+
args: [{ color: "red" }],
|
|
422
|
+
}));
|
|
423
|
+
});
|
|
424
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|