Skip to content

Commit

Permalink
feat: Implement native dependency pruning
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanis committed Oct 15, 2021
1 parent 472c704 commit c2b775c
Show file tree
Hide file tree
Showing 53 changed files with 2,003 additions and 140 deletions.
50 changes: 50 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
File renamed without changes.
File renamed without changes.
Binary file not shown.
Binary file not shown.
98 changes: 58 additions & 40 deletions packages/acceptance-tests/pkg-tests-core/sources/utils/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,62 @@ export type PackageRunDriver = (
opts: RunDriverOptions,
) => Promise<ExecResult>;

export enum RequestType {
Login = `login`,
PackageInfo = `packageInfo`,
PackageTarball = `packageTarball`,
Whoami = `whoami`,
Repository = `repository`,
Publish = `publish`,
}

export type Request = {
type: RequestType.Login;
username: string,
} | {
type: RequestType.PackageInfo;
scope?: string;
localName: string;
} | {
type: RequestType.PackageTarball;
scope?: string;
localName: string;
version?: string;
} | {
type: RequestType.Whoami;
login: Login
} | {
type: RequestType.Repository;
} | {
type: RequestType.Publish;
scope?: string;
localName: string;
};

export interface Login {
username: string;
password: string;
requiresOtp: boolean;
otp?: string;
npmAuthToken: string;
}

let whitelist = new Map();
let recording: Array<Request> | null = null;

export const startRegistryRecording = async (
fn: () => Promise<void>,
) => {
const currentRecording: Array<Request> = [];
recording = currentRecording;

try {
await fn();
return currentRecording;
} finally {
recording = null;
}
};

export const setPackageWhitelist = async (
packages: Map<string, Set<string>>,
Expand Down Expand Up @@ -184,38 +239,6 @@ export const startPackageServer = ({type}: { type: keyof typeof packageServerUrl
if (serverUrl !== null)
return Promise.resolve(serverUrl);

enum RequestType {
Login = `login`,
PackageInfo = `packageInfo`,
PackageTarball = `packageTarball`,
Whoami = `whoami`,
Repository = `repository`,
Publish = `publish`,
}

type Request = {
type: RequestType.Login;
username: string,
} | {
type: RequestType.PackageInfo;
scope?: string;
localName: string;
} | {
type: RequestType.PackageTarball;
scope?: string;
localName: string;
version?: string;
} | {
type: RequestType.Whoami;
login: Login
} | {
type: RequestType.Repository;
} | {
type: RequestType.Publish;
scope?: string;
localName: string;
};

const processors: {[requestType in RequestType]: (parsedRequest: Request, request: IncomingMessage, response: ServerResponse) => Promise<void>} = {
async [RequestType.PackageInfo](parsedRequest, _, response) {
if (parsedRequest.type !== RequestType.PackageInfo)
Expand Down Expand Up @@ -472,14 +495,6 @@ export const startPackageServer = ({type}: { type: keyof typeof packageServerUrl
}
};

interface Login {
username: string;
password: string;
requiresOtp: boolean;
otp?: string;
npmAuthToken: string;
}

const validLogins: Record<string, Login> = {
testUser: {
username: `testUser`,
Expand Down Expand Up @@ -518,6 +533,9 @@ export const startPackageServer = ({type}: { type: keyof typeof packageServerUrl
return;
}

if (recording !== null)
recording.push(parsedRequest);

const {authorization} = req.headers;
if (authorization != null) {
const auth = validAuthorizations.get(authorization);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* @flow */

module.exports = require(`./package.json`);

for (const key of [`dependencies`, `devDependencies`, `peerDependencies`]) {
for (const dep of Object.keys(module.exports[key] || {})) {
// $FlowFixMe The whole point of this file is to be dynamic
module.exports[key][dep] = require(dep);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "native",
"version": "1.0.0",
"dependencies": {
"native-bar-x64": "1.0.0",
"native-foo-x64": "1.0.0",
"native-foo-x86": "1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* @flow */

module.exports = require(`./package.json`);

for (const key of [`dependencies`, `devDependencies`, `peerDependencies`]) {
for (const dep of Object.keys(module.exports[key] || {})) {
// $FlowFixMe The whole point of this file is to be dynamic
module.exports[key][dep] = require(dep);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "native-bar-x64",
"version": "1.0.0",
"os": ["bar"],
"cpu": ["x64"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* @flow */

module.exports = require(`./package.json`);

for (const key of [`dependencies`, `devDependencies`, `peerDependencies`]) {
for (const dep of Object.keys(module.exports[key] || {})) {
// $FlowFixMe The whole point of this file is to be dynamic
module.exports[key][dep] = require(dep);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "native-foo-x64",
"version": "1.0.0",
"os": ["foo"],
"cpu": ["x64"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* @flow */

module.exports = require(`./package.json`);

for (const key of [`dependencies`, `devDependencies`, `peerDependencies`]) {
for (const dep of Object.keys(module.exports[key] || {})) {
// $FlowFixMe The whole point of this file is to be dynamic
module.exports[key][dep] = require(dep);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "native-foo-x86",
"version": "1.0.0",
"os": ["foo"],
"cpu": ["x86"]
}
Loading

0 comments on commit c2b775c

Please sign in to comment.