@proteinjs/user-server 1.24.1 → 1.24.2
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 +11 -0
- package/dist/generated/index.d.ts +1 -0
- package/dist/generated/index.d.ts.map +1 -1
- package/dist/generated/index.js +2 -1
- package/dist/generated/index.js.map +1 -1
- package/dist/src/emails/AccountDeletionEmailConfigs.d.ts +10 -5
- package/dist/src/emails/AccountDeletionEmailConfigs.d.ts.map +1 -1
- package/dist/src/emails/AccountDeletionEmailConfigs.js.map +1 -1
- package/dist/test/PublicDeclarationSurface.test.d.ts +2 -0
- package/dist/test/PublicDeclarationSurface.test.d.ts.map +1 -0
- package/dist/test/PublicDeclarationSurface.test.js +157 -0
- package/dist/test/PublicDeclarationSurface.test.js.map +1 -0
- package/generated/index.ts +2 -1
- package/package.json +3 -2
- package/src/emails/AccountDeletionEmailConfigs.ts +11 -5
- package/test/PublicDeclarationSurface.test.ts +175 -0
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { builtinModules } from 'module';
|
|
4
|
+
import ts from 'typescript';
|
|
5
|
+
|
|
6
|
+
const PACKAGE_DIR = path.resolve(__dirname, '..');
|
|
7
|
+
|
|
8
|
+
type PackageManifest = {
|
|
9
|
+
types: string;
|
|
10
|
+
dependencies?: Record<string, string>;
|
|
11
|
+
peerDependencies?: Record<string, string>;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/** One bare module specifier named by a public declaration file. */
|
|
15
|
+
type DeclarationImport = {
|
|
16
|
+
/** The specifier as written (`@scope/name/sub`, `name`, `node:fs`). */
|
|
17
|
+
specifier: string;
|
|
18
|
+
/** The package a consumer resolves it from (`@scope/name`, `name`); `node:` stripped. */
|
|
19
|
+
packageName: string;
|
|
20
|
+
/** The declaration file naming it, relative to the package. */
|
|
21
|
+
file: string;
|
|
22
|
+
/** How it is named: an import / export clause, an `import('x')` type, or a `/// <reference types>`. */
|
|
23
|
+
via: 'import' | 'import()' | 'reference types';
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The emitted declaration files a consumer's compiler reads — every `.d.ts` reachable from the
|
|
28
|
+
* package's `types` entry through relative imports, exports and `/// <reference path>`
|
|
29
|
+
* directives — and every bare module specifier those files name: import / export … from,
|
|
30
|
+
* `import type`, `import('x')` type nodes, `import x = require('x')`, `/// <reference types="x" />`.
|
|
31
|
+
* Read from the built `dist` with TypeScript's own parser (a doc comment that mentions `'users'`
|
|
32
|
+
* is not an import), so the suite runs after `npm run build`.
|
|
33
|
+
*/
|
|
34
|
+
class PublicDeclarationSurface {
|
|
35
|
+
readonly files: string[] = [];
|
|
36
|
+
readonly imports: DeclarationImport[] = [];
|
|
37
|
+
private seen = new Set<string>();
|
|
38
|
+
|
|
39
|
+
constructor(
|
|
40
|
+
private packageDir: string,
|
|
41
|
+
typesEntry: string
|
|
42
|
+
) {
|
|
43
|
+
this.walk(path.resolve(packageDir, typesEntry));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The distinct packages the surface names, as a consumer resolves them. */
|
|
47
|
+
packageNames(): string[] {
|
|
48
|
+
return Array.from(new Set(this.imports.map((entry) => entry.packageName))).sort();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** `@scope/name/sub` -> `@scope/name`; `name/sub` -> `name`; `node:fs` -> `fs`. */
|
|
52
|
+
static packageNameOf(specifier: string): string {
|
|
53
|
+
const bare = specifier.startsWith('node:') ? specifier.slice('node:'.length) : specifier;
|
|
54
|
+
const parts = bare.split('/');
|
|
55
|
+
return bare.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private walk(file: string) {
|
|
59
|
+
if (this.seen.has(file)) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
this.seen.add(file);
|
|
63
|
+
if (!fs.existsSync(file)) {
|
|
64
|
+
throw new Error(`public declaration file missing: ${path.relative(this.packageDir, file)} — build first`);
|
|
65
|
+
}
|
|
66
|
+
this.files.push(file);
|
|
67
|
+
const source = ts.createSourceFile(file, fs.readFileSync(file, 'utf8'), ts.ScriptTarget.Latest, true);
|
|
68
|
+
for (const reference of source.referencedFiles) {
|
|
69
|
+
this.walk(this.declarationFileFor(file, reference.fileName));
|
|
70
|
+
}
|
|
71
|
+
for (const reference of source.typeReferenceDirectives) {
|
|
72
|
+
this.record(file, reference.fileName, 'reference types');
|
|
73
|
+
}
|
|
74
|
+
const visit = (node: ts.Node) => {
|
|
75
|
+
const specifier = this.specifierOf(node);
|
|
76
|
+
if (specifier) {
|
|
77
|
+
if (specifier.text.startsWith('.')) {
|
|
78
|
+
this.walk(this.declarationFileFor(file, specifier.text));
|
|
79
|
+
} else {
|
|
80
|
+
this.record(file, specifier.text, specifier.via);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
ts.forEachChild(node, visit);
|
|
84
|
+
};
|
|
85
|
+
visit(source);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
private specifierOf(node: ts.Node): { text: string; via: DeclarationImport['via'] } | undefined {
|
|
89
|
+
if ((ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) && node.moduleSpecifier) {
|
|
90
|
+
return ts.isStringLiteral(node.moduleSpecifier) ? { text: node.moduleSpecifier.text, via: 'import' } : undefined;
|
|
91
|
+
}
|
|
92
|
+
if (ts.isImportEqualsDeclaration(node) && ts.isExternalModuleReference(node.moduleReference)) {
|
|
93
|
+
const { expression } = node.moduleReference;
|
|
94
|
+
return ts.isStringLiteral(expression) ? { text: expression.text, via: 'import' } : undefined;
|
|
95
|
+
}
|
|
96
|
+
if (ts.isImportTypeNode(node) && ts.isLiteralTypeNode(node.argument)) {
|
|
97
|
+
const { literal } = node.argument;
|
|
98
|
+
return ts.isStringLiteral(literal) ? { text: literal.text, via: 'import()' } : undefined;
|
|
99
|
+
}
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
private record(file: string, specifier: string, via: DeclarationImport['via']) {
|
|
104
|
+
this.imports.push({
|
|
105
|
+
specifier,
|
|
106
|
+
packageName: PublicDeclarationSurface.packageNameOf(specifier),
|
|
107
|
+
file: path.relative(this.packageDir, file),
|
|
108
|
+
via,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** A relative specifier's declaration file: `x.d.ts`, else `x/index.d.ts`; a `.d.ts` path as is. */
|
|
113
|
+
private declarationFileFor(from: string, relative: string): string {
|
|
114
|
+
const target = path.resolve(path.dirname(from), relative);
|
|
115
|
+
const candidates = relative.endsWith('.d.ts') ? [target] : [`${target}.d.ts`, path.join(target, 'index.d.ts')];
|
|
116
|
+
const found = candidates.find((candidate) => fs.existsSync(candidate));
|
|
117
|
+
if (!found) {
|
|
118
|
+
throw new Error(
|
|
119
|
+
`${path.relative(this.packageDir, from)} names ${relative}, but none of ${candidates
|
|
120
|
+
.map((candidate) => path.relative(this.packageDir, candidate))
|
|
121
|
+
.join(', ')} exists`
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
return found;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* The law: a package's public declarations resolve against its own `dependencies` and
|
|
130
|
+
* `peerDependencies` (and node's builtins) alone — never a devDependency, never a package reached
|
|
131
|
+
* only through another dependency's tree. A consumer that type-checks this package then sees the
|
|
132
|
+
* same types whether its copy is installed from the registry (devDependencies absent) or linked
|
|
133
|
+
* from a checkout (devDependencies present), and the same whether or not a transitive package
|
|
134
|
+
* happens to hoist beside it.
|
|
135
|
+
*/
|
|
136
|
+
describe('the public declaration surface resolves against the declared dependencies alone', () => {
|
|
137
|
+
const manifest = JSON.parse(fs.readFileSync(path.join(PACKAGE_DIR, 'package.json'), 'utf8')) as PackageManifest;
|
|
138
|
+
const declared = new Set([
|
|
139
|
+
...Object.keys(manifest.dependencies ?? {}),
|
|
140
|
+
...Object.keys(manifest.peerDependencies ?? {}),
|
|
141
|
+
]);
|
|
142
|
+
const builtins = new Set(builtinModules);
|
|
143
|
+
const surface = new PublicDeclarationSurface(PACKAGE_DIR, manifest.types);
|
|
144
|
+
|
|
145
|
+
/** `name` is declared as itself or as its DefinitelyTyped package (`@types/name`, `@types/scope__name`). */
|
|
146
|
+
const isDeclared = (packageName: string) => {
|
|
147
|
+
const typesName = packageName.startsWith('@')
|
|
148
|
+
? `@types/${packageName.slice(1).replace('/', '__')}`
|
|
149
|
+
: `@types/${packageName}`;
|
|
150
|
+
return declared.has(packageName) || declared.has(typesName);
|
|
151
|
+
};
|
|
152
|
+
/** A builtin module, or the `node` types that `/// <reference types="node" />` names for them. */
|
|
153
|
+
const isBuiltin = (entry: DeclarationImport) =>
|
|
154
|
+
builtins.has(entry.packageName) || (entry.via === 'reference types' && entry.packageName === 'node');
|
|
155
|
+
|
|
156
|
+
it('reads the whole surface from the types entry (the control: the walk sees files and their imports)', () => {
|
|
157
|
+
const relativeFiles = surface.files.map((file) => path.relative(PACKAGE_DIR, file));
|
|
158
|
+
expect(relativeFiles).toContain(path.normalize('dist/index.d.ts'));
|
|
159
|
+
expect(relativeFiles).toContain(path.normalize('dist/src/emails/AccountDeletionEmailConfigs.d.ts'));
|
|
160
|
+
expect(relativeFiles.length).toBeGreaterThanOrEqual(10);
|
|
161
|
+
expect(surface.packageNames()).toEqual(
|
|
162
|
+
expect.arrayContaining(['@proteinjs/reflection', '@proteinjs/user', 'moment'])
|
|
163
|
+
);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('names no package outside dependencies / peerDependencies / node builtins', () => {
|
|
167
|
+
const undeclared = surface.imports
|
|
168
|
+
.filter((entry) => !isBuiltin(entry) && !isDeclared(entry.packageName))
|
|
169
|
+
.map(
|
|
170
|
+
(entry) =>
|
|
171
|
+
`${entry.file} names '${entry.specifier}' (${entry.via}) — ${entry.packageName} is not in dependencies or peerDependencies`
|
|
172
|
+
);
|
|
173
|
+
expect(undeclared).toEqual([]);
|
|
174
|
+
});
|
|
175
|
+
});
|