UNPKG

svelte-check

Version:

Svelte Code Checker Terminal Interface

1,547 lines (1,507 loc) 5.5 MB
'use strict'; var require$$0$6 = require('chokidar'); var fs$1 = require('fs'); var path$1 = require('path'); var require$$0$3 = require('node:util'); var require$$0$2 = require('util'); var require$$2 = require('os'); var require$$3 = require('crypto'); var require$$4 = require('net'); var require$$5 = require('url'); var require$$3$1 = require('child_process'); var require$$0$4 = require('events'); var ts = require('typescript'); var require$$1$3 = require('fs/promises'); var require$$0$5 = require('@jridgewell/trace-mapping'); var compiler = require('svelte/compiler'); var require$$0$7 = require('perf_hooks'); var sade = require('sade'); var pc = require('picocolors'); function _interopNamespaceDefault(e) { var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n.default = e; return Object.freeze(n); } var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs$1); var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path$1); var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; function getDefaultExportFromCjs (x) { return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x; } function getAugmentedNamespace(n) { if (n.__esModule) return n; var f = n.default; if (typeof f == "function") { var a = function a () { if (this instanceof a) { var args = [null]; args.push.apply(args, arguments); var Ctor = Function.bind.apply(f, args); return new Ctor(); } return f.apply(this, arguments); }; a.prototype = f.prototype; } else a = {}; Object.defineProperty(a, '__esModule', {value: true}); Object.keys(n).forEach(function (k) { var d = Object.getOwnPropertyDescriptor(n, k); Object.defineProperty(a, k, d.get ? d : { enumerable: true, get: function () { return n[k]; } }); }); return a; } var src$1 = {}; var server$1 = {}; var main$8 = {}; var is$4 = {}; Object.defineProperty(is$4, "__esModule", { value: true }); is$4.thenable = is$4.typedArray = is$4.stringArray = is$4.array = is$4.func = is$4.error = is$4.number = is$4.string = is$4.boolean = void 0; function boolean$2(value) { return value === true || value === false; } is$4.boolean = boolean$2; function string$2(value) { return typeof value === 'string' || value instanceof String; } is$4.string = string$2; function number$2(value) { return typeof value === 'number' || value instanceof Number; } is$4.number = number$2; function error$4(value) { return value instanceof Error; } is$4.error = error$4; function func$2(value) { return typeof value === 'function'; } is$4.func = func$2; function array$2(value) { return Array.isArray(value); } is$4.array = array$2; function stringArray$2(value) { return array$2(value) && value.every(elem => string$2(elem)); } is$4.stringArray = stringArray$2; function typedArray$2(value, check) { return Array.isArray(value) && value.every(check); } is$4.typedArray = typedArray$2; function thenable(value) { return value && func$2(value.then); } is$4.thenable = thenable; var server = {}; var main$7 = {}; var main$6 = {}; var ril$1 = {}; var api$4 = {}; var messages$3 = {}; var is$3 = {}; var hasRequiredIs$1; function requireIs$1 () { if (hasRequiredIs$1) return is$3; hasRequiredIs$1 = 1; Object.defineProperty(is$3, "__esModule", { value: true }); is$3.stringArray = is$3.array = is$3.func = is$3.error = is$3.number = is$3.string = is$3.boolean = void 0; function boolean(value) { return value === true || value === false; } is$3.boolean = boolean; function string(value) { return typeof value === 'string' || value instanceof String; } is$3.string = string; function number(value) { return typeof value === 'number' || value instanceof Number; } is$3.number = number; function error(value) { return value instanceof Error; } is$3.error = error; function func(value) { return typeof value === 'function'; } is$3.func = func; function array(value) { return Array.isArray(value); } is$3.array = array; function stringArray(value) { return array(value) && value.every(elem => string(elem)); } is$3.stringArray = stringArray; return is$3; } var hasRequiredMessages$1; function requireMessages$1 () { if (hasRequiredMessages$1) return messages$3; hasRequiredMessages$1 = 1; Object.defineProperty(messages$3, "__esModule", { value: true }); messages$3.Message = messages$3.NotificationType9 = messages$3.NotificationType8 = messages$3.NotificationType7 = messages$3.NotificationType6 = messages$3.NotificationType5 = messages$3.NotificationType4 = messages$3.NotificationType3 = messages$3.NotificationType2 = messages$3.NotificationType1 = messages$3.NotificationType0 = messages$3.NotificationType = messages$3.RequestType9 = messages$3.RequestType8 = messages$3.RequestType7 = messages$3.RequestType6 = messages$3.RequestType5 = messages$3.RequestType4 = messages$3.RequestType3 = messages$3.RequestType2 = messages$3.RequestType1 = messages$3.RequestType = messages$3.RequestType0 = messages$3.AbstractMessageSignature = messages$3.ParameterStructures = messages$3.ResponseError = messages$3.ErrorCodes = void 0; const is = requireIs$1(); var ErrorCodes; (function (ErrorCodes) { ErrorCodes.ParseError = -32700; ErrorCodes.InvalidRequest = -32600; ErrorCodes.MethodNotFound = -32601; ErrorCodes.InvalidParams = -32602; ErrorCodes.InternalError = -32603; ErrorCodes.jsonrpcReservedErrorRangeStart = -32099; ErrorCodes.serverErrorStart = -32099; ErrorCodes.MessageWriteError = -32099; ErrorCodes.MessageReadError = -32098; ErrorCodes.PendingResponseRejected = -32097; ErrorCodes.ConnectionInactive = -32096; ErrorCodes.ServerNotInitialized = -32002; ErrorCodes.UnknownErrorCode = -32001; ErrorCodes.jsonrpcReservedErrorRangeEnd = -32000; ErrorCodes.serverErrorEnd = -32000; })(ErrorCodes || (messages$3.ErrorCodes = ErrorCodes = {})); class ResponseError extends Error { constructor(code, message, data) { super(message); this.code = is.number(code) ? code : ErrorCodes.UnknownErrorCode; this.data = data; Object.setPrototypeOf(this, ResponseError.prototype); } toJson() { const result = { code: this.code, message: this.message }; if (this.data !== undefined) { result.data = this.data; } return result; } } messages$3.ResponseError = ResponseError; class ParameterStructures { constructor(kind) { this.kind = kind; } static is(value) { return value === ParameterStructures.auto || value === ParameterStructures.byName || value === ParameterStructures.byPosition; } toString() { return this.kind; } } messages$3.ParameterStructures = ParameterStructures; ParameterStructures.auto = new ParameterStructures('auto'); ParameterStructures.byPosition = new ParameterStructures('byPosition'); ParameterStructures.byName = new ParameterStructures('byName'); class AbstractMessageSignature { constructor(method, numberOfParams) { this.method = method; this.numberOfParams = numberOfParams; } get parameterStructures() { return ParameterStructures.auto; } } messages$3.AbstractMessageSignature = AbstractMessageSignature; class RequestType0 extends AbstractMessageSignature { constructor(method) { super(method, 0); } } messages$3.RequestType0 = RequestType0; class RequestType extends AbstractMessageSignature { constructor(method, _parameterStructures = ParameterStructures.auto) { super(method, 1); this._parameterStructures = _parameterStructures; } get parameterStructures() { return this._parameterStructures; } } messages$3.RequestType = RequestType; class RequestType1 extends AbstractMessageSignature { constructor(method, _parameterStructures = ParameterStructures.auto) { super(method, 1); this._parameterStructures = _parameterStructures; } get parameterStructures() { return this._parameterStructures; } } messages$3.RequestType1 = RequestType1; class RequestType2 extends AbstractMessageSignature { constructor(method) { super(method, 2); } } messages$3.RequestType2 = RequestType2; class RequestType3 extends AbstractMessageSignature { constructor(method) { super(method, 3); } } messages$3.RequestType3 = RequestType3; class RequestType4 extends AbstractMessageSignature { constructor(method) { super(method, 4); } } messages$3.RequestType4 = RequestType4; class RequestType5 extends AbstractMessageSignature { constructor(method) { super(method, 5); } } messages$3.RequestType5 = RequestType5; class RequestType6 extends AbstractMessageSignature { constructor(method) { super(method, 6); } } messages$3.RequestType6 = RequestType6; class RequestType7 extends AbstractMessageSignature { constructor(method) { super(method, 7); } } messages$3.RequestType7 = RequestType7; class RequestType8 extends AbstractMessageSignature { constructor(method) { super(method, 8); } } messages$3.RequestType8 = RequestType8; class RequestType9 extends AbstractMessageSignature { constructor(method) { super(method, 9); } } messages$3.RequestType9 = RequestType9; class NotificationType extends AbstractMessageSignature { constructor(method, _parameterStructures = ParameterStructures.auto) { super(method, 1); this._parameterStructures = _parameterStructures; } get parameterStructures() { return this._parameterStructures; } } messages$3.NotificationType = NotificationType; class NotificationType0 extends AbstractMessageSignature { constructor(method) { super(method, 0); } } messages$3.NotificationType0 = NotificationType0; class NotificationType1 extends AbstractMessageSignature { constructor(method, _parameterStructures = ParameterStructures.auto) { super(method, 1); this._parameterStructures = _parameterStructures; } get parameterStructures() { return this._parameterStructures; } } messages$3.NotificationType1 = NotificationType1; class NotificationType2 extends AbstractMessageSignature { constructor(method) { super(method, 2); } } messages$3.NotificationType2 = NotificationType2; class NotificationType3 extends AbstractMessageSignature { constructor(method) { super(method, 3); } } messages$3.NotificationType3 = NotificationType3; class NotificationType4 extends AbstractMessageSignature { constructor(method) { super(method, 4); } } messages$3.NotificationType4 = NotificationType4; class NotificationType5 extends AbstractMessageSignature { constructor(method) { super(method, 5); } } messages$3.NotificationType5 = NotificationType5; class NotificationType6 extends AbstractMessageSignature { constructor(method) { super(method, 6); } } messages$3.NotificationType6 = NotificationType6; class NotificationType7 extends AbstractMessageSignature { constructor(method) { super(method, 7); } } messages$3.NotificationType7 = NotificationType7; class NotificationType8 extends AbstractMessageSignature { constructor(method) { super(method, 8); } } messages$3.NotificationType8 = NotificationType8; class NotificationType9 extends AbstractMessageSignature { constructor(method) { super(method, 9); } } messages$3.NotificationType9 = NotificationType9; var Message; (function (Message) { function isRequest(message) { const candidate = message; return candidate && is.string(candidate.method) && (is.string(candidate.id) || is.number(candidate.id)); } Message.isRequest = isRequest; function isNotification(message) { const candidate = message; return candidate && is.string(candidate.method) && message.id === void 0; } Message.isNotification = isNotification; function isResponse(message) { const candidate = message; return candidate && (candidate.result !== void 0 || !!candidate.error) && (is.string(candidate.id) || is.number(candidate.id) || candidate.id === null); } Message.isResponse = isResponse; })(Message || (messages$3.Message = Message = {})); return messages$3; } var linkedMap$1 = {}; var hasRequiredLinkedMap$1; function requireLinkedMap$1 () { if (hasRequiredLinkedMap$1) return linkedMap$1; hasRequiredLinkedMap$1 = 1; var _a; Object.defineProperty(linkedMap$1, "__esModule", { value: true }); linkedMap$1.LRUCache = linkedMap$1.LinkedMap = linkedMap$1.Touch = void 0; var Touch; (function (Touch) { Touch.None = 0; Touch.First = 1; Touch.AsOld = Touch.First; Touch.Last = 2; Touch.AsNew = Touch.Last; })(Touch || (linkedMap$1.Touch = Touch = {})); class LinkedMap { constructor() { this[_a] = 'LinkedMap'; this._map = new Map(); this._head = undefined; this._tail = undefined; this._size = 0; this._state = 0; } clear() { this._map.clear(); this._head = undefined; this._tail = undefined; this._size = 0; this._state++; } isEmpty() { return !this._head && !this._tail; } get size() { return this._size; } get first() { return this._head?.value; } get last() { return this._tail?.value; } has(key) { return this._map.has(key); } get(key, touch = Touch.None) { const item = this._map.get(key); if (!item) { return undefined; } if (touch !== Touch.None) { this.touch(item, touch); } return item.value; } set(key, value, touch = Touch.None) { let item = this._map.get(key); if (item) { item.value = value; if (touch !== Touch.None) { this.touch(item, touch); } } else { item = { key, value, next: undefined, previous: undefined }; switch (touch) { case Touch.None: this.addItemLast(item); break; case Touch.First: this.addItemFirst(item); break; case Touch.Last: this.addItemLast(item); break; default: this.addItemLast(item); break; } this._map.set(key, item); this._size++; } return this; } delete(key) { return !!this.remove(key); } remove(key) { const item = this._map.get(key); if (!item) { return undefined; } this._map.delete(key); this.removeItem(item); this._size--; return item.value; } shift() { if (!this._head && !this._tail) { return undefined; } if (!this._head || !this._tail) { throw new Error('Invalid list'); } const item = this._head; this._map.delete(item.key); this.removeItem(item); this._size--; return item.value; } forEach(callbackfn, thisArg) { const state = this._state; let current = this._head; while (current) { if (thisArg) { callbackfn.bind(thisArg)(current.value, current.key, this); } else { callbackfn(current.value, current.key, this); } if (this._state !== state) { throw new Error(`LinkedMap got modified during iteration.`); } current = current.next; } } keys() { const state = this._state; let current = this._head; const iterator = { [Symbol.iterator]: () => { return iterator; }, next: () => { if (this._state !== state) { throw new Error(`LinkedMap got modified during iteration.`); } if (current) { const result = { value: current.key, done: false }; current = current.next; return result; } else { return { value: undefined, done: true }; } } }; return iterator; } values() { const state = this._state; let current = this._head; const iterator = { [Symbol.iterator]: () => { return iterator; }, next: () => { if (this._state !== state) { throw new Error(`LinkedMap got modified during iteration.`); } if (current) { const result = { value: current.value, done: false }; current = current.next; return result; } else { return { value: undefined, done: true }; } } }; return iterator; } entries() { const state = this._state; let current = this._head; const iterator = { [Symbol.iterator]: () => { return iterator; }, next: () => { if (this._state !== state) { throw new Error(`LinkedMap got modified during iteration.`); } if (current) { const result = { value: [current.key, current.value], done: false }; current = current.next; return result; } else { return { value: undefined, done: true }; } } }; return iterator; } [(_a = Symbol.toStringTag, Symbol.iterator)]() { return this.entries(); } trimOld(newSize) { if (newSize >= this.size) { return; } if (newSize === 0) { this.clear(); return; } let current = this._head; let currentSize = this.size; while (current && currentSize > newSize) { this._map.delete(current.key); current = current.next; currentSize--; } this._head = current; this._size = currentSize; if (current) { current.previous = undefined; } this._state++; } addItemFirst(item) { if (!this._head && !this._tail) { this._tail = item; } else if (!this._head) { throw new Error('Invalid list'); } else { item.next = this._head; this._head.previous = item; } this._head = item; this._state++; } addItemLast(item) { if (!this._head && !this._tail) { this._head = item; } else if (!this._tail) { throw new Error('Invalid list'); } else { item.previous = this._tail; this._tail.next = item; } this._tail = item; this._state++; } removeItem(item) { if (item === this._head && item === this._tail) { this._head = undefined; this._tail = undefined; } else if (item === this._head) { if (!item.next) { throw new Error('Invalid list'); } item.next.previous = undefined; this._head = item.next; } else if (item === this._tail) { if (!item.previous) { throw new Error('Invalid list'); } item.previous.next = undefined; this._tail = item.previous; } else { const next = item.next; const previous = item.previous; if (!next || !previous) { throw new Error('Invalid list'); } next.previous = previous; previous.next = next; } item.next = undefined; item.previous = undefined; this._state++; } touch(item, touch) { if (!this._head || !this._tail) { throw new Error('Invalid list'); } if ((touch !== Touch.First && touch !== Touch.Last)) { return; } if (touch === Touch.First) { if (item === this._head) { return; } const next = item.next; const previous = item.previous; if (item === this._tail) { previous.next = undefined; this._tail = previous; } else { next.previous = previous; previous.next = next; } item.previous = undefined; item.next = this._head; this._head.previous = item; this._head = item; this._state++; } else if (touch === Touch.Last) { if (item === this._tail) { return; } const next = item.next; const previous = item.previous; if (item === this._head) { next.previous = undefined; this._head = next; } else { next.previous = previous; previous.next = next; } item.next = undefined; item.previous = this._tail; this._tail.next = item; this._tail = item; this._state++; } } toJSON() { const data = []; this.forEach((value, key) => { data.push([key, value]); }); return data; } fromJSON(data) { this.clear(); for (const [key, value] of data) { this.set(key, value); } } } linkedMap$1.LinkedMap = LinkedMap; class LRUCache extends LinkedMap { constructor(limit, ratio = 1) { super(); this._limit = limit; this._ratio = Math.min(Math.max(0, ratio), 1); } get limit() { return this._limit; } set limit(limit) { this._limit = limit; this.checkTrim(); } get ratio() { return this._ratio; } set ratio(ratio) { this._ratio = Math.min(Math.max(0, ratio), 1); this.checkTrim(); } get(key, touch = Touch.AsNew) { return super.get(key, touch); } peek(key) { return super.get(key, Touch.None); } set(key, value) { super.set(key, value, Touch.Last); this.checkTrim(); return this; } checkTrim() { if (this.size > this._limit) { this.trimOld(Math.round(this._limit * this._ratio)); } } } linkedMap$1.LRUCache = LRUCache; return linkedMap$1; } var disposable$1 = {}; var hasRequiredDisposable; function requireDisposable () { if (hasRequiredDisposable) return disposable$1; hasRequiredDisposable = 1; Object.defineProperty(disposable$1, "__esModule", { value: true }); disposable$1.Disposable = void 0; var Disposable; (function (Disposable) { function create(func) { return { dispose: func }; } Disposable.create = create; })(Disposable || (disposable$1.Disposable = Disposable = {})); return disposable$1; } var events$1 = {}; var ral$2 = {}; var hasRequiredRal; function requireRal () { if (hasRequiredRal) return ral$2; hasRequiredRal = 1; Object.defineProperty(ral$2, "__esModule", { value: true }); let _ral; function RAL() { if (_ral === undefined) { throw new Error(`No runtime abstraction layer installed`); } return _ral; } (function (RAL) { function install(ral) { if (ral === undefined) { throw new Error(`No runtime abstraction layer provided`); } _ral = ral; } RAL.install = install; })(RAL || (RAL = {})); ral$2.default = RAL; return ral$2; } var hasRequiredEvents$1; function requireEvents$1 () { if (hasRequiredEvents$1) return events$1; hasRequiredEvents$1 = 1; Object.defineProperty(events$1, "__esModule", { value: true }); events$1.Emitter = events$1.Event = void 0; const ral_1 = requireRal(); var Event; (function (Event) { const _disposable = { dispose() { } }; Event.None = function () { return _disposable; }; })(Event || (events$1.Event = Event = {})); class CallbackList { add(callback, context = null, bucket) { if (!this._callbacks) { this._callbacks = []; this._contexts = []; } this._callbacks.push(callback); this._contexts.push(context); if (Array.isArray(bucket)) { bucket.push({ dispose: () => this.remove(callback, context) }); } } remove(callback, context = null) { if (!this._callbacks) { return; } let foundCallbackWithDifferentContext = false; for (let i = 0, len = this._callbacks.length; i < len; i++) { if (this._callbacks[i] === callback) { if (this._contexts[i] === context) { this._callbacks.splice(i, 1); this._contexts.splice(i, 1); return; } else { foundCallbackWithDifferentContext = true; } } } if (foundCallbackWithDifferentContext) { throw new Error('When adding a listener with a context, you should remove it with the same context'); } } invoke(...args) { if (!this._callbacks) { return []; } const ret = [], callbacks = this._callbacks.slice(0), contexts = this._contexts.slice(0); for (let i = 0, len = callbacks.length; i < len; i++) { try { ret.push(callbacks[i].apply(contexts[i], args)); } catch (e) { (0, ral_1.default)().console.error(e); } } return ret; } isEmpty() { return !this._callbacks || this._callbacks.length === 0; } dispose() { this._callbacks = undefined; this._contexts = undefined; } } class Emitter { constructor(_options) { this._options = _options; } get event() { if (!this._event) { this._event = (listener, thisArgs, disposables) => { if (!this._callbacks) { this._callbacks = new CallbackList(); } if (this._options && this._options.onFirstListenerAdd && this._callbacks.isEmpty()) { this._options.onFirstListenerAdd(this); } this._callbacks.add(listener, thisArgs); const result = { dispose: () => { if (!this._callbacks) { return; } this._callbacks.remove(listener, thisArgs); result.dispose = Emitter._noop; if (this._options && this._options.onLastListenerRemove && this._callbacks.isEmpty()) { this._options.onLastListenerRemove(this); } } }; if (Array.isArray(disposables)) { disposables.push(result); } return result; }; } return this._event; } fire(event) { if (this._callbacks) { this._callbacks.invoke.call(this._callbacks, event); } } dispose() { if (this._callbacks) { this._callbacks.dispose(); this._callbacks = undefined; } } } events$1.Emitter = Emitter; Emitter._noop = function () { }; return events$1; } var cancellation$1 = {}; var hasRequiredCancellation$1; function requireCancellation$1 () { if (hasRequiredCancellation$1) return cancellation$1; hasRequiredCancellation$1 = 1; Object.defineProperty(cancellation$1, "__esModule", { value: true }); cancellation$1.CancellationTokenSource = cancellation$1.CancellationToken = void 0; const ral_1 = requireRal(); const Is = requireIs$1(); const events_1 = requireEvents$1(); var CancellationToken; (function (CancellationToken) { CancellationToken.None = Object.freeze({ isCancellationRequested: false, onCancellationRequested: events_1.Event.None }); CancellationToken.Cancelled = Object.freeze({ isCancellationRequested: true, onCancellationRequested: events_1.Event.None }); function is(value) { const candidate = value; return candidate && (candidate === CancellationToken.None || candidate === CancellationToken.Cancelled || (Is.boolean(candidate.isCancellationRequested) && !!candidate.onCancellationRequested)); } CancellationToken.is = is; })(CancellationToken || (cancellation$1.CancellationToken = CancellationToken = {})); const shortcutEvent = Object.freeze(function (callback, context) { const handle = (0, ral_1.default)().timer.setTimeout(callback.bind(context), 0); return { dispose() { handle.dispose(); } }; }); class MutableToken { constructor() { this._isCancelled = false; } cancel() { if (!this._isCancelled) { this._isCancelled = true; if (this._emitter) { this._emitter.fire(undefined); this.dispose(); } } } get isCancellationRequested() { return this._isCancelled; } get onCancellationRequested() { if (this._isCancelled) { return shortcutEvent; } if (!this._emitter) { this._emitter = new events_1.Emitter(); } return this._emitter.event; } dispose() { if (this._emitter) { this._emitter.dispose(); this._emitter = undefined; } } } class CancellationTokenSource { get token() { if (!this._token) { this._token = new MutableToken(); } return this._token; } cancel() { if (!this._token) { this._token = CancellationToken.Cancelled; } else { this._token.cancel(); } } dispose() { if (!this._token) { this._token = CancellationToken.None; } else if (this._token instanceof MutableToken) { this._token.dispose(); } } } cancellation$1.CancellationTokenSource = CancellationTokenSource; return cancellation$1; } var sharedArrayCancellation = {}; var hasRequiredSharedArrayCancellation; function requireSharedArrayCancellation () { if (hasRequiredSharedArrayCancellation) return sharedArrayCancellation; hasRequiredSharedArrayCancellation = 1; Object.defineProperty(sharedArrayCancellation, "__esModule", { value: true }); sharedArrayCancellation.SharedArrayReceiverStrategy = sharedArrayCancellation.SharedArraySenderStrategy = void 0; const cancellation_1 = requireCancellation$1(); var CancellationState; (function (CancellationState) { CancellationState.Continue = 0; CancellationState.Cancelled = 1; })(CancellationState || (CancellationState = {})); class SharedArraySenderStrategy { constructor() { this.buffers = new Map(); } enableCancellation(request) { if (request.id === null) { return; } const buffer = new SharedArrayBuffer(4); const data = new Int32Array(buffer, 0, 1); data[0] = CancellationState.Continue; this.buffers.set(request.id, buffer); request.$cancellationData = buffer; } async sendCancellation(_conn, id) { const buffer = this.buffers.get(id); if (buffer === undefined) { return; } const data = new Int32Array(buffer, 0, 1); Atomics.store(data, 0, CancellationState.Cancelled); } cleanup(id) { this.buffers.delete(id); } dispose() { this.buffers.clear(); } } sharedArrayCancellation.SharedArraySenderStrategy = SharedArraySenderStrategy; class SharedArrayBufferCancellationToken { constructor(buffer) { this.data = new Int32Array(buffer, 0, 1); } get isCancellationRequested() { return Atomics.load(this.data, 0) === CancellationState.Cancelled; } get onCancellationRequested() { throw new Error(`Cancellation over SharedArrayBuffer doesn't support cancellation events`); } } class SharedArrayBufferCancellationTokenSource { constructor(buffer) { this.token = new SharedArrayBufferCancellationToken(buffer); } cancel() { } dispose() { } } class SharedArrayReceiverStrategy { constructor() { this.kind = 'request'; } createCancellationTokenSource(request) { const buffer = request.$cancellationData; if (buffer === undefined) { return new cancellation_1.CancellationTokenSource(); } return new SharedArrayBufferCancellationTokenSource(buffer); } } sharedArrayCancellation.SharedArrayReceiverStrategy = SharedArrayReceiverStrategy; return sharedArrayCancellation; } var messageReader$1 = {}; var semaphore$1 = {}; var hasRequiredSemaphore$1; function requireSemaphore$1 () { if (hasRequiredSemaphore$1) return semaphore$1; hasRequiredSemaphore$1 = 1; Object.defineProperty(semaphore$1, "__esModule", { value: true }); semaphore$1.Semaphore = void 0; const ral_1 = requireRal(); class Semaphore { constructor(capacity = 1) { if (capacity <= 0) { throw new Error('Capacity must be greater than 0'); } this._capacity = capacity; this._active = 0; this._waiting = []; } lock(thunk) { return new Promise((resolve, reject) => { this._waiting.push({ thunk, resolve, reject }); this.runNext(); }); } get active() { return this._active; } runNext() { if (this._waiting.length === 0 || this._active === this._capacity) { return; } (0, ral_1.default)().timer.setImmediate(() => this.doRunNext()); } doRunNext() { if (this._waiting.length === 0 || this._active === this._capacity) { return; } const next = this._waiting.shift(); this._active++; if (this._active > this._capacity) { throw new Error(`To many thunks active`); } try { const result = next.thunk(); if (result instanceof Promise) { result.then((value) => { this._active--; next.resolve(value); this.runNext(); }, (err) => { this._active--; next.reject(err); this.runNext(); }); } else { this._active--; next.resolve(result); this.runNext(); } } catch (err) { this._active--; next.reject(err); this.runNext(); } } } semaphore$1.Semaphore = Semaphore; return semaphore$1; } var hasRequiredMessageReader$1; function requireMessageReader$1 () { if (hasRequiredMessageReader$1) return messageReader$1; hasRequiredMessageReader$1 = 1; Object.defineProperty(messageReader$1, "__esModule", { value: true }); messageReader$1.ReadableStreamMessageReader = messageReader$1.AbstractMessageReader = messageReader$1.MessageReader = void 0; const ral_1 = requireRal(); const Is = requireIs$1(); const events_1 = requireEvents$1(); const semaphore_1 = requireSemaphore$1(); var MessageReader; (function (MessageReader) { function is(value) { let candidate = value; return candidate && Is.func(candidate.listen) && Is.func(candidate.dispose) && Is.func(candidate.onError) && Is.func(candidate.onClose) && Is.func(candidate.onPartialMessage); } MessageReader.is = is; })(MessageReader || (messageReader$1.MessageReader = MessageReader = {})); class AbstractMessageReader { constructor() { this.errorEmitter = new events_1.Emitter(); this.closeEmitter = new events_1.Emitter(); this.partialMessageEmitter = new events_1.Emitter(); } dispose() { this.errorEmitter.dispose(); this.closeEmitter.dispose(); } get onError() { return this.errorEmitter.event; } fireError(error) { this.errorEmitter.fire(this.asError(error)); } get onClose() { return this.closeEmitter.event; } fireClose() { this.closeEmitter.fire(undefined); } get onPartialMessage() { return this.partialMessageEmitter.event; } firePartialMessage(info) { this.partialMessageEmitter.fire(info); } asError(error) { if (error instanceof Error) { return error; } else { return new Error(`Reader received error. Reason: ${Is.string(error.message) ? error.message : 'unknown'}`); } } } messageReader$1.AbstractMessageReader = AbstractMessageReader; var ResolvedMessageReaderOptions; (function (ResolvedMessageReaderOptions) { function fromOptions(options) { let charset; let contentDecoder; const contentDecoders = new Map(); let contentTypeDecoder; const contentTypeDecoders = new Map(); if (options === undefined || typeof options === 'string') { charset = options ?? 'utf-8'; } else { charset = options.charset ?? 'utf-8'; if (options.contentDecoder !== undefined) { contentDecoder = options.contentDecoder; contentDecoders.set(contentDecoder.name, contentDecoder); } if (options.contentDecoders !== undefined) { for (const decoder of options.contentDecoders) { contentDecoders.set(decoder.name, decoder); } } if (options.contentTypeDecoder !== undefined) { contentTypeDecoder = options.contentTypeDecoder; contentTypeDecoders.set(contentTypeDecoder.name, contentTypeDecoder); } if (options.contentTypeDecoders !== undefined) { for (const decoder of options.contentTypeDecoders) { contentTypeDecoders.set(decoder.name, decoder); } } } if (contentTypeDecoder === undefined) { contentTypeDecoder = (0, ral_1.default)().applicationJson.decoder; contentTypeDecoders.set(contentTypeDecoder.name, contentTypeDecoder); } return { charset, contentDecoder, contentDecoders, contentTypeDecoder, contentTypeDecoders }; } ResolvedMessageReaderOptions.fromOptions = fromOptions; })(ResolvedMessageReaderOptions || (ResolvedMessageReaderOptions = {})); class ReadableStreamMessageReader extends AbstractMessageReader { constructor(readable, options) { super(); this.readable = readable; this.options = ResolvedMessageReaderOptions.fromOptions(options); this.buffer = (0, ral_1.default)().messageBuffer.create(this.options.charset); this._partialMessageTimeout = 10000; this.nextMessageLength = -1; this.messageToken = 0; this.readSemaphore = new semaphore_1.Semaphore(1); } set partialMessageTimeout(timeout) { this._partialMessageTimeout = timeout; } get partialMessageTimeout() { return this._partialMessageTimeout; } listen(callback) { this.nextMessageLength = -1; this.messageToken = 0; this.partialMessageTimer = undefined; this.callback = callback; const result = this.readable.onData((data) => { this.onData(data); }); this.readable.onError((error) => this.fireError(error)); this.readable.onClose(() => this.fireClose()); return result; } onData(data) { try { this.buffer.append(data); while (true) { if (this.nextMessageLength === -1) { const headers = this.buffer.tryReadHeaders(true); if (!headers) { return; } const contentLength = headers.get('content-length'); if (!contentLength) { this.fireError(new Error(`Header must provide a Content-Length property.\n${JSON.stringify(Object.fromEntries(headers))}`)); return; } const length = parseInt(contentLength); if (isNaN(length)) { this.fireError(new Error(`Content-Length value must be a number. Got ${contentLength}`)); return; } this.nextMessageLength = length; } const body = this.buffer.tryReadBody(this.nextMessageLength); if (body === undefined) { this.setPartialMessageTimer(); return; } this.clearPartialMessageTimer(); this.nextMessageLength = -1; this.readSemaphore.lock(async () => { const bytes = this.options.contentDecoder !== undefined ? await this.options.contentDecoder.decode(body) : body; const message = await this.options.contentTypeDecoder.decode(bytes, this.options); this.callback(message); }).catch((error) => { this.fireError(error); }); } } catch (error) { this.fireError(error); } } clearPartialMessageTimer() { if (this.partialMessageTimer) { this.partialMessageTimer.dispose(); this.partialMessageTimer = undefined; } } setPartialMessageTimer() { this.clearPartialMessageTimer(); if (this._partialMessageTimeout <= 0) { return; } this.partialMessageTimer = (0, ral_1.default)().timer.setTimeout((token, timeout) => { this.partialMessageTimer = undefined; if (token === this.messageToken) { this.firePartialMessage({ messageToken: token, waitingTime: timeout }); this.setPartialMessageTimer(); } }, this._partialMessageTimeout, this.messageToken, this._partialMessageTimeout); } } messageReader$1.ReadableStreamMessageReader = ReadableStreamMessageReader; return messageReader$1; } var messageWriter$1 = {}; var hasRequiredMessageWriter$1; function requireMessageWriter$1 () { if (hasRequiredMessageWriter$1) return messageWriter$1; hasRequiredMessageWriter$1 = 1; Object.defineProperty(messageWriter$1, "__esModule", { value: true }); messageWriter$1.WriteableStreamMessageWriter = messageWriter$1.AbstractMessageWriter = messageWriter$1.MessageWriter = void 0; const ral_1 = requireRal(); const Is = requireIs$1(); const semaphore_1 = requireSemaphore$1(); const events_1 = requireEvents$1(); const ContentLength = 'Content-Length: '; const CRLF = '\r\n'; var MessageWriter; (function (MessageWriter) { function is(value) { let candidate = value; return candidate && Is.func(candidate.dispose) && Is.func(candidate.onClose) && Is.func(candidate.onError) && Is.func(candidate.write); } MessageWriter.is = is; })(MessageWriter || (messageWriter$1.MessageWriter = MessageWriter = {})); class AbstractMessageWriter { constructor() { this.errorEmitter = new events_1.Emitter(); this.closeEmitter = new events_1.Emitter(); } dispose() { this.errorEmitter.dispose(); this.closeEmitter.dispose(); } get onError() { return this.errorEmitter.event; } fireError(error, message, count) { this.errorEmitter.fire([this.asError(error), message, count]); } get onClose() { return this.closeEmitter.event; } fireClose() { this.closeEmitter.fire(undefined); } asError(error) { if (error instanceof Error) { return error; } else { return new Error(`Writer received error. Reason: ${Is.string(error.message) ? error.message : 'unknown'}`); } } } messageWriter$1.AbstractMessageWriter = AbstractMessageWriter; var ResolvedMessageWriterOptions; (function (ResolvedMessageWriterOptions) { function fromOptions(options) { if (options === undefined || typeof options === 'string') { return { charset: options ?? 'utf-8', contentTypeEncoder: (0, ral_1.default)().applicationJson.encoder }; } else { return { charset: options.charset ?? 'utf-8', contentEncoder: options.contentEncoder, contentTypeEncoder: options.contentTypeEncoder ?? (0, ral_1.default)().applicationJson.encoder }; } } ResolvedMessageWriterOptions.fromOptions = fromOptions; })(ResolvedMessageWriterOptions || (ResolvedMessageWriterOptions = {})); class WriteableStreamMessageWriter extends AbstractMessageWriter { constructor(writable, options) { super(); this.writable = writable; this.options = ResolvedMessageWriterOptions.fromOptions(options); this.errorCount = 0; this.writeSemaphore = new semaphore_1.Semaphore(1); this.writable.onError((error) => this.fireError(error)); this.writable.onClose(() => this.fireClose()); } async write(msg) { return this.writeSemaphore.lock(async () => { const payload = this.options.contentTypeEncoder.encode(msg, this.options).then((buffer) => { if (this.options.contentEncoder !== undefined) { return this.options.contentEncoder.encode(buffer); } else { return buffer; } }); return payload.then((buffer) => { const headers = []; headers.push(ContentLength, buffer.byteLength.toString(), CRLF); headers.push(CRLF); return this.doWrite(msg, headers, buffer); }, (error) => { this.fireError(error); throw error; }); }); } async doWrite(msg, headers, data) { try { await this.writable.write(headers.join(''), 'ascii'); return this.writable.write(data); } catch (error) { this.handleError(error, msg); return Promise.reject(error); } } handleError(error, msg) { this.errorCount++; this.fireError(error, msg, this.errorCount); } end() { this.writable.end(); } } messageWriter$1.WriteableStreamMessageWriter = WriteableStreamMessageWriter; return messageWriter$1; } var messageBuffer$1 = {}; var hasRequiredMessageBuffer; function requireMessageBuffer () { if (hasRequiredMessageBuffer) return messageBuffer$1; hasRequiredMessageBuffer = 1; Object.defineProperty(messageBuffer$1, "__esModule", { value: true }); messageBuffer$1.AbstractMessageBuffer = void 0; const CR = 13; const LF = 10; const CRLF = '\r\n'; class AbstractMessageBuffer { constructor(encoding = 'utf-8') { this._encoding = encoding; this._chunks = []; this._totalLength = 0; } get encoding() { return this._encoding; } append(chunk) { const toAppend = typeof chunk === 'string' ? this.fromStrin