-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsatellite.config.ts
169 lines (148 loc) · 4.69 KB
/
satellite.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import {ICManagementCanister} from '@dfinity/ic-management';
import {Principal} from '@dfinity/principal';
import {isNullish} from '@dfinity/utils';
import {
listRules,
listSatelliteControllers,
setRule,
setSatelliteControllers,
type SatelliteParameters
} from '@junobuild/admin';
import type {Controller} from '@junobuild/admin/dist/declarations/satellite/satellite.did';
import type {
Rule,
RulesType,
SatelliteDevController,
SatelliteDevDataStoreCollection,
SatelliteDevStorageCollection
} from '@junobuild/config';
import fetch from 'node-fetch';
import {readJunoDevConfig} from '../../configs/juno.dev.config';
import {MAIN_IDENTITY_KEY} from '../../constants/constants';
import type {CliContext} from '../../types/context';
import type {ModuleMetadata} from '../../types/module';
const list = async ({type, satellite}): Promise<Rule[]> => {
return await listRules({
type,
satellite
});
};
const configRules = async ({
type,
collections,
satellite
}: {
type: RulesType;
collections: Array<SatelliteDevDataStoreCollection | SatelliteDevStorageCollection>;
satellite: SatelliteParameters;
}) => {
const existingRules = await list({type, satellite});
await Promise.all(
collections.map(async ({collection, memory, ...rest}) => {
await setRule({
type,
satellite,
rule: {
...(existingRules.find(
({collection: existingCollection}) => existingCollection === collection
) ?? {}),
collection,
memory: memory.toLowerCase() === 'stable' ? 'stable' : 'heap',
...rest
}
});
})
);
};
const buildSatelliteParams = ({
canisterId,
identities: {[MAIN_IDENTITY_KEY]: identity}
}: SatelliteConfigContext): SatelliteParameters => ({
satelliteId: canisterId,
// TODO: TypeScript incompatibility window.fetch vs nodejs.fetch vs agent-ts using typeof fetch
// @ts-expect-error
fetch,
identity,
container: true
});
export type SatelliteConfigContext = CliContext & Pick<ModuleMetadata, 'canisterId'>;
export const configureCollections = async (context: SatelliteConfigContext) => {
const {
satellite: {
collections: {datastore, db, storage}
}
} = await readJunoDevConfig();
const satellite = buildSatelliteParams(context);
await Promise.all([
configRules({type: 'db', collections: datastore ?? db ?? [], satellite}),
configRules({type: 'storage', collections: storage ?? [], satellite})
]);
};
export const configureControllers = async (context: SatelliteConfigContext) => {
const {
satellite: {controllers}
} = await readJunoDevConfig();
if ((controllers ?? []).length === 0) {
return;
}
const satellite = buildSatelliteParams(context);
type ControllerId = string;
const existingControllers: Record<ControllerId, Controller> = (
await listSatelliteControllers({satellite})
).reduce(
(acc, [controller, details]) => ({
...acc,
[controller.toText()]: details
}),
{}
);
const newControllers = (controllers ?? []).filter(({id}) => isNullish(existingControllers[id]));
// If no new controllers need to be applied, we can return.
if (newControllers.length === 0) {
return;
}
const [write, admin] = newControllers.reduce(
([write, admin]: [SatelliteDevController[], SatelliteDevController[]], controller) => [
[...write, ...(controller.scope === 'write' ? [controller] : [])],
[...admin, ...(controller.scope === 'admin' ? [controller] : [])]
],
[[], []]
);
const {agent, canisterId} = context;
// We do not have mission control in this context, therefore we need to set the admin controllers ourselves.
if (admin.length > 0) {
const {updateSettings} = ICManagementCanister.create({
agent
});
await updateSettings({
canisterId: Principal.from(canisterId),
settings: {
controllers: [...Object.keys(existingControllers), ...admin.map(({id}) => id)]
}
});
}
const setControllers = async ({
controllerScope,
controllers
}: {
controllerScope: 'Write' | 'Admin';
controllers: SatelliteDevController[];
}) => {
await setSatelliteControllers({
args: {
controller: {
metadata: [],
scope: controllerScope === 'Write' ? {Write: null} : {Admin: null},
expires_at: []
},
controllers: controllers.map(({id}) => Principal.from(id))
},
satellite
});
};
// Finally we can set the controllers within the satellite heap memory
await Promise.all([
...[write.length > 0 ? [setControllers({controllerScope: 'Write', controllers: write})] : []],
...[admin.length > 0 ? [setControllers({controllerScope: 'Admin', controllers: admin})] : []]
]);
};