@interopio/browser-worker
Version:
IO Connect Browser service worker module
1,279 lines (1,255 loc) • 56.8 kB
JavaScript
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
function __generator(thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
}
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};
var serviceWorkerBroadcastChannelName = "glue42-core-worker";
var platformPingTimeoutMS = 3000;
var platformOpenTimeoutMS = 60000;
var dbName = "glue42core";
var dbVersion = 3;
/**
* Wraps values in an `Ok` type.
*
* Example: `ok(5) // => {ok: true, result: 5}`
*/
var ok = function (result) { return ({ ok: true, result: result }); };
/**
* Wraps errors in an `Err` type.
*
* Example: `err('on fire') // => {ok: false, error: 'on fire'}`
*/
var err = function (error) { return ({ ok: false, error: error }); };
/**
* Create a `Promise` that either resolves with the result of `Ok` or rejects
* with the error of `Err`.
*/
var asPromise = function (r) {
return r.ok === true ? Promise.resolve(r.result) : Promise.reject(r.error);
};
/**
* Unwraps a `Result` and returns either the result of an `Ok`, or
* `defaultValue`.
*
* Example:
* ```
* Result.withDefault(5, number().run(json))
* ```
*
* It would be nice if `Decoder` had an instance method that mirrored this
* function. Such a method would look something like this:
* ```
* class Decoder<A> {
* runWithDefault = (defaultValue: A, json: any): A =>
* Result.withDefault(defaultValue, this.run(json));
* }
*
* number().runWithDefault(5, json)
* ```
* Unfortunately, the type of `defaultValue: A` on the method causes issues
* with type inference on the `object` decoder in some situations. While these
* inference issues can be solved by providing the optional type argument for
* `object`s, the extra trouble and confusion doesn't seem worth it.
*/
var withDefault = function (defaultValue, r) {
return r.ok === true ? r.result : defaultValue;
};
/**
* Return the successful result, or throw an error.
*/
var withException = function (r) {
if (r.ok === true) {
return r.result;
}
else {
throw r.error;
}
};
/**
* Apply `f` to the result of an `Ok`, or pass the error through.
*/
var map = function (f, r) {
return r.ok === true ? ok(f(r.result)) : r;
};
/**
* Apply `f` to the result of two `Ok`s, or pass an error through. If both
* `Result`s are errors then the first one is returned.
*/
var map2 = function (f, ar, br) {
return ar.ok === false ? ar :
br.ok === false ? br :
ok(f(ar.result, br.result));
};
/**
* Apply `f` to the error of an `Err`, or pass the success through.
*/
var mapError = function (f, r) {
return r.ok === true ? r : err(f(r.error));
};
/**
* Chain together a sequence of computations that may fail, similar to a
* `Promise`. If the first computation fails then the error will propagate
* through. If it succeeds, then `f` will be applied to the value, returning a
* new `Result`.
*/
var andThen = function (f, r) {
return r.ok === true ? f(r.result) : r;
};
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise */
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function __rest(s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
}
function isEqual(a, b) {
if (a === b) {
return true;
}
if (a === null && b === null) {
return true;
}
if (typeof (a) !== typeof (b)) {
return false;
}
if (typeof (a) === 'object') {
// Array
if (Array.isArray(a)) {
if (!Array.isArray(b)) {
return false;
}
if (a.length !== b.length) {
return false;
}
for (var i = 0; i < a.length; i++) {
if (!isEqual(a[i], b[i])) {
return false;
}
}
return true;
}
// Hash table
var keys = Object.keys(a);
if (keys.length !== Object.keys(b).length) {
return false;
}
for (var i = 0; i < keys.length; i++) {
if (!b.hasOwnProperty(keys[i])) {
return false;
}
if (!isEqual(a[keys[i]], b[keys[i]])) {
return false;
}
}
return true;
}
}
/*
* Helpers
*/
var isJsonArray = function (json) { return Array.isArray(json); };
var isJsonObject = function (json) {
return typeof json === 'object' && json !== null && !isJsonArray(json);
};
var typeString = function (json) {
switch (typeof json) {
case 'string':
return 'a string';
case 'number':
return 'a number';
case 'boolean':
return 'a boolean';
case 'undefined':
return 'undefined';
case 'object':
if (json instanceof Array) {
return 'an array';
}
else if (json === null) {
return 'null';
}
else {
return 'an object';
}
default:
return JSON.stringify(json);
}
};
var expectedGot = function (expected, got) {
return "expected " + expected + ", got " + typeString(got);
};
var printPath = function (paths) {
return paths.map(function (path) { return (typeof path === 'string' ? "." + path : "[" + path + "]"); }).join('');
};
var prependAt = function (newAt, _a) {
var at = _a.at, rest = __rest(_a, ["at"]);
return (__assign({ at: newAt + (at || '') }, rest));
};
/**
* Decoders transform json objects with unknown structure into known and
* verified forms. You can create objects of type `Decoder<A>` with either the
* primitive decoder functions, such as `boolean()` and `string()`, or by
* applying higher-order decoders to the primitives, such as `array(boolean())`
* or `dict(string())`.
*
* Each of the decoder functions are available both as a static method on
* `Decoder` and as a function alias -- for example the string decoder is
* defined at `Decoder.string()`, but is also aliased to `string()`. Using the
* function aliases exported with the library is recommended.
*
* `Decoder` exposes a number of 'run' methods, which all decode json in the
* same way, but communicate success and failure in different ways. The `map`
* and `andThen` methods modify decoders without having to call a 'run' method.
*
* Alternatively, the main decoder `run()` method returns an object of type
* `Result<A, DecoderError>`. This library provides a number of helper
* functions for dealing with the `Result` type, so you can do all the same
* things with a `Result` as with the decoder methods.
*/
var Decoder = /** @class */ (function () {
/**
* The Decoder class constructor is kept private to separate the internal
* `decode` function from the external `run` function. The distinction
* between the two functions is that `decode` returns a
* `Partial<DecoderError>` on failure, which contains an unfinished error
* report. When `run` is called on a decoder, the relevant series of `decode`
* calls is made, and then on failure the resulting `Partial<DecoderError>`
* is turned into a `DecoderError` by filling in the missing information.
*
* While hiding the constructor may seem restrictive, leveraging the
* provided decoder combinators and helper functions such as
* `andThen` and `map` should be enough to build specialized decoders as
* needed.
*/
function Decoder(decode) {
var _this = this;
this.decode = decode;
/**
* Run the decoder and return a `Result` with either the decoded value or a
* `DecoderError` containing the json input, the location of the error, and
* the error message.
*
* Examples:
* ```
* number().run(12)
* // => {ok: true, result: 12}
*
* string().run(9001)
* // =>
* // {
* // ok: false,
* // error: {
* // kind: 'DecoderError',
* // input: 9001,
* // at: 'input',
* // message: 'expected a string, got 9001'
* // }
* // }
* ```
*/
this.run = function (json) {
return mapError(function (error) { return ({
kind: 'DecoderError',
input: json,
at: 'input' + (error.at || ''),
message: error.message || ''
}); }, _this.decode(json));
};
/**
* Run the decoder as a `Promise`.
*/
this.runPromise = function (json) { return asPromise(_this.run(json)); };
/**
* Run the decoder and return the value on success, or throw an exception
* with a formatted error string.
*/
this.runWithException = function (json) { return withException(_this.run(json)); };
/**
* Construct a new decoder that applies a transformation to the decoded
* result. If the decoder succeeds then `f` will be applied to the value. If
* it fails the error will propagated through.
*
* Example:
* ```
* number().map(x => x * 5).run(10)
* // => {ok: true, result: 50}
* ```
*/
this.map = function (f) {
return new Decoder(function (json) { return map(f, _this.decode(json)); });
};
/**
* Chain together a sequence of decoders. The first decoder will run, and
* then the function will determine what decoder to run second. If the result
* of the first decoder succeeds then `f` will be applied to the decoded
* value. If it fails the error will propagate through.
*
* This is a very powerful method -- it can act as both the `map` and `where`
* methods, can improve error messages for edge cases, and can be used to
* make a decoder for custom types.
*
* Example of adding an error message:
* ```
* const versionDecoder = valueAt(['version'], number());
* const infoDecoder3 = object({a: boolean()});
*
* const decoder = versionDecoder.andThen(version => {
* switch (version) {
* case 3:
* return infoDecoder3;
* default:
* return fail(`Unable to decode info, version ${version} is not supported.`);
* }
* });
*
* decoder.run({version: 3, a: true})
* // => {ok: true, result: {a: true}}
*
* decoder.run({version: 5, x: 'abc'})
* // =>
* // {
* // ok: false,
* // error: {... message: 'Unable to decode info, version 5 is not supported.'}
* // }
* ```
*
* Example of decoding a custom type:
* ```
* // nominal type for arrays with a length of at least one
* type NonEmptyArray<T> = T[] & { __nonEmptyArrayBrand__: void };
*
* const nonEmptyArrayDecoder = <T>(values: Decoder<T>): Decoder<NonEmptyArray<T>> =>
* array(values).andThen(arr =>
* arr.length > 0
* ? succeed(createNonEmptyArray(arr))
* : fail(`expected a non-empty array, got an empty array`)
* );
* ```
*/
this.andThen = function (f) {
return new Decoder(function (json) {
return andThen(function (value) { return f(value).decode(json); }, _this.decode(json));
});
};
/**
* Add constraints to a decoder _without_ changing the resulting type. The
* `test` argument is a predicate function which returns true for valid
* inputs. When `test` fails on an input, the decoder fails with the given
* `errorMessage`.
*
* ```
* const chars = (length: number): Decoder<string> =>
* string().where(
* (s: string) => s.length === length,
* `expected a string of length ${length}`
* );
*
* chars(5).run('12345')
* // => {ok: true, result: '12345'}
*
* chars(2).run('HELLO')
* // => {ok: false, error: {... message: 'expected a string of length 2'}}
*
* chars(12).run(true)
* // => {ok: false, error: {... message: 'expected a string, got a boolean'}}
* ```
*/
this.where = function (test, errorMessage) {
return _this.andThen(function (value) { return (test(value) ? Decoder.succeed(value) : Decoder.fail(errorMessage)); });
};
}
/**
* Decoder primitive that validates strings, and fails on all other input.
*/
Decoder.string = function () {
return new Decoder(function (json) {
return typeof json === 'string'
? ok(json)
: err({ message: expectedGot('a string', json) });
});
};
/**
* Decoder primitive that validates numbers, and fails on all other input.
*/
Decoder.number = function () {
return new Decoder(function (json) {
return typeof json === 'number'
? ok(json)
: err({ message: expectedGot('a number', json) });
});
};
/**
* Decoder primitive that validates booleans, and fails on all other input.
*/
Decoder.boolean = function () {
return new Decoder(function (json) {
return typeof json === 'boolean'
? ok(json)
: err({ message: expectedGot('a boolean', json) });
});
};
Decoder.constant = function (value) {
return new Decoder(function (json) {
return isEqual(json, value)
? ok(value)
: err({ message: "expected " + JSON.stringify(value) + ", got " + JSON.stringify(json) });
});
};
Decoder.object = function (decoders) {
return new Decoder(function (json) {
if (isJsonObject(json) && decoders) {
var obj = {};
for (var key in decoders) {
if (decoders.hasOwnProperty(key)) {
var r = decoders[key].decode(json[key]);
if (r.ok === true) {
// tslint:disable-next-line:strict-type-predicates
if (r.result !== undefined) {
obj[key] = r.result;
}
}
else if (json[key] === undefined) {
return err({ message: "the key '" + key + "' is required but was not present" });
}
else {
return err(prependAt("." + key, r.error));
}
}
}
return ok(obj);
}
else if (isJsonObject(json)) {
return ok(json);
}
else {
return err({ message: expectedGot('an object', json) });
}
});
};
Decoder.array = function (decoder) {
return new Decoder(function (json) {
if (isJsonArray(json) && decoder) {
var decodeValue_1 = function (v, i) {
return mapError(function (err$$1) { return prependAt("[" + i + "]", err$$1); }, decoder.decode(v));
};
return json.reduce(function (acc, v, i) {
return map2(function (arr, result) { return arr.concat([result]); }, acc, decodeValue_1(v, i));
}, ok([]));
}
else if (isJsonArray(json)) {
return ok(json);
}
else {
return err({ message: expectedGot('an array', json) });
}
});
};
Decoder.tuple = function (decoders) {
return new Decoder(function (json) {
if (isJsonArray(json)) {
if (json.length !== decoders.length) {
return err({
message: "expected a tuple of length " + decoders.length + ", got one of length " + json.length
});
}
var result = [];
for (var i = 0; i < decoders.length; i++) {
var nth = decoders[i].decode(json[i]);
if (nth.ok) {
result[i] = nth.result;
}
else {
return err(prependAt("[" + i + "]", nth.error));
}
}
return ok(result);
}
else {
return err({ message: expectedGot("a tuple of length " + decoders.length, json) });
}
});
};
Decoder.union = function (ad, bd) {
var decoders = [];
for (var _i = 2; _i < arguments.length; _i++) {
decoders[_i - 2] = arguments[_i];
}
return Decoder.oneOf.apply(Decoder, [ad, bd].concat(decoders));
};
Decoder.intersection = function (ad, bd) {
var ds = [];
for (var _i = 2; _i < arguments.length; _i++) {
ds[_i - 2] = arguments[_i];
}
return new Decoder(function (json) {
return [ad, bd].concat(ds).reduce(function (acc, decoder) { return map2(Object.assign, acc, decoder.decode(json)); }, ok({}));
});
};
/**
* Escape hatch to bypass validation. Always succeeds and types the result as
* `any`. Useful for defining decoders incrementally, particularly for
* complex objects.
*
* Example:
* ```
* interface User {
* name: string;
* complexUserData: ComplexType;
* }
*
* const userDecoder: Decoder<User> = object({
* name: string(),
* complexUserData: anyJson()
* });
* ```
*/
Decoder.anyJson = function () { return new Decoder(function (json) { return ok(json); }); };
/**
* Decoder identity function which always succeeds and types the result as
* `unknown`.
*/
Decoder.unknownJson = function () {
return new Decoder(function (json) { return ok(json); });
};
/**
* Decoder for json objects where the keys are unknown strings, but the values
* should all be of the same type.
*
* Example:
* ```
* dict(number()).run({chocolate: 12, vanilla: 10, mint: 37});
* // => {ok: true, result: {chocolate: 12, vanilla: 10, mint: 37}}
* ```
*/
Decoder.dict = function (decoder) {
return new Decoder(function (json) {
if (isJsonObject(json)) {
var obj = {};
for (var key in json) {
if (json.hasOwnProperty(key)) {
var r = decoder.decode(json[key]);
if (r.ok === true) {
obj[key] = r.result;
}
else {
return err(prependAt("." + key, r.error));
}
}
}
return ok(obj);
}
else {
return err({ message: expectedGot('an object', json) });
}
});
};
/**
* Decoder for values that may be `undefined`. This is primarily helpful for
* decoding interfaces with optional fields.
*
* Example:
* ```
* interface User {
* id: number;
* isOwner?: boolean;
* }
*
* const decoder: Decoder<User> = object({
* id: number(),
* isOwner: optional(boolean())
* });
* ```
*/
Decoder.optional = function (decoder) {
return new Decoder(function (json) { return (json === undefined || json === null ? ok(undefined) : decoder.decode(json)); });
};
/**
* Decoder that attempts to run each decoder in `decoders` and either succeeds
* with the first successful decoder, or fails after all decoders have failed.
*
* Note that `oneOf` expects the decoders to all have the same return type,
* while `union` creates a decoder for the union type of all the input
* decoders.
*
* Examples:
* ```
* oneOf(string(), number().map(String))
* oneOf(constant('start'), constant('stop'), succeed('unknown'))
* ```
*/
Decoder.oneOf = function () {
var decoders = [];
for (var _i = 0; _i < arguments.length; _i++) {
decoders[_i] = arguments[_i];
}
return new Decoder(function (json) {
var errors = [];
for (var i = 0; i < decoders.length; i++) {
var r = decoders[i].decode(json);
if (r.ok === true) {
return r;
}
else {
errors[i] = r.error;
}
}
var errorsList = errors
.map(function (error) { return "at error" + (error.at || '') + ": " + error.message; })
.join('", "');
return err({
message: "expected a value matching one of the decoders, got the errors [\"" + errorsList + "\"]"
});
});
};
/**
* Decoder that always succeeds with either the decoded value, or a fallback
* default value.
*/
Decoder.withDefault = function (defaultValue, decoder) {
return new Decoder(function (json) {
return ok(withDefault(defaultValue, decoder.decode(json)));
});
};
/**
* Decoder that pulls a specific field out of a json structure, instead of
* decoding and returning the full structure. The `paths` array describes the
* object keys and array indices to traverse, so that values can be pulled out
* of a nested structure.
*
* Example:
* ```
* const decoder = valueAt(['a', 'b', 0], string());
*
* decoder.run({a: {b: ['surprise!']}})
* // => {ok: true, result: 'surprise!'}
*
* decoder.run({a: {x: 'cats'}})
* // => {ok: false, error: {... at: 'input.a.b[0]' message: 'path does not exist'}}
* ```
*
* Note that the `decoder` is ran on the value found at the last key in the
* path, even if the last key is not found. This allows the `optional`
* decoder to succeed when appropriate.
* ```
* const optionalDecoder = valueAt(['a', 'b', 'c'], optional(string()));
*
* optionalDecoder.run({a: {b: {c: 'surprise!'}}})
* // => {ok: true, result: 'surprise!'}
*
* optionalDecoder.run({a: {b: 'cats'}})
* // => {ok: false, error: {... at: 'input.a.b.c' message: 'expected an object, got "cats"'}
*
* optionalDecoder.run({a: {b: {z: 1}}})
* // => {ok: true, result: undefined}
* ```
*/
Decoder.valueAt = function (paths, decoder) {
return new Decoder(function (json) {
var jsonAtPath = json;
for (var i = 0; i < paths.length; i++) {
if (jsonAtPath === undefined) {
return err({
at: printPath(paths.slice(0, i + 1)),
message: 'path does not exist'
});
}
else if (typeof paths[i] === 'string' && !isJsonObject(jsonAtPath)) {
return err({
at: printPath(paths.slice(0, i + 1)),
message: expectedGot('an object', jsonAtPath)
});
}
else if (typeof paths[i] === 'number' && !isJsonArray(jsonAtPath)) {
return err({
at: printPath(paths.slice(0, i + 1)),
message: expectedGot('an array', jsonAtPath)
});
}
else {
jsonAtPath = jsonAtPath[paths[i]];
}
}
return mapError(function (error) {
return jsonAtPath === undefined
? { at: printPath(paths), message: 'path does not exist' }
: prependAt(printPath(paths), error);
}, decoder.decode(jsonAtPath));
});
};
/**
* Decoder that ignores the input json and always succeeds with `fixedValue`.
*/
Decoder.succeed = function (fixedValue) {
return new Decoder(function (json) { return ok(fixedValue); });
};
/**
* Decoder that ignores the input json and always fails with `errorMessage`.
*/
Decoder.fail = function (errorMessage) {
return new Decoder(function (json) { return err({ message: errorMessage }); });
};
/**
* Decoder that allows for validating recursive data structures. Unlike with
* functions, decoders assigned to variables can't reference themselves
* before they are fully defined. We can avoid prematurely referencing the
* decoder by wrapping it in a function that won't be called until use, at
* which point the decoder has been defined.
*
* Example:
* ```
* interface Comment {
* msg: string;
* replies: Comment[];
* }
*
* const decoder: Decoder<Comment> = object({
* msg: string(),
* replies: lazy(() => array(decoder))
* });
* ```
*/
Decoder.lazy = function (mkDecoder) {
return new Decoder(function (json) { return mkDecoder().decode(json); });
};
return Decoder;
}());
/* tslint:disable:variable-name */
/** See `Decoder.string` */
var string = Decoder.string;
/** See `Decoder.number` */
Decoder.number;
/** See `Decoder.boolean` */
var boolean = Decoder.boolean;
/** See `Decoder.anyJson` */
var anyJson = Decoder.anyJson;
/** See `Decoder.unknownJson` */
Decoder.unknownJson;
/** See `Decoder.constant` */
Decoder.constant;
/** See `Decoder.object` */
var object = Decoder.object;
/** See `Decoder.array` */
var array = Decoder.array;
/** See `Decoder.tuple` */
Decoder.tuple;
/** See `Decoder.dict` */
Decoder.dict;
/** See `Decoder.optional` */
var optional = Decoder.optional;
/** See `Decoder.oneOf` */
Decoder.oneOf;
/** See `Decoder.union` */
Decoder.union;
/** See `Decoder.intersection` */
Decoder.intersection;
/** See `Decoder.withDefault` */
Decoder.withDefault;
/** See `Decoder.valueAt` */
Decoder.valueAt;
/** See `Decoder.succeed` */
Decoder.succeed;
/** See `Decoder.fail` */
var fail = Decoder.fail;
/** See `Decoder.lazy` */
Decoder.lazy;
/* eslint-disable @typescript-eslint/no-explicit-any */
var nonEmptyStringDecoder = string().where(function (s) { return s.length > 0; }, "Expected a non-empty string");
var functionCheck = function (input, propDescription) {
var providedType = typeof input;
return providedType === "function" ?
anyJson() :
fail("The provided argument as ".concat(propDescription, " should be of type function, provided: ").concat(typeof providedType));
};
var glue42NotificationClickHandlerDecoder = object({
action: nonEmptyStringDecoder,
handler: anyJson().andThen(function (result) { return functionCheck(result, "handler"); })
});
var webWorkerConfigDecoder = object({
platform: optional(object({
url: optional(nonEmptyStringDecoder),
openIfMissing: optional(boolean())
})),
notifications: optional(object({
defaultClick: optional(anyJson().andThen(function (result) { return functionCheck(result, "defaultClick"); })),
actionClicks: optional(array(glue42NotificationClickHandlerDecoder))
}))
});
/* @ts-self-types="./index.d.ts" */
let urlAlphabet =
'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict';
let nanoid = (size = 21) => {
let id = '';
let i = size | 0;
while (i--) {
id += urlAlphabet[(Math.random() * 64) | 0];
}
return id
};
const instanceOfAny = (object, constructors) => constructors.some((c) => object instanceof c);
let idbProxyableTypes;
let cursorAdvanceMethods;
// This is a function to prevent it throwing up in node environments.
function getIdbProxyableTypes() {
return (idbProxyableTypes ||
(idbProxyableTypes = [
IDBDatabase,
IDBObjectStore,
IDBIndex,
IDBCursor,
IDBTransaction,
]));
}
// This is a function to prevent it throwing up in node environments.
function getCursorAdvanceMethods() {
return (cursorAdvanceMethods ||
(cursorAdvanceMethods = [
IDBCursor.prototype.advance,
IDBCursor.prototype.continue,
IDBCursor.prototype.continuePrimaryKey,
]));
}
const cursorRequestMap = new WeakMap();
const transactionDoneMap = new WeakMap();
const transactionStoreNamesMap = new WeakMap();
const transformCache = new WeakMap();
const reverseTransformCache = new WeakMap();
function promisifyRequest(request) {
const promise = new Promise((resolve, reject) => {
const unlisten = () => {
request.removeEventListener('success', success);
request.removeEventListener('error', error);
};
const success = () => {
resolve(wrap(request.result));
unlisten();
};
const error = () => {
reject(request.error);
unlisten();
};
request.addEventListener('success', success);
request.addEventListener('error', error);
});
promise
.then((value) => {
// Since cursoring reuses the IDBRequest (*sigh*), we cache it for later retrieval
// (see wrapFunction).
if (value instanceof IDBCursor) {
cursorRequestMap.set(value, request);
}
// Catching to avoid "Uncaught Promise exceptions"
})
.catch(() => { });
// This mapping exists in reverseTransformCache but doesn't doesn't exist in transformCache. This
// is because we create many promises from a single IDBRequest.
reverseTransformCache.set(promise, request);
return promise;
}
function cacheDonePromiseForTransaction(tx) {
// Early bail if we've already created a done promise for this transaction.
if (transactionDoneMap.has(tx))
return;
const done = new Promise((resolve, reject) => {
const unlisten = () => {
tx.removeEventListener('complete', complete);
tx.removeEventListener('error', error);
tx.removeEventListener('abort', error);
};
const complete = () => {
resolve();
unlisten();
};
const error = () => {
reject(tx.error || new DOMException('AbortError', 'AbortError'));
unlisten();
};
tx.addEventListener('complete', complete);
tx.addEventListener('error', error);
tx.addEventListener('abort', error);
});
// Cache it for later retrieval.
transactionDoneMap.set(tx, done);
}
let idbProxyTraps = {
get(target, prop, receiver) {
if (target instanceof IDBTransaction) {
// Special handling for transaction.done.
if (prop === 'done')
return transactionDoneMap.get(target);
// Polyfill for objectStoreNames because of Edge.
if (prop === 'objectStoreNames') {
return target.objectStoreNames || transactionStoreNamesMap.get(target);
}
// Make tx.store return the only store in the transaction, or undefined if there are many.
if (prop === 'store') {
return receiver.objectStoreNames[1]
? undefined
: receiver.objectStore(receiver.objectStoreNames[0]);
}
}
// Else transform whatever we get back.
return wrap(target[prop]);
},
set(target, prop, value) {
target[prop] = value;
return true;
},
has(target, prop) {
if (target instanceof IDBTransaction &&
(prop === 'done' || prop === 'store')) {
return true;
}
return prop in target;
},
};
function replaceTraps(callback) {
idbProxyTraps = callback(idbProxyTraps);
}
function wrapFunction(func) {
// Due to expected object equality (which is enforced by the caching in `wrap`), we
// only create one new func per func.
// Edge doesn't support objectStoreNames (booo), so we polyfill it here.
if (func === IDBDatabase.prototype.transaction &&
!('objectStoreNames' in IDBTransaction.prototype)) {
return function (storeNames, ...args) {
const tx = func.call(unwrap(this), storeNames, ...args);
transactionStoreNamesMap.set(tx, storeNames.sort ? storeNames.sort() : [storeNames]);
return wrap(tx);
};
}
// Cursor methods are special, as the behaviour is a little more different to standard IDB. In
// IDB, you advance the cursor and wait for a new 'success' on the IDBRequest that gave you the
// cursor. It's kinda like a promise that can resolve with many values. That doesn't make sense
// with real promises, so each advance methods returns a new promise for the cursor object, or
// undefined if the end of the cursor has been reached.
if (getCursorAdvanceMethods().includes(func)) {
return function (...args) {
// Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use
// the original object.
func.apply(unwrap(this), args);
return wrap(cursorRequestMap.get(this));
};
}
return function (...args) {
// Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use
// the original object.
return wrap(func.apply(unwrap(this), args));
};
}
function transformCachableValue(value) {
if (typeof value === 'function')
return wrapFunction(value);
// This doesn't return, it just creates a 'done' promise for the transaction,
// which is later returned for transaction.done (see idbObjectHandler).
if (value instanceof IDBTransaction)
cacheDonePromiseForTransaction(value);
if (instanceOfAny(value, getIdbProxyableTypes()))
return new Proxy(value, idbProxyTraps);
// Return the same value back if we're not going to transform it.
return value;
}
function wrap(value) {
// We sometimes generate multiple promises from a single IDBRequest (eg when cursoring), because
// IDB is weird and a single IDBRequest can yield many responses, so these can't be cached.
if (value instanceof IDBRequest)
return promisifyRequest(value);
// If we've already transformed this value before, reuse the transformed value.
// This is faster, but it also provides object equality.
if (transformCache.has(value))
return transformCache.get(value);
const newValue = transformCachableValue(value);
// Not all types are transformed.
// These may be primitive types, so they can't be WeakMap keys.
if (newValue !== value) {
transformCache.set(value, newValue);
reverseTransformCache.set(newValue, value);
}
return newValue;
}
const unwrap = (value) => reverseTransformCache.get(value);
/**
* Open a database.
*
* @param name Name of the database.
* @param version Schema version.
* @param callbacks Additional callbacks.
*/
function openDB(name, version, { blocked, upgrade, blocking, terminated } = {}) {
const request = indexedDB.open(name, version);
const openPromise = wrap(request);
if (upgrade) {
request.addEventListener('upgradeneeded', (event) => {
upgrade(wrap(request.result), event.oldVersion, event.newVersion, wrap(request.transaction), event);
});
}
if (blocked) {
request.addEventListener('blocked', (event) => blocked(
// Casting due to https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/1405
event.oldVersion, event.newVersion, event));
}
openPromise
.then((db) => {
if (terminated)
db.addEventListener('close', () => terminated());
if (blocking) {
db.addEventListener('versionchange', (event) => blocking(event.oldVersion, event.newVersion, event));
}
})
.catch(() => { });
return openPromise;
}
const readMethods = ['get', 'getKey', 'getAll', 'getAllKeys', 'count'];
const writeMethods = ['put', 'add', 'delete', 'clear'];
const cachedMethods = new Map();
function getMethod(target, prop) {
if (!(target instanceof IDBDatabase &&
!(prop in target) &&
typeof prop === 'string')) {
return;
}
if (cachedMethods.get(prop))
return cachedMethods.get(prop);
const targetFuncName = prop.replace(/FromIndex$/, '');
const useIndex = prop !== targetFuncName;
const isWrite = writeMethods.includes(targetFuncName);
if (
// Bail if the target doesn't exist on the target. Eg, getAll isn't in Edge.
!(targetFuncName in (useIndex ? IDBIndex : IDBObjectStore).prototype) ||
!(isWrite || readMethods.includes(targetFuncName))) {
return;
}
const method = async function (storeName, ...args) {
// isWrite ? 'readwrite' : undefined gzipps better, but fails in Edge :(
const tx = this.transaction(storeName, isWrite ? 'readwrite' : 'readonly');
let target = tx.store;
if (useIndex)
target = target.index(args.shift());
// Must reject if op rejects.
// If it's a write operation, must reject if tx.done rejects.
// Must reject with op rejection first.
// Must resolve with op value.
// Must handle both promises (no unhandled rejections)
return (await Promise.all([
target[targetFuncName](...args),
isWrite && tx.done,
]))[0];
};
cachedMethods.set(prop, method);
return method;
}
replaceTraps((oldTraps) => ({
...oldTraps,
get: (target, prop, receiver) => getMethod(target, prop) || oldTraps.get(target, prop, receiver),
has: (target, prop) => !!getMethod(target, prop) || oldTraps.has(target, prop),
}));
var openDbPromise;
var trimUrlQueryHashAndTrailingSlash = function (url) {
var trimmedQueryHash = url.split("?")[0].split("#")[0];
var trimmedTrailingSlash = trimmedQueryHash.replace(/\/$/, "");
return trimmedTrailingSlash;
};
var startDb = function () {
if (typeof openDbPromise !== "undefined") {
return openDbPromise;
}
openDbPromise = openDB(dbName, dbVersion, {
upgrade: function (db) {
if (!db.objectStoreNames.contains("workspaceLayouts")) {
db.createObjectStore("workspaceLayouts");
}
if (!db.objectStoreNames.contains("globalLayouts")) {
db.createObjectStore("globalLayouts");
}
if (!db.objectStoreNames.contains("serviceWorker")) {
db.createObjectStore("serviceWorker");
}
if (!db.objectStoreNames.contains("prefs")) {
db.createObjectStore("prefs");
}
}
});
return openDbPromise;
};
var checkPlatformOpen = function () {
var checkPromise = new Promise(function (resolve) {
var channel = new BroadcastChannel(serviceWorkerBroadcastChannelName);
var existenceHandler = function (event) {
var data = event.data;
// check to see if somehow I have reference to the client that answers
if (data.pong) {
channel.removeEventListener("message", existenceHandler);
resolve(true);
}
};
channel.addEventListener("message", existenceHandler);
channel.postMessage({ messageType: "ping" });
});
var timeoutPromise = new Promise(function (resolve) { return setTimeout(function () { return resolve(false); }, platformPingTimeoutMS); });
return Promise.race([checkPromise, timeoutPromise]);
};
var getPlatformUrl = function (config) { return __awaiter(void 0, void 0, void 0, function () {
var url_1, db, workerData, url;
var _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
if ((_a = config.platform) === null || _a === void 0 ? void 0 : _a.url) {
url_1 = config.platform.url.split("?")[0].split("#")[0];
console.debug("getting url from config: ".concat(url_1));
return [2 /*return*/, trimUrlQueryHashAndTrailingSlash(url_1)];
}
console.debug("starting the db");
return [4 /*yield*/, startDb()];
case 1:
db = _c.sent();
if (!db.objectStoreNames.contains("serviceWorker")) {
console.warn("there is no service worker store");
return [2 /*return*/];
}
return [4 /*yield*/, db.get("serviceWorker", "workerData")];
case 2:
workerData = _c.sent();
url = (_b = workerData === null || workerData === void 0 ? void 0 : workerData.platformUrl) === null || _b === void 0 ? void 0 : _b.split("?")[0].split("#")[0];
return [2 /*return*/, trimUrlQueryHashAndTrailingSlash(url)];
}
});
}); };
var validateConfig = function (config) {
var _a;
if (config === void 0) { config = {}; }
var validated = webWorkerConfigDecoder.runWithException(config);
if ((_a = validated.platform) === null || _a === void 0 ? void 0 : _a.url) {
validated.platform.url = validated.platform.url.replace(/\/$/, "");
}
return validated;
};
var raiseIONotification = function (settings) { return __awaiter(void 0, void 0, void 0, function () {
var options, glueData;
var _a;
return __generator(this, function (_b) {
options = Object.assign({}, settings, { title: undefined, clickInterop: undefined, actions: undefined });
options.actions = (_a = settings.actions) === null || _a === void 0 ? void 0 : _a.map(function (action) {
return {
action: action.action,
title: action.title,
icon: action.icon
};
});
glueData = {
clickInterop: settings.clickInterop,
actions: settings.actions,
id: nanoid()
};
if (options.data) {
options.data.glueData = glueData;
}
else {
options.data = { glueData: glueData };
}
return [2 /*return*/, self.registration.showNotification(settings.title, options)];
});
}); };
var openBrowserPlatform = function (url) {
return new Promise(function (resolve, reject) {
if (!url) {
return reject("Cannot open the platform, because a url was not provided");
}
var channel = new BroadcastChannel(serviceWorkerBroadcastChannelName);
var openHandler = function (event) {
var data = event.data;
if (data.platformStarted) {
channel.removeEventListener("message", openHandler);
resolve();
}
};
channel.addEventListener("message", openHandler);
self.clients.openWindow(url).catch(reject);
setTimeout(function () { return reject("Timed out waiting for the platform to open and send a ready signal: ".concat(platformOpenTimeoutMS, " MS")); }, platformOpenTimeoutMS);
});
};
var focusCorePlatform = function (url) { return __awaiter(void 0, void 0, void 0, function () {
var allWindows, _i, allWindows_1, client, urlStrippedQueryHash, urlStrippedTrailingSlash;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!url) {
console.warn("Cannot open the platform, because a url was not provided");
return [2 /*return*/];
}
return [4 /*yield*/, self.clients.matchAll({ type: "window" })];
c