Skip to content

Commit

Permalink
add more builtin function & model
Browse files Browse the repository at this point in the history
  • Loading branch information
peze authored and JacksonTian committed Mar 6, 2024
1 parent 3ade1c6 commit 2c599bf
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 19 deletions.
20 changes: 10 additions & 10 deletions builtin/crypto.dara
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,40 @@
* HmacSHA1 Signature
* @param stringToSign string
* @param secret string
* @return signed bytes
* @return signed string
*/
static function HmacSHA1Sign(stringToSign: string, secret: string): bytes;
static function HmacSHA1Sign(stringToSign: string, secret: string): string;


/**
* HmacSHA256 Signature
* @param stringToSign string
* @param secret string
* @return signed bytes
* @return signed string
*/
static function HmacSHA256Sign(stringToSign: string, secret: string): bytes;
static function HmacSHA256Sign(stringToSign: string, secret: string): string;


/**
* HmacSM3 Signature
* @param stringToSign string
* @param secret string
* @return signed bytes
* @return signed string
*/
static function HmacSM3Sign(stringToSign: string, secret: string): bytes;
static function HmacSM3Sign(stringToSign: string, secret: string): string;


/**
* SHA256withRSA Signature
* @param stringToSign string
* @param secret string
* @return signed bytes
* @return signed string
*/
static function SHA256withRSASign(stringToSign: string, secret: string): bytes;
static function SHA256withRSASign(stringToSign: string, secret: string): string;

/**
* MD5 Signature
* @param stringToSign string
* @return signed bytes
* @return signed string
*/
static function MD5Sign(stringToSign: string): bytes;
static function MD5Sign(stringToSign: string): string;
10 changes: 5 additions & 5 deletions builtin/file.dara
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ init(path: string) {}

function path(): string;

function length(): integer;
async function length(): integer;

function createTime(): $Date;
async function createTime(): $Date;

function modifyTime(): $Date;
async function modifyTime(): $Date;

function read(size: number): bytes;
async function read(size: number): bytes;

function write(data: bytes): void;
async function write(data: bytes): void;

static function createReadStream(path: string): readable;

Expand Down
4 changes: 3 additions & 1 deletion builtin/map.dara
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ function keySet(): [ string ];

function entries(): [ entry[ $type ] ];

function toJSON(): string;
function toJSON(): string;

function merge(data: map[string]any): map[string]any;
4 changes: 3 additions & 1 deletion builtin/stream.dara
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ static async function readAsJSON(stream: readable): any ;
* @param stream the readable stream
* @return the string result
*/
static async function readAsString(stream: readable): string;
static async function readAsString(stream: readable): string;

static async function readAsSSE(stream: readable): asyncIterator[$SSEEvent];
4 changes: 4 additions & 0 deletions builtin/url.dara
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ function protocol(): string;

function hostname(): string;

function host(): string;

function port(): string;

function hash(): string;

function search(): string;
Expand Down
16 changes: 16 additions & 0 deletions lib/builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,14 @@ function _mapfield(name, keyType, valueType, required = false) {
const builtin = new Map();
// built-in types, starts with $
builtin.set('$Model', _model('$Model', []));

builtin.set('$Response', _model('$Response', [
_field('statusCode', 'number', true),
_field('statusMessage', 'string', true),
_mapfield('headers', 'string', 'string', true),
_field('body', 'readable')
]));

builtin.set('$Request', _model('$Request', [
_field('protocol', 'string'),
_field('port', 'number'),
Expand All @@ -124,13 +126,27 @@ builtin.set('$Request', _model('$Request', [
_mapfield('headers', 'string', 'string'),
_field('body', 'readable')
]));

builtin.set('$SSEEvent', _model('$SSEEvent', [
_field('id', 'string'),
_field('event', 'string'),
_field('data', 'string'),
_field('retry', 'integer')
]));

builtin.set('$Error', _model('$Error', [
_field('name', 'string'),
_field('message', 'string'),
_field('code', 'string'),
_field('stack', 'string')
]));

builtin.set('$FileField', _model('$FileField', [
_field('filename', 'string'),
_field('contentType', 'string'),
_field('content', 'readable'),
]));

builtin.set('$URL', _module('url'));

builtin.set('$File', _module('file'));
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/builtin_module/map.dara
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ static async function main(args: [string]): void {
}

var json = mapTest.toJSON();
var mapTest2 = {
key1 = 'value4',
key4 = 'value5',
};
var mapTest3 = mapTest.merge(mapTest2);
if(mapTest3['key1'] == 'value4') {
return;
}
}
8 changes: 8 additions & 0 deletions test/fixtures/builtin_module/stream.dara
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,13 @@ static async function main(args: [string]): void {
data = $Stream.readAsBytes(rs);
var obj = $Stream.readAsJSON(rs);
var jsonStr = $Stream.readAsString(rs);

var it = $Stream.readAsSSE(rs);
for(var data : it ) {
$Logger.log(data.id);
$Logger.log(data.event);
$Logger.log($JSON.stringify(data.data));
var num: integer = data.retry + 3;
}
}
}
4 changes: 3 additions & 1 deletion test/fixtures/builtin_module/url.dara
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ static async function main(args: [string]): void {
var search = url.search();
var href = url.href();
var auth = url.auth();
var host = url.host();
var port = url.port();
var url2 = $URL.parse(args[1]);
path = url2.path();
var newUrl = $URL.urlEncode(args[2]);
var newSearch = $URL.percentEncode(search);
var newPath = $URL.pathEncode(pathname);
var all = 'test' + path + protocol + hostname + hash + search + href + auth + newUrl + newSearch + newPath;
var all = 'test' + path + protocol + hostname + hash + search + href + auth + newUrl + newSearch + newPath + host + port;
}
1 change: 0 additions & 1 deletion test/fixtures/multi_module/model/user.dara
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ static async function test(): iterator[string]{
for(var test : it) {
yield test;
}

}

0 comments on commit 2c599bf

Please sign in to comment.