-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure resources are relative to the module path
Just using std.read() for resources won't work in general, because the desired path may not be under the input directory (and reads are restricted to paths under the read directory). To enable reads from module directories, while keeping the guard against reading outside the allowed paths, keep track of each module that imports `@jkcfg/std/resource' and look up the module path when its `resource` procedure is called, to use as a base path.
- Loading branch information
Showing
14 changed files
with
95 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,48 @@ | ||
package std | ||
|
||
import ( | ||
"crypto/sha256" | ||
"fmt" | ||
) | ||
|
||
func MakeResourceModule(basePath string) ([]byte, string) { | ||
return []byte(fmt.Sprintf(` | ||
// ModuleResources keeps track of the base paths for modules, as well | ||
// as generating the magic modules when they are imported. | ||
type ModuleResources struct { | ||
// module hash -> basePath for resource reads | ||
modules map[string]string | ||
} | ||
|
||
// NewModuleResources initialises a new ModuleResources | ||
func NewModuleResources() *ModuleResources { | ||
return &ModuleResources{ | ||
modules: map[string]string{}, | ||
} | ||
} | ||
|
||
// ResourceBase provides the module base path given the hash. | ||
func (r *ModuleResources) ResourceBase(hash string) (string, bool) { | ||
path, ok := r.modules[hash] | ||
return path, ok | ||
} | ||
|
||
// MakeModule generates resource module code (and path) given the | ||
// importing module's base path. | ||
func (r *ModuleResources) MakeModule(basePath string) ([]byte, string) { | ||
hash := sha256.New() | ||
hash.Write([]byte(basePath)) | ||
moduleHash := fmt.Sprintf("%x", hash.Sum(nil)) | ||
r.modules[moduleHash] = basePath | ||
|
||
code := ` | ||
import std from '@jkcfg/std'; | ||
const base = %q; | ||
const module = %q; | ||
function resource(path, ...rest) { | ||
return std.read(base +'/' + path, ...rest); | ||
function resource(path, {...rest} = {}) { | ||
return std.read(path, {...rest, module}); | ||
} | ||
export default resource; | ||
`, | ||
basePath)), "resource:" + basePath | ||
` | ||
return []byte(fmt.Sprintf(code, moduleHash)), "resource:" + basePath | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,5 @@ table ReadArgs { | |
timeout: uint; | ||
encoding: Encoding; | ||
format: Format; | ||
module: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import std from '@jkcfg/std'; | ||
import resource from './test-module-resource/resource'; | ||
import resource1 from './test-module-resource/resource'; | ||
import resource2 from './test-module-resource/submodule/resource'; | ||
|
||
resource.then(std.log); | ||
resource1.then(std.log); | ||
resource2.then(std.log); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
jk run -v --input-directory=/tmp %f | ||
jk run --input-directory=/tmp %f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"foo": { | ||
"bar": 1 | ||
} | ||
"module": "top" | ||
} | ||
{ | ||
"module": "sub" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import resource from '@jkcfg/std/resource'; | ||
|
||
export default resource('values.json'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"module": "sub" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
{ | ||
"foo": { | ||
"bar": 1 | ||
} | ||
"module": "top" | ||
} |