@neon-rs/manifest 0.0.2 → 0.0.4

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.
@@ -0,0 +1,308 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.BinaryManifest = exports.NPMCacheCfg = void 0;
30
+ const fs = __importStar(require("node:fs/promises"));
31
+ const path = __importStar(require("node:path"));
32
+ const change_cjs_1 = require("../change.cjs");
33
+ const jscodeshift_1 = __importDefault(require("jscodeshift"));
34
+ const platform_cjs_1 = require("../platform.cjs");
35
+ const util_cjs_1 = require("../util.cjs");
36
+ const neon_cjs_1 = require("../neon.cjs");
37
+ const abstract_cjs_1 = require("../abstract.cjs");
38
+ const PLATFORMS_DIR = 'platforms';
39
+ class NPMCacheCfg {
40
+ constructor(manifest, outDir = PLATFORMS_DIR) {
41
+ this.type = 'npm';
42
+ this.manifest = manifest;
43
+ this.outDir = outDir;
44
+ }
45
+ // packageFor(target: RustTarget): string | undefined {
46
+ // const cfg = this.manifest.cfg();
47
+ // const platforms = this.manifest.allPlatforms();
48
+ // for (const key in platforms) {
49
+ // const value = platforms[key as NodePlatform];
50
+ // if (value === target) {
51
+ // return `${cfg.org}/${key}`;
52
+ // }
53
+ // }
54
+ // return undefined;
55
+ // }
56
+ manifestFor(node, rust) {
57
+ const targetInfo = (0, platform_cjs_1.describeTarget)(rust);
58
+ const org = this.manifest.cfg().org;
59
+ const name = `${org}/${node}`;
60
+ const json = {
61
+ name,
62
+ description: `Prebuilt binary package for \`${this.manifest.name}\` on \`${targetInfo.node}\`.`,
63
+ version: this.manifest.version,
64
+ os: [targetInfo.os],
65
+ cpu: [targetInfo.arch],
66
+ main: "index.node",
67
+ files: ["index.node"],
68
+ neon: {
69
+ type: "binary",
70
+ rust: rust,
71
+ node: targetInfo.node,
72
+ os: targetInfo.os,
73
+ arch: targetInfo.arch,
74
+ abi: targetInfo.abi
75
+ }
76
+ };
77
+ this.manifest.copyOptionalKeys(json);
78
+ return new BinaryManifest(json);
79
+ }
80
+ async makeBinaryPackage(node, rust, log) {
81
+ const binaryManifest = this.manifestFor(node, rust);
82
+ log(`prebuild manifest: ${binaryManifest.stringify()}`);
83
+ const treeDir = path.join(this.outDir, node);
84
+ log(`creating ${treeDir}`);
85
+ await fs.mkdir(treeDir, { recursive: true });
86
+ log(`created ${treeDir}`);
87
+ log(`creating ${treeDir}/package.json`);
88
+ await binaryManifest.save(treeDir, log);
89
+ log(`creating ${treeDir}/README.md`);
90
+ await fs.writeFile(path.join(treeDir, "README.md"), `# \`${binaryManifest.name}\`\n\n${binaryManifest.description}\n`);
91
+ }
92
+ async updateBinaryManifest(node, rust, log) {
93
+ const targetInfo = (0, platform_cjs_1.describeTarget)(rust);
94
+ const dir = path.join(this.outDir, node);
95
+ const binaryManifest = await BinaryManifest.load(dir);
96
+ binaryManifest.setTarget(rust, targetInfo);
97
+ await binaryManifest.save(dir, log);
98
+ }
99
+ async saveChanges(dir, changes, log) {
100
+ const newPlatforms = [];
101
+ for (const change of changes) {
102
+ switch (change.kind) {
103
+ case change_cjs_1.EventKind.PLATFORM_ADDED: {
104
+ await this.makeBinaryPackage(change.event.node, change.event.rust, log);
105
+ break;
106
+ }
107
+ case change_cjs_1.EventKind.PLATFORM_MODIFIED: {
108
+ await this.updateBinaryManifest(change.event.node, change.event.rust, log);
109
+ break;
110
+ }
111
+ default:
112
+ continue;
113
+ }
114
+ }
115
+ if (newPlatforms.length) {
116
+ await this.updateLoader(newPlatforms);
117
+ }
118
+ }
119
+ async updateLoader(platforms) {
120
+ const cfg = this.manifest.cfg();
121
+ if (!cfg.load) {
122
+ return;
123
+ }
124
+ const loader = await fs.readFile(cfg.load, 'utf8');
125
+ function isPlatformTable(p) {
126
+ return p.value.properties.every(p => {
127
+ return p.type === 'Property' &&
128
+ p.key.type === 'Literal' &&
129
+ (0, platform_cjs_1.isNodePlatform)(p.key.value);
130
+ });
131
+ }
132
+ const result = (0, jscodeshift_1.default)(loader)
133
+ .find(jscodeshift_1.default.ObjectExpression)
134
+ .filter(isPlatformTable)
135
+ .replaceWith((p) => {
136
+ const newProps = platforms.map(platform => {
137
+ return jscodeshift_1.default.property('init', jscodeshift_1.default.literal(platform), jscodeshift_1.default.arrowFunctionExpression([], jscodeshift_1.default.callExpression(jscodeshift_1.default.identifier('require'), [jscodeshift_1.default.literal(`${cfg.org}/${platform}`)])));
138
+ });
139
+ return jscodeshift_1.default.objectExpression([...p.value.properties, ...newProps]);
140
+ })
141
+ .toSource({ quote: 'single' });
142
+ await fs.writeFile(cfg.load, result, 'utf8');
143
+ }
144
+ }
145
+ exports.NPMCacheCfg = NPMCacheCfg;
146
+ function assertIsBinaryCfg(json) {
147
+ (0, util_cjs_1.assertHasProps)(['type', 'rust', 'node', 'os', 'arch', 'abi'], json, "neon");
148
+ if (json.type !== 'binary') {
149
+ throw new TypeError(`expected "neon.type" property to be "binary", found ${json.type}`);
150
+ }
151
+ if (typeof json.rust !== 'string' || !(0, platform_cjs_1.isRustTarget)(json.rust)) {
152
+ throw new TypeError(`expected "neon.rust" to be a valid Rust target, found ${json.rust}`);
153
+ }
154
+ if (typeof json.node !== 'string' || !(0, platform_cjs_1.isNodePlatform)(json.node)) {
155
+ throw new TypeError(`expected "neon.node" to be a valid Node target, found ${json.node}`);
156
+ }
157
+ if (typeof json.os !== 'string') {
158
+ throw new TypeError(`expected "neon.os" to be a string, found ${json.os}`);
159
+ }
160
+ if (typeof json.arch !== 'string') {
161
+ throw new TypeError(`expected "neon.arch" to be a string, found ${json.arch}`);
162
+ }
163
+ if (json.abi !== null && typeof json.abi !== 'string') {
164
+ throw new TypeError(`expected "neon.abi" to be a string or null, found ${json.abi}`);
165
+ }
166
+ }
167
+ function assertIsBinaryV2(json) {
168
+ if (!json || typeof json !== 'object') {
169
+ throw new TypeError(`expected "neon" to be an object, found ${json}`);
170
+ }
171
+ (0, util_cjs_1.assertHasProps)(['rust', 'node', 'platform', 'arch', 'abi'], json, "neon");
172
+ if (!(0, platform_cjs_1.isRustTarget)(json.rust)) {
173
+ throw new TypeError(`expected "neon.rust" to be a valid Rust target, found ${json.rust}`);
174
+ }
175
+ if (!(0, platform_cjs_1.isNodePlatform)(json.node)) {
176
+ throw new TypeError(`expected "neon.node" to be a valid Node platform, found ${json.node}`);
177
+ }
178
+ if (typeof json.platform !== 'string') {
179
+ throw new TypeError(`expected "neon.platform" to be a string, found ${json.platform}`);
180
+ }
181
+ if (typeof json.arch !== 'string') {
182
+ throw new TypeError(`expected "neon.arch" to be a string, found ${json.arch}`);
183
+ }
184
+ if (json.abi !== null && typeof json.abi !== 'string') {
185
+ throw new TypeError(`expected "neon.abi" to be a string or null, found ${json.abi}`);
186
+ }
187
+ }
188
+ function assertIsBinaryV1(json) {
189
+ (0, util_cjs_1.assertHasProps)(['binary'], json, "neon");
190
+ const binary = json.binary;
191
+ if (!binary || typeof binary !== 'object') {
192
+ throw new TypeError(`expected "neon.binary" to be an object, found ${binary}`);
193
+ }
194
+ (0, util_cjs_1.assertHasProps)(['rust', 'node', 'platform', 'arch', 'abi'], binary, "neon.binary");
195
+ if (typeof binary.rust !== 'string' || !(0, platform_cjs_1.isRustTarget)(binary.rust)) {
196
+ throw new TypeError(`expected "neon.binary.rust" to be a valid Rust target, found ${binary.rust}`);
197
+ }
198
+ if (!(0, platform_cjs_1.isNodePlatform)(binary.node)) {
199
+ throw new TypeError(`expected "neon.binary.node" to be a valid Node platform, found ${binary.node}`);
200
+ }
201
+ if (typeof binary.platform !== 'string') {
202
+ throw new TypeError(`expected "neon.binary.platform" to be a string, found ${binary.platform}`);
203
+ }
204
+ if (typeof binary.arch !== 'string') {
205
+ throw new TypeError(`expected "neon.binary.arch" to be a string, found ${binary.arch}`);
206
+ }
207
+ if (binary.abi !== null && typeof binary.abi !== 'string') {
208
+ throw new TypeError(`expected "neon.binary.abi" to be a string or null, found ${binary.abi}`);
209
+ }
210
+ }
211
+ function assertHasBinaryCfg(json) {
212
+ (0, neon_cjs_1.assertHasNeonCfg)(json);
213
+ assertIsBinaryCfg(json.neon);
214
+ }
215
+ class BinaryManifest extends abstract_cjs_1.AbstractManifest {
216
+ constructor(json) {
217
+ super(json);
218
+ if (normalizeBinaryCfg(this._json)) {
219
+ this._changes.push({
220
+ kind: change_cjs_1.EventKind.SCHEMA_UPGRADED,
221
+ event: new change_cjs_1.SchemaUpgradedEvent('3')
222
+ });
223
+ }
224
+ assertHasBinaryCfg(this._json);
225
+ this._binaryJSON = this._json;
226
+ }
227
+ cfg() {
228
+ return this._binaryJSON.neon;
229
+ }
230
+ setTarget(target, targetInfo) {
231
+ const json = this._json;
232
+ json.os = targetInfo.os;
233
+ json.cpu = targetInfo.arch;
234
+ this._binaryJSON.neon.rust = target;
235
+ this._binaryJSON.neon.os = targetInfo.os;
236
+ this._binaryJSON.neon.arch = targetInfo.arch;
237
+ this._binaryJSON.neon.abi = targetInfo.abi;
238
+ }
239
+ static async load(dir) {
240
+ return new BinaryManifest(await (0, abstract_cjs_1.readManifest)(dir));
241
+ }
242
+ }
243
+ exports.BinaryManifest = BinaryManifest;
244
+ function normalizeBinaryCfg(json) {
245
+ (0, neon_cjs_1.assertHasNeonCfg)(json);
246
+ // V3 format: {
247
+ // neon: {
248
+ // type: 'binary',
249
+ // rust: RustTarget,
250
+ // node: NodeTarget,
251
+ // os: string,
252
+ // arch: string,
253
+ // abi: string | null
254
+ // }
255
+ // }
256
+ if ('type' in json.neon && 'os' in json.neon) {
257
+ return false;
258
+ }
259
+ // V2 format: {
260
+ // neon: {
261
+ // type: 'binary',
262
+ // rust: RustTarget,
263
+ // node: NodeTarget,
264
+ // platform: string,
265
+ // arch: string,
266
+ // abi: string | null
267
+ // }
268
+ // }
269
+ if ('type' in json.neon) {
270
+ json.neon = upgradeBinaryV2(json.neon);
271
+ return true;
272
+ }
273
+ // V1 format: {
274
+ // neon: {
275
+ // binary: {
276
+ // rust: RustTarget,
277
+ // node: NodeTarget,
278
+ // platform: string,
279
+ // arch: string,
280
+ // abi: string | null
281
+ // }
282
+ // }
283
+ // }
284
+ json.neon = upgradeBinaryV1(json.neon);
285
+ return true;
286
+ }
287
+ function upgradeBinaryV1(json) {
288
+ assertIsBinaryV1(json);
289
+ return {
290
+ type: 'binary',
291
+ rust: json.binary.rust,
292
+ node: json.binary.node,
293
+ os: json.binary.platform,
294
+ arch: json.binary.arch,
295
+ abi: json.binary.abi
296
+ };
297
+ }
298
+ function upgradeBinaryV2(json) {
299
+ assertIsBinaryV2(json);
300
+ return {
301
+ type: 'binary',
302
+ rust: json.rust,
303
+ node: json.node,
304
+ os: json.platform,
305
+ arch: json.arch,
306
+ abi: json.abi
307
+ };
308
+ }
@@ -0,0 +1,31 @@
1
+ import { CacheCfg } from '../cache.cjs';
2
+ import { Change } from '../change.cjs';
3
+ import { LibraryManifest } from '../index.cjs';
4
+ import { RustTarget, NodePlatform, PlatformDescriptor } from '../platform.cjs';
5
+ import { AbstractManifest } from '../abstract.cjs';
6
+ export declare class NPMCacheCfg implements CacheCfg {
7
+ readonly type: string;
8
+ private manifest;
9
+ private outDir;
10
+ constructor(manifest: LibraryManifest, outDir?: string);
11
+ manifestFor(node: NodePlatform, rust: RustTarget): BinaryManifest;
12
+ makeBinaryPackage(node: NodePlatform, rust: RustTarget, log: (msg: string) => void): Promise<void>;
13
+ updateBinaryManifest(node: NodePlatform, rust: RustTarget, log: (msg: string) => void): Promise<void>;
14
+ saveChanges(dir: string, changes: Change[], log: (msg: string) => void): Promise<void>;
15
+ updateLoader(platforms: NodePlatform[]): Promise<void>;
16
+ }
17
+ export interface BinaryCfg {
18
+ type: "binary";
19
+ rust: RustTarget;
20
+ node: NodePlatform;
21
+ os: string;
22
+ arch: string;
23
+ abi: string | null;
24
+ }
25
+ export declare class BinaryManifest extends AbstractManifest {
26
+ private _binaryJSON;
27
+ constructor(json: unknown);
28
+ cfg(): BinaryCfg;
29
+ setTarget(target: RustTarget, targetInfo: PlatformDescriptor): void;
30
+ static load(dir?: string | undefined): Promise<BinaryManifest>;
31
+ }
package/lib/cache.cjs ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,5 @@
1
+ import { Change } from './change.cjs';
2
+ export interface CacheCfg {
3
+ readonly type: string;
4
+ saveChanges(dir: string, changes: Change[], log: (msg: string) => void): Promise<void>;
5
+ }
package/lib/change.cjs ADDED
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ManifestChangedEvent = exports.PlatformModifiedEvent = exports.PlatformAddedEvent = exports.SchemaUpgradedEvent = exports.EventKind = void 0;
4
+ var EventKind;
5
+ (function (EventKind) {
6
+ EventKind[EventKind["SCHEMA_UPGRADED"] = 0] = "SCHEMA_UPGRADED";
7
+ EventKind[EventKind["PLATFORM_ADDED"] = 1] = "PLATFORM_ADDED";
8
+ EventKind[EventKind["PLATFORM_MODIFIED"] = 2] = "PLATFORM_MODIFIED";
9
+ EventKind[EventKind["MANIFEST_CHANGED"] = 3] = "MANIFEST_CHANGED";
10
+ })(EventKind || (exports.EventKind = EventKind = {}));
11
+ class SchemaUpgradedEvent {
12
+ constructor(schemaVersion) {
13
+ this.schemaVersion = schemaVersion;
14
+ }
15
+ description() {
16
+ return `Neon manifest schema v${this.schemaVersion} upgrade`;
17
+ }
18
+ }
19
+ exports.SchemaUpgradedEvent = SchemaUpgradedEvent;
20
+ class PlatformAddedEvent {
21
+ constructor(node, rust) {
22
+ this.node = node;
23
+ this.rust = rust;
24
+ }
25
+ description() {
26
+ return `added ${this.node} platform`;
27
+ }
28
+ }
29
+ exports.PlatformAddedEvent = PlatformAddedEvent;
30
+ class PlatformModifiedEvent {
31
+ constructor(node, rust) {
32
+ this.node = node;
33
+ this.rust = rust;
34
+ }
35
+ description() {
36
+ return `modified ${this.node} platform to ${this.rust} target`;
37
+ }
38
+ }
39
+ exports.PlatformModifiedEvent = PlatformModifiedEvent;
40
+ class ManifestChangedEvent {
41
+ constructor(version) {
42
+ this.version = version;
43
+ }
44
+ description() {
45
+ return `update platforms to v${this.version}`;
46
+ }
47
+ }
48
+ exports.ManifestChangedEvent = ManifestChangedEvent;
@@ -0,0 +1,49 @@
1
+ import { NodePlatform, RustTarget } from "./platform.cjs";
2
+ export declare enum EventKind {
3
+ SCHEMA_UPGRADED = 0,
4
+ PLATFORM_ADDED = 1,
5
+ PLATFORM_MODIFIED = 2,
6
+ MANIFEST_CHANGED = 3
7
+ }
8
+ export type SchemaUpgraded = {
9
+ kind: EventKind.SCHEMA_UPGRADED;
10
+ event: SchemaUpgradedEvent;
11
+ };
12
+ export type PlatformAdded = {
13
+ kind: EventKind.PLATFORM_ADDED;
14
+ event: PlatformAddedEvent;
15
+ };
16
+ export type PlatformModified = {
17
+ kind: EventKind.PLATFORM_MODIFIED;
18
+ event: PlatformModifiedEvent;
19
+ };
20
+ export type ManifestChanged = {
21
+ kind: EventKind.MANIFEST_CHANGED;
22
+ event: ManifestChangedEvent;
23
+ };
24
+ export type Change = SchemaUpgraded | PlatformAdded | PlatformModified | ManifestChanged;
25
+ export interface Event {
26
+ description(): string;
27
+ }
28
+ export declare class SchemaUpgradedEvent implements Event {
29
+ private schemaVersion;
30
+ constructor(schemaVersion: string);
31
+ description(): string;
32
+ }
33
+ export declare class PlatformAddedEvent implements Event {
34
+ readonly node: NodePlatform;
35
+ readonly rust: RustTarget;
36
+ constructor(node: NodePlatform, rust: RustTarget);
37
+ description(): string;
38
+ }
39
+ export declare class PlatformModifiedEvent implements Event {
40
+ readonly node: NodePlatform;
41
+ readonly rust: RustTarget;
42
+ constructor(node: NodePlatform, rust: RustTarget);
43
+ description(): string;
44
+ }
45
+ export declare class ManifestChangedEvent implements Event {
46
+ private version;
47
+ constructor(version: string);
48
+ description(): string;
49
+ }