@flex-development/mlly 1.0.0-beta.2 → 1.0.0-beta.3

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 CHANGED
@@ -1,3 +1,20 @@
1
+ ## [1.0.0-beta.3](https://github.com/flex-development/mlly/compare/1.0.0-beta.2...1.0.0-beta.3) (2026-02-03)
2
+
3
+ ### ⚠ BREAKING CHANGES
4
+
5
+ - **lib:** [`getSource`] return `FileContent`
6
+ - **ts:** [`ReadFile`] return string given `encoding`
7
+
8
+ ### :package: Build
9
+
10
+ - [[`2a5ffdb`](https://github.com/flex-development/mlly/commit/2a5ffdb20ce9b1fc5be481bd1de15cf429d0aafa)] **deps-dev:** Bump cspell from 9.6.2 to 9.6.3 ([#960](https://github.com/flex-development/mlly/issues/960))
11
+
12
+ ### :mechanical_arm: Refactors
13
+
14
+ - [[`eaca2a7`](https://github.com/flex-development/mlly/commit/eaca2a7549deadd7362f8a2f5993167d9313bb45)] **lib:** [`getSource`] return `FileContent`
15
+ - [[`490d72c`](https://github.com/flex-development/mlly/commit/490d72cba0f59cb4f2fe4ff27cac4b81fa597cc6)] **ts:** match `this` context to node
16
+ - [[`cd54387`](https://github.com/flex-development/mlly/commit/cd54387ed7324085ed94d7b5ebb996e5cac85b4e)] **ts:** [`ReadFile`] return string given `encoding`
17
+
1
18
  ## [1.0.0-beta.2](https://github.com/flex-development/mlly/compare/1.0.0-beta.1...1.0.0-beta.2) (2026-02-02)
2
19
 
3
20
  ### :sparkles: Features
@@ -1260,3 +1277,4 @@
1260
1277
  - [[`36c4b74`](https://github.com/flex-development/mlly/commit/36c4b7475c9bb6c924f5e75c8d6d215a8d23e79c)] **specifiers:** [`toBareSpecifier`] improve `exports` path search
1261
1278
 
1262
1279
 
1280
+
package/README.md CHANGED
@@ -57,6 +57,8 @@
57
57
  - [Types](#types)
58
58
  - [`Aliases`](#aliases)
59
59
  - [`Awaitable<T>`](#awaitablet)
60
+ - [`BufferEncodingMap`](#bufferencodingmap)
61
+ - [`BufferEncoding`](#bufferencoding)
60
62
  - [`ChangeExtFn<[Ext]>`](#changeextfnext)
61
63
  - [`ConditionMap`](#conditionmap)
62
64
  - [`Condition`](#condition)
@@ -65,6 +67,7 @@
65
67
  - [`EmptyObject`](#emptyobject)
66
68
  - [`EmptyString`](#emptystring)
67
69
  - [`Ext`](#ext)
70
+ - [`FileContent`](#filecontent)
68
71
  - [`FileSystem`](#filesystem)
69
72
  - [`GetSourceContext`](#getsourcecontext)
70
73
  - [`GetSourceHandler`](#getsourcehandler)
@@ -271,7 +274,7 @@ Get the source code for a module.
271
274
 
272
275
  #### Type Parameters
273
276
 
274
- - `T` ([`Awaitable<string | null | undefined>`](#awaitablet))
277
+ - `T` ([`Awaitable<FileContent | null | undefined>`](#filecontent))
275
278
  — the module source code
276
279
 
277
280
  #### Parameters
@@ -1039,6 +1042,31 @@ type Awaitable<T> = PromiseLike<T> | T
1039
1042
  - `T` (`any`)
1040
1043
  - the value
1041
1044
 
1045
+ ### `BufferEncodingMap`
1046
+
1047
+ Registry of character encodings that can be used when working with `Buffer` objects (`interface`).
1048
+
1049
+ When developing extensions that use additional encodings, augment `BufferEncodingMap` to register custom encodings:
1050
+
1051
+ ```ts
1052
+ declare module '@flex-development/mlly' {
1053
+ interface BufferEncodingMap {
1054
+ custom: 'custom'
1055
+ }
1056
+ }
1057
+ ```
1058
+
1059
+ ### `BufferEncoding`
1060
+
1061
+ Union of values that can occur where a buffer encoding is expected (`type`).
1062
+
1063
+ To register new encodings, augment [`BufferEncodingMap`](#bufferencodingmap).
1064
+ They will be added to this union automatically.
1065
+
1066
+ ```ts
1067
+ type BufferEncoding = BufferEncodingMap[keyof BufferEncodingMap]
1068
+ ```
1069
+
1042
1070
  ### `ChangeExtFn<[Ext]>`
1043
1071
 
1044
1072
  Get a new file extension for `url` (`type`).
@@ -1132,6 +1160,14 @@ A file extension (`type`).
1132
1160
  type Ext = `${Dot}${string}`
1133
1161
  ```
1134
1162
 
1163
+ ### `FileContent`
1164
+
1165
+ Union of values that can occur where file content is expected (`type`).
1166
+
1167
+ ```ts
1168
+ type FileContent = Uint8Array | string
1169
+ ```
1170
+
1135
1171
  ### `FileSystem`
1136
1172
 
1137
1173
  The file system API (`interface`).
@@ -1172,7 +1208,7 @@ Get the source code for a module (`type`).
1172
1208
  type GetSourceHandler = (
1173
1209
  this: GetSourceContext,
1174
1210
  url: URL
1175
- ) => Awaitable<Uint8Array | string | null | undefined>
1211
+ ) => Awaitable<FileContent | null | undefined>
1176
1212
  ```
1177
1213
 
1178
1214
  #### Parameters
@@ -1184,7 +1220,7 @@ type GetSourceHandler = (
1184
1220
 
1185
1221
  #### Returns
1186
1222
 
1187
- ([`Awaitable<Uint8Array | string | null | undefined>`](#awaitablet)) The source code
1223
+ ([`Awaitable<FileContent | null | undefined>`](#filecontent)) The source code
1188
1224
 
1189
1225
  ### `GetSourceHandlers`
1190
1226
 
@@ -1202,6 +1238,9 @@ Options for retrieving source code (`interface`).
1202
1238
 
1203
1239
  #### Properties
1204
1240
 
1241
+ - `encoding?` ([`BufferEncoding`](#bufferencoding) | `null` | `undefined`)
1242
+ — the encoding of the result
1243
+ > 👉 **note**: used when the `file:` handler is called
1205
1244
  - `format?` ([`ModuleFormat`](#moduleformat) | `null` | `undefined`)
1206
1245
  — the module format hint
1207
1246
  - `fs?` ([`FileSystem`](#filesystem) | `null` | `undefined`)
@@ -1376,15 +1415,25 @@ type Protocol = ProtocolMap[keyof ProtocolMap]
1376
1415
 
1377
1416
  Read the entire contents of a file (`interface`).
1378
1417
 
1418
+ #### Overloads
1419
+
1420
+ ```ts
1421
+ (id: ModuleId, encoding: BufferEncoding): Awaitable<string>
1422
+ (id: ModuleId, encoding?: BufferEncoding | null | undefined): T
1423
+ ```
1424
+
1379
1425
  #### Type Parameters
1380
1426
 
1381
- - `T` ([`Awaitable<Buffer | string>`](#awaitablet), optional)
1427
+ - `T` ([`Awaitable<FileContent | null | undefined>`](#filecontent), optional)
1382
1428
  — the file contents
1429
+ - **default**: [`Awaitable<FileContent>`](#filecontent)
1383
1430
 
1384
1431
  #### Parameters
1385
1432
 
1386
1433
  - `id` ([`ModuleId`](#moduleid))
1387
1434
  — the module id
1435
+ - `encoding` ([`BufferEncoding`](#bufferencoding))
1436
+ — the encoding of the file contents
1388
1437
 
1389
1438
  #### Returns
1390
1439
 
@@ -0,0 +1,29 @@
1
+ /**
2
+ * @file Interfaces - BufferEncodingMap
3
+ * @module mlly/interfaces/BufferEncodingMap
4
+ */
5
+ /**
6
+ * Registry of character encodings that can be used when working
7
+ * with {@linkcode Buffer} objects.
8
+ *
9
+ * This interface can be augmented to register custom encodings.
10
+ *
11
+ * @example
12
+ * declare module '@flex-development/mlly' {
13
+ * interface BufferEncodingMap {
14
+ * custom: 'custom'
15
+ * }
16
+ * }
17
+ */
18
+ interface BufferEncodingMap {
19
+ ascii: 'ascii';
20
+ base64: 'base64';
21
+ base64url: 'base64url';
22
+ binary: 'binary';
23
+ hex: 'hex';
24
+ latin1: 'latin1';
25
+ ucs2: 'ucs2' | 'ucs-2';
26
+ utf16le: 'utf16le' | 'utf-16le';
27
+ utf8: 'utf8' | 'utf-8';
28
+ }
29
+ export type { BufferEncodingMap as default };
@@ -2,11 +2,19 @@
2
2
  * @file Interfaces - GetSourceOptions
3
3
  * @module mlly/interfaces/GetSourceOptions
4
4
  */
5
- import type { FileSystem, GetSourceHandlers, List, ModuleFormat } from '@flex-development/mlly';
5
+ import type { BufferEncoding, FileSystem, GetSourceHandlers, List, ModuleFormat } from '@flex-development/mlly';
6
6
  /**
7
7
  * Options for retrieving source code.
8
8
  */
9
9
  interface GetSourceOptions {
10
+ /**
11
+ * The encoding of the result.
12
+ *
13
+ * > 👉 **Note**: Used when the `file:` handler is called.
14
+ *
15
+ * @see {@linkcode BufferEncoding}
16
+ */
17
+ encoding?: BufferEncoding | null | undefined;
10
18
  /**
11
19
  * The module format hint.
12
20
  *
@@ -3,6 +3,7 @@
3
3
  * @module mlly/interfaces
4
4
  */
5
5
  export type { default as Aliases } from '#interfaces/aliases';
6
+ export type { default as BufferEncodingMap } from '#interfaces/buffer-encoding-map';
6
7
  export type { default as ConditionMap } from '#interfaces/condition-map';
7
8
  export type { default as FileSystem } from '#interfaces/file-system';
8
9
  export type { default as GetSourceContext } from '#interfaces/get-source-context';
@@ -7,11 +7,11 @@
7
7
  */
8
8
  interface IsDirectory {
9
9
  /**
10
- * @this {void}
10
+ * @this {unknown}
11
11
  *
12
12
  * @return {boolean}
13
13
  * `true` if stats describes directory, `false` otherwise
14
14
  */
15
- (this: void): boolean;
15
+ (): boolean;
16
16
  }
17
17
  export type { IsDirectory as default };
@@ -12,6 +12,6 @@ interface IsFile {
12
12
  * @return {boolean}
13
13
  * `true` if stats object describes regular file, `false` otherwise
14
14
  */
15
- (this: void): boolean;
15
+ (): boolean;
16
16
  }
17
17
  export type { IsFile as default };
@@ -2,27 +2,42 @@
2
2
  * @file Interfaces - ReadFile
3
3
  * @module mlly/interfaces/ReadFile
4
4
  */
5
- import type { Awaitable, ModuleId } from '@flex-development/mlly';
5
+ import type { Awaitable, BufferEncoding, FileContent, ModuleId } from '@flex-development/mlly';
6
6
  /**
7
7
  * Read the entire contents of a file.
8
8
  *
9
9
  * @see {@linkcode Awaitable}
10
10
  * @see {@linkcode Buffer}
11
11
  *
12
- * @template {Awaitable<Buffer | string>} [T]
12
+ * @template {Awaitable<FileContent | null | undefined>} [T]
13
13
  * The file contents
14
14
  */
15
- interface ReadFile<T extends Awaitable<Buffer | string> = Awaitable<Buffer | string>> {
15
+ interface ReadFile<T extends Awaitable<FileContent | null | undefined> = Awaitable<FileContent>> {
16
16
  /**
17
17
  * @see {@linkcode ModuleId}
18
18
  *
19
- * @this {void}
19
+ * @this {unknown}
20
20
  *
21
21
  * @param {ModuleId} id
22
22
  * The module id
23
+ * @param {BufferEncoding} encoding
24
+ * The encoding of the file contents
25
+ * @return {Awaitable<string>}
26
+ * The file contents
27
+ */
28
+ (id: ModuleId, encoding: BufferEncoding): Awaitable<string>;
29
+ /**
30
+ * @see {@linkcode ModuleId}
31
+ *
32
+ * @this {unknown}
33
+ *
34
+ * @param {ModuleId} id
35
+ * The module id
36
+ * @param {BufferEncoding | null | undefined} [encoding]
37
+ * The encoding of the file contents
23
38
  * @return {T}
24
39
  * The file contents
25
40
  */
26
- (this: void, id: ModuleId): T;
41
+ (id: ModuleId, encoding?: BufferEncoding | null | undefined): T;
27
42
  }
28
43
  export type { ReadFile as default };
@@ -18,13 +18,13 @@ interface Realpath<T extends Awaitable<string> = Awaitable<string>> {
18
18
  /**
19
19
  * @see {@linkcode ModuleId}
20
20
  *
21
- * @this {void}
21
+ * @this {unknown}
22
22
  *
23
23
  * @param {ModuleId} id
24
24
  * The module id
25
25
  * @return {T}
26
26
  * The canonical pathname
27
27
  */
28
- (this: void, id: ModuleId): T;
28
+ (id: ModuleId): T;
29
29
  }
30
30
  export type { Realpath as default };
@@ -16,13 +16,13 @@ interface Stat<T extends Awaitable<Stats> = Awaitable<Stats>> {
16
16
  /**
17
17
  * @see {@linkcode ModuleId}
18
18
  *
19
- * @this {void}
19
+ * @this {unknown}
20
20
  *
21
21
  * @param {ModuleId} id
22
22
  * The module id
23
23
  * @return {T}
24
24
  * The info
25
25
  */
26
- (this: void, id: ModuleId): T;
26
+ (id: ModuleId): T;
27
27
  }
28
28
  export type { Stat as default };
@@ -2,7 +2,7 @@
2
2
  * @file getSource
3
3
  * @module mlly/lib/getSource
4
4
  */
5
- import type { Awaitable, EmptyString, GetSourceOptions, ModuleId } from '@flex-development/mlly';
5
+ import type { Awaitable, EmptyString, FileContent, GetSourceOptions, ModuleId } from '@flex-development/mlly';
6
6
  export default getSource;
7
7
  /**
8
8
  * Get the source code for a module.
@@ -30,7 +30,7 @@ declare function getSource(this: void, id: EmptyString | null | undefined, optio
30
30
  * @see {@linkcode GetSourceOptions}
31
31
  * @see {@linkcode ModuleId}
32
32
  *
33
- * @template {Awaitable<string | null | undefined>} T
33
+ * @template {Awaitable<FileContent | null | undefined>} T
34
34
  * The module source code
35
35
  *
36
36
  * @this {void}
@@ -43,4 +43,4 @@ declare function getSource(this: void, id: EmptyString | null | undefined, optio
43
43
  * The module source code
44
44
  * @throws {ErrUnsupportedEsmUrlScheme}
45
45
  */
46
- declare function getSource<T extends Awaitable<string | null | undefined>>(this: void, id: ModuleId | null | undefined, options?: GetSourceOptions | null | undefined): T;
46
+ declare function getSource<T extends Awaitable<FileContent | null | undefined>>(this: void, id: ModuleId | null | undefined, options?: GetSourceOptions | null | undefined): T;
@@ -28,7 +28,7 @@ export default getSource;
28
28
  * The module id
29
29
  * @param {GetSourceOptions | null | undefined} [options]
30
30
  * Source code retrieval options
31
- * @return {Awaitable<string | null | undefined>}
31
+ * @return {Awaitable<FileContent | null | undefined>}
32
32
  * The module source code
33
33
  * @throws {ErrUnsupportedEsmUrlScheme}
34
34
  */
@@ -79,7 +79,7 @@ function getSource(id, options) {
79
79
  /**
80
80
  * The source code.
81
81
  *
82
- * @var {Awaitable<Uint8Array | string | null | undefined>} code
82
+ * @var {ReturnType<GetSourceHandler>} code
83
83
  */
84
84
  let code = handle.call(context, url);
85
85
  // resolve source code.
@@ -87,10 +87,7 @@ function getSource(id, options) {
87
87
  void code.then(resolved => (code = resolved));
88
88
  }
89
89
  return chainOrCall(code, () => {
90
- ok(!isPromise(code), 'expected `code` to be resolved');
91
- if (code !== null && code !== undefined)
92
- code = String(code);
93
- return code;
90
+ return ok(!isPromise(code), 'expected `code` to be resolved'), code;
94
91
  });
95
92
  }
96
93
  /**
@@ -115,7 +112,7 @@ function data(url) {
115
112
  *
116
113
  * @param {URL} url
117
114
  * The module URL
118
- * @return {Awaitable<Buffer | string | null>}
115
+ * @return {Awaitable<FileContent | null>}
119
116
  * The source code
120
117
  */
121
118
  function file(url) {
@@ -127,7 +124,7 @@ function file(url) {
127
124
  */
128
125
  const exists = isFile(url, this.fs);
129
126
  return chainOrCall(exists, isFile => {
130
- return isFile ?? exists ? this.fs.readFile(url) : null;
127
+ return isFile ?? exists ? this.fs.readFile(url, this.encoding) : null;
131
128
  });
132
129
  }
133
130
  /**
@@ -62,19 +62,18 @@ function readPackageJson(id, specifier, parent, fs) {
62
62
  /**
63
63
  * The stringified contents of the package manifest file.
64
64
  *
65
- * @const {Awaitable<Buffer | string>} contents
65
+ * @const {Awaitable<string>} contents
66
66
  */
67
- const contents = fs.readFile(url);
67
+ const contents = fs.readFile(url, 'utf8');
68
68
  // parse file content.
69
- return chainOrCall(contents, data => {
70
- data ??= contents;
69
+ return chainOrCall(contents, (data = contents) => {
71
70
  try {
72
71
  /**
73
72
  * The parsed file contents.
74
73
  *
75
- * @const {any} parsed
74
+ * @const {unknown} parsed
76
75
  */
77
- const parsed = JSON.parse(String(data));
76
+ const parsed = JSON.parse(data);
78
77
  if (isPackageJson(parsed))
79
78
  return parsed;
80
79
  throw new Error('Invalid package manifest object', { cause: parsed });
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @file Type Aliases - BufferEncoding
3
+ * @module mlly/types/BufferEncoding
4
+ */
5
+ import type { BufferEncodingMap } from '@flex-development/mlly';
6
+ /**
7
+ * Union of values that can occur where a buffer encoding is expected.
8
+ *
9
+ * To register new encodings, augment {@linkcode BufferEncodingMap}.
10
+ * They will be added to this union automatically.
11
+ */
12
+ type BufferEncoding = BufferEncodingMap[keyof BufferEncodingMap];
13
+ export type { BufferEncoding as default };
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @file Type Aliases - FileContent
3
+ * @module mlly/types/FileContent
4
+ */
5
+ /**
6
+ * Union of values that can occur where file content is expected.
7
+ *
8
+ * @see {@linkcode Uint8Array}
9
+ */
10
+ type FileContent = Uint8Array | string;
11
+ export type { FileContent as default };
@@ -2,11 +2,12 @@
2
2
  * @file Type Aliases - GetSourceHandler
3
3
  * @module mlly/types/GetSourceHandler
4
4
  */
5
- import type { Awaitable, GetSourceContext } from '@flex-development/mlly';
5
+ import type { Awaitable, FileContent, GetSourceContext } from '@flex-development/mlly';
6
6
  /**
7
7
  * Get the source code for a module.
8
8
  *
9
9
  * @see {@linkcode Awaitable}
10
+ * @see {@linkcode FileContent}
10
11
  * @see {@linkcode GetSourceContext}
11
12
  * @see {@linkcode URL}
12
13
  *
@@ -15,8 +16,8 @@ import type { Awaitable, GetSourceContext } from '@flex-development/mlly';
15
16
  *
16
17
  * @param {URL} url
17
18
  * The module URL
18
- * @return {Awaitable<Uint8Array | string | null | undefined>}
19
+ * @return {Awaitable<FileContent | null | undefined>}
19
20
  * The source code
20
21
  */
21
- type GetSourceHandler = (this: GetSourceContext, url: URL) => Awaitable<Uint8Array | string | null | undefined>;
22
+ type GetSourceHandler = (this: GetSourceContext, url: URL) => Awaitable<FileContent | null | undefined>;
22
23
  export type { GetSourceHandler as default };
@@ -3,6 +3,7 @@
3
3
  * @module mlly/types
4
4
  */
5
5
  export type { default as Awaitable } from '#types/awaitable';
6
+ export type { default as BufferEncoding } from '#types/buffer-encoding';
6
7
  export type { default as ChangeExtFn } from '#types/change-ext-fn';
7
8
  export type { default as Condition } from '#types/condition';
8
9
  export type { default as Dot } from '#types/dot';
@@ -10,6 +11,7 @@ export type { default as EmptyArray } from '#types/empty-array';
10
11
  export type { default as EmptyObject } from '#types/empty-object';
11
12
  export type { default as EmptyString } from '#types/empty-string';
12
13
  export type { default as Ext } from '#types/ext';
14
+ export type { default as FileContent } from '#types/file-content';
13
15
  export type { default as GetSourceHandler } from '#types/get-source-handler';
14
16
  export type { default as GetSourceHandlers } from '#types/get-source-handlers';
15
17
  export type { default as List } from '#types/list';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@flex-development/mlly",
3
3
  "description": "ECMAScript module utilities",
4
- "version": "1.0.0-beta.2",
4
+ "version": "1.0.0-beta.3",
5
5
  "keywords": [
6
6
  "ecmascript",
7
7
  "esm",
@@ -180,7 +180,7 @@
180
180
  "chai-string": "1.6.0",
181
181
  "concat-stream": "2.0.0",
182
182
  "cross-env": "10.1.0",
183
- "cspell": "9.6.2",
183
+ "cspell": "9.6.3",
184
184
  "dprint": "0.51.1",
185
185
  "editorconfig": "3.0.1",
186
186
  "eslint": "9.39.2",