const process = async (inputUrl: string, context: string, resourceCache: Map<string, string>) => {
    // If root-relative, absolute or protocol relative url, leave as is
    if (/^((?:\w+:)?\/\/|data:|chrome:|#)/.test(inputUrl)) {
      return inputUrl;
    }

    // If starts with a caret, remove and return remainder
    // this supports bypassing asset processing
    if (inputUrl.startsWith('^')) {
      return inputUrl.substr(1);
    }

    const cacheKey = path.resolve(context, inputUrl);
    const cachedUrl = resourceCache.get(cacheKey);
    if (cachedUrl) {
      return cachedUrl;
    }

    if (inputUrl.startsWith('~')) {
      inputUrl = inputUrl.substr(1);
    }

    if (inputUrl.startsWith('/')) {
      let outputUrl = '';
      if (deployUrl.match(/:\/\//) || deployUrl.startsWith('/')) {
        // If deployUrl is absolute or root relative, ignore baseHref & use deployUrl as is.
        outputUrl = `${deployUrl.replace(/\/$/, '')}${inputUrl}`;
      } else if (baseHref.match(/:\/\//)) {
        // If baseHref contains a scheme, include it as is.
        outputUrl = baseHref.replace(/\/$/, '') + dedupeSlashes(`/${deployUrl}/${inputUrl}`);
      } else {
        // Join together base-href, deploy-url and the original URL.
        outputUrl = dedupeSlashes(`/${baseHref}/${deployUrl}/${inputUrl}`);
      }

      resourceCache.set(cacheKey, outputUrl);

      return outputUrl;
    }

    const { pathname, hash, search } = url.parse(inputUrl.replace(/\\/g, '/'));
    const resolver = (file: string, base: string) => new Promise<string>((resolve, reject) => {
      loader.resolve(base, file, (err, result) => {
        if (err) {
          reject(err);

          return;
        }
        resolve(result);
      });
    });

    const result = await resolve(pathname as string, context, resolver);

    return new Promise<string>((resolve, reject) => {
      loader.fs.readFile(result, (err: Error, content: Buffer) => {
        if (err) {
          reject(err);

          return;
        }

        let outputPath = interpolateName(
          { resourcePath: result } as webpack.loader.LoaderContext,
          filename,
          { content },
        );

        if (resourcesOutputPath) {
          outputPath = path.posix.join(resourcesOutputPath, outputPath);
        }

        loader.addDependency(result);
        loader.emitFile(outputPath, content, undefined);

        let outputUrl = outputPath.replace(/\\/g, '/');
        if (hash || search) {
          outputUrl = url.format({ pathname: outputUrl, hash, search });
        }

        if (deployUrl && loader.loaders[loader.loaderIndex].options.ident !== 'extracted') {
          outputUrl = url.resolve(deployUrl, outputUrl);
        }

        resourceCache.set(cacheKey, outputUrl);
        resolve(outputUrl);
      });
    });
  };
Exemple #2
0
dispatcher.onGet('/', function(request: any, result: any) {
  const query: any = url.parse(request.url, true).query;

  const page: number = _.isUndefined(query.page) ? 1 : query.page;
  const pageSize: number = query.pageSize;

  const start: number = (page - 1) * pageSize;
  const end: number = start + pageSize;

  let sortType: string;
  let sortBy: string;

  if (!_.isUndefined(query.orderBy)) {
    sortType = query.orderBy.indexOf('-') === 0 ? 'desc' : 'asc';
    sortBy = query.orderBy.replace('-', '');
  }

  let filters: string[] = [];
  for (let filter in query) {
    if (filter !== 'page' && filter !== 'pageSize' && filter !== 'orderBy'
        && filter !== 'expand') {
      filters[filter] = query[filter];
    }
  }

  let data: any[] = DEMO_DATA;

  // apply filters
  data = _.filter(data, function(item: any) {
    let match = true;
    for (let filter in filters) {
      if (filters.hasOwnProperty(filter)) {
        let value = _.get(item, filter).toString();

        match = match &&
          (value.match(new RegExp(filters[filter], 'i')) !== null);
      }
    }

    return match;
  });

  result.writeHead(200, {
    'Content-Type': 'text/plain',
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Expose-Headers': 'X-Pagination-Total-Count, X-Pagination-Page-Count, X-Pagination-Current-Page, X-Pagination-Per-Page',
    'X-Pagination-Total-Count': data.length,
    'X-Pagination-Page-Count': Math.ceil(data.length / pageSize),
    'X-Pagination-Current-Page': page,
    'X-Pagination-Per-Page': !_.isUndefined(pageSize) ? pageSize : ''
  });

  // sort data
  if (!_.isUndefined(sortBy)) {
    data = _.orderBy(data, [sortBy], [sortType]);
  }

  // slice page
  if (!_.isUndefined(pageSize)) {
    data = data.slice(start, end);
  }

  result.end(JSON.stringify(data));
});
Exemple #3
0
Repo.prototype.clone_ = function(user, repo, repo_url) {
    logger.log(`Cloning ${logger.colors.green(`${user}/${repo}`)}`)
    git.clone(url.parse(repo_url).href, repo)
}
Exemple #4
0
 allAccounts.find(a => {
   const htmlURL = getHTMLURL(a.endpoint)
   const parsedEndpoint = URL.parse(htmlURL)
   return parsedURL.hostname === parsedEndpoint.hostname
 }) || null
Exemple #5
0
    let server = http.createServer((req, res) => {
        let error = (code: number, msg: string = null) => {
            res.writeHead(code, { "Content-Type": "text/plain" })
            res.end(msg || "Error " + code)
        }

        let sendJson = (v: any) => {
            res.writeHead(200, { 'Content-Type': 'application/json; charset=utf8' })
            res.end(JSON.stringify(v))
        }

        let sendHtml = (s: string) => {
            res.writeHead(200, { 'Content-Type': 'text/html; charset=utf8' })
            res.end(s)
        }

        let sendFile = (filename: string) => {
            let stat = fs.statSync(filename);

            res.writeHead(200, {
                'Content-Type': U.getMime(filename),
                'Content-Length': stat.size
            });

            fs.createReadStream(filename).pipe(res);
        }

        let pathname = decodeURI(url.parse(req.url).pathname);

        if (pathname == "/") {
            res.writeHead(301, { location: '/index.html' })
            res.end()
            return
        }
        
        if (pathname.slice(0, appTarget.id.length + 2) == "/" + appTarget.id + "/") {
            res.writeHead(301, { location: req.url.slice(appTarget.id.length + 1) })
            res.end()
            return            
        }

        let elts = pathname.split("/").filter(s => !!s)
        if (elts.some(s => s[0] == ".")) {
            return error(400, "Bad path :-(\n")
        }

        if (elts[0] == "api") {
            return handleApiAsync(req, res, elts)
                .then(sendJson, err => {
                    if (err.statusCode) {
                        error(err.statusCode)
                        console.log("Error " + err.statusCode)
                    }
                    else {
                        error(500)
                        console.log(err.stack)
                    }
                })
        }

        if (pathname == "/--embed") {
            // use microbit for now
            res.writeHead(302, { location: 'https://codemicrobit.com/--embed' })
            res.end()
            return
        }

        if (!/\.js\.map$/.test(pathname)) {
            for (let dir of dirs) {
                let filename = path.resolve(path.join(dir, pathname))
                if (fileExistsSync(filename)) {
                    sendFile(filename)
                    return;
                }
            }
        }

        let webFile = path.join(docsDir, pathname)
        if (!fileExistsSync(webFile)) {
            if (fileExistsSync(webFile + ".html")) {
                webFile += ".html"
                pathname += ".html"
            } else if (fileExistsSync(webFile + ".md")) {
                webFile += ".md"
                pathname += ".md"
            }
        }

        if (fileExistsSync(webFile)) {
            if (/\.md$/.test(webFile)) {
                let html = ks.docs.renderMarkdown(docsTemplate, fs.readFileSync(webFile, "utf8"))
                sendHtml(html)
            } else {
                sendFile(webFile)
            }
            return
        }

        return error(404, "Not found :(\n")
    });
export async function httpConnectionRequest(
  address: string,
  authExp: Expression,
  context: {},
  allowBasicAuth: boolean,
  timeout: number
): Promise<void> {
  const now = Date.now();
  const options: http.RequestOptions = parse(address);
  if (options.protocol !== "http:")
    throw new Error("Invalid connection request URL or protocol");

  options.agent = new http.Agent({
    maxSockets: 1,
    keepAlive: true
  });

  let authHeader: {};
  let username: string;
  let password: string;

  while (!authHeader || (username != null && password != null)) {
    let opts = options;
    if (authHeader) {
      if (authHeader["method"] === "Basic") {
        if (!allowBasicAuth)
          throw new Error("Basic HTTP authentication not allowed");

        opts = Object.assign(
          {
            headers: {
              Authorization: auth.basic(username || "", password || "")
            }
          },
          options
        );
      } else if (authHeader["method"] === "Digest") {
        opts = Object.assign(
          {
            headers: {
              Authorization: auth.digest(
                username,
                password,
                options.path,
                "GET",
                null,
                authHeader
              )
            }
          },
          options
        );
      } else {
        throw new Error("Unrecognized auth method");
      }
    }

    let res = await httpGet(opts, timeout);

    // Workaround for some devices unexpectedly closing the connection
    if (res.statusCode === 0 && authHeader) res = await httpGet(opts, timeout);
    if (res.statusCode === 0) throw new Error("Device is offline");
    if (res.statusCode === 200 || res.statusCode === 204) return;

    if (res.statusCode === 401 && res.headers["www-authenticate"]) {
      authHeader = auth.parseAuthHeader(res.headers["www-authenticate"]);
      [username, password, authExp] = extractAuth(authExp, context, now, false);
    } else {
      throw new Error(
        `Unexpected response code from device: ${res.statusCode}`
      );
    }
  }
  throw new Error("Incorrect connection request credentials");
}
Exemple #7
0
    let server = http.createServer((req, res) => {
        let error = (code: number, msg: string = null) => {
            res.writeHead(code, { "Content-Type": "text/plain" })
            res.end(msg || "Error " + code)
        }

        let sendJson = (v: any) => {
            res.writeHead(200, { 'Content-Type': 'application/json; charset=utf8' })
            res.end(JSON.stringify(v))
        }

        let sendHtml = (s: string) => {
            res.writeHead(200, { 'Content-Type': 'text/html; charset=utf8' })
            res.end(s)
        }

        let sendFile = (filename: string) => {
            let stat = fs.statSync(filename);

            res.writeHead(200, {
                'Content-Type': U.getMime(filename),
                'Content-Length': stat.size
            });

            fs.createReadStream(filename).pipe(res);
        }

        let pathname = decodeURI(url.parse(req.url).pathname);

        if (pathname == "/") {
            res.writeHead(301, { location: '/index.html' })
            res.end()
            return
        }

        let elts = pathname.split("/").filter(s => !!s)
        if (elts.some(s => s[0] == ".")) {
            return error(400, "Bad path :-(\n")
        }

        if (elts[0] == "api") {
            if (!isAuthorizedLocalRequest(req)) {
                error(403);
                return null;
            }

            return handleApiAsync(req, res, elts)
                .then(sendJson, err => {
                    if (err.statusCode) {
                        error(err.statusCode)
                        console.log("Error " + err.statusCode)
                    }
                    else {
                        error(500)
                        console.log(err.stack)
                    }
                })
        }

        if (options.packaged) {
            let filename = path.resolve(path.join(packagedDir, pathname))
            if (fileExistsSync(filename)) {
                return sendFile(filename)
            } else {
                return error(404, "Packaged file not found")
            }
        }

        if (pathname.slice(0, pxt.appTarget.id.length + 2) == "/" + pxt.appTarget.id + "/") {
            res.writeHead(301, { location: req.url.slice(pxt.appTarget.id.length + 1) })
            res.end()
            return
        }

        if (pathname == "/--embed") {
            sendFile(path.join(fileDir, 'node_modules/pxt-core/webapp/public/embed.js'));
            return
        }

        if (pathname == "/--run") {
            sendFile(path.join(fileDir, 'node_modules/pxt-core/webapp/public/run.html'));
            return
        }

        if (pathname == "/--docs") {
            sendFile(path.join(fileDir, 'node_modules/pxt-core/webapp/public/docs.html'));
            return
        }

        if (!/\.js\.map$/.test(pathname)) {
            let dd = dirs
            if (U.startsWith(pathname, "/sim/")) {
                pathname = pathname.slice(4)
                dd = simdirs
            } else if (U.startsWith(pathname, "/cdn/")) {
                pathname = pathname.slice(4)
                dd = dirs
            } else if (U.startsWith(pathname, "/docfiles/")) {
                pathname = pathname.slice(10)
                dd = docfilesdirs
            }
            for (let dir of dd) {
                let filename = path.resolve(path.join(dir, pathname))
                if (fileExistsSync(filename)) {
                    sendFile(filename)
                    return;
                }
            }
        }

        let webFile = path.join(docsDir, pathname)
        if (!fileExistsSync(webFile)) {
            if (fileExistsSync(webFile + ".html")) {
                webFile += ".html"
                pathname += ".html"
            } else if (fileExistsSync(webFile + ".md")) {
                webFile += ".md"
                pathname += ".md"
            }
        }

        if (fileExistsSync(webFile)) {
            if (/\.md$/.test(webFile)) {
                let bc = elts.map((e, i) => {
                    return {
                        href: "/" + elts.slice(0, i + 1).join("/"),
                        name: e
                    }
                })
                let html = pxt.docs.renderMarkdown(docsTemplate, fs.readFileSync(webFile, "utf8"), pxt.appTarget.appTheme, null, bc)
                sendHtml(html)
            } else {
                sendFile(webFile)
            }
            return
        }

        return error(404, "Not found :(\n")
    });
 snippet => {
   const newIssueUrl = url.parse('https://github.com/rust-lang/rust/issues/new', true);
   newIssueUrl.query = { body: snippet };
   return url.format(newIssueUrl);
 },
 (baseUrl, query, gist) => {
   const u = url.parse(baseUrl, true);
   u.query = { ...query, code: gist.code };
   return url.format(u);
 },
Exemple #10
0
    "return"?: number;
}



/**
 * FakeApi - fetch parameter creator
 */
export const FakeApiFetchParamCreator = {
    /** 
     * To test code injection *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r
     * @param test code inject * &#39; &quot; &#x3D;end  rn n r To test code injection *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r
     */
    testCodeInjectEndRnNR(params: {  "test code inject * &#39; &quot; &#x3D;end  rn n r"?: string; }, options?: any): FetchArgs {
        const baseUrl = `/fake`;
        let urlObj = url.parse(baseUrl, true);
        let fetchOptions: RequestInit = assign({}, { method: "PUT" }, options);

        let contentTypeHeader: Dictionary<string> = {};
        contentTypeHeader = { "Content-Type": "application/x-www-form-urlencoded" };
        fetchOptions.body = querystring.stringify({
            "test code inject */ &#39; &quot; &#x3D;end -- \r\n \n \r": params["test code inject * &#39; &quot; &#x3D;end  rn n r"],
        });
        if (contentTypeHeader) {
            fetchOptions.headers = contentTypeHeader;
        }
        return {
            url: url.format(urlObj),
            options: fetchOptions,
        };
    },