Remove bare switcher & update devcontainer files
This commit is contained in:
parent
6bbdaa921c
commit
3288b37be7
9 changed files with 597 additions and 858 deletions
|
|
@ -1,14 +1,12 @@
|
|||
# [Choice] Node.js version (use -bullseye variants on local arm64/Apple Silicon): 18, 16, 14, 18-bullseye, 16-bullseye, 14-bullseye, 18-buster, 16-buster, 14-buster
|
||||
ARG VARIANT=16-bullseye
|
||||
FROM mcr.microsoft.com/vscode/devcontainers/javascript-node:0-${VARIANT}
|
||||
FROM node:18
|
||||
|
||||
# [Optional] Uncomment this section to install additional OS packages.
|
||||
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
|
||||
# && apt-get -y install --no-install-recommends <your-package-list-here>
|
||||
# Install basic development tools
|
||||
RUN apt update && apt install -y less man-db sudo
|
||||
|
||||
# [Optional] Uncomment if you want to install an additional version of node using nvm
|
||||
# ARG EXTRA_NODE_VERSION=10
|
||||
# RUN su node -c "source /usr/local/share/nvm/nvm.sh && nvm install ${EXTRA_NODE_VERSION}"
|
||||
# Ensure default `node` user has access to `sudo`
|
||||
ARG USERNAME=node
|
||||
RUN echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
|
||||
&& chmod 0440 /etc/sudoers.d/$USERNAME
|
||||
|
||||
# [Optional] Uncomment if you want to install more global node modules
|
||||
# RUN su node -c "npm install -g <your-package-list-here>"
|
||||
# Set `DEVCONTAINER` environment variable to help with orientation
|
||||
ENV DEVCONTAINER=true
|
||||
|
|
|
|||
|
|
@ -1,55 +0,0 @@
|
|||
# [Choice] Node.js version (use -bullseye variants on local arm64/Apple Silicon): 18-bullseye, 16-bullseye, 14-bullseye, 18-buster, 16-buster, 14-buster
|
||||
ARG VARIANT=16-bullseye
|
||||
FROM node:${VARIANT}
|
||||
|
||||
# [Option] Install zsh
|
||||
ARG INSTALL_ZSH="true"
|
||||
# [Option] Upgrade OS packages to their latest versions
|
||||
ARG UPGRADE_PACKAGES="true"
|
||||
|
||||
# Install needed packages, yarn, nvm and setup non-root user. Use a separate RUN statement to add your own dependencies.
|
||||
ARG USERNAME=node
|
||||
ARG USER_UID=1000
|
||||
ARG USER_GID=$USER_UID
|
||||
ARG NPM_GLOBAL=/usr/local/share/npm-global
|
||||
ENV NVM_DIR=/usr/local/share/nvm
|
||||
ENV NVM_SYMLINK_CURRENT=true \
|
||||
PATH=${NPM_GLOBAL}/bin:${NVM_DIR}/current/bin:${PATH}
|
||||
COPY library-scripts/*.sh library-scripts/*.env /tmp/library-scripts/
|
||||
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
|
||||
# Remove imagemagick due to https://security-tracker.debian.org/tracker/CVE-2019-10131
|
||||
&& apt-get purge -y imagemagick imagemagick-6-common \
|
||||
# Install common packages, non-root user, update yarn and install nvm
|
||||
&& bash /tmp/library-scripts/common-debian.sh "${INSTALL_ZSH}" "${USERNAME}" "${USER_UID}" "${USER_GID}" "${UPGRADE_PACKAGES}" "true" "true" \
|
||||
# Install yarn, nvm
|
||||
&& rm -rf /opt/yarn-* /usr/local/bin/yarn /usr/local/bin/yarnpkg \
|
||||
&& bash /tmp/library-scripts/node-debian.sh "${NVM_DIR}" "none" "${USERNAME}" \
|
||||
# Configure global npm install location, use group to adapt to UID/GID changes
|
||||
&& if ! cat /etc/group | grep -e "^npm:" > /dev/null 2>&1; then groupadd -r npm; fi \
|
||||
&& usermod -a -G npm ${USERNAME} \
|
||||
&& umask 0002 \
|
||||
&& mkdir -p ${NPM_GLOBAL} \
|
||||
&& touch /usr/local/etc/npmrc \
|
||||
&& chown ${USERNAME}:npm ${NPM_GLOBAL} /usr/local/etc/npmrc \
|
||||
&& chmod g+s ${NPM_GLOBAL} \
|
||||
&& npm config -g set prefix ${NPM_GLOBAL} \
|
||||
&& sudo -u ${USERNAME} npm config -g set prefix ${NPM_GLOBAL} \
|
||||
# Install eslint
|
||||
&& su ${USERNAME} -c "umask 0002 && npm install -g eslint" \
|
||||
&& npm cache clean --force > /dev/null 2>&1 \
|
||||
# Install python-is-python3 on bullseye to prevent node-gyp regressions
|
||||
&& . /etc/os-release \
|
||||
&& if [ "${VERSION_CODENAME}" = "bullseye" ]; then apt-get -y install --no-install-recommends python-is-python3; fi \
|
||||
# Clean up
|
||||
&& apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/* /root/.gnupg /tmp/library-scripts
|
||||
|
||||
# [Optional] Uncomment this section to install additional OS packages.
|
||||
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
|
||||
# && apt-get -y install --no-install-recommends <your-package-list-here>
|
||||
|
||||
# [Optional] Uncomment if you want to install an additional version of node using nvm
|
||||
# ARG EXTRA_NODE_VERSION=10
|
||||
# RUN su node -c "source /usr/local/share/nvm/nvm.sh && nvm install ${EXTRA_NODE_VERSION}"
|
||||
|
||||
# [Optional] Uncomment if you want to install more global node modules
|
||||
# RUN su node -c "npm install -g <your-package-list-here>""
|
||||
|
|
@ -1,32 +1,9 @@
|
|||
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
|
||||
// https://github.com/microsoft/vscode-dev-containers/tree/v0.245.2/containers/javascript-node
|
||||
// See https://containers.dev/implementors/json_reference/ for configuration reference
|
||||
{
|
||||
"name": "Node.js",
|
||||
"name": "Nebula",
|
||||
"build": {
|
||||
"dockerfile": "Dockerfile",
|
||||
// Update 'VARIANT' to pick a Node version: 18, 16, 14.
|
||||
// Append -bullseye or -buster to pin to an OS version.
|
||||
// Use -bullseye variants on local arm64/Apple Silicon.
|
||||
"args": { "VARIANT": "16-bullseye" }
|
||||
"dockerfile": "Dockerfile"
|
||||
},
|
||||
|
||||
// Configure tool-specific properties.
|
||||
"customizations": {
|
||||
// Configure properties specific to VS Code.
|
||||
"vscode": {
|
||||
// Add the IDs of extensions you want installed when the container is created.
|
||||
"extensions": [
|
||||
"dbaeumer.vscode-eslint"
|
||||
]
|
||||
}
|
||||
},
|
||||
|
||||
// Use 'forwardPorts' to make a list of ports inside the container available locally.
|
||||
// "forwardPorts": [],
|
||||
|
||||
// Use 'postCreateCommand' to run commands after the container is created.
|
||||
"postCreateCommand": "npm install",
|
||||
|
||||
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
|
||||
"remoteUser": "node"
|
||||
"remoteUser": "node",
|
||||
"postCreateCommand": "npm install"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,6 @@
|
|||
<!-- Embed -->
|
||||
|
||||
<script src="../uv/uv.bundle.js"></script>
|
||||
<script src="../resources/appModules/BareSwitcher.js"></script>
|
||||
<script src="../resources/options.js"></script>
|
||||
<meta name="theme-color" content="#5a189a">
|
||||
<meta property=og:title content=Nebula>
|
||||
|
|
@ -141,13 +140,6 @@
|
|||
<label disabled for="ssuggestion" style="background: grey;">
|
||||
</label>
|
||||
</div>
|
||||
<div class="settings-cont">
|
||||
<p class="name" style="font-size: 24px;"> Bare location </p>
|
||||
<p class="description">Choose the location where a bare server is hosted. </p>
|
||||
<input id="bareLocationInput" class="bareLocationInput" type="url">
|
||||
<p id="validIndicator" class="bareValidSignal" > </p>
|
||||
</label>
|
||||
</div>
|
||||
<div class="settings-cont">
|
||||
<p class="name"> Misc </p>
|
||||
<p class="description"></p>
|
||||
|
|
@ -187,11 +179,6 @@ if (cloakStored == "on") {
|
|||
}
|
||||
themeSelector.value = themeStored
|
||||
proxySel.value = proxyStored
|
||||
|
||||
getBareLocation().then(bareLocation => {
|
||||
document.getElementById("bareLocationInput").value = bareLocation;
|
||||
});
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
|||
|
|
@ -1,103 +0,0 @@
|
|||
// NOTE: THIS FILE MUST BE ACCOMPANIED BY THE ULTRAVIOLET BUNDLE BECAUSE IT CONTAINS THE INDEXEDDB LIBRARY
|
||||
|
||||
const dbPromise = Ultraviolet.openDB('keyval-store', 1, {
|
||||
upgrade (db) {
|
||||
db.createObjectStore('keyval')
|
||||
}
|
||||
})
|
||||
|
||||
function getBareLocation () {
|
||||
return dbPromise
|
||||
.then(db => db.get('keyval', 'bareLocation'))
|
||||
.then(value => value || '')
|
||||
}
|
||||
|
||||
self.storage = {
|
||||
async get (key) {
|
||||
return (await dbPromise).get('keyval', key)
|
||||
},
|
||||
|
||||
async set (key, val) {
|
||||
return (await dbPromise).put('keyval', val, key)
|
||||
},
|
||||
|
||||
async del (key) {
|
||||
return (await dbPromise).delete('keyval', key)
|
||||
}
|
||||
}
|
||||
|
||||
function setBareLocation (location) {
|
||||
if (
|
||||
/^http(s?):\/\//.test(location) ||
|
||||
(location.includes('.') && val.substr(0, 1) !== ' ') ||
|
||||
location.includes('/bare/')
|
||||
) {
|
||||
storage.set('bareLocation', location)
|
||||
return 'Bare is located at: ' + location
|
||||
} else {
|
||||
console.log(
|
||||
'Invalid Location provided, please provide a server in the format of http(s)://server.domain.com/'
|
||||
)
|
||||
return 'Invalid Location provided'
|
||||
}
|
||||
}
|
||||
|
||||
function bareValidator (bareLocation) {
|
||||
try {
|
||||
// open a request to the bare location
|
||||
var xmlHttp = new XMLHttpRequest()
|
||||
xmlHttp.open('GET', bareLocation, false) // false for synchronous request
|
||||
xmlHttp.send(null)
|
||||
const _response = xmlHttp.responseText
|
||||
// turn the response text into json
|
||||
|
||||
const response = JSON.parse(_response)
|
||||
|
||||
if (response.project.name === 'bare-server-node') {
|
||||
console.log('Bare located at: ' + bareLocation + '')
|
||||
return true
|
||||
} else {
|
||||
console.error('Bare not found at: ' + bareLocation)
|
||||
return false
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(
|
||||
'An error occured while attempting to identify the bare server at: ' +
|
||||
bareLocation
|
||||
)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener('load', () => {
|
||||
console.log('Loaded ')
|
||||
const _loc = document.getElementById('bareLocationInput')
|
||||
const indicator = document.getElementById('validIndicator')
|
||||
|
||||
// wait 3 seconds
|
||||
setTimeout(() => {
|
||||
if (bareValidator(_loc.value) === true) {
|
||||
indicator.innerText = 'Connected to server: ' + _loc.value
|
||||
indicator.style.color = '#42f851'
|
||||
} else if (bareValidator(_loc.value) === false) {
|
||||
indicator.innerText = 'Could not connect to server: ' + _loc.value
|
||||
indicator.style.color = '#f45145bd'
|
||||
}
|
||||
}, 1000)
|
||||
|
||||
document
|
||||
.getElementById('bareLocationInput')
|
||||
.addEventListener('keydown', function (event) {
|
||||
if (event.key === 'Enter') {
|
||||
if (bareValidator(_loc.value) === true) {
|
||||
indicator.innerText = 'Connected to server: ' + _loc.value
|
||||
indicator.style.color = '#42f851'
|
||||
setBareLocation(_loc.value)
|
||||
} else if (bareValidator(_loc.value) === false) {
|
||||
_loc.value = ''
|
||||
indicator.innerText = 'Could not connect to server: ' + _loc.value
|
||||
indicator.style.color = '#f45145bd'
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
// NOTE - This file is not used in the current version of the app.
|
||||
// this is just a snippet of code that I am keeping for future reference.
|
||||
// NOTE: THIS FILE MUST BE ACCOMPANIED BY THE ULTRAVIOLET BUNDLE BECAUSE IT CONTAINS THE INDEXEDDB LIBRARY
|
||||
|
||||
|
||||
const dbPromise = Ultraviolet.openDB('keyval-store', 1, {
|
||||
upgrade (db) {
|
||||
db.createObjectStore('keyval')
|
||||
}
|
||||
})
|
||||
|
||||
self.storage = {
|
||||
async get (key) {
|
||||
return (await dbPromise).get('keyval', key)
|
||||
},
|
||||
|
||||
async set (key, val) {
|
||||
return (await dbPromise).put('keyval', val, key)
|
||||
},
|
||||
|
||||
async del (key) {
|
||||
return (await dbPromise).delete('keyval', key)
|
||||
}
|
||||
}
|
||||
|
|
@ -40,26 +40,6 @@ window.addEventListener('load', () => {
|
|||
}
|
||||
}
|
||||
|
||||
function getBareLocation () {
|
||||
return dbPromise
|
||||
.then(db => db.get('keyval', 'bareLocation'))
|
||||
.then(value => value || '')
|
||||
}
|
||||
|
||||
getBareLocation().then(bareLocation => {
|
||||
|
||||
console.log('Bare Location: ' + bareLocation)
|
||||
if (bareLocation === '') {
|
||||
console.log('No bare location found, automatically setting to default')
|
||||
storage.set('bareLocation', '/bare/')
|
||||
|
||||
}
|
||||
|
||||
// if bare location is not only whitespace
|
||||
// if (bareLocation.trim() !== '') {
|
||||
|
||||
})
|
||||
|
||||
navigator.serviceWorker.register('./sw.js', {
|
||||
scope: '/service/'
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
self.__uv$config = {
|
||||
prefix: '/service/go/',
|
||||
bare: '',
|
||||
bare: '/bare/',
|
||||
encodeUrl: Ultraviolet.codec.xor.encode,
|
||||
decodeUrl: Ultraviolet.codec.xor.decode,
|
||||
handler: '/uv/uv.handler.js',
|
||||
|
|
|
|||
|
|
@ -1,28 +1,11 @@
|
|||
importScripts('/uv/uv.bundle.js');
|
||||
importScripts('/uv/uv.config.js');
|
||||
|
||||
importScripts("/uv/uv.bundle.js");
|
||||
importScripts("/uv/uv.config.js");
|
||||
|
||||
class UVServiceWorker extends EventEmitter {
|
||||
constructor(config = __uv$config) {
|
||||
super();
|
||||
|
||||
const dbPromise = Ultraviolet.openDB('keyval-store', 1, {
|
||||
upgrade(db) {
|
||||
db.createObjectStore('keyval');
|
||||
},
|
||||
});
|
||||
|
||||
function getBareLocation() {
|
||||
return dbPromise.then(db => db.get('keyval', 'bareLocation')).then(value => value || '');
|
||||
}
|
||||
getBareLocation().then(bareLocation => {
|
||||
if (!config.bare) config.bare = bareLocation;
|
||||
this.addresses = Array.isArray(config.bare)
|
||||
? config.bare.map(str => new URL(str, location))
|
||||
: typeof config.bare === 'string'
|
||||
? [new URL(config.bare, location)]
|
||||
: [];
|
||||
});
|
||||
if (!config.bare) config.bare = '/bare/';
|
||||
this.addresses = typeof config.bare === 'string' ? [ new URL(config.bare, location) ] : config.bare.map(str => new URL(str, location));
|
||||
this.headers = {
|
||||
csp: [
|
||||
'cross-origin-embedder-policy',
|
||||
|
|
@ -205,12 +188,8 @@ getBareLocation().then(bareLocation => {
|
|||
});
|
||||
|
||||
} catch(err) {
|
||||
console.error(err);
|
||||
return new Response(err.toString(), {
|
||||
status: 500,
|
||||
headers: {
|
||||
'x-uv-error': err,
|
||||
}
|
||||
});
|
||||
};
|
||||
};
|
||||
|
|
@ -338,35 +317,35 @@ class HookEvent {
|
|||
|
||||
var R = typeof Reflect === 'object' ? Reflect : null
|
||||
var ReflectApply = R && typeof R.apply === 'function'
|
||||
? R.apply
|
||||
: function ReflectApply(target, receiver, args) {
|
||||
? R.apply
|
||||
: function ReflectApply(target, receiver, args) {
|
||||
return Function.prototype.apply.call(target, receiver, args);
|
||||
}
|
||||
}
|
||||
|
||||
var ReflectOwnKeys
|
||||
if (R && typeof R.ownKeys === 'function') {
|
||||
ReflectOwnKeys = R.ownKeys
|
||||
ReflectOwnKeys = R.ownKeys
|
||||
} else if (Object.getOwnPropertySymbols) {
|
||||
ReflectOwnKeys = function ReflectOwnKeys(target) {
|
||||
ReflectOwnKeys = function ReflectOwnKeys(target) {
|
||||
return Object.getOwnPropertyNames(target)
|
||||
.concat(Object.getOwnPropertySymbols(target));
|
||||
};
|
||||
};
|
||||
} else {
|
||||
ReflectOwnKeys = function ReflectOwnKeys(target) {
|
||||
ReflectOwnKeys = function ReflectOwnKeys(target) {
|
||||
return Object.getOwnPropertyNames(target);
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
function ProcessEmitWarning(warning) {
|
||||
if (console && console.warn) console.warn(warning);
|
||||
if (console && console.warn) console.warn(warning);
|
||||
}
|
||||
|
||||
var NumberIsNaN = Number.isNaN || function NumberIsNaN(value) {
|
||||
return value !== value;
|
||||
return value !== value;
|
||||
}
|
||||
|
||||
function EventEmitter() {
|
||||
EventEmitter.init.call(this);
|
||||
EventEmitter.init.call(this);
|
||||
}
|
||||
|
||||
// Backwards-compat with node 0.10.x
|
||||
|
|
@ -381,68 +360,68 @@ EventEmitter.prototype._maxListeners = undefined;
|
|||
var defaultMaxListeners = 10;
|
||||
|
||||
function checkListener(listener) {
|
||||
if (typeof listener !== 'function') {
|
||||
if (typeof listener !== 'function') {
|
||||
throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof listener);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return defaultMaxListeners;
|
||||
},
|
||||
set: function(arg) {
|
||||
},
|
||||
set: function(arg) {
|
||||
if (typeof arg !== 'number' || arg < 0 || NumberIsNaN(arg)) {
|
||||
throw new RangeError('The value of "defaultMaxListeners" is out of range. It must be a non-negative number. Received ' + arg + '.');
|
||||
}
|
||||
defaultMaxListeners = arg;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
EventEmitter.init = function() {
|
||||
|
||||
if (this._events === undefined ||
|
||||
if (this._events === undefined ||
|
||||
this._events === Object.getPrototypeOf(this)._events) {
|
||||
this._events = Object.create(null);
|
||||
this._eventsCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
this._maxListeners = this._maxListeners || undefined;
|
||||
this._maxListeners = this._maxListeners || undefined;
|
||||
};
|
||||
|
||||
// Obviously not all Emitters should be limited to 10. This function allows
|
||||
// that to be increased. Set to zero for unlimited.
|
||||
EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
|
||||
if (typeof n !== 'number' || n < 0 || NumberIsNaN(n)) {
|
||||
if (typeof n !== 'number' || n < 0 || NumberIsNaN(n)) {
|
||||
throw new RangeError('The value of "n" is out of range. It must be a non-negative number. Received ' + n + '.');
|
||||
}
|
||||
this._maxListeners = n;
|
||||
return this;
|
||||
}
|
||||
this._maxListeners = n;
|
||||
return this;
|
||||
};
|
||||
|
||||
function _getMaxListeners(that) {
|
||||
if (that._maxListeners === undefined)
|
||||
if (that._maxListeners === undefined)
|
||||
return EventEmitter.defaultMaxListeners;
|
||||
return that._maxListeners;
|
||||
return that._maxListeners;
|
||||
}
|
||||
|
||||
EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
|
||||
return _getMaxListeners(this);
|
||||
return _getMaxListeners(this);
|
||||
};
|
||||
|
||||
EventEmitter.prototype.emit = function emit(type) {
|
||||
var args = [];
|
||||
for (var i = 1; i < arguments.length; i++) args.push(arguments[i]);
|
||||
var doError = (type === 'error');
|
||||
var args = [];
|
||||
for (var i = 1; i < arguments.length; i++) args.push(arguments[i]);
|
||||
var doError = (type === 'error');
|
||||
|
||||
var events = this._events;
|
||||
if (events !== undefined)
|
||||
var events = this._events;
|
||||
if (events !== undefined)
|
||||
doError = (doError && events.error === undefined);
|
||||
else if (!doError)
|
||||
else if (!doError)
|
||||
return false;
|
||||
|
||||
// If there is no 'error' event listener then throw.
|
||||
if (doError) {
|
||||
// If there is no 'error' event listener then throw.
|
||||
if (doError) {
|
||||
var er;
|
||||
if (args.length > 0)
|
||||
er = args[0];
|
||||
|
|
@ -455,37 +434,37 @@ EventEmitter.prototype.emit = function emit(type) {
|
|||
var err = new Error('Unhandled error.' + (er ? ' (' + er.message + ')' : ''));
|
||||
err.context = er;
|
||||
throw err; // Unhandled 'error' event
|
||||
}
|
||||
}
|
||||
|
||||
var handler = events[type];
|
||||
var handler = events[type];
|
||||
|
||||
if (handler === undefined)
|
||||
if (handler === undefined)
|
||||
return false;
|
||||
|
||||
if (typeof handler === 'function') {
|
||||
if (typeof handler === 'function') {
|
||||
ReflectApply(handler, this, args);
|
||||
} else {
|
||||
} else {
|
||||
var len = handler.length;
|
||||
var listeners = arrayClone(handler, len);
|
||||
for (var i = 0; i < len; ++i)
|
||||
ReflectApply(listeners[i], this, args);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
return true;
|
||||
};
|
||||
|
||||
function _addListener(target, type, listener, prepend) {
|
||||
var m;
|
||||
var events;
|
||||
var existing;
|
||||
var m;
|
||||
var events;
|
||||
var existing;
|
||||
|
||||
checkListener(listener);
|
||||
checkListener(listener);
|
||||
|
||||
events = target._events;
|
||||
if (events === undefined) {
|
||||
events = target._events;
|
||||
if (events === undefined) {
|
||||
events = target._events = Object.create(null);
|
||||
target._eventsCount = 0;
|
||||
} else {
|
||||
} else {
|
||||
// To avoid recursion in the case that type === "newListener"! Before
|
||||
// adding it to the listeners, first emit "newListener".
|
||||
if (events.newListener !== undefined) {
|
||||
|
|
@ -497,13 +476,13 @@ function _addListener(target, type, listener, prepend) {
|
|||
events = target._events;
|
||||
}
|
||||
existing = events[type];
|
||||
}
|
||||
}
|
||||
|
||||
if (existing === undefined) {
|
||||
if (existing === undefined) {
|
||||
// Optimize the case of one listener. Don't need the extra array object.
|
||||
existing = events[type] = listener;
|
||||
++target._eventsCount;
|
||||
} else {
|
||||
} else {
|
||||
if (typeof existing === 'function') {
|
||||
// Adding the second element, need to change to array.
|
||||
existing = events[type] =
|
||||
|
|
@ -531,13 +510,13 @@ function _addListener(target, type, listener, prepend) {
|
|||
w.count = existing.length;
|
||||
ProcessEmitWarning(w);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return target;
|
||||
return target;
|
||||
}
|
||||
|
||||
EventEmitter.prototype.addListener = function addListener(type, listener) {
|
||||
return _addListener(this, type, listener, false);
|
||||
return _addListener(this, type, listener, false);
|
||||
};
|
||||
|
||||
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
|
||||
|
|
@ -548,27 +527,27 @@ EventEmitter.prototype.prependListener =
|
|||
};
|
||||
|
||||
function onceWrapper() {
|
||||
if (!this.fired) {
|
||||
if (!this.fired) {
|
||||
this.target.removeListener(this.type, this.wrapFn);
|
||||
this.fired = true;
|
||||
if (arguments.length === 0)
|
||||
return this.listener.call(this.target);
|
||||
return this.listener.apply(this.target, arguments);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function _onceWrap(target, type, listener) {
|
||||
var state = { fired: false, wrapFn: undefined, target: target, type: type, listener: listener };
|
||||
var wrapped = onceWrapper.bind(state);
|
||||
wrapped.listener = listener;
|
||||
state.wrapFn = wrapped;
|
||||
return wrapped;
|
||||
var state = { fired: false, wrapFn: undefined, target: target, type: type, listener: listener };
|
||||
var wrapped = onceWrapper.bind(state);
|
||||
wrapped.listener = listener;
|
||||
state.wrapFn = wrapped;
|
||||
return wrapped;
|
||||
}
|
||||
|
||||
EventEmitter.prototype.once = function once(type, listener) {
|
||||
checkListener(listener);
|
||||
this.on(type, _onceWrap(this, type, listener));
|
||||
return this;
|
||||
checkListener(listener);
|
||||
this.on(type, _onceWrap(this, type, listener));
|
||||
return this;
|
||||
};
|
||||
|
||||
EventEmitter.prototype.prependOnceListener =
|
||||
|
|
@ -685,43 +664,43 @@ EventEmitter.prototype.removeAllListeners =
|
|||
};
|
||||
|
||||
function _listeners(target, type, unwrap) {
|
||||
var events = target._events;
|
||||
var events = target._events;
|
||||
|
||||
if (events === undefined)
|
||||
if (events === undefined)
|
||||
return [];
|
||||
|
||||
var evlistener = events[type];
|
||||
if (evlistener === undefined)
|
||||
var evlistener = events[type];
|
||||
if (evlistener === undefined)
|
||||
return [];
|
||||
|
||||
if (typeof evlistener === 'function')
|
||||
if (typeof evlistener === 'function')
|
||||
return unwrap ? [evlistener.listener || evlistener] : [evlistener];
|
||||
|
||||
return unwrap ?
|
||||
return unwrap ?
|
||||
unwrapListeners(evlistener) : arrayClone(evlistener, evlistener.length);
|
||||
}
|
||||
|
||||
EventEmitter.prototype.listeners = function listeners(type) {
|
||||
return _listeners(this, type, true);
|
||||
return _listeners(this, type, true);
|
||||
};
|
||||
|
||||
EventEmitter.prototype.rawListeners = function rawListeners(type) {
|
||||
return _listeners(this, type, false);
|
||||
return _listeners(this, type, false);
|
||||
};
|
||||
|
||||
EventEmitter.listenerCount = function(emitter, type) {
|
||||
if (typeof emitter.listenerCount === 'function') {
|
||||
if (typeof emitter.listenerCount === 'function') {
|
||||
return emitter.listenerCount(type);
|
||||
} else {
|
||||
} else {
|
||||
return listenerCount.call(emitter, type);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
EventEmitter.prototype.listenerCount = listenerCount;
|
||||
function listenerCount(type) {
|
||||
var events = this._events;
|
||||
var events = this._events;
|
||||
|
||||
if (events !== undefined) {
|
||||
if (events !== undefined) {
|
||||
var evlistener = events[type];
|
||||
|
||||
if (typeof evlistener === 'function') {
|
||||
|
|
@ -729,38 +708,38 @@ function listenerCount(type) {
|
|||
} else if (evlistener !== undefined) {
|
||||
return evlistener.length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
EventEmitter.prototype.eventNames = function eventNames() {
|
||||
return this._eventsCount > 0 ? ReflectOwnKeys(this._events) : [];
|
||||
return this._eventsCount > 0 ? ReflectOwnKeys(this._events) : [];
|
||||
};
|
||||
|
||||
function arrayClone(arr, n) {
|
||||
var copy = new Array(n);
|
||||
for (var i = 0; i < n; ++i)
|
||||
var copy = new Array(n);
|
||||
for (var i = 0; i < n; ++i)
|
||||
copy[i] = arr[i];
|
||||
return copy;
|
||||
return copy;
|
||||
}
|
||||
|
||||
function spliceOne(list, index) {
|
||||
for (; index + 1 < list.length; index++)
|
||||
for (; index + 1 < list.length; index++)
|
||||
list[index] = list[index + 1];
|
||||
list.pop();
|
||||
list.pop();
|
||||
}
|
||||
|
||||
function unwrapListeners(arr) {
|
||||
var ret = new Array(arr.length);
|
||||
for (var i = 0; i < ret.length; ++i) {
|
||||
var ret = new Array(arr.length);
|
||||
for (var i = 0; i < ret.length; ++i) {
|
||||
ret[i] = arr[i].listener || arr[i];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
function once(emitter, name) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
function errorListener(err) {
|
||||
emitter.removeListener(name, resolver);
|
||||
reject(err);
|
||||
|
|
@ -777,23 +756,23 @@ function once(emitter, name) {
|
|||
if (name !== 'error') {
|
||||
addErrorHandlerIfEventEmitter(emitter, errorListener, { once: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function addErrorHandlerIfEventEmitter(emitter, handler, flags) {
|
||||
if (typeof emitter.on === 'function') {
|
||||
if (typeof emitter.on === 'function') {
|
||||
eventTargetAgnosticAddListener(emitter, 'error', handler, flags);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
|
||||
if (typeof emitter.on === 'function') {
|
||||
if (typeof emitter.on === 'function') {
|
||||
if (flags.once) {
|
||||
emitter.once(name, listener);
|
||||
} else {
|
||||
emitter.on(name, listener);
|
||||
}
|
||||
} else if (typeof emitter.addEventListener === 'function') {
|
||||
} else if (typeof emitter.addEventListener === 'function') {
|
||||
// EventTarget does not have `error` event semantics like Node
|
||||
// EventEmitters, we do not listen for `error` events here.
|
||||
emitter.addEventListener(name, function wrapListener(arg) {
|
||||
|
|
@ -804,7 +783,7 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
|
|||
}
|
||||
listener(arg);
|
||||
});
|
||||
} else {
|
||||
} else {
|
||||
throw new TypeError('The "emitter" argument must be of type EventEmitter. Received type ' + typeof emitter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue