@eik/common 4.0.8 → 4.1.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/CHANGELOG.md +14 -0
- package/README.md +109 -137
- package/eikjson.d.ts +2 -2
- package/lib/helpers/type-slug.js +5 -3
- package/lib/helpers/type-title.js +4 -3
- package/lib/schemas/assert.js +24 -3
- package/lib/schemas/eikjson.schema.json +2 -2
- package/lib/schemas/validate.js +71 -5
- package/lib/stream.js +14 -2
- package/lib/validators/index.js +56 -10
- package/package.json +1 -1
- package/types/classes/eik-config.d.ts +1 -1
- package/types/helpers/type-slug.d.ts +1 -1
- package/types/helpers/type-title.d.ts +1 -1
- package/types/schemas/assert.d.ts +9 -8
- package/types/schemas/validate.d.ts +45 -24
- package/types/stream.d.ts +2 -2
- package/types/validators/index.d.ts +8 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
# [4.1.0](https://github.com/eik-lib/common/compare/v4.0.9...v4.1.0) (2024-08-14)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* add support for Eik image type ([75a5830](https://github.com/eik-lib/common/commit/75a5830df7bf59f7ef627b8d143cda5c0bbbba1b))
|
|
7
|
+
|
|
8
|
+
## [4.0.9](https://github.com/eik-lib/common/compare/v4.0.8...v4.0.9) (2024-08-13)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* add jsdoc for schema validators ([#325](https://github.com/eik-lib/common/issues/325)) ([062446a](https://github.com/eik-lib/common/commit/062446a1fa91cf0f1a63078b087a3670815b134c))
|
|
14
|
+
|
|
1
15
|
## [4.0.8](https://github.com/eik-lib/common/compare/v4.0.7...v4.0.8) (2024-08-12)
|
|
2
16
|
|
|
3
17
|
|
package/README.md
CHANGED
|
@@ -1,149 +1,72 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @eik/common
|
|
2
2
|
|
|
3
|
-
This package contains common utilities and schemas
|
|
3
|
+
This package contains common utilities and schemas used in other Eik modules.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Schema
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
The [schema for `eik.json`](https://eik.dev/docs/reference/eik-json#json-schema)
|
|
8
|
+
can be found here in this repo. Here is how you can use it in your `eik.json`.
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
import { schemas, assert } from '@eik/common';
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
Validating an `eik.json` file
|
|
22
|
-
|
|
23
|
-
```js
|
|
24
|
-
const { error, value } = schemas.validate.eikJSON({
|
|
25
|
-
name: 'my-app',
|
|
26
|
-
version: '1.0.0',
|
|
27
|
-
server: 'http://eik-server',
|
|
28
|
-
files: [],
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
//or
|
|
32
|
-
|
|
33
|
-
assert.eikJSON({
|
|
34
|
-
name: 'my-app',
|
|
35
|
-
version: '1.0.0',
|
|
36
|
-
server: 'http://eik-server',
|
|
37
|
-
files: [],
|
|
38
|
-
});
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
Using individual schema validators
|
|
42
|
-
|
|
43
|
-
##### name
|
|
44
|
-
|
|
45
|
-
```js
|
|
46
|
-
const { error, value } = schemas.validate.name('my-app');
|
|
47
|
-
|
|
48
|
-
// or
|
|
49
|
-
|
|
50
|
-
assert.name('my-app');
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
##### version
|
|
54
|
-
|
|
55
|
-
```js
|
|
56
|
-
const { error, value } = schemas.validate.version('1.0.0');
|
|
57
|
-
|
|
58
|
-
// or
|
|
59
|
-
|
|
60
|
-
assert.version('1.0.0');
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
##### type
|
|
64
|
-
|
|
65
|
-
```js
|
|
66
|
-
const { error, value } = schemas.validate.type('package');
|
|
67
|
-
|
|
68
|
-
// or
|
|
69
|
-
|
|
70
|
-
assert.type('package');
|
|
10
|
+
```json
|
|
11
|
+
{
|
|
12
|
+
"$schema": "https://raw.githubusercontent.com/eik-lib/common/main/lib/schemas/eikjson.schema.json",
|
|
13
|
+
"name": "my-app",
|
|
14
|
+
"version": "1.0.0",
|
|
15
|
+
"server": "https://eik.store.com",
|
|
16
|
+
"files": "./public",
|
|
17
|
+
"import-map": ["https://eik.store.com/map/store/v1"]
|
|
18
|
+
}
|
|
71
19
|
```
|
|
72
20
|
|
|
73
|
-
|
|
21
|
+
`@eik/common` has a JavaScript API to [check against the schema](#schemas).
|
|
74
22
|
|
|
75
|
-
|
|
76
|
-
const { error, value } = schemas.validate.server('http://myeikserver.com');
|
|
77
|
-
|
|
78
|
-
// or
|
|
23
|
+
## API
|
|
79
24
|
|
|
80
|
-
|
|
81
|
-
```
|
|
25
|
+
### helpers
|
|
82
26
|
|
|
83
|
-
|
|
27
|
+
`helpers` has utility functions used by several other Eik modules.
|
|
84
28
|
|
|
85
29
|
```js
|
|
86
|
-
|
|
87
|
-
'./index.js': '/path/to/file.js',
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
// or
|
|
30
|
+
import { helpers } from '@eik/common';
|
|
91
31
|
|
|
92
|
-
|
|
93
|
-
'./index.js': '/path/to/file.js',
|
|
94
|
-
});
|
|
32
|
+
let config = helpers.getDefaults();
|
|
95
33
|
```
|
|
96
34
|
|
|
97
|
-
|
|
35
|
+
These are the available functions on `helpers`.
|
|
36
|
+
|
|
37
|
+
| Name | Description |
|
|
38
|
+
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
39
|
+
| `getDefaults` | Reads configuration from `eik.json` or `package.json`. Includes defaults for missing optional settings. |
|
|
40
|
+
| `localAssets` | Sets up asset routes for local development. Mounted paths match those on Eik server and values are read from projects eik.json file. |
|
|
41
|
+
| `typeSlug` | Maps between Eik configuration values for the package type and its URL/file system value. |
|
|
42
|
+
| `typeTitle` | Maps between a type config value and its title. Essentially uppercases the input. |
|
|
43
|
+
| `addTrailingSlash` | |
|
|
44
|
+
| `removeTrailingSlash` | |
|
|
45
|
+
| `addLeadingSlash` | |
|
|
46
|
+
| `removeLeadingSlash` | |
|
|
47
|
+
| `resolveFiles` | Uses an Eik JSON "files" definition to resolve files on disk into a data structure. Returns a list of [ResolvedFile](https://github.com/eik-lib/common/blob/main/lib/classes/resolved-files.js). |
|
|
48
|
+
| `configStore` | Collection of helper methods for reading and writing Eik configuration files. |
|
|
98
49
|
|
|
99
|
-
|
|
100
|
-
const { error, value } = schemas.validate.importMap(
|
|
101
|
-
'http://meserver.com/map.json',
|
|
102
|
-
);
|
|
103
|
-
|
|
104
|
-
const { error, value } = schemas.validate.importMap([
|
|
105
|
-
'http://meserver.com/map1.json',
|
|
106
|
-
'http://meserver.com/map2.json',
|
|
107
|
-
]);
|
|
108
|
-
|
|
109
|
-
// or
|
|
50
|
+
#### localAssets
|
|
110
51
|
|
|
111
|
-
|
|
112
|
-
'http://meserver.com/map1.json',
|
|
113
|
-
'http://meserver.com/map2.json',
|
|
114
|
-
]);
|
|
115
|
-
```
|
|
52
|
+
Sets up asset routes for local development. Mounted paths match those on Eik server and values are read from projects eik.json file.
|
|
116
53
|
|
|
117
|
-
|
|
54
|
+
Given this server and `eik.json`, the following routes would be added to your app.
|
|
118
55
|
|
|
119
56
|
```js
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
// or
|
|
123
|
-
|
|
124
|
-
assert.out('./.eik');
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
### helpers
|
|
128
|
-
|
|
129
|
-
#### localAssets
|
|
57
|
+
import { helpers } from '@eik/common';
|
|
58
|
+
import express from 'express';
|
|
130
59
|
|
|
131
|
-
|
|
60
|
+
let app = express();
|
|
132
61
|
|
|
133
|
-
```js
|
|
134
|
-
import express from 'express';
|
|
135
|
-
import { helpers } from '@eik/common';
|
|
136
|
-
const app = express();
|
|
137
62
|
await helpers.localAssets(app);
|
|
138
63
|
```
|
|
139
64
|
|
|
140
|
-
For an `eik.json` file such as
|
|
141
|
-
|
|
142
65
|
```json
|
|
143
66
|
{
|
|
144
67
|
"name": "my-app",
|
|
145
68
|
"version": "1.0.0",
|
|
146
|
-
"server": "https://
|
|
69
|
+
"server": "https://eik.store.com",
|
|
147
70
|
"files": {
|
|
148
71
|
"esm.js": "./assets/esm.js",
|
|
149
72
|
"esm.css": "./assets/esm.css",
|
|
@@ -152,8 +75,6 @@ For an `eik.json` file such as
|
|
|
152
75
|
}
|
|
153
76
|
```
|
|
154
77
|
|
|
155
|
-
A number of routes would be mounted into your app.
|
|
156
|
-
|
|
157
78
|
```
|
|
158
79
|
/pkg/my-app/1.0.0/esm.js
|
|
159
80
|
/pkg/my-app/1.0.0/esm.css
|
|
@@ -161,30 +82,81 @@ A number of routes would be mounted into your app.
|
|
|
161
82
|
/pkg/my-app/1.0.0/esm.css.map
|
|
162
83
|
```
|
|
163
84
|
|
|
164
|
-
|
|
85
|
+
### schemas
|
|
165
86
|
|
|
166
|
-
|
|
87
|
+
`schemas` has functions to check values against the [`eik.json` schema](#schema).
|
|
88
|
+
You can check a value against the schema for `eik.json` as a whole, or for individual
|
|
89
|
+
values in the schema.
|
|
167
90
|
|
|
168
|
-
|
|
91
|
+
```js
|
|
92
|
+
import { schemas } from '@eik/common';
|
|
169
93
|
|
|
170
|
-
|
|
171
|
-
{
|
|
172
|
-
|
|
173
|
-
"version": "1.0.0",
|
|
174
|
-
"server": "https://assets.myeikserver.com",
|
|
175
|
-
"files": {
|
|
176
|
-
"esm.js": "./assets/esm.js",
|
|
177
|
-
"esm.css": "./assets/esm.css",
|
|
178
|
-
"/": "./assets/**/*.map"
|
|
179
|
-
}
|
|
94
|
+
let { error, value } = schemas.validate.eikJSON(eikConfig);
|
|
95
|
+
if (error) {
|
|
96
|
+
// fallback
|
|
180
97
|
}
|
|
181
98
|
```
|
|
182
99
|
|
|
183
|
-
|
|
100
|
+
If you prefer, you can use the `assert` API which throws on error.
|
|
184
101
|
|
|
185
102
|
```js
|
|
186
|
-
import {
|
|
187
|
-
|
|
103
|
+
import { schemas } from '@eik/common';
|
|
104
|
+
|
|
105
|
+
try {
|
|
106
|
+
schemas.assert.eikJSON(eikConfig);
|
|
107
|
+
} catch {
|
|
108
|
+
// fallback
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
These are the available functions on `schemas.validate` and `schemas.assert`.
|
|
113
|
+
|
|
114
|
+
| Name | Description |
|
|
115
|
+
| ----------- | ----------------------------------------------------------------------- |
|
|
116
|
+
| `eikJSON` | Checks that the given value includes required fields that are valid |
|
|
117
|
+
| `name` | Checks [name](https://eik.dev/docs/reference/eik-json#name) |
|
|
118
|
+
| `version` | Checks [version](https://eik.dev/docs/reference/eik-json#version) |
|
|
119
|
+
| `type` | Checks [type](https://eik.dev/docs/reference/eik-json#type) |
|
|
120
|
+
| `server` | Checks [server](https://eik.dev/docs/reference/eik-json#server) |
|
|
121
|
+
| `files` | Checks [files](https://eik.dev/docs/reference/eik-json#files) |
|
|
122
|
+
| `importMap` | Checks [import-map](https://eik.dev/docs/reference/eik-json#import-map) |
|
|
123
|
+
| `out` | Checks [out](https://eik.dev/docs/reference/eik-json#out) |
|
|
124
|
+
|
|
125
|
+
### stream
|
|
126
|
+
|
|
127
|
+
`stream` has functions to check that a value is a Stream.
|
|
128
|
+
|
|
129
|
+
```js
|
|
130
|
+
import { stream } from '@eik/common';
|
|
131
|
+
|
|
132
|
+
if (stream.isStream(maybeStream)) {
|
|
133
|
+
// yup, it's a Stream
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (stream.isReadableStream(maybeReadableStream)) {
|
|
137
|
+
// yup, it's a ReadableStream
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### validators
|
|
142
|
+
|
|
143
|
+
`validators` functions return the provided string normalized to lowercase, or throw an Error if the value does not pass the validation rules.
|
|
144
|
+
Where possible, prefer using the [`schemas` API](#schemas).
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
import { validators } from '@eik/common';
|
|
148
|
+
|
|
149
|
+
let alias = validators.alias('1');
|
|
188
150
|
```
|
|
189
151
|
|
|
190
|
-
|
|
152
|
+
These are the available functions on `validators`.
|
|
153
|
+
|
|
154
|
+
| Name | Description |
|
|
155
|
+
| ------------ | ---------------------------------------------------------------- |
|
|
156
|
+
| `alias` | Checks that a value is a valid alias value (ex 1) |
|
|
157
|
+
| `name` | Checks that a value is a valid package name |
|
|
158
|
+
| `org` | Checks that a value is a valid organisation name. |
|
|
159
|
+
| `origin` | Check that a value looks like an HTTP origin. |
|
|
160
|
+
| `version` | Checks that a value is a valid semver version |
|
|
161
|
+
| `semverType` | Checks that a value is a valid semver type (major, minor, patch) |
|
|
162
|
+
| `type` | Checks that the value is a valid Eik type (pkg, npm, map) |
|
package/eikjson.d.ts
CHANGED
|
@@ -20,9 +20,9 @@ export interface EikjsonSchema {
|
|
|
20
20
|
*/
|
|
21
21
|
version: string;
|
|
22
22
|
/**
|
|
23
|
-
* The type of the Eik package. Must be one of 'package', 'npm' or '
|
|
23
|
+
* The type of the Eik package. Must be one of 'package', 'npm', 'map' or 'image'. Setting this value changes the URL publish namespace between '/pkg' (default), '/npm', '/map' and '/img', use 'npm' when publishing NPM packages. Use 'image' when publishing images. Use 'map' when publishing import maps.
|
|
24
24
|
*/
|
|
25
|
-
type?: "package" | "npm" | "map";
|
|
25
|
+
type?: "package" | "npm" | "map" | "image";
|
|
26
26
|
/**
|
|
27
27
|
* File mapping definition for the package. Keys represent files or paths to be created on the Eik Server. Values represent paths to local files to be published.
|
|
28
28
|
*/
|
package/lib/helpers/type-slug.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
// @ts-check
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
|
-
*
|
|
2
|
+
* Maps between Eik configuration values for the package type and its URL/file system value.
|
|
3
|
+
* Essentially `package` -> `pkg`.
|
|
4
|
+
* @param {string} type
|
|
5
|
+
* @returns {string} the Eik package type
|
|
5
6
|
*/
|
|
6
7
|
export default (type) => {
|
|
7
8
|
if (type === 'package') return 'pkg';
|
|
9
|
+
if (type === 'image') return 'img';
|
|
8
10
|
return type;
|
|
9
11
|
};
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
// @ts-check
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
|
-
*
|
|
2
|
+
* Maps between a type config value and its title. Essentially uppercases the input.
|
|
3
|
+
* @param {string} type
|
|
4
|
+
* @param {"PACKAGE" | "NPM" | "MAP"} type
|
|
5
5
|
*/
|
|
6
6
|
export default (type) => {
|
|
7
7
|
if (type === 'package') return 'PACKAGE';
|
|
8
8
|
if (type === 'npm') return 'NPM';
|
|
9
|
+
if (type === 'image') return 'IMAGE';
|
|
9
10
|
return 'MAP';
|
|
10
11
|
};
|
package/lib/schemas/assert.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @ts-check
|
|
2
1
|
import {
|
|
3
2
|
eikJSON,
|
|
4
3
|
name,
|
|
@@ -11,7 +10,20 @@ import {
|
|
|
11
10
|
} from './validate.js';
|
|
12
11
|
import ValidationError from './validation-error.js';
|
|
13
12
|
|
|
14
|
-
|
|
13
|
+
/**
|
|
14
|
+
* @template T
|
|
15
|
+
* @callback SchemaAssert
|
|
16
|
+
* @param {T} value
|
|
17
|
+
* @returns {void}
|
|
18
|
+
* @throws {ValidationError} if validation fails
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @template T
|
|
23
|
+
* @param {import('./validate.js').SchemaValidator<T>} validate
|
|
24
|
+
* @param {string} message
|
|
25
|
+
* @returns {SchemaAssert<T>}
|
|
26
|
+
*/
|
|
15
27
|
const assert = (validate, message) => (value) => {
|
|
16
28
|
const valid = validate(value);
|
|
17
29
|
if (valid.error) {
|
|
@@ -20,22 +32,31 @@ const assert = (validate, message) => (value) => {
|
|
|
20
32
|
.map((err) => {
|
|
21
33
|
let msg = err.message;
|
|
22
34
|
if (err.params && err.params.allowedValues) {
|
|
23
|
-
msg += ` ("${err.params.allowedValues.join('", ')}")`;
|
|
35
|
+
msg += ` ("${err.params.allowedValues.join('", "')}")`;
|
|
24
36
|
}
|
|
25
37
|
return msg;
|
|
26
38
|
})
|
|
27
39
|
.join(',');
|
|
40
|
+
// @ts-expect-error Maybe some toString magic happens here?
|
|
28
41
|
throw new ValidationError(`${message}: ${errorMessage}`, valid.error);
|
|
29
42
|
}
|
|
30
43
|
};
|
|
31
44
|
|
|
32
45
|
export default {
|
|
46
|
+
/** Asserts the given [eik.json](https://eik.dev/docs/reference/eik-json) includes required fields that are valid */
|
|
33
47
|
eikJSON: assert(eikJSON, 'Invalid eik.json schema'),
|
|
48
|
+
/** Asserts the given [name](https://eik.dev/docs/reference/eik-json#name) value is valid*/
|
|
34
49
|
name: assert(name, 'Parameter "name" is not valid'),
|
|
50
|
+
/** Asserts the given [type](https://eik.dev/docs/reference/eik-json#type) value is valid*/
|
|
35
51
|
type: assert(type, 'Parameter "type" is not valid'),
|
|
52
|
+
/** Asserts the given [version](https://eik.dev/docs/reference/eik-json#version) value is valid*/
|
|
36
53
|
version: assert(version, 'Parameter "version" is not valid'),
|
|
54
|
+
/** Asserts the given [server](https://eik.dev/docs/reference/eik-json#server) value is valid*/
|
|
37
55
|
server: assert(server, 'Parameter "server" is not valid'),
|
|
56
|
+
/** Asserts the given [files](https://eik.dev/docs/reference/eik-json#files) value is valid */
|
|
38
57
|
files: assert(files, 'Parameter "files" is not valid'),
|
|
58
|
+
/** Asserts the given [import-map](https://eik.dev/docs/reference/eik-json#import-map) value is valid */
|
|
39
59
|
importMap: assert(importMap, 'Parameter "import-map" is not valid'),
|
|
60
|
+
/** Asserts the given [out](https://eik.dev/docs/reference/eik-json#out) value is valid */
|
|
40
61
|
out: assert(out, 'Parameter "out" is not valid'),
|
|
41
62
|
};
|
|
@@ -24,9 +24,9 @@
|
|
|
24
24
|
"minLength": 1
|
|
25
25
|
},
|
|
26
26
|
"type": {
|
|
27
|
-
"description": "The type of the Eik package. Must be one of 'package', 'npm' or '
|
|
27
|
+
"description": "The type of the Eik package. Must be one of 'package', 'npm', 'map' or 'image'. Setting this value changes the URL publish namespace between '/pkg' (default), '/npm', '/map' and '/img', use 'npm' when publishing NPM packages. Use 'image' when publishing images. Use 'map' when publishing import maps.",
|
|
28
28
|
"type": "string",
|
|
29
|
-
"enum": ["package", "npm", "map"],
|
|
29
|
+
"enum": ["package", "npm", "map", "image"],
|
|
30
30
|
"default": "package"
|
|
31
31
|
},
|
|
32
32
|
"files": {
|
package/lib/schemas/validate.js
CHANGED
|
@@ -13,7 +13,24 @@ const eikJSONSchema = JSON.parse(
|
|
|
13
13
|
readFileSync(join(__dirname, './eikjson.schema.json'), 'utf8'),
|
|
14
14
|
);
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
/**
|
|
17
|
+
* @typedef {import('ajv').ErrorObject & { dataPath?: string }} ErrorObject
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @template [T=unknown]
|
|
22
|
+
* @callback SchemaValidator
|
|
23
|
+
* @param {T} data
|
|
24
|
+
* @returns {{ value: T; error: false | import('ajv').ErrorObject[]}}
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Create a validator for the schema using {@link Ajv}
|
|
29
|
+
* @template [T=unknown]
|
|
30
|
+
* @param {unknown} schema
|
|
31
|
+
* @param {import('ajv').Options} [ajvOptions]
|
|
32
|
+
* @returns {SchemaValidator<T>}
|
|
33
|
+
*/
|
|
17
34
|
const createValidator = (schema, ajvOptions) => {
|
|
18
35
|
// @ts-ignore
|
|
19
36
|
const ajv = new Ajv(ajvOptions);
|
|
@@ -21,7 +38,6 @@ const createValidator = (schema, ajvOptions) => {
|
|
|
21
38
|
formats(ajv); // Needed to support "uri"
|
|
22
39
|
const validate = ajv.compile(schema);
|
|
23
40
|
|
|
24
|
-
// @ts-ignore
|
|
25
41
|
return (data) => {
|
|
26
42
|
const cloned = JSON.parse(JSON.stringify(data));
|
|
27
43
|
const valid = validate(cloned);
|
|
@@ -29,20 +45,30 @@ const createValidator = (schema, ajvOptions) => {
|
|
|
29
45
|
};
|
|
30
46
|
};
|
|
31
47
|
|
|
48
|
+
/**
|
|
49
|
+
* Checks that the given value includes required fields that are valid
|
|
50
|
+
* @type {SchemaValidator<import('../../eikjson.js').EikjsonSchema>}
|
|
51
|
+
*/
|
|
32
52
|
const eikJSON = createValidator(eikJSONSchema, {
|
|
33
53
|
removeAdditional: true,
|
|
34
54
|
useDefaults: true,
|
|
35
55
|
});
|
|
36
|
-
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @param {SchemaValidator<string>} jsonSchemaValidator
|
|
59
|
+
* @returns {SchemaValidator<string>}
|
|
60
|
+
*/
|
|
37
61
|
const createNameValidator = (jsonSchemaValidator) => (value) => {
|
|
38
62
|
const result = jsonSchemaValidator(value);
|
|
39
63
|
if (!result.error) {
|
|
40
64
|
const pkvalid = npmPkg(value);
|
|
65
|
+
/** @type {ErrorObject[]} */
|
|
41
66
|
const errors = [];
|
|
42
67
|
if (!pkvalid.validForNewPackages) {
|
|
43
68
|
errors.push({
|
|
44
69
|
keyword: 'validForNewPackages',
|
|
45
|
-
|
|
70
|
+
instancePath: '.name',
|
|
71
|
+
dataPath: '.name', // this was here before, but maybe replaced by instancePath?
|
|
46
72
|
schemaPath: '',
|
|
47
73
|
params: [],
|
|
48
74
|
message: 'should be valid package name',
|
|
@@ -54,15 +80,21 @@ const createNameValidator = (jsonSchemaValidator) => (value) => {
|
|
|
54
80
|
}
|
|
55
81
|
return result;
|
|
56
82
|
};
|
|
57
|
-
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @param {SchemaValidator<string>} jsonSchemaValidator
|
|
86
|
+
* @returns {SchemaValidator<string>}
|
|
87
|
+
*/
|
|
58
88
|
const createVersionValidator = (jsonSchemaValidator) => (value) => {
|
|
59
89
|
const result = jsonSchemaValidator(value);
|
|
60
90
|
if (!result.error) {
|
|
61
91
|
const version = semver.valid(value);
|
|
92
|
+
/** @type {ErrorObject[]} */
|
|
62
93
|
const errors = [];
|
|
63
94
|
if (!version) {
|
|
64
95
|
errors.push({
|
|
65
96
|
keyword: 'invalidSemverRange',
|
|
97
|
+
instancePath: '.version',
|
|
66
98
|
dataPath: '.version',
|
|
67
99
|
schemaPath: '',
|
|
68
100
|
params: [],
|
|
@@ -76,16 +108,50 @@ const createVersionValidator = (jsonSchemaValidator) => (value) => {
|
|
|
76
108
|
return result;
|
|
77
109
|
};
|
|
78
110
|
|
|
111
|
+
/**
|
|
112
|
+
* Checks [name](https://eik.dev/docs/reference/eik-json#name)
|
|
113
|
+
* @type {SchemaValidator<string>}
|
|
114
|
+
*/
|
|
79
115
|
const name = createNameValidator(
|
|
80
116
|
createValidator(eikJSONSchema.properties.name),
|
|
81
117
|
);
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Checks [version](https://eik.dev/docs/reference/eik-json#version)
|
|
121
|
+
* @type {SchemaValidator<string>}
|
|
122
|
+
*/
|
|
82
123
|
const version = createVersionValidator(
|
|
83
124
|
createValidator(eikJSONSchema.properties.version),
|
|
84
125
|
);
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Checks [type](https://eik.dev/docs/reference/eik-json#type)
|
|
129
|
+
* @type {SchemaValidator<string>}
|
|
130
|
+
*/
|
|
85
131
|
const type = createValidator(eikJSONSchema.properties.type);
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Checks [server](https://eik.dev/docs/reference/eik-json#server)
|
|
135
|
+
* @type {SchemaValidator<string>}
|
|
136
|
+
*/
|
|
86
137
|
const server = createValidator(eikJSONSchema.properties.server);
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Checks [files](https://eik.dev/docs/reference/eik-json#files)
|
|
141
|
+
* @type {SchemaValidator<string | Record<string, string>>}
|
|
142
|
+
*/
|
|
87
143
|
const files = createValidator(eikJSONSchema.properties.files);
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Checks [import-map](https://eik.dev/docs/reference/eik-json#import-map)
|
|
147
|
+
* @type {SchemaValidator<string | string[]>}
|
|
148
|
+
*/
|
|
88
149
|
const importMap = createValidator(eikJSONSchema.properties['import-map']);
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Checks [out](https://eik.dev/docs/reference/eik-json#out)
|
|
153
|
+
* @type {SchemaValidator<string>}
|
|
154
|
+
*/
|
|
89
155
|
const out = createValidator(eikJSONSchema.properties.out);
|
|
90
156
|
|
|
91
157
|
export { eikJSON, name, version, type, server, files, importMap, out };
|
package/lib/stream.js
CHANGED
|
@@ -1,12 +1,24 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the value is a Stream
|
|
3
|
+
* @param {unknown} stream
|
|
4
|
+
* @returns {boolean}
|
|
5
|
+
*/
|
|
2
6
|
export const isStream = (stream) =>
|
|
3
7
|
stream !== null &&
|
|
4
8
|
typeof stream === 'object' &&
|
|
9
|
+
// @ts-expect-error
|
|
5
10
|
typeof stream.pipe === 'function';
|
|
6
11
|
|
|
7
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Checks if the value is a {@link ReadableStream}
|
|
14
|
+
* @param {unknown} stream
|
|
15
|
+
* @returns {boolean}
|
|
16
|
+
*/
|
|
8
17
|
export const isReadableStream = (stream) =>
|
|
9
18
|
isStream(stream) &&
|
|
19
|
+
// @ts-expect-error
|
|
10
20
|
stream.readable !== false &&
|
|
21
|
+
// @ts-expect-error
|
|
11
22
|
typeof stream._read === 'function' &&
|
|
23
|
+
// @ts-expect-error
|
|
12
24
|
typeof stream._readableState === 'object';
|
package/lib/validators/index.js
CHANGED
|
@@ -2,7 +2,13 @@ import semver from 'semver';
|
|
|
2
2
|
import npmPkg from 'validate-npm-package-name';
|
|
3
3
|
|
|
4
4
|
const urlIsh = /^https?:\/\/[a-zA-Z0-9-_./]+(:[0-9]+)?/;
|
|
5
|
-
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Check that a value looks like an HTTP origin.
|
|
8
|
+
* @param {string} value
|
|
9
|
+
* @returns {string} the value in lowercase
|
|
10
|
+
* @throws {Error} if the parameter does not match a URL pattern
|
|
11
|
+
*/
|
|
6
12
|
export const origin = (value) => {
|
|
7
13
|
if (urlIsh.test(value)) {
|
|
8
14
|
return value.toLowerCase();
|
|
@@ -10,7 +16,12 @@ export const origin = (value) => {
|
|
|
10
16
|
throw new Error('Parameter "origin" is not valid');
|
|
11
17
|
};
|
|
12
18
|
|
|
13
|
-
|
|
19
|
+
/**
|
|
20
|
+
* Checks that a value is a valid organisation name.
|
|
21
|
+
* @param {string} value
|
|
22
|
+
* @returns {string} the value in lowercase
|
|
23
|
+
* @throws {Error} if the value is not a valid organisation name
|
|
24
|
+
*/
|
|
14
25
|
export const org = (value) => {
|
|
15
26
|
if (/^[a-zA-Z0-9_-]+$/.test(value)) {
|
|
16
27
|
return value.toLowerCase();
|
|
@@ -18,7 +29,12 @@ export const org = (value) => {
|
|
|
18
29
|
throw new Error(`Parameter "org" is not valid - Value: ${value}`);
|
|
19
30
|
};
|
|
20
31
|
|
|
21
|
-
|
|
32
|
+
/**
|
|
33
|
+
* Checks that a value is a valid package name.
|
|
34
|
+
* @param {string} value
|
|
35
|
+
* @returns {string} the value in lowercase
|
|
36
|
+
* @throws {Error} if the value is not a valid package name
|
|
37
|
+
*/
|
|
22
38
|
export const name = (value) => {
|
|
23
39
|
const result = npmPkg(value);
|
|
24
40
|
if (result.validForNewPackages || result.validForOldPackages) {
|
|
@@ -27,7 +43,12 @@ export const name = (value) => {
|
|
|
27
43
|
throw new Error(`Parameter "name" is not valid - Value: ${value}`);
|
|
28
44
|
};
|
|
29
45
|
|
|
30
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Checks that a value is a valid semver version.
|
|
48
|
+
* @param {string} value
|
|
49
|
+
* @returns {string} the value in lowercase
|
|
50
|
+
* @throws {Error} if the value is not a valid semver version
|
|
51
|
+
*/
|
|
31
52
|
export const version = (value) => {
|
|
32
53
|
const result = semver.valid(value);
|
|
33
54
|
if (result) {
|
|
@@ -36,7 +57,12 @@ export const version = (value) => {
|
|
|
36
57
|
throw new Error(`Parameter "version" is not valid - Value: ${value}`);
|
|
37
58
|
};
|
|
38
59
|
|
|
39
|
-
|
|
60
|
+
/**
|
|
61
|
+
* Checks that a value is a valid alias value.
|
|
62
|
+
* @param {string} value
|
|
63
|
+
* @returns {string} the value in lowercase
|
|
64
|
+
* @throws {Error} if the value is not a valid alias value
|
|
65
|
+
*/
|
|
40
66
|
export const alias = (value) => {
|
|
41
67
|
if (/^[0-9]+$/.test(value)) {
|
|
42
68
|
return value;
|
|
@@ -44,19 +70,39 @@ export const alias = (value) => {
|
|
|
44
70
|
throw new Error(`Parameter "alias" is not valid - Value: ${value}`);
|
|
45
71
|
};
|
|
46
72
|
|
|
47
|
-
|
|
73
|
+
/**
|
|
74
|
+
* Checks that a value is a valid Eik package type.
|
|
75
|
+
* @param {string} value
|
|
76
|
+
* @returns {string} the value in lowercase
|
|
77
|
+
* @throws {Error} if the value is not a valid Eik package type
|
|
78
|
+
*/
|
|
48
79
|
export const type = (value) => {
|
|
49
|
-
if (
|
|
80
|
+
if (
|
|
81
|
+
value === 'pkg' ||
|
|
82
|
+
value === 'map' ||
|
|
83
|
+
value === 'npm' ||
|
|
84
|
+
value === 'img'
|
|
85
|
+
) {
|
|
50
86
|
return value;
|
|
51
87
|
}
|
|
52
88
|
throw new Error(`Parameter "type" is not valid - Value: ${value}`);
|
|
53
89
|
};
|
|
54
90
|
|
|
55
|
-
|
|
56
|
-
|
|
91
|
+
/**
|
|
92
|
+
* No-op, returning its value.
|
|
93
|
+
* @param {string} value
|
|
94
|
+
* @returns {string} the value
|
|
95
|
+
* @todo https://github.com/asset-pipe/core/issues/12
|
|
96
|
+
* @todo Can we remove this?
|
|
97
|
+
*/
|
|
57
98
|
export const extra = (value) => value;
|
|
58
99
|
|
|
59
|
-
|
|
100
|
+
/**
|
|
101
|
+
* Checks that a value is a valid semver type (major, minor, patch).
|
|
102
|
+
* @param {string} value
|
|
103
|
+
* @returns {string} the value
|
|
104
|
+
* @throws {Error} if the value is not a valid semver type
|
|
105
|
+
*/
|
|
60
106
|
export const semverType = (value) => {
|
|
61
107
|
if (value === 'major' || value === 'minor' || value === 'patch') {
|
|
62
108
|
return value;
|
package/package.json
CHANGED
|
@@ -17,7 +17,7 @@ export default class EikConfig {
|
|
|
17
17
|
/** @type {EikjsonSchema["version"]} */
|
|
18
18
|
get version(): string;
|
|
19
19
|
/** @type {EikjsonSchema["type"]} */
|
|
20
|
-
get type(): "map" | "npm" | "package";
|
|
20
|
+
get type(): "map" | "npm" | "package" | "image";
|
|
21
21
|
/** @type {string} */
|
|
22
22
|
get server(): string;
|
|
23
23
|
/** @type {[string, string][]} */
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare
|
|
1
|
+
declare function _default(type: string): string;
|
|
2
2
|
export default _default;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare
|
|
1
|
+
declare function _default(type: string): "PACKAGE" | "NPM" | "MAP" | "IMAGE";
|
|
2
2
|
export default _default;
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
declare namespace _default {
|
|
2
|
-
function eikJSON(value:
|
|
3
|
-
function name(value:
|
|
4
|
-
function type(value:
|
|
5
|
-
function version(value:
|
|
6
|
-
function server(value:
|
|
7
|
-
function files(value:
|
|
8
|
-
function importMap(value:
|
|
9
|
-
function out(value:
|
|
2
|
+
function eikJSON(value: import("../../eikjson.js").EikjsonSchema): void;
|
|
3
|
+
function name(value: string): void;
|
|
4
|
+
function type(value: string): void;
|
|
5
|
+
function version(value: string): void;
|
|
6
|
+
function server(value: string): void;
|
|
7
|
+
function files(value: string | Record<string, string>): void;
|
|
8
|
+
function importMap(value: string | string[]): void;
|
|
9
|
+
function out(value: string): void;
|
|
10
10
|
}
|
|
11
11
|
export default _default;
|
|
12
|
+
export type SchemaAssert<T> = (value: T) => void;
|
|
@@ -1,26 +1,47 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
error: any;
|
|
1
|
+
export type ErrorObject = import("ajv").ErrorObject & {
|
|
2
|
+
dataPath?: string;
|
|
4
3
|
};
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
value: any;
|
|
9
|
-
error: any;
|
|
10
|
-
};
|
|
11
|
-
export function server(data: any): {
|
|
12
|
-
value: any;
|
|
13
|
-
error: any;
|
|
14
|
-
};
|
|
15
|
-
export function files(data: any): {
|
|
16
|
-
value: any;
|
|
17
|
-
error: any;
|
|
18
|
-
};
|
|
19
|
-
export function importMap(data: any): {
|
|
20
|
-
value: any;
|
|
21
|
-
error: any;
|
|
22
|
-
};
|
|
23
|
-
export function out(data: any): {
|
|
24
|
-
value: any;
|
|
25
|
-
error: any;
|
|
4
|
+
export type SchemaValidator<T = unknown> = (data: T) => {
|
|
5
|
+
value: T;
|
|
6
|
+
error: false | import("ajv").ErrorObject[];
|
|
26
7
|
};
|
|
8
|
+
/**
|
|
9
|
+
* Checks that the given value includes required fields that are valid
|
|
10
|
+
* @type {SchemaValidator<import('../../eikjson.js').EikjsonSchema>}
|
|
11
|
+
*/
|
|
12
|
+
export const eikJSON: SchemaValidator<import("../../eikjson.js").EikjsonSchema>;
|
|
13
|
+
/**
|
|
14
|
+
* Checks [name](https://eik.dev/docs/reference/eik-json#name)
|
|
15
|
+
* @type {SchemaValidator<string>}
|
|
16
|
+
*/
|
|
17
|
+
export const name: SchemaValidator<string>;
|
|
18
|
+
/**
|
|
19
|
+
* Checks [version](https://eik.dev/docs/reference/eik-json#version)
|
|
20
|
+
* @type {SchemaValidator<string>}
|
|
21
|
+
*/
|
|
22
|
+
export const version: SchemaValidator<string>;
|
|
23
|
+
/**
|
|
24
|
+
* Checks [type](https://eik.dev/docs/reference/eik-json#type)
|
|
25
|
+
* @type {SchemaValidator<string>}
|
|
26
|
+
*/
|
|
27
|
+
export const type: SchemaValidator<string>;
|
|
28
|
+
/**
|
|
29
|
+
* Checks [server](https://eik.dev/docs/reference/eik-json#server)
|
|
30
|
+
* @type {SchemaValidator<string>}
|
|
31
|
+
*/
|
|
32
|
+
export const server: SchemaValidator<string>;
|
|
33
|
+
/**
|
|
34
|
+
* Checks [files](https://eik.dev/docs/reference/eik-json#files)
|
|
35
|
+
* @type {SchemaValidator<string | Record<string, string>>}
|
|
36
|
+
*/
|
|
37
|
+
export const files: SchemaValidator<string | Record<string, string>>;
|
|
38
|
+
/**
|
|
39
|
+
* Checks [import-map](https://eik.dev/docs/reference/eik-json#import-map)
|
|
40
|
+
* @type {SchemaValidator<string | string[]>}
|
|
41
|
+
*/
|
|
42
|
+
export const importMap: SchemaValidator<string | string[]>;
|
|
43
|
+
/**
|
|
44
|
+
* Checks [out](https://eik.dev/docs/reference/eik-json#out)
|
|
45
|
+
* @type {SchemaValidator<string>}
|
|
46
|
+
*/
|
|
47
|
+
export const out: SchemaValidator<string>;
|
package/types/stream.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export function isStream(stream:
|
|
2
|
-
export function isReadableStream(stream:
|
|
1
|
+
export function isStream(stream: unknown): boolean;
|
|
2
|
+
export function isReadableStream(stream: unknown): boolean;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export function origin(value:
|
|
2
|
-
export function org(value:
|
|
3
|
-
export function name(value:
|
|
4
|
-
export function version(value:
|
|
5
|
-
export function alias(value:
|
|
6
|
-
export function type(value:
|
|
7
|
-
export function extra(value:
|
|
8
|
-
export function semverType(value:
|
|
1
|
+
export function origin(value: string): string;
|
|
2
|
+
export function org(value: string): string;
|
|
3
|
+
export function name(value: string): string;
|
|
4
|
+
export function version(value: string): string;
|
|
5
|
+
export function alias(value: string): string;
|
|
6
|
+
export function type(value: string): string;
|
|
7
|
+
export function extra(value: string): string;
|
|
8
|
+
export function semverType(value: string): string;
|