
Research
/Security News
Toptal’s GitHub Organization Hijacked: 10 Malicious Packages Published
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
angular-server-side-configuration
Advanced tools
Configure an angular application at runtime on the server via environment variables.
The Angular CLI provides build time configuration (via environment.ts). In a Continuous Delivery environment this is sometimes not enough.
Environment variables are used for configuration. This package provides a script to search for usages in bundled angular files and a script for inserting populated environment variables into index.html file(s) (Missing environment variables will be represented by null). This should be done on the host serving the bundled angular files.
This will not work in Module.forRoot or Module.forChild scripts or parameters. These are build time only due to AOT restrictions.
npm install --save angular-server-side-configuration
Use process.env.NAME in your environment.prod.ts, where NAME is the environment variable that should be used.
export const environment = {
production: process.env.PROD !== 'false',
apiAddress: process.env.API_ADDRESS || 'https://example-api.com'
};
Import angular-server-side-configuration/process
in polyfill.ts. This will enable
the typings for process.env.* in the code and register a fallback value for process.env.
/**
* This file includes polyfills needed by Angular and is loaded before the app.
* You can add your own extra polyfills to this file.
...
/***************************************************************************************************
* APPLICATION IMPORTS
*/
import 'angular-server-side-configuration/process';
EnvironmentVariablesConfiguration
provides a way of looking for usages of process.env.*
in the bundled angular javascript files and provides a way of inserting the environment variables
into the index.html file(s).
...
import { EnvironmentVariablesConfiguration } from 'angular-server-side-configuration';
import * as express from 'express';
...
const app = express();
const envVariables = EnvironmentVariablesConfiguration.searchEnvironmentVariables(bundledAngularFilesRoot);
...
envVariables.insertAndSaveRecursively(bundledAngularFilesRoot)
.then(() => {
app.listen(PORT, () => {
console.log(`Node server listening on http://localhost:${PORT}`);
});
});
Import angular-server-side-configuration/process
in polyfill.ts. This will enable
the typings for process.env.* in the code and register a fallback value for process.env.
import 'angular-server-side-configuration/process';
Accepts an array of environment variables.
const envVariables = new EnvironmentVariablesConfiguration(['PROD', 'API_ADDRESS']);
By default the searchEnvironmentVariables method uses a regex
(/process\s*\.\s*env\s*\.\s*[a-zA-Z0-9_]+/gm
) to look for process.env.* usages.
It is possible to provide a discovery function in the options as a second parameter.
root
The root directory from which to search.
options
Optional options for searching environment variables.
options.filePattern
The file pattern in which environment variables should be searched
(Defaults to /.js$/).
options.environmentVariablesDiscovery
The function to discover environment variables in
the matched files (Defaults to process.env.VARIABLE => VARIABLE).
Returns an instance of EnvironmentVariablesConfiguration.
const envVariables: EnvironmentVariablesConfiguration =
EnvironmentVariablesConfiguration.searchEnvironmentVariables(bundledAngularFilesRoot);
Generates an object, with the environment variable names being the key and the actual values being the values. Missing environment variables will be represented by null.
const envVariables = new EnvironmentVariablesConfiguration(['PROD', 'API_ADDRESS']);
process.env.API_ADDRESS = 'https://example-api.com';
const variables = envVariables.populateVariables();
// { PROD: null, API_ADDRESS: 'https://example-api.com' }
Generates the IIFE in which the environment variables are assigned to window.process.env.
const envVariables = new EnvironmentVariablesConfiguration(['PROD', 'API_ADDRESS']);
process.env.API_ADDRESS = 'https://example-api.com';
const iife = envVariables.generateIIFE();
// (function(self){self.process={env:{"PROD":null,"API_ADDRESS":"https://example-api.com"};})(window)
Inserts the discovered environment variables as an IIFE wrapped in a script tag into the specified file content without saving the file.
file
The file to be read.
options
Optional options for insertion.
options.insertionRegex
The replacement pattern, where the configuration should be inserted
(Defaults to /<!--\s*CONFIG\s*-->/
).
Returns a promise, which resolves to the file content with the environment variables inserted.
const envVariables = EnvironmentVariablesConfiguration.searchEnvironmentVariables(bundledAngularFilesRoot);
const content = await envVariables.apply(pathToIndexHtml);
// <html>...<script>(function(self){self.process={env:{"PROD":null,"API_ADDRESS":"https://example-api.com"};})(window)</script>...</html>
Inserts the discovered environment variables as an IIFE wrapped in a script tag into the specified file.
file
The file into which the environment variables should be inserted.
options
Optional options for insertion.
options.insertionRegex
The replacement pattern, where the configuration should be inserted
(Defaults to /<!--\s*CONFIG\s*-->/
).
Returns a promise, which resolves after the enivornment variables have been saved to the given file.
const envVariables = EnvironmentVariablesConfiguration.searchEnvironmentVariables(bundledAngularFilesRoot);
await envVariables.insertAndSave(pathToIndexHtml);
Inserts the discovered enviornment variables as an IIFE wrapped in a script tag into the matched files.
root
The root directory from which to search insertion files.
options
Optional options for insertion.
options.filePattern
The file pattern in which the configuration should be inserted
(Defaults to /index.html$/).
options.insertionRegex
The replacement pattern, where the configuration should
be inserted (Defaults to /<!--\s*CONFIG\s*-->/
).
Returns a promise, which resolves after the environment variables have been inserted to all matched files.
const envVariables = EnvironmentVariablesConfiguration.searchEnvironmentVariables(bundledAngularFilesRoot);
await envVariables.insertAndSaveRecursively(bundledAngularFilesRoot);
Apache License, Version 2.0
FAQs
Configure an angular application on the server
The npm package angular-server-side-configuration receives a total of 1,551 weekly downloads. As such, angular-server-side-configuration popularity was classified as popular.
We found that angular-server-side-configuration demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
Research
/Security News
Socket researchers investigate 4 malicious npm and PyPI packages with 56,000+ downloads that install surveillance malware.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.