Skip to content

Commit

Permalink
Fix: eval('require'), fs import, process import optimized (#22)
Browse files Browse the repository at this point in the history
* Update fs.ets and fs-polyfill.js

* fix: Update eval require, fs import and process import
  • Loading branch information
Joker-john authored Mar 2, 2022
1 parent 27e22f0 commit 5f86754
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 53 deletions.
28 changes: 1 addition & 27 deletions examples/sdk7-demo/entry/src/main/ets/default/pages/fs.ets
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fileio from '@ohos.fileio';
import ability_featureAbility from '@ohos.ability.featureAbility'
var fs = global.fs;

@Entry
@Component
Expand All @@ -19,10 +20,6 @@ struct FSPage {
this.createFiles();
}

private getFs() {
return Function("return " + "quire".replace(/^/, "re") + "('fs')")();
}

async getAppDir() {
return new Promise((resolve, reject) => {
if (this.path) {
Expand All @@ -45,7 +42,6 @@ struct FSPage {
}

async createFiles() {
let fs = this.getFs();
await this.getAppDir()

const file1 = this.appDir + '/test1.text';
Expand Down Expand Up @@ -79,7 +75,6 @@ struct FSPage {
}`;

this.protoFilePath = this.appDir + this.protoFilePath;
let fs = this.getFs();
if (!fs.existsSync(this.protoFilePath)) {
let fd = fileio.openSync(this.protoFilePath, 0o102, 0o666);
fileio.writeSync(fd, data);
Expand All @@ -99,7 +94,6 @@ struct FSPage {
})
Row() {
Button('Write').onClick(async () => {
let fs = this.getFs();
let fd = fileio.openSync(this.path, 65, 0o666);
fs.write(fd, this.text, (err, written, string) => {
if (err) {
Expand All @@ -113,7 +107,6 @@ struct FSPage {
})
Button('Overwrite').onClick(() => {
try {
let fs = this.getFs();
fs.writeFileSync(this.path, this.text);
console.info('File directory :' + this.path);
this.status = "Overwrite Success!";
Expand All @@ -123,7 +116,6 @@ struct FSPage {
}
})
Button('OverwriteAsync').onClick(async () => {
let fs = this.getFs();
fs.writeFile(this.path, this.text, (err) => {
if (!err) {
this.status = "Overwrite Success!";
Expand All @@ -135,7 +127,6 @@ struct FSPage {
}

Button('readdir').onClick(() => {
let fs = this.getFs();
fs.readdir(this.appDir,(err,res)=>{
if(!err){
this.readText = `${JSON.stringify(res)}`;
Expand All @@ -149,15 +140,13 @@ struct FSPage {
Row() {
Button('appendSync').onClick(() => {
try {
let fs = this.getFs();
fs.appendFileSync(this.path, this.text)
this.status = "appendSync Success!";
} catch (e) {
this.status = "appendSync Fail!";
}
})
Button('appendAsync').onClick(() => {
let fs = this.getFs();
fs.appendFile(this.path, this.text, (err) => {
if (!err) {
this.status = "appendAsync Success!";
Expand All @@ -168,7 +157,6 @@ struct FSPage {
})
Button('Add to end').onClick(async () => {
try {
let fs = this.getFs();
fs.writeFileSync(this.path, this.text, { flag: 'a' });
console.info('File directory :' + this.path);
this.status = "Add success!";
Expand All @@ -182,7 +170,6 @@ struct FSPage {
Row() {
Button('Get File Stat(sync)').onClick(() => {
try {
let fs = this.getFs();
let status = fs.statSync(this.path);
this.status = "Stat Success(sync)!";
this.readText = "";
Expand All @@ -196,7 +183,6 @@ struct FSPage {
}
})
Button('Get File Stat(async)').onClick(() => {
let fs = this.getFs();
fs.stat(this.path)
.then((status) => {
this.status = "Stat Success(async)!";
Expand All @@ -219,7 +205,6 @@ struct FSPage {
Row(){
Button('Create').onClick(() => {
try {
let fs = this.getFs();
this.stream = fs.createWriteStream(this.path);
this.status = "Create Success!";
this.writeStreamState = this.stream.path;
Expand All @@ -244,7 +229,6 @@ struct FSPage {
}
})
Button('Write').onClick(() => {
let fs = this.getFs();
this.stream.write(this.text,(err)=>{
if(err){
this.status = "Stream Write Fail!";
Expand Down Expand Up @@ -275,7 +259,6 @@ struct FSPage {
.border({ width: 1, color: "red" }).width('100%')
Row(){
Button('Create').onClick(() => {
let fs = this.getFs();
const readPath = this.appDir + '/testfs2.text';
try {
let fd = fileio.openSync(readPath, 0o102, 0o666);
Expand All @@ -285,7 +268,6 @@ struct FSPage {
throw('Read File create fail!')
}
try{
let fs = this.getFs();
this.rstream = fs.createReadStream(readPath);
this.status = "Create Readstream Success!";
this.readStreamState = this.rstream.path;
Expand All @@ -307,7 +289,6 @@ struct FSPage {
}
})
Button('Pipe').onClick(() => {
let fs = this.getFs();
try {
this.rstream.pipe(this.stream, { end: false });
this.status = "Pipe Stream Success!";
Expand All @@ -317,7 +298,6 @@ struct FSPage {
}
})
Button('Read').onClick(()=>{
let fs = this.getFs();
let chunks = '';
const fileToBuffer = (readStream, cb) => {
readStream.on('error', err => {
Expand Down Expand Up @@ -358,7 +338,6 @@ struct FSPage {
Row(){
Button('Delete file').onClick(() => {
try {
let fs = this.getFs();
fs.unlinkSync(this.path);
this.status = "Delete Success!";
} catch (err) {
Expand All @@ -377,7 +356,6 @@ struct FSPage {
Row(){
Button('Read File 1').onClick(async () => {
try {
let fs = this.getFs();
this.readText = fs.readFileSync(this.path, { encoding: 'utf8', flag: 'r' })
//this.readText = fileio.readTextSync(this.path);
this.status = "Read file Success!";
Expand All @@ -388,7 +366,6 @@ struct FSPage {
}).alignSelf(ItemAlign.Center)
Button('Read File 2').onClick(async () => {
try {
let fs = this.getFs();
this.readText = fs.readFileSync(this.appDir+'/testfs2.text', { encoding: 'utf8', flag: 'r' })
//this.readText = fileio.readTextSync(this.appDir+'/testfs2.text');
this.status = "Read file Success!";
Expand All @@ -401,7 +378,6 @@ struct FSPage {

Button('Read Dir').onClick(async () => {
try {
let fs = this.getFs();
const result1 = fs.readdirSync(this.appDir, { withFileTypes: true });
console.log(JSON.stringify(result1));
this.readText = `${JSON.stringify(result1)}`;
Expand All @@ -414,7 +390,6 @@ struct FSPage {
}).alignSelf(ItemAlign.Center)

Button('create folder sync').onClick(() => {
const fs = this.getFs();
const path = `${this.appDir}/dir1/dir2/dir3/dir4`;
try {
const returnValue = fs.mkdirSync(path, { recursive: true });
Expand All @@ -425,7 +400,6 @@ struct FSPage {
}).alignSelf(ItemAlign.Center)

Button('create folder async').onClick(() => {
const fs = this.getFs();
const path = `${this.appDir}/dir11/dir22/dir33/dir44`;
fs.mkdir(path, { recursive: true }, (err, returnValue) => {
if (!err) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import util from '@ohos.util';
//import util from '@ohos.util';
@Entry
@Component
struct TextPage {
Expand Down
1 change: 0 additions & 1 deletion packages/polyfill/node/fs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fileio from '@ohos.fileio';
import { Writable, Readable } from 'stream-browserify';
//globalThis.process = require('process');

const FILE_TYPE = { File: 1, Dirent: 2 };
//type BufferEncoding = 'ascii' | 'utf8' | 'utf-8' | 'utf16le' | 'ucs2' | 'ucs-2' | 'base64' | 'base64url' | 'latin1' | 'binary' | 'hex';
Expand Down
48 changes: 24 additions & 24 deletions packages/polyfill/node/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import * as harmonyFS from './fs';
import * as process from 'process/browser'

if (!globalThis.fs) {
globalThis.harmonyFS = harmonyFS;

if (!globalThis.process) {
globalThis.process = require('process');
}
globalThis.fs = harmonyFS;
}

if (!globalThis.process.versions) {
globalThis.process.versions = {};
}
if (!globalThis.process) {
globalThis.process = process;
}

if (!globalThis.process.versions.node) {
globalThis.process.versions.node = '0.0.0';
}
if (!globalThis.process.versions) {
globalThis.process.versions = {};
}

globalThis['eval'] = function (functionBody) {
if ('require' == functionBody) {
return globalThis['require'];
}
if (!globalThis.process.versions.node) {
globalThis.process.versions.node = '0.0.0';
}

return eval(functionBody);
};
globalThis['eval'] = function (functionBody) {
if (functionBody == 'require') {
return function protoRequire(moduleName){
if (moduleName == 'fs') {
return harmonyFS;
}else{
return __webpack_require__(moduleName)
}
};
}
return eval(functionBody);
};

globalThis['require'] = function (moduleName) {
if (moduleName == 'fs') {
return harmonyFS;
}
return require(moduleName);
};
}

0 comments on commit 5f86754

Please sign in to comment.