Skip to content

Commit fbd1815

Browse files
committed
fix: stabilize license ordering and reject stale jpeg-js notices
1 parent b90d5ec commit fbd1815

2 files changed

Lines changed: 89 additions & 26 deletions

File tree

‎scripts/__tests__/bundled-license-notices.test.ts‎

Lines changed: 71 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,18 @@ async function bundle(root: string, dts = false): Promise<void> {
3434
outExtensions: () => ({ js: '.js', dts: '.d.ts' }),
3535
dts,
3636
minify: true,
37-
deps: { onlyBundle: ['included', 'shared', 'distinct', 'unused', 'jpeg-js'] },
37+
deps: {
38+
onlyBundle: [
39+
'included',
40+
'shared',
41+
'distinct',
42+
'unused',
43+
'jpeg-js',
44+
'included-extra',
45+
'included.extra',
46+
'included_extra',
47+
],
48+
},
3849
plugins: [bundledLicenseNotices(root, path.join(root, 'dist'))],
3950
logLevel: 'silent',
4051
});
@@ -144,6 +155,36 @@ test('identical notices are shared without losing packages or distinct copyright
144155
assert.equal(await fs.readFile(path.join(root, 'dist/THIRD_PARTY_NOTICES.txt'), 'utf8'), notices);
145156
});
146157

158+
test('notice references use code-unit ordering for package names', async () => {
159+
const names = ['included_extra', 'included-extra', 'included.extra'];
160+
const files: Record<string, string> = {
161+
'input.ts': names
162+
.map((name, index) => `export { value as value${index} } from '${name}';`)
163+
.join('\n'),
164+
};
165+
for (const name of names) {
166+
files[`node_modules/${name}/package.json`] = JSON.stringify({
167+
name,
168+
version: '1.0.0',
169+
license: 'MIT',
170+
main: 'index.js',
171+
});
172+
files[`node_modules/${name}/index.js`] = `export const value = '${name}';`;
173+
files[`node_modules/${name}/LICENSE`] = 'Copyright ordering fixture\nShared license terms.';
174+
}
175+
const root = await fixture(files);
176+
await bundle(root);
177+
const notices = await fs.readFile(path.join(root, 'dist/THIRD_PARTY_NOTICES.txt'), 'utf8');
178+
assert.equal(
179+
notices.split('\n\n')[0],
180+
[
181+
'[email protected] (MIT): [1]',
182+
'[email protected] (MIT): [1]',
183+
'[email protected] (MIT): [1]',
184+
].join('\n'),
185+
);
186+
});
187+
147188
test('declaration generation preserves JavaScript dependency notices', async () => {
148189
const root = await fixture({
149190
'input.ts': "export { value } from 'included';",
@@ -166,24 +207,32 @@ test('declaration generation preserves JavaScript dependency notices', async ()
166207
assert.equal(await fs.readFile(path.join(root, 'dist/THIRD_PARTY_NOTICES.txt'), 'utf8'), notices);
167208
});
168209

169-
test('bundled jpeg-js retains source-file notices and the Apache license', async () => {
170-
const root = await fixture({
171-
'input.ts': "export { value } from 'jpeg-js';",
172-
'node_modules/jpeg-js/package.json': JSON.stringify({
173-
name: 'jpeg-js',
174-
version: '0.4.4',
175-
license: 'BSD-3-Clause',
176-
main: 'index.js',
177-
}),
178-
'node_modules/jpeg-js/index.js': 'export const value = process.env.FIXTURE_NUMBER;',
179-
'node_modules/jpeg-js/LICENSE': 'Package-level BSD terms.',
180-
'LICENSES/jpeg-js.txt': 'Copyright 2011 notmasteryet\nCopyright 2008 Adobe.',
181-
'LICENSES/Apache-2.0.txt': 'Full Apache license text.',
182-
});
183-
await bundle(root);
184-
const notices = await fs.readFile(path.join(root, 'dist/THIRD_PARTY_NOTICES.txt'), 'utf8');
185-
assert.match(notices, /Package-level BSD terms/);
186-
assert.match(notices, /Copyright 2011 notmasteryet/);
187-
assert.match(notices, /Copyright 2008 Adobe/);
188-
assert.match(notices, /Full Apache license text/);
189-
});
210+
test.each(['0.4.4', '0.4.5'])(
211+
'bundled jpeg-js@%s requires matching source-file notices',
212+
async (version) => {
213+
const root = await fixture({
214+
'input.ts': "export { value } from 'jpeg-js';",
215+
'node_modules/jpeg-js/package.json': JSON.stringify({
216+
name: 'jpeg-js',
217+
version,
218+
license: 'BSD-3-Clause',
219+
main: 'index.js',
220+
}),
221+
'node_modules/jpeg-js/index.js': 'export const value = process.env.FIXTURE_NUMBER;',
222+
'node_modules/jpeg-js/LICENSE': 'Package-level BSD terms.',
223+
'LICENSES/jpeg-js.txt':
224+
'jpeg-js 0.4.4 source-file notices\n\nCopyright 2011 notmasteryet\nCopyright 2008 Adobe.',
225+
'LICENSES/Apache-2.0.txt': 'Full Apache license text.',
226+
});
227+
if (version === '0.4.5') {
228+
await assert.rejects(bundle(root), /LICENSES\/jpeg-js\.txt.*jpeg-js@0\.4\.5/);
229+
return;
230+
}
231+
await bundle(root);
232+
const notices = await fs.readFile(path.join(root, 'dist/THIRD_PARTY_NOTICES.txt'), 'utf8');
233+
assert.match(notices, /Package-level BSD terms/);
234+
assert.match(notices, /Copyright 2011 notmasteryet/);
235+
assert.match(notices, /Copyright 2008 Adobe/);
236+
assert.match(notices, /Full Apache license text/);
237+
},
238+
);

‎scripts/lib/bundled-license-notices.ts‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ export function bundledLicenseNotices(root: string, outputDirectory: string) {
2323
return `[${id}]`;
2424
};
2525
const packages: string[] = [];
26-
const sortedDependencies = [...dependencies].sort((left, right) =>
27-
`${left.name}@${left.version}`.localeCompare(`${right.name}@${right.version}`),
28-
);
26+
const sortedDependencies = [...dependencies].sort((left, right) => {
27+
const a = `${left.name}@${left.version}`;
28+
const b = `${right.name}@${right.version}`;
29+
return a < b ? -1 : a > b ? 1 : 0;
30+
});
2931
for (const dependency of sortedDependencies) {
3032
if (!dependency.license || !dependency.licenseText?.trim()) {
3133
throw new Error(
@@ -35,8 +37,20 @@ export function bundledLicenseNotices(root: string, outputDirectory: string) {
3537
const references = [reference(dependency.licenseText)];
3638
if (dependency.noticeText?.trim()) references.push(reference(dependency.noticeText));
3739
if (dependency.name === 'jpeg-js') {
40+
const sourceNotices = fs.readFileSync(
41+
path.join(root, 'LICENSES/jpeg-js.txt'),
42+
'utf8',
43+
);
44+
const noticeVersion = sourceNotices
45+
.split(/\r?\n/, 1)[0]
46+
?.match(/^jpeg-js (\S+) source-file notices$/)?.[1];
47+
if (noticeVersion !== dependency.version) {
48+
throw new Error(
49+
`Refresh LICENSES/jpeg-js.txt for bundled jpeg-js@${dependency.version}; its first line must name that version.`,
50+
);
51+
}
3852
references.push(
39-
reference(fs.readFileSync(path.join(root, 'LICENSES/jpeg-js.txt'), 'utf8')),
53+
reference(sourceNotices),
4054
reference(fs.readFileSync(path.join(root, 'LICENSES/Apache-2.0.txt'), 'utf8')),
4155
);
4256
}

0 commit comments

Comments
 (0)