func Scan(ctx context.Context, path string) (*Package, error) { p := new(Package) // use a new system context for the duration of the scan ctx = sysctx.NewContext(ctx) env, err := parser.ScanForEnv(ctx, path) if err != nil { return nil, kerr.Wrap("SFPSTLPKMX", err) } p.Env = env files := scanner.ScanDirToFiles(ctx, env.Dir, env.Recursive) bytes := scanner.ScanFilesToBytes(ctx, files) localContext := envctx.NewContext(ctx, env) for b := range bytes { f := new(File) f.Package = p if b.Err != nil { return nil, kerr.Wrap("IPUHPBBWEA", b.Err) } o := &system.ObjectStub{} if err := system.Unmarshal(localContext, b.Bytes, o); err != nil { return nil, kerr.Wrap("DCAGIDLXRT", err) } if o.Type == nil { return nil, kerr.New("AJPCEQTWPS", "%s has no type", b.File) } if o.Id == nil && *o.Type != *system.NewReference("kego.io/system", "package") { // we tolerate missing ID only for system:package return nil, kerr.New("YDKYLXTGYC", "%s has no id", b.File) } f.AbsoluteFilepath = b.File f.Type = o.Type f.Id = o.Id if f.RelativeFilepath, err = filepath.Rel(env.Dir, b.File); err != nil { return nil, kerr.Wrap("QDAEGOWTWP", err) } f.Directory, f.Filename = filepath.Split(b.File) f.IsInRoot = DirEqual(f.Directory, env.Dir) f.Extension = filepath.Ext(b.File) f.Yaml = f.Extension == ".yaml" || f.Extension == ".yml" p.Files = append(p.Files, f) } return p, nil }
func script(ctx context.Context, w http.ResponseWriter, req *http.Request, sourceMap bool) error { wgctx.Add(ctx, "script") defer wgctx.Done(ctx, "script") path := "" if strings.HasSuffix(req.URL.Path, ".map") { path = req.URL.Path[1 : len(req.URL.Path)-14] } else { path = req.URL.Path[1 : len(req.URL.Path)-10] } env, err := parser.ScanForEnv(ctx, path) // This is the client code for the editor which we will compile to // Javascript using GopherJs below. source, err := generate.Editor(ctx, env) if err != nil { return kerr.Wrap("UWPDBQXURR", err) } var out []byte withCancel(ctx, func() { out, err = Compile(ctx, source, sourceMap) }) if err != nil { return kerr.Wrap("LSUXHJMSSX", err) } withCancel(ctx, func() { err = writeWithTimeout(w, out) }) if err != nil { return kerr.Wrap("GAOWWBAIAL", err) } return nil }
func (s *Server) Data(request *shared.DataRequest, response *shared.DataResponse) error { if !s.auth.Auth([]byte(request.Path), request.Hash) { return kerr.New("SYEKLIUMVY", "Auth failed") } env, err := parser.ScanForEnv(s.ctx, request.Package) if err != nil { return kerr.Wrap("PNAGGKHDYL", err) } full, err := pkghelp.Check(env.Dir, request.File, env.Recursive) if err != nil { return kerr.Wrap("JEYTFWKMYF", err) } bytes, err := scanner.ProcessFile(full) if err != nil { return kerr.Wrap("HQXMIMWXFY", err) } if bytes == nil { return kerr.New("HIUINHIAPY", "Error reading %s", request.File) } localContext := envctx.NewContext(s.ctx, env) o := &system.ObjectStub{} if err := system.Unmarshal(localContext, bytes, o); err != nil { return kerr.Wrap("SVINFEMKBG", err) } if o.Id.Name != request.Name { return kerr.New("TNJSLMPMLB", "Id %s in %s does not match request %s", o.Id.Name, request.File, request.Name) } response.Package = request.Package response.Name = request.Name response.Found = true response.Data = bytes return nil }
func root(ctx context.Context, w http.ResponseWriter, req *http.Request, auth auther.Auther) error { wgctx.Add(ctx, "root") defer wgctx.Done(ctx, "root") if b, err := static.Asset(req.URL.Path[1:]); err == nil { if strings.HasSuffix(req.URL.Path, ".css") { w.Header().Set("Content-Type", "text/css") } if err := writeWithTimeout(w, b); err != nil { return kerr.Wrap("PVPCXBIUJT", err) } return nil } path := req.URL.Path[1:] if strings.HasSuffix(path, "/") { path = path[0 : len(path)-1] } // use a new context with a blank sysctx for the duration of this function // to prevent caching ctx = sysctx.NewContext(ctx) scache := sysctx.FromContext(ctx) env, err := parser.ScanForEnv(ctx, path) if err != nil { if _, ok := kerr.Source(err).(gopackages.NotFoundError); ok { w.WriteHeader(404) return nil } return kerr.Wrap("ALINBMKDRP", err) } pcache, err := parser.Parse(ctx, path) if err != nil { return kerr.Wrap("HIHWJRPUKE", err) } if err := process.GenerateAll(ctx, env.Path, map[string]bool{}); err != nil { return kerr.Wrap("LVGHABDYNQ", err) } data := map[string]string{} for _, name := range pcache.Globals.Keys() { if g, ok := pcache.Globals.Get(name); ok { data[name] = g.File } } pkgBytes := pcache.PackageBytes pkgFilename := pcache.PackageFilename if pkgBytes == nil { b, err := system.Marshal(ctx, system.EmptyPackage()) if err != nil { return kerr.Wrap("OUBOTYGPKU", err) } pkgBytes = b pkgFilename = "package.ke.json" } imports := map[string]shared.ImportInfo{} var scan func(string) error scan = func(path string) error { if _, ok := imports[path]; ok { return nil } syspi, ok := scache.Get(path) if !ok { return kerr.New("VIGKIUPNCF", "%s not found in sys ctx", path) } info := shared.ImportInfo{ Path: path, Aliases: syspi.Aliases, Types: map[string]shared.TypeInfo{}, } for _, name := range syspi.Files.Keys() { if b, ok := syspi.Files.Get(name); ok { info.Types[name] = shared.TypeInfo{ File: b.File, Bytes: b.Bytes, } } } imports[path] = info for _, child := range syspi.Aliases { if err := scan(child); err != nil { return kerr.Wrap("NCULMUUUOT", err) } } return nil } // First we always import system if err := scan("kego.io/system"); err != nil { return kerr.Wrap("KRXSLOJKWV", err) } if err := scan(env.Path); err != nil { return kerr.Wrap("EELKQDCJGN", err) } info := shared.Info{ Path: env.Path, Aliases: env.Aliases, Data: data, Package: pkgBytes, PackageFilename: pkgFilename, Imports: imports, Hash: auth.Sign([]byte(env.Path)), } buf := bytes.NewBuffer([]byte{}) err = gob.NewEncoder(buf).Encode(info) if err != nil { return kerr.Wrap("OHBYTULHUQ", err) } base64EncodedString := base64.StdEncoding.EncodeToString(buf.Bytes()) attrib := base64EncodedString source := []byte(` <html> <head> <meta charset="utf-8"> <link rel="stylesheet" href="/bootstrap/css/bootstrap.min.css"> <link rel="stylesheet" href="/bootstrap/css/bootstrap-theme.min.css"> <link rel="stylesheet" href="/split.css"> <link rel="stylesheet" href="/editors.css"> <link rel="stylesheet" href="/tree.css"> <script src="/jquery-2.2.4.min.js"></script> <script src="/jquery-ui/jquery-ui.min.js"></script> <script src="/split.min.js"></script> <script src="/bootstrap/js/bootstrap.min.js"></script> <link rel="icon" type="image/png" href="data:image/png;base64,iVBORw0KGgo="> </head> <body id="body" info="` + attrib + `"></body> <script src="/` + env.Path + `/script.js"></script> </html>`) if err := writeWithTimeout(w, source); err != nil { return kerr.Wrap("ICJSAIMDRF", err) } return nil }