@ostro/filesystem 0.0.0-alpha.2 → 0.0.0-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/adapter/local.js CHANGED
@@ -1,141 +1,141 @@
1
- const fs = require('fs-extra')
2
- const CoreAdapter = require('./CoreAdapter')
3
- const path = require('path')
4
- const { v4 } = require("uuid");
5
- const kHandler = Symbol('handler')
6
-
7
- class Local extends CoreAdapter {
8
- constructor(root = '', $config) {
9
- super()
10
- this.setPathPrefix(root)
11
- this.setConfig($config)
12
- this[kHandler] = (typeof $config.handler == 'function' ? new $config.handler : (typeof $config.handler == 'object' ? $config.handler : {}))
13
- }
14
- move(path, newpath, callback) {
15
- if (callback)
16
- return fs.move(path, newpath, {
17
- overwrite: true
18
- }, function(err, res) {
19
- callback(err, res, newpath)
20
- })
21
- else
22
- return fs.move(path, newpath, {
23
- overwrite: true
24
- })
25
- }
26
- delete($path, options = {}, callback) {
27
- let location = this.applyPathPrefix($path);
28
- fs.remove(location, (error) => {
29
- if (typeof this[kHandler].delete == 'function') {
30
- this[kHandler].delete({
31
- basePath: location,
32
- path: $path,
33
- options,
34
- callback,
35
- error,
36
- })
37
- } else {
38
- callback(error, true, location)
39
- }
40
- });
41
- }
42
- createDir($path, options = {}, callback) {
43
- let location = this.applyPathPrefix($path);
44
- fs.mkdir(location, { recursive: true }, (error) => {
45
- if (typeof this[kHandler].createDir == 'function') {
46
- this[kHandler].createDir({
47
- path: $path,
48
- options,
49
- callback,
50
- error,
51
- })
52
- } else {
53
- callback(error, true, location)
54
- }
55
- });
56
- }
57
- has($path, options = { constants: 'F_OK' }, callback) {
58
- let location = this.applyPathPrefix($path);
59
- fs.access(location, fs.constants[options.constants], (error) => {
60
- if (typeof this[kHandler].has == 'function') {
61
- this[kHandler].has({
62
- basePath: location,
63
- path: $path,
64
- options,
65
- callback,
66
- error,
67
- })
68
- } else {
69
- callback(error, true)
70
- }
71
- });
72
- }
73
-
74
- write($path, $contents, options = {}, ...rest) {
75
-
76
- let callback = Array.from(rest).find(arg => typeof arg == 'function')
77
- let location = this.applyPathPrefix($path);
78
- fs.stat(location).then(res=>{
79
- if(res.isDirectory()){
80
- return path.join(location,v4())
81
- }
82
- return location
83
- })
84
- .catch(e=>location)
85
- .then(location=>{
86
- this.ensureDirectory(this.dirname(location), () => {
87
- fs.writeFile(location, $contents, (error, data) => {
88
- if (typeof this[kHandler].write == 'function') {
89
- this[kHandler].write({
90
- basePath: location,
91
- path: $path,
92
- options,
93
- callback,
94
- error,
95
- content: $contents
96
- })
97
- } else {
98
- callback(error, {
99
- 'type': 'file',
100
- 'path': location,
101
- 'message': 'File created successfully'
102
- }, location)
103
- }
104
- })
105
- });
106
- })
107
-
108
-
109
-
110
- }
111
-
112
- read($path, options = {}, callback) {
113
- let location = this.applyPathPrefix($path);
114
- fs.readFile(location, {
115
- encoding: (this.getConfig().encoding || (options.encoding || 'utf-8'))
116
- }, (error, data) => {
117
- if (typeof this[kHandler].read == 'function') {
118
- this[kHandler].read({
119
- basePath: location,
120
- path: $path,
121
- options,
122
- callback,
123
- error,
124
- response: data
125
- })
126
- } else {
127
- callback(error, {
128
- 'type': 'file',
129
- 'path': $path,
130
- 'contents': data
131
- }, location)
132
- }
133
- });
134
-
135
- }
136
- global($path, options = {}, callback) {
137
-
138
- }
139
-
140
- }
1
+ const fs = require('fs-extra')
2
+ const CoreAdapter = require('./CoreAdapter')
3
+ const path = require('path')
4
+ const { v4 } = require("uuid");
5
+ const kHandler = Symbol('handler')
6
+
7
+ class Local extends CoreAdapter {
8
+ constructor(root = '', $config) {
9
+ super()
10
+ this.setPathPrefix(root)
11
+ this.setConfig($config)
12
+ this[kHandler] = (typeof $config.handler == 'function' ? new $config.handler : (typeof $config.handler == 'object' ? $config.handler : {}))
13
+ }
14
+ move(path, newpath, callback) {
15
+ if (callback)
16
+ return fs.move(path, newpath, {
17
+ overwrite: true
18
+ }, function(err, res) {
19
+ callback(err, res, newpath)
20
+ })
21
+ else
22
+ return fs.move(path, newpath, {
23
+ overwrite: true
24
+ })
25
+ }
26
+ delete($path, options = {}, callback) {
27
+ let location = this.applyPathPrefix($path);
28
+ fs.remove(location, (error) => {
29
+ if (typeof this[kHandler].delete == 'function') {
30
+ this[kHandler].delete({
31
+ basePath: location,
32
+ path: $path,
33
+ options,
34
+ callback,
35
+ error,
36
+ })
37
+ } else {
38
+ callback(error, true, location)
39
+ }
40
+ });
41
+ }
42
+ createDir($path, options = {}, callback) {
43
+ let location = this.applyPathPrefix($path);
44
+ fs.mkdir(location, { recursive: true }, (error) => {
45
+ if (typeof this[kHandler].createDir == 'function') {
46
+ this[kHandler].createDir({
47
+ path: $path,
48
+ options,
49
+ callback,
50
+ error,
51
+ })
52
+ } else {
53
+ callback(error, true, location)
54
+ }
55
+ });
56
+ }
57
+ has($path, options = { constants: 'F_OK' }, callback) {
58
+ let location = this.applyPathPrefix($path);
59
+ fs.access(location, fs.constants[options.constants], (error) => {
60
+ if (typeof this[kHandler].has == 'function') {
61
+ this[kHandler].has({
62
+ basePath: location,
63
+ path: $path,
64
+ options,
65
+ callback,
66
+ error,
67
+ })
68
+ } else {
69
+ callback(error, true)
70
+ }
71
+ });
72
+ }
73
+
74
+ write($path, $contents, options = {}, ...rest) {
75
+
76
+ let callback = Array.from(rest).find(arg => typeof arg == 'function')
77
+ let location = this.applyPathPrefix($path);
78
+ fs.stat(location).then(res=>{
79
+ if(res.isDirectory()){
80
+ return path.join(location,v4())
81
+ }
82
+ return location
83
+ })
84
+ .catch(e=>location)
85
+ .then(location=>{
86
+ this.ensureDirectory(this.dirname(location), () => {
87
+ fs.writeFile(location, $contents, (error, data) => {
88
+ if (typeof this[kHandler].write == 'function') {
89
+ this[kHandler].write({
90
+ basePath: location,
91
+ path: $path,
92
+ options,
93
+ callback,
94
+ error,
95
+ content: $contents
96
+ })
97
+ } else {
98
+ callback(error, {
99
+ 'type': 'file',
100
+ 'path': location,
101
+ 'message': 'File created successfully'
102
+ }, location)
103
+ }
104
+ })
105
+ });
106
+ })
107
+
108
+
109
+
110
+ }
111
+
112
+ read($path, options = {}, callback) {
113
+ let location = this.applyPathPrefix($path);
114
+ fs.readFile(location, {
115
+ encoding: (this.getConfig().encoding || (options.encoding || 'utf-8'))
116
+ }, (error, data) => {
117
+ if (typeof this[kHandler].read == 'function') {
118
+ this[kHandler].read({
119
+ basePath: location,
120
+ path: $path,
121
+ options,
122
+ callback,
123
+ error,
124
+ response: data
125
+ })
126
+ } else {
127
+ callback(error, {
128
+ 'type': 'file',
129
+ 'path': $path,
130
+ 'contents': data
131
+ }, location)
132
+ }
133
+ });
134
+
135
+ }
136
+ global($path, options = {}, callback) {
137
+
138
+ }
139
+
140
+ }
141
141
  module.exports = Local
package/adapter/s3.js CHANGED
@@ -1,84 +1,84 @@
1
- const CoreAdapter = require('./CoreAdapter')
2
- const kClient = Symbol('client')
3
- const kHandler = Symbol('handler')
4
- const { v4 } = require("uuid");
5
- const path = require('path')
6
-
7
- class Azure extends CoreAdapter {
8
-
9
- constructor($client, $config) {
10
- super()
11
- this[kClient] = $client;
12
- this[kHandler] = typeof $config.handler == 'function' ? new $config.handler : typeof $config.handler == 'object' ? $config.handler : {};
13
- this.setUrlPrefix(this[kClient].url)
14
- }
15
-
16
- read($path, options = {}, callback) {
17
- let basePath = this.applyUrlPrefix();
18
- $path = $path.replaceAll('\\', '/')
19
- if ($path.charAt(0) == "/") {
20
- $path = $path.substr(1)
21
- }
22
- this[kClient].getConnection().getObject({ Key: $path }).promise()
23
- .then(response => ({ error: null, data: response.Body }))
24
- .catch(error => Promise.resolve({ error, data: null }))
25
- .then(({ error, data }) => {
26
- if (typeof this[kHandler].read == 'function') {
27
- this[kHandler].read({
28
- path: $path,
29
- options,
30
- callback,
31
- error,
32
- basePath,
33
- contents: data
34
- })
35
- } else {
36
- callback(error, function() {
37
- return {
38
- 'type': 'file',
39
- 'path': $path,
40
- 'contents': (options.dataType == 'base64') ? data.toString('base64') : data.toString(),
41
- }
42
- }, basePath)
43
- }
44
- });
45
- }
46
- write($path, data, options = {}, callback) {
47
-
48
- $path = $path.replaceAll('\\', '/')
49
- if ($path.charAt(0) == "/") {
50
- $path = $path.substr(1)
51
- }
52
- let basePath = this.applyUrlPrefix();
53
- this[kClient].getConnection().upload({
54
- Key: $path,
55
- Body: data.toString("base64"),
56
- }).promise()
57
-
58
- .then(response => ({ error: null, response }))
59
- .catch(error => Promise.resolve({ error, response: null }))
60
- .then(({ error, response }) => {
61
- if (typeof this[kHandler].read == 'function') {
62
- this[kHandler].write({
63
- path: $path,
64
- options,
65
- callback,
66
- error,
67
- basePath,
68
- response,
69
- contents: data
70
- })
71
- } else {
72
- callback(error, function() {
73
- return {
74
- 'type': 'file',
75
- 'path': $path,
76
- 'message': 'File uploaded successfully'
77
- }
78
- }, basePath)
79
- }
80
- });
81
- }
82
-
83
- }
1
+ const CoreAdapter = require('./CoreAdapter')
2
+ const kClient = Symbol('client')
3
+ const kHandler = Symbol('handler')
4
+ const { v4 } = require("uuid");
5
+ const path = require('path')
6
+
7
+ class Azure extends CoreAdapter {
8
+
9
+ constructor($client, $config) {
10
+ super()
11
+ this[kClient] = $client;
12
+ this[kHandler] = typeof $config.handler == 'function' ? new $config.handler : typeof $config.handler == 'object' ? $config.handler : {};
13
+ this.setUrlPrefix(this[kClient].url)
14
+ }
15
+
16
+ read($path, options = {}, callback) {
17
+ let basePath = this.applyUrlPrefix();
18
+ $path = $path.replaceAll('\\', '/')
19
+ if ($path.charAt(0) == "/") {
20
+ $path = $path.substr(1)
21
+ }
22
+ this[kClient].getConnection().getObject({ Key: $path }).promise()
23
+ .then(response => ({ error: null, data: response.Body }))
24
+ .catch(error => Promise.resolve({ error, data: null }))
25
+ .then(({ error, data }) => {
26
+ if (typeof this[kHandler].read == 'function') {
27
+ this[kHandler].read({
28
+ path: $path,
29
+ options,
30
+ callback,
31
+ error,
32
+ basePath,
33
+ contents: data
34
+ })
35
+ } else {
36
+ callback(error, function() {
37
+ return {
38
+ 'type': 'file',
39
+ 'path': $path,
40
+ 'contents': (options.dataType == 'base64') ? data.toString('base64') : data.toString(),
41
+ }
42
+ }, basePath)
43
+ }
44
+ });
45
+ }
46
+ write($path, data, options = {}, callback) {
47
+
48
+ $path = $path.replaceAll('\\', '/')
49
+ if ($path.charAt(0) == "/") {
50
+ $path = $path.substr(1)
51
+ }
52
+ let basePath = this.applyUrlPrefix();
53
+ this[kClient].getConnection().upload({
54
+ Key: $path,
55
+ Body: data.toString("base64"),
56
+ }).promise()
57
+
58
+ .then(response => ({ error: null, response }))
59
+ .catch(error => Promise.resolve({ error, response: null }))
60
+ .then(({ error, response }) => {
61
+ if (typeof this[kHandler].read == 'function') {
62
+ this[kHandler].write({
63
+ path: $path,
64
+ options,
65
+ callback,
66
+ error,
67
+ basePath,
68
+ response,
69
+ contents: data
70
+ })
71
+ } else {
72
+ callback(error, function() {
73
+ return {
74
+ 'type': 'file',
75
+ 'path': $path,
76
+ 'message': 'File uploaded successfully'
77
+ }
78
+ }, basePath)
79
+ }
80
+ });
81
+ }
82
+
83
+ }
84
84
  module.exports = Azure
@@ -1,49 +1,49 @@
1
- class FilesystemHandler {
2
- exists(error, data, callback, path, options, basePath) {
3
- if (error) {
4
- callback(error)
5
- } else {
6
- callback(data)
7
- }
8
-
9
- }
10
-
11
- get(error, data, callback, path, options, basePath) {
12
- if (error) {
13
- callback(error)
14
- } else {
15
- callback(data)
16
- }
17
- }
18
-
19
- delete(error, data, callback, path, options, basePath) {
20
- if (error) {
21
- callback(error)
22
- } else {
23
- callback(data)
24
- }
25
- }
26
-
27
- put(error, data, callback, path, options, basePath) {
28
- if (error) {
29
- callback(error)
30
- } else {
31
- callback(data)
32
- }
33
- }
34
- global(error, data, callback, path, options) {
35
- if (error) {
36
- callback(error)
37
- } else {
38
- callback(data)
39
- }
40
- }
41
- createDir(error, data, callback) {
42
- if (error) {
43
- callback(error)
44
- } else {
45
- callback(true)
46
- }
47
- }
48
- }
1
+ class FilesystemHandler {
2
+ exists(error, data, callback, path, options, basePath) {
3
+ if (error) {
4
+ callback(error)
5
+ } else {
6
+ callback(data)
7
+ }
8
+
9
+ }
10
+
11
+ get(error, data, callback, path, options, basePath) {
12
+ if (error) {
13
+ callback(error)
14
+ } else {
15
+ callback(data)
16
+ }
17
+ }
18
+
19
+ delete(error, data, callback, path, options, basePath) {
20
+ if (error) {
21
+ callback(error)
22
+ } else {
23
+ callback(data)
24
+ }
25
+ }
26
+
27
+ put(error, data, callback, path, options, basePath) {
28
+ if (error) {
29
+ callback(error)
30
+ } else {
31
+ callback(data)
32
+ }
33
+ }
34
+ global(error, data, callback, path, options) {
35
+ if (error) {
36
+ callback(error)
37
+ } else {
38
+ callback(data)
39
+ }
40
+ }
41
+ createDir(error, data, callback) {
42
+ if (error) {
43
+ callback(error)
44
+ } else {
45
+ callback(true)
46
+ }
47
+ }
48
+ }
49
49
  module.exports = FilesystemHandler
package/clients/azure.js CHANGED
@@ -1,27 +1,27 @@
1
- const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");
2
- const kContainer = Symbol('container')
3
- const kConnection = Symbol('connection')
4
- class AzureClient {
5
- constructor($config) {
6
- this[kConnection] = this.getConnection($config['account'], Buffer.from($config['key'], 'base64').toString('ascii'));
7
- this[kContainer] = this.getContainer($config['container']);
8
- return this
9
- }
10
- getConnection(account, key) {
11
- if (account || key) {
12
- return new BlobServiceClient(
13
- `https://${account}.blob.core.windows.net`,
14
- new StorageSharedKeyCredential(account, key)
15
- );
16
- }
17
- return this[kConnection]
18
- }
19
- getContainer(container) {
20
- if (container) {
21
- return this[kConnection].getContainerClient(container)
22
- }
23
- return this[kContainer]
24
- }
25
-
26
- }
1
+ const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");
2
+ const kContainer = Symbol('container')
3
+ const kConnection = Symbol('connection')
4
+ class AzureClient {
5
+ constructor($config) {
6
+ this[kConnection] = this.getConnection($config['account'], Buffer.from($config['key'], 'base64').toString('ascii'));
7
+ this[kContainer] = this.getContainer($config['container']);
8
+ return this
9
+ }
10
+ getConnection(account, key) {
11
+ if (account || key) {
12
+ return new BlobServiceClient(
13
+ `https://${account}.blob.core.windows.net`,
14
+ new StorageSharedKeyCredential(account, key)
15
+ );
16
+ }
17
+ return this[kConnection]
18
+ }
19
+ getContainer(container) {
20
+ if (container) {
21
+ return this[kConnection].getContainerClient(container)
22
+ }
23
+ return this[kContainer]
24
+ }
25
+
26
+ }
27
27
  module.exports = AzureClient
package/clients/s3.js CHANGED
@@ -1,17 +1,17 @@
1
- const { S3 } = require("aws-sdk");
2
- const kConnection = Symbol('connection')
3
-
4
- class S3Client {
5
- constructor($config) {
6
- this.applyConnection($config)
7
- return this
8
- }
9
- getConnection() {
10
- return this[kConnection]
11
- }
12
- applyConnection($config) {
13
- this[kConnection] = new S3({ accessKeyId: $config['key'], secretAccessKey: $config['secret'], params: { Bucket: $config['bucket'] } })
14
- }
15
-
16
- }
1
+ const { S3 } = require("aws-sdk");
2
+ const kConnection = Symbol('connection')
3
+
4
+ class S3Client {
5
+ constructor($config) {
6
+ this.applyConnection($config)
7
+ return this
8
+ }
9
+ getConnection() {
10
+ return this[kConnection]
11
+ }
12
+ applyConnection($config) {
13
+ this[kConnection] = new S3({ accessKeyId: $config['key'], secretAccessKey: $config['secret'], params: { Bucket: $config['bucket'] } })
14
+ }
15
+
16
+ }
17
17
  module.exports = S3Client
@@ -1,14 +1,14 @@
1
- const DirectoryCreateExceptionContract = require('@ostro/contracts/filesystem/directoryCreateException')
2
- class DirectoryCreateException extends DirectoryCreateExceptionContract {
3
- constructor(errors) {
4
- super();
5
-
6
- this.name = this.constructor.name;
7
- this.message = 'Unable to create directory';
8
- this.errors = errors;
9
- this.statusCode = 404;
10
- Error.captureStackTrace(this, this.constructor);
11
-
12
- }
13
- }
1
+ const DirectoryCreateExceptionContract = require('@ostro/contracts/filesystem/directoryCreateException')
2
+ class DirectoryCreateException extends DirectoryCreateExceptionContract {
3
+ constructor(errors) {
4
+ super();
5
+
6
+ this.name = this.constructor.name;
7
+ this.message = 'Unable to create directory';
8
+ this.errors = errors;
9
+ this.statusCode = 404;
10
+ Error.captureStackTrace(this, this.constructor);
11
+
12
+ }
13
+ }
14
14
  module.exports = DirectoryCreateException