• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*! *****************************************************************************
2Copyright (c) Microsoft Corporation. All rights reserved.
3Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4this file except in compliance with the License. You may obtain a copy of the
5License at http://www.apache.org/licenses/LICENSE-2.0
6
7THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
9WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
10MERCHANTABLITY OR NON-INFRINGEMENT.
11
12See the Apache Version 2.0 License for specific language governing permissions
13and limitations under the License.
14***************************************************************************** */
15
16
17"use strict";
18var __create = Object.create;
19var __defProp = Object.defineProperty;
20var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
21var __getOwnPropNames = Object.getOwnPropertyNames;
22var __getProtoOf = Object.getPrototypeOf;
23var __hasOwnProp = Object.prototype.hasOwnProperty;
24var __copyProps = (to, from, except, desc) => {
25  if (from && typeof from === "object" || typeof from === "function") {
26    for (let key of __getOwnPropNames(from))
27      if (!__hasOwnProp.call(to, key) && key !== except)
28        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
29  }
30  return to;
31};
32var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
33  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
34  mod
35));
36
37// src/cancellationToken/cancellationToken.ts
38var fs = __toESM(require("fs"));
39function pipeExists(name) {
40  return fs.existsSync(name);
41}
42function createCancellationToken(args) {
43  let cancellationPipeName;
44  for (let i = 0; i < args.length - 1; i++) {
45    if (args[i] === "--cancellationPipeName") {
46      cancellationPipeName = args[i + 1];
47      break;
48    }
49  }
50  if (!cancellationPipeName) {
51    return {
52      isCancellationRequested: () => false,
53      setRequest: (_requestId) => void 0,
54      resetRequest: (_requestId) => void 0
55    };
56  }
57  if (cancellationPipeName.charAt(cancellationPipeName.length - 1) === "*") {
58    const namePrefix = cancellationPipeName.slice(0, -1);
59    if (namePrefix.length === 0 || namePrefix.indexOf("*") >= 0) {
60      throw new Error("Invalid name for template cancellation pipe: it should have length greater than 2 characters and contain only one '*'.");
61    }
62    let perRequestPipeName;
63    let currentRequestId;
64    return {
65      isCancellationRequested: () => perRequestPipeName !== void 0 && pipeExists(perRequestPipeName),
66      setRequest(requestId) {
67        currentRequestId = requestId;
68        perRequestPipeName = namePrefix + requestId;
69      },
70      resetRequest(requestId) {
71        if (currentRequestId !== requestId) {
72          throw new Error(`Mismatched request id, expected ${currentRequestId}, actual ${requestId}`);
73        }
74        perRequestPipeName = void 0;
75      }
76    };
77  } else {
78    return {
79      isCancellationRequested: () => pipeExists(cancellationPipeName),
80      setRequest: (_requestId) => void 0,
81      resetRequest: (_requestId) => void 0
82    };
83  }
84}
85module.exports = createCancellationToken;
86//# sourceMappingURL=cancellationToken.js.map
87