@environment-safe/file 0.0.1

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/package.json ADDED
@@ -0,0 +1,107 @@
1
+ {
2
+ "name": "@environment-safe/file",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "main": "dist/index.mjs",
6
+ "module": "src/index.mjs",
7
+ "commonjs": "dist/index.cjs",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./src/index.mjs",
11
+ "require": "./dist/index.cjs"
12
+ },
13
+ "./package.json": {
14
+ "import": "./package.json",
15
+ "require": "./package.json"
16
+ },
17
+ "./src/index.mjs": {
18
+ "import": "./src/index.mjs",
19
+ "require": "./dist/index.cjs"
20
+ },
21
+ "./dist/index.cjs": {
22
+ "import": "./src/index.mjs",
23
+ "require": "./dist/index.cjs"
24
+ }
25
+ },
26
+ "homepage": "https://github.com/environment-safe/file",
27
+ "description": "File abstraction for server and browser.",
28
+ "keywords": [],
29
+ "author": "Abbey Hawk Sparrow <@khrome> (http://patternweaver.com)",
30
+ "contributors": [],
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git@github.com:environment-safe/file.git"
34
+ },
35
+ "bugs": {
36
+ "url": "https://github.com/environment-safe/file/issues"
37
+ },
38
+ "moka": {
39
+ "firefox": {
40
+ "engine": "@open-automaton/playwright-mining-engine",
41
+ "options": {
42
+ "type": "firefox"
43
+ }
44
+ },
45
+ "chrome": {
46
+ "engine": "@open-automaton/playwright-mining-engine",
47
+ "options": {
48
+ "type": "chromium"
49
+ }
50
+ },
51
+ "safari": {
52
+ "engine": "@open-automaton/playwright-mining-engine",
53
+ "options": {
54
+ "type": "webkit"
55
+ }
56
+ },
57
+ "stub": "node_modules/@open-automaton/moka/src/stub.mjs",
58
+ "stubs": [
59
+ "express",
60
+ "module",
61
+ "path"
62
+ ],
63
+ "shims": {
64
+ "chai": "node_modules/chai/chai.js",
65
+ "browser-or-node": "node_modules/browser-or-node/src/index.js"
66
+ }
67
+ },
68
+ "license": "MIT",
69
+ "devDependencies": {
70
+ "@babel/cli": "^7.22.5",
71
+ "@babel/core": "^7.22.5",
72
+ "@babel/plugin-transform-modules-commonjs": "^7.22.5",
73
+ "@environment-safe/chai": "^0.1.0",
74
+ "@environment-safe/commonjs-builder": "^0.0.3",
75
+ "@environment-safe/jsdoc-builder": "^0.0.2",
76
+ "@open-automaton/moka": "^0.0.4",
77
+ "babel-plugin-search-and-replace": "^1.1.1",
78
+ "babel-plugin-transform-import-meta": "^2.2.0",
79
+ "chai": "^4.3.7",
80
+ "eslint": "^8.43.0",
81
+ "husky": "^8.0.3",
82
+ "mocha": "^10.2.0",
83
+ "wing-kong": "^1.0.0"
84
+ },
85
+ "scripts": {
86
+ "lint": "./node_modules/.bin/eslint src/*.mjs test/*.mjs",
87
+ "test": "npm run import-test; npm run browser-test; npm run require-test",
88
+ "import-test": "./node_modules/.bin/mocha test/test.mjs",
89
+ "require-test": "./node_modules/.bin/mocha test/test.cjs",
90
+ "build-test-importmap": "wing-kong -i .import-config.json -f test/index.html rewrite dependencies",
91
+ "build-commonjs": "./node_modules/@environment-safe/commonjs-builder/bin/build.mjs",
92
+ "local-server": "open 'http://localhost:8085/' ; npx http-server -p 8085",
93
+ "headless-browser-test": "npm run headless-chrome-test; npm run headless-firefox-test; npm run headless-safari-test",
94
+ "headless-chrome-test": "moka --server . --browser chrome --relaxed --prefix ../ test/test.mjs",
95
+ "headless-firefox-test": "moka --server . --browser firefox --relaxed --prefix ../ test/test.mjs",
96
+ "headless-safari-test": "moka --server . --browser safari --relaxed --prefix ../ test/test.mjs",
97
+ "browser-test": "moka --server . --local-browser --relaxed --prefix ../ test/test.mjs",
98
+ "container-test": "docker build . -t environment-safe-package.json -f ./containers/test.dockerfile; docker logs --follow \"$(docker run -d environment-safe-package.json)\"",
99
+ "build-docs": "build-jsdoc docs",
100
+ "build-types": "build-jsdoc types",
101
+ "add-generated-files-to-commit": "git add docs/*.md; git add src/*.d.ts; git add dist/*.cjs",
102
+ "prepare": "husky install"
103
+ },
104
+ "dependencies": {
105
+ "browser-or-node": "^2.1.1"
106
+ }
107
+ }
package/src/buffer.mjs ADDED
@@ -0,0 +1,73 @@
1
+ /* global Buffer:false */
2
+ /*
3
+ import { isBrowser, isJsDom } from 'browser-or-node';
4
+ import * as mod from 'module';
5
+ import * as path from 'path';
6
+ let internalRequire = null;
7
+ if(typeof require !== 'undefined') internalRequire = require;
8
+ const ensureRequire = ()=> (!internalRequire) && (internalRequire = mod.createRequire(import.meta.url));
9
+ //*/
10
+
11
+ /**
12
+ * A JSON object
13
+ * @typedef { object } JSON
14
+ */
15
+
16
+ // TODO: make this live in streams
17
+
18
+ import { isBrowser, isJsDom } from 'browser-or-node';
19
+
20
+ let InternalBuffer = null;
21
+
22
+ if(isBrowser || isJsDom){
23
+ InternalBuffer = function(){};
24
+ var enc = new TextEncoder();
25
+ var dec = new TextDecoder('utf-8');
26
+ InternalBuffer.from = (ob)=>{
27
+ const type = Array.isArray(ob)?'array':(typeof ob);
28
+ switch(type){
29
+ case 'object':
30
+ case 'array':
31
+ case 'string':
32
+ return enc.encode(ob).buffer;
33
+ case '':
34
+ }
35
+ };
36
+ InternalBuffer.to = (type, buffer)=>{
37
+ switch(type){
38
+ case 'object':
39
+ case 'array':
40
+ case 'string':
41
+ return dec.decode(buffer);
42
+ case '':
43
+ }
44
+ };
45
+ InternalBuffer.alloc = (size, fill, encoding='utf-8')=>{
46
+ const result = new Uint8Array(size);
47
+ if(fill){
48
+ for(let lcv=0; lcv< size; lcv++){
49
+ result[lcv] = fill;
50
+ }
51
+ }
52
+ //todo: convert encoding to byte offset
53
+ };
54
+ }else{
55
+ InternalBuffer = function(){};
56
+ InternalBuffer.from = (ob='')=>{
57
+ return Buffer.from(ob);
58
+ };
59
+ //InternalBuffer = Buffer;
60
+ InternalBuffer.to = (type, buffer)=>{
61
+ switch(type){
62
+ case 'object':
63
+ return buffer.toJSON();
64
+ case 'array':
65
+ return buffer.toJSON();
66
+ case 'string':
67
+ return buffer.toString();
68
+ case '':
69
+ }
70
+ };
71
+ }
72
+
73
+ export const FileBuffer = InternalBuffer;